* {
  box-sizing: border-box;
}

:root {
  --bg: #fafaf9;
  --bg-alt: #f5f5f4;
  --fg: #1c1917;
  --fg-muted: #78716c;
  --border: #e7e5e4;
  --link: #b45309;
  --link-hover: #92400e;
  --code-bg: #f5f5f4;
  --toc-width: 220px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1c1917;
    --bg-alt: #292524;
    --fg: #e7e5e4;
    --fg-muted: #a8a29e;
    --border: #44403c;
    --link: #fbbf24;
    --link-hover: #fcd34d;
    --code-bg: #292524;
  }
}

body {
  font-family: Charter, "Bitstream Charter", "Sitka Text", Cambria, serif;
  font-size: 18px;
  line-height: 1.7;
  color: var(--fg);
  margin: 0;
  padding: 0;
  background: linear-gradient(180deg, var(--bg) 0%, var(--bg-alt) 100%);
  background-attachment: fixed;
  min-height: 100vh;
  -webkit-text-size-adjust: 100%;
  -webkit-font-smoothing: antialiased;
}

/* Drop cap */
#content > p:first-of-type::first-letter {
  float: left;
  font-size: 3.2em;
  line-height: 0.8;
  padding-right: 10px;
  padding-top: 6px;
  font-weight: 600;
  color: var(--link);
}

/* Tagline */
.tagline {
  font-size: 14px;
  color: var(--fg-muted);
  font-weight: 400;
  margin-left: 12px;
}

/* TOC sidebar */
#table-of-contents {
  position: fixed;
  left: 0;
  top: 73px;
  width: var(--toc-width);
  height: calc(100vh - 73px);
  overflow-y: auto;
  padding: 24px 20px;
  background: var(--bg);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
  font-size: 13px;
}

#table-of-contents h2 {
  font-size: 11px;
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--fg-muted);
  margin: 0 0 12px 0;
}

#table-of-contents ul {
  list-style: none;
  padding: 0;
  margin: 0;
}

#table-of-contents li {
  margin: 0;
  padding: 6px 0;
  border-bottom: 1px solid var(--border);
}

#table-of-contents li:last-child {
  border-bottom: none;
}

#table-of-contents a {
  color: var(--fg-muted);
  text-decoration: none;
  transition: color 0.15s;
}

#table-of-contents a:hover {
  color: var(--fg);
}

/* Header */
#preamble {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 10;
  background: var(--bg);
  height: 72px;
  padding: 0 48px;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
  font-size: 14px;
  display: flex;
  align-items: center;
  border-bottom: 1px solid var(--border);
}

#preamble a {
  color: var(--fg-muted);
  text-decoration: none;
}

#preamble a:hover {
  color: var(--fg);
}

.site-name {
  font-weight: 600;
  font-size: 20px;
  color: var(--fg);
  text-decoration: none;
}

.site-name:hover {
  color: var(--fg-muted);
}

/* Dark mode toggle */
.theme-toggle {
  margin-left: auto;
  background: none;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 6px 10px;
  cursor: pointer;
  color: var(--fg-muted);
  font-size: 14px;
  transition: color 0.15s, border-color 0.15s;
}

.theme-toggle:hover {
  color: var(--fg);
  border-color: var(--fg-muted);
}

/* Override system preference when toggled */
html.light {
  --bg: #fafaf9;
  --bg-alt: #f5f5f4;
  --fg: #1c1917;
  --fg-muted: #78716c;
  --border: #e7e5e4;
  --link: #b45309;
  --link-hover: #92400e;
  --code-bg: #f5f5f4;
}

html.dark {
  --bg: #1c1917;
  --bg-alt: #292524;
  --fg: #e7e5e4;
  --fg-muted: #a8a29e;
  --border: #44403c;
  --link: #fbbf24;
  --link-hover: #fcd34d;
  --code-bg: #292524;
}

/* Main content */
#content {
  margin-left: var(--toc-width);
  margin-top: 73px;
  max-width: 700px;
  padding: 48px 48px;
  background: var(--bg);
}

/* Typography */
h1, h2, h3, h4 {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
  font-weight: 600;
  letter-spacing: -0.02em;
  line-height: 1.3;
}

h1 {
  font-size: 2em;
  margin: 0 0 24px 0;
}

h2 {
  font-size: 1.4em;
  margin: 48px 0 20px 0;
  padding-top: 24px;
  border-top: 1px solid var(--border);
}

h1 + p + h2,
h1 + h2,
#content > h2:first-child {
  margin-top: 0;
  padding-top: 0;
  border-top: none;
}

h3 {
  font-size: 1.15em;
  margin: 32px 0 16px 0;
}

h4 {
  font-size: 1em;
  margin: 24px 0 12px 0;
}

/* Anchor links on headings */
h2, h3, h4 {
    position: relative;
    scroll-margin-top: 90px;
}

.heading-anchor {
  position: absolute;
  left: -1.5em;
  opacity: 0;
  text-decoration: none;
  color: var(--fg-muted);
  font-weight: 400;
  transition: opacity 0.15s;
}

h2:hover .heading-anchor,
h3:hover .heading-anchor,
h4:hover .heading-anchor {
  opacity: 1;
}

p {
  margin: 0 0 20px 0;
}

/* Links */
a {
  color: var(--link);
  text-decoration: underline;
  text-decoration-thickness: 1px;
  text-underline-offset: 2px;
  transition: color 0.15s;
}

a:hover {
  color: var(--link-hover);
}

/* Index page list */
#content > ul {
  list-style: none;
  padding-left: 0;
}

#content > ul li {
  margin: 0;
  padding: 0;
  border-bottom: 1px solid var(--border);
}

#content > ul li a {
  display: block;
  padding: 14px 0;
  text-decoration: none;
  transition: padding-left 0.15s, color 0.15s;
}

#content > ul li a:hover {
  padding-left: 8px;
  color: var(--link-hover);
}

#content > ul li .description {
  display: block;
  font-size: 0.9em;
  color: var(--fg-muted);
  margin-top: 4px;
  padding-bottom: 14px;
}

#content > ul li:last-child {
  border-bottom: none;
}

/* Lists */
ul, ol {
  margin: 0 0 20px 0;
  padding-left: 24px;
}

li {
  margin: 8px 0;
}

li::marker {
  color: var(--fg-muted);
}

/* Code */
pre, code {
  font-family: "JetBrains Mono", "Fira Code", "SF Mono", Consolas, monospace;
  font-size: 0.85em;
}

code {
  background: var(--code-bg);
  padding: 2px 6px;
  border-radius: 4px;
}

pre {
  background: var(--code-bg);
  padding: 20px 24px;
  overflow-x: auto;
  border-radius: 8px;
  line-height: 1.5;
  margin: 0 0 24px 0;
}

pre code {
  background: none;
  padding: 0;
  border-radius: 0;
}

/* Blockquotes */
blockquote {
  margin: 0 0 24px 0;
  padding: 0 0 0 24px;
  border-left: 4px solid var(--border);
  color: var(--fg-muted);
  font-style: italic;
}

blockquote p:last-child {
  margin-bottom: 0;
}

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

/* Tables */
table {
  width: 100%;
  border-collapse: collapse;
  margin: 0 0 24px 0;
  font-size: 0.9em;
}

th, td {
  padding: 12px 16px;
  text-align: left;
  border-bottom: 1px solid var(--border);
}

th {
  font-weight: 600;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
}

/* Images */
img {
  max-width: 100%;
  height: auto;
  border-radius: 8px;
}

/* Footer */
#postamble {
  margin-left: var(--toc-width);
  max-width: 700px;
  padding: 24px 48px;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
  font-size: 14px;
  border-top: 1px solid var(--border);
}

#postamble a {
  color: var(--fg-muted);
  text-decoration: none;
}

#postamble a:hover {
  color: var(--fg);
}

/* No TOC pages - center content (desktop only) */
/* @media (min-width: 901px) { */
/*   body:not(:has(#table-of-contents)) #content, */
/*   body:not(:has(#table-of-contents)) #preamble, */
/*   body:not(:has(#table-of-contents)) #postamble { */
/*     margin-left: auto; */
/*     margin-right: auto; */
/*   } */
/* } */

@media (min-width: 901px) {
  body:not(:has(#table-of-contents)) #content,
  body:not(:has(#table-of-contents)) #postamble {
    margin-left: var(--toc-width);
  }
}

/* Mobile */
@media (max-width: 900px) {
  body {
    font-size: 17px;
  }

  #table-of-contents {
    display: none;
  }

  #preamble {
    position: sticky;
    height: auto;
    padding: 16px 20px;
  }

  .site-name {
    width: auto;
  }

  #content,
  #postamble {
    margin-left: 0;
    margin-top: 0;
    max-width: 100%;
    padding-left: 20px;
    padding-right: 20px;
    text-align: left;
  }

  #content {
    padding-top: 32px;
    padding-bottom: 32px;
  }

  #postamble {
    padding-top: 20px;
    padding-bottom: 20px;
    margin-left: 0;
    max-width: 100%;
  }

  h1 {
    font-size: 1.7em;
  }

  h2 {
    font-size: 1.25em;
    margin-top: 36px;
    padding-top: 20px;
  }

  pre {
    padding: 16px;
    border-radius: 6px;
    font-size: 0.8em;
  }
}
