:root {
  --bg: #f5f4fa;
  --card: #ffffff;
  --text: #1e1b2e;
  --muted: #5f5a75;
  --line: #e3e0ef;
  --brand: #4338ca;
  --brand-soft: #ecebfb;
  --on-brand: #ffffff;
  --done: #15803d;
  --paper: #fffdf6;
  --shadow: 0 1px 2px rgba(30, 27, 46, .06), 0 4px 14px rgba(30, 27, 46, .05);
  --bar: #4338ca;
}
/* Each task type sets --h (a hue); these derive its tint and ink colours. */
.typed {
  --tint: hsl(var(--h) 80% 94%);
  --ink: hsl(var(--h) 60% 33%);
}
[data-mode="dark"] {
  --bg: #12111a;
  --card: #1c1a28;
  --text: #ecebf5;
  --muted: #a8a3c0;
  --line: #2e2b40;
  --brand: #9a93f8;
  --brand-soft: #262345;
  --on-brand: #12111a;
  --done: #4ade80;
  --paper: #1f1d2b;
  --shadow: none;
}
[data-mode="dark"] .typed {
  --tint: hsl(var(--h) 38% 21%);
  --ink: hsl(var(--h) 80% 82%);
}

/* Colour themes: each sets the top bar, the brand colour and its soft tint. */
[data-accent="ocean"] { --bar: #0369a1; --brand: #0369a1; --brand-soft: #e0f0fa; --bg: #f3f7fa; }
[data-accent="forest"] { --bar: #166534; --brand: #166534; --brand-soft: #e2f3e7; --bg: #f4f8f4; }
[data-accent="berry"] { --bar: #a21a5b; --brand: #a21a5b; --brand-soft: #fbe6ef; --bg: #faf4f7; }
[data-accent="ocean"][data-mode="dark"] { --brand: #5cc0f5; --brand-soft: #14303f; --bg: #0f1418; }
[data-accent="forest"][data-mode="dark"] { --brand: #6dd695; --brand-soft: #173323; --bg: #0f1511; }
[data-accent="berry"][data-mode="dark"] { --brand: #f58bbd; --brand-soft: #3d1a2b; --bg: #171014; }

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}
button, input { font: inherit; color: inherit; }
button { cursor: pointer; }
h2, h3, h4, p { margin: 0; }

/* ---------- Top bar and timer ---------- */

.bar {
  position: sticky; top: 0; z-index: 10;
  display: flex; flex-wrap: wrap; align-items: center; gap: 8px;
  padding: calc(8px + env(safe-area-inset-top)) max(12px, env(safe-area-inset-right)) 8px max(12px, env(safe-area-inset-left));
  background: var(--bar); color: #fff;
  box-shadow: 0 2px 10px rgba(30, 27, 46, .18);
}
.bar h1 {
  flex: 1; min-width: 0; margin: 0; font-size: 1.1rem; letter-spacing: -.01em;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.bar { gap: 6px; }
.icon-btn, .bar-pill {
  height: 40px; flex: none; padding: 0;
  display: grid; place-items: center;
  border: 0; border-radius: 999px;
  background: rgba(255, 255, 255, .14); color: #fff; text-decoration: none;
}
.icon-btn { width: 40px; font-size: 1.15rem; }
.bar-pill { padding: 0 11px; font-size: .9rem; font-weight: 700; white-space: nowrap; }
.icon-btn:active, .bar-pill:active { background: rgba(255, 255, 255, .28); }
[hidden] { display: none !important; }

.timer {
  flex-basis: 100%;
  display: grid; grid-template-columns: 1fr auto; align-items: center; gap: 6px 10px;
  padding: 8px 10px; border-radius: 12px;
  background: rgba(255, 255, 255, .14);
}
.timer.over { background: #b91c1c; }
.timer-info {
  display: flex; align-items: baseline; gap: 10px; min-width: 0;
  color: #fff; text-decoration: none;
}
.timer-clock { font-size: 1.35rem; font-weight: 700; font-variant-numeric: tabular-nums; order: -1; }
.timer-phase { font-size: .85rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.timer-stop {
  min-height: 36px; padding: 4px 14px;
  border: 1px solid rgba(255, 255, 255, .6); border-radius: 999px;
  background: transparent; color: #fff; font-weight: 600; font-size: .85rem;
}
.timer-track {
  grid-column: 1 / -1; height: 4px; border-radius: 4px;
  background: rgba(255, 255, 255, .25); overflow: hidden;
}
.timer-track span { display: block; height: 100%; background: #fff; transition: width .5s linear; }

/* ---------- Layout ---------- */

main {
  max-width: 820px; margin: 0 auto;
  padding: 16px 16px calc(40px + env(safe-area-inset-bottom));
  outline: none;
}
main > * { animation: rise .22s ease-out; }
@keyframes rise { from { opacity: 0; transform: translateY(6px); } }
@media (prefers-reduced-motion: reduce) {
  main > * { animation: none; }
  .timer-track span { transition: none; }
}

/* ---------- Home ---------- */

.stats { display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; gap: 8px; margin-top: 12px; }
.stats div { padding: 8px 4px; border-radius: 12px; background: var(--bg); text-align: center; }
.stats b { display: block; font-size: 1.1rem; }
.stats small { color: var(--muted); font-size: .76rem; }

.chips {
  display: flex; gap: 8px; overflow-x: auto;
  margin: 0 -16px; padding: 2px 16px;
  scrollbar-width: none;
}
.chips::-webkit-scrollbar { display: none; }
.chip {
  flex: none; min-height: 40px; padding: 6px 14px;
  border: 1px solid var(--line); border-radius: 999px;
  background: var(--card); color: var(--muted); white-space: nowrap;
}
.chip[aria-pressed="true"] {
  background: var(--brand); border-color: var(--brand); color: var(--on-brand); font-weight: 600;
}
@media (min-width: 700px) {
  .chips { flex-wrap: wrap; overflow: visible; margin: 0; padding: 0; }
}

.chips.topics { margin-top: 8px; }
.chips.topics .chip { min-height: 36px; padding: 4px 12px; font-size: .88rem; }
@media (min-width: 700px) { .chips.topics { margin-top: 8px; } }

.count-line { margin: 14px 2px 8px; font-size: .82rem; color: var(--muted); }

.list { display: grid; gap: 10px; list-style: none; margin: 0; padding: 0; }
@media (min-width: 700px) { .list { grid-template-columns: 1fr 1fr; gap: 14px; } }
.empty {
  grid-column: 1 / -1; padding: 28px 16px; text-align: center; color: var(--muted);
  border: 1px dashed var(--line); border-radius: 16px;
}

.card {
  display: flex; align-items: center; gap: 12px; height: 100%; padding: 14px;
  background: var(--card); border: 1px solid var(--line); border-radius: 16px;
  box-shadow: var(--shadow);
  color: inherit; text-decoration: none;
  transition: transform .12s ease, border-color .12s ease;
}
.card:active { transform: scale(.985); }
@media (hover: hover) { .card:hover { border-color: var(--ink); } }
.badge {
  flex: none; width: 46px; height: 46px; border-radius: 14px;
  display: grid; place-items: center;
  background: var(--tint); font-size: 1.4rem;
}
.card-body { flex: 1; min-width: 0; }
.card h2 { margin: 4px 0 2px; font-size: 1.04rem; letter-spacing: -.01em; }
.card p {
  color: var(--muted); font-size: .9rem; line-height: 1.4;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
}
.status { flex: none; width: 28px; text-align: center; color: var(--muted); font-size: 1.5rem; }
.stars { color: #f59e0b; letter-spacing: 1px; }
.status.stars { width: auto; font-size: .95rem; }
.tick.stars { color: #f59e0b; font-size: .95rem; }

.meta { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; font-size: .8rem; color: var(--muted); }
.tag {
  padding: 2px 9px; border-radius: 999px;
  background: var(--tint); color: var(--ink); font-weight: 600;
}
.theme::before { content: "#"; opacity: .6; }
.tick { margin-left: auto; color: var(--done); font-weight: 600; }

/* ---------- Scenario ---------- */

.prompt-box {
  margin: 12px 0 16px; padding: 16px;
  background: var(--tint); border-radius: 16px;
  font-size: 1.1rem; line-height: 1.45; font-weight: 500;
}
.label {
  display: block; margin-bottom: 4px;
  font-size: .72rem; font-weight: 700; letter-spacing: .08em; text-transform: uppercase; color: var(--ink);
}

.tabs {
  display: grid; grid-template-columns: repeat(3, 1fr); gap: 4px;
  padding: 4px; margin-bottom: 16px;
  background: var(--card); border: 1px solid var(--line); border-radius: 14px;
}
.tab {
  min-height: 44px; border: 0; border-radius: 10px;
  background: transparent; color: var(--muted); font-weight: 600;
}
.tab[aria-selected="true"] { background: var(--brand); color: var(--on-brand); }

.box {
  padding: 14px 16px; margin-bottom: 12px;
  background: var(--card); border: 1px solid var(--line); border-radius: 14px;
}
.box h3 { margin-bottom: 8px; font-size: 1rem; }
.checks { list-style: none; margin: 0; padding: 0; }
.checks li { position: relative; margin: 8px 0; padding-left: 28px; }
.checks li::before {
  content: "\2713"; position: absolute; left: 0; top: 2px;
  width: 20px; height: 20px; border-radius: 50%;
  background: var(--tint); color: var(--ink);
  font-size: .75rem; font-weight: 700; text-align: center; line-height: 20px;
}

.note { color: var(--muted); font-size: .92rem; margin-bottom: 10px; }
.box .note:last-child { margin-bottom: 0; }

.steps { list-style: none; margin: 0; padding: 0; counter-reset: step; }
.steps > li {
  counter-increment: step; position: relative;
  margin: 0 0 10px; padding: 12px 14px 12px 52px;
  background: var(--card); border: 1px solid var(--line); border-radius: 14px;
}
.steps > li::before {
  content: counter(step);
  position: absolute; left: 14px; top: 12px;
  width: 26px; height: 26px; border-radius: 50%;
  background: var(--ink); color: var(--card);
  font-weight: 700; font-size: .85rem; text-align: center; line-height: 26px;
}
.steps strong { display: block; margin-bottom: 2px; }
.steps ul { margin: 0; padding-left: 18px; color: var(--muted); }
.steps ul li { margin: 3px 0; }

.paper {
  padding: 20px 18px 8px;
  background: var(--paper); border: 1px solid var(--line); border-radius: 14px;
  box-shadow: var(--shadow);
  font-family: Georgia, "Iowan Old Style", "Times New Roman", serif;
  font-size: 1.07rem; line-height: 1.75;
}
.paper p { margin: 0 0 12px; }
.paper h4 { margin: 16px 0 6px; font-size: 1.08rem; line-height: 1.3; }
.paper h4:first-child { margin-top: 0; font-size: 1.3rem; }
.words { margin: 8px 2px 14px; font-size: .8rem; color: var(--muted); text-align: right; }

.timer-start, .next-btn, .done-btn {
  display: block; width: 100%; min-height: 50px; padding: 10px 16px;
  border-radius: 14px; font-weight: 600;
}
.timer-start { border: 1px solid var(--line); background: var(--bg); }
.next-btn { margin-top: 4px; border: 0; background: var(--brand); color: var(--on-brand); }
.done-btn { margin-top: 12px; border: 1px solid var(--brand); background: transparent; color: var(--brand); }
.done-btn { border: 0; background: linear-gradient(90deg, #f59e0b, #f97316); color: #1e1b2e; font-size: 1.05rem; }
.done-btn.is-done { background: transparent; border: 1px solid var(--line); color: var(--text); font-size: 1rem; }
.timer-start:active, .next-btn:active, .done-btn:active { transform: scale(.985); }

.pager {
  display: grid; grid-template-columns: 1fr 1fr; gap: 10px;
  margin-top: 24px; padding-top: 16px; border-top: 1px solid var(--line);
}
.pager a {
  min-width: 0; padding: 10px 12px; border-radius: 12px;
  color: var(--text); text-decoration: none; font-weight: 600; font-size: .92rem;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.pager a:active { background: var(--brand-soft); }
.pager small { display: block; color: var(--muted); font-weight: 400; }
.pager .right { text-align: right; }

/* ---------- Colour theme sheet ---------- */

.sheet {
  width: 100%; max-width: 460px; margin: auto auto 0; padding: 0;
  border: 0; border-radius: 20px 20px 0 0;
  background: var(--card); color: var(--text);
}
.sheet::backdrop { background: rgba(10, 8, 20, .5); }
.sheet-body { padding: 20px 16px calc(16px + env(safe-area-inset-bottom)); }
.sheet h2 { font-size: 1.15rem; }
.sheet h3 { margin: 16px 0 8px; font-size: .78rem; letter-spacing: .08em; text-transform: uppercase; color: var(--muted); }
.sheet .next-btn { margin-top: 20px; }
@media (min-width: 700px) { .sheet { margin: auto; border-radius: 20px; } }

.seg {
  display: grid; grid-template-columns: repeat(3, 1fr); gap: 4px; padding: 4px;
  background: var(--bg); border: 1px solid var(--line); border-radius: 14px;
}
.seg button {
  min-height: 44px; border: 0; border-radius: 10px;
  background: transparent; color: var(--muted); font-weight: 600;
}
.seg button[aria-pressed="true"] { background: var(--brand); color: var(--on-brand); }

.swatches { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.swatches button {
  display: grid; justify-items: center; gap: 6px; padding: 10px 4px;
  border: 2px solid transparent; border-radius: 14px;
  background: var(--bg); font-size: .85rem;
}
.swatches button[aria-pressed="true"] { border-color: var(--brand); font-weight: 600; }
.swatches i { width: 34px; height: 34px; border-radius: 50%; background: var(--sw); }

/* ---------- Badges, spinner and celebrations ---------- */

.title-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin: 2px 2px 12px; }
.section-title { font-size: 1.2rem; }
.surprise {
  flex: none; min-height: 40px; padding: 6px 14px;
  border: 0; border-radius: 999px; font-weight: 600;
  background: var(--brand-soft); color: var(--brand);
}
.page-title { margin: 8px 0 0; font-size: 1.35rem; letter-spacing: -.01em; line-height: 1.25; }

.medal-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-bottom: 4px; }
.medal-grid .medal { width: auto; }
.levels { list-style: none; margin: 14px 0; padding: 0; }
.levels li {
  display: flex; justify-content: space-between; align-items: center; gap: 12px;
  padding: 8px 12px; border-radius: 10px; color: var(--muted);
}
.levels li.past { color: var(--text); }
.levels li.now { background: var(--brand-soft); color: var(--text); font-weight: 700; }
.levels small { font-weight: 400; font-size: .8rem; }
.medal-row.centre { justify-content: center; flex-wrap: wrap; margin: 0 0 8px; padding: 0; overflow: visible; }
.medal {
  flex: none; width: 104px; padding: 10px 6px; text-align: center;
  background: var(--card); border: 1px solid #f59e0b; border-radius: 14px;
}
.medal span { display: block; font-size: 1.7rem; line-height: 1.2; }
.medal strong { display: block; font-size: .8rem; line-height: 1.2; margin: 4px 0 2px; }
.medal small { display: block; font-size: .7rem; line-height: 1.25; color: var(--muted); }
.medal.locked { border: 1px dashed var(--line); background: transparent; }
.medal.locked span { opacity: .45; }
.medal.locked strong { color: var(--muted); }


.reels { display: grid; gap: 10px; margin: 14px 0; }
@media (min-width: 700px) { .reels { grid-template-columns: repeat(3, 1fr); } }
.reel {
  padding: 12px 14px; min-height: 84px;
  background: var(--card); border: 2px solid var(--line); border-radius: 16px;
}
.reel small { display: block; color: var(--muted); font-size: .78rem; font-weight: 600; margin-bottom: 2px; }
.reel span { font-size: 1.1rem; font-weight: 600; }
.reel.rolling { border-color: #db2777; }
.reel.rolling span { opacity: .55; }
.spin-btn { margin-bottom: 14px; min-height: 56px; font-size: 1.1rem; background: linear-gradient(120deg, #7c3aed, #db2777); color: #fff; }

.sheet { overflow-x: hidden; max-height: 88vh; overflow-y: auto; }
.rate-mascot { font-size: 3rem; text-align: center; line-height: 1.1; }
#rate-body h2 { text-align: center; margin-bottom: 6px; }
.center { text-align: center; }
.tick-row {
  display: flex; align-items: flex-start; gap: 12px; padding: 12px; margin: 8px 0;
  background: var(--bg); border: 1px solid var(--line); border-radius: 14px;
}
.tick-row input { flex: none; width: 24px; height: 24px; margin: 0; accent-color: #f59e0b; }
.tick-row:has(input:checked) { border-color: #f59e0b; }
.link-btn {
  display: block; width: 100%; min-height: 44px; margin-top: 4px;
  border: 0; background: transparent; color: var(--muted); text-decoration: underline;
}
.big-stars {
  text-align: center; font-size: 3.4rem; line-height: 1.2; color: #f59e0b; letter-spacing: 4px;
  animation: pop .5s cubic-bezier(.2, 1.6, .4, 1);
}
@keyframes pop { from { transform: scale(.2); opacity: 0; } }
.level-up {
  margin: 10px 0; padding: 10px; border-radius: 12px; text-align: center; font-weight: 700;
  background: linear-gradient(120deg, #7c3aed, #db2777); color: #fff;
}

.confetti { position: fixed; inset: 0; pointer-events: none; overflow: hidden; }
.confetti span { position: absolute; top: -40px; animation: fall 2.2s ease-in forwards; }
@keyframes fall { to { transform: translateY(110vh) rotate(540deg); } }

@media (prefers-reduced-motion: reduce) {
  .big-stars { animation: none; }
}

:focus-visible { outline: 3px solid #f59e0b; outline-offset: 2px; }
