:root, html[data-theme="light"] {
  --primary: #0b3d91;
  --primary-dark: #082a66;
  --accent: #ffb703;
  --bg: #f4f6fb;
  --card: #ffffff;
  --text: #1b2238;
  --muted: #5a6378;
  --border: #e2e6ef;
  --correct: #16a34a;
  --correct-bg: #dcfce7;
  --wrong: #dc2626;
  --wrong-bg: #fee2e2;
  --neutral-hover: #eef2fb;
  --code-bg: #f1f5f9;
  --radius: 10px;
  --shadow: 0 1px 3px rgba(10, 25, 70, 0.06), 0 4px 12px rgba(10, 25, 70, 0.06);
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

html[data-theme="dark"] {
  --primary: #6b8eea;
  --primary-dark: #8aa3ef;
  --accent: #ffb703;
  --bg: #0f1424;
  --card: #1a2138;
  --text: #e3e7f3;
  --muted: #95a0b8;
  --border: #2a334e;
  --correct: #4ade80;
  --correct-bg: #14532d;
  --wrong: #f87171;
  --wrong-bg: #5d1a1a;
  --neutral-hover: #232b46;
  --code-bg: #1e2640;
  --shadow: 0 1px 3px rgba(0, 0, 0, 0.3), 0 4px 12px rgba(0, 0, 0, 0.3);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 16px;
  line-height: 1.45;
  -webkit-text-size-adjust: 100%;
  transition: background 0.2s, color 0.2s;
}

#app {
  max-width: 760px;
  margin: 0 auto;
  padding: 16px;
  padding-bottom: 80px;
}

.screen { display: none; }
.screen.active { display: block; }

header { margin: 8px 0 16px; }

.title-row {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 12px;
  flex-wrap: wrap;
}

.theme-toggle {
  background: var(--card);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 6px 12px;
  border-radius: 6px;
  font-size: 0.82rem;
  cursor: pointer;
  white-space: nowrap;
}
.theme-toggle:hover { background: var(--neutral-hover); }

h1 {
  font-size: 1.5rem;
  margin: 0 0 4px;
  color: var(--primary);
  letter-spacing: -0.01em;
}
h2 {
  font-size: 1.05rem;
  margin: 0 0 10px;
  color: var(--primary-dark);
}
.sub {
  color: var(--muted);
  font-size: 0.9rem;
  margin: 0;
}
.hint {
  color: var(--muted);
  font-size: 0.85rem;
  margin: 0 0 12px;
}

.link-row {
  display: flex;
  flex-wrap: wrap;
  gap: 6px 10px;
  align-items: center;
}
.link-row a {
  color: var(--primary);
  font-weight: 600;
  text-decoration: underline;
  font-size: 0.85rem;
}
.link-row a.cram-link {
  background: #fff7ed;
  color: #c2410c;
  padding: 2px 8px;
  border-radius: 4px;
  text-decoration: none;
}
html[data-theme="dark"] .link-row a.cram-link {
  background: #4a2a0e;
  color: #ffb380;
}

.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 14px;
  box-shadow: var(--shadow);
}

.chapter-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 8px;
  margin-bottom: 10px;
}

.chap-item {
  display: flex;
  align-items: center;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  cursor: pointer;
  user-select: none;
  background: var(--card);
  transition: border-color 0.15s, background 0.15s;
}
.chap-item:hover { background: var(--neutral-hover); }
.chap-item.selected {
  border-color: var(--primary);
  background: var(--neutral-hover);
}
.chap-item .meta {
  margin-left: auto;
  font-size: 0.78rem;
  color: var(--muted);
  text-align: right;
  white-space: nowrap;
}
.chap-item .title {
  font-weight: 600;
  font-size: 0.95rem;
}
.chap-item .num {
  display: inline-block;
  width: 24px;
  height: 24px;
  line-height: 24px;
  text-align: center;
  background: var(--primary);
  color: #fff;
  border-radius: 50%;
  font-size: 0.78rem;
  margin-right: 10px;
  flex-shrink: 0;
}
.chap-item.heavy .num { background: #dc2626; }
.chap-item.medium .num { background: #f59e0b; }
.chap-item.light .num { background: #6b7280; }

.row { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 10px; }
.row-label {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  padding: 8px 0;
  border-bottom: 1px solid var(--border);
  color: var(--text);
}
.row-label:last-child { border-bottom: none; }
.row-label.inline { justify-content: flex-start; }
.row-label.inline span { margin-left: 6px; }
.row-label > span { font-size: 0.92rem; }
.row-label select {
  font-size: 0.92rem;
  padding: 6px 8px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--card);
  color: var(--text);
  max-width: 60%;
}

button {
  font-family: inherit;
  font-size: 0.95rem;
  padding: 10px 16px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--card);
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s, transform 0.05s;
  color: var(--text);
}
button:hover { background: var(--neutral-hover); }
button:active { transform: translateY(1px); }
button.primary {
  background: var(--primary);
  color: #fff;
  border-color: var(--primary);
}
button.primary:hover { background: var(--primary-dark); }
button.big {
  font-size: 1.05rem;
  padding: 14px 22px;
  width: 100%;
}
button.link {
  background: transparent;
  border: none;
  color: var(--primary);
  padding: 4px 8px;
  font-size: 0.85rem;
  text-decoration: underline;
}
button.link:hover { background: transparent; color: var(--primary-dark); }
button.link.danger { color: var(--wrong); }

.actions { display: flex; gap: 10px; }
.actions.row { margin-top: 14px; flex-wrap: wrap; }

.quiz-header {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 12px;
}
.progress-wrap { flex: 1; }
.progress-bar {
  width: 100%;
  height: 6px;
  background: var(--border);
  border-radius: 3px;
  overflow: hidden;
  margin-bottom: 4px;
}
#progress-fill {
  height: 100%;
  background: var(--primary);
  width: 0%;
  transition: width 0.25s ease-out;
}
.progress-text {
  font-size: 0.8rem;
  color: var(--muted);
}

.chip {
  display: inline-block;
  font-size: 0.72rem;
  padding: 3px 8px;
  background: var(--neutral-hover);
  color: var(--primary-dark);
  border-radius: 999px;
  margin-bottom: 10px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.question-text {
  font-size: 1.05rem;
  margin: 0 0 14px;
  line-height: 1.5;
  white-space: pre-wrap;
}

.options { display: grid; gap: 8px; }
.option-btn {
  text-align: left;
  padding: 12px 14px;
  border: 1px solid var(--border);
  background: var(--card);
  border-radius: 8px;
  width: 100%;
  cursor: pointer;
  font-size: 0.95rem;
  line-height: 1.4;
  display: flex;
  gap: 10px;
  align-items: flex-start;
  color: var(--text);
}
.option-btn .letter {
  font-weight: 700;
  color: var(--primary);
  min-width: 18px;
}
.option-btn:hover:not(:disabled) { background: var(--neutral-hover); }
.option-btn.correct {
  background: var(--correct-bg);
  border-color: var(--correct);
}
.option-btn.correct .letter { color: var(--correct); }
.option-btn.wrong {
  background: var(--wrong-bg);
  border-color: var(--wrong);
}
.option-btn.wrong .letter { color: var(--wrong); }
.option-btn:disabled { cursor: default; }

/* Confidence rating row */
.confidence-row {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  align-items: center;
  margin-top: 12px;
  padding: 10px 12px;
  background: #fef3c7;
  border-left: 4px solid var(--accent);
  border-radius: 6px;
}
html[data-theme="dark"] .confidence-row {
  background: #3a2f0e;
}
.confidence-row .conf-label {
  font-weight: 600;
  font-size: 0.88rem;
  margin-right: 8px;
}
.confidence-row .conf-btn {
  font-size: 0.85rem;
  padding: 6px 10px;
  border: 1px solid var(--border);
  background: var(--card);
  color: var(--text);
  border-radius: 6px;
  cursor: pointer;
}
.confidence-row .conf-btn:hover {
  background: var(--neutral-hover);
}

.feedback {
  margin-top: 12px;
  padding: 12px;
  border-radius: 8px;
  font-size: 0.92rem;
  line-height: 1.5;
}
.feedback.correct {
  background: var(--correct-bg);
  border-left: 4px solid var(--correct);
}
.feedback.wrong {
  background: var(--wrong-bg);
  border-left: 4px solid var(--wrong);
}
.feedback .verdict {
  font-weight: 700;
  display: block;
  margin-bottom: 6px;
}
.feedback .ref {
  display: block;
  margin-top: 6px;
  font-size: 0.8rem;
  color: var(--muted);
}
.feedback .walkthrough-link {
  color: var(--primary);
  font-weight: 600;
  text-decoration: underline;
  font-size: 0.85rem;
}
.lucky-tag, .dangerous-tag {
  display: inline-block;
  margin-left: 8px;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 0.72rem;
  font-weight: 700;
}
.lucky-tag {
  background: #fef3c7;
  color: #92400e;
}
.dangerous-tag {
  background: var(--wrong-bg);
  color: var(--wrong);
}
html[data-theme="dark"] .lucky-tag { background: #3a2f0e; color: #ffd166; }
html[data-theme="dark"] .dangerous-tag { background: var(--wrong-bg); color: #ffb3b3; }

.running-score {
  text-align: center;
  color: var(--muted);
  font-size: 0.85rem;
  padding: 8px;
}

.score-big {
  font-size: 2rem;
  text-align: center;
  margin: 8px 0 20px;
  font-weight: 700;
  color: var(--primary);
}

.chapter-result-row {
  display: flex;
  justify-content: space-between;
  padding: 8px 0;
  border-bottom: 1px solid var(--border);
  font-size: 0.92rem;
}
.chapter-result-row:last-child { border-bottom: none; }
.chapter-result-row .pct {
  font-weight: 700;
}
.chapter-result-row.pass .pct { color: var(--correct); }
.chapter-result-row.fail .pct { color: var(--wrong); }

.wrong-review-item {
  padding: 12px 0;
  border-bottom: 1px solid var(--border);
}
.wrong-review-item:last-child { border-bottom: none; }
.wrong-review-item .q-text {
  font-weight: 600;
  margin-bottom: 6px;
  font-size: 0.92rem;
}
.wrong-review-item .answer-line {
  font-size: 0.85rem;
  color: var(--muted);
  margin: 2px 0;
}
.wrong-review-item .answer-line.user { color: var(--wrong); }
.wrong-review-item .answer-line.right { color: var(--correct); }
.wrong-review-item .explain {
  font-size: 0.85rem;
  color: var(--text);
  margin-top: 6px;
  padding: 6px 8px;
  background: var(--bg);
  border-radius: 6px;
}
.wrong-review-item .walkthrough-link {
  color: var(--primary);
  font-weight: 600;
}

.stats-card .stat-row, .stat-row {
  display: flex;
  justify-content: space-between;
  padding: 6px 0;
  font-size: 0.9rem;
  border-bottom: 1px dotted var(--border);
}
.stats-card .stat-row:last-child { border-bottom: none; }
.stat-pass { color: var(--correct); font-weight: 700; }
.stat-fail { color: var(--wrong); font-weight: 700; }

/* Mode selector */
.mode-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}
.mode-btn {
  text-align: left;
  padding: 12px 14px;
  border: 1px solid var(--border);
  background: var(--card);
  border-radius: 8px;
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s;
  color: var(--text);
}
.mode-btn:hover:not(:disabled) { background: var(--neutral-hover); }
.mode-btn.selected {
  border-color: var(--primary);
  background: var(--neutral-hover);
  border-width: 2px;
  padding: 11px 13px;
}
.mode-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}
.mode-title {
  font-weight: 700;
  font-size: 0.98rem;
  color: var(--primary-dark);
  margin-bottom: 2px;
}
.mode-desc {
  font-size: 0.82rem;
  color: var(--muted);
  line-height: 1.35;
}

.timer {
  background: var(--primary-dark);
  color: #fff;
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  font-size: 1rem;
  padding: 6px 12px;
  border-radius: 6px;
  font-weight: 700;
  white-space: nowrap;
  letter-spacing: 0.02em;
}
.timer.warning { background: #d97706; }
.timer.danger {
  background: var(--wrong);
  animation: pulse 1s ease-in-out infinite;
}
@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.6; }
}

.mode-badge {
  display: inline-block;
  margin-left: 6px;
  padding: 2px 7px;
  border-radius: 999px;
  font-size: 0.7rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.mode-badge.review { background: #fef3c7; color: #92400e; }
.mode-badge.mock { background: var(--wrong-bg); color: var(--wrong); }
.mode-badge.smart { background: #dbeafe; color: var(--primary-dark); }
html[data-theme="dark"] .mode-badge.review { background: #3a2f0e; color: #ffd166; }
html[data-theme="dark"] .mode-badge.smart { background: #1e2c5a; color: #c4d2ff; }

.mock-nav {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  margin-top: 8px;
  padding: 0 4px;
}
.mock-nav button { font-size: 0.85rem; }

.option-btn.flagged-current { border-left: 4px solid var(--accent); }
.option-btn.selected-current {
  background: var(--neutral-hover);
  border-color: var(--primary);
}

.verdict-big {
  text-align: center;
  font-size: 1.2rem;
  font-weight: 700;
  padding: 12px;
  margin: 0 0 16px;
  border-radius: 8px;
}
.verdict-big.merit { background: var(--correct-bg); color: var(--correct); border: 2px solid var(--correct); }
.verdict-big.pass { background: var(--neutral-hover); color: var(--primary); border: 2px solid var(--primary); }
.verdict-big.fail { background: var(--wrong-bg); color: var(--wrong); border: 2px solid var(--wrong); }

/* Smart mode preview */
#smart-preview {
  background: var(--neutral-hover);
  padding: 10px 12px;
  border-radius: 6px;
  margin-bottom: 10px;
}
.smart-weaknesses {
  margin: 0;
  padding-left: 20px;
  font-size: 0.88rem;
}
.smart-weaknesses li {
  margin: 3px 0;
}

/* History table */
.history-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.85rem;
  margin-top: 6px;
}
.history-table th {
  text-align: left;
  padding: 6px 4px;
  font-size: 0.78rem;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  border-bottom: 1px solid var(--border);
}
.history-table td {
  padding: 6px 4px;
  border-bottom: 1px solid var(--border);
}
.history-table tr:last-child td { border-bottom: none; }
.history-table .pass { color: var(--correct); }
.history-table .fail { color: var(--wrong); }

/* Confidence analysis (results screen) */
.conf-section {
  padding: 10px 12px;
  border-radius: 8px;
  margin-bottom: 10px;
  font-size: 0.88rem;
}
.conf-section.lucky {
  background: #fef3c7;
  border-left: 4px solid var(--accent);
  color: #78350f;
}
.conf-section.dangerous {
  background: var(--wrong-bg);
  border-left: 4px solid var(--wrong);
  color: var(--wrong);
}
html[data-theme="dark"] .conf-section.lucky { background: #3a2f0e; color: #ffd166; }
html[data-theme="dark"] .conf-section.dangerous { background: #5d1a1a; color: #ffb3b3; }
.conf-section p { margin: 4px 0 6px; font-size: 0.85rem; }
.conf-section ul { margin: 4px 0; padding-left: 18px; font-size: 0.82rem; }

/* TODAY widget */
.today-card { padding: 12px 14px; }
.today-widget {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
  align-items: stretch;
}
@media (min-width: 600px) {
  .today-widget { grid-template-columns: repeat(4, 1fr); }
}
.today-block {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 2px;
}
.today-label {
  font-size: 0.74rem;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  font-weight: 600;
}
.today-value {
  font-size: 1.3rem;
  font-weight: 800;
  color: var(--primary-dark);
  line-height: 1.1;
}
.today-sub {
  font-size: 0.75rem;
  color: var(--muted);
}
.today-block.streak .today-value { color: #d97706; }
.today-block.streak.broken .today-value { color: var(--muted); font-size: 1rem; }
.today-block.countdown.urgent .today-value { color: var(--wrong); }
.today-block.countdown.unset {
  border-style: dashed;
}
.exam-date-input {
  background: var(--card);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 4px 6px;
  border-radius: 4px;
  font-size: 0.85rem;
  font-family: inherit;
  margin-top: 4px;
  width: 100%;
}
.today-block.target .today-value { color: var(--correct); }
.today-block.daily-action {
  align-items: stretch;
  justify-content: center;
  padding: 8px;
}
.daily-q-btn {
  width: 100%;
  padding: 12px 8px;
  font-size: 0.95rem;
  font-weight: 700;
  border: none;
  background: var(--primary);
  color: #fff;
  border-radius: 6px;
  cursor: pointer;
}
.daily-q-btn:hover { background: var(--primary-dark); }

/* Calc mode preview */
#calc-preview {
  background: var(--neutral-hover);
  padding: 10px 12px;
  border-radius: 6px;
  margin-bottom: 10px;
}

/* Topic radar */
.radar-h {
  font-size: 0.92rem;
  color: var(--primary-dark);
  margin: 14px 0 8px;
}
.radar-chapter {
  margin-bottom: 12px;
}
.radar-chapter h4 {
  font-size: 0.78rem;
  color: var(--muted);
  margin: 6px 0 4px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.radar-row {
  display: grid;
  grid-template-columns: 1fr 2fr auto auto;
  gap: 6px 8px;
  align-items: center;
  font-size: 0.8rem;
  padding: 2px 0;
}
.radar-topic {
  color: var(--text);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.radar-bar-wrap {
  background: var(--border);
  height: 8px;
  border-radius: 4px;
  overflow: hidden;
}
.radar-bar {
  height: 100%;
  border-radius: 4px;
  transition: width 0.2s;
}
.radar-bar.pass { background: var(--correct); }
.radar-bar.medium { background: #f59e0b; }
.radar-bar.fail { background: var(--wrong); }
.radar-pct {
  font-weight: 700;
  font-size: 0.78rem;
}
.radar-pct.pass { color: var(--correct); }
.radar-pct.medium { color: #f59e0b; }
.radar-pct.fail { color: var(--wrong); }
.radar-n {
  color: var(--muted);
  font-size: 0.72rem;
}

/* Mode badges add calc */
.mode-badge.calc { background: #ecfdf5; color: #047857; }
html[data-theme="dark"] .mode-badge.calc { background: #0d2a1f; color: #5eead4; }

@media (min-width: 600px) {
  .chapter-grid { grid-template-columns: 1fr 1fr; }
  .mode-grid { grid-template-columns: 1fr 1fr; }
  #app { padding: 24px; }
}
@media (min-width: 800px) {
  .mode-grid { grid-template-columns: repeat(5, 1fr); }
}
@media (max-width: 380px) {
  body { font-size: 15px; }
  .question-text { font-size: 1rem; }
  h1 { font-size: 1.3rem; }
}
