/* ---------- Colour tokens ---------- */

:root {
  --bg: #fbfdfc;
  --fg: #1b2621;
  --muted: #5a6b63;
  --border: #d5e2db;
  --surface: #eef5f1;
  --code-bg: #f1f6f3;
  --accent: #047857;
  --accent-hover: #065f46;
  --accent-soft: #d1fae5;
  --banner-bg: linear-gradient(90deg, #064e3b, #0b6b47);
  --banner-fg: #ecfdf5;

  --syn-keyword: #9a3412;
  --syn-string: #047857;
  --syn-number: #1d4ed8;
  --syn-comment: #6b7c74;
  --syn-title: #7c3aed;
  --syn-type: #b45309;

  --banner-h: 3.25rem;
  --sidebar-w: 17.5rem;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0d1512;
    --fg: #d9e4de;
    --muted: #8fa39a;
    --border: #23332c;
    --surface: #14201b;
    --code-bg: #111c18;
    --accent: #34d399;
    --accent-hover: #6ee7b7;
    --accent-soft: #0b3a2a;
    --banner-bg: linear-gradient(90deg, #022c22, #064e3b);
    --banner-fg: #d1fae5;

    --syn-keyword: #fb923c;
    --syn-string: #6ee7b7;
    --syn-number: #93c5fd;
    --syn-comment: #7d9188;
    --syn-title: #c4b5fd;
    --syn-type: #fcd34d;
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --bg: #0d1512;
  --fg: #d9e4de;
  --muted: #8fa39a;
  --border: #23332c;
  --surface: #14201b;
  --code-bg: #111c18;
  --accent: #34d399;
  --accent-hover: #6ee7b7;
  --accent-soft: #0b3a2a;
  --banner-bg: linear-gradient(90deg, #022c22, #064e3b);
  --banner-fg: #d1fae5;

  --syn-keyword: #fb923c;
  --syn-string: #6ee7b7;
  --syn-number: #93c5fd;
  --syn-comment: #7d9188;
  --syn-title: #c4b5fd;
  --syn-type: #fcd34d;
  color-scheme: dark;
}

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

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

html {
  -webkit-text-size-adjust: 100%;
  scroll-padding-top: calc(var(--banner-h) + 1rem);
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--fg);
  font: 1.0625rem/1.65 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
}

a {
  color: var(--accent);
  text-underline-offset: 0.15em;
}

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

/* ---------- Banner ---------- */

.banner {
  position: sticky;
  top: 0;
  z-index: 30;
  display: flex;
  align-items: center;
  gap: 0.75rem;
  height: var(--banner-h);
  padding: 0 1rem;
  background: var(--banner-bg);
  color: var(--banner-fg);
  box-shadow: 0 1px 4px rgb(0 0 0 / 0.2);
}

.site-title {
  flex: 1;
  color: inherit;
  font-weight: 650;
  font-size: 1.1rem;
  letter-spacing: 0.01em;
  text-decoration: none;
}

.site-title:hover {
  color: inherit;
}

.menu-button,
.theme-button {
  display: grid;
  place-items: center;
  width: 2.25rem;
  height: 2.25rem;
  border: 1px solid rgb(255 255 255 / 0.25);
  border-radius: 0.5rem;
  background: rgb(255 255 255 / 0.08);
  color: inherit;
  font-size: 1.15rem;
  line-height: 1;
  cursor: pointer;
}

.menu-button:hover,
.theme-button:hover {
  background: rgb(255 255 255 / 0.18);
}

.menu-button {
  display: none;
}

.nav-toggle {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}

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

.shell {
  display: flex;
  align-items: flex-start;
}

.sidebar {
  position: sticky;
  top: var(--banner-h);
  flex: 0 0 var(--sidebar-w);
  height: calc(100vh - var(--banner-h));
  overflow-y: auto;
  padding: 1.25rem 1rem 3rem;
  border-right: 1px solid var(--border);
  background: var(--bg);
  font-size: 0.925rem;
  line-height: 1.4;
}

.sidebar section + section {
  margin-top: 1.25rem;
}

.sidebar h2 {
  margin: 0 0 0.4rem 0.6rem;
  color: var(--muted);
  font-size: 0.75rem;
  font-weight: 650;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}

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

.sidebar a {
  display: block;
  padding: 0.35rem 0.6rem;
  border-radius: 0.4rem;
  color: var(--fg);
  text-decoration: none;
}

.sidebar a:hover {
  background: var(--surface);
  color: var(--accent);
}

.sidebar .current > a {
  background: var(--accent-soft);
  color: var(--accent);
  font-weight: 600;
}

.sidebar .toc {
  margin: 0.25rem 0 0.5rem 0.6rem;
  padding-left: 0.5rem;
  border-left: 2px solid var(--border);
  font-size: 0.85rem;
}

.sidebar .toc a {
  padding: 0.2rem 0.5rem;
  color: var(--muted);
}

main {
  flex: 1;
  min-width: 0;
  padding: 2rem clamp(1rem, 4vw, 3rem) 4rem;
}

.content {
  max-width: 50rem;
  margin: 0 auto;
}

.scrim {
  display: none;
}

/* ---------- Small screens: slide-out sidebar ---------- */

@media (max-width: 60rem) {
  .menu-button {
    display: grid;
  }

  .sidebar {
    position: fixed;
    top: var(--banner-h);
    left: 0;
    bottom: 0;
    z-index: 20;
    width: min(var(--sidebar-w), 85vw);
    height: auto;
    transform: translateX(-100%);
    transition: transform 0.2s ease;
    box-shadow: 2px 0 12px rgb(0 0 0 / 0.25);
  }

  .nav-toggle:checked ~ .shell .sidebar {
    transform: none;
  }

  .nav-toggle:checked ~ .shell .scrim {
    display: block;
    position: fixed;
    inset: var(--banner-h) 0 0 0;
    z-index: 10;
    background: rgb(0 0 0 / 0.4);
  }

  .nav-toggle:focus-visible ~ .banner .menu-button {
    outline: 2px solid var(--banner-fg);
  }

  main {
    padding-top: 1.25rem;
  }
}

/* ---------- Article typography ---------- */

.content h1,
.content h2,
.content h3,
.content h4 {
  line-height: 1.25;
  font-weight: 680;
}

.content h1 {
  margin: 0.5rem 0 1.25rem;
  font-size: clamp(1.7rem, 4.5vw, 2.25rem);
  letter-spacing: -0.01em;
}

.content h1:not(:first-child) {
  margin-top: 3rem;
  padding-top: 1.5rem;
  border-top: 2px solid var(--accent);
}

.content h2 {
  margin: 2.5rem 0 0.9rem;
  padding-bottom: 0.3rem;
  border-bottom: 1px solid var(--border);
  font-size: clamp(1.35rem, 3.5vw, 1.6rem);
}

.content h3 {
  margin: 2rem 0 0.75rem;
  font-size: 1.2rem;
}

.content h4 {
  margin: 1.5rem 0 0.5rem;
  font-size: 1.05rem;
}

.content .anchor {
  margin-left: 0.25rem;
  color: var(--muted);
  font-weight: 400;
  text-decoration: none;
  opacity: 0;
}

.content :is(h2, h3, h4):hover .anchor,
.content .anchor:focus {
  opacity: 1;
}

.content p,
.content ul,
.content ol {
  margin: 0 0 1rem;
}

.content li + li {
  margin-top: 0.25rem;
}

.content blockquote {
  margin: 1.25rem 0;
  padding: 0.6rem 1rem;
  border-left: 4px solid var(--accent);
  border-radius: 0 0.4rem 0.4rem 0;
  background: var(--surface);
}

.content blockquote > :last-child {
  margin-bottom: 0;
}

.content hr {
  margin: 2.5rem 0;
  border: 0;
  border-top: 1px solid var(--border);
}

.content img {
  max-width: 100%;
  height: auto;
}

/* ---------- Code ---------- */

code,
pre {
  font-family: ui-monospace, SFMono-Regular, "JetBrains Mono", Menlo, Consolas, monospace;
  font-size: 0.875em;
}

:not(pre) > code {
  padding: 0.1em 0.35em;
  border-radius: 0.3rem;
  background: var(--code-bg);
  border: 1px solid var(--border);
  overflow-wrap: anywhere;
}

pre {
  margin: 0 0 1.25rem;
  padding: 0.9rem 1rem;
  overflow-x: auto;
  border: 1px solid var(--border);
  border-radius: 0.5rem;
  background: var(--code-bg);
  line-height: 1.5;
}

pre code {
  font-size: inherit;
}

.hljs-keyword,
.hljs-selector-tag,
.hljs-section {
  color: var(--syn-keyword);
}

.hljs-string,
.hljs-attr {
  color: var(--syn-string);
}

.hljs-number,
.hljs-literal {
  color: var(--syn-number);
}

.hljs-comment,
.hljs-meta {
  color: var(--syn-comment);
  font-style: italic;
}

.hljs-title,
.hljs-title.function_ {
  color: var(--syn-title);
}

.hljs-type,
.hljs-built_in {
  color: var(--syn-type);
}

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

.table-wrap {
  margin: 0 0 1.25rem;
  overflow-x: auto;
  border: 1px solid var(--border);
  border-radius: 0.5rem;
}

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

th,
td {
  padding: 0.5rem 0.75rem;
  border-bottom: 1px solid var(--border);
  text-align: left;
  vertical-align: top;
}

th {
  background: var(--surface);
  font-weight: 650;
  white-space: nowrap;
}

tbody tr:last-child td {
  border-bottom: 0;
}

tbody tr:nth-child(even) {
  background: color-mix(in srgb, var(--surface) 45%, transparent);
}

/* ---------- Previous / next ---------- */

.pager {
  display: flex;
  gap: 1rem;
  max-width: 50rem;
  margin: 3rem auto 0;
}

.pager a {
  flex: 1;
  padding: 0.75rem 1rem;
  border: 1px solid var(--border);
  border-radius: 0.5rem;
  text-decoration: none;
  font-weight: 600;
}

.pager a:hover {
  border-color: var(--accent);
}

.pager span {
  display: block;
  color: var(--muted);
  font-size: 0.8rem;
  font-weight: 400;
}

.pager .next {
  text-align: right;
}

.pager > span {
  flex: 1;
}

/* Touch screens have no hover, so keep heading permalinks faintly visible. */
@media (hover: none) {
  .content .anchor {
    opacity: 0.4;
  }
}
