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

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-body);
  line-height: 1.6;
  overflow-wrap: break-word;
}

a { color: var(--accent); }
a:focus-visible, button:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.skip-link { position: absolute; left: -9999px; }
.skip-link:focus { left: var(--gutter); top: 8px; background: var(--surface); padding: 4px 8px; }

/* Top navigation */
.site-header { border-bottom: 1px solid var(--border); background: var(--surface); }
.site-nav {
  max-width: 1100px; margin: 0 auto; padding: 8px var(--gutter);
  display: flex; flex-wrap: wrap; align-items: center; gap: 4px 16px;
}
.site-nav .brand { font-weight: 700; color: var(--text); text-decoration: none; margin-right: auto; }

main { max-width: 1100px; margin: 0 auto; padding: 24px var(--gutter) 48px; }

h1 { font-size: clamp(1.6rem, 4vw, 2.2rem); line-height: 1.2; margin: 0.5em 0; }
.lede { font-size: 1.1rem; color: var(--text-muted); }

.placeholder {
  border: 1px dashed var(--border); border-radius: var(--radius);
  padding: 16px; color: var(--text-muted);
}

.module-list { list-style: none; padding: 0; display: grid; gap: 12px; }
.module-list a {
  display: block; padding: 12px 16px; border: 1px solid var(--border);
  border-radius: var(--radius); background: var(--surface); text-decoration: none;
}
.module-list a span { display: block; color: var(--text-muted); font-size: 0.95rem; }

.site-footer { border-top: 1px solid var(--border); color: var(--text-muted); font-size: 0.9rem; }
.site-footer p { max-width: 1100px; margin: 0 auto; padding: 16px var(--gutter); }

/* Content sections */
h2 { margin-top: 2em; line-height: 1.25; }
.note { font-size: 0.9rem; color: var(--text-muted); border-left: 3px solid var(--border); padding-left: 12px; }
.katex-display { overflow-x: auto; overflow-y: hidden; padding: 4px 0; }

/* Interactive demos */
.demo {
  border: 1px solid var(--border); border-radius: var(--radius);
  background: var(--surface); padding: 16px; margin: 24px 0;
}
.demo-panels { display: grid; grid-template-columns: 1fr; gap: 16px; }
@media (min-width: 760px) { .demo-panels { grid-template-columns: 3fr 2fr; } }
.demo figure { margin: 0; }
.demo figcaption { font-size: 0.9rem; color: var(--text-muted); margin-top: 6px; }
.demo canvas { display: block; width: 100%; height: auto; border: 1px solid var(--border); border-radius: 4px; }

.axis-label { font-size: 0.8rem; color: var(--text-muted); display: flex; justify-content: space-between; }

.controls { display: flex; flex-wrap: wrap; gap: 12px 20px; align-items: end; margin-top: 16px; }
.controls fieldset { border: 0; margin: 0; padding: 0; display: flex; gap: 10px; flex-wrap: wrap; }
.controls legend, .controls .label { font-size: 0.85rem; color: var(--text-muted); display: block; margin-bottom: 4px; padding: 0; }
.control { display: flex; flex-direction: column; }
.controls select, .controls input[type="range"] { font: inherit; }
.controls select { padding: 4px 6px; border: 1px solid var(--border); border-radius: 4px; background: var(--bg); color: var(--text); }
.button-row { display: flex; gap: 8px; }
.demo button {
  font: inherit; padding: 6px 14px; border-radius: 4px; cursor: pointer;
  border: 1px solid var(--accent); background: var(--surface); color: var(--accent);
}
.demo button.primary { background: var(--accent); color: var(--accent-contrast); }
.demo-status { font-size: 0.9rem; color: var(--text-muted); margin: 12px 0 0; font-variant-numeric: tabular-nums; }

.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

/* Charts (D3) */
.chart { display: block; width: 100%; height: auto; font-family: var(--font-body); }
.chart-axis text { fill: var(--text-muted); font-size: 12px; }
.chart-axis path, .chart-axis line { stroke: var(--border); }
.chart-grid line { stroke: var(--chart-grid); }
.chart-label { fill: var(--text-muted); font-size: 13px; }
.chart-line { fill: none; stroke-width: 2; }
.chart-line.dashed { stroke-dasharray: 5 4; }
.series-a { stroke: var(--series-a); color: var(--series-a); }
.series-b { stroke: var(--series-b); color: var(--series-b); }
.chart-marker line { stroke: var(--text-muted); stroke-dasharray: 3 3; }
.chart-marker text { fill: var(--text-muted); font-size: 12px; }
.chart-legend { display: flex; flex-wrap: wrap; gap: 4px 16px; font-size: 0.85rem; color: var(--text-muted); margin: 4px 0 0; padding: 0; list-style: none; }
.chart-legend li::before { content: ""; display: inline-block; width: 16px; height: 0; border-top: 3px solid currentColor; vertical-align: middle; margin-right: 6px; }
.chart-legend li.dashed::before { border-top-style: dashed; }
.chart-legend li span { color: var(--text-muted); }

/* Inverse-problem demos */
@media (min-width: 760px) {
  .demo-panels.even { grid-template-columns: 1fr 1fr; }
  .demo-panels.models { grid-template-columns: 1fr 1fr; }
  .narrow { max-width: calc(50% - 8px); }
}
/* Chart keeps a readable size; its caption uses the full width. */
.chart-block { margin: 16px 0 0; }
.chart-block > [data-role] { max-width: 640px; }
.scenario-row { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 16px; }
.demo-readout { margin: 12px 0 0; padding: 8px 12px; border-radius: 4px; background: var(--bg); min-height: 1.6em; }
.demo-readout.warning { background: var(--warn-bg); color: var(--warn-text); }
.demo-readout:empty { display: none; }
.demo button:disabled { opacity: 0.5; cursor: default; }

/* Citations and bibliography */
.bibliography { padding-left: 1.4em; }
.bibliography li { margin-bottom: 0.9em; }
.cited-on { display: block; font-size: 0.85rem; color: var(--text-muted); }

/* Single-page layout: collapsible sections */
html { scroll-padding-top: 12px; }
.section-tools { display: flex; gap: 8px; margin: 8px 0 20px; }
.section-tools button {
  font: inherit; font-size: 0.9rem; padding: 4px 12px; border-radius: 4px; cursor: pointer;
  border: 1px solid var(--border); background: var(--surface); color: var(--text-muted);
}
details.section {
  border: 1px solid var(--border); border-radius: var(--radius);
  background: var(--surface); margin: 12px 0;
}
details.section > summary {
  list-style: none; cursor: pointer; padding: 14px 44px 14px 16px; position: relative;
  display: flex; flex-wrap: wrap; align-items: baseline; gap: 2px 12px;
}
details.section > summary::-webkit-details-marker { display: none; }
details.section > summary::after {
  content: ""; position: absolute; right: 18px; top: 50%; width: 9px; height: 9px;
  border-right: 2px solid var(--text-muted); border-bottom: 2px solid var(--text-muted);
  transform: translateY(-70%) rotate(45deg); transition: transform 0.15s;
}
details.section[open] > summary::after { transform: translateY(-30%) rotate(-135deg); }
details.section > summary:hover .section-title { color: var(--accent); }
details.section > summary:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; border-radius: var(--radius); }
.section-title { margin: 0; font-size: 1.25rem; line-height: 1.3; }
.section-sub { color: var(--text-muted); font-size: 0.95rem; }
.section-body { padding: 0 16px 24px; border-top: 1px solid var(--border); }
.section-body .demo { background: var(--bg); }
.section-body .lede { margin-top: 16px; }
@media (prefers-reduced-motion: reduce) { details.section > summary::after { transition: none; } }

/* Open problems */
.problem-cards { display: grid; gap: 12px; grid-template-columns: 1fr; margin-top: 16px; }
.problem-card { border: 1px solid var(--border); border-radius: var(--radius); padding: 4px 16px 12px; background: var(--bg); }
.problem-card h3 { margin-top: 12px; font-size: 1.05rem; }
.card-refs { font-size: 0.9rem; color: var(--text-muted); }

/* Language switch */
.lang-switch { display: flex; border: 1px solid var(--border); border-radius: 4px; overflow: hidden; }
.lang-switch a { padding: 2px 10px; font-size: 0.85rem; font-weight: 600; color: var(--text-muted); text-decoration: none; }
.lang-switch a + a { border-left: 1px solid var(--border); }
.lang-switch a[aria-current="true"] { background: var(--accent); color: var(--accent-contrast); }
.lang-switch a:not([aria-current="true"]):hover { color: var(--accent); }

/* Theme toggle (icon shows the theme it switches to) */
.theme-toggle {
  width: 30px; height: 26px; padding: 0; border: 1px solid var(--border); border-radius: 4px;
  background: var(--surface); color: var(--text-muted); cursor: pointer; font-size: 15px; line-height: 1;
}
.theme-toggle:hover { color: var(--accent); border-color: var(--accent); }
.theme-toggle::before { content: "☾"; }
.theme-toggle[data-next="light"]::before { content: "☀"; }

/* ---------------------------------------------------------------- NAG demos */
.demo button[aria-pressed="true"] { background: var(--accent); color: var(--accent-contrast); }
.metrics { width: 100%; border-collapse: collapse; margin-top: 16px; font-size: 0.95rem; font-variant-numeric: tabular-nums; }
.metrics caption { text-align: left; font-size: 0.85rem; color: var(--text-muted); margin-bottom: 6px; }
.metrics th, .metrics td { text-align: left; padding: 6px 16px 6px 0; border-bottom: 1px solid var(--border); font-weight: normal; }
.metrics thead th { color: var(--text-muted); font-size: 0.85rem; }
.metrics tbody th { color: var(--text-muted); }

.control.grow { flex: 1 1 260px; }
.control.grow input[type="range"] { width: 100%; }
.chart-caption { font-size: 0.9rem; color: var(--text-muted); margin: 4px 0 0; }
.series-muted { stroke: var(--series-muted); color: var(--series-muted); }
.bar.bar-a { fill: var(--series-a); }
.bar.bar-b { fill: var(--series-b); }
.bar.bar-muted { fill: var(--series-muted); }
.chart-bars rect:hover { opacity: 0.75; }
.chart-marker.marker-best line { stroke: var(--text); stroke-dasharray: 5 4; }
.chart-marker.marker-best text { fill: var(--text); }

/* Cell diagrams. Operation hues: 3 validated categorical slots; skip and none
   are neutral, and every edge also carries a text label. */
.cell-view { display: block; width: 100%; max-width: 360px; height: auto; margin: 0 auto; font-family: var(--font-body); }
.cell-edge { fill: none; stroke-width: 4; stroke-linecap: round; }
.cell-edge.op-none { stroke: var(--border); stroke-width: 2; stroke-dasharray: 3 5; }
.cell-edge.op-skip { stroke: var(--op-skip); }
.cell-edge.op-conv1x1 { stroke: var(--op-conv1x1); }
.cell-edge.op-conv3x3 { stroke: var(--op-conv3x3); }
.cell-edge.op-avgpool { stroke: var(--op-avgpool); }
.cell-label { font-size: 12px; fill: var(--text); paint-order: stroke; stroke: var(--bg); stroke-width: 4px; stroke-linejoin: round; }
.cell-node { fill: var(--surface); stroke: var(--text-muted); stroke-width: 2; }
.cell-node-label { font-size: 10px; fill: var(--text); }
.cell-view.invalid .cell-node { stroke: var(--series-b); stroke-dasharray: 3 2; }
.cell-pair { display: grid; grid-template-columns: 1fr; gap: 16px; margin-top: 16px; }
@media (min-width: 640px) { .cell-pair { grid-template-columns: 1fr 1fr; } }
.cell-pair figure { margin: 0; }

.mechanisms { display: grid; gap: 16px; grid-template-columns: 1fr; }
.demo.mechanism { margin: 0; }
.latent-layout { display: grid; gap: 16px; grid-template-columns: 1fr; align-items: start; }
@media (min-width: 760px) { .latent-layout { grid-template-columns: minmax(0, 420px) minmax(0, 1fr); } }
.demo canvas[data-role="map"] { aspect-ratio: 1; cursor: crosshair; image-rendering: pixelated; }
.demo canvas[data-role="plot"] { aspect-ratio: 16 / 9; max-width: 640px; }
/* Must match RAMP in assets/js/latent-demo.js */
.legend-bar.seq { height: 10px; border-radius: 2px; background: linear-gradient(to right, rgb(205,226,251), rgb(109,167,236), rgb(42,120,214), rgb(28,92,171), rgb(13,54,107)); }
.legend-bar { margin-top: 2px; }
