:root {
  --ink: #1e1b4b;
  --muted: #5b6178;
  --card: #ffffff;
  --paper: #fffbeb;
  --paper-edge: #fde68a;
  --brand: #1d4ed8;
  --brand-2: #7c3aed;
  --sel: #eff6ff;
  --sel-border: #3b82f6;
  --play: #16a34a;
  --play-dark: #15803d;
  --good: #16a34a;
  --bad: #dc2626;
  --bg-1: #34d399;
  --bg-2: #3b82f6;
  --bg-3: #8b5cf6;
  --radius: 22px;
  --font: 'Fredoka', ui-rounded, 'SF Pro Rounded', 'Segoe UI', system-ui, sans-serif;
  --emoji: 'Apple Color Emoji', 'Segoe UI Emoji', 'Noto Color Emoji', sans-serif;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
}

* { box-sizing: border-box; }
html, body {
  margin: 0; min-height: 100%;
  font-family: var(--font); color: var(--ink);
  background: linear-gradient(160deg, var(--bg-1), var(--bg-2) 50%, var(--bg-3)) fixed;
  background-color: var(--bg-2);
  -webkit-tap-highlight-color: transparent;
  overflow-x: hidden;
}
button { font: inherit; color: inherit; cursor: pointer; }
button:disabled { cursor: default; }
[hidden] { display: none !important; }
:focus-visible { outline: 3px solid #fde047; outline-offset: 2px; }

.screen { display: none; }
.screen.active { display: block; }

/* ---------- Home & results ---------- */
#home, #results {
  min-height: 100dvh;
  padding: calc(14px + var(--safe-t)) calc(16px + var(--safe-r)) calc(24px + var(--safe-b)) calc(16px + var(--safe-l));
}
.inner { max-width: 820px; margin: 0 auto; display: grid; gap: 16px; }
.back { color: #fff; font-weight: 600; text-decoration: none; justify-self: start; padding: 10px 2px; min-height: 44px; }
.brand { text-align: center; color: #fff; }
.brand .logo { font-size: clamp(40px, 10vw, 64px); line-height: 1.1; font-family: var(--emoji); }
.brand h1 { margin: 2px 0 0; font-size: clamp(34px, 8vw, 56px); text-shadow: 0 3px 0 rgba(30, 27, 75, .25); }
.brand p { margin: 4px 0 0; font-size: clamp(17px, 4vw, 20px); font-weight: 500; }

.card { background: var(--card); border-radius: var(--radius); padding: 16px; box-shadow: 0 10px 30px rgba(30, 27, 75, .2); }
.card h2 { margin: 0 0 12px; font-size: 20px; display: flex; align-items: center; gap: 8px; }
.step { display: inline-grid; place-items: center; width: 28px; height: 28px; border-radius: 50%; background: var(--brand); color: #fff; font-size: 16px; }

.years { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
@media (min-width: 640px) { .years { grid-template-columns: repeat(6, 1fr); } }
.year { border: 3px solid #e5e7eb; background: #f8fafc; border-radius: 18px; min-height: 76px; display: grid; place-content: center; padding: 6px; }
.year b { font-size: 28px; line-height: 1; }
.year small { color: var(--muted); font-size: 13px; }
.year[aria-checked="true"] { border-color: var(--sel-border); background: var(--sel); box-shadow: 0 0 0 3px rgba(59,130,246,.2); }

.dskp { margin-top: 12px; background: #f8fafc; border-radius: 14px; padding: 10px 12px; }
.dskp summary { cursor: pointer; font-weight: 600; color: var(--brand); min-height: 28px; }
.dskp ul { margin: 10px 0 0; padding: 0; list-style: none; display: grid; gap: 6px; }
.dskp li { display: grid; grid-template-columns: auto 1fr; gap: 8px; font-size: 14px; line-height: 1.35; }
.dskp .code { font-weight: 700; color: var(--brand); white-space: nowrap; }
.dskp .note { margin: 10px 0 0; font-size: 12px; color: var(--muted); }

.option { display: grid; gap: 6px; margin-top: 12px; }
.option-label { font-weight: 600; color: var(--muted); font-size: 14px; }
.seg { display: flex; flex-wrap: wrap; gap: 6px; }
.seg button { flex: 1 1 auto; min-height: 44px; padding: 8px 12px; border-radius: 14px; border: 2px solid #e5e7eb; background: #f8fafc; font-weight: 500; font-size: 15px; }
.seg button[aria-pressed="true"] { border-color: var(--sel-border); background: var(--sel); font-weight: 600; }
.level-seg button { min-height: 56px; font-size: 17px; }
.level-note { margin: 8px 0 0; font-size: 14px; color: var(--muted); line-height: 1.35; }
.tip { margin: 12px 0 0; font-size: 14px; color: var(--muted); line-height: 1.4; }
kbd { font: inherit; font-weight: 600; background: #f1f5f9; border: 1px solid #cbd5e1; border-bottom-width: 2px; border-radius: 6px; padding: 0 5px; }

/* Story cards */
.stories { display: grid; gap: 10px; grid-template-columns: 1fr; }
@media (min-width: 640px) { .stories { grid-template-columns: 1fr 1fr; } }
.story-card {
  display: grid; grid-template-columns: auto 1fr; grid-template-rows: auto auto 1fr; column-gap: 12px; row-gap: 2px;
  text-align: left; border: 3px solid var(--paper-edge); background: var(--paper); border-radius: 18px; padding: 12px 14px;
  min-height: 96px; box-shadow: 0 4px 0 #fcd34d; transition: transform .1s;
}
.story-card:active { transform: translateY(2px); box-shadow: 0 2px 0 #fcd34d; }
.story-card .cover { grid-row: 1 / 4; font-family: var(--emoji); font-size: 40px; line-height: 1; align-self: center; }
.story-card b { font-size: 19px; line-height: 1.2; color: var(--brand); }
.story-card .blurb { font-size: 15px; color: var(--ink); line-height: 1.3; }
.story-card .meta { font-size: 13px; color: var(--muted); margin-top: 2px; }
.story-card.surprise { background: var(--sel); border-color: #bfdbfe; box-shadow: 0 4px 0 #93c5fd; }
@media (min-width: 640px) { .story-card.surprise { grid-column: 1 / -1; min-height: 72px; } }

.btn { border: 0; border-radius: 999px; min-height: 52px; padding: 10px 26px; font-weight: 700; font-size: 20px; transition: transform .1s; }
.btn:active { transform: translateY(2px); }
.btn:disabled { background: #94a3b8; box-shadow: 0 5px 0 #64748b; transform: none; }
.btn-play { background: var(--play); color: #fff; box-shadow: 0 5px 0 var(--play-dark); }
.btn-ghost { background: #fff; color: var(--brand); box-shadow: 0 5px 0 #bfdbfe; }
.btn-small { border: 2px solid #bfdbfe; background: #fff; border-radius: 999px; padding: 6px 16px; font-weight: 600; font-size: 16px; min-height: 44px; }
.install { display: grid; justify-items: center; gap: 6px; color: #fff; font-size: 14px; text-align: center; }
.install p { margin: 0; }

/* ---------- Story ---------- */
#game.active { min-height: 100dvh; }
.layout {
  min-height: 100dvh; max-width: 860px; margin: 0 auto;
  display: flex; flex-direction: column; gap: 10px;
  padding: calc(10px + var(--safe-t)) calc(12px + var(--safe-r)) 0 calc(12px + var(--safe-l));
}
.hud { display: flex; gap: 8px; align-items: center; }
.pill { background: rgba(255,255,255,.95); border: 0; border-radius: 999px; padding: 6px 12px; font-weight: 700; font-size: clamp(15px, 2.4vw, 20px); box-shadow: 0 4px 12px rgba(0,0,0,.12); white-space: nowrap; }
.hud-year { margin-left: auto; background: var(--brand); color: #fff; }
.hud-pause { min-width: 48px; min-height: 44px; }
.progress { height: 8px; border-radius: 999px; background: rgba(255,255,255,.35); overflow: hidden; }
.progress i { display: block; height: 100%; width: 0; background: #fde047; border-radius: inherit; transition: width .4s; }

/* The book page */
.page {
  position: relative; background: var(--paper); border-radius: 8px var(--radius) var(--radius) 8px;
  padding: 14px 18px 18px 26px; box-shadow: inset 12px 0 14px -10px rgba(120, 53, 15, .35), 0 10px 30px rgba(30, 27, 75, .25);
  border-left: 6px solid #b45309; display: grid; grid-template-columns: minmax(0, 1fr); gap: 10px;
}
.page.turn { animation: turn .35s ease-out; transform-origin: left center; }
@keyframes turn { from { opacity: 0; transform: translateX(-10px); } }
.ptitle { margin: 0; font-size: 14px; font-weight: 600; color: #92400e; text-align: center; letter-spacing: .02em; }
/* --n = number of emoji, so the picture always fits on one line. */
.page > * { min-width: 0; }
.ppic { overflow: hidden; font-family: var(--emoji); font-size: clamp(36px, min(11vh, calc(56vw / var(--n, 3))), 110px); line-height: 1.15; text-align: center; letter-spacing: 4px; white-space: nowrap; }
@media (min-width: 640px) { .ppic { font-size: clamp(48px, min(12vh, calc(560px / var(--n, 3))), 110px); } }
.ptext { font-size: clamp(19px, 2.6vw, 24px); line-height: 1.55; border-radius: 12px; transition: background .3s, box-shadow .3s; }
.ptext p { margin: 0 0 .5em; }
.ptext p:last-child { margin-bottom: 0; }
.ptext b { color: var(--brand-2); }
.page[data-year="1"] .ptext, .page[data-year="2"] .ptext { font-size: clamp(26px, 6.5vw, 40px); text-align: center; font-weight: 500; line-height: 1.4; }
.page[data-year="3"] .ptext { font-size: clamp(21px, 3.6vw, 28px); }
.ptext.lookback { background: #fef9c3; box-shadow: 0 0 0 4px #fde047; animation: pulse 1s ease-in-out 2; }
@keyframes pulse { 50% { box-shadow: 0 0 0 9px rgba(253, 224, 71, .6); } }
.ptext .w { cursor: pointer; border-radius: 6px; border-bottom: 2px dotted rgba(29, 78, 216, .35); }
.ptext .w.said { animation: said .9s; }
@keyframes said { 20% { background: #bfdbfe; } }
.end-title { font-size: 1.6em; font-weight: 700; color: var(--brand); text-align: center; }
.page.the-end .ptext { text-align: center; }

.ptools { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; justify-content: center; }
.ptools:not(:has(:not([hidden]))) { display: none; }
.tap-tip { font-size: 15px; color: var(--muted); }

/* Choices and questions */
.choice, .qbox, .react { border-top: 2px dashed var(--paper-edge); padding-top: 12px; display: grid; gap: 8px; }
.cprompt, .qprompt { margin: 0; font-size: clamp(19px, 2.8vw, 24px); font-weight: 700; text-align: center; }
.qprompt b { color: var(--brand-2); }
.qhint { margin: 0; font-size: 16px; color: #92400e; text-align: center; line-height: 1.35; }
.qhint:empty { display: none; }
.std { justify-self: center; font-size: 12px; font-weight: 600; color: var(--brand); background: var(--sel); border-radius: 999px; padding: 2px 10px; }
.options { display: grid; gap: 8px; }
@media (min-width: 640px) { .options { grid-template-columns: 1fr 1fr; } }
.opt {
  display: flex; align-items: center; gap: 10px; text-align: left; min-height: 56px; padding: 8px 14px;
  border: 3px solid #e5e7eb; border-radius: 16px; background: #fff; font-size: clamp(18px, 2.4vw, 21px); font-weight: 600; line-height: 1.25;
  box-shadow: 0 4px 0 #dbeafe; transition: transform .1s;
}
.opt:active { transform: translateY(2px); }
.opt .key { display: inline-grid; place-items: center; min-width: 26px; height: 26px; border-radius: 50%; background: var(--sel); color: var(--brand); font-size: 14px; font-weight: 700; }
.opt .o-emoji { font-family: var(--emoji); font-size: 1.5em; line-height: 1; }
.opt .o-emoji:empty { display: none; }
.opt .o-text b { color: var(--brand-2); }
.opt.right { border-color: var(--good); background: #dcfce7; box-shadow: 0 4px 0 #86efac; }
.opt.wrong { border-color: var(--bad); background: #fee2e2; box-shadow: none; animation: shake .45s; }
.opt:disabled:not(.right):not(.wrong) { opacity: .5; }
.opt.locked { pointer-events: none; }
@keyframes shake { 20%, 60% { transform: translateX(-6px); } 40%, 80% { transform: translateX(6px); } }
.qfeedback { margin: 0; text-align: center; font-size: 19px; font-weight: 700; min-height: 1.3em; }
.qfeedback.good { color: var(--good); }
.qfeedback.bad { color: #b45309; }
.qfeedback b { color: var(--ink); }

/* The end: reaction */
.enjoy { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.favs { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; }
.enjoy-btn, .fav-btn {
  display: grid; justify-items: center; gap: 2px; min-height: 56px; padding: 8px 10px; border-radius: 16px;
  border: 3px solid #e5e7eb; background: #fff; font-size: 16px; font-weight: 600; box-shadow: 0 4px 0 #dbeafe;
}
.fav-btn { flex: 1 1 120px; max-width: 200px; }
.enjoy-btn .big, .fav-btn .big { font-family: var(--emoji); font-size: 34px; line-height: 1.1; }
.enjoy-btn[aria-pressed="true"], .fav-btn[aria-pressed="true"] { border-color: var(--sel-border); background: var(--sel); box-shadow: 0 0 0 3px rgba(59,130,246,.25); }

.actions {
  position: sticky; bottom: 0; z-index: 3; display: flex; justify-content: center;
  padding: 10px 0 calc(14px + var(--safe-b)); margin-top: auto;
}
#nextBtn { min-width: min(100%, 280px); font-size: 22px; min-height: 58px; }

.overlay { position: fixed; inset: 0; z-index: 7; display: grid; place-items: center; padding: 16px; background: rgba(30, 27, 75, .45); }
.panel { background: #fff; border-radius: var(--radius); padding: 20px; width: min(100%, 480px); text-align: center; box-shadow: 0 10px 40px rgba(0,0,0,.3); }
.ov-icon { font-size: 52px; line-height: 1.1; }
.panel h2 { margin: 6px 0; font-size: 26px; }
.panel p { margin: 0 0 14px; color: var(--muted); font-size: 17px; line-height: 1.4; }
.buttons { display: flex; flex-wrap: wrap; gap: 10px; justify-content: center; }

/* ---------- Results ---------- */
.results { display: grid; gap: 10px; text-align: center; }
.results h2 { justify-content: center; font-size: 30px; margin: 0; }
.results h3 { margin: 10px 0 0; font-size: 18px; }
.stars { font-size: clamp(48px, 13vw, 72px); letter-spacing: 4px; }
.stars .off { filter: grayscale(1); opacity: .3; }
.res-score { margin: 0; font-size: 19px; font-weight: 600; line-height: 1.35; }
.res-react { margin: 0; font-size: 17px; background: var(--paper); border-radius: 14px; padding: 8px 12px; line-height: 1.4; }
.res-path { margin: 0; font-size: 15px; color: var(--muted); }
.res-path:empty { display: none; }
.table-wrap { overflow-x: auto; }
.std-table { width: 100%; border-collapse: collapse; text-align: left; font-size: 14px; }
.std-table th, .std-table td { padding: 8px; border-bottom: 1px solid #f1f5f9; vertical-align: top; }
.std-table th { color: var(--muted); font-weight: 600; }
.std-table td:first-child { font-weight: 700; color: var(--brand); white-space: nowrap; }
.std-table td:last-child { font-weight: 700; }
#copyBtn { justify-self: center; }
.review { text-align: left; margin: 0; padding-left: 24px; display: grid; gap: 10px; font-size: 16px; line-height: 1.4; }
.review .std { font-size: 11px; vertical-align: middle; }
.review .rq { font-weight: 600; }
.review .rq b { color: var(--brand-2); }
.review .ra, .review .rc { color: var(--muted); }
.review .mark { font-size: 14px; font-weight: 700; margin-left: 4px; white-space: nowrap; }
.review .ok { color: var(--good); }
.review .mid { color: #b45309; }
.review .no { color: var(--bad); }

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