/* ===== JLPT 練習 — 樣式 ===== */
:root {
  --bg: #f6f8fa;
  --surface: #ffffff;
  --surface-2: #eef1f4;
  --text: #1a1f24;
  --text-dim: #5b6570;
  --border: #d6dde4;
  --primary: #1f6feb;
  --primary-ink: #ffffff;
  --good: #1a7f37;
  --good-bg: #dcffe4;
  --bad: #cf222e;
  --bad-bg: #ffe3e3;
  --warn: #9a6700;
  --accent: #8250df;
  --shadow: 0 1px 3px rgba(0,0,0,.08), 0 1px 2px rgba(0,0,0,.06);
  --radius: 14px;
  --tabbar-h: 62px;
  --topbar-h: 52px;
}
:root[data-theme="dark"] {
  --bg: #0d1117;
  --surface: #161b22;
  --surface-2: #21262d;
  --text: #e6edf3;
  --text-dim: #9198a1;
  --border: #2d333b;
  --primary: #388bfd;
  --primary-ink: #ffffff;
  --good: #3fb950;
  --good-bg: #12261b;
  --bad: #f85149;
  --bad-bg: #2d1214;
  --warn: #d29922;
  --accent: #a371f7;
  --shadow: 0 1px 3px rgba(0,0,0,.4);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
/* hidden 屬性優先於任何 display 設定（否則 .chips 等 display:flex 會蓋掉瀏覽器預設的 [hidden]{display:none}） */
[hidden] { display: none !important; }
html, body { margin: 0; padding: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, "Segoe UI", "Noto Sans TC", "Hiragino Sans", "Microsoft JhengHei", "PingFang TC", sans-serif;
  line-height: 1.6;
  overscroll-behavior-y: none;
}
button, input, select { font-family: inherit; font-size: inherit; color: inherit; }

#app {
  display: flex;
  flex-direction: column;
  min-height: 100%;
  max-width: 560px;
  margin: 0 auto;
  background: var(--bg);
}

/* ---- Topbar ---- */
#topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  height: calc(var(--topbar-h) + env(safe-area-inset-top));
  padding-top: env(safe-area-inset-top);
  display: flex;
  align-items: center;
  gap: 4px;
  padding-left: 8px;
  padding-right: 8px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
#topTitle {
  font-size: 17px;
  font-weight: 700;
  margin: 0;
  flex: 1;
  text-align: center;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.icon-btn {
  background: none;
  border: none;
  font-size: 22px;
  width: 40px;
  height: 40px;
  border-radius: 10px;
  cursor: pointer;
  color: var(--text);
}
.icon-btn:active { background: var(--surface-2); }
#backBtn { font-size: 30px; line-height: 1; }
#backBtn[hidden] { display: none; }

/* ---- Main view ---- */
#view {
  flex: 1;
  padding: 16px 14px calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 16px);
}

/* ---- Tabbar ---- */
#tabbar {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  z-index: 20;
  height: calc(var(--tabbar-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  display: flex;
  background: var(--surface);
  border-top: 1px solid var(--border);
  max-width: 560px;
  margin: 0 auto;
}
.tab {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  text-decoration: none;
  color: var(--text-dim);
  font-size: 11px;
  padding-top: 6px;
}
.tab .ti { font-size: 20px; filter: grayscale(1) opacity(.7); }
.tab.active { color: var(--primary); font-weight: 700; }
.tab.active .ti { filter: none; }

/* ---- Generic components ---- */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--shadow);
  margin-bottom: 12px;
}
.section-title { font-size: 13px; color: var(--text-dim); font-weight: 700; margin: 18px 4px 8px; text-transform: uppercase; letter-spacing: .04em; }
.row { display: flex; align-items: center; gap: 10px; }
.spread { justify-content: space-between; }
.muted { color: var(--text-dim); }
.small { font-size: 13px; }
.big-num { font-size: 30px; font-weight: 800; line-height: 1.1; }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  background: var(--primary);
  color: var(--primary-ink);
  border: none;
  border-radius: 12px;
  padding: 12px 16px;
  font-weight: 700;
  font-size: 15px;
  cursor: pointer;
  width: 100%;
  min-height: 48px;
}
.btn:active { filter: brightness(.94); }
.btn.secondary { background: var(--surface-2); color: var(--text); border: 1px solid var(--border); }
.btn.ghost { background: none; color: var(--primary); border: 1px solid var(--border); }
.btn.sm { width: auto; padding: 8px 12px; min-height: 0; font-size: 13px; border-radius: 10px; }
.btn-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }

.pill {
  display: inline-block;
  padding: 2px 9px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 700;
  background: var(--surface-2);
  border: 1px solid var(--border);
}
/* 級別色票（文字對比皆 ≥ 4.5:1） */
.pill.n5 { background: #e7f5ff; color: #10548c; border-color: #a5d8ff; }
.pill.n4 { background: #e6fcf5; color: #087f5b; border-color: #96f2d7; }
.pill.n3 { background: #fff4e0; color: #9a5000; border-color: #ffd8a8; }
.pill.n2 { background: #fff0f6; color: #a61e4d; border-color: #fcc2d7; }
.pill.n1 { background: #f3f0ff; color: #5235ab; border-color: #d0bfff; }
.pill.travel { background: #e6f7f4; color: #0b6b7d; border-color: #99e9f2; }
:root[data-theme="dark"] .pill.n5 { background: var(--surface-2); color: #6cb6ff; border-color: var(--border); }
:root[data-theme="dark"] .pill.n4 { background: var(--surface-2); color: #4bdcb0; border-color: var(--border); }
:root[data-theme="dark"] .pill.n3 { background: var(--surface-2); color: #ffb066; border-color: var(--border); }
:root[data-theme="dark"] .pill.n2 { background: var(--surface-2); color: #ff9bc0; border-color: var(--border); }
:root[data-theme="dark"] .pill.n1 { background: var(--surface-2); color: #bda6ff; border-color: var(--border); }
:root[data-theme="dark"] .pill.travel { background: var(--surface-2); color: #5cc7d6; border-color: var(--border); }

/* progress bar */
.bar { position: relative; height: 8px; background: var(--surface-2); border-radius: 999px; overflow: hidden; }
.bar > i { display: block; height: 100%; background: var(--primary); border-radius: 999px; transition: width .35s ease; }
.bar.good > i { background: var(--good); }
/* 「學習中」畫在主進度後面的淺色段：讓第一天就看得到自己在前進 */
.bar > i.soft { position: absolute; inset: 0 auto 0 0; background: var(--primary); opacity: .3; }
.bar.good > i.soft { background: var(--good); }

/* ---- Level / mode lists ---- */
.tile {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  text-align: left;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  margin-bottom: 10px;
  cursor: pointer;
  box-shadow: var(--shadow);
}
.tile:active { background: var(--surface-2); }
.tile .tile-main { flex: 1; }
.tile .tile-title { font-weight: 700; font-size: 15px; }
.tile .tile-sub { font-size: 12px; color: var(--text-dim); }
.tile .chev { color: var(--text-dim); font-size: 20px; }

/* ---- Flashcard ---- */
.study-head {
  display: flex; align-items: center; gap: 10px;
  margin-bottom: 14px;
}
.study-head .bar { flex: 1; }
.study-count { font-size: 13px; color: var(--text-dim); font-variant-numeric: tabular-nums; }

.flashcard-wrap { perspective: 1400px; margin: 8px 0 16px; }
.flashcard {
  position: relative;
  width: 100%;
  min-height: 320px;
  transform-style: preserve-3d;
  transition: transform .5s cubic-bezier(.2,.7,.2,1);
  cursor: pointer;
}
.flashcard.flipped { transform: rotateY(180deg); }
.flash-face {
  position: absolute;
  inset: 0;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 18px;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 24px;
  gap: 10px;
}
.flash-face.back { transform: rotateY(180deg); }
.flash-kana { font-size: 18px; color: var(--text-dim); }
.flash-main { font-size: 40px; font-weight: 800; }
.flash-main.jp { font-family: "Hiragino Sans", "Noto Sans JP", "Yu Gothic", "Meiryo", sans-serif; }
.flash-pos { font-size: 12px; }
.flash-meaning { font-size: 26px; font-weight: 700; }
.flash-hint { position: absolute; bottom: 12px; font-size: 12px; color: var(--text-dim); }
.flash-example {
  margin-top: 6px;
  font-size: 14px;
  color: var(--text-dim);
  border-top: 1px dashed var(--border);
  padding-top: 10px;
  width: 100%;
}
.flash-example b { color: var(--text); font-weight: 600; }

.grade-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.grade-grid .btn { min-height: 52px; }
.btn.know { background: var(--good); }
.btn.dont { background: var(--bad); }

/* ---- Quiz ---- */
.quiz-q {
  font-size: 15px;
  color: var(--text-dim);
  margin-bottom: 6px;
}
.quiz-prompt {
  font-size: 30px;
  font-weight: 800;
  text-align: center;
  padding: 22px 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  margin-bottom: 8px;
  box-shadow: var(--shadow);
}
.quiz-prompt .sub { display:block; font-size: 15px; color: var(--text-dim); font-weight: 500; margin-top: 6px; }
.opt {
  display: block;
  width: 100%;
  text-align: left;
  background: var(--surface);
  border: 1.5px solid var(--border);
  border-radius: 12px;
  padding: 14px;
  margin-bottom: 10px;
  font-size: 16px;
  cursor: pointer;
}
.opt:active { background: var(--surface-2); }
.opt.correct { border-color: var(--good); background: var(--good-bg); }
.opt.wrong { border-color: var(--bad); background: var(--bad-bg); }
.opt[disabled] { cursor: default; opacity: .95; }
.quiz-feedback {
  margin-top: 4px;
  padding: 12px 14px;
  border-radius: 12px;
  font-size: 14px;
  background: var(--surface-2);
  border: 1px solid var(--border);
}
.quiz-feedback .fb-title { font-weight: 800; }
.quiz-feedback.ok .fb-title { color: var(--good); }
.quiz-feedback.no .fb-title { color: var(--bad); }

/* ---- Result screen ---- */
.result-hero { text-align: center; padding: 20px 0; }
.result-score { font-size: 52px; font-weight: 800; }
.dist { display: flex; gap: 8px; }
.dist > div { flex: 1; text-align: center; padding: 10px; border-radius: 12px; background: var(--surface-2); }

/* ---- Stats ---- */
.stat-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.heat { display: flex; gap: 3px; flex-wrap: wrap; }
.heat i {
  width: 13px; height: 13px; border-radius: 3px;
  background: var(--surface-2);
  display: block;
}
.heat i.l1 { background: #9be9a8; }
.heat i.l2 { background: #40c463; }
.heat i.l3 { background: #30a14e; }
.heat i.l4 { background: #216e39; }
/* ---------- 學習熱力圖 ---------- */
.hm-months, .hm-grid { display: flex; gap: 2px; }
.hm-mcol { flex: 1; min-width: 0; font-size: 8.5px; color: var(--text-dim); white-space: nowrap; }
.hm-col { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 2px; }
.hm-c {
  display: block; width: 100%; aspect-ratio: 1 / 1;
  border-radius: 2px; background: var(--surface-2);
}
.hm-c.none { background: transparent; }
.hm-c.l1 { background: color-mix(in srgb, var(--primary) 28%, var(--surface-2)); }
.hm-c.l2 { background: color-mix(in srgb, var(--primary) 52%, var(--surface-2)); }
.hm-c.l3 { background: color-mix(in srgb, var(--primary) 76%, var(--surface-2)); }
.hm-c.l4 { background: var(--primary); }
.hm-legend { display: flex; align-items: center; gap: 3px; margin-top: 8px; }
.hm-legend .hm-c { width: 10px; aspect-ratio: 1 / 1; flex: 0 0 10px; }

.empty {
  text-align: center;
  color: var(--text-dim);
  padding: 40px 20px;
}
.empty .big { font-size: 44px; }

.list-item {
  display: flex;
  gap: 10px;
  padding: 12px 0;
  border-bottom: 1px solid var(--border);
}
.list-item:last-child { border-bottom: none; }
.list-item .li-main { flex: 1; }
.li-jp { font-weight: 700; }
.li-sub { font-size: 13px; color: var(--text-dim); }

.toggle-line { display: flex; align-items: center; justify-content: space-between; padding: 10px 0; }
select.sel {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 8px 10px;
}

/* ---- 操作列：最愛 / 朗讀 ---- */
.action-row { display: flex; gap: 2px; align-items: center; flex-shrink: 0; }
.star-btn { font-size: 20px; color: var(--text-dim); }
.star-btn.on { color: #f5a623; }
.speak-btn { font-size: 18px; }
.card-tools { display: flex; justify-content: center; margin: 4px 0 12px; }
.card-tools .action-row .icon-btn { border: 1px solid var(--border); border-radius: 10px; margin: 0 4px; }

.kbd-hint { text-align: center; margin-top: 10px; }
@media (pointer: coarse) { .kbd-hint { display: none; } }

.opt-num {
  display: inline-flex; align-items: center; justify-content: center;
  width: 22px; height: 22px; margin-right: 10px;
  border: 1px solid var(--border); border-radius: 6px;
  font-size: 12px; color: var(--text-dim); vertical-align: middle;
}

.linklike { background: none; border: none; padding: 0; color: var(--primary); cursor: pointer; text-decoration: underline; font: inherit; }

/* ---- chips ---- */
.chips { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.chip {
  background: var(--surface-2); border: 1px solid var(--border);
  border-radius: 999px; padding: 5px 12px; font-size: 13px; cursor: pointer; color: var(--text);
}
.chip.on { background: var(--primary); color: #fff; border-color: var(--primary); font-weight: 700; }
/* 深色主題的 --primary 較亮，白字對比不足 → 選中的 chip 用較深藍 */
:root[data-theme="dark"] .chip.on { background: #1862ab; border-color: #1862ab; }
.chip-sep { width: 1px; height: 18px; background: var(--border); margin: 0 2px; }

/* ---- 搜尋 ---- */
.search-bar { margin-bottom: 10px; }
.search-input {
  width: 100%; padding: 12px 14px; font-size: 16px;
  background: var(--surface); border: 1.5px solid var(--border); border-radius: 12px;
}
.search-input:focus { outline: none; border-color: var(--primary); }
.search-results { margin-top: 12px; }

.detail-card .detail-main { font-size: 22px; font-weight: 800; }
.detail-meaning { font-size: 17px; font-weight: 600; margin-top: 8px; }
.detail-example {
  margin-top: 10px; padding-top: 8px; border-top: 1px dashed var(--border); font-size: 14px;
}
.detail-example b { font-weight: 600; }

/* ---- range / switch ---- */
.range { width: 100%; margin: 4px 0 2px; accent-color: var(--primary); }
input.switch { width: 44px; height: 26px; accent-color: var(--primary); flex-shrink: 0; }

/* ---- 引導卡 ---- */
.guide-card { border-color: var(--primary); }
.guide-list { margin: 10px 0 14px; padding-left: 20px; font-size: 14px; }
.guide-list li { margin-bottom: 6px; }

#toast {
  position: fixed;
  left: 50%;
  bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 16px);
  transform: translateX(-50%) translateY(20px);
  background: #000;
  color: #fff;
  padding: 10px 16px;
  border-radius: 999px;
  font-size: 13px;
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s, transform .2s;
  z-index: 50;
  max-width: 90%;
  text-align: center;
}
#toast.show { opacity: .92; transform: translateX(-50%) translateY(0); }

.spinner {
  width: 26px; height: 26px;
  border: 3px solid var(--border);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin .8s linear infinite;
  margin: 40px auto;
}
@keyframes spin { to { transform: rotate(360deg); } }

.fade-in { animation: fade .25s ease; }
@keyframes fade { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }

@media (min-width: 480px) {
  .flash-main { font-size: 46px; }
}

/* ---- 無障礙 ---- */
/* 鍵盤聚焦時的清楚外框（滑鼠點擊不顯示） */
:focus-visible {
  outline: 3px solid var(--primary);
  outline-offset: 2px;
  border-radius: 6px;
}
.flashcard:focus-visible { outline-offset: 4px; }

/* 只給螢幕報讀器的文字 */
.sr-only {
  position: absolute !important;
  width: 1px; height: 1px;
  padding: 0; margin: -1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  border: 0;
}

/* 尊重「減少動態效果」的系統偏好 */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: .001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: .001ms !important;
    scroll-behavior: auto !important;
  }
  .flashcard { transition: none !important; }
}

/* ---- 聽力練習 ---- */
.play-btn {
  min-height: 64px;
  font-size: 20px;
  letter-spacing: 2px;
}
.play-btn.playing {
  opacity: .75;
  animation: playPulse 1.1s ease-in-out infinite;
}
@keyframes playPulse { 50% { opacity: .45; } }
.audio-help {
  display: flex;
  align-items: center;
  gap: 8px;
  justify-content: center;
  margin: 10px 0 2px;
  flex-wrap: wrap;
}

/* ---------- 模擬考 ---------- */
.exam-head {
  display: flex; align-items: center; gap: 8px;
  margin-bottom: 10px;
}
.exam-head .exam-sec { flex: 1; font-weight: 700; font-size: 15px; }
.exam-timer {
  font-variant-numeric: tabular-nums;
  font-weight: 700; font-size: 17px;
  padding: 3px 10px; border-radius: 999px;
  background: var(--surface-2); color: var(--text-dim);
}
.exam-timer.urgent { background: var(--bad-bg); color: var(--bad); }
/* 題號導覽：一眼看出哪幾題還沒作答，也能直接跳過去改 */
.exam-nav {
  display: flex; flex-wrap: wrap; gap: 6px;
  margin-bottom: 14px;
}
.exam-no {
  min-width: 30px; height: 30px;
  border: 1.5px solid var(--border); border-radius: 8px;
  background: var(--surface); color: var(--text-dim);
  font-size: 13px; font-variant-numeric: tabular-nums; cursor: pointer;
}
.exam-no.done { background: var(--surface-2); color: var(--text); font-weight: 700; }
.exam-no.cur { border-color: var(--primary); color: var(--primary); font-weight: 700; }
/* 模擬考作答當下不揭曉對錯，所以選取狀態只能是中性色，不能用對/錯的綠紅 */
.opt.picked { border-color: var(--primary); background: var(--surface-2); font-weight: 600; }
.pill.ok { background: var(--good-bg); color: var(--good); border-color: var(--good); }
.pill.no { background: var(--bad-bg); color: var(--bad); border-color: var(--bad); }

/* ---------- 複習預測日曆 ---------- */
.fc-row { display: flex; align-items: flex-end; gap: 3px; }
.fc-col { flex: 1; min-width: 0; text-align: center; }
.fc-n { font-size: 10px; color: var(--text-dim); height: 13px; font-variant-numeric: tabular-nums; }
.fc-track { height: 60px; display: flex; align-items: flex-end; }
.fc-bar {
  display: block; width: 100%; border-radius: 3px 3px 0 0;
  background: var(--primary); opacity: .75;
}
.fc-col.today .fc-bar { opacity: 1; }
.fc-col.today .fc-lab { color: var(--primary); font-weight: 700; }
.fc-lab { font-size: 9.5px; color: var(--text-dim); margin-top: 3px; line-height: 1.2; }
.fc-wk { font-size: 9px; color: var(--text-dim); opacity: .7; }
