:root {
  --bg: #fbf8f1;
  --panel: #ffffff;
  --ink: #1f2430;
  --muted: #6b7280;
  --line: #e4ded1;
  --primary: #1f5fbf;
  --primary-ink: #fff;
  --accent: #e8771e;
  --ok: #1a8f4c;
  --bad: #d23b3b;
  --sel: #e8771e;
  --piece: #1d3b74;
  --radius: 14px;
  --kai: "BiauKai", "DFKai-SB", "標楷體", "TW-Kai", "Kaiti TC", "KaiTi", serif;
  font-family: "Noto Sans TC", "Microsoft JhengHei", "PingFang TC", system-ui, sans-serif;
  color: var(--ink);
}
* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); }
body { font-size: 20px; -webkit-user-select: none; user-select: none; }
input, textarea, select { -webkit-user-select: text; user-select: text; }
a { color: var(--primary); }
.kai { font-family: var(--kai); }
.muted { color: var(--muted); }
.tiny { font-size: 13px; }
.big { font-size: 1.3em; }
.hidden { display: none !important; }
.grow { flex: 1; }
.row-gap { display: flex; gap: 12px; flex-wrap: wrap; align-items: center; margin-top: 12px; }

/* ---------- 導覽列 ---------- */
.topnav { display: flex; gap: 4px; align-items: center; padding: 6px 12px; background: #24324d; overflow-x: auto; }
.topnav a { color: #dfe6f3; text-decoration: none; padding: 8px 14px; border-radius: 10px; white-space: nowrap; font-size: 18px; }
.topnav a.active, .topnav a:hover { background: #ffffff22; color: #fff; }
.topnav .brand { font-weight: 700; color: #fff; }
.topnav .nav-settings { margin-left: auto; }

/* ---------- 按鈕 ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 52px; padding: 8px 18px; border-radius: 12px; border: 2px solid var(--line);
  background: #fff; color: var(--ink); font: inherit; font-size: 20px; cursor: pointer; text-decoration: none;
  touch-action: manipulation;
}
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn small { font-size: 14px; opacity: .8; }
.btn-sm { min-height: 38px; padding: 4px 12px; font-size: 16px; }
.btn-lg { min-height: 62px; font-size: 22px; padding: 10px 22px; }
.btn-xl { min-height: 78px; font-size: 26px; font-weight: 700; padding: 10px 22px; }
.btn-primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn-accent { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-danger { background: var(--bad); border-color: var(--bad); color: #fff; }
.btn-danger-outline { color: var(--bad); border-color: #f0c2c2; }
.btn.on { background: #fff1e4; border-color: var(--accent); color: #9a4a08; font-weight: 700; }

.input { font: inherit; font-size: 20px; padding: 10px 12px; border: 2px solid var(--line); border-radius: 10px; background: #fff; width: 100%; }
.input.num { width: 90px; display: inline-block; margin: 0 6px; }
.panel { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 18px 22px; margin: 16px; }
.panel h2 { margin: 0 0 10px; font-size: 24px; }
.error { color: var(--bad); }

/* ---------- 首頁 ---------- */
.home { max-width: 1200px; margin: 0 auto; padding: 30px 20px; text-align: center; min-height: calc(100vh - 60px); display: flex; flex-direction: column; justify-content: center; }
.home-h1 { font-size: 44px; margin: 0 0 30px; }
.home-grid { display: grid; grid-template-columns: 1.4fr 1fr 1fr 1fr; gap: 20px; }
.home-card { background: #fff; border: 2px solid var(--line); border-radius: 22px; padding: 34px 16px; text-decoration: none; color: var(--ink); box-shadow: 0 4px 0 var(--line); }
.home-card:active { transform: translateY(3px); box-shadow: 0 1px 0 var(--line); }
.home-main { background: #fff6ec; border-color: #f3c28f; box-shadow: 0 4px 0 #f3c28f; }
.home-icon { font-size: 64px; }
.home-title { font-size: 34px; font-weight: 800; margin: 10px 0 6px; }
.home-desc { color: var(--muted); }
.home-links { margin-top: 34px; display: flex; gap: 16px; justify-content: center; }

/* ---------- 數學 ---------- */
.math-pick, .math-result { max-width: 1100px; margin: 0 auto; padding: 20px; text-align: center; min-height: calc(100vh - 60px); display: flex; flex-direction: column; justify-content: center; align-items: center; gap: 14px; }
.math-pick h1 { font-size: 40px; margin: 0; }
.name-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 14px; width: 100%; }
.name-btn { background: #fff; border: 2px solid var(--line); border-radius: 18px; padding: 18px 10px; font: inherit; cursor: pointer; box-shadow: 0 4px 0 var(--line); touch-action: manipulation; }
.name-btn:active { transform: translateY(3px); box-shadow: none; }
.name-btn-name { font-size: 34px; font-weight: 700; }
.name-btn-sub { color: var(--muted); font-size: 18px; }
.math-stage { min-height: calc(100vh - 60px); display: flex; flex-direction: column; align-items: center; justify-content: flex-end; padding: 20px 20px 70px; gap: 22px; position: relative; }
.math-top { position: absolute; top: 16px; left: 20px; right: 20px; display: flex; align-items: center; justify-content: space-between; gap: 16px; }
.math-player { font-size: 32px; font-weight: 800; }
.math-clock { font-size: 44px; font-variant-numeric: tabular-nums; font-weight: 800; color: var(--primary); min-width: 140px; text-align: right; }
.math-progress { display: flex; gap: 8px; }
.dot { width: 22px; height: 22px; border-radius: 50%; background: #e6e0d3; }
.dot.done { background: var(--ok); }
.dot.now { background: var(--accent); transform: scale(1.25); }
.math-hint { font-size: 24px; color: var(--muted); }
.math-question { font-size: 120px; font-weight: 800; display: flex; gap: 30px; align-items: center; line-height: 1; font-variant-numeric: tabular-nums; }
.q-blank { color: var(--accent); border-bottom: 8px solid var(--accent); min-width: 1.2em; text-align: center; }
.math-options { display: flex; gap: 18px; flex-wrap: wrap; justify-content: center; }
.opt-btn { min-width: 170px; height: 150px; font-size: 76px; font-weight: 800; border-radius: 22px; border: 3px solid var(--line); background: #fff; cursor: pointer; box-shadow: 0 6px 0 var(--line); touch-action: manipulation; font-variant-numeric: tabular-nums; color: var(--ink); }
.math-options-ten .opt-btn { min-width: 120px; height: 130px; font-size: 64px; }
.opt-btn:active { transform: translateY(4px); box-shadow: 0 2px 0 var(--line); }
.opt-btn.right { background: #dff5e7; border-color: var(--ok); color: var(--ok); }
.opt-btn.wrong { background: #fde4e4; border-color: var(--bad); color: var(--bad); animation: shake .3s; opacity: .55; }
@keyframes shake { 25% { transform: translateX(-8px); } 75% { transform: translateX(8px); } }
.math-quit { position: absolute; bottom: 12px; right: 16px; }
.countdown { font-size: 220px; font-weight: 900; color: var(--accent); margin-bottom: 15vh; }
.result-time { font-size: 110px; font-weight: 900; color: var(--primary); }
.star-award { font-size: 34px; font-weight: 700; padding: 14px 28px; border-radius: 18px; background: #fff; border: 2px solid var(--line); }
.star-icon { font-size: 60px; margin-right: 10px; vertical-align: middle; }
.star-detail { font-size: 22px; font-weight: 400; color: var(--muted); margin-top: 6px; }
.star-gold { background: #fff6d6; border-color: #e5b92a; }
.star-silver { background: #f1f3f6; border-color: #a9b2bf; }
.star-bronze { background: #fbeee3; border-color: #c98a55; }

/* ---------- 排行榜 ---------- */
.board { max-width: 1400px; margin: 0 auto; }
.tabs { display: flex; gap: 8px; padding: 16px 16px 0; }
.tab { padding: 12px 26px; border-radius: 12px 12px 0 0; background: #ece6d9; text-decoration: none; color: var(--ink); font-size: 22px; }
.tab.active { background: #fff; font-weight: 700; }
.board-cols { display: grid; grid-template-columns: 1fr 1fr; }
.table { width: 100%; border-collapse: collapse; margin: 8px 0 14px; }
.table th, .table td { text-align: left; padding: 10px 8px; border-bottom: 1px solid var(--line); }
.table.small { font-size: 17px; }
.table.small td { padding: 6px 8px; }

/* ---------- 對話框與提示 ---------- */
.modal-overlay { position: fixed; inset: 0; background: #0006; display: flex; align-items: center; justify-content: center; z-index: 50; }
.modal { background: #fff; border-radius: 18px; padding: 28px; max-width: 560px; width: 90%; }
.modal-msg { font-size: 24px; margin: 0 0 20px; }
.modal-actions { display: flex; gap: 12px; justify-content: flex-end; }
.toast { position: fixed; left: 50%; bottom: 30px; transform: translate(-50%, 20px); background: #24324d; color: #fff; padding: 14px 24px; border-radius: 12px; opacity: 0; transition: .25s; z-index: 60; font-size: 20px; max-width: 80vw; }
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast-error { background: var(--bad); }
.toast-warn { background: #a35c0e; }

/* ---------- 拆字造字 ---------- */
body[data-page="hanzi"] { overflow-x: hidden; }
.hz-settings h2 { font-size: 22px; }
.set-grid { display: grid; grid-template-columns: 1fr 2fr; gap: 14px 24px; }
.set-item { display: flex; flex-direction: column; gap: 6px; font-size: 18px; }
.att-list { display: flex; flex-wrap: wrap; gap: 8px; }
.att { display: inline-flex; align-items: center; gap: 6px; padding: 8px 14px; border: 2px solid var(--line); border-radius: 10px; font-size: 22px; background: #fff; }
.att input { width: 24px; height: 24px; }

.hz-stage { height: 100vh; display: flex; flex-direction: column; position: relative; background: var(--bg); }
.hz-bar { display: flex; align-items: center; gap: 12px; padding: 8px 12px; border-bottom: 1px solid var(--line); background: #fff; }
.hz-lesson { font-weight: 700; white-space: nowrap; font-size: 18px; color: var(--muted); }
.char-chips { display: flex; gap: 8px; overflow-x: auto; flex: 1; padding: 2px; }
.char-chip { font-family: var(--kai); font-size: 34px; min-width: 60px; height: 60px; border-radius: 12px; border: 2px solid var(--line); background: #fff; cursor: pointer; position: relative; color: var(--ink); }
.char-chip.on { background: var(--primary); color: #fff; border-color: var(--primary); }
.char-chip.done::after { content: "✓"; position: absolute; top: -4px; right: 2px; font-size: 14px; color: var(--ok); font-family: sans-serif; }
.char-chip.on.done::after { color: #bff0d2; }
.char-chip:disabled { opacity: .35; }
.hz-bar-right { display: flex; gap: 8px; }

.hz-cols { flex: 1; min-height: 0; display: grid; grid-template-columns: minmax(200px, 15fr) minmax(260px, 24fr) minmax(420px, 40fr) minmax(250px, 21fr); gap: 10px; padding: 10px; }
.hz-stage.expanded .hz-cols { grid-template-columns: 0 minmax(240px, 20fr) minmax(500px, 58fr) minmax(240px, 22fr); }
.hz-stage.expanded .col-turn { display: none; }
.hz-col { background: #fff; border: 1px solid var(--line); border-radius: var(--radius); padding: 10px; display: flex; flex-direction: column; gap: 8px; min-height: 0; overflow: hidden; }
.col-title { font-weight: 700; color: var(--muted); font-size: 17px; }

.score-list { display: flex; flex-direction: column; gap: 2px; overflow-y: auto; }
.score-row { display: flex; justify-content: space-between; padding: 3px 8px; border-radius: 8px; font-size: 18px; }
.score-row.now { background: #fff1e4; font-weight: 700; }
.phase-label { font-size: 18px; font-weight: 700; padding: 6px 10px; border-radius: 10px; background: #eef3fb; color: var(--primary); text-align: center; }
.phase-observe { background: #fff1e4; color: #9a4a08; }
.phase-done { background: #e6f5ec; color: var(--ok); }
.queue { display: flex; flex-wrap: wrap; gap: 4px; font-size: 16px; align-items: center; }
.q-name { background: #f2efe7; padding: 2px 8px; border-radius: 8px; }
.current-card { text-align: center; padding: 10px 4px; border: 3px solid var(--accent); border-radius: 16px; background: #fffaf4; }
.cur-label { font-size: 17px; color: var(--muted); }
.cur-name { font-size: 40px; font-weight: 900; line-height: 1.2; word-break: keep-all; }
.cur-name.teacher { color: var(--primary); }
.col-turn .btn { width: 100%; flex-direction: column; gap: 0; }

.picker-svg { width: 100%; aspect-ratio: 1; background: #fffdf8; border: 2px solid var(--line); border-radius: 12px; touch-action: none; cursor: pointer; }
.pk-stroke { fill: #3d4350; transition: fill .1s; }
.pk-stroke.sel { fill: var(--sel); }
.tian-grid line { stroke: #e9b3a3; stroke-width: 3; stroke-dasharray: 18 14; }
.tian-border { fill: none; stroke: #e9b3a3; stroke-width: 6; }
.comp-chips { display: flex; flex-wrap: wrap; gap: 6px; }
.comp-chip { font-family: var(--kai); font-size: 26px; min-width: 50px; height: 50px; border-radius: 10px; border: 2px solid var(--line); background: #fff; cursor: pointer; color: var(--ink); }
.comp-chip.on { border-color: var(--sel); background: #fff1e4; color: #9a4a08; }
.sel-info { font-size: 16px; min-height: 22px; }
.tool-row { display: flex; gap: 6px; }
.tool-row .btn { flex: 1; padding: 6px; }

.col-pad { container-type: inline-size; }
.pad-row { display: flex; gap: 8px; align-items: flex-end; }
.pad-wrap { flex: 1; display: flex; justify-content: center; }
.pad-svg { width: min(100cqw - 100px, 100vh - 230px); height: min(100cqw - 100px, 100vh - 230px); background: #fffdf8; border-radius: 12px; touch-action: none; }
.pad-svg[data-mode="draw"] { cursor: crosshair; }
.pad-svg[data-mode="move"] { cursor: grab; }
.piece path { fill: var(--piece); }
.ink { fill: none; stroke: #111; stroke-linecap: round; stroke-linejoin: round; }
.ink-dot { fill: #111; }
.piece-box { fill: none; stroke: #9aa7bd; stroke-width: 4; stroke-dasharray: 14 10; }
.piece-box.active { stroke: var(--accent); stroke-width: 6; }
.piece-handle { fill: var(--accent); stroke: #fff; stroke-width: 6; }
.pad-tools { display: flex; flex-direction: column; gap: 6px; width: 88px; }
.tool-btn { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; height: 74px; border-radius: 12px; border: 2px solid var(--line); background: #fff; font: inherit; font-size: 17px; cursor: pointer; touch-action: manipulation; color: var(--ink); }
.tool-icon { font-size: 26px; line-height: 1; }
.tool-btn.on { background: var(--primary); border-color: var(--primary); color: #fff; }
.submit-btn { width: calc(100% - 96px); }

.res-body { display: flex; flex-direction: column; gap: 10px; overflow-y: auto; min-height: 0; }
.res-idle p { margin: 6px 0; color: var(--muted); font-size: 18px; }
.res-loading { display: flex; align-items: center; gap: 12px; font-size: 22px; padding: 20px 0; }
.spinner { width: 34px; height: 34px; border: 5px solid #dfe6f3; border-top-color: var(--primary); border-radius: 50%; animation: spin 0.8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.res-success { text-align: center; background: #effaf3; border: 2px solid #9fd8b4; border-radius: 16px; padding: 10px; }
.res-tag { display: inline-block; background: var(--ok); color: #fff; border-radius: 8px; padding: 2px 10px; font-size: 16px; }
.res-char { font-size: 130px; line-height: 1.1; }
.res-zhuyin { font-size: 34px; font-weight: 700; letter-spacing: 2px; }
.res-def { font-size: 15px; font-weight: 400; color: var(--muted); letter-spacing: 0; }
.reading-box { display: flex; flex-direction: column; gap: 6px; align-items: center; }
.reading-alt { display: flex; flex-wrap: wrap; gap: 4px; align-items: center; justify-content: center; font-size: 15px; }
.speak-src { font-size: 14px; }
.res-score { font-size: 26px; font-weight: 800; color: var(--ok); }
.res-score.muted { font-size: 18px; font-weight: 400; color: var(--muted); }
.res-confirm-title { font-size: 26px; font-weight: 800; color: #9a4a08; }
.reasons { margin: 0; padding-left: 20px; font-size: 16px; color: #7a4a14; }
.cand-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.cand-btn { display: flex; flex-direction: column; align-items: center; padding: 6px 2px; border: 2px solid var(--line); border-radius: 12px; background: #fff; cursor: pointer; font: inherit; color: var(--ink); }
.cand-btn .kai { font-size: 56px; line-height: 1.1; }
.cand-zy { font-size: 15px; color: var(--muted); min-height: 18px; }
.manual { display: flex; gap: 6px; align-items: center; font-size: 17px; }
.manual-input { width: 70px; font-size: 30px; text-align: center; padding: 4px; font-family: var(--kai); }
.res-error { color: var(--bad); }
.res-rejected { font-size: 20px; color: var(--muted); }
.engine-details { font-size: 14px; color: var(--muted); }
.engine-details ul { margin: 4px 0; padding-left: 18px; }
.made-list { border-top: 1px dashed var(--line); padding-top: 6px; }
.made-chips { display: flex; flex-wrap: wrap; gap: 4px; }
.made { display: inline-flex; align-items: baseline; gap: 4px; font-size: 14px; background: #f2efe7; border-radius: 8px; padding: 2px 8px; }
.made b { font-size: 26px; font-weight: 400; }
.made.by-teacher { background: #eef3fb; }
.res-summary h3 { margin: 4px 0 8px; }

.col-pad { position: relative; }
.observe-overlay { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; pointer-events: none; }
.observe-box { background: #fffffff0; border: 3px solid var(--accent); border-radius: 22px; padding: 20px 50px; text-align: center; font-size: 28px; font-weight: 700; box-shadow: 0 10px 30px #0002; }
.observe-num { font-size: 120px; color: var(--accent); line-height: 1; }
.teacher-banner { background: #24324d; color: #fff; padding: 8px 16px; font-size: 20px; text-align: center; }

/* ---------- 設定 ---------- */
.settings { max-width: 1200px; margin: 0 auto; }
.settings h1 { margin: 20px 16px 0; }
.lesson-edit { border: 1px solid var(--line); border-radius: 12px; padding: 12px; margin: 10px 0; }
.lesson-edit.new { border-color: var(--accent); background: #fffaf4; }
.lesson-fields { display: grid; grid-template-columns: 1fr 2fr; gap: 10px; }
.chars-input { font-family: var(--kai); font-size: 28px; }
.char-check { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 8px; }
.cc { font-family: var(--kai); font-size: 24px; padding: 0 6px; border-radius: 6px; background: #eef3fb; }
.cc.missing { background: #fde4e4; color: var(--bad); text-decoration: line-through; }
.warn-text { width: 100%; color: var(--bad); font-size: 16px; }
.ocr-box { border: 2px dashed var(--accent); border-radius: 12px; padding: 12px; margin: 10px 0; }
.ocr-img { max-width: 100%; max-height: 360px; display: block; margin-bottom: 8px; }
.star-inputs { display: flex; flex-direction: column; gap: 8px; margin: 10px 0; }

@media (max-width: 900px) {
  .home-grid, .board-cols, .set-grid, .lesson-fields { grid-template-columns: 1fr; }
  .name-grid { grid-template-columns: repeat(2, 1fr); }
}
