:root {
  --sky-top: #141a3a;
  --sky-bottom: #2b2470;
  --card: rgba(255, 255, 255, 0.09);
  --card-hover: rgba(255, 255, 255, 0.16);
  --line: rgba(255, 255, 255, 0.18);
  --text: #fdf8ec;
  --soft: #c9c4e8;
  --gold: #ffd36e;
  --gold-deep: #f4a93b;
  --pink: #ff9bb8;
  --error: #ffb4a8;
  --radius: 28px;
  --tap: 0.97;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: 'Fredoka', ui-rounded, system-ui, sans-serif;
  color: var(--text);
  background: linear-gradient(180deg, var(--sky-top), var(--sky-bottom)) fixed;
  overscroll-behavior: none;
  -webkit-user-select: none;
  user-select: none;
}

.sky {
  position: fixed; inset: 0; pointer-events: none; z-index: 0; opacity: 0.8;
  background-image:
    radial-gradient(2px 2px at 10% 20%, #fff 50%, transparent 51%),
    radial-gradient(1.5px 1.5px at 30% 70%, #fff 50%, transparent 51%),
    radial-gradient(2px 2px at 55% 15%, #fff 50%, transparent 51%),
    radial-gradient(1px 1px at 70% 45%, #fff 50%, transparent 51%),
    radial-gradient(2px 2px at 85% 80%, #fff 50%, transparent 51%),
    radial-gradient(1.5px 1.5px at 92% 25%, #fff 50%, transparent 51%),
    radial-gradient(1px 1px at 20% 90%, #fff 50%, transparent 51%),
    radial-gradient(1.5px 1.5px at 45% 50%, #fff 50%, transparent 51%);
  animation: twinkle 6s ease-in-out infinite alternate;
}
@keyframes twinkle { from { opacity: 0.45; } to { opacity: 0.9; } }

.screen {
  display: none; position: relative; z-index: 1;
  min-height: 100dvh;
  padding: max(16px, env(safe-area-inset-top)) 16px max(24px, env(safe-area-inset-bottom));
  max-width: 1100px; margin: 0 auto;
}
.screen.active { display: block; animation: fade 0.35s ease; }
@keyframes fade { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }

.center { display: flex; flex-direction: column; align-items: center; text-align: center; gap: 18px; padding-top: 4vh; }
.center-text { text-align: center; }
h1 { font-size: clamp(2.4rem, 7vw, 4rem); margin: 0; letter-spacing: 0.5px; }
h2 { font-size: clamp(1.6rem, 4.5vw, 2.6rem); margin: 8px 0 20px; text-align: center; font-weight: 600; }
h3 { margin: 28px 0 12px; }
.soft { color: var(--soft); margin: 0; }
.error { color: var(--error); min-height: 1.2em; margin: 0; }
.demo { background: rgba(255, 211, 110, 0.15); border: 1px dashed var(--gold); padding: 8px 14px; border-radius: 14px; color: var(--gold); margin: 0; }

.moon { font-size: 4rem; filter: drop-shadow(0 0 24px rgba(255, 211, 110, 0.55)); }
.moon.big { font-size: clamp(4.5rem, 14vw, 7rem); animation: float 5s ease-in-out infinite; }
@keyframes float { 50% { transform: translateY(-10px) rotate(-6deg); } }

/* Buttons */
.btn {
  font: inherit; color: var(--text); background: var(--card);
  border: 2px solid var(--line); border-radius: 999px;
  padding: 14px 26px; font-size: 1.2rem; font-weight: 600; cursor: pointer;
  transition: transform 0.12s ease, background 0.2s ease;
}
.btn:active { transform: scale(var(--tap)); }
.btn:disabled { opacity: 0.55; cursor: wait; }
.btn:hover { background: var(--card-hover); }
.btn.primary { background: linear-gradient(180deg, var(--gold), var(--gold-deep)); color: #3a2300; border-color: transparent; }
.btn.huge {
  display: flex; flex-direction: column; align-items: center; gap: 6px;
  font-size: clamp(2.6rem, 8vw, 3.6rem); padding: 26px 34px; border-radius: var(--radius);
  min-width: min(320px, 100%);
}
.btn.huge span { font-size: clamp(1.25rem, 3.5vw, 1.6rem); }
.btn.round { width: 58px; height: 58px; padding: 0; font-size: 1.6rem; display: grid; place-items: center; flex: none; }
.btn.ghost { background: transparent; }
.link { background: none; border: 0; color: var(--soft); font: inherit; text-decoration: underline; cursor: pointer; padding: 12px; }
.parent-link { position: absolute; bottom: max(16px, env(safe-area-inset-bottom)); left: 50%; transform: translateX(-50%); }

.home-actions { display: flex; flex-wrap: wrap; justify-content: center; gap: 20px; margin-top: 12px; width: 100%; }

/* Forms */
.stack { display: flex; flex-direction: column; gap: 12px; width: min(360px, 100%); }
.narrow { margin: 6vh auto 0; text-align: center; }
input {
  font: inherit; font-size: 1.3rem; color: var(--text); background: rgba(0, 0, 0, 0.25);
  border: 2px solid var(--line); border-radius: 18px; padding: 14px 18px; width: 100%;
  -webkit-user-select: text; user-select: text;
}
input:focus { outline: 3px solid var(--gold); outline-offset: 2px; }
label { display: flex; flex-direction: column; gap: 6px; text-align: left; color: var(--soft); }

/* Header bar */
.bar { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 8px; }
.bar-title { margin: 0; flex: 1; font-size: clamp(1.3rem, 4vw, 2rem); }
.dots { display: flex; gap: 10px; }
.dots i { width: 14px; height: 14px; border-radius: 50%; background: var(--line); display: block; transition: background 0.2s; }
.dots i.on { background: var(--gold); }

/* Choice grid */
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 16px; }
.choice {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 8px;
  aspect-ratio: 1 / 1; background: var(--card); border: 3px solid var(--line); border-radius: var(--radius);
  color: var(--text); font: inherit; font-size: 1.1rem; font-weight: 500; cursor: pointer; padding: 10px;
  transition: transform 0.12s ease, background 0.2s ease, border-color 0.2s ease;
}
.choice .emoji { font-size: clamp(3rem, 9vw, 4.2rem); line-height: 1; }
.choice:active { transform: scale(0.94); }
.choice.picked { border-color: var(--gold); background: rgba(255, 211, 110, 0.18); }
.choice.name { aspect-ratio: auto; min-height: 140px; font-size: 1.6rem; }

/* Wizard footer (multi-select "Suivant") */
.wiz-footer { position: sticky; bottom: 0; display: flex; justify-content: center; padding: 16px 0 max(8px, env(safe-area-inset-bottom)); }
.wiz-footer .btn { min-width: min(320px, 100%); font-size: 1.3rem; padding: 16px 28px; }

/* First names */
.names { display: flex; flex-direction: column; gap: 12px; max-width: 640px; margin: 0 auto; }
.name-row {
  display: flex; flex-direction: row; align-items: center; gap: 14px; flex-wrap: wrap;
  background: var(--card); border: 2px solid var(--line); border-radius: 22px; padding: 12px 16px; color: var(--text);
}
.name-row .emoji { font-size: 2.6rem; line-height: 1; }
.name-label { font-size: 1.2rem; font-weight: 600; min-width: 8em; }
.name-row input { flex: 1; min-width: 160px; }

/* Story settings on the recap screen */
.settings { display: flex; flex-direction: column; gap: 14px; width: min(760px, 100%); }
.setting { display: flex; flex-direction: column; gap: 8px; text-align: left; }
.setting-label { color: var(--soft); font-size: 1rem; padding-left: 6px; }
.seg { display: flex; flex-wrap: wrap; gap: 8px; }
.seg button {
  font: inherit; font-size: 1.05rem; font-weight: 600; color: var(--text); cursor: pointer;
  background: var(--card); border: 2px solid var(--line); border-radius: 999px; padding: 10px 16px;
}
.seg button.on { background: rgba(255, 211, 110, 0.2); border-color: var(--gold); color: var(--gold); }

/* Recap */
.recap { display: flex; flex-wrap: wrap; justify-content: center; gap: 12px; max-width: 700px; }
.recap span { background: var(--card); border: 2px solid var(--line); border-radius: 999px; padding: 10px 18px; font-size: 1.2rem; }

/* Waiting */
.book { position: relative; width: 160px; height: 160px; display: grid; place-items: center; font-size: 5rem; }
.quill { animation: write 1.2s ease-in-out infinite; display: inline-block; }
@keyframes write { 50% { transform: translate(14px, 6px) rotate(14deg); } }
.spark { position: absolute; font-size: 1.8rem; animation: rise 2.4s ease-in infinite; opacity: 0; }
.s1 { left: 10%; animation-delay: 0s; } .s2 { left: 50%; animation-delay: 0.8s; } .s3 { left: 80%; animation-delay: 1.6s; }
@keyframes rise { 0% { top: 80%; opacity: 0; } 30% { opacity: 1; } 100% { top: -10%; opacity: 0; } }
.steps { display: flex; flex-wrap: wrap; justify-content: center; gap: 10px; }
.step { padding: 8px 16px; border-radius: 999px; background: var(--card); color: var(--soft); border: 2px solid transparent; }
.step.active { color: var(--text); border-color: var(--gold); }
.step.done { color: var(--gold); }

/* Player */
.night { background: #05060f; max-width: none; }
.screen.night.active ~ .sky, body:has(#player.active) .sky { opacity: 0.25; }
body:has(#player.active) { background: #05060f; }
.player-title { max-width: 800px; color: #e9e4ff; margin-top: 0; }
.player-cover {
  width: min(78vw, 46vh, 420px); aspect-ratio: 3 / 4; object-fit: cover;
  border-radius: 20px; box-shadow: 0 18px 60px rgba(0, 0, 0, 0.6), 0 0 0 1px rgba(255, 255, 255, 0.08);
  animation: rise-in 0.6s ease both;
}
@keyframes rise-in { from { opacity: 0; transform: translateY(14px) scale(0.98); } to { opacity: 1; transform: none; } }
#player .center { gap: 14px; padding-top: 1vh; }
/* With a cover, the play button gets smaller so everything fits on screen. */
#player-cover:not([hidden]) ~ .btn.play { width: 104px; height: 104px; font-size: 3rem; }

/* Library cover thumbnails (tap to listen) */
.cover-thumb {
  position: relative; flex: none; width: 78px; aspect-ratio: 3 / 4; padding: 0; border: 0; cursor: pointer;
  border-radius: 12px; overflow: hidden; background: var(--card); box-shadow: 0 6px 18px rgba(0, 0, 0, 0.35);
}
.cover-thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.cover-play {
  position: absolute; right: 6px; bottom: 6px; width: 30px; height: 30px; border-radius: 50%;
  display: grid; place-items: center; font-size: 0.9rem; color: #3a2300;
  background: linear-gradient(180deg, var(--gold), var(--gold-deep));
}
.btn.play {
  width: clamp(150px, 34vw, 220px); height: clamp(150px, 34vw, 220px); border-radius: 50%;
  font-size: clamp(4rem, 10vw, 6rem); padding: 0; display: grid; place-items: center;
  background: rgba(255, 211, 110, 0.12); border: 3px solid rgba(255, 211, 110, 0.5); color: var(--gold);
}
.progress { position: relative; width: min(600px, 100%); height: 14px; background: rgba(255, 255, 255, 0.1); border-radius: 999px; overflow: hidden; }
#progress-fill { position: absolute; inset: 0 auto 0 0; width: 0; background: var(--gold); border-radius: 999px; }
#seek { position: absolute; inset: -10px 0; width: 100%; opacity: 0; cursor: pointer; padding: 0; }
.time { font-variant-numeric: tabular-nums; }
.hint { font-size: 0.95rem; opacity: 0.7; }

/* Lists */
.list { display: flex; flex-direction: column; gap: 12px; }
.item {
  display: flex; align-items: center; gap: 14px; background: var(--card); border: 2px solid var(--line);
  border-radius: 22px; padding: 12px 14px;
}
.item .play-mini { flex: none; width: 64px; height: 64px; font-size: 1.8rem; border-radius: 50%; padding: 0; display: grid; place-items: center; }
.item .meta { flex: 1; min-width: 0; text-align: left; }
.item .meta b { display: block; font-size: 1.2rem; font-weight: 600; overflow: hidden; text-overflow: ellipsis; }
.item .meta small { color: var(--soft); }
.item .star { background: none; border: 0; font-size: 2rem; cursor: pointer; color: var(--gold); padding: 6px; }
.card { background: var(--card); border: 2px solid var(--line); border-radius: 22px; padding: 18px; width: 100%; max-width: 560px; }
details { width: 100%; }
details summary { cursor: pointer; color: var(--soft); }
details p { white-space: pre-wrap; -webkit-user-select: text; user-select: text; line-height: 1.5; }
.danger { border-color: rgba(255, 140, 120, 0.6); color: var(--error); }

/* Trash (parent area) */
.trash-hint { margin: -4px 0 12px; }
.item.trashed { opacity: 0.85; flex-wrap: wrap; }
.trash-cover { width: 54px; aspect-ratio: 3 / 4; object-fit: cover; border-radius: 8px; filter: grayscale(0.6); flex: none; }
.trash-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.trash-actions .btn { font-size: 1rem; padding: 10px 16px; }

@media (max-width: 480px) {
  .grid { grid-template-columns: repeat(2, 1fr); gap: 12px; }
  .choice .emoji { font-size: 2.8rem; }
}

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

/* Waiting screen: seconds counter and retry */
.wait-clock { margin: 0; font-variant-numeric: tabular-nums; color: var(--gold); font-size: 1.4rem; font-weight: 600; opacity: 0.85; }
.wait-actions { display: flex; gap: 12px; flex-wrap: wrap; justify-content: center; }

/* ===== Library: floating storybooks under a Three.js sky ===== */
.sky3d { position: fixed; inset: 0; width: 100%; height: 100%; z-index: 0; pointer-events: none; opacity: 0; transition: opacity 1.2s ease; }
.sky3d.on { opacity: 1; }
.night { background: transparent; }
body:has(#player.active) .sky, body:has(#library.active) .sky { opacity: 0.35; }

.lib-bar { margin-bottom: 18px; }
.lib-title {
  font-size: clamp(1.9rem, 5vw, 3rem); font-weight: 700; letter-spacing: 0.5px;
  background: linear-gradient(180deg, #fff6dc, var(--gold)); -webkit-background-clip: text; background-clip: text; color: transparent;
  filter: drop-shadow(0 4px 18px rgba(255, 211, 110, 0.25));
}
.lib-fav.on { background: rgba(255, 211, 110, 0.22); border-color: var(--gold); color: var(--gold); }

.shelf {
  display: grid; gap: clamp(26px, 4vw, 44px) clamp(18px, 3vw, 34px);
  grid-template-columns: repeat(auto-fill, minmax(clamp(138px, 24vw, 210px), 1fr));
  padding: 8px clamp(4px, 2vw, 18px) 48px; perspective: 1400px;
}
.tome { position: relative; display: flex; flex-direction: column; align-items: center; gap: 10px; text-align: center; }

.book-open {
  position: relative; display: block; width: 100%; aspect-ratio: 3 / 4; padding: 0; border: 0; background: none; cursor: pointer;
  animation: book-float 6s ease-in-out infinite; animation-delay: calc(var(--i, 0) * -0.9s);
  -webkit-tap-highlight-color: transparent;
}
@keyframes book-float { 0%, 100% { transform: translateY(0); } 50% { transform: translateY(-8px); } }

.book-3d {
  position: relative; display: block; width: 100%; height: 100%;
  border-radius: 4px 14px 14px 4px; transform-style: preserve-3d;
  transform: perspective(1000px) rotateY(-16deg) rotateX(3deg);
  transition: transform 0.45s cubic-bezier(0.22, 1, 0.36, 1), box-shadow 0.45s ease;
  box-shadow:
    -2px 0 0 rgba(255, 255, 255, 0.08),
    14px 22px 34px rgba(0, 0, 0, 0.55),
    0 0 0 1px rgba(255, 255, 255, 0.06);
}
/* spine on the left, page edges on the right: it reads as a real book */
.book-3d::before {
  content: ''; position: absolute; inset: 0 auto 0 0; width: 9%; z-index: 2; border-radius: 4px 0 0 4px;
  background: linear-gradient(90deg, rgba(0, 0, 0, 0.45), rgba(255, 255, 255, 0.18) 45%, rgba(0, 0, 0, 0.25));
}
.book-3d::after {
  content: ''; position: absolute; top: 3%; bottom: 3%; right: -7px; width: 8px; z-index: -1; border-radius: 0 4px 4px 0;
  background: repeating-linear-gradient(90deg, #f7efdc 0 1px, #d9ccb0 1px 2px);
  transform: rotateY(60deg); transform-origin: left;
}
.book-cover { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; border-radius: inherit; display: block; }
.book-plain {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 10px; padding: 14px;
  background: radial-gradient(circle at 50% 30%, #4b3fa0, #1d1a4f); color: var(--text); font-weight: 600; font-size: 1rem; line-height: 1.2;
}
.book-plain-moon { font-size: 2.6rem; }
.book-shine {
  position: absolute; inset: 0; z-index: 3; border-radius: inherit; pointer-events: none;
  background: linear-gradient(115deg, rgba(255, 255, 255, 0) 30%, rgba(255, 255, 255, 0.22) 45%, rgba(255, 255, 255, 0) 60%);
  background-size: 250% 100%; background-position: 120% 0; transition: background-position 0.9s ease;
}
.book-open:hover .book-3d, .book-open:focus-visible .book-3d {
  transform: perspective(1000px) rotateY(-4deg) rotateX(0deg) translateY(-6px) scale(1.03);
  box-shadow: -2px 0 0 rgba(255, 255, 255, 0.08), 18px 30px 46px rgba(0, 0, 0, 0.6), 0 0 34px rgba(255, 211, 110, 0.25);
}
.book-open:hover .book-shine, .book-open:focus-visible .book-shine { background-position: -40% 0; }
.book-open:active .book-3d { transform: perspective(1000px) rotateY(-2deg) scale(0.97); transition-duration: 0.12s; }
.book-open:focus-visible { outline: none; }
.book-open:focus-visible .book-3d { outline: 3px solid var(--gold); outline-offset: 4px; }

.book-star {
  position: absolute; top: -10px; right: -6px; z-index: 4; width: 46px; height: 46px; border-radius: 50%;
  display: grid; place-items: center; font-size: 1.45rem; cursor: pointer; color: var(--soft);
  background: rgba(20, 22, 58, 0.82); border: 2px solid var(--line); backdrop-filter: blur(4px);
}
.book-star.on { color: #3a2300; background: linear-gradient(180deg, var(--gold), var(--gold-deep)); border-color: transparent; box-shadow: 0 0 18px rgba(255, 211, 110, 0.55); }
.book-title {
  font-size: clamp(1.02rem, 2.3vw, 1.2rem); font-weight: 600; line-height: 1.2; color: var(--text); padding: 0 4px;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
}
.book-meta { font-size: 0.88rem; color: var(--soft); margin-top: -4px; }

/* The flying, opening book */
.book-fly { position: fixed; inset: 0; z-index: 50; cursor: pointer; }
.fly-book { position: absolute; transform-style: preserve-3d; perspective: 1600px; will-change: transform; }
.fly-inside {
  position: absolute; inset: 0; border-radius: 4px 14px 14px 4px; opacity: 0; overflow: hidden;
  background: radial-gradient(circle at 40% 45%, #fff8e2, #ffd36e 45%, #f4a93b 80%, #8a4d12);
  box-shadow: 0 30px 80px rgba(0, 0, 0, 0.6), 0 0 90px rgba(255, 211, 110, 0.55);
}
.fly-glow { position: absolute; inset: -20%; background: radial-gradient(circle, rgba(255, 255, 255, 0.95), rgba(255, 230, 160, 0.4) 35%, transparent 65%); }
.fly-cover {
  position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; border-radius: 4px 14px 14px 4px;
  transform-origin: left center; backface-visibility: hidden; will-change: transform;
  box-shadow: 10px 18px 40px rgba(0, 0, 0, 0.55);
}

@media (max-width: 480px) {
  .shelf { grid-template-columns: repeat(2, 1fr); gap: 30px 18px; }
  .book-star { width: 40px; height: 40px; font-size: 1.2rem; }
}
@media (prefers-reduced-motion: reduce) {
  .book-open { animation: none; }
  .book-3d { transform: none; }
  .sky3d { display: none; }
}
.sky3d.front { z-index: 60; }
.fly-leaf { position: absolute; inset: 0; transform-origin: left center; transform-style: preserve-3d; will-change: transform; }
.fly-leaf .fly-cover { transform-origin: initial; }
.fly-leaf-back {
  position: absolute; inset: 0; border-radius: 14px 4px 4px 14px; transform: rotateY(180deg); backface-visibility: hidden;
  background: linear-gradient(90deg, #efe2c2, #fbf4e1 70%, #e9dbb8); box-shadow: inset -14px 0 24px rgba(90, 60, 20, 0.18);
}
.fly-text {
  position: absolute; left: 0; right: 0; top: 50%; transform: translateY(-50%); text-align: center; padding: 0 12%;
  font-size: clamp(1.3rem, 3.4vw, 2rem); font-weight: 600; color: #6b3c0a; opacity: 0;
  text-shadow: 0 1px 0 rgba(255, 255, 255, 0.6);
}
