/* ============================================================
   SHARED TEXTBOOK RUNTIME: base.css
   Loaded by every page of every book. Spec: docs/textbook/RUNTIME_SPEC.md.
   The look it implements: docs/textbook/STYLE_STANDARD.md.

   Holds: shared tokens, reset, page foundation, type by element, layout,
   top bar, card shell, buttons, chips, tables, figures, callout, brief,
   glossary tooltip, lightbox, accessibility mode, reduced motion.

   Per-book colours come from the book's assets/tokens.css, loaded after this
   file. Nothing here names a hex colour. Zero em-dashes.
   ============================================================ */

@import url('https://fonts.googleapis.com/css2?family=Hanken+Grotesk:wght@400;500;600;700;800&family=Spectral:ital,wght@0,400;0,500;0,600;1,400&family=IBM+Plex+Mono:wght@400;500;600&display=swap');

/* ---- shared tokens (standard, section 2.1): identical in every book ---- */
:root {
  --display: 'Hanken Grotesk', system-ui, sans-serif;
  --serif: 'Spectral', Georgia, serif;
  --mono: 'IBM Plex Mono', ui-monospace, monospace;

  --text-xs: 0.75rem;
  --text-sm: 0.875rem;
  --text-base: 1rem;
  --text-md: 1.125rem;
  --text-lg: 1.25rem;
  --text-xl: 1.5rem;
  --text-2xl: 1.875rem;
  --text-3xl: 2.25rem;
  --text-4xl: 3rem;
  --text-read: var(--text-lg);
  --text-hero: 4.7rem;

  --prose: 46rem;
  --wide: 68rem;
  --gutter: clamp(1.25rem, 5vw, 4rem);
  --gap-text: 1.25rem;
  --gap-block: 1.75rem;
  --gap-section: clamp(3rem, 6vw, 4rem);

  --r-tag: 4px;
  --r-ctl: 6px;
  --r-card: 8px;

  --fig-sm: 24rem;
  --fig-md: 34rem;
  --fig-full: var(--prose);
  --fig-wide: var(--wide);

  /* diagram geometry: chem's set, shared unchanged. SVG user units. */
  --dia-r-particle: 2;
  --dia-r-atom-sm: 5;
  --dia-r-atom: 7.5;
  --dia-stroke: 1;
  --dia-stroke-bond: 1.2;
  --dia-atom-size: 22px;
  --dia-header-size: 14px;
  --dia-label-size: 12px;
  --dia-caption-size: 12px;

  --topbar-h: 3.5rem;
}

/* ---- reset and page foundation ---- */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html { scroll-behavior: smooth; background-color: var(--paper); scroll-padding-top: calc(var(--topbar-h) + 1rem); }

body {
  font-family: var(--serif);
  font-size: var(--text-read);
  line-height: 1.65;
  color: var(--ink);
  background-color: var(--paper);
  background-image:
    linear-gradient(var(--grid) 1px, transparent 1px),
    linear-gradient(90deg, var(--grid) 1px, transparent 1px);
  background-size: 26px 26px, 26px 26px;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
  overflow-x: hidden;
}

[hidden] { display: none !important; }

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

/* ---- type by element, inside <main> (standard, section 3) ---- */
:where(main) h1, :where(main) h2, :where(main) h3, :where(main) h4 {
  font-family: var(--display);
  color: var(--ink);
  line-height: 1.12;
  letter-spacing: -0.02em;
}
:where(main) h1 { font-size: clamp(var(--text-3xl), 7vw, var(--text-hero)); font-weight: 800; line-height: 1.04; }
:where(main) h2 { font-size: clamp(var(--text-2xl), 3.6vw, var(--text-3xl)); font-weight: 700; }
:where(main) h3 { font-size: var(--text-xl); font-weight: 700; margin-top: 2.25rem; margin-bottom: var(--gap-text); }
:where(main) h4 { font-size: var(--text-lg); font-weight: 700; letter-spacing: -0.01em; }
:where(main) h2 { margin-bottom: var(--gap-text); }

:where(main) p, :where(main) li { font-family: var(--serif); font-size: var(--text-read); line-height: 1.65; color: var(--ink); }
:where(main) p + p { margin-top: var(--gap-text); }
:where(main) :is(p, ul, ol, dl) + :is(p, ul, ol, dl) { margin-top: var(--gap-text); }
:where(main) ul, :where(main) ol { padding-left: 1.4em; }
:where(main) li { margin-bottom: 0.35em; }
:where(main) li:last-child { margin-bottom: 0; }

a:not([class]) { color: var(--ink); text-decoration: underline; text-decoration-color: var(--accent); text-underline-offset: 0.15em; }
em { font-style: italic; color: inherit; }
strong { font-weight: 600; }
abbr { text-decoration: none; }
sub, sup { font-size: 0.7em; line-height: 0; }

.lede { font-size: var(--text-read); line-height: 1.55; color: var(--ink-soft); }
:where(main) .lede { color: var(--ink-soft); }

.label {
  font-family: var(--mono);
  font-size: var(--text-xs);
  font-weight: 600;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--ink-mute);
}

.hint { font-family: var(--display); font-size: var(--text-sm); line-height: 1.45; color: var(--ink-soft); }
:where(main) p.hint { font-family: var(--display); font-size: var(--text-sm); color: var(--ink-soft); }

.eq-display {
  display: block;
  text-align: center;
  font-family: var(--mono);
  font-size: var(--text-xl);
  font-weight: 500;
  line-height: 1.6;
  padding: 0.5rem 0;
  overflow-wrap: anywhere;
}

/* A formula kept in one piece inside a flex or grid parent. */
.formula { white-space: nowrap; }

strong.term {
  font-weight: 700;
  border-bottom: 1.5px dashed var(--accent-soft);
  padding-bottom: 1px;
  cursor: help;
}

/* ---- layout and rhythm (standard, section 4) ---- */
section { position: relative; padding: calc(var(--gap-section) / 2) var(--gutter); }
.narrow { max-width: var(--prose); margin-inline: auto; width: 100%; }
.wrap { max-width: var(--wide); margin-inline: auto; width: 100%; }

.narrow > * + *, .wrap > .narrow > * + * { margin-top: var(--gap-block); }
.narrow > p + p { margin-top: var(--gap-text); }
.narrow > .section-tag + h2 { margin-top: 0; }
.narrow > h2 + * { margin-top: var(--gap-text); }
.narrow > h3 { margin-top: 2.25rem; }
.narrow > h3 + * { margin-top: var(--gap-text); }

.section-tag { display: flex; align-items: baseline; gap: 0.9rem; margin-bottom: 1.1rem; }
.section-tag .num {
  font-family: var(--mono);
  font-size: var(--text-xs);
  font-weight: 600;
  letter-spacing: 0.12em;
  color: var(--accent);
  border: 1px solid var(--accent-soft);
  border-radius: var(--r-tag);
  padding: 0.18rem 0.55rem;
}

/* ---- card shell: one box for every boxed component (standard, section 5) ---- */
.card, .callout, .brief, .sim-box, .exam-frame, .recall, [data-widget] {
  background: var(--card);
  border: 1px solid var(--hair);
  border-radius: var(--r-card);
  padding: clamp(1.1rem, 3vw, 1.5rem);
}
.card > * + *, .callout > * + *, .brief > * + *, .sim-box > * + *, .exam-frame > * + *, .recall > * + * {
  margin-top: 0.9rem;
}
.callout > .label { color: var(--accent); }
:where(main) .callout p, :where(main) .brief p { max-width: none; }
.callout p + p, .brief p + p { margin-top: 0.9rem; }

/* ---- buttons and chips ---- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  min-height: 44px;
  padding: 0.5rem 1.2rem;
  font-family: var(--display);
  font-size: var(--text-base);
  font-weight: 600;
  line-height: 1.2;
  text-decoration: none;
  color: var(--paper);
  background: var(--ink);
  border: 1.5px solid var(--ink);
  border-radius: var(--r-ctl);
  cursor: pointer;
  transition: background-color 0.15s ease, color 0.15s ease, border-color 0.15s ease;
}
.btn:hover:not(:disabled) { background: var(--ink-soft); border-color: var(--ink-soft); }
.btn.ghost { color: var(--ink); background: transparent; border-color: var(--ink); }
.btn.ghost:hover:not(:disabled) { background: var(--paper-2); }
.btn:disabled, .btn[aria-disabled="true"] { opacity: 0.45; cursor: not-allowed; }

.chip {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 44px;
  padding: 0.35rem 0.95rem;
  font-family: var(--display);
  font-size: var(--text-sm);
  font-weight: 600;
  color: var(--ink);
  background: var(--card);
  border: 1.5px solid var(--hair);
  border-radius: var(--r-ctl);
  cursor: pointer;
  transition: background-color 0.15s ease, border-color 0.15s ease;
}
.chip:hover:not(:disabled) { border-color: var(--ink); }
.chip[aria-pressed="true"], .chip.is-on { color: var(--paper); background: var(--ink); border-color: var(--ink); }
.chip:disabled { opacity: 0.45; cursor: not-allowed; }

/* ---- tables (standard, section 5.2) ---- */
.table-wrap { overflow-x: auto; max-width: 100%; border: 1px solid var(--hair); border-radius: var(--r-card); background: var(--card); }
.data-table { width: 100%; border-collapse: collapse; font-family: var(--display); font-size: var(--text-base); }
.data-table caption {
  caption-side: top;
  text-align: left;
  padding: 0.7rem 1rem;
  font-family: var(--mono);
  font-size: var(--text-sm);
  color: var(--ink-soft);
}
.data-table th, .data-table td { padding: 0.6rem 1rem; text-align: left; border-top: 1px solid var(--hair-soft); vertical-align: top; }
.data-table th { font-family: var(--display); font-size: var(--text-base); font-weight: 600; color: var(--ink); background: var(--paper-2); }
.data-table .num { text-align: right; font-family: var(--mono); font-variant-numeric: tabular-nums; white-space: nowrap; }

/* ---- figures (standard, section 5.2) ---- */
.figure { width: 100%; max-width: var(--fig-full); margin-inline: auto; }
.figure--sm { max-width: var(--fig-sm); }
.figure--md { max-width: var(--fig-md); }
.figure--full { max-width: var(--fig-full); }
.figure--wide { max-width: var(--fig-wide); }
.figure .diagram { background: var(--card); border: 1px solid var(--hair); border-radius: var(--r-card); padding: 0.75rem; }
.figure .diagram svg, .figure .diagram img { display: block; width: 100%; height: auto; cursor: zoom-in; }
figcaption {
  margin-top: 0.6rem;
  font-family: var(--mono);
  font-size: var(--text-sm);
  line-height: 1.45;
  color: var(--ink-soft);
}
figcaption b { font-weight: 600; color: var(--ink); }

/* ---- top bar (rendered by core.js) ---- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 60;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  min-height: var(--topbar-h);
  padding: 0.35rem var(--gutter);
  background: color-mix(in srgb, var(--paper) 90%, transparent);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--hair-soft);
}
.topbar-left, .topbar-right { display: flex; align-items: center; gap: 0.5rem; min-width: 0; }
.topbar-right { flex-shrink: 0; }
.crumb {
  font-family: var(--mono);
  font-size: var(--text-xs);
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--ink-mute);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.crumb b { color: var(--ink); font-weight: 600; }
.tb-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.45rem;
  min-height: 44px;
  min-width: 44px;
  padding: 0 0.7rem;
  font-family: var(--mono);
  font-size: var(--text-xs);
  letter-spacing: 0.1em;
  text-transform: uppercase;
  text-decoration: none;
  white-space: nowrap;
  color: var(--ink-soft);
  background: var(--card);
  border: 1px solid var(--hair);
  border-radius: var(--r-ctl);
  cursor: pointer;
  flex-shrink: 0;
}
.tb-btn:hover { color: var(--ink); border-color: var(--ink); background: var(--paper-2); }
.tb-btn svg { width: 16px; height: 16px; flex-shrink: 0; }
.tb-btn .sun { display: none; }
:root.dark .tb-btn .moon { display: none; }
:root.dark .tb-btn .sun { display: block; }

.toc-drop {
  position: fixed;
  top: calc(var(--topbar-h) + 0.25rem);
  left: var(--gutter);
  z-index: 59;
  min-width: 18rem;
  max-width: calc(100vw - 2 * var(--gutter));
  max-height: calc(100vh - var(--topbar-h) - 1.5rem);
  overflow-y: auto;
  background: var(--card);
  border: 1px solid var(--hair);
  border-radius: var(--r-card);
  opacity: 0;
  visibility: hidden;
  transform: translateY(-6px);
  transition: opacity 0.15s ease, transform 0.15s ease, visibility 0.15s;
}
.toc-drop.open { opacity: 1; visibility: visible; transform: none; }
.toc-drop a {
  display: flex;
  align-items: baseline;
  gap: 0.7rem;
  min-height: 44px;
  padding: 0.65rem 0.9rem;
  font-family: var(--display);
  font-size: var(--text-base);
  font-weight: 500;
  color: var(--ink-soft);
  text-decoration: none;
  border-bottom: 1px solid var(--hair-soft);
}
.toc-drop a:last-child { border-bottom: none; }
.toc-drop a:hover { background: var(--paper-2); color: var(--ink); }
.toc-drop .tnum { min-width: 1.6rem; font-family: var(--mono); font-size: var(--text-xs); color: var(--ink-mute); }
.toc-drop .tag-enrichment { margin-left: auto; }

@media (max-width: 768px) {
  .tb-btn .tb-label { display: none; }
  .tb-btn { padding: 0 0.55rem; }
}
@media (max-width: 600px) {
  .crumb { display: none; }
  .topbar { padding-inline: 0.75rem; }
  .toc-drop { left: 0.75rem; right: 0.75rem; min-width: 0; }
}

/* ---- glossary tooltip (rendered by core.js) ---- */
.tb-tooltip {
  position: absolute;
  z-index: 90;
  width: max-content;
  max-width: min(22rem, calc(100vw - 1.5rem));
  padding: 0.8rem 1rem;
  background: var(--card);
  color: var(--ink);
  border: 1px solid var(--ink);
  border-radius: var(--r-card);
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  transition: opacity 0.12s ease;
}
.tb-tooltip.visible { opacity: 1; visibility: visible; }
.tb-tooltip::after {
  content: '';
  position: absolute;
  left: var(--arrow-left, 50%);
  bottom: -6px;
  width: 10px;
  height: 10px;
  background: var(--card);
  border-right: 1px solid var(--ink);
  border-bottom: 1px solid var(--ink);
  transform: translateX(-50%) rotate(45deg);
}
.tb-tooltip-term { font-family: var(--display); font-size: var(--text-base); font-weight: 700; margin-bottom: 0.25rem; }
.tb-tooltip-definition { font-family: var(--serif); font-size: var(--text-base); line-height: 1.45; color: var(--ink-soft); }
.tb-tooltip-example { margin-top: 0.4rem; font-family: var(--serif); font-size: var(--text-sm); font-style: italic; color: var(--ink-mute); }

/* ---- lightbox (rendered by core.js) ---- */
.lightbox-overlay {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: none;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1rem;
  padding: 1.5rem;
  background: color-mix(in srgb, var(--paper) 94%, transparent);
  cursor: zoom-out;
}
.lightbox-overlay.open { display: flex; }
.lightbox-content { max-width: min(96vw, 80rem); max-height: 78vh; overflow: auto; background: var(--card); border: 1px solid var(--hair); border-radius: var(--r-card); padding: 1rem; cursor: default; }
.lightbox-content svg, .lightbox-content img { display: block; width: 100%; height: auto; max-height: 72vh; }
.lightbox-cap { max-width: 60rem; font-family: var(--mono); font-size: var(--text-sm); color: var(--ink-soft); text-align: center; }
.lightbox-close {
  position: absolute;
  top: 0.75rem;
  right: 0.75rem;
  min-width: 44px;
  min-height: 44px;
  font-size: var(--text-xl);
  line-height: 1;
  color: var(--ink);
  background: var(--card);
  border: 1px solid var(--hair);
  border-radius: var(--r-ctl);
  cursor: pointer;
}

/* ---- accessibility mode (sp_accessibility_mode, same key as the app) ---- */
.accessibility-mode body { font-family: Verdana, 'Segoe UI', sans-serif; line-height: 1.8; letter-spacing: 0.05em; }
.accessibility-mode :where(main) :is(h1, h2, h3, h4) { letter-spacing: 0.08em; }
.accessibility-mode :where(main) :is(p, li) { font-family: Verdana, 'Segoe UI', sans-serif; font-weight: 500; }

/* ---- reduced motion ---- */
@media (prefers-reduced-motion: reduce) {
  html { scroll-behavior: auto; }
  *, *::before, *::after { animation-duration: 0.001ms !important; animation-iteration-count: 1 !important; transition-duration: 0.001ms !important; }
}
