/* ========== Design tokens ========== */
:root {
  --bg: #0f1115;
  --bg-elev: #181b22;
  --bg-card: #1f242c;
  --bg-card-2: #252b35;
  --border: #2a3140;
  --border-strong: #38414f;
  --text: #e8ebf1;
  --text-dim: #9ba3b3;
  --text-faint: #6b7383;
  --accent: #4f8df9;
  --accent-strong: #2563eb;
  --green: #34c779;
  --green-bg: rgba(52,199,121,.10);
  --green-border: rgba(52,199,121,.55);
  --red: #ef5b5b;
  --red-bg: rgba(239,91,91,.10);
  --red-border: rgba(239,91,91,.55);
  --yellow: #f5b942;
  --hint-bg: #232936;

  --radius: 16px;
  --radius-sm: 10px;
  --pad-card: 18px;
  --shadow-card: 0 1px 0 rgba(255,255,255,.03), 0 8px 28px rgba(0,0,0,.35);
  --tap-min: 44px;

  --font-sans: -apple-system, BlinkMacSystemFont, "PingFang TC",
    "Noto Sans TC", "Microsoft JhengHei", "Helvetica Neue",
    Arial, sans-serif;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; padding: 0; }
html { -webkit-text-size-adjust: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-sans);
  font-size: 16px;
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  min-height: 100vh;
  min-height: 100dvh;
}

/* ========== Layout ========== */
.app {
  max-width: 760px;
  margin: 0 auto;
  padding: 24px 16px 120px;
  padding-top: max(24px, env(safe-area-inset-top));
  /* 預留空間給 quiz 頁的 fixed bottombar；首頁多留一點也無妨 */
  padding-bottom: max(120px, calc(120px + env(safe-area-inset-bottom)));
}
.view-root { display: flex; flex-direction: column; gap: 20px; }

/* ========== Header ========== */
.topbar { display: flex; flex-direction: column; gap: 4px; }
.title {
  font-size: 22px;
  font-weight: 700;
  letter-spacing: .02em;
  margin: 0;
}
.subtitle {
  margin: 0;
  color: var(--text-dim);
  font-size: 14px;
}

/* ========== Buttons / links ========== */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: var(--tap-min);
  padding: 0 22px;
  border-radius: 999px;
  border: 1px solid transparent;
  font-size: 16px;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
  user-select: none;
  transition: background .15s, border-color .15s, opacity .15s, transform .05s;
}
.btn:active { transform: scale(.98); }
.btn-primary {
  background: var(--accent-strong);
  color: white;
}
.btn-primary:hover { background: var(--accent); }
.btn-primary:disabled { background: #2a3140; color: var(--text-faint); cursor: not-allowed; }
.btn-ghost {
  background: transparent;
  color: var(--text);
  border-color: var(--border-strong);
}
.btn-ghost:hover { background: var(--bg-card-2); }
.btn-ghost:disabled { color: var(--text-faint); border-color: var(--border); cursor: not-allowed; }

.link {
  background: none;
  border: none;
  padding: 4px 0;
  color: var(--accent);
  font: inherit;
  font-size: 14px;
  cursor: pointer;
}
.link:hover { text-decoration: underline; }
.link-danger { color: #ff7676; }
.dot { color: var(--text-faint); margin: 0 6px; }

.icon-btn {
  background: transparent;
  border: none;
  color: var(--text);
  font-size: 22px;
  width: var(--tap-min);
  height: var(--tap-min);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  border-radius: 999px;
}
.icon-btn:hover { background: var(--bg-card-2); }

/* ========== Overall stats ========== */
.overall-stats {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 8px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
}
.stat-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  text-align: center;
  min-width: 0;
}
.stat-num {
  font-variant-numeric: tabular-nums;
  font-weight: 700;
  font-size: 22px;
  line-height: 1.1;
}
.stat-label {
  color: var(--text-dim);
  font-size: 12px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 100%;
}
.stat-num.green { color: var(--green); }
.stat-num.red { color: var(--red); }

/* ========== Actions block ========== */
.actions {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}
.actions .btn { flex: 1 1 auto; min-width: 140px; }

/* ========== Categories ========== */
.categories-head {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  margin-bottom: 8px;
}
.categories h2 {
  font-size: 16px;
  font-weight: 600;
  margin: 0;
  color: var(--text-dim);
  letter-spacing: .04em;
}
.range-actions { display: inline-flex; align-items: center; }

.cat-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.cat-row {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 12px 14px;
  display: flex;
  gap: 12px;
  align-items: center;
  cursor: pointer;
  transition: background .15s, border-color .15s, box-shadow .15s;
  min-height: var(--tap-min);
}
.cat-row:hover { background: var(--bg-card-2); border-color: var(--border-strong); }
.cat-row:focus-visible {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(79, 141, 249, .25);
}
.cat-check {
  flex: 0 0 auto;
  width: 22px;
  height: 22px;
  border: 1.5px solid var(--border-strong);
  border-radius: 6px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: var(--bg-elev);
}
.cat-row.is-checked .cat-check {
  background: var(--accent-strong);
  border-color: var(--accent-strong);
}
.cat-row.is-checked .cat-check::after {
  content: "✓";
  color: white;
  font-size: 14px;
  line-height: 1;
}
.cat-info { flex: 1 1 auto; min-width: 0; }
.cat-name {
  font-weight: 600;
  font-size: 15px;
  display: flex;
  gap: 8px;
  align-items: baseline;
  flex-wrap: wrap;
}
.cat-id {
  color: var(--text-faint);
  font-size: 12px;
  font-variant-numeric: tabular-nums;
}
.cat-meta {
  color: var(--text-dim);
  font-size: 12px;
  margin-top: 2px;
}
.cat-bar {
  position: relative;
  height: 4px;
  background: var(--border);
  border-radius: 2px;
  margin-top: 6px;
  overflow: hidden;
}
.cat-bar > span {
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  background: var(--accent);
  border-radius: 2px;
  transition: width .3s;
}

/* ========== History ========== */
.history h2 {
  font-size: 16px;
  font-weight: 600;
  color: var(--text-dim);
  margin: 0 0 8px;
}
.history-empty {
  color: var(--text-faint);
  font-size: 14px;
  padding: 12px;
  background: var(--bg-card);
  border-radius: var(--radius-sm);
  border: 1px dashed var(--border);
}
.hist-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.hist-row {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  font-size: 13px;
  display: grid;
  grid-template-columns: auto 1fr auto;
  gap: 8px;
  align-items: center;
}
.hist-time { color: var(--text-dim); font-variant-numeric: tabular-nums; }
.hist-mode { color: var(--text); }
.hist-mode-tag {
  font-size: 11px;
  padding: 2px 6px;
  border-radius: 999px;
  background: var(--border);
  color: var(--text-dim);
  margin-left: 4px;
}
.hist-mode-tag.review { background: rgba(245,185,66,.15); color: var(--yellow); }
.hist-score { font-variant-numeric: tabular-nums; font-weight: 600; }
.hist-score.green { color: var(--green); }
.hist-score.red { color: var(--red); }

/* ========== Footer ========== */
.footer {
  margin-top: 8px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  color: var(--text-faint);
}
.footer-actions {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 4px;
}

/* ========== Sync dialog ========== */
.sync-modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, .55);
  z-index: 200;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  padding-bottom: max(16px, env(safe-area-inset-bottom));
}
.sync-modal-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px;
  max-width: 520px;
  width: 100%;
  max-height: calc(100vh - 32px);
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.sync-modal-card h3 {
  margin: 0;
  font-size: 17px;
  font-weight: 600;
}
.sync-modal-card p {
  margin: 0;
  color: var(--text-dim);
  font-size: 14px;
  line-height: 1.6;
}
.sync-link-box {
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  font-size: 12px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  word-break: break-all;
  max-height: 120px;
  overflow-y: auto;
  user-select: all;
  color: var(--text-dim);
  line-height: 1.5;
}
.sync-stats {
  display: flex;
  gap: 12px;
  font-size: 12px;
  color: var(--text-faint);
  flex-wrap: wrap;
}
.sync-modal-actions {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
  flex-wrap: wrap;
  margin-top: 4px;
}
.footnote {
  font-size: 12px;
  margin: 0;
  line-height: 1.6;
}

/* ========== Quiz topbar ========== */
.quiz-topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 8px;
}
.quiz-progress {
  flex: 1 1 auto;
  text-align: center;
  font-size: 13px;
  color: var(--text-dim);
  font-variant-numeric: tabular-nums;
}

/* ========== Voice toggle button ========== */
.icon-btn.voice-btn {
  font-size: 18px;
  position: relative;
}
.icon-btn.voice-btn.is-on {
  background: var(--accent-strong);
  color: white;
}
.icon-btn.voice-btn.is-on:hover { background: var(--accent); }

/* ========== Voice status banner ========== */
.voice-status {
  text-align: center;
  font-size: 13px;
  color: var(--text-dim);
  padding: 8px 14px;
  margin: 0 0 4px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 999px;
  align-self: center;
}
.voice-status.is-speaking {
  color: var(--accent);
  border-color: var(--accent);
}
.voice-status.is-listening {
  color: var(--accent);
  border-color: var(--accent);
  animation: voice-pulse 1.4s ease-in-out infinite;
}
.voice-status.is-correct {
  color: var(--green);
  border-color: var(--green-border);
  background: var(--green-bg);
}
.voice-status.is-wrong {
  color: var(--red);
  border-color: var(--red-border);
  background: var(--red-bg);
}
@keyframes voice-pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(79, 141, 249, .35); }
  50%      { box-shadow: 0 0 0 8px rgba(79, 141, 249, 0); }
}

/* ========== Quiz card ========== */
.quiz-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px 16px 8px;
  box-shadow: var(--shadow-card);
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.cat-badge {
  align-self: flex-start;
  font-size: 11px;
  color: var(--text-dim);
  background: var(--bg-card-2);
  padding: 4px 10px;
  border-radius: 999px;
  letter-spacing: .04em;
}

.stem {
  font-size: 17px;
  line-height: 1.65;
  color: var(--text);
  word-break: break-word;
}
.stem .sym {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  vertical-align: middle;
  background: white;
  border-radius: 6px;
  padding: 4px 6px;
  margin: 0 4px;
  min-height: 30px;
}
.stem .sym img {
  display: block;
  max-height: 36px;
  width: auto;
  max-width: 100%;
}
.stem-image {
  display: block;
  margin: 12px auto 0;
  background: white;
  padding: 8px 12px;
  border-radius: 8px;
  max-width: 100%;
  width: max-content;
}
.stem-image img {
  display: block;
  max-height: 80px;
  width: auto;
  max-width: 100%;
}

.options {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.opt {
  background: var(--bg-card-2);
  border: 1.5px solid var(--bg-card-2);
  border-radius: 14px;
  padding: 14px 16px;
  cursor: pointer;
  transition: background .15s, border-color .15s;
  min-height: var(--tap-min);
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.opt:hover { background: #2c333f; border-color: #303948; }
.opt-row {
  display: flex;
  gap: 10px;
  align-items: flex-start;
}
.opt-letter {
  flex: 0 0 auto;
  width: 22px;
  font-weight: 700;
  color: var(--text-dim);
  line-height: 1.5;
}
.opt-text {
  flex: 1 1 auto;
  word-break: break-word;
  line-height: 1.5;
}

.opt.is-correct {
  border-color: var(--green-border);
  background: var(--green-bg);
}
.opt.is-wrong {
  border-color: var(--red-border);
  background: var(--red-bg);
}
.opt.is-locked { cursor: default; }
.opt.is-locked:hover { background: var(--bg-card-2); border-color: var(--bg-card-2); }
.opt.is-locked.is-correct:hover { background: var(--green-bg); border-color: var(--green-border); }
.opt.is-locked.is-wrong:hover { background: var(--red-bg); border-color: var(--red-border); }

.opt-tag {
  font-size: 13px;
  display: flex;
  align-items: center;
  gap: 4px;
  margin-top: 2px;
}
.opt.is-correct .opt-tag { color: var(--green); }
.opt.is-wrong .opt-tag { color: var(--red); }

.opt-explain {
  font-size: 13.5px;
  color: var(--text-dim);
  line-height: 1.6;
  margin-top: 0;
}
.opt.is-correct .opt-explain { color: #c5ecd6; }
.opt.is-wrong .opt-explain { color: #f4c4c4; }

/* ========== Hint ========== */
.hint-box {
  display: flex;
  gap: 8px;
  align-items: flex-start;
  background: var(--hint-bg);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px 14px;
  font-size: 14px;
  color: var(--text-dim);
  line-height: 1.6;
}
.hint-box::before {
  content: "💡";
  font-size: 16px;
  flex: 0 0 auto;
  line-height: 1.4;
}

/* ========== Quiz bottombar ========== */
.quiz-bottombar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 100;
  background: rgba(15, 17, 21, 0.92);
  backdrop-filter: saturate(140%) blur(8px);
  -webkit-backdrop-filter: saturate(140%) blur(8px);
  border-top: 1px solid var(--border);
  padding: 10px 16px;
  padding-bottom: max(10px, calc(env(safe-area-inset-bottom) + 6px));
}
.quiz-bottombar-inner {
  max-width: 760px;
  margin: 0 auto;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
}
@media (min-width: 900px) {
  .quiz-bottombar-inner { max-width: 860px; }
}
.hint-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: var(--bg-card-2);
  border: 1px solid var(--border);
  color: var(--text);
  font: inherit;
  padding: 0 14px;
  height: var(--tap-min);
  border-radius: 999px;
  cursor: pointer;
  transition: background .15s;
}
.hint-btn:hover { background: #2c333f; }
.hint-btn[aria-expanded="true"] {
  background: #2c333f;
  border-color: var(--border-strong);
}
.bottom-right { display: inline-flex; gap: 8px; flex-wrap: wrap; justify-content: flex-end; }
.btn.btn-compact { padding: 0 16px; min-width: 0; font-size: 15px; }
@media (max-width: 360px) {
  .btn.btn-compact { padding: 0 12px; font-size: 14px; }
  .hint-btn { padding: 0 10px; }
  .hint-btn .hint-label { display: none; }
}

/* ========== Results view ========== */
.results {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.results-row {
  display: flex;
  justify-content: space-between;
  font-size: 15px;
}
.results-row .lbl { color: var(--text-dim); }
.results-row .val { font-variant-numeric: tabular-nums; font-weight: 600; }
.results-row.big .val { font-size: 22px; }

/* ========== Toasts ========== */
.toast {
  position: fixed;
  left: 50%;
  bottom: 32px;
  transform: translateX(-50%);
  background: #2a3140;
  color: var(--text);
  border: 1px solid var(--border-strong);
  border-radius: 999px;
  padding: 10px 18px;
  font-size: 14px;
  box-shadow: 0 8px 24px rgba(0,0,0,.4);
  z-index: 999;
  pointer-events: none;
  opacity: 0;
  transition: opacity .2s;
}
.toast.show { opacity: 1; }

/* ========== Larger viewports ========== */
@media (min-width: 600px) {
  .app { padding: 32px 24px 96px; }
  .title { font-size: 26px; }
  .stem { font-size: 18px; }
  .opt { padding: 16px 18px; }
}

@media (min-width: 900px) {
  .app { max-width: 860px; }
}

/* ========== Loading state ========== */
.loading {
  text-align: center;
  color: var(--text-dim);
  padding: 60px 20px;
}
.loading::after {
  content: "";
  display: inline-block;
  width: 16px;
  height: 16px;
  margin-left: 8px;
  border: 2px solid var(--border-strong);
  border-top-color: var(--accent);
  border-radius: 50%;
  vertical-align: middle;
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ========== Reduced motion ========== */
@media (prefers-reduced-motion: reduce) {
  * { animation-duration: 0.01ms !important; transition-duration: 0.01ms !important; }
}
