/* =========================================================================
   Bugsnax and the Cupcake Crown — stylesheet
   Warm storybook aesthetic. iPad-first. Designed for Noah (7yo).
   ========================================================================= */

@import url('https://fonts.googleapis.com/css2?family=Fraunces:opsz,wght@9..144,500;9..144,600;9..144,700&family=Atkinson+Hyperlegible:wght@400;700&display=swap');

:root, [data-theme="light"] {
  /* Paper & ink */
  --paper:          #f7ead3;
  --paper-soft:     #fbf3df;
  --paper-edge:     #ead5a8;
  --ink:            #3d2614;
  --ink-muted:      #7a5a3a;

  /* Warm core */
  --caramel:        #c47a3c;
  --honey:          #e6a65d;
  --honey-light:    #f4c983;
  --toffee:         #8b4a20;
  --maple:          #b06630;

  /* Tap highlight */
  --tap-bg:         #ffd89e;
  --tap-ring:       #e6a65d;
  --tap-shadow:     rgba(230, 166, 93, 0.35);

  /* Body / shell */
  --body-bg:        radial-gradient(ellipse at center, #3d2818 0%, #1a0e06 100%);
  --book-bg-overlay: radial-gradient(circle at 20% 15%, rgba(255,255,255,0.4) 0%, transparent 40%), radial-gradient(circle at 80% 85%, rgba(180,130,70,0.08) 0%, transparent 50%);
  --book-shadow:    0 0 0 1px rgba(90, 60, 30, 0.2), 0 40px 80px rgba(0, 0, 0, 0.5), inset 0 0 60px rgba(139, 74, 32, 0.08);
  --cover-grad:     radial-gradient(ellipse at center top, #fce8c4 0%, var(--paper) 70%);

  /* Cards / controls */
  --card-bg:        var(--paper-soft);
  --card-border:    rgba(180, 130, 70, 0.2);
  --ctrl-bg:        rgba(251, 243, 223, 0.75);
  --ctrl-border:    rgba(180, 130, 70, 0.25);
  --dot-rest:       rgba(139, 74, 32, 0.2);
  --dot-hover:      rgba(139, 74, 32, 0.4);
  --scrim:          rgba(40, 22, 10, 0.3);
  --crown-grad:     linear-gradient(135deg, #fff0d2 0%, #ffe0b0 100%);
  --crown-glow:     radial-gradient(circle, #fff 0%, var(--honey-light) 60%, transparent 75%);
  --hint-bg:        rgba(61, 38, 20, 0.88);
  --hint-fg:        #fbf3df;
  --btn-fg:         #fff8e8;

  /* Type */
  --font-display:   "Fraunces", Georgia, serif;
  --font-body:      "Atkinson Hyperlegible", system-ui, sans-serif;

  /* Shadow & radius */
  --radius-sm:      6px;
  --radius-md:      14px;
  --radius-lg:      22px;
  --shadow-illo:    0 10px 30px rgba(90, 50, 20, 0.18), 0 2px 8px rgba(90, 50, 20, 0.08);
  --shadow-card:    0 20px 60px rgba(60, 35, 15, 0.22), 0 4px 14px rgba(60, 35, 15, 0.12);
}

[data-theme="dark"] {
  /* Night-read palette: deep plum/indigo paper, warm cream ink.
     Keeps the book feeling like a bedtime story, not a slick app. */
  --paper:          #211a2e;   /* deep night plum */
  --paper-soft:     #2c2340;
  --paper-edge:     #3a2f50;
  --ink:            #f4e6c8;   /* warm cream */
  --ink-muted:      #b5a585;

  --caramel:        #f4b872;   /* honey-amber, brightened for contrast */
  --honey:          #ffc98a;
  --honey-light:    #ffd89e;
  --toffee:         #ffdca8;   /* used as "display" colour on dark */
  --maple:          #ffb77a;

  --tap-bg:         #6b4a1e;
  --tap-ring:       #f4b872;
  --tap-shadow:     rgba(244, 184, 114, 0.45);

  --body-bg:        radial-gradient(ellipse at center, #14101f 0%, #06040c 100%);
  --book-bg-overlay: radial-gradient(circle at 20% 15%, rgba(255, 210, 150, 0.06) 0%, transparent 40%), radial-gradient(circle at 80% 85%, rgba(100, 70, 140, 0.12) 0%, transparent 50%);
  --book-shadow:    0 0 0 1px rgba(255, 210, 150, 0.08), 0 40px 80px rgba(0, 0, 0, 0.7), inset 0 0 80px rgba(255, 200, 130, 0.04);
  --cover-grad:     radial-gradient(ellipse at center top, #3a2c50 0%, var(--paper) 70%);

  --card-bg:        #2c2340;
  --card-border:    rgba(255, 210, 150, 0.14);
  --ctrl-bg:        rgba(44, 35, 64, 0.8);
  --ctrl-border:    rgba(255, 210, 150, 0.15);
  --dot-rest:       rgba(244, 230, 200, 0.22);
  --dot-hover:      rgba(244, 230, 200, 0.4);
  --scrim:          rgba(6, 4, 12, 0.6);
  --crown-grad:     linear-gradient(135deg, #3f2f5a 0%, #5a3e6e 100%);
  --crown-glow:     radial-gradient(circle, rgba(255, 216, 158, 0.8) 0%, rgba(244, 184, 114, 0.5) 60%, transparent 75%);
  --hint-bg:        rgba(244, 230, 200, 0.95);
  --hint-fg:        #14101f;
  --btn-fg:         #14101f;

  --shadow-illo:    0 10px 30px rgba(0, 0, 0, 0.55), 0 2px 8px rgba(0, 0, 0, 0.35);
  --shadow-card:    0 20px 60px rgba(0, 0, 0, 0.7), 0 4px 14px rgba(0, 0, 0, 0.4);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  padding: 0;
  background: #2a1a0e;
  color: var(--ink);
  font-family: var(--font-body);
  overflow: hidden;
  height: 100%;
  width: 100%;
  overscroll-behavior: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}

body {
  /* Bookshelf behind the book — shifts with theme */
  background: var(--body-bg);
  transition: background 400ms ease;
}

#root {
  width: 100vw;
  height: 100vh;
  height: 100dvh;
  position: relative;
  overflow: hidden;
}

/* ======================== STAGE / BOOK ======================== */

.stage {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}

.book {
  position: relative;
  width: 100%;
  height: 100%;
  max-width: 1400px;
  max-height: 100%;
  background: var(--paper);
  background-image: var(--book-bg-overlay);
  box-shadow: var(--book-shadow);
  overflow: hidden;
  transition: background 400ms ease, box-shadow 400ms ease;
}

/* On very wide viewports, frame the book nicely */
@media (min-width: 1000px) {
  .book {
    margin: 2vh 2vw;
    border-radius: 4px;
    width: calc(100vw - 4vw);
    height: calc(100dvh - 4vh);
  }
}

/* ======================== SPREADS ======================== */

.spreads {
  position: absolute;
  inset: 0;
  display: flex;
  transition: transform 520ms cubic-bezier(0.4, 0.0, 0.2, 1);
  will-change: transform;
}

.spread {
  flex: 0 0 100%;
  width: 100%;
  height: 100%;
  position: relative;
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
}

.spread::-webkit-scrollbar { width: 0; display: none; }

/* ======================== COVER ======================== */

.cover-spread {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 4vh 6vw;
  text-align: center;
  background: var(--cover-grad);
}

.cover-illo {
  width: min(62vh, 62vw, 560px);
  aspect-ratio: 1 / 1;
  border-radius: 8px;
  background-size: cover;
  background-position: center;
  box-shadow: var(--shadow-illo);
  margin-bottom: 3vh;
}

.cover-title {
  font-family: var(--font-display);
  font-weight: 600;
  font-size: clamp(2.2rem, 5.5vw, 4.2rem);
  line-height: 1.05;
  color: var(--toffee);
  letter-spacing: -0.01em;
  margin: 0 0 0.4rem;
  text-wrap: balance;
}

.cover-title em {
  font-style: italic;
  color: var(--caramel);
}

.cover-subtitle {
  font-family: var(--font-display);
  font-style: italic;
  font-weight: 500;
  font-size: clamp(1rem, 2vw, 1.35rem);
  color: var(--ink-muted);
  margin: 0 0 2vh;
}

.cover-dedication {
  font-family: var(--font-body);
  font-size: clamp(0.95rem, 1.5vw, 1.15rem);
  color: var(--ink);
  opacity: 0.8;
  margin-bottom: 2.5vh;
}

.cover-dedication strong {
  color: var(--caramel);
  font-weight: 700;
}

.cover-begin {
  display: inline-flex;
  align-items: center;
  gap: 0.6rem;
  padding: 0.9rem 1.8rem;
  background: var(--caramel);
  color: var(--btn-fg);
  font-family: var(--font-display);
  font-weight: 600;
  font-size: clamp(1rem, 1.8vw, 1.2rem);
  border: none;
  border-radius: 999px;
  box-shadow: 0 6px 20px rgba(196, 122, 60, 0.4), inset 0 -3px 0 rgba(0,0,0,0.1);
  cursor: pointer;
  transition: transform 180ms ease, box-shadow 180ms ease, background 300ms ease;
}
.cover-begin:active {
  transform: translateY(2px);
  box-shadow: 0 2px 8px rgba(196, 122, 60, 0.4), inset 0 -1px 0 rgba(0,0,0,0.1);
}
.cover-begin .arrow { font-size: 1.2em; }

/* ======================== CONTENTS ======================== */

.contents-spread {
  padding: 6vh max(6vw, 84px) 12vh;
}

.contents-title {
  font-family: var(--font-display);
  font-size: clamp(2rem, 4vw, 3rem);
  font-weight: 600;
  color: var(--toffee);
  text-align: center;
  margin: 0 0 1vh;
}

.contents-act-label {
  font-family: var(--font-display);
  font-style: italic;
  color: var(--ink-muted);
  text-align: center;
  margin: 0 0 3vh;
  font-size: clamp(0.95rem, 1.5vw, 1.15rem);
}

.contents-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
  gap: 18px;
  max-width: 1100px;
  margin: 0 auto;
}

.contents-item {
  display: flex;
  gap: 14px;
  align-items: center;
  padding: 12px;
  background: var(--paper-soft);
  border: 1px solid var(--card-border);
  border-radius: var(--radius-md);
  cursor: pointer;
  transition: transform 150ms ease, box-shadow 150ms ease;
  text-align: left;
  font-family: var(--font-body);
  color: var(--ink);
}
.contents-item:active { transform: scale(0.98); }
.contents-item.available {
  box-shadow: 0 2px 6px rgba(90, 50, 20, 0.08);
}
.contents-item.unavailable {
  opacity: 0.45;
  cursor: default;
}

.contents-thumb {
  width: 64px;
  height: 64px;
  flex-shrink: 0;
  border-radius: 8px;
  background-color: var(--paper-edge);
  background-size: cover;
  background-position: center;
  box-shadow: 0 2px 4px rgba(90, 50, 20, 0.2);
}
.contents-thumb.placeholder {
  background: repeating-linear-gradient(
    45deg,
    var(--paper-edge) 0px, var(--paper-edge) 6px,
    var(--paper-soft) 6px, var(--paper-soft) 12px
  );
}

.contents-chnum {
  font-family: var(--font-display);
  font-weight: 600;
  color: var(--caramel);
  font-size: 0.85rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  margin-bottom: 2px;
}

.contents-chtitle {
  font-family: var(--font-display);
  font-size: 1.05rem;
  font-weight: 600;
  line-height: 1.2;
  color: var(--toffee);
}

/* ======================== CHAPTER SPREAD ======================== */

.chapter-spread {
  display: grid;
  grid-template-rows: auto 1fr;
  min-height: 100%;
}

.chapter-header {
  padding: 4vh max(6vw, 84px) 1vh;
  text-align: center;
}

.chapter-num {
  font-family: var(--font-display);
  font-weight: 600;
  font-size: 0.85rem;
  letter-spacing: 0.2em;
  text-transform: uppercase;
  color: var(--caramel);
  margin-bottom: 4px;
}

.chapter-title {
  font-family: var(--font-display);
  font-weight: 600;
  font-size: clamp(1.8rem, 3.6vw, 2.8rem);
  line-height: 1.1;
  color: var(--toffee);
  margin: 0;
  text-wrap: balance;
}

.chapter-divider {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  margin: 12px auto 0;
  color: var(--honey);
  opacity: 0.7;
}
.chapter-divider::before,
.chapter-divider::after {
  content: "";
  display: block;
  width: 40px;
  height: 1px;
  background: currentColor;
}

.chapter-body {
  display: grid;
  grid-template-columns: 1fr;
  gap: 3vh;
  padding: 2vh max(6vw, 84px) 14vh;
  max-width: 1200px;
  margin: 0 auto;
  width: 100%;
}

@media (min-width: 900px) and (orientation: landscape) {
  .chapter-body {
    grid-template-columns: 1.1fr 1fr;
    gap: 4vw;
    align-items: start;
  }
  .chapter-body.reverse {
    grid-template-columns: 1fr 1.1fr;
  }
  .chapter-body.reverse .chapter-illo { order: 2; }
  .chapter-body.reverse .chapter-text { order: 1; }
}

.chapter-illo {
  width: 100%;
  aspect-ratio: 1 / 1;
  border-radius: 10px;
  background-size: cover;
  background-position: center;
  background-color: var(--paper-edge);
  box-shadow: var(--shadow-illo);
  position: sticky;
  top: 4vh;
}
@media (max-width: 899px) {
  .chapter-illo { position: static; aspect-ratio: 4 / 3; }
}

.chapter-text {
  font-family: var(--font-body);
  font-size: clamp(1.15rem, 1.6vw, 1.4rem);
  line-height: 1.7;
  color: var(--ink);
  max-width: 60ch;
}

.chapter-text p {
  margin: 0 0 1.25em;
  text-wrap: pretty;
}

@media (min-width: 900px) and (orientation: landscape) {
  .chapter-body.no-illo {
    grid-template-columns: 1fr;
  }
  .chapter-body.no-illo .chapter-text {
    max-width: 64ch;
    margin: 0 auto;
  }
}

.chapter-illo.chapter-illo-secondary {
  position: static;
  margin-top: 2.5em;
  aspect-ratio: 4 / 3;
  width: 100%;
}

/* ======================== WORD TAPS ======================== */

.word {
  display: inline;
  cursor: pointer;
  border-radius: 6px;
  padding: 0 1px;
  margin: 0 -1px;
  transition: background 200ms ease, box-shadow 200ms ease, color 200ms ease;
  position: relative;
}

.word.bugsnak-name {
  font-weight: 700;
  color: var(--caramel);
}

.word.onomatopoeia {
  font-style: italic;
  color: var(--maple);
  font-weight: 700;
}

.word.active {
  background: var(--tap-bg);
  box-shadow: 0 0 0 2px var(--tap-ring), 0 4px 12px var(--tap-shadow);
  color: var(--toffee);
}

.word.fading {
  background: rgba(255, 216, 158, 0.4);
}

/* ======================== WORD POPUP CARD ======================== */

.word-card-backdrop {
  position: fixed;
  inset: 0;
  background: var(--scrim);
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  animation: fadeIn 180ms ease;
}

.word-card {
  background: var(--card-bg);
  border-radius: var(--radius-lg);
  padding: 28px 26px 22px;
  max-width: 420px;
  width: 100%;
  box-shadow: var(--shadow-card);
  border: 1px solid var(--card-border);
  position: relative;
  animation: popIn 260ms cubic-bezier(0.34, 1.56, 0.64, 1);
  max-height: 85vh;
  overflow-y: auto;
}

@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
@keyframes popIn {
  from { opacity: 0; transform: scale(0.85) translateY(16px); }
  to   { opacity: 1; transform: scale(1) translateY(0); }
}

.word-card-close {
  position: absolute;
  top: 12px;
  right: 12px;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: none;
  background: var(--paper-edge);
  color: var(--toffee);
  font-size: 20px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: var(--font-body);
}
.word-card-close:active { background: var(--honey); }

.word-card-headline {
  font-family: var(--font-display);
  font-weight: 600;
  font-size: 2.2rem;
  color: var(--toffee);
  margin: 0 0 2px;
  line-height: 1.1;
}

.word-card-sublabel {
  font-family: var(--font-body);
  font-size: 0.85rem;
  color: var(--ink-muted);
  margin: 0 0 18px;
  letter-spacing: 0.05em;
  text-transform: uppercase;
}

.syllables {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 20px;
}

.syllable {
  display: inline-flex;
  align-items: center;
  padding: 10px 16px;
  background: var(--honey-light);
  border-radius: 999px;
  font-family: var(--font-display);
  font-weight: 600;
  font-size: 1.35rem;
  color: var(--toffee);
  cursor: pointer;
  border: 2px solid transparent;
  transition: transform 120ms ease, background 150ms ease, border-color 150ms ease;
  position: relative;
}
.syllable:active {
  transform: scale(0.94);
  background: var(--honey);
}
.syllable.speaking {
  background: var(--caramel);
  color: var(--btn-fg);
  border-color: var(--toffee);
  transform: scale(1.08);
}

.syllable-dot {
  color: var(--honey);
  font-size: 1.3rem;
  font-weight: 700;
  padding: 0 2px;
  display: inline-flex;
  align-items: center;
}

.word-card-definition {
  font-family: var(--font-body);
  font-size: 1.15rem;
  line-height: 1.55;
  color: var(--ink);
  margin: 0 0 22px;
}

.word-card-actions {
  display: flex;
  gap: 10px;
}

.say-btn {
  flex: 1;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 14px 16px;
  border: none;
  border-radius: 14px;
  font-family: var(--font-display);
  font-weight: 600;
  font-size: 1.05rem;
  cursor: pointer;
  background: var(--caramel);
  color: var(--btn-fg);
  box-shadow: 0 4px 14px rgba(196, 122, 60, 0.4), inset 0 -2px 0 rgba(0,0,0,0.12);
  transition: transform 120ms ease;
}
.say-btn:active { transform: translateY(2px); box-shadow: 0 2px 6px rgba(196, 122, 60, 0.4), inset 0 -1px 0 rgba(0,0,0,0.12); }

.say-btn.secondary {
  background: var(--paper-edge);
  color: var(--toffee);
  box-shadow: 0 2px 6px rgba(90, 50, 20, 0.12), inset 0 -2px 0 rgba(0,0,0,0.06);
}

.no-def {
  font-style: italic;
  opacity: 0.7;
}

/* ======================== CORNER CONTROLS ======================== */

.corner-controls {
  position: absolute;
  top: 16px;
  left: 16px;
  right: 16px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  pointer-events: none;
  z-index: 30;
}

.ctrl-btn {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  border: 1px solid var(--ctrl-border);
  background: var(--ctrl-bg);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  color: var(--toffee);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  pointer-events: auto;
  transition: transform 120ms ease, background 150ms ease;
  box-shadow: 0 2px 8px rgba(0,0,0,0.08);
}
.ctrl-btn:active { transform: scale(0.92); background: var(--honey-light); }

/* ======================== BOTTOM PROGRESS STRIP ======================== */

.progress-strip {
  position: absolute;
  bottom: 18px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  gap: 8px;
  padding: 8px 14px;
  background: var(--ctrl-bg);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  border-radius: 999px;
  border: 1px solid var(--ctrl-border);
  z-index: 30;
  box-shadow: 0 4px 14px rgba(0,0,0,0.08);
}

.progress-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--dot-rest);
  cursor: pointer;
  transition: transform 150ms ease, background 150ms ease, width 150ms ease;
  padding: 0;
  border: none;
}
.progress-dot.active {
  background: var(--caramel);
  width: 22px;
  border-radius: 5px;
}
.progress-dot:hover:not(.active) { background: var(--dot-hover); }

/* ======================== PAGE ARROWS ======================== */

.page-arrow {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  width: 52px;
  height: 52px;
  border-radius: 50%;
  border: none;
  background: var(--ctrl-bg);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  color: var(--toffee);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  z-index: 25;
  font-size: 28px;
  box-shadow: 0 4px 14px rgba(0,0,0,0.12);
  opacity: 0;
  transition: opacity 300ms ease, transform 150ms ease;
}

.page-arrow.visible { opacity: 1; }
.page-arrow:active { transform: translateY(-50%) scale(0.92); }
.page-arrow.left { left: 16px; }
.page-arrow.right { right: 16px; }
.page-arrow.hidden { display: none; }

/* ======================== CUPCAKE CROWN MOMENT (Ch22) ======================== */

.crown-quote {
  margin: 4vh auto;
  padding: 28px 32px;
  max-width: 540px;
  background: var(--crown-grad);
  border-radius: var(--radius-lg);
  border: 1px solid var(--honey);
  box-shadow: 0 10px 40px rgba(230, 166, 93, 0.35), inset 0 1px 0 rgba(255,255,255,0.6);
  font-family: var(--font-display);
  font-style: italic;
  font-weight: 500;
  font-size: clamp(1.15rem, 1.8vw, 1.45rem);
  line-height: 1.5;
  color: var(--toffee);
  text-align: center;
  position: relative;
}

.crown-quote::before,
.crown-quote::after {
  content: "";
  position: absolute;
  width: 40px;
  height: 40px;
  background: var(--crown-glow);
  border-radius: 50%;
  opacity: 0.7;
}
.crown-quote::before { top: -12px; left: -12px; }
.crown-quote::after { bottom: -12px; right: -12px; }

/* ======================== HINT OVERLAY ======================== */

/* Dark-mode-only: dim the illustrations slightly so they feel like book art at night */
[data-theme="dark"] .cover-illo,
[data-theme="dark"] .chapter-illo,
[data-theme="dark"] .contents-thumb {
  filter: brightness(0.78) saturate(0.9);
}

/* Dark-mode tap-hint: arrow colour tuned */
[data-theme="dark"] .word.bugsnak-name { color: var(--honey); }
[data-theme="dark"] .word.onomatopoeia { color: var(--maple); }

.tap-hint {
  position: absolute;
  bottom: 70px;
  left: 50%;
  transform: translateX(-50%);
  padding: 10px 18px;
  background: var(--hint-bg);
  color: var(--hint-fg);
  border-radius: 999px;
  font-family: var(--font-body);
  font-size: 0.9rem;
  z-index: 40;
  pointer-events: none;
  animation: hintFade 4.5s ease-in forwards;
  box-shadow: 0 4px 14px rgba(0,0,0,0.3);
}
@keyframes hintFade {
  0% { opacity: 0; transform: translate(-50%, 10px); }
  15% { opacity: 1; transform: translate(-50%, 0); }
  85% { opacity: 1; }
  100% { opacity: 0; transform: translate(-50%, -10px); }
}
