/* ==========================================================================
   Xác suất Thống kê — Đại học Cần Thơ
   Dark Mode OLED Default + High-Contrast Light Mode Toggle
   ========================================================================== */

:root,
[data-theme="dark"] {
  color-scheme: dark;
  --bg: #020617;
  --surface: #0E1223;
  --surface-elevated: #1A1E2F;
  --surface-inset: #080C18;
  --header-bg: rgba(2, 6, 23, 0.94);
  --border: #334155;
  --border-subtle: #1E293B;
  --border-strong: #475569;
  --text: #F8FAFC;
  --text-muted: #94A3B8;
  --accent: #22C55E;
  --accent-hover: #16A34A;
  --on-accent: #020617;
  --accent-soft: rgba(34, 197, 94, 0.14);
  --accent-border: rgba(34, 197, 94, 0.42);
  --accent-text: #4ADE80;
  --math: #38BDF8;
  --math-soft: rgba(56, 189, 248, 0.12);
  --math-border: rgba(56, 189, 248, 0.36);
  --amber: #F59E0B;
  --amber-soft: rgba(245, 158, 11, 0.14);
  --amber-border: rgba(245, 158, 11, 0.42);
  --amber-text: #FBBF24;
  --danger: #DC2626;
  --danger-hover: #B91C1C;
  --on-danger: #FFFFFF;
  --danger-soft: rgba(239, 68, 68, 0.14);
  --danger-border: rgba(239, 68, 68, 0.46);
  --danger-text: #FCA5A5;
  --ring: #38BDF8;
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.35);
  --shadow-md: 0 6px 18px rgba(0, 0, 0, 0.42);
  --shadow-lg: 0 20px 50px rgba(0, 0, 0, 0.65);

  /* Statistical SVG Diagram Tokens */
  --diag-bg: #080C18;
  --diag-grid: rgba(148, 163, 184, 0.14);
  --diag-axis: #94A3B8;
  --diag-curve: #22C55E;
  --diag-curve-sec: #38BDF8;
  --diag-fill-accept: rgba(34, 197, 94, 0.18);
  --diag-fill-reject: rgba(239, 68, 68, 0.30);
  --diag-fill-band2: rgba(56, 189, 248, 0.16);
  --diag-reject-stroke: #F87171;
  --diag-text: #F8FAFC;
  --diag-muted: #94A3B8;

  /* Spacing & Typography */
  --space-xs: 4px;
  --space-sm: 8px;
  --space-md: 16px;
  --space-lg: 24px;
  --radius-sm: 8px;
  --radius-md: 12px;
  --radius-lg: 16px;
  --header-h: 64px;
  --bnav-h: 66px;
  --font-sans: 'Be Vietnam Pro', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  --font-mono: 'JetBrains Mono', ui-monospace, SFMono-Regular, Consolas, monospace;
}

[data-theme="light"] {
  color-scheme: light;
  --bg: #F8FAFC;
  --surface: #FFFFFF;
  --surface-elevated: #F1F5F9;
  --surface-inset: #F8FAFC;
  --header-bg: rgba(255, 255, 255, 0.94);
  --border: #CBD5E1;
  --border-subtle: #E2E8F0;
  --border-strong: #94A3B8;
  --text: #0F172A;
  --text-muted: #475569;
  --accent: #0F766E;
  --accent-hover: #115E59;
  --on-accent: #FFFFFF;
  --accent-soft: #ECFDF5;
  --accent-border: #6EE7B7;
  --accent-text: #0F766E;
  --math: #1D4ED8;
  --math-soft: #EFF6FF;
  --math-border: #93C5FD;
  --amber: #B45309;
  --amber-soft: #FFFBEB;
  --amber-border: #FCD34D;
  --amber-text: #92400E;
  --danger: #DC2626;
  --danger-hover: #B91C1C;
  --on-danger: #FFFFFF;
  --danger-soft: #FEF2F2;
  --danger-border: #FCA5A5;
  --danger-text: #991B1B;
  --ring: #1D4ED8;
  --shadow-sm: 0 1px 2px rgba(15, 23, 42, 0.06);
  --shadow-md: 0 4px 14px rgba(15, 23, 42, 0.08);
  --shadow-lg: 0 20px 50px rgba(15, 23, 42, 0.22);

  --diag-bg: #F8FAFC;
  --diag-grid: rgba(15, 23, 42, 0.08);
  --diag-axis: #475569;
  --diag-curve: #0F766E;
  --diag-curve-sec: #1D4ED8;
  --diag-fill-accept: rgba(15, 118, 110, 0.14);
  --diag-fill-reject: rgba(220, 38, 38, 0.22);
  --diag-fill-band2: rgba(29, 78, 216, 0.12);
  --diag-reject-stroke: #DC2626;
  --diag-text: #0F172A;
  --diag-muted: #475569;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html {
  font-size: 16px;
  -webkit-text-size-adjust: 100%;
  scroll-behavior: smooth;
  max-width: 100%;
  overflow-x: clip;
}

body {
  margin: 0;
  padding: 0;
  font-family: var(--font-sans);
  background-color: var(--bg);
  color: var(--text);
  line-height: 1.65;
  padding-top: var(--header-h);
  padding-bottom: calc(var(--bnav-h) + 20px);
  max-width: 100%;
  overflow-x: clip;
  transition: background-color 180ms ease, color 180ms ease;
}

body.modal-open {
  overflow: hidden;
}

@media (min-width: 860px) {
  body {
    padding-bottom: 40px;
  }
}

h1, h2, h3, h4, p, ul, ol {
  margin-top: 0;
}

button, input, select {
  font-family: inherit;
}

button, a, [role="button"], select, input[type="checkbox"] {
  cursor: pointer;
}

:focus-visible {
  outline: 2px solid var(--ring);
  outline-offset: 2px;
}

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

.ico-slot {
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.mono-num {
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
}

/* ==========================================================================
   HEADER & NAVIGATION
   ========================================================================== */

.app-header {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  width: 100%;
  max-width: 100%;
  height: var(--header-h);
  background: var(--header-bg);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
  color: var(--text);
  z-index: 900;
  contain: layout;
}

.header-inner {
  max-width: 1200px;
  width: 100%;
  min-width: 0;
  height: 100%;
  margin: 0 auto;
  padding: 0 12px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  flex: 1 1 0%;
  min-width: 0;
}

.brand-logo {
  width: 38px;
  height: 38px;
  border-radius: 10px;
  background: var(--accent-soft);
  border: 1px solid var(--accent-border);
  color: var(--accent-text);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.brand-text {
  min-width: 0;
}

.brand-title {
  font-size: 0.96rem;
  font-weight: 700;
  margin: 0;
  line-height: 1.2;
  letter-spacing: -0.01em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.brand-sub {
  font-size: 0.72rem;
  font-family: var(--font-mono);
  color: var(--text-muted);
  margin: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.desktop-nav {
  display: none;
  align-items: center;
  gap: 4px;
  background: var(--surface-elevated);
  border: 1px solid var(--border);
  padding: 4px;
  border-radius: 999px;
}

@media (min-width: 860px) {
  .desktop-nav {
    display: flex;
  }
  .brand {
    flex: 0 1 auto;
  }
  .brand-title {
    font-size: 1.05rem;
  }
}

.nav-pill {
  appearance: none;
  border: 1px solid transparent;
  background: transparent;
  color: var(--text-muted);
  padding: 8px 15px;
  border-radius: 999px;
  font-size: 0.88rem;
  font-weight: 600;
  display: inline-flex;
  align-items: center;
  gap: 7px;
  min-height: 44px;
  transition: background-color 160ms ease, color 160ms ease, border-color 160ms ease;
}

.nav-pill:hover {
  color: var(--text);
  background: var(--surface);
}

.nav-pill.active {
  background: var(--accent);
  color: var(--on-accent);
  box-shadow: var(--shadow-sm);
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

.util-btn {
  appearance: none;
  border: 1px solid var(--border);
  background: var(--surface-elevated);
  color: var(--text);
  border-radius: 10px;
  padding: 8px 12px;
  font-size: 0.82rem;
  font-weight: 600;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 44px;
  min-width: 44px;
  transition: background-color 160ms ease, border-color 160ms ease, color 160ms ease;
}

.util-btn:hover {
  border-color: var(--math);
  background: var(--surface);
}

@media (max-width: 520px) {
  .theme-label {
    display: none;
  }
}

@media (max-width: 360px) {
  .util-overview .util-label {
    display: none;
  }
}

/* Mobile Bottom Nav */
.bottom-nav {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: var(--bnav-h);
  background: var(--surface);
  border-top: 1px solid var(--border);
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  z-index: 900;
  box-shadow: 0 -2px 12px rgba(0, 0, 0, 0.16);
  padding-bottom: env(safe-area-inset-bottom, 0px);
}

@media (min-width: 860px) {
  .bottom-nav {
    display: none;
  }
}

.bnav-btn {
  appearance: none;
  border: none;
  background: transparent;
  color: var(--text-muted);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  font-size: 0.74rem;
  font-weight: 600;
  padding: 6px 4px;
  min-height: 48px;
  min-width: 0;
  position: relative;
  transition: color 160ms ease;
}

.bnav-btn.active {
  color: var(--accent-text);
}

.bnav-btn.active::before {
  content: '';
  position: absolute;
  top: 0;
  left: 20%;
  right: 20%;
  height: 3px;
  background: var(--accent);
  border-radius: 0 0 3px 3px;
}

/* ==========================================================================
   MAIN LAYOUT & PANELS
   ========================================================================== */

.app-main {
  max-width: 1200px;
  width: 100%;
  min-width: 0;
  margin: 0 auto;
  padding: 16px 12px 24px;
  contain: layout;
}

@media (min-width: 860px) {
  .app-main {
    padding: 20px 20px 40px;
  }
}

.tab-panel {
  display: none;
  min-width: 0;
  width: 100%;
}

.tab-panel.active {
  display: block;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-sm);
  padding: 16px;
  margin-bottom: 16px;
  min-width: 0;
  max-width: 100%;
  overflow-wrap: break-word;
  contain: layout;
  overflow-x: auto;
}

@media (min-width: 720px) {
  .card {
    padding: 20px 22px;
  }
}

.section-heading {
  font-size: 1.12rem;
  font-weight: 700;
  color: var(--text);
  margin-bottom: 6px;
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
  letter-spacing: -0.01em;
}

.muted {
  color: var(--text-muted);
  font-size: 0.92rem;
}

.measure-prose {
  max-width: 74ch;
}

/* ==========================================================================
   BADGES & PILLS (ONLY EXAM TAGS + STATUS)
   ========================================================================== */

.badge {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 4px 10px;
  border-radius: 999px;
  font-size: 0.75rem;
  font-weight: 700;
  line-height: 1.3;
  letter-spacing: 0.01em;
  white-space: nowrap;
}

.badge-neutral {
  background: var(--surface-elevated);
  color: var(--text);
  border: 1px solid var(--border);
}

.badge-math {
  background: var(--math-soft);
  color: var(--math);
  border: 1px solid var(--math-border);
}

.badge-accent {
  background: var(--accent-soft);
  color: var(--accent-text);
  border: 1px solid var(--accent-border);
}

.badge-amber {
  background: var(--amber-soft);
  color: var(--amber-text);
  border: 1px solid var(--amber-border);
}

.badge-danger {
  background: var(--danger-soft);
  color: var(--danger-text);
  border: 1px solid var(--danger-border);
}

.scope-pill {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 4px 10px;
  border-radius: 999px;
  font-size: 0.74rem;
  font-weight: 700;
  line-height: 1.25;
  white-space: nowrap;
}

.scope-exam {
  background: var(--amber-soft);
  color: var(--amber-text);
  border: 1px solid var(--amber-border);
}

.scope-embedded {
  background: var(--math-soft);
  color: var(--math);
  border: 1px solid var(--math-border);
}

.self-study-dot {
  display: inline-block;
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: #020617;
  border: 1.5px solid #94A3B8;
  flex-shrink: 0;
  vertical-align: middle;
}

[data-theme="light"] .self-study-dot {
  background: #09090B;
  border: 1px solid #09090B;
}

/* ==========================================================================
   BUTTONS (>=44px Touch Targets)
   ========================================================================== */

.btn {
  appearance: none;
  border: 1px solid transparent;
  border-radius: 10px;
  padding: 10px 16px;
  min-height: 44px;
  font-size: 0.92rem;
  font-weight: 600;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  transition: background-color 160ms ease, border-color 160ms ease, color 160ms ease, box-shadow 160ms ease;
  text-decoration: none;
}

.btn:disabled {
  opacity: 0.48;
  cursor: not-allowed;
}

.btn-primary {
  background: var(--accent);
  color: var(--on-accent);
  border-color: var(--accent);
}

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

.btn-math {
  background: var(--math-soft);
  color: var(--math);
  border-color: var(--math-border);
}

.btn-math:hover:not(:disabled) {
  background: var(--surface-elevated);
  border-color: var(--math);
}

.btn-outline {
  background: var(--surface-elevated);
  color: var(--text);
  border-color: var(--border);
}

.btn-outline:hover:not(:disabled) {
  background: var(--surface);
  border-color: var(--math);
}

.btn-danger {
  background: var(--danger);
  color: var(--on-danger);
  border-color: var(--danger);
}

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

.btn-sm {
  min-height: 44px;
  padding: 8px 14px;
  font-size: 0.84rem;
  border-radius: 9px;
}

/* ==========================================================================
   FORMULA, TABLE, CALLOUT & SVG STATISTICAL DIAGRAMS
   ========================================================================== */

.formula-box {
  background: var(--surface-inset);
  border: 1px solid var(--border);
  border-left: 4px solid var(--math);
  border-radius: 10px;
  padding: 12px 14px;
  margin: 12px 0;
  max-width: 100%;
  min-width: 0;
  overflow-x: auto;
  overflow-y: hidden;
  -webkit-overflow-scrolling: touch;
  contain: layout inline-size;
}

.formula-box.highlight-teal {
  background: var(--accent-soft);
  border-color: var(--accent-border);
  border-left-color: var(--accent);
}

.formula-box.highlight-amber {
  background: var(--amber-soft);
  border-color: var(--amber-border);
  border-left-color: var(--amber);
}

.katex-display {
  margin: 0.5em 0 !important;
  max-width: 100%;
  overflow-x: auto;
  overflow-y: hidden;
  padding: 2px 0 4px;
  contain: layout inline-size;
}

table.data-table .katex-display {
  contain: layout;
}

.table-wrap {
  width: 100%;
  max-width: 100%;
  min-width: 0;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  margin: 12px 0;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface-inset);
  contain: layout inline-size;
}

table.data-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.88rem;
  font-variant-numeric: tabular-nums;
  min-width: 340px;
}

table.data-table th,
table.data-table td {
  padding: 10px 12px;
  border-bottom: 1px solid var(--border-subtle);
  border-right: 1px solid var(--border-subtle);
  text-align: center;
  vertical-align: middle;
}

table.data-table th:last-child,
table.data-table td:last-child {
  border-right: none;
}

table.data-table tr:last-child td {
  border-bottom: none;
}

table.data-table th {
  background: var(--surface-elevated);
  color: var(--text);
  font-weight: 700;
  border-bottom: 1.5px solid var(--border);
  white-space: nowrap;
}

table.data-table td.row-label,
table.data-table th.row-label {
  background: var(--surface-elevated);
  color: var(--math);
  font-weight: 700;
  text-align: left;
  white-space: nowrap;
}

.stat-diagram {
  margin: 14px 0;
  min-width: 0;
  max-width: 100%;
}

.stat-diagram-card {
  background: var(--diag-bg);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px 14px;
  overflow-x: auto;
  overflow-y: hidden;
  contain: layout inline-size;
}

.stat-diagram-svg {
  display: block;
  width: 100%;
  max-width: 540px;
  height: auto;
  margin: 0 auto;
}

.stat-diagram-cap {
  margin-top: 8px;
  padding-top: 8px;
  border-top: 1px dashed var(--border-subtle);
  font-size: 0.82rem;
  font-family: var(--font-mono);
  color: var(--text-muted);
  text-align: center;
}

/* ==========================================================================
   TAB 1: LEARN (HỌC)
   ========================================================================== */

.chapter-strip {
  display: flex;
  gap: 8px;
  width: 100%;
  max-width: 100%;
  overflow-x: auto;
  padding-bottom: 6px;
  margin-bottom: 14px;
  -webkit-overflow-scrolling: touch;
  contain: layout inline-size;
}

.chapter-chip {
  appearance: none;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  border-radius: 12px;
  padding: 10px 14px;
  min-height: 58px;
  flex: 0 0 auto;
  min-width: 175px;
  max-width: 240px;
  text-align: left;
  transition: border-color 160ms ease, background-color 160ms ease;
  box-shadow: var(--shadow-sm);
}

.chapter-chip:hover {
  border-color: var(--math);
}

.chapter-chip.active {
  background: var(--surface-elevated);
  border-color: var(--accent);
  box-shadow: inset 0 0 0 1px var(--accent);
}

.chapter-chip-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  font-size: 0.76rem;
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
  font-weight: 700;
  color: var(--text-muted);
  margin-bottom: 4px;
}

.chapter-chip.active .chapter-chip-top {
  color: var(--accent-text);
}

.chapter-chip-title {
  font-size: 0.88rem;
  font-weight: 700;
  line-height: 1.3;
  color: var(--text);
}

.learn-layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 16px;
}

.learn-layout > *,
.lesson-sidebar,
.lesson-content {
  min-width: 0;
}

@media (min-width: 940px) {
  .learn-layout {
    grid-template-columns: 300px minmax(0, 1fr);
    align-items: start;
  }
  .lesson-sidebar {
    position: sticky;
    top: calc(var(--header-h) + 16px);
  }
}

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

.lesson-item-btn {
  appearance: none;
  border: 1px solid var(--border);
  background: var(--surface-inset);
  border-radius: 10px;
  padding: 10px 12px;
  text-align: left;
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 10px;
  min-height: 48px;
  width: 100%;
  transition: border-color 160ms ease, background-color 160ms ease;
}

.lesson-item-btn:hover {
  border-color: var(--math);
  background: var(--surface-elevated);
}

.lesson-item-btn.active {
  border-color: var(--accent);
  background: var(--accent-soft);
  box-shadow: inset 3px 0 0 var(--accent);
}

.lesson-item-title {
  font-size: 0.88rem;
  font-weight: 600;
  color: var(--text);
  line-height: 1.35;
}

.lesson-item-meta {
  font-size: 0.76rem;
  color: var(--text-muted);
  margin-top: 4px;
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 6px;
}

.done-check {
  flex-shrink: 0;
  width: 24px;
  height: 24px;
  border-radius: 999px;
  background: var(--accent);
  color: var(--on-accent);
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.lesson-index-pill {
  flex-shrink: 0;
  min-width: 24px;
  height: 24px;
  border-radius: 999px;
  background: var(--surface-elevated);
  border: 1px solid var(--border);
  color: var(--text-muted);
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
  font-size: 0.74rem;
  font-weight: 700;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.lesson-header-card {
  border-left: 4px solid var(--accent);
}

.objectives-list {
  margin-bottom: 0;
  padding-left: 20px;
  font-size: 0.93rem;
  max-width: 74ch;
}

.objectives-list li {
  margin-bottom: 5px;
}

.lesson-block {
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 16px;
  margin-bottom: 16px;
  background: var(--surface);
  min-width: 0;
  max-width: 100%;
  overflow-wrap: break-word;
  contain: layout;
  overflow-x: auto;
}

@media (min-width: 720px) {
  .lesson-block {
    padding: 18px 20px;
  }
}

.lesson-block-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 12px;
  padding-bottom: 10px;
  border-bottom: 1px dashed var(--border);
}

.lesson-block-title {
  font-size: 1.02rem;
  font-weight: 700;
  color: var(--math);
  margin: 0;
}

.lesson-block-body {
  min-width: 0;
  max-width: 100%;
}

.lesson-block-body > p,
.lesson-block-body > ul,
.lesson-block-body > ol {
  max-width: 74ch;
}

.lesson-block-body p {
  margin-bottom: 10px;
}

.lesson-block-body ul,
.lesson-block-body ol {
  padding-left: 20px;
  margin-bottom: 10px;
}

.lesson-block-body li {
  margin-bottom: 5px;
}

.example-box {
  background: var(--surface-elevated);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 14px;
  margin-top: 12px;
  min-width: 0;
  max-width: 100%;
  overflow-wrap: break-word;
  contain: layout;
  overflow-x: auto;
}

.example-box > div {
  max-width: 74ch;
}

.example-toggle {
  margin-top: 10px;
}

.example-sol {
  margin-top: 12px;
  padding-top: 12px;
  border-top: 1px solid var(--border);
  max-width: 100%;
  min-width: 0;
  overflow-x: auto;
  overflow-y: hidden;
  -webkit-overflow-scrolling: touch;
  contain: layout inline-size;
}

.callout-warn {
  background: var(--amber-soft);
  border: 1px solid var(--amber-border);
  border-left: 4px solid var(--amber);
  border-radius: 10px;
  padding: 12px 14px;
  margin: 12px 0;
  font-size: 0.92rem;
  min-width: 0;
  max-width: 100%;
  overflow-x: auto;
  overflow-y: hidden;
  overflow-wrap: break-word;
  contain: layout inline-size;
}

.callout-tip {
  background: var(--accent-soft);
  border: 1px solid var(--accent-border);
  border-left: 4px solid var(--accent);
  border-radius: 10px;
  padding: 12px 14px;
  margin: 12px 0;
  font-size: 0.92rem;
  min-width: 0;
  max-width: 100%;
  overflow-x: auto;
  overflow-y: hidden;
  overflow-wrap: break-word;
  contain: layout inline-size;
}

/* Interactive Exercise inside Lesson */
.drill-section {
  background: var(--surface);
  border: 1.5px solid var(--math-border);
  border-radius: 14px;
  padding: 16px;
  margin-top: 18px;
  min-width: 0;
  max-width: 100%;
  contain: layout;
}

.drill-card {
  background: var(--surface-inset);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 14px;
  margin-top: 12px;
  min-width: 0;
  max-width: 100%;
  overflow-wrap: break-word;
  contain: layout;
  overflow-x: auto;
}

.drill-q-text {
  font-weight: 600;
  margin-bottom: 12px;
  color: var(--text);
  max-width: 74ch;
}

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

.opt-btn {
  appearance: none;
  border: 1.5px solid var(--border);
  background: var(--surface);
  color: var(--text);
  border-radius: 10px;
  padding: 11px 13px;
  min-height: 46px;
  width: 100%;
  max-width: 100%;
  text-align: left;
  display: flex;
  align-items: flex-start;
  gap: 10px;
  font-size: 0.93rem;
  line-height: 1.45;
  transition: border-color 150ms ease, background-color 150ms ease;
}

.opt-btn > span:last-child {
  flex: 1 1 0%;
  min-width: 0;
  max-width: 100%;
  overflow-x: auto;
  overflow-y: hidden;
  overflow-wrap: break-word;
  contain: layout inline-size;
}

.opt-btn:hover:not(:disabled) {
  border-color: var(--math);
  background: var(--surface-elevated);
}

.opt-btn.selected {
  border-color: var(--math);
  background: var(--math-soft);
  font-weight: 600;
}

.opt-btn.correct {
  border-color: var(--accent);
  background: var(--accent-soft);
  color: var(--text);
}

.opt-btn.wrong {
  border-color: var(--danger);
  background: var(--danger-soft);
  color: var(--text);
}

.opt-letter {
  width: 28px;
  height: 28px;
  border-radius: 999px;
  background: var(--surface-elevated);
  border: 1px solid var(--border);
  color: var(--text);
  font-family: var(--font-mono);
  font-weight: 700;
  font-size: 0.8rem;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.opt-btn.selected .opt-letter {
  background: var(--math);
  border-color: var(--math);
  color: #FFFFFF;
}

.opt-btn.correct .opt-letter {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--on-accent);
}

.opt-btn.wrong .opt-letter {
  background: var(--danger);
  border-color: var(--danger);
  color: var(--on-danger);
}

.numeric-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
  max-width: 100%;
}

.num-input {
  flex: 1 1 160px;
  min-width: 0;
  max-width: 100%;
  min-height: 44px;
  padding: 8px 12px;
  border: 1.5px solid var(--border);
  background: var(--surface);
  color: var(--text);
  border-radius: 10px;
  font-size: 0.96rem;
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
}

.num-input:focus {
  border-color: var(--math);
}

.feedback-box {
  margin-top: 12px;
  padding: 12px 14px;
  border-radius: 10px;
  font-size: 0.92rem;
  border: 1px solid var(--border);
  color: var(--text);
  min-width: 0;
  max-width: 100%;
  overflow-x: auto;
  overflow-y: hidden;
  overflow-wrap: break-word;
  contain: layout inline-size;
}

.feedback-box.ok {
  background: var(--accent-soft);
  border-color: var(--accent-border);
}

.feedback-box.err {
  background: var(--danger-soft);
  border-color: var(--danger-border);
}

.feedback-title {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-weight: 700;
}

.feedback-box.ok .feedback-title {
  color: var(--accent-text);
}

.feedback-box.err .feedback-title {
  color: var(--danger-text);
}

.lesson-nav-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 10px;
  margin-top: 18px;
  padding-top: 16px;
  border-top: 1px solid var(--border);
}

/* ==========================================================================
   TAB 2: GLOSSARY (THUẬT NGỮ)
   ========================================================================== */

.glossary-toolbar {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.search-wrap {
  position: relative;
  display: flex;
  align-items: center;
}

.search-wrap .search-ico {
  position: absolute;
  left: 14px;
  color: var(--text-muted);
  pointer-events: none;
}

.search-input {
  width: 100%;
  min-width: 0;
  min-height: 46px;
  padding: 10px 14px 10px 42px;
  border: 1.5px solid var(--border);
  border-radius: 10px;
  font-size: 0.95rem;
  background: var(--surface-inset);
  color: var(--text);
}

.search-input::placeholder {
  color: var(--text-muted);
}

.search-input:focus {
  border-color: var(--math);
}

.filter-pills {
  display: flex;
  gap: 6px;
  width: 100%;
  max-width: 100%;
  overflow-x: auto;
  padding-bottom: 4px;
  contain: layout inline-size;
}

.flex-wrap-gap > .filter-pills {
  width: auto;
  flex: 0 1 auto;
  contain: layout;
}

.filter-pill {
  appearance: none;
  border: 1px solid var(--border);
  background: var(--surface-elevated);
  color: var(--text-muted);
  border-radius: 999px;
  padding: 8px 14px;
  min-height: 44px;
  font-size: 0.83rem;
  font-weight: 600;
  white-space: nowrap;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  transition: background-color 150ms ease, color 150ms ease, border-color 150ms ease;
}

.filter-pill:hover {
  color: var(--text);
  border-color: var(--math);
}

.filter-pill.active {
  background: var(--accent);
  color: var(--on-accent);
  border-color: var(--accent);
}

.glossary-grid {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 12px;
}

.glossary-grid > * {
  min-width: 0;
}

@media (min-width: 760px) {
  .glossary-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}

.term-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 16px;
  transition: border-color 150ms ease, box-shadow 150ms ease;
  min-width: 0;
  max-width: 100%;
  contain: layout;
  overflow-x: auto;
}

.term-card:hover {
  border-color: var(--math);
  box-shadow: var(--shadow-sm);
}

.term-card.expanded {
  grid-column: 1 / -1;
  border-color: var(--accent);
  box-shadow: var(--shadow-md);
}

.term-head-btn {
  appearance: none;
  background: transparent;
  border: none;
  padding: 0;
  width: 100%;
  text-align: left;
  color: inherit;
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 10px;
  min-height: 44px;
}

.term-name {
  font-size: 1rem;
  font-weight: 700;
  color: var(--text);
}

.term-symbol {
  font-family: var(--font-mono);
  font-size: 0.84rem;
  color: var(--math);
  margin-top: 3px;
}

.term-detail {
  margin-top: 12px;
  padding-top: 12px;
  border-top: 1px solid var(--border);
  min-width: 0;
  max-width: 100%;
  overflow-x: auto;
  overflow-y: hidden;
  overflow-wrap: break-word;
  contain: layout inline-size;
}

.rel-chip {
  appearance: none;
  border: 1px solid var(--math-border);
  background: var(--math-soft);
  color: var(--math);
  border-radius: 999px;
  padding: 6px 12px;
  min-height: 44px;
  font-size: 0.8rem;
  font-weight: 600;
  display: inline-flex;
  align-items: center;
  gap: 5px;
  margin-right: 6px;
  margin-top: 6px;
  transition: background-color 150ms ease, border-color 150ms ease;
}

.rel-chip:hover {
  background: var(--surface-elevated);
  border-color: var(--math);
}

/* ==========================================================================
   TAB 3: PRACTICE (ÔN TẬP)
   ========================================================================== */

.mode-grid {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 12px;
}

.mode-grid > * {
  min-width: 0;
}

@media (min-width: 640px) {
  .mode-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}

@media (min-width: 980px) {
  .mode-grid {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }
}

.mode-card {
  appearance: none;
  text-align: left;
  background: var(--surface);
  border: 1.5px solid var(--border);
  border-radius: 12px;
  padding: 16px;
  min-height: 124px;
  color: var(--text);
  transition: border-color 150ms ease, background-color 150ms ease, box-shadow 150ms ease;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  gap: 10px;
}

.mode-card:hover:not(:disabled) {
  border-color: var(--accent);
  background: var(--surface-elevated);
  box-shadow: var(--shadow-md);
}

.mode-card:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.mode-title {
  font-size: 0.96rem;
  font-weight: 700;
  color: var(--text);
  display: flex;
  align-items: center;
  gap: 8px;
}

.mode-title .ui-ico {
  color: var(--math);
}

.mode-desc {
  font-size: 0.85rem;
  color: var(--text-muted);
  margin-top: 4px;
}

/* ==========================================================================
   TAB 4: MOCK EXAM (THI THỬ)
   ========================================================================== */

.config-grid {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 16px;
}

.config-grid > * {
  min-width: 0;
}

@media (min-width: 740px) {
  .config-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}

.field-group {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.field-label {
  font-size: 0.86rem;
  font-weight: 700;
  color: var(--text);
}

.field-control {
  min-height: 44px;
  min-width: 0;
  max-width: 100%;
  padding: 8px 12px;
  border: 1.5px solid var(--border);
  border-radius: 10px;
  font-size: 0.92rem;
  background: var(--surface-inset);
  color: var(--text);
}

.check-row {
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 44px;
  font-size: 0.9rem;
  color: var(--text);
}

.check-row input[type="checkbox"] {
  width: 20px;
  height: 20px;
  flex-shrink: 0;
  accent-color: var(--accent);
}

.preset-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
}

.exam-topbar {
  position: sticky;
  top: calc(var(--header-h) + 8px);
  z-index: 400;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px 14px;
  margin-bottom: 14px;
  box-shadow: var(--shadow-md);
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.timer-badge {
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
  font-size: 1.04rem;
  font-weight: 700;
  padding: 8px 14px;
  min-height: 44px;
  border-radius: 10px;
  background: var(--surface-elevated);
  border: 1px solid var(--border);
  color: var(--text);
  display: inline-flex;
  align-items: center;
  gap: 7px;
}

.timer-badge.warn-10 {
  background: #B45309;
  border-color: #D97706;
  color: #FFFFFF;
}

.timer-badge.warn-5 {
  background: var(--danger);
  border-color: var(--danger);
  color: var(--on-danger);
  animation: pulseTimer 1.2s infinite;
}

@keyframes pulseTimer {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.78; }
}

.progress-track {
  width: 100%;
  height: 8px;
  background: var(--surface-elevated);
  border-radius: 999px;
  overflow: hidden;
}

.progress-fill {
  height: 100%;
  background: linear-gradient(90deg, var(--math) 0%, var(--accent) 100%);
  transition: width 200ms ease;
}

.exam-workspace {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 16px;
}

.exam-workspace > * {
  min-width: 0;
}

@media (min-width: 920px) {
  .exam-workspace {
    grid-template-columns: minmax(0, 1fr) 288px;
    align-items: start;
  }
}

.q-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(44px, 1fr));
  gap: 6px;
}

@media (min-width: 920px) {
  .q-grid {
    grid-template-columns: repeat(5, minmax(0, 1fr));
  }
}

.q-node {
  appearance: none;
  border: 1.5px solid var(--border);
  background: var(--surface-inset);
  color: var(--text);
  border-radius: 9px;
  min-height: 44px;
  min-width: 0;
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
  font-size: 0.84rem;
  font-weight: 700;
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: border-color 150ms ease, background-color 150ms ease;
}

.q-node.answered {
  background: var(--math-soft);
  border-color: var(--math);
  color: var(--math);
}

.q-node.current {
  box-shadow: 0 0 0 2px var(--accent);
}

.q-node.flagged::after {
  content: '';
  position: absolute;
  top: 4px;
  right: 4px;
  width: 7px;
  height: 7px;
  border-radius: 999px;
  background: var(--amber);
}

.score-hero {
  background: linear-gradient(135deg, var(--surface-elevated) 0%, var(--surface) 100%);
  border: 1px solid var(--accent-border);
  color: var(--text);
  border-radius: 14px;
  padding: 20px;
  margin-bottom: 16px;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
}

.score-big {
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
  font-size: 2.2rem;
  font-weight: 800;
  line-height: 1.15;
  color: var(--accent-text);
  margin: 4px 0;
}

.stat-row {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 10px;
  margin-bottom: 16px;
}

.stat-row > * {
  min-width: 0;
}

.stat-box {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px 8px;
  text-align: center;
}

.stat-val {
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
  font-size: 1.25rem;
  font-weight: 800;
}

.stat-lbl {
  font-size: 0.78rem;
  color: var(--text-muted);
  margin-top: 2px;
}

.review-opt {
  padding: 8px 12px;
  border-radius: 8px;
  margin-top: 6px;
  border: 1px solid var(--border);
  background: var(--surface-inset);
  width: 100%;
  max-width: 100%;
  min-width: 0;
  overflow-x: auto;
  overflow-y: hidden;
  overflow-wrap: break-word;
  contain: layout inline-size;
  display: flex;
  align-items: flex-start;
  gap: 8px;
}

.review-opt > span:last-child {
  flex: 1 1 0%;
  min-width: 0;
  max-width: 100%;
  overflow-x: auto;
  overflow-y: hidden;
}

.review-opt.is-correct {
  background: var(--accent-soft);
  border-color: var(--accent-border);
  font-weight: 600;
}

.review-opt.is-wrong {
  background: var(--danger-soft);
  border-color: var(--danger-border);
}

/* ==========================================================================
   MODAL
   ========================================================================== */

.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(2, 6, 23, 0.72);
  backdrop-filter: blur(4px);
  z-index: 1200;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 14px;
}

.modal-overlay[hidden] {
  display: none !important;
}

.modal-dialog {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 16px;
  width: 100%;
  max-width: 780px;
  max-height: 88vh;
  display: flex;
  flex-direction: column;
  box-shadow: var(--shadow-lg);
  overflow: hidden;
  contain: layout;
}

.modal-header {
  padding: 14px 18px;
  background: var(--surface-elevated);
  border-bottom: 1px solid var(--border);
  color: var(--text);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.modal-title {
  margin: 0;
  font-size: 1.05rem;
  font-weight: 700;
}

.modal-close {
  appearance: none;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  min-width: 44px;
  min-height: 44px;
  border-radius: 10px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: background-color 150ms ease, border-color 150ms ease;
}

.modal-close:hover {
  border-color: var(--math);
  background: var(--surface-elevated);
}

.modal-body {
  padding: 16px 18px;
  min-width: 0;
  overflow-y: auto;
  overflow-x: hidden;
  font-size: 0.93rem;
}

.modal-footer {
  padding: 12px 18px;
  border-top: 1px solid var(--border);
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 8px;
  background: var(--surface-elevated);
}

.modal-footer:empty {
  display: none;
}

.mt-8 { margin-top: 8px; }
.mt-12 { margin-top: 12px; }
.mb-8 { margin-bottom: 8px; }
.mb-12 { margin-bottom: 12px; }
.flex-wrap-gap {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
}

@media (prefers-reduced-motion: reduce) {
  html {
    scroll-behavior: auto;
  }
  *,
  *::before,
  *::after {
    transition: none !important;
    animation: none !important;
  }
}
