/* ---------------------------------------------------------------------- */
/* Tokens                                                                   */
/* ---------------------------------------------------------------------- */

:root {
  --paper: #f4ead9;
  --paper-deep: #ecdec4;
  --card: #fffbf3;
  --card-edge: #f1e6d2;
  --ink: #3c2f24;
  --ink-soft: #7a6a58;
  --ink-faint: #a5947f;
  --accent: #5f7350;
  --accent-deep: #46593a;
  --accent-warm: #b1552f;
  --gold: #c99a3f;
  --line: #e3d3b6;
  --shadow-1: 30 22% 15%;

  --font-display: "Fraunces", "Iowan Old Style", "Palatino Linotype", Georgia, serif;
  --font-body: "Lora", Georgia, "Times New Roman", serif;
  --font-ui: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;

  --radius-card: 10px;
  --nav-h: 64px;
  --top-h: 56px;
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
  overscroll-behavior: none;
}

@supports (height: 100dvh) {
  html, body { height: 100dvh; }
}

body {
  background:
    radial-gradient(ellipse 120% 90% at 50% 0%, var(--paper) 0%, var(--paper-deep) 100%);
  color: var(--ink);
  font-family: var(--font-body);
  -webkit-font-smoothing: antialiased;
  min-height: 100%;
}

body::before {
  content: "";
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 0;
  opacity: 0.5;
  background-image:
    repeating-linear-gradient(115deg, rgba(120, 96, 60, 0.025) 0px, rgba(120, 96, 60, 0.025) 1px, transparent 1px, transparent 3px);
}

button {
  font-family: inherit;
  cursor: pointer;
  background: none;
  border: none;
  color: inherit;
}

h1, h2, h3 { font-family: var(--font-display); margin: 0; }

.app {
  position: relative;
  z-index: 1;
  height: 100%;
  display: flex;
  flex-direction: column;
}

/* ---------------------------------------------------------------------- */
/* Top bar                                                                  */
/* ---------------------------------------------------------------------- */

.topbar {
  height: var(--top-h);
  flex: 0 0 auto;
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  padding: 0 14px;
}

/* Grid (not flex space-between) so the title stays visually centered even
   though the two side buttons are different widths (a circular icon vs a
   text pill). */
.topbar > button:first-child { justify-self: start; }
.topbar > button:last-child { justify-self: end; }

.topbar-title {
  justify-self: center;
  display: flex;
  align-items: baseline;
  gap: 8px;
  font-family: var(--font-display);
  color: var(--ink);
}

.topbar-monogram {
  font-weight: 600;
  font-size: 1.05rem;
  letter-spacing: 0.04em;
  color: var(--accent-deep);
}

.topbar-label {
  font-style: italic;
  font-size: 0.95rem;
  color: var(--ink-soft);
}

.iconbtn {
  width: 38px;
  height: 38px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  color: var(--ink-soft);
  transition: background 0.15s ease, color 0.15s ease;
}

.iconbtn:hover { background: rgba(95, 115, 80, 0.12); color: var(--accent-deep); }
.iconbtn:active { transform: scale(0.94); }

.textbtn {
  height: 38px;
  padding: 0 16px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 19px;
  color: var(--ink-soft);
  font-family: var(--font-ui);
  font-size: 0.85rem;
  letter-spacing: 0.02em;
  transition: background 0.15s ease, color 0.15s ease;
}

.textbtn:hover { background: rgba(95, 115, 80, 0.12); color: var(--accent-deep); }
.textbtn:active { transform: scale(0.96); }

/* ---------------------------------------------------------------------- */
/* Stage / book                                                             */
/* ---------------------------------------------------------------------- */

.stage {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 22px;
  padding: 18px 16px 28px;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.book-column {
  width: 100%;
  max-width: 480px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  flex: 0 0 auto;
}

.book {
  position: relative;
  width: 100%;
  aspect-ratio: 3 / 4;
  perspective: 1700px;
}

/*
 * Two stacked leaves, not one. The back leaf holds the destination page and
 * never moves — it's what makes the turn look like a page lifting off a
 * stack instead of a lone card spinning in empty space. Only the front leaf
 * (the page currently on top) rotates away on its spine edge to reveal it.
 * Once the turn finishes, the front leaf is snapped (no transition) to match
 * the back leaf's content and rest transform, ready to be "on top" again.
 */
.leaf {
  position: absolute;
  inset: 0;
  transform-style: preserve-3d;
}

.leaf--back {
  z-index: 1;
}

.leaf--front {
  z-index: 2;
  will-change: transform, opacity;
  /* transform-origin is set per-flip by app.js: the leaf pivots on its
     spine edge (left for "next", right for "prev") so it turns over like a
     page instead of spinning in place around its center. */
  /* Past 90°, an element with no real back face would otherwise render a
     mirrored copy of its front — hiding it there keeps the last stretch of
     the turn (already faded, past turn-out's 96°) genuinely blank instead
     of flashing a reversed image right before the reset snap. */
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
}

.leaf--front.turning {
  /* The page stays fully opaque for most of the turn and only fades in the
     last stretch, once it's nearly edge-on — opacity here is just covering
     for the lack of a real back-face texture, not something a real page
     does as it turns. It fades all the way to 0 (not just faint) because
     the moment the turn finishes, this leaf is snapped back to its resting
     transform/opacity with no transition of its own — if it's still
     partly visible at that instant, that snap reads as a jarring pop. At
     opacity 0 there's nothing to see, so the snap is invisible. */
  transition: transform var(--flip-dur, 0.52s) cubic-bezier(0.61, 0.06, 0.4, 0.96),
              opacity calc(var(--flip-dur, 0.52s) * 0.3) ease-out calc(var(--flip-dur, 0.52s) * 0.7);
}

.leaf--front.turn-out-next { transform: rotateY(-96deg); opacity: 0; }
.leaf--front.turn-out-prev { transform: rotateY(96deg); opacity: 0; }

.leaf-face {
  position: absolute;
  inset: 0;
  border-radius: var(--radius-card);
  background: var(--card);
  box-shadow:
    0 1px 2px hsl(var(--shadow-1) / 0.12),
    0 18px 40px -18px hsl(var(--shadow-1) / 0.45),
    0 6px 16px -6px hsl(var(--shadow-1) / 0.28);
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 10px;
  border: 1px solid var(--card-edge);
}

.leaf-face::after {
  content: "";
  position: absolute;
  inset: 0;
  pointer-events: none;
  opacity: 0;
  transition: opacity var(--flip-dur, 0.42s) ease;
}

/* Shadow gathers toward the free (swinging) edge, away from the pivot. */
.leaf--front.turn-out-next .leaf-face::after {
  opacity: 1;
  background: linear-gradient(to right, transparent 45%, rgba(45, 35, 25, 0.4) 100%);
}

.leaf--front.turn-out-prev .leaf-face::after {
  opacity: 1;
  background: linear-gradient(to left, transparent 45%, rgba(45, 35, 25, 0.4) 100%);
}

/* The back leaf also casts a faint contact shadow under the lifting edge,
   so it doesn't look like it's floating free of the stack. This is an
   animation (fade in, hold, fade out), not a transition tied to the
   turning-next/prev class — the back leaf never fades itself, so if this
   shadow were still at full strength when that class gets removed at reset,
   its opacity would visibly snap to 0 on an otherwise fully-visible layer.
   Baking the fade-out into the animation guarantees it's already back at 0
   well before the class goes away. */
.leaf--back .leaf-face::before {
  content: "";
  position: absolute;
  inset: 0;
  opacity: 0;
  pointer-events: none;
}

@keyframes contactShadowPulse {
  0% { opacity: 0; }
  15% { opacity: 1; }
  65% { opacity: 1; }
  100% { opacity: 0; }
}

.book.turning-next .leaf--back .leaf-face::before {
  background: linear-gradient(to left, transparent 88%, rgba(45, 35, 25, 0.18) 100%);
  animation: contactShadowPulse var(--flip-dur, 0.52s) ease-in-out;
}

.book.turning-prev .leaf--back .leaf-face::before {
  background: linear-gradient(to right, transparent 88%, rgba(45, 35, 25, 0.18) 100%);
  animation: contactShadowPulse var(--flip-dur, 0.52s) ease-in-out;
}

.leaf-face img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  border-radius: 4px;
  user-select: none;
  -webkit-user-drag: none;
}

.leaf-face .cover-caption {
  position: absolute;
  bottom: 14px;
  left: 0;
  right: 0;
  text-align: center;
  font-family: var(--font-ui);
  font-size: 0.78rem;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--ink-faint);
  opacity: 0;
  animation: fadeUp 1s ease 0.6s forwards;
}

@keyframes fadeUp {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: translateY(0); }
}

.flip-zone {
  position: absolute;
  top: 0;
  bottom: 0;
  width: 30%;
  z-index: 3;
  opacity: 0;
}

.flip-zone--prev { left: 0; }
.flip-zone--next { right: 0; }

.flip-zone--zoom {
  left: 30%;
  right: 30%;
  width: auto;
  cursor: zoom-in;
}

/* Page indicator: its own row below the book (not squeezed inside the
   flip card), so it reads as a distinct, deliberate control rather than a
   faint afterthought hugging the image edge. */
.page-indicator {
  width: 100%;
  max-width: 480px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  margin-top: 2px;
}

.page-indicator[hidden] {
  display: none;
}

.page-dots {
  display: flex;
  align-items: center;
  gap: 4px;
}

.page-dots button {
  position: relative;
  width: 26px;
  height: 26px;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* The visible mark is a pseudo-element, not the button's own box — this
   keeps the tap target comfortably large (26px) while the dot itself stays
   small and precise. The active page morphs into a wider pill instead of
   just changing color, so which page you're on is obvious at a glance. */
.page-dots button::before {
  content: "";
  width: 6px;
  height: 6px;
  border-radius: 3px;
  background: var(--ink-faint);
  opacity: 0.5;
  transition: width 0.22s ease, opacity 0.22s ease, background 0.22s ease;
}

.page-dots button:hover::before {
  opacity: 0.8;
}

.page-dots button.active::before {
  width: 20px;
  opacity: 1;
  background: var(--accent-warm);
}

.page-indicator-label {
  font-family: var(--font-ui);
  font-size: 0.78rem;
  color: var(--ink-soft);
  letter-spacing: 0.02em;
  white-space: nowrap;
}

/* ---------------------------------------------------------------------- */
/* Transcript                                                               */
/* ---------------------------------------------------------------------- */

.transcript {
  width: 100%;
  max-width: 620px;
}

.transcript-card {
  background: var(--card);
  border: 1px solid var(--card-edge);
  border-radius: var(--radius-card);
  padding: 26px 24px 30px;
  box-shadow: 0 14px 32px -20px hsl(var(--shadow-1) / 0.35);
}

.transcript-title {
  font-size: 1.7rem;
  font-weight: 600;
  color: var(--ink);
  line-height: 1.2;
}

.transcript-subtitle {
  font-family: var(--font-body);
  font-style: italic;
  color: var(--ink-soft);
  margin-top: 6px;
  font-size: 1rem;
}

.transcript-intro {
  margin-top: 12px;
  color: var(--ink-soft);
  font-size: 0.98rem;
}

.transcript-part {
  margin-top: 26px;
  padding-top: 22px;
  border-top: 1px solid var(--line);
}

.transcript-part:first-of-type {
  margin-top: 22px;
  padding-top: 0;
  border-top: none;
}

.part-name {
  font-family: var(--font-display);
  font-size: 1.15rem;
  font-weight: 600;
  color: var(--accent-deep);
}

.part-subtitle {
  font-family: var(--font-body);
  font-style: italic;
  color: var(--ink-soft);
  font-size: 0.92rem;
  margin-top: 2px;
}

.section-label {
  font-family: var(--font-ui);
  font-size: 0.72rem;
  font-weight: 600;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  color: var(--accent);
  margin-top: 18px;
  margin-bottom: 10px;
  display: flex;
  align-items: center;
  gap: 8px;
}

.section-label::after {
  content: "";
  flex: 1;
  height: 1px;
  background: linear-gradient(to right, var(--line), transparent);
}

.ingredients-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  gap: 7px;
}

.ingredients-list li {
  position: relative;
  padding-left: 20px;
  font-size: 1rem;
  line-height: 1.5;
}

.ingredients-list li::before {
  content: "";
  position: absolute;
  left: 2px;
  top: 0.62em;
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background: var(--gold);
}

.method-list {
  margin: 0;
  padding: 0;
  list-style: none;
  counter-reset: step;
  display: grid;
  gap: 14px;
}

.method-list li {
  counter-increment: step;
  padding-left: 34px;
  position: relative;
  font-size: 1rem;
  line-height: 1.62;
}

.method-list li::before {
  content: counter(step);
  position: absolute;
  left: 0;
  top: 0;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: var(--paper-deep);
  color: var(--accent-deep);
  font-family: var(--font-ui);
  font-size: 0.78rem;
  font-weight: 600;
  display: flex;
  align-items: center;
  justify-content: center;
}

.method-prose {
  font-size: 1rem;
  line-height: 1.65;
  margin: 0;
}

.part-notes {
  margin-top: 14px;
  display: grid;
  gap: 6px;
}

.part-notes p {
  margin: 0;
  font-size: 0.9rem;
  font-style: italic;
  color: var(--ink-soft);
  line-height: 1.5;
}

/* ---------------------------------------------------------------------- */
/* Nav bar                                                                  */
/* ---------------------------------------------------------------------- */

.navbar {
  flex: 0 0 auto;
  height: var(--nav-h);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 26px;
  background: linear-gradient(to top, rgba(255,251,243,0.95), rgba(255,251,243,0.7));
  border-top: 1px solid var(--line);
  backdrop-filter: blur(6px);
}

.navbtn {
  width: 46px;
  height: 46px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--accent-deep);
  background: var(--card);
  border: 1px solid var(--card-edge);
  box-shadow: 0 4px 10px -4px hsl(var(--shadow-1) / 0.3);
  transition: transform 0.15s ease, box-shadow 0.15s ease;
}

.navbtn:hover { box-shadow: 0 6px 14px -4px hsl(var(--shadow-1) / 0.4); }
.navbtn:active { transform: scale(0.92); }
.navbtn:disabled { opacity: 0.35; box-shadow: none; cursor: default; }
.navbtn:disabled:active { transform: none; }

.navstatus {
  font-family: var(--font-ui);
  font-size: 0.85rem;
  color: var(--ink-soft);
  min-width: 56px;
  text-align: center;
  letter-spacing: 0.02em;
}

.navsep { margin: 0 3px; opacity: 0.5; }

/* ---------------------------------------------------------------------- */
/* Overlays: search + shelf                                                 */
/* ---------------------------------------------------------------------- */

.overlay {
  position: fixed;
  inset: 0;
  z-index: 20;
  background: rgba(60, 47, 36, 0.32);
  backdrop-filter: blur(3px);
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: 8vh 16px 16px;
  animation: overlayIn 0.2s ease;
}

.overlay[hidden] {
  display: none;
}

@keyframes overlayIn { from { opacity: 0; } to { opacity: 1; } }

.overlay-panel {
  width: 100%;
  max-width: 560px;
  background: var(--card);
  border-radius: 16px;
  border: 1px solid var(--card-edge);
  box-shadow: 0 30px 60px -20px hsl(var(--shadow-1) / 0.5);
  max-height: 78vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.search-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 16px 18px;
  border-bottom: 1px solid var(--line);
  flex: 0 0 auto;
}

.search-icon { color: var(--ink-faint); flex: 0 0 auto; }

#searchInput {
  flex: 1 1 auto;
  border: none;
  outline: none;
  background: none;
  font-family: var(--font-body);
  font-size: 1.05rem;
  color: var(--ink);
}

#searchInput::placeholder { color: var(--ink-faint); }

.results {
  overflow-y: auto;
  padding: 8px;
}

.result-item {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  text-align: left;
  padding: 10px 10px;
  border-radius: 10px;
  transition: background 0.12s ease;
}

.result-item:hover { background: rgba(95, 115, 80, 0.1); }

.result-thumb {
  width: 42px;
  height: 42px;
  border-radius: 6px;
  object-fit: cover;
  flex: 0 0 auto;
  background: var(--paper-deep);
  border: 1px solid var(--card-edge);
}

.result-text { display: flex; flex-direction: column; gap: 2px; }
.result-title { font-family: var(--font-display); font-size: 1rem; color: var(--ink); }
.result-sub { font-family: var(--font-ui); font-size: 0.78rem; color: var(--ink-faint); }

.no-results {
  padding: 30px 16px;
  text-align: center;
  color: var(--ink-faint);
  font-style: italic;
}

.shelf-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 18px;
  border-bottom: 1px solid var(--line);
  flex: 0 0 auto;
}

.shelf-header h2 {
  font-size: 1.15rem;
  color: var(--ink);
}

.shelf-grid {
  overflow-y: auto;
  padding: 16px;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 12px;
}

.shelf-item {
  display: flex;
  flex-direction: column;
  gap: 6px;
  text-align: left;
}

.shelf-thumb {
  width: 100%;
  aspect-ratio: 3/4;
  object-fit: cover;
  border-radius: 8px;
  border: 1px solid var(--card-edge);
  background: var(--paper-deep);
  transition: transform 0.15s ease, box-shadow 0.15s ease;
}

.shelf-item:hover .shelf-thumb {
  transform: translateY(-2px);
  box-shadow: 0 8px 16px -8px hsl(var(--shadow-1) / 0.4);
}

.shelf-title {
  font-family: var(--font-ui);
  font-size: 0.76rem;
  color: var(--ink-soft);
  line-height: 1.3;
}

/* ---------------------------------------------------------------------- */
/* Lightbox                                                                  */
/* ---------------------------------------------------------------------- */

.overlay--lightbox {
  align-items: center;
  justify-content: center;
  padding: 28px;
  background: rgba(28, 21, 15, 0.86);
  backdrop-filter: blur(2px);
}

.overlay--lightbox img {
  max-width: 100%;
  max-height: 100%;
  width: auto;
  height: auto;
  object-fit: contain;
  border-radius: 6px;
  background: var(--card);
  box-shadow: 0 40px 80px -24px rgba(0, 0, 0, 0.6);
  cursor: zoom-out;
}

.lightbox-close {
  position: fixed;
  top: 16px;
  right: 16px;
  z-index: 21;
  color: #fff;
  background: rgba(255, 255, 255, 0.12);
}

.lightbox-close:hover {
  background: rgba(255, 255, 255, 0.22);
  color: #fff;
}

/* ---------------------------------------------------------------------- */
/* Intro / nav-disabled state                                               */
/* ---------------------------------------------------------------------- */

.navbar.disabled,
.topbar.disabled {
  pointer-events: none;
  opacity: 0.4;
  transition: opacity 0.3s ease;
}

/* ---------------------------------------------------------------------- */
/* Responsive: desktop layout                                               */
/* ---------------------------------------------------------------------- */

@media (min-width: 900px) {
  .stage {
    flex-direction: row;
    align-items: flex-start;
    justify-content: center;
    gap: 46px;
    padding: 40px 40px 40px;
  }

  .book-column {
    max-width: 420px;
    position: sticky;
    /* Sticky offsets are measured from the scrollport's padding edge, which
       already includes .stage's own 40px padding-top — using top:40px here
       would double-count it and rest 40px lower than the transcript card. */
    top: 0;
  }

  .transcript {
    max-width: 560px;
  }

  .shelf-grid { grid-template-columns: repeat(4, 1fr); }
}

@media (max-width: 480px) {
  .transcript-card { padding: 20px 16px 24px; }
  .transcript-title { font-size: 1.45rem; }
  .shelf-grid { grid-template-columns: repeat(2, 1fr); }
}

@media (prefers-reduced-motion: reduce) {
  .leaf.turning,
  .transcript,
  .leaf-face .cover-caption,
  .tap-hint {
    transition: none !important;
    animation: none !important;
  }
}
