:root {
  --bg: #0e0f13;
  --bg-2: #16181f;
  --panel: #1c1f28;
  --ink: #f4f5f7;
  --ink-dim: #9aa0ad;
  --ink-faint: #5b6270;
  --accent: #ff5a3c;
  --accent-2: #37e0a6;
  --good: #37e0a6;
  --warn: #ffcf5a;
  --line: #262a35;
  --radius: 18px;
  --touch: 44px;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --stage: min(92vw, 60vh, 520px);
}

@media (prefers-color-scheme: light) {
  :root {
    --bg: #f6f7f9; --bg-2: #eceef2; --panel: #ffffff;
    --ink: #14161c; --ink-dim: #5b6270; --ink-faint: #9aa0ad;
    --line: #e2e5ea;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--ink);
  font-family: "Segoe UI", system-ui, -apple-system, Roboto, Helvetica, Arial, sans-serif;
  overscroll-behavior: none;
  -webkit-user-select: none; user-select: none;
  touch-action: manipulation;
}
button { font-family: inherit; cursor: pointer; }

#app { position: relative; height: 100dvh; width: 100%; overflow: hidden; }

.screen {
  position: absolute; inset: 0;
  display: none;
  flex-direction: column;
  padding: calc(12px + var(--safe-t)) 20px calc(16px + var(--safe-b));
  animation: fade .22s ease;
}
.screen.is-active { display: flex; }
@keyframes fade { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }

/* ---- top / bottom bars ---- */
.topbar { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.brand { font-weight: 800; letter-spacing: .18em; font-size: 15px; }
.icon-btn {
  min-width: var(--touch); min-height: var(--touch);
  display: grid; place-items: center;
  background: transparent; border: none; color: var(--ink);
  font-size: 22px; border-radius: 12px;
}
.icon-btn:active { background: var(--bg-2); }
.icon-btn.ghost { opacity: 0; pointer-events: none; }
.bottombar { display: flex; align-items: center; justify-content: space-between; color: var(--ink-dim); font-size: 13px; }

/* ---- home ---- */
.home-main { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center; gap: 4px; }
.day-label { color: var(--ink-dim); letter-spacing: .22em; text-transform: uppercase; font-size: 12px; margin: 0 0 8px; }
.glyph-badge {
  width: 92px; height: 92px; border-radius: 26px;
  display: grid; place-items: center;
  font-size: 44px; color: var(--accent);
  background: radial-gradient(120% 120% at 30% 20%, var(--panel), var(--bg-2));
  border: 1px solid var(--line);
  box-shadow: 0 18px 44px rgba(0,0,0,.35);
}
.glyph-badge.small { width: 60px; height: 60px; border-radius: 18px; font-size: 28px; }
.task-name { font-size: clamp(30px, 9vw, 46px); letter-spacing: .04em; margin: 14px 0 2px; font-weight: 800; }
.task-primitive { color: var(--ink-dim); font-size: 13px; margin: 0 0 10px; letter-spacing: .04em; }
.demo-frame {
  width: min(78vw, 320px); height: min(38vh, 210px);
  background: var(--bg-2); border: 1px solid var(--line); border-radius: var(--radius);
  overflow: hidden; position: relative; margin-top: 6px;
}
.demo-caption { color: var(--ink-faint); font-size: 12px; margin: 8px 0 0; min-height: 15px; }
.home-actions { margin-top: 22px; display: flex; flex-direction: column; align-items: center; gap: 10px; width: 100%; }
.one-shot-warn { color: var(--accent); font-size: 12px; letter-spacing: .06em; margin: 0; text-transform: uppercase; }

.primary-btn {
  min-height: 54px; padding: 0 40px;
  border: none; border-radius: 999px;
  background: var(--accent); color: #fff;
  font-size: 18px; font-weight: 700; letter-spacing: .04em;
  box-shadow: 0 12px 30px rgba(255,90,60,.35);
  transition: transform .08s ease, filter .12s ease;
}
.primary-btn:active { transform: translateY(1px) scale(.99); filter: brightness(1.05); }
.primary-btn[disabled] { background: var(--ink-faint); box-shadow: none; opacity: .6; }
.link-btn { background: none; border: none; color: var(--ink-dim); font-size: 14px; text-decoration: underline; text-underline-offset: 3px; padding: 10px; }
.link-btn:active { color: var(--ink); }

/* ---- countdown ---- */
.countdown-screen { align-items: center; justify-content: center; text-align: center; }
.count-num { font-size: 34vw; font-weight: 800; line-height: 1; color: var(--ink); }
.count-num.go { color: var(--accent-2); font-size: 22vw; }
.count-sub { color: var(--ink-dim); letter-spacing: .3em; text-transform: uppercase; font-size: 13px; }

/* ---- play ---- */
.play-screen { align-items: center; justify-content: center; }
.stage-wrap { flex: 1; display: grid; place-items: center; width: 100%; }
.stage {
  width: var(--stage); height: var(--stage);
  background: var(--bg-2); border: 1px solid var(--line);
  border-radius: var(--radius); position: relative; overflow: hidden;
  touch-action: none; outline: none;
}
.stage:focus-visible { border-color: var(--accent); }
.play-hint { color: var(--ink-faint); font-size: 13px; text-align: center; min-height: 18px; }

/* ---- result ---- */
.result-main { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center; gap: 6px; }
.result-eyebrow { color: var(--ink-dim); letter-spacing: .2em; text-transform: uppercase; font-size: 12px; margin: 0; }
.score-big { font-size: clamp(64px, 22vw, 120px); font-weight: 800; line-height: 1; margin-top: 6px; }
.score-label { color: var(--ink-dim); font-size: 12px; letter-spacing: .2em; text-transform: uppercase; margin: 0; }
.percentile-pill {
  margin-top: 14px; padding: 8px 20px; border-radius: 999px;
  background: rgba(55,224,166,.14); color: var(--good);
  font-weight: 700; font-size: 18px; letter-spacing: .04em;
}
.result-meta { display: flex; gap: 8px; margin-top: 22px; width: 100%; max-width: 380px; }
.meta-cell { flex: 1; background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 12px 8px; }
.meta-k { display: block; color: var(--ink-faint); font-size: 11px; text-transform: uppercase; letter-spacing: .08em; }
.meta-v { display: block; font-size: 20px; font-weight: 700; margin-top: 4px; }
.result-actions { margin-top: 26px; display: flex; flex-direction: column; align-items: center; gap: 6px; }
.countdown-next { color: var(--ink-faint); font-size: 12px; margin-top: 18px; letter-spacing: .06em; }

/* ---- stats ---- */
.stats-main, .archive-main { flex: 1; overflow-y: auto; padding-top: 10px; -webkit-overflow-scrolling: touch; }
.stat-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.stat-tile { background: var(--panel); border: 1px solid var(--line); border-radius: 16px; padding: 18px 14px; text-align: center; }
.stat-num { font-size: 32px; font-weight: 800; }
.stat-k { color: var(--ink-dim); font-size: 12px; letter-spacing: .08em; text-transform: uppercase; margin-top: 4px; }
.section-h { color: var(--ink-dim); font-size: 12px; letter-spacing: .12em; text-transform: uppercase; margin: 24px 0 10px; }
.spark { display: flex; align-items: flex-end; gap: 6px; height: 90px; padding: 8px; background: var(--panel); border: 1px solid var(--line); border-radius: 16px; }
.spark .bar { flex: 1; background: linear-gradient(var(--accent-2), rgba(55,224,166,.25)); border-radius: 5px 5px 2px 2px; min-height: 3px; position: relative; }
.spark .bar.empty { background: var(--bg-2); }
.breakdown { display: flex; flex-direction: column; gap: 8px; }
.bd-row { display: flex; align-items: center; gap: 10px; font-size: 13px; }
.bd-glyph { width: 26px; color: var(--accent); text-align: center; }
.bd-name { width: 84px; color: var(--ink-dim); }
.bd-track { flex: 1; height: 8px; background: var(--bg-2); border-radius: 6px; overflow: hidden; }
.bd-fill { height: 100%; background: var(--accent); border-radius: 6px; }
.bd-val { width: 40px; text-align: right; font-weight: 700; }
.data-actions { display: flex; justify-content: center; gap: 8px; margin: 26px 0 10px; }

/* ---- archive ---- */
.archive-note { color: var(--ink-dim); font-size: 13px; text-align: center; margin: 6px 0 16px; }
.archive-list { display: flex; flex-direction: column; gap: 8px; }
.archive-row {
  display: flex; align-items: center; gap: 12px;
  background: var(--panel); border: 1px solid var(--line); border-radius: 14px;
  padding: 12px 14px; width: 100%; text-align: left; color: var(--ink);
}
.archive-row:active { background: var(--bg-2); }
.ar-day { width: 54px; color: var(--ink-faint); font-size: 12px; }
.ar-glyph { width: 26px; color: var(--accent); text-align: center; font-size: 18px; }
.ar-name { flex: 1; font-weight: 600; }
.ar-score { color: var(--ink-dim); font-size: 13px; }
.ar-badge { font-size: 11px; padding: 2px 8px; border-radius: 999px; background: var(--bg-2); color: var(--ink-faint); }
.ar-badge.today { background: rgba(255,90,60,.16); color: var(--accent); }

/* ---- toast ---- */
.toast {
  position: fixed; left: 50%; bottom: calc(24px + var(--safe-b)); transform: translateX(-50%) translateY(20px);
  background: var(--ink); color: var(--bg); padding: 12px 18px; border-radius: 999px;
  font-size: 14px; font-weight: 600; opacity: 0; pointer-events: none; transition: all .24s ease; z-index: 50; max-width: 86vw; text-align: center;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ---- level bar / progression ---- */
.level-strip {
  display: flex; align-items: center; gap: 10px; width: 100%;
  background: var(--panel); border: 1px solid var(--line); border-radius: 999px;
  padding: 6px 8px 6px 6px; margin-top: 8px; color: var(--ink);
}
.level-strip:active { background: var(--bg-2); }
.lvl-badge {
  min-width: 30px; height: 30px; padding: 0 8px; border-radius: 999px;
  display: grid; place-items: center; font-weight: 800; font-size: 14px;
  background: var(--accent); color: #fff;
}
.lvl-badge.big { min-width: 46px; height: 46px; font-size: 20px; }
.lvl-track { flex: 1; height: 8px; background: var(--bg-2); border-radius: 6px; overflow: hidden; }
.lvl-fill { display: block; height: 100%; width: 0%; background: linear-gradient(90deg, var(--accent-2), var(--accent)); border-radius: 6px; }
.lvl-xp { color: var(--ink-dim); font-size: 12px; font-weight: 600; padding-right: 6px; white-space: nowrap; }

.ghost-btn {
  background: transparent; border: 1px solid var(--line); color: var(--ink);
  min-height: 46px; padding: 0 28px; border-radius: 999px; font-size: 15px; font-weight: 600;
}
.ghost-btn:active { background: var(--bg-2); }
.secondary-btn {
  background: var(--panel); border: 1px solid var(--line); color: var(--ink);
  min-height: 50px; padding: 0 34px; border-radius: 999px; font-size: 16px; font-weight: 700;
}
.secondary-btn:active { background: var(--bg-2); }

/* ---- free play ---- */
.freeplay-main { flex: 1; overflow-y: auto; padding-top: 10px; }
.tier-seg { display: flex; gap: 6px; background: var(--bg-2); border-radius: 12px; padding: 4px; }
.tier-seg button {
  flex: 1; min-height: 40px; border: none; border-radius: 9px; background: transparent;
  color: var(--ink-dim); font-size: 14px; font-weight: 700; letter-spacing: .02em;
}
.tier-seg button.active { background: var(--accent); color: #fff; }
.freeplay-note { color: var(--ink-faint); font-size: 12px; text-align: center; margin: 12px 0 14px; }
.variant-grid { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 10px; padding-bottom: 20px; }
.variant-card {
  background: var(--panel); border: 1px solid var(--line); border-radius: 16px;
  padding: 14px 6px 10px; display: flex; flex-direction: column; align-items: center; gap: 4px; color: var(--ink);
}
.variant-card:active { background: var(--bg-2); }
.vc-glyph { font-size: 30px; color: var(--accent); line-height: 1; }
.vc-name { font-size: 11px; font-weight: 700; letter-spacing: .04em; }
.vc-pb { font-size: 11px; color: var(--ink-faint); }
.vc-pb b { color: var(--accent-2); }

/* ---- how to play ---- */
.howto-main { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center; gap: 6px; }
.howto-tier { color: var(--accent); font-size: 12px; letter-spacing: .18em; text-transform: uppercase; margin: 2px 0 10px; }
.howto-text { color: var(--ink-dim); font-size: 15px; line-height: 1.5; max-width: 34ch; margin: 14px 0 22px; }

/* ---- result additions ---- */
.newbest-banner {
  margin-top: 12px; color: var(--warn); font-weight: 800; letter-spacing: .08em;
  text-transform: uppercase; font-size: 13px;
}
.xp-block { margin-top: 18px; width: 100%; max-width: 340px; }
.xp-gain { color: var(--accent-2); font-weight: 800; font-size: 15px; }
.xp-row { display: flex; align-items: center; gap: 10px; margin-top: 8px; }

/* ---- stats level hero ---- */
.level-hero { display: flex; align-items: center; gap: 14px; background: var(--panel); border: 1px solid var(--line); border-radius: 16px; padding: 16px; margin-bottom: 12px; }
.level-hero-bar { flex: 1; }
.level-hero-top { display: flex; justify-content: space-between; color: var(--ink-dim); font-size: 12px; font-weight: 600; margin-bottom: 8px; }

@media (prefers-reduced-motion: reduce) {
  .screen, .toast, .primary-btn { animation: none !important; transition: none !important; }
}
