:root {
  --night: #141b3d;
  --night2: #1f2a5c;
  --cream: #fff7e6;
  --gold: #ffd873;
  --soft: #b8c2f0;
  --card: #fffaf0;
  --ink: #3b3365;
}
* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; }
body {
  background: linear-gradient(180deg, var(--night) 0%, var(--night2) 70%, #2c3a75 100%);
  color: var(--cream);
  font-family: "Comic Sans MS", "Chalkboard SE", "Segoe UI", ui-rounded, sans-serif;
  min-height: 100vh;
  overflow-x: hidden;
}
.stars, .stars2 {
  position: fixed; inset: 0; pointer-events: none; z-index: 0;
  background-image:
    radial-gradient(2px 2px at 20px 30px, #fff 50%, transparent 51%),
    radial-gradient(2px 2px at 130px 80px, #ffe9a8 50%, transparent 51%),
    radial-gradient(1.5px 1.5px at 70px 150px, #fff 50%, transparent 51%),
    radial-gradient(2px 2px at 200px 40px, #fff 50%, transparent 51%),
    radial-gradient(1.5px 1.5px at 260px 120px, #cfe0ff 50%, transparent 51%),
    radial-gradient(2px 2px at 320px 60px, #fff 50%, transparent 51%);
  background-size: 340px 200px;
  animation: twinkle 4s ease-in-out infinite alternate;
  opacity: .8;
}
.stars2 { background-position: 90px 60px; animation-delay: 2s; opacity: .5; }
@keyframes twinkle { from { opacity: .35; } to { opacity: .9; } }

#app { position: relative; z-index: 1; max-width: 1080px; margin: 0 auto; padding: 24px 16px 48px; }
.view { display: flex; flex-direction: column; align-items: center; text-align: center; }
[hidden] { display: none !important; }

.moon { font-size: 64px; filter: drop-shadow(0 0 24px rgba(255, 216, 115, .6)); }
h1 { font-size: clamp(34px, 6vw, 52px); margin: 8px 0 4px; color: var(--gold); text-shadow: 0 2px 12px rgba(0,0,0,.35); }
h2 { color: var(--gold); margin: 12px 0 6px; }
.tagline { color: var(--soft); max-width: 460px; margin: 4px auto 20px; line-height: 1.5; }
.tagline.small { font-size: 14px; }

form { width: 100%; max-width: 520px; background: rgba(255, 250, 240, .07); border: 2px solid rgba(255, 216, 115, .25); border-radius: 24px; padding: 22px; backdrop-filter: blur(4px); }
.big-label { display: block; font-size: 20px; color: var(--cream); margin-bottom: 10px; }
textarea {
  width: 100%; border-radius: 16px; border: 2px solid rgba(255,216,115,.35); background: rgba(20,27,61,.65);
  color: var(--cream); padding: 14px; font-size: 17px; font-family: inherit; resize: vertical; min-height: 110px;
}
textarea:focus, input:focus { outline: 3px solid rgba(255,216,115,.45); }
.row { display: flex; gap: 12px; margin-top: 14px; }
.field { flex: 1; text-align: left; }
.field label { display: block; font-size: 14px; color: var(--soft); margin-bottom: 6px; }
.opt { opacity: .7; font-size: 12px; }
.field input {
  width: 100%; border-radius: 12px; border: 2px solid rgba(255,216,115,.25); background: rgba(20,27,61,.65);
  color: var(--cream); padding: 10px 12px; font-size: 16px; font-family: inherit;
}
.length { border: none; margin: 16px 0 6px; padding: 0; display: flex; gap: 10px; justify-content: center; }
.length legend { float: left; width: 100%; font-size: 14px; color: var(--soft); margin-bottom: 8px; }
.length label { cursor: pointer; }
.length input { position: absolute; opacity: 0; }
.length span {
  display: inline-block; padding: 10px 18px; border-radius: 999px; border: 2px solid rgba(255,216,115,.35);
  color: var(--soft); font-size: 16px; transition: all .15s;
}
.length input:checked + span { background: var(--gold); color: var(--ink); border-color: var(--gold); font-weight: bold; }
.length input:focus-visible + span { outline: 3px solid rgba(255,216,115,.5); }

#make-btn {
  margin-top: 14px; width: 100%; padding: 16px; font-size: 22px; font-family: inherit; font-weight: bold;
  color: var(--ink); background: linear-gradient(180deg, #ffe9a8, var(--gold)); border: none; border-radius: 18px;
  cursor: pointer; box-shadow: 0 6px 18px rgba(255,216,115,.35); transition: transform .1s;
}
#make-btn:active { transform: scale(.98); }
.error { color: #ffb3b3; background: rgba(255,80,80,.12); border-radius: 12px; padding: 10px; line-height: 1.45; }
.fine { font-size: 12.5px; color: rgba(184,194,240,.8); margin-top: 16px; max-width: 460px; }
.fine a { color: var(--gold); }
.center { text-align: center; }

.dream-cloud { font-size: 80px; animation: bob 2.5s ease-in-out infinite; }
.sleep { font-size: 44px; animation: bob 2.5s ease-in-out infinite reverse; }
@keyframes bob { 0%,100% { transform: translateY(0); } 50% { transform: translateY(-14px); } }

.reader-bar { display: flex; align-items: center; justify-content: space-between; gap: 10px; width: 100%; margin-bottom: 12px; }
.reader-bar h2 { margin: 0; font-size: clamp(18px, 3vw, 26px); flex: 1; }
.home { font-size: 28px; text-decoration: none; }
.autoplay { font-size: 14px; color: var(--soft); display: flex; align-items: center; gap: 6px; cursor: pointer; white-space: nowrap; }
.autoplay input { width: 18px; height: 18px; accent-color: var(--gold); }

.spread { display: flex; gap: 18px; width: 100%; justify-content: center; align-items: stretch; touch-action: pan-y; }
.page {
  background: var(--card); color: var(--ink); border-radius: 20px; overflow: hidden; flex: 1; max-width: 520px;
  display: flex; flex-direction: column; box-shadow: 0 10px 30px rgba(0,0,0,.4); min-height: 320px;
}
.page .art { width: 100%; aspect-ratio: 4/3; object-fit: cover; display: block; background: #efe6d4; }
.page .art-pending {
  width: 100%; aspect-ratio: 4/3; display: flex; flex-direction: column; gap: 10px; align-items: center; justify-content: center;
  background: linear-gradient(160deg, #efe6d4, #f7efdd); color: #8a7f68; font-size: 15px; padding: 16px;
}
.page .art-pending .spinner { font-size: 40px; animation: spinpulse 1.6s ease-in-out infinite; }
@keyframes spinpulse { 0%,100% { transform: scale(1) rotate(0deg); opacity:.7;} 50% { transform: scale(1.2) rotate(12deg); opacity:1;} }
.page p.text {
  margin: 0; padding: 18px 20px 22px; font-size: clamp(17px, 2.2vw, 20px); line-height: 1.6;
  font-family: Georgia, "Times New Roman", serif; text-align: left; flex: 1;
}
.page .cover-tag { padding: 10px; font-size: 13px; color: #8a7f68; letter-spacing: .08em; text-transform: uppercase; }

.reader-nav { display: flex; align-items: center; gap: 18px; margin-top: 18px; }
.nav-btn {
  width: 56px; height: 56px; border-radius: 50%; border: none; font-size: 30px; line-height: 1;
  background: linear-gradient(180deg, #ffe9a8, var(--gold)); color: var(--ink); cursor: pointer;
  box-shadow: 0 4px 14px rgba(255,216,115,.35);
}
.nav-btn:disabled { opacity: .35; cursor: default; box-shadow: none; }
.nav-btn.wide { width: auto; border-radius: 999px; padding: 0 28px; height: 56px; font-size: 19px; }
#page-indicator { color: var(--soft); min-width: 90px; }
.btn-link {
  display: inline-block; padding: 16px 28px; border-radius: 999px; background: rgba(255,216,115,.15);
  border: 2px solid var(--gold); color: var(--gold); text-decoration: none; font-size: 19px;
}
.end-actions { display: flex; gap: 14px; flex-wrap: wrap; justify-content: center; margin-top: 10px; }
.link-btn { background: none; border: none; color: rgba(184,194,240,.8); text-decoration: underline; cursor: pointer; font-size: 12.5px; font-family: inherit; }

@media (max-width: 899px) {
  .spread { flex-direction: column; align-items: center; }
  .page { max-width: 560px; width: 100%; }
  .page.spread-only { display: none; }
}

/* Narration control */
.listen-btn {
  border: none; border-radius: 999px; padding: 8px 16px; cursor: pointer;
  font: inherit; font-weight: 600; font-size: 0.95rem;
  background: #ffd76e; color: #2a2150; transition: transform .15s ease, opacity .15s ease;
}
.listen-btn:hover { transform: scale(1.04); }
.listen-btn[data-mode="playing"] { background: #b9a8ff; color: #1c1540; }
.listen-btn[data-mode="blocked"] { animation: listenPulse 1.6s ease-in-out infinite; }
.listen-btn[data-mode="error"] { background: #ff9d9d; color: #4a1020; }
.listen-btn[data-mode="pending"] { opacity: 0.7; cursor: wait; }
@keyframes listenPulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(255, 215, 110, 0.65); }
  50% { box-shadow: 0 0 0 10px rgba(255, 215, 110, 0); }
}

/* Narrow screens: keep the reader bar usable - controls on one row, title below */
@media (max-width: 640px) {
  .reader-bar { flex-wrap: wrap; }
  .reader-bar h2 { flex-basis: 100%; order: 3; text-align: center; }
}
