/* ═══ 色彩（跟隨系統淺色／深色） ═══ */
:root {
  --bg: #f7f4ef;
  --surface: #ffffff;
  --surface-2: #efebe4;
  --text: #22201c;
  --muted: #6f6a62;
  --line: #ddd6cb;
  --focus: #2f6fdb;

  /* 平假名：朱色系 */
  --hira: #c4462f;
  --hira-soft: #fbe7e2;
  /* 片假名：藍色系 */
  --kata: #2f5aa8;
  --kata-soft: #e3ebf9;

  --radius: 12px;
  --tabbar-h: 60px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #17181c;
    --surface: #212329;
    --surface-2: #2b2e35;
    --text: #ece9e3;
    --muted: #a19d95;
    --line: #3a3d45;
    --focus: #7aa7ff;
    --hira: #f08770;
    --hira-soft: #3d2622;
    --kata: #87a9f0;
    --kata-soft: #222d43;
  }
}

/* ═══ 字體：只用系統字體；日文元素強制用日文字體 ═══ */
html {
  font-family: -apple-system, "PingFang TC", "Microsoft JhengHei", "Noto Sans TC", "Noto Sans CJK TC", sans-serif;
  -webkit-text-size-adjust: 100%;
}
:lang(ja) {
  font-family: "Hiragino Sans", "Hiragino Kaku Gothic ProN", "Yu Gothic UI", "Yu Gothic", YuGothic, Meiryo,
               "Noto Sans JP", "Noto Sans CJK JP", "Source Han Sans JP", sans-serif;
}

*, *::before, *::after { box-sizing: border-box; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  padding-bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom));
}
button { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }

/* ═══ 版面 ═══ */
.topbar {
  position: sticky; top: 0; z-index: 5;
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
  background: var(--bg);
  border-bottom: 1px solid var(--line);
}
.brand { color: var(--text); text-decoration: none; font-weight: 700; font-size: 1.1rem; }
.brand [lang="ja"] { color: var(--hira); }
.brand .brand-kata { color: var(--kata); margin-right: 4px; }

.view { max-width: 760px; margin: 0 auto; padding: 16px; outline: none; }

.notices { max-width: 760px; margin: 0 auto; padding: 0 16px; }
.notice {
  display: flex; gap: 8px; align-items: flex-start;
  margin-top: 10px; padding: 10px 12px;
  background: var(--surface-2); border-radius: var(--radius);
  font-size: .9rem; color: var(--text);
}
.notice span { flex: 1; }
.notice-close { background: none; border: 0; cursor: pointer; color: var(--muted); padding: 0 4px; font-size: 1rem; }

/* 底部分頁列 */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 10;
  display: flex;
  height: calc(var(--tabbar-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  background: var(--surface); border-top: 1px solid var(--line);
}
.tabbar a {
  flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  color: var(--muted); text-decoration: none; font-size: .72rem;
}
.tab-icon { font-size: 1.15rem; line-height: 1; }
.tabbar a[aria-current="page"] { color: var(--text); font-weight: 700; }
.tabbar a[aria-current="page"] .tab-icon { color: var(--hira); }

@media (min-width: 900px) {
  .tabbar { position: sticky; top: 0; height: auto; padding: 0; max-width: 760px; margin: 0 auto; border: 0; background: transparent; }
  body { padding-bottom: 24px; }
  .tabbar { width: 100%; }
  .tabbar a { flex-direction: row; gap: 6px; padding: 12px 0; font-size: .9rem; white-space: nowrap; }
  /* 桌面版把分頁列放在頂端 */
  body { display: flex; flex-direction: column; }
  .topbar { order: 0; } .tabbar { order: 1; border-bottom: 1px solid var(--line); background: var(--bg); top: 0; z-index: 6; }
  .notices { order: 2; } .view { order: 3; } noscript { order: 4; }
  .topbar { position: static; border-bottom: 0; }
}

/* ═══ 共用控制項 ═══ */
.controls { display: flex; flex-wrap: wrap; gap: 10px 14px; align-items: center; margin-bottom: 12px; }

.seg { display: inline-flex; background: var(--surface-2); border-radius: 999px; padding: 3px; }
.seg-btn {
  border: 0; background: transparent; padding: 7px 14px; border-radius: 999px; cursor: pointer;
  color: var(--muted); font-size: .92rem; min-height: 36px;
}
.seg-btn[aria-checked="true"] { background: var(--surface); color: var(--text); font-weight: 700; box-shadow: 0 1px 3px rgba(0,0,0,.12); }
.seg-btn[aria-checked="true"][data-script="hira"] { color: var(--hira); }
.seg-btn[aria-checked="true"][data-script="kata"] { color: var(--kata); }

.chips { display: inline-flex; flex-wrap: wrap; gap: 6px; }
.chip {
  border: 1px solid var(--line); background: var(--surface); color: var(--muted);
  padding: 6px 12px; border-radius: 999px; cursor: pointer; font-size: .88rem; min-height: 34px;
}
.chip[aria-pressed="true"] { border-color: var(--text); color: var(--text); background: var(--surface-2); font-weight: 600; }
.chip[aria-pressed="true"]::before { content: "✓ "; }

.switch { display: inline-flex; align-items: center; gap: 6px; font-size: .9rem; color: var(--muted); cursor: pointer; }
.switch input { width: 18px; height: 18px; accent-color: var(--hira); }

.icon-btn {
  border: 1px solid var(--line); background: var(--surface); border-radius: 999px;
  width: 44px; height: 44px; cursor: pointer; font-size: 1.2rem; flex: none;
}

/* ═══ 詳細資訊列 ═══ */
.detail {
  display: flex; align-items: center; gap: 14px;
  min-height: 76px; padding: 10px 14px; margin-bottom: 14px;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  position: sticky; top: calc(52px + env(safe-area-inset-top)); z-index: 4;
}
.detail-hint { color: var(--muted); font-size: .92rem; }
.detail-kana { display: flex; gap: 10px; font-size: 2.6rem; line-height: 1; }
.detail-info { flex: 1; }
.detail-romaji { font-size: 1.4rem; font-weight: 700; letter-spacing: .02em; }
.detail-meta { color: var(--muted); font-size: .85rem; }
@media (min-width: 900px) { .detail { top: 50px; } }

/* ═══ 五十音表 ═══ */
.kana-section { margin-bottom: 22px; }
.kana-section h2 { font-size: 1rem; margin: 0 0 8px; color: var(--muted); font-weight: 600; }
.kana-grid { display: flex; flex-direction: column; gap: 6px; }
.kana-row { display: grid; grid-template-columns: repeat(var(--cols, 5), 1fr); gap: 6px; }

.cell {
  position: relative;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  aspect-ratio: 1 / 1; min-height: 52px; padding: 2px;
  border: 1px solid var(--line); border-radius: 10px;
  background: var(--surface); cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  transition: transform .08s ease, border-color .15s;
}
.cell:active { transform: scale(.96); }
.cell.empty { border: 1px dashed var(--line); background: transparent; cursor: default; opacity: .5; }
.cell.active { border-color: var(--text); border-width: 2px; }

.cell-kana { display: flex; gap: 4px; align-items: baseline; font-size: clamp(1.5rem, 7vw, 2.1rem); line-height: 1.1; }
.cell-romaji { font-size: .78rem; color: var(--muted); height: 1.1em; visibility: hidden; }
.cell.revealed .cell-romaji { visibility: visible; }

.k-hira { color: var(--hira); }
.k-kata { color: var(--kata); }

/* 依文字種類換底色 */
.tables[data-script="hira"] .cell:not(.empty) { background: var(--hira-soft); border-color: transparent; }
.tables[data-script="kata"] .cell:not(.empty) { background: var(--kata-soft); border-color: transparent; }
.tables .cell.active { border-color: var(--text) !important; }
.tables[data-script="both"] .cell-kana { font-size: clamp(1.15rem, 5vw, 1.7rem); }

/* ═══ 其他 ═══ */
.placeholder { text-align: center; color: var(--muted); padding: 48px 16px; }
.placeholder h2 { color: var(--text); }

@media (prefers-reduced-motion: reduce) { * { transition: none !important; } }

/* ═══ 第 2 階段：按鈕、表單 ═══ */
:root { --ok: #2e8b57; --ok-soft: #e1f3e8; --bad: #c8382b; --bad-soft: #fbe3e0; }
@media (prefers-color-scheme: dark) {
  :root { --ok: #5fcf8e; --ok-soft: #1f3a2b; --bad: #ff8a7e; --bad-soft: #432422; }
}

.page-title { font-size: 1.3rem; margin: 4px 0 14px; }
.section-title { font-size: 1rem; margin: 20px 0 10px; color: var(--muted); font-weight: 600; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 46px; padding: 10px 18px; border-radius: var(--radius);
  border: 1px solid var(--line); background: var(--surface); color: var(--text);
  font-size: 1rem; font-weight: 600; cursor: pointer;
}
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn-primary { background: var(--text); color: var(--bg); border-color: var(--text); }
.btn-ghost { background: transparent; border-color: transparent; color: var(--muted); }
.btn-block { display: flex; width: 100%; }
.link-btn { background: none; border: 0; color: var(--focus); cursor: pointer; padding: 6px 4px; font-size: .9rem; }
.icon-btn-sm { width: 34px; height: 34px; font-size: .95rem; margin-left: 6px; vertical-align: middle; }

.form { display: flex; flex-direction: column; gap: 18px; }
.field { display: flex; flex-direction: column; gap: 8px; align-items: flex-start; }
.field .btn-block { margin-top: 4px; }
.field-label { font-size: .9rem; color: var(--muted); font-weight: 600; }
.field-head { display: flex; align-items: center; justify-content: space-between; width: 100%; margin-bottom: 8px; }
.form-error { color: var(--bad); margin: 0; font-size: .9rem; min-height: 0; }
.form-error:empty { display: none; }
.chip[lang="ja"]::before { font-family: -apple-system, "PingFang TC", sans-serif; }
.chip-plain[aria-pressed="true"]::before { content: none; }
.chip-plain[aria-pressed="true"] { background: var(--text); color: var(--bg); border-color: var(--text); }

/* ═══ 分行學習 ═══ */
.row-picker { display: flex; flex-direction: column; gap: 8px; margin-bottom: 16px; }
.row-picker-group { display: flex; gap: 10px; align-items: flex-start; }
.row-picker-label { flex: none; width: 3.4em; padding-top: 7px; font-size: .85rem; color: var(--muted); }

.learn-stage { display: flex; flex-direction: column; gap: 14px; }
.learn-card {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 6px;
  min-height: 260px; padding: 20px; cursor: pointer;
  background: var(--surface); border: 1px solid var(--line); border-radius: 20px;
  -webkit-tap-highlight-color: transparent;
}
.learn-card:active { transform: scale(.99); }
.learn-kana { display: flex; gap: 24px; font-size: clamp(5rem, 30vw, 8rem); line-height: 1.05; }
.learn-romaji { font-size: 1.8rem; font-weight: 700; letter-spacing: .03em; }
.learn-tap { font-size: .8rem; color: var(--muted); }
.learn-nav { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.learn-pos { color: var(--muted); font-variant-numeric: tabular-nums; }
.learn-strip { display: grid; grid-template-columns: repeat(5, 1fr); gap: 6px; }
.strip-cell {
  min-height: 48px; font-size: 1.5rem; border: 1px solid var(--line); border-radius: 10px;
  background: var(--surface); cursor: pointer;
}
.strip-cell.active { border: 2px solid var(--text); }

/* ═══ 練習 ═══ */
.quiz-head { display: flex; align-items: center; gap: 12px; margin-bottom: 8px; }
.quiz-count { font-weight: 700; font-variant-numeric: tabular-nums; }
.quiz-score { color: var(--muted); font-size: .9rem; flex: 1; }
.progress-bar { height: 6px; background: var(--surface-2); border-radius: 999px; overflow: hidden; }
.progress-fill { height: 100%; background: var(--text); transition: width .25s ease; }
.quiz-type { text-align: center; color: var(--muted); font-size: .85rem; margin: 16px 0 6px; }

.quiz-prompt {
  display: flex; align-items: center; justify-content: center;
  min-height: 170px; margin-bottom: 16px;
  background: var(--surface); border: 1px solid var(--line); border-radius: 20px;
}
.quiz-prompt-kana { font-size: clamp(5rem, 28vw, 7rem); line-height: 1; }
.quiz-prompt-romaji { font-size: 3.2rem; font-weight: 700; letter-spacing: .04em; }
.quiz-listen { width: 100%; flex-direction: column; gap: 6px; cursor: pointer; color: var(--text); }
.quiz-listen:active { transform: scale(.99); }
.listen-icon { font-size: 3.2rem; line-height: 1; }
.listen-text { color: var(--muted); font-size: .95rem; }

.quiz-options { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.quiz-opt {
  position: relative; display: flex; align-items: center; justify-content: center;
  min-height: 84px; padding: 8px; cursor: pointer;
  background: var(--surface); border: 2px solid var(--line); border-radius: 16px;
  -webkit-tap-highlight-color: transparent;
}
.quiz-opt:not(:disabled):active { transform: scale(.98); }
.quiz-opt:disabled { cursor: default; opacity: .55; }
.quiz-opt.is-correct, .quiz-opt.is-wrong { opacity: 1; }
.quiz-opt.is-correct { border-color: var(--ok); background: var(--ok-soft); }
.quiz-opt.is-wrong { border-color: var(--bad); background: var(--bad-soft); }
.opt-num { position: absolute; top: 6px; left: 10px; font-size: .72rem; color: var(--muted); }
.opt-kana { font-size: 2.4rem; line-height: 1; }
.opt-romaji { font-size: 1.5rem; font-weight: 700; }
@media (hover: none) { .opt-num { display: none; } }

.quiz-feedback { display: flex; align-items: center; justify-content: center; flex-wrap: wrap; min-height: 48px; margin: 12px 0; font-weight: 700; }
.quiz-feedback [lang="ja"] { font-size: 1.4rem; }
.quiz-feedback.ok { color: var(--ok); }
.quiz-feedback.bad { color: var(--bad); }
.quiz-feedback .k-hira, .quiz-feedback .k-kata { color: inherit; }

/* ═══ 成績 ═══ */
.result-card { text-align: center; padding: 28px 16px; background: var(--surface); border: 1px solid var(--line); border-radius: 20px; }
.result-big { font-size: 4rem; font-weight: 800; line-height: 1; }
.result-total { font-size: 1.6rem; color: var(--muted); }
.result-pct { margin-top: 8px; color: var(--muted); }
.wrong-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(76px, 1fr)); gap: 8px; }
.wrong-item {
  display: flex; flex-direction: column; align-items: center; padding: 10px 4px;
  background: var(--bad-soft); border: 0; border-radius: 12px; cursor: pointer;
}
.wrong-kana { font-size: 2rem; line-height: 1.1; }
.wrong-romaji { font-size: .85rem; color: var(--muted); }
.result-actions { display: flex; flex-direction: column; gap: 10px; margin-top: 22px; }

/* ═══ 第 3 階段：進度 ═══ */
:root { --hira-rgb: 196, 70, 47; --kata-rgb: 47, 90, 168; }
@media (prefers-color-scheme: dark) { :root { --hira-rgb: 240, 135, 112; --kata-rgb: 135, 169, 240; } }

.stat-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin-bottom: 14px; }
@media (max-width: 420px) { .stat-grid { grid-template-columns: repeat(2, 1fr); } }
.stat { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 10px 12px; }
.stat-value { font-size: 1.35rem; font-weight: 800; font-variant-numeric: tabular-nums; }
.stat-label { font-size: .82rem; color: var(--muted); }
.stat-sub { font-size: .75rem; color: var(--muted); }

.legend { display: flex; flex-wrap: wrap; gap: 6px 14px; margin-bottom: 12px; font-size: .82rem; color: var(--muted); }
.legend-item { display: inline-flex; align-items: center; gap: 5px; }
.legend-swatch { width: 16px; height: 16px; border-radius: 4px; display: inline-block; }

/* 熟練度顏色：越熟練顏色越深 */
.heat { --rgb: var(--hira-rgb); background: var(--surface); border: 1px solid var(--line); }
.heat[data-script="kata"] { --rgb: var(--kata-rgb); }
.heat[data-level="-1"] { background: var(--surface); border-style: dashed; }
.heat[data-level="0"]  { background: rgba(var(--rgb), .10); border-color: rgba(var(--rgb), .35); border-style: dotted; }
.heat[data-level="1"]  { background: rgba(var(--rgb), .22); border-color: transparent; }
.heat[data-level="2"]  { background: rgba(var(--rgb), .42); border-color: transparent; }
.heat[data-level="3"]  { background: rgba(var(--rgb), .68); border-color: transparent; }
.heat[data-level="4"]  { background: rgb(var(--rgb)); border-color: transparent; }
.cell.heat .cell-kana { color: var(--text); }
.cell.heat[data-level="3"] .cell-kana, .cell.heat[data-level="4"] .cell-kana { color: #fff; }
@media (prefers-color-scheme: dark) {
  .cell.heat[data-level="3"] .cell-kana, .cell.heat[data-level="4"] .cell-kana { color: #111; }
}
.cell.heat.active { border: 2px solid var(--text); }

.danger-zone { margin-top: 28px; text-align: center; }
.btn-danger { color: var(--bad); }

/* ═══ 首頁底部 ═══ */
.site-footer {
  margin-top: 32px; padding-top: 16px; border-top: 1px solid var(--line);
  text-align: center; color: var(--muted); font-size: .82rem; line-height: 1.7;
}
.site-footer p { margin: 0; }
.site-footer a { color: inherit; text-decoration: underline; }
