/* Midterm Quiz Study System — shell + component styles (UI polish). */

:root {
  /* Neutrals */
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #f1f3f6;
  --text: #1c2230;
  --muted: #6b7280;
  --border: #e3e6eb;
  --border-strong: #c9cfd8;

  /* Blue accent */
  --accent: #2563eb;
  --accent-strong: #1d4ed8;
  --accent-soft: #eef4ff;
  --accent-border: #c7d8fb;

  /* Semantic */
  --success: #16a34a;
  --success-strong: #15803d;
  --success-soft: #ecfdf5;
  --success-border: #bbe6cd;
  --danger: #dc2626;
  --danger-strong: #b91c1c;
  --danger-soft: #fef2f2;
  --danger-border: #f3c4c4;

  /* Spacing scale */
  --sp-1: 4px;
  --sp-2: 8px;
  --sp-3: 12px;
  --sp-4: 16px;
  --sp-5: 20px;
  --sp-6: 24px;
  --sp-8: 32px;

  /* Radius scale */
  --radius-sm: 6px;
  --radius-md: 10px;
  --radius-lg: 14px;
  --radius-full: 999px;

  /* Shadows */
  --shadow-sm: 0 1px 2px rgba(16, 24, 40, 0.06);
  --shadow-md: 0 4px 14px rgba(16, 24, 40, 0.08);
  --shadow-lg: 0 12px 32px rgba(16, 24, 40, 0.12);

  /* Font sizes */
  --fs-xs: 0.75rem;
  --fs-sm: 0.85rem;
  --fs-md: 0.95rem;
  --fs-lg: 1.125rem;
  --fs-xl: 1.5rem;

  /* Focus */
  --focus-ring: 0 0 0 3px rgba(37, 99, 235, 0.35);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
}

body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.55;
  font-size: 16px;
  -webkit-font-smoothing: antialiased;
}

/* ---- Layout ---- */

.app-header {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.app-header-inner {
  max-width: 800px;
  margin: 0 auto;
  padding: 12px 16px;
  display: flex;
  align-items: center;
  gap: 8px;
}

.app-brand {
  font-size: 1.05rem;
  font-weight: 700;
  color: var(--accent);
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  gap: 8px;
}

.app-brand:hover { color: var(--accent-strong); }

.app-main {
  max-width: 800px;
  margin: 0 auto;
  padding: 24px 16px 72px;
  overflow-x: hidden;
}

/* ---- Typography ---- */

.screen-title {
  margin: 0 0 4px;
  font-size: var(--fs-xl);
  letter-spacing: -0.01em;
}

.screen-hint {
  margin: 0 0 24px;
  color: var(--muted);
}

.back-link {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  margin-bottom: 20px;
  color: var(--muted);
  font-weight: 600;
  font-size: var(--fs-sm);
  text-decoration: none;
  transition: color 0.12s ease;
}

.back-link:hover { color: var(--accent); }

/* ---- Components ---- */

.card, .panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-sm);
}

.icon {
  display: inline-block;
  flex-shrink: 0;
  vertical-align: middle;
}

.badge, .pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 2px 10px;
  border-radius: var(--radius-full);
  font-size: var(--fs-sm);
  font-weight: 600;
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--border);
  white-space: nowrap;
}

.badge-muted { color: var(--muted); background: var(--surface); }

/* ---- Buttons ---- */

.btn,
.btn-primary,
.btn-secondary,
.btn-danger,
.sa-btn,
.btn-mark {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  font: inherit;
  font-weight: 600;
  border-radius: var(--radius-md);
  cursor: pointer;
  padding: 9px 16px;
  border: 1px solid transparent;
  transition: background-color 0.12s ease, border-color 0.12s ease,
    color 0.12s ease, box-shadow 0.12s ease;
}

.btn:focus-visible,
.btn-primary:focus-visible,
.btn-secondary:focus-visible,
.btn-danger:focus-visible,
.sa-btn:focus-visible,
.btn-mark:focus-visible,
.btn-icon:focus-visible {
  outline: none;
  box-shadow: var(--focus-ring);
}

.btn:disabled,
.btn-primary:disabled,
.btn-secondary:disabled,
.btn-danger:disabled,
.sa-btn:disabled,
.btn-mark:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.btn-primary {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
}
.btn-primary:hover:not(:disabled) {
  background: var(--accent-strong);
  border-color: var(--accent-strong);
}

.btn-secondary {
  background: var(--surface);
  color: var(--accent);
  border-color: var(--border-strong);
}
.btn-secondary:hover:not(:disabled) {
  background: var(--accent-soft);
  border-color: var(--accent);
}

.btn-danger {
  background: var(--danger);
  color: #fff;
  border-color: var(--danger);
}
.btn-danger:hover:not(:disabled) {
  background: var(--danger-strong);
  border-color: var(--danger-strong);
}

.sa-btn {
  background: var(--surface);
  color: var(--accent);
  border-color: var(--border-strong);
}
.sa-btn:hover:not(:disabled) {
  background: var(--accent-soft);
  border-color: var(--accent);
}

.btn-mark {
  background: var(--surface);
  color: var(--muted);
  border-color: var(--border-strong);
}
.btn-mark:hover { background: var(--surface-2); color: var(--accent); border-color: var(--accent); }
.btn-mark.is-marked {
  background: var(--accent-soft);
  border-color: var(--accent);
  color: var(--accent-strong);
}

.btn-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  padding: 0;
  border-radius: var(--radius-md);
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border-strong);
  cursor: pointer;
  transition: background-color 0.12s ease, border-color 0.12s ease, color 0.12s ease;
}
.btn-icon:hover:not(:disabled) {
  background: var(--surface-2);
  border-color: var(--accent);
  color: var(--accent);
}

/* ---- Dashboard tiles ---- */

.tile-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  gap: 12px;
}

.tile {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 64px;
  padding: 12px 16px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  color: var(--text);
  text-align: center;
  font-weight: 600;
  font-size: var(--fs-md);
  text-decoration: none;
  box-shadow: var(--shadow-sm);
  transition: border-color 0.12s ease, box-shadow 0.12s ease, transform 0.12s ease;
}

.tile:hover {
  border-color: var(--accent);
  box-shadow: var(--shadow-md);
  transform: translateY(-1px);
}

.tile-title { display: block; }

/* ---- Quiz screen ---- */

.question-card {
  max-width: 760px;
  margin: 0 auto;
  padding: 24px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-md);
}

.q-header {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
  margin: 12px 0 16px;
}

.q-prompt {
  margin: 0 0 20px;
  font-size: var(--fs-lg);
  font-weight: 600;
  line-height: 1.5;
}

.q-form {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.choices {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.choice {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 14px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  cursor: pointer;
  transition: border-color 0.12s ease, background-color 0.12s ease, box-shadow 0.12s ease;
}

.choice:hover {
  border-color: var(--border-strong);
  background: var(--surface-2);
}

.choice:has(input:checked) {
  border-color: var(--accent);
  background: var(--accent-soft);
}

.choice:focus-within {
  outline: none;
  box-shadow: var(--focus-ring);
  border-color: var(--accent);
}

.choice input[type="radio"] {
  margin: 0;
  accent-color: var(--accent);
  flex-shrink: 0;
}

.choice-text { line-height: 1.45; flex: 1; }

/* Option letter badge (A/B/C/D) — matches the letters used by Read Aloud. */
.choice-letter {
  flex: 0 0 auto;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 1.65em;
  height: 1.65em;
  border: 1px solid var(--border);
  border-radius: var(--radius-full);
  background: var(--surface-2);
  color: var(--muted);
  font-size: 0.82em;
  font-weight: 600;
  line-height: 1;
}
.choice:has(input:checked) .choice-letter {
  border-color: var(--accent);
  background: var(--accent);
  color: #fff;
}
.choice-static .choice-letter { background: var(--surface); }
.choice.is-correct .choice-letter {
  border-color: var(--success);
  background: var(--success);
  color: #fff;
}
.choice.is-wrong .choice-letter {
  border-color: var(--danger);
  background: var(--danger);
  color: #fff;
}

/* Static recap of choices shown in feedback (correct / selected-wrong). */
.choice-static { cursor: default; }
.choice-static:hover { border-color: var(--border); background: var(--surface); }
.choice.is-correct {
  border-color: var(--success-border);
  background: var(--success-soft);
  color: var(--success-strong);
}
.choice.is-wrong {
  border-color: var(--danger-border);
  background: var(--danger-soft);
  color: var(--danger-strong);
}
.choice.is-correct .choice-text,
.choice.is-wrong .choice-text { font-weight: 600; }
.choice > .icon { margin-left: auto; }

.numeric-input input[type="number"],
.short-input textarea {
  width: 100%;
  max-width: 340px;
  padding: 10px 12px;
  font: inherit;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-md);
  background: var(--surface);
  transition: border-color 0.12s ease, box-shadow 0.12s ease;
}

.numeric-input input[type="number"]:focus-visible,
.short-input textarea:focus-visible {
  outline: none;
  border-color: var(--accent);
  box-shadow: var(--focus-ring);
}

.short-input textarea {
  max-width: 100%;
  resize: vertical;
  min-height: 120px;
}

.q-error {
  margin: 0;
  color: var(--danger-strong);
  font-size: var(--fs-sm);
  font-weight: 600;
}

/* ---- Feedback ---- */

.feedback-banner {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 14px;
  border-radius: var(--radius-md);
  border: 1px solid;
  font-weight: 700;
  margin-bottom: 16px;
}
.feedback-banner .icon { flex-shrink: 0; }
.feedback-banner.correct {
  background: var(--success-soft);
  border-color: var(--success-border);
  color: var(--success-strong);
}
.feedback-banner.incorrect {
  background: var(--danger-soft);
  border-color: var(--danger-border);
  color: var(--danger-strong);
}

.feedback-line { margin: 8px 0; }

.feedback-explanation {
  margin: 14px 0 8px;
  padding: 12px 14px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
}

.why-not {
  margin: 14px 0 8px;
  padding: 12px 14px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
}

.why-not-list {
  margin: 8px 0 0;
  padding-left: 20px;
}

/* Letter of the option each "why not" reason refers to. */
.why-not-letter {
  display: inline-block;
  min-width: 1.6em;
  margin-right: 2px;
  font-weight: 700;
  color: var(--accent-strong);
}

.explain-more {
  margin: 12px 0;
  padding: 12px 14px;
  background: var(--accent-soft);
  border: 1px solid var(--accent-border);
  border-radius: var(--radius-md);
}

/* ---- Short-answer feedback ---- */

.sa-block {
  margin: 0 0 16px;
  padding: 12px 14px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
}

.sa-block > strong {
  display: block;
  margin-bottom: 6px;
}

.sa-block ul { margin: 6px 0 0; padding-left: 20px; }
.sa-block p { margin: 4px 0; }

.sa-your-answer {
  white-space: pre-wrap;
  font-style: italic;
  color: var(--muted);
}

.self-assessment {
  margin-top: 6px;
  padding: 14px;
  background: var(--accent-soft);
  border: 1px solid var(--accent-border);
  border-radius: var(--radius-md);
}

.sa-prompt { margin: 0 0 10px; font-weight: 700; }

.sa-buttons {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.sa-recorded {
  margin: 14px 0 8px;
  font-weight: 600;
  color: var(--accent-strong);
}

/* ---- Summary ---- */

.summary-title { margin: 0 0 4px; }

.summary-mode { margin: 0 0 16px; color: var(--accent-strong); font-weight: 700; }

.summary-line { margin: 0 0 16px; color: var(--muted); }

.summary-section {
  margin: 0 0 16px;
  padding: 14px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
}

.summary-section h3 { margin: 0 0 8px; }
.summary-section p { margin: 4px 0; }

/* ---- Learn / Practice setup ---- */

.learn-setup {
  display: flex;
  flex-direction: column;
  gap: 16px;
  max-width: 520px;
  margin: 0 auto;
  padding: 20px;
}

.filter-control {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.filter-label {
  font-weight: 600;
  font-size: var(--fs-md);
  color: var(--text);
}

.filter-select {
  width: 100%;
  padding: 10px 12px;
  font: inherit;
  color: var(--text);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-md);
  background: var(--surface);
  transition: border-color 0.12s ease, box-shadow 0.12s ease;
}

.filter-select:focus-visible {
  outline: none;
  border-color: var(--accent);
  box-shadow: var(--focus-ring);
}

.filter-count { margin: 0; color: var(--muted); font-weight: 600; }
.filter-empty { margin: 0; color: var(--danger-strong); font-weight: 600; }

/* ---- Weak Topics ---- */

.weak-topic-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
  max-width: 640px;
  margin-bottom: 20px;
}

.weak-topic-row {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  padding: 12px 16px;
  font: inherit;
  text-align: left;
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  cursor: pointer;
  box-shadow: var(--shadow-sm);
  transition: border-color 0.12s ease, box-shadow 0.12s ease;
}

.weak-topic-row:hover {
  border-color: var(--accent);
  box-shadow: var(--shadow-md);
}

.weak-topic-row:focus-visible { outline: none; box-shadow: var(--focus-ring); }

.weak-topic-name { flex: 1; font-weight: 600; }
.weak-topic-accuracy { color: var(--danger-strong); font-weight: 700; white-space: nowrap; }
.weak-topic-attempts { color: var(--muted); font-size: var(--fs-sm); white-space: nowrap; }

/* ---- Review Mistakes ---- */

.review-section { margin: 0 0 24px; max-width: 640px; }

.review-section-title { margin: 0 0 10px; font-size: var(--fs-lg); }

.review-empty { margin: 0; color: var(--muted); }

.review-list { display: flex; flex-direction: column; gap: 10px; }

.review-row {
  display: flex;
  flex-direction: column;
  gap: 6px;
  width: 100%;
  padding: 12px 16px;
  font: inherit;
  text-align: left;
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  cursor: pointer;
  box-shadow: var(--shadow-sm);
  transition: border-color 0.12s ease, box-shadow 0.12s ease;
}

.review-row:hover { border-color: var(--accent); box-shadow: var(--shadow-md); }
.review-row:focus-visible { outline: none; box-shadow: var(--focus-ring); }

.review-prompt { line-height: 1.4; }

.review-meta { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; }
.review-topic { color: var(--muted); font-size: var(--fs-sm); font-weight: 600; }
.review-count { color: var(--accent-strong); font-size: var(--fs-sm); font-weight: 700; }

/* ---- Read Aloud controls ---- */

.speech-controls {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  margin: 12px 0;
}

.speech-status {
  flex-basis: 100%;
  margin: 4px 0 0;
  color: var(--danger-strong);
  font-size: var(--fs-sm);
  font-weight: 600;
}

/* The status line is now reserved for errors, so an empty one must not leave a
   gap under the controls. */
.speech-status:empty {
  display: none;
}

/* The Read Aloud button shows its own spinner and "Loading…" label while the
   clip is fetched, so keep it at full opacity instead of the faded :disabled look. */
.btn[aria-busy="true"],
.btn-secondary[aria-busy="true"]:disabled,
.ai-tts-read[aria-busy="true"] {
  opacity: 1;
  cursor: progress;
}

/* ---- Progress dashboard ---- */

.progress-section { margin: 0 0 24px; max-width: 640px; }
.progress-section-title { margin: 0 0 10px; font-size: var(--fs-lg); }
.progress-empty { margin: 0; color: var(--muted); }

.stat-row {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 10px;
  padding: 8px 0;
  border-bottom: 1px solid var(--border);
}

.stat-label { color: var(--text); font-weight: 600; }
.stat-value { color: var(--muted); }

.stat-link {
  color: var(--accent);
  font-weight: 700;
  text-decoration: none;
}
.stat-link:hover { color: var(--accent-strong); text-decoration: underline; }

.ptable {
  display: flex;
  flex-direction: column;
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  overflow: hidden;
}

.ptable-row {
  display: grid;
  grid-template-columns: 2fr 1fr 1fr 1fr 1fr;
}

.ptable-row + .ptable-row { border-top: 1px solid var(--border); }
.ptable-header { background: var(--surface-2); font-weight: 700; }

.ptable-cell {
  padding: 8px 12px;
  font-size: var(--fs-sm);
  overflow-wrap: anywhere;
}

/* ---- Session persistence & history (Task 19) ---- */

.resume-banner {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px;
  max-width: 640px;
  margin: 0 0 20px;
  padding: 14px 16px;
  background: var(--accent-soft);
  border: 1px solid var(--accent-border);
  border-radius: var(--radius-md);
}

.resume-banner-title { color: var(--accent-strong); font-size: var(--fs-md); }

.resume-banner-text { flex: 1 1 100%; margin: 0; color: var(--text); }

.history-delta {
  margin: 0 0 10px;
  color: var(--text);
  font-weight: 600;
}

.history-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.history-row {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  padding: 10px 14px;
  font: inherit;
  text-align: left;
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  cursor: pointer;
  box-shadow: var(--shadow-sm);
  transition: border-color 0.12s ease, box-shadow 0.12s ease;
}

.history-row:hover { border-color: var(--accent); box-shadow: var(--shadow-md); }
.history-row:focus-visible { outline: none; box-shadow: var(--focus-ring); }

.history-mode { font-weight: 600; }
.history-meta { color: var(--muted); font-size: var(--fs-sm); margin-left: auto; }

.history-detail {
  padding: 12px 14px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
}

.history-detail-title { margin: 0 0 6px; font-size: var(--fs-md); }

.history-detail p { margin: 4px 0; }

.history-questions {
  margin: 8px 0 0;
  padding-left: 18px;
}

.history-question {
  display: flex;
  gap: 10px;
  align-items: baseline;
  padding: 3px 0;
}

.history-q-prompt { flex: 1; color: var(--text); }
.history-q-status { color: var(--muted); font-size: var(--fs-sm); white-space: nowrap; }

/* ---- Responsive ---- */

@media (max-width: 520px) {
  .app-main { padding: 16px 12px 64px; }
  .question-card { padding: 16px; }
  .tile-grid { grid-template-columns: repeat(auto-fill, minmax(130px, 1fr)); }
  .ptable-cell { padding: 6px 8px; font-size: var(--fs-xs); }
  .stat-row { flex-direction: column; gap: 2px; }
}

@media (max-width: 380px) {
  .question-card { padding: 14px 12px; }
  .choice { padding: 10px 12px; }
  .ptable-cell { padding: 5px 6px; }
  .btn, .btn-primary, .btn-secondary, .btn-danger, .sa-btn, .btn-mark { padding: 8px 12px; }
}

/* ---- Midterm Simulation (Task 20) ---- */

.sim-structure { margin: 8px 0 16px; padding-left: 20px; }

.sim-topbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  margin-bottom: 12px;
}

.sim-timer { font-variant-numeric: tabular-nums; background: var(--surface); color: var(--accent-strong); }

.sim-nav {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 14px;
}

.sim-nav-btn {
  font: inherit;
  font-weight: 600;
  font-size: var(--fs-sm);
  padding: 6px 12px;
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border-strong);
  cursor: pointer;
}

.sim-nav-btn.is-active { background: var(--accent); color: #fff; border-color: var(--accent); }
.sim-nav-btn:focus-visible { outline: none; box-shadow: var(--focus-ring); }

.sim-controls {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-top: 18px;
}

.sim-summary {
  padding: 12px 14px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  margin-bottom: 16px;
}

.sim-summary p { margin: 4px 0; }

.sim-section-heading {
  margin: 18px 0 8px;
  font-size: var(--fs-lg);
  border-bottom: 1px solid var(--border);
  padding-bottom: 6px;
}

.sim-review-item {
  padding: 12px 0;
  border-bottom: 1px solid var(--border);
}

/* ---- AI Tutor (inline chat) ---- */

.ai-tutor { margin: 12px 0 4px; }

.ai-tutor-host { margin-top: 10px; }

.ai-tutor-panel {
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  background: var(--surface);
  overflow: hidden;
}

.ai-tutor-header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 14px;
  background: var(--surface-2);
  border-bottom: 1px solid var(--border);
}

.ai-tutor-header strong { flex: 1; }

.ai-tutor-messages {
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 12px 14px;
  max-height: 380px;
  overflow-y: auto;
}

.ai-msg { display: flex; flex-direction: column; gap: 6px; }
.ai-msg-user { align-items: flex-end; }
.ai-msg-assistant { align-items: flex-start; }

.ai-msg-bubble {
  max-width: 88%;
  padding: 9px 12px;
  border-radius: var(--radius-md);
  font-size: var(--fs-md);
  line-height: 1.5;
  overflow-wrap: anywhere;
}
.ai-msg-user .ai-msg-bubble { background: var(--accent); color: #fff; }
.ai-msg-assistant .ai-msg-bubble { background: var(--surface-2); color: var(--text); border: 1px solid var(--border); }

.ai-msg-line { min-height: 0; }
.ai-msg-break { height: 6px; }

.ai-loading { color: var(--muted); font-style: italic; padding: 4px 0; }

.ai-tutor-starters {
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.ai-starter { text-align: left; }

.ai-tutor-footer {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: flex-start;
  padding: 10px 14px;
  border-top: 1px solid var(--border);
}

.ai-tutor-input {
  flex: 1 1 100%;
  padding: 9px 12px;
  font: inherit;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-md);
  background: var(--surface);
  resize: vertical;
  min-height: 44px;
}

.ai-tutor-input:focus-visible { outline: none; border-color: var(--accent); box-shadow: var(--focus-ring); }

.ai-tutor-error {
  flex-basis: 100%;
  margin: 0;
  color: var(--danger-strong);
  font-size: var(--fs-sm);
  font-weight: 600;
}

.ai-tutor-link {
  color: var(--accent);
  font-weight: 700;
  text-decoration: underline;
  white-space: nowrap;
}
.ai-tutor-link:hover { color: var(--accent-strong); }

.ai-tts-bar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px;
}

.ai-tts-read { padding: 5px 10px; font-size: var(--fs-sm); }

.ai-tts-status { display: inline-flex; align-items: center; }

.sa-btn.is-active {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
}

/* ---- Markdown rendering + TTS spinner ---- */

.spinner {
  display: inline-block;
  width: 14px;
  height: 14px;
  border: 2px solid var(--border-strong);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
  vertical-align: -2px;
  margin-right: 6px;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

.tutor-md p { margin: 0 0 6px; }
.tutor-md p:last-child { margin-bottom: 0; }
.tutor-md ul, .tutor-md ol { margin: 4px 0 8px; padding-left: 20px; }
.tutor-md li { margin: 2px 0; }
.tutor-md h4, .tutor-md h5, .tutor-md h6 { margin: 10px 0 4px; font-weight: 700; line-height: 1.3; }
.tutor-md h4 { font-size: 1.18em; }
.tutor-md h5 { font-size: 1.09em; }
.tutor-md h6 { font-size: 1em; }
.tutor-md code {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 1px 4px;
  font-size: 0.9em;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
}
.tutor-md pre {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 8px 10px;
  overflow-x: auto;
  margin: 6px 0;
}
.tutor-md pre code { background: none; border: none; padding: 0; }
.tutor-md strong { font-weight: 700; }
.tutor-md em { font-style: italic; }
.tutor-md .math {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  background: var(--surface-2);
  border-radius: 4px;
  padding: 0 3px;
  white-space: nowrap;
}

/* Markdown tables from the tutor. The wrapper scrolls horizontally so a wide
   table never breaks the layout on ~390px / ~320px screens. */
.tutor-md .table-wrap {
  margin: 8px 0;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}
.tutor-md table.tutor-table {
  border-collapse: collapse;
  width: 100%;
  font-size: 0.95em;
}
.tutor-md table.tutor-table th,
.tutor-md table.tutor-table td {
  border: 1px solid var(--border);
  padding: 6px 10px;
  vertical-align: top;
  line-height: 1.4;
}
.tutor-md table.tutor-table thead th {
  background: var(--surface-2);
  font-weight: 600;
  white-space: nowrap;
}

/* ---- Session Review (shared component) ---- */

.session-review { margin: 14px 0; }

.session-review-header {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 10px;
}

.session-review-tabs {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.review-tab {
  padding: 6px 12px;
  font: inherit;
  font-size: var(--fs-sm);
  font-weight: 600;
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 999px;
  cursor: pointer;
}

.review-tab:hover { border-color: var(--accent); }
.review-tab.is-active { background: var(--accent); color: #fff; border-color: var(--accent); }

.session-review-retry { padding: 7px 14px; font-size: var(--fs-sm); }

.session-review-rows { display: flex; flex-direction: column; gap: 8px; }

.session-review-row {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  padding: 12px 14px;
  font: inherit;
  text-align: left;
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  cursor: pointer;
  box-shadow: var(--shadow-sm);
  transition: border-color 0.12s ease, box-shadow 0.12s ease;
}

.session-review-row:hover { border-color: var(--accent); box-shadow: var(--shadow-md); }
.session-review-row:focus-visible { outline: none; box-shadow: var(--focus-ring); }

.session-review-row .icon { flex-shrink: 0; color: var(--muted); margin-left: auto; }

.review-status {
  flex-shrink: 0;
  font-size: var(--fs-sm);
  font-weight: 700;
  white-space: nowrap;
}
.review-status.is-incorrect { color: var(--danger-strong); }
.review-status.is-needs-review { color: var(--warning-strong, #b45309); }
.review-status.is-correct { color: var(--success-strong, #15803d); }
.review-status.is-unanswered { color: var(--muted); }

.session-review-prompt {
  flex: 1;
  line-height: 1.4;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.session-review-meta {
  flex-shrink: 0;
  color: var(--muted);
  font-size: var(--fs-sm);
}

.study-card { margin-top: 12px; }

/* ---- Midterm Simulation AI check ---- */

.sim-checking {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 14px;
  padding: 36px 20px;
  text-align: center;
}

.spinner-lg { width: 28px; height: 28px; border-width: 3px; }

.sim-checking-status { color: var(--muted); font-size: var(--fs-md); margin: 0; }

.ai-check-block {
  margin: 10px 0;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  background: var(--surface-2);
}

.ai-check-block p { margin: 4px 0; }

.ai-check-loading { display: flex; align-items: center; gap: 8px; }

.ai-check-badge {
  display: inline-block;
  font-size: var(--fs-sm);
  font-weight: 700;
  padding: 2px 8px;
  border-radius: 999px;
  margin-bottom: 4px;
}

.ai-check-got_it { background: var(--success-soft); color: var(--success-strong); }
.ai-check-partial { background: #fef3c7; color: #b45309; }
.ai-check-didnt_know { background: var(--danger-soft); color: var(--danger-strong); }

.ai-check-feedback { font-style: italic; }
.ai-check-fallback { color: var(--muted); font-size: var(--fs-sm); }
