/* ==========================================================================
   MARVELS OF HOPE — Bespoke Anime Storybook Design System (Micro-Task 1)
   100% Hand-Crafted Organic Framing | Zero Emojis | Zero Default SaaS UI
   ========================================================================== */

:root {
  /* Core Realm Color Tokens */
  --bg-parchment: #FDFBF4;
  --parchment-warm: #F8F1DF;
  --parchment-card: #FFFDF8;
  --eos-gold: #E9C46A;
  --eos-gold-bright: #F3D583;
  --cinnamon-amber: #F4A261;
  --terracotta-apple: #E76F51;
  --emerald-leaf: #2A9D8F;
  --emerald-soft: #D9F0EC;
  --ink-slate: #1E2D38;
  --ink-outline: #264653;

  /* Panel 2 Exclusive Frost Tokens (Nachtwald) */
  --nachtwald-ice: #2B3D4F;
  --nachtwald-frost: #A8C5DA;
  --nachtwald-pale: #DCEAF5;

  /* Dynamic Page Atmosphere Hooks (Ready for Micro-Task 2 GSAP Shifts) */
  --page-bg: var(--bg-parchment);
  --page-text: var(--ink-slate);

  /* Organic Storybook Radii */
  --radius-frame-odd: 28px 12px 32px 16px;
  --radius-frame-even: 14px 30px 18px 32px;
  --radius-plate: 26px 14px 30px 18px;
  --radius-bubble: 30px 14px 28px 18px;
  --radius-cloud: 38px 24px 40px 26px;
  --radius-btn: 26px 16px 28px 18px;

  /* Cel-Shaded Shadows & Borders */
  --border-ink: 2.5px solid var(--ink-outline);
  --border-ink-thick: 3px solid var(--ink-outline);
  --shadow-cel: 4px 5px 0px var(--ink-outline);
  --shadow-cel-lg: 6px 7px 0px var(--ink-outline);

  /* Typography Stacks */
  --font-display: 'Fredoka', ui-rounded, 'SF Pro Rounded', 'Quicksand', system-ui, sans-serif;
  --font-prose: 'Quicksand', ui-rounded, 'SF Pro Rounded', 'Nunito', system-ui, sans-serif;

  /* Squash-and-Stretch Spring Physics */
  --ease-spring: cubic-bezier(0.34, 1.56, 0.64, 1);

  /* ==========================================================================
     D5: CSS-ONLY BOOK-OPEN FLIP TOKENS
     The cover hinges on its leather spine (the card's left edge). The hinge
     owns the perspective and places the vanishing point exactly on that
     spine, so the edge stays visually pinned while the cover swings open.
     ========================================================================== */
  --cover-w: min(clamp(320px, 86vw, 680px), calc(100vw - 16px));
  --cover-h: min(clamp(420px, 86vh, 760px), calc(100dvh - 30px));
  --cover-tilt-y: -3.5deg;
  --cover-tilt-x: 1.2deg;
  --cover-open-angle: -172deg;
  --flip-perspective: 2400px;
  --flip-duration: 0.92s;
  --flip-ease: cubic-bezier(0.36, 0.08, 0.24, 1);
  --flip-fade-duration: 0.28s;
  --flip-fade-delay: 0.64s;
  --flip-back-duration: 0.72s;
  --flip-back-delay: 0.04s;
  --flip-page-fade-duration: 0.48s;
  --flip-page-fade-delay: 0.06s;
}

/* Reset & Cross-Device Overflow Protection */
*,
*::before,
*::after {
  box-sizing: border-box;
}

html {
  scroll-behavior: smooth;
  overflow: hidden;
  width: 100vw;
  height: 100dvh;
  max-width: 100vw;
  max-height: 100dvh;
  -webkit-text-size-adjust: 100%;
}

body {
  margin: 0;
  padding: 0;
  width: 100vw;
  height: 100dvh;
  max-width: 100vw;
  max-height: 100dvh;
  overflow: hidden;
  background-color: var(--page-bg);
  color: var(--page-text);
  font-family: var(--font-prose);
  font-weight: 600;
  line-height: 1.7;
  position: relative;
  /* Warm Sunlit Paper Wash (Strictly Zero Purple/Indigo) */
  background-image:
    radial-gradient(circle at 12% 8%, rgba(233, 196, 106, 0.20) 0%, transparent 42%),
    radial-gradient(circle at 88% 22%, rgba(244, 162, 97, 0.14) 0%, transparent 38%),
    radial-gradient(circle at 50% 85%, rgba(42, 157, 143, 0.08) 0%, transparent 48%);
}

/* Layered Tactile Paper-Grain CSS Texture Overlay */
body::before {
  content: "";
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 950;
  opacity: 0.42;
  transform: translateZ(0);
  contain: strict;
  background-image:
    repeating-linear-gradient(
      0deg,
      rgba(38, 70, 83, 0.018) 0px,
      rgba(38, 70, 83, 0.018) 1px,
      transparent 1px,
      transparent 4px
    ),
    repeating-linear-gradient(
      90deg,
      rgba(233, 196, 106, 0.025) 0px,
      rgba(233, 196, 106, 0.025) 1px,
      transparent 1px,
      transparent 5px
    ),
    url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='160' height='160' viewBox='0 0 160 160'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.82' numOctaves='3' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='160' height='160' filter='url(%23n)' opacity='0.065'/%3E%3C/svg%3E");
}

/* GPU-Composited Frostbite Realm Atmosphere Veil */
.realm-atmosphere-veil {
  position: fixed;
  inset: 0;
  z-index: 0;
  pointer-events: none;
  opacity: 0;
  will-change: opacity;
  background-color: #1D2D3A;
  background-image:
    radial-gradient(circle at 20% 18%, rgba(168, 197, 218, 0.22) 0%, transparent 46%),
    radial-gradient(circle at 82% 68%, rgba(43, 61, 79, 0.55) 0%, transparent 52%),
    linear-gradient(180deg, #182632 0%, #1D2D3A 52%, #233747 100%);
}

/* Dual-Mode Ambient Weather Canvas Overlay (Pointer-Events None) */
.weather-canvas {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 800;
  display: block;
}

.storybook-hero,
.storybook-main,
.storybook-footer {
  position: relative;
  z-index: 2;
}

.sr-svg-defs {
  position: absolute;
  width: 0;
  height: 0;
  overflow: hidden;
  pointer-events: none;
}

[hidden] {
  display: none !important;
}

/*
 * Hard guard for the page deck: several page-level rules (notably the D1
 * `#book-cover` rule further down) scope `display` to an ID/class, whose
 * specificity beats the generic `[hidden]` rule above even when both are
 * `!important`. Without this guard a page can remain visible after
 * `setActivePage()` sets `page.hidden = true`, which rendered the book cover
 * on top of every chapter (the deck is 8 stacked 100dvh pages inside an
 * `overflow: hidden` stage, so only the first visible page is ever seen).
 */
#book-cover[hidden],
.book-page[hidden],
.storybook-hero[hidden] {
  display: none !important;
}

img {
  display: block;
  max-width: 100%;
  height: auto;
}

/* ==========================================================================
   CUSTOM INLINE SVG ICON SYSTEM
   ========================================================================== */

.cel-icon {
  width: 40px;
  height: 40px;
  display: block;
  flex-shrink: 0;
  overflow: visible;
}

.cel-icon--lg {
  width: 64px;
  height: 64px;
}

.cel-icon--sm {
  width: 34px;
  height: 34px;
}

.cel-icon--xs {
  width: 28px;
  height: 28px;
}

/* ==========================================================================
   TACTILE 3D PILLOWED BUTTONS & FLOATING TOP-RIGHT HUD
   ========================================================================== */

.tactile-btn {
  --btn-lip: #264653;
  min-height: 68px;
  min-width: 68px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.65rem;
  padding: 0.65rem 1.25rem;
  border: var(--border-ink);
  border-radius: var(--radius-btn);
  background-color: var(--parchment-card);
  color: var(--ink-slate);
  font-family: var(--font-display);
  font-size: clamp(0.92rem, 1.1vw, 1.05rem);
  font-weight: 700;
  letter-spacing: 0.01em;
  line-height: 1.15;
  cursor: pointer;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  box-shadow:
    0 8px 0 var(--btn-lip),
    0 12px 20px rgba(30, 45, 56, 0.25),
    inset 0 4px 0 rgba(255, 255, 255, 0.45);
  transition:
    transform 0.26s var(--ease-spring),
    box-shadow 0.26s var(--ease-spring),
    background-color 0.2s ease;
}

.tactile-btn:hover {
  transform: translateY(-2px) scale(1.02);
  box-shadow:
    0 10px 0 var(--btn-lip),
    0 15px 24px rgba(30, 45, 56, 0.28),
    inset 0 4px 0 rgba(255, 255, 255, 0.55);
}

.tactile-btn:active {
  transform: translateY(6px) scale(0.98);
  box-shadow:
    0 2px 0 var(--btn-lip),
    0 4px 10px rgba(30, 45, 56, 0.2),
    inset 0 2px 0 rgba(255, 255, 255, 0.35);
}

.tactile-btn:focus-visible {
  outline: 3px dashed var(--cinnamon-amber);
  outline-offset: 4px;
}

.tactile-btn:disabled {
  opacity: 0.48;
  cursor: not-allowed;
  transform: none;
  box-shadow:
    0 5px 0 var(--btn-lip),
    0 6px 12px rgba(30, 45, 56, 0.16),
    inset 0 2px 0 rgba(255, 255, 255, 0.35);
}

.floating-hud {
  position: fixed;
  top: clamp(12px, 2vw, 22px);
  right: clamp(12px, 2.4vw, 28px);
  z-index: 900;
  display: flex;
  align-items: center;
  gap: clamp(8px, 1.2vw, 14px);
  max-width: calc(100vw - 24px);
}

.hud-btn--cast {
  background-color: var(--eos-gold);
  border-radius: 28px 14px 30px 18px;
}

.hud-btn--audio {
  background-color: #FFFDF8;
  border-radius: 18px 28px 16px 30px;
}

.hud-btn--audio[data-audio-state="off"] {
  background-color: var(--parchment-warm);
  opacity: 0.92;
}

.hud-btn--audio[data-audio-state="off"] .audio-wave {
  opacity: 0.25;
}

.hud-btn--motion {
  background-color: #D9F0EC;
  border-radius: 16px 26px 18px 28px;
}

.hud-btn--motion[data-motion-state="auto"] {
  background-color: #FFF3D6;
}

.hud-btn--motion[data-motion-state="reduce"] {
  background-color: var(--parchment-warm);
  opacity: 0.92;
}

.hud-btn--motion[data-motion-state="reduce"] .cel-icon {
  opacity: 0.45;
}

.btn-icon-wrap {
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.hud-btn-label {
  white-space: nowrap;
  text-shadow: 0 1px 0 rgba(255, 255, 255, 0.55);
}

.audio-mixer-popover {
  position: absolute;
  top: calc(100% + 10px);
  right: 0;
  background: #FFFDF8;
  border: 2.5px solid #264653;
  border-radius: 20px 10px 22px 12px;
  box-shadow: 4px 5px 0 #264653, 0 10px 22px rgba(0, 0, 0, 0.22);
  padding: 10px 14px;
  display: none;
  flex-direction: column;
  gap: 6px;
  min-width: 190px;
  z-index: 920;
}

.floating-hud:hover .audio-mixer-popover,
.floating-hud:focus-within .audio-mixer-popover,
.audio-mixer-popover.is-open {
  display: flex;
}

.mixer-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  font-family: var(--font-display);
  font-size: 0.78rem;
  font-weight: 700;
  color: #1E2D38;
}

.mixer-row input[type="range"] {
  width: 96px;
  accent-color: #2A9D8F;
  cursor: pointer;
}

/* ==========================================================================
   HERO COVER HEADER
   ========================================================================== */

.storybook-hero {
  width: 100vw;
  height: 100dvh;
  max-width: 100vw;
  max-height: 100dvh;
  margin: 0;
  align-items: center;
}

.storybook-hero .hero-card {
  width: 100%;
  max-width: 1000px;
  max-height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

.hero-start-wrap {
  margin-top: clamp(10px, 1.8dvh, 22px);
  display: flex;
  justify-content: center;
  align-items: center;
  flex-wrap: wrap;
  gap: clamp(10px, 1.8vw, 18px);
}

.hero-start-btn {
  background-color: var(--eos-gold);
  border-radius: 30px 16px 32px 18px;
  padding: 0.55rem 1.4rem;
  font-size: clamp(0.95rem, 1.2vw, 1.12rem);
}

.hero-card {
  position: relative;
  background: linear-gradient(165deg, #FFFDF8 0%, #FBF3DF 100%);
  border: var(--border-ink-thick);
  border-radius: 34px 16px 38px 18px;
  box-shadow: var(--shadow-cel-lg);
  padding: clamp(14px, 2.5dvh, 36px) clamp(16px, 4vw, 52px);
  text-align: center;
}

/* Hand-Drawn Double Corner Stitch Accent */
.hero-card::after {
  content: "";
  position: absolute;
  inset: 9px;
  border: 1.5px dashed rgba(38, 70, 83, 0.28);
  border-radius: 26px 10px 30px 12px;
  pointer-events: none;
}

.hero-crest-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  margin-bottom: clamp(0.25rem, 0.8dvh, 0.55rem);
  filter: drop-shadow(3px 4px 0px #264653);
}

.hero-crest-badge svg {
  width: clamp(54px, 8dvh, 84px);
  height: clamp(54px, 8dvh, 84px);
}

.hero-kicker {
  margin: 0 0 0.25rem;
  font-family: var(--font-display);
  font-size: clamp(0.76rem, 1vw, 0.95rem);
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.12em;
  color: var(--emerald-leaf);
}

.hero-title {
  margin: 0 0 clamp(0.35rem, 1dvh, 0.65rem);
  font-family: var(--font-display);
  font-size: clamp(1.85rem, 4.4vw, 3.6rem);
  font-weight: 700;
  line-height: 1.16;
  padding-bottom: 4px;
  color: var(--ink-slate);
  letter-spacing: -0.01em;
  text-shadow: 3px 3px 0px var(--eos-gold);
}

.hero-subtitle {
  margin: 0 auto clamp(0.5rem, 1.4dvh, 1rem);
  max-width: 60ch;
  font-size: clamp(0.88rem, 1.15vw, 1.12rem);
  line-height: 1.38;
  font-weight: 600;
  color: var(--ink-slate);
}

.hero-divider {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.85rem;
}

.divider-flourish {
  width: clamp(48px, 10vw, 110px);
  height: 3px;
  background-color: var(--ink-outline);
  border-radius: 99px;
}

/* ==========================================================================
   STORYBOOK SPINE & CHAPTER-BY-CHAPTER BOOK SPREAD ARCHITECTURE
   ========================================================================== */

.storybook-main {
  width: 100vw;
  height: 100dvh;
  max-width: 100vw;
  max-height: 100dvh;
  margin: 0;
  padding: 0;
  overflow: hidden;
  position: relative;
}

.book-page {
  width: 100vw;
  height: 100dvh;
  max-width: 100vw;
  max-height: 100dvh;
  box-sizing: border-box;
  overflow: hidden;
  position: relative;
  padding: clamp(56px, 7.2dvh, 72px) clamp(10px, 2.2vw, 28px) 98px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  backface-visibility: hidden;
  will-change: auto;
}

.book-page.is-active-page {
  will-change: transform, opacity;
}

.story-panel {
  position: relative;
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  transform-style: flat;
  width: 100vw;
  height: 100dvh;
  max-width: 100vw;
  max-height: 100dvh;
  gap: clamp(6px, 1dvh, 12px);
  padding: clamp(56px, 7.2dvh, 72px) clamp(10px, 2.2vw, 28px) 98px;
  background: transparent;
  border: none;
  border-radius: 0;
  box-shadow: none;
  overflow: hidden;
}

.story-panel::after {
  display: none;
}

/* Chapter Medallion & Panel Title */
.panel-header-row {
  flex: 0 0 auto;
  display: flex;
  flex-wrap: nowrap;
  align-items: center;
  gap: 0.65rem 0.9rem;
  min-height: 0;
}

.chapter-pill {
  flex-shrink: 0;
  display: inline-flex;
  align-items: center;
  padding: 0.24rem 0.78rem;
  background-color: var(--eos-gold);
  color: var(--ink-slate);
  font-family: var(--font-display);
  font-size: clamp(0.74rem, 0.95vw, 0.9rem);
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  border: var(--border-ink);
  border-radius: 20px 9px 22px 11px;
  box-shadow: 2px 3px 0px var(--ink-outline);
  transform: rotate(-1.2deg);
}

.chapter-pill--amber {
  background-color: var(--cinnamon-amber);
  transform: rotate(1.2deg);
}

.chapter-pill--emerald {
  background-color: var(--emerald-leaf);
  color: #FFFDF8;
}

.panel-heading {
  margin: 0;
  font-family: var(--font-display);
  font-size: clamp(1.05rem, 2vw, 1.75rem);
  font-weight: 700;
  line-height: 1.12;
  color: inherit;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  transition: color 0.55s ease, text-shadow 0.55s ease;
}

/* Panel Stage & Organic Artwork Frame (CSS Grid Stage) */
.panel-stage {
  flex: 1 1 0;
  min-height: 0;
  width: 100%;
  max-width: 1440px;
  margin: 0 auto;
  display: grid;
  grid-template-columns: minmax(0, 1.25fr) minmax(0, 0.95fr);
  grid-template-rows: minmax(0, 1fr);
  gap: clamp(10px, 1.5vw, 22px);
  align-items: stretch;
}

@media (max-width: 920px) {
  .panel-stage {
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: minmax(0, 42%) minmax(0, 58%);
    gap: clamp(8px, 1.2dvh, 12px);
  }
}

@media (max-width: 520px) {
  .panel-stage {
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: minmax(0, 36%) minmax(0, 64%);
    gap: 6px;
  }
}

.panel-artwork-frame {
  position: relative;
  min-height: 0;
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background-color: var(--parchment-card);
  padding: clamp(5px, 0.7vw, 10px);
  border: var(--border-ink-thick);
  border-radius: var(--radius-frame-odd);
  box-shadow: var(--shadow-cel), 0 12px 26px rgba(30, 45, 56, 0.12);
  overflow: hidden;
}

/*
 * Artwork letterbox backdrop. The 16:9 illustrations are fitted inside this
 * frame at their true aspect ratio by initArtworkFit() in app.js, so the
 * remaining frame area is filled with a blurred, darkened copy of the same
 * art instead of cropping the illustration itself.
 */
.panel-artwork-frame::before {
  content: "";
  position: absolute;
  inset: -24px;
  z-index: 0;
  background-image: var(--panel-art, none);
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  opacity: 0.55;
  transform: scale(1.06);
  pointer-events: none;
}

.story-panel:nth-child(even) .panel-artwork-frame {
  border-radius: var(--radius-frame-even);
}

.sunbeam-map-wrap {
  position: relative;
  z-index: 1;
  width: 100%;
  height: 100%;
  min-height: 0;
  flex: 0 0 auto;
}

.panel-image-viewport {
  position: relative;
  z-index: 1;
  width: 100%;
  height: 100%;
  max-width: 100%;
  max-height: 100%;
  min-height: 0;
  flex: 0 0 auto;
  overflow: hidden;
  /* Drawn as an inset outline (not a border) so it never steals layout space
     from the 16:9 artwork: with a 2px border on each side the fitted image
     content box was 326x180 instead of 330x184 at 390x844 (~1px clipped). */
  border: none;
  outline: 2px solid var(--ink-outline);
  outline-offset: -2px;
  border-radius: 18px 8px 20px 10px;
  background-color: var(--parchment-warm);
  box-shadow: 0 10px 24px rgba(20, 32, 44, 0.22);
}

.story-panel:nth-child(even) .panel-image-viewport {
  border-radius: 10px 22px 12px 24px;
}

.panel-illustration-picture {
  display: block;
  width: 100%;
  height: 100%;
}

.panel-illustration {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transform-origin: center center;
  will-change: transform;
}

/* Task B5: Word-by-word Narration Sync Reveal */
.narration-word {
  transition: background-color 0.16s ease, color 0.16s ease;
  border-radius: 4px;
  padding: 0 1px;
}

.narration-word.is-word-revealed {
  background-color: rgba(233, 196, 106, 0.34);
}

.narration-word.is-word-current {
  background-color: var(--eos-gold);
  color: #1E2D38;
  font-weight: 700;
}

body.is-nachtwald-realm .narration-word.is-word-revealed {
  background-color: rgba(168, 197, 218, 0.25);
}

body.is-nachtwald-realm .narration-word.is-word-current {
  background-color: var(--nachtwald-frost);
  color: #0F1A24;
}

/* ==========================================================================
   STORY NARRATION PLATES & STORY BEATS DECK
   ========================================================================== */

.panel-story-column,
.panel-dialogue-grid {
  min-height: 0;
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  gap: clamp(8px, 1.2dvh, 14px);
}

.narration-plate {
  position: relative;
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  gap: clamp(8px, 1.2dvh, 14px);
  background-color: var(--parchment-card);
  border: var(--border-ink);
  border-radius: var(--radius-plate);
  box-shadow: var(--shadow-cel);
  padding: clamp(12px, 1.8dvh, 22px) clamp(14px, 2vw, 24px);
}

.narration-plate::before {
  content: "";
  position: absolute;
  top: 10px;
  left: 10px;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background-color: var(--eos-gold);
  border: 2px solid var(--ink-outline);
}

.story-beats-deck {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  align-items: center;
}

.story-prose {
  margin: 0;
  padding-left: 0.35rem;
  font-family: var(--font-prose);
  font-size: clamp(0.88rem, 1.12vw, 1.08rem);
  font-weight: 600;
  line-height: clamp(1.42, 1.9dvh, 1.62);
  color: var(--ink-slate);
  max-width: 68ch;
}

.beat-cycle-btn {
  background-color: #FFFDF8;
  border-radius: 22px 14px 24px 16px;
}

/* ==========================================================================
   MANGA SPEECH BUBBLES & THOUGHT CLOUDS (ANGLED SVG TAILS)
   ========================================================================== */

.manga-speech-bubble,
.manga-thought-cloud {
  position: relative;
  flex: 0 0 auto;
  background-color: #FFFDF7;
  border: var(--border-ink);
  box-shadow: var(--shadow-cel);
  padding: clamp(10px, 1.4dvh, 16px) clamp(12px, 1.8vw, 20px);
  margin-top: 8px;
}

.manga-speech-bubble {
  border-radius: var(--radius-bubble);
}

.manga-thought-cloud {
  border-radius: var(--radius-cloud);
  background-color: #FDFBF4;
  border-style: solid;
}

.manga-thought-cloud--golden {
  background-color: #FFF9E6;
}

/* Crisp SVG Speech Tail Joined Seamlessly to Top Bubble Rim */
.speech-tail {
  position: absolute;
  top: -21px;
  left: 24px;
  width: 36px;
  height: 23px;
  pointer-events: none;
  overflow: visible;
}

.thought-trail {
  position: absolute;
  top: -24px;
  left: 20px;
  width: 46px;
  height: 26px;
  pointer-events: none;
}

.speaker-header {
  display: flex;
  align-items: center;
  gap: 0.45rem;
  margin-bottom: 0.25rem;
}

.speaker-icon-wrap {
  display: inline-flex;
  flex-shrink: 0;
}

.speaker-name {
  font-family: var(--font-display);
  font-size: clamp(0.84rem, 1vw, 0.98rem);
  font-weight: 700;
  color: var(--ink-slate);
  letter-spacing: 0.01em;
}

.dialogue-quote {
  margin: 0;
  font-family: var(--font-display);
  font-size: clamp(0.86rem, 1.05vw, 1.02rem);
  font-weight: 600;
  line-height: 1.38;
  color: var(--ink-slate);
}

/* ==========================================================================
   PANEL 2 EXCLUSIVE REALM STYLING: NACHTWALD FROST & ICE
   ========================================================================== */

.story-panel--nachtwald {
  background: transparent;
  border: none;
  border-radius: 0;
  box-shadow: none;
}

.chapter-pill--frost {
  background-color: var(--nachtwald-frost);
  color: var(--nachtwald-ice);
  border-color: var(--nachtwald-ice);
  box-shadow: 3px 4px 0px var(--nachtwald-ice);
  transition: border-color 0.55s ease, box-shadow 0.55s ease, background-color 0.55s ease, color 0.55s ease;
}

.panel-artwork-frame--frost {
  background-color: var(--nachtwald-pale);
  border-color: var(--nachtwald-ice);
  box-shadow: 6px 7px 0px var(--nachtwald-ice), 0 18px 34px rgba(43, 61, 79, 0.2);
  transition: border-color 0.55s ease, box-shadow 0.55s ease, background-color 0.55s ease;
}

.panel-artwork-frame--frost .panel-image-viewport {
  outline-color: var(--nachtwald-ice);
  transition: outline-color 0.55s ease;
}

.narration-plate--frost {
  background-color: #F2F7FB;
  border-color: var(--nachtwald-ice);
  box-shadow: 4px 5px 0px var(--nachtwald-ice);
  transition: border-color 0.55s ease, box-shadow 0.55s ease, background-color 0.55s ease;
}

.narration-plate--frost::before {
  background-color: var(--nachtwald-frost);
  border-color: var(--nachtwald-ice);
}

.manga-speech-bubble--frost {
  background-color: var(--nachtwald-pale);
  border-color: var(--nachtwald-ice);
  box-shadow: 4px 5px 0px var(--nachtwald-ice);
  transition: border-color 0.55s ease, box-shadow 0.55s ease, background-color 0.55s ease;
}

.manga-speech-bubble--frost .speaker-name,
.manga-speech-bubble--frost .dialogue-quote {
  color: var(--nachtwald-ice);
}

/* Active Frostbite Slate Atmosphere Overrides (Triggered via GSAP ScrollTrigger) */
body.is-nachtwald-realm .panel-heading {
  color: #EAF4FC;
  text-shadow: 2px 3px 0px #0F1A24;
}

body.is-nachtwald-realm .story-panel--nachtwald {
  background: transparent;
  border: none;
  box-shadow: none;
}

body.is-nachtwald-realm .chapter-pill--frost {
  background-color: #1D2D3A;
  color: #EAF4FC;
  border-color: var(--nachtwald-frost);
  box-shadow: 3px 4px 0px #0F1A24;
}

body.is-nachtwald-realm .panel-artwork-frame--frost {
  background-color: #243748;
  border-color: var(--nachtwald-frost);
  box-shadow: 6px 8px 0px #0F1A24, 0 20px 40px rgba(8, 15, 22, 0.45);
}

body.is-nachtwald-realm .panel-artwork-frame--frost .panel-image-viewport {
  outline-color: var(--nachtwald-frost);
}

body.is-nachtwald-realm .narration-plate--frost {
  background-color: #E6F0F8;
  border-color: var(--nachtwald-frost);
  box-shadow: 4px 6px 0px #0F1A24;
}

body.is-nachtwald-realm .manga-speech-bubble--frost {
  background-color: #DCEAF5;
  border-color: var(--nachtwald-frost);
  box-shadow: 4px 6px 0px #0F1A24;
}

body.is-nachtwald-realm .manga-speech-bubble--frost .speech-tail path:last-child {
  stroke: var(--nachtwald-frost);
}

body.is-nachtwald-realm .storybook-main .panel-artwork-frame:not(.panel-artwork-frame--frost),
body.is-nachtwald-realm .storybook-main .narration-plate:not(.narration-plate--frost) {
  border-color: var(--nachtwald-frost);
}

/* ==========================================================================
   PANEL 4 MANGA SFX RIBBON BADGES (ANGLED CEL-SHADED COMIC STRIPS)
   ========================================================================== */

.sfx-ribbon-strip {
  position: absolute;
  left: 8px;
  right: 8px;
  bottom: 8px;
  z-index: 8;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: 0.45rem 0.65rem;
  padding: 0;
  pointer-events: none;
}

.sfx-badge {
  pointer-events: auto;
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.32rem 0.72rem 0.32rem 0.55rem;
  border: var(--border-ink);
  border-radius: 18px 8px 20px 10px;
  box-shadow: var(--shadow-cel);
  font-family: var(--font-display);
  font-size: clamp(0.74rem, 0.95vw, 0.92rem);
  font-weight: 700;
  letter-spacing: 0.03em;
  color: var(--ink-slate);
  transform: skewX(-6deg) rotate(-1.5deg);
  transition: transform 0.24s var(--ease-spring), box-shadow 0.24s var(--ease-spring);
}

.sfx-badge:nth-child(even) {
  border-radius: 10px 22px 8px 20px;
  transform: skewX(-6deg) rotate(1.6deg);
}

.sfx-badge:hover {
  transform: skewX(-4deg) translateY(-3px) scale(1.04);
  box-shadow: 6px 7px 0px var(--ink-outline);
}

.sfx-badge--gold {
  background-color: var(--eos-gold);
}

.sfx-badge--emerald {
  background-color: var(--emerald-soft);
}

.sfx-badge--sky {
  background-color: #FFF4D2;
}

.sfx-badge--cinnamon {
  background-color: var(--cinnamon-amber);
}

.sfx-text {
  display: inline-block;
  transform: skewX(6deg);
}

/* ==========================================================================
   STORYBOOK FOOTER COLOPHON
   ========================================================================== */

.storybook-footer {
  display: none;
}

.footer-ornament-card {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: 0.85rem;
  padding: 1.15rem 1.6rem;
  background-color: var(--parchment-card);
  border: var(--border-ink);
  border-radius: 28px 14px 30px 16px;
  box-shadow: var(--shadow-cel);
  text-align: center;
}

.footer-seal {
  display: inline-flex;
}

.footer-text {
  margin: 0;
  font-family: var(--font-display);
  font-size: clamp(0.92rem, 1.1vw, 1.04rem);
  font-weight: 600;
  color: var(--ink-slate);
}

/* ==========================================================================
   MICRO-TASK 4: CAST & WORLD GUIDE ASYMMETRIC STORYBOOK DRAWER
   ========================================================================== */

.cast-backdrop {
  position: fixed;
  inset: 0;
  z-index: 955;
  background-color: rgba(24, 38, 50, 0.72);
  opacity: 0;
  transition: opacity 0.3s ease;
}

.cast-backdrop.is-open {
  opacity: 1;
}

.cast-drawer {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  width: min(650px, 95vw);
  max-width: 100vw;
  z-index: 960;
  display: flex;
  flex-direction: column;
  background: linear-gradient(175deg, #FDFBF4 0%, #F6ECDA 100%);
  color: var(--ink-slate);
  border-left: 3.5px solid var(--ink-outline);
  border-top: 2.5px solid var(--ink-outline);
  border-bottom: 2.5px solid var(--ink-outline);
  border-radius: 36px 0 0 28px;
  box-shadow: -8px 0 0 var(--ink-outline), -24px 0 54px rgba(18, 29, 38, 0.38);
  overflow-y: auto;
  overflow-x: hidden;
  transform: translateX(100%);
  transition: transform 0.38s var(--ease-spring);
}

.cast-drawer.is-open {
  transform: translateX(0);
}

.cast-drawer-header {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: clamp(16px, 2.4vw, 24px) clamp(18px, 3vw, 28px);
  background: rgba(253, 251, 244, 0.96);
  border-bottom: 2.5px dashed rgba(38, 70, 83, 0.25);
}

.cast-drawer-heading-group {
  display: flex;
  flex-direction: column;
  gap: 0.2rem;
}

.cast-drawer-kicker {
  font-family: var(--font-display);
  font-size: 0.8rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.09em;
  color: var(--emerald-leaf);
}

.cast-drawer-title {
  margin: 0;
  font-family: var(--font-display);
  font-size: clamp(1.38rem, 2.2vw, 1.85rem);
  font-weight: 700;
  line-height: 1.15;
  color: var(--ink-slate);
}

.cast-drawer-body {
  display: flex;
  flex-direction: column;
  gap: 1.25rem;
  padding: clamp(18px, 2.6vw, 26px) clamp(18px, 3vw, 28px) clamp(28px, 4vw, 40px);
}

/* Reference Sheet Stage & Character Spotlight Overlay */
.cast-sheet-frame {
  background-color: var(--parchment-card);
  padding: 8px;
  border: var(--border-ink-thick);
  border-radius: 26px 12px 28px 14px;
  box-shadow: var(--shadow-cel);
}

.cast-sheet-viewport {
  position: relative;
  width: 100%;
  aspect-ratio: 2752 / 1536;
  overflow: hidden;
  border: 2px solid var(--ink-outline);
  border-radius: 18px 8px 20px 10px;
  background-color: #FAF4E6;
}

.cast-sheet-preview {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.cast-sheet-highlight {
  position: absolute;
  pointer-events: none;
  border: 3.5px solid var(--eos-gold);
  outline: 2.5px solid var(--ink-outline);
  border-radius: 20px 10px 22px 12px;
  box-shadow:
    0 0 0 999px rgba(24, 38, 50, 0.36),
    inset 0 0 0 2px rgba(255, 255, 255, 0.65);
  transition:
    left 0.42s var(--ease-spring),
    top 0.42s var(--ease-spring),
    width 0.42s var(--ease-spring),
    height 0.42s var(--ease-spring);
}

/* Character Filter Tab Strip */
.cast-tabs-bar {
  display: flex;
  flex-wrap: wrap;
  gap: 0.65rem;
}

.cast-tab-btn {
  padding: 0.55rem 1rem;
  font-size: clamp(0.86rem, 1vw, 0.96rem);
  background-color: var(--parchment-card);
  border-radius: 24px 12px 26px 14px;
}

.cast-tab-btn.is-active,
.cast-tab-btn[aria-selected="true"] {
  background-color: var(--eos-gold);
  color: var(--ink-slate);
}

.cast-tab-btn[data-cast-id="morwenna"].is-active,
.cast-tab-btn[data-cast-id="philip"].is-active {
  background-color: var(--nachtwald-frost);
  color: var(--nachtwald-ice);
}

/* Character Bio Parchment Plate */
.cast-dossier-card {
  background-color: var(--parchment-card);
  border: var(--border-ink);
  border-radius: 26px 14px 30px 16px;
  box-shadow: var(--shadow-cel);
  padding: 1.2rem 1.4rem 1.35rem;
}

.cast-dossier-meta {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.6rem;
  margin-bottom: 0.55rem;
}

.cast-realm-badge {
  display: inline-flex;
  align-items: center;
  padding: 0.25rem 0.8rem;
  background-color: var(--emerald-soft);
  color: var(--ink-slate);
  border: 2px solid var(--ink-outline);
  border-radius: 18px 8px 18px 10px;
  font-family: var(--font-display);
  font-size: 0.82rem;
  font-weight: 700;
}

.cast-role-tag {
  font-family: var(--font-display);
  font-size: 0.88rem;
  font-weight: 600;
  color: var(--ink-outline);
}

.cast-profile-name {
  margin: 0 0 0.45rem;
  font-family: var(--font-display);
  font-size: clamp(1.3rem, 2vw, 1.6rem);
  font-weight: 700;
  color: var(--ink-slate);
}

.cast-profile-bio {
  margin: 0;
  font-family: var(--font-prose);
  font-size: clamp(0.98rem, 1.15vw, 1.08rem);
  font-weight: 600;
  line-height: 1.66;
  color: var(--ink-slate);
}

/* ==========================================================================
   MICRO-TASK 5: READ-ALOUD NARRATOR BUTTONS & ACTIVE SPEAKING BORDERS
   ========================================================================== */

.narration-actions {
  margin-top: 0;
  display: flex;
  flex-wrap: nowrap;
  align-items: center;
  gap: clamp(6px, 1vw, 12px);
}

.narration-actions .tactile-btn {
  flex: 1 1 0;
  padding: 0.45rem 0.75rem;
  font-size: clamp(0.78rem, 0.95vw, 0.95rem);
}

.read-aloud-btn {
  padding: 0.55rem 1.2rem;
  background-color: #FFF9E6;
  border-radius: 26px 14px 28px 16px;
}

.read-aloud-btn--frost {
  background-color: #E6F0F8;
  color: var(--nachtwald-ice);
  border-color: var(--nachtwald-ice);
  --btn-lip: #2B3D4F;
}

.read-aloud-btn .read-icon-stop {
  display: none;
}

.read-aloud-btn.is-reading {
  background-color: var(--cinnamon-amber);
  color: var(--ink-slate);
}

.read-aloud-btn.is-reading .read-icon-play {
  display: none;
}

.read-aloud-btn.is-reading .read-icon-stop {
  display: block;
}

/* Active Speaking Panel & Narration Plate Cel-Shaded Highlight (Zero Neon) */
.story-panel.is-speaking .panel-artwork-frame {
  border-color: var(--cinnamon-amber);
  outline: 4px solid var(--eos-gold);
  outline-offset: 3px;
}

.narration-plate.is-speaking {
  background-color: #FFF9E6;
  border-color: var(--cinnamon-amber);
  outline: 3.5px solid var(--eos-gold);
  outline-offset: 2px;
}

.story-panel--nachtwald.is-speaking .panel-artwork-frame--frost,
.narration-plate--frost.is-speaking {
  border-color: var(--nachtwald-frost);
  outline: 3.5px solid #EAF4FC;
}

/* Interactive SFX Ribbon Badges */
.sfx-badge[data-sfx] {
  cursor: pointer;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

.sfx-badge[data-sfx]:focus-visible {
  outline: 3px dashed var(--cinnamon-amber);
  outline-offset: 4px;
}

/* ==========================================================================
   CHAPTER-BY-CHAPTER PAGINATOR DOCK (PREVIOUS / STEP PILLS / NEXT)
   ========================================================================== */

.book-paginator {
  position: fixed;
  bottom: 6px;
  left: clamp(6px, 1.5vw, 24px);
  right: clamp(6px, 1.5vw, 24px);
  z-index: 910;
  max-width: 1360px;
  height: 86px;
  margin: 0 auto;
  display: flex;
  flex-wrap: nowrap;
  align-items: center;
  justify-content: space-between;
  gap: clamp(6px, 1vw, 14px);
  padding: 6px clamp(8px, 1.4vw, 18px);
  background: linear-gradient(165deg, #FFFDF8 0%, #FBF2DC 100%);
  border: var(--border-ink-thick);
  border-radius: 26px 14px 28px 16px;
  box-shadow: var(--shadow-cel);
  box-sizing: border-box;
  transition: background 0.55s ease, border-color 0.55s ease, box-shadow 0.55s ease;
}

.paginator-controls-left,
.paginator-controls-right {
  display: flex;
  align-items: center;
  gap: clamp(4px, 0.7vw, 10px);
  flex-shrink: 0;
}

.paginator-btn {
  padding: 0.3rem clamp(0.45rem, 0.9vw, 0.85rem);
  gap: 0.35rem;
}

.paginator-btn--prev {
  background-color: #FFFDF8;
  border-radius: 24px 12px 26px 14px;
}

.paginator-btn--play {
  background-color: #FFF7DC;
  border-radius: 18px 24px 16px 22px;
}

.paginator-btn--play .autoplay-icon-pause {
  display: none;
}

.paginator-btn--play.is-playing {
  background-color: var(--cinnamon-amber);
}

.paginator-btn--play.is-playing .autoplay-icon-play {
  display: none;
}

.paginator-btn--play.is-playing .autoplay-icon-pause {
  display: block;
}

.paginator-btn--mute {
  background-color: #FFFDF8;
  border-radius: 22px 16px 24px 18px;
}

.paginator-btn--mute .bottom-audio-mute-x {
  display: none;
}

.paginator-btn--mute[data-audio-state="off"] {
  background-color: var(--parchment-warm);
}

.paginator-btn--mute[data-audio-state="off"] .bottom-audio-wave {
  display: none;
}

.paginator-btn--mute[data-audio-state="off"] .bottom-audio-mute-x {
  display: block;
}

.paginator-btn--next {
  background-color: var(--eos-gold);
  border-radius: 14px 26px 12px 24px;
}

.paginator-btn-label {
  white-space: nowrap;
  font-size: clamp(0.75rem, 0.9vw, 0.92rem);
}

@media (max-width: 960px) {
  .paginator-btn-label {
    display: none;
  }
}

.book-paginator-center {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  justify-content: center;
  gap: 5px;
  flex: 1 1 auto;
  min-width: 0;
  max-width: 560px;
}

.book-paginator-meta {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
}

.book-page-indicator {
  flex-shrink: 0;
  font-family: var(--font-display);
  font-size: clamp(0.72rem, 0.88vw, 0.86rem);
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--ink-slate);
  white-space: nowrap;
  transition: color 0.55s ease;
}

.book-progress-track {
  flex: 1 1 auto;
  height: 10px;
  background-color: var(--parchment-warm);
  border: 2px solid var(--ink-outline);
  border-radius: 99px;
  overflow: hidden;
  box-shadow: inset 0 2px 3px rgba(30, 45, 56, 0.15);
}

.book-progress-fill {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, var(--emerald-leaf) 0%, var(--eos-gold) 65%, var(--cinnamon-amber) 100%);
  border-radius: 99px;
  transition: width 0.42s var(--ease-spring);
}

.chapter-dots-row {
  display: flex;
  flex-wrap: nowrap;
  align-items: center;
  justify-content: space-between;
  gap: 4px;
  width: 100%;
}

.chapter-dot-btn {
  --btn-lip: #264653;
  flex: 1 1 0;
  min-width: 0;
  height: 28px;
  padding: 0 6px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 2px solid var(--ink-outline);
  border-radius: 12px 7px 13px 8px;
  background-color: #FFFDF8;
  color: var(--ink-slate);
  font-family: var(--font-display);
  font-size: clamp(0.7rem, 0.82vw, 0.82rem);
  font-weight: 700;
  cursor: pointer;
  user-select: none;
  box-shadow: 0 3px 0 var(--btn-lip), inset 0 2px 0 rgba(255, 255, 255, 0.5);
  transition: transform 0.2s var(--ease-spring), background-color 0.2s ease, box-shadow 0.2s var(--ease-spring);
}

.chapter-dot-btn:hover {
  transform: translateY(-1px);
}

.chapter-dot-btn:active {
  transform: translateY(2px);
  box-shadow: 0 1px 0 var(--btn-lip);
}

.chapter-dot-btn.is-active {
  background-color: var(--cinnamon-amber);
  color: var(--ink-slate);
  box-shadow: 0 3px 0 var(--btn-lip), inset 0 2px 0 rgba(255, 255, 255, 0.65);
}

/* Nachtwald Frostbite Slate Paginator Overrides */
body.is-nachtwald-realm .book-paginator {
  background: linear-gradient(165deg, rgba(36, 55, 72, 0.94) 0%, rgba(25, 39, 52, 0.96) 100%);
  border-color: var(--nachtwald-frost);
  box-shadow: 6px 8px 0px #0F1A24;
}

body.is-nachtwald-realm .book-page-indicator {
  color: #EAF4FC;
}

body.is-nachtwald-realm .chapter-dot-btn:not(.is-active),
body.is-nachtwald-realm .paginator-btn--prev {
  background-color: #E6F0F8;
  color: var(--nachtwald-ice);
  border-color: var(--nachtwald-ice);
}

body.is-nachtwald-realm .chapter-dot-btn.is-active,
body.is-nachtwald-realm .paginator-btn--next {
  background-color: var(--nachtwald-frost);
  color: #0F1A24;
  border-color: #0F1A24;
}

/* ==========================================================================
   RESPONSIVE MOBILE & TABLET REFINEMENTS
   ========================================================================== */

@media (max-width: 680px) {
  /* The motion toggle collapses to its icon so the HUD keeps fitting. */
  .hud-btn--motion .hud-btn-label {
    display: none;
  }

  .panel-header-row {
    flex-wrap: wrap;
    gap: 0.25rem 0.5rem;
  }

  .panel-heading {
    white-space: normal;
    font-size: clamp(0.78rem, 3.2vw, 1.05rem);
    line-height: 1.18;
    overflow: visible;
    text-overflow: clip;
  }

  .floating-hud {
    top: 10px;
    right: 10px;
    left: 10px;
    justify-content: flex-end;
    gap: 8px;
  }

  .hud-btn {
    padding: 0.5rem 0.8rem;
    gap: 0.45rem;
    font-size: 0.84rem;
  }

  .hud-btn .cel-icon {
    width: 32px;
    height: 32px;
  }

  .book-page,
  .story-panel {
    padding-bottom: 154px;
  }

  .book-paginator {
    height: auto;
    flex-wrap: wrap;
    align-content: flex-start;
    padding: 6px 8px 8px;
    gap: 5px;
  }

  .book-paginator-center {
    order: -1;
    flex: 0 0 100%;
    max-width: 100%;
    gap: 3px;
  }

  .paginator-controls-left,
  .paginator-controls-right {
    gap: 6px;
  }

  .paginator-btn {
    padding: 0.25rem;
  }

  .hero-card::after {
    inset: 6px;
  }

  .sfx-ribbon-strip {
    justify-content: flex-start;
    gap: 0.65rem;
  }

  .sfx-badge {
    padding: 0.48rem 0.85rem;
    font-size: 0.86rem;
  }
}

@media (max-width: 420px) {
  .hud-btn-label {
    font-size: 0.78rem;
  }

  .hud-btn {
    padding: 0.45rem 0.65rem;
  }
}

/* ==========================================================================
   ACCESSIBILITY: REDUCED MOTION COMPLIANCE
   ========================================================================== */

@media (prefers-reduced-motion: reduce) {
  /* `data-motion="full"` is the reader's explicit opt-in, so it outranks the
     operating system preference. */
  html:not([data-motion='full']) {
    scroll-behavior: auto;
  }

  html:not([data-motion='full']) *,
  html:not([data-motion='full']) *::before,
  html:not([data-motion='full']) *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }

  html:not([data-motion='full']) .tactile-btn:hover,
  html:not([data-motion='full']) .tactile-btn:active,
  html:not([data-motion='full']) .sfx-badge:hover {
    transform: none;
  }
}

/* ==========================================================================
   TASK E4: THE END SCREEN & CELEBRATION STAGE
   ========================================================================== */
.end-screen-stage,
.end-screen-stage.story-panel,
.end-screen-stage.book-page {
  position: relative !important;
  width: 100vw !important;
  height: 100vh !important;
  height: 100dvh !important;
  max-width: 100vw !important;
  max-height: 100dvh !important;
  overflow: hidden !important;
  box-sizing: border-box !important;
  display: flex !important;
  flex-direction: column !important;
  align-items: center !important;
  justify-content: center !important;
  padding: clamp(8px, 1.8vh, 20px) clamp(12px, 3vw, 32px) clamp(64px, 8vh, 92px) !important;
  background:
    radial-gradient(110% 80% at 50% 4%, rgba(255, 196, 110, 0.28) 0%, rgba(255, 140, 90, 0.10) 38%, transparent 70%),
    radial-gradient(90% 70% at 50% 110%, rgba(124, 96, 255, 0.32) 0%, transparent 62%),
    linear-gradient(180deg, #150B2E 0%, #1C103C 35%, #27164F 70%, #321A60 100%) !important;
  color: #FFFDF8 !important;
  font-family: ui-rounded, 'SF Pro Rounded', 'Hiragino Maru Gothic ProN', 'Quicksand', 'Nunito', system-ui, sans-serif !important;
  z-index: 1 !important;
}

.end-screen-stage[hidden] {
  display: none !important;
}

/* Dynamic Confetti Canvas */
.end-confetti-canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 4;
}

/* Ambient Celebration Auroras */
.end-aurora {
  position: absolute;
  inset: -15%;
  z-index: 1;
  pointer-events: none;
  background:
    radial-gradient(35% 28% at 24% 28%, rgba(255, 186, 120, 0.24), transparent 70%),
    radial-gradient(32% 26% at 76% 60%, rgba(100, 220, 210, 0.18), transparent 70%),
    radial-gradient(38% 30% at 50% 88%, rgba(180, 130, 255, 0.22), transparent 72%);
  will-change: transform;
  animation: endAuroraDrift 22s ease-in-out infinite alternate;
}

@keyframes endAuroraDrift {
  from { transform: scale(1) translate3d(-1%, -1%, 0); }
  to   { transform: scale(1.05) translate3d(1%, 2%, 0); }
}

/* Decorative Sparkles Layer (Contained & Overflow Hidden to prevent 390px star bleed) */
.end-sparkles-layer {
  position: absolute;
  inset: 0;
  z-index: 2;
  pointer-events: none;
  overflow: hidden;
  contain: paint;
}

.end-spark {
  position: absolute;
  width: clamp(18px, 4vmin, 32px);
  max-width: 36px;
  max-height: 36px;
  height: auto;
  opacity: 0.75;
  filter: drop-shadow(0 0 10px rgba(255, 200, 100, 0.55));
  animation: endSparkleTwinkle 4.5s ease-in-out infinite;
  display: block;
}

.end-spark--1 { top: 8%; left: 8%; animation-delay: 0s; }
.end-spark--2 { top: 12%; right: 10%; width: clamp(16px, 3vmin, 26px); animation-delay: -1.6s; }
.end-spark--3 { bottom: 18%; left: 10%; width: clamp(14px, 2.5vmin, 22px); animation-delay: -3.1s; }

@keyframes endSparkleTwinkle {
  0%, 100% { opacity: 0.25; transform: scale(0.9) rotate(-8deg); }
  50%      { opacity: 0.85; transform: scale(1.1) rotate(8deg); }
}

/* Stage Inner Content Container */
.end-stage-content {
  position: relative;
  z-index: 3;
  width: min(880px, 92vw);
  max-height: calc(100dvh - 90px);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: clamp(6px, 1.6vh, 18px);
  text-align: center;
  margin: auto;
  box-sizing: border-box;
}

/* Climax Header */
.end-header {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: clamp(3px, 0.8vh, 8px);
}

.end-kicker {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.75rem;
  font-size: clamp(0.68rem, 1vmin + 0.25rem, 0.85rem);
  font-weight: 800;
  letter-spacing: 0.22em;
  text-transform: uppercase;
  color: rgba(255, 228, 170, 0.88);
}

.end-kicker-line {
  display: block;
  width: clamp(20px, 4.5vmin, 48px);
  height: 2px;
  border-radius: 2px;
  background: linear-gradient(90deg, transparent, rgba(255, 214, 120, 0.85));
}

.end-kicker-line:last-child {
  transform: scaleX(-1);
}

.end-title {
  margin: 0;
  font-family: 'Cinzel Decorative', Georgia, 'Times New Roman', serif;
  font-size: clamp(2.2rem, 7.5vmin, 4.8rem);
  line-height: 0.98;
  font-weight: 700;
  letter-spacing: 0.02em;
  background: linear-gradient(180deg, #FFFFFF 0%, #FFE9B3 34%, #F4A261 74%, #E76F51 100%);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  -webkit-text-fill-color: transparent;
  filter:
    drop-shadow(0 4px 0 rgba(70, 24, 0, 0.55))
    drop-shadow(0 14px 28px rgba(255, 170, 70, 0.35));
}

.end-sub {
  margin: 0;
  max-width: 38ch;
  font-size: clamp(0.82rem, 1.25vmin + 0.38rem, 1.08rem);
  line-height: 1.35;
  color: rgba(255, 246, 230, 0.9);
  font-style: italic;
  text-wrap: balance;
}

/* Character Celebration Avatar Row */
.end-avatar-row {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: clamp(8px, 2vw, 18px);
  margin-top: clamp(2px, 0.6vh, 8px);
}

.end-avatar-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
}

.end-avatar-frame {
  display: grid;
  place-items: center;
  width: clamp(40px, 5.8vmin, 54px);
  height: clamp(40px, 5.8vmin, 54px);
  border-radius: 50%;
  box-shadow:
    0 4px 0 rgba(0, 0, 0, 0.35),
    0 8px 16px rgba(0, 0, 0, 0.4);
  background: #FFFDF8;
  padding: 2px;
  transition: transform 0.2s cubic-bezier(0.34, 1.56, 0.64, 1);
}

.end-avatar-frame:hover {
  transform: translateY(-3px) scale(1.08);
}

.end-avatar-svg {
  width: 100%;
  height: 100%;
  display: block;
}

.end-avatar-name {
  font-size: clamp(0.66rem, 0.9vmin + 0.28rem, 0.8rem);
  font-weight: 800;
  color: #FFE6A8;
  text-shadow: 0 1px 3px rgba(0, 0, 0, 0.7);
}

/* Tactile 3D Action Buttons (2026 Kids Standard) */
.end-actions-group {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: clamp(8px, 1.6vw, 16px);
  width: 100%;
  max-width: 800px;
}

.end-action-btn {
  --btn-lip: #A85A14;
  min-height: 64px;
  min-width: 64px;
  border-radius: 22px;
  padding: 0.65rem clamp(1rem, 2vw, 1.5rem);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.6rem;
  border: 3px solid #264653;
  color: #264653;
  font-family: inherit;
  font-size: clamp(0.88rem, 1vmin + 0.52rem, 1.08rem);
  font-weight: 900;
  letter-spacing: 0.02em;
  box-shadow:
    inset 0 4px 0 rgba(255, 255, 255, 0.55),
    0 8px 0 var(--btn-lip),
    0 14px 22px rgba(0, 0, 0, 0.35);
  cursor: pointer;
  transform: translateY(0);
  transition:
    transform 0.12s cubic-bezier(0.34, 1.56, 0.64, 1),
    box-shadow 0.12s ease,
    filter 0.15s ease;
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
}

.end-action-btn:hover {
  filter: brightness(1.06);
}

.end-action-btn:active {
  transform: translateY(6px) scale(0.98);
  box-shadow:
    inset 0 2px 0 rgba(255, 255, 255, 0.35),
    0 2px 0 var(--btn-lip),
    0 6px 10px rgba(0, 0, 0, 0.28);
}

.end-action-btn .btn-icon-wrap {
  width: 30px;
  height: 30px;
  flex-shrink: 0;
  display: grid;
  place-items: center;
}

.end-action-btn .cel-icon {
  width: 30px;
  height: 30px;
}

.end-action-btn--replay {
  --btn-lip: #A85A14;
  background: linear-gradient(180deg, #FFF0C2 0%, #F4A261 55%, #E76F51 100%);
  color: #264653;
}

.end-action-btn--chapters {
  --btn-lip: #145950;
  background: linear-gradient(180deg, #D4F4F0 0%, #2A9D8F 55%, #1B6E64 100%);
  color: #FFFDF8;
}

.end-action-btn--cover {
  --btn-lip: #21153D;
  background: linear-gradient(180deg, #6C5B9E 0%, #483973 55%, #332657 100%);
  color: #FFFDF8;
}

/* Chapter Select Modal */
.chapter-select-modal {
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: grid;
  place-items: center;
  padding: clamp(10px, 2.5vmin, 24px);
}

.chapter-select-modal[hidden] {
  display: none !important;
}

.chapter-select-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(12, 7, 28, 0.88);
  animation: modalFadeIn 0.2s ease-out both;
}

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

.chapter-select-card {
  position: relative;
  z-index: 2;
  width: min(640px, 94vw);
  max-height: calc(100dvh - 36px);
  display: flex;
  flex-direction: column;
  gap: clamp(8px, 1.5vh, 16px);
  padding: clamp(12px, 2.2vmin, 20px);
  border-radius: 28px;
  background:
    radial-gradient(120% 90% at 50% 0%, rgba(255, 200, 120, 0.12), transparent 60%),
    linear-gradient(180deg, #2A1D4E 0%, #1A1133 100%);
  border: 3px solid rgba(255, 214, 120, 0.35);
  box-shadow:
    inset 0 2px 0 rgba(255, 255, 255, 0.2),
    0 24px 64px rgba(0, 0, 0, 0.8);
  overflow-y: auto;
  overscroll-behavior: contain;
  scrollbar-width: none;
  animation: modalPopIn 0.25s cubic-bezier(0.34, 1.56, 0.64, 1) both;
}

.chapter-select-card::-webkit-scrollbar {
  display: none;
}

@keyframes modalPopIn {
  from { opacity: 0; transform: translateY(16px) scale(0.96); }
  to   { opacity: 1; transform: translateY(0) scale(1); }
}

.chapter-select-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.chapter-select-title {
  margin: 0;
  font-family: 'Cinzel Decorative', Georgia, serif;
  font-size: clamp(1.1rem, 2.2vmin + 0.3rem, 1.5rem);
  font-weight: 700;
  color: #FFE6A8;
  letter-spacing: 0.02em;
}

.chapter-select-close-btn {
  min-height: 44px;
  min-width: 44px;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  padding: 0;
  display: grid;
  place-items: center;
  background: #FFFDF8;
  border: 2.5px solid #264653;
  box-shadow: 0 4px 0 #182C35;
  cursor: pointer;
}

.chapter-select-close-btn:active {
  transform: translateY(3px);
  box-shadow: 0 1px 0 #182C35;
}

.chapter-select-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: clamp(8px, 1.2vh, 12px);
}

@media (max-width: 540px) {
  .chapter-select-grid {
    grid-template-columns: 1fr;
  }
}

.chapter-card-btn {
  --btn-lip: #9C7736;
  min-height: 60px;
  border-radius: 18px;
  padding: 8px 12px;
  display: flex;
  align-items: center;
  gap: 10px;
  background: linear-gradient(180deg, #FFF7DC 0%, #F4E2B2 100%);
  border: 2.5px solid #264653;
  box-shadow:
    inset 0 2px 0 rgba(255, 255, 255, 0.6),
    0 5px 0 var(--btn-lip),
    0 8px 14px rgba(0, 0, 0, 0.25);
  cursor: pointer;
  text-align: left;
  transform: translateY(0);
  transition: transform 0.12s cubic-bezier(0.34, 1.56, 0.64, 1);
}

.chapter-card-btn:hover {
  filter: brightness(1.05);
  transform: translateY(-2px);
}

.chapter-card-btn:active {
  transform: translateY(4px);
  box-shadow: 0 1px 0 var(--btn-lip);
}

.chapter-card-num {
  flex-shrink: 0;
  display: grid;
  place-items: center;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: linear-gradient(180deg, #3A2472 0%, #241546 100%);
  color: #FFE6A8;
  font-weight: 900;
  font-size: 0.9rem;
  border: 2px solid rgba(255, 214, 120, 0.4);
}

.chapter-card-info {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.chapter-card-realm {
  font-size: 0.66rem;
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  padding: 1px 6px;
  border-radius: 6px;
  width: fit-content;
}

.chapter-card-realm--aurelia {
  background: rgba(42, 157, 143, 0.18);
  color: #17655B;
}

.chapter-card-realm--hivern {
  background: rgba(69, 123, 157, 0.2);
  color: #1D3557;
}

.chapter-card-title {
  font-size: clamp(0.76rem, 0.95vmin + 0.32rem, 0.9rem);
  font-weight: 800;
  color: #264653;
  line-height: 1.15;
}

/* Reduced Motion Overrides */
@media (prefers-reduced-motion: reduce) {
  html:not([data-motion='full']) .end-aurora,
  html:not([data-motion='full']) .end-spark,
  html:not([data-motion='full']) .chapter-select-backdrop,
  html:not([data-motion='full']) .chapter-select-card {
    animation: none !important;
  }

  html:not([data-motion='full']) .end-action-btn,
  html:not([data-motion='full']) .chapter-card-btn {
    transition: none !important;
  }
}


/* ==========================================================================
   TASK E3: 3D TACTILE BOOKMARK RESUME BUTTON (2026 KIDS STANDARD)
   ========================================================================== */
.hero-resume-btn {
  --btn-lip: #A85A14;
  min-height: 64px;
  min-width: 64px;
  border-radius: 22px;
  padding: 0.65rem clamp(1rem, 2vw, 1.5rem);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.75rem;
  border: 3px solid #264653;
  color: #264653;
  font-family: inherit;
  font-size: clamp(0.88rem, 1vmin + 0.52rem, 1.08rem);
  font-weight: 900;
  letter-spacing: 0.02em;
  background: linear-gradient(180deg, #FFE8B8 0%, #F4A261 55%, #E76F51 100%);
  box-shadow:
    inset 0 4px 0 rgba(255, 255, 255, 0.55),
    0 8px 0 var(--btn-lip),
    0 14px 22px rgba(0, 0, 0, 0.35);
  cursor: pointer;
  transform: translateY(0);
  transition:
    transform 0.12s cubic-bezier(0.34, 1.56, 0.64, 1),
    box-shadow 0.12s ease,
    filter 0.15s ease;
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
}

.hero-resume-btn[hidden] {
  display: none !important;
}

.hero-resume-btn:hover {
  filter: brightness(1.06);
}

.hero-resume-btn:active {
  transform: translateY(6px) scale(0.98);
  box-shadow:
    inset 0 2px 0 rgba(255, 255, 255, 0.35),
    0 2px 0 var(--btn-lip),
    0 6px 10px rgba(0, 0, 0, 0.28);
}

.hero-resume-btn .btn-icon-wrap {
  width: 32px;
  height: 32px;
  flex-shrink: 0;
  display: grid;
  place-items: center;
}

.hero-resume-btn .cel-icon {
  width: 32px;
  height: 32px;
}

.resume-btn-text {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 2px;
  text-align: left;
}

.resume-btn-kicker {
  font-size: 0.68rem;
  font-weight: 800;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: #7A4512;
  text-shadow: 0 1px 0 rgba(255, 255, 255, 0.4);
}

.resume-btn-target {
  font-size: clamp(0.85rem, 1.1vw, 1rem);
  font-weight: 900;
  color: #264653;
  line-height: 1.15;
}

@media (prefers-reduced-motion: reduce) {
  html:not([data-motion='full']) .hero-resume-btn {
    transition: none !important;
  }
}


/* ==========================================================================
   TASK D1: FANTASY BOOK COVER (3D LEATHER & GOLD BINDING)
   ========================================================================== */

#book-cover,
.book-cover-stage,
.storybook-hero.book-page {
  position: relative !important;
  width: 100vw !important;
  height: 100vh !important;
  height: 100dvh !important;
  max-width: 100vw !important;
  max-height: 100dvh !important;
  overflow: hidden !important;
  box-sizing: border-box !important;
  display: flex;
  flex-direction: column !important;
  align-items: center !important;
  justify-content: center !important;
  padding: 0 !important;
  background: transparent !important;
}

/* Night-realm ambient room sits on a fading pseudo-layer so opening the book
   immediately uncovers the sunlit Chapter I spread beneath instead of a dark void. */
#book-cover::before,
.book-cover-stage::before {
  content: "";
  position: absolute;
  inset: 0;
  z-index: 0;
  pointer-events: none;
  opacity: 1;
  background:
    radial-gradient(120% 90% at 50% 0%, #2a1746 0%, #150c26 45%, #090512 100%);
  transition: opacity 0.44s var(--flip-ease);
}

/* The hinge: a static 3D context. It owns the perspective and never
   fades via opacity, so preserve-3d stays active and back endpapers render cleanly. */
.book-cover-hinge {
  position: absolute;
  top: 50%;
  left: 50%;
  width: var(--cover-w);
  height: var(--cover-h);
  translate: -50% -50%;
  z-index: 3;
  perspective: var(--flip-perspective);
  perspective-origin: 50% 50%;
  transition: translate 0.72s var(--flip-ease);
}

/* Closed book page-block edges + interior cream flyleaf sitting right behind the board */
.book-cover-body {
  position: absolute;
  inset: 5px -10px -8px 8px;
  border-radius: 6px 16px 16px 6px;
  z-index: 1;
  pointer-events: none;
  transition: opacity 0.34s var(--flip-ease) 0.14s;
}

.book-cover-body__edges {
  position: absolute;
  inset: 0;
  border-radius: inherit;
  background:
    linear-gradient(90deg, rgba(40, 22, 10, 0.25) 0%, transparent 6%),
    repeating-linear-gradient(
      90deg,
      #f8f0da 0px,
      #f8f0da 2px,
      #e2d2ae 2px,
      #e2d2ae 3px
    ),
    linear-gradient(180deg, #fffaf0 0%, #eadbbf 100%);
  border: 1px solid rgba(214, 171, 79, 0.55);
  box-shadow:
    8px 14px 28px rgba(0, 0, 0, 0.55),
    inset 0 -3px 6px rgba(120, 82, 24, 0.22);
}

.book-cover-body__flyleaf {
  position: absolute;
  inset: 2px 6px 5px 0;
  border-radius: 4px 12px 12px 4px;
  background:
    linear-gradient(90deg, rgba(64, 38, 18, 0.26) 0%, rgba(250, 244, 228, 0.96) 7%, #fdf9ee 100%);
  box-shadow: inset 0 0 24px rgba(180, 140, 75, 0.18);
}

/* The rotating plane. NEVER put opacity < 1 on this element or browsers flatten preserve-3d. */
.book-cover-3d {
  position: absolute;
  inset: 0;
  z-index: 3;
  transform-origin: left center;
  transform-style: preserve-3d;
  transform: rotateY(var(--cover-tilt-y)) rotateX(var(--cover-tilt-x));
  will-change: transform, translate;
  animation: bookFloat 6s ease-in-out infinite;
  transition: transform 0.16s ease-out;
}

/* The two faces of the cover board. Only one of them is ever facing the
   reader, so each hides its own backface. */
.book-cover-face {
  position: absolute;
  inset: 0;
  overflow: hidden;
  -webkit-backface-visibility: hidden;
  backface-visibility: hidden;
}

.book-cover-face--front {
  transform: rotateY(0deg) translateZ(1.5px);
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 8px 16px 16px 8px;
  border: 2px solid rgba(214, 171, 79, .85);
  background:
    radial-gradient(120% 80% at 78% 8%, rgba(255, 214, 140, .10), transparent 55%),
    radial-gradient(90% 70% at 15% 100%, rgba(120, 70, 180, .28), transparent 60%),
    linear-gradient(145deg, #4A1A2C 0%, #2A1330 38%, #1E1233 62%, #2D1B4D 100%);
  box-shadow:
    0 34px 70px -20px rgba(0, 0, 0, .85),
    0 0 0 1px rgba(255, 226, 150, .18),
    inset 0 0 0 1px rgba(255, 220, 140, .22),
    inset 0 0 26px rgba(0, 0, 0, .55),
    inset 0 0 90px rgba(58, 22, 60, .55);
  isolation: isolate;
}

.book-cover-face--back {
  transform: rotateY(180deg) translateZ(1.5px);
  border-radius: 16px 8px 8px 16px;
  border: 2px solid rgba(232, 194, 106, .58);
  background:
    linear-gradient(270deg, rgba(10, 5, 18, 0.34) 0%, transparent 10%),
    radial-gradient(70% 45% at 50% 12%, rgba(233, 196, 106, .18), transparent 62%),
    linear-gradient(150deg, #3A1A56 0%, #241040 55%, #150A26 100%);
  box-shadow:
    0 26px 60px -24px rgba(0, 0, 0, .8),
    0 0 0 1px rgba(255, 226, 150, .12),
    inset 0 0 40px rgba(0, 0, 0, .5);
  opacity: 1;
  transition: opacity var(--flip-fade-duration) linear var(--flip-fade-delay);
}

/* Inside cover: the endpaper printed on the back of the leather board. */
.cover-inside {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: clamp(4px, 0.9vh, 10px);
  padding: clamp(18px, 4vh, 44px) clamp(16px, 4vw, 40px);
  text-align: center;
  color: #F3E7CF;
}

.cover-inside__crest {
  width: clamp(40px, 7vh, 58px);
  height: auto;
  flex: 0 0 auto;
  filter: drop-shadow(0 4px 10px rgba(0, 0, 0, .5));
}

.cover-inside__kicker {
  margin: 0;
  font-family: var(--font-display);
  font-size: clamp(0.52rem, 1.05vh, 0.66rem);
  font-weight: 700;
  letter-spacing: 0.2em;
  text-transform: uppercase;
  color: var(--emerald-soft);
}

.cover-inside__title {
  margin: 0;
  font-family: 'Cinzel Decorative', 'Cinzel', Georgia, 'Times New Roman', serif;
  font-size: clamp(1.5rem, 3.6vh, 2.4rem);
  font-weight: 700;
  line-height: 0.98;
  color: var(--eos-gold-bright);
  text-shadow: 0 2px 0 rgba(0, 0, 0, .55);
}

.cover-inside__quote {
  max-width: 26ch;
  margin: 0;
  font-family: var(--font-prose);
  font-size: clamp(0.58rem, 1.2vh, 0.74rem);
  font-weight: 600;
  line-height: 1.5;
  color: #E7DCC7;
}

.cover-inside__note {
  margin: 0;
  font-family: var(--font-display);
  font-size: clamp(0.5rem, 1vh, 0.62rem);
  font-weight: 700;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: #CBBDA0;
}

.cover-inside__rule {
  width: clamp(34px, 6vw, 56px);
  height: 2px;
  border-radius: 2px;
  background: linear-gradient(90deg, transparent, var(--eos-gold), transparent);
  opacity: 0.6;
}

/* Leather spine */
.cover-spine {
  position: absolute;
  inset: 0 auto 0 0;
  width: clamp(18px, 3.2vw, 30px);
  z-index: 4;
  pointer-events: none;
  border-right: 1px solid rgba(232, 194, 106, .35);
  background:
    linear-gradient(90deg, rgba(0, 0, 0, .78), rgba(0, 0, 0, .12) 60%, rgba(255, 220, 140, .10)),
    repeating-linear-gradient(180deg, transparent 0 46px, rgba(232, 194, 106, .55) 46px 49px, transparent 49px 54px),
    linear-gradient(180deg, #3A1A24, #1A0E20);
  box-shadow:
    inset -6px 0 12px rgba(0, 0, 0, .6),
    inset 2px 0 4px rgba(255, 220, 150, .12);
}

/* Ornate double border */
.cover-border {
  position: absolute;
  inset: clamp(10px, 2.4vw, 18px);
  z-index: 2;
  pointer-events: none;
  border: 2px solid rgba(232, 194, 106, .85);
  border-radius: 6px 12px 12px 6px;
  box-shadow:
    inset 0 0 0 1px rgba(255, 232, 170, .35),
    inset 0 0 0 6px rgba(20, 8, 30, .55),
    inset 0 0 0 7px rgba(232, 194, 106, .45),
    inset 0 0 30px rgba(0, 0, 0, .5);
}

/* Gold filigree corners */
.filigree {
  position: absolute;
  width: clamp(34px, 7vw, 56px);
  height: clamp(34px, 7vw, 56px);
  color: rgba(236, 201, 118, .95);
  pointer-events: none;
  filter: drop-shadow(0 0 6px rgba(255, 214, 130, .35));
}

.filigree::before {
  content: "";
  position: absolute;
  inset: 0;
  border: 2px solid currentColor;
  border-right: 0;
  border-bottom: 0;
  border-radius: 24px 0 0 0;
}

.filigree::after {
  content: "";
  position: absolute;
  top: -3px;
  left: -3px;
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: radial-gradient(circle at 35% 30%, #FFF6D0, #D9A93F 60%, #8A641C);
  box-shadow: 0 0 10px rgba(255, 220, 140, .7);
}

.filigree--tl { top: 8px; left: 8px; }
.filigree--tr { top: 8px; right: 8px; transform: rotate(90deg); }
.filigree--br { right: 8px; bottom: 8px; transform: rotate(180deg); }
.filigree--bl { left: 8px; bottom: 8px; transform: rotate(270deg); }

/* Decorative branches */
.branch {
  position: absolute;
  bottom: -8px;
  z-index: 1;
  width: clamp(90px, 25vw, 190px);
  height: auto;
  aspect-ratio: 200 / 460;
  display: block;
  pointer-events: none;
  opacity: .92;
  filter: drop-shadow(0 6px 10px rgba(0, 0, 0, .45));
}

.branch--willow {
  left: clamp(-28px, -1vw, -6px);
  transform: rotate(-2deg);
}

.branch--winter {
  right: clamp(-28px, -1vw, -6px);
  transform: scaleX(-1) rotate(-2deg);
}

.cover-glow {
  position: absolute;
  inset: 0;
  z-index: 0;
  pointer-events: none;
  background:
    radial-gradient(60% 45% at 50% 38%, rgba(255, 220, 140, .14), transparent 70%),
    radial-gradient(40% 30% at 22% 12%, rgba(174, 120, 255, .10), transparent 70%);
}

/* Cover content */
.cover-content {
  position: relative;
  z-index: 5;
  width: 100%;
  height: 100%;
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: clamp(6px, 1.4vh, 14px);
  padding:
    clamp(24px, 5vh, 48px)
    clamp(16px, 4vw, 44px)
    clamp(18px, 4vh, 36px)
    clamp(28px, 6vw, 56px);
  text-align: center;
}

.book-crest {
  width: clamp(52px, 11vmin, 90px);
  height: auto;
  flex: 0 0 auto;
  filter:
    drop-shadow(0 4px 10px rgba(0, 0, 0, .55))
    drop-shadow(0 0 14px rgba(255, 214, 120, .25));
}

.book-kicker {
  margin: 0;
  font-family: 'Cinzel', Georgia, serif;
  font-size: clamp(.62rem, 1.65vmin, .86rem);
  letter-spacing: .32em;
  text-transform: uppercase;
  color: #EBD59A;
  text-shadow: 0 1px 6px rgba(0, 0, 0, .8);
  opacity: .95;
}

.book-title {
  margin: 0;
  font-family: 'Cinzel Decorative', 'Cinzel', Georgia, 'Times New Roman', serif;
  font-size: clamp(1.8rem, 5.5vmin, 3.4rem);
  font-weight: 700;
  line-height: 1.05;
  letter-spacing: .035em;
  background: linear-gradient(
    180deg,
    #FFF8DA 0%,
    #F2D98A 28%,
    #D9A83F 55%,
    #8C5F16 78%,
    #F7E7A6 100%
  );
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  -webkit-text-fill-color: transparent;
  filter:
    drop-shadow(0 2px 0 rgba(72, 44, 8, .55))
    drop-shadow(0 8px 18px rgba(0, 0, 0, .5));
}

.book-subtitle {
  margin: clamp(2px, .6vh, 6px) auto 0;
  max-width: 46ch;
  font-family: Georgia, 'Times New Roman', serif;
  font-size: clamp(.76rem, 1.8vmin, 0.98rem);
  line-height: 1.45;
  font-style: italic;
  color: #F0E3C4;
  text-shadow: 0 1px 8px rgba(0, 0, 0, .75);
}

/* Drifting sparkles */
.sparkle-field {
  position: absolute;
  inset: 0;
  z-index: 6;
  overflow: hidden;
  pointer-events: none;
}

.sparkle {
  position: absolute;
  left: var(--x);
  top: var(--y);
  width: var(--s, 10px);
  height: var(--s, 10px);
  opacity: 0;
  transform-origin: center;
  /*
   * The star and its glow are baked into one SVG texture.
   *
   * The star used to be a clip-path over a radial gradient with a
   * drop-shadow filter on top. Both clip-path and filter are grouping
   * properties, so fifteen sparkles inside the rotating cover forced the
   * whole cover face to re-rasterise on every frame of the flip, which
   * showed up as dropped frames. One flat texture costs nothing.
   */
  background: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 100'%3E%3Cdefs%3E%3CradialGradient id='g' gradientUnits='userSpaceOnUse' cx='50' cy='50' r='50'%3E%3Cstop offset='0' stop-color='%23FFFDF2'/%3E%3Cstop offset='38' stop-color='%23FFE9A8'/%3E%3Cstop offset='70' stop-color='%23E7B44A'/%3E%3Cstop offset='72' stop-color='%23E7B44A' stop-opacity='0'/%3E%3C/radialGradient%3E%3Cfilter id='b' filterUnits='userSpaceOnUse' x='-60' y='-60' width='220' height='220'%3E%3CfeGaussianBlur stdDeviation='16'/%3E%3C/filter%3E%3C/defs%3E%3Cpath d='M50 2L59 41L98 50L59 59L50 98L41 59L2 50L41 41Z' fill='%23FFE496' opacity='.75' filter='url(%23b)'/%3E%3Cpath d='M50 0L60 40L100 50L60 60L50 100L40 60L0 50L40 40Z' fill='url(%23g)'/%3E%3C/svg%3E") center / 100% 100% no-repeat;
  animation: sparkleDrift var(--t, 10s) ease-in-out var(--d, 0s) infinite;
  will-change: transform, opacity;
}

.sparkle--1  { --x: 14%; --y: 22%; --s: 9px;  --d: -1.4s; --t: 9.2s; }
.sparkle--2  { --x: 78%; --y: 16%; --s: 11px; --d: -3.1s; --t: 11s; }
.sparkle--3  { --x: 62%; --y: 78%; --s: 8px;  --d: -5.2s; --t: 8.4s; }
.sparkle--4  { --x: 26%; --y: 68%; --s: 7px;  --d: -2.3s; --t: 10.5s; }
.sparkle--5  { --x: 88%; --y: 52%; --s: 10px; --d: -6.4s; --t: 12s; }
.sparkle--6  { --x: 8%;  --y: 48%; --s: 8px;  --d: -4.6s; --t: 9.8s; }
.sparkle--7  { --x: 44%; --y: 10%; --s: 7px;  --d: -2.8s; --t: 8.9s; }
.sparkle--8  { --x: 54%; --y: 90%; --s: 9px;  --d: -7.1s; --t: 10.2s; }
.sparkle--9  { --x: 18%; --y: 88%; --s: 6px;  --d: -3.7s; --t: 9.1s; }
.sparkle--10 { --x: 70%; --y: 34%; --s: 7px;  --d: -5.9s; --t: 11.6s; }
.sparkle--11 { --x: 34%; --y: 40%; --s: 6px;  --d: -1.9s; --t: 8.6s; }
.sparkle--12 { --x: 92%; --y: 78%; --s: 8px;  --d: -6.8s; --t: 10.8s; }
.sparkle--13 { --x: 6%;  --y: 76%; --s: 7px;  --d: -4.2s; --t: 9.6s; }
.sparkle--14 { --x: 50%; --y: 56%; --s: 6px;  --d: -7.6s; --t: 12.4s; }
.sparkle--15 { --x: 82%; --y: 8%;  --s: 8px;  --d: -2.6s; --t: 9.4s; }

@keyframes sparkleDrift {
  0% {
    opacity: 0;
    transform: translate3d(0, 8px, 0) scale(.35) rotate(0deg);
  }
  15% {
    opacity: .95;
  }
  40% {
    opacity: .55;
    transform: translate3d(6px, -10px, 0) scale(1) rotate(45deg);
  }
  70% {
    opacity: .85;
    transform: translate3d(-4px, -26px, 0) scale(.8) rotate(90deg);
  }
  100% {
    opacity: 0;
    transform: translate3d(3px, -44px, 0) scale(.3) rotate(135deg);
  }
}

/* Compact-height safeguards */
@media (max-height: 620px) {
  .cover-content {
    gap: 4px;
    padding-top: 18px;
    padding-bottom: 16px;
  }

  .book-title {
    font-size: clamp(1.5rem, 4.4vmin, 2.4rem);
  }

  .book-subtitle {
    font-size: .72rem;
    line-height: 1.35;
  }

  .book-crest {
    width: clamp(40px, 8vmin, 58px);
  }
}

/* Reduced motion fallback */
@media (prefers-reduced-motion: reduce) {
  html:not([data-motion='full']) .book-cover-3d {
    animation: none;
    transform: none;
  }

  html:not([data-motion='full']) .sparkle {
    animation: none;
    opacity: .55;
    transform: none;
  }
}


/* ==========================================================================
   TASK D2: COVER ANIMATIONS (LIGHT RAYS, SHEEN, FIREFLIES, BOOK FLOAT)
   ========================================================================== */

/* The float bob only moves the card. It rides on the independent `translate`
   property so the flip keeps sole ownership of `transform`. */
@keyframes bookFloat {
  0%, 100% {
    translate: 0 0;
  }
  50% {
    translate: 0 -8px;
  }
}

/* 2. Soft pillowed floating shadow */
.book-shadow {
  position: absolute;
  bottom: clamp(8px, 2.5vh, 26px);
  left: 50%;
  transform: translateX(-50%);
  width: clamp(240px, 60vw, 500px);
  height: 24px;
  border-radius: 50%;
  pointer-events: none;
  background: radial-gradient(ellipse at center, rgba(10, 5, 20, 0.58) 0%, rgba(10, 5, 20, 0.22) 52%, transparent 74%);
  z-index: 1;
  animation: shadowFloat 6s ease-in-out infinite;
  will-change: transform, opacity;
}

@keyframes shadowFloat {
  0%, 100% {
    transform: translateX(-50%) scale(1);
    opacity: 0.85;
  }
  50% {
    transform: translateX(-50%) scale(0.85);
    opacity: 0.5;
  }
}

/* 3. Celestial golden light rays */
.light-rays {
  position: absolute;
  top: 50%;
  left: 50%;
  width: clamp(500px, 120vmin, 900px);
  height: clamp(500px, 120vmin, 900px);
  transform: translate(-50%, -50%);
  border-radius: 50%;
  pointer-events: none;
  /*
   * No `mix-blend-mode: screen` and no `filter: blur()` here on purpose.
   * Both force the browser to read the backdrop and re-composite the whole
   * cover region, and this element rotates forever behind the book. Dropping
   * them lifted the idle cover from ~34fps to ~75fps on a software
   * rasteriser; the alpha below is raised slightly to keep the same
   * brightness the screen blend used to provide.
   */
  background: repeating-conic-gradient(
    from 0deg at 50% 50%,
    rgba(255, 214, 130, 0) 0deg 6deg,
    rgba(255, 214, 130, 0.18) 6deg 9deg,
    rgba(255, 214, 130, 0) 9deg 18deg
  );
  -webkit-mask-image: radial-gradient(circle at center, rgba(0,0,0,0.85) 0%, transparent 68%);
  mask-image: radial-gradient(circle at center, rgba(0,0,0,0.85) 0%, transparent 68%);
  z-index: 0;
  opacity: 0.72;
  animation: raySweep 12s linear infinite;
  will-change: transform;
}

@keyframes raySweep {
  from { transform: translate(-50%, -50%) rotate(0deg); }
  to   { transform: translate(-50%, -50%) rotate(360deg); }
}

/* 4. Sweeping gold sheen */
.cover-sheen {
  position: absolute;
  inset: -20% -20%;
  z-index: 4;
  pointer-events: none;
  background: linear-gradient(
    105deg,
    transparent 0%,
    transparent 35%,
    rgba(255, 244, 205, 0.22) 45%,
    rgba(255, 255, 255, 0.42) 50%,
    rgba(255, 244, 205, 0.22) 55%,
    transparent 65%,
    transparent 100%
  );
  animation: sheenSweep 7s ease-in-out infinite;
  will-change: transform, opacity;
}

@keyframes sheenSweep {
  0%   { transform: translateX(-160%) skewX(-16deg); opacity: 0; }
  10%  { transform: translateX(-100%) skewX(-16deg); opacity: 0.9; }
  50%  { transform: translateX(180%)  skewX(-16deg); opacity: 0.9; }
  60%  { transform: translateX(280%)  skewX(-16deg); opacity: 0; }
  100% { transform: translateX(280%)  skewX(-16deg); opacity: 0; }
}

/* 5. Ten drifting fireflies */
.firefly {
  position: absolute;
  width: 7px;
  height: 7px;
  border-radius: 50%;
  pointer-events: none;
  background: radial-gradient(circle, #FFFBE8 0%, #FFD977 45%, rgba(255, 200, 90, 0) 72%);
  box-shadow: 0 0 10px 3px rgba(255, 214, 128, 0.55), 0 0 24px 9px rgba(255, 190, 90, 0.18);
  opacity: 0;
  z-index: 3;
  animation: fireflyDrift 8s ease-in-out infinite, fireflyPulse 2.5s ease-in-out infinite;
  will-change: transform, opacity;
}

.firefly-1  { top: 16%; left: 10%; --dx:  16px; --dy: -26px; animation-delay: 0.0s, 0.0s; }
.firefly-2  { top: 33%; left:  4%; --dx: -14px; --dy: -20px; animation-delay: 0.9s, 0.35s; }
.firefly-3  { top: 56%; left:  6%; --dx:  18px; --dy:  14px; animation-delay: 1.8s, 0.70s; }
.firefly-4  { top: 78%; left: 14%; --dx: -16px; --dy:  22px; animation-delay: 2.6s, 1.10s; }
.firefly-5  { top:  8%; left: 47%; --dx:  12px; --dy:  18px; animation-delay: 3.4s, 1.50s; }
.firefly-6  { top: 92%; left: 54%; --dx: -18px; --dy:  16px; animation-delay: 0.4s, 1.90s; }
.firefly-7  { top: 22%; left: 88%; --dx:  14px; --dy:  20px; animation-delay: 1.3s, 0.20s; }
.firefly-8  { top: 46%; left: 95%; --dx: -20px; --dy: -16px; animation-delay: 2.1s, 0.90s; }
.firefly-9  { top: 68%; left: 86%; --dx:  16px; --dy: -22px; animation-delay: 3.0s, 1.40s; }
.firefly-10 { top: 87%; left: 72%; --dx: -12px; --dy: -18px; animation-delay: 3.8s, 2.00s; }

@keyframes fireflyDrift {
  0%, 100% { transform: translate3d(0, 0, 0); }
  25% { transform: translate3d(var(--dx, 14px), var(--dy, -22px), 0); }
  50% { transform: translate3d(calc(var(--dx, 14px) * -0.7), calc(var(--dy, -22px) * 1.2), 0); }
  75% { transform: translate3d(calc(var(--dx, 14px) * 0.85), calc(var(--dy, -22px) * -0.6), 0); }
}

@keyframes fireflyPulse {
  0%, 100% { opacity: 0.08; transform: scale(0.65); }
  45% { opacity: 0.95; transform: scale(1.15); }
  60% { opacity: 0.75; transform: scale(1.05); }
}

/* 6. Awakened cover state */
#book-cover.is-awake .book-cover-face--front {
  box-shadow:
    0 34px 70px -20px rgba(0, 0, 0, .85),
    0 0 0 1px rgba(255, 226, 150, .18),
    inset 0 0 0 1px rgba(255, 220, 140, .22),
    inset 0 0 26px rgba(0, 0, 0, .55),
    inset 0 0 90px rgba(58, 22, 60, .55);
}

#book-cover.is-awake .light-rays {
  opacity: 0.95;
}

#book-cover.is-awake .cover-sheen {
  animation-duration: 4.5s;
}

#book-cover.is-awake .firefly {
  opacity: 0.92;
}

/* D2 Reduced motion overrides */
@media (prefers-reduced-motion: reduce) {
  html:not([data-motion='full']) .book-cover-3d,
  html:not([data-motion='full']) .book-shadow,
  html:not([data-motion='full']) .light-rays,
  html:not([data-motion='full']) .cover-sheen,
  html:not([data-motion='full']) .firefly {
    animation: none !important;
  }

  html:not([data-motion='full']) .book-cover-3d {
    transform: none !important;
  }

  html:not([data-motion='full']) .book-shadow {
    transform: translateX(-50%) !important;
    opacity: 0.55 !important;
  }

  html:not([data-motion='full']) .light-rays {
    transform: translate(-50%, -50%) !important;
    opacity: 0.45 !important;
  }

  html:not([data-motion='full']) .cover-sheen {
    opacity: 0 !important;
  }

  html:not([data-motion='full']) .firefly {
    transform: none !important;
    opacity: 0.6 !important;
  }
}


/* ==========================================================================
   TASK D3: BOOK COVER MODE BUTTONS (PLAY STORY & READ MYSELF)
   ========================================================================== */

.hero-start-wrap {
  display: flex;
  flex-direction: row;
  gap: 12px;
  justify-content: center;
  align-items: center;
  flex-wrap: wrap;
  width: 100%;
}

.mode-btn {
  --btn-lip: #b8862b;
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  min-height: 52px;
  min-width: 170px;
  padding: 10px 20px;
  border: 0;
  border-radius: 18px;
  font-family: inherit;
  font-size: clamp(0.9rem, 1.1vw, 1.05rem);
  font-weight: 800;
  letter-spacing: 0.015em;
  line-height: 1.1;
  color: #2b1d0a;
  background-color: #e9c46a;
  background-image: linear-gradient(180deg, #ffe9a8 0%, #e9c46a 52%, #d4a83f 100%);
  box-shadow: 0 6px 0 var(--btn-lip), 0 10px 18px rgba(10, 8, 4, 0.28);
  transform: translateY(0);
  transition: transform 0.18s cubic-bezier(0.34, 1.56, 0.64, 1), box-shadow 0.18s ease, filter 0.18s ease;
  cursor: pointer;
  appearance: none;
  -webkit-appearance: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

.mode-btn::before {
  content: "";
  position: absolute;
  inset: 2px 2px auto 2px;
  height: 48%;
  border-radius: 15px 15px 60% 60% / 15px 15px 30px 30px;
  background: linear-gradient(180deg, rgba(255, 255, 255, 0.88) 0%, rgba(255, 255, 255, 0.28) 55%, rgba(255, 255, 255, 0) 100%);
  pointer-events: none;
}

.mode-btn::after {
  content: "";
  position: absolute;
  left: 10px;
  right: 10px;
  bottom: 4px;
  height: 9px;
  border-radius: 999px;
  background: linear-gradient(180deg, rgba(255, 255, 255, 0.55) 0%, rgba(255, 255, 255, 0.05) 100%);
  opacity: 0.75;
  pointer-events: none;
}

.mode-btn__icon,
.mode-btn__label {
  position: relative;
  z-index: 1;
}

.mode-btn__label {
  white-space: nowrap;
}

.mode-btn .cel-icon {
  display: block;
  width: 32px;
  height: 32px;
  filter: drop-shadow(0 2px 1px rgba(0, 0, 0, 0.25));
}

.mode-btn:active {
  transform: translateY(5px) scale(0.982);
  box-shadow: 0 1px 0 var(--btn-lip), 0 3px 8px rgba(10, 8, 4, 0.30);
  transition: transform 0.07s ease-out, box-shadow 0.07s ease-out, filter 0.07s ease;
}

.mode-btn:focus-visible {
  outline: 3px solid #ffffff;
  outline-offset: 3px;
}

.mode-btn--play {
  --btn-lip: #b8862b;
  color: #2b1d0a;
  background-image: linear-gradient(180deg, #fff0b8 0%, #e9c46a 52%, #cf9d33 100%);
  box-shadow: 0 6px 0 var(--btn-lip), 0 0 22px rgba(233, 196, 106, 0.72);
}

.mode-btn--play:active {
  box-shadow: 0 1px 0 var(--btn-lip), 0 3px 8px rgba(43, 29, 10, 0.34);
}

.mode-btn--play::after {
  will-change: opacity;
  animation: moPlayGlow 2.4s ease-in-out infinite;
}

.mode-btn--read {
  --btn-lip: #2f6f6a;
  color: #062a28;
  background-image: linear-gradient(180deg, #d8f7f2 0%, #8ed7cf 52%, #5fbcb2 100%);
  box-shadow: 0 6px 0 var(--btn-lip), 0 10px 18px rgba(6, 42, 40, 0.30);
}

.mode-btn--read:active {
  box-shadow: 0 1px 0 var(--btn-lip), 0 3px 8px rgba(6, 42, 40, 0.32);
}

@keyframes moPlayGlow {
  0%, 100% {
    opacity: 0.45;
  }
  50% {
    opacity: 0.95;
  }
}

@media (max-width: 600px) {
  .hero-start-wrap {
    flex-direction: column !important;
    width: 100%;
    max-width: 320px;
    gap: 10px;
  }

  .mode-btn {
    width: 100%;
    min-width: 0;
    min-height: 48px;
    padding: 8px 16px;
  }
}

@media (prefers-reduced-motion: reduce) {
  html:not([data-motion='full']) #btn-play-story {
    animation: none !important;
  }

  html:not([data-motion='full']) .mode-btn {
    transition: none;
  }

  html:not([data-motion='full']) .mode-btn:active {
    transform: translateY(4px);
  }
}


/* ==========================================================================
   TASK D5: CSS-ONLY BOOK-OPEN FLIP
   --------------------------------------------------------------------------
   The cover hinges on its leather spine. Three layers keep the work cheap:

     .book-cover-hinge   static 3D context, owns the perspective
     .book-cover-3d      the rotating plane, owns the flip transform
     .book-cover-face    the two boards, front and back

   The heavy chapter artwork never enters this context, so the browser only
   re-renders the cover while it swings.
   ========================================================================== */

#book-cover.book-cover-open-stage {
  position: absolute !important;
  inset: 0 !important;
  pointer-events: none;
  z-index: 10 !important;
}

#book-cover.book-cover-open-stage::before {
  opacity: 0;
}

#book-cover.book-cover-open-stage.is-closing::before {
  opacity: 0;
  transition: none;
  animation: bookCoverBackdropIn 0.44s var(--flip-ease) 0.30s both;
}

@keyframes bookCoverBackdropIn {
  from {
    opacity: 0;
  }

  to {
    opacity: 1;
  }
}

#book-cover.book-cover-open-stage .book-cover-body {
  opacity: 0;
}

#book-cover.book-cover-open-stage.is-opening .book-cover-body {
  opacity: 0;
  transition: opacity 0.14s linear 0s;
}

#book-cover.book-cover-open-stage.is-closing .book-cover-body {
  opacity: 0;
  transition: none;
  animation: bookCoverBackdropIn 0.24s linear 0.50s both;
}

#book-cover.book-cover-open-stage.is-opening .book-cover-face--back {
  opacity: 0;
}

#book-cover.book-cover-open-stage.is-closing .book-cover-face--back {
  opacity: 1;
  transition: none;
}

#book-cover.book-cover-open-stage .book-cover-3d {
  animation: none;
  translate: 0 0;
}

@media (min-width: 921px) {
  #book-cover.book-cover-open-stage.is-opening .book-cover-hinge {
    translate: calc(-50% + min(13vw, 175px)) -50%;
  }

  #book-cover.book-cover-open-stage.is-closing.is-closing-start .book-cover-hinge {
    translate: calc(-50% + min(13vw, 175px)) -50%;
    transition: none;
  }
}

.book-page.book-cover-underlay {
  position: absolute !important;
  inset: 0 !important;
  z-index: 1 !important;
}

body.book-transition-lock {
  overflow: hidden !important;
  pointer-events: none;
}

/* Forward: cover rotates purely via transform (opacity stays 1 so preserve-3d
   stays intact and back endpaper replaces front face past -90deg cleanly). */
.book-cover-3d.book-cover-flip-out {
  transform: rotateY(var(--cover-open-angle)) rotateX(0deg);
  transition: transform var(--flip-duration) var(--flip-ease);
}

/* Reverse, step one: park cover open (back endpaper visible) before closing. */
.book-cover-3d.book-cover-flip-in {
  transform: rotateY(var(--cover-open-angle)) rotateX(0deg);
  transition: none;
}

/* Reverse, step two: swing cover shut onto the front leather board. */
.book-cover-3d.book-cover-flip-back {
  transform: rotateY(var(--cover-tilt-y)) rotateX(var(--cover-tilt-x));
  transition: transform var(--flip-back-duration) var(--flip-ease) var(--flip-back-delay);
}

/* While the cover turns, the ambient dress stops being painted. */
#book-cover.book-cover-open-stage .light-rays,
#book-cover.book-cover-open-stage .book-shadow,
#book-cover.book-cover-open-stage .firefly,
#book-cover.book-cover-open-stage .sparkle-field,
#book-cover.book-cover-open-stage .cover-sheen {
  animation: none;
  opacity: 0;
  visibility: hidden;
  transition:
    opacity 160ms linear,
    visibility 0s linear 160ms;
}

.book-shadow,
.light-rays,
.firefly,
.sparkle-field,
.cover-sheen {
  transition: opacity 160ms linear;
}

/* The chapter page sits crisp right under the lifting book lid. */
.book-page.book-page-fade-in {
  animation: bookPageFadeIn var(--flip-page-fade-duration) var(--flip-ease) var(--flip-page-fade-delay) both;
}

.book-page.book-page-fade-out {
  animation: bookPageFadeOut 0.44s var(--flip-ease) 0.26s both;
}

@keyframes bookPageFadeIn {
  from {
    opacity: 0.72;
  }

  to {
    opacity: 1;
  }
}

@keyframes bookPageFadeOut {
  from {
    opacity: 1;
  }

  to {
    opacity: 0.6;
  }
}

/* ==========================================================================
   OPTION A: 3D SPINE-HINGED PAGE LEAF TURN (CHAPTERS <-> THE END)
   Desktop (>920px): true center-spine BookBlock turn across the middle crease.
   Mobile (<=920px): left-spine folio leaf turn (never slices wrapped prose).
   ========================================================================== */

.book-flip-stage {
  position: absolute;
  inset: 0;
  z-index: 20;
  pointer-events: none;
  perspective: 2600px;
  perspective-origin: var(--flip-spine, 50%) 50%;
  transform-style: preserve-3d;
  overflow: hidden;
}

.book-flip-gutter {
  position: absolute;
  top: 0;
  bottom: 0;
  left: var(--flip-spine, 50%);
  width: 56px;
  transform: translateX(-50%);
  z-index: 6;
  pointer-events: none;
  background: linear-gradient(
    90deg,
    rgba(24, 18, 12, 0) 0%,
    rgba(24, 18, 12, 0.16) 44%,
    rgba(16, 12, 8, 0.28) 50%,
    rgba(24, 18, 12, 0.14) 56%,
    rgba(24, 18, 12, 0) 100%
  );
}

.book-flip-underlay-shadow {
  position: absolute;
  inset: 0;
  z-index: 3;
  pointer-events: none;
  background: rgba(14, 12, 18, 0.32);
  opacity: 0;
}

.book-flip-leaf {
  position: absolute;
  inset: 0;
  z-index: 5;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  will-change: transform;
  overflow: hidden;
  pointer-events: none;
}

.book-flip-leaf--right {
  clip-path: inset(0 0 0 var(--flip-spine, 50%));
  transform-origin: var(--flip-spine, 50%) 50%;
}

.book-flip-leaf--left {
  clip-path: inset(0 calc(100% - var(--flip-spine, 50%)) 0 0);
  transform-origin: var(--flip-spine, 50%) 50%;
}

.book-flip-leaf--full {
  transform-origin: 0% 50%;
}

.book-flip-leaf--right::after {
  content: "";
  position: absolute;
  top: 0;
  bottom: 0;
  left: var(--flip-spine, 50%);
  width: 28px;
  z-index: 41;
  pointer-events: none;
  background: linear-gradient(90deg, rgba(20, 14, 8, 0.18), transparent);
}

.book-flip-leaf--left::after {
  content: "";
  position: absolute;
  top: 0;
  bottom: 0;
  right: calc(100% - var(--flip-spine, 50%));
  width: 28px;
  z-index: 41;
  pointer-events: none;
  background: linear-gradient(270deg, rgba(20, 14, 8, 0.18), transparent);
}

.book-flip-leaf--full::after {
  content: "";
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  width: 32px;
  z-index: 41;
  pointer-events: none;
  background: linear-gradient(90deg, rgba(20, 14, 8, 0.22), transparent);
}

.book-flip-leaf .book-page,
.book-flip-leaf--aurelia {
  background-color: var(--page-bg) !important;
  background-image:
    radial-gradient(circle at 12% 8%, rgba(233, 196, 106, 0.20) 0%, transparent 42%),
    radial-gradient(circle at 88% 22%, rgba(244, 162, 97, 0.14) 0%, transparent 38%),
    radial-gradient(circle at 50% 85%, rgba(42, 157, 143, 0.08) 0%, transparent 48%) !important;
  color: var(--page-text) !important;
}

.book-flip-leaf--hivern,
.book-flip-leaf--hivern .story-panel--nachtwald {
  background-color: #1D2D3A !important;
  background-image:
    radial-gradient(circle at 20% 18%, rgba(168, 197, 218, 0.22) 0%, transparent 46%),
    radial-gradient(circle at 82% 68%, rgba(43, 61, 79, 0.55) 0%, transparent 52%),
    linear-gradient(180deg, #182632 0%, #1D2D3A 52%, #233747 100%) !important;
  color: #EAF4FC !important;
}

.book-flip-leaf--aurelia .panel-heading,
.book-page.is-leaf-base-full:not([data-realm='hivern']) .panel-heading,
.book-page.is-leaf-base-left:not([data-realm='hivern']) .panel-heading,
.book-page.is-leaf-base-right:not([data-realm='hivern']) .panel-heading,
.book-page.book-flip-underlay:not([data-realm='hivern']) .panel-heading {
  color: var(--page-text) !important;
  text-shadow: none !important;
}

.book-flip-leaf--hivern .panel-heading,
.book-page.is-leaf-base-full[data-realm='hivern'] .panel-heading,
.book-page.is-leaf-base-left[data-realm='hivern'] .panel-heading,
.book-page.is-leaf-base-right[data-realm='hivern'] .panel-heading,
.book-page.book-flip-underlay[data-realm='hivern'] .panel-heading {
  color: #EAF4FC !important;
  text-shadow: 2px 3px 0px #0F1A24 !important;
}

.book-flip-leaf--hivern .chapter-pill--frost,
.book-page.is-leaf-base-full[data-realm='hivern'] .chapter-pill--frost,
.book-page.is-leaf-base-left[data-realm='hivern'] .chapter-pill--frost,
.book-page.is-leaf-base-right[data-realm='hivern'] .chapter-pill--frost,
.book-page.book-flip-underlay[data-realm='hivern'] .chapter-pill--frost {
  background-color: #1D2D3A;
  color: #EAF4FC;
  border-color: var(--nachtwald-frost);
  box-shadow: 3px 4px 0px #0F1A24;
}

.book-flip-leaf--hivern .panel-artwork-frame--frost,
.book-page.is-leaf-base-full[data-realm='hivern'] .panel-artwork-frame--frost,
.book-page.is-leaf-base-left[data-realm='hivern'] .panel-artwork-frame--frost,
.book-page.is-leaf-base-right[data-realm='hivern'] .panel-artwork-frame--frost,
.book-page.book-flip-underlay[data-realm='hivern'] .panel-artwork-frame--frost {
  background-color: #243748;
  border-color: var(--nachtwald-frost);
  box-shadow: 6px 8px 0px #0F1A24, 0 20px 40px rgba(8, 15, 22, 0.45);
}

.book-flip-leaf--hivern .narration-plate--frost,
.book-page.is-leaf-base-full[data-realm='hivern'] .narration-plate--frost,
.book-page.is-leaf-base-left[data-realm='hivern'] .narration-plate--frost,
.book-page.is-leaf-base-right[data-realm='hivern'] .narration-plate--frost,
.book-page.book-flip-underlay[data-realm='hivern'] .narration-plate--frost {
  background-color: #E6F0F8;
  border-color: var(--nachtwald-frost);
  box-shadow: 4px 6px 0px #0F1A24;
}

.book-flip-leaf--hivern .manga-speech-bubble--frost,
.book-page.is-leaf-base-full[data-realm='hivern'] .manga-speech-bubble--frost,
.book-page.is-leaf-base-left[data-realm='hivern'] .manga-speech-bubble--frost,
.book-page.is-leaf-base-right[data-realm='hivern'] .manga-speech-bubble--frost,
.book-page.book-flip-underlay[data-realm='hivern'] .manga-speech-bubble--frost {
  background-color: #DCEAF5;
  border-color: var(--nachtwald-frost);
  box-shadow: 4px 6px 0px #0F1A24;
}

.book-flip-shade {
  position: absolute;
  inset: 0;
  z-index: 45;
  pointer-events: none;
  background: linear-gradient(135deg, rgba(16, 12, 24, 0.72), rgba(20, 16, 12, 0.85));
  opacity: 0;
  will-change: opacity;
}

.book-page.is-leaf-base-full,
.book-page.is-leaf-base-left,
.book-page.is-leaf-base-right,
.book-page.book-flip-underlay,
.book-page.book-flip-under-source {
  position: absolute !important;
  inset: 0 !important;
}

.book-page.is-leaf-base-full {
  z-index: 2 !important;
}

.book-page.is-leaf-base-left {
  clip-path: inset(0 calc(100% - var(--flip-spine, 50%)) 0 0);
  z-index: 2 !important;
}

.book-page.is-leaf-base-right {
  clip-path: inset(0 0 0 var(--flip-spine, 50%));
  z-index: 2 !important;
}

.book-page.book-flip-underlay {
  z-index: 1 !important;
}

.book-page.book-flip-under-source {
  z-index: 0 !important;
  visibility: hidden !important;
}

.book-page.is-leaf-base-full:not([data-realm='hivern']),
.book-page.is-leaf-base-left:not([data-realm='hivern']),
.book-page.is-leaf-base-right:not([data-realm='hivern']),
.book-page.book-flip-underlay:not([data-realm='hivern']) {
  background-color: var(--page-bg) !important;
  background-image:
    radial-gradient(circle at 12% 8%, rgba(233, 196, 106, 0.20) 0%, transparent 42%),
    radial-gradient(circle at 88% 22%, rgba(244, 162, 97, 0.14) 0%, transparent 38%),
    radial-gradient(circle at 50% 85%, rgba(42, 157, 143, 0.08) 0%, transparent 48%) !important;
  color: var(--page-text) !important;
}

.book-page.is-leaf-base-full[data-realm='hivern'],
.book-page.is-leaf-base-left[data-realm='hivern'],
.book-page.is-leaf-base-right[data-realm='hivern'],
.book-page.book-flip-underlay[data-realm='hivern'] {
  background-color: #1D2D3A !important;
  background-image:
    radial-gradient(circle at 20% 18%, rgba(168, 197, 218, 0.22) 0%, transparent 46%),
    radial-gradient(circle at 82% 68%, rgba(43, 61, 79, 0.55) 0%, transparent 52%),
    linear-gradient(180deg, #182632 0%, #1D2D3A 52%, #233747 100%) !important;
  color: #EAF4FC !important;
}

@media (max-width: 920px) {
  .book-flip-stage {
    perspective: 2200px;
    perspective-origin: 0% 50%;
  }

  .book-flip-gutter {
    left: 0;
    transform: none;
    width: 34px;
    background: linear-gradient(90deg, rgba(16, 12, 8, 0.34), transparent);
  }
}

html[data-perf='low'] body::before {
  display: none !important;
}

/* Reduced motion overrides */
@media (prefers-reduced-motion: reduce) {
  html:not([data-motion='full']) #book-cover.book-cover-open-stage {
    pointer-events: auto;
    z-index: auto;
  }

  html:not([data-motion='full']) body.book-transition-lock {
    overflow: auto !important;
    pointer-events: auto;
  }

  html:not([data-motion='full']) .book-cover-3d {
    transition: none !important;
  }

  html:not([data-motion='full']) .book-cover-3d.book-cover-flip-out,
  html:not([data-motion='full']) .book-cover-3d.book-cover-flip-in,
  html:not([data-motion='full']) .book-cover-3d.book-cover-flip-back {
    transform: none !important;
    opacity: 1 !important;
  }
}

/* The opposite opt-in: `data-motion="reduce"` forces the calm path even when
   the operating system does not ask for it. */
html[data-motion='reduce'] *,
html[data-motion='reduce'] *::before,
html[data-motion='reduce'] *::after {
  animation-duration: 0.01ms !important;
  animation-iteration-count: 1 !important;
  transition-duration: 0.01ms !important;
}

/* ==========================================================================
   ADAPTIVE QUALITY
   --------------------------------------------------------------------------
   The ambient dress is cheap on a GPU and expensive on a software
   rasteriser. Rather than guess from hardware hints, the app samples its own
   frame rate once the cover settles and sets `data-perf="low"` when it cannot
   hold the animation. The artwork stays; only the motion stops.

   The frozen poses below matter: `.sparkle` and `.firefly` are invisible at
   rest because their animations drive opacity from 0, so freezing them
   without an explicit opacity would make them vanish instead of hold still.
   ========================================================================== */
html[data-perf='low'] .book-cover-3d,
html[data-perf='low'] .light-rays,
html[data-perf='low'] .book-shadow,
html[data-perf='low'] .firefly,
html[data-perf='low'] .sparkle,
html[data-perf='low'] .cover-sheen {
  animation: none !important;
}

html[data-perf='low'] .light-rays {
  transform: translate(-50%, -50%) !important;
  opacity: 0.6 !important;
}

html[data-perf='low'] .book-shadow {
  transform: translateX(-50%) !important;
  opacity: 0.55 !important;
}

html[data-perf='low'] .firefly {
  transform: none !important;
  opacity: 0.45 !important;
}

html[data-perf='low'] .sparkle {
  transform: scale(0.9) !important;
  opacity: 0.5 !important;
}

html[data-perf='low'] .cover-sheen {
  transform: none !important;
  opacity: 0.22 !important;
}

/* ==========================================================================
   V3 MOBILE LAYOUT FIXES
   - reserve space for the fixed floating HUD so it never covers the chapter
     pill/heading (previously the heading sat behind the HUD on phones)
   - tighten the narration plate / prose so every beat fits without clipping
     on phone heights, including short 667px screens
   ========================================================================== */
@media (max-width: 920px) {
  .book-page,
  .story-panel {
    padding-top: 92px;
  }
}

@media (max-width: 680px) {
  .narration-plate {
    padding: 8px 10px;
    gap: 6px;
  }

  .story-beats-deck {
    align-items: stretch;
  }

  .story-prose {
    font-size: clamp(0.8rem, 3.05vw, 0.95rem);
    line-height: 1.34;
  }

  .narration-actions {
    gap: 6px;
  }

  .narration-actions .tactile-btn {
    min-height: 52px;
    padding: 0.28rem 0.45rem;
  }

  .narration-actions .cel-icon {
    width: 26px;
    height: 26px;
  }

  .manga-thought-cloud,
  .manga-speech-bubble {
    padding: 8px 10px;
    margin-top: 4px;
  }

  .manga-thought-cloud .dialogue-quote,
  .manga-speech-bubble .dialogue-quote {
    font-size: 0.78rem;
    line-height: 1.3;
  }

  .panel-header-row {
    margin-bottom: 2px;
  }

  .panel-stage {
    gap: 6px;
  }

  .floating-hud {
    top: 6px;
  }

  .hud-btn {
    min-height: 52px;
    padding: 0.25rem 0.7rem;
  }

  .hud-btn .cel-icon {
    width: 28px;
    height: 28px;
  }

  .hud-btn-label {
    font-size: 0.78rem;
  }
}

@media (max-width: 680px) and (max-height: 760px) {
  .book-page,
  .story-panel {
    padding-top: 82px;
  }

  .panel-stage {
    grid-template-rows: minmax(0, 30%) minmax(0, 70%);
  }

  .story-prose {
    font-size: 0.74rem;
    line-height: 1.24;
  }

  .narration-plate {
    padding: 6px 8px;
    gap: 4px;
  }

  .narration-actions .tactile-btn {
    min-height: 46px;
  }
}

/* ============================================================================
   Task H1 — Step 2: Illustration SVG Hotspot Engine & Outline Aura
   ============================================================================ */

.hotspot-overlay-svg {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  z-index: 6;
  pointer-events: none;
  overflow: visible;
  display: block;
}

.hotspot-halo {
  fill: none;
  stroke: #FFD54A;
  stroke-linejoin: round;
  stroke-linecap: round;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s ease;
}

.hotspot-halo--far {
  stroke: rgba(255, 213, 74, 0.15);
  stroke-width: 54px;
}

.hotspot-halo--outer {
  stroke: rgba(255, 213, 74, 0.28);
  stroke-width: 34px;
}

.hotspot-halo--mid {
  stroke: rgba(255, 228, 122, 0.48);
  stroke-width: 20px;
}

.hotspot-subject-path {
  fill: transparent;
  fill-opacity: 1;
  stroke: #FFF3B0;
  stroke-width: 11px;
  stroke-linejoin: round;
  stroke-linecap: round;
  paint-order: stroke fill;
  opacity: 0;
  pointer-events: all;
  cursor: pointer;
  outline: none;
  filter: none;
  transition: opacity 0.25s ease;
  -webkit-tap-highlight-color: transparent;
}

.hotspot-hit-path {
  fill: transparent;
  stroke: none;
  pointer-events: all;
  cursor: pointer;
}

@keyframes hotspotAuraHoldPulse {
  0%, 100% {
    opacity: 1;
  }
  50% {
    opacity: 0.72;
  }
}

.hotspot-subject-path:hover,
.hotspot-subject-path:focus-visible,
.hotspot-subject-path.is-active-aura {
  opacity: 1;
  stroke: #FFF3B0;
}

.hotspot-subject-group.is-active-aura .hotspot-halo,
.hotspot-subject-group:hover .hotspot-halo,
.hotspot-subject-group:has(.hotspot-subject-path:focus-visible, .hotspot-subject-path.is-active-aura) .hotspot-halo {
  opacity: 1;
}

.hotspot-subject-path.is-holding {
  opacity: 1;
  stroke: #FFF3B0;
  will-change: opacity;
  animation: hotspotAuraHoldPulse 1.4s ease-in-out infinite;
}

.hotspot-subject-group.is-holding .hotspot-halo {
  opacity: 1;
  animation: hotspotAuraHoldPulse 1.4s ease-in-out infinite;
}

body.debug-hotspots .hotspot-halo {
  display: none !important;
}

@media (prefers-reduced-motion: reduce) {
  .hotspot-halo,
  .hotspot-subject-path {
    transition: none;
  }

  .hotspot-subject-path:hover,
  .hotspot-subject-path:focus-visible,
  .hotspot-subject-path.is-active-aura,
  .hotspot-subject-path.is-holding {
    opacity: 1;
    stroke: #FFF3B0;
    animation: none;
  }

  .hotspot-subject-group.is-holding .hotspot-halo {
    animation: none;
  }
}

html[data-motion="reduce"] .hotspot-halo,
html[data-motion="reduce"] .hotspot-subject-path {
  transition: none;
}

html[data-motion="reduce"] .hotspot-subject-path:hover,
html[data-motion="reduce"] .hotspot-subject-path:focus-visible,
html[data-motion="reduce"] .hotspot-subject-path.is-active-aura,
html[data-motion="reduce"] .hotspot-subject-path.is-holding {
  opacity: 1;
  stroke: #FFF3B0;
  animation: none;
}

html[data-motion="reduce"] .hotspot-subject-group.is-holding .hotspot-halo {
  animation: none;
}

.hotspot-text-pop {
  font-family: var(--font-heading, 'Fredoka', sans-serif);
  font-weight: 800;
  font-size: 46px;
  fill: #FFF3B0;
  stroke: #3A2308;
  stroke-width: 8px;
  paint-order: stroke fill;
  text-anchor: middle;
  pointer-events: none;
  user-select: none;
  filter: drop-shadow(0 4px 8px rgba(0, 0, 0, 0.55));
}

.hotspot-debug-label {
  display: none;
}

body.debug-hotspots .hotspot-subject-path {
  opacity: 1 !important;
  filter: none !important;
  stroke-width: 8px !important;
  stroke-linejoin: round;
  stroke-linecap: round;
}

body.debug-hotspots .hotspot-subject-path[data-color-idx="0"] {
  stroke: #FF2E93 !important;
  fill: rgba(255, 46, 147, 0.28) !important;
}

body.debug-hotspots .hotspot-subject-path[data-color-idx="1"] {
  stroke: #00F5D4 !important;
  fill: rgba(0, 245, 212, 0.28) !important;
}

body.debug-hotspots .hotspot-subject-path[data-color-idx="2"] {
  stroke: #FEE440 !important;
  fill: rgba(254, 228, 64, 0.28) !important;
}

body.debug-hotspots .hotspot-subject-path[data-color-idx="3"] {
  stroke: #00BBF9 !important;
  fill: rgba(0, 187, 249, 0.28) !important;
}

body.debug-hotspots .hotspot-subject-path[data-color-idx="4"] {
  stroke: #9B5DE5 !important;
  fill: rgba(155, 93, 229, 0.28) !important;
}

body.debug-hotspots .hotspot-subject-path[data-color-idx="5"] {
  stroke: #FF9F1C !important;
  fill: rgba(255, 159, 28, 0.28) !important;
}

body.debug-hotspots .hotspot-debug-label {
  display: block;
  font-family: 'Fredoka', sans-serif;
  font-weight: 800;
  font-size: 40px;
  fill: #FFFFFF;
  stroke: #000000;
  stroke-width: 9px;
  paint-order: stroke fill;
  text-anchor: middle;
  dominant-baseline: central;
  pointer-events: none;
}

