/* =========================================================================
   Palette
   ========================================================================= */

:root {
  color-scheme: light dark;
  --bg: #ffffff;
  --fg: #1a1a1a;
  --muted: #595959;
  --rule: #e5e5e5;
  --panel: #f5f5f5;
  --accent: #0b3d91;
  --code-bg: #f5f5f5;
}

:root[data-theme="dark"] {
  --bg: #0e1116;
  --fg: #e8e8e8;
  --muted: #9aa0a6;
  --rule: #2a2f37;
  --panel: #161b22;
  --accent: #7aa7e6;
  --code-bg: #161b22;
}

:root[data-theme="light"] {
  --bg: #ffffff;
  --fg: #1a1a1a;
  --muted: #595959;
  --rule: #e5e5e5;
  --panel: #f5f5f5;
  --accent: #0b3d91;
  --code-bg: #f5f5f5;
}

/* =========================================================================
   Reset + body
   ========================================================================= */

* { box-sizing: border-box; }
html { scroll-padding-top: 24px; }
html, body { background: var(--bg); color: var(--fg); }

body {
  font-family: Charter, "Bitstream Charter", "Sitka Text", Cambria, Georgia, serif;
  font-size: 18px;
  line-height: 1.65;
  max-width: 680px;
  margin: 0 auto;
  padding: 64px 24px 96px;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

/* =========================================================================
   Typography
   ========================================================================= */

h1, h2, h3 {
  font-family: inherit;
  font-weight: 600;
  letter-spacing: -0.01em;
  line-height: 1.25;
  color: var(--fg);
}
h1 { font-size: 2.4rem; margin: 0 0 0.4em; letter-spacing: -0.015em; }
h2 { font-size: 1.5rem; margin: 3em 0 0.6em; }
h3 { font-size: 1.2rem; margin: 2.2em 0 0.5em; }

p { margin: 0 0 1.1em; }
a { color: var(--accent); text-underline-offset: 3px; }
a:hover { text-decoration-thickness: 2px; }
strong { font-weight: 600; }
em { font-style: italic; }

/* Series tag / site tag (above H1) */
.series-tag, .site-tag {
  font-family: inherit;
  font-size: 12.5px;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
  margin-bottom: 14px;
}

/* Reading-time meta line (between H1 and dek) */
.meta {
  font-family: inherit;
  font-size: 12.5px;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
  margin: 0 0 16px;
}
.reading-time:empty::before { content: ""; }

/* Dek (subtitle) */
.dek {
  font-family: inherit;
  font-style: italic;
  font-weight: 400;
  font-size: 1.15rem;
  line-height: 1.5;
  color: var(--muted);
  margin: 0 0 2em;
}

/* =========================================================================
   Code
   ========================================================================= */

code, pre, kbd {
  font-family: ui-monospace, "SF Mono", "Cascadia Mono", Menlo, Consolas, "Liberation Mono", monospace;
}
:not(pre) > code {
  font-size: 0.88em;
  padding: 0.12em 0.35em;
  border-radius: 3px;
  background: var(--code-bg);
}
pre {
  font-size: 0.86em;
  line-height: 1.55;
  background: var(--code-bg);
  border: 1px solid var(--rule);
  border-radius: 4px;
  padding: 1rem 1.1rem;
  overflow-x: auto;
  margin: 1.4em 0;
  position: relative;
}
pre code { background: none; padding: 0; font-size: inherit; }

/* =========================================================================
   Blockquote, callout, figures
   ========================================================================= */

blockquote {
  font-style: italic;
  font-size: 1.02em;
  border-left: 2px solid var(--rule);
  padding: 0.15em 0 0.15em 1.2em;
  margin: 1.6em 0;
  color: var(--muted);
}

.callout {
  background: var(--panel);
  border: 1px solid var(--rule);
  border-radius: 4px;
  padding: 1em 1.2em;
  margin: 1.6em 0;
  font-size: 0.96em;
}
.callout-title {
  font-family: inherit;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
  margin-bottom: 0.6em;
}
.callout p:last-child { margin-bottom: 0; }
.callout pre {
  margin: 0.6em 0 0.4em;
  background: transparent;
  border-color: var(--rule);
  border-width: 1px 0 0 0;
  border-radius: 0;
  padding: 0.6em 0 0;
}

/* Figures: forced light-on-white so the embedded SVG diagrams (which use
   warm-paper hex colors) render consistently in both themes — like printed
   figures in an academic paper. */
figure {
  margin: 2.4em 0;
  background: #ffffff;
  color: #1a1a1a;
  border: 1px solid #e5e5e5;
  border-radius: 4px;
  padding: 12px 16px;
}
figure svg { display: block; width: 100%; height: auto; max-width: 100%; }
figcaption {
  font-family: Charter, "Bitstream Charter", "Sitka Text", Cambria, Georgia, serif;
  font-size: 0.86em;
  font-style: italic;
  color: #595959;
  text-align: center;
  margin-top: 1em;
  line-height: 1.5;
}
/* Inline-SVG class hooks carried over from old design */
.diagram-text { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif; }
.diagram-mono { font-family: ui-monospace, "SF Mono", "Cascadia Mono", Menlo, Consolas, monospace; }

/* Horizontal rule */
hr { border: none; border-top: 1px solid var(--rule); margin: 3em 0; }

/* =========================================================================
   Table of contents (per-part)
   ========================================================================= */

.toc { margin: 2.4em 0 3em; }
.toc-heading {
  font-family: inherit;
  font-size: 12.5px;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
  margin: 0 0 0.8em;
  padding: 0;
  border: none;
}
.toc ol {
  list-style: decimal;
  padding-left: 1.6em;
  margin: 0;
  font-size: 0.96em;
}
.toc li { margin: 0.25em 0; color: var(--muted); }
.toc a {
  color: var(--fg);
  text-decoration: none;
  border-bottom: 1px solid transparent;
  transition: color 0.15s, border-color 0.15s;
}
.toc a:hover { color: var(--accent); border-bottom-color: var(--accent); }

/* =========================================================================
   Next-link (replaces ornamental .next-up card)
   ========================================================================= */

.next-link {
  margin-top: 3em;
  font-size: 1.02em;
  color: var(--muted);
}
.next-link a { font-weight: 500; }

/* =========================================================================
   Part nav row (prev/All parts)
   ========================================================================= */

.part-nav {
  text-align: center;
  margin: 2.5em 0 0;
  padding-top: 1.6em;
  border-top: 1px solid var(--rule);
  font-family: inherit;
  font-size: 0.92em;
  color: var(--muted);
}
.part-nav a { text-decoration: none; color: var(--muted); transition: color 0.18s ease; }
.part-nav a:hover { color: var(--accent); }
.part-nav .sep { margin: 0 0.7em; opacity: 0.55; }

/* =========================================================================
   Footer colophon
   ========================================================================= */

footer {
  margin-top: 4em;
  padding-top: 1.5em;
  border-top: 1px solid var(--rule);
  font-size: 0.86em;
  color: var(--muted);
}

/* =========================================================================
   Index — part cards
   ========================================================================= */

.parts-heading {
  font-family: inherit;
  font-size: 12.5px;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
  margin: 3em 0 0;
  padding-top: 2em;
  border-top: 1px solid var(--rule);
}
a.part-card {
  display: block;
  text-decoration: none;
  color: inherit;
  padding: 1.4em 0;
  border-bottom: 1px solid var(--rule);
  transition: color 0.15s;
}
a.part-card:hover h2 { color: var(--accent); }
.part-card-label {
  font-family: inherit;
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
  margin-bottom: 0.3em;
}
.part-card h2 {
  margin: 0.1em 0 0.4em;
  padding: 0;
  border: none;
  font-size: 1.3rem;
  font-weight: 600;
  transition: color 0.15s;
}
.part-card p {
  margin: 0;
  color: var(--muted);
  font-size: 0.98em;
  line-height: 1.55;
}
.part-card-meta {
  font-family: inherit;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
  margin-left: 0.7em;
  opacity: 0.8;
}

/* =========================================================================
   Reading progress bar (fixed top)
   ========================================================================= */

.progress-bar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: 2px;
  background: var(--accent);
  transform: scaleX(0);
  transform-origin: left;
  z-index: 50;
  pointer-events: none;
}

/* =========================================================================
   Copy-code button on <pre>
   ========================================================================= */

.copy-btn {
  position: absolute;
  top: 6px;
  right: 6px;
  padding: 3px 8px;
  font-size: 11px;
  font-family: inherit;
  font-weight: 500;
  background: var(--bg);
  border: 1px solid var(--rule);
  color: var(--muted);
  border-radius: 3px;
  cursor: pointer;
  opacity: 0;
  transition: opacity 0.15s, color 0.15s, border-color 0.15s;
}
pre:hover .copy-btn,
pre:focus-within .copy-btn,
.copy-btn:focus { opacity: 1; }
.copy-btn:hover { color: var(--accent); border-color: var(--accent); }
@media (hover: none) { .copy-btn { opacity: 0.7; } }

/* =========================================================================
   Theme toggle (carry-over button, restyled neutral)
   ========================================================================= */

.theme-toggle {
  position: fixed;
  top: 18px;
  right: 18px;
  background: var(--bg);
  border: 1px solid var(--rule);
  color: var(--muted);
  border-radius: 50%;
  width: 36px;
  height: 36px;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: color 0.18s ease, border-color 0.18s ease, background 0.18s ease;
  z-index: 100;
}
.theme-toggle:hover { color: var(--accent); border-color: var(--accent); }
.theme-toggle:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.theme-toggle svg { width: 16px; height: 16px; display: block; }
.theme-toggle .icon-sun { display: none; }
.theme-toggle .icon-moon { display: block; }
:root[data-theme="dark"] .theme-toggle .icon-sun { display: block; }
:root[data-theme="dark"] .theme-toggle .icon-moon { display: none; }

/* =========================================================================
   Small viewports
   ========================================================================= */

@media (max-width: 480px) {
  body { font-size: 17px; padding: 56px 18px 80px; }
  h1 { font-size: 2rem; }
  .theme-toggle { top: 12px; right: 12px; width: 32px; height: 32px; }
  .theme-toggle svg { width: 14px; height: 14px; }
}
