:root {
  --bg: #0e1014;
  --panel: #161a21;
  --line: #252b35;
  --text: #e8eaef;
  --dim: #6e7787;
  --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;
  /* The backdrop's glow and motes: base hue, saturation, and whether the page is light. */
  --glow-hue: 258;
  --glow-sat: 80%;
  --glow-light: 0;
  color-scheme: dark;
}

/* Dark is the default for everyone; the header picker sets data-theme. */
:root[data-theme='light'] {
  --bg: #f6f5f1;
  --panel: #ffffff;
  --line: #e2e0d8;
  --text: #1d2026;
  --dim: #868b95;
  --muted: #666c78;
  --you: #a36c00;
  --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;
  --glow-light: 1;
  color-scheme: light;
}

/* Green phosphor on black, monospace throughout. */
:root[data-theme='terminal'] {
  --bg: #050a05;
  --panel: #0b140b;
  --line: #173317;
  --text: #b8ffb8;
  --dim: #3f8f48;
  --muted: #64b56c;
  --you: #e8ff6a;
  --ghost: #4cff7a;
  --ghost-wash: rgba(76, 255, 122, 0.14);
  --sheet: #d9ffe0;
  --sheet-eye: #062b10;
  --ice: 120, 220, 255;
  --err: #ff5f56;
  --err-wash: rgba(255, 95, 86, 0.18);
  --clean: #7dff9a;
  --sans: var(--mono);
  --glow-hue: 130;
  --glow-sat: 70%;
  color-scheme: dark;
}

/* Ink on cream paper, with a serif for everything but the typing line. */
:root[data-theme='paper'] {
  --bg: #f3ecdc;
  --panel: #fbf6ea;
  --line: #ddd2bb;
  --text: #2b2116;
  --dim: #85775f;
  --muted: #6f6250;
  --you: #b5651d;
  --ghost: #4f63a0;
  --ghost-wash: rgba(79, 99, 160, 0.14);
  --sheet: #fffdf6;
  --sheet-eye: #2b2116;
  --ice: 80, 130, 200;
  --err: #b8322a;
  --err-wash: rgba(184, 50, 42, 0.12);
  --clean: #3d7d3a;
  --sans: Georgia, 'Iowan Old Style', 'Palatino Linotype', serif;
  --glow-hue: 38;
  --glow-sat: 45%;
  --glow-light: 1;
  color-scheme: light;
}

/* Neon pink and cyan on deep purple. */
:root[data-theme='synthwave'] {
  --bg: #140a24;
  --panel: #1e1035;
  --line: #34205a;
  --text: #fbe9ff;
  --dim: #7e63a3;
  --muted: #b49ad6;
  --you: #ffe14d;
  --ghost: #ff4fd8;
  --ghost-wash: rgba(255, 79, 216, 0.16);
  --sheet: #ffe6fb;
  --sheet-eye: #2a0a3d;
  --ice: 90, 240, 255;
  --err: #ff3b6b;
  --err-wash: rgba(255, 59, 107, 0.2);
  --clean: #36f1cd;
  --glow-hue: 300;
  --glow-sat: 90%;
  color-scheme: dark;
}

/* Sea-glass and teal on deep navy. */
:root[data-theme='ocean'] {
  --bg: #071521;
  --panel: #0c2133;
  --line: #173650;
  --text: #e2f4ff;
  --dim: #52809c;
  --muted: #86aec7;
  --you: #ffcf5c;
  --ghost: #5ee0d0;
  --ghost-wash: rgba(94, 224, 208, 0.15);
  --sheet: #e8fffb;
  --sheet-eye: #06303a;
  --ice: 170, 225, 255;
  --err: #ff6b6b;
  --err-wash: rgba(255, 107, 107, 0.18);
  --clean: #7cf0a8;
  --glow-hue: 195;
  --glow-sat: 75%;
  color-scheme: dark;
}

/* Amber and orange on charcoal. */
:root[data-theme='ember'] {
  --bg: #140d0a;
  --panel: #1f1510;
  --line: #3a271c;
  --text: #fbeee4;
  --dim: #85695a;
  --muted: #b9978a;
  --you: #ffd36b;
  --ghost: #ff8a3d;
  --ghost-wash: rgba(255, 138, 61, 0.16);
  --sheet: #fff0e2;
  --sheet-eye: #3a1a08;
  --ice: 160, 210, 255;
  --err: #ff5a5a;
  --err-wash: rgba(255, 90, 90, 0.18);
  --clean: #a6e36b;
  --glow-hue: 22;
  --glow-sat: 85%;
  color-scheme: dark;
}

* { 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;
}
.theme {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  color: var(--muted);
  font-family: var(--mono);
  font-size: 0.75rem;
}
.theme select {
  font: inherit;
  color: var(--text);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 3px 6px;
  cursor: pointer;
}
.theme select:focus-visible { outline: 2px solid var(--ghost); outline-offset: 1px; }
#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, #done { display: none; }
body[data-screen='menu'] #menu,
body[data-screen='done'] #done,
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); }

.mode {
  display: inline-flex;
  border: 1px solid var(--line);
  border-radius: 8px;
  overflow: hidden;
  margin-bottom: 10px;
}
.mode button {
  background: none;
  border: 0;
  padding: 7px 16px;
  color: var(--muted);
  font-family: var(--mono);
  font-size: 0.9rem;
}
.mode button[aria-checked='true'] { background: var(--panel); color: var(--text); box-shadow: inset 0 -2px 0 var(--ghost); }
.mode button[data-mode='hard'][aria-checked='true'] { box-shadow: inset 0 -2px 0 var(--err); color: var(--err); }
.mode-note { color: var(--muted); font-size: 0.9rem; line-height: 1.5; margin: 0 0 10px; min-height: 0; }
.mode-note:empty { display: none; }
.mode-tag { display: none; color: var(--err); margin-left: 6px; }
body.hard .mode-tag { display: inline; }
.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. Awake,
   it trails at text height just left of its marker, over text you've already
   typed, so waking is a short glide rather than a leap. */
#ghost.asleep .sprite { left: -46px; }
/* 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: -38px;
  bottom: 14%;
  width: 30px;
  height: 34px;
  animation: bob 1.6s ease-in-out infinite;
  transition: left 0.4s ease-out, bottom 0.4s ease-out, width 0.4s, height 0.4s;
}
#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.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))); }

/* ---- Learn mode ---- */

/* The menu swaps scores for the lesson list. */
.lessons { display: none; }
body.learn-menu .lessons {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(104px, 1fr));
  gap: 8px;
  margin: 4px 0 16px;
}
body.learn-menu #recalibrate, body.learn-menu #showBoard, body.learn-menu #menuBoard, body.learn-menu .rules { display: none; }
.lesson {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 2px;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  color: var(--text);
  font-family: var(--mono);
  font-size: 0.8rem;
  text-align: left;
}
.lesson b { color: var(--ghost); }
.lesson span { color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 100%; }
.lesson i { font-style: normal; color: var(--you); letter-spacing: 0.1em; }
.lesson:disabled { opacity: 0.4; cursor: default; }
.lesson:not(:disabled):hover { border-color: var(--ghost); }

/* In a lesson: no ghost, hearts or score, and the keyboard guide shows. */
body.learn #ghost, body.learn .hud { display: none; }
.keyboard { display: none; }
body.learn .keyboard {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 5px;
  margin-top: 24px;
  font-family: var(--mono);
}
.keyboard .row { display: flex; gap: 5px; }
.keyboard .r1 { margin-left: 18px; }
.keyboard .r2 { margin-left: 30px; }
.keyboard .k {
  position: relative;
  width: 38px;
  height: 38px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 6px;
  border: 1px solid var(--line);
  background: color-mix(in srgb, var(--panel) 75%, hsl(var(--fh), 70%, 50%));
  color: var(--text);
  font-size: 0.9rem;
  transition: transform 0.08s, box-shadow 0.08s;
}
.keyboard .k.wide { width: 70px; font-size: 0.7rem; }
.keyboard .k.space { width: 260px; font-size: 0.7rem; }
.keyboard .k.dim { opacity: 0.35; }
.keyboard .k.bump::after {
  content: '';
  position: absolute;
  bottom: 5px;
  width: 10px;
  height: 2px;
  border-radius: 1px;
  background: var(--muted);
}
.keyboard .k.next {
  opacity: 1;
  background: hsl(var(--fh), 75%, 55%);
  color: #111;
  transform: translateY(-2px);
  box-shadow: 0 0 0 2px var(--text), 0 0 16px hsl(var(--fh), 80%, 60%);
}
/* One hue per finger, mirrored left and right. */
.f-lp, .f-rp { --fh: 350; }
.f-lr, .f-rr { --fh: 30; }
.f-lm, .f-rm { --fh: 140; }
.f-li, .f-ri { --fh: 210; }
.f-th { --fh: 270; }
.keyboard .finger { margin: 8px 0 0; color: var(--muted); font-size: 0.85rem; min-height: 1.2em; }

#done { text-align: left; }
.done-stars { font-size: 2rem; color: var(--you); letter-spacing: 0.15em; margin: 0 0 6px; }
.done-stats { color: var(--muted); font-family: var(--mono); line-height: 1.6; margin: 0; }
.done-stats b { color: var(--text); }

@media (max-width: 560px) {
  .keyboard .k { width: 26px; height: 30px; font-size: 0.75rem; }
  .keyboard .k.wide { width: 40px; }
  .keyboard .k.space { width: 160px; }
  .keyboard .row { gap: 3px; }
  .keyboard .r1 { margin-left: 10px; }
  .keyboard .r2 { margin-left: 16px; }
}

/* ---- 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 .sprite { left: -12px; bottom: 100%; width: 24px; height: 27px; }
  #ghost.asleep .sprite { left: -6px; }
  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; }
}
