/* Typing Speed — a clean timed typing test with a live speed trail.
 * One Page Toys tool family, self-contained, theme-aware (light/dark). */

@import url("https://fonts.googleapis.com/css2?family=Archivo:wght@500;600;700;800;900&family=Inter:wght@400;500;600;700&family=Geist+Mono:wght@400;500;600&display=swap");

:root{
  --font-display: "Archivo", system-ui, -apple-system, "Segoe UI", sans-serif;
  --font-sans: "Inter", system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --font-mono: "Geist Mono", ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  --ink: #1a2230;
  --bg: #f6f8fb;
  --surface: #ffffff;
  --surface-2: #eef2f8;
  --muted: #6a7688;
  --dim: #aab3c2;
  --line: #e3e9f1;
  --line-strong: #cdd6e3;
  --accent: #3aa76d;
  --accent-hover: #2c8d59;
  --accent-soft: rgba(58, 167, 109, 0.14);
  --bad: #d2453f;
  --bad-soft: rgba(210, 69, 63, 0.15);
  --radius: 18px;
  --radius-sm: 12px;
  --radius-pill: 999px;
  --shadow: 0 2px 8px rgba(20, 32, 52, 0.06);
  --shadow-md: 0 8px 22px rgba(20, 32, 52, 0.10);
  --shadow-lg: 0 16px 40px rgba(20, 32, 52, 0.12);
  --glass: rgba(255, 255, 255, 0.74);
}
:root[data-theme="dark"]{
  --ink: #e7edf6;
  --bg: #0e1420;
  --surface: #172030;
  --surface-2: #1f2a3c;
  --muted: #93a1b6;
  --dim: #4d5a6e;
  --line: #232f42;
  --line-strong: #35435c;
  --accent: #4fd08c;
  --accent-hover: #6fdda2;
  --accent-soft: rgba(79, 208, 140, 0.16);
  --bad: #f2645e;
  --bad-soft: rgba(242, 100, 94, 0.18);
  --shadow: 0 2px 10px rgba(0, 0, 0, 0.45);
  --shadow-md: 0 10px 30px -6px rgba(0, 0, 0, 0.5);
  --shadow-lg: 0 18px 48px -10px rgba(0, 0, 0, 0.6);
  --glass: rgba(23, 32, 48, 0.62);
}

*, *::before, *::after{ box-sizing: border-box; }
html, body{ height: 100%; -webkit-tap-highlight-color: transparent; }
body{
  margin: 0; min-height: 100%;
  display: flex; flex-direction: column;
  font-family: var(--font-sans); color: var(--ink);
  background: radial-gradient(64% 46% at 50% 0%, var(--accent-soft), transparent 70%), var(--bg);
  overflow-x: hidden; -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}

/* ---------- Top bar ---------- */
.topbar{
  position: relative; z-index: 3;
  display: flex; align-items: center; justify-content: space-between;
  gap: 14px; flex-wrap: wrap;
  padding: 14px clamp(16px, 4vw, 30px);
  padding-top: max(14px, env(safe-area-inset-top));
  border-bottom: 1px solid var(--line);
  background: color-mix(in srgb, var(--bg) 82%, transparent);
  backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
}
.brand{ display: inline-flex; align-items: center; gap: 10px; font-family: var(--font-display); font-weight: 800; font-size: 0.86rem; letter-spacing: 0.16em; text-transform: uppercase; color: var(--ink); text-decoration: none; }
.brand__mark{ width: 1.1em; height: 1.1em; border: 1.5px solid var(--ink); border-radius: 6px; background: linear-gradient(90deg, var(--ink) 0 50%, var(--surface) 50% 100%); }
.topbar__right{ display: inline-flex; align-items: center; gap: 12px; }
.topbar__tag{ font-family: var(--font-display); font-size: 0.72rem; font-weight: 600; letter-spacing: 0.16em; text-transform: uppercase; color: var(--muted); }
.iconbtn{ appearance: none; display: inline-flex; align-items: center; justify-content: center; width: 38px; height: 38px; font-size: 1rem; line-height: 1; border: 1.5px solid var(--line-strong); border-radius: var(--radius-pill); background: var(--surface); color: var(--ink); cursor: pointer; box-shadow: var(--shadow); transition: transform 140ms ease, box-shadow 140ms ease, border-color 140ms ease; }
.iconbtn:hover{ transform: translateY(-2px); box-shadow: var(--shadow-md); border-color: var(--ink); }
.iconbtn:focus-visible{ outline: none; box-shadow: 0 0 0 2px var(--bg), 0 0 0 5px var(--accent-soft); }
.theme-toggle__sun, .theme-toggle__moon, .theme-toggle__auto{ display: none; }
:root[data-theme-mode="system"] .theme-toggle__auto{ display: inline; }
:root[data-theme-mode="light"] .theme-toggle__sun{ display: inline; }
:root[data-theme-mode="dark"] .theme-toggle__moon{ display: inline; }

/* ---------- Stage ---------- */
.stage{
  position: relative; z-index: 2; flex: 1 0 auto;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 22px;
  padding: clamp(20px, 4vw, 40px) 18px clamp(48px, 6vw, 72px);
  padding-bottom: max(clamp(48px, 6vw, 72px), env(safe-area-inset-bottom));
}
.ts{ width: 100%; max-width: 720px; }

/* ---------- HUD ---------- */
.hud{ display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; }
.hud__cell{
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  padding: 12px 6px; border: 1px solid var(--line); border-radius: var(--radius-sm);
  background: var(--glass); backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
}
.hud__k{ font-family: var(--font-display); font-size: 0.58rem; font-weight: 700; letter-spacing: 0.14em; text-transform: uppercase; color: var(--muted); }
.hud__v{ font-family: var(--font-display); font-size: clamp(1.15rem, 5vw, 1.7rem); font-weight: 900; color: var(--ink); font-variant-numeric: tabular-nums; line-height: 1.1; }
.hud__v.is-hot{ color: var(--accent); }
.hud__v.is-low{ color: var(--bad); }

/* ---------- Speed trail ---------- */
.trail{ display: block; width: 100%; height: 62px; margin-top: 12px; }

/* ---------- Type box ---------- */
.typebox{
  position: relative; margin-top: 12px;
  padding: clamp(18px, 4vw, 26px);
  border: 1px solid var(--line); border-radius: var(--radius);
  background: var(--surface); box-shadow: var(--shadow-lg);
  cursor: text; outline: none;
  min-height: 168px;
}
.typebox:focus-visible, .typebox.is-focus{ border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft), var(--shadow-lg); }
.words{
  font-family: var(--font-mono);
  font-size: clamp(1rem, 3.4vw, 1.22rem); line-height: 2.05;
  letter-spacing: 0.01em;
  color: var(--dim);
  user-select: none; -webkit-user-select: none;
  /* Five lines, always. 60 words are rendered ahead of the caret, which wraps
     to ~13 lines on a phone and turns the box into a wall of grey; the script
     re-renders to keep the active line near the top, so clipping the rest is
     exactly right. 2.05 line-height x 5. */
  max-height: 10.25em;
  overflow: hidden;
}
/* Each word is an unbreakable unit. Left to itself the browser will happily
   split "turtle" across two lines, because every character is its own span. */
.w{ display: inline-block; white-space: nowrap; vertical-align: top; }
.ch{ position: relative; white-space: pre; }
.ch.ok{ color: var(--ink); }
.ch.bad{ color: var(--bad); background: var(--bad-soft); border-radius: 3px; }
/* a wrong space has nothing to tint, so give it a body */
.ch.bad.sp{ background: var(--bad); opacity: 0.55; }
.ch.cur::before{
  content: ""; position: absolute; left: -1px; top: 0.1em; bottom: 0.1em;
  width: 2px; background: var(--accent); border-radius: 2px;
  animation: blink 1.06s steps(1) infinite;
}
@keyframes blink{ 0%,49%{ opacity: 1; } 50%,100%{ opacity: 0; } }
.caretHint{
  position: absolute; inset: 0; display: flex; align-items: center; justify-content: center;
  font-family: var(--font-display); font-size: 0.9rem; font-weight: 700; color: var(--muted);
  background: color-mix(in srgb, var(--surface) 95%, transparent);
  border-radius: var(--radius); pointer-events: none;
  transition: opacity 200ms ease;
}
.caretHint[hidden]{ display: none; }

/* the real input — offscreen but focusable, so mobile keyboards still open */
.sink{
  position: absolute; opacity: 0; pointer-events: none;
  width: 1px; height: 1px; left: -9999px; top: 0;
}

/* ---------- Controls ---------- */
.controls{ margin-top: 18px; display: flex; flex-wrap: wrap; gap: 12px; justify-content: center; align-items: center; }
.seg{ display: inline-flex; padding: 4px; gap: 4px; border: 1.5px solid var(--line-strong); border-radius: var(--radius-pill); background: var(--surface); box-shadow: var(--shadow); }
.seg__b{
  appearance: none; border: 0; background: transparent; cursor: pointer;
  font-family: var(--font-display); font-size: 0.82rem; font-weight: 700; color: var(--muted);
  padding: 8px 16px; border-radius: var(--radius-pill);
  transition: background 150ms ease, color 150ms ease;
}
.seg__b.is-on{ background: var(--ink); color: var(--bg); }
.seg__b:focus-visible{ outline: none; box-shadow: 0 0 0 3px var(--accent-soft); }

.btn{
  appearance: none; font-family: var(--font-display); font-size: 0.88rem; font-weight: 700;
  padding: 11px 20px; border-radius: var(--radius-pill);
  border: 1.5px solid var(--line-strong); background: var(--surface); color: var(--muted);
  cursor: pointer; box-shadow: var(--shadow);
  transition: transform 160ms ease, box-shadow 160ms ease, color 160ms ease, border-color 160ms ease, background 160ms ease;
}
.btn:hover{ transform: translateY(-2px); color: var(--ink); border-color: var(--ink); box-shadow: var(--shadow-md); }
.btn--primary{ background: var(--ink); border-color: var(--ink); color: var(--bg); }
.btn--primary:hover{ color: var(--bg); }
.btn:focus-visible{ outline: none; box-shadow: 0 0 0 2px var(--bg), 0 0 0 5px var(--accent-soft), var(--shadow); }

/* ---------- Result ---------- */
.result{
  margin-top: 20px; padding: clamp(18px, 4vw, 26px); text-align: center;
  border: 1px solid var(--line); border-radius: var(--radius);
  background: var(--glass); backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px);
  box-shadow: var(--shadow-lg);
}
.result[hidden]{ display: none; }
.result__eyebrow{ margin: 0 0 2px; font-family: var(--font-display); font-size: 0.66rem; font-weight: 700; letter-spacing: 0.18em; text-transform: uppercase; color: var(--muted); }
.result__big{ margin: 0; font-family: var(--font-display); font-weight: 900; font-size: clamp(2.4rem, 11vw, 3.6rem); line-height: 1; color: var(--accent); font-variant-numeric: tabular-nums; }
.result__big small{ font-size: 0.3em; letter-spacing: 0.16em; text-transform: uppercase; color: var(--muted); }
.result__line{ margin: 10px 0 0; font-size: 0.92rem; color: var(--ink); }
.result__grid{ margin-top: 16px; display: grid; grid-template-columns: repeat(auto-fit, minmax(96px, 1fr)); gap: 8px; }
.rstat{ padding: 10px 6px; border: 1px solid var(--line); border-radius: var(--radius-sm); background: var(--surface); }
.rstat b{ display: block; font-family: var(--font-display); font-size: 1.05rem; font-weight: 900; color: var(--ink); font-variant-numeric: tabular-nums; }
.rstat span{ font-family: var(--font-display); font-size: 0.56rem; font-weight: 700; letter-spacing: 0.12em; text-transform: uppercase; color: var(--muted); }
.result__actions{ margin-top: 18px; display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; }

.backlink{ font-family: var(--font-display); font-size: 0.72rem; font-weight: 600; letter-spacing: 0.16em; text-transform: uppercase; color: var(--muted); text-decoration: none; border-bottom: 1px solid transparent; transition: color 140ms ease, border-color 140ms ease; }
.backlink:hover{ color: var(--ink); border-bottom-color: var(--accent); }

@media (max-width: 430px){
  .hud{ grid-template-columns: repeat(4, 1fr); gap: 6px; }
  .hud__cell{ padding: 10px 2px; }
  .hud__k{ font-size: 0.5rem; letter-spacing: 0.08em; }
  .trail{ height: 48px; }
  .typebox{ min-height: 190px; }
}
@media (prefers-reduced-motion: reduce){
  .btn, .iconbtn, .seg__b, .caretHint{ transition: none; }
  .ch.cur::before{ animation: none; }
}
