:root {
  --bg: #f8f6f2;
  --bg2: #efeae3;
  --bg3: #e2ddd5;
  --text: #2d2a26;
  --text2: #7a756e;
  --accent: #5b7fa6;
  --accent-hover: #4a6e95;
  --correct: #6b9e78;
  --correct-bg: #dceee0;
  --wrong: #c4645a;
  --wrong-bg: #f5ddd9;
  --border: #d4cfc7;
  --card-bg: #ffffff;
  --card-shadow: 0 2px 16px rgba(45,42,38,0.06);
  --radius: 14px;
  --header-gradient: linear-gradient(135deg, #5b7fa6 0%, #7a9bb8 50%, #a3b5c8 100%);
}
[data-theme="dark"] {
  --bg: #1a1917;
  --bg2: #262420;
  --bg3: #33302b;
  --text: #e8e4de;
  --text2: #9e9a93;
  --accent: #7da3c7;
  --accent-hover: #93b5d5;
  --correct: #7db88a;
  --correct-bg: #1e2e22;
  --wrong: #d47a72;
  --wrong-bg: #2e1e1c;
  --border: #4a4640;
  --card-bg: #262420;
  --card-shadow: 0 2px 16px rgba(0,0,0,0.25);
  --header-gradient: linear-gradient(135deg, #3d5a7a 0%, #4a6e8f 50%, #5b7fa6 100%);
}
* { margin: 0; padding: 0; box-sizing: border-box; }
body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.6;
  min-height: 100vh;
  transition: background 0.3s, color 0.3s;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}
.container {
  max-width: 720px;
  margin: 0 auto;
  padding: 20px;
  width: 100%;
}
/* Header */
.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 14px 20px;
  margin: -20px -20px 20px -20px;
  background: var(--header-gradient);
  border-radius: 0 0 var(--radius) var(--radius);
  color: #fff;
}
.header h1 {
  font-size: 1.2rem;
  font-weight: 600;
  color: #fff;
  text-shadow: 0 1px 2px rgba(0,0,0,0.15);
}
.header-right {
  display: flex;
  align-items: center;
  gap: 10px;
}
/* Timer */
.timer-display {
  font-size: 0.85rem;
  font-weight: 600;
  color: rgba(255,255,255,0.9);
  background: rgba(255,255,255,0.15);
  padding: 4px 10px;
  border-radius: 8px;
  font-variant-numeric: tabular-nums;
}
.timer-display .q-timer {
  color: rgba(255,255,255,0.7);
  font-size: 0.75rem;
  margin-left: 6px;
}
.theme-toggle {
  background: rgba(255,255,255,0.2);
  border: none;
  border-radius: 20px;
  padding: 6px 14px;
  cursor: pointer;
  font-size: 1.1rem;
  color: #fff;
  color: var(--text);
  transition: background 0.2s;
}
.theme-toggle:hover { background: var(--border); }

/* Progress bar */
.progress-container {
  background: var(--bg3);
  border-radius: 8px;
  height: 8px;
  margin-bottom: 8px;
  overflow: hidden;
}
.progress-bar {
  height: 100%;
  background: var(--accent);
  border-radius: 8px;
  transition: width 0.4s ease;
  width: 0%;
}
.progress-text {
  font-size: 0.85rem;
  color: var(--text2);
  margin-bottom: 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.exit-btn {
  background: none;
  border: 1px solid var(--border);
  color: var(--text2);
  padding: 4px 12px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 0.8rem;
  transition: all 0.2s;
}
.exit-btn:hover {
  background: var(--wrong-bg);
  color: var(--wrong);
  border-color: var(--wrong);
}

/* Cards */
.card {
  background: var(--card-bg);
  border-radius: var(--radius);
  box-shadow: var(--card-shadow);
  padding: 28px;
  margin-bottom: 20px;
  border: 1px solid var(--border);
}

/* Course selection */
.course-select { text-align: center; padding: 40px 20px; }
.course-select h2 { font-size: 1.8rem; margin-bottom: 8px; }
.course-select p { color: var(--text2); margin-bottom: 32px; font-size: 1.05rem; }
.course-btn {
  display: block;
  width: 100%;
  padding: 18px 24px;
  margin-bottom: 14px;
  background: var(--bg2);
  border: 2px solid var(--border);
  border-radius: var(--radius);
  font-size: 1.05rem;
  font-weight: 600;
  color: var(--text);
  cursor: pointer;
  transition: all 0.2s;
  text-align: left;
}
.course-btn:hover {
  border-color: var(--accent);
  background: var(--bg3);
  transform: translateY(-1px);
}
.course-btn .course-code { color: var(--accent); }
.course-btn .course-desc {
  display: block;
  font-size: 0.85rem;
  font-weight: 400;
  color: var(--text2);
  margin-top: 4px;
}

/* Mode selection */
.mode-select { text-align: center; }
.mode-select h2 { font-size: 1.4rem; margin-bottom: 6px; }
.mode-select .subtitle { color: var(--text2); margin-bottom: 24px; }
.mode-btn {
  display: block;
  width: 100%;
  padding: 16px 20px;
  margin-bottom: 10px;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: var(--radius);
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
}
.mode-btn:hover { background: var(--accent-hover); transform: translateY(-1px); }
.mode-btn.secondary {
  background: var(--bg2);
  color: var(--text);
  border: 2px solid var(--border);
}
.mode-btn.secondary:hover { border-color: var(--accent); }
/* Mode section divider */
.mode-divider {
  font-size: 0.8rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text2);
  margin: 16px 0 8px 0;
  padding-top: 12px;
  border-top: 1px solid var(--border);
  text-align: center;
}

.back-link {
  display: inline-block;
  margin-top: 16px;
  color: var(--accent);
  cursor: pointer;
  font-size: 0.9rem;
  border: none;
  background: none;
  text-decoration: underline;
}

/* Shuffle toggle */
.shuffle-toggle {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-top: 12px;
  padding: 10px 16px;
  justify-content: center;
}
.toggle-label {
  position: relative;
  display: inline-block;
  width: 44px;
  height: 24px;
  cursor: pointer;
}
.toggle-label input {
  opacity: 0;
  width: 0;
  height: 0;
}
.toggle-slider {
  position: absolute;
  inset: 0;
  background: var(--bg3);
  border-radius: 24px;
  border: 2px solid var(--border);
  transition: all 0.3s;
}
.toggle-slider::before {
  content: '';
  position: absolute;
  width: 16px;
  height: 16px;
  left: 2px;
  bottom: 2px;
  background: var(--text2);
  border-radius: 50%;
  transition: all 0.3s;
}
.toggle-label input:checked + .toggle-slider {
  background: var(--accent);
  border-color: var(--accent);
}
.toggle-label input:checked + .toggle-slider::before {
  transform: translateX(20px);
  background: #fff;
}
.toggle-text {
  font-size: 0.9rem;
  color: var(--text2);
  font-weight: 500;
}

/* Custom random count input */
.custom-random {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-top: 10px;
  padding: 10px 16px;
  background: var(--bg2);
  border: 2px solid var(--border);
  border-radius: var(--radius);
}
.custom-random label {
  font-size: 0.95rem;
  font-weight: 600;
  color: var(--text);
  white-space: nowrap;
}
.custom-input {
  flex: 1;
  padding: 10px 12px;
  border: 2px solid var(--border);
  border-radius: 8px;
  background: var(--card-bg);
  color: var(--text);
  font-size: 1rem;
  font-weight: 600;
  text-align: center;
  max-width: 100px;
  -moz-appearance: textfield;
}
.custom-input::-webkit-outer-spin-button,
.custom-input::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }
.custom-input:focus { outline: none; border-color: var(--accent); }
.custom-go {
  padding: 10px 20px !important;
  margin-bottom: 0 !important;
  width: auto !important;
  flex-shrink: 0;
}

/* Question */
.q-tier {
  display: inline-block;
  font-size: 0.75rem;
  font-weight: 600;
  padding: 3px 10px;
  border-radius: 20px;
  margin-bottom: 12px;
  background: var(--bg3);
  color: var(--text2);
}
.q-number {
  font-size: 0.9rem;
  color: var(--text2);
  margin-bottom: 6px;
}
.q-text {
  font-size: 1.1rem;
  font-weight: 600;
  margin-bottom: 20px;
  line-height: 1.5;
}
.q-text .MathJax { font-size: 1em !important; }

/* Question tables */
.q-table {
  border-collapse: collapse;
  margin: 12px 0;
  font-size: 0.92rem;
  font-weight: 400;
  width: auto;
  min-width: 60%;
}
.q-table th, .q-table td {
  border: 1px solid var(--border);
  padding: 6px 14px;
  text-align: center;
}
.q-table th {
  background: var(--bg3);
  font-weight: 700;
  color: var(--text);
}
.q-table td { background: var(--card-bg); }
.q-table tr:nth-child(even) td { background: var(--bg2); }
.q-table td:first-child, .q-table th:first-child { text-align: left; font-weight: 600; }

/* Options */
.option-btn {
  display: block;
  width: 100%;
  padding: 14px 18px;
  margin-bottom: 10px;
  background: var(--bg2);
  border: 2px solid var(--border);
  border-radius: 10px;
  font-size: 0.95rem;
  color: var(--text);
  cursor: pointer;
  text-align: left;
  transition: all 0.15s;
  line-height: 1.5;
}
.option-btn:hover:not(.disabled) {
  border-color: var(--accent);
  background: var(--bg3);
}
.option-btn.disabled { cursor: default; opacity: 0.85; }
.option-btn.correct {
  border-color: var(--correct) !important;
  background: var(--correct-bg) !important;
  opacity: 1 !important;
}
.option-btn.wrong {
  border-color: var(--wrong) !important;
  background: var(--wrong-bg) !important;
  opacity: 1 !important;
}
.option-letter {
  display: inline-block;
  width: 28px;
  height: 28px;
  line-height: 28px;
  text-align: center;
  border-radius: 50%;
  background: var(--bg3);
  font-weight: 700;
  font-size: 0.85rem;
  margin-right: 12px;
  flex-shrink: 0;
}
.option-btn.correct .option-letter { background: var(--correct); color: #fff; }
.option-btn.wrong .option-letter { background: var(--wrong); color: #fff; }
.option-content { display: flex; align-items: flex-start; }
.option-text { flex: 1; }

/* Feedback */
.feedback {
  margin-top: 16px;
  padding: 16px;
  border-radius: 10px;
  font-size: 0.95rem;
  display: none;
}
.feedback.correct-fb {
  background: var(--correct-bg);
  border: 1px solid var(--correct);
}
.feedback.wrong-fb {
  background: var(--wrong-bg);
  border: 1px solid var(--wrong);
}
.feedback .fb-title {
  font-weight: 700;
  font-size: 1.05rem;
  margin-bottom: 8px;
}
.feedback .fb-explanation {
  color: var(--text);
  line-height: 1.6;
}

/* Next button */
.next-btn {
  display: none;
  width: 100%;
  padding: 14px;
  margin-top: 16px;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: var(--radius);
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s;
}
.next-btn:hover { background: var(--accent-hover); }

/* Summary */
.summary { text-align: center; }
.summary h2 { font-size: 1.6rem; margin-bottom: 8px; }
.score-display {
  font-size: 3rem;
  font-weight: 800;
  color: var(--accent);
  margin: 16px 0;
}
.score-pct {
  font-size: 1.2rem;
  color: var(--text2);
  margin-bottom: 24px;
}
.score-bar-container {
  background: var(--bg3);
  border-radius: 10px;
  height: 14px;
  margin-bottom: 28px;
  overflow: hidden;
}
.score-bar {
  height: 100%;
  border-radius: 10px;
  transition: width 1s ease;
}
.summary-actions {
  display: flex;
  gap: 12px;
  margin-bottom: 28px;
}
.summary-actions button {
  flex: 1;
  padding: 14px;
  border-radius: var(--radius);
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  border: none;
  transition: all 0.2s;
}
.restart-btn { background: var(--accent); color: #fff; }
.restart-btn:hover { background: var(--accent-hover); }
.switch-btn { background: var(--bg2); color: var(--text); border: 2px solid var(--border) !important; }
.switch-btn:hover { border-color: var(--accent) !important; }

/* Review list */
.review-list { text-align: left; }
.review-item {
  padding: 14px;
  border-radius: 10px;
  margin-bottom: 8px;
  background: var(--bg2);
  border: 1px solid var(--border);
}
.review-item.review-correct { border-left: 4px solid var(--correct); }
.review-item.review-wrong { border-left: 4px solid var(--wrong); }
.review-q {
  font-weight: 600;
  font-size: 0.9rem;
  margin-bottom: 4px;
}
.review-detail {
  font-size: 0.85rem;
  color: var(--text2);
  line-height: 1.5;
}
.review-detail strong { color: var(--text); }
.review-filter {
  display: flex;
  gap: 8px;
  margin-bottom: 16px;
  justify-content: center;
}
.filter-btn {
  padding: 6px 16px;
  border-radius: 20px;
  border: 1px solid var(--border);
  background: var(--bg2);
  color: var(--text);
  cursor: pointer;
  font-size: 0.85rem;
  font-weight: 500;
}
.filter-btn.active {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
}

/* ===== RESPONSIVE: Tablet (≤ 768px) ===== */
@media (max-width: 768px) {
  .container { padding: 16px; }
  .card { padding: 22px; }
  .header { padding: 12px 16px; margin: -16px -16px 16px -16px; }
  .header h1 { font-size: 1.1rem; }
  .course-select { padding: 30px 16px; }
  .course-select h2 { font-size: 1.5rem; }
  .course-btn { padding: 16px 20px; font-size: 1rem; }
  .mode-btn { padding: 14px 18px; }
  .q-text { font-size: 1.05rem; }
  .option-btn { padding: 13px 16px; }
  .score-display { font-size: 2.6rem; }
  .sg-modal { padding: 24px; max-width: 95vw; }
  .fs-modal { padding: 24px; max-width: 95vw; }
  .q-table { font-size: 0.85rem; }
  .q-table th, .q-table td { padding: 5px 10px; }
}

/* ===== RESPONSIVE: Phone (≤ 480px) ===== */
@media (max-width: 480px) {
  .container { padding: 10px; }
  .card { padding: 16px; }
  .header {
    padding: 10px 12px;
    margin: -10px -10px 12px -10px;
    flex-wrap: wrap;
    gap: 6px;
  }
  .header h1 { font-size: 0.95rem; max-width: 55%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .header-right { gap: 6px; }
  .timer-display { font-size: 0.75rem; padding: 3px 8px; }
  .timer-display .q-timer { font-size: 0.65rem; margin-left: 4px; }
  .theme-toggle { padding: 5px 10px; font-size: 0.95rem; }

  .course-select { padding: 24px 12px; }
  .course-select h2 { font-size: 1.3rem; }
  .course-select p { font-size: 0.92rem; margin-bottom: 20px; }
  .course-btn { padding: 14px 16px; font-size: 0.95rem; }
  .course-btn .course-desc { font-size: 0.78rem; }

  .mode-select h2 { font-size: 1.2rem; }
  .mode-btn { padding: 13px 16px; font-size: 0.92rem; }

  .progress-text { font-size: 0.78rem; gap: 6px; flex-wrap: wrap; }
  .exit-btn { padding: 3px 10px; font-size: 0.75rem; }
  .formula-btn { padding: 3px 8px; font-size: 0.82rem; }

  .q-tier { font-size: 0.7rem; padding: 2px 8px; }
  .q-number { font-size: 0.82rem; }
  .q-text { font-size: 0.95rem; margin-bottom: 16px; }
  .option-btn { padding: 11px 12px; font-size: 0.88rem; margin-bottom: 8px; }
  .option-letter { width: 24px; height: 24px; line-height: 24px; font-size: 0.78rem; margin-right: 10px; }

  .feedback { padding: 12px; font-size: 0.88rem; }
  .feedback .fb-title { font-size: 0.95rem; }

  .nav-buttons { gap: 8px; }
  .prev-btn { padding: 10px 14px; font-size: 0.88rem; }
  .next-btn { padding: 12px; font-size: 0.92rem; }

  .summary h2 { font-size: 1.3rem; }
  .score-display { font-size: 2.2rem; }
  .score-pct { font-size: 1rem; }
  .summary-actions { flex-direction: column; gap: 8px; }
  .summary-actions button { padding: 12px; font-size: 0.92rem; }
  .review-filter { gap: 6px; flex-wrap: wrap; }
  .filter-btn { padding: 5px 12px; font-size: 0.8rem; }
  .review-item { padding: 10px; }
  .review-q { font-size: 0.85rem; }
  .review-detail { font-size: 0.8rem; }

  /* Modals on phone: full-screen style */
  .sg-overlay, .fs-overlay { padding: 8px; }
  .sg-modal, .fs-modal {
    padding: 16px;
    margin: 8px auto;
    max-height: 95vh;
    border-radius: 10px;
  }
  .sg-modal h2 { font-size: 1.2rem; }
  .sg-modal h3 { font-size: 1rem; }
  .sg-modal ul { font-size: 0.85rem; margin-left: 14px; }
  .fs-modal h2 { font-size: 1.2rem; }
  .fs-section h3 { font-size: 0.88rem; }
  .fs-name { font-size: 0.78rem; min-width: auto; }
  .fs-latex { font-size: 0.88rem; }

  /* Tables scroll horizontally on small screens */
  .q-table { font-size: 0.8rem; display: block; overflow-x: auto; white-space: nowrap; }
  .q-table th, .q-table td { padding: 4px 8px; }
}

/* ===== RESPONSIVE: Very small phones (≤ 360px) ===== */
@media (max-width: 360px) {
  .container { padding: 6px; }
  .card { padding: 12px; }
  .header h1 { font-size: 0.85rem; max-width: 50%; }
  .course-select h2 { font-size: 1.15rem; }
  .q-text { font-size: 0.9rem; }
  .option-btn { padding: 10px; font-size: 0.84rem; }
  .option-letter { width: 22px; height: 22px; line-height: 22px; font-size: 0.72rem; margin-right: 8px; }
  .score-display { font-size: 1.8rem; }
}

/* Nav buttons */
.nav-buttons {
  display: flex;
  gap: 10px;
  margin-top: 16px;
}
.prev-btn {
  flex: 0 0 auto;
  padding: 12px 20px;
  background: var(--bg3);
  color: var(--text2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  cursor: pointer;
  font-size: 0.95rem;
  font-weight: 500;
  transition: all 0.2s;
}
.prev-btn:hover { background: var(--bg2); color: var(--text); }

/* Animations */
@keyframes fadeIn {
  from { opacity: 0; transform: translateY(10px); }
  to { opacity: 1; transform: translateY(0); }
}
.fade-in { animation: fadeIn 0.3s ease; }

/* Hide screens */
.screen { display: none; }
.screen.active { display: block; }

/* Scrollbar */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: var(--bg); }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }

/* Study Guide Modal */
.sg-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.55);
  z-index: 1000;
  overflow-y: auto;
  padding: 24px 12px;
}
.sg-overlay.active { display: flex; justify-content: center; align-items: flex-start; }
.sg-modal {
  background: var(--card-bg);
  border-radius: var(--radius);
  box-shadow: 0 8px 40px rgba(0,0,0,0.3);
  max-width: 800px;
  width: 100%;
  padding: 32px;
  position: relative;
  margin: 20px auto;
  max-height: 90vh;
  overflow-y: auto;
}
.sg-close {
  position: sticky;
  top: 0;
  float: right;
  background: var(--bg3);
  border: 2px solid var(--border);
  border-radius: 50%;
  width: 40px;
  height: 40px;
  font-size: 1.3rem;
  cursor: pointer;
  color: var(--text);
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}
.sg-close:hover, .sg-close:active { background: var(--wrong-bg); color: var(--wrong); border-color: var(--wrong); }
.sg-modal h2 { font-size: 1.5rem; margin-bottom: 16px; color: var(--accent); }
.sg-modal h3 {
  font-size: 1.1rem;
  margin: 18px 0 8px 0;
  padding-bottom: 4px;
  border-bottom: 2px solid var(--bg3);
  color: var(--text);
}
.sg-modal h4 {
  font-size: 0.95rem;
  margin: 12px 0 4px 0;
  color: var(--accent);
}
.sg-modal ul {
  margin: 4px 0 8px 18px;
  font-size: 0.9rem;
  line-height: 1.7;
}
.sg-modal li { margin-bottom: 2px; }
.sg-modal .sg-formula {
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 14px;
  margin: 6px 0;
  font-size: 0.92rem;
  overflow-x: auto;
}
.sg-modal .sg-section {
  margin-bottom: 20px;
}
.sg-modal code {
  background: var(--bg3);
  padding: 1px 5px;
  border-radius: 4px;
  font-size: 0.88rem;
}

/* Formula Sheet Button */
.formula-btn {
  background: none;
  border: 1px solid var(--border);
  color: var(--text2);
  padding: 4px 10px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 0.9rem;
  transition: all 0.2s;
  line-height: 1;
}
.formula-btn:hover {
  background: var(--bg3);
  border-color: var(--accent);
  color: var(--accent);
}

/* Formula Sheet Modal */
.fs-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.55);
  z-index: 1000;
  overflow-y: auto;
  padding: 24px 12px;
}
.fs-overlay.active { display: flex; justify-content: center; align-items: flex-start; }
.fs-modal {
  background: var(--card-bg);
  border-radius: var(--radius);
  box-shadow: 0 8px 40px rgba(0,0,0,0.3);
  max-width: 700px;
  width: 100%;
  padding: 28px;
  position: relative;
  margin: 20px auto;
  max-height: 90vh;
  overflow-y: auto;
}
.fs-close {
  position: sticky;
  top: 0;
  float: right;
  background: var(--bg3);
  border: 2px solid var(--border);
  border-radius: 50%;
  width: 40px;
  height: 40px;
  font-size: 1.3rem;
  cursor: pointer;
  color: var(--text);
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}
.fs-close:hover, .fs-close:active { background: var(--wrong-bg); color: var(--wrong); border-color: var(--wrong); }
.fs-modal h2 { font-size: 1.4rem; margin-bottom: 4px; color: var(--accent); }
.fs-section {
  margin-bottom: 20px;
}
.fs-section h3 {
  font-size: 0.95rem;
  font-weight: 700;
  color: var(--text);
  margin-bottom: 8px;
  padding-bottom: 4px;
  border-bottom: 2px solid var(--bg3);
}
.fs-formula {
  display: flex;
  align-items: baseline;
  gap: 12px;
  padding: 6px 12px;
  margin-bottom: 4px;
  border-radius: 8px;
  background: var(--bg2);
  border: 1px solid var(--border);
}
.fs-formula:hover {
  border-color: var(--accent);
}
.fs-name {
  font-size: 0.82rem;
  font-weight: 600;
  color: var(--text2);
  min-width: 140px;
  flex-shrink: 0;
}
.fs-latex {
  font-size: 0.95rem;
  color: var(--text);
  overflow-x: auto;
}

/* Bottom close button for modals (visible on all devices, especially useful on touch) */
.modal-close-bottom {
  display: block;
  width: 100%;
  margin-top: 24px;
  padding: 14px;
  background: var(--bg3);
  color: var(--text);
  border: 2px solid var(--border);
  border-radius: var(--radius);
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}
.modal-close-bottom:hover, .modal-close-bottom:active {
  background: var(--wrong-bg);
  color: var(--wrong);
  border-color: var(--wrong);
}

@media (max-width: 600px) {
  .fs-formula {
    flex-direction: column;
    gap: 2px;
  }
  .fs-name {
    min-width: auto;
    font-size: 0.78rem;
  }
}

/* ===== TOUCH-FRIENDLY: Larger tap targets for mobile/tablet ===== */
@media (hover: none) and (pointer: coarse) {
  /* Minimum 44px tap targets per Apple HIG */
  .option-btn { min-height: 48px; }
  .course-btn { min-height: 56px; }
  .mode-btn { min-height: 48px; }
  .prev-btn, .next-btn { min-height: 44px; }
  .exit-btn { min-height: 36px; padding: 6px 14px; }
  .formula-btn { min-height: 36px; padding: 6px 12px; }
  .theme-toggle { min-height: 36px; min-width: 36px; }
  .filter-btn { min-height: 36px; padding: 8px 16px; }
  .back-link { min-height: 44px; display: inline-flex; align-items: center; }
  .sg-close, .fs-close {
    width: 44px;
    height: 44px;
    font-size: 1.4rem;
  }
  /* Prevent double-tap zoom on buttons */
  button { touch-action: manipulation; }
}

/* ===== SAFE AREA: iPhone notch / home indicator ===== */
@supports (padding-bottom: env(safe-area-inset-bottom)) {
  .container {
    padding-left: max(env(safe-area-inset-left), 10px);
    padding-right: max(env(safe-area-inset-right), 10px);
    padding-bottom: env(safe-area-inset-bottom);
  }
  .sg-overlay, .fs-overlay {
    padding-bottom: env(safe-area-inset-bottom);
  }
}

/* ===== LANDSCAPE PHONE ===== */
@media (max-height: 500px) and (orientation: landscape) {
  .header { padding: 8px 12px; margin-bottom: 8px; }
  .header h1 { font-size: 0.9rem; }
  .card { padding: 14px; margin-bottom: 12px; }
  .course-select { padding: 16px 12px; }
  .course-select h2 { font-size: 1.2rem; margin-bottom: 4px; }
  .course-select p { margin-bottom: 12px; }
  .course-btn { padding: 10px 14px; margin-bottom: 8px; }
  .q-text { font-size: 0.95rem; margin-bottom: 12px; }
  .option-btn { padding: 8px 12px; margin-bottom: 6px; }
  .sg-modal, .fs-modal { max-height: 92vh; padding: 14px; margin: 4px auto; }
}
