:root {
  --bg: #f6f5f1;
  --surface: #ffffff;
  --text: #1f2328;
  --muted: #6b7078;
  --border: #e2e0da;
  --accent: #3b5bdb;
  --accent-soft: #e7ecff;
  --good: #2b8a3e;
  --good-soft: #e3f5e7;
  --bad: #c92a2a;
  --bad-soft: #fde8e8;
  --warn-soft: #fff6dd;
  --bar-a: #adb5bd;
  --bar-b: #3b5bdb;
  --radius: 12px;
  --key-white: #ffffff;
  --key-white-text: #1f2328;
  --key-black: #1f2328;
  --key-black-text: #ffffff;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #15171b;
    --surface: #1e2126;
    --text: #e6e7e9;
    --muted: #9aa0a8;
    --border: #30343b;
    --accent: #7c95ff;
    --accent-soft: #252c47;
    --good: #51cf66;
    --good-soft: #1c3324;
    --bad: #ff6b6b;
    --bad-soft: #3a2020;
    --warn-soft: #352e1a;
    --bar-a: #5c636c;
    --bar-b: #7c95ff;
    --key-white: #d9dbdf;
    --key-white-text: #15171b;
    --key-black: #0b0c0e;
    --key-black-text: #e6e7e9;
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --bg: #15171b;
  --surface: #1e2126;
  --text: #e6e7e9;
  --muted: #9aa0a8;
  --border: #30343b;
  --accent: #7c95ff;
  --accent-soft: #252c47;
  --good: #51cf66;
  --good-soft: #1c3324;
  --bad: #ff6b6b;
  --bad-soft: #3a2020;
  --warn-soft: #352e1a;
  --bar-a: #5c636c;
  --bar-b: #7c95ff;
  --key-white: #d9dbdf;
  --key-white-text: #15171b;
  --key-black: #0b0c0e;
  --key-black-text: #e6e7e9;
  color-scheme: dark;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Noto Sans CJK SC", sans-serif;
  line-height: 1.65;
  -webkit-text-size-adjust: 100%;
}

a { color: var(--accent); }
h1, h2, h3, h4 { line-height: 1.3; margin: 0 0 0.6em; }
h1 { font-size: 1.6rem; }
h2 { font-size: 1.3rem; }
h3 { font-size: 1.05rem; margin-top: 1.2em; }
.card > h3:first-child, .card > h2:first-child { margin-top: 0; }

.wrap { max-width: 860px; margin: 0 auto; padding: 0 16px; }

.guide section { scroll-margin-top: 72px; }
.site-header {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
}
.header-inner { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; padding-top: 10px; padding-bottom: 10px; }
.brand { font-weight: 700; color: var(--text); text-decoration: none; }
nav { display: flex; gap: 4px; flex-wrap: wrap; }
nav a { color: var(--muted); text-decoration: none; padding: 4px 10px; border-radius: 8px; font-size: 0.95rem; }
nav a.active, nav a:hover { color: var(--accent); background: var(--accent-soft); }

main { padding-top: 20px; padding-bottom: 60px; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px 20px;
  margin-bottom: 16px;
}
.card.highlight { border-color: var(--accent); background: var(--accent-soft); }
.card.warn { background: var(--warn-soft); }
.center { text-align: center; }
.muted { color: var(--muted); }
.small { font-size: 0.875rem; }
.strong { font-weight: 600; }
.hidden { display: none; }
.warn-text { color: var(--bad); }
.eyebrow { font-size: 0.8rem; color: var(--muted); letter-spacing: 0.02em; }
.row { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; }
.order { font-size: 1.1rem; font-weight: 600; text-align: center; }

button, .button {
  font: inherit;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 8px 16px;
  cursor: pointer;
  text-decoration: none;
  display: inline-block;
  touch-action: manipulation;
}
button:hover:not(:disabled), .button:hover { border-color: var(--accent); }
button:disabled { opacity: 0.5; cursor: not-allowed; }
button.primary, .button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
button.danger { color: var(--bad); border-color: var(--bad); }
button.big { font-size: 1.1rem; padding: 12px 36px; margin: 8px 0; }
button.link { background: none; border: none; color: var(--accent); padding: 0; }

input[type="text"], input[type="number"], select {
  font: inherit;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 6px 10px;
  max-width: 100%;
}
input[type="range"] { flex: 1; min-width: 160px; }
.field { display: flex; flex-direction: column; gap: 4px; margin: 10px 0; }
.field select { align-self: flex-start; }

/* Training overview */
.level-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; }
.level-head h2 { font-size: 1.6rem; margin: 0; }
.badge { padding: 4px 12px; border-radius: 999px; font-size: 0.85rem; white-space: nowrap; }
.badge-fb { background: var(--good-soft); color: var(--good); }
.badge-nofb { background: var(--accent-soft); color: var(--accent); }
.level-summary { display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); gap: 6px 20px; margin-top: 12px; }
.kv { display: flex; gap: 10px; }
.kv .k { color: var(--muted); min-width: 5em; }
.kv .v { font-weight: 500; }

/* Trials */
.trial-head { display: flex; justify-content: space-between; flex-wrap: wrap; gap: 8px; color: var(--muted); font-size: 0.9rem; margin-bottom: 12px; }
.trial-status {
  min-height: 56px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.4rem;
  font-weight: 600;
  color: var(--accent);
}
.hold { display: flex; align-items: center; justify-content: center; gap: 12px; flex-wrap: wrap; font-size: 1rem; font-weight: 500; color: var(--text); }
.timer { height: 6px; background: var(--border); border-radius: 3px; overflow: hidden; margin-bottom: 16px; }
.timer-fill { height: 100%; width: 100%; background: var(--accent); transform-origin: left; }
.timer-fill.running { animation-name: countdown; animation-timing-function: linear; animation-fill-mode: forwards; }
@keyframes countdown { from { transform: scaleX(1); } to { transform: scaleX(0); } }

.choice-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(76px, 96px)); justify-content: center; gap: 8px; }
.choice-grid.compact { grid-template-columns: repeat(auto-fit, minmax(64px, 80px)); justify-content: start; }
.choice {
  position: relative;
  min-height: 64px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 6px;
  font-size: 1.15rem;
  font-weight: 600;
}
.choice.small { min-height: 48px; font-size: 1rem; }
.key-hint { position: absolute; top: 3px; right: 7px; font-size: 0.7rem; font-weight: 400; color: var(--muted); }
@media (hover: none) { .key-hint { display: none; } }
.choice .flat { font-size: 0.75rem; font-weight: 400; color: var(--muted); }
.choice.picked { border-color: var(--accent); background: var(--accent-soft); }
.feedback-line { min-height: 2em; margin-top: 12px; text-align: center; font-weight: 600; }
.feedback-line.good { color: var(--good); }
.feedback-line.bad { color: var(--bad); }
.feedback-line.neutral { color: var(--muted); font-weight: 400; }

.progress { height: 8px; background: var(--border); border-radius: 4px; overflow: hidden; margin: 16px 0 8px; }
.progress-fill { height: 100%; width: 0; background: var(--accent); }

/* Results */
.result { text-align: center; }
.result.pass { border-color: var(--good); }
.result.fail { border-color: var(--bad); }
.big-number { font-size: 3rem; font-weight: 700; line-height: 1.2; }
.result.pass .big-number { color: var(--good); }
.result.fail .big-number { color: var(--bad); }
.mini-table { margin: 12px auto 0; border-collapse: collapse; font-size: 0.9rem; }
.mini-table th, .mini-table td { padding: 2px 14px; border-bottom: 1px solid var(--border); }

.data-table { width: 100%; border-collapse: collapse; font-size: 0.9rem; margin: 8px 0; display: block; overflow-x: auto; }
.data-table th, .data-table td { padding: 6px 10px; border-bottom: 1px solid var(--border); text-align: left; white-space: nowrap; }
.data-table th { color: var(--muted); font-weight: 500; }

/* Stats */
.stat-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px; margin-bottom: 16px; }
.stat { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px 14px; }
.stat-label { font-size: 0.8rem; color: var(--muted); }
.stat-value { font-size: 1.4rem; font-weight: 700; }
.stat-sub { font-size: 0.75rem; color: var(--muted); }
.chart { width: 100%; height: auto; display: block; }
.chart text { font-size: 11px; fill: var(--muted); }
.chart .axis-title { font-size: 12px; }
.chart .grid { stroke: var(--border); stroke-width: 1; }
.chart .series { fill: none; stroke: var(--accent); stroke-width: 2.5; }
.chart .bar, .chart .bar-b { fill: var(--bar-b); }
.chart .bar-a { fill: var(--bar-a); }
.chart .bar-label { fill: var(--text); font-size: 11px; }
.chart .ref { stroke: var(--muted); stroke-dasharray: 4 4; }
.swatch { display: inline-block; width: 10px; height: 10px; border-radius: 2px; vertical-align: middle; }
.swatch.a { background: var(--bar-a); }
.swatch.b { background: var(--bar-b); }
.test-row { border-top: 1px solid var(--border); padding: 8px 0; }
.test-row summary { cursor: pointer; }

.guide ul, .guide ol { padding-left: 1.3em; }
.guide li { margin: 4px 0; }

/* On-screen piano keyboard for answering */
.piano-pad {
  display: grid;
  grid-template-columns: repeat(14, minmax(0, 40px));
  grid-template-rows: 72px 80px;
  gap: 8px 0;
  justify-content: center;
  margin: 8px 0;
}
.pkey {
  position: relative;
  margin: 0 3px;
  padding: 4px 2px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  border-radius: 8px;
  border-bottom-width: 3px;
  font-size: 1.05rem;
  font-weight: 600;
  min-width: 0;
}
.piano-pad.with-oob { grid-template-columns: repeat(16, minmax(0, 40px)); }
.pkey.white { grid-row: 2; background: var(--key-white); color: var(--key-white-text); }
.pkey.black { grid-row: 1; background: var(--key-black); color: var(--key-black-text); border-color: var(--key-black); }
.pkey.oob { background: var(--bg); color: var(--text); font-size: 0.95rem; }
.pkey .key-hint { position: static; font-size: 0.7rem; font-weight: 400; opacity: 0.7; color: inherit; }
.pkey:disabled { opacity: 1; cursor: default; }
.pkey.unused { opacity: 0.18; }
.pkey.picked { outline: 3px solid var(--accent); outline-offset: -3px; }
.pkey.correct { background: var(--good-soft); color: var(--good); border-color: var(--good); }
.pkey.wrong { background: var(--bad-soft); color: var(--bad); border-color: var(--bad); }

@media (max-width: 520px) {
  h1 { font-size: 1.35rem; }
  .card { padding: 14px; }
  .piano-pad { grid-template-rows: 60px 68px; }
  .pkey { margin: 0 2px; font-size: 0.9rem; }
}

.keymap {
  display: grid;
  grid-template-columns: repeat(16, minmax(0, 26px));
  grid-auto-rows: 52px;
  gap: 6px 0;
  justify-content: center;
  margin: 12px 0;
}
.keymap .cap {
  grid-row: 2;
  margin: 0 3px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  border: 1px solid var(--border);
  border-bottom-width: 3px;
  border-radius: 6px;
  background: var(--surface);
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-weight: 600;
  line-height: 1.2;
}
.keymap .cap:not(.black):not(.oob-cap) { background: var(--key-white); color: var(--key-white-text); }
.keymap .cap.black { grid-row: 1; background: var(--key-black); color: var(--key-black-text); border-color: var(--key-black); }
.keymap .cap.oob-cap { background: var(--bg); }
.keymap .cap small { font-family: inherit; font-weight: 400; font-size: 0.7rem; opacity: 0.8; }
kbd {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.85em;
  border: 1px solid var(--border);
  border-bottom-width: 2px;
  border-radius: 4px;
  padding: 0 5px;
  background: var(--surface);
}

/* Test diagnostics */
.table-scroll { overflow-x: auto; margin: 8px 0; }
.table-scroll .data-table { display: table; margin: 0; }
.diag-controls { margin: 8px 0 4px; }
.diag-section { border-top: 1px solid var(--border); padding-top: 14px; margin-top: 14px; }
.diag-section h4 { margin-bottom: 4px; }
.seg { display: inline-flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.seg button { border: none; border-radius: 0; padding: 4px 12px; font-size: 0.875rem; }
.seg button + button { border-left: 1px solid var(--border); }
.seg button.active { background: var(--accent); color: #fff; }
.confusion th, .confusion td { text-align: center; padding: 4px 6px; min-width: 30px; font-variant-numeric: tabular-nums; }
.confusion thead th { font-size: 0.8rem; }
.confusion tbody th { text-align: right; }
.cm-cell.heat { background: color-mix(in srgb, var(--accent) calc(var(--heat) * 70%), transparent); }
.cm-cell.diag { box-shadow: inset 0 0 0 2px var(--text); font-weight: 700; }
.cm-total { color: var(--muted); }
.cm-foot td, .cm-foot th { border-top: 2px solid var(--border); font-weight: 600; }
.sig-row th, .sig-row td { font-weight: 600; }
.chart .whisker { stroke: var(--text); stroke-width: 1.5; }
.chart .hit { fill: transparent; }
.chart .hover-mark:hover .hit { fill: var(--accent-soft); }
.err-bars { display: grid; grid-template-columns: repeat(12, 1fr); gap: 4px; height: 160px; align-items: end; margin: 8px 0; }
.err-col { display: flex; flex-direction: column; align-items: center; justify-content: flex-end; height: 100%; }
.err-bar { width: 70%; min-height: 0; background: var(--bar-a); border-radius: 4px 4px 0 0; }
.err-col.zero .err-bar { background: var(--bar-b); }
.err-n { font-size: 0.75rem; color: var(--text); }
.err-label { font-size: 0.75rem; color: var(--muted); margin-top: 2px; }
