:root {
  --te-bg: #fffff8;
  --te-surface: #f7f6ef;
  --te-ink: #111111;
  --te-muted: #57534e;
  --te-border: #e2dfd5;
  --te-accent: #9b2c2c;
  --te-font-serif: "Source Serif 4", Georgia, "Times New Roman", serif;
  --te-font-sans: "Inter", system-ui, -apple-system, sans-serif;
  --te-font-mono: "JetBrains Mono", ui-monospace, SFMono-Regular, Menlo, monospace;
  --te-body-font: var(--te-font-serif);
  --te-measure: 68ch;
  --te-sidenote-w: 18rem;
  --te-chart-1: #9b2c2c;
  --te-chart-2: #2b6cb0;
  --te-chart-3: #2f855a;
  --te-chart-4: #b7791f;
  --te-chart-5: #6b46c1;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --te-bg: #141413;
    --te-surface: #1e1e1c;
    --te-ink: #ecebe4;
    --te-muted: #a8a29e;
    --te-border: #302f2b;
    --te-accent: #e06c6c;
  }
}

:root[data-theme="dark"] {
  --te-bg: #141413;
  --te-surface: #1e1e1c;
  --te-ink: #ecebe4;
  --te-muted: #a8a29e;
  --te-border: #302f2b;
  --te-accent: #e06c6c;
}

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

html { font-size: 100%; scroll-behavior: smooth; }
@media (prefers-reduced-motion: reduce) { html { scroll-behavior: auto; } }

body {
  margin: 0;
  background: var(--te-bg);
  color: var(--te-ink);
  font-family: var(--te-body-font);
  font-size: 1.125rem;
  line-height: 1.65;
  text-rendering: optimizeLegibility;
}

a { color: var(--te-accent); text-decoration-thickness: 1px; text-underline-offset: 0.16em; }
a:hover { text-decoration-thickness: 2px; }
:focus-visible { outline: 2px solid var(--te-accent); outline-offset: 2px; }

.te-skip-link {
  position: absolute; inset-block-start: -4rem; inset-inline-start: 1rem;
  background: var(--te-accent); color: #fff; padding: 0.5rem 1rem; z-index: 100;
  font-family: var(--te-font-sans); font-size: 0.875rem; border-radius: 4px;
}
.te-skip-link:focus { inset-block-start: 1rem; }

.te-shell { max-width: 78rem; margin-inline: auto; padding-inline: 1.25rem; }

.te-header {
  border-block-end: 1px solid var(--te-border);
  padding-block: 1rem;
  font-family: var(--te-font-sans);
}
.te-header-inner { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 1rem; }
.te-brand { font-size: 1.15rem; font-weight: 700; color: var(--te-ink); text-decoration: none; letter-spacing: -0.01em; }
.te-nav { display: flex; flex-wrap: wrap; align-items: center; gap: 1.25rem; font-size: 0.9375rem; }
.te-nav a { color: var(--te-ink); text-decoration: none; min-height: 24px; display: inline-flex; align-items: center; }
.te-nav a:hover { color: var(--te-accent); }
.te-theme-toggle {
  background: transparent; border: 1px solid var(--te-border); color: var(--te-ink);
  font-family: var(--te-font-sans); font-size: 0.85rem; padding: 0.35rem 0.65rem; border-radius: 4px; cursor: pointer;
  min-height: 32px;
}

.te-main { padding-block: 2.5rem 4rem; }
.te-article { max-width: var(--te-measure); position: relative; }
.te-article h1 { font-size: clamp(1.9rem, 3.2vw, 2.55rem); line-height: 1.18; margin-block: 0.5rem 0.75rem; letter-spacing: -0.02em; }
.te-dek { font-size: 1.22rem; color: var(--te-muted); margin-block: 0 1.25rem; line-height: 1.45; }
.te-meta { font-family: var(--te-font-sans); font-size: 0.875rem; color: var(--te-muted); display: flex; flex-wrap: wrap; gap: 0.75rem 1.25rem; margin-block-end: 2rem; border-block-end: 1px solid var(--te-border); padding-block-end: 1rem; }
.te-breadcrumbs { font-family: var(--te-font-sans); font-size: 0.82rem; color: var(--te-muted); margin-block-end: 0.75rem; }
.te-breadcrumbs a { color: var(--te-muted); text-decoration: none; }

.te-prose h2, .te-prose h3, .te-prose h4 { margin-block: 2rem 0.75rem; line-height: 1.28; }
.te-anchor { margin-inline-start: 0.4rem; opacity: 0; font-size: 0.85em; text-decoration: none; color: var(--te-muted); }
h2:hover .te-anchor, h3:hover .te-anchor, h4:hover .te-anchor, .te-anchor:focus { opacity: 1; }

.te-math-block { display: flex; align-items: center; justify-content: space-between; gap: 1rem; margin-block: 1.4rem; overflow-x: auto; }
.te-math-formula { flex: 1 1 auto; text-align: center; }
.te-eq-num { font-family: var(--te-font-sans); font-size: 0.9rem; color: var(--te-muted); flex-shrink: 0; }

/* Tufte sidenotes */
.margin-toggle { display: none; }
label.sidenote-number { display: inline; cursor: pointer; }
.sidenote {
  float: inline-end; clear: inline-end;
  margin-inline-end: calc(-1 * var(--te-sidenote-w) - 2.5rem);
  width: var(--te-sidenote-w);
  margin-block: 0.25rem 0.75rem;
  font-size: 0.875rem; line-height: 1.45; color: var(--te-muted);
  vertical-align: baseline; position: relative;
}
@media (max-width: 1199px) {
  .sidenote { display: none; float: none; margin-inline-end: 0; width: 100%; background: var(--te-surface); padding: 0.65rem 0.85rem; border-radius: 4px; margin-block: 0.5rem; }
  .margin-toggle:checked + .sidenote { display: block; }
}

.te-toc { font-family: var(--te-font-sans); font-size: 0.875rem; background: var(--te-surface); padding: 1rem 1.25rem; border-radius: 6px; margin-block: 1.5rem; border: 1px solid var(--te-border); }
.te-toc ul { margin: 0.5rem 0 0; padding-inline-start: 1.2rem; }
.te-toc a { color: var(--te-ink); text-decoration: none; }
.te-toc a.is-active { color: var(--te-accent); font-weight: 600; }

.te-callout { border-inline-start: 3px solid var(--te-accent); background: var(--te-surface); padding: 0.9rem 1.15rem; margin-block: 1.5rem; border-radius: 0 6px 6px 0; }
.te-callout-title { font-family: var(--te-font-sans); font-weight: 600; margin-block-end: 0.35rem; }
.te-qed { float: inline-end; font-style: normal; }

.te-code-block { margin-block: 1.5rem; border: 1px solid var(--te-border); border-radius: 6px; overflow: hidden; background: var(--te-surface); }
.te-code-header { display: flex; justify-content: space-between; align-items: center; padding: 0.4rem 0.85rem; font-family: var(--te-font-mono); font-size: 0.78rem; border-block-end: 1px solid var(--te-border); color: var(--te-muted); }
.te-code-block pre { margin: 0; padding: 0.9rem 1rem; overflow-x: auto; font-family: var(--te-font-mono); font-size: 0.875rem; line-height: 1.5; }
.te-code-line { display: block; }
.te-code-line-hl { background: rgba(155, 44, 44, 0.08); }
.te-copy { background: transparent; border: 1px solid var(--te-border); color: var(--te-ink); font-size: 0.75rem; padding: 0.2rem 0.55rem; border-radius: 4px; cursor: pointer; }

.te-figure { margin: 1.75rem 0; }
.te-figure img, .te-figure video, .te-chart-svg { max-width: 100%; height: auto; display: block; border-radius: 4px; }
.te-align-wide { width: min(1100px, calc(100vw - 2.5rem)); }
.te-align-full { width: 100vw; margin-inline: calc(50% - 50vw); }
figcaption { font-family: var(--te-font-sans); font-size: 0.875rem; color: var(--te-muted); margin-block-start: 0.5rem; }
.te-fig-prefix { font-variant-caps: small-caps; color: var(--te-ink); }

.te-embed { display: flex; align-items: center; justify-content: space-between; padding: 1rem 1.25rem; border: 1px solid var(--te-border); border-radius: 6px; background: var(--te-surface); text-decoration: none; color: var(--te-ink); margin-block: 1.5rem; font-family: var(--te-font-sans); }

.te-paywall { border: 1px solid var(--te-accent); background: var(--te-surface); padding: 1.5rem; border-radius: 8px; margin-block: 2rem; text-align: center; font-family: var(--te-font-sans); }
.te-series-nav { background: var(--te-surface); border: 1px solid var(--te-border); border-radius: 6px; padding: 1rem 1.25rem; margin-block: 1.5rem; font-family: var(--te-font-sans); font-size: 0.92rem; }
.te-series-links { display: flex; justify-content: space-between; gap: 1rem; margin-block-start: 0.5rem; }

.te-post-list { list-style: none; padding: 0; margin: 0; }
.te-post-item { padding-block: 1.5rem; border-block-end: 1px solid var(--te-border); max-width: var(--te-measure); }
.te-post-item h2 { margin-block: 0.25rem 0.4rem; font-size: 1.45rem; }
.te-post-item h2 a { color: var(--te-ink); text-decoration: none; }
.te-post-item h2 a:hover { color: var(--te-accent); }

.te-form, .te-comment-form { display: grid; gap: 0.85rem; max-width: 36rem; font-family: var(--te-font-sans); }
.te-field { display: grid; gap: 0.3rem; }
.te-field label { font-size: 0.875rem; font-weight: 500; }
.te-field input, .te-field textarea, .te-field select { font: inherit; padding: 0.5rem 0.65rem; border: 1px solid var(--te-border); border-radius: 4px; background: var(--te-bg); color: var(--te-ink); }
.te-btn { display: inline-flex; align-items: center; justify-content: center; padding: 0.55rem 1.1rem; border-radius: 4px; border: 1px solid var(--te-border); font-family: var(--te-font-sans); font-size: 0.9rem; cursor: pointer; text-decoration: none; min-height: 38px; }
.te-btn-primary { background: var(--te-accent); border-color: var(--te-accent); color: #fff; }
.te-hp { position: absolute; left: -9999px; width: 1px; height: 1px; overflow: hidden; }

.te-comments { margin-block-start: 3rem; padding-block-start: 2rem; border-block-start: 1px solid var(--te-border); max-width: var(--te-measure); }
.te-comment { padding: 1rem 0; border-block-end: 1px solid var(--te-border); }
.te-comment[data-depth="1"] { margin-inline-start: 1.5rem; }
.te-comment[data-depth="2"] { margin-inline-start: 3rem; }
.te-comment[data-depth="3"] { margin-inline-start: 4.5rem; }
.te-comment-meta { font-family: var(--te-font-sans); font-size: 0.85rem; color: var(--te-muted); margin-block-end: 0.4rem; }

.te-footer { border-block-start: 1px solid var(--te-border); padding-block: 2rem; font-family: var(--te-font-sans); font-size: 0.875rem; color: var(--te-muted); }
.te-footer-inner { display: flex; flex-wrap: wrap; justify-content: space-between; gap: 1rem; }

@media print {
  .te-header, .te-footer, .te-comments, .te-copy, .te-theme-toggle, .te-paywall { display: none !important; }
  .sidenote { display: none !important; }
  .te-footnotes { display: block !important; }
  figure, .te-math-block, .te-callout { break-inside: avoid; }
  a[href^="http"]::after { content: " (" attr(href) ")"; font-size: 0.8em; }
}
