/* ===========================================================================
   AI from Scratch in C  ::  web edition

   Colours are the print book's own tokens from template.typ, so a listing
   on screen matches the same listing on paper.
   =========================================================================== */

:root {
  --accent:  #7a2846;   /* ACCENT   */
  --ink:     #0f1318;   /* INK      */
  --muted:   #5a6068;   /* MUTED    */
  --rule:    #98a1aa;   /* RULE     */
  --band:    #f2f3f5;   /* BAND     */
  --paper:   #ffffff;
  --panel:   #fafbfc;

  --kw: #7a2846;        /* keywords      maroon */
  --pl: #0f1318;        /* plain                */
  --cm: #4a7c59;        /* comments      green  */
  --pp: #6b3fa0;        /* preprocessor  purple */
  --st: #9a6400;        /* strings       amber  */
  --nu: #1f6f8b;        /* numbers       teal   */

  /* Local families first. Anyone with the print book's fonts installed sees
     the book's own typography; everyone else gets a close fallback. Drop in
     @font-face rules here if you decide to serve them. */
  --serif: "Libertinus Serif", "Linux Libertine O", Cambria, Georgia, serif;
  --sans:  Inter, "Segoe UI", -apple-system, BlinkMacSystemFont, Roboto, sans-serif;
  --mono:  "DejaVu Sans Mono", ui-monospace, "SF Mono", Menlo, Consolas, monospace;
}

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--paper);
  color: var(--ink);
  font-family: var(--serif);
  font-size: 19px;
  line-height: 1.62;
}

main {
  max-width: 42rem;
  margin: 0 auto;
  padding: 2rem 1.2rem 5rem;
}

/* -------------------------------------------------------------- headings */

h1 {
  font-family: var(--sans);
  font-size: 2.1rem;
  font-weight: 800;
  line-height: 1.15;
  margin: 1.6rem 0 0.6rem;
  padding-bottom: 0.7rem;
  border-bottom: 3px solid var(--accent);
}

h2 {
  font-family: var(--sans);
  font-size: 1.3rem;
  font-weight: 700;
  margin: 3rem 0 0.9rem;
  padding-left: 0.7rem;
  border-left: 4px solid var(--accent);
}

h3 {
  font-family: var(--sans);
  font-size: 1.05rem;
  font-weight: 700;
  margin: 2.2rem 0 0.7rem;
}

h1:target, h2:target, h3:target { scroll-margin-top: 1rem; }

.subtitle,
h1 + p {
  color: var(--muted);
  font-style: italic;
  margin-top: 0;
}

p  { margin: 0 0 1.15rem; }
ul, ol { margin: 0 0 1.2rem; padding-left: 1.5rem; }
li { margin-bottom: 0.5rem; }
ul li::marker { color: var(--accent); }

a { color: var(--accent); text-decoration-thickness: 1px; text-underline-offset: 2px; }
a:hover { text-decoration-style: dotted; }
a:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ------------------------------------------------------------- equations */

.eqn {
  font-size: 1.03rem;
  font-weight: 700;
  text-align: center;
  margin: 1.6rem 0;
  overflow-x: auto;
}

/* Do not set `display` on a <math> element. MathML uses its own inner layout
   modes, and `display: block` clobbers them so every token stacks vertically.
   The display="block" attribute already makes the element block level. */
math { font-size: 1.08em; }
math[display="block"] { margin: 1.6rem 0; overflow-x: auto; }

/* Chrome shrinks nested fractions hard. In a reference table that puts the
   Softmax row below legibility, so lift the base size where math sits in a
   cell and stop the fraction from stepping down again. */
/* math-style: normal puts cell maths into display style, so a fraction is
   set full size and stacked instead of shrunk to a script. */
td math, th math { font-size: 1.1em; math-style: normal; }

pre.eqmono {
  font-family: var(--mono);
  font-size: 0.85rem;
  font-weight: 700;
  line-height: 1.55;
  margin: 1.6rem 0;
  padding: 0 0 0 1.5rem;
  border-left: 2px solid var(--rule);
  overflow-x: auto;
}

/* --------------------------------------------------------------- listings */

.listing-wrap { margin: 1.8rem 0; }

.filename {
  font-family: var(--mono);
  font-size: 0.78rem;
  font-weight: 700;
  color: var(--accent);
  margin-bottom: 0.35rem;
}

pre.listing {
  font-family: var(--mono);
  font-size: 0.82rem;
  line-height: 1.6;
  background: var(--panel);
  border: 1px solid var(--rule);
  border-radius: 2px;
  margin: 1.8rem 0;
  padding: 0.8rem 0;
  overflow-x: auto;
  tab-size: 4;
}

.listing-wrap pre.listing { margin: 0; }
pre.listing code { font: inherit; }

/* One block per source line, so the even-row band runs the full width of the
   listing the way it does on paper instead of stopping at the last glyph.
   The line number in ::before guarantees every line has a line box, so blank
   lines keep their height. */
.line { display: block; width: max-content; min-width: 100%; }
.line.even { background: var(--band); }

/* Line numbers are generated content, so a reader who selects a listing and
   copies it gets the source with no gutter mixed in. Same for the notes. */
.line::before {
  content: attr(data-ln);
  display: inline-block;
  width: 2.6em;
  padding-right: 1em;
  text-align: right;
  color: #a8b0b8;
  user-select: none;
  -webkit-user-select: none;
}

.note::before { content: attr(data-note); }

.note {
  display: block;
  margin: 0.8rem 0 0.25rem 3.6em;
  font-family: var(--serif);
  font-style: italic;
  font-size: 0.92rem;
  color: var(--muted);
  user-select: none;
  -webkit-user-select: none;
}

.note:first-child { margin-top: 0; }

.kw { color: var(--kw); font-weight: 700; }
.pl { color: var(--pl); }
.cm { color: var(--cm); font-style: italic; }
.pp { color: var(--pp); font-weight: 700; }
.st { color: var(--st); }
.nu { color: var(--nu); }

/* Untagged fenced block: arithmetic, pseudocode, ASCII figures. */
pre.plainblock {
  font-family: var(--mono);
  font-size: 0.82rem;
  line-height: 1.6;
  border: 1px solid var(--rule);
  border-radius: 2px;
  padding: 0.8rem 0.9rem;
  margin: 1.6rem 0;
  overflow-x: auto;
}

/* --------------------------------------------------------------- callouts */

.callout {
  background: #eef3f9;
  border-left: 3px solid var(--accent);
  border-radius: 2px;
  padding: 0.85rem 1rem;
  margin: 1.7rem 0;
  font-size: 0.97rem;
}

.callout h4 {
  font-family: var(--sans);
  font-size: 0.78rem;
  font-weight: 700;
  letter-spacing: 0.06em;
  color: var(--accent);
  margin: 0 0 0.4rem;
}

.callout p:last-child { margin-bottom: 0; }

.codeblock {
  border: 1px solid var(--rule);
  border-radius: 2px;
  margin: 1.7rem 0;
  overflow: hidden;
}

.cb-label {
  background: #eceff2;
  font-family: var(--sans);
  font-size: 0.7rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  color: var(--muted);
  padding: 0.35rem 0.7rem;
}

.codeblock pre {
  font-family: var(--mono);
  font-size: 0.82rem;
  margin: 0;
  padding: 0.7rem;
  overflow-x: auto;
}

/* ---------------------------------------------------------------- figures */

figure { margin: 2.2rem 0; text-align: center; }

figure img {
  max-width: 100%;
  height: auto;
  background: #fff;
}

figcaption {
  margin-top: 0.7rem;
  font-size: 0.88rem;
  line-height: 1.5;
  color: var(--muted);
  text-align: center;
}

figcaption b { color: var(--ink); }

/* ----------------------------------------------------------------- tables */

.tablewrap { overflow-x: auto; margin: 1.7rem 0; }

table {
  border-collapse: collapse;
  font-size: 0.93rem;
  margin: 1.7rem auto;
  max-width: 100%;
}

th, td {
  padding: 0.4rem 0.85rem;
  text-align: left;
  border-bottom: 1px solid #dfe3e7;
  vertical-align: top;
}

/* datatable: grey header band, as in print */
.tablewrap thead th { background: #eceff2; font-weight: 700; }

/* the appendix reference tables: maroon band */
main > table thead th,
main > div > table thead th {
  background: var(--accent);
  color: #fff;
  font-weight: 700;
}

.tablewrap table { margin: 0 auto; }

/* ------------------------------------------------------------ store blocks */

.promo {
  border: 1px solid var(--rule);
  border-left: 3px solid var(--accent);
  border-radius: 2px;
  background: var(--panel);
  padding: 0.8rem 1rem;
  font-size: 0.93rem;
  line-height: 1.55;
}

.promo p { margin: 0; }
.promo p + p { margin-top: 0.7rem; }
.promo strong { color: var(--ink); }

.promo-top { margin: 1.6rem 0 2.2rem; }

.promo-bottom {
  margin: 3rem 0 0;
  padding: 1.1rem 1.2rem;
}

.promo-bottom h4 {
  font-family: var(--sans);
  font-size: 1rem;
  font-weight: 700;
  color: var(--ink);
  margin: 0 0 0.5rem;
}

a.buy {
  display: inline-block;
  background: var(--accent);
  color: #fff;
  font-family: var(--sans);
  font-size: 0.88rem;
  font-weight: 600;
  text-decoration: none;
  padding: 0.5rem 1rem;
  border-radius: 3px;
}

a.buy:hover { background: #601f37; text-decoration: none; }
a.buy:focus-visible { outline: 2px solid var(--ink); outline-offset: 2px; }

/* ------------------------------------------------------------- navigation */

nav { font-family: var(--sans); font-size: 0.85rem; color: var(--muted); }
nav a { color: var(--muted); text-decoration: none; }
nav a:hover { color: var(--accent); text-decoration: underline; }

nav.top    { padding-bottom: 1rem; }
nav.bottom { padding-top: 1.4rem; margin-top: 3.5rem; border-top: 1px solid #dfe3e7; }

nav .sep { margin: 0 0.55rem; color: var(--rule); }

/* --------------------------------------------------------------- contents */

.toc { list-style: none; padding: 0; }
.toc li { margin-bottom: 0.4rem; }

.toc .num {
  display: inline-block;
  width: 2.4em;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
  font-family: var(--sans);
  font-size: 0.85rem;
}

/* ----------------------------------------------------------- small screens */

@media (max-width: 36rem) {
  body { font-size: 17px; }
  main { padding: 1.2rem 0.85rem 4rem; }
  h1 { font-size: 1.6rem; }
  h2 { font-size: 1.15rem; }
  pre.listing, pre.plainblock, .codeblock pre { font-size: 0.72rem; }
  pre.eqmono { font-size: 0.74rem; }
  .line::before { width: 2.2em; padding-right: 0.55em; }
  .note { margin-left: 2.75em; }

  /* The appendix reference tables come straight from Typst's `table` with no
     wrapper of their own, so give them their own scroll container rather than
     letting them push the whole page sideways. */
  main table { display: block; overflow-x: auto; max-width: 100%; }
  th, td { padding: 0.35rem 0.6rem; }
}

/* ------------------------------------------------------------------- dark */

@media (prefers-color-scheme: dark) {
  :root {
    --accent: #d98aa8;
    --ink:    #dfe1e4;
    --muted:  #949aa2;
    --rule:   #383c42;
    --band:   #1e2126;
    --paper:  #131518;
    --panel:  #191c20;

    --kw: #e094b0;
    --pl: #dfe1e4;
    --cm: #7fb894;
    --pp: #b39ae0;
    --st: #d9a850;
    --nu: #6fb3cc;
  }
  .line::before { color: #4c525a; }
  th, td { border-bottom-color: #2a2e34; }
  nav.bottom { border-top-color: #2a2e34; }
  .tablewrap thead th { background: #22262b; }
  main > table thead th,
  main > div > table thead th { background: #5c1e34; color: #f0dde4; }
  .callout { background: #1a2029; }
  a.buy { background: #7a2846; color: #f4e6eb; }
  a.buy:hover { background: #8f3050; }
  figure img { background: #f4f4f2; }
}

/* ------------------------------------------------------------------ print */

@media print {
  nav { display: none; }
  main { max-width: none; }
  pre.listing { break-inside: avoid; }
}
