@layer reset, base, components, utilities;

:root {
  color-scheme: light dark;
  --in-oklab: ;
  --topbar-h: 3.5rem;

  /* Tier 1 & 2 Semantic Tokens (Light default + @media fallback + light-dark progressive enhancement) */
  --bg-page: #f1f5f9;
  --bg-surface: #ffffff;
  --bg-subtle: #f8fafc;
  --bg-elevated: #ffffff;
  --bg-topbar: #0f172a;
  --bg-ai: #f0f6ff;

  --text-primary: #0f172a;
  --text-secondary: #475569;
  --text-muted: #64748b;
  --text-on-dark: #f8fafc;

  --border-subtle: #e2e8f0;
  --border-strong: #94a3b8;
  --border-ai: #bfdbfe;

  --brand: #2563eb;
  --brand-hover: #1d4ed8;
  --brand-soft: #eff6ff;
  --brand-text: #1e40af;

  --success: #15803d;
  --success-hover: #166534;
  --success-soft: #dcfce7;
  --success-text: #166534;

  --warning: #b45309;
  --warning-hover: #92400e;
  --warning-soft: #fef3c7;
  --warning-text: #92400e;

  --danger: #b91c1c;
  --danger-hover: #991b1b;
  --danger-soft: #fee2e2;
  --danger-text: #991b1b;

  --twk-bg: #dbeafe;
  --twk-fg: #1e3a8a;
  --tiu-bg: #ede9fe;
  --tiu-fg: #4c1d95;
  --tkp-bg: #dcfce7;
  --tkp-fg: #14532d;

  --focus-ring: #2563eb;
  --shadow-sm: 0 1px 2px rgba(15, 23, 42, 0.05);
  --shadow-card: 0 1px 3px rgba(15, 23, 42, 0.07), 0 6px 16px -4px rgba(15, 23, 42, 0.05);
  --shadow-modal: 0 20px 50px -12px rgba(15, 23, 42, 0.32);

  --radius-sm: 0.45rem;
  --radius-md: 0.75rem;
  --radius-lg: 1rem;

  --scrollbar-thumb: #94a3b8;
  --scrollbar-track: #e2e8f0;

  &:has(> head > meta[name="color-scheme"][content="dark"]) {
    color-scheme: dark;
  }
  &:has(> head > meta[name="color-scheme"][content="light"]) {
    color-scheme: light;
  }
}

@media (prefers-color-scheme: dark) {
  :root:not(:has(> head > meta[name="color-scheme"][content="light"])) {
    --bg-page: #0b1120;
    --bg-surface: #111827;
    --bg-subtle: #1e293b;
    --bg-elevated: #172033;
    --bg-topbar: #090e1a;
    --bg-ai: #132038;

    --text-primary: #f1f5f9;
    --text-secondary: #cbd5e1;
    --text-muted: #94a3b8;

    --border-subtle: #283548;
    --border-strong: #475569;
    --border-ai: #1e3a5f;

    --brand: #60a5fa;
    --brand-hover: #3b82f6;
    --brand-soft: #172554;
    --brand-text: #bfdbfe;

    --success: #22c55e;
    --success-hover: #16a34a;
    --success-soft: #14532d;
    --success-text: #bbf7d0;

    --warning: #f59e0b;
    --warning-hover: #d97706;
    --warning-soft: #451a03;
    --warning-text: #fde68a;

    --danger: #f87171;
    --danger-hover: #ef4444;
    --danger-soft: #450a0a;
    --danger-text: #fecaca;

    --twk-bg: #1e3a8a;
    --twk-fg: #dbeafe;
    --tiu-bg: #4c1d95;
    --tiu-fg: #ede9fe;
    --tkp-bg: #14532d;
    --tkp-fg: #dcfce7;

    --focus-ring: #93c5fd;
    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.35);
    --shadow-card: 0 1px 3px rgba(0, 0, 0, 0.4), 0 8px 20px -4px rgba(0, 0, 0, 0.35);
    --shadow-modal: 0 24px 60px rgba(0, 0, 0, 0.7);

    --scrollbar-thumb: #475569;
    --scrollbar-track: #0f172a;
  }
}

@supports (color: light-dark(white, black)) {
  :root {
    --bg-page: light-dark(#f1f5f9, #0b1120);
    --bg-surface: light-dark(#ffffff, #111827);
    --bg-subtle: light-dark(#f8fafc, #1e293b);
    --bg-elevated: light-dark(#ffffff, #172033);
    --bg-topbar: light-dark(#0f172a, #090e1a);
    --bg-ai: light-dark(#f0f6ff, #132038);

    --text-primary: light-dark(#0f172a, #f1f5f9);
    --text-secondary: light-dark(#475569, #cbd5e1);
    --text-muted: light-dark(#64748b, #94a3b8);

    --border-subtle: light-dark(#e2e8f0, #283548);
    --border-strong: light-dark(#94a3b8, #475569);
    --border-ai: light-dark(#bfdbfe, #1e3a5f);

    --brand: light-dark(#2563eb, #60a5fa);
    --brand-hover: light-dark(#1d4ed8, #3b82f6);
    --brand-soft: light-dark(#eff6ff, #172554);
    --brand-text: light-dark(#1e40af, #bfdbfe);

    --success: light-dark(#15803d, #22c55e);
    --success-hover: light-dark(#166534, #16a34a);
    --success-soft: light-dark(#dcfce7, #14532d);
    --success-text: light-dark(#166534, #bbf7d0);

    --warning: light-dark(#b45309, #f59e0b);
    --warning-hover: light-dark(#92400e, #d97706);
    --warning-soft: light-dark(#fef3c7, #451a03);
    --warning-text: light-dark(#92400e, #fde68a);

    --danger: light-dark(#b91c1c, #f87171);
    --danger-hover: light-dark(#991b1b, #ef4444);
    --danger-soft: light-dark(#fee2e2, #450a0a);
    --danger-text: light-dark(#991b1b, #fecaca);

    --twk-bg: light-dark(#dbeafe, #1e3a8a);
    --twk-fg: light-dark(#1e3a8a, #dbeafe);
    --tiu-bg: light-dark(#ede9fe, #4c1d95);
    --tiu-fg: light-dark(#4c1d95, #ede9fe);
    --tkp-bg: light-dark(#dcfce7, #14532d);
    --tkp-fg: light-dark(#14532d, #dcfce7);

    --focus-ring: light-dark(#2563eb, #93c5fd);
    --scrollbar-thumb: light-dark(#94a3b8, #475569);
    --scrollbar-track: light-dark(#e2e8f0, #0f172a);
  }
}

@supports (linear-gradient(in oklab, white, black)) {
  :root {
    --in-oklab: in oklab;
  }
}

@layer reset {
  html {
    box-sizing: border-box;
    accent-color: var(--brand);
    scrollbar-color: var(--scrollbar-thumb) var(--scrollbar-track);
    scrollbar-width: thin;
  }
  *, *::before, *::after {
    box-sizing: inherit;
  }
  body, h1, h2, h3, h4, p, ul, ol, figure, blockquote {
    margin: 0;
  }
  input, button, textarea, select {
    box-sizing: border-box;
    font: inherit;
    color: inherit;
  }
  img {
    max-inline-size: 100%;
    height: auto;
  }
}

@supports not (scrollbar-color: auto) {
  ::-webkit-scrollbar { width: 10px; height: 10px; }
  ::-webkit-scrollbar-thumb { background: var(--scrollbar-thumb); border-radius: 999px; }
  ::-webkit-scrollbar-track { background: var(--scrollbar-track); }
}

@layer base {
  body {
    font-family: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Inter, sans-serif;
    font-size: 0.95rem;
    line-height: 1.55;
    text-align: start;
    background-color: var(--bg-page);
    background-image: radial-gradient(ellipse 80% 45% at 50% -10%, rgba(37, 99, 235, 0.08), transparent);
    color: var(--text-primary);
    min-block-size: 100dvh;
    overflow-x: clip;
  }
  code { overflow-wrap: anywhere; }

  h1, h2, h3, h4 {
    text-wrap: balance;
    line-height: 1.25;
    letter-spacing: -0.015em;
  }

  p { text-wrap: pretty; }

  a {
    color: var(--brand);
    text-decoration: none;
  }
  a:hover { text-decoration: underline; }

  :where(a:any-link, button, input, select, textarea, [tabindex]):focus-visible {
    outline: 2.5px solid var(--focus-ring);
    outline-offset: 2px;
  }

  progress {
    accent-color: var(--brand);
    inline-size: 100%;
    block-size: 0.5rem;
    border-radius: 999px;
    overflow: hidden;
  }
}

@layer components {
  .visually-hidden:where(:not(:focus-within, :active)) {
    position: absolute !important;
    clip-path: inset(50%) !important;
    overflow: hidden !important;
    width: 1px !important;
    height: 1px !important;
    margin: -1px !important;
    padding: 0 !important;
    border: 0 !important;
    white-space: nowrap !important;
  }

  .skip-link:focus {
    position: fixed;
    inset-block-start: 0.75rem;
    inset-inline-start: 0.75rem;
    z-index: 100;
    background: var(--brand);
    color: #fff;
    padding: 0.55rem 0.95rem;
    border-radius: var(--radius-sm);
    font-weight: 700;
    box-shadow: var(--shadow-modal);
  }

  /* Compact sticky header (single row) */
  .topbar {
    position: sticky;
    inset-block-start: 0;
    z-index: 40;
    background: var(--bg-topbar);
    color: var(--text-on-dark);
    border-block-end: 1px solid rgba(255, 255, 255, 0.08);
    box-shadow: 0 2px 10px rgba(2, 6, 23, 0.18);
  }

  .topbar-inner {
    inline-size: 100%;
    max-inline-size: none;
    margin: 0;
    padding-inline: 1.25rem;
    min-block-size: var(--topbar-h);
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.75rem;
  }

  .topbar-brand {
    display: inline-flex;
    align-items: center;
    gap: 0.55rem;
    font-weight: 800;
    font-size: 1.05rem;
    color: #fff;
    letter-spacing: -0.02em;
  }
  .topbar-brand:hover { text-decoration: none; opacity: 0.92; }

  .brand-icon {
    inline-size: 1.35rem;
    block-size: 1.35rem;
    color: #60a5fa;
  }

  .brand-pill {
    background: linear-gradient(135deg var(--in-oklab), #2563eb, #4f46e5);
    color: #fff;
    font-size: 0.7rem;
    font-weight: 700;
    padding: 0.16rem 0.55rem;
    border-radius: 999px;
    letter-spacing: 0.03em;
  }

  .topbar-actions {
    display: flex;
    align-items: center;
    gap: 0.65rem;
    flex-wrap: wrap;
  }

  .topbar-nav {
    display: flex;
    align-items: center;
    gap: 0.35rem;
  }
  .topbar-nav a {
    color: #cbd5e1;
    font-weight: 600;
    font-size: 0.88rem;
    padding: 0.4rem 0.7rem;
    border-radius: var(--radius-sm);
    transition: background 0.15s, color 0.15s;
  }
  .topbar-nav a:hover {
    background: rgba(255, 255, 255, 0.1);
    color: #fff;
    text-decoration: none;
  }

  .user-chip {
    display: inline-flex;
    align-items: center;
    gap: 0.35rem;
    color: #e2e8f0;
    background: rgba(255, 255, 255, 0.1);
    border: 1px solid rgba(255, 255, 255, 0.12);
    padding: 0.25rem 0.65rem;
    border-radius: 999px;
    font-size: 0.8rem;
    font-weight: 600;
  }

  .btn-icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-inline-size: 2.35rem;
    min-block-size: 2.35rem;
    padding: 0.35rem;
    border-radius: var(--radius-sm);
    border: 1px solid rgba(255, 255, 255, 0.18);
    background: rgba(255, 255, 255, 0.08);
    color: #f8fafc;
    cursor: pointer;
    transition: background 0.15s;
  }
  .btn-icon:hover { background: rgba(255, 255, 255, 0.18); }
  .btn-icon svg { inline-size: 1.1rem; block-size: 1.1rem; }

  /* Two-state theme icon switching without JS class soup */
  .icon-sun { display: none; }
  .icon-moon { display: block; }

  @media (prefers-color-scheme: dark) {
    :root:not(:has(> head > meta[name="color-scheme"][content="light"])) .icon-sun { display: block; }
    :root:not(:has(> head > meta[name="color-scheme"][content="light"])) .icon-moon { display: none; }
  }
  :root:has(> head > meta[name="color-scheme"][content="dark"]) .icon-sun { display: block; }
  :root:has(> head > meta[name="color-scheme"][content="dark"]) .icon-moon { display: none; }

  .logout-form { margin: 0; }

  .container {
    inline-size: 100%;
    max-inline-size: none;
    min-block-size: calc(100dvh - var(--topbar-h));
    margin: 0;
    padding: 1.25rem 1.5rem 2rem;
  }
  .container:has(.admin-shell) {
    padding: 0;
  }
  .container:has(.login-wrap) {
    display: grid;
    place-items: center;
    padding: 1.25rem;
  }
  .container:has(.exam-shell) {
    display: flex;
    flex-direction: column;
  }
  @media (max-width: 720px) {
    .topbar-inner { padding-inline: 0.9rem; }
    .brand-pill, .user-chip { display: none; }
    .container { padding: 1rem 1rem 2rem; }
    .container:has(.admin-shell) { padding: 0; }
  }

  /* Admin Full-Screen Sidebar Shell */
  .admin-shell {
    display: grid;
    grid-template-columns: 16.5rem minmax(0, 1fr);
    gap: 0;
    min-block-size: calc(100dvh - var(--topbar-h));
    align-items: stretch;
  }

  .admin-sidebar {
    position: sticky;
    inset-block-start: var(--topbar-h);
    block-size: calc(100dvh - var(--topbar-h));
    display: flex;
    flex-direction: column;
    background: var(--bg-surface);
    border-inline-end: 1px solid var(--border-subtle);
    padding: 1.15rem 1rem;
    overflow-y: auto;
    overscroll-behavior: contain;
    scrollbar-gutter: stable;
  }

  .admin-sidebar-footer {
    margin-block-start: auto;
    padding-block-start: 1rem;
    border-block-start: 1px solid var(--border-subtle);
  }

  .admin-sidebar-head {
    padding-block-end: 0.85rem;
    margin-block-end: 0.85rem;
    border-block-end: 1px solid var(--border-subtle);
  }

  .admin-mini-stats {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 0.4rem;
    margin-block-start: 0.65rem;
  }
  .admin-mini-stat {
    background: var(--bg-subtle);
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-sm);
    padding: 0.35rem 0.4rem;
    text-align: center;
    font-size: 0.74rem;
    font-weight: 700;
  }

  .admin-nav {
    display: flex;
    flex-direction: column;
    gap: 0.3rem;
  }

  .admin-nav-btn {
    display: flex;
    align-items: center;
    gap: 0.65rem;
    inline-size: 100%;
    text-align: start;
    padding: 0.62rem 0.78rem;
    min-block-size: 2.55rem;
    border-radius: var(--radius-sm);
    border: 1px solid transparent;
    background: transparent;
    color: var(--text-secondary);
    font-weight: 600;
    font-size: 0.9rem;
    cursor: pointer;
    transition: background 0.14s, color 0.14s, border-color 0.14s;
  }
  .admin-nav-btn svg {
    inline-size: 1.1rem;
    block-size: 1.1rem;
    flex-shrink: 0;
  }
  .admin-nav-btn:hover {
    background: var(--bg-subtle);
    color: var(--text-primary);
  }
  .admin-nav-btn[aria-selected="true"],
  .admin-nav-btn.active {
    background: var(--brand-soft);
    color: var(--brand-text);
    border-color: color-mix(in oklab, var(--brand) 30%, transparent);
    font-weight: 700;
  }

  .admin-main {
    min-inline-size: 0;
    padding: 1.5rem 1.75rem 3rem;
  }

  .admin-mobile-bar {
    display: none;
    align-items: center;
    justify-content: space-between;
    gap: 0.75rem;
    margin: 0.9rem 1rem 0;
  }

  .admin-backdrop {
    display: none;
  }

  @media (max-width: 960px) {
    .admin-shell {
      grid-template-columns: 1fr;
    }
    .admin-main {
      padding: 1rem 1rem 2.5rem;
    }
    .admin-mobile-bar {
      display: flex;
    }
    .admin-sidebar {
      position: fixed;
      inset-block: 0;
      inset-inline-start: 0;
      z-index: 60;
      inline-size: min(18.5rem, 84dvw);
      block-size: 100dvh;
      max-block-size: 100dvh;
      box-shadow: var(--shadow-modal);
      transform: translateX(-105%);
      transition: transform 0.22s ease;
    }
    .admin-sidebar.is-open {
      transform: translateX(0);
    }
    .admin-backdrop.is-open {
      display: block;
      position: fixed;
      inset: 0;
      z-index: 55;
      background: rgba(2, 6, 23, 0.52);
      backdrop-filter: blur(2px);
    }
  }

  .page-hero {
    display: flex;
    align-items: flex-end;
    justify-content: space-between;
    gap: 1rem;
    flex-wrap: wrap;
    margin-block-end: 1.25rem;
  }
  .page-title {
    font-size: clamp(1.35rem, 1.1rem + 1.1vw, 1.85rem);
    font-weight: 800;
  }
  .page-subtitle {
    color: var(--text-muted);
    font-size: 0.92rem;
    margin-block-start: 0.2rem;
  }

  .card {
    background: var(--bg-surface);
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-card);
    padding: 1.35rem;
    margin-block-end: 1.15rem;
  }

  .card-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.75rem;
    margin-block-end: 1rem;
    flex-wrap: wrap;
  }

  .card-title {
    font-size: 1.08rem;
    font-weight: 700;
  }

  .grid-2 { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 1.15rem; }
  .grid-3 { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 0.95rem; }
  .grid-4 { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 0.95rem; }
  .card, .grid-2 > *, .grid-3 > *, .grid-4 > * { min-inline-size: 0; }

  @media (max-width: 860px) {
    .grid-2, .grid-3, .grid-4 { grid-template-columns: 1fr; }
  }

  .stat-box {
    background: var(--bg-surface);
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-md);
    box-shadow: var(--shadow-sm);
    padding: 1rem 1.1rem;
    display: flex;
    flex-direction: column;
    gap: 0.35rem;
  }
  .stat-label {
    font-size: 0.76rem;
    color: var(--text-muted);
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.04em;
  }
  .stat-value {
    font-size: 1.65rem;
    font-weight: 800;
    font-variant-numeric: tabular-nums;
    line-height: 1.1;
  }

  .package-item {
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-md);
    padding: 1rem 1.1rem;
    margin-block-end: 0.75rem;
    background: var(--bg-subtle);
    transition: border-color 0.15s, transform 0.15s;
  }
  .package-item:hover {
    border-color: var(--brand);
    transform: translateY(-1px);
  }

  /* Buttons */
  .btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 0.45rem;
    min-block-size: 2.45rem;
    padding: 0.5rem 1rem;
    font-size: 0.9rem;
    font-weight: 600;
    border-radius: var(--radius-sm);
    border: 1px solid transparent;
    cursor: pointer;
    background: var(--brand);
    color: #fff;
    transition: background 0.15s ease, transform 0.1s ease, opacity 0.15s ease;
  }
  .btn:hover:not(:disabled) {
    background: var(--brand-hover);
    text-decoration: none;
  }
  .btn:active:not(:disabled) { transform: scale(0.99); }
  .btn:disabled { opacity: 0.55; cursor: not-allowed; }

  .btn-sm { min-block-size: 2rem; padding: 0.35rem 0.75rem; font-size: 0.82rem; }
  .btn-outline {
    background: var(--bg-surface);
    color: var(--text-primary);
    border-color: var(--border-subtle);
  }
  .btn-outline:hover:not(:disabled) {
    background: var(--bg-subtle);
    border-color: var(--border-strong);
  }
  .btn-success { background: var(--success); color: #fff; }
  .btn-success:hover:not(:disabled) { background: var(--success-hover); }
  .btn-warning { background: var(--warning); color: #fff; }
  .btn-warning:hover:not(:disabled) { background: var(--warning-hover); }
  .btn-danger { background: var(--danger); color: #fff; }
  .btn-danger:hover:not(:disabled) { background: var(--danger-hover); }
  .btn-ghost {
    background: transparent;
    color: #e2e8f0;
    border-color: rgba(255, 255, 255, 0.22);
  }
  .btn-ghost:hover:not(:disabled) {
    background: rgba(255, 255, 255, 0.12);
    color: #fff;
  }

  /* Badges */
  .badge {
    display: inline-flex;
    align-items: center;
    gap: 0.3rem;
    padding: 0.18rem 0.6rem;
    border-radius: 999px;
    font-size: 0.75rem;
    font-weight: 700;
    white-space: nowrap;
  }
  .badge-twk { background: var(--twk-bg); color: var(--twk-fg); }
  .badge-tiu { background: var(--tiu-bg); color: var(--tiu-fg); }
  .badge-tkp { background: var(--tkp-bg); color: var(--tkp-fg); }
  .badge-pass { background: var(--success-soft); color: var(--success-text); }
  .badge-fail { background: var(--danger-soft); color: var(--danger-text); }
  .badge-muted { background: var(--bg-subtle); color: var(--text-muted); border: 1px solid var(--border-subtle); }

  /* Forms */
  label {
    display: block;
    font-size: 0.84rem;
    font-weight: 600;
    margin-block-end: 0.35rem;
    color: var(--text-secondary);
  }
  .checkbox-label {
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    cursor: pointer;
    font-weight: 600;
    color: var(--text-primary);
  }
  .checkbox-label input[type="checkbox"] { inline-size: 1.05rem; block-size: 1.05rem; }

  input[type="text"],
  input[type="password"],
  input[type="number"],
  input[type="url"],
  input[type="file"],
  select,
  textarea {
    width: 100%;
    padding: 0.58rem 0.78rem;
    border: 1px solid var(--border-strong);
    border-radius: var(--radius-sm);
    font-size: 0.92rem;
    background: var(--bg-surface);
    color: var(--text-primary);
    transition: border-color 0.15s, box-shadow 0.15s;
  }
  textarea { resize: vertical; }
  input:focus, select:focus, textarea:focus {
    border-color: var(--brand);
    box-shadow: 0 0 0 3px color-mix(in oklab, var(--brand) 18%, transparent);
    outline: none;
  }

  .form-row { margin-block-end: 0.9rem; }
  .field-hint { font-size: 0.78rem; color: var(--text-muted); margin-block-start: 0.25rem; display: block; }

  /* Password field with toggle */
  .input-with-action {
    position: relative;
    display: flex;
    align-items: center;
  }
  .input-with-action input { padding-inline-end: 4.5rem; }
  .input-action-btn {
    position: absolute;
    inset-inline-end: 0.4rem;
    background: var(--bg-subtle);
    border: 1px solid var(--border-subtle);
    color: var(--text-secondary);
    font-size: 0.76rem;
    font-weight: 600;
    padding: 0.25rem 0.55rem;
    border-radius: var(--radius-sm);
    cursor: pointer;
  }

  /* Alerts */
  .alert {
    display: flex;
    align-items: flex-start;
    gap: 0.55rem;
    padding: 0.75rem 0.95rem;
    border-radius: var(--radius-sm);
    font-size: 0.9rem;
    margin-block-end: 0.95rem;
  }
  .alert-error { background: var(--danger-soft); color: var(--danger-text); border: 1px solid color-mix(in oklab, var(--danger) 35%, transparent); }
  .alert-success { background: var(--success-soft); color: var(--success-text); border: 1px solid color-mix(in oklab, var(--success) 35%, transparent); }
  .alert-info { background: var(--brand-soft); color: var(--brand-text); border: 1px solid color-mix(in oklab, var(--brand) 30%, transparent); }

  /* Responsive Tables (container query stacked cards on narrow screens) */
  .table-wrapper {
    overflow: auto;
    max-inline-size: 100%;
    max-block-size: min(560px, 75vh);
    container-type: inline-size;
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-md);
  }
  .table-wrapper table {
    inline-size: 100%;
    border-collapse: separate;
    border-spacing: 0;
    font-size: 0.88rem;
  }
  .table-wrapper caption {
    text-align: start;
    padding: 0.75rem 0.9rem;
    font-weight: 700;
    font-size: 0.92rem;
    color: var(--text-secondary);
    background: var(--bg-subtle);
    border-block-end: 1px solid var(--border-subtle);
  }
  .table-wrapper thead {
    position: sticky;
    inset-block-start: 0;
    z-index: 2;
    background: var(--bg-subtle);
  }
  .table-wrapper th,
  .table-wrapper td {
    text-align: start;
    padding: 0.72rem 0.85rem;
    border-block-end: 1px solid var(--border-subtle);
    vertical-align: middle;
  }
  .table-wrapper th {
    font-size: 0.75rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    color: var(--text-muted);
    text-wrap: balance;
  }
  .table-wrapper tbody tr:hover { background: var(--bg-subtle); }

  @container (width < 580px) {
    .table-wrapper table,
    .table-wrapper thead,
    .table-wrapper tbody,
    .table-wrapper tr,
    .table-wrapper th,
    .table-wrapper td {
      display: block;
    }
    .table-wrapper thead { display: none; }
    .table-wrapper tr {
      margin: 0.65rem;
      border: 1px solid var(--border-subtle);
      border-radius: var(--radius-sm);
      background: var(--bg-surface);
      padding: 0.4rem 0.65rem;
    }
    .table-wrapper td,
    .table-wrapper tbody th {
      display: flex;
      justify-content: space-between;
      align-items: center;
      gap: 0.75rem;
      padding: 0.4rem 0;
      border-block-end: 1px dashed var(--border-subtle);
    }
    .table-wrapper td:last-child { border-block-end: none; }
    .table-wrapper td[data-label]::before {
      content: attr(data-label);
      font-weight: 700;
      font-size: 0.76rem;
      color: var(--text-muted);
      text-transform: uppercase;
    }
  }

  /* Login screen */
  .login-wrap {
    inline-size: min(27rem, 100%);
    margin: 0;
  }
  .login-brand-head {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    margin-block-end: 1rem;
  }
  .login-logo-box {
    inline-size: 2.75rem;
    block-size: 2.75rem;
    border-radius: var(--radius-md);
    background: linear-gradient(135deg var(--in-oklab), #2563eb, #4f46e5);
    color: #fff;
    display: grid;
    place-items: center;
    flex-shrink: 0;
  }
  .login-logo-box svg { inline-size: 1.5rem; block-size: 1.5rem; }

  /* Exam & CAT Workspace */
  .exam-shell {
    flex: 1;
    display: grid;
    grid-template-columns: minmax(0, 1fr) 20rem;
    gap: 1.25rem;
    align-items: start;
  }
  .exam-shell > * { min-inline-size: 0; }
  .exam-shell > aside {
    max-block-size: calc(100dvh - var(--topbar-h) - 2rem);
    overflow-y: auto;
    overscroll-behavior: contain;
    scrollbar-gutter: stable;
  }
  @media (max-width: 920px) {
    .exam-shell { grid-template-columns: 1fr; }
    .exam-shell > aside {
      position: static !important;
      max-block-size: none;
    }
  }

  .timer-box {
    font-variant-numeric: tabular-nums;
    font-size: 1.35rem;
    font-weight: 800;
    color: var(--brand-text);
    background: var(--brand-soft);
    border: 1px solid color-mix(in oklab, var(--brand) 30%, transparent);
    padding: 0.42rem 0.9rem;
    border-radius: var(--radius-sm);
    text-align: center;
    min-inline-size: 5.5rem;
  }
  .timer-box.danger {
    color: var(--danger-text);
    background: var(--danger-soft);
    border-color: color-mix(in oklab, var(--danger) 40%, transparent);
  }

  .q-nav-grid {
    display: grid;
    grid-template-columns: repeat(5, 1fr);
    gap: 0.45rem;
  }
  .q-nav-btn {
    min-block-size: 2.35rem;
    border-radius: var(--radius-sm);
    border: 1px solid var(--border-subtle);
    background: var(--bg-subtle);
    font-weight: 700;
    font-size: 0.84rem;
    font-variant-numeric: tabular-nums;
    cursor: pointer;
    color: var(--text-primary);
    transition: transform 0.1s, border-color 0.12s;
  }
  .q-nav-btn:hover { border-color: var(--brand); }
  .q-nav-btn.answered { background: #16a34a; color: #fff; border-color: #15803d; }
  .q-nav-btn.flagged { background: #d97706; color: #fff; border-color: #b45309; }
  .q-nav-btn.active { outline: 3px solid var(--brand); outline-offset: 1px; }

  .question-stem {
    font-size: 1.03rem;
    line-height: 1.65;
    max-inline-size: min(92ch, 100%);
    white-space: pre-wrap;
    margin-block-end: 1.1rem;
    overflow-wrap: break-word;
  }
  .question-image {
    max-inline-size: 100%;
    max-block-size: 22rem;
    border-radius: var(--radius-md);
    border: 1px solid var(--border-subtle);
    margin-block-end: 1.1rem;
    display: block;
    background: #fff;
  }

  .option-list { display: flex; flex-direction: column; gap: 0.55rem; }
  .option-item {
    display: flex;
    align-items: flex-start;
    gap: 0.75rem;
    padding: 0.75rem 0.95rem;
    border: 1.5px solid var(--border-subtle);
    border-radius: var(--radius-md);
    cursor: pointer;
    background: var(--bg-surface);
    text-align: start;
    inline-size: 100%;
    transition: border-color 0.12s, background 0.12s;
  }
  .option-item:hover { background: var(--bg-subtle); border-color: var(--border-strong); }
  .option-item.selected { background: var(--brand-soft); border-color: var(--brand); }
  .option-item.correct { background: var(--success-soft); border-color: var(--success); }
  .option-item.wrong { background: var(--danger-soft); border-color: var(--danger); }

  .option-key {
    font-weight: 800;
    min-inline-size: 1.65rem;
    block-size: 1.65rem;
    border-radius: var(--radius-sm);
    background: var(--bg-subtle);
    border: 1px solid var(--border-subtle);
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-size: 0.84rem;
    flex-shrink: 0;
  }
  .option-item.selected .option-key { background: var(--brand); border-color: var(--brand); color: #fff; }
  .option-item.correct .option-key { background: var(--success); border-color: var(--success); color: #fff; }
  .option-item.wrong .option-key { background: var(--danger); border-color: var(--danger); color: #fff; }

  /* AI Box & Markdown */
  .ai-box {
    margin-block-start: 1.1rem;
    border: 1px solid var(--border-ai);
    background: var(--bg-ai);
    border-radius: var(--radius-md);
    padding: 1rem 1.15rem;
  }
  .ai-md { max-inline-size: min(96ch, 100%); overflow-wrap: break-word; }
  .ai-md h1, .ai-md h2, .ai-md h3, .ai-md h4 { margin: 0.85rem 0 0.35rem; font-size: 1rem; }
  .ai-md p { margin: 0.45rem 0; }
  .ai-md ul, .ai-md ol { margin: 0.4rem 0 0.65rem 1.3rem; padding: 0; }
  .ai-md code { background: var(--bg-subtle); border: 1px solid var(--border-subtle); padding: 0.1rem 0.35rem; border-radius: 4px; font-size: 0.86em; }
  .ai-md pre { background: #0f172a; color: #e2e8f0; padding: 0.85rem 1rem; border-radius: var(--radius-sm); overflow-x: auto; font-size: 0.84rem; margin: 0.6rem 0; }
  .ai-md pre code { background: transparent; border: none; padding: 0; color: inherit; }

  .chat-box {
    margin-block-start: 0.9rem;
    border-block-start: 1px dashed var(--border-strong);
    padding-block-start: 0.9rem;
  }
  .chat-messages {
    display: flex;
    flex-direction: column;
    gap: 0.55rem;
    max-block-size: 22rem;
    overflow-y: auto;
    margin-block-end: 0.75rem;
    padding-inline-end: 0.25rem;
  }
  .chat-bubble {
    padding: 0.65rem 0.9rem;
    border-radius: var(--radius-md);
    font-size: 0.9rem;
  }
  .chat-bubble.user {
    background: var(--brand-soft);
    border: 1px solid color-mix(in oklab, var(--brand) 25%, transparent);
    align-self: flex-end;
    max-inline-size: 85%;
  }
  .chat-bubble.assistant {
    background: var(--bg-surface);
    border: 1px solid var(--border-subtle);
    align-self: flex-start;
    max-inline-size: 95%;
  }

  /* Accessible Tabs */
  .tabs {
    display: flex;
    gap: 0.45rem;
    flex-wrap: wrap;
    border-block-end: 1px solid var(--border-subtle);
    padding-block-end: 0.75rem;
    margin-block-end: 1.15rem;
  }
  .tab-btn {
    background: var(--bg-surface);
    color: var(--text-secondary);
    border: 1px solid var(--border-subtle);
    padding: 0.48rem 0.9rem;
    min-block-size: 2.35rem;
    border-radius: var(--radius-sm);
    font-weight: 600;
    font-size: 0.88rem;
    cursor: pointer;
    transition: background 0.12s, color 0.12s, border-color 0.12s;
  }
  .tab-btn:hover { background: var(--bg-subtle); color: var(--text-primary); }
  .tab-btn[aria-selected="true"],
  .tab-btn.active {
    background: var(--brand);
    color: #fff;
    border-color: var(--brand);
  }

  /* Native Modal Dialog */
  .app-dialog {
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-lg);
    background: var(--bg-surface);
    color: var(--text-primary);
    box-shadow: var(--shadow-modal);
    padding: 0;
    max-inline-size: min(27rem, calc(100vw - 2rem));
    inline-size: 100%;
  }
  .app-dialog:modal { overscroll-behavior: contain; }
  .app-dialog::backdrop {
    background: rgba(2, 6, 23, 0.58);
    backdrop-filter: blur(3px);
    overflow: hidden;
    overscroll-behavior: contain;
  }
  .dialog-body { padding: 1.35rem; }
  .dialog-title { font-size: 1.1rem; font-weight: 700; margin-block-end: 0.5rem; }
  .dialog-message { color: var(--text-secondary); font-size: 0.94rem; margin-block-end: 1.25rem; }
  .dialog-actions { display: flex; justify-content: flex-end; gap: 0.6rem; }

  /* Persistent Toast Stack */
  .toast-stack {
    position: fixed;
    inset-block-end: 1.1rem;
    inset-inline-end: 1.1rem;
    z-index: 90;
    display: flex;
    flex-direction: column;
    gap: 0.55rem;
    max-inline-size: min(24rem, calc(100vw - 2rem));
    pointer-events: none;
  }
  .toast-item {
    pointer-events: auto;
    margin: 0;
    inset: auto;
    position: static;
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-md);
    background: var(--bg-elevated);
    color: var(--text-primary);
    box-shadow: var(--shadow-modal);
    padding: 0.75rem 0.95rem;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.75rem;
    font-size: 0.88rem;
    font-weight: 500;
  }
  .toast-item[popover]:where(:popover-open, .\:popover-open) {
    display: flex;
    position: fixed;
    inset-block-end: 1.1rem;
    inset-inline-end: 1.1rem;
    inset-block-start: auto;
    inset-inline-start: auto;
    max-inline-size: min(24rem, calc(100vw - 2rem));
  }
  .toast-item.toast-success { border-inline-start: 4px solid var(--success); }
  .toast-item.toast-error { border-inline-start: 4px solid var(--danger); }
  .toast-item.toast-info { border-inline-start: 4px solid var(--brand); }
  .toast-close {
    background: transparent;
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-sm);
    color: var(--text-muted);
    cursor: pointer;
    padding: 0.15rem 0.45rem;
    font-size: 0.78rem;
  }
}

@layer utilities {
  .hidden { display: none !important; }
  .muted { color: var(--text-muted); font-size: 0.86rem; }
  .flex-row { display: flex; align-items: center; gap: 0.6rem; flex-wrap: wrap; }
  .spacer { flex: 1; }
  .kbd-hint {
    font-size: 0.75rem;
    padding: 0.1rem 0.4rem;
    border-radius: 4px;
    border: 1px solid var(--border-subtle);
    background: var(--bg-subtle);
    color: var(--text-secondary);
    font-family: ui-monospace, monospace;
  }
}

@media (pointer: coarse) {
  .btn, .q-nav-btn, .tab-btn { min-block-size: 2.75rem; }
  .option-item { padding: 0.9rem 1rem; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    transition-duration: 0.01ms !important;
    animation-duration: 0.01ms !important;
  }
}

@media (forced-colors: active) {
  .card, .stat-box, .option-item, .q-nav-btn, .app-dialog {
    border: 1px solid CanvasText;
  }
  .option-item.selected, .q-nav-btn.active {
    outline: 3px solid Highlight;
  }
}

@media print {
  .topbar,
  .btn,
  button,
  [role="tablist"],
  .chat-box,
  .bookmark-editor,
  .toast-container {
    display: none !important;
  }
  body {
    background: #fff !important;
    color: #111827 !important;
    font-size: 11pt;
  }
  .page-shell, .page-wide {
    padding: 0 !important;
    min-block-size: auto !important;
  }
  .card {
    box-shadow: none !important;
    border: 1px solid #d1d5db !important;
    margin-bottom: 1rem !important;
  }
  article.card {
    break-inside: avoid;
    page-break-inside: avoid;
  }
  .ai-panel, .alert {
    border: 1px solid #d1d5db !important;
    background: #f9fafb !important;
  }
}
