:root {
  --bg: #f4f5f7;
  --card: #ffffff;
  --text: #1c2330;
  --muted: #5d6675;
  --border: #d5d9e0;
  --accent: #2456c9;
  --accent-text: #ffffff;
  --done: #1e7a3c;
  --error: #b3261e;
  --error-bg: #fdecea;
  --warn-bg: #fff4d6;
  --warn-border: #e2b53a;
  --current-bg: #ffe58a;
  --rec: #d21f1f;
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14171c;
    --card: #1e232b;
    --text: #e7eaf0;
    --muted: #a0a8b6;
    --border: #39414e;
    --accent: #7aa2ff;
    --accent-text: #0d1320;
    --done: #6fd08c;
    --error: #ff8a80;
    --error-bg: #3a1f1d;
    --warn-bg: #3a3117;
    --warn-border: #a9892c;
    --current-bg: #6b5a12;
    --rec: #ff5252;
  }
}

* { box-sizing: border-box; }

html { font-size: 16px; }

body {
  margin: 0;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
  line-height: 1.5;
}

[hidden] { display: none !important; }

.top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.6rem 1.2rem;
  background: var(--card);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 5;
}

.brand { font-weight: 600; }

.rec {
  display: inline-flex;
  align-items: center;
  gap: 0.45rem;
  padding: 0.25rem 0.7rem;
  border: 2px solid var(--rec);
  border-radius: 999px;
  color: var(--rec);
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.rec-dot {
  width: 0.8rem;
  height: 0.8rem;
  border-radius: 50%;
  background: var(--rec);
  animation: pulse 1.1s ease-in-out infinite;
}

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.25; }
}

.banner { padding: 0.6rem 1.2rem; }
.banner.warn { background: var(--warn-bg); border-bottom: 1px solid var(--warn-border); }

main {
  flex: 1;
  width: 100%;
  padding: 1.5rem 1rem 2rem;
}

.card {
  max-width: 44rem;
  margin: 0 auto;
  padding: 1.5rem 1.75rem;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 10px;
}

.card.wide { max-width: 60rem; }

h1 { font-size: 1.45rem; margin: 0 0 0.75rem; line-height: 1.25; }
h2 { font-size: 1.05rem; margin: 1.25rem 0 0.4rem; }
p { margin: 0 0 0.8rem; }
.small { font-size: 0.875rem; color: var(--muted); }
.optional {
  font-size: 0.75rem;
  font-weight: 500;
  color: var(--muted);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 0.1rem 0.5rem;
  vertical-align: middle;
}

code {
  font-family: ui-monospace, "Cascadia Mono", Consolas, Menlo, monospace;
  font-size: 0.95em;
  overflow-wrap: anywhere;
}

button {
  font: inherit;
  padding: 0.5rem 1rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--card);
  color: var(--text);
  cursor: pointer;
}

button:hover:not(:disabled) { border-color: var(--accent); }
button:disabled { opacity: 0.5; cursor: not-allowed; }
button:focus-visible, input:focus-visible, select:focus-visible, a:focus-visible {
  outline: 3px solid var(--accent);
  outline-offset: 2px;
}

button.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-text);
  font-weight: 600;
}

button.link {
  padding: 0 0.3rem;
  border: none;
  background: none;
  color: var(--accent);
  text-decoration: underline;
}

.actions { display: flex; flex-wrap: wrap; gap: 0.6rem; margin: 1rem 0 0.5rem; }

label { display: block; font-weight: 500; margin-bottom: 0.25rem; }
label.radio, label.checkbox { font-weight: 400; margin: 0.3rem 0; display: flex; gap: 0.5rem; align-items: baseline; }

input[type="text"], select {
  width: 100%;
  font: inherit;
  padding: 0.5rem 0.6rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--card);
  color: var(--text);
}

fieldset { border: 1px solid var(--border); border-radius: 8px; margin: 0 0 1rem; padding: 0.6rem 0.9rem; }
legend { font-weight: 600; padding: 0 0.3rem; }
.field { margin-bottom: 1rem; }

.error { color: var(--error); }
.notice {
  padding: 0.6rem 0.8rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg);
}
.notice.error { background: var(--error-bg); border-color: var(--error); color: var(--text); }

/* searchable dropdown */
.combo { position: relative; margin-bottom: 1rem; }
.combo.disabled { opacity: 0.55; }
.combo ul {
  position: absolute;
  z-index: 10;
  left: 0;
  right: 0;
  max-height: 15rem;
  overflow-y: auto;
  margin: 0.15rem 0 0;
  padding: 0.25rem 0;
  list-style: none;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 6px;
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.18);
}
.combo li { padding: 0.35rem 0.7rem; cursor: pointer; }
.combo li.pinned { color: var(--muted); font-style: italic; }
.combo li.pinned:first-of-type, .combo li:not(.pinned) + li.pinned { border-top: 1px solid var(--border); }
.combo li:hover, .combo li.active { background: var(--accent); color: var(--accent-text); }
.combo li[aria-selected="true"] { font-weight: 700; }

/* rules */
.rules { padding-left: 1.2rem; margin: 0 0 1rem; }
.rules li { margin-bottom: 0.3rem; }
.rules.compact { font-size: 0.93rem; color: var(--muted); }

/* rounds list */
.rounds { list-style: none; padding: 0; margin: 0.4rem 0 0; }
.rounds li {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.6rem;
  padding: 0.45rem 0.7rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  margin-bottom: 0.4rem;
}
.rounds li[data-status="uploaded"] { border-left: 4px solid var(--done); }
.rounds li[data-status="failed"], .rounds li[data-status="rejected"] { border-left: 4px solid var(--error); }

/* typing */
.phase { font-weight: 600; min-height: 1.5rem; }
.phase[data-phase="typing"] { color: var(--done); }

.passage {
  font-family: ui-monospace, "Cascadia Mono", Consolas, Menlo, monospace;
  font-size: 1.35rem;
  line-height: 1.9;
  white-space: pre-wrap;
  overflow-wrap: break-word;
  padding: 1rem 1.1rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  transition: opacity 0.15s;
}
.passage.dimmed { opacity: 0.45; }
.ch { border-radius: 3px; }
.ch.done { color: var(--done); }
.ch.current { background: var(--current-bg); box-shadow: 0 2px 0 var(--accent); }
.ch.current.blocked { background: var(--error-bg); box-shadow: 0 2px 0 var(--error); }
.err-buf {
  background: var(--error);
  color: #ffffff;
  border-radius: 3px;
  text-decoration: line-through;
}

.progress-row { display: flex; align-items: center; gap: 0.75rem; margin-top: 0.8rem; }
.progress { flex: 1; height: 0.5rem; background: var(--border); border-radius: 999px; overflow: hidden; }
.progress-bar { height: 100%; width: 0; background: var(--done); transition: width 0.08s linear; }
.progress-text { font-variant-numeric: tabular-nums; color: var(--muted); font-size: 0.9rem; }

.feedback { min-height: 1.6rem; margin: 0.7rem 0 0; font-weight: 600; }
.feedback[data-kind="rule"] { color: var(--error); }
.feedback[data-kind="typo"] { color: var(--muted); }
.feedback.flash { animation: flash 0.35s ease-out; }
@keyframes flash {
  from { transform: translateX(-4px); }
  50% { transform: translateX(4px); }
  to { transform: translateX(0); }
}

.round-footer { display: flex; align-items: center; justify-content: space-between; gap: 1rem; margin-top: 1.2rem; }
.meter { flex: 0 1 14rem; height: 0.6rem; background: var(--border); border-radius: 999px; overflow: hidden; }
.meter-bar { height: 100%; width: 0; background: var(--accent); transition: width 0.05s linear; }
.meter-bar.hot { background: var(--error); }

/* review */
.stats { display: grid; grid-template-columns: max-content 1fr; gap: 0.25rem 1.2rem; margin: 0 0 1rem; }
.stats dt { color: var(--muted); }
.stats dd { margin: 0; font-variant-numeric: tabular-nums; }
.warnings { list-style: none; padding: 0; margin: 0 0 1rem; }
.warnings li {
  padding: 0.55rem 0.8rem;
  margin-bottom: 0.4rem;
  background: var(--warn-bg);
  border: 1px solid var(--warn-border);
  border-radius: 6px;
}
audio { width: 100%; }

/* consent dialog */
dialog {
  width: min(46rem, calc(100vw - 2rem));
  max-height: calc(100vh - 2rem);
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--card);
  color: var(--text);
}
dialog::backdrop { background: rgba(10, 14, 22, 0.6); }
#consent-form { display: flex; flex-direction: column; max-height: calc(100vh - 2rem); padding: 1.4rem 1.6rem; }
.consent-body {
  overflow-y: auto;
  padding: 0.6rem 0.8rem;
  margin-bottom: 0.8rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  outline: none;
}
.consent-body:focus-visible { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent); }
.consent-body > :first-child { margin-top: 0; }
.consent-body ul { padding-left: 1.2rem; margin: 0 0 0.8rem; }
.consent-body li { margin-bottom: 0.35rem; }

.bottom {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 0.4rem 1.5rem;
  padding: 0.7rem 1rem;
  font-size: 0.85rem;
  color: var(--muted);
  border-top: 1px solid var(--border);
}
.bottom a { color: var(--muted); }
.api-status::before { content: "\25CF  "; }
.api-status[data-state="ok"]::before { color: var(--done); }
.api-status[data-state="warn"]::before { color: var(--warn-border); }
.api-status[data-state="down"]::before { color: var(--error); }

@media (max-width: 40rem) {
  .card { padding: 1.1rem; }
  .passage { font-size: 1.1rem; }
}

@media (prefers-reduced-motion: reduce) {
  .rec-dot, .feedback.flash { animation: none; }
}

/* diagnostics page */
.diag table { border-collapse: collapse; width: 100%; font-size: 0.9rem; }
.diag th, .diag td { text-align: left; padding: 0.25rem 0.6rem; border-bottom: 1px solid var(--border); vertical-align: top; }
.diag pre {
  max-height: 18rem;
  overflow: auto;
  padding: 0.6rem;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 0.82rem;
}
.ok { color: var(--done); font-weight: 600; }
.bad { color: var(--error); font-weight: 600; }
