:root {
  --bg: #0e1014;
  --panel: #161a21;
  --line: #252b35;
  --text: #e8eaef;
  --dim: #565e6c;
  --muted: #8a93a3;
  --you: #f5c542;
  --ghost: #a78bfa;
  --ghost-wash: rgba(167, 139, 250, 0.16);
  --sheet: #efeaff;
  --sheet-eye: #1b1530;
  --ice: 150, 200, 255;
  --err: #f0616d;
  --err-wash: rgba(240, 97, 109, 0.18);
  --clean: #4ade80;
  --mono: ui-monospace, 'SF Mono', SFMono-Regular, Menlo, Consolas, 'Liberation Mono', monospace;
  --sans: system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  color-scheme: dark;
}

@media (prefers-color-scheme: light) {
  :root {
    --bg: #f6f5f1;
    --panel: #ffffff;
    --line: #e2e0d8;
    --text: #1d2026;
    --dim: #a3a7b0;
    --muted: #666c78;
    --you: #c98a00;
    --ghost: #7c5cdb;
    --ghost-wash: rgba(124, 92, 219, 0.14);
    --sheet: #ffffff;
    --sheet-eye: #241a45;
    --ice: 70, 140, 230;
    --err: #d63848;
    --err-wash: rgba(214, 56, 72, 0.14);
    --clean: #17994a;
    color-scheme: light;
  }
}

* { box-sizing: border-box; }

html, body { margin: 0; }

body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--sans);
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
}

#bg {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  z-index: 0;
  pointer-events: none;
}
header, main { position: relative; z-index: 1; }

header {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 14px 20px;
  font-family: var(--mono);
  font-size: 0.8rem;
  letter-spacing: 0.08em;
}

.back {
  color: var(--muted);
  text-decoration: none;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 3px 8px;
}
.back:hover { color: var(--text); }
.brand { color: var(--ghost); font-weight: 700; }

button {
  font: inherit;
  color: inherit;
  cursor: pointer;
}

.np {
  margin-left: auto;
  color: var(--muted);
  font-size: 0.72rem;
  letter-spacing: 0.04em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  min-width: 0;
}
#sound {
  background: none;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 3px 10px;
  color: var(--muted);
  font-family: var(--mono);
  font-size: 0.75rem;
}
#sound[aria-pressed='true'] { color: var(--text); }

main {
  flex: 1;
  width: 100%;
  max-width: 960px;
  margin: 0 auto;
  padding: 16px 20px 40px;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

/* One screen at a time, chosen by body[data-screen]. */
#menu, #play, #over { display: none; }
body[data-screen='menu'] #menu,
body[data-screen='over'] #over { display: block; }
body[data-screen='play'] #play { display: block; }

.panel {
  max-width: 620px;
  width: 100%;
  margin: 0 auto;
}

h1 {
  font-family: var(--mono);
  font-size: clamp(2rem, 6vw, 3rem);
  margin: 0 0 8px;
  letter-spacing: -0.02em;
}
#menu h1 { color: var(--ghost); }

.tagline { color: var(--muted); font-size: 1.15rem; margin: 0 0 24px; }

.rules {
  list-style: none;
  padding: 0;
  margin: 0 0 24px;
  display: grid;
  gap: 10px;
  line-height: 1.5;
}
.rules li { padding-left: 20px; position: relative; }
.rules li::before { content: '›'; position: absolute; left: 4px; color: var(--ghost); }

.status { color: var(--muted); font-family: var(--mono); font-size: 0.9rem; min-height: 1.4em; }
.status b { color: var(--text); }

.actions { display: flex; align-items: center; gap: 18px; margin-top: 24px; flex-wrap: wrap; }

.primary {
  background: var(--ghost);
  color: var(--bg);
  border: 0;
  border-radius: 8px;
  padding: 12px 22px;
  font-weight: 700;
  font-size: 1rem;
}
.primary kbd { background: rgba(0, 0, 0, 0.18); color: inherit; border: 0; }

.link {
  background: none;
  border: 0;
  color: var(--muted);
  text-decoration: underline;
  text-underline-offset: 3px;
  padding: 4px;
}
.link:hover { color: var(--text); }

kbd {
  font-family: var(--mono);
  font-size: 0.75em;
  border: 1px solid var(--line);
  border-radius: 4px;
  padding: 1px 5px;
  margin-left: 4px;
}

/* ---- Play ---- */

.hud {
  display: flex;
  gap: 12px 28px;
  flex-wrap: wrap;
  align-items: flex-end;
  font-family: var(--mono);
  margin-bottom: 28px;
}
.stat { display: flex; flex-direction: column; gap: 2px; }
.stat b { font-size: 1.35rem; font-variant-numeric: tabular-nums; }
.stat.pace b { color: var(--ghost); font-size: 1.9rem; }
.stat.pace span:last-child { color: var(--muted); }
.stat.score { margin-left: auto; text-align: right; }
.label { font-size: 0.7rem; letter-spacing: 0.12em; text-transform: uppercase; color: var(--muted); }
.hearts { color: var(--err); letter-spacing: 0.15em; }
.streak { color: var(--muted); font-size: 0.85rem; font-variant-numeric: tabular-nums; }
#hudMult.hot { color: var(--clean); }
.stat.pace b.angry { color: var(--err); }
.bump { animation: bump 0.35s ease-out; }
@keyframes bump { 40% { transform: scale(1.25); } }

#stage {
  position: relative;
  outline: none;
  cursor: text;
}

#lineWrap { position: relative; }

#line {
  font-family: var(--mono);
  font-size: clamp(1.25rem, 3.2vw, 1.9rem);
  line-height: 2.3;
  white-space: pre-wrap;
  overflow-wrap: break-word;
  color: var(--dim);
  min-height: 4.6em;
}
#line span { position: relative; border-radius: 2px; }
#line .ok { color: var(--text); }
#line .err { color: var(--err); background: var(--err-wash); }
#line .behind { background: var(--ghost-wash); }
#line .cur::before {
  content: '';
  position: absolute;
  left: -1px;
  top: 0.18em;
  bottom: 0.18em;
  width: 2px;
  background: var(--you);
  border-radius: 1px;
}
#line .cur.wrong::before { background: var(--err); }
/* The half-second shield after a miss: a wider, glowing caret. */
#line.shield .cur::before {
  width: 3px;
  background: var(--clean);
  box-shadow: 0 0 10px var(--clean), 0 0 2px var(--clean);
}
#line.enter { animation: enter 0.14s ease-out; }
@keyframes enter { from { opacity: 0; transform: translateY(5px); } }
#line.waiting .cur::before { animation: blink 1s steps(1) infinite; }
@keyframes blink { 50% { opacity: 0; } }

/* The ghost: a sprite floating above the text at its position, with a thin
   marker down to the exact character. Position is set on #ghost by script;
   the bob, lunge and catch animations run on the inner .sprite. */
#ghost {
  position: absolute;
  left: 0;
  top: 0;
  width: 0;
  height: var(--row, 3rem);
  pointer-events: none;
  will-change: transform;
  transition: opacity 0.2s;
}
#ghost .mark {
  position: absolute;
  left: -1px;
  top: 22%;
  bottom: 22%;
  width: 2px;
  border-radius: 1px;
  background: var(--ghost);
  box-shadow: 0 0 10px var(--ghost);
  opacity: 0.85;
}
#ghost.asleep .mark { opacity: 0; }
/* Asleep, it waits in the margin so the first letter stays readable. */
#ghost.asleep .sprite { left: -44px; bottom: 22%; }
/* Held after a miss until the right key: it freezes mid-air. */
#ghost.held .sprite { animation-play-state: paused; }
#ghost.held .mark { opacity: 0.4; }
#ghost .sprite {
  position: absolute;
  left: -15px;
  bottom: 80%;
  width: 30px;
  height: 34px;
  animation: bob 1.6s ease-in-out infinite;
}
#ghost svg { width: 100%; height: 100%; overflow: visible; display: block; filter: drop-shadow(0 0 7px var(--ghost)); }
#ghost .body { fill: var(--sheet); }
#ghost .eyes-open { fill: var(--sheet-eye); }
#ghost .shine { fill: var(--sheet); }
#ghost .eyes-shut, #ghost .brows, #ghost .smile { fill: none; stroke: var(--sheet-eye); stroke-width: 2; stroke-linecap: round; }
#ghost .mouth { fill: var(--sheet-eye); }
#ghost .eyes-shut, #ghost .brows, #ghost .mouth, #ghost .zzz { display: none; }
#ghost.asleep .eyes-open, #ghost.asleep .smile { display: none; }
#ghost.asleep .eyes-shut, #ghost.asleep .zzz { display: block; }
#ghost.asleep .sprite { animation-duration: 3.2s; }
#ghost.near .brows, #ghost.near .mouth { display: block; }
#ghost.near .smile { display: none; }
#ghost.near svg { filter: drop-shadow(0 0 9px var(--err)); }
#ghost.near .body { fill: color-mix(in srgb, var(--sheet) 80%, var(--err)); }
#ghost.lunge .sprite { animation: lunge 0.32s ease-out, bob 1.6s ease-in-out 0.32s infinite; }
#ghost.boo .sprite { animation: boo 0.6s ease-out, bob 1.6s ease-in-out 0.6s infinite; }
#ghost .zzz {
  position: absolute;
  left: 28px;
  top: -8px;
  font-family: var(--mono);
  font-size: 0.8rem;
  color: var(--ghost);
  animation: zzz 2.4s ease-in-out infinite;
}
#ghost .zzz i { font-style: normal; font-size: 0.7em; margin-left: 2px; }
#ghost .gain {
  position: absolute;
  left: 14px;
  bottom: 110%;
  font-family: var(--mono);
  font-size: 0.75rem;
  font-weight: 700;
  color: var(--err);
  opacity: 0;
  white-space: nowrap;
}
#ghost .gain.pop { animation: gain 0.8s ease-out; }
@keyframes bob { 50% { transform: translateY(-4px); } }
@keyframes lunge {
  0% { transform: translateX(-10px) scale(1.15, 0.9); }
  60% { transform: translateX(3px) scale(0.95, 1.08); }
}
@keyframes boo {
  0% { transform: scale(1); }
  35% { transform: scale(1.7) translateY(-6px); }
  100% { transform: scale(1); }
}
@keyframes gain {
  0% { opacity: 0; transform: translateY(4px); }
  20% { opacity: 1; }
  100% { opacity: 0; transform: translateY(-12px); }
}
@keyframes zzz { 50% { transform: translate(3px, -4px); opacity: 0.5; } }

.shake { animation: shake 0.18s linear; }
@keyframes shake {
  25% { transform: translateX(-4px); }
  75% { transform: translateX(4px); }
}

.meta {
  margin-top: 18px;
  display: flex;
  gap: 8px 22px;
  flex-wrap: wrap;
  font-family: var(--mono);
  font-size: 0.8rem;
  color: var(--muted);
}
#focus b { color: var(--you); font-weight: 600; }
#tier .source { color: var(--text); }
#tier .source i { font-style: italic; }
#tier .story { color: var(--ghost); }

#verdict {
  font-family: var(--mono);
  font-size: 1.05rem;
  margin-top: 22px;
  min-height: 1.6em;
  opacity: 0;
}
#verdict.show { animation: verdict 1.6s ease-out forwards; }
@keyframes verdict {
  0% { opacity: 0; transform: translateY(4px) scale(0.98); }
  5% { opacity: 1; transform: none; }
  75% { opacity: 1; }
  100% { opacity: 0.35; }
}
#verdict.clean { color: var(--clean); }
#verdict.ok { color: var(--text); }
#verdict.caught { color: var(--err); }
#verdict.warm { color: var(--ghost); }

.blurred-msg {
  display: none;
  position: absolute;
  inset: -12px;
  align-items: center;
  justify-content: center;
  background: color-mix(in srgb, var(--bg) 82%, transparent);
  backdrop-filter: blur(3px);
  font-family: var(--mono);
  color: var(--muted);
  border-radius: 10px;
}
#stage.blurred .blurred-msg { display: flex; }

.hint { margin-top: 36px; font-size: 0.8rem; color: var(--muted); font-family: var(--mono); }

/* ---- Over ---- */

#overTitle.best { color: var(--clean); }
.big-score { font-family: var(--mono); color: var(--muted); margin: 0 0 20px; }
.big-score b { font-size: 2.2rem; color: var(--text); font-variant-numeric: tabular-nums; }

.summary {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(130px, 1fr));
  gap: 12px;
  margin: 0 0 24px;
}
.summary div { background: var(--panel); border: 1px solid var(--line); border-radius: 8px; padding: 10px 12px; }
.summary dt { font-size: 0.7rem; letter-spacing: 0.12em; text-transform: uppercase; color: var(--muted); }
.summary dd { margin: 4px 0 0; font-family: var(--mono); font-size: 1.2rem; font-variant-numeric: tabular-nums; }

#chart {
  width: 100%;
  height: 140px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  display: block;
}
#chart .pace-line { fill: none; stroke: var(--ghost); stroke-width: 2; vector-effect: non-scaling-stroke; }
#chart .grid { stroke: var(--line); stroke-width: 1; vector-effect: non-scaling-stroke; }
#chart .clean { fill: var(--clean); }
#chart .ok { fill: var(--muted); }
#chart .caught { fill: var(--err); }
#chart text { fill: var(--muted); font-family: var(--mono); font-size: 11px; }

.weak { margin-top: 18px; font-size: 0.9rem; color: var(--muted); line-height: 2; }
.weak p { margin: 0 0 4px; line-height: 1.4; }
.weak .key {
  display: inline-block;
  font-family: var(--mono);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 0 8px;
  margin: 0 6px 0 0;
  color: var(--text);
}
.weak .key b { color: var(--you); margin-right: 6px; }

.runstats {
  display: flex;
  flex-wrap: wrap;
  gap: 6px 22px;
  margin-top: 14px;
  font-family: var(--mono);
  font-size: 0.78rem;
  color: var(--muted);
  min-height: 1.2em;
}
.runstats b { color: var(--text); font-weight: 600; font-variant-numeric: tabular-nums; }

/* ---- Frozen: held after a wrong key until the right one ---- */

body::after {
  content: '';
  position: fixed;
  inset: 0;
  z-index: 2;
  pointer-events: none;
  opacity: 0;
  background: radial-gradient(ellipse at 50% 45%, transparent 40%, rgba(var(--ice), 0.1) 70%, rgba(var(--ice), 0.28) 100%);
  box-shadow: inset 0 0 90px rgba(var(--ice), 0.35), inset 0 0 0 2px rgba(var(--ice), 0.25);
  transition: opacity 0.18s ease-out;
}
body.frozen::after { opacity: 1; transition-duration: 0.06s; }
#bg, .hud, .meta, #verdict, .hint, #line span { transition: opacity 0.15s, filter 0.15s; }
body.frozen #bg { filter: saturate(0.25) brightness(0.8); }
body.frozen .hud, body.frozen .meta, body.frozen #verdict, body.frozen .hint, body.frozen .runstats { opacity: 0.4; filter: saturate(0.3); }
body.frozen #line span:not(.cur) { opacity: 0.4; }
/* The one thing that matters while frozen: the letter you need. */
body.frozen #line .cur {
  color: var(--text);
  background: rgba(var(--ice), 0.2);
  box-shadow: 0 0 0 1px rgba(var(--ice), 0.7), 0 0 14px rgba(var(--ice), 0.45);
  animation: ice 0.7s ease-in-out infinite;
}
/* A space you need is invisible, so mark it while frozen. */
body.frozen #line .cur.sp::after {
  content: '\2423';
  position: absolute;
  left: 0;
  right: 0;
  text-align: center;
  color: rgb(var(--ice));
}
@keyframes ice { 50% { box-shadow: 0 0 0 1px rgba(var(--ice), 0.95), 0 0 22px rgba(var(--ice), 0.7); } }
body.frozen #ghost svg { filter: drop-shadow(0 0 8px rgb(var(--ice))); }
body.frozen #ghost .body { fill: color-mix(in srgb, var(--sheet) 70%, rgb(var(--ice))); }

/* ---- Leaderboard ---- */

.board { margin-top: 26px; }
.board[hidden] { display: none; }
.board h2 { font-family: var(--mono); font-size: 0.75rem; letter-spacing: 0.12em; text-transform: uppercase; color: var(--muted); margin: 0 0 10px; font-weight: 600; }
.lb-dialog {
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--panel);
  color: var(--text);
  padding: 26px 28px 22px;
  width: min(380px, calc(100vw - 32px));
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.45), 0 0 40px var(--ghost-wash);
}
.lb-dialog::backdrop { background: rgba(8, 10, 16, 0.55); backdrop-filter: blur(3px); }
.lb-dialog[open] { animation: pop 0.16s ease-out; }
@keyframes pop { from { opacity: 0; transform: translateY(8px) scale(0.98); } }
.lb-dialog h2 { font-family: var(--mono); font-size: 1.2rem; margin: 0 0 6px; color: var(--ghost); }
.lb-score { font-family: var(--mono); color: var(--muted); margin: 0 0 18px; }
.lb-score b { font-size: 1.6rem; color: var(--text); font-variant-numeric: tabular-nums; }
.lb-dialog .actions { margin-top: 18px; }
.lb-dialog input {
  font: inherit;
  font-family: var(--mono);
  width: 100%;
  padding: 10px 12px;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--text);
}
.lb-dialog input:focus { outline: 2px solid var(--ghost); outline-offset: 1px; }
.lb-status { font-family: var(--mono); font-size: 0.85rem; color: var(--muted); margin: 0 0 8px; min-height: 1.2em; }
.lb-status b { color: var(--text); }
.lb-list { list-style: none; margin: 0; padding: 0; font-family: var(--mono); font-size: 0.9rem; }
.lb-list li {
  display: grid;
  grid-template-columns: 2.4em 1fr auto auto;
  gap: 14px;
  padding: 6px 10px;
  border-radius: 6px;
  font-variant-numeric: tabular-nums;
}
.lb-list li:nth-child(odd) { background: var(--panel); }
.lb-list li.you { outline: 1px solid var(--you); }
.lb-list .r { color: var(--muted); }
.lb-list .n { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.lb-list .s { text-align: right; }
.lb-list .d { color: var(--muted); text-align: right; min-width: 9em; }
@media (max-width: 560px) {
  .lb-list li { grid-template-columns: 2em 1fr auto; }
  .lb-list .d { display: none; }
}

#sink {
  position: fixed;
  left: 0;
  top: 0;
  width: 1px;
  height: 1px;
  opacity: 0;
  font-size: 16px;
  border: 0;
  padding: 0;
}

@media (prefers-reduced-motion: reduce) {
  .shake, .bump, #line.enter, #ghost .sprite, #ghost .zzz, body.frozen #line .cur { animation: none !important; }
}

@media (max-width: 560px) {
  /* No margin to sleep in on a phone: it floats higher above the first letter instead. */
  #ghost.asleep .sprite { left: -6px; bottom: 100%; width: 24px; height: 27px; }
  header { padding: 12px 16px; }
  main { padding: 8px 16px 32px; justify-content: flex-start; }
  .hud { gap: 10px 18px; margin-bottom: 20px; }
  .stat b { font-size: 1.1rem; }
  .stat.pace b { font-size: 1.5rem; }
  .stat.score { margin-left: 0; text-align: left; }
}
