/* Shared by the by-hand articles: Computing a Transformer by Hand and How a Spiking Neuron Computes.
   Both use one muted palette, and each hue names exactly one role. Transformer: Q blue, K green, V purple,
   attention weights gold, negative values crimson. Everything else stays ink. */

/* Inter for worked numbers, served from this site so no font server is needed. One variable file covers every weight; Greek covers β, θ and the like. */
@font-face {
  font-family: Inter;
  font-style: normal;
  font-weight: 100 900;
  font-display: swap;
  src: url("fonts/inter-latin.woff2") format("woff2");
  unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+0304, U+0308, U+0329, U+2000-206F, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
}
@font-face {
  font-family: Inter;
  font-style: normal;
  font-weight: 100 900;
  font-display: swap;
  src: url("fonts/inter-greek.woff2") format("woff2");
  unicode-range: U+0370-0377, U+037A-037F, U+0384-038A, U+038C, U+038E-03A1, U+03A3-03FF;
}

.transformer-article {
  --tf-q: #3567b0;
  --tf-k: #3e7536;
  --tf-v: #6340a0;
  --tf-w: #7a5c00;
  --tf-neg: #9c3560;
  /* Heatmaps: each ramp runs from a pale tint (-0) to its pole; 0 itself is the palest gray.
     Positive values carry no role, so they ramp to ink; negative ones ramp to crimson; attention weights ramp to gold.
     White text clears 5:1 on the ink and crimson poles; the gold pole keeps dark text. */
  --heat-mid: #f7f6f3;
  --heat-pos-0: #ebe9e4; --heat-pos: #4f4c47;
  --heat-neg-0: #f6e4eb; --heat-neg: #9c3560;
  --heat-seq-0: #f8f1dc; --heat-seq: #d6ad3e;
  --tf-bg: color-mix(in srgb, var(--text) 4%, var(--bg));
  --tf-line: color-mix(in srgb, var(--text) 14%, var(--bg));
  --tf-mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  /* Worked numbers: Inter with tabular figures, so digits line up from row to row. */
  --tf-num: Inter, var(--font-body);
}
.transformer-article h2 { scroll-margin-top: 24px; }
.transformer-article li + li { margin-top: 6px; }
.transformer-article code, .transformer-article pre { font-family: var(--tf-mono); font-size: .94em; }
/* Tokens and short code in prose sit on a soft dark chip so they stand out from the text. */
.transformer-article .article-copy > p code,
.transformer-article section > p code,
.transformer-article section > ul code {
  padding: 1px 6px;
  border-radius: 4px;
  background: #2f2e2b;
  color: #fff;
  font-size: .82em;
  white-space: nowrap;
}
.transformer-article .c-q { color: var(--tf-q); }
.transformer-article .c-k { color: var(--tf-k); }
.transformer-article .c-v { color: var(--tf-v); }
.transformer-article .c-w { color: var(--tf-w); }
.transformer-article sub, .transformer-article sup { font-size: .9em; }
/* A square root draws its bar over the radicand, so √2 never shows a bare, thin tick next to bolder text. */
.transformer-article .rad { border-top: max(1px, .075em) solid currentColor; padding-top: .05em; }
.fl .rad { text-decoration: overline; }
.transformer-article td code { font-size: 1em; }
.transformer-article .num { font-family: var(--tf-num); font-size: .88em; font-variant-numeric: tabular-nums; white-space: nowrap; }

/* Section links; the numbers give the order. */
.transformer-article .article-copy > .tf-path-title { margin-top: 40px; font-size: 16px; font-weight: 600; }
.transformer-article .article-copy > .tf-path-title + .tf-path { margin-top: 12px; }
.tf-path { display: flex; flex-wrap: wrap; gap: 6px; margin: 28px 0; padding: 0; list-style: none; font-size: 13px; }
.transformer-article .tf-path li { margin: 0; }
.tf-path a { display: inline-block; padding: 5px 9px; border: 1px solid var(--tf-line); border-radius: 4px; text-decoration: none; }
.tf-path a:hover { border-color: var(--muted); }

/* Equations and worked arithmetic. */
.tf-eq { margin: 20px 0; padding: 16px 18px; background: var(--tf-bg); overflow-x: auto; text-align: center; }
.tf-eq math { font-family: "STIX Two Math", "Cambria Math", "Latin Modern Math", math; font-size: 1.25em; margin: 6px 0; }
.tf-eq math + math { margin-top: 14px; }
.transformer-article .tf-eq-label { margin: 0; color: var(--muted); font-size: 13px; text-align: left; }
.transformer-article .tf-eq-label + math { margin-top: 4px; }
.transformer-article math + .tf-eq-label { margin-top: 18px; }
.tf-work { margin: 20px 0; padding: 4px 0 4px 18px; border-left: 2px solid var(--tf-line); font-family: var(--tf-num); font-size: 14px; font-variant-numeric: tabular-nums; line-height: 1.9; overflow-x: auto; }
.tf-work div { white-space: nowrap; }
.tf-work .tf-label { font-family: var(--font-body); color: var(--muted); }
/* Rows split into .tf-cell parts share one grid (subgrid), so labels right-align and each part of an expression lines up in a column. */
.tf-work:has(.tf-cell) { display: grid; grid-template-columns: repeat(12, max-content) minmax(0, 1fr); }
.tf-work:has(.tf-cell) > div { display: grid; grid-column: 1 / -1; grid-template-columns: subgrid; }
.tf-work:has(.tf-cell) .tf-label { padding-right: .5em; text-align: right; }
.tf-work .tf-cell { padding-right: .45em; }
.tf-note { color: var(--muted); font-size: 14px; }

/* Flow figures: values are boxes, operations are arrows. */
.transformer-article .tf-map { margin: 8px 0 28px; padding: 0; border: 0; }
.transformer-article .tf-map figcaption { margin-top: 6px; color: var(--muted); font-size: 13px; font-style: normal; text-align: center; }
.transformer-article .tf-map .tf-flow-head { margin: 0 0 8px; color: var(--text); font-weight: 600; }
.tf-map-svg { overflow-x: auto; }
/* Figures never shrink below 380px, so their 11.5–13px text stays readable on phones. */
.tf-map-svg .fl { display: block; width: 100%; min-width: 380px; max-width: 500px; height: auto; margin: 0 auto; }
.tf-flow-pair { display: grid; gap: 32px; margin: 24px 0; }
.tf-flow-pair .tf-map { margin: 0; }
/* Black outlines on white boxes. Only a box that holds Q, K, V or the attention weights A takes a fill: the pale tint of its role. */
.fl { --fl-ink: #111; --fl-group: #f4f3f0; }
.fl text { font: 13px var(--font-body); fill: var(--fl-ink); text-anchor: middle; }
.fl .fl-title { font-weight: 600; }
.fl .fl-shape { font-weight: 400; fill: #555; font-size: 12px; }
.fl .fl-value { font: 11.5px var(--tf-num); font-variant-numeric: tabular-nums; fill: #333; }
.fl .fl-tok { font: 600 12px var(--font-body); fill: var(--fl-ink); }
.fl .fl-from { font-weight: 400; }
.fl .fl-label, .fl .fl-note { font-size: 12px; fill: #333; text-anchor: start; }
.fl rect, .fl circle { fill: #fff; stroke: var(--fl-ink); stroke-width: 1.25; }
.fl [data-kind="q"] rect { fill: color-mix(in srgb, var(--tf-q) 22%, #fff); }
.fl [data-kind="k"] rect { fill: color-mix(in srgb, var(--tf-k) 22%, #fff); }
.fl [data-kind="v"] rect { fill: color-mix(in srgb, var(--tf-v) 22%, #fff); }
.fl [data-kind="w"] rect { fill: color-mix(in srgb, var(--tf-w) 18%, #fff); }
.fl .fl-frame { fill: var(--fl-group); }
.fl-ghost rect { stroke-dasharray: 4 3; }
.fl-ghost text { font-style: italic; }
.fl-sum text { font-size: 15px; }
.fl-edge { fill: none; stroke: var(--fl-ink); stroke-width: 1.25; }
/* Arrowheads take the color of their line, so grey context arrows get grey heads. */
.fl marker path { fill: context-stroke; }
.fl-stream { fill: none; stroke: var(--fl-ink); stroke-width: 2; }
/* Focus: the part a section explains keeps its paper colors and gets a heavy black outline.
   Everything else is drawn as context: white boxes, grey outlines and grey text. Solid grey, never transparency, so it stays readable. */
.fl { --fl-ctx: #b4b4b4; --fl-ctx-text: #6b6b6b; }
.fl .is-on rect, .fl .is-on circle, .fl .fl-edge.is-on, .fl .fl-stream.is-on { stroke-width: 2.4; }
.fl.has-focus > g.is-on .fl-title { font-weight: 700; }
.fl.has-focus > g:not(.is-on) :is(rect, circle) { fill: #fff; stroke: var(--fl-ctx); }
.fl.has-focus > g:not(.is-on) :is(text, tspan) { fill: var(--fl-ctx-text); }
.fl.has-focus > g:not(.is-on) .fl-title { font-weight: 400; }
.fl.has-focus > :is(.fl-edge, .fl-stream):not(.is-on) { stroke: var(--fl-ctx); }
.fl.has-focus > :is(.fl-label, .fl-note):not(.is-on) { fill: var(--fl-ctx-text); }
.fl.has-focus > .fl-label.is-on { font-weight: 600; }
.fl.has-focus > .fl-frame { fill: #fafafa; stroke: #d6d6d6; }
/* The source box names where every number comes from, so its text keeps full ink even as context. */
.fl.has-focus > g[data-kind="embed"]:not(.is-on) :is(text, tspan) { fill: var(--fl-ink); }

/* Matrices: textbook style. Numbers sit inside thin square brackets; nothing is boxed. */
.tf-viz { margin: 32px 0; }
.tf-row { display: flex; align-items: center; flex-wrap: wrap; gap: 28px 16px; overflow-x: auto; padding-bottom: 4px; }
.tf-row + .tf-row, .tf-row + .tf-eqs { margin-top: 32px; }
.tf-unit, .tf-group { display: flex; align-items: center; gap: 16px; }
.tf-eqs { display: grid; grid-template-columns: repeat(4, max-content); align-items: center; gap: 20px 16px; overflow-x: auto; padding-bottom: 4px; }
/* 44px = title row + column-label row, so the operator centers on the numbers, not on the whole figure. */
.tf-op { padding-top: 44px; color: var(--muted); font-size: 17px; white-space: nowrap; }

.transformer-article .mx {
  --mx-ink: var(--text);
  display: grid;
  width: max-content;
  grid-template-columns: auto 5px repeat(var(--n), minmax(42px, auto)) 5px;
  grid-template-rows: minmax(24px, auto) 20px 3px repeat(var(--m), 30px) 3px;
  position: relative;
  margin: 0; padding: 0; border: 0;
  font-size: 14px;
  font-variant-numeric: tabular-nums;
}
.mx.tone-q { --mx-ink: var(--tf-q); }
.mx.tone-k { --mx-ink: var(--tf-k); }
.mx.tone-v { --mx-ink: var(--tf-v); }
.mx.tone-w { --mx-ink: var(--tf-w); }
.mx .mx-r { display: contents; }
/* The title is centered over the brackets and never widens the columns: a title wider than the matrix wraps onto a second line. */
.transformer-article .mx .mx-title { contain: inline-size; align-self: end; margin: 0 0 4px; text-align: center; font-size: 13.5px; line-height: 18px; color: var(--text); }
.mx .mx-title > span { display: inline-flex; flex-wrap: wrap; justify-content: center; column-gap: 6px; }
.mx .mx-title b { font-weight: 600; color: var(--mx-ink); white-space: nowrap; }
.mx .mx-title small { color: var(--muted); font-size: 12px; white-space: nowrap; }
.mx .mx-col { align-self: start; text-align: center; color: var(--muted); font-size: 12px; line-height: 16px; }
.mx .mx-lab { align-self: center; padding-right: 8px; text-align: right; color: var(--muted); font-size: 13px; white-space: nowrap; }
.mx .is-focus .mx-lab { color: var(--text); font-weight: 600; }
.mx .mx-band { background: color-mix(in srgb, var(--text) 6%, var(--bg)); border-radius: 3px; }
/* Brackets: a left and a right edge with short serifs, in the matrix's role color. */
.mx .mx-br { position: relative; pointer-events: none; }
.mx .mx-br::before, .mx .mx-br::after { content: ""; position: absolute; top: 0; bottom: 0; width: 5px; border: 1.25px solid var(--mx-ink); }
.mx .mx-br::before { left: 0; border-right: 0; }
.mx .mx-br::after { right: 0; border-left: 0; }

.mx .mx-cell { display: flex; position: relative; }
.mx .mx-cell > span, .mx .mx-cell > button { flex: 1; display: flex; align-items: center; justify-content: center; margin: 1px; padding: 0 7px; border-radius: 3px; white-space: nowrap; }
.mx .mx-cell > button { border: 0; background: transparent; font: inherit; color: inherit; cursor: pointer; }
.mx .mx-cell > button:hover { box-shadow: inset 0 0 0 1.5px var(--muted); }
.mx .is-focus .mx-cell { font-weight: 600; }
/* The picked cell keeps its outline while the pointer is still on it; hover only outlines cells that are not picked. */
.mx .mx-cell.is-picked > *, .mx .mx-cell.is-picked > button:hover { box-shadow: inset 0 0 0 1.5px var(--mx-ink); }
.mx .mx-cell.is-masked > * { color: var(--muted); background: repeating-linear-gradient(135deg, transparent 0 4px, color-mix(in srgb, var(--text) 8%, var(--bg)) 4px 8px); }
.mx .mx-cell.is-tile > * { box-shadow: inset 0 0 0 2px var(--text); }
.mx .mx-cell.is-done { opacity: .45; }

/* The calculation panel under a matrix. */
.tf-calc { margin: 24px 0 28px; padding: 12px 16px; background: var(--tf-bg); font-size: 14px; line-height: 1.85; overflow-x: auto; }
.tf-calc p { margin: 0; }
.tf-calc .tf-calc-title { font-weight: 600; margin-bottom: 4px; }
.tf-calc code { font-family: var(--tf-num); font-size: 1em; font-variant-numeric: tabular-nums; white-space: nowrap; }
.tf-hint { color: var(--muted); font-size: 13px; margin: 0 0 8px; }

/* Bars: attention weights, vectors, activations. */
.tf-bars { display: grid; gap: 8px; margin: 20px 0; font-size: 14px; max-width: 560px; }
.tf-bar { display: grid; grid-template-columns: 56px 1fr 56px; gap: 10px; align-items: center; }
.tf-bar-track { height: 14px; background: var(--tf-bg); position: relative; }
.tf-bar-fill { position: absolute; inset: 0 auto 0 0; background: var(--tf-w); }
.tf-bar code { text-align: right; }

.tf-vec { display: inline-grid; grid-auto-flow: column; justify-self: start; gap: 3px; align-items: center; height: 52px; vertical-align: middle; }
.tf-vec span { width: 12px; position: relative; height: 52px; }
.tf-vec span::before { content: ""; position: absolute; left: 0; right: 0; top: 50%; border-top: 1px solid var(--tf-line); }
.tf-vec i { position: absolute; left: 0; right: 0; background: var(--text); }
.tf-vec i.is-neg { background: var(--tf-neg); }

.tf-cols { display: flex; gap: 6px; align-items: stretch; margin: 20px 0 6px; }
.tf-col { flex: 1; min-width: 0; text-align: center; font-size: 13px; }
.tf-col-plot { position: relative; height: 120px; background: var(--tf-bg); }
.tf-col-plot::after { content: ""; position: absolute; left: 0; right: 0; top: 50%; border-top: 1px solid var(--tf-line); }
.tf-col-plot i { position: absolute; left: 22%; right: 22%; background: var(--text); }
.tf-col-plot i.is-neg { background: var(--tf-neg); }
.tf-col-plot i.is-zeroed { background: none; border: 1px dashed var(--tf-neg); }
.tf-col code { display: block; margin-top: 4px; font-size: 13px; }
.tf-col small { display: block; color: var(--muted); font-size: 13px; }
.tf-cols-label { font-size: 13px; color: var(--muted); margin: 0 0 18px; }
.tf-pair { display: grid; grid-template-columns: 1fr 1fr; gap: 28px; margin-top: 20px; max-width: 620px; }
.tf-pair .tf-cols-label { margin: 0; }
.tf-pair .tf-cols { margin-top: 6px; }
.tf-kv { gap: 32px 36px; }

/* Controls. */
.tf-controls { display: flex; flex-wrap: wrap; align-items: center; gap: 10px 18px; margin: 20px 0; font-size: 13px; }
.tf-seg { display: inline-flex; border: 1px solid var(--tf-line); border-radius: 4px; overflow: hidden; }
.tf-seg button { border: 0; background: var(--bg); padding: 6px 12px; font: inherit; color: var(--text); cursor: pointer; }
.tf-seg button + button { border-left: 1px solid var(--tf-line); }
.tf-seg button[aria-pressed="true"] { background: var(--text); color: var(--bg); }
.tf-btn { border: 1px solid var(--tf-line); border-radius: 4px; background: var(--bg); padding: 6px 12px; font: inherit; color: var(--text); cursor: pointer; }
.tf-btn:disabled { opacity: .4; cursor: default; }
.transformer-article :is(button, a, summary):focus-visible { outline: 2px solid var(--text); outline-offset: 2px; }

/* Residual stream. */
.tf-stream { position: relative; margin: 24px 0; padding-left: 26px; }
.tf-stream::before { content: ""; position: absolute; left: 8px; top: 8px; bottom: 8px; width: 3px; background: var(--tf-line); }
.tf-node { position: relative; display: grid; grid-template-columns: 150px auto 1fr; gap: 14px; align-items: center; padding: 8px 0; font-size: 13px; }
.tf-node::before { content: ""; position: absolute; left: -23px; top: 50%; width: 9px; height: 9px; margin-top: -5px; border-radius: 50%; background: var(--text); }
.tf-node.is-branch { margin-left: 24px; grid-template-columns: 126px auto 1fr; color: var(--muted); }
.tf-node.is-branch::before { left: -47px; width: 30px; height: 0; border-radius: 0; border-top: 2px dashed var(--muted); background: none; margin-top: 0; }
.tf-node.is-norm::before { background: var(--tf-w); }
.tf-node strong { font-weight: 600; color: var(--text); }
.tf-node code { font-size: 13px; }
.tf-node .tf-rms { color: var(--muted); font-size: 13px; margin-left: 8px; }
.tf-sumtable { width: 100%; border-collapse: collapse; font-size: 14px; margin: 20px 0; font-variant-numeric: tabular-nums; }
/* A cell never breaks inside a value; a table too wide for the column scrolls sideways in its .tf-table box instead. */
.tf-table { margin: 20px 0; overflow-x: auto; }
.tf-table > .tf-sumtable { margin: 0; }
.tf-sumtable th, .tf-sumtable td { padding: 7px 8px; border-bottom: 1px solid var(--tf-line); text-align: right; font-family: var(--tf-num); white-space: nowrap; }
.tf-sumtable th { font-family: var(--font-body); color: var(--muted); font-weight: 600; }
.tf-sumtable th:first-child, .tf-sumtable td:first-child { text-align: left; font-family: var(--font-body); }
.tf-states { max-width: 520px; }
.tf-states td:last-child, .tf-states th:last-child { text-align: left; }
.tf-sumtable tr.is-total td { font-weight: 600; border-bottom: 0; border-top: 2px solid var(--text); }

/* Heads. */
.tf-routing { width: 100%; max-width: 540px; height: auto; display: block; margin: 16px 0; }
/* Drawn like the flow figures: black outlines, white boxes, and only the query heads take their role's pale tint. A KV head holds both K and V, so it stays white. */
.tf-routing text { font: 13px var(--font-body); fill: #111; }
.tf-routing rect { fill: #fff; stroke: #111; stroke-width: 1.25; }
.tf-routing .q rect { fill: color-mix(in srgb, var(--tf-q) 22%, #fff); }
.tf-routing line { stroke: #111; stroke-width: 1.25; }
.tf-cache { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 16px; margin: 20px 0; font-size: 13px; }
.tf-cache > div { padding: 10px 0; border-top: 1px solid var(--tf-line); }
.tf-cache > div.is-current { border-top: 2px solid var(--text); }
.tf-cache strong { display: block; }
.tf-cache .tf-cells { display: flex; flex-wrap: wrap; gap: 2px; margin: 8px 0; }
.tf-cache .tf-cells i { width: 10px; height: 10px; }
.tf-cache .tf-cells i.k { background: var(--tf-k); }
.tf-cache .tf-cells i.v { background: var(--tf-v); }

/* FlashAttention. */
.tf-flash { display: grid; grid-template-columns: auto 1fr; gap: 36px; align-items: start; margin: 28px 0; }
.tf-flash > div > .mx + .mx { margin-top: 32px; }
.tf-memory { font-size: 13px; }
.tf-memory h4 { margin: 0 0 16px; font-size: 13px; }
.tf-memory .tf-row + .tf-row, .tf-memory .tf-row + .tf-table { margin-top: 32px; }

@media (max-width: 680px) {
  .tf-eq { padding: 14px 12px; }
  .tf-eq math { font-size: 1.02em; }
  .tf-flash { grid-template-columns: 1fr; }
  .tf-node, .tf-node.is-branch { grid-template-columns: 1fr; gap: 4px; }
  .tf-cache { grid-template-columns: 1fr; gap: 6px; }
  .transformer-article .mx { grid-template-columns: auto 4px repeat(var(--n), minmax(36px, auto)) 4px; }
  .mx .mx-cell > span, .mx .mx-cell > button { padding: 0 4px; }
  .tf-bar { grid-template-columns: 44px 1fr 50px; }
  /* Prose around the figures follows the phone type scale; the figures' own labels stay sized to their grids. */
  .tf-hint, .tf-cols-label, .tf-path, .transformer-article .tf-eq-label, .transformer-article .tf-map figcaption { font-size: 14px; }
}

/* Drawn content keeps its margins inside the figure, so the stack alone sets the gap around it. */
.transformer-article .tf-viz > :first-child { margin-top: 0; }
.transformer-article .tf-viz > :last-child { margin-bottom: 0; }

/* "On this page" rail. It sits in the right margin and narrows with it; below 1100px there is no room. */
.tf-toc { display: none; }
@media (min-width: 1100px) {
  .tf-toc {
    display: block;
    position: fixed;
    top: 72px;
    left: calc(50% + var(--content-width) / 2 + 32px);
    width: min(220px, calc(50vw - var(--content-width) / 2 - 56px));
    max-height: calc(100vh - 104px);
    overflow-y: auto;
    font-size: 13.5px;
    line-height: 1.4;
  }
}
.tf-toc ol { margin: 0; padding: 0; list-style: none; border-left: 1px solid color-mix(in srgb, var(--text) 14%, var(--bg)); }
.tf-toc li { margin: 0; }
.tf-toc a { display: block; margin-left: -1px; padding: 5px 0 5px 12px; border-left: 2px solid transparent; color: var(--muted); text-decoration: none; }
.tf-toc a:hover { color: var(--text); text-decoration: underline; text-underline-offset: 3px; }
/* The current section changes only color and the bar, never the weight: bold letters are wider and would rewrap two-line items as the reader scrolls. */
.tf-toc a.is-active { color: var(--text); border-left-color: var(--text); }
.tf-toc a:focus-visible { outline: 2px solid var(--text); outline-offset: 2px; }
@media print { .tf-toc { display: none; } }
