/* Font Khải chính cho chữ Hán: 文鼎PL简中楷 (Arphic Public License), chuẩn tự hình GB, cũng là font gốc
   của dữ liệu nét hanzi-writer. Một file ~2,3 MB (jsDelivr nén brotli), chỉ có đúng 6763 chữ GB2312 →
   chữ ngoài bảng rơi xuống LXGW WenKai GB (index.html, chia mảnh). Service worker cache cả hai. */
@font-face {
  font-family: "AR PL KaitiM GB";
  src: url("https://cdn.jsdelivr.net/npm/@fontpkg/ar-pl-kaiti-m-gb@2.11.0/gkai00mp.ttf") format("truetype");
  font-display: swap;
}
:root {
  /* Font Khải có sẵn trên máy dùng tạm khi web font chưa tải xong */
  --font-kai: "AR PL KaitiM GB", "LXGW WenKai GB", "KaiTi", "STKaiti", "Kaiti SC", "BiauKai", "Noto Serif SC", serif;
  --bg: #f6f7f6;
  --surface: #ffffff;
  --fg: #1f2933;
  --muted: #66727f;
  --line: #e2e6e4;
  --accent: #1f8a70;
  --accent-fg: #ffffff;
  --accent-soft: #e3f3ee;
  --danger: #c0392b;
  --danger-soft: #fbeae8;
  --warn: #9a6b00;
  --warn-soft: #fff4d6;
  /* Màu nét chữ cho hanzi-writer — phải là mã hex (#rrggbb) vì thư viện chỉ đọc hex/rgb */
  --ink: #1f2933;
  --stroke-outline: #d3dad7;
  --stroke-hint: #4f8fe8;
  --radius: 12px;
  /* Hệ số cỡ chữ trên thẻ ôn (textsize.js đặt lại theo lựa chọn của người dùng) */
  --card-scale: 1;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121615;
    --surface: #1c2220;
    --fg: #e6ebe9;
    --muted: #93a09b;
    --line: #2c3532;
    --accent: #3cb393;
    --accent-fg: #0d1513;
    --accent-soft: #1d3530;
    --danger: #ef7d70;
    --danger-soft: #3a2220;
    --warn: #e9c46a;
    --warn-soft: #3a3220;
    --ink: #e6ebe9;
    --stroke-outline: #3d4945;
    --stroke-hint: #6fa8ff;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--fg);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "PingFang SC", "Microsoft YaHei", sans-serif;
  line-height: 1.5;
  -webkit-tap-highlight-color: transparent;
}

a { color: var(--accent); }

/* ── Thanh trên ── */
.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: calc(8px + env(safe-area-inset-top)) 16px 8px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
}
.brand {
  font-size: 1.25rem;
  font-weight: 700;
  color: var(--fg);
  text-decoration: none;
}
.brand span { font-weight: 400; color: var(--muted); font-size: 1rem; }
.icon-btn {
  display: grid;
  place-items: center;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  font-size: 1.3rem;
  color: var(--fg);
  text-decoration: none;
}
.icon-btn:hover { background: var(--bg); }

main {
  max-width: 520px;
  margin: 0 auto;
  padding: 16px 16px calc(32px + env(safe-area-inset-bottom));
}

h2 { font-size: 1.1rem; margin: 24px 0 8px; }
h2.flush { margin-top: 0; }
.back-link { display: inline-block; margin-bottom: 12px; text-decoration: none; font-weight: 600; }
.muted { color: var(--muted); }
.small { font-size: 0.875rem; }
.center { text-align: center; }

/* ── Khối nội dung ── */
.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 12px;
}

.hero {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}
.hero .big { font-size: 2.5rem; font-weight: 700; line-height: 1; }
.streak { font-size: 1.1rem; white-space: nowrap; }

.stats {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 8px;
  margin: 0;
}
.stat {
  background: var(--bg);
  border-radius: 8px;
  padding: 10px 12px;
}
.stat dt { font-size: 0.85rem; color: var(--muted); }
.stat dd { margin: 0; font-size: 1.05rem; }
.stat dd b { font-size: 1.4rem; }
@media (max-width: 340px) {
  .stats { grid-template-columns: 1fr; }
}

/* ── Nút ── */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 44px;
  padding: 0 18px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--surface);
  color: var(--fg);
  font: inherit;
  font-weight: 600;
  text-decoration: none;
  cursor: pointer;
}
.btn:disabled { opacity: 0.55; cursor: not-allowed; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-fg); }
.btn.block { display: flex; width: 100%; }
.row { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.row.spread { justify-content: space-between; }

/* ── Thông báo ── */
.notice {
  border-radius: 8px;
  padding: 10px 12px;
  margin: 12px 0;
  font-size: 0.9rem;
}
.notice.warn { background: var(--warn-soft); color: var(--warn); }
.notice.error { background: var(--danger-soft); color: var(--danger); }
.notice.ok { background: var(--accent-soft); color: var(--accent); }
details summary { cursor: pointer; }
details ul { margin: 8px 0 0; padding-left: 20px; }

/* ── Form ── */
.field { margin-bottom: 14px; }
.field label { display: block; font-weight: 600; margin-bottom: 4px; }
.field .hint { font-size: 0.85rem; color: var(--muted); margin-top: 4px; }
.field input[type="text"],
.field input[type="url"],
.field input[type="password"],
.field input[type="number"] {
  width: 100%;
  min-height: 44px;
  padding: 8px 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--bg);
  color: var(--fg);
  font: inherit;
}
.field input:focus { outline: 2px solid var(--accent); outline-offset: 1px; }
.check { display: flex; gap: 8px; align-items: center; font-weight: 600; }
.check input { width: 20px; height: 20px; accent-color: var(--accent); }
.input-row { display: flex; gap: 8px; }
.input-row input { flex: 1; }

/* ── Toast ── */
#toast {
  position: fixed;
  left: 50%;
  bottom: calc(24px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  max-width: calc(100% - 32px);
  padding: 10px 16px;
  border-radius: 999px;
  background: var(--fg);
  color: var(--bg);
  font-size: 0.9rem;
  box-shadow: 0 4px 16px rgb(0 0 0 / 0.2);
}
#toast.error { background: var(--danger); color: #fff; }

/* ── Thanh báo có phiên bản mới (service worker) ── */
.update-bar {
  position: fixed;
  left: 16px;
  right: 16px;
  bottom: calc(16px + env(safe-area-inset-bottom));
  z-index: 20;
  display: flex;
  gap: 12px;
  align-items: center;
  justify-content: space-between;
  max-width: 480px;
  margin: 0 auto;
  padding: 10px 12px 10px 16px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: 0 4px 16px rgb(0 0 0 / 0.2);
}
.update-bar[hidden] { display: none; }

/* ── Tùy chọn lượt ôn ── */
fieldset { border: 0; margin: 0 0 16px; padding: 0; }
legend { font-weight: 600; margin-bottom: 8px; padding: 0; }
.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip { position: relative; cursor: pointer; }
.chip input { position: absolute; opacity: 0; pointer-events: none; }
.chip span {
  display: inline-block;
  min-height: 36px;
  padding: 6px 14px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--bg);
}
.chip input:checked + span { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); font-weight: 600; }
.chip input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 2px; }
.chip.disabled { cursor: not-allowed; opacity: 0.5; }

/* ── Màn hình ôn ── */
.review-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 8px;
}
.icon-btn { border: 0; background: transparent; cursor: pointer; font: inherit; font-size: 1.3rem; }
.counter { font-variant-numeric: tabular-nums; color: var(--muted); }
.top-right { display: flex; align-items: center; gap: 4px; }
#text-size { font-family: Georgia, serif; font-size: 1.1rem; min-width: 2.2em; }
#text-size[aria-expanded="true"] { color: var(--accent); }
.size-pop { margin-bottom: 8px; justify-content: center; }
.size-pop[hidden] { display: none; }
/* Cỡ chữ trên thẻ: cỡ gốc từng loại thẻ × --card-scale. Thẻ là container để giới hạn theo bề ngang thẻ. */
.flash, .writing { container-type: inline-size; }
.flash {
  text-align: center;
  padding: 20px 16px 24px;
  cursor: pointer;
  /* Tắt độ trễ và phóng to khi chạm 2 lần trên iPhone */
  touch-action: manipulation;
}
.flash:active { background: var(--bg); }
.flash-meta { display: flex; gap: 6px; justify-content: center; margin-bottom: 8px; }
.tag {
  font-size: 0.75rem;
  padding: 2px 10px;
  border-radius: 999px;
  background: var(--bg);
  color: var(--muted);
}
.tag.new { background: var(--warn-soft); color: var(--warn); }
.hanzi-big {
  font-family: var(--font-kai);
  /* Không vượt bề ngang thẻ chia cho số chữ (--chars đặt trên phần tử), tối thiểu 2rem rồi xuống dòng */
  font-size: max(2rem, min(calc(clamp(3.5rem, 18vw, 5.5rem) * var(--card-scale)), calc(95cqi / var(--chars, 1))));
  line-height: 1.2;
}
/* Chữ thường trong đáp án (pinyin, Hán Việt, nghĩa ở thẻ Đọc) phóng theo; câu ví dụ giữ cỡ cũ */
.answer { margin-top: 12px; border-top: 1px solid var(--line); padding-top: 12px; font-size: calc(1rem * var(--card-scale)); }
.pinyin-big { font-size: calc(2rem * var(--card-scale)); color: var(--accent); margin-bottom: 4px; }
.meaning { font-size: calc(1.4rem * var(--card-scale)); font-weight: 600; margin-bottom: 4px; }
.example { margin-top: 14px; font-size: 1rem; }
.example-py { font-size: 0.875rem; color: var(--accent); }
.extra { margin-top: 8px; }
.link-btn { border: 0; background: none; cursor: pointer; font-size: 1rem; padding: 0 4px; }
.actions { margin-top: 12px; }
.ratings { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; }
.ratings.two { grid-template-columns: repeat(2, 1fr); }
.more-box > .row + .row { margin-top: 10px; }
.btn.rate {
  flex-direction: column;
  gap: 0;
  min-height: 56px;
  padding: 6px 4px;
  border-radius: 12px;
  line-height: 1.2;
}
.btn.rate small { font-weight: 400; font-size: 0.75rem; opacity: 0.85; }
.btn.rate.r1 { background: var(--danger-soft); border-color: var(--danger); color: var(--danger); }
.btn.rate.r2 { background: var(--warn-soft); border-color: var(--warn); color: var(--warn); }
.btn.rate.r3 { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); }
.btn.rate.r4 { background: var(--surface); border-color: var(--line); }
.btn.rate.suggested { border-width: 3px; box-shadow: 0 0 0 2px var(--surface), 0 0 0 4px currentColor; }

/* ── Thẻ Viết ── */
.writing { text-align: center; padding: 16px 12px 12px; }
.word-slots { display: flex; justify-content: center; gap: 10px; flex-wrap: wrap; margin-bottom: 6px; }
.slot {
  display: flex;
  flex-direction: column;
  align-items: center;
  min-width: calc(56px * var(--card-scale));
  padding: 4px 6px;
  border-radius: 8px;
}
.slot-hanzi {
  display: grid;
  place-items: center;
  width: calc(48px * var(--card-scale));
  height: calc(48px * var(--card-scale));
  font-family: var(--font-kai);
  font-size: calc(2.4rem * var(--card-scale));
  line-height: 1;
  border-bottom: 2px solid var(--line);
}
.slot.current .slot-hanzi { border-bottom-color: var(--accent); }
.slot.done .slot-hanzi { border-bottom-color: transparent; }
.slot-py { font-size: calc(1rem * var(--card-scale)); color: var(--accent); }
.slot-hv { font-size: calc(0.8rem * var(--card-scale)); color: var(--muted); }
.writing .meaning { font-size: calc(1.15rem * var(--card-scale)); }
.pad-wrap {
  position: relative;
  /* Vừa màn hình điện thoại thấp: không quá 45% chiều cao để thấy được cả nút bên dưới */
  width: min(100%, 320px, 45vh);
  aspect-ratio: 1;
  margin: 8px auto;
  border-radius: 8px;
  background: var(--bg);
  /* Vẽ không làm cuộn/phóng trang, không hiện menu khi giữ ngón tay */
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}
.pad-grid {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  fill: none;
  stroke: var(--line);
  stroke-width: 0.6;
  stroke-dasharray: 2 2;
}
.pad-grid rect { stroke-dasharray: none; }
.pad { position: absolute; inset: 0; touch-action: none; }
.pad svg { display: block; }
.pad-tools { justify-content: center; }
.pad-tools[hidden] { display: none; }

/* ── Thẻ Thanh điệu (dùng chung khung .writing, .word-slots, .pad-wrap với thẻ Viết) ── */
.slot-py.good { color: var(--accent); font-weight: 600; }
.slot-py.bad { color: var(--danger); font-weight: 600; }
.slot-py.hint, .slot-py.skip { color: var(--muted); }
.tone-svg { position: absolute; inset: 0; width: 100%; height: 100%; touch-action: none; }
.tone-svg .guide { stroke: var(--line); stroke-width: 0.6; stroke-dasharray: 2 2; }
#tone-stroke {
  fill: none;
  stroke: var(--ink);
  stroke-width: 4;
  stroke-linecap: round;
  stroke-linejoin: round;
  vector-effect: non-scaling-stroke;
}
#tone-stroke.good { stroke: var(--accent); }
#tone-stroke.bad { stroke: var(--danger); }
.tone-feedback { min-height: 1.5em; }
.tone-feedback .good { color: var(--accent); }
.tone-feedback .bad { color: var(--danger); }
.tone-buttons { display: grid; grid-template-columns: repeat(6, 1fr); gap: 6px; margin-top: 4px; }
.tone-buttons[hidden] { display: none; }
.btn.tone-btn { flex-direction: column; gap: 0; min-height: 52px; padding: 4px 0; border-radius: 12px; line-height: 1.1; }
.tone-mark { font-size: 1.4rem; font-weight: 700; }
.btn.tone-btn small { font-weight: 400; font-size: 0.7rem; color: var(--muted); }
kbd {
  font: inherit;
  font-size: 0.7rem;
  padding: 0 4px;
  border: 1px solid currentColor;
  border-radius: 4px;
  opacity: 0.6;
}
@media (hover: none) { kbd { display: none; } }

/* ── Báo cáo ── */
.big.good { color: var(--accent); }
.big.bad { color: var(--danger); }
.word-list { list-style: none; margin: 0; padding: 0; }
.word-list li { display: flex; gap: 8px; align-items: baseline; flex-wrap: wrap; padding: 6px 0; border-bottom: 1px solid var(--line); }
.word-list li:last-child { border-bottom: 0; }
.hanzi-sm { font-family: var(--font-kai); font-size: 1.4rem; }
