* { margin: 0; padding: 0; box-sizing: border-box; }

:root {
  --primary: #2563eb;
  --primary-dark: #1d4ed8;
  --success: #16a34a;
  --danger: #dc2626;
  --warning: #f59e0b;
  --bg: #f8fafc;
  --card: #ffffff;
  --text: #1e293b;
  --text-light: #64748b;
  --border: #e2e8f0;
  --radius: 12px;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.6;
  min-height: 100dvh;
}

/* Header */
.header {
  background: var(--primary);
  color: white;
  padding: 16px 20px;
  position: sticky;
  top: 0;
  z-index: 100;
  box-shadow: 0 2px 8px rgba(0,0,0,0.15);
}
.header h1 { font-size: 18px; font-weight: 700; }
.header .sub { font-size: 12px; opacity: 0.8; margin-top: 2px; }
.header .back-btn {
  background: rgba(255,255,255,0.2);
  border: none;
  color: white;
  padding: 6px 12px;
  border-radius: 6px;
  font-size: 14px;
  cursor: pointer;
  display: none;
}
.header .back-btn.visible { display: inline-block; }
.header-top { display: flex; align-items: center; gap: 12px; }
.header-title { flex: 1; }

/* Header tabs */
.header-tabs {
  display: flex;
  gap: 6px;
  margin-top: 10px;
}
.header-tab {
  flex: 1;
  padding: 8px 0;
  background: rgba(255,255,255,0.15);
  border: none;
  border-radius: 8px;
  color: rgba(255,255,255,0.7);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.15s;
}
.header-tab.active {
  background: white;
  color: var(--primary);
}
.header-tab:active { transform: scale(0.95); }
.header-tab.tab-back {
  flex: 0 0 40px;
  font-size: 16px;
  font-weight: 700;
  background: rgba(255,255,255,0.25);
  color: white;
}
.header-tab.tab-back.hidden { display: none; }

/* Progress bar */
.progress-wrap {
  background: rgba(255,255,255,0.2);
  border-radius: 4px;
  height: 6px;
  margin-top: 10px;
  display: none;
}
.progress-wrap.visible { display: block; }
.progress-bar {
  background: white;
  height: 100%;
  border-radius: 4px;
  transition: width 0.3s;
  width: 0%;
}

/* Main */
.container { max-width: 600px; margin: 0 auto; padding: 16px; }

/* Subject cards - Home */
.subject-grid { display: flex; flex-direction: column; gap: 12px; margin-top: 8px; }
.subject-card {
  background: var(--card);
  border-radius: var(--radius);
  padding: 18px 20px;
  box-shadow: 0 1px 3px rgba(0,0,0,0.08);
  cursor: pointer;
  transition: transform 0.15s, box-shadow 0.15s;
  border-left: 4px solid var(--primary);
}
.subject-card:active { transform: scale(0.98); }
.subject-card.top-menu { padding: 22px 20px; }
.subject-card.top-menu h3 { font-size: 18px; }
.subject-card h3 { font-size: 16px; margin-bottom: 4px; }
.subject-card .meta { font-size: 13px; color: var(--text-light); }
.subject-card .badge {
  display: inline-block;
  background: #eff6ff;
  color: var(--primary);
  font-size: 11px;
  padding: 2px 8px;
  border-radius: 10px;
  margin-top: 6px;
  font-weight: 600;
}

/* Set list */
.set-list { display: flex; flex-direction: column; gap: 8px; }
.set-item {
  background: var(--card);
  border-radius: var(--radius);
  padding: 16px 20px;
  box-shadow: 0 1px 3px rgba(0,0,0,0.08);
  cursor: pointer;
  display: flex;
  justify-content: space-between;
  align-items: center;
  transition: transform 0.15s;
}
.set-item:active { transform: scale(0.98); }
.set-item .set-name { font-weight: 600; font-size: 15px; }
.set-item .set-score { font-size: 13px; color: var(--text-light); }
.set-item .set-score.completed { color: var(--success); font-weight: 600; }
.set-item.hard { border-left: 4px solid var(--danger); background: linear-gradient(135deg, var(--card) 90%, #fee2e2); }
.set-item.hard .set-name { color: var(--danger); }

/* Quiz */
.quiz-card {
  background: var(--card);
  border-radius: var(--radius);
  padding: 20px;
  box-shadow: 0 1px 3px rgba(0,0,0,0.08);
  margin-bottom: 16px;
}
.q-number {
  font-size: 13px;
  color: var(--primary);
  font-weight: 700;
  margin-bottom: 8px;
}
.q-text {
  font-size: 15px;
  font-weight: 600;
  margin-bottom: 16px;
  line-height: 1.5;
}
.options { display: flex; flex-direction: column; gap: 8px; }
.option-btn {
  background: #f8fafc;
  border: 2px solid var(--border);
  border-radius: 10px;
  padding: 12px 16px;
  text-align: left;
  font-size: 14px;
  cursor: pointer;
  transition: all 0.15s;
  line-height: 1.4;
  display: flex;
  gap: 10px;
}
.option-btn .opt-num {
  flex-shrink: 0;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: var(--border);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  font-weight: 700;
  color: var(--text-light);
}
.option-btn:active { transform: scale(0.98); }
.option-btn.selected { border-color: var(--primary); background: #eff6ff; }
.option-btn.selected .opt-num { background: var(--primary); color: white; }
.option-btn.correct { border-color: var(--success); background: #f0fdf4; }
.option-btn.correct .opt-num { background: var(--success); color: white; }
.option-btn.wrong { border-color: var(--danger); background: #fef2f2; }
.option-btn.wrong .opt-num { background: var(--danger); color: white; }
.option-btn.disabled { pointer-events: none; }

/* Explanation */
.explanation {
  margin-top: 12px;
  padding: 14px 16px;
  background: #f0fdf4;
  border-radius: 10px;
  border-left: 3px solid var(--success);
  font-size: 13px;
  line-height: 1.5;
  display: none;
}
.explanation.visible { display: block; }
.explanation.wrong-bg { background: #fef2f2; border-left-color: var(--danger); }
.explanation strong { display: block; margin-bottom: 4px; font-size: 13px; }
.theory-link-btn {
  display: inline-block;
  margin-top: 10px;
  padding: 8px 16px;
  background: var(--primary);
  color: white;
  border: none;
  border-radius: 8px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.15s;
}
.theory-link-btn:active { transform: scale(0.96); opacity: 0.9; }

/* Navigation */
.quiz-nav {
  display: flex;
  gap: 10px;
  margin-top: 8px;
}
.quiz-nav button {
  flex: 1;
  padding: 14px;
  border: none;
  border-radius: 10px;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.15s;
}
.btn-prev { background: var(--border); color: var(--text); }
.btn-next { background: var(--primary); color: white; }
.btn-next:active { background: var(--primary-dark); }

/* Result */
.result-card {
  background: var(--card);
  border-radius: var(--radius);
  padding: 30px 20px;
  text-align: center;
  box-shadow: 0 1px 3px rgba(0,0,0,0.08);
}
.result-score {
  font-size: 48px;
  font-weight: 800;
  color: var(--primary);
  margin: 12px 0;
}
.result-score.pass { color: var(--success); }
.result-score.fail { color: var(--danger); }
.result-label { font-size: 14px; color: var(--text-light); }
.result-badge {
  display: inline-block;
  padding: 6px 20px;
  border-radius: 20px;
  font-weight: 700;
  font-size: 14px;
  margin-top: 8px;
}
.result-badge.pass { background: #dcfce7; color: var(--success); }
.result-badge.fail { background: #fee2e2; color: var(--danger); }
.result-actions { margin-top: 20px; display: flex; flex-direction: column; gap: 10px; }
.result-actions button {
  padding: 14px;
  border: none;
  border-radius: 10px;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
}
.btn-review { background: var(--border); color: var(--text); }
.btn-retry { background: var(--primary); color: white; }
.btn-home { background: #f1f5f9; color: var(--text-light); }

/* Stats on home */
.stats-bar {
  display: flex;
  gap: 8px;
  margin-bottom: 16px;
}
.stat-item {
  flex: 1;
  background: var(--card);
  border-radius: 10px;
  padding: 12px;
  text-align: center;
  box-shadow: 0 1px 3px rgba(0,0,0,0.08);
}
.stat-num { font-size: 22px; font-weight: 800; color: var(--primary); }
.stat-label { font-size: 11px; color: var(--text-light); margin-top: 2px; }

/* Chapter quick nav */
.chapter-nav {
  display: flex;
  gap: 6px;
  padding: 10px 0;
  position: sticky;
  top: 0;
  z-index: 50;
  background: var(--bg);
  margin-bottom: 4px;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
}
.chapter-nav::-webkit-scrollbar { display: none; }
.chapter-nav-btn {
  padding: 8px 12px;
  background: var(--card);
  border: 1.5px solid var(--border);
  border-radius: 20px;
  font-size: 12px;
  font-weight: 600;
  color: var(--primary);
  cursor: pointer;
  white-space: nowrap;
  transition: all 0.15s;
  box-shadow: 0 1px 2px rgba(0,0,0,0.06);
}
.chapter-nav-btn:active {
  background: var(--primary);
  color: white;
  border-color: var(--primary);
  transform: scale(0.95);
}
@media (prefers-color-scheme: dark) {
  .chapter-nav { background: var(--bg); }
  .chapter-nav-btn { background: var(--card); border-color: var(--border); color: #93c5fd; }
}

/* ===== Theory Body Styles ===== */
.theory-body { font-size: 14px; line-height: 1.8; color: var(--text); }
.theory-body h4 {
  font-size: 16px;
  font-weight: 700;
  color: var(--primary);
  margin: 24px 0 10px;
  padding-bottom: 6px;
  border-bottom: 2px solid #dbeafe;
  display: flex;
  align-items: center;
  gap: 6px;
}
.theory-body h4::before {
  content: '';
  display: inline-block;
  width: 4px;
  height: 18px;
  background: var(--primary);
  border-radius: 2px;
  flex-shrink: 0;
}
.theory-body p { margin: 8px 0; }
.theory-body strong { color: #1e40af; background: #eff6ff; padding: 1px 4px; border-radius: 3px; }
.theory-body ul, .theory-body ol {
  margin: 8px 0 8px 8px;
  padding-left: 16px;
}
.theory-body li {
  margin: 6px 0;
  padding-left: 4px;
}
.theory-body li::marker { color: var(--primary); }

/* Theory highlight boxes (from data files) */
.theory-body div[style*="background:#f0f4ff"],
.theory-body div[style*="background: #f0f4ff"] {
  border-left: 4px solid var(--primary) !important;
  font-size: 13px !important;
}

/* Theory tables */
.theory-body table {
  width: 100%;
  border-collapse: collapse;
  margin: 12px 0;
  font-size: 13px;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 1px 3px rgba(0,0,0,0.08);
}
.theory-body th {
  background: var(--primary);
  color: white;
  padding: 10px 12px;
  text-align: left;
  font-weight: 600;
  font-size: 12px;
}
.theory-body td {
  padding: 8px 12px;
  border-bottom: 1px solid var(--border);
  vertical-align: top;
}
.theory-body tr:nth-child(even) td { background: #f8fafc; }
.theory-body tr:last-child td { border-bottom: none; }

/* Theory chapter card styling */
#theoryView .quiz-card {
  padding: 24px 20px;
  margin-bottom: 20px;
  border-top: 4px solid var(--primary);
}
#theoryView .quiz-card .q-number {
  font-size: 17px;
  font-weight: 800;
  color: var(--primary);
  margin-bottom: 16px;
  padding-bottom: 8px;
  border-bottom: 1px solid var(--border);
}

/* Key concept cards inline */
.theory-body .concept-box {
  background: linear-gradient(135deg, #eff6ff, #f8fafc);
  border: 1px solid #dbeafe;
  border-radius: 10px;
  padding: 14px 16px;
  margin: 12px 0;
}

/* Exam tip box styling */
.theory-body div[style*="background:#fff7ed"],
.theory-body div[style*="background: #fff7ed"] {
  border-left: 4px solid var(--warning) !important;
}

/* Exam tips card */
.exam-tips-card { border-top: 4px solid #f43f5e !important; }
.exam-tips-card .q-number { color: #f43f5e !important; }

/* Dark mode theory */
@media (prefers-color-scheme: dark) {
  .theory-body strong { color: #93c5fd; background: #1e3a5f; }
  .theory-body h4 { color: #93c5fd; border-bottom-color: #334155; }
  .theory-body h4::before { background: #3b82f6; }
  .theory-body th { background: #1e3a5f; }
  .theory-body td { border-bottom-color: #334155; }
  .theory-body tr:nth-child(even) td { background: #1e293b; }
  #theoryView .quiz-card { border-top-color: #3b82f6; }
}

/* Mode buttons in set list / exam list */
.mode-btns { display: flex; gap: 6px; margin-top: 8px; }
.mode-btn {
  padding: 6px 14px;
  border: 1.5px solid var(--primary);
  border-radius: 8px;
  background: white;
  color: var(--primary);
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
}
.mode-btn.read { border-color: #f59e0b; color: #b45309; }
.mode-btn.active { background: var(--primary); color: white; }
.mode-btn:active { transform: scale(0.95); }

/* Star bookmark */
.star {
  background: none;
  border: none;
  font-size: 22px;
  color: #cbd5e1;
  cursor: pointer;
  padding: 4px;
  line-height: 1;
}
.star.active { color: #f59e0b; }
.read-header { display: flex; justify-content: space-between; align-items: center; }

/* Read mode card */
.read-card { border-left: 3px solid var(--primary); }
.read-card .explanation { display: block; }
.read-top-bar { position: sticky; top: 0; z-index: 50; background: var(--bg); padding: 8px 0; }

/* Bookmark view */
.bm-set-divider {
  font-size: 15px;
  font-weight: 700;
  color: var(--primary);
  padding: 12px 0 6px;
  margin-top: 8px;
  border-bottom: 2px solid #dbeafe;
}
.bm-set-label { font-size: 13px; font-weight: 600; color: var(--text-light); margin: 12px 0 6px; }
.bm-questions { display: flex; flex-wrap: wrap; gap: 6px; }
.bm-q-chip {
  padding: 6px 12px;
  background: #eff6ff;
  color: var(--primary);
  border-radius: 8px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
}
.bm-q-chip:active { background: var(--primary); color: white; }

/* Set item layout fix for mode buttons */
.set-item { flex-wrap: wrap; }

/* Dark mode extras */
@media (prefers-color-scheme: dark) {
  .mode-btn { background: var(--card); }
  .star { color: #475569; }
  .star.active { color: #f59e0b; }
  .bm-q-chip { background: #1e3a5f; }
  .read-top-bar { background: var(--bg); }
}

/* Fade transition */
.view { display: none; animation: fadeIn 0.2s; }
.view.active { display: block; }
@keyframes fadeIn { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: translateY(0); } }

/* Scrollbar - always visible */
html { overflow-y: scroll; }
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: #f1f5f9; }
::-webkit-scrollbar-thumb { background: #94a3b8; border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: #64748b; }

/* Dark mode support */
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f172a;
    --card: #1e293b;
    --text: #f1f5f9;
    --text-light: #94a3b8;
    --border: #334155;
  }
  .option-btn { background: #0f172a; }
  .option-btn.selected { background: #1e3a5f; }
  .option-btn.correct { background: #14532d; border-color: var(--success); }
  .option-btn.wrong { background: #7f1d1d; border-color: var(--danger); }
  .explanation { background: #14532d; }
  .explanation.wrong-bg { background: #7f1d1d; }
  .subject-card .badge { background: #1e3a5f; }
}
