:root {
  --bg: #fafaf7;
  --surface: #ffffff;
  --text: #1d1d1b;
  --muted: #66665f;
  --border: #deded6;
  --accent: #1e6b3a;
  --ok-bg: #e6f2ea;
  --ok-text: #1e6b3a;
  --fail-bg: #fbe9e7;
  --fail-text: #a3261a;
  --covered: #e6f2ea;
  --uncovered: #f1f1ec;
  --gap: #fbe9e7;
  --mark: #fff0b3;
  --check: #fff3d6;
  --check-text: #7a5300;
  --almost-bg: #fff3d6;
  --almost-text: #7a5300;
  --sq-new: #ffffff;
  --sq-learning: #f3c96b;
  --sq-mastered: #2f8a52;
  --sq-uncovered: #e4e4dd;
  color-scheme: light;
}

/* Dark palette: when the system is dark (unless Light is chosen), or when Dark is chosen.
   The theme switch on the home screen sets data-theme on <html>; none = follow system. */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #161715;
    --surface: #1f201e;
    --text: #e9e9e4;
    --muted: #a1a199;
    --border: #3a3b37;
    --accent: #6cc58d;
    --ok-bg: #1d3325;
    --ok-text: #8fd9a9;
    --fail-bg: #3d1e1a;
    --fail-text: #f2a094;
    --covered: #1d3325;
    --uncovered: #2a2b28;
    --gap: #3d1e1a;
    --mark: #5a4a10;
    --check: #3b2f12;
    --check-text: #f0cf80;
    --almost-bg: #3b2f12;
    --almost-text: #f0cf80;
    --sq-new: #2a2b28;
    --sq-learning: #b8902f;
    --sq-mastered: #5fbf83;
    --sq-uncovered: #1c1d1b;
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --bg: #161715;
  --surface: #1f201e;
  --text: #e9e9e4;
  --muted: #a1a199;
  --border: #3a3b37;
  --accent: #6cc58d;
  --ok-bg: #1d3325;
  --ok-text: #8fd9a9;
  --fail-bg: #3d1e1a;
  --fail-text: #f2a094;
  --covered: #1d3325;
  --uncovered: #2a2b28;
  --gap: #3d1e1a;
  --mark: #5a4a10;
  --check: #3b2f12;
  --check-text: #f0cf80;
  --almost-bg: #3b2f12;
  --almost-text: #f0cf80;
  --sq-new: #2a2b28;
  --sq-learning: #b8902f;
  --sq-mastered: #5fbf83;
  --sq-uncovered: #1c1d1b;
  color-scheme: dark;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", sans-serif;
}

header, main {
  max-width: 1100px;
  margin: 0 auto;
  padding: 0 16px;
}

header { padding-top: 20px; padding-bottom: 12px; display: flex; align-items: baseline; justify-content: space-between; gap: 12px; }
h1 { margin: 0; font-size: 1.6rem; }
h1 a { color: var(--accent); text-decoration: none; }
nav a { color: var(--muted); font-size: 0.95rem; }
a { color: var(--accent); }
.muted { color: var(--muted); }
h2 { margin: 28px 0 10px; font-size: 1.2rem; }
.sub { margin: 4px 0 16px; color: var(--muted); }
code { font-size: 0.92em; }

.banner {
  padding: 12px 14px;
  border-radius: 8px;
  background: var(--surface);
  border: 1px solid var(--border);
}
/* When the data is fine the banner is one quiet line. */
.banner.ok { background: none; border: none; padding: 0 0 4px; color: var(--muted); font-size: 0.85rem; }
.banner.fail { background: var(--fail-bg); color: var(--fail-text); border-color: transparent; }
.banner.warn { margin-top: 8px; background: var(--almost-bg); color: var(--almost-text); border-color: transparent; }
.banner.warn p { margin: 0; }
.banner.warn p + p { margin-top: 6px; }
.warn-text { color: var(--almost-text); }
.banner ul { margin: 8px 0 0; padding-left: 20px; }
.banner p { margin: 6px 0 0; }

table {
  border-collapse: collapse;
  background: var(--surface);
  width: 100%;
}
th, td {
  padding: 6px 8px;
  border-bottom: 1px solid var(--border);
  text-align: left;
  vertical-align: top;
}
thead th { color: var(--muted); font-weight: 600; font-size: 0.85rem; }

/* Verbs table scrolls sideways on narrow screens. */
.scroll { overflow-x: auto; -webkit-overflow-scrolling: touch; }
table.verbs { min-width: 640px; }
table.verbs > tbody > tr > th { font-size: 1.05rem; }

table.forms { width: auto; background: transparent; }
table.forms th, table.forms td { padding: 2px 8px; border: none; font-size: 0.9rem; }
table.forms th { color: var(--muted); font-weight: 500; }

.cell { border-radius: 4px; white-space: nowrap; }
.cell.covered { background: var(--covered); }
.cell.uncovered { background: var(--uncovered); color: var(--muted); font-style: italic; }
.cell.gap { background: var(--gap); color: var(--fail-text); font-weight: 600; }
.cell.none { color: var(--muted); }

.legend { display: flex; flex-wrap: wrap; gap: 6px 10px; align-items: center; color: var(--muted); font-size: 0.85rem; }
.legend .cell { padding: 1px 8px; }

.controls { display: flex; flex-wrap: wrap; align-items: center; gap: 12px; margin-bottom: 10px; }
select { font: inherit; padding: 4px 6px; margin-left: 6px; }
.count { color: var(--muted); font-size: 0.9rem; }

.anchors .id { font-family: ui-monospace, Menlo, monospace; font-size: 0.82rem; white-space: nowrap; }
.anchors .pt { font-weight: 500; }
mark { background: var(--mark); color: inherit; border-radius: 3px; padding: 0 2px; }
.targets { margin: 0; padding: 0; list-style: none; font-size: 0.88rem; white-space: nowrap; }
.flag { display: inline-block; margin: 0 4px 4px 0; padding: 1px 7px; border-radius: 10px; font-size: 0.78rem; white-space: nowrap; }
.flag.est { background: var(--ok-bg); color: var(--ok-text); }
.flag.check { background: var(--check); color: var(--check-text); }

/* Both tables stack into cards on phones. */
@media (max-width: 700px) {
  table.verbs { min-width: 0; }
  table.verbs > thead { display: none; }
  table.verbs, table.verbs > tbody, table.verbs > tbody > tr,
  table.verbs > tbody > tr > th, table.verbs > tbody > tr > td { display: block; width: 100%; }
  table.verbs > tbody > tr { border: 1px solid var(--border); border-radius: 8px; margin-bottom: 10px; padding: 6px 0; }
  table.verbs > tbody > tr > th, table.verbs > tbody > tr > td { border: none; padding: 3px 12px; }
  table.anchors thead { display: none; }
  table.anchors, table.anchors tbody, table.anchors tr, table.anchors td { display: block; width: 100%; }
  table.anchors tr { border: 1px solid var(--border); border-radius: 8px; margin-bottom: 10px; padding: 6px 0; }
  table.anchors td { border: none; padding: 3px 12px; }
  table.verbs > tbody > tr > td::before,
  table.anchors td::before {
    content: attr(data-label);
    display: block;
    color: var(--muted);
    font-size: 0.75rem;
    text-transform: uppercase;
    letter-spacing: 0.03em;
  }
  .targets { white-space: normal; }
}

/* Buttons */
button {
  font: inherit;
  padding: 8px 16px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
}
button:disabled { opacity: 0.45; cursor: default; }
button.primary { background: var(--accent); border-color: var(--accent); color: var(--bg); font-weight: 600; }
button.link { border: none; background: none; padding: 4px 0; color: var(--muted); font-size: 0.9rem; }
button.chip { padding: 5px 12px; border-radius: 999px; font-size: 0.92rem; }
button.chip.quiet { color: var(--muted); }

/* Session picker */
.picker { max-width: 720px; }
fieldset { border: 1px solid var(--border); border-radius: 10px; margin: 0 0 14px; padding: 10px 14px 12px; background: var(--surface); }
legend { padding: 0 6px; font-weight: 600; }
.chips { display: flex; flex-wrap: wrap; gap: 8px; margin: 4px 0 12px; }
.checks { display: flex; flex-wrap: wrap; gap: 6px 18px; }
.verbs-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(250px, 1fr)); gap: 6px 18px; }
.verb-row { display: flex; align-items: center; justify-content: space-between; gap: 10px; }

/* Mastery grid: one row per tense, one square per person */
.minigrid { display: grid; grid-template-columns: repeat(var(--cols, 5), 11px); gap: 2px; flex: none; }
.sq { display: inline-block; width: 11px; height: 11px; border-radius: 2px; border: 1px solid var(--border); background: var(--sq-new); vertical-align: -1px; }
.sq.learning { background: var(--sq-learning); border-color: var(--sq-learning); }
.sq.mastered { background: var(--sq-mastered); border-color: var(--sq-mastered); }
.sq.uncovered { background: var(--sq-uncovered); border-style: dashed; }
.sq.none { visibility: hidden; }
.grid-legend { margin: -4px 0 12px; }
.mastered-tag, .reset-tag { margin-left: 6px; padding: 1px 7px; border-radius: 10px; font-size: 0.75rem; white-space: nowrap; }
.mastered-tag { background: var(--ok-bg); color: var(--ok-text); }
.reset-tag { background: var(--fail-bg); color: var(--fail-text); }
.badges:empty { display: none; }
.progress-box { margin-top: 22px; }
.progress-box .chips button { padding: 6px 14px; font-size: 0.92rem; }
button.quiet { color: var(--muted); }
label.check { display: flex; align-items: baseline; gap: 8px; padding: 3px 0; cursor: pointer; }
label.check input { transform: translateY(1px); }
.start-row { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 14px; margin: 18px 0 6px; }
.start-row .primary { padding: 10px 28px; }
.cell-count { font-weight: 600; }
.skipped { color: var(--muted); font-size: 0.9rem; }
.start-note { color: var(--fail-text); font-size: 0.9rem; }

/* Practice */
.practice { max-width: 640px; }
.practice-top { margin: 4px 0 6px; }
.step { color: var(--muted); font-size: 0.9rem; margin: 8px 0; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 16px 18px; }
.card .pt { font-size: 1.35rem; font-weight: 500; margin: 6px 0; }
.card .en { color: var(--muted); margin: 4px 0 10px; }
.card .cue { margin: 6px 0; }
.kind { display: inline-block; font-size: 0.75rem; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); }
.blank { display: inline-block; min-width: 3.5em; border-bottom: 2px solid var(--accent); color: transparent; }
.actions { display: flex; flex-wrap: wrap; gap: 10px; margin: 16px 0; }

.answer-row { display: flex; gap: 8px; margin: 12px 0 8px; }
input.answer {
  flex: 1;
  min-width: 0;
  font: inherit;
  font-size: 1.15rem;
  padding: 8px 10px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
}
input.answer:disabled { opacity: 0.8; }
.keys { display: flex; flex-wrap: wrap; gap: 6px; }
button.key { min-width: 40px; padding: 6px 0; font-size: 1.05rem; }

.feedback:empty { display: none; }
.feedback { margin-top: 14px; padding: 10px 14px; border-radius: 10px; }
.feedback p { margin: 4px 0; }
.feedback.correct { background: var(--ok-bg); color: var(--ok-text); }
.feedback.almost { background: var(--almost-bg); color: var(--almost-text); }
.feedback.wrong { background: var(--fail-bg); color: var(--fail-text); }
.feedback .sentence { font-size: 1.1rem; color: var(--text); }
.feedback .pt { color: var(--text); }
.hint { margin-top: 10px; padding-top: 8px; border-top: 1px solid currentColor; }
.hint-label { font-size: 0.75rem; text-transform: uppercase; letter-spacing: 0.04em; }
.hint .pt { color: var(--text); font-size: 1.05rem; }
.hint .en { color: var(--muted); }

table.summary { width: auto; min-width: min(100%, 420px); background: transparent; }
table.summary th, table.summary td { padding: 6px 14px 6px 0; white-space: nowrap; }
table.summary td.missed { color: var(--fail-text); }
table.summary td.first, table.summary td.retry { color: var(--ok-text); }
table.summary td.none { color: var(--muted); }
table.summary td.partial { color: var(--almost-text); }
.practice-tag { color: var(--muted); font-size: 0.85em; }
.small { font-size: 0.85rem; }

/* Recall and Echo prompts */
.card .gloss { font-size: 1.15rem; margin: 8px 0 4px; }
.card .instruction { font-size: 0.88rem; margin: 2px 0 6px; }
.question { display: flex; align-items: center; flex-wrap: wrap; gap: 4px 12px; }
.question .pt { margin: 6px 0; }
button.play { padding: 4px 12px; font-size: 0.9rem; }
.almost-tag { margin-left: 8px; padding: 1px 7px; border-radius: 10px; font-size: 0.78rem; background: var(--almost-bg); color: var(--almost-text); }

/* Discriminate */
.card .pt.big { font-size: 2rem; margin: 4px 0 12px; }
.options { display: flex; flex-wrap: wrap; gap: 10px; margin: 12px 0 4px; }
button.option { min-width: 7em; padding: 12px 18px; font-size: 1.15rem; }
button.option:disabled { opacity: 1; cursor: default; }
button.option.right { background: var(--ok-bg); color: var(--ok-text); border-color: var(--ok-text); font-weight: 600; }
button.option.wrong { background: var(--fail-bg); color: var(--fail-text); border-color: var(--fail-text); text-decoration: line-through; }

/* Audio: Play buttons only when a pt-PT voice exists (decision 0010) */
body.no-audio button.play { display: none; }
.replay { margin: 6px 0 2px; }
.audio-line { display: flex; flex-wrap: wrap; align-items: center; gap: 6px 10px; margin: 12px 0 0; }
.audio-line select { margin-left: 0; }

.steps-note { margin: 8px 0 0; }

/* Forms button and panel (decision 0015) */
.practice-top { display: flex; justify-content: space-between; align-items: center; gap: 10px; }
button.forms-btn { padding: 4px 12px; font-size: 0.88rem; border-radius: 999px; }
button.forms-btn.small-btn { padding: 2px 9px; font-size: 0.78rem; }
.row-end { display: flex; align-items: center; gap: 8px; flex: none; }
.looked-tag { margin-left: 6px; padding: 1px 7px; border-radius: 10px; font-size: 0.75rem; font-weight: 400; white-space: nowrap; background: var(--uncovered); color: var(--muted); }
dialog.forms-panel {
  border: 1px solid var(--border); border-radius: 14px; padding: 14px 18px 18px;
  background: var(--surface); color: var(--text); max-width: min(720px, calc(100vw - 32px));
  max-height: calc(100vh - 48px); overflow: auto;
}
dialog.forms-panel::backdrop { background: rgb(0 0 0 / 0.35); }
.panel-head { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; }
.panel-head h2 { margin: 0 0 6px; font-size: 1.1rem; }
.panel-blocks { display: flex; flex-wrap: wrap; gap: 14px 28px; }
.panel-block { flex: 1 1 300px; min-width: 0; }
.panel-block h3 { margin: 8px 0 4px; font-size: 1rem; }
.panel-block table.forms { width: auto; background: transparent; }
.panel-block table.forms td.cell.here { background: var(--mark); font-weight: 700; border-radius: 4px; }
/* Phones: a bottom sheet */
@media (max-width: 700px) {
  dialog.forms-panel {
    margin: auto 0 0; width: 100%; max-width: 100%; max-height: 80vh;
    border-radius: 14px 14px 0 0; border-bottom: none;
  }
  .panel-blocks { flex-direction: column; gap: 6px; }
}
