/*
 * Typing's page: the options bar at the top, the words in the middle (three
 * lines, a caret that glides), the hints at the bottom; the result takes the
 * words' place. The quiet grey of the letters still to type against the full
 * colour of the typed ones is the whole look, so everything else stays out of
 * the way: the bar and the hints fade while a test runs. Colours are the
 * panel's tokens, so both themes follow; the page is about 720 by 390 (560
 * wide in compact) and nothing scrolls.
 */
:root {
  --bg: var(--pal-bg, #f4f4f6);
  --text: var(--pal-fg, #1a1a1f);
  --main: var(--pal-accent, #4f46d6);
  --sub: color-mix(in srgb, var(--text) 34%, var(--bg));
  --faint: color-mix(in srgb, var(--text) 52%, var(--bg));
  --well: color-mix(in srgb, var(--text) 5.5%, var(--bg));
  --err: var(--pal-tag-red, #b02925);
  --err-extra: color-mix(in srgb, var(--err) 55%, var(--bg));
  --mono: var(--pal-font-mono, ui-monospace, "SF Mono", Menlo, monospace);
  --size: clamp(19px, 3.35vw, 26px);
  --line: calc(var(--size) * 1.6);
  --ease: cubic-bezier(0.2, 0.8, 0.2, 1);
  color-scheme: light;
}
:root[data-theme="dark"] {
  --sub: color-mix(in srgb, var(--text) 30%, var(--bg));
  --well: color-mix(in srgb, var(--text) 6%, var(--bg));
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; overflow: hidden; }
body {
  background: var(--bg);
  color: var(--text);
  font: 12.5px/1.3 var(--pal-font-ui, system-ui, sans-serif);
  display: flex;
  flex-direction: column;
  align-items: center;
  user-select: none;
  -webkit-user-select: none;
  cursor: default;
}
body.typing { cursor: none; }
button { font: inherit; color: inherit; background: none; border: 0; padding: 0; cursor: pointer; outline: none; }
svg { fill: none; stroke: currentColor; stroke-width: 1.5; stroke-linecap: round; stroke-linejoin: round; }

/* ---- the options bar ---- */
#bar {
  margin-top: 14px;
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 3px 6px;
  border-radius: 9px;
  background: var(--well);
  transition: opacity 240ms, transform 240ms var(--ease);
}
.group { display: flex; }
#bar button {
  display: flex;
  align-items: center;
  gap: 5px;
  height: 24px;
  padding: 0 8px;
  border-radius: 6px;
  color: var(--sub);
  font-size: 12px;
  transition: color 140ms;
}
#bar button:hover { color: var(--text); }
#bar button.on { color: var(--main); }
#bar button b { font: 600 12px var(--mono); }
#bar button svg { width: 13px; height: 13px; }
#lengths button { font: 12px var(--mono); padding: 0 7px; }
.sep { width: 3px; height: 14px; border-radius: 2px; background: color-mix(in srgb, var(--text) 10%, var(--bg)); margin: 0 3px; }
body.running.hush #bar, body.running.hush #hints { opacity: 0; transform: translateY(-3px); pointer-events: none; }
body.running.hush #hints { transform: translateY(3px); }

/* ---- the stage: the test or its result ---- */
#stage { flex: 1; width: 100%; display: grid; place-items: center; position: relative; }
#test, #result { grid-area: 1 / 1; transition: opacity 160ms, transform 260ms var(--ease); }
body.over #test { opacity: 0; transform: translateY(-6px); pointer-events: none; }
#result { opacity: 0; pointer-events: none; transform: translateY(8px); }
body.over #result { opacity: 1; pointer-events: auto; transform: none; }
#test.swap { opacity: 0; transition-duration: 90ms; }

#test { position: relative; width: min(88vw, 900px); margin-top: -6px; }
#meta {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  height: calc(var(--size) * 1.35);
  font: 500 calc(var(--size) * 0.96) var(--mono);
  font-variant-numeric: tabular-nums;
}
#counter { color: var(--main); opacity: 0; transition: opacity 200ms; }
body.running #counter { opacity: 1; }
#pb { font-size: 11.5px; color: var(--sub); font-family: var(--pal-font-ui, system-ui); transition: opacity 200ms; }
body.running #pb { opacity: 0; }

#words { height: calc(var(--line) * 3); overflow: hidden; position: relative; transition: filter 200ms, opacity 200ms; }
#inner {
  position: relative;
  display: flex;
  flex-wrap: wrap;
  font: var(--size) / var(--line) var(--mono);
  color: var(--sub);
  transition: transform 150ms var(--ease);
}
.word { margin-right: 0.6em; white-space: nowrap; height: var(--line); }
/* An underline, not a border: it stays inside the line, so a line slid out of the box leaves nothing behind. */
.word.err { text-decoration: underline 2px color-mix(in srgb, var(--err) 70%, transparent); text-underline-offset: 0.32em; text-decoration-skip-ink: none; }
.word span { transition: color 60ms; }
.word .ok { color: var(--text); }
.word .bad { color: var(--err); }
.word .extra { color: var(--err-extra); }

#caret {
  position: absolute;
  left: 0;
  top: 0;
  width: 2.5px;
  height: calc(var(--size) * 1.18);
  margin-top: calc((var(--line) - var(--size) * 1.18) / 2);
  margin-left: -1.25px;
  border-radius: 2px;
  background: var(--main);
  /* Short and front-loaded: most of the way on the first frame, so it keeps up with fast typing and still glides. */
  transition: transform 55ms cubic-bezier(0.25, 1, 0.5, 1);
  z-index: 1;
}
body.idle #caret { animation: blink 1s ease-in-out infinite; }
@keyframes blink { 0%, 100% { opacity: 1; } 50% { opacity: 0; } }

#focus {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  color: var(--text);
  font-size: 13px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 200ms;
}
#focus svg { width: 14px; height: 14px; }
body.blurred:not(.over) #words { filter: blur(5px); opacity: 0.4; }
body.blurred:not(.over) #focus { opacity: 1; }
body.blurred #caret { animation: none; opacity: 0; }

/* ---- the result ---- */
#result {
  width: min(92vw, 900px);
  display: grid;
  grid-template-columns: auto 1fr;
  grid-template-rows: 1fr auto;
  column-gap: 26px;
  row-gap: 14px;
  align-items: center;
  height: 100%;
  max-height: 300px;
  padding: 8px 0 4px;
}
.lbl { color: var(--sub); font-size: 12px; letter-spacing: 0.01em; display: flex; align-items: center; gap: 8px; }
#big { display: flex; flex-direction: column; gap: 10px; min-width: 128px; }
#big .num { font: 500 clamp(40px, 7.4vw, 58px) / 1 var(--mono); color: var(--main); font-variant-numeric: tabular-nums; letter-spacing: -0.02em; }
#r-note { color: var(--sub); font-size: 11.5px; line-height: 1.5; min-height: 1.5em; }
#r-note .bad { color: var(--err); }
#newbest {
  display: none;
  align-items: center;
  gap: 4px;
  padding: 2px 7px 2px 6px;
  border-radius: 99px;
  background: var(--pal-accent-soft, rgba(79, 70, 214, 0.12));
  color: var(--main);
  font-size: 11px;
  font-weight: 600;
}
#newbest svg { width: 12px; height: 12px; fill: currentColor; stroke-width: 1.2; }
body.best #newbest { display: inline-flex; animation: pop 520ms var(--ease) 380ms both; }
@keyframes pop { 0% { transform: scale(0.4); opacity: 0; } 60% { transform: scale(1.12); opacity: 1; } 100% { transform: scale(1); } }

#chart { position: relative; height: 100%; min-height: 120px; }
#plot { position: absolute; inset: 0; width: 100%; height: 100%; overflow: visible; }
#plot text { fill: var(--sub); stroke: none; font: 10px var(--mono); }
#plot .grid { stroke: color-mix(in srgb, var(--text) 8%, var(--bg)); stroke-width: 1; }
#plot .wpm { stroke: var(--main); stroke-width: 2.25; }
#plot .raw { stroke: var(--faint); stroke-width: 1.5; opacity: 0.75; }
#plot .area { stroke: none; fill: url(#fillgrad); }
#plot .stop { stop-color: var(--main); }
#plot .errx { stroke: var(--err); stroke-width: 1.6; }
#plot .guide { stroke: color-mix(in srgb, var(--text) 25%, var(--bg)); stroke-width: 1; opacity: 0; }
#plot .dot { fill: var(--main); stroke: var(--bg); stroke-width: 2; opacity: 0; }
#chart.hover .guide, #chart.hover .dot { opacity: 1; }
#plot .draw { animation: draw 900ms var(--ease) both; }
#plot .fade { animation: fadein 700ms ease 250ms both; }
@keyframes draw { from { stroke-dashoffset: var(--len); } to { stroke-dashoffset: 0; } }
@keyframes fadein { from { opacity: 0; } }

#tip {
  position: absolute;
  top: 0;
  pointer-events: none;
  padding: 6px 9px;
  border-radius: 7px;
  background: var(--pal-bg-elevated, #fff);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.14), 0 0 0 1px color-mix(in srgb, var(--text) 8%, transparent);
  font-size: 11.5px;
  line-height: 1.45;
  white-space: nowrap;
  opacity: 0;
  transition: opacity 120ms;
  font-variant-numeric: tabular-nums;
}
#chart.hover #tip { opacity: 1; }
#tip b { font-weight: 600; }
#tip i { font-style: normal; display: inline-block; width: 8px; height: 8px; border-radius: 2px; margin-right: 6px; }

#small { grid-column: 1 / -1; display: flex; justify-content: space-between; gap: 14px; }
#small .val { font: 500 18px/1.35 var(--mono); color: var(--main); font-variant-numeric: tabular-nums; white-space: nowrap; }
#r-type { font-size: 12.5px !important; line-height: 1.3 !important; white-space: normal !important; max-width: 24ch; }

/* The result's parts come in one after another. */
body.over #big, body.over #chart, body.over #small { animation: rise 420ms var(--ease) both; }
body.over #chart { animation-delay: 60ms; }
body.over #small { animation-delay: 120ms; }
@keyframes rise { from { opacity: 0; transform: translateY(6px); } }

/* ---- the hints ---- */
#hints {
  height: 34px;
  display: flex;
  align-items: center;
  gap: 16px;
  color: var(--sub);
  font-size: 11.5px;
  transition: opacity 240ms, transform 240ms var(--ease);
}
#hints span { display: inline-flex; align-items: center; gap: 5px; }
kbd {
  font: 10.5px/1 var(--mono);
  padding: 3px 5px;
  border-radius: 4px;
  background: var(--well);
  color: var(--faint);
  box-shadow: inset 0 -1px 0 color-mix(in srgb, var(--text) 10%, transparent);
}

/* Compact: a tighter bar and result row. */
@media (max-width: 600px) {
  #bar button[data-id] { font-size: 11.5px; padding: 0 6px; }
  #small .val { font-size: 16px; }
  #small { gap: 8px; }
}

/* ---- the stats page ---- */
#stats {
  grid-area: 1 / 1;
  align-self: stretch;
  width: min(92vw, 900px);
  height: 100%;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 10px 2px 18px;
  scrollbar-width: thin;
  -webkit-mask-image: linear-gradient(transparent, #000 10px, #000 calc(100% - 18px), transparent);
  mask-image: linear-gradient(transparent, #000 10px, #000 calc(100% - 18px), transparent);
  opacity: 0;
  pointer-events: none;
  transform: translateY(8px);
  transition: opacity 160ms, transform 260ms var(--ease);
  /* Absolute inside the stage's grid cell, so its height is the stage's and not its content's. */
  position: absolute;
  inset: 0;
  margin: 0 auto;
}
#stats::-webkit-scrollbar { width: 6px; }
#stats::-webkit-scrollbar-thumb { background: color-mix(in srgb, var(--text) 14%, transparent); border-radius: 3px; }
body.stats #stats { opacity: 1; pointer-events: auto; transform: none; }
body.stats #test, body.stats #result { opacity: 0; pointer-events: none; transform: translateY(-6px); }
body.stats #bar button[data-id="stats"] { color: var(--main); }

#kinds { display: flex; gap: 6px; overflow-x: auto; scrollbar-width: none; padding-bottom: 2px; }
#kinds::-webkit-scrollbar { display: none; }
#kinds button {
  flex: none;
  height: 24px;
  padding: 0 10px;
  border-radius: 99px;
  background: var(--well);
  color: var(--sub);
  font: 11.5px var(--mono);
  transition: color 140ms, background 140ms;
}
#kinds button:hover { color: var(--text); }
#kinds button.on { background: var(--pal-accent-soft, rgba(79, 70, 214, 0.12)); color: var(--main); }

#nothing { display: none; color: var(--sub); text-align: center; padding: 70px 0; font-size: 13px; }
#stats.empty > :not(#nothing) { display: none; }
#stats.empty #nothing { display: block; }

#tiles { display: grid; grid-template-columns: repeat(6, 1fr); gap: 8px; margin-top: 14px; }
.tile, .pb { padding: 9px 11px 10px; border-radius: 10px; background: var(--well); min-width: 0; }
.tile .val, .pb .val { font: 500 22px/1.2 var(--mono); color: var(--main); font-variant-numeric: tabular-nums; margin-top: 2px; white-space: nowrap; }
.tile .sub, .pb .sub { color: var(--sub); font-size: 10.5px; margin-top: 1px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tile .lbl, .pb .lbl { font-size: 11px; white-space: nowrap; }

.head { display: flex; justify-content: space-between; align-items: baseline; margin: 20px 0 8px; font-size: 12px; color: var(--faint); }
.head .note { color: var(--sub); font-size: 11px; display: inline-flex; align-items: center; gap: 5px; }
.k-dot { width: 6px; height: 6px; border-radius: 50%; background: color-mix(in srgb, var(--main) 45%, var(--bg)); }
.k-line { width: 12px; height: 2.5px; border-radius: 2px; background: var(--main); margin-left: 8px; }

#progress { position: relative; height: 150px; }
#progress svg { position: absolute; inset: 0; width: 100%; height: 100%; overflow: visible; }
#progress text { fill: var(--sub); stroke: none; font: 10px var(--mono); }
#progress .grid { stroke: color-mix(in srgb, var(--text) 8%, var(--bg)); stroke-width: 1; }
#progress .pt { fill: color-mix(in srgb, var(--main) 40%, var(--bg)); stroke: none; }
#progress .trend { stroke: var(--main); stroke-width: 2.25; }
#progress .dot { fill: var(--main); stroke: var(--bg); stroke-width: 2; opacity: 0; }
#progress.hover .dot, #progress.hover #ptip { opacity: 1; }
#ptip { position: absolute; top: 0; pointer-events: none; padding: 6px 9px; border-radius: 7px; background: var(--pal-bg-elevated, #fff);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.14), 0 0 0 1px color-mix(in srgb, var(--text) 8%, transparent);
  font-size: 11.5px; line-height: 1.45; white-space: nowrap; opacity: 0; transition: opacity 120ms; }
#ptip span { color: var(--sub); }

#bests { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.pb.none .val { color: var(--sub); }

#list { width: 100%; border-collapse: collapse; font-size: 12px; font-variant-numeric: tabular-nums; }
#list th { text-align: right; font-weight: 400; color: var(--sub); font-size: 11px; padding: 0 8px 6px; }
#list td { text-align: right; padding: 6px 8px; font-family: var(--mono); white-space: nowrap; }
#list th:nth-child(-n+2), #list td:nth-child(-n+2) { text-align: left; }
#list tbody tr:nth-child(odd) td { background: var(--well); }
#list tbody tr td:first-child { border-radius: 6px 0 0 6px; }
#list tbody tr td:last-child { border-radius: 0 6px 6px 0; }
#list td.date { color: var(--sub); }
#list td.kind { font-family: var(--pal-font-ui, system-ui); }
#list td.wpm { color: var(--main); font-weight: 600; }
#list td.wpm svg { width: 11px; height: 11px; fill: currentColor; margin-left: 4px; vertical-align: -1px; }

@media (max-width: 600px) {
  #tiles { grid-template-columns: repeat(3, 1fr); }
  #list th:nth-child(6), #list td:nth-child(6) { display: none; }
}

/* ---- the pace caret, stop on error, zen ---- */
#pace {
  position: absolute;
  left: 0;
  top: 0;
  width: 2.5px;
  height: calc(var(--size) * 1.18);
  margin-top: calc((var(--line) - var(--size) * 1.18) / 2);
  margin-left: -1.25px;
  border-radius: 2px;
  background: color-mix(in srgb, var(--main) 38%, transparent);
  opacity: 0;
  transition: opacity 200ms;
}
body.running.pacing #pace { opacity: 1; }

/* A key stop on error refused: the caret shakes, red for a moment. */
#caret.nope { animation: nope 260ms ease-out; }
@keyframes nope {
  0% { background: var(--err); translate: 0; }
  25% { translate: -3px; }
  50% { translate: 3px; }
  75% { background: var(--err); translate: -1.5px; }
  100% { translate: 0; }
}

#zenhint {
  position: absolute;
  left: 0;
  top: 0;
  height: var(--line);
  display: none;
  align-items: center;
  padding-left: 14px;
  color: var(--sub);
  font-size: 13px;
  pointer-events: none;
}
body.zen.idle #zenhint { display: flex; }
body.zen #lengths, body.zen .sep:has(+ #lengths) { display: none; }
body.zen #bar [data-id="punctuation"], body.zen #bar [data-id="numbers"] { opacity: 0.4; pointer-events: none; }
/* Zen's text is all typed: no grey words ahead, so the typed ones are simply the text colour. */
body.zen #inner { color: var(--text); }
