:root {
  --bg: #f2f5fa;
  --card-bg: #ffffff;
  --text: #1c2430;
  --text-muted: #5c6779;
  --border: #d8e0ee;
  --primary: #3e6fbf;
  --primary-hover: #325aa0;
  --danger: #d84343;
  --danger-hover: #c13636;
  --success: #3f9d5d;
  --success-bg: #e3f5e9;
  --error-bg: #fdeceb;
  --chip-bg: #e7edf8;
  --chip-bg-active: #cbdaf3;
  --shadow: 0 1px 3px rgba(0,0,0,0.06), 0 1px 2px rgba(0,0,0,0.04);
}

:root[data-theme="dark"] {
  --bg: #12151f;
  --card-bg: #1b2030;
  --text: #e7ebf5;
  --text-muted: #9aa5bc;
  --border: #2c3550;
  --primary: #7fa6f0;
  --primary-hover: #96b8f4;
  --danger: #e5645f;
  --danger-hover: #ea7d78;
  --success: #4cc27c;
  --success-bg: #1c3427;
  --error-bg: #3a1e22;
  --chip-bg: #232b42;
  --chip-bg-active: #35406a;
  --shadow: 0 1px 3px rgba(0,0,0,0.3), 0 1px 2px rgba(0,0,0,0.2);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #12151f;
    --card-bg: #1b2030;
    --text: #e7ebf5;
    --text-muted: #9aa5bc;
    --border: #2c3550;
    --primary: #7fa6f0;
    --primary-hover: #96b8f4;
    --danger: #e5645f;
    --danger-hover: #ea7d78;
    --success: #4cc27c;
    --success-bg: #1c3427;
    --error-bg: #3a1e22;
    --chip-bg: #232b42;
    --chip-bg-active: #35406a;
    --shadow: 0 1px 3px rgba(0,0,0,0.3), 0 1px 2px rgba(0,0,0,0.2);
  }
}

@font-face {
  font-family: "Inter Variable";
  font-style: normal;
  font-weight: 100 900;
  font-display: swap;
  src: url("fonts/InterVariable.woff2") format("woff2");
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: "Inter Variable", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  font-optical-sizing: auto;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  font-size: 16px;
  line-height: 1.65;
}

#app {
  max-width: 1200px;
  margin: 0 auto;
  padding: 24px 16px 60px;
}

@media (min-width: 601px) {
  /* clears the fixed theme-toggle button so it never overlaps the
     quiz screen's Exit button or other top-of-card content */
  #app { padding-top: 56px; }
}

.hidden { display: none !important; }

.theme-toggle-btn {
  position: fixed;
  top: calc(14px + env(safe-area-inset-top));
  right: calc(14px + env(safe-area-inset-right));
  z-index: 10;
  background: var(--chip-bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 7px 14px;
  font-size: 0.8rem;
  font-weight: 600;
  cursor: pointer;
}
.theme-toggle-btn:hover { background: var(--chip-bg-active); }

@media (max-width: 600px) {
  .theme-toggle-btn {
    position: static;
    display: block;
    width: fit-content;
    margin: 0 0 14px auto;
  }
}

/* Result screen's review list can run to hundreds of questions, so unlike
   theme-toggle this stays position:fixed at every width — it's the only way
   back to home without scrolling past the whole list. Styled to match
   theme-toggle-btn (same pill shape) so it reads as an equally prominent
   control instead of a tiny, easy-to-miss circular icon. */
.back-fab {
  position: fixed;
  top: calc(14px + env(safe-area-inset-top));
  left: calc(14px + env(safe-area-inset-left));
  z-index: 10;
  background: var(--chip-bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 7px 14px;
  font-size: 0.8rem;
  font-weight: 600;
  cursor: pointer;
  box-shadow: var(--shadow);
}
.back-fab:hover { background: var(--chip-bg-active); }

.app-header {
  text-align: center;
  margin-bottom: 24px;
}
.app-header h1 {
  margin: 0 0 4px;
  font-size: 1.6rem;
}
.subtitle {
  color: var(--text-muted);
  margin: 0;
  font-size: 0.95rem;
}

.card {
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 18px 20px;
  margin-bottom: 16px;
  box-shadow: var(--shadow);
}
.card h2 {
  font-size: 1rem;
  margin: 0 0 12px;
  color: var(--text-muted);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.02em;
}
.card h3 {
  margin: 0 0 12px;
  font-size: 1.05rem;
}

.option-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 10px;
  margin-bottom: 14px;
}
.option-grid:last-child { margin-bottom: 0; }

.tier-select-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.tier-select-row {
  display: flex;
  align-items: center;
  gap: 12px;
}
.tier-check-label {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 180px;
  flex-shrink: 0;
  cursor: pointer;
}
.tier-toggle { accent-color: var(--primary); flex-shrink: 0; cursor: pointer; }
.tier-select-label {
  font-size: 0.85rem;
  color: var(--text-muted);
  cursor: pointer;
  white-space: nowrap;
}
.tier-select {
  flex: 1;
  min-width: 0;
  font-family: inherit;
  font-size: 0.92rem;
  padding: 9px 30px 9px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--card-bg);
  color: var(--text);
  cursor: pointer;
  /* native select arrows ignore padding-right on mobile Safari/Chrome, so
     draw our own and disable the OS one for consistent spacing everywhere */
  appearance: none;
  -webkit-appearance: none;
  -moz-appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='6' viewBox='0 0 10 6'%3E%3Cpath d='M1 1l4 4 4-4' fill='none' stroke='%235c6779' stroke-width='1.6' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 12px center;
  background-size: 10px 6px;
}
.tier-select:focus { outline: 2px solid var(--primary); outline-offset: 1px; }
.tier-select-row.tier-off .tier-select-label { color: var(--border); }
.tier-select:disabled { opacity: 0.45; cursor: not-allowed; }

/* data-URI background-images can't read var(--text-muted), so the chevron's
   dark-mode color is spelled out again here to match it (#9aa5bc). */
:root[data-theme="dark"] .tier-select {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='6' viewBox='0 0 10 6'%3E%3Cpath d='M1 1l4 4 4-4' fill='none' stroke='%239aa5bc' stroke-width='1.6' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .tier-select {
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='6' viewBox='0 0 10 6'%3E%3Cpath d='M1 1l4 4 4-4' fill='none' stroke='%239aa5bc' stroke-width='1.6' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
  }
}

@media (max-width: 480px) {
  .tier-select-row { flex-wrap: wrap; }
  .tier-select { flex-basis: 100%; }
}

.mix-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 0 2px;
  margin-top: 4px;
  border-top: 1px solid var(--border);
  font-size: 0.92rem;
  cursor: pointer;
}
.mix-row input { accent-color: var(--primary); }
.mix-row .ka-row-count { color: var(--text-muted); font-size: 0.8rem; margin-left: auto; }

.pill-radio {
  display: flex;
  align-items: center;
  gap: 8px;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
  cursor: pointer;
  font-size: 0.92rem;
  transition: border-color 0.15s, background 0.15s;
}
.pill-radio:has(input:checked) {
  border-color: var(--primary);
}
.pill-radio small {
  display: block;
  color: var(--text-muted);
  font-weight: 400;
  font-size: 0.78rem;
}
.pill-radio input { accent-color: var(--primary); }

.chip {
  display: flex;
  align-items: center;
  gap: 6px;
  background: var(--chip-bg);
  border-radius: 20px;
  padding: 7px 12px;
  font-size: 0.85rem;
  cursor: pointer;
}
.chip:has(input:checked) {
  background: var(--chip-bg-active);
}
.chip input { accent-color: var(--primary); }

.row-between {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
}
/* h3's own margin-bottom only shows up below the whole row when it doesn't
   wrap; once the chip drops to its own line, that gap ends up between the
   heading and the chip instead of below them — so give the row its own
   fixed gap and neutralize the heading's margin here. */
.review-header { margin-bottom: 14px; }
.review-header h3 { margin: 0; }

/* button pairs (Start/Review, Review mistakes/Back home) size to their own
   text by default, which makes the longer one push the other to a new row
   at medium widths. Split the row evenly instead so they always stay put. */
.row-between:has(> .btn) {
  flex-wrap: nowrap;
}
.row-between:has(> .btn) > .btn {
  flex: 1;
  min-width: 0;
}

.btn {
  border: none;
  border-radius: 8px;
  padding: 11px 18px;
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s, opacity 0.15s;
}
.btn:disabled { opacity: 0.45; cursor: not-allowed; }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover:not(:disabled) { background: var(--primary-hover); }
.btn-secondary { background: var(--chip-bg); color: var(--text); }
.btn-secondary:hover:not(:disabled) { background: var(--chip-bg-active); }
.btn-danger { background: var(--danger); color: #fff; }
.btn-danger:hover:not(:disabled) { background: var(--danger-hover); }
.btn-ghost { background: none; border: 1px solid var(--border); color: var(--text-muted); }

/* Quiz screen */
.quiz-topbar {
  display: flex;
  align-items: center;
  gap: 14px;
  margin-bottom: 16px;
}
.quiz-progress { flex: 1; }
.quiz-progress span {
  font-size: 0.82rem;
  color: var(--text-muted);
}
.progress-bar {
  background: var(--border);
  border-radius: 6px;
  height: 6px;
  margin-top: 6px;
  overflow: hidden;
}
.progress-fill {
  background: var(--primary);
  height: 100%;
  width: 0%;
  transition: width 0.2s;
}
.quiz-timer {
  font-variant-numeric: tabular-nums;
  font-weight: 700;
  padding: 6px 12px;
  background: var(--chip-bg);
  border-radius: 8px;
  font-size: 0.9rem;
}
.quiz-timer.low-time { color: var(--danger); background: var(--error-bg); }

.question-card { min-height: 320px; }
.question-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
}
.ka-tag {
  display: inline-block;
  background: var(--chip-bg-active);
  color: var(--primary);
  font-size: 0.75rem;
  font-weight: 700;
  padding: 3px 10px;
  border-radius: 20px;
}
.bookmark-btn {
  background: none;
  border: none;
  font-size: 1.4rem;
  cursor: pointer;
  color: var(--text-muted);
  line-height: 1;
  padding: 2px 4px;
}
.bookmark-btn.active { color: #e8b923; }

.question-text {
  font-size: 1.1rem;
  line-height: 1.6;
  margin: 0 0 18px;
}

.options-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.option-btn {
  display: flex;
  gap: 10px;
  text-align: left;
  border: 1px solid var(--border);
  background: var(--card-bg);
  color: var(--text);
  border-radius: 10px;
  padding: 12px 14px;
  cursor: pointer;
  font-size: 1rem;
  line-height: 1.55;
  transition: border-color 0.15s, background 0.15s;
}
.option-btn:hover:not(.locked) { border-color: var(--primary); }
.option-btn .opt-letter { font-weight: 700; flex-shrink: 0; }
.option-btn.selected { border-color: var(--primary); background: var(--chip-bg-active); }
.option-btn.correct { border-color: var(--success); background: var(--success-bg); }
.option-btn.incorrect { border-color: var(--danger); background: var(--error-bg); }
.option-btn.locked { cursor: default; }

.explanation-box {
  margin-top: 16px;
  padding: 14px 16px;
  background: var(--chip-bg);
  border-radius: 10px;
  font-size: 0.96rem;
  line-height: 1.6;
  border-left: 3px solid var(--primary);
}
.explanation-box .expl-label {
  font-weight: 700;
  margin-bottom: 6px;
  display: block;
}
.explanation-box p,
.rq-expl p {
  margin: 0 0 10px;
}
.explanation-box p:last-child,
.rq-expl p:last-child {
  margin-bottom: 0;
}

.quiz-nav {
  display: flex;
  gap: 10px;
  justify-content: space-between;
  /* never let Prev/Next/Submit wrap onto separate rows; if a very narrow
     screen or larger accessibility font size ever makes them not fit,
     scroll horizontally rather than break the icon+label apart */
  overflow-x: auto;
}
.quiz-nav .btn { flex: 1; flex-shrink: 0; white-space: nowrap; }

/* Result screen */
.result-summary { text-align: center; }
.score-big { font-size: 2.4rem; font-weight: 800; }
.score-pct { color: var(--text-muted); font-size: 1.1rem; }

.ka-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.9rem;
}
.ka-table th, .ka-table td {
  text-align: left;
  padding: 8px 6px;
  border-bottom: 1px solid var(--border);
}
.ka-table th { color: var(--text-muted); font-weight: 600; font-size: 0.8rem; }

.review-item {
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 14px 16px;
  margin-bottom: 12px;
}
.review-item.wrong { border-left: 4px solid var(--danger); }
.review-item.correct { border-left: 4px solid var(--success); }
.review-item .rq-head {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 8px;
  font-size: 0.8rem;
  color: var(--text-muted);
}
.review-item .rq-text { margin: 0 0 10px; font-weight: 600; font-size: 1rem; line-height: 1.55; }
.review-item .rq-answer { font-size: 0.94rem; margin: 2px 0; line-height: 1.55; }
.review-item .rq-answer.wrong-ans { color: var(--danger); }
.review-item .rq-answer.correct-ans { color: var(--success); }
.review-item .rq-expl {
  margin-top: 8px;
  font-size: 0.94rem;
  line-height: 1.6;
  color: var(--text-muted);
}

@media (max-width: 480px) {
  .quiz-topbar { flex-wrap: wrap; }
}

/* ---- Chinese chunk rendering (Han + Pinyin), click to hear pronunciation ---- */
:root { --han: #b3261e; --pinyin: #0f8a72; }
:root[data-theme="dark"] { --han: #e5847e; --pinyin: #5fd3b8; }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) { --han: #e5847e; --pinyin: #5fd3b8; }
}
.tv-han {
  font-family: "PingFang SC", "Microsoft YaHei", "Noto Sans SC", "Heiti SC", sans-serif;
  color: var(--han);
  font-weight: 600;
}
.tv-pinyin { color: var(--pinyin); font-style: italic; }
.tv-speak { cursor: pointer; border-radius: 3px; transition: background-color .12s; padding: 0 1px; }
.tv-speak:hover { background-color: rgba(179,38,30,.1); text-decoration: underline dotted; text-underline-offset: 2px; }
.tv-speak:active { background-color: rgba(179,38,30,.18); }
