/* ==========================================================================
   YogiExam — design foundation
   Mobile-first. Every colour, radius, shadow and space is a token (spec §21);
   nothing downstream hardcodes a hex value.
   ========================================================================== */

/* --- 1. Tokens ---------------------------------------------------------- */

:root {
  /* Brand. --primary is overwritten at runtime from Settings → Branding. */
  --primary:          #4f46e5;
  --primary-dark:     #4338ca;
  --primary-soft:     #eef2ff;
  /* The brand colour as FOREGROUND. On a pale background the brand colour
     itself reads well; dark mode lightens it, because a dark indigo on a
     dark surface is invisible. Derived with color-mix so it follows any
     brand colour set in Settings. */
  --primary-ink:      var(--primary);
  --primary-contrast: #ffffff;
  --secondary:        #0ea5e9;
  --secondary-soft:   #e0f2fe;

  /* Surfaces */
  --background:   #f6f7fb;
  --surface:      #ffffff;
  --surface-alt:  #f1f3f9;
  --surface-sunk: #eaedf5;
  --overlay:      rgba(15, 23, 42, .48);

  /* Tell the browser which way its own widgets should be painted.
     Without this, a machine set to dark gave form controls and default
     buttons a dark UA background inside the light theme. */
  color-scheme: light;

  /* Text */
  --text-primary:   #0f172a;
  --text-secondary: #4e5866;
  --text-tertiary:  #626c7d;
  --text-inverse:   #ffffff;

  /* Lines */
  --border:        #e3e7ef;
  --border-strong: #cbd5e1;

  /* Status */
  --success:      #059669;
  --success-soft: #d1fae5;
  --warning:      #d97706;
  --warning-soft: #fef3c7;
  --danger:       #dc2626;
  --danger-soft:  #fee2e2;
  --info:         #0284c7;
  --info-soft:    #e0f2fe;

  /* Status colours as INK — i.e. text on their own soft tint. The plain
     tokens above stay the fill colours (solid buttons, bars, dots). */
  --success-ink:  #056f4e;
  /* Same green, dark enough to carry white text (5.5:1). */
  --success-solid: #047857;
  --warning-ink:  #8a5200;
  --danger-ink:   #b91c1c;
  --info-ink:     #0369a1;
  --premium-ink:  #92400e;
  --answered-ink: #056f4e;
  --unanswered-ink: #c02626;
  --review-ink:   #6d28d9;

  /* Exam palette — the question-palette states (spec §59). Each state also
     carries an icon and a text label, so colour is never the only signal. */
  --answered:        #059669;
  --answered-solid:  #047857;
  --answered-soft:   #d1fae5;
  --review:          #7c3aed;
  --review-soft:     #ede9fe;
  --answered-review: #0891b2;
  --answered-review-soft: #cffafe;
  --unanswered:      #ef4444;
  --unanswered-soft: #fee2e2;
  --not-visited:     #94a3b8;
  --not-visited-soft:#f1f5f9;

  /* Premium */
  --premium:      #b45309;
  --premium-soft: #fef3c7;
  --premium-gold: #f59e0b;

  /* Radii */
  --r-xs: 6px;
  --r-sm: 10px;
  --r-md: 14px;
  --r-lg: 18px;
  --r-xl: 24px;
  --r-full: 999px;

  /* Elevation — soft, never heavy */
  --shadow-xs: 0 1px 2px rgba(15, 23, 42, .05);
  --shadow-sm: 0 1px 3px rgba(15, 23, 42, .07), 0 1px 2px rgba(15, 23, 42, .04);
  --shadow-md: 0 4px 14px rgba(15, 23, 42, .07);
  --shadow-lg: 0 12px 32px rgba(15, 23, 42, .12);
  --shadow-up: 0 -4px 20px rgba(15, 23, 42, .08);

  /* Space scale */
  --s-1: 4px;  --s-2: 8px;  --s-3: 12px; --s-4: 16px;
  --s-5: 20px; --s-6: 24px; --s-8: 32px; --s-10: 40px; --s-12: 48px;

  /* Type */
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue",
          "Noto Sans Tamil", "Latha", sans-serif;
  --font-mono: ui-monospace, "SF Mono", "Cascadia Mono", Consolas, monospace;

  --t-xs: .75rem;    /* 12 */
  --t-sm: .8125rem;  /* 13 */
  --t-base: .9375rem;/* 15 */
  --t-md: 1rem;      /* 16 */
  --t-lg: 1.125rem;  /* 18 */
  --t-xl: 1.375rem;  /* 22 */
  --t-2xl: 1.75rem;  /* 28 */
  --t-3xl: 2.125rem; /* 34 */

  /* Layout */
  --header-h: 60px;
  --bottomnav-h: 64px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-top: env(safe-area-inset-top, 0px);
  --content-max: 1180px;
  /* Horizontal padding of the current page container. Edge-to-edge scrollers
     out-dent by exactly this much, so they can never overflow the viewport. */
  --page-gutter: var(--s-4);

  --ease: cubic-bezier(.4, 0, .2, 1);
  --ease-out: cubic-bezier(.16, 1, .3, 1);
}

/* Dark mode (spec §89). Math, images, the palette and inputs all stay legible
   because every one of them reads from these same tokens. */
:root[data-theme="dark"] {
  --primary-soft:     #232445;
  --primary-ink:      color-mix(in srgb, var(--primary) 40%, #ffffff);
  --primary-contrast: #ffffff;
  --secondary-soft:   #10314a;

  --background:   #0b1020;
  --surface:      #141a2e;
  --surface-alt:  #1b2238;
  --surface-sunk: #0f1526;
  --overlay:      rgba(2, 6, 23, .7);

  --text-primary:   #eaeef7;
  --text-secondary: #97a3bd;
  --text-tertiary:  #8f9cbb;
  --text-inverse:   #0b1020;

  --border:        #26304b;
  --border-strong: #38425f;

  --success-soft: #06301f;
  --warning-soft: #33230a;
  --danger-soft:  #3a1113;
  --info-soft:    #06243a;

  /* On the near-black tints above the ink has to go the other way. */
  --success-ink:  #5fdcaa;
  --success-solid: #047857;
  --warning-ink:  #f5c451;
  --danger-ink:   #ff9b9b;
  --info-ink:     #7fd0f7;
  --premium-ink:  #f5c451;
  --answered-ink: #5fdcaa;
  --answered-solid: #047857;
  --unanswered-ink: #ff9b9b;
  --review-ink:   #c3aeff;

  --answered-soft:        #06301f;
  --review-soft:          #221a45;
  --answered-review-soft: #072e37;
  --unanswered-soft:      #3a1113;
  --not-visited-soft:     #1b2238;
  --premium-soft:         #33230a;

  --shadow-xs: 0 1px 2px rgba(0, 0, 0, .4);
  --shadow-sm: 0 1px 3px rgba(0, 0, 0, .45);
  --shadow-md: 0 4px 14px rgba(0, 0, 0, .5);
  --shadow-lg: 0 12px 32px rgba(0, 0, 0, .6);
  --shadow-up: 0 -4px 20px rgba(0, 0, 0, .5);

  color-scheme: dark;
}

/* --- 2. Reset ----------------------------------------------------------- */

*, *::before, *::after { box-sizing: border-box; }

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
  scroll-behavior: smooth;
}

body {
  margin: 0;
  font-family: var(--font);
  font-size: var(--t-base);
  line-height: 1.6;
  color: var(--text-primary);
  background: var(--background);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  overflow-x: hidden;
  /* Stops the double-tap-to-zoom delay on Android without disabling pinch. */
  touch-action: manipulation;
}

h1, h2, h3, h4, h5 { margin: 0 0 var(--s-3); font-weight: 650; line-height: 1.25;
                     letter-spacing: -.011em; color: var(--text-primary); }
h1 { font-size: var(--t-2xl); }
h2 { font-size: var(--t-xl); }
h3 { font-size: var(--t-lg); }
h4 { font-size: var(--t-md); }
p  { margin: 0 0 var(--s-3); }

a { color: var(--primary-ink); text-decoration: none; }
a:hover { text-decoration: underline; }

img, svg, video { max-width: 100%; height: auto; display: block; }

button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
ul, ol { margin: 0 0 var(--s-3); padding-left: 1.25rem; }
hr { border: 0; border-top: 1px solid var(--border); margin: var(--s-5) 0; }

code, kbd, pre { font-family: var(--font-mono); font-size: .9em; }

:focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: 2px;
  border-radius: var(--r-xs);
}

::selection { background: var(--primary); color: var(--primary-contrast); }

/* Accessible hiding — visible to screen readers, invisible on screen. */
.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0;
}

.skip-link {
  position: absolute; left: var(--s-3); top: -60px; z-index: 200;
  background: var(--surface); color: var(--text-primary);
  padding: var(--s-2) var(--s-4); border-radius: var(--r-sm);
  box-shadow: var(--shadow-md); transition: top .18s var(--ease);
}
.skip-link:focus { top: calc(var(--s-3) + var(--safe-top)); }

/* The bare shell — layouts/bare.php. Used by the admin sign-in, the error
   pages, the installer, the offline page and the exam runner.

   It lives here rather than in auth.css because base.css is the only
   stylesheet every page loads: when these rules sat in auth.css, the two bare
   pages that do not load it — admin sign-in and every error screen — had no
   layout at all and their content collapsed into the top-left corner. */
.bare-page { background: var(--background); }

.bare-main {
  min-height: 100dvh; min-height: 100vh;
  display: grid; place-items: center;
  padding: var(--s-6);
}

/* --- 3. Icons ----------------------------------------------------------- */

.icon      { width: 20px; height: 20px; flex: 0 0 auto; }
.icon-sm   { width: 16px; height: 16px; flex: 0 0 auto; }
.icon-lg   { width: 24px; height: 24px; flex: 0 0 auto; }
.icon-xl   { width: 32px; height: 32px; flex: 0 0 auto; }

/* --- 4. Layout helpers -------------------------------------------------- */

.container { width: 100%; max-width: var(--content-max); margin-inline: auto;
             padding-inline: var(--s-4); }
@media (min-width: 768px) { .container { padding-inline: var(--s-6); } }

.stack > * + * { margin-top: var(--s-4); }
.stack-sm > * + * { margin-top: var(--s-2); }
.stack-lg > * + * { margin-top: var(--s-6); }

.row      { display: flex; align-items: center; gap: var(--s-3); }
.row-tight{ display: flex; align-items: center; gap: var(--s-2); }
.row-between { display: flex; align-items: center; justify-content: space-between; gap: var(--s-3); }
.row-wrap { display: flex; flex-wrap: wrap; gap: var(--s-2); }
.col      { display: flex; flex-direction: column; gap: var(--s-3); }
.grow     { flex: 1 1 auto; min-width: 0; }
.spacer   { flex: 1 1 auto; }

.grid { display: grid; gap: var(--s-3); }
.grid-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid-3 { grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); }
.grid-4 { grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); }

.text-center { text-align: center; }
.text-right  { text-align: right; }
.muted   { color: var(--text-secondary); }
.dim     { color: var(--text-tertiary); }
.tiny    { font-size: var(--t-xs); }
.small   { font-size: var(--t-sm); }
.lead    { font-size: var(--t-md); color: var(--text-secondary); }
.bold    { font-weight: 650; }
.nowrap  { white-space: nowrap; }
.truncate { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.clamp-2 { display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical;
           overflow: hidden; }
.clamp-3 { display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical;
           overflow: hidden; }
.hidden  { display: none !important; }
.mono    { font-family: var(--font-mono); }
.tabnum  { font-variant-numeric: tabular-nums; }

/* --- 5. Card ------------------------------------------------------------ */

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  box-shadow: var(--shadow-sm);
}
.card-pad     { padding: var(--s-4); }
.card-pad-lg  { padding: var(--s-5); }
.card-flush   { padding: 0; overflow: hidden; }
.card-quiet   { box-shadow: none; }
.card-sunk    { background: var(--surface-alt); border-color: transparent; box-shadow: none; }

.card-head {
  display: flex; align-items: center; justify-content: space-between; gap: var(--s-3);
  padding: var(--s-4) var(--s-4) var(--s-3);
}
.card-head h2, .card-head h3 { margin: 0; }
.card-body { padding: 0 var(--s-4) var(--s-4); }
.card-foot { padding: var(--s-3) var(--s-4); border-top: 1px solid var(--border);
             background: var(--surface-alt); }

.card-link { display: block; color: inherit; }
.card-link:hover { text-decoration: none; border-color: var(--border-strong);
                   box-shadow: var(--shadow-md); }
.card-link:active { transform: scale(.995); }
.card-link { transition: box-shadow .18s var(--ease), border-color .18s var(--ease),
                         transform .12s var(--ease); }

.section-title {
  display: flex; align-items: center; justify-content: space-between;
  gap: var(--s-3); margin: 0 0 var(--s-3);
}
.section-title h2 { font-size: var(--t-lg); margin: 0; }
/* The class is also used directly on a bare heading with no "see all" link.
   That form must NOT inherit space-between: with an icon plus its label it
   would fling the two to opposite ends of the row. */
h2.section-title, h3.section-title {
  font-size: var(--t-lg);
  justify-content: flex-start;
  gap: var(--s-2);
}
.section-title a { font-size: var(--t-sm); font-weight: 600; }

/* --- 6. Buttons --------------------------------------------------------- */

.btn {
  --btn-bg: var(--surface);
  --btn-fg: var(--text-primary);
  --btn-bd: var(--border);
  display: inline-flex; align-items: center; justify-content: center;
  gap: var(--s-2);
  min-height: 44px;                       /* touch target (spec §145) */
  padding: 0 var(--s-4);
  border: 1px solid var(--btn-bd);
  border-radius: var(--r-sm);
  background: var(--btn-bg);
  color: var(--btn-fg);
  font-size: var(--t-base);
  font-weight: 600;
  line-height: 1;
  text-decoration: none;
  white-space: nowrap;
  transition: background .16s var(--ease), border-color .16s var(--ease),
              transform .1s var(--ease), opacity .16s var(--ease);
  -webkit-tap-highlight-color: transparent;
}
.btn:hover  { text-decoration: none; }
.btn:active { transform: scale(.97); }
.btn[disabled], .btn.is-disabled, .btn[aria-disabled="true"] {
  opacity: .55; pointer-events: none;
}

.btn-primary { --btn-bg: var(--primary); --btn-fg: var(--primary-contrast); --btn-bd: var(--primary); }
.btn-primary:hover { --btn-bg: var(--primary-dark); --btn-bd: var(--primary-dark); }

.btn-secondary { --btn-bg: var(--primary-soft); --btn-fg: var(--primary-ink); --btn-bd: transparent; }
.btn-ghost   { --btn-bg: transparent; --btn-bd: transparent; --btn-fg: var(--text-secondary); }
.btn-ghost:hover { --btn-bg: var(--surface-alt); }
/* A trash icon in a table row: quiet until you reach for it, and red the
   moment you do, so a destructive click is never a surprise. */
.btn-icon-danger:hover { --btn-bg: var(--danger-soft); --btn-fg: var(--danger-ink); }
.btn-icon-danger:focus-visible { --btn-fg: var(--danger-ink); }
.btn-outline { --btn-bg: transparent; --btn-bd: var(--border-strong); }
.btn-outline:hover { --btn-bg: var(--surface-alt); }
.btn-danger  { --btn-bg: var(--danger); --btn-fg: #fff; --btn-bd: var(--danger); }
.btn-danger-soft { --btn-bg: var(--danger-soft); --btn-fg: var(--danger-ink); --btn-bd: transparent; }
.btn-success { --btn-bg: var(--success-solid); --btn-fg: #fff; --btn-bd: var(--success-solid); }
.btn-premium { --btn-bg: var(--premium-gold); --btn-fg: #3b2503; --btn-bd: var(--premium-gold); }

.btn-sm  { min-height: 36px; padding: 0 var(--s-3); font-size: var(--t-sm); border-radius: var(--r-xs); }
.btn-lg  { min-height: 52px; padding: 0 var(--s-6); font-size: var(--t-md); border-radius: var(--r-md); }
.btn-block { display: flex; width: 100%; }
.btn-icon { width: 44px; min-height: 44px; padding: 0; border-radius: var(--r-sm); }
.btn-icon.btn-sm { width: 36px; min-height: 36px; }

/* In-button progress: never a full-screen spinner for a small action (§131). */
.btn.is-loading { position: relative; color: transparent !important; pointer-events: none; }
.btn.is-loading > * { visibility: hidden; }
.btn.is-loading::after {
  content: ""; position: absolute; inset: 0; margin: auto;
  width: 18px; height: 18px; border-radius: 50%;
  border: 2px solid currentColor; border-top-color: transparent;
  color: var(--btn-fg);
  animation: yx-spin .7s linear infinite;
}
@keyframes yx-spin { to { transform: rotate(360deg); } }

/* --- 7. Forms ----------------------------------------------------------- */

.field { margin-bottom: var(--s-4); }
.field:last-child { margin-bottom: 0; }

.label {
  display: block; margin-bottom: var(--s-2);
  font-size: var(--t-sm); font-weight: 600; color: var(--text-primary);
}
.label .opt { font-weight: 400; color: var(--text-tertiary); }
.label .req { color: var(--danger-ink); }

.input, .select, .textarea {
  display: block; width: 100%;
  min-height: 46px;
  padding: var(--s-3);
  border: 1px solid var(--border-strong);
  border-radius: var(--r-sm);
  background: var(--surface);
  color: var(--text-primary);
  font-size: var(--t-md);     /* 16px stops iOS zooming the page on focus */
  line-height: 1.4;
  transition: border-color .16s var(--ease), box-shadow .16s var(--ease);
  -webkit-appearance: none;
  appearance: none;
}
.textarea { min-height: 110px; resize: vertical; line-height: 1.6; }
.input::placeholder, .textarea::placeholder { color: var(--text-tertiary); }

.input:focus, .select:focus, .textarea:focus {
  outline: none;
  border-color: var(--primary-ink);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--primary) 18%, transparent);
}

.select {
  padding-right: var(--s-8);
  background-image: url("data:image/svg+xml;charset=utf-8,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%2364748b' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='m5 9 7 7 7-7'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right var(--s-3) center;
  background-size: 18px;
}
:root[data-theme="dark"] .select {
  background-image: url("data:image/svg+xml;charset=utf-8,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%2397a3bd' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='m5 9 7 7 7-7'/%3E%3C/svg%3E");
}

.input.is-invalid, .select.is-invalid, .textarea.is-invalid { border-color: var(--danger-ink); }
.field-error { display: block; margin-top: var(--s-2); font-size: var(--t-sm); color: var(--danger-ink); }
.field-hint  { display: block; margin-top: var(--s-2); font-size: var(--t-sm); color: var(--text-secondary); }

.input-group { position: relative; display: flex; align-items: stretch; }
.input-group .input { padding-right: 48px; }
.input-affix {
  position: absolute; right: 4px; top: 4px; bottom: 4px; width: 40px;
  display: flex; align-items: center; justify-content: center;
  border: 0; background: transparent; color: var(--text-secondary);
  border-radius: var(--r-xs);
}
.input-affix:hover { background: var(--surface-alt); color: var(--text-primary); }

.check {
  display: flex; align-items: flex-start; gap: var(--s-3);
  cursor: pointer; user-select: none;
  font-size: var(--t-base); line-height: 1.5;
}
.check input[type="checkbox"], .check input[type="radio"] {
  flex: 0 0 auto; width: 20px; height: 20px; margin: 1px 0 0;
  accent-color: var(--primary-ink); cursor: pointer;
}

.switch { display: inline-flex; align-items: center; gap: var(--s-3); cursor: pointer; }
.switch input { position: absolute; opacity: 0; width: 0; height: 0; }
.switch-track {
  position: relative; flex: 0 0 auto; width: 46px; height: 27px;
  background: var(--border-strong); border-radius: var(--r-full);
  transition: background .18s var(--ease);
}
.switch-track::after {
  content: ""; position: absolute; top: 3px; left: 3px;
  width: 21px; height: 21px; border-radius: 50%;
  background: #fff; box-shadow: var(--shadow-xs);
  transition: transform .18s var(--ease);
}
.switch input:checked + .switch-track { background: var(--primary); }
.switch input:checked + .switch-track::after { transform: translateX(19px); }
.switch input:focus-visible + .switch-track { outline: 2px solid var(--primary); outline-offset: 2px; }

/* --- 8. Badges, chips, pills -------------------------------------------- */

.badge {
  display: inline-flex; align-items: center; gap: 5px;
  padding: 3px 9px;
  border-radius: var(--r-full);
  font-size: var(--t-xs); font-weight: 650; line-height: 1.5;
  background: var(--surface-alt); color: var(--text-secondary);
  white-space: nowrap;
}
.badge svg { width: 13px; height: 13px; }
.badge-free    { background: var(--success-soft); color: var(--success-ink); }
.badge-premium { background: var(--premium-soft); color: var(--premium-ink); }
.badge-success { background: var(--success-soft); color: var(--success-ink); }
.badge-warning { background: var(--warning-soft); color: var(--warning-ink); }
.badge-danger  { background: var(--danger-soft);  color: var(--danger-ink); }
.badge-info    { background: var(--info-soft);    color: var(--info-ink); }
.badge-primary { background: var(--primary-soft); color: var(--primary-ink); }
.badge-neutral { background: var(--surface-alt);  color: var(--text-secondary); }
.badge-dot::before {
  content: ""; width: 6px; height: 6px; border-radius: 50%;
  background: currentColor; flex: 0 0 auto;
}

.chip {
  display: inline-flex; align-items: center; gap: var(--s-2);
  min-height: 34px; padding: 0 var(--s-3);
  border: 1px solid var(--border); border-radius: var(--r-full);
  background: var(--surface); color: var(--text-secondary);
  font-size: var(--t-sm); font-weight: 600;
  transition: all .14s var(--ease);
}
.chip:hover { text-decoration: none; border-color: var(--border-strong); }
.chip.is-active { background: var(--primary); border-color: var(--primary-ink); color: var(--primary-contrast); }
.chip-remove { display: inline-flex; margin-right: -4px; padding: 2px; border: 0;
               background: transparent; color: inherit; border-radius: 50%; }
.chip-remove:hover { background: rgba(0,0,0,.1); }

.chip-scroller {
  display: flex; gap: var(--s-2); overflow-x: auto;
  padding-bottom: var(--s-2); margin-inline: calc(var(--page-gutter) * -1);
  padding-inline: var(--page-gutter);
  max-width: 100vw;
  scrollbar-width: none; -webkit-overflow-scrolling: touch;
}
.chip-scroller::-webkit-scrollbar { display: none; }
.chip-scroller > * { flex: 0 0 auto; }

/* --- 9. Alerts ---------------------------------------------------------- */

.alert {
  display: flex; gap: var(--s-3); align-items: flex-start;
  padding: var(--s-3) var(--s-4);
  border-radius: var(--r-md);
  border: 1px solid transparent;
  font-size: var(--t-base);
}
.alert svg { flex: 0 0 auto; margin-top: 2px; }
.alert-title { font-weight: 650; margin: 0 0 2px; }
.alert p:last-child { margin-bottom: 0; }
.alert-success { background: var(--success-soft); color: var(--success-ink); }
.alert-warning { background: var(--warning-soft); color: var(--warning-ink); }
.alert-danger  { background: var(--danger-soft);  color: var(--danger-ink); }
.alert-info    { background: var(--info-soft);    color: var(--info-ink); }
.alert-error   { background: var(--danger-soft);  color: var(--danger-ink); }
.alert a { color: inherit; text-decoration: underline; font-weight: 600; }

/* --- 10. Toast (never alert(); spec §95 / §20) -------------------------- */

.toast-host {
  position: fixed; left: 50%; bottom: calc(var(--s-6) + var(--safe-bottom));
  transform: translateX(-50%);
  z-index: 120;
  display: flex; flex-direction: column; gap: var(--s-2);
  width: min(440px, calc(100vw - var(--s-6)));
  pointer-events: none;
}
@media (min-width: 900px) {
  .toast-host { left: auto; right: var(--s-6); transform: none; bottom: var(--s-6); }
}
.toast {
  display: flex; align-items: flex-start; gap: var(--s-3);
  padding: var(--s-3) var(--s-4);
  border-radius: var(--r-md);
  background: var(--text-primary); color: var(--surface);
  box-shadow: var(--shadow-lg);
  font-size: var(--t-base); font-weight: 500;
  pointer-events: auto;
  animation: yx-toast-in .28s var(--ease-out);
}
.toast.is-leaving { animation: yx-toast-out .2s var(--ease) forwards; }
.toast-success { background: var(--success-solid); color: #fff; }
.toast-error   { background: var(--danger);  color: #fff; }
.toast-warning { background: var(--warning); color: #fff; }
.toast-close   { margin-left: auto; border: 0; background: transparent; color: inherit;
                 opacity: .7; padding: 2px; }
.toast-close:hover { opacity: 1; }
@keyframes yx-toast-in  { from { opacity: 0; transform: translateY(14px) scale(.97); } }
@keyframes yx-toast-out { to   { opacity: 0; transform: translateY(8px); } }

/* --- 11. Modal + bottom sheet ------------------------------------------ */

.scrim {
  position: fixed; inset: 0; z-index: 100;
  background: var(--overlay);
  opacity: 0; pointer-events: none;
  transition: opacity .22s var(--ease);
  backdrop-filter: blur(2px);
}
.scrim.is-open { opacity: 1; pointer-events: auto; }

.modal {
  position: fixed; z-index: 101;
  left: 50%; top: 50%; transform: translate(-50%, -46%) scale(.97);
  width: min(560px, calc(100vw - var(--s-8)));
  max-height: min(85vh, 760px);
  display: flex; flex-direction: column;
  background: var(--surface);
  border-radius: var(--r-xl);
  box-shadow: var(--shadow-lg);
  opacity: 0; pointer-events: none;
  transition: opacity .22s var(--ease), transform .26s var(--ease-out);
}
.modal.is-open { opacity: 1; pointer-events: auto; transform: translate(-50%, -50%) scale(1); }
.modal-lg { width: min(880px, calc(100vw - var(--s-8))); }

.modal-head {
  display: flex; align-items: flex-start; justify-content: space-between; gap: var(--s-3);
  padding: var(--s-5) var(--s-5) var(--s-3);
}
.modal-head h2, .modal-head h3 { margin: 0; font-size: var(--t-lg); }
.modal-body { padding: 0 var(--s-5) var(--s-5); overflow-y: auto; flex: 1 1 auto;
              -webkit-overflow-scrolling: touch; }
.modal-foot {
  display: flex; gap: var(--s-3); justify-content: flex-end;
  padding: var(--s-4) var(--s-5);
  border-top: 1px solid var(--border);
  background: var(--surface-alt);
  border-radius: 0 0 var(--r-xl) var(--r-xl);
}
.modal-foot .btn { flex: 1 1 auto; }
@media (min-width: 640px) { .modal-foot .btn { flex: 0 0 auto; } }

/* Bottom sheet — the mobile-native pattern for premium locks and pickers. */
.sheet {
  position: fixed; z-index: 101;
  left: 0; right: 0; bottom: 0;
  max-height: 88vh;
  display: flex; flex-direction: column;
  background: var(--surface);
  border-radius: var(--r-xl) var(--r-xl) 0 0;
  box-shadow: var(--shadow-lg);
  transform: translateY(100%);
  transition: transform .3s var(--ease-out);
  padding-bottom: var(--safe-bottom);
}
.sheet.is-open { transform: translateY(0); }
.sheet-grip {
  width: 40px; height: 4px; border-radius: var(--r-full);
  background: var(--border-strong); margin: var(--s-3) auto var(--s-2);
  flex: 0 0 auto;
}
.sheet-head { padding: var(--s-2) var(--s-5) var(--s-3); }
.sheet-head h3 { margin: 0; font-size: var(--t-lg); }
.sheet-body { padding: 0 var(--s-5) var(--s-5); overflow-y: auto; flex: 1 1 auto;
              -webkit-overflow-scrolling: touch; }
.sheet-foot { padding: var(--s-4) var(--s-5); border-top: 1px solid var(--border);
              display: flex; flex-direction: column; gap: var(--s-2); }

@media (min-width: 720px) {
  .sheet { left: 50%; right: auto; bottom: var(--s-6); transform: translate(-50%, 120%);
           width: min(520px, calc(100vw - var(--s-8))); border-radius: var(--r-xl); }
  .sheet.is-open { transform: translate(-50%, 0); }
}

/* --- 12. Skeletons and empty states ------------------------------------- */

.skeleton {
  position: relative; overflow: hidden;
  background: var(--surface-alt);
  border-radius: var(--r-sm);
}
.skeleton::after {
  content: ""; position: absolute; inset: 0;
  background: linear-gradient(90deg, transparent,
              color-mix(in srgb, var(--surface) 70%, transparent), transparent);
  animation: yx-shimmer 1.4s infinite;
}
@keyframes yx-shimmer { to { transform: translateX(100%); } }
.skeleton::after { transform: translateX(-100%); }
.sk-line  { height: 12px; margin-bottom: var(--s-2); }
.sk-line.w-40 { width: 40%; } .sk-line.w-60 { width: 60%; }
.sk-line.w-80 { width: 80%; } .sk-line.w-100 { width: 100%; }
.sk-title { height: 18px; width: 55%; margin-bottom: var(--s-3); }
.sk-card  { height: 108px; border-radius: var(--r-lg); }
.sk-chip  { height: 34px; width: 90px; border-radius: var(--r-full); }

.empty {
  display: flex; flex-direction: column; align-items: center; text-align: center;
  padding: var(--s-10) var(--s-5);
  color: var(--text-secondary);
}
.empty-icon {
  display: grid; place-items: center;
  width: 60px; height: 60px; margin-bottom: var(--s-4);
  border-radius: var(--r-lg);
  background: var(--surface-alt); color: var(--text-tertiary);
}
.empty-icon svg { width: 28px; height: 28px; }
.empty h3 { margin: 0 0 var(--s-2); font-size: var(--t-md); color: var(--text-primary); }
.empty p  { margin: 0 0 var(--s-4); max-width: 34ch; font-size: var(--t-base); }

/* --- 13. Progress ------------------------------------------------------- */

.progress {
  height: 8px; border-radius: var(--r-full);
  background: var(--surface-sunk); overflow: hidden;
}
.progress-bar {
  height: 100%; border-radius: var(--r-full);
  background: var(--primary);
  transition: width .5s var(--ease-out);
}
.progress-bar.is-success { background: var(--success); }
.progress-bar.is-warning { background: var(--warning); }
.progress-bar.is-danger  { background: var(--danger); }
.progress-sm { height: 5px; }

/* Circular score dial — the result screen's hero (spec §68). */
.dial { position: relative; display: grid; place-items: center; }
.dial svg { transform: rotate(-90deg); }
.dial-track { stroke: var(--surface-sunk); }
.dial-value { stroke: var(--primary); stroke-linecap: round;
              transition: stroke-dashoffset 1s var(--ease-out); }
.dial-label { position: absolute; text-align: center; line-height: 1.15; }
.dial-label .n { display: block; font-size: var(--t-2xl); font-weight: 700;
                 font-variant-numeric: tabular-nums; letter-spacing: -.02em; }
.dial-label .u { display: block; font-size: var(--t-xs); color: var(--text-secondary); }

/* --- 14. Tables --------------------------------------------------------- */

/* Wide content scrolls inside its own container; the page never scrolls
   sideways (spec §144). */
/* Why one option is right or wrong. Shown beside the option during review and
   in the admin preview — two different shells, so it belongs here rather than
   in either one's stylesheet. Set under the option text and quieter than it, so
   the option still reads as the option. */
.option-reason {
  display: block;
  margin-top: 6px;
  padding-left: var(--s-3);
  border-left: 2px solid var(--border-strong);
  font-size: var(--t-sm);
  font-weight: 400;
  color: var(--text-secondary);
}

.table-wrap { width: 100%; overflow-x: auto; -webkit-overflow-scrolling: touch; }

/* Tables that stop being tables on a phone.
   A five-column table in 375px wraps every cell to one word per line and still
   scrolls sideways — the student ends up reading "Demo / Free / Mock / Test"
   down the screen. Add .table-cards and a data-label to each cell, and below
   640px every row becomes a card with its values labelled in place.
   The rule is opt-in: wide admin tables are better left scrolling. */
@media (max-width: 640px) {
  .table-cards-wrap { overflow-x: visible; }

  .table-cards,
  .table-cards tbody,
  .table-cards tr,
  .table-cards td { display: block; width: auto; }

  .table-cards thead { display: none; }

  .table-cards tr {
    padding: var(--s-3);
    margin-bottom: var(--s-3);
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--r-md);
  }
  .table-cards tr:last-child { margin-bottom: 0; }

  .table-cards td {
    padding: 3px 0;
    border-bottom: 0;
    text-align: left;
  }

  /* A labelled cell reads as "label ......... value" on one line. The label
     takes the slack so that a value made of several parts — "0.17" and
     "/ 10.00" — stays together on the right instead of being pushed apart. */
  .table-cards td[data-label] {
    display: flex;
    align-items: baseline;
    gap: 6px;
  }
  .table-cards td[data-label]::before {
    content: attr(data-label);
    flex: none;
    margin-right: auto;
    padding-right: var(--s-3);
    font-size: var(--t-xs);
    font-weight: 650;
    text-transform: uppercase;
    letter-spacing: .04em;
    color: var(--text-secondary);
  }

  /* The first cell is the card's heading, so it keeps the full width and the
     weight to match. */
  .table-cards td:first-child {
    padding-bottom: var(--s-2);
    font-weight: 650;
    color: var(--text-primary);
  }

  /* A row action belongs at the bottom, reachable with a thumb — and it needs
     an edge, because a ghost button alone on a card reads as a caption. */
  .table-cards td.row-action { padding-top: var(--s-3); }
  .table-cards td.row-action .btn {
    width: 100%;
    justify-content: center;
    --btn-bd: var(--border-strong);
    --btn-fg: var(--text-primary);
  }
}

.table { width: 100%; border-collapse: collapse; font-size: var(--t-sm); }
.table th, .table td {
  padding: var(--s-3);
  text-align: left;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}
.table th {
  font-weight: 650; font-size: var(--t-xs); text-transform: uppercase;
  letter-spacing: .04em; color: var(--text-secondary);
  background: var(--surface-alt);
  white-space: nowrap;
}
.table tbody tr:last-child td { border-bottom: 0; }
.table tbody tr:hover td { background: var(--surface-alt); }
.table .num { text-align: right; font-variant-numeric: tabular-nums; }
.table-compact th, .table-compact td { padding: var(--s-2) var(--s-3); }

/* Question and explanation bodies may contain a table; it must scroll, not
   break the layout (spec §91). */
.rich table { width: 100%; border-collapse: collapse; margin: var(--s-3) 0;
              font-size: var(--t-sm); }
.rich th, .rich td { border: 1px solid var(--border); padding: var(--s-2) var(--s-3); }
.rich th { background: var(--surface-alt); font-weight: 650; }
.rich pre { background: var(--surface-sunk); padding: var(--s-3); border-radius: var(--r-sm);
            overflow-x: auto; margin: var(--s-3) 0; }
.rich code { background: var(--surface-sunk); padding: 1px 5px; border-radius: 4px; }
.rich pre code { background: transparent; padding: 0; }
.rich img { border-radius: var(--r-sm); margin: var(--s-2) 0; cursor: zoom-in; }
.rich p:last-child { margin-bottom: 0; }
.rich ul, .rich ol { margin: var(--s-2) 0 var(--s-3); }

/* --- 15. Tabs and segmented control ------------------------------------ */

.tabs {
  display: flex; gap: var(--s-1);
  border-bottom: 1px solid var(--border);
  overflow-x: auto; scrollbar-width: none;
}
.tabs::-webkit-scrollbar { display: none; }
.tab {
  flex: 0 0 auto;
  padding: var(--s-3) var(--s-4);
  border: 0; background: transparent;
  color: var(--text-secondary);
  font-size: var(--t-base); font-weight: 600;
  border-bottom: 2px solid transparent;
  margin-bottom: -1px;
  transition: color .15s var(--ease), border-color .15s var(--ease);
}
.tab:hover { color: var(--text-primary); text-decoration: none; }
.tab.is-active { color: var(--primary-ink); border-bottom-color: var(--primary-ink); }

.segmented {
  display: inline-flex; padding: 3px; gap: 2px;
  background: var(--surface-sunk); border-radius: var(--r-sm);
}
.segmented button, .segmented a {
  flex: 1 1 0; min-height: 36px; padding: 0 var(--s-3);
  border: 0; background: transparent; border-radius: calc(var(--r-sm) - 2px);
  font-size: var(--t-sm); font-weight: 600; color: var(--text-secondary);
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  transition: background .16s var(--ease), color .16s var(--ease);
}
.segmented .is-active { background: var(--surface); color: var(--text-primary);
                        box-shadow: var(--shadow-xs); }

/* --- 16. Avatar --------------------------------------------------------- */

.avatar {
  display: grid; place-items: center; flex: 0 0 auto;
  width: 40px; height: 40px; border-radius: 50%;
  background: var(--primary-soft); color: var(--primary-ink);
  font-weight: 700; font-size: var(--t-sm);
  overflow: hidden; user-select: none;
}
.avatar img { width: 100%; height: 100%; object-fit: cover; }
.avatar-sm { width: 30px; height: 30px; font-size: var(--t-xs); }
.avatar-lg { width: 64px; height: 64px; font-size: var(--t-lg); }
.avatar-xl { width: 88px; height: 88px; font-size: var(--t-xl); }

/* --- 17. Math (KaTeX) --------------------------------------------------- */

.katex { font-size: 1.05em; }
.katex-display {
  margin: var(--s-3) 0;
  overflow-x: auto; overflow-y: hidden;
  padding-bottom: 2px;
}
/* Unparseable LaTeX must be obvious to the author, not silently blank. */
.math-error {
  display: inline-block; padding: 1px 6px; border-radius: 4px;
  background: var(--danger-soft); color: var(--danger-ink);
  font-family: var(--font-mono); font-size: .85em;
}

/* --- 18. Pagination ----------------------------------------------------- */

.pagination { display: flex; align-items: center; gap: var(--s-2); flex-wrap: wrap; }
.pagination a, .pagination span {
  min-width: 38px; height: 38px; padding: 0 var(--s-2);
  display: inline-flex; align-items: center; justify-content: center;
  border: 1px solid var(--border); border-radius: var(--r-xs);
  background: var(--surface); color: var(--text-secondary);
  font-size: var(--t-sm); font-weight: 600;
}
.pagination a:hover { border-color: var(--border-strong); text-decoration: none;
                      color: var(--text-primary); }
.pagination .is-current { background: var(--primary); border-color: var(--primary-ink);
                          color: var(--primary-contrast); }
.pagination .is-disabled { opacity: .4; pointer-events: none; }
.pagination .gap { border: 0; background: transparent; }

/* --- 19. Misc ----------------------------------------------------------- */

.divider { height: 1px; background: var(--border); margin: var(--s-4) 0; }
.divider-text {
  display: flex; align-items: center; gap: var(--s-3);
  margin: var(--s-5) 0; color: var(--text-tertiary); font-size: var(--t-sm);
}
.divider-text::before, .divider-text::after {
  content: ""; flex: 1 1 auto; height: 1px; background: var(--border);
}

.kbd {
  display: inline-block; padding: 1px 6px; border-radius: 5px;
  border: 1px solid var(--border-strong); border-bottom-width: 2px;
  background: var(--surface-alt); font-family: var(--font-mono);
  font-size: var(--t-xs); color: var(--text-secondary);
}

.stat {
  display: flex; flex-direction: column; gap: 2px;
  padding: var(--s-4);
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--r-md);
}
.stat-label { font-size: var(--t-xs); font-weight: 600; color: var(--text-secondary);
              text-transform: uppercase; letter-spacing: .04em; }
.stat-value { font-size: var(--t-xl); font-weight: 700; letter-spacing: -.02em;
              font-variant-numeric: tabular-nums; }
.stat-meta  { font-size: var(--t-xs); color: var(--text-tertiary); }

/* Fullscreen image viewer for question diagrams (spec §92). */
.lightbox {
  position: fixed; inset: 0; z-index: 130;
  display: none; place-items: center;
  background: rgba(2, 6, 23, .93);
  padding: var(--s-4);
}
.lightbox.is-open { display: grid; }
.lightbox img { max-width: 100%; max-height: 92vh; object-fit: contain;
                border-radius: var(--r-sm); }
.lightbox-close {
  position: absolute; top: calc(var(--s-4) + var(--safe-top)); right: var(--s-4);
  width: 44px; height: 44px; display: grid; place-items: center;
  border: 0; border-radius: 50%;
  background: rgba(255,255,255,.14); color: #fff;
}

/* --- 20. Motion preferences --------------------------------------------- */

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: .01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: .01ms !important;
    scroll-behavior: auto !important;
  }
}

/* --- 21. Print ---------------------------------------------------------- */

@media print {
  .no-print, .bottom-nav, .app-header, .admin-sidebar, .toast-host { display: none !important; }
  body { background: #fff; }
  .card { box-shadow: none; border-color: #ddd; }
}


/* --- 21b. Dropdown menu -------------------------------------------------
   Used by the admin topbar and by the student account button, so it belongs
   to the shared foundation rather than to either shell. */

.menu {
  position: absolute; right: 0; top: calc(100% + 6px); z-index: 50;
  min-width: 210px; padding: var(--s-2);
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--r-md); box-shadow: var(--shadow-lg);
  display: none;
}
.menu.is-open { display: block; }
.menu a, .menu button {
  display: flex; align-items: center; gap: var(--s-3); width: 100%;
  min-height: 38px; padding: 0 var(--s-3);
  border: 0; background: transparent; border-radius: var(--r-xs);
  color: var(--text-primary); font-size: var(--t-sm); font-weight: 550; text-align: left;
}
.menu a:hover, .menu button:hover { background: var(--surface-alt); text-decoration: none; }
.menu .sep { height: 1px; background: var(--border); margin: var(--s-2) 0; }
.menu .danger { color: var(--danger-ink); }

.has-menu { position: relative; }

/* --- 22. Touch-device minimums (spec §145) -------------------------------
   Compact controls are right for a mouse and wrong for a thumb. Rather than
   inflating every button everywhere, the minimums are raised only where the
   primary input is a finger, so the admin panel stays dense on a desktop and
   the student app stays comfortable on a phone. */

@media (pointer: coarse) {
  .btn-sm            { min-height: 40px; }
  .btn-icon.btn-sm   { width: 40px; min-height: 40px; }
  .chip              { min-height: 40px; }
  .tab               { padding-block: var(--s-3); }
  .menu a,
  .menu button       { min-height: 44px; }
  .input, .select    { min-height: 48px; }
  .check input[type="checkbox"],
  .check input[type="radio"] { width: 22px; height: 22px; }

  /* Give small icon triggers a finger-sized hit area without changing how
     large they look. */
  /* "See all" links sit beside a heading, so they cannot simply grow taller
     without pushing the heading around — pad them instead. */
  .section-title a,
  .card-head a {
    display: inline-flex;
    align-items: center;
    min-height: 44px;
    padding-inline: var(--s-2);
    margin-inline: calc(var(--s-2) * -1);
  }

  .app-avatar,
  .icon-tap {
    position: relative;
  }
  .app-avatar::after,
  .icon-tap::after {
    content: "";
    position: absolute;
    top: 50%; left: 50%;
    width: 44px; height: 44px;
    transform: translate(-50%, -50%);
  }
}
