:root {
  --bg: #0c0e12;
  --bg-raised: #12151b;
  --line: #1f242c;
  --text-dim: #4d5561;
  --text-mid: #7c8794;
  --text: #d8dee6;
  --accent: #ffb703;
  --accent-dim: #7a6115;
  --error: #e8555f;
  --error-bg: rgba(232, 85, 95, 0.12);
  --caret: #ffb703;
  --radius: 3px;
  --mono: 'JetBrains Mono', ui-monospace, 'SFMono-Regular', Menlo, Consolas, monospace;
  --sans: 'Sora', ui-sans-serif, system-ui, sans-serif;
}

:root[data-theme="light"] {
  --bg: #f2efe9;
  --bg-raised: #e9e4da;
  --line: #d9d2c3;
  --text-dim: #b3a891;
  --text-mid: #857a63;
  --text: #37342c;
  --accent: #b5730a;
  --accent-dim: #d8c4a0;
  --caret: #b5730a;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--mono);
  height: 100%;
  overflow-x: hidden;
}

body {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  position: relative;
  transition: background 0.25s ease, color 0.25s ease;
}

.bg-grid {
  position: fixed;
  inset: 0;
  z-index: 0;
  pointer-events: none;
  background-image:
    linear-gradient(var(--line) 1px, transparent 1px),
    linear-gradient(90deg, var(--line) 1px, transparent 1px);
  background-size: 42px 42px;
  opacity: 0.35;
  mask-image: radial-gradient(ellipse 70% 55% at 50% 30%, black 0%, transparent 75%);
}

::selection { background: var(--accent); color: #14110a; }

/* ---------- topbar ---------- */

.topbar {
  position: relative;
  z-index: 2;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 28px 40px 8px;
  flex-wrap: wrap;
}

.brand {
  display: flex;
  align-items: center;
  gap: 8px;
  font-family: var(--sans);
  font-weight: 800;
  font-size: 19px;
  letter-spacing: -0.02em;
  color: var(--text);
}

.brand-mark {
  color: var(--accent);
  font-size: 22px;
  line-height: 1;
  transform: translateY(-1px);
}

.controls {
  display: flex;
  align-items: center;
  gap: 14px;
  background: var(--bg-raised);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 8px 14px;
  flex-wrap: wrap;
}

.control-sep {
  width: 1px;
  height: 16px;
  background: var(--line);
}

.control-group {
  display: flex;
  align-items: center;
  gap: 2px;
}

.control-btn {
  background: transparent;
  border: none;
  color: var(--text-mid);
  font-family: var(--mono);
  font-size: 13px;
  padding: 5px 9px;
  border-radius: var(--radius);
  cursor: pointer;
  transition: color 0.15s ease, background 0.15s ease;
  white-space: nowrap;
}

.control-btn:hover { color: var(--text); }

.control-btn.active {
  color: var(--accent);
}

.control-btn.on {
  color: var(--accent);
}

.theme-toggle {
  background: var(--bg-raised);
  border: 1px solid var(--line);
  color: var(--text-mid);
  width: 36px;
  height: 36px;
  border-radius: var(--radius);
  cursor: pointer;
  font-size: 15px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: color 0.15s ease, border-color 0.15s ease;
}
.theme-toggle:hover { color: var(--accent); border-color: var(--accent-dim); }

/* ---------- stage ---------- */

.stage {
  position: relative;
  z-index: 1;
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 20px 24px 40px;
  gap: 22px;
}

.live-stats {
  display: flex;
  gap: 26px;
  height: 44px;
  align-items: center;
}

.live-stat-value {
  font-size: 34px;
  font-weight: 600;
  color: var(--accent);
  font-variant-numeric: tabular-nums;
  min-width: 2ch;
  text-align: center;
  display: inline-block;
}

.type-area {
  position: relative;
  width: min(860px, 92vw);
  outline: none;
}

.words {
  font-size: 27px;
  line-height: 1.7;
  letter-spacing: 0.3px;
  max-height: calc(1.7em * 3);
  overflow: hidden;
  display: flex;
  flex-wrap: wrap;
  gap: 0 12px;
  filter: blur(0);
  transition: filter 0.2s ease;
  user-select: none;
}

.type-area:not(.focused) .words { filter: blur(4px); opacity: 0.6; }

.word {
  display: inline-flex;
  position: relative;
}

.word.current::after {
  content: "";
}

.letter {
  color: var(--text-dim);
  position: relative;
  transition: color 0.06s ease;
}

.letter.correct { color: var(--text); }
.letter.incorrect { color: var(--error); background: var(--error-bg); border-radius: 2px; }
.letter.incorrect.is-space { text-decoration: underline; text-decoration-color: var(--error); }
.letter.extra { color: var(--error); opacity: 0.75; }

.caret {
  position: absolute;
  width: 2px;
  background: var(--caret);
  border-radius: 2px;
  top: 2px;
  bottom: 2px;
  animation: blink 1s steps(1) infinite;
  transition: left 0.09s cubic-bezier(.4,0,.2,1), top 0.09s cubic-bezier(.4,0,.2,1);
}

@keyframes blink {
  0%, 50% { opacity: 1; }
  51%, 100% { opacity: 0; }
}

.hidden-input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
  height: 1px;
  width: 1px;
}

.focus-veil {
  position: absolute;
  inset: -12px;
  display: none;
  align-items: center;
  justify-content: center;
  background: color-mix(in srgb, var(--bg) 78%, transparent);
  backdrop-filter: blur(1px);
  border-radius: 6px;
  cursor: text;
}
.type-area:not(.focused) .focus-veil { display: flex; }
.focus-veil p {
  color: var(--text-mid);
  font-size: 14px;
  margin: 0;
}

.hint-row {
  display: flex;
  align-items: center;
  gap: 14px;
  min-height: 34px;
}

.restart-btn {
  background: transparent;
  border: none;
  color: var(--text-dim);
  cursor: pointer;
  display: flex;
  align-items: center;
  padding: 6px;
  border-radius: var(--radius);
  transition: color 0.15s ease, transform 0.15s ease;
}
.restart-btn:hover { color: var(--accent); transform: rotate(-40deg); }

.hint-text {
  color: var(--text-dim);
  font-size: 12px;
}
.hint-text kbd {
  font-family: var(--mono);
  background: var(--bg-raised);
  border: 1px solid var(--line);
  border-radius: 3px;
  padding: 1px 5px;
  color: var(--text-mid);
}

/* ---------- results ---------- */

.results {
  width: min(860px, 92vw);
  display: flex;
  flex-direction: column;
  gap: 22px;
  animation: rise 0.35s ease;
}

@keyframes rise {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}

.results-grid {
  display: flex;
  gap: 48px;
  align-items: flex-start;
  flex-wrap: wrap;
}

.result-label {
  color: var(--text-dim);
  font-size: 12px;
  text-transform: lowercase;
  margin-bottom: 4px;
}

.result-value {
  font-size: 56px;
  font-weight: 700;
  color: var(--accent);
  line-height: 1;
}

.result-secondary-grid {
  display: grid;
  grid-template-columns: repeat(2, auto);
  gap: 14px 34px;
}

.result-value-sm {
  font-size: 18px;
  color: var(--text);
}

.chart-wrap {
  background: var(--bg-raised);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 14px;
}

.next-btn {
  align-self: flex-start;
  background: var(--accent);
  color: #14110a;
  border: none;
  font-family: var(--mono);
  font-weight: 600;
  font-size: 14px;
  padding: 10px 18px;
  border-radius: var(--radius);
  cursor: pointer;
  transition: filter 0.15s ease;
}
.next-btn:hover { filter: brightness(1.08); }
.key-tip { font-weight: 400; opacity: 0.7; font-size: 12px; }

/* ---------- footer ---------- */

.footer {
  position: relative;
  z-index: 1;
  text-align: center;
  padding: 16px 20px 26px;
  color: var(--text-dim);
  font-size: 12px;
}

/* ---------- responsive ---------- */

@media (max-width: 720px) {
  .topbar { padding: 20px 18px 4px; justify-content: center; }
  .brand { width: 100%; justify-content: center; margin-bottom: 8px; }
  .words { font-size: 21px; line-height: 1.6; max-height: calc(1.6em * 4); }
  .result-value { font-size: 40px; }
  .results-grid { gap: 28px; }
}
