/* ── Reset ─────────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body { margin: 0; }
img, svg { display: block; max-width: 100%; }
button { cursor: pointer; font: inherit; }
details > summary { list-style: none; }
details > summary::-webkit-details-marker { display: none; }

/* ── Design tokens ─────────────────────────────────────────────── */
:root {
  /* Fonts */
  --font-serif: "Lora", Georgia, "Times New Roman", serif;
  --font-ui:    system-ui, -apple-system, BlinkMacSystemFont, sans-serif;
  --font-mono:  "JetBrains Mono", "Fira Mono", ui-monospace, "Courier New", monospace;

  /* Layout */
  --sidebar-width:     260px;
  --content-max-width: 700px;
  --header-height:     3rem;

  /* Colors — light */
  --bg:            #f8f7f4;
  --surface:       #f0ede7;
  --surface-2:     #e4dfd6;
  --border:        #d4cfc6;
  --border-strong: #b5afa6;
  --text:          #1c1917;
  --text-muted:    #78716c;
  --accent:        #1e3a5f;
  --accent-mid:    #2c5282;
  --accent-light:  #eaf0f8;

  /* Milestones */
  --color-arch: #2c4f7c;
  --color-sys:  #7a3e1a;

  /* Viz */
  --viz-bg:     var(--surface);
  --viz-border: var(--border);

  /* Tooltip */
  --tooltip-bg:   rgba(18, 16, 14, 0.92);
  --tooltip-text: #f0ede7;
}

[data-theme="dark"] {
  --bg:            #1c1917;
  --surface:       #242220;
  --surface-2:     #2e2b27;
  --border:        #403d38;
  --border-strong: #57534e;
  --text:          #e7e5e0;
  --text-muted:    #a8a29e;
  --accent:        #90b8e0;
  --accent-mid:    #7aa8d8;
  --accent-light:  #1e2c3e;

  --color-arch: #7aa8d8;
  --color-sys:  #c49a6a;

  --viz-bg:     var(--surface);
  --viz-border: var(--border);
}

/* ── Color scheme ──────────────────────────────────────────────── */
html { color-scheme: light dark; }

/* ── Base typography ───────────────────────────────────────────── */
body {
  font-family: var(--font-serif);
  font-size: 17px;
  line-height: 1.75;
  color: var(--text);
  background: var(--bg);
}

h1, h2, h3, h4, h5, h6 {
  font-family: var(--font-serif);
  font-weight: 600;
  line-height: 1.3;
  color: var(--text);
  margin: 0 0 0.4em;
}

h1 { font-size: 2rem; }
h2 { font-size: 1.45rem; }
h3 { font-size: 1.15rem; }
h4 { font-size: 1rem; }

p { margin: 0; }

a {
  color: var(--accent-mid);
  text-decoration: underline;
  text-underline-offset: 3px;
  text-decoration-thickness: 1px;
}

a:hover { text-decoration-thickness: 2px; }

strong { font-weight: 700; }
em { font-style: italic; }

ul, ol { margin: 0; padding-left: 1.75em; }
li + li { margin-top: 0.35em; }

code, kbd, samp {
  font-family: var(--font-mono);
  font-size: 0.875em;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 3px;
  padding: 0.1em 0.35em;
}

pre {
  font-family: var(--font-mono);
  font-size: 0.85rem;
  line-height: 1.6;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 1.25rem 1.5rem;
  overflow-x: auto;
  margin: 0;
}

pre code {
  background: none;
  border: none;
  padding: 0;
  font-size: inherit;
}

blockquote {
  border-left: 3px solid var(--border-strong);
  margin: 0;
  padding: 0.25rem 0 0.25rem 1.5rem;
  color: var(--text-muted);
  font-style: italic;
}

hr {
  border: none;
  border-top: 1px solid var(--border);
  margin: 2rem 0;
}

table {
  border-collapse: collapse;
  width: 100%;
  font-size: 0.9rem;
}

thead { border-bottom: 2px solid var(--border-strong); }
th { font-weight: 600; padding: 0.5rem 0.75rem; text-align: left; }
td { border-bottom: 1px solid var(--border); padding: 0.45rem 0.75rem; }

/* ── Button (used for CTA and theme toggle) ────────────────────── */
[role="button"], button {
  display: inline-block;
  padding: 0.55rem 1.25rem;
  background: var(--accent);
  color: #fff;
  border: 1px solid var(--accent);
  border-radius: 3px;
  font-family: var(--font-ui);
  font-size: 0.875rem;
  font-weight: 600;
  text-decoration: none;
  letter-spacing: 0.02em;
}

[role="button"]:hover { background: var(--accent-mid); border-color: var(--accent-mid); text-decoration: none; color: #fff; }

/* ── Site header ───────────────────────────────────────────────── */
.site-header {
  position: sticky;
  top: 0;
  z-index: 100;
  height: var(--header-height);
  background: var(--bg);
  border-bottom: 1px solid var(--border);
}

.site-header nav {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 100%;
  padding: 0 1.5rem;
}

.site-title {
  font-family: var(--font-ui);
  font-weight: 700;
  font-size: 0.8rem;
  letter-spacing: 0.09em;
  text-transform: uppercase;
  text-decoration: none;
  color: var(--text);
}

#theme-toggle {
  background: none;
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 0.2rem 0.5rem;
  font-size: 1rem;
  line-height: 1;
  color: var(--text);
}

/* ── Page shell ────────────────────────────────────────────────── */
.page-shell {
  display: grid;
  grid-template-columns: var(--sidebar-width) 1fr;
  min-height: calc(100vh - var(--header-height));
}

/* Prevent grid blowout: let 1fr tracks shrink below content min-width */
.page-shell > * { min-width: 0; }

/* ── Sidebar ───────────────────────────────────────────────────── */
.sidebar {
  position: sticky;
  top: var(--header-height);
  height: calc(100vh - var(--header-height));
  overflow-y: auto;
  border-right: 1px solid var(--border);
  background: var(--bg);
}

.sidebar-collapse-btn { display: none; }

.sidebar-inner { padding: 0.75rem 0; }

.sidebar details { border: none; background: none; }

.sidebar details summary {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.4rem 1rem;
  font-family: var(--font-ui);
  font-size: 0.67rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.09em;
  cursor: pointer;
  color: var(--text-muted);
}

.sidebar details[open] summary { color: var(--text); }

.milestone-label {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 1.25rem;
  height: 1.25rem;
  border-radius: 2px;
  font-family: var(--font-ui);
  font-size: 0.65rem;
  font-weight: 700;
  background: var(--color-arch);
  color: #fff;
  flex-shrink: 0;
}

.sidebar details:nth-child(n+7) .milestone-label { background: var(--color-sys); }

.sidebar ul { list-style: none; margin: 0; padding: 0; }

.sidebar-step {
  display: flex;
  align-items: baseline;
  gap: 0.4rem;
  padding: 0.25rem 1rem 0.25rem 1.5rem;
  font-family: var(--font-ui);
  font-size: 0.73rem;
  text-decoration: none;
  color: var(--text-muted);
  line-height: 1.4;
}

.sidebar-step:hover { background: var(--surface); color: var(--text); }

.sidebar-step.disabled {
  opacity: 0.35;
  cursor: default;
  pointer-events: none;
}

.sidebar-step.active {
  background: var(--accent-light);
  color: var(--accent);
  font-weight: 600;
}

.sidebar-step.visited .step-num::after {
  content: ' ✓';
  font-size: 0.65rem;
  opacity: 0.5;
}

.step-num {
  font-family: var(--font-mono);
  font-size: 0.65rem;
  opacity: 0.4;
  flex-shrink: 0;
}

/* ── Step content ──────────────────────────────────────────────── */
.step-content {
  max-width: 920px;
  padding: 3rem 3rem 5rem;
}

.step-header {
  margin-bottom: 2.5rem;
  padding-bottom: 1.25rem;
  border-bottom: 1px solid var(--border);
}

.step-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-bottom: 0.75rem;
  font-family: var(--font-ui);
  font-size: 0.72rem;
}

.milestone-badge {
  padding: 0.15rem 0.5rem;
  border-radius: 2px;
  background: var(--color-arch);
  color: #fff;
  font-weight: 700;
  font-family: var(--font-ui);
  letter-spacing: 0.03em;
}

.phase-label, .step-number { color: var(--text-muted); }

.concept-intro {
  margin: 0.5rem 0 0;
  font-size: 0.95rem;
  color: var(--text-muted);
  font-style: italic;
}

/* ── Prose ─────────────────────────────────────────────────────── */
.prose { max-width: 600px; }
.prose > * + * { margin-top: 1.4rem; }

/* ── Sidenotes ─────────────────────────────────────────────────── */
.sidenote-marker {
  font-size: 0.72em;
  vertical-align: super;
  color: var(--accent-mid);
  font-family: var(--font-ui);
  font-weight: 600;
  line-height: 1;
  cursor: default;
  user-select: none;
}

/* Wide screens: float into right margin */
.sidenote {
  float: right;
  clear: right;
  width: 200px;
  margin-right: -240px;
  padding-left: 0.75rem;
  border-left: 2px solid var(--border);
  font-size: 0.78rem;
  font-family: var(--font-ui);
  color: var(--text-muted);
  line-height: 1.55;
  margin-top: 0.15rem;
}

/* Narrow screens: hidden by default, toggled inline on tap */
@media (max-width: 1100px) {
  .sidenote {
    float: none;
    display: none;
    width: auto;
    margin-right: 0;
    margin-top: 0.4rem;
    border-left: none;
    border-top: 1px solid var(--border);
    padding: 0.4rem 0 0.2rem;
  }

  .sidenote.is-open { display: block; }

  .sidenote-marker {
    cursor: pointer;
    text-decoration: underline;
    text-decoration-style: dotted;
    text-decoration-color: var(--accent-mid);
  }
}

/* ── Syntax highlighting (highlight.js) ────────────────────────── */
:root {
  --hl-keyword:  #6d28d9;
  --hl-string:   #b45309;
  --hl-comment:  var(--text-muted);
  --hl-number:   #0f766e;
  --hl-built_in: #1e40af;
  --hl-title:    #1e40af;
}

[data-theme="dark"] {
  --hl-keyword:  #a78bfa;
  --hl-string:   #fbbf24;
  --hl-number:   #34d399;
  --hl-built_in: #60a5fa;
  --hl-title:    #60a5fa;
}

.hljs-keyword, .hljs-operator { color: var(--hl-keyword); }
.hljs-string, .hljs-attr      { color: var(--hl-string); }
.hljs-comment                 { color: var(--hl-comment); font-style: italic; }
.hljs-number, .hljs-literal   { color: var(--hl-number); }
.hljs-built_in                { color: var(--hl-built_in); }
.hljs-title.function_         { color: var(--hl-title); font-weight: 600; }
.hljs-params                  { color: var(--text); }
.hljs-punctuation             { color: var(--text-muted); }

/* ── Visualizations ────────────────────────────────────────────── */
.viz-wrapper { margin: 2.5rem 0; }

.viz-container {
  position: relative;
  background: var(--viz-bg);
  border: 1px solid var(--viz-border);
  border-radius: 4px;
  padding: 1.5rem;
  min-height: 80px;
}

.viz-loading, .viz-error {
  text-align: center;
  font-family: var(--font-ui);
  font-size: 0.85rem;
  color: var(--text-muted);
  padding: 2rem;
}

.viz-error { color: #b91c1c; }

.viz-tooltip {
  position: absolute;
  background: var(--tooltip-bg);
  color: var(--tooltip-text);
  padding: 0.35rem 0.6rem;
  border-radius: 3px;
  font-family: var(--font-ui);
  font-size: 0.75rem;
  line-height: 1.4;
  pointer-events: none;
  z-index: 10;
  max-width: 200px;
  white-space: nowrap;
}

/* ── Step navigation ───────────────────────────────────────────── */
.step-nav {
  display: flex;
  justify-content: space-between;
  margin-top: 3rem;
  padding-top: 1.5rem;
  border-top: 1px solid var(--border);
  font-family: var(--font-ui);
  font-size: 0.82rem;
}

.nav-prev, .nav-next {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  text-decoration: none;
  color: var(--accent-mid);
  max-width: 45%;
}

.nav-next { margin-left: auto; text-align: right; }
.nav-prev span, .nav-next span { color: var(--text-muted); }

/* ── Progress bar ──────────────────────────────────────────────── */
.progress-bar {
  width: 100%;
  height: 4px;
  appearance: none;
  border: none;
  background: var(--surface-2);
  border-radius: 2px;
}

.progress-bar::-webkit-progress-bar { background: var(--surface-2); border-radius: 2px; }
.progress-bar::-webkit-progress-value { background: var(--accent-mid); border-radius: 2px; }
.progress-bar::-moz-progress-bar { background: var(--accent-mid); border-radius: 2px; }

/* ── Responsive ────────────────────────────────────────────────── */
@media (max-width: 768px) {
  .page-shell { grid-template-columns: 1fr; }

  .sidebar {
    position: static;
    height: auto;
    border-right: none;
    border-bottom: 1px solid var(--border);
  }

  .sidebar-collapse-btn {
    display: flex;
    align-items: center;
    justify-content: space-between;
    width: 100%;
    padding: 0.65rem 1rem;
    background: none;
    border: none;
    border-radius: 0;
    font-family: var(--font-ui);
    font-size: 0.78rem;
    font-weight: 600;
    color: var(--text-muted);
  }

  .sidebar-collapse-btn:hover { color: var(--text); background: var(--surface); }

  .sidebar-chevron {
    font-size: 0.9rem;
    transition: transform 0.15s ease;
    display: inline-block;
  }

  .sidebar.is-open .sidebar-chevron { transform: rotate(180deg); }

  .sidebar-inner { display: none; }
  .sidebar.is-open .sidebar-inner { display: block; }

  .step-content { padding: 1.5rem 1.25rem 3rem; }
}
