/* ==========================================================================
   Inference Internals — reading-first stylesheet
   ========================================================================== */

:root {
  --serif: "Source Serif 4", "Iowan Old Style", "Charter", Georgia, serif;
  --sans: "Inter", ui-sans-serif, system-ui, -apple-system, "Segoe UI", sans-serif;
  --mono: "JetBrains Mono", ui-monospace, "SF Mono", Menlo, monospace;

  --bg: #fbfaf7;
  --bg-sidebar: #f5f3ee;
  --surface: #f1eee7;
  --text: #1f1d1a;
  --text-muted: #6b665e;
  --text-faint: #9a948a;
  --rule: #e4e0d7;
  --accent: #b4441f;
  --accent-soft: rgba(180, 68, 31, 0.1);
  --link: #1f1d1a;
  --link-underline: rgba(180, 68, 31, 0.45);
  --code-bg: #f3f0ea;
  --code-text: #2b2823;
  --tok-k: #9b3b1b;
  --tok-f: #2d5f8a;
  --tok-n: #7a4fa0;
  --tok-c: #8f897e;
  --selection: rgba(180, 68, 31, 0.18);

  --measure: 42rem;
  --wide: 60rem;
  --sidebar-w: 17.5rem;
  --topbar-h: 3.25rem;

  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #151412;
    --bg-sidebar: #1a1917;
    --surface: #211f1c;
    --text: #e9e5dc;
    --text-muted: #a39d92;
    --text-faint: #6f6a62;
    --rule: #2e2b27;
    --accent: #f08a5d;
    --accent-soft: rgba(240, 138, 93, 0.12);
    --link: #e9e5dc;
    --link-underline: rgba(240, 138, 93, 0.5);
    --code-bg: #1d1c19;
    --code-text: #e2ddd2;
    --tok-k: #f0956c;
    --tok-f: #8cbcea;
    --tok-n: #c9a4ec;
    --tok-c: #7d776d;
    --selection: rgba(240, 138, 93, 0.25);
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --bg: #151412;
  --bg-sidebar: #1a1917;
  --surface: #211f1c;
  --text: #e9e5dc;
  --text-muted: #a39d92;
  --text-faint: #6f6a62;
  --rule: #2e2b27;
  --accent: #f08a5d;
  --accent-soft: rgba(240, 138, 93, 0.12);
  --link: #e9e5dc;
  --link-underline: rgba(240, 138, 93, 0.5);
  --code-bg: #1d1c19;
  --code-text: #e2ddd2;
  --tok-k: #f0956c;
  --tok-f: #8cbcea;
  --tok-n: #c9a4ec;
  --tok-c: #7d776d;
  --selection: rgba(240, 138, 93, 0.25);
  color-scheme: dark;
}

/* ---------- Base ---------- */

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

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
  scroll-behavior: smooth;
  scroll-padding-top: 2rem;
}
@media (prefers-reduced-motion: reduce) { html { scroll-behavior: auto; } }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--serif);
  font-size: clamp(1.0625rem, 0.98rem + 0.35vw, 1.1875rem);
  line-height: 1.7;
  font-optical-sizing: auto;
  font-kerning: normal;
  font-variant-ligatures: common-ligatures contextual;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-rendering: optimizeLegibility;
}

::selection { background: var(--selection); }

img, svg, video, canvas { display: block; max-width: 100%; }
img { height: auto; }

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

/* ---------- Reading progress ---------- */

.progress {
  position: fixed;
  inset: 0 0 auto 0;
  height: 2px;
  z-index: 60;
  pointer-events: none;
}
.progress span {
  display: block;
  height: 100%;
  width: 100%;
  background: var(--accent);
  transform-origin: 0 50%;
  transform: scaleX(0);
}

/* ---------- Layout ---------- */

.layout {
  display: grid;
  grid-template-columns: var(--sidebar-w) minmax(0, 1fr);
  min-height: 100vh;
}

.sidebar {
  position: sticky;
  top: 0;
  height: 100vh;
  background: var(--bg-sidebar);
  border-right: 1px solid var(--rule);
  font-family: var(--sans);
}
.sidebar-inner {
  height: 100%;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 2.25rem 1.5rem 1.5rem 1.75rem;
  display: flex;
  flex-direction: column;
  scrollbar-width: thin;
}

.sidebar-brand {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  color: var(--text);
  text-decoration: none;
  font-weight: 600;
  font-size: 0.95rem;
  line-height: 1.2;
  letter-spacing: -0.01em;
  margin-bottom: 2.5rem;
}
.mark {
  width: 1.75rem;
  height: 1.75rem;
  border-radius: 6px;
  background:
    linear-gradient(135deg, transparent 0 46%, var(--bg-sidebar) 46% 54%, transparent 54%),
    var(--accent);
  flex: none;
}

.sidebar-kicker {
  margin: 0 0 0.75rem;
  font-size: 0.6875rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.12em;
  color: var(--text-faint);
}

/* ---------- TOC ---------- */

.toc ol { list-style: none; margin: 0; padding: 0; }
.toc > ol { counter-reset: sec; }
.toc li { margin: 0; }

.toc a {
  position: relative;
  display: block;
  padding: 0.4rem 0.5rem 0.4rem 0.875rem;
  color: var(--text-muted);
  text-decoration: none;
  font-size: 0.875rem;
  line-height: 1.4;
  border-radius: 6px;
  transition: color 0.15s ease, background-color 0.15s ease;
}
.toc a::before {
  content: "";
  position: absolute;
  left: 0;
  top: 0.45rem;
  bottom: 0.45rem;
  width: 2px;
  border-radius: 2px;
  background: transparent;
  transition: background-color 0.15s ease;
}
.toc a:hover { color: var(--text); background: var(--accent-soft); }

.toc > ol > li > a { counter-increment: sec; font-weight: 500; }
.toc > ol > li > a .num {
  display: inline-block;
  min-width: 1.6em;
  color: var(--text-faint);
  font-variant-numeric: tabular-nums;
  font-weight: 400;
}
.toc ol ol a { padding-left: 2.25rem; font-size: 0.8125rem; }

.toc a.active { color: var(--text); }
.toc a.active::before { background: var(--accent); }
.toc a.active .num { color: var(--accent); }

.sidebar-foot {
  margin-top: auto;
  padding-top: 2rem;
}
.text-btn {
  font: inherit;
  font-size: 0.8125rem;
  color: var(--text-muted);
  background: none;
  border: 1px solid var(--rule);
  border-radius: 999px;
  padding: 0.35rem 0.85rem;
  cursor: pointer;
}
.text-btn:hover { color: var(--text); border-color: var(--text-faint); }
.label-light { display: none; }
:root[data-theme="dark"] .label-dark { display: none; }
:root[data-theme="dark"] .label-light { display: inline; }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .label-dark { display: none; }
  :root:not([data-theme="light"]) .label-light { display: inline; }
}

/* ---------- Top bar (mobile only) ---------- */

.topbar { display: none; }

.icon-btn {
  display: inline-grid;
  place-items: center;
  width: 2.5rem;
  height: 2.5rem;
  border: 0;
  background: none;
  color: var(--text);
  border-radius: 8px;
  cursor: pointer;
}
.icon-btn:hover { background: var(--accent-soft); }
.i-sun { display: none; }
:root[data-theme="dark"] .i-sun { display: block; }
:root[data-theme="dark"] .i-moon { display: none; }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .i-sun { display: block; }
  :root:not([data-theme="light"]) .i-moon { display: none; }
}

/* ---------- Content column ---------- */

.content {
  min-width: 0;
  padding: 5rem 2.5rem 6rem;
}

.prose {
  max-width: var(--measure);
  margin: 0 auto;
  hanging-punctuation: first;
}

/* Hero */
.hero { margin-bottom: 4rem; }
.eyebrow {
  font-family: var(--sans);
  font-size: 0.75rem;
  font-weight: 600;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--accent);
  margin: 0 0 1.25rem;
}
.hero h1 {
  font-size: clamp(2.5rem, 1.6rem + 3.6vw, 4rem);
  line-height: 1.04;
  letter-spacing: -0.025em;
  font-weight: 700;
  margin: 0 0 1.5rem;
  text-wrap: balance;
}
.dek {
  font-size: 1.3em;
  line-height: 1.45;
  color: var(--text-muted);
  margin: 0 0 1.75rem;
  text-wrap: pretty;
}
.byline {
  font-family: var(--sans);
  font-size: 0.8125rem;
  color: var(--text-faint);
  margin: 0;
  padding-top: 1.25rem;
  border-top: 1px solid var(--rule);
}
.byline span { color: var(--text-muted); font-weight: 500; }

/* Headings */
.prose h2, .prose h3, .prose h4 {
  font-weight: 600;
  letter-spacing: -0.015em;
  text-wrap: balance;
}
.prose h2 {
  font-size: 1.75em;
  line-height: 1.2;
  margin: 4.5rem 0 1.25rem;
  padding-top: 2.5rem;
  border-top: 1px solid var(--rule);
}
.prose h2 .sec-num {
  display: block;
  font-family: var(--sans);
  font-size: 0.75rem;
  font-weight: 600;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--accent);
  margin-bottom: 0.6rem;
}
.prose h3 {
  font-size: 1.25em;
  line-height: 1.3;
  margin: 2.75rem 0 0.75rem;
}
.prose h2 + p, .prose h3 + p { margin-top: 0; }

.heading-anchor {
  margin-left: 0.4em;
  color: var(--text-faint);
  text-decoration: none;
  font-weight: 400;
  opacity: 0;
  transition: opacity 0.15s;
}
.prose h2:hover .heading-anchor,
.prose h3:hover .heading-anchor,
.heading-anchor:focus-visible { opacity: 1; }

/* Body text */
.prose p, .prose ul, .prose ol { margin: 0 0 1.4em; text-wrap: pretty; }
.prose .lead { font-size: 1.15em; line-height: 1.6; }
.prose ul, .prose ol { padding-left: 1.4em; }
.prose li { margin-bottom: 0.5em; padding-left: 0.2em; }
.prose li::marker { color: var(--text-faint); }
.prose strong { font-weight: 600; }

.prose a {
  color: var(--link);
  text-decoration: underline;
  text-decoration-color: var(--link-underline);
  text-decoration-thickness: 1.5px;
  text-underline-offset: 0.18em;
  transition: text-decoration-color 0.15s, color 0.15s;
}
.prose a:hover { color: var(--accent); text-decoration-color: currentColor; }

abbr[title] { text-decoration: underline dotted; text-underline-offset: 0.2em; cursor: help; }
.nowrap { white-space: nowrap; }

kbd {
  font-family: var(--sans);
  font-size: 0.72em;
  padding: 0.12em 0.45em;
  border: 1px solid var(--rule);
  border-bottom-width: 2px;
  border-radius: 5px;
  background: var(--surface);
  vertical-align: 0.1em;
}

/* Code */
code, pre { font-family: var(--mono); font-variant-ligatures: none; }
:not(pre) > code {
  font-size: 0.84em;
  padding: 0.12em 0.38em;
  border-radius: 4px;
  background: var(--code-bg);
  color: var(--code-text);
  border: 1px solid var(--rule);
}
pre {
  margin: 2rem 0;
  padding: 1.25rem 1.5rem;
  background: var(--code-bg);
  color: var(--code-text);
  border: 1px solid var(--rule);
  border-radius: 10px;
  overflow-x: auto;
  font-size: 0.8125rem;
  line-height: 1.7;
  tab-size: 4;
  scrollbar-width: thin;
}
/* highlight.js tokens (applied at build time) */
.hljs-keyword, .hljs-selector-tag, .hljs-literal, .hljs-built_in, .hljs-type, .hljs-tag { color: var(--tok-k); }
.hljs-title, .hljs-section, .hljs-attr, .hljs-attribute, .hljs-name { color: var(--tok-f); }
.hljs-number, .hljs-string, .hljs-regexp, .hljs-symbol, .hljs-variable.language_ { color: var(--tok-n); }
.hljs-comment, .hljs-quote, .hljs-meta { color: var(--tok-c); font-style: italic; }
.hljs-emphasis { font-style: italic; }
.hljs-strong { font-weight: 600; }

/* Blockquote */
blockquote {
  margin: 2.5rem 0;
  padding: 0 0 0 1.5rem;
  border-left: 3px solid var(--accent);
  font-size: 1.2em;
  line-height: 1.5;
  font-style: italic;
  color: var(--text);
}
blockquote p:last-child { margin-bottom: 0; }

/* Callouts */
.callout {
  margin: 2.25rem 0;
  padding: 1.1rem 1.35rem;
  border-radius: 10px;
  background: var(--accent-soft);
  font-size: 0.95em;
}
.callout.note { background: var(--surface); }
.callout p { margin: 0 0 0.5em; }
.callout p:last-child { margin-bottom: 0; }
.callout-title {
  font-family: var(--sans);
  font-size: 0.75rem !important;
  font-weight: 600;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--accent);
}
.callout.note .callout-title { color: var(--text-muted); }

/* Equations */
.equation {
  margin: 2rem 0 !important;
  padding: 1rem 1.25rem;
  text-align: center;
  font-family: var(--mono);
  font-size: 0.85em;
  background: var(--surface);
  border-radius: 8px;
  overflow-x: auto;
}

/* LaTeX (KaTeX, rendered at build time) */
.prose .katex { font-size: 1.08em; }
.prose .katex-block { margin: 1.75rem 0; }
.prose .katex-display {
  margin: 0;
  padding: 0.35rem 0;
  overflow-x: auto;
  overflow-y: hidden;
  scrollbar-width: thin;
}
.prose .katex-error { font-family: var(--mono); font-size: 0.85em; }

/* ---------- Figures & media ---------- */

figure { margin: 2.75rem 0; }
figure.wide {
  width: min(var(--wide), calc(100vw - var(--sidebar-w) - 5rem));
  margin-left: 50%;
  transform: translateX(-50%);
}
figure img, figure video, figure .three-stage, figure .video {
  width: 100%;
  border-radius: 10px;
  border: 1px solid var(--rule);
  background: var(--surface);
}
figcaption {
  max-width: var(--measure);
  margin: 0.9rem auto 0;
  font-family: var(--sans);
  font-size: 0.8125rem;
  line-height: 1.55;
  color: var(--text-muted);
}
figcaption strong { color: var(--text); font-weight: 600; }
figcaption a { color: inherit; }

/* three.js stage */
.three-stage {
  position: relative;
  aspect-ratio: 16 / 8;
  overflow: hidden;
  cursor: grab;
  touch-action: pan-y;
}
.three-stage:active { cursor: grabbing; }
.three-stage canvas { width: 100%; height: 100%; }
.three-hud {
  position: absolute;
  left: 1rem;
  bottom: 1rem;
  display: flex;
  align-items: center;
  gap: 0.75rem;
  font-family: var(--sans);
  font-size: 0.75rem;
  color: var(--text-muted);
}
.three-hud b { color: var(--text); font-variant-numeric: tabular-nums; font-weight: 600; }
.chip {
  font: inherit;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--rule);
  border-radius: 999px;
  padding: 0.3rem 0.75rem;
  cursor: pointer;
}
.chip:hover { border-color: var(--text-faint); }
.three-fallback {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  margin: 0 !important;
  font-family: var(--sans);
  font-size: 0.875rem;
  color: var(--text-faint);
}
.three-stage.ready .three-fallback { display: none; }

/* Click-to-load video */
.video {
  position: relative;
  aspect-ratio: 16 / 9;
  overflow: hidden;
  background-size: cover;
  background-position: center;
}
.video iframe { position: absolute; inset: 0; width: 100%; height: 100%; border: 0; }
.video-play {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  border: 0;
  background: rgba(0, 0, 0, 0.18);
  color: #e33;
  cursor: pointer;
  transition: background-color 0.2s;
}
.video-play:hover { background: rgba(0, 0, 0, 0.05); }
.video-play svg { filter: drop-shadow(0 4px 12px rgba(0, 0, 0, 0.3)); }

/* ---------- Tables ---------- */

.table-wrap {
  margin: 2.25rem 0;
  overflow-x: auto;
  border: 1px solid var(--rule);
  border-radius: 10px;
}
table {
  width: 100%;
  border-collapse: collapse;
  font-family: var(--sans);
  font-size: 0.875rem;
  line-height: 1.5;
}
th, td { padding: 0.7rem 1rem; text-align: left; border-bottom: 1px solid var(--rule); }
thead th {
  font-size: 0.75rem;
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--text-muted);
  background: var(--surface);
}
tbody tr:last-child td { border-bottom: 0; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
/* Markdown `---:` columns */
th[style*="right"], td[style*="right"] { font-variant-numeric: tabular-nums; white-space: nowrap; }

/* ---------- Share ---------- */

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

.share { font-family: var(--sans); }
.share-row { display: flex; flex-wrap: wrap; gap: 0.375rem; }
.share-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  min-width: 2.125rem;
  height: 2.125rem;
  padding: 0;
  border: 1px solid var(--rule);
  border-radius: 999px;
  background: none;
  color: var(--text-muted);
  font: inherit;
  font-size: 0.8125rem;
  font-weight: 500;
  text-decoration: none;
  cursor: pointer;
  transition: color 0.15s, border-color 0.15s, background-color 0.15s;
}
.share-btn:hover { color: var(--text); border-color: var(--text-faint); background: var(--accent-soft); }
.share-icon { position: relative; display: inline-grid; place-items: center; width: 0.9375rem; height: 0.9375rem; }
.share-icon svg { width: 100%; height: 100%; fill: currentColor; grid-area: 1 / 1; transition: opacity 0.15s, transform 0.15s; }
.share-icon svg:nth-child(2) { opacity: 0; transform: scale(0.6); }
.share-copy.copied { color: var(--accent); border-color: var(--accent); }
.share-copy.copied .share-icon svg:nth-child(1) { opacity: 0; transform: scale(0.6); }
.share-copy.copied .share-icon svg:nth-child(2) { opacity: 1; transform: none; }

/* Under the title, beside date and read time */
.byline {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 0.75rem 1.5rem;
}
.byline p { margin: 0; }
.share--hero .share-btn { border-color: transparent; }
.share--hero .share-btn:hover { border-color: var(--rule); }

/* Sidebar */
.share--sidebar { margin-bottom: 1.25rem; }
.share--sidebar .sidebar-kicker { margin-bottom: 0.6rem; }
.share--sidebar .share-row { margin-left: -0.3rem; gap: 0.125rem; }
.share--sidebar .share-btn { border-color: transparent; }

/* End of the article */
.share--end {
  margin-top: 4.5rem;
  padding: 1.75rem;
  border-radius: 12px;
  background: var(--surface);
  text-align: center;
}
.share-title {
  margin: 0 0 1rem !important;
  font-family: var(--serif);
  font-size: 1.25em;
  font-weight: 600;
  letter-spacing: -0.01em;
  color: var(--text);
}
.share--end .share-row { justify-content: center; gap: 0.5rem; }
.share--end .share-btn { height: 2.375rem; padding: 0 1rem 0 0.875rem; background: var(--bg); color: var(--text); }
.share--end .share-btn:hover { background: var(--accent-soft); }
.share--end + .article-foot { margin-top: 2.5rem; }

/* ---------- References & footer ---------- */

.refs { padding-left: 1.6em; }
.refs li { margin-bottom: 0.9em; }
.ref-meta {
  display: block;
  font-family: var(--sans);
  font-size: 0.8125rem;
  color: var(--text-faint);
}

.article-foot {
  margin-top: 5rem;
  padding-top: 1.5rem;
  border-top: 1px solid var(--rule);
  font-family: var(--sans);
  font-size: 0.8125rem;
  color: var(--text-faint);
}
.article-foot a { color: var(--text-muted) !important; }

/* ---------- Responsive ---------- */

@media (max-width: 1100px) {
  :root { --sidebar-w: 15.5rem; }
  .content { padding-left: 2rem; padding-right: 2rem; }
}

@media (max-width: 860px) {
  .layout { display: block; }

  .topbar {
    position: sticky;
    top: 0;
    z-index: 40;
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: var(--topbar-h);
    padding: 0 0.5rem;
    background: color-mix(in srgb, var(--bg) 88%, transparent);
    backdrop-filter: saturate(1.4) blur(10px);
    -webkit-backdrop-filter: saturate(1.4) blur(10px);
    border-bottom: 1px solid var(--rule);
  }
  .brand {
    font-family: var(--sans);
    font-weight: 600;
    font-size: 0.9375rem;
    color: var(--text);
    text-decoration: none;
    letter-spacing: -0.01em;
  }
  html { scroll-padding-top: calc(var(--topbar-h) + 1.25rem); }

  .sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    z-index: 50;
    width: min(20rem, 86vw);
    transform: translateX(-100%);
    transition: transform 0.28s cubic-bezier(0.2, 0.7, 0.2, 1);
    box-shadow: none;
  }
  .sidebar.open {
    transform: none;
    box-shadow: 0 0 40px rgba(0, 0, 0, 0.18);
  }
  .scrim {
    position: fixed;
    inset: 0;
    z-index: 45;
    background: rgba(0, 0, 0, 0.32);
  }

  .content { padding: 2.5rem 1rem 4rem; }
  .hero { margin-bottom: 2.5rem; }
  .prose h2 { margin-top: 3.25rem; padding-top: 2rem; }

  figure.wide { width: 100%; margin-left: 0; transform: none; }
  pre { margin-left: -1rem; margin-right: -1rem; border-radius: 0; border-left: 0; border-right: 0; padding: 1rem; }
  .three-stage { aspect-ratio: 4 / 3; }
}

@media (prefers-reduced-motion: reduce) {
  .sidebar { transition: none; }
}

@media print {
  .sidebar, .topbar, .progress, .three-hud, .video-play, .share { display: none !important; }
  .layout { display: block; }
  .content { padding: 0; }
  figure.wide { width: 100%; margin-left: 0; transform: none; }
}
