/* ============================================================
   Storytime — bedtime design system. Dark mode only.
   Soft, warm, unhurried. No sharp edges, no harsh colours.
   ============================================================ */

:root {
  --bg: #0d0d14;
  --bg-2: #16162a;
  --bg-3: #1e1e38;
  --card: #1a1a30;
  --card-hover: #222244;
  --ink: #f0ecff;
  --ink-soft: #b8b2d8;
  --ink-faint: #7d77a3;
  --gold: #f5c97b;
  --gold-soft: #f5c97b33;
  --purple: #a78bfa;
  --purple-soft: #a78bfa26;
  --blue: #8bb8fa;
  --rose: #f5a3c0;
  --green: #9ae6b4;
  --red-soft: #f59ba3;
  --radius: 16px;
  --radius-sm: 12px;
  --shadow: 0 8px 32px rgba(0, 0, 0, 0.45);
  --glow: 0 0 24px var(--gold-soft);
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  font-size: 16px;
}

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

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  min-height: 100%;
  overscroll-behavior: none;
}

body { overflow-x: hidden; }

#app {
  position: relative;
  z-index: 2;
  min-height: 100vh;
  min-height: 100dvh;
}

.view {
  max-width: 720px;
  margin: 0 auto;
  padding: calc(var(--safe-top) + 20px) 20px calc(var(--safe-bottom) + 32px);
  animation: viewIn 0.45s ease both;
}

@keyframes viewIn {
  from { opacity: 0; transform: translateY(10px); }
  to { opacity: 1; transform: none; }
}

/* ---------- starfield backdrop ---------- */
#stars {
  position: fixed;
  inset: 0;
  z-index: 1;
  pointer-events: none;
  background:
    radial-gradient(ellipse at 20% 0%, #1c1838 0%, transparent 55%),
    radial-gradient(ellipse at 85% 110%, #181434 0%, transparent 55%),
    var(--bg);
}
.star {
  position: absolute;
  border-radius: 50%;
  background: #fff;
  opacity: 0.5;
  animation: twinkle 4s ease-in-out infinite;
}
@keyframes twinkle {
  0%, 100% { opacity: 0.15; }
  50% { opacity: 0.7; }
}

/* ---------- typography ---------- */
h1, h2, h3 { font-weight: 700; margin: 0; }
h1 { font-size: 1.7rem; letter-spacing: 0.01em; }
h2 { font-size: 1.3rem; }
.subtitle { color: var(--ink-soft); margin: 6px 0 0; font-size: 0.95rem; }

/* ---------- buttons ---------- */
button {
  font-family: inherit;
  border: none;
  cursor: pointer;
  color: inherit;
  background: none;
  font-size: 1rem;
}
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 14px 24px;
  border-radius: 999px;
  font-weight: 650;
  font-size: 1.05rem;
  background: var(--card);
  color: var(--ink);
  transition: transform 0.18s ease, background 0.25s ease, box-shadow 0.25s ease;
  min-height: 52px;
}
.btn:active { transform: scale(0.96); }
.btn-primary {
  background: linear-gradient(135deg, #7c5cd6, #a78bfa);
  color: #fff;
  box-shadow: 0 6px 24px #7c5cd655;
}
.btn-gold {
  background: linear-gradient(135deg, #d9a753, #f5c97b);
  color: #2a2018;
  box-shadow: 0 6px 24px #f5c97b40;
}
.btn-ghost { background: transparent; color: var(--ink-soft); }
.btn-danger { background: #4a2330; color: var(--red-soft); }
.btn:disabled { opacity: 0.45; pointer-events: none; }
.btn-block { width: 100%; }

.icon-btn {
  width: 46px;
  height: 46px;
  border-radius: 50%;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 1.25rem;
  background: var(--card);
  transition: transform 0.18s ease, background 0.25s ease;
}
.icon-btn:active { transform: scale(0.92); }

/* ---------- inputs ---------- */
input[type='text'], input[type='password'], input[type='number'], select {
  width: 100%;
  padding: 14px 16px;
  border-radius: var(--radius-sm);
  border: 1.5px solid #2e2e52;
  background: var(--bg-2);
  color: var(--ink);
  font-size: 1.05rem;
  font-family: inherit;
  outline: none;
  transition: border-color 0.25s ease, box-shadow 0.25s ease;
}
input:focus, select:focus { border-color: var(--purple); box-shadow: 0 0 0 3px var(--purple-soft); }
label.field-label {
  display: block;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--ink-soft);
  margin: 18px 0 7px;
  letter-spacing: 0.03em;
}

/* ---------- header bar ---------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 22px;
}
.topbar .title-wrap { flex: 1; min-width: 0; }

/* ---------- library grid ---------- */
.story-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 16px;
}
@media (min-width: 480px) {
  .story-grid { grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); }
}
.story-card {
  position: relative;
  border-radius: var(--radius);
  overflow: hidden;
  background: var(--card);
  box-shadow: var(--shadow);
  aspect-ratio: 1 / 1.18;
  cursor: pointer;
  transition: transform 0.25s ease, box-shadow 0.25s ease;
  animation: viewIn 0.5s ease both;
}
.story-card:active { transform: scale(0.97); }
.story-card img {
  width: 100%;
  height: 72%;
  object-fit: cover;
  display: block;
}
.story-card .cover-placeholder {
  width: 100%;
  height: 72%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 2.6rem;
  background: linear-gradient(160deg, var(--bg-3), var(--bg-2));
}
.story-card .card-meta {
  padding: 10px 12px;
}
.story-card .card-title {
  font-weight: 650;
  font-size: 0.92rem;
  line-height: 1.25;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
.story-card .card-sub {
  font-size: 0.75rem;
  color: var(--ink-faint);
  margin-top: 3px;
}
.badge {
  position: absolute;
  top: 10px;
  left: 10px;
  padding: 5px 11px;
  border-radius: 999px;
  font-size: 0.72rem;
  font-weight: 700;
  backdrop-filter: blur(8px);
}
.badge.generating { background: #f5c97bd9; color: #2a2018; animation: pulseSoft 1.8s ease-in-out infinite; }
.badge.failed { background: #4a2330e6; color: var(--red-soft); }
@keyframes pulseSoft {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.55; }
}
.card-delete {
  position: absolute;
  top: 8px;
  right: 8px;
  width: 34px;
  height: 34px;
  border-radius: 50%;
  background: #00000080;
  backdrop-filter: blur(6px);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.95rem;
}

.empty-state {
  text-align: center;
  padding: 70px 20px;
  color: var(--ink-soft);
}
.empty-state .big { font-size: 3.4rem; margin-bottom: 14px; animation: floaty 4s ease-in-out infinite; }
@keyframes floaty {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-9px); }
}

/* ---------- wizard ---------- */
.wizard-progress {
  display: flex;
  gap: 6px;
  margin: 4px 0 24px;
}
.wizard-progress span {
  flex: 1;
  height: 5px;
  border-radius: 99px;
  background: var(--bg-3);
  transition: background 0.4s ease;
}
.wizard-progress span.done { background: linear-gradient(90deg, var(--purple), var(--gold)); }

.choice-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(104px, 1fr));
  gap: 12px;
  margin-top: 18px;
}
.choice {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 7px;
  padding: 16px 8px;
  border-radius: var(--radius);
  background: var(--card);
  border: 2px solid transparent;
  font-weight: 600;
  font-size: 0.86rem;
  text-align: center;
  transition: transform 0.15s ease, border-color 0.25s ease, background 0.25s ease, box-shadow 0.25s ease;
  min-height: 96px;
}
.choice .emoji { font-size: 1.9rem; }
.choice:active { transform: scale(0.94); }
.choice.selected {
  border-color: var(--gold);
  background: var(--card-hover);
  box-shadow: var(--glow);
}
.wizard-nav {
  display: flex;
  gap: 12px;
  margin-top: 30px;
}
.wizard-nav .btn { flex: 1; }

.length-row { display: flex; flex-direction: column; gap: 12px; margin-top: 18px; }
.length-option {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 18px;
  border-radius: var(--radius);
  background: var(--card);
  border: 2px solid transparent;
  text-align: left;
  transition: border-color 0.25s ease, box-shadow 0.25s ease;
}
.length-option.selected { border-color: var(--gold); box-shadow: var(--glow); }
.length-option .emoji { font-size: 1.8rem; }
.length-option .name { font-weight: 700; }
.length-option .desc { font-size: 0.82rem; color: var(--ink-faint); }

/* ---------- PIN pad ---------- */
.login-wrap {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 82vh;
  text-align: center;
}
.login-logo { font-size: 3.6rem; animation: floaty 4.5s ease-in-out infinite; }
.login-title { margin-top: 10px; font-size: 1.9rem; background: linear-gradient(135deg, var(--gold), var(--purple)); -webkit-background-clip: text; background-clip: text; color: transparent; }

.user-row {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 18px;
  margin-top: 30px;
}
.user-chip {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  width: 86px;
}
.user-chip .avatar {
  width: 68px;
  height: 68px;
  border-radius: 50%;
  background: var(--card);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.9rem;
  border: 2px solid transparent;
  transition: transform 0.18s ease, border-color 0.25s ease, box-shadow 0.25s ease;
}
.user-chip:active .avatar { transform: scale(0.93); }
.user-chip.selected .avatar { border-color: var(--gold); box-shadow: var(--glow); }
.user-chip .name { font-size: 0.85rem; font-weight: 600; color: var(--ink-soft); }

.pin-dots {
  display: flex;
  gap: 12px;
  justify-content: center;
  margin: 26px 0 8px;
  min-height: 16px;
}
.pin-dots span {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: var(--bg-3);
  transition: background 0.15s ease, transform 0.15s ease;
}
.pin-dots span.filled { background: var(--gold); transform: scale(1.15); }

.pin-pad {
  display: grid;
  grid-template-columns: repeat(3, 76px);
  gap: 14px;
  margin-top: 18px;
}
.pin-key {
  width: 76px;
  height: 76px;
  border-radius: 50%;
  background: var(--card);
  font-size: 1.5rem;
  font-weight: 650;
  transition: transform 0.12s ease, background 0.2s ease;
  display: flex;
  align-items: center;
  justify-content: center;
}
.pin-key:active { transform: scale(0.9); background: var(--card-hover); }
.pin-error { color: var(--red-soft); font-size: 0.9rem; min-height: 22px; margin-top: 14px; }
.shake { animation: shake 0.4s ease; }
@keyframes shake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-8px); }
  75% { transform: translateX(8px); }
}

.avatar-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  justify-content: center;
  margin-top: 12px;
}
.avatar-pick {
  width: 56px;
  height: 56px;
  border-radius: 50%;
  background: var(--card);
  font-size: 1.5rem;
  border: 2px solid transparent;
  display: flex;
  align-items: center;
  justify-content: center;
}
.avatar-pick.selected { border-color: var(--gold); box-shadow: var(--glow); }

/* ---------- waiting game ---------- */
.waiting-wrap {
  position: fixed;
  inset: 0;
  z-index: 5;
  display: flex;
  flex-direction: column;
  background: radial-gradient(ellipse at 50% 0%, #1c1838, var(--bg));
  padding: calc(var(--safe-top) + 16px) 16px calc(var(--safe-bottom) + 16px);
}
.waiting-head { text-align: center; }
.waiting-head .status-line {
  color: var(--ink-soft);
  font-size: 0.92rem;
  margin-top: 6px;
  animation: pulseSoft 2.5s ease-in-out infinite;
}
.game-target {
  margin: 12px auto 0;
  padding: 10px 22px;
  border-radius: 999px;
  background: var(--card);
  font-weight: 700;
  box-shadow: var(--shadow);
  display: inline-block;
}
.game-score { color: var(--gold); font-weight: 700; }
#game-field {
  flex: 1;
  position: relative;
  overflow: hidden;
  border-radius: var(--radius);
  margin-top: 14px;
  touch-action: manipulation;
}
.floater {
  position: absolute;
  font-size: 2.6rem;
  user-select: none;
  cursor: pointer;
  transition: transform 0.15s ease;
  will-change: transform;
}
.floater.pop { animation: pop 0.45s ease forwards; }
@keyframes pop {
  40% { transform: scale(1.5); opacity: 1; }
  100% { transform: scale(0); opacity: 0; }
}
.floater.nope { animation: shake 0.35s ease; }

/* ---------- reader ---------- */
.reader {
  position: fixed;
  inset: 0;
  z-index: 6;
  background: var(--bg);
  overflow: hidden;
}
.reader-pages {
  display: flex;
  height: 100%;
  transition: transform 0.45s cubic-bezier(0.22, 0.9, 0.3, 1);
  will-change: transform;
}
.reader-page {
  flex: 0 0 100%;
  height: 100%;
  position: relative;
  display: flex;
  flex-direction: column;
}
.reader-page .art {
  position: absolute;
  inset: 0;
  background-size: cover;
  background-position: center;
}
.reader-page .art::after {
  content: '';
  position: absolute;
  inset: 0;
  background: linear-gradient(to bottom, rgba(13,13,20,0.25) 0%, transparent 30%, transparent 45%, rgba(13,13,20,0.93) 78%, #0d0d14 100%);
}
.reader-page .page-text {
  position: relative;
  margin-top: auto;
  padding: 0 26px calc(var(--safe-bottom) + 108px);
  font-size: 1.18rem;
  line-height: 1.65;
  text-shadow: 0 2px 12px rgba(0, 0, 0, 0.8);
  font-weight: 500;
  max-width: 640px;
  margin-left: auto;
  margin-right: auto;
}
.reader-controls {
  position: absolute;
  bottom: calc(var(--safe-bottom) + 18px);
  left: 0;
  right: 0;
  z-index: 8;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 18px;
}
.reader-close {
  position: absolute;
  top: calc(var(--safe-top) + 14px);
  right: 14px;
  z-index: 8;
  background: #00000066;
  backdrop-filter: blur(8px);
}
.reader-title-tap {
  position: absolute;
  top: calc(var(--safe-top) + 14px);
  left: 14px;
  right: 74px;
  z-index: 8;
  font-weight: 700;
  font-size: 1rem;
  text-shadow: 0 2px 10px rgba(0,0,0,0.8);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  text-align: left;
  padding: 8px 0;
}
.page-dots {
  display: flex;
  gap: 7px;
  align-items: center;
}
.page-dots span {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: #ffffff3d;
  transition: all 0.3s ease;
}
.page-dots span.active { background: var(--gold); transform: scale(1.45); }
.play-btn {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  background: #ffffff1f;
  backdrop-filter: blur(10px);
  font-size: 1.45rem;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 1.5px solid #ffffff2e;
}
.auto-btn {
  font-size: 0.78rem;
  font-weight: 700;
  padding: 9px 15px;
  border-radius: 999px;
  background: #ffffff14;
  backdrop-filter: blur(8px);
  color: var(--ink-soft);
  border: 1.5px solid transparent;
}
.auto-btn.on { color: var(--gold); border-color: var(--gold); }

#particle-layer {
  position: absolute;
  inset: 0;
  z-index: 7;
  pointer-events: none;
  overflow: hidden;
}
.particle {
  position: absolute;
  pointer-events: none;
  animation: drift linear forwards;
  opacity: 0;
}
@keyframes drift {
  0% { opacity: 0; transform: translateY(0) rotate(0deg); }
  12% { opacity: 0.85; }
  88% { opacity: 0.85; }
  100% { opacity: 0; transform: translateY(-110vh) rotate(50deg); }
}

/* ---------- settings ---------- */
.settings-section {
  background: var(--card);
  border-radius: var(--radius);
  padding: 20px;
  margin-bottom: 18px;
}
.settings-section h2 { font-size: 1.05rem; margin-bottom: 4px; }
.settings-section .hint { font-size: 0.8rem; color: var(--ink-faint); margin: 2px 0 8px; }
.user-list { display: flex; flex-direction: column; gap: 10px; margin-top: 14px; }
.user-list-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 14px;
  border-radius: var(--radius-sm);
  background: var(--bg-2);
}
.user-list-item .avatar { font-size: 1.4rem; }
.user-list-item .name { flex: 1; font-weight: 600; }
.user-list-item .tag { font-size: 0.72rem; color: var(--gold); font-weight: 700; }
.range-row { display: flex; align-items: center; gap: 12px; margin: 10px 0; }
.range-row label { flex: 1; font-size: 0.88rem; color: var(--ink-soft); }
.range-row input[type='range'] { flex: 2; accent-color: var(--purple); }
.range-row .val { width: 40px; text-align: right; font-size: 0.85rem; color: var(--ink-faint); }

/* ---------- toast ---------- */
#toast {
  position: fixed;
  bottom: calc(var(--safe-bottom) + 28px);
  left: 50%;
  transform: translateX(-50%) translateY(20px);
  z-index: 50;
  background: var(--card-hover);
  color: var(--ink);
  padding: 13px 22px;
  border-radius: 999px;
  font-weight: 600;
  font-size: 0.92rem;
  box-shadow: var(--shadow);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.3s ease, transform 0.3s ease;
  max-width: 86vw;
  text-align: center;
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ---------- modal ---------- */
.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 40;
  background: rgba(8, 8, 14, 0.75);
  backdrop-filter: blur(6px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  animation: fadeIn 0.25s ease;
}
@keyframes fadeIn { from { opacity: 0; } }
.modal {
  background: var(--card);
  border-radius: var(--radius);
  padding: 26px;
  max-width: 380px;
  width: 100%;
  box-shadow: var(--shadow);
  animation: viewIn 0.3s ease;
  text-align: center;
}
.modal h3 { margin-bottom: 8px; }
.modal p { color: var(--ink-soft); font-size: 0.95rem; margin: 0 0 20px; }
.modal .row { display: flex; gap: 12px; }
.modal .row .btn { flex: 1; }

/* desktop niceties */
@media (hover: hover) {
  .story-card:hover { transform: translateY(-4px); box-shadow: 0 14px 40px rgba(0,0,0,0.55); }
  .choice:hover, .btn:hover, .pin-key:hover { background: var(--card-hover); }
  .btn-primary:hover { background: linear-gradient(135deg, #8a6ce2, #b69cff); }
  .btn-gold:hover { background: linear-gradient(135deg, #e5b363, #ffd98f); }
}

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