@font-face {
  font-family: "Inter";
  src: url("fonts/InterVariable.woff2") format("woff2");
  font-weight: 100 900;
  font-style: normal;
  font-display: swap;
}

:root {
  --bg: #ffffff;
  --bg-page: #f7f6f3;
  --fg: #37352f;
  --muted: #787774;
  --muted-soft: #9b9a97;
  --border: #e9e9e7;
  --hover: rgba(55, 53, 47, 0.06);
  --accent: #2383e2;
  --accent-fg: #ffffff;
  --radius: 6px;
  --radius-lg: 8px;
  --shadow-card: 0 1px 2px rgba(15, 15, 15, 0.05), 0 1px 3px rgba(15, 15, 15, 0.04);
}

* { box-sizing: border-box; margin: 0; padding: 0; }
[hidden] { display: none !important; }

html, body {
  height: 100%;
  background: var(--bg-page);
  color: var(--fg);
  font-family: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  font-size: 16px;
  line-height: 1.6;
}

a { color: var(--accent); text-decoration: none; }
.muted { color: var(--muted); font-size: 0.85em; }

a:focus-visible,
button:focus-visible,
[tabindex]:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  border-radius: 3px;
}

/* --- List screens --- */
.container { max-width: 720px; margin: 0 auto; padding: 2.5rem 1.5rem; }
.container h1 {
  margin-bottom: 1.5rem;
  font-size: 1.875rem;
  font-weight: 700;
  letter-spacing: -0.02em;
  color: var(--fg);
}
.list { list-style: none; }
.list li { margin: 0.125rem 0; }
.list li a {
  display: flex; align-items: center;
  padding: 0.75rem 0.6rem;
  border-radius: var(--radius);
  color: var(--fg);
  transition: background 0.15s ease;
}
.list li a:hover { background: var(--hover); }
.list li a:active { background: rgba(55, 53, 47, 0.1); }
.list li a .muted { margin-left: auto; }
.list li a::after {
  content: "\203A";
  margin-left: 0.75rem;
  color: var(--muted-soft);
  font-size: 1.1rem;
  line-height: 1;
}
.back {
  display: inline-block; margin-bottom: 0.5rem;
  color: var(--muted);
  transition: color 0.15s ease;
}
.back:hover { color: var(--fg); }

/* --- Session --- */
.session {
  position: fixed; top: 0; left: 0; right: 0;
  height: 100vh;            /* fallback for old browsers */
  height: 100dvh;           /* visible viewport (excludes mobile browser UI) */
  background: var(--bg-page);
  touch-action: pan-y;
  overflow: hidden;
}
.card {
  position: absolute; inset: 0;
  overflow: hidden;
  cursor: pointer;
}
.card-image {
  position: absolute; inset: 0;
  width: 100%; height: 100%;
  object-fit: contain; display: block;
}

/* Review mode: image fills the viewport (cover) and is draggable to
   reveal the cropped parts. Navigation is via the rating buttons, so
   gestures on the card are free for panning. */
#review .card { cursor: grab; touch-action: none; }
#review .card:active { cursor: grabbing; }
#review .card-image {
  inset: auto; top: 0; left: 0; right: auto; bottom: auto;
  width: auto; height: auto; max-width: none; max-height: none;
  object-fit: fill;
  transform-origin: 0 0;
  will-change: transform;
}
#review .fiche { touch-action: pan-y; }

.session-bar {
  position: absolute; top: 0; left: 0; right: 0; z-index: 4;
  display: flex; align-items: center; gap: 0.75rem;
  padding: max(0.85rem, env(safe-area-inset-top)) 1.1rem 0.85rem;
  font-size: 0.95rem;
  color: var(--fg);
  background: rgba(255, 255, 255, 0.72);
  border-bottom: 1px solid var(--border);
  -webkit-backdrop-filter: blur(20px) saturate(180%);
  backdrop-filter: blur(20px) saturate(180%);
}
.session-bar .back { margin: 0; color: var(--fg); font-size: 1.3rem; }
.session-bar .back:hover { color: var(--accent); }
.session-bar .deck-name { flex: 1; color: var(--muted); }
.session-bar .counter { color: var(--muted); font-variant-numeric: tabular-nums; }

.reveal-hint {
  position: absolute; left: 0; right: 0; bottom: 2.25rem; z-index: 3;
  display: flex; justify-content: center; pointer-events: none;
}
.reveal-hint span {
  padding: 0.55rem 1.15rem; border-radius: 999px;
  background: rgba(255, 255, 255, 0.85);
  border: 1px solid var(--border);
  color: var(--fg); font-size: 0.9rem; font-weight: 500;
  -webkit-backdrop-filter: blur(8px);
  backdrop-filter: blur(8px);
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.12);
}

.fiche {
  position: absolute; left: 0; right: 0; bottom: 0; z-index: 5;
  max-height: 72%; overflow-y: auto;
  display: flex; flex-direction: column; gap: 0.6rem;
  padding: 0.75rem 1.5rem calc(1.75rem + env(safe-area-inset-bottom));
  background: rgba(255, 255, 255, 0.80);
  border-top: 1px solid var(--border);
  border-radius: 18px 18px 0 0;
  -webkit-backdrop-filter: blur(24px) saturate(180%);
  backdrop-filter: blur(24px) saturate(180%);
  box-shadow: 0 -6px 24px rgba(0, 0, 0, 0.14);
  transform: translateY(100%);
  transition: transform 0.34s cubic-bezier(0.22, 1, 0.36, 1);
  cursor: default;
}
.fiche.is-open { transform: translateY(0); }
.fiche .term { font-size: 1.5rem; font-weight: 600; color: var(--fg); }
.info { display: grid; grid-template-columns: auto 1fr; gap: 0.35rem 0.9rem; }
.info dt { color: var(--muted); }
.info dd { word-break: break-word; color: var(--fg); }
.info dd a { text-decoration: underline; }

/* Done / empty */
.done-screen, .empty-screen {
  position: fixed; inset: 0;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 1.5rem; text-align: center; padding: 2rem;
  background: var(--bg-page);
  color: var(--fg);
}
.done-screen h2 { font-size: 1.5rem; font-weight: 700; letter-spacing: -0.02em; }
.done-actions { display: flex; flex-direction: column; gap: 0.75rem; width: 100%; max-width: 280px; }
.btn, #restart-btn {
  display: block; padding: 0.85rem 1.25rem; border-radius: var(--radius); border: 0;
  background: var(--accent); color: var(--accent-fg); font-size: 1rem; font-weight: 500;
  font-family: inherit; cursor: pointer;
  transition: filter 0.15s ease, background 0.15s ease;
}
#restart-btn:hover { filter: brightness(0.94); }
.btn {
  background: var(--bg); color: var(--fg);
  border: 1px solid var(--border);
}
.btn:hover { background: var(--hover); }

/* Review rating bar */
.rate-bar {
  display: flex; gap: 0.5rem;
  margin-top: 0.5rem;
}
.rate {
  flex: 1 1 0; min-width: 0;
  display: flex; align-items: center; justify-content: center;
  padding: 0.85rem 0.4rem;
  border-radius: 14px;
  border: 1px solid transparent;
  background: var(--hover);
  color: var(--fg);
  font-family: inherit; font-size: 0.9rem; font-weight: 600;
  line-height: 1.2; text-align: center;
  cursor: pointer;
  transition: filter 0.15s ease, transform 0.1s ease;
}
.rate:hover { filter: brightness(0.97); }
.rate:active { transform: scale(0.96); }
.rate[data-rating="1"] { background: #fdeceb; border-color: #f3cfcc; color: #b42318; }
.rate[data-rating="2"] { background: #fdf2e3; border-color: #f3ddbc; color: #9a5b00; }
.rate[data-rating="3"] { background: #e9f6ec; border-color: #cce8d3; color: #1f7a34; }
.rate[data-rating="4"] { background: #e8f1fb; border-color: #cfe1f4; color: #1a66ad; }

/* Clickable Latin words inside the term */
.latin-word {
  cursor: pointer;
  padding: 0 0.05em;
  border-radius: 3px;
  text-decoration: underline dotted var(--accent);
  text-underline-offset: 0.18em;
}
.latin-word:hover,
.latin-word:active { background: var(--hover); }

/* Word definition: a second fiche stacked BEHIND the first one. It slides
   up from behind and its top peeks out above the main fiche, which stays
   in front (higher z-index). */
.word-fiche {
  position: absolute; left: 0; right: 0; bottom: 0; z-index: 4;
  max-height: 60vh; overflow-y: auto;
  display: flex; flex-direction: column; gap: 0.5rem;
  padding: 0.75rem 1.5rem 1.5rem;
  background: rgba(255, 255, 255, 0.80);
  border-top: 1px solid var(--border);
  border-radius: 18px 18px 0 0;
  -webkit-backdrop-filter: blur(24px) saturate(180%);
  backdrop-filter: blur(24px) saturate(180%);
  box-shadow: 0 -8px 28px rgba(0, 0, 0, 0.18);
  transform: translateY(100%);
  /* Closed: faded out AND fully hidden, so the translucent panel never
     whitens the main fiche behind it. Opacity animates so the image
     comes back smoothly instead of snapping; visibility only flips
     after the fade/slide finishes. */
  opacity: 0;
  visibility: hidden;
  transition: transform 0.34s cubic-bezier(0.22, 1, 0.36, 1),
              opacity 0.34s ease,
              visibility 0s linear 0.34s;
}
.word-fiche.is-open {
  transform: translateY(0);
  opacity: 1;
  visibility: visible;
  transition: transform 0.34s cubic-bezier(0.22, 1, 0.36, 1),
              opacity 0.34s ease,
              visibility 0s linear 0s;
}
.word-pop-term {
  font-size: 1.3rem; font-weight: 600; color: var(--fg);
}
.word-pop-def { color: var(--muted); }

/* Clickable family name inside the info list. */
.family-link {
  cursor: pointer;
  color: var(--accent);
  text-decoration: underline;
  text-underline-offset: 0.15em;
}
.family-link:hover,
.family-link:active { filter: brightness(0.9); }

/* Fullscreen family overlay. [hidden] (display:none) keeps it out while
   closed; the JS drops the attribute, forces a reflow, then adds .is-open
   so the fade/scale-in transition runs. */
.family-fiche {
  position: fixed; inset: 0; z-index: 20;
  overflow: hidden;
  background: rgba(255, 255, 255, 0.96);
  -webkit-backdrop-filter: blur(24px) saturate(180%);
  backdrop-filter: blur(24px) saturate(180%);
  opacity: 0;
  transform: scale(0.98);
  transition: opacity 0.3s ease, transform 0.3s cubic-bezier(0.22, 1, 0.36, 1);
}
.family-fiche.is-open { opacity: 1; transform: scale(1); }

.family-back {
  position: absolute;
  top: max(0.85rem, env(safe-area-inset-top));
  left: calc(1.1rem + env(safe-area-inset-left));
  z-index: 2;
  padding: 0;
  border: 0; background: none;
  color: var(--fg); font-size: 1.3rem; line-height: 1;
  font-family: inherit; cursor: pointer;
  transition: color 0.15s ease;
}
.family-back:hover { color: var(--accent); }

/* Scrollable content area (family page or species fiche). */
.family-stage {
  position: absolute; inset: 0;
  overflow-y: auto;
}

.family-view {
  min-height: 100%;
  padding: max(0.85rem, env(safe-area-inset-top)) 1.25rem
           calc(2rem + env(safe-area-inset-bottom));
}
.family-head {
  margin-bottom: 1.5rem;
  /* left padding clears the floating back arrow so the family name sits
     on the same line as it */
  padding: 0 0 1rem 2.4rem;
  border-bottom: 1px solid var(--border);
}
.family-name {
  font-size: 1.4rem; font-weight: 700; line-height: 1.3;
  letter-spacing: -0.02em; color: var(--fg);
}
.family-def {
  margin-top: 0.4rem;
  color: var(--muted); font-size: 0.95rem; line-height: 1.5;
}

.species-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 0.9rem;
}
.species-card {
  display: flex; flex-direction: column;
  padding: 0; overflow: hidden;
  border: 1px solid var(--border); border-radius: var(--radius-lg);
  background: var(--bg); color: var(--fg);
  font-family: inherit; text-align: left; cursor: pointer;
  box-shadow: var(--shadow-card);
  transition: filter 0.15s ease, transform 0.1s ease;
}
.species-card:hover { filter: brightness(0.97); }
.species-card:active { transform: scale(0.98); }
.species-thumb {
  width: 100%; aspect-ratio: 4 / 3;
  object-fit: cover; display: block;
  background: var(--hover);
}
.species-name {
  padding: 0.55rem 0.7rem;
  font-size: 0.9rem; line-height: 1.3;
  word-break: break-word;
}

.family-msg {
  display: flex; align-items: center; justify-content: center;
  min-height: 50vh;
  padding: 2rem; text-align: center; color: var(--muted);
}

/* Plant fiche shown inside the overlay (one history step deeper). */
.plant-fiche {
  min-height: 100%;
  display: flex; flex-direction: column; gap: 0.9rem;
  padding: calc(3.75rem + env(safe-area-inset-top)) 1.5rem
           calc(2rem + env(safe-area-inset-bottom));
}
.plant-img {
  width: 100%; max-height: 45vh; object-fit: contain;
  border-radius: var(--radius-lg);
}
.plant-term { font-size: 1.5rem; font-weight: 600; color: var(--fg); }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    transition: none !important;
    animation: none !important;
  }
}
