:root {
  --ink: #2b2438;
  --night-1: #1a1730;
  --night-2: #241b3d;
  --night-3: #120f24;
  --parchment: #f7efe1;
  --parchment-edge: #ece0c9;
  --moon: #f4d58d;
  --moon-soft: #f7e3b0;
  --lilac: #b9a6e6;
  --rose: #e6a4a4;
  --mist: #cdc4e6;
  --line: rgba(205, 196, 230, 0.18);
  --card-shadow: 0 22px 48px -24px rgba(8, 6, 20, 0.8);
  --serif: "Newsreader", Georgia, serif;
  --display: "Fraunces", Georgia, serif;
}

* { box-sizing: border-box; }

html { scroll-behavior: smooth; }

body {
  margin: 0;
  min-height: 100vh;
  font-family: var(--serif);
  color: var(--mist);
  background: radial-gradient(140% 120% at 80% -10%, #2e2350 0%, var(--night-2) 38%, var(--night-3) 100%);
  background-attachment: fixed;
  -webkit-font-smoothing: antialiased;
}

/* ───────────── atmosphere ───────────── */
.sky { position: fixed; inset: 0; z-index: 0; overflow: hidden; pointer-events: none; }
.moon {
  position: absolute;
  top: 7vh; right: 9vw;
  width: 150px; height: 150px;
  border-radius: 50%;
  background: radial-gradient(circle at 35% 32%, #fdf3d6, var(--moon) 60%, #e7be72 100%);
  box-shadow: 0 0 90px 24px rgba(244, 213, 141, 0.28), inset -16px -10px 0 -4px rgba(33, 27, 61, 0.32);
}
.stars {
  position: absolute; inset: 0;
  background-image:
    radial-gradient(1.4px 1.4px at 12% 28%, #fff8e6 50%, transparent),
    radial-gradient(1.2px 1.2px at 24% 64%, #ffffff 50%, transparent),
    radial-gradient(1.6px 1.6px at 47% 18%, #fdeecb 50%, transparent),
    radial-gradient(1.1px 1.1px at 63% 48%, #ffffff 50%, transparent),
    radial-gradient(1.5px 1.5px at 78% 72%, #fff3d0 50%, transparent),
    radial-gradient(1.2px 1.2px at 88% 32%, #ffffff 50%, transparent),
    radial-gradient(1.3px 1.3px at 34% 84%, #fdeecb 50%, transparent),
    radial-gradient(1.1px 1.1px at 56% 76%, #ffffff 50%, transparent),
    radial-gradient(1.4px 1.4px at 7% 56%, #fff3d0 50%, transparent);
  animation: twinkle 6s ease-in-out infinite alternate;
}
@keyframes twinkle { from { opacity: 0.55; } to { opacity: 1; } }

/* ───────────── masthead ───────────── */
.masthead {
  position: relative; z-index: 1;
  max-width: 820px;
  margin: 0 auto;
  padding: 9vh 24px 2.4rem;
  text-align: center;
  animation: rise 0.9s cubic-bezier(0.2, 0.8, 0.2, 1) both;
}
.kicker {
  font-family: var(--serif);
  font-style: italic;
  letter-spacing: 0.04em;
  color: var(--lilac);
  margin: 0 0 0.6rem;
  font-size: 0.98rem;
}
.masthead h1 {
  font-family: var(--display);
  font-weight: 600;
  font-optical-sizing: auto;
  font-size: clamp(3rem, 9vw, 5.6rem);
  line-height: 0.96;
  margin: 0;
  color: var(--moon-soft);
  text-shadow: 0 2px 30px rgba(244, 213, 141, 0.22);
}
.masthead .lede {
  max-width: 36rem;
  margin: 1.1rem auto 0;
  font-size: 1.08rem;
  line-height: 1.6;
  color: var(--mist);
}

/* ───────────── layout ───────────── */
.layout {
  position: relative; z-index: 1;
  max-width: 1120px;
  margin: 0 auto;
  padding: 1rem 22px 4rem;
  display: grid;
  grid-template-columns: minmax(0, 0.82fr) minmax(0, 1.18fr);
  gap: 26px;
  align-items: start;
}
@media (max-width: 880px) {
  .layout { grid-template-columns: 1fr; }
}

.panel {
  background: linear-gradient(180deg, rgba(58, 46, 92, 0.55), rgba(30, 23, 52, 0.55));
  border: 1px solid var(--line);
  border-radius: 22px;
  padding: 1.5rem 1.5rem 1.7rem;
  backdrop-filter: blur(6px);
  box-shadow: var(--card-shadow);
  animation: rise 0.9s cubic-bezier(0.2, 0.8, 0.2, 1) both;
}
.tales { animation-delay: 0.12s; }

.panel-head {
  display: flex; align-items: center; justify-content: space-between;
  gap: 1rem;
  margin-bottom: 1.2rem;
}
.panel-head h2 {
  font-family: var(--display);
  font-weight: 560;
  font-size: 1.7rem;
  margin: 0;
  color: #fdf3d6;
}
.count {
  font-family: var(--display);
  font-size: 1.1rem;
  color: var(--moon);
  background: rgba(244, 213, 141, 0.12);
  border: 1px solid rgba(244, 213, 141, 0.25);
  border-radius: 999px;
  padding: 0.1rem 0.7rem;
}

/* ───────────── form ───────────── */
.char-form { display: grid; gap: 0.85rem; margin-bottom: 1.4rem; }
.field { display: grid; gap: 0.3rem; }
.field label, .younger-toggle span {
  font-size: 0.8rem;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  color: var(--lilac);
}
.char-form input[type="text"],
.char-form input#char-name,
.char-form select,
.char-form textarea {
  width: 100%;
  font-family: var(--serif);
  font-size: 1rem;
  color: #f4eede;
  background: rgba(16, 12, 30, 0.5);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 0.6rem 0.75rem;
  transition: border-color 0.2s, box-shadow 0.2s;
}
.char-form textarea { resize: vertical; line-height: 1.5; }
.char-form input:focus, .char-form select:focus, .char-form textarea:focus {
  outline: none;
  border-color: var(--moon);
  box-shadow: 0 0 0 3px rgba(244, 213, 141, 0.16);
}
.char-form select { appearance: none; cursor: pointer; }
.row { display: grid; grid-template-columns: 1fr auto; gap: 1rem; align-items: end; }
.younger-toggle {
  display: flex; align-items: center; gap: 0.5rem;
  cursor: pointer; user-select: none;
  padding-bottom: 0.6rem;
}
.younger-toggle input { width: 1.05rem; height: 1.05rem; accent-color: var(--moon); cursor: pointer; }
.younger-toggle span { text-transform: none; letter-spacing: 0; font-size: 0.92rem; color: var(--mist); }

.form-actions { display: flex; gap: 0.6rem; align-items: center; }
.form-error { color: var(--rose); font-style: italic; margin: 0; font-size: 0.92rem; }

/* ───────────── buttons ───────────── */
.btn {
  font-family: var(--display);
  font-weight: 540;
  font-size: 0.98rem;
  border: none;
  border-radius: 12px;
  padding: 0.6rem 1.1rem;
  cursor: pointer;
  transition: transform 0.16s ease, box-shadow 0.2s ease, background 0.2s;
}
.btn:active { transform: translateY(1px) scale(0.99); }
.btn-primary {
  background: linear-gradient(180deg, var(--moon-soft), var(--moon));
  color: #3a2c10;
  box-shadow: 0 10px 22px -10px rgba(244, 213, 141, 0.7);
}
.btn-primary:hover { box-shadow: 0 14px 28px -10px rgba(244, 213, 141, 0.85); transform: translateY(-1px); }
.btn-ghost {
  background: transparent;
  color: var(--mist);
  border: 1px solid var(--line);
}
.btn-ghost:hover { border-color: var(--lilac); color: #efeaff; }
.btn-moon {
  background: linear-gradient(180deg, rgba(185, 166, 230, 0.22), rgba(185, 166, 230, 0.08));
  color: #f3eeff;
  border: 1px solid rgba(185, 166, 230, 0.4);
  display: inline-flex; align-items: center; gap: 0.5rem;
}
.btn-moon::before {
  content: ""; width: 0.85rem; height: 0.85rem; border-radius: 50%;
  background: radial-gradient(circle at 35% 35%, #fff, var(--moon));
  box-shadow: 0 0 10px 1px rgba(244, 213, 141, 0.7);
}
.btn-moon:hover { transform: translateY(-1px); background: linear-gradient(180deg, rgba(185, 166, 230, 0.32), rgba(185, 166, 230, 0.12)); }
.btn-moon[disabled] { opacity: 0.55; cursor: progress; }
.btn-moon.thinking .btn-label::after {
  content: ""; animation: dots 1.4s steps(4, end) infinite;
}
@keyframes dots { 0% { content: ""; } 25% { content: "."; } 50% { content: ".."; } 75% { content: "..."; } }

/* ───────────── character list ───────────── */
.char-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 0.7rem; }
.char-card {
  position: relative;
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 0.4rem 0.8rem;
  background: rgba(16, 12, 30, 0.4);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 0.8rem 0.9rem;
  animation: rise 0.5s ease both;
}
.char-card .name {
  font-family: var(--display);
  font-size: 1.18rem;
  color: #fdf3d6;
  display: flex; align-items: center; gap: 0.5rem; flex-wrap: wrap;
}
.pill {
  font-family: var(--serif);
  font-size: 0.72rem;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  padding: 0.12rem 0.55rem;
  border-radius: 999px;
  border: 1px solid rgba(185, 166, 230, 0.4);
  color: var(--lilac);
  background: rgba(185, 166, 230, 0.1);
}
.pill.younger { color: var(--moon); border-color: rgba(244, 213, 141, 0.4); background: rgba(244, 213, 141, 0.1); }
.char-card .traits { grid-column: 1 / -1; color: var(--mist); font-size: 0.96rem; line-height: 1.45; opacity: 0.92; }
.char-card .traits.empty { font-style: italic; opacity: 0.55; }
.char-actions { display: flex; gap: 0.35rem; align-self: start; }
.icon-btn {
  background: transparent; border: 1px solid var(--line); color: var(--mist);
  border-radius: 9px; width: 2rem; height: 2rem; cursor: pointer;
  font-size: 0.9rem; line-height: 1; transition: all 0.18s;
}
.icon-btn:hover { color: #fff; border-color: var(--lilac); }
.icon-btn.danger:hover { color: var(--rose); border-color: var(--rose); }

/* ───────────── stories ───────────── */
#stories { display: grid; gap: 0.85rem; }
.story-card {
  text-align: left;
  width: 100%;
  cursor: pointer;
  background:
    linear-gradient(180deg, var(--parchment), var(--parchment-edge));
  border: none;
  border-radius: 16px;
  padding: 1.1rem 1.25rem;
  box-shadow: var(--card-shadow);
  transition: transform 0.18s ease, box-shadow 0.2s ease;
  animation: rise 0.5s ease both;
  position: relative;
  overflow: hidden;
}
.story-card::before {
  content: ""; position: absolute; left: 0; top: 0; bottom: 0; width: 5px;
  background: linear-gradient(var(--moon), var(--rose));
}
.story-card:hover { transform: translateY(-2px); box-shadow: 0 26px 52px -22px rgba(8, 6, 20, 0.9); }
.story-card .s-activity {
  font-style: italic; color: #8a6d4b; font-size: 0.86rem; margin: 0 0 0.2rem;
}
.story-card .s-title {
  font-family: var(--display); font-weight: 600; font-size: 1.4rem;
  color: #2e2233; margin: 0 0 0.35rem; line-height: 1.1;
}
.story-card .s-meta { color: #7a6750; font-size: 0.82rem; margin: 0; display: flex; gap: 0.5rem; flex-wrap: wrap; }
.story-card .s-snippet { color: #4a3c30; font-size: 0.96rem; line-height: 1.5; margin: 0.5rem 0 0; }

/* ───────────── empty / skeleton ───────────── */
.empty {
  text-align: center; padding: 2.2rem 1rem; color: var(--mist);
  border: 1px dashed var(--line); border-radius: 16px;
}
.empty strong { color: var(--moon-soft); font-family: var(--display); display: block; font-size: 1.2rem; margin-bottom: 0.3rem; }
.skeleton {
  height: 92px; border-radius: 16px;
  background: linear-gradient(100deg, rgba(247, 239, 225, 0.12) 30%, rgba(247, 239, 225, 0.22) 50%, rgba(247, 239, 225, 0.12) 70%);
  background-size: 200% 100%;
  animation: shimmer 1.5s infinite;
}
@keyframes shimmer { from { background-position: 200% 0; } to { background-position: -200% 0; } }

/* ───────────── reader ───────────── */
.reader[hidden] { display: none; }
.reader {
  position: fixed; inset: 0; z-index: 50;
  display: flex; align-items: flex-start; justify-content: center;
  padding: 5vh 20px;
  background: rgba(10, 8, 22, 0.72);
  backdrop-filter: blur(5px);
  overflow-y: auto;
  animation: fade 0.25s ease both;
}
.reader-card {
  position: relative;
  max-width: 680px; width: 100%;
  background: linear-gradient(180deg, var(--parchment), #f1e7d4);
  border-radius: 20px;
  padding: 2.6rem clamp(1.4rem, 5vw, 3rem) 2.8rem;
  box-shadow: 0 40px 90px -30px rgba(0, 0, 0, 0.85);
  color: #2e2233;
  animation: rise 0.4s cubic-bezier(0.2, 0.8, 0.2, 1) both;
}
.reader-close {
  position: absolute; top: 0.9rem; right: 1.1rem;
  background: transparent; border: none; font-size: 1.9rem; line-height: 1;
  color: #8a6d4b; cursor: pointer; transition: color 0.2s, transform 0.2s;
}
.reader-close:hover { color: #2e2233; transform: rotate(90deg); }
.reader-activity { font-style: italic; color: #8a6d4b; margin: 0 0 0.3rem; }
.reader-card h3 {
  font-family: var(--display); font-weight: 600;
  font-size: clamp(1.8rem, 5vw, 2.6rem); line-height: 1.05;
  margin: 0 0 0.4rem; color: #281d2f;
}
.reader-cast { color: #7a6750; font-size: 0.9rem; margin: 0 0 1.4rem; }
.reader-body { font-size: 1.12rem; line-height: 1.75; }
.reader-body p { margin: 0 0 1.1rem; }
.reader-body p:first-letter {
  /* drop cap on the opening paragraph */
}
.reader-body p.lead:first-letter {
  font-family: var(--display); font-weight: 700;
  float: left; font-size: 3.4rem; line-height: 0.78;
  padding: 0.3rem 0.6rem 0 0; color: #b8893f;
}

/* ───────────── footer ───────────── */
.footer {
  position: relative; z-index: 1;
  text-align: center; padding: 1rem 20px 3rem;
  color: var(--lilac); font-style: italic; opacity: 0.8;
}

@keyframes rise { from { opacity: 0; transform: translateY(16px); } to { opacity: 1; transform: none; } }
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }

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