/* ── Fonts ───────────────────────────────────────────────────────────────── */

@import url("https://fonts.googleapis.com/css2?family=Barlow:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,100;1,200;1,300;1,400;1,500;1,600;1,700;1,800;1,900&family=Crimson+Pro:ital,wght@0,200..900;1,200..900&family=EB+Garamond:ital,wght@0,400..800;1,400..800&family=Fira+Code:wght@300..700&family=Inter:ital,opsz,wght@0,14..32,100..900;1,14..32,100..900&family=Lora:ital,wght@0,400..700;1,400..700&display=swap");

@font-face {
  font-family: "JuliaMono";
  src: url("https://cdnjs.cloudflare.com/ajax/libs/juliamono/0.051/JuliaMono-Regular.woff")
    format("woff2");
  font-weight: normal;
  font-style: normal;
  font-display: swap;
}

/* ── Reset ───────────────────────────────────────────────────────────────── */

* {
  margin: 0;
  padding: 0;
}

/* Prevent any overflowing element from causing page-level horizontal scroll */
html,
body {
  overflow-x: hidden;
}

/* Prevent iOS Safari from automatically inflating font sizes in narrow
   elements (most visible in code blocks with long lines) */
html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

/* ── Base ────────────────────────────────────────────────────────────────── */

body {
  background-color: #fefefe;
  color: #222;

  --nav-width: 260px;
}

/* ── Article layout ──────────────────────────────────────────────────────── */

article {
  font-family: "Inter", sans-serif;
  font-weight: 400;
  font-style: normal;
  font-size: 16px;
  line-height: 25px;

  max-width: 700px;
  margin-left: calc(128px + var(--nav-width));
  margin-right: 128px;
  margin-top: 112px;
  margin-bottom: 256px;
}

/* Ensure sections and h3s don't get hidden under the top of the viewport when
   scrolled to via an anchor link */
section,
h3 {
  scroll-margin-top: 16px;
}

/* ── Headings ────────────────────────────────────────────────────────────── */

h1,
h2,
h3 {
  font-family: "Lora", serif;
  font-optical-sizing: auto;
  font-weight: 600;
  font-style: normal;

  /* Flex layout lets justify-content: space-between push the section number
     badge to the right edge */
  position: relative;
  display: flex;
  justify-content: space-between;
  width: 100%;
}

h1 {
  font-size: 45px;
  margin-bottom: 32px;
  line-height: 45px;
}

/* Blue chapter number badge, absolutely positioned at the bottom-right of h1 */
.chapter-num {
  display: flex;
  align-items: center;
  justify-content: center;

  position: absolute;
  right: 0;
  bottom: 0;

  background: rgb(48, 112, 183);
  border-radius: 2px;
  color: white;
  width: 80px;
  height: 80px;
  font-size: 40px;
}

h2 {
  font-size: 28px;
  margin-top: 64px;
  margin-bottom: 16px;
}

h2 .num {
  font-size: 24px;
}

h3 {
  font-size: 20px;
  margin-top: 24px;
  margin-bottom: 12px;
}

h3 .num {
  font-size: 18px;
}

/* § anchor link that appears on hover to the left of a heading */
.header-link {
  color: inherit;
  text-decoration: none;
  position: relative;
}

.header-link::before {
  content: "§";
  position: absolute;
  left: -1.2em;
  opacity: 0;
  transition: opacity 0.15s;
  color: lightgray;
}

.header-link:hover::before {
  opacity: 1;
}

/* ── Prose spacing ───────────────────────────────────────────────────────── */

p + p,
pre + p {
  margin-top: 16px;
}

p + pre,
pre + pre {
  margin-top: 8px;
}

/* ── Blockquote ──────────────────────────────────────────────────────────── */

blockquote {
  margin: 0 24px;
  font-style: italic;
  font-size: 18px;
}

blockquote *:first-child:before {
  content: "\201C";
  position: absolute;
  margin-left: -0.4em;
}

blockquote *:last-child:after {
  content: "\201D";
}

.cite {
  text-align: right;
}

/* ── Code blocks ─────────────────────────────────────────────────────────── */

/* Allow code to extend to the right edge of the visible screen area.
   Formula: full viewport width - sidebar - left padding - right padding */
pre {
  width: calc(100vw - var(--nav-width) - 128px - 64px);
}

code {
  font-family: "Fira Code", "JuliaMono", monospace;
  font-size: 15px;
  line-height: 22px;
}

code.code-block {
  --num-gap: 24px;

  border-radius: 4px;
  /* Shift left to make room for the line-number column outside the text margin */
  margin-left: calc(-4ch - var(--num-gap));
  padding: 8px 0;

  /* Two-column grid: line numbers | code content */
  display: grid;
  grid-template-columns: 4ch auto;
  column-gap: var(--num-gap);

  /* Preserve indentation while allowing long lines to wrap */
  white-space: pre-wrap;
}

/* Line number column — right-aligned and non-selectable */
code .line {
  display: inline-block;
  text-align: right;

  color: #7c7f93;
  user-select: none;
}

/* ── Syntax highlighting ─────────────────────────────────────────────────── */

code .kw {
  color: #8839ef;
}
code .name {
  color: #1e66f5;
  font-style: italic;
}
code .literal {
  color: #179299;
}
code .str {
  color: #40a02b;
}
code .lit {
  color: #179299;
}
code .comment {
  color: #7c7f93;
  font-style: italic;
}

/* ── KaTeX ───────────────────────────────────────────────────────────────── */

/* Render fraction lines a little thicker */
.katex .mfrac .frac-line {
  border-bottom-width: 1px !important;
}

/* ── Navigation sidebar ──────────────────────────────────────────────────── */

.nav {
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  width: var(--nav-width);

  overflow-x: hidden;
  overflow-y: auto;

  padding: 8px;
  padding-top: 32px;

  font-family: "Lora", serif;
  font-weight: 400;
  font-style: normal;
  font-size: 15px;

  background: rgb(40, 51, 64);
  color: rgb(138, 173, 196);
}

.nav ol {
  list-style: none;
}

.nav .num {
  color: rgb(103, 136, 159);
  font-weight: normal;
  font-variant-numeric: tabular-nums;
}

.nav .chapter {
  font-weight: 600;
  padding: 0 12px;
}

.nav .chapter-list > li:not(:first-child) {
  margin-top: 16px;
}

.nav .section {
  padding: 0 24px;
}

.nav .section,
.nav .chapter {
  padding-top: 3px;
  padding-bottom: 3px;
  position: relative;
  cursor: pointer;
}

.nav .chapter,
.nav .section a {
  /* Reset anchor styles */
  color: inherit;
  display: block;
  width: 100%;
  text-decoration: none;
}

/* Hover highlight — uses a pseudo-element so z-index doesn't affect text */
.nav .chapter:hover::after,
.nav .section:hover::after {
  display: block;
  content: "";

  position: absolute;
  left: -8px;
  right: -8px;
  top: 0px;
  bottom: 0px;

  background: #70828b36;
  z-index: -1;
}

/* Active section indicator — same as hover but with a left accent bar */
.nav .selected::after {
  display: block;
  content: "";

  position: absolute;
  left: -8px;
  right: -8px;
  top: 0px;
  bottom: 0px;

  background: #70828b36;
  border-left: 3px solid rgb(107, 158, 194);
  z-index: -1;
}

/* ── Mobile navigation toggle ────────────────────────────────────────────── */

/* Hidden on desktop; shown as a hamburger button on mobile */
.nav-toggle {
  display: none;
}

/* Full-screen semi-transparent backdrop shown behind the open sidebar */
.nav-overlay {
  display: none;
}

/* ── Responsive: tablet / mobile (≤ 900px) ───────────────────────────────── */

@media (max-width: 900px) {
  /* Hamburger button — fixed in the top-left corner */
  .nav-toggle {
    display: flex;
    align-items: center;
    justify-content: center;
    position: fixed;
    top: 14px;
    left: 14px;
    z-index: 200;
    width: 40px;
    height: 40px;
    background: rgb(40, 51, 64);
    color: rgb(138, 173, 196);
    border: none;
    border-radius: 4px;
    font-size: 20px;
    cursor: pointer;
  }

  /* Backdrop — invisible until .open is toggled on by JS */
  .nav-overlay {
    display: block;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: 99;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.2s ease;
  }

  .nav-overlay.open {
    opacity: 1;
    pointer-events: auto;
  }

  /* Sidebar slides in from the left as a drawer */
  .nav {
    transform: translateX(-100%);
    transition: transform 0.2s ease;
    z-index: 100;
    /* Extra top padding so the first chapter title clears the hamburger button */
    padding-top: 68px;
  }

  .nav.open {
    transform: translateX(0);
  }

  /* Article fills the viewport width with small side margins */
  article {
    max-width: none;
    margin-left: 20px;
    margin-right: 20px;
    margin-top: 72px;
    margin-bottom: 128px;
  }

  h1 {
    font-size: 32px;
    line-height: 38px;
    /* Align the chapter-num badge to the bottom of the heading */
    align-items: flex-end;
  }

  /* On mobile, .chapter-num becomes a flex item rather than absolutely
     positioned, so it stays within the visible article width */
  .chapter-num {
    position: static;
    flex-shrink: 0;
    width: 56px;
    height: 56px;
    font-size: 28px;
  }

  /* pre is the scroll container for wide code on mobile; its width is
     constrained to the article, and overflow scrolls horizontally */
  pre {
    width: 100%;
    overflow-x: auto;
  }

  code.code-block {
    /* Remove the leftward shift used to accommodate line numbers on desktop */
    margin-left: 0;
    /* Don't wrap lines — let pre scroll instead */
    white-space: pre;
  }
}

/* ── Responsive: small phones (≤ 600px) ─────────────────────────────────── */

@media (max-width: 600px) {
  /* Hide line numbers entirely — not enough horizontal room */
  code.code-block {
    grid-template-columns: auto;
    column-gap: 0;
  }

  code .line {
    display: none;
  }
}
