/* Cantastorie player shell — hand-crafted watercolor CSS.
   Faithful to Claude Design "Cantastorie Prototype". Wobble belongs to the
   child's world: blob radii 42–58% / 40–60%, tilts ±1.5–3° alternating,
   sticker borders. Child tap targets ≥ 96px. */

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  font-family: var(--font-app);
  background: var(--surface);
  -webkit-tap-highlight-color: transparent;
  user-select: none;
}

/* The app frame: mobile-first, so on a phone #app IS the viewport and every
   screen is full-bleed. On a larger viewport the frame caps at a phone width
   and centers, lifted subtly off the backdrop — a held-in-hand shelf, never
   a magnified one. Screens are absolute inset:0 within this frame, so the
   full-bleed page art fills the frame edge to edge at every size. */
#app {
  position: absolute;
  inset: 0;
  max-width: var(--app-max);
  margin-inline: auto;
  overflow: hidden;
}

@media (min-width: 600px) {
  #app {
    box-shadow: 0 0 80px rgba(0, 0, 0, 0.28);
  }
}

.screen {
  position: absolute;
  inset: 0;
}

.screen {
  transition: opacity var(--fade);
}

button {
  all: unset;
  cursor: pointer;
}

/* `all: unset` strips the browser's focus ring, so keyboard focus gets one
   back (WCAG 2.4.7). :focus-visible keeps it off taps and clicks. */
:focus-visible {
  outline: 3px solid var(--focus-ring, #A88BE0);
  outline-offset: 3px;
}

/* Read aloud, never painted: the page-turn live region (AI-498). */
.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  border: 0;
}

button:active .presses,
button.presses:active {
  transform: scale(0.95);
}

/* ── Shelf ─────────────────────────────────────────────────────────── */

.shelf {
  /* Indigo tokens in both modes (AI-453 retired the warm browns). */
  background: var(--page-glow), var(--surface);
  display: flex;
  flex-direction: column;
  padding: max(24px, env(safe-area-inset-top)) 20px 0;
  isolation: isolate;
}

/* With room to spare (a tall frame), center the shelf column vertically so
   it isn't a top-aligned strip; short viewports keep it top-aligned so
   nothing clips. */
@media (min-height: 760px) {
  .shelf {
    justify-content: center;
    padding-bottom: 40px;
  }
}

.greeting {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 10px 4px 22px;
}

.greeting h1 {
  margin: 0;
  font: 700 30px/1.1 var(--font-app);
  color: var(--ink);
}

.greeting p {
  margin: 0;
  font: 600 16px/1.25 var(--font-app);
  color: var(--greeting-sub);
}

/* Sun mascot (light) / sphere-lit moon (dusk) */
.mascot {
  width: 58px;
  height: 58px;
  border-radius: 50%;
  background: radial-gradient(circle at 34% 30%, #f7e8b0, #e8b44c 55%, #c98a1a 100%);
  box-shadow:
    0 0 0 10px rgba(232, 180, 76, 0.15),
    inset -7px -5px 14px rgba(120, 70, 0, 0.28);
  position: relative;
  flex: none;
}

/* Eyes */
.mascot::before,
.mascot::after {
  content: "";
  position: absolute;
  top: 22px;
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: #4a3b2e;
}

.mascot::before {
  left: 16px;
}

.mascot::after {
  right: 15px;
}

.mascot .smile {
  position: absolute;
  top: 33px;
  left: 52%;
  transform: translateX(-50%);
  width: 20px;
  height: 10px;
  border-radius: 0 0 20px 20px;
  background: #4a3b2e;
}

/* Crater discs — purely decorative */
.mascot .crater-a,
.mascot .crater-b,
.mascot .crater-c {
  position: absolute;
  border-radius: 50%;
  background: rgba(120, 70, 0, 0.14);
  pointer-events: none;
}

.mascot .crater-a {
  width: 8px;
  height: 8px;
  top: 10px;
  right: 14px;
}

.mascot .crater-b {
  width: 6px;
  height: 6px;
  bottom: 16px;
  right: 10px;
}

.mascot .crater-c {
  width: 4px;
  height: 4px;
  bottom: 12px;
  right: 22px;
}

/* Dusk: sphere-lit moon — radial highlight from upper-left, terminator shadow */
[data-theme="dusk"] .mascot {
  background: radial-gradient(circle at 34% 30%, var(--moon-light), var(--moon-mid) 52%, var(--accent) 100%);
  box-shadow:
    0 0 26px 7px var(--accent-22),
    inset -7px -5px 14px var(--moon-shade);
}

[data-theme="dusk"] .mascot::before,
[data-theme="dusk"] .mascot::after,
[data-theme="dusk"] .mascot .smile {
  background: var(--moon-ink);
}

/* Moon eyes — narrow slit at dusk */
[data-theme="dusk"] .mascot::before,
[data-theme="dusk"] .mascot::after {
  top: 27px;
  width: 12px;
  height: 4px;
  border-radius: 0 0 10px 10px;
}

[data-theme="dusk"] .mascot::before {
  left: 13px;
}

[data-theme="dusk"] .mascot::after {
  right: 12px;
}

[data-theme="dusk"] .mascot .smile {
  top: 38px;
  width: 14px;
  height: 7px;
  border-radius: 0 0 14px 14px;
}

/* Moon craters slightly more visible at dusk */
[data-theme="dusk"] .mascot .crater-a,
[data-theme="dusk"] .mascot .crater-b,
[data-theme="dusk"] .mascot .crater-c {
  background: var(--moon-crater);
}

.covers {
  display: grid;
  /* minmax(0, 1fr): a bare 1fr track cannot shrink below its content's
     min-content width, so cover art at its natural size pushed each column
     past half the shelf and the right column was clipped (#180). */
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  gap: 18px 16px;
  /* 12px sides: the cover's 7px sticker ring plus its 2deg tilt reach past
     the card, and a narrower padding cut the ring flat at the shelf edges. */
  padding: 6px 12px 0;
  /* Own stacking context: the captions' z-index lifts them above the covers
     only, never above the shelf's bottom fade or its buttons (#204). */
  isolation: isolate;
  flex: 1 1 auto;
  min-height: 0;
  overflow-x: hidden;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  align-content: start;
  grid-auto-rows: max-content;
  padding-bottom: 96px;
}

.empty-shelf {
  grid-column: 1 / -1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 24px;
  padding: 48px 24px;
  text-align: center;
}

.meadow-bird {
  width: 72px;
  height: 72px;
  border-radius: 50% 42% 50% 40%;
  background: var(--accent-22);
  opacity: 0.5;
}

.empty-shelf-text {
  font-size: 1.1rem;
  color: var(--ink-soft);
  margin: 0;
  line-height: 1.5;
}

.cover-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  min-width: 0;
}

.cover-caption {
  /* Paint above the tilted, positioned .cover: its sticker ring otherwise
     covered the top of the first line, where Devanagari vowel loops sit (#204). */
  position: relative;
  z-index: 1;
  font: 700 14px/1.2 var(--font-app, system-ui);
  text-align: center;
  color: var(--caption-ink, var(--ink-soft));
  max-width: 100%;
  overflow-wrap: anywhere;
  padding: 0 4px;
}

.cover {
  /* Explicit width: inside the centred .cover-card flex column a width-less
     button shrink-wraps its content, so a wash-only cover (no <img>) collapsed
     to 0×0 — invisible and untappable. */
  width: 100%;
  min-width: 0;
  max-width: 100%;
  aspect-ratio: 5 / 6;
  box-shadow: var(--sticker-ring), var(--card-shadow);
  display: flex;
  flex-direction: column;
  align-items: stretch;
  transition: transform var(--settle), box-shadow var(--settle);
  overflow: hidden;
  position: relative;
}

@media (hover: hover) {
  .cover:hover {
    box-shadow: var(--sticker-ring), 0 14px 30px rgba(74, 59, 46, 0.22);
  }
}

.cover-art {
  display: block;
  width: 100%;
  min-width: 0;
  max-width: 100%;
  aspect-ratio: 5 / 6;
  object-fit: cover;
}

/* Wobble — four cover shapes, alternating tilt. Keyed on the card: each
   .cover is the only button in its .cover-card, so .cover:nth-child is always 1. */
.cover-card:nth-child(4n+1) .cover {
  border-radius: 58% 42% 55% 45% / 52% 48% 40% 60%;
  transform: rotate(-2deg);
}

.cover-card:nth-child(4n+2) .cover {
  border-radius: 45% 55% 48% 52% / 55% 45% 58% 42%;
  transform: rotate(2deg);
}

.cover-card:nth-child(4n+3) .cover {
  border-radius: 50% 50% 44% 56% / 46% 54% 52% 48%;
  transform: rotate(1.5deg);
}

.cover-card:nth-child(4n+4) .cover {
  border-radius: 52% 48% 58% 42% / 50% 50% 45% 55%;
  transform: rotate(-1.5deg);
}

/* Family covers — violet ring + CSS star badge */
.cover--family {
  box-shadow: 0 0 0 3px var(--accent), var(--card-shadow);
}

@media (hover: hover) {
  .cover--family:hover {
    box-shadow: 0 0 0 3px var(--accent), 0 14px 30px rgba(74, 59, 46, 0.22);
  }
}

.cover--family::after {
  content: "";
  position: absolute;
  top: 6px;
  right: 6px;
  /* CSS star via clip-path — no SVG, no hex */
  clip-path: polygon(
    50% 0%, 61% 35%, 98% 35%, 68% 57%,
    79% 91%, 50% 70%, 21% 91%, 32% 57%,
    2% 35%, 39% 35%
  );
  width: 14px;
  height: 14px;
  background: var(--accent);
  pointer-events: none;
}

.cover:active {
  transform: scale(0.96);
}

.cover.loading {
  position: relative;
  overflow: hidden;
}

.cover.loading::after {
  content: "";
  position: absolute;
  inset: 0;
  background: linear-gradient(
    90deg,
    transparent 0%,
    rgba(245, 223, 174, 0.15) 50%,
    transparent 100%
  );
  animation: shimmer 1.5s ease-in-out infinite;
}

@keyframes shimmer {
  0% {
    transform: translateX(-100%);
  }
  100% {
    transform: translateX(100%);
  }
}

@media (prefers-reduced-motion: reduce) {
  .cover.loading::after {
    animation: none;
  }
}

/* Watercolor washes: 2–3 radial gradients of accents over a warm base */
.wash-barchetta {
  background:
    radial-gradient(120% 90% at 25% 20%, rgba(242, 217, 160, 0.9), rgba(242, 217, 160, 0) 60%),
    radial-gradient(110% 100% at 75% 80%, rgba(127, 166, 168, 0.8), rgba(127, 166, 168, 0) 65%),
    linear-gradient(165deg, #fdf8ef, #dbe4dc);
}

.wash-panetteria {
  background:
    radial-gradient(120% 90% at 70% 25%, rgba(201, 113, 79, 0.6), rgba(201, 113, 79, 0) 60%),
    radial-gradient(110% 100% at 25% 75%, rgba(143, 163, 126, 0.65), rgba(143, 163, 126, 0) 65%),
    linear-gradient(165deg, #fdf8ef, #f0dfc8);
}

.wash-bosco {
  background:
    radial-gradient(120% 90% at 30% 70%, rgba(143, 163, 126, 0.75), rgba(143, 163, 126, 0) 62%),
    radial-gradient(100% 90% at 75% 20%, rgba(224, 166, 63, 0.55), rgba(224, 166, 63, 0) 60%),
    linear-gradient(165deg, #fbf5e9, #e0e2c9);
}

.wash-guanto {
  background:
    radial-gradient(120% 90% at 60% 30%, rgba(127, 166, 168, 0.65), rgba(127, 166, 168, 0) 60%),
    radial-gradient(110% 100% at 30% 85%, rgba(201, 113, 79, 0.45), rgba(201, 113, 79, 0) 60%),
    linear-gradient(165deg, #fcf7ed, #d9e2db);
}

[data-theme="dusk"] .wash-barchetta {
  background:
    radial-gradient(120% 90% at 25% 20%, rgba(245, 223, 174, 0.4), rgba(245, 223, 174, 0) 60%),
    radial-gradient(110% 100% at 75% 80%, rgba(127, 166, 168, 0.45), rgba(127, 166, 168, 0) 65%),
    linear-gradient(165deg, #3b3022, #33342e);
}

[data-theme="dusk"] .wash-panetteria {
  background:
    radial-gradient(120% 90% at 70% 25%, rgba(217, 139, 102, 0.5), rgba(217, 139, 102, 0) 60%),
    linear-gradient(165deg, #3b3022, #3e2e23);
}

[data-theme="dusk"] .wash-bosco {
  background:
    radial-gradient(120% 90% at 30% 70%, rgba(152, 165, 131, 0.5), rgba(152, 165, 131, 0) 62%),
    radial-gradient(100% 90% at 75% 20%, rgba(232, 183, 90, 0.4), rgba(232, 183, 90, 0) 60%),
    linear-gradient(165deg, #37301f, #2e3226);
}

[data-theme="dusk"] .wash-guanto {
  background:
    radial-gradient(120% 90% at 60% 30%, rgba(127, 166, 168, 0.45), rgba(127, 166, 168, 0) 60%),
    radial-gradient(110% 100% at 30% 85%, rgba(217, 139, 102, 0.35), rgba(217, 139, 102, 0) 60%),
    linear-gradient(165deg, #3b3022, #2c3129);
}

.settings-gear {
  position: absolute;
  left: 20px;
  bottom: 28px;
  width: 56px;
  height: 56px;
  border-radius: 50% 50% 46% 54% / 54% 46% 50% 50%;
  background: var(--sticker);
  transform: rotate(-2deg);
  box-shadow: var(--btn-shadow), 0 0 0 4px var(--primary-16);
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--ink);
  cursor: pointer;
  transition: transform 0.15s ease, box-shadow 0.15s ease;
}

.shelf::after {
  content: '';
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  height: 120px;
  background: linear-gradient(to top, var(--surface) 40%, transparent);
  pointer-events: none;
  z-index: 0;
}

.settings-gear,
.lang-sticker {
  z-index: 1;
}

/* 2×2 CSS dots replace the gear SVG */
.settings-dots {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 5px;
  width: 20px;
  height: 20px;
}

.settings-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: currentColor;
}

.settings-gear:active {
  transform: rotate(-2deg) scale(0.95);
}

@media (hover: hover) {
  .settings-gear:hover {
    transform: rotate(-2deg) scale(1.06);
    box-shadow: var(--btn-shadow), 0 0 0 4px var(--primary-ring);
  }
}

[data-theme="dusk"] .settings-gear {
  background: var(--ghost);
  box-shadow: inset 0 0 0 2px var(--primary-ring), 0 0 24px var(--primary-16);
  color: var(--ink);
}

/* ── Settings sheet (bottom sheet, replaces centred panel) ────────── */

.settings-backdrop {
  position: fixed;
  inset: 0;
  z-index: 10;
  background: rgba(0, 0, 0, 0.45);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  animation: backdrop-in 0.22s ease-out;
}

@keyframes backdrop-in {
  from { opacity: 0; }
  to   { opacity: 1; }
}

.settings-sheet {
  background: var(--card);
  border-radius: 30px 30px 0 0;
  padding: 12px 20px 28px;
  width: 100%;
  max-width: var(--app-max);
  max-height: 88%;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  display: flex;
  flex-direction: column;
  gap: 20px;
  box-shadow: 0 -8px 40px rgba(0, 0, 0, 0.22);
  animation: sheet-up 0.28s cubic-bezier(0.22, 0.61, 0.36, 1);
}

@keyframes sheet-up {
  from { transform: translateY(100%); }
  to   { transform: translateY(0); }
}

@media (prefers-reduced-motion: reduce) {
  .settings-backdrop { animation: none; }
  .settings-sheet    { animation: none; }
}

/* Drag handle */
.settings-handle {
  align-self: center;
  flex-shrink: 0;
  width: 36px;
  height: 4px;
  border-radius: 999px;
  background: var(--ink-faint, rgba(0, 0, 0, 0.18));
  margin-bottom: 4px;
}

/* Section labels */
.settings-section {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.settings-section-label {
  font: 700 12px var(--font-app);
  color: var(--ink-soft, var(--ink));
  text-transform: uppercase;
  letter-spacing: 0.09em;
}

/* ── Language tiles (2-col grid) ─────────────────────────────────── */
.settings-lang-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.settings-lang-tile {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  border-radius: 14px;
  min-height: 44px;
  font: 600 14px var(--font-app);
  color: var(--ink);
  background: transparent;
  box-shadow: inset 0 0 0 1.5px var(--primary-outline, rgba(0,0,0,.12));
  cursor: pointer;
  transition: background 0.12s ease, box-shadow 0.12s ease;
}

.settings-lang-tile.selected {
  background: var(--primary-16);
  box-shadow: inset 0 0 0 2px var(--primary-ring);
}

.settings-lang-tile:active {
  transform: scale(0.97);
}

.settings-lang-flag {
  font-size: 20px;
  line-height: 1;
  pointer-events: none;
}

.settings-lang-name {
  font: 600 13px var(--font-app);
  color: var(--ink);
  pointer-events: none;
}

/* ── Light tiles (3-col row) ─────────────────────────────────────── */
.settings-light-grid {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 8px;
}

.settings-light-tile {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  padding: 12px 6px 10px;
  border-radius: 14px;
  min-height: 64px;
  font: 600 12px var(--font-app);
  color: var(--ink);
  background: transparent;
  box-shadow: inset 0 0 0 1.5px var(--primary-outline, rgba(0,0,0,.12));
  cursor: pointer;
  transition: background 0.12s ease, box-shadow 0.12s ease;
}

.settings-light-tile.selected {
  background: var(--primary-16);
  box-shadow: inset 0 0 0 2px var(--primary-ring);
}

.settings-light-tile:active {
  transform: scale(0.97);
}

.settings-light-label {
  font: 600 12px var(--font-app);
  color: var(--ink);
  pointer-events: none;
}

/* CSS shape icons for light tiles */
.settings-light-icon {
  flex-shrink: 0;
  width: 22px;
  height: 22px;
  pointer-events: none;
}

/* Day — full circle (sun) */
.settings-light-icon--lightDay {
  border-radius: 50%;
  background: var(--primary);
}

/* Auto — half-circle (half day, half dusk) */
.settings-light-icon--lightAuto {
  border-radius: 50%;
  background: linear-gradient(90deg, var(--primary) 50%, rgba(0,0,0,.35) 50%);
}

/* Evening — circle with dark overlay (moon-like) */
.settings-light-icon--lightEvening {
  border-radius: 50%;
  background: rgba(0, 0, 0, 0.50);
  box-shadow: inset 5px -4px 0 0 var(--primary);
}

.settings-light-hint {
  margin: 0;
  font: 400 12px var(--font-app);
  color: var(--ink-soft, var(--ink));
  opacity: 0.7;
}

/* ── Grown-ups rows ───────────────────────────────────────────────── */
.settings-row {
  /* width: 100% plus padding must stay inside the sheet (buttons are all: unset). */
  box-sizing: border-box;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 4px;
  min-height: 44px;
  border-radius: 0;
  width: 100%;
  cursor: default;
}

.settings-row--workshop {
  cursor: pointer;
  border-radius: 14px;
  padding: 12px;
  box-shadow: inset 0 0 0 1.5px var(--primary-outline, rgba(0,0,0,.12));
  transition: background 0.12s ease;
}

.settings-row--workshop:active {
  background: var(--primary-16);
}

.settings-row-label {
  font: 600 15px var(--font-app);
  color: var(--ink);
}

/* Toggle switch */
.settings-toggle {
  position: relative;
  width: 48px;
  height: 28px;
  border-radius: 999px;
  background: var(--primary-outline, rgba(0,0,0,.18));
  box-shadow: inset 0 0 0 1.5px var(--primary-outline, rgba(0,0,0,.12));
  flex-shrink: 0;
  cursor: pointer;
  transition: background 0.18s ease;
}

.settings-toggle.on {
  background: var(--primary);
  box-shadow: none;
}

.settings-toggle-thumb {
  position: absolute;
  top: 3px;
  left: 3px;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: var(--card);
  box-shadow: 0 1px 4px rgba(0,0,0,.20);
  transition: transform 0.18s ease;
  pointer-events: none;
}

.settings-toggle.on .settings-toggle-thumb {
  transform: translateX(20px);
}

/* Lock icon (CSS shape) — shackle + body */
.settings-lock-icon {
  position: relative;
  width: 20px;
  height: 22px;
  flex-shrink: 0;
}

.settings-lock-shackle {
  position: absolute;
  top: 0;
  left: 3px;
  width: 14px;
  height: 10px;
  border: 2.5px solid var(--ink);
  border-bottom: none;
  border-radius: 7px 7px 0 0;
}

.settings-lock-body {
  position: absolute;
  bottom: 0;
  left: 0;
  width: 20px;
  height: 14px;
  background: var(--ink);
  border-radius: 4px;
}

/* ── Close pill ──────────────────────────────────────────────────── */
.settings-close-pill {
  align-self: stretch;
  padding: 14px 24px;
  border-radius: 999px;
  font: 700 16px var(--font-app);
  color: var(--primary-text);
  background: var(--primary);
  box-shadow: 0 4px 14px rgba(0,0,0,.18);
  cursor: pointer;
  text-align: center;
  min-height: 50px;
  transition: opacity 0.12s ease;
}

.settings-close-pill:active {
  opacity: 0.85;
  transform: scale(0.98);
}

/* ── Page text for Read-with-me ──────────────────────────────────── */
.player-page-text {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 100px;
  z-index: 2;
  margin: 0 20px;
  padding: 12px 16px;
  border-radius: 16px;
  background: rgba(0, 0, 0, 0.54);
  color: var(--moonlight, #f2d8a7);
  font: 500 16px/1.55 var(--font-app);
  text-align: center;
  pointer-events: none;
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
}

/* ── Legacy settings-panel (kept for any existing tests / compat) ── */
.settings-section-label,
.settings-label {
  font: 700 12px var(--font-app);
  color: var(--ink-soft, var(--ink));
  text-transform: uppercase;
  letter-spacing: 0.09em;
}

.settings-select {
  position: relative;
}

.settings-select-current {
  box-sizing: border-box;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  width: 100%;
  padding: 12px 16px;
  border-radius: 16px;
  font: 700 15px var(--font-app);
  color: var(--ink);
  background: var(--sticker);
  box-shadow: inset 0 0 0 1.5px rgba(74, 59, 46, 0.18);
  cursor: pointer;
}

[data-theme="dusk"] .settings-select-current {
  color: #f3e9da;
  background: rgba(243, 233, 218, 0.08);
  box-shadow: inset 0 0 0 1.5px rgba(245, 223, 174, 0.25);
}

.settings-select-chevron {
  opacity: 0.55;
  transition: transform 0.2s ease;
}

.settings-select-current[aria-expanded="true"] .settings-select-chevron {
  transform: rotate(180deg);
}

.settings-menu {
  position: fixed;
  z-index: 3;
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 6px;
  border-radius: 16px;
  background: var(--surface);
  box-shadow: 0 12px 30px rgba(0, 0, 0, 0.35);
  max-height: 260px;
  overflow-y: auto;
}

[data-theme="dusk"] .settings-menu {
  background: #2a2119;
}

.settings-menu[aria-hidden="true"] {
  display: none;
}

.settings-menu-item {
  padding: 12px 10px;
  border-radius: 10px;
  text-align: left;
  font: 700 15px var(--font-app);
  color: var(--ink);
  cursor: pointer;
}

[data-theme="dusk"] .settings-menu-item {
  color: #f3e9da;
}

.settings-menu-item[aria-current="true"] {
  box-shadow: inset 0 0 0 2px var(--sage);
  background: rgba(127, 166, 168, 0.22);
}

@media (hover: hover) {
  .settings-menu-item:hover {
    background: rgba(127, 166, 168, 0.14);
  }
}

.settings-done {
  align-self: center;
  margin-top: 4px;
  padding: 12px 28px;
  border-radius: 999px;
  font: 700 16px var(--font-app);
  color: #f5dfae;
  background: rgba(245, 223, 174, 0.16);
  box-shadow: inset 0 0 0 1.5px rgba(245, 223, 174, 0.4);
  cursor: pointer;
}

.settings-done:active {
  transform: scale(0.96);
}

@media (hover: hover) {
  .settings-done:hover {
    background: rgba(245, 223, 174, 0.24);
  }
}

.lang-sticker {
  position: absolute;
  right: 20px;
  bottom: 28px;
  width: 48px;
  height: 48px;
  border-radius: 50% 46% 54% 50% / 50% 54% 46% 50%;
  background: var(--sticker);
  transform: rotate(2deg);
  box-shadow: var(--btn-shadow), 0 0 0 4px var(--info-20);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: transform 0.15s ease, box-shadow 0.15s ease;
}

.lang-sticker-label {
  font: 700 11px/1 var(--font-app);
  color: var(--ink);
  letter-spacing: 0.04em;
  pointer-events: none;
}

.lang-sticker:active {
  transform: rotate(2deg) scale(0.95);
}

@media (hover: hover) {
  .lang-sticker:hover {
    transform: rotate(2deg) scale(1.06);
    box-shadow: var(--btn-shadow), 0 0 0 4px var(--info-ring);
  }
}

[data-theme="dusk"] .lang-sticker {
  background: var(--ghost);
  box-shadow: inset 0 0 0 2px var(--info-ring), 0 0 24px var(--info-20);
}

[data-theme="dusk"] .lang-sticker-label {
  color: var(--ink);
}

/* Stars (dusk shelf, player, end) */
.stars {
  position: absolute;
  width: 3px;
  height: 3px;
  border-radius: 50%;
  background: rgba(245, 223, 174, 0.7);
  box-shadow:
    70px 34px 0 0 rgba(245, 223, 174, 0.4),
    180px -14px 0 1px rgba(245, 223, 174, 0.55),
    230px 50px 0 0 rgba(245, 223, 174, 0.3);
}

/* ── Player ────────────────────────────────────────────────────────── */

.player {
  background: #2a2119;
}

.page-wash {
  position: absolute;
  inset: 0;
  opacity: 0;
  transition: opacity var(--fade);
}

.page-wash.current {
  opacity: 1;
}

/* Full-bleed page art from the published story, layered over the wash;
   it rides the same slow opacity ramp — nothing snaps at bedtime.
   Images are generated in portrait orientation to match the phone screen. */
.page-art {
  position: absolute;
  inset: 0;
  opacity: 0;
  transition: opacity var(--fade);
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
}

.page-art.current {
  opacity: 1;
}

/* Bead progress — read like beads on a string, never a number */
.beads {
  position: absolute;
  top: max(28px, env(safe-area-inset-top));
  left: 0;
  right: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 10px;
  padding-top: 8px;
}

.bead {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  opacity: 0.3;
  transition: opacity 500ms ease;
}

.bead.past {
  opacity: 0.75;
}

.bead.current {
  opacity: 1;
  box-shadow: 0 0 8px 2px rgba(245, 223, 174, 0.6);
}

.exit {
  position: absolute;
  top: max(26px, env(safe-area-inset-top));
  left: 18px;
  width: 52px;
  height: 52px;
  border-radius: 50%;
  background: rgba(243, 233, 218, 0.09);
  box-shadow: inset 0 0 0 1.5px rgba(243, 233, 218, 0.18);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform var(--settle);
}

.exit:active {
  transform: scale(0.92);
}

@media (hover: hover) {
  .exit:hover {
    background: rgba(243, 233, 218, 0.16);
    box-shadow: inset 0 0 0 1.5px rgba(243, 233, 218, 0.3);
  }
}

.exit .grid {
  display: grid;
  grid-template-columns: 9px 9px;
  gap: 3px;
}

.exit .grid div {
  height: 9px;
  border-radius: 3px;
  background: rgba(243, 233, 218, 0.55);
}

/* Play-pause: 140px, the only player control */
.play-pause {
  position: absolute;
  bottom: max(50px, env(safe-area-inset-bottom));
  left: 50%;
  transform: translateX(-50%) rotate(-2deg);
  width: 140px;
  height: 140px;
  border-radius: 52% 48% 50% 50% / 50% 50% 55% 45%;
  background: rgba(243, 233, 218, 0.1);
  backdrop-filter: blur(6px);
  box-shadow:
    0 0 0 3px rgba(245, 223, 174, 0.4),
    0 0 44px 6px rgba(245, 223, 174, 0.15),
    0 14px 34px rgba(0, 0, 0, 0.4);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform var(--settle);
}

.play-pause:active {
  transform: translateX(-50%) rotate(-2deg) scale(0.94);
}

@media (hover: hover) {
  .play-pause:hover {
    box-shadow:
      0 0 0 3px rgba(245, 223, 174, 0.5),
      0 0 52px 8px rgba(245, 223, 174, 0.22),
      0 14px 34px rgba(0, 0, 0, 0.4);
  }
}

.icon-play {
  width: 0;
  height: 0;
  border-left: 42px solid var(--moonlight, #f2d8a7);
  border-top: 27px solid transparent;
  border-bottom: 27px solid transparent;
  margin-left: 10px;
}

.icon-pause {
  display: flex;
  gap: 12px;
}

.icon-pause div {
  width: 14px;
  height: 48px;
  border-radius: 7px;
  background: var(--moonlight, #f2d8a7);
}

/* Prev/next page — quiet sidekicks to the play-pause, hidden only where
   there is nothing to go back to. The branch is never skippable: next on
   the choice page hands the child the two cards instead of turning. */
.nav {
  position: absolute;
  bottom: calc(max(50px, env(safe-area-inset-bottom)) + 30px);
  width: 80px;
  height: 80px;
  border-radius: 50%;
  background: rgba(243, 233, 218, 0.09);
  box-shadow: inset 0 0 0 1.5px rgba(243, 233, 218, 0.18);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform var(--settle);
}

.nav:active {
  transform: scale(0.92);
}

.nav.disabled {
  visibility: hidden;
}

@media (hover: hover) {
  .nav:hover {
    background: rgba(243, 233, 218, 0.16);
    box-shadow: inset 0 0 0 1.5px rgba(243, 233, 218, 0.3);
  }
}

.nav-prev {
  /* Clamped so a narrow phone (<336px) never clips the chevrons off-screen. */
  left: max(8px, calc(50% - 168px));
}

.nav-next {
  right: max(8px, calc(50% - 168px));
}

/* Below 336px the clamped chevrons close in on the 140px play-pause; a
   slightly smaller button keeps clear air between them (still ≥48px). */
@media (max-width: 335px) {
  .nav {
    width: 70px;
    height: 70px;
  }
}

.chevron-left {
  width: 0;
  height: 0;
  border-top: 18px solid transparent;
  border-bottom: 18px solid transparent;
  border-right: 25px solid var(--moonlight, #f2d8a7);
  margin-left: -5px;
}

.chevron-right {
  width: 0;
  height: 0;
  border-top: 18px solid transparent;
  border-bottom: 18px solid transparent;
  border-left: 25px solid var(--moonlight, #f2d8a7);
  margin-right: -5px;
}

/* Page washes — one watercolor night per page of "La barchetta e la luna" */
.wash-p0 {
  background:
    radial-gradient(60% 32% at 70% 20%, rgba(245, 223, 174, 0.6), rgba(245, 223, 174, 0) 68%),
    radial-gradient(120% 80% at 28% 78%, rgba(94, 124, 138, 0.7), rgba(94, 124, 138, 0) 75%),
    linear-gradient(178deg, #2a2119 8%, #33342e 55%, #20261f);
}

.wash-p1 {
  background:
    radial-gradient(70% 40% at 30% 30%, rgba(127, 166, 168, 0.55), rgba(127, 166, 168, 0) 70%),
    linear-gradient(178deg, #26223c 8%, #2e3a4a 60%, #1e2a36);
}

.wash-p2 {
  background:
    radial-gradient(55% 35% at 65% 65%, rgba(152, 165, 131, 0.6), rgba(152, 165, 131, 0) 72%),
    radial-gradient(40% 25% at 30% 25%, rgba(245, 223, 174, 0.4), rgba(245, 223, 174, 0) 70%),
    linear-gradient(178deg, #2a2119 8%, #2e3226 60%, #20261f);
}

.wash-p3 {
  background:
    radial-gradient(65% 40% at 70% 35%, rgba(232, 183, 90, 0.55), rgba(232, 183, 90, 0) 70%),
    linear-gradient(178deg, #33281b 8%, #3b3022 60%, #241d14);
}

.wash-p4 {
  background:
    radial-gradient(60% 38% at 40% 60%, rgba(217, 139, 102, 0.5), rgba(217, 139, 102, 0) 72%),
    linear-gradient(178deg, #2a2119 8%, #3a2e28 60%, #20261f);
}

.wash-p5 {
  background:
    radial-gradient(60% 32% at 70% 20%, rgba(245, 223, 174, 0.7), rgba(245, 223, 174, 0) 66%),
    radial-gradient(120% 70% at 40% 75%, rgba(94, 124, 138, 0.8), rgba(94, 124, 138, 0) 75%),
    linear-gradient(178deg, #26223c 8%, #2e3a4a 60%, #1e2a36);
}

.wash-p6 {
  background:
    radial-gradient(50% 30% at 55% 40%, rgba(216, 226, 228, 0.5), rgba(216, 226, 228, 0) 70%),
    linear-gradient(178deg, #23343f 8%, #2e4650 60%, #1c2b33);
}

.wash-p7 {
  background:
    radial-gradient(45% 28% at 68% 24%, rgba(245, 223, 174, 0.45), rgba(245, 223, 174, 0) 68%),
    linear-gradient(178deg, #221d33 8%, #2a2743 60%, #1a1728);
}

/* ── Overlays: choice + resume ─────────────────────────────────────── */

.overlay {
  position: absolute;
  inset: 0;
  background: rgba(14, 10, 6, 0.5);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 30px;
}

.overlay .prompt {
  font: 700 24px var(--font-app);
  color: #f5dfae;
  text-align: center;
}

.overlay .prompt strong {
  display: block;
  font: 700 28px var(--font-app);
}

.overlay .prompt small {
  display: block;
  font: 600 16px var(--font-app);
  color: rgba(243, 233, 218, 0.75);
}

.options {
  display: flex;
  gap: 16px;
  align-items: center;
}

.option {
  display: flex;
  flex-direction: column;
  gap: 12px;
  align-items: center;
  min-height: 96px;
  transition: transform var(--settle);
}

.option:nth-child(odd) {
  transform: rotate(-3deg);
}

.option:nth-child(even) {
  transform: rotate(3deg);
}

.option:nth-child(odd):active {
  transform: rotate(-3deg) scale(0.95);
}

.option:nth-child(even):active {
  transform: rotate(3deg) scale(0.95);
}

@media (hover: hover) {
  .option:hover .blob-button,
  .option:hover .choice-card {
    box-shadow:
      0 0 0 6px rgba(245, 223, 174, 0.45),
      0 0 48px 6px rgba(245, 223, 174, 0.22),
      0 18px 40px rgba(0, 0, 0, 0.55);
  }
}

.option .pill {
  padding: 8px 20px;
  border-radius: 999px;
  background: rgba(245, 223, 174, 0.16);
  box-shadow: inset 0 0 0 1.5px rgba(245, 223, 174, 0.35);
  font: 700 17px var(--font-app);
  color: #f5dfae;
}

.choice-card {
  width: 164px;
  height: 196px;
  box-shadow:
    0 0 0 6px rgba(245, 223, 174, 0.35),
    0 0 44px 4px rgba(245, 223, 174, 0.18),
    0 18px 40px rgba(0, 0, 0, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

/* The idle choice nudge (M31): after 30 seconds the cards glow softly,
   beside the spoken nudge, asking for a tap. */
.options.nudge .choice-card {
  animation: choice-nudge 2.4s ease-in-out infinite;
}

@keyframes choice-nudge {
  0%,
  100% {
    box-shadow:
      0 0 0 6px rgba(245, 223, 174, 0.35),
      0 0 44px 4px rgba(245, 223, 174, 0.18),
      0 18px 40px rgba(0, 0, 0, 0.55);
  }
  50% {
    box-shadow:
      0 0 0 8px rgba(245, 223, 174, 0.6),
      0 0 64px 10px rgba(245, 223, 174, 0.32),
      0 18px 40px rgba(0, 0, 0, 0.55);
  }
}

@media (prefers-reduced-motion: reduce) {
  .options.nudge .choice-card {
    animation: none;
    box-shadow:
      0 0 0 8px rgba(245, 223, 174, 0.6),
      0 0 64px 10px rgba(245, 223, 174, 0.32),
      0 18px 40px rgba(0, 0, 0, 0.55);
  }
}

.choice-card-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: inherit;
}

.choice-card span {
  font: 10px ui-monospace, Menlo, monospace;
  color: rgba(243, 233, 218, 0.45);
}

.choice-card.wash-lanterna {
  border-radius: 56% 44% 52% 48% / 48% 52% 44% 56%;
  background:
    radial-gradient(85% 65% at 50% 38%, rgba(232, 183, 90, 0.85), rgba(232, 183, 90, 0) 72%),
    linear-gradient(170deg, #453824, #38301f);
}

.choice-card.wash-barchetta-notte {
  border-radius: 44% 56% 48% 52% / 56% 44% 52% 48%;
  background:
    radial-gradient(90% 70% at 50% 62%, rgba(127, 166, 168, 0.7), rgba(127, 166, 168, 0) 78%),
    radial-gradient(60% 35% at 50% 46%, rgba(217, 139, 102, 0.65), rgba(217, 139, 102, 0) 70%),
    linear-gradient(170deg, #3a3a30, #2e3226);
}

/* Big round option buttons (resume + end) */
.blob-button {
  width: 140px;
  height: 140px;
  background: rgba(243, 233, 218, 0.1);
  box-shadow:
    0 0 0 5px rgba(245, 223, 174, 0.35),
    0 0 40px 4px rgba(245, 223, 174, 0.15),
    0 14px 32px rgba(0, 0, 0, 0.45);
  display: flex;
  align-items: center;
  justify-content: center;
}

.option:nth-child(odd) .blob-button {
  border-radius: 52% 48% 50% 50% / 50% 50% 55% 45%;
}

.option:nth-child(even) .blob-button {
  border-radius: 48% 52% 50% 50% / 50% 50% 45% 55%;
}

.blob-button .icon-play {
  border-left-width: 34px;
  border-top-width: 22px;
  border-bottom-width: 22px;
  margin-left: 8px;
}

.icon-replay {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  border: 8px solid #f2d8a7;
  border-top-color: transparent;
  transform: rotate(45deg);
}

.icon-shelf {
  display: grid;
  grid-template-columns: 24px 24px;
  gap: 7px;
}

.icon-shelf div {
  height: 30px;
  border-radius: 8px;
}

.icon-shelf div:nth-child(1) {
  background: radial-gradient(circle at 35% 30%, rgba(245, 223, 174, 0.9), rgba(217, 139, 102, 0.7));
}

.icon-shelf div:nth-child(2) {
  background: radial-gradient(circle at 65% 60%, rgba(127, 166, 168, 0.9), rgba(152, 165, 131, 0.7));
}

.icon-shelf div:nth-child(3) {
  background: radial-gradient(circle at 55% 30%, rgba(152, 165, 131, 0.9), rgba(232, 183, 90, 0.7));
}

.icon-shelf div:nth-child(4) {
  background: radial-gradient(circle at 40% 65%, rgba(217, 139, 102, 0.85), rgba(127, 166, 168, 0.7));
}

/* ── Audio-error: the sleeping bird (AI-367) ───────────────────────── */

.audio-error {
  gap: 22px;
}

/* The hand-painted sleeping bird (src/static/img/sleeping-bird.webp). Sized
   for small hands to see and tap; the gentle breathe keeps it feeling asleep,
   not frozen. Same steady-state discipline as the rest of the player. */
.audio-error .bird {
  width: min(56vw, 240px);
  height: auto;
  display: block;
  animation: bird-breathe 3.2s ease-in-out infinite;
}

.audio-error .prompt {
  max-width: 280px;
  font: 700 22px/1.35 var(--font-app);
}

@keyframes bird-breathe {
  0%,
  100% {
    transform: scale(1);
  }
  50% {
    transform: scale(1.04);
  }
}

@media (prefers-reduced-motion: reduce) {
  .audio-error .bird {
    animation: none;
  }
}

/* ── Offline: soft clouds took the stories (AI-367) ─────────────────── */

.screen.offline {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 28px;
  text-align: center;
}

.offline .clouds {
  position: relative;
  width: 220px;
  height: 100px;
}

/* Three overlapping puffs, the middle one proud — same gradient trick
   as .mascot, drifting a little unless motion is reduced. */
.offline .puff {
  position: absolute;
  bottom: 0;
  border-radius: 50%;
  background: radial-gradient(circle at 40% 30%, #f3f0ea, #cfd6df 80%);
  box-shadow: 0 0 0 10px rgba(207, 214, 223, 0.18);
  animation: cloud-drift 6s ease-in-out infinite;
}

.offline .puff:nth-child(1) {
  left: 0;
  width: 84px;
  height: 64px;
}

.offline .puff:nth-child(2) {
  left: 58px;
  bottom: 10px;
  width: 110px;
  height: 84px;
  animation-delay: -2s;
}

.offline .puff:nth-child(3) {
  right: 0;
  width: 78px;
  height: 58px;
  animation-delay: -4s;
}

/* A cover retry is pending: the same shimmer sweep as .cover.loading.
   .screen is already absolutely positioned, so it anchors the ::after. */
.screen.offline.loading {
  overflow: hidden;
}

.screen.offline.loading::after {
  content: "";
  position: absolute;
  inset: 0;
  background: linear-gradient(
    90deg,
    transparent 0%,
    rgba(245, 223, 174, 0.15) 50%,
    transparent 100%
  );
  animation: shimmer 1.5s ease-in-out infinite;
  pointer-events: none;
}

.offline .prompt {
  max-width: 280px;
  font: 700 22px/1.35 var(--font-app);
  color: var(--greeting-sub);
}

@keyframes cloud-drift {
  0%,
  100% {
    transform: translateX(0);
  }
  50% {
    transform: translateX(8px);
  }
}

@media (prefers-reduced-motion: reduce) {
  .offline .puff,
  .screen.offline.loading::after {
    animation: none;
  }
}

/* ── Story end ─────────────────────────────────────────────────────── */

.end {
  background:
    radial-gradient(60% 32% at 70% 20%, rgba(245, 223, 174, 0.5), rgba(245, 223, 174, 0) 68%),
    linear-gradient(178deg, #221d33 8%, #2a2743 60%, #1a1728);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 28px;
}

.end h2 {
  margin: 0;
  font: 700 28px var(--font-app);
  color: #f5dfae;
  animation: fade-in-up 0.8s ease-out;
}

@keyframes fade-in-up {
  from {
    opacity: 0;
    transform: translateY(12px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@media (prefers-reduced-motion: reduce) {
  .end h2 {
    animation: none;
  }
}

/* ── Math gate modal (AI-444) ──────────────────────────────────────────── */

.gate-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 200;
  /* Not dismissible by tapping outside — intentional */
}

.gate-modal {
  background: var(--card);
  border-radius: 20px;
  padding: 32px 28px 24px;
  max-width: 320px;
  width: calc(100% - 48px);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
  box-shadow: var(--card-shadow);
}

.gate-heading {
  margin: 0;
  font: 700 22px/1.2 var(--font-app);
  color: var(--ink);
  text-align: center;
}

.gate-equation {
  font: 700 28px/1 var(--font-app);
  color: var(--primary);
  text-align: center;
}

.gate-choices {
  display: flex;
  gap: 12px;
  justify-content: center;
  width: 100%;
}

.gate-choice {
  flex: 1;
  /* `button { all: unset }` drops centring and border-box; put them back. */
  display: flex;
  align-items: center;
  justify-content: center;
  box-sizing: border-box;
  min-height: 64px;
  min-width: 64px;
  border-radius: 14px;
  background: var(--field);
  border: 2px solid var(--line-12);
  font: 700 24px/1 var(--font-app);
  color: var(--ink);
  cursor: pointer;
  transition: background var(--settle), border-color var(--settle), transform var(--settle);
}

.gate-choice:active {
  transform: scale(0.95);
  background: var(--primary-16);
  border-color: var(--primary-ring);
}

@media (hover: hover) {
  .gate-choice:hover {
    background: var(--primary-16);
    border-color: var(--primary-outline);
  }
}

.gate-wrong {
  margin: 0;
  font: 600 15px/1.3 var(--font-app);
  color: var(--rest-text);
  text-align: center;
  opacity: 0;
  min-height: 1.3em;
  transition: opacity var(--settle);
}

.gate-wrong--visible {
  opacity: 1;
}

.gate-back {
  background: none;
  border: none;
  font: 600 15px var(--font-app);
  color: var(--ink-soft);
  cursor: pointer;
  padding: 8px 16px;
  min-height: 44px;
  border-radius: 8px;
  transition: color var(--settle);
}

.gate-back:active,
.gate-back:hover {
  color: var(--ink);
}
