/**
 * STUDY SURFACE — flat, high-contrast reading background for subject pages
 * Loaded by every subjects/<subject>/{teoria,problemas}/*.html page right after
 * celestial-sky.css (and before responsive.css). It switches off the nebula
 * gradients, glass blur and translucent cards that celestial-sky.css gives the
 * portal, so derivations and KaTeX blocks sit on a still, matte background.
 * The animated canvas itself is portal-only (index.html); subject pages do not
 * load assets/js/celestial-sky.js.
 */

:root {
  --study-bg-dark: #0a0e17;
  --study-surface-dark: #131b2b;
  --study-inset-dark: #151e32;
  --study-border-dark: rgba(255, 255, 255, 0.08);
  --study-bg-light: #f6f8fa;
  --study-surface-light: #ffffff;
  --study-inset-light: #f8fafc;
  --study-border-light: #e2e8f0;
}

/* 1. Flat page background: no nebula gradients, no dot grid (on html too:
   some pages draw a starry dot pattern on [data-theme], i.e. the root) */
html[data-theme="dark"],
html[data-theme="dark"] body {
  background: var(--study-bg-dark) !important;
}

html[data-theme="light"],
html[data-theme="light"] body {
  background: var(--study-bg-light) !important;
}

/* Leftover decorative layers stay hidden */
.celestial-canvas,
#celestial-canvas,
.bg-glow {
  display: none !important;
}

/* 2. Opaque content surfaces (no blur, no see-through) */
html[data-theme="dark"] :is(.problem-card, .concept-card, .exam-card, .topic-card, .card,
  .info-card, .example-card, .diagram-card, .toc-card, .set-card, .subchapter-card,
  .formula-box, .formula-card, .solution-card, .block-container, .definition-box) {
  background: var(--study-surface-dark) !important;
  border: 1px solid var(--study-border-dark) !important;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.35) !important;
  backdrop-filter: none !important;
  -webkit-backdrop-filter: none !important;
}

html[data-theme="light"] :is(.problem-card, .concept-card, .exam-card, .topic-card, .card,
  .info-card, .example-card, .diagram-card, .toc-card, .set-card, .subchapter-card,
  .formula-box, .formula-card, .solution-card, .block-container, .definition-box) {
  background: var(--study-surface-light) !important;
  border: 1px solid var(--study-border-light) !important;
  box-shadow: 0 1px 2px rgba(15, 23, 42, 0.05) !important;
  backdrop-filter: none !important;
  -webkit-backdrop-filter: none !important;
}

/* Statement boxes and tables: opaque, one step apart from the card around them */
html[data-theme="dark"] :is(.statement-box, .problem-statement-box, .problem-statement,
  .table-container, .table-wrapper) {
  background: var(--study-inset-dark) !important;
  border-color: var(--study-border-dark) !important;
  backdrop-filter: none !important;
  -webkit-backdrop-filter: none !important;
}

html[data-theme="light"] :is(.statement-box, .problem-statement-box, .problem-statement,
  .table-container, .table-wrapper) {
  background: var(--study-inset-light) !important;
  border-color: var(--study-border-light) !important;
  backdrop-filter: none !important;
  -webkit-backdrop-filter: none !important;
}

/* Long reading cards stay still under the cursor; navigation cards keep their hover */
:is(.problem-card, .concept-card, .exam-card, .diagram-card, .example-card, .info-card):hover {
  transform: none !important;
}

html[data-theme="dark"] :is(.problem-card, .concept-card, .exam-card):hover {
  border-color: var(--study-border-dark) !important;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.35) !important;
}

html[data-theme="light"] :is(.problem-card, .concept-card, .exam-card):hover {
  border-color: var(--study-border-light) !important;
  box-shadow: 0 1px 2px rgba(15, 23, 42, 0.05) !important;
}

/* 3. Opaque header and navigation (they scroll over the text) */
html[data-theme="dark"] :is(.site-header, .page-header, .top-nav, .sticky-nav) {
  background: var(--study-bg-dark) !important;
  backdrop-filter: none !important;
  -webkit-backdrop-filter: none !important;
}

html[data-theme="light"] :is(.site-header, .page-header, .top-nav, .sticky-nav) {
  background: var(--study-bg-light) !important;
  backdrop-filter: none !important;
  -webkit-backdrop-filter: none !important;
}

:is(.nav-pill, .ex-pill, .filter-btn, .back-link, .btn-secondary) {
  backdrop-filter: none !important;
  -webkit-backdrop-filter: none !important;
}

/* 4. Display maths: opaque block, no glow */
html[data-theme="dark"] .katex-display {
  background: var(--study-inset-dark) !important;
  border: 1px solid var(--study-border-dark) !important;
  box-shadow: none !important;
}

html[data-theme="light"] .katex-display {
  background: var(--study-inset-light) !important;
  border: 1px solid var(--study-border-light) !important;
  box-shadow: none !important;
}

/* Headings on a flat background need no glow */
html[data-theme="dark"] :is(h1, h2, h3, .topic-title, .set-title) {
  text-shadow: none;
}

/* ==========================================================================
   5. READING PALETTES (data-surface on <html>, chosen with the picker that
   assets/js/theme.js injects next to #themeToggle; stored under 'ae_surface').

   A palette re-points the --study-* surface variables used by sections 1-4
   above, so cards, statement boxes, tables, headers and KaTeX display blocks
   follow it without repeating those selector lists. It also re-points the
   text/background/border variables the pages define themselves.

   Each palette is bound to one polarity (light or dark): the picker switches
   data-theme to match, and a theme toggle that would break the pairing resets
   the surface to "default". Screen only: the PDF layout sets data-theme="print",
   which none of these selectors match.

   Pure white (#fff) and pure black (#000) are avoided on purpose (glare and
   halation). Contrast ratios (WCAG 2.1) of text on bg / card / inset; every
   figure is AAA (7:1) or better:
     cream        main 13.4 / 15.0 / 12.2    secondary 7.8 / 8.7 / 7.1
     sepia        main 11.5 / 12.5 / 10.2    secondary 8.1 / 8.8 / 7.2
     slate-paper  main 13.9 / 15.1 / 12.8    secondary 7.8 / 8.4 / 7.1
     sage         main 13.0 / 13.9 / 12.0    secondary 7.7 / 8.2 / 7.1
     charcoal     main 14.1 / 12.7 / 13.6    secondary 8.0 / 7.2 / 7.6
   The secondary colours were nudged from the requested hex values (see the
   comments next to each) because those only reached AA; the hue is kept.
   ========================================================================== */
@media screen {
  html[data-surface="cream"][data-theme="light"] {
    color-scheme: light; /* native scrollbars and controls follow the palette */
    --study-l-min: 0;
    --study-l-max: 0.31;
    --study-bg-light: #f5f2eb;
    --study-surface-light: #ffffff;
    --study-inset-light: #ece8df;
    --study-border-light: rgba(43, 45, 49, 0.12);
    --study-text: #23272f;
    --study-text-2: #474b56; /* requested #575b66 gives 5.5:1 on inset; this reaches AAA */
  }

  html[data-surface="sepia"][data-theme="light"] {
    color-scheme: light; /* native scrollbars and controls follow the palette */
    --study-l-min: 0;
    --study-l-max: 0.31;
    --study-bg-light: #efe8d8;
    --study-surface-light: #f7f2e4;
    --study-inset-light: #e5dcb7;
    --study-border-light: rgba(60, 48, 38, 0.14);
    --study-text: #342a22;
    --study-text-2: #4d4137; /* requested #63564a gives 5.2:1 on inset */
  }

  html[data-surface="slate-paper"][data-theme="light"] {
    color-scheme: light; /* native scrollbars and controls follow the palette */
    --study-l-min: 0;
    --study-l-max: 0.31;
    --study-bg-light: #eceff2;
    --study-surface-light: #f6f8fa;
    --study-inset-light: #e2e6eb;
    --study-border-light: rgba(26, 34, 45, 0.12);
    --study-text: #1a222d;
    --study-text-2: #414a58; /* requested #505a69 gives 5.6:1 on inset */
  }

  html[data-surface="sage"][data-theme="light"] {
    color-scheme: light; /* native scrollbars and controls follow the palette */
    --study-l-min: 0;
    --study-l-max: 0.31;
    --study-bg-light: #ebf0ea;
    --study-surface-light: #f4f7f3;
    --study-inset-light: #e1e8e0;
    --study-border-light: rgba(31, 41, 34, 0.12);
    --study-text: #1f2922;
    --study-text-2: #404d43; /* requested #516155 gives 5.3:1 on inset */
  }

  html[data-surface="charcoal"][data-theme="dark"] {
    color-scheme: dark; /* native scrollbars and controls follow the palette */
    --study-l-min: 0.91;
    --study-l-max: 1;
    --study-bg-dark: #111620;
    --study-surface-dark: #18202f;
    --study-inset-dark: #141a26;
    --study-border-dark: rgba(255, 255, 255, 0.08);
    --study-text: #dde4ec;
    --study-text-2: #9fadc0; /* requested #94a3b8 gives 6.4:1 on card */
  }

  /* Page-level variables (every page names them slightly differently) */
  html:is([data-surface="cream"], [data-surface="sepia"], [data-surface="slate-paper"], [data-surface="sage"])[data-theme="light"],
  html[data-surface="charcoal"][data-theme="dark"] {
    --text-primary: var(--study-text);
    --text-main: var(--study-text);
    --text-body: var(--study-text);
    --text-heading: var(--study-text);
    --text-secondary: var(--study-text-2);
    --text-muted: var(--study-text-2);
    --text-faint: var(--study-text-2);
    --bg-primary: var(--study-bg);
    --bg-base: var(--study-bg);
    --bg-secondary: var(--study-bg);
    --bg-card: var(--study-surface);
    --bg-card-hover: var(--study-surface);
    --bg-surface: var(--study-surface);
    --bg-subtle: var(--study-inset);
    --bg-input: var(--study-inset);
    --bg-glass: var(--study-inset);
    --callout-bg: var(--study-inset);
    --statement-bg: var(--study-inset);
    --border-subtle: var(--study-border);
    --border-card: var(--study-border);
    --border-strong: color-mix(in srgb, var(--study-text-2) 35%, transparent);
    color: var(--study-text);
  }

  /* Alias the active polarity's palette to polarity-free names */
  html[data-theme="light"] {
    --study-bg: var(--study-bg-light);
    --study-surface: var(--study-surface-light);
    --study-inset: var(--study-inset-light);
    --study-border: var(--study-border-light);
  }

  html[data-theme="dark"] {
    --study-bg: var(--study-bg-dark);
    --study-surface: var(--study-surface-dark);
    --study-inset: var(--study-inset-dark);
    --study-border: var(--study-border-dark);
  }

  html:is([data-surface="cream"], [data-surface="sepia"], [data-surface="slate-paper"], [data-surface="sage"])[data-theme="light"] body,
  html[data-surface="charcoal"][data-theme="dark"] body {
    color: var(--study-text);
  }

  /* KaTeX inherits the palette's text colour (no pure black or pure white glyphs) */
  html:is([data-surface="cream"], [data-surface="sepia"], [data-surface="slate-paper"], [data-surface="sage"])[data-theme="light"] .katex,
  html[data-surface="charcoal"][data-theme="dark"] .katex {
    color: var(--study-text);
  }
}

/* ==========================================================================
   6. SURFACE PICKER (built by assets/js/theme.js; pages carry no markup for it)
   ========================================================================== */
.surface-picker {
  position: relative;
  display: inline-flex;
  align-items: center;
  flex: 0 0 auto;
}

.surface-picker__btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 22px;
  padding: 0;
  border-radius: 11px;
  border: 1px solid var(--border-subtle, rgba(128, 128, 128, 0.3));
  background: var(--bg-input, transparent);
  cursor: pointer;
  transition: border-color 0.2s ease, background-color 0.2s ease;
}

.surface-picker__btn:hover,
.surface-picker.is-open .surface-picker__btn {
  border-color: var(--text-secondary, currentColor);
}

.surface-picker__btn:focus-visible,
.surface-swatch:focus-visible {
  outline: 2px solid var(--accent, #6366f1);
  outline-offset: 2px;
}

.surface-panel {
  position: fixed;
  top: 0;
  left: 0;
  z-index: 1000;
  box-sizing: border-box;
  width: 264px;
  max-width: calc(100vw - 24px);
  padding: 14px 16px 10px;
  border-radius: 14px;
  border: 1px solid var(--study-border-dark);
  background: var(--study-surface-dark);
  color: var(--text-primary, inherit);
  font-family: var(--font-sans, system-ui, sans-serif);
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.28);
  visibility: hidden;
  opacity: 0;
  transform: translateY(-4px);
  transition: opacity 0.16s ease, transform 0.16s ease, visibility 0s linear 0.16s;
}

html[data-theme="light"] .surface-panel {
  border-color: var(--study-border-light);
  background: var(--study-surface-light);
  box-shadow: 0 12px 32px rgba(15, 23, 42, 0.14);
}

.surface-panel.is-open {
  visibility: visible;
  opacity: 1;
  transform: none;
  transition-delay: 0s;
}

.surface-panel__swatches {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: 8px;
}

.surface-swatch {
  --sw-bg: #f6f8fa;
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 30px;
  height: 30px;
  padding: 0;
  border-radius: 50%;
  border: 2px solid transparent;
  background: var(--sw-bg);
  box-shadow: inset 0 0 0 1px rgba(128, 128, 128, 0.4);
  cursor: pointer;
  transition: transform 0.16s ease, border-color 0.16s ease;
}

.surface-swatch:hover,
.surface-swatch:focus-visible {
  transform: scale(1.12);
}

.surface-swatch[aria-checked="true"] {
  border-color: var(--accent, #6366f1);
}

/* Name tooltip (also mirrored in the caption below, for touch screens) */
.surface-swatch::after {
  content: attr(data-label);
  position: absolute;
  bottom: calc(100% + 8px);
  left: 50%;
  padding: 3px 8px;
  border-radius: 6px;
  background: #0f172a;
  color: #f8fafc;
  font: 500 0.7rem/1.3 var(--font-sans, system-ui, sans-serif);
  white-space: nowrap;
  pointer-events: none;
  opacity: 0;
  transform: translate(-50%, 3px);
  transition: opacity 0.14s ease, transform 0.14s ease;
}

.surface-swatch:hover::after,
.surface-swatch:focus-visible::after {
  opacity: 1;
  transform: translate(-50%, 0);
}

.surface-panel__caption {
  margin: 10px 0 0;
  min-height: 2.6em;
  font-size: 0.74rem;
  line-height: 1.35;
  color: var(--text-secondary, inherit);
}

.surface-panel__caption strong {
  color: var(--text-primary, inherit);
  font-weight: 600;
}

/* Swatch colours: a plain disc of each palette's background */
.surface-swatch[data-surface="default"] {
  --sw-bg: linear-gradient(135deg, #f6f8fa 50%, #0a0e17 50%);
}
.surface-swatch[data-surface="cream"] { --sw-bg: #f5f2eb; }
.surface-swatch[data-surface="sepia"] { --sw-bg: #efe8d8; }
.surface-swatch[data-surface="slate-paper"] { --sw-bg: #eceff2; }
.surface-swatch[data-surface="sage"] { --sw-bg: #ebf0ea; }
.surface-swatch[data-surface="charcoal"] { --sw-bg: #111620; }

.surface-picker__btn .surface-swatch {
  width: 14px;
  height: 14px;
  border-width: 0;
  pointer-events: none;
}
.surface-picker__btn .surface-swatch::after {
  content: none;
}

/* Phone: the panel becomes a bottom sheet so it can never leave the viewport */
@media (max-width: 600px) {
  .surface-panel {
    position: fixed;
    top: auto;
    right: 12px;
    bottom: 12px;
    left: 12px;
    width: auto;
    max-width: none;
    transform: translateY(8px);
  }
  .surface-swatch {
    width: 36px;
    height: 36px;
  }
  .surface-panel__swatches {
    justify-items: center;
  }
}

/* Very narrow phones: some headers cannot wrap, so the trigger shrinks to a plain dot */
@media (max-width: 360px) {
  .surface-picker__btn {
    width: 22px;
  }
}

@media (prefers-reduced-motion: reduce) {
  .surface-panel,
  .surface-swatch,
  .surface-swatch::after,
  .surface-picker__btn {
    transition: none;
  }
}

html[data-theme="print"] .surface-picker,
html[data-theme="print"] .surface-panel {
  display: none !important;
}

/* ==========================================================================
   7. ACCENT TEXT UNDER A READING PALETTE
   Subject accents (cyan, amber, indigo, red, green, sky...) were tuned for the
   portal's own surfaces; as text on a palette they fall below 4.5:1 and often
   below 2:1. The rules below clamp only the *lightness* (and chroma) of those
   text colours in OKLCH, relative to the element's own colour, so the hue
   coding survives, the original colour is the starting point (hover states
   included) and text that already complies is left untouched. Light palettes
   cap lightness at 0.31, charcoal raises it to at least 0.91; with chroma at
   most 0.13 that keeps every hue at 7:1 or better against all palette
   surfaces, tinted hover backgrounds included. Elements filled with the accent
   (listed in :not()) keep their own text colour, which already contrasts with
   that fill; so does the updates badge (.upd-badge), filled with the palette's
   text colour and written in the palette's card colour. Only text paint changes: no variable is touched, so filled badges
   and buttons that use the accent as a background are unaffected.
   Browsers without relative colour syntax ignore the declaration.
   Add a selector here when a new page introduces accent-coloured text;
   tools/tests/test_surface.py audits the contrast of real pages.
   ========================================================================== */
@media screen {
  html[data-surface]:not([data-surface="default"]):not([data-theme="print"]) :is(
  .accent,
  .ae-quote-badge-field,
  .ae-quote-badge-icon,
  .ae-quote-badge-tag,
  .ae-quote-mark.ae-quote-mark-close,
  .ae-quote-mark.ae-quote-mark-open,
  .article-category,
  .article-container a,
  .ask-ai-bar-label > span,
  .back,
  .back-link,
  .back-link > span,
  .badge-status.badge-available,
  .block-badge,
  .bottom-nav-btn.center > span,
  .brand-badge,
  .callout a,
  .callout-title,
  .callout-title > span,
  .callout-title code,
  .card > h3,
  .card-arrow,
  .card-badge,
  .card-footer > span,
  .card-pill,
  .cbadge.interactive > span,
  .cbadge.problems,
  .cbadge.problems > span,
  .cbadge.theory,
  .cbadge.theory > span,
  .cite,
  .container span,
  .data-table strong,
  .diagram-open,
  .enunciado-box > h4,
  .eq-num,
  .ex-num,
  .exam-badge,
  .example-badge,
  .example-card > h5,
  .fig b,
  .fig.pair b,
  .hero-badge,
  .hero-pill > span,
  .jump a,
  .jump-btn,
  .method-box a,
  .minor-heading,
  .nav-badge,
  .nav-btn-title,
  .nav-pill,
  .note-box > strong,
  .note-box-title,
  .p-pill,
  .page-header span,
  .pdf-link > span,
  .phase-badge,
  .phase-badge-title.phase-1-badge,
  .phase-badge-title.phase-2-badge,
  .phase-badge-title.phase-3-badge,
  .phase-badge-title.phase-4-badge,
  .phase-block > h4,
  .phase-num,
  .pill-link,
  .problem-header > span,
  .problem-id,
  .problem-num,
  .problem-num-badge,
  .problem-tag,
  .question-label,
  .reference-title,
  .result-box > strong,
  .result-box-title,
  .result-callout-title,
  .section-num,
  .section-tag,
  .set-badge.available,
  .set-card.available,
  .set-code,
  .site-footer a,
  .solution-box,
  .solution-summary > span,
  .source-banner > strong,
  .source-box,
  .source-box > code,
  .source-box > span,
  .src-cite,
  .src-link,
  .src-panel > a,
  .status-solved,
  .step-heading,
  .step-subheading,
  .sub-case-header,
  .subchapter-num,
  .summary-hint,
  .switch-btn.active > span,
  .tab-link.active,
  .tabs a,
  .tabs span,
  .toc-num,
  .topic-arrow,
  .toc-title,
  .topic-badge,
  .topic-badge.available,
  .topic-badge.badge-soon,
  .topic-card.available,
  .topic-code,
  .topic-nav > a,
  .topic-num,
  .topic-number,
  .topic-pill,
  .topic-tag,
  .walkthrough-badge,
  .warning-box-title,
  .year-label,
  li a
  ):not(
    .step-badge, .alg-step, .switch-btn.active, .switch-btn.active *,
    .pill-link.active, .p-pill.active, .nav-pill.active, .nav-pill:hover,
    .upd-badge,
    .ae-quote-copied-tooltip,
    [style*="background: var(--accent)"], [style*="background: var(--accent)"] *
  ) {
    -webkit-text-fill-color: oklch(from currentcolor clamp(var(--study-l-min), l, var(--study-l-max)) min(c, 0.13) h);
  }
}

/* ==========================================================================
   8. ACCENT-COLOURED TEXT INSIDE SVG FIGURES
   SVG paints text with `fill`, which section 7 cannot reach, so the colours the
   figures use for labels (literal hex values and the --fig-* fallbacks written by
   tools/figures/svgkit.py) get the same lightness window. Only <text>/<tspan>
   change; strokes, atoms and boxes keep their colours.
   ========================================================================== */
@media screen {
  html[data-surface]:not([data-surface="default"]):not([data-theme="print"]) svg :is(text, tspan)[fill="#f59e0b"],
  html[data-surface]:not([data-surface="default"]):not([data-theme="print"]) svg g[fill="#f59e0b"] :is(text, tspan) {
    fill: oklch(from #f59e0b clamp(var(--study-l-min), l, var(--study-l-max)) min(c, 0.13) h);
  }

  html[data-surface]:not([data-surface="default"]):not([data-theme="print"]) svg :is(text, tspan)[fill="#38bdf8"],
  html[data-surface]:not([data-surface="default"]):not([data-theme="print"]) svg g[fill="#38bdf8"] :is(text, tspan) {
    fill: oklch(from #38bdf8 clamp(var(--study-l-min), l, var(--study-l-max)) min(c, 0.13) h);
  }

  html[data-surface]:not([data-surface="default"]):not([data-theme="print"]) svg :is(text, tspan)[fill="#94a3b8"],
  html[data-surface]:not([data-surface="default"]):not([data-theme="print"]) svg g[fill="#94a3b8"] :is(text, tspan) {
    fill: oklch(from #94a3b8 clamp(var(--study-l-min), l, var(--study-l-max)) min(c, 0.13) h);
  }

  html[data-surface]:not([data-surface="default"]):not([data-theme="print"]) svg :is(text, tspan)[fill="#64748b"],
  html[data-surface]:not([data-surface="default"]):not([data-theme="print"]) svg g[fill="#64748b"] :is(text, tspan) {
    fill: oklch(from #64748b clamp(var(--study-l-min), l, var(--study-l-max)) min(c, 0.13) h);
  }

  html[data-surface]:not([data-surface="default"]):not([data-theme="print"]) svg :is(text, tspan)[fill="#ef4444"],
  html[data-surface]:not([data-surface="default"]):not([data-theme="print"]) svg g[fill="#ef4444"] :is(text, tspan) {
    fill: oklch(from #ef4444 clamp(var(--study-l-min), l, var(--study-l-max)) min(c, 0.13) h);
  }

  html[data-surface]:not([data-surface="default"]):not([data-theme="print"]) svg :is(text, tspan)[fill="#10b981"],
  html[data-surface]:not([data-surface="default"]):not([data-theme="print"]) svg g[fill="#10b981"] :is(text, tspan) {
    fill: oklch(from #10b981 clamp(var(--study-l-min), l, var(--study-l-max)) min(c, 0.13) h);
  }

  html[data-surface]:not([data-surface="default"]):not([data-theme="print"]) svg :is(text, tspan)[fill="var(--accent)"],
  html[data-surface]:not([data-surface="default"]):not([data-theme="print"]) svg g[fill="var(--accent)"] :is(text, tspan) {
    fill: oklch(from var(--accent) clamp(var(--study-l-min), l, var(--study-l-max)) min(c, 0.13) h);
  }

  html[data-surface]:not([data-surface="default"]):not([data-theme="print"]) svg :is(text, tspan)[fill*="--fig-acc"],
  html[data-surface]:not([data-surface="default"]):not([data-theme="print"]) svg g[fill*="--fig-acc"] :is(text, tspan) {
    fill: oklch(from #f59e0b clamp(var(--study-l-min), l, var(--study-l-max)) min(c, 0.13) h);
  }

  html[data-surface]:not([data-surface="default"]):not([data-theme="print"]) svg :is(text, tspan)[fill*="--fig-atom"],
  html[data-surface]:not([data-surface="default"]):not([data-theme="print"]) svg g[fill*="--fig-atom"] :is(text, tspan) {
    fill: oklch(from #f59e0b clamp(var(--study-l-min), l, var(--study-l-max)) min(c, 0.13) h);
  }

  html[data-surface]:not([data-surface="default"]):not([data-theme="print"]) svg :is(text, tspan)[fill*="--fig-blue"],
  html[data-surface]:not([data-surface="default"]):not([data-theme="print"]) svg g[fill*="--fig-blue"] :is(text, tspan) {
    fill: oklch(from #3b82f6 clamp(var(--study-l-min), l, var(--study-l-max)) min(c, 0.13) h);
  }

  html[data-surface]:not([data-surface="default"]):not([data-theme="print"]) svg :is(text, tspan)[fill*="--fig-pink"],
  html[data-surface]:not([data-surface="default"]):not([data-theme="print"]) svg g[fill*="--fig-pink"] :is(text, tspan) {
    fill: oklch(from #ec4899 clamp(var(--study-l-min), l, var(--study-l-max)) min(c, 0.13) h);
  }

  html[data-surface]:not([data-surface="default"]):not([data-theme="print"]) svg :is(text, tspan)[fill*="--fig-green"],
  html[data-surface]:not([data-surface="default"]):not([data-theme="print"]) svg g[fill*="--fig-green"] :is(text, tspan) {
    fill: oklch(from #10b981 clamp(var(--study-l-min), l, var(--study-l-max)) min(c, 0.13) h);
  }

  /* Geometry simulator of the Engineering Mechanics problem cards (assets/js/mechanics-3d-sim.js):
     its labels, matrix entries and readouts are text, never accent, so they take the palette's own
     text colour; colour stays in the strokes and fills of the drawing. */
  html[data-surface]:not([data-surface="default"]):not([data-theme="print"]) .m3-svg :is(text, tspan) {
    fill: var(--study-text);
  }
  html[data-surface]:not([data-surface="default"]):not([data-theme="print"]) :is(.m3-readouts dd, .m3-title, .m3-control output) {
    color: var(--study-text);
  }
}

/* ==========================================================================
   9. "ASK AI" WIDGET UNDER A READING PALETTE
   assets/css/ask-ai.css derives every colour from the page's variables, which
   the palettes already re-point; only its accent needs the lightness window of
   section 7 (it is used both as text and as the fill of the primary buttons,
   so the text on those fills becomes the palette background colour).
   ========================================================================== */
@media screen {
  html[data-surface]:not([data-surface="default"]):not([data-theme="print"]) :is(.ask-ai-pill, .ask-ai-panel, .ask-ai-bar) {
    --ai-accent: oklch(from var(--accent, #f59e0b) clamp(var(--study-l-min), l, var(--study-l-max)) min(c, 0.13) h);
    --ai-on-accent: var(--study-bg) !important; /* ask-ai.js sets it inline from the page accent */
    --ai-muted: color-mix(in srgb, var(--study-text-2) 55%, var(--study-text));
  }
}

/* ==========================================================================
   10. FIGURES (RASTER) AND THE QUOTE CARD
   The official figures are PNGs on a pure white canvas: a glaring slab on a
   cream page and worse on charcoal. Light palettes multiply them into the
   surface (white becomes the surface colour, ink stays ink); charcoal inverts
   the lightness of the ink (hue is rotated back) and screens it onto the card.
   The image files are untouched and the print layout ignores all of this.
   The quote card's copy button and its "Copied!" notice used white on cyan or
   green; under a palette they take the palette's text/background pair.
   ========================================================================== */
@media screen {
  html:is([data-surface="cream"], [data-surface="sepia"], [data-surface="slate-paper"], [data-surface="sage"])[data-theme="light"] :is(figure, .statement-figure) img {
    mix-blend-mode: multiply;
  }

  html[data-surface="charcoal"][data-theme="dark"] :is(figure, .statement-figure) img {
    filter: invert(1) hue-rotate(180deg) brightness(0.92);
    mix-blend-mode: screen;
  }

  html[data-surface]:not([data-surface="default"]):not([data-theme="print"]) .ae-quote-btn:hover {
    background: var(--study-text);
    border-color: var(--study-text);
    color: var(--study-bg);
  }

  html[data-surface]:not([data-surface="default"]):not([data-theme="print"]) .ae-quote-copied-tooltip {
    background: var(--study-text);
    color: var(--study-bg);
  }
}

/* ==========================================================================
   11. FILLED SWITCH BUTTON
   The active "Theory / Problem sheets" switch is filled with the subject accent
   and carries a fixed text colour (white on indigo is only 4.47:1). Under a
   palette the fill takes the lightness window and the label the palette's
   background colour, which is AAA for every subject accent.
   ========================================================================== */
@media screen {
  html[data-surface]:not([data-surface="default"]):not([data-theme="print"]) .switch-btn.active {
    background: oklch(from var(--accent) clamp(var(--study-l-min), l, var(--study-l-max)) min(c, 0.13) h);
    border-color: transparent;
  }

  html[data-surface]:not([data-surface="default"]):not([data-theme="print"]) .switch-btn.active,
  html[data-surface]:not([data-surface="default"]):not([data-theme="print"]) .switch-btn.active * {
    color: var(--study-bg);
    -webkit-text-fill-color: var(--study-bg);
  }
}

/* ==========================================================================
   12. QUICK-NAV PILLS (hover and active)
   Maths pills hover as accent text on a tint and are active as white on the accent
   (3.8:1 and 4.5:1); Materials pills hover and are active as black on the accent.
   Under a palette both states become one filled pair: accent lightness window as the
   fill, the palette background colour as the label, AAA for every subject accent.
   ========================================================================== */
@media screen {
  html[data-surface]:not([data-surface="default"]):not([data-theme="print"]) :is(.nav-pill:hover, .nav-pill.active) {
    background: oklch(from var(--accent) clamp(var(--study-l-min), l, var(--study-l-max)) min(c, 0.13) h);
    border-color: transparent;
  }

  html[data-surface]:not([data-surface="default"]):not([data-theme="print"]) :is(.nav-pill:hover, .nav-pill.active),
  html[data-surface]:not([data-surface="default"]):not([data-theme="print"]) :is(.nav-pill:hover, .nav-pill.active) * {
    color: var(--study-bg);
    -webkit-text-fill-color: var(--study-bg);
  }
}

/* ==========================================================================
   13. NO NEON ON STUDY PAGES
   Topic pages still carried coloured glows of their own: a halo around a
   problem card on hover, tinted shadows under result boxes and active pills,
   glowing text. On a flat reading surface they become plain neutral shadows.
   Screen only, so the PDF layout (print.css) is unaffected.
   ========================================================================== */
@media screen {
  html:not([data-theme="print"]) body :is(main, article, header, section, nav) :is(h1, h2, h3, h4, a, span, p, div, li, td, th, strong, summary, button) {
    text-shadow: none !important;
  }

  html:not([data-theme="print"]) :is(.problem-card, .problem-box, .card, .subject-card, .topic-card, .set-card, .concept-card, .theory-card):hover {
    box-shadow: 0 8px 28px rgba(0, 0, 0, 0.28) !important;
  }
  html[data-theme="light"]:not([data-theme="print"]) :is(.problem-card, .problem-box, .card, .subject-card, .topic-card, .set-card, .concept-card, .theory-card):hover {
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.10) !important;
  }

  html:not([data-theme="print"]) :is(.result-box, .final-result, .formula-box, .nav-pill.active, .switch-btn.active, .theme-toggle::after, .section-number, .phase-number, .badge) {
    box-shadow: none !important;
  }
}

/* ==========================================================================
   14. ONE TITLE COLOUR PER SUBJECT, SHORT TITLE BLOCKS
   Some pages painted half of their <h1> in the accent or with a gradient and
   others did not, so the title changed colour from page to page. Every page
   title is now the plain text colour of the page; the subject keeps its
   identity in the icon, the kicker and the borders.
   The long descriptions under the title of a topic page (what the page
   contains, how it was written, where it comes from) are hidden on screen:
   the kicker, the title, the "Official material" links and the PDF buttons
   stay. They are still part of the page, so the PDFs are unchanged.
   ========================================================================== */
@media screen {
  html:not([data-theme="print"]) h1 :is(span, em, strong, b, i) {
    color: inherit !important;
    -webkit-text-fill-color: currentColor !important;
    background: none !important;
    -webkit-background-clip: border-box !important;
    background-clip: border-box !important;
  }

  html:not([data-theme="print"]) :is(.article-header, .page-intro, .page-title-box) >
    :is(p, .article-lead, .article-subtitle, .article-meta, .page-meta, .source-box, .source-banner, .reading-meta, .brand-badge),
  html:not([data-theme="print"]) .page-hero > :is(p.subtitle, .hero-badge),
  html:not([data-theme="print"]) .article-header + .note-banner,
  html:not([data-theme="print"]) .midterm-part-lead {
    display: none !important;
  }
  html:not([data-theme="print"]) :is(.article-header, .page-intro, .page-title-box) > h1 { margin-bottom: 14px; }
}

/* ==========================================================================
   15. COLLAPSIBLE PROBLEM CARDS (assets/js/problem-collapse.js)
   Every problem card of a problem page starts folded to its heading. The
   script marks the heading children (.pc-head) and adds the toggle; the rest
   of the card is hidden while it carries .pc-collapsed. Screen only: print
   shows every problem in full. Text colours are the page's own.
   ========================================================================== */
@media screen {
  .pc-card { position: relative; }
  [data-pc] { scroll-margin-top: 76px; }   /* a linked card lands below the sticky header */
  .pc-card.pc-collapsed > :not(.pc-head) { display: none !important; }
  .pc-card > .pc-head { cursor: pointer; padding-right: 44px; }
  .pc-card.pc-collapsed { padding-top: 18px !important; padding-bottom: 18px !important; margin-bottom: 12px !important; }
  .pc-card.pc-collapsed > .pc-head:last-of-type, .pc-card.pc-collapsed > .pc-head { margin-bottom: 0 !important; border-bottom: 0 !important; padding-bottom: 0 !important; }
  .pc-card.pc-collapsed > .pc-head + .pc-head { margin-top: 8px !important; }
  .pc-card.pc-collapsed:hover { border-color: var(--accent) !important; }
  /* folded: the heading only; the PDF and source chips come back when the card opens */
  .pc-card.pc-collapsed > .pc-head :is(.src-link, .pdf-chips, .ask-ai-bar) { display: none !important; }
  .pc-card.pc-collapsed > .pc-head :is(h2, h3, h4, .problem-title) { margin-bottom: 0 !important; }
  details.problem-card[data-pc]:not([open]) { padding-top: 18px !important; padding-bottom: 18px !important; }

  .pc-toggle {
    position: absolute; top: 14px; right: 14px; z-index: 1;
    display: inline-flex; align-items: center; justify-content: center; width: 32px; height: 32px; padding: 0;
    border: 1px solid var(--border-card, rgba(127, 127, 127, 0.3)); border-radius: 50%;
    background: transparent; color: var(--text-primary, inherit); cursor: pointer;
    transition: transform 0.2s ease, border-color 0.2s ease;
  }
  .pc-toggle svg { fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
  .pc-toggle[aria-expanded="true"] { transform: rotate(180deg); }
  .pc-toggle:hover { border-color: var(--accent); }
  .pc-toggle:focus-visible, .pc-bar__btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

  /* Aerospace Materials I: the cards are <details>; same arrow, no "Click to collapse" label */
  details.problem-card[data-pc] { position: relative; }
  details.problem-card[data-pc] > summary .problem-header > span[style] { display: none; }
  details.problem-card[data-pc] > summary { padding-right: 44px; }
  details.problem-card[data-pc] > summary::after {
    content: ""; position: absolute; top: 18px; right: 18px; width: 10px; height: 10px;
    border-right: 2px solid var(--text-primary, currentColor); border-bottom: 2px solid var(--text-primary, currentColor);
    transform: rotate(45deg); transition: transform 0.2s ease;
  }
  details.problem-card[data-pc][open] > summary::after { transform: rotate(225deg); top: 24px; }
  details.problem-card[data-pc]:not([open]) { margin-bottom: 12px !important; }
  details.problem-card[data-pc]:not([open]) > :not(summary) { display: none !important; }
  details.problem-card[data-pc]:not([open]) > summary :is(.problem-title, h2, h3) { margin-bottom: 0 !important; }

  .pc-bar { display: flex; flex-wrap: wrap; align-items: center; justify-content: flex-end; gap: 8px; margin: 0 0 14px; }
  .pc-bar__count {
    margin-right: auto; font-family: var(--font-mono, monospace); font-size: 0.74rem; letter-spacing: 0.08em;
    text-transform: uppercase; color: var(--text-secondary, inherit);
  }
  .pc-bar__btn {
    padding: 6px 14px; border: 1px solid var(--border-card, rgba(127, 127, 127, 0.3)); border-radius: 999px;
    background: transparent; color: var(--text-primary, inherit); cursor: pointer;
    font-family: var(--font-body, "Newsreader", Georgia, serif); font-size: 0.95rem; font-weight: 600;
  }
  .pc-bar__btn:hover { border-color: var(--accent); }

  /* Subtle gold constellation seal on the PDF download bar (screen only, print.css untouched) */
  .pdf-bar-label {
    display: inline-flex; align-items: center; gap: 7px;
    --sideris-seal: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='96 96 832 832'%3E%3Cpath d='M 398.51,379.43 A 355,160 -14 0 1 652.50,634.24 M 624.34,644.98 A 355,160 -14 0 1 370.40,390.21' fill='none' stroke='black' stroke-width='20' stroke-linecap='round'/%3E%3Cpolyline points='652,286 512,212 368,290 392,430 512,512 632,594 652,736 510,812 368,738' fill='none' stroke='black' stroke-width='22' stroke-linecap='round' stroke-linejoin='round'/%3E%3Ccircle cx='652' cy='286' r='26' fill='black'/%3E%3Ccircle cx='512' cy='212' r='30' fill='black'/%3E%3Ccircle cx='368' cy='290' r='24' fill='black'/%3E%3Ccircle cx='392' cy='430' r='28' fill='black'/%3E%3Ccircle cx='632' cy='594' r='26' fill='black'/%3E%3Ccircle cx='652' cy='736' r='30' fill='black'/%3E%3Ccircle cx='510' cy='812' r='26' fill='black'/%3E%3Ccircle cx='368' cy='738' r='28' fill='black'/%3E%3Cpath d='M 512,456 Q 521,503 568,512 Q 521,521 512,568 Q 503,521 456,512 Q 503,503 512,456 Z' fill='black'/%3E%3C/svg%3E");
  }
  .pdf-bar-label::before {
    content: ""; width: 16px; height: 16px; flex: none;
    background-color: #E1C170;
    -webkit-mask: var(--sideris-seal) center / contain no-repeat;
    mask: var(--sideris-seal) center / contain no-repeat;
  }
  html[data-theme="light"] .pdf-bar-label::before { background-color: #9a6e17; }
}
@media print { .pc-toggle, .pc-bar { display: none !important; } }
