:root {
  --bg: #f6f7fb; --surface: #ffffff; --ink: #14213d; --muted: #66708a; --line: #d9deea;
  --accent: #1f6feb; --accent-soft: #dbe9ff; --cursor: #ffd23f; --ok: #1a9b5b; --err: #e5484d; --err-soft: #ffe1e2;
  --key: #ffffff; --key-ink: #2b3550; --key-line: #b9c1d6; --key-target: #7cb8ff;
  --hand-fill: #ffffff; --hand-line: #33415e; --hand-opacity: 0.62;
  --f-lp: #ffd6e0; --f-lr: #ffe8c7; --f-lm: #fff6bf; --f-li: #d4f5d4; --f-ri: #cdeefe; --f-rm: #e1dcff; --f-rr: #f9d8f3; --f-rp: #ffd9cc; --f-t: #eceff5;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1420; --surface: #171e2d; --ink: #e8ecf5; --muted: #98a2b8; --line: #2a3346;
    --accent: #5b9dff; --accent-soft: #1d2f50; --cursor: #c99a00; --err-soft: #4a1f24;
    --key: #222b3d; --key-ink: #dfe5f2; --key-line: #39445c; --key-target: #2f6fd6;
    --hand-fill: #8693ad; --hand-line: #d8deea; --hand-opacity: 0.38;
    --f-lp: #5a2d3b; --f-lr: #5a4426; --f-lm: #555022; --f-li: #23502f; --f-ri: #1f4a5c; --f-rm: #3a3566; --f-rr: #57304f; --f-rp: #5c3426; --f-t: #2a3346;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0f1420; --surface: #171e2d; --ink: #e8ecf5; --muted: #98a2b8; --line: #2a3346;
  --accent: #5b9dff; --accent-soft: #1d2f50; --cursor: #c99a00; --err-soft: #4a1f24;
  --key: #222b3d; --key-ink: #dfe5f2; --key-line: #39445c; --key-target: #2f6fd6;
  --hand-fill: #8693ad; --hand-line: #d8deea; --hand-opacity: 0.38;
  --f-lp: #5a2d3b; --f-lr: #5a4426; --f-lm: #555022; --f-li: #23502f; --f-ri: #1f4a5c; --f-rm: #3a3566; --f-rr: #57304f; --f-rp: #5c3426; --f-t: #2a3346;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { background: var(--bg); color: var(--ink); font: 15px/1.45 system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif; }
button { font: inherit; color: inherit; }

/* Header */
.top { display: flex; align-items: center; gap: 12px; padding: 10px 16px; background: var(--surface); border-bottom: 1px solid var(--line); position: sticky; top: 0; z-index: 5; }
.top h1 { font-size: 18px; font-weight: 600; margin: 0; flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.top h1 small { color: var(--muted); font-weight: 400; margin-inline-start: 8px; }
.icon-btn { width: 40px; height: 40px; border: 0; background: none; border-radius: 10px; display: grid; place-items: center; cursor: pointer; color: var(--ink); flex: none; }
.icon-btn:hover { background: var(--accent-soft); }
.icon-btn[aria-pressed="false"] { opacity: 0.4; }
.icon-btn svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.tools { display: flex; gap: 2px; }

/* Main */
main { max-width: 1080px; margin: 0 auto; padding: 20px 16px 40px; }
.tip { color: var(--muted); text-align: center; min-height: 1.5em; margin: 0 0 6px; }
.goals { display: flex; justify-content: center; gap: 24px; font-size: 13px; color: var(--muted); margin-bottom: 10px; flex-wrap: wrap; }
.goals b { color: var(--ink); font-size: 17px; }
.goals .star { color: #f5b301; }

.text { font-size: clamp(20px, 3.2vw, 32px); line-height: 1.75; height: calc(1.75em * 3); overflow: hidden; position: relative; max-width: 900px; margin: 0 auto; scroll-behavior: smooth; }
.text .w { display: inline-block; white-space: pre; }
.text .c { border-radius: 3px; padding: 0 0.01em; }
.text .c.done { color: var(--muted); background: var(--accent-soft); }
.text .c.fixed { color: var(--err); background: var(--accent-soft); }
.text .c.wrong { color: var(--err); background: var(--err-soft); text-decoration: underline; }
.text .c.cur { background: var(--cursor); color: #14213d; }
.text .c.cur.shake { animation: shake 0.18s; background: var(--err); color: #fff; }
.text .sp.cur { box-shadow: inset 0 -3px 0 var(--accent); background: var(--cursor); }
@keyframes shake { 25% { transform: translateX(-2px); } 75% { transform: translateX(2px); } }

.bar { height: 3px; background: var(--line); max-width: 900px; margin: 14px auto 18px; border-radius: 2px; overflow: hidden; }
.bar > div { height: 100%; width: 0; background: var(--accent); transition: width 0.15s; }

.board { display: grid; grid-template-columns: 1fr 120px; gap: 16px; align-items: start; max-width: 1080px; margin: 0 auto; }
.kb-wrap svg { width: 100%; height: auto; display: block; overflow: hidden; }
.kb-wrap.with-hands svg { -webkit-mask-image: linear-gradient(#000 78%, transparent); mask-image: linear-gradient(#000 78%, transparent); }
.stats { display: grid; gap: 14px; padding-top: 4px; }
.stats .label { font-size: 13px; color: var(--muted); }
.stats .val { font-size: 28px; font-weight: 500; font-variant-numeric: tabular-nums; }
.stats .val small { font-size: 13px; color: var(--muted); margin-inline-start: 4px; }
.finger-hint { text-align: center; color: var(--muted); font-size: 13px; min-height: 1.4em; margin-top: 6px; }

/* Keyboard SVG */
.kb .key rect { fill: var(--key); stroke: var(--key-line); stroke-width: 0.025; transition: fill 0.12s; }
.kb .lbl { fill: var(--key-ink); font-size: 0.34px; text-anchor: middle; font-family: system-ui, sans-serif; }
.kb .lbl.sm { font-size: 0.27px; }
.kb .lbl.mod { font-size: 0.2px; fill: var(--muted); }
.kb .lbl.mod.start { text-anchor: start; } .kb .lbl.mod.end { text-anchor: end; }
.kb .bump { stroke: var(--key-ink); stroke-width: 0.03; stroke-linecap: round; opacity: 0.6; }
.kb.colored .key[data-finger="lp"] rect { fill: var(--f-lp); } .kb.colored .key[data-finger="lr"] rect { fill: var(--f-lr); }
.kb.colored .key[data-finger="lm"] rect { fill: var(--f-lm); } .kb.colored .key[data-finger="li"] rect { fill: var(--f-li); }
.kb.colored .key[data-finger="ri"] rect { fill: var(--f-ri); } .kb.colored .key[data-finger="rm"] rect { fill: var(--f-rm); }
.kb.colored .key[data-finger="rr"] rect { fill: var(--f-rr); } .kb.colored .key[data-finger="rp"] rect { fill: var(--f-rp); }
.kb.colored .key[data-finger="rt"] rect { fill: var(--f-t); }
.kb .key.target rect { fill: var(--key-target); stroke: var(--accent); }
.kb .key.target .lbl { fill: #fff; }
.kb .key.hit rect { fill: var(--ok); }
.kb .key.miss rect { fill: var(--err); }
.hands .hand { opacity: var(--hand-opacity); }
.hands .hand-outline * { stroke: var(--hand-line); fill: var(--hand-line); stroke-linecap: round; stroke-linejoin: round; }
.hands .hand-fill * { stroke: var(--hand-fill); fill: var(--hand-fill); stroke-linecap: round; stroke-linejoin: round; }
.hands .finger-hi path { fill: color-mix(in srgb, var(--accent) 22%, transparent); stroke: var(--accent); stroke-width: 0.06; }
.hands .finger-hi path.shift { stroke-dasharray: 0.12 0.08; }

/* Drawer */
.drawer { position: fixed; inset: 0 auto 0 0; width: min(360px, 88vw); background: var(--surface); border-inline-end: 1px solid var(--line); transform: translateX(-102%); transition: transform 0.2s; z-index: 20; overflow-y: auto; padding: 8px 0 24px; }
[dir="rtl"] .drawer { inset: 0 0 0 auto; transform: translateX(102%); }
.drawer.open { transform: none; }
.scrim { position: fixed; inset: 0; background: rgb(0 0 0 / 0.3); z-index: 15; display: none; }
.scrim.show { display: block; }
.drawer h2 { font-size: 13px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); margin: 18px 16px 6px; }
.drawer button.lesson { display: flex; width: 100%; text-align: start; border: 0; background: none; padding: 8px 16px; cursor: pointer; gap: 10px; align-items: center; }
.drawer button.lesson:hover { background: var(--accent-soft); }
.drawer button.lesson.current { background: var(--accent-soft); font-weight: 600; }
.drawer .num { color: var(--muted); width: 2.2em; flex: none; font-variant-numeric: tabular-nums; }
.drawer .name { flex: 1; }
.drawer .st { color: #f5b301; letter-spacing: 1px; font-size: 13px; flex: none; }
.drawer .st .off { color: var(--line); }

/* Dialogs */
dialog { border: 1px solid var(--line); border-radius: 16px; background: var(--surface); color: var(--ink); padding: 24px; width: min(420px, 92vw); }
dialog::backdrop { background: rgb(0 0 0 / 0.35); }
dialog h3 { margin: 0 0 12px; font-size: 20px; }
.result-stars { font-size: 40px; color: #f5b301; letter-spacing: 6px; text-align: center; margin: 4px 0 8px; }
.result-stars .off { color: var(--line); }
.result-grid { display: grid; grid-template-columns: repeat(3, 1fr); text-align: center; gap: 8px; margin: 12px 0 20px; }
.result-grid b { display: block; font-size: 24px; }
.result-grid span { color: var(--muted); font-size: 13px; }
.row-btns { display: flex; gap: 10px; justify-content: flex-end; flex-wrap: wrap; }
.btn { border: 1px solid var(--line); background: var(--surface); padding: 9px 16px; border-radius: 10px; cursor: pointer; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.hint { color: var(--muted); font-size: 13px; margin: 4px 0 0; }
.setting { margin-bottom: 16px; }
.setting label { display: flex; gap: 10px; align-items: center; justify-content: space-between; }
.setting select { font: inherit; padding: 6px 8px; border-radius: 8px; border: 1px solid var(--line); background: var(--surface); color: var(--ink); max-width: 60%; }

@media (max-width: 720px) {
  .board { grid-template-columns: 1fr; }
  .stats { grid-template-columns: repeat(2, 1fr); order: -1; }
  .tools .opt { display: none; }
}
