/* ===== Màu & chữ ===== */
:root {
  --bg: #f5efe6; --bg-line: #ece3d5; --paper: #fffdf8; --ink: #2a241d; --muted: #7a6f62; --line: #e4d9c6; --line-strong: #cdbfa6;
  --red: #b8322a; --red-ink: #ffffff; --red-soft: #f8e4df; --gold: #a8842c; --gold-soft: #f4ead2;
  --ok: #2f7a4a; --ok-soft: #e1f0e4; --bad: #c0392b; --bad-soft: #fbe3df; --blue: #2c5d8a; --blue-soft: #e3edf6;
  --chip: #f1e9db; --shadow: 0 1px 0 rgba(60, 40, 10, .04), 0 6px 18px -12px rgba(60, 40, 10, .25);
  --f-ui: "Be Vietnam Pro", system-ui, "Segoe UI", sans-serif;
  --f-zh: "Noto Serif SC", "Songti SC", "SimSun", serif;
  --r: 12px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #17140f; --bg-line: #1d1912; --paper: #221e17; --ink: #efe7da; --muted: #a99c89; --line: #3a3328; --line-strong: #544a3b;
    --red: #e0675c; --red-ink: #1b0f0d; --red-soft: #3d1f1b; --gold: #d9b45a; --gold-soft: #3a3020;
    --ok: #6cc38a; --ok-soft: #1d3324; --bad: #ef7d70; --bad-soft: #3f1f1b; --blue: #7fb1df; --blue-soft: #1c2b3a;
    --chip: #2c271e; --shadow: none; color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #17140f; --bg-line: #1d1912; --paper: #221e17; --ink: #efe7da; --muted: #a99c89; --line: #3a3328; --line-strong: #544a3b;
  --red: #e0675c; --red-ink: #1b0f0d; --red-soft: #3d1f1b; --gold: #d9b45a; --gold-soft: #3a3020;
  --ok: #6cc38a; --ok-soft: #1d3324; --bad: #ef7d70; --bad-soft: #3f1f1b; --blue: #7fb1df; --blue-soft: #1c2b3a;
  --chip: #2c271e; --shadow: none; color-scheme: dark;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; font-family: var(--f-ui); font-size: 15px; line-height: 1.55; color: var(--ink);
  background: var(--bg) repeating-linear-gradient(to bottom, transparent 0 31px, var(--bg-line) 31px 32px);
}
a { color: inherit; }
button { font: inherit; color: inherit; }
.zh, [lang="zh"] { font-family: var(--f-zh); }
:focus-visible { outline: 3px solid var(--gold); outline-offset: 2px; }
[hidden] { display: none !important; }

/* ===== Thanh trên cùng ===== */
.topbar { position: sticky; top: 0; z-index: 20; display: flex; align-items: center; gap: 16px; padding: 10px 16px;
  background: color-mix(in srgb, var(--paper) 92%, transparent); backdrop-filter: blur(6px); border-bottom: 1px solid var(--line); }
.brand { display: flex; align-items: center; gap: 10px; text-decoration: none; font-weight: 700; }
.seal { width: 30px; height: 30px; border-radius: 8px; background: var(--red); color: var(--red-ink); display: grid; place-items: center; font-family: var(--f-zh); font-size: 17px; }
.topnav { display: flex; gap: 4px; margin-left: auto; overflow-x: auto; }
.topnav a { text-decoration: none; padding: 6px 12px; border-radius: 999px; font-size: 14px; color: var(--muted); white-space: nowrap; }
.topnav a:hover { color: var(--ink); background: var(--chip); }
.topnav a.on { color: var(--ink); background: var(--chip); font-weight: 600; }
@media (max-width: 560px) { .brand span:last-child { display: none; } }

.app { max-width: 1320px; margin: 0 auto; padding: 18px 16px 64px; }

/* ===== Khối chung ===== */
.card { background: var(--paper); border: 1px solid var(--line); border-radius: var(--r); box-shadow: var(--shadow); }
.eyebrow { font-size: 11.5px; letter-spacing: .14em; text-transform: uppercase; color: var(--gold); font-weight: 700; margin: 0; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; padding: 9px 18px; border-radius: 999px;
  border: 1.5px solid var(--line-strong); background: var(--paper); cursor: pointer; font-weight: 600; text-decoration: none; min-height: 40px; }
.btn:hover:not(:disabled) { border-color: var(--ink); }
.btn:disabled { opacity: .45; cursor: default; }
.btn.primary { background: var(--red); border-color: var(--red); color: var(--red-ink); }
.btn.primary:hover:not(:disabled) { filter: brightness(1.06); border-color: var(--red); }
.btn.gold { border-color: var(--gold); color: var(--gold); }
.btn.blue { border-color: var(--blue); color: var(--blue); }
.btn.sm { padding: 5px 12px; min-height: 32px; font-size: 13.5px; }
.loading, .error { padding: 40px; text-align: center; color: var(--muted); }
.error { color: var(--bad); }

/* ===== Trang chủ / danh mục ===== */
.hero { padding: 28px clamp(18px, 4vw, 40px); display: grid; gap: 8px; margin-bottom: 18px; }
.hero h1 { margin: 0; font-size: clamp(26px, 4vw, 36px); line-height: 1.2; }
.hero p { margin: 0; color: var(--muted); max-width: 70ch; }
.tiles { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 14px; }
.tile { padding: 20px; display: grid; gap: 8px; text-decoration: none; transition: border-color .15s; align-content: start; }
a.tile:hover { border-color: var(--red); }
.tile h2, .tile h3 { margin: 0; font-size: 19px; }
.tile .zh { color: var(--red); font-size: 18px; }
.tile p { margin: 0; color: var(--muted); font-size: 14px; }
.tile .meta { font-size: 13px; color: var(--gold); font-weight: 600; }
.tile.off { opacity: .55; }
.section-title { margin: 26px 0 10px; font-size: 13px; letter-spacing: .12em; text-transform: uppercase; color: var(--gold); }
.exam-chips { display: flex; flex-wrap: wrap; gap: 8px; }
.exam-chip { padding: 7px 14px; border: 1px solid var(--line); border-radius: 10px; background: var(--paper); text-decoration: none; font-weight: 600; font-size: 14px; }
.exam-chip:hover { border-color: var(--ink); }
.exam-chip small { font-weight: 500; color: var(--muted); margin-left: 6px; }
.exam-chip.done small { color: var(--ok); }

/* ===== Trang làm bài: 3 cột ===== */
.head { padding: 18px 22px; display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 8px 18px; align-items: start; margin-bottom: 14px; }
.head h1 { margin: 2px 0 4px; font-family: var(--f-zh); font-size: clamp(24px, 3vw, 30px); line-height: 1.2; }
.head p { margin: 0; color: var(--muted); font-size: 14px; max-width: 95ch; }
.stamp { width: 74px; height: 74px; border-radius: 50%; border: 2px solid var(--red); color: var(--red); display: grid; place-content: center; text-align: center;
  font-family: var(--f-zh); line-height: 1.05; transform: rotate(-6deg); }
.stamp b { font-size: 20px; }
.stamp span { font-size: 12px; display: block; border-top: 1px solid currentColor; margin-top: 2px; padding-top: 2px; }
.progress { display: flex; align-items: center; gap: 12px; margin: 0 2px 14px; font-size: 13.5px; color: var(--muted); }
.progress .bar { flex: 1; height: 6px; border-radius: 6px; background: var(--line); overflow: hidden; }
.progress .bar i { display: block; height: 100%; background: var(--gold); border-radius: 6px; transition: width .25s; }
.progress .tally { color: var(--ok); font-weight: 600; white-space: nowrap; }
.progress .timer { font-variant-numeric: tabular-nums; font-weight: 700; color: var(--red); white-space: nowrap; }

.layout { display: grid; grid-template-columns: 190px minmax(0, 1fr) 230px; gap: 18px; align-items: start; }
.side { position: sticky; top: 70px; display: grid; gap: 6px; max-height: calc(100vh - 86px); overflow: auto; padding-right: 2px; }
.side h4 { margin: 10px 2px 2px; font-size: 11.5px; letter-spacing: .14em; text-transform: uppercase; color: var(--gold); }
.side h4:first-child { margin-top: 0; }
.side a { display: flex; justify-content: space-between; align-items: center; gap: 6px; padding: 9px 12px; border: 1px solid var(--line); border-radius: 9px; background: var(--paper);
  text-decoration: none; font-weight: 600; font-size: 14px; }
.side a:hover { border-color: var(--ink); }
.side a.on { background: var(--ink); color: var(--paper); border-color: var(--ink); }
.side a small { font-weight: 500; opacity: .7; font-size: 12px; }
.side-select { display: none; }

.main { display: grid; gap: 14px; min-width: 0; }
.audio { padding: 14px 16px; display: grid; gap: 10px; border-color: var(--gold); }
.audio .title { font-family: var(--f-zh); font-weight: 700; font-size: 14.5px; display: flex; align-items: center; gap: 8px; }
.audio audio { width: 100%; height: 40px; }
.audio iframe { width: 100%; height: 64px; border: 0; border-radius: 10px; background: var(--chip); }
.audio .row { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; justify-content: space-between; font-size: 13px; color: var(--muted); }

.qcard { padding: 20px clamp(16px, 2.4vw, 24px); display: grid; gap: 14px; }
.qcard .crumb { font-family: var(--f-zh); font-size: 12.5px; color: var(--gold); font-weight: 700; }
.passage { background: var(--bg); border-radius: 10px; padding: 14px 16px; font-family: var(--f-zh); font-size: 17px; line-height: 2; max-height: 46vh; overflow: auto; }
.passage p { margin: 0 0 .6em; text-indent: 2em; }
.passage p:last-child { margin-bottom: 0; }
.passage .blank { font-family: var(--f-ui); font-size: 13px; font-weight: 700; display: inline-block; min-width: 3.4em; text-align: center; text-indent: 0;
  border-bottom: 2px solid var(--line-strong); color: var(--muted); padding: 0 6px; margin: 0 2px; line-height: 1.4; }
.passage .blank.cur { border-color: var(--red); color: var(--red); background: var(--red-soft); border-radius: 4px 4px 0 0; }
.passage .blank.filled { color: var(--ink); font-family: var(--f-zh); font-size: 15px; }
.qhead { display: flex; gap: 12px; align-items: flex-start; }
.qno { flex: none; width: 30px; height: 30px; border-radius: 50%; background: var(--ink); color: var(--paper); display: grid; place-items: center; font-weight: 700; font-size: 13.5px; }
.qtext { font-family: var(--f-zh); font-size: 17.5px; line-height: 1.7; padding-top: 1px; white-space: pre-line; }
.qtext.long { font-size: 16.5px; line-height: 1.9; }
.opts { display: grid; gap: 10px; }
.opt { display: flex; align-items: center; gap: 12px; padding: 12px 14px; border: 1.5px solid var(--line); border-radius: 10px; background: var(--paper); cursor: pointer; text-align: left; width: 100%; }
.opt:hover:not(:disabled) { border-color: var(--line-strong); background: var(--bg); }
.opt .l { flex: none; width: 26px; height: 26px; border-radius: 50%; border: 1.5px solid var(--line-strong); display: grid; place-items: center; font-size: 12.5px; font-weight: 700; color: var(--muted); font-family: var(--f-zh); }
.opt .w { font-family: var(--f-zh); font-size: 17px; }
.opt.picked { border-color: var(--ink); background: var(--bg); }
.opt.picked .l { background: var(--ink); color: var(--paper); border-color: var(--ink); }
.opt.ok { border-color: var(--ok); background: var(--ok-soft); }
.opt.ok .l { background: var(--ok); border-color: var(--ok); color: #fff; }
.opt.bad { border-color: var(--bad); background: var(--bad-soft); }
.opt.bad .l { background: var(--bad); border-color: var(--bad); color: #fff; }
.opt:disabled { cursor: default; }
.opts.tf { grid-template-columns: 1fr 1fr; }
.opts.tf .opt { justify-content: center; font-weight: 600; }

.verdict { font-size: 14px; font-weight: 600; padding: 10px 14px; border-radius: 10px; }
.verdict.ok { background: var(--ok-soft); color: var(--ok); }
.verdict.bad { background: var(--bad-soft); color: var(--bad); }
.verdict.info { background: var(--gold-soft); color: var(--gold); }
.verdict .zh { font-weight: 700; }
.extras { display: flex; gap: 10px; flex-wrap: wrap; }
.panel { background: var(--bg); border-radius: 10px; padding: 12px 14px; font-size: 14.5px; }
.panel h5 { margin: 0 0 6px; font-size: 12.5px; text-transform: uppercase; letter-spacing: .1em; color: var(--muted); }
.panel .zh { font-size: 16.5px; line-height: 1.9; white-space: pre-line; }
.panel h5 + * { margin-bottom: 10px; }
.panel > :last-child { margin-bottom: 0; }
.btn.green { border-color: var(--ok); color: var(--ok); }
.btn.gold.on { background: var(--gold); border-color: var(--gold); color: var(--paper); }
.btn.blue.on { background: var(--blue); border-color: var(--blue); color: var(--paper); }
.btn.green.on { background: var(--ok); border-color: var(--ok); color: var(--paper); }
.vi-text { white-space: pre-line; line-height: 1.7; }
.vi-opts, .gram { margin: 0; padding-left: 0; list-style: none; display: grid; gap: 6px; }
.vi-opts li { padding: 4px 8px; border-radius: 6px; }
.vi-opts li.key { background: var(--ok-soft); }
.vi-opts .zh { font-size: 15.5px; white-space: normal; }
.gram li { padding-left: 14px; position: relative; line-height: 1.65; }
.gram li::before { content: "•"; position: absolute; left: 2px; color: var(--gold); }
.gram .zh { font-size: 15.5px; color: var(--red); white-space: normal; }
.panel .vocab { margin-top: 0; }
.panel .vocab div { background: var(--paper); }
.vocab .py { display: block; font-size: 12.5px; color: var(--muted); font-style: italic; }

/* Sắp xếp câu */
.tray { min-height: 54px; display: flex; flex-wrap: wrap; gap: 8px; padding: 10px; border: 1.5px dashed var(--line-strong); border-radius: 10px; align-content: flex-start; }
.tray.answer { background: var(--bg); }
.tray .ph { color: var(--muted); font-size: 13.5px; align-self: center; }
.word { font-family: var(--f-zh); font-size: 17px; padding: 6px 12px; border-radius: 8px; border: 1.5px solid var(--line-strong); background: var(--paper); cursor: pointer; }
.word:hover:not(:disabled) { border-color: var(--ink); }
.word.used { visibility: hidden; }
.word:disabled { cursor: default; }
/* Viết đoạn văn */
.essay-words { display: flex; flex-wrap: wrap; gap: 8px; }
.essay-words span { font-family: var(--f-zh); font-size: 16px; padding: 4px 12px; border-radius: 999px; background: var(--chip); border: 1.5px solid transparent; }
.essay-words span.used { border-color: var(--ok); color: var(--ok); background: var(--ok-soft); }
textarea.essay { width: 100%; min-height: 170px; resize: vertical; padding: 12px 14px; border-radius: 10px; border: 1.5px solid var(--line-strong); background: var(--paper);
  font-family: var(--f-zh); font-size: 17px; line-height: 1.9; color: var(--ink); }
.essay-img { max-width: min(100%, 420px); border-radius: 10px; border: 1px solid var(--line); display: block; }
.counter { font-size: 13px; color: var(--muted); }
.counter b.ok { color: var(--ok); }
/* Điền từ */
.fill-text { font-family: var(--f-zh); font-size: 17.5px; line-height: 2.3; }
.fill-text input { font-family: var(--f-zh); font-size: 16px; width: 7em; padding: 2px 8px; margin: 0 3px; border: 0; border-bottom: 2px solid var(--line-strong); background: var(--bg); color: var(--ink); border-radius: 4px 4px 0 0; }
.fill-text input.ok { border-color: var(--ok); background: var(--ok-soft); }
.fill-text input.bad { border-color: var(--bad); background: var(--bad-soft); }

.navrow { display: flex; justify-content: space-between; gap: 10px; }
details.more { padding: 14px 18px; }
details.more summary { cursor: pointer; color: var(--red); font-weight: 600; }
.vocab { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 8px; margin-top: 10px; }
.vocab div { border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; font-size: 13.5px; }
.vocab .zh { font-size: 18px; color: var(--red); display: block; }

/* Lưới câu bên phải */
.navpanel { position: sticky; top: 70px; display: grid; gap: 12px; }
.grid-card { padding: 12px 12px 6px; max-height: calc(100vh - 170px); overflow: auto; }
.grid-sec { border-bottom: 1px solid var(--line); padding: 6px 0 10px; }
.grid-sec:last-child { border-bottom: 0; }
.grid-sec > button { width: 100%; display: flex; justify-content: space-between; align-items: center; background: none; border: 0; padding: 4px 2px; cursor: pointer;
  font-family: var(--f-zh); font-weight: 700; font-size: 15px; color: var(--ink); }
.grid-sec > button .sum { font-family: var(--f-ui); font-weight: 500; font-size: 12px; color: var(--muted); }
.grid-sec > button::after { content: "▸"; font-size: 11px; color: var(--muted); margin-left: 6px; }
.grid-sec.open > button::after { content: "▾"; }
.grid-sec:not(.open) .grid-body { display: none; }
.grid-part { font-family: var(--f-zh); font-size: 12.5px; color: var(--muted); margin: 8px 2px 4px; }
.nums { display: grid; grid-template-columns: repeat(7, 1fr); gap: 5px; }
.num { aspect-ratio: 1; border-radius: 50%; border: 1px solid var(--line-strong); background: var(--paper); font-size: 11.5px; cursor: pointer; padding: 0; display: grid; place-items: center; color: var(--muted); font-family: var(--f-zh); }
.num:hover { border-color: var(--ink); color: var(--ink); }
.num.done { background: var(--chip); color: var(--ink); border-color: var(--line-strong); }
.num.ok { background: var(--ok-soft); color: var(--ok); border-color: var(--ok); }
.num.bad { background: var(--bad-soft); color: var(--bad); border-color: var(--bad); }
.num.cur { box-shadow: 0 0 0 2px var(--paper), 0 0 0 4px var(--ink); color: var(--ink); font-weight: 700; }
.legend { display: flex; flex-wrap: wrap; gap: 4px 12px; font-size: 12px; color: var(--muted); padding: 8px 2px 4px; }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 50%; margin-right: 4px; vertical-align: -1px; border: 1px solid var(--line-strong); }
.legend .ok { background: var(--ok-soft); border-color: var(--ok); }
.legend .bad { background: var(--bad-soft); border-color: var(--bad); }
.legend .done { background: var(--chip); }
.mode-btn { width: 100%; padding: 13px; border-radius: 10px; border: 1.5px solid var(--red); background: var(--red-soft); color: var(--red); font-weight: 600; cursor: pointer; }
.mode-btn:hover { filter: brightness(.98); }
.mode-btn.solid { background: var(--red); color: var(--red-ink); }
.result { padding: 16px 18px; display: grid; gap: 8px; border-color: var(--ok); }
.result h3 { margin: 0; font-size: 17px; }
.result .scores { display: flex; flex-wrap: wrap; gap: 8px 20px; font-size: 14px; }
.result .scores b { font-size: 18px; color: var(--ok); }

/* Sổ tay */
.list { display: grid; gap: 8px; }
.list a { display: flex; gap: 12px; align-items: baseline; padding: 10px 14px; text-decoration: none; }
.list a:hover { border-color: var(--ink); }
.list .tag { font-size: 12px; font-weight: 700; color: var(--bad); white-space: nowrap; }
.empty { padding: 30px; text-align: center; color: var(--muted); }

/* ===== Màn hình nhỏ ===== */
@media (max-width: 1100px) {
  .layout { grid-template-columns: 170px minmax(0, 1fr); }
  .navpanel { grid-column: 1 / -1; position: static; }
  .grid-card { max-height: none; }
  .nums { grid-template-columns: repeat(auto-fill, minmax(30px, 1fr)); }
}
@media (max-width: 760px) {
  .layout { grid-template-columns: minmax(0, 1fr); }
  .side { display: none; }
  .side-select { display: block; width: 100%; padding: 10px 12px; border-radius: 10px; border: 1px solid var(--line-strong); background: var(--paper); color: var(--ink); font: inherit; font-weight: 600; }
  .head { grid-template-columns: minmax(0, 1fr) auto; padding: 14px 16px; }
  .stamp { width: 58px; height: 58px; }
  .stamp b { font-size: 16px; }
  .progress { flex-wrap: wrap; }
  .progress .bar { order: 3; flex-basis: 100%; }
  .opts.tf { grid-template-columns: 1fr; }
}
@media (prefers-reduced-motion: reduce) { * { transition: none !important; } }

/* HSK 1–2: câu có hình, pinyin, phương án chỉ có chữ cái */
.py-line { display: block; font-family: var(--f-ui); font-size: 13.5px; color: var(--muted); letter-spacing: .01em; }
.opt .w .py { display: block; font-family: var(--f-ui); font-size: 12.5px; color: var(--muted); }
.q-img { display: block; max-width: min(100%, 480px); max-height: 320px; object-fit: contain; margin: 12px 0 14px 40px; border-radius: 10px; border: 1px solid var(--line); background: #fff; }
.q-img.wide { max-width: min(100%, 560px); max-height: 460px; }
.opts.letters { grid-template-columns: repeat(auto-fill, minmax(64px, 1fr)); }
.opts.letters .opt { justify-content: center; padding: 10px; }
.opts.short { grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); }
@media (max-width: 560px) { .q-img { margin-left: 0; } }

/* HSK 3–4: viết chữ theo pinyin, xếp vế A B C, đặt câu theo tranh (chép từ hoc/assets/app.css) */
.fill-line { font-size: 19px; line-height: 2.6; margin: 6px 0 4px; }
.fill-slot { display: inline-flex; flex-direction: column; align-items: center; vertical-align: bottom; margin: 0 4px; line-height: 1.2; }
.fill-slot .py { font-family: var(--f-ui); font-size: 13px; color: var(--muted); }
.fill-slot input { width: 3.2em; text-align: center; font: inherit; font-family: var(--f-zh); font-size: 19px; padding: 2px 4px; border: 0; border-bottom: 2px solid var(--line-strong); background: transparent; color: var(--ink); }
.fill-slot input:focus { outline: none; border-bottom-color: var(--ink); }
.fill-slot input.ok { border-bottom-color: var(--ok); color: var(--ok); }
.fill-slot input.bad { border-bottom-color: var(--bad); color: var(--bad); }
.word.sent { text-align: left; font-size: 16px; }
.word .wl { font-family: var(--f-ui); color: var(--muted); margin-right: 2px; }
textarea.essay.short { min-height: 80px; }
