.typing-app {
  width: 100%;
  max-width: var(--content-width);
  margin-inline: auto;
  padding: 8px 24px 0;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

/* ---------- Config toolbar ---------- */
.test-config {
  align-self: center;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: 4px 6px;
  padding: 6px 8px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow);
}
.cfg-divider { width: 1px; height: 20px; background: var(--border-strong); margin-inline: 4px; }

.sub-select { display: inline-flex; align-items: center; gap: 6px; font-size: 0.8rem; color: var(--text-faint); }
.sub-select select { border-color: transparent; background-color: transparent; padding: 6px 28px 6px 8px; font-weight: 500; color: var(--text-muted); text-transform: capitalize; }
.sub-select select:hover { background-color: var(--surface-hover); color: var(--text); border-color: transparent; }
[data-sub-options]:empty { display: none; }
[data-sub-options] { display: inline-flex; }

.locked-note { text-align: center; font-size: 0.84rem; color: var(--text-faint); margin-top: -4px; }
.locked-note a { color: var(--accent); font-weight: 600; }

/* ---------- Typing card ---------- */
.typing-card {
  --pad: clamp(20px, 4vw, 40px);
  position: relative;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow);
  padding: 22px var(--pad) 18px;
}

.typing-card-head {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
  margin-bottom: 14px;
}

.stats-bar { display: flex; gap: 28px; flex-wrap: wrap; }
.stats-bar .stat { display: flex; flex-direction: column; gap: 1px; min-width: 52px; }
.stats-bar .stat span {
  font-family: var(--font-mono);
  font-size: 1.6rem;
  font-weight: 600;
  line-height: 1.1;
  letter-spacing: -0.03em;
  font-variant-numeric: tabular-nums;
  color: var(--text);
}
.stats-bar .stat:first-child span { color: var(--accent); }
.stats-bar .stat label { order: -1; font-size: 0.7rem; font-weight: 500; color: var(--text-faint); text-transform: uppercase; letter-spacing: 0.08em; }

.info-line {
  font-family: var(--font-mono);
  color: var(--text-faint);
  font-size: 0.76rem;
  letter-spacing: 0.04em;
  text-transform: lowercase;
}

.progress-track {
  height: 2px;
  border-radius: 999px;
  background: var(--border);
  overflow: hidden;
  margin-bottom: 22px;
}
.progress-bar {
  height: 100%;
  width: 0%;
  background: var(--accent);
  transition: width 0.2s linear;
}

/* ---------- Typing viewport ---------- */
.typing-viewport {
  --line-height: calc(2.9rem * var(--font-scale));
  position: relative;
  height: calc(3 * var(--line-height));
  overflow: hidden;
  cursor: text;
}
.typing-viewport.shake { animation: shake 0.18s ease; }
@keyframes shake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-3px); }
  75% { transform: translateX(3px); }
}

.typing-text {
  font-family: var(--font-mono);
  font-size: calc(1.5rem * var(--font-scale));
  line-height: var(--line-height);
  letter-spacing: -0.01em;
  color: var(--text-faint);
  text-align: justify;
  text-justify: inter-word;
  hyphens: none;
  will-change: transform;
  transition: filter 0.25s ease, opacity 0.25s ease;
}
.typing-app[data-mono="true"] .typing-text { text-align: left; }

.typing-text .word { display: inline-block; white-space: nowrap; }
.typing-text .char { position: relative; transition: color 0.08s ease; }
.typing-text .char.pending { color: var(--text-faint); }
.typing-text .char.correct { color: var(--text); }
.typing-text .char.incorrect { color: var(--error); background: var(--error-soft); border-radius: 3px; }
.typing-text .char.incorrect.space { text-decoration: underline wavy var(--error); }
.typing-text .char.current { color: var(--text-faint); }

.typing-caret {
  position: absolute;
  top: 0; left: 0;
  width: 2px;
  background: var(--caret);
  border-radius: 2px;
  transition: transform 0.1s cubic-bezier(.4,0,.2,1);
  opacity: 0;
  animation: caret-blink 1.1s steps(1) infinite;
}
.typing-viewport:focus-within .typing-caret { animation-play-state: paused; }
@keyframes caret-blink { 50% { visibility: hidden; } }
:root[data-cursor="block"] .typing-caret { width: 0.62em; background: var(--accent-soft); border: 1px solid var(--caret); }
:root[data-cursor="underline"] .typing-caret { width: 0.62em; height: 3px !important; margin-top: calc(var(--line-height) * 0.62); }

.typing-input-hidden {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  opacity: 0;
  border: none;
  padding: 0;
  cursor: text;
  caret-color: transparent;
  z-index: 2;
  /* A textarea, so code and data entry can hold line breaks. */
  resize: none;
  overflow: hidden;
  font-size: 16px; /* stops iOS zooming in on focus */
  color: transparent;
  background: transparent;
}

/* Code and data entry: keep indentation and don't stretch lines to justify. */
.typing-text.is-multiline { white-space: pre-wrap; text-align: left; }

/* Line ends in code and data entry: a faint ↵ shows where Enter is expected. */
.typing-text .char.newline::before {
  content: "↵";
  margin-left: 0.15em;
  font-size: 0.8em;
  color: var(--text-faint);
  opacity: 0.5;
}
.typing-text .char.newline.current::before { color: var(--caret); opacity: 1; }
.typing-text .char.newline.correct::before { opacity: 0; }
.typing-text .char.newline.incorrect::before { color: var(--error); opacity: 1; }

/* Focus guard: when the hidden input loses focus, blur the text and invite a click. */
.focus-guard {
  position: absolute;
  inset: 0;
  z-index: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  font-size: 0.95rem;
  font-weight: 500;
  color: var(--text);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease;
}
.focus-guard svg { width: 18px; height: 18px; color: var(--accent); }
.typing-viewport:not(:focus-within):not(:has(input:disabled)) .typing-text { filter: blur(5px); opacity: 0.5; }
.typing-viewport:not(:focus-within):not(:has(input:disabled)) .focus-guard { opacity: 1; }

.typing-card-foot {
  margin-top: 18px;
  padding-top: 14px;
  border-top: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
}
.typing-hint { color: var(--text-faint); font-size: 0.84rem; }
.typing-hint[hidden] { display: block !important; visibility: hidden; }
.shortcut-hints { display: flex; gap: 14px; flex-wrap: wrap; font-size: 0.78rem; color: var(--text-faint); }
.shortcut-hints span { display: inline-flex; align-items: center; gap: 6px; }

.custom-text-panel { display: flex; flex-direction: column; gap: 10px; width: 100%; }
.custom-text-panel textarea {
  width: 100%;
  min-height: 120px;
  resize: vertical;
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-md);
  padding: 14px 16px;
  color: var(--text);
  font-family: var(--font-mono);
  font-size: 0.92rem;
  line-height: 1.6;
}
.custom-text-panel textarea:focus { outline: none; border-color: var(--accent); }
.custom-text-panel .actions-row { display: flex; justify-content: flex-end; }

@media (max-width: 640px) {
  .typing-app { padding-inline: 16px; }
  .test-config { align-self: stretch; }
  .cfg-divider { display: none; }
  .typing-viewport { --line-height: calc(2.3rem * var(--font-scale)); height: calc(4 * var(--line-height)); }
  .typing-text { font-size: calc(1.12rem * var(--font-scale)); }
  .stats-bar { gap: 18px; }
  .stats-bar .stat span { font-size: 1.25rem; }
  .shortcut-hints { display: none; }
}
