/* ==========================================================================
   Rosetta viz theme: design tokens for every figure, animation and
   interactive visualization in the post.

   Source of the rules: the user's paper figures (Helvetica Neue Light,
   Rosetta blue / non-Rosetta red, viridis series, log-log power-law fits,
   staged slide builds) merged with the sophicle/reason `plotstyle` rules
   (one hue one meaning; 400 marks / 600 names / 500@20% tints; neutral-50
   field with white gridlines; left+bottom spines; Light for labels and
   ticks, Regular for names; literal tokens in IBM Plex Mono chips; lines named
   at their ends; hollow ink reference points).

   Theme switch: the site's single source of truth is
   <html data-theme="light|dark"> (default light; the nav toggle flips it and
   the site's own dark theme in static/css/variables.css uses the same
   ground, --bg-color = --viz-page = #111111). Dark tokens apply under
   :root[data-theme="dark"] (or on any element with data-theme="dark",
   for a figure that must be dark on its own). There is deliberately no
   prefers-color-scheme block: the site decides the theme, so a figure can
   never turn dark on a light page.

   JS mirror: viz-theme.js exports the same values (TOKENS.light/.dark).
   Keep the two files in sync.
   ========================================================================== */

/* Helvetica Neue (system font on macOS/iOS; Helvetica, then Arial elsewhere)
   for figure text, matching the prose. IBM Plex Mono (OFL) is the mono
   face for chips and code: its parentheses are round (B612 Mono drew them
   like square brackets, so code read wrong).
   The mono face is loaded once per page by the <link> from ssg/config.py
   WEB_FONT_URL, not by an @import here. */

:root {
  /* ---- Type ------------------------------------------------------------ */
  --viz-font-sans: "Helvetica Neue", Helvetica, Arial, sans-serif;
  --viz-font-mono: "IBM Plex Mono", ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  --viz-w-light:   300;   /* axis labels, ticks, notes, annotations     */
  --viz-w-regular: 400;   /* names: line-end labels, panel/model names   */
  --viz-w-medium:  500;   /* row titles (model names heading a row)      */

  /* Sizes in px for a figure drawn at the 704px text column (1pt ~ 1.36px
     at print width 7.2in). Do not shrink below these on phones; reflow or
     drop elements instead. */
  --viz-fs-tick:    13px;   /* tick labels                    Light   */
  --viz-fs-label:   15px;   /* axis labels                    Light   */
  --viz-fs-name:    14px;   /* line-end names, panel names    Regular */
  --viz-fs-note:    13px;   /* in-plot annotations            Light   */
  --viz-fs-chip:    12px;   /* every token chip, one size     Mono    */
  --viz-fs-ui:      13px;   /* buttons, toggles, slider values        */
  --viz-fs-tooltip: 13px;
  --viz-lh-tight:   1.2;

  /* ---- Geometry (px) ---------------------------------------------------- */
  --viz-stroke-line:   2px;     /* data lines                           */
  --viz-stroke-fit:    2px;     /* power-law fits, dashed               */
  --viz-dash-fit:      6 4;     /* stroke-dasharray for fits            */
  --viz-dash-null:     2 3;     /* second null (input permutation)      */
  --viz-stroke-spine:  1px;
  --viz-stroke-grid:   1px;
  --viz-stroke-err:    1.5px;   /* error bars: no caps, 50% opacity     */
  --viz-marker-r:      4.5px;   /* filled condition point radius        */
  --viz-marker-edge:   1.5px;   /* white (field-colored) edge           */
  --viz-tick-len:      5px;
  --viz-radius-card:   0px;     /* legend/tooltip cards are square      */
  --viz-radius-chip:   0.44em;  /* 8/18 of the chip font size           */

  /* ---- Motion ----------------------------------------------------------- */
  /* Easings: smoothstep (3t^2 - 2t^3) for things that MOVE (axis domains,
     morphs); ease-out cubic for things that APPEAR (fades, labels); ease-out
     quad for a pen that DRAWS a line. CSS curves approximate the JS
     functions exported by viz-theme.js. */
  --viz-ease-move:  cubic-bezier(0.42, 0, 0.58, 1);
  --viz-ease-in:    cubic-bezier(0.33, 1, 0.68, 1);
  --viz-ease-draw:  cubic-bezier(0.5, 1, 0.89, 1);
  --viz-dur-hover:   150ms;  /* hover, focus, tooltip                    */
  --viz-dur-fade:    450ms;  /* labels / ticks fading out or in          */
  --viz-dur-move:   1100ms;  /* an axis domain or a layout moving        */
  --viz-dur-draw:   1600ms;  /* one line drawn left to right             */
  --viz-dur-pause:   500ms;  /* between two beats                        */
  --viz-dur-hold:   1500ms;  /* the start state holds before a build     */
  --viz-dur-stagger:  60ms;  /* per point when points appear in order    */

  /* ---- Colors: light (default) ------------------------------------------ */
  /* Ground */
  --viz-page:   #ffffff;   /* figure background = page                  */
  --viz-field:  #fafafa;   /* plot area (neutral-50)                    */
  --viz-grid:   #ffffff;   /* gridlines: lighter than the field         */
  --viz-spine:  #101828;   /* left + bottom spines only (gray-900)      */
  --viz-tick:   #555555;   /* tick marks; tick LABELS are ink           */
  --viz-ink:    #111111;   /* all text that is not naming a hue         */
  --viz-ink-2:  #525252;   /* secondary text: units, captions in-figure (neutral-600) */
  --viz-rule:   #e5e5e5;   /* hairline separators in UI (neutral-200)   */

  /* Semantic hues: one hue, one meaning, across the whole post.
     mark = 400 (line, area, filled point); text = 600 (the words naming it);
     tint = the hue's 500 at 20% flattened on white (bands, card fills). */
  --viz-rosetta:          #56a2ff;   /* blue-400  (paper #5DA5DA) */
  --viz-rosetta-text:     #155dfc;   /* blue-600                  */
  --viz-rosetta-tint:     #d6e6ff;   /* blue-500 @20%             */
  --viz-nonrosetta:       #ff6568;   /* red-400   (paper #F17C79) */
  --viz-nonrosetta-text:  #e40016;   /* red-600                   */
  --viz-nonrosetta-tint:  #fed5d7;   /* red-500 @20%              */

  /* Greys are for marks that are not the point. */
  --viz-null:        #a1a1a1;   /* null controls (untrained, permuted) neutral-400 */
  --viz-null-text:   #525252;   /* neutral-600                                     */
  --viz-baseline:    #d4d4d4;   /* baseline bars, "other runs behind" neutral-300   */
  --viz-ref-fill:    #ffffff;   /* reference points: hollow ink circles            */
  --viz-ref-edge:    #111111;

  /* Matched-set ramp: the five curves of each scaling panel, ordered loosest
     (pair, most Rosetta neurons) to strictest (largest matched set). The
     same slot means the same rank in Language and Vision; hues trace the
     paper's viridis order (violet > indigo > teal > green > yellow).
     Lightness increases with slot, so the order survives grayscale and
     color-vision deficiency. Text shades raised to >= 4.5:1 on the field. */
  --viz-set-1: #6e11b0;  --viz-set-1-text: #6e11b0;   /* purple-800 (hue kept apart from indigo set-2) */
  --viz-set-2: #4f39f6;  --viz-set-2-text: #432dd7;   /* indigo-600 / 700 */
  --viz-set-3: #009488;  --viz-set-3-text: #00776e;   /* teal-600   / 700 */
  --viz-set-4: #00c65a;  --viz-set-4-text: #00813a;   /* green-500  / 700 */
  --viz-set-5: #f7c900;  --viz-set-5-text: #a26200;   /* yellow-400 / 700 */

  /* Activation (one meaning: "how strongly this neuron fires here"), for
     token highlights and image-patch overlays alike: orange-500 at alpha
     0..0.85 over the surface. Text on it stays ink. */
  --viz-act-rgb:      252, 113, 0;   /* orange-500 #fc7100 */
  --viz-act-max:      0.85;
  --viz-act-edge:     #ec5600;       /* orange-600: ring on the max token */

  /* Chips: literal tokens, neuron ids (L16/U11168), code. One design. */
  --viz-chip-fill:    #f6f6f6;
  --viz-chip-edge:    #111111;
  --viz-chip-text:    #111111;
  --viz-chip-shadow:  0.04em 0.1em 0.22em rgba(17, 17, 17, 0.30);

  /* Cards: legend (only if a line cannot be named at its end), tooltip. */
  --viz-card-fill:    #ffffff;
  --viz-card-edge:    #111111;
  --viz-card-shadow:  0 1px 3px rgba(17, 17, 17, 0.10);

  /* Interactive controls */
  --viz-ctl-fill:       #ffffff;
  --viz-ctl-edge:       #d4d4d4;
  --viz-ctl-edge-hover: #111111;
  --viz-ctl-on-fill:    #111111;
  --viz-ctl-on-text:    #ffffff;
  --viz-focus:          #155dfc;   /* focus ring = rosetta-text, 2px */
}

/* ---- Colors: dark ---------------------------------------------------------
   Marks keep their 400 step (it reads on dark); text moves to 300; tints are
   the 500 step at 20% flattened on the dark field; ground inverts so the
   field is a step above the page and gridlines a step above the field. */
:root[data-theme="dark"],
[data-theme="dark"] .viz,
.viz[data-theme="dark"] {
  --viz-page:   #111111;
  --viz-field:  #171717;   /* neutral-900 */
  --viz-grid:   #262626;   /* neutral-800 */
  --viz-spine:  #a1a1a1;   /* neutral-400 */
  --viz-tick:   #737373;   /* neutral-500 */
  --viz-ink:    #f5f5f5;   /* neutral-100 */
  --viz-ink-2:  #a1a1a1;
  --viz-rule:   #262626;

  --viz-rosetta:          #56a2ff;   /* blue-400 */
  --viz-rosetta-text:     #91c5ff;   /* blue-300 */
  --viz-rosetta-tint:     #1c2c45;   /* blue-500 @20% on #171717 */
  --viz-nonrosetta:       #ff6568;   /* red-400 */
  --viz-nonrosetta-text:  #ffa3a4;   /* red-300 */
  --viz-nonrosetta-tint:  #451b1d;   /* red-500 @20% on #171717 */

  --viz-null:        #737373;   /* neutral-500: recedes on dark */
  --viz-null-text:   #a1a1a1;   /* neutral-400 */
  --viz-baseline:    #404040;   /* neutral-700 */
  --viz-ref-fill:    #171717;
  --viz-ref-edge:    #f5f5f5;

  /* Ramp on dark: every slot at L >= 0.67 so none sinks into the ground;
     text = mark (all >= 5.7:1 on #171717). */
  --viz-set-1: #dab2ff;  --viz-set-1-text: #dab2ff;   /* purple-300 (hue kept apart from indigo set-2) */
  --viz-set-2: #7d87ff;  --viz-set-2-text: #a4b4ff;   /* indigo-400 / 300 */
  --viz-set-3: #00d3bd;  --viz-set-3-text: #00d3bd;   /* teal-400 */
  --viz-set-4: #00c65a;  --viz-set-4-text: #05df72;   /* green-500 / 400 */
  --viz-set-5: #ffe030;  --viz-set-5-text: #ffe030;   /* yellow-300 */

  --viz-act-rgb:       252, 113, 0;
  --viz-act-max:       0.70;      /* capped so light text keeps >= 4.5:1 at a = 1 */
  --viz-act-edge:      #ff8b1f;   /* orange-400 */

  --viz-chip-fill:    #262626;
  --viz-chip-edge:    #d4d4d4;
  --viz-chip-text:    #f5f5f5;
  --viz-chip-shadow:  0.04em 0.1em 0.22em rgba(0, 0, 0, 0.55);

  --viz-card-fill:    #1c1c1c;
  --viz-card-edge:    #d4d4d4;
  --viz-card-shadow:  0 1px 4px rgba(0, 0, 0, 0.5);

  --viz-ctl-fill:       #171717;
  --viz-ctl-edge:       #404040;
  --viz-ctl-edge-hover: #f5f5f5;
  --viz-ctl-on-fill:    #f5f5f5;
  --viz-ctl-on-text:    #111111;
  --viz-focus:          #91c5ff;
}

/* ==========================================================================
   Base scope: put class="viz" on a figure's root (it composes with the
   loader's .rv-figure / .rv-stage).
   ========================================================================== */
.viz {
  font-family: var(--viz-font-sans);
  font-weight: var(--viz-w-light);
  color: var(--viz-ink);
  font-variant-numeric: tabular-nums;
  -webkit-font-smoothing: antialiased;
}
.viz svg { overflow: visible; }
/* Default text paint, at zero specificity so any component class wins, and
   only for <text> without a fill attribute: a CSS fill (at any specificity)
   would otherwise override a fill="..." presentation attribute. */
:where(.viz) text { font-family: var(--viz-font-sans); }
:where(.viz text:not([fill])) { fill: var(--viz-ink); }

/* ---- Axes (d3: add class viz-axis to the axis <g>) ------------------------ */
.viz .viz-field         { fill: var(--viz-field); }
.viz .viz-grid line     { stroke: var(--viz-grid); stroke-width: var(--viz-stroke-grid); shape-rendering: crispEdges; }
.viz .viz-grid .domain  { display: none; }
.viz .viz-axis .domain  { stroke: var(--viz-spine); stroke-width: var(--viz-stroke-spine); shape-rendering: crispEdges; }
.viz .viz-axis .tick line { stroke: var(--viz-tick); stroke-width: 1px; }
.viz .viz-axis .tick text {
  fill: var(--viz-ink); font-size: var(--viz-fs-tick); font-weight: var(--viz-w-light);
}
.viz .viz-axis--minor .tick text { display: none; }  /* log minor ticks: marks only */
.viz .viz-axis--minor .tick line { stroke-width: 0.6px; }
.viz .viz-axis-label {
  fill: var(--viz-ink); font-size: var(--viz-fs-label); font-weight: var(--viz-w-light);
}
.viz .viz-axis-label .viz-name-part { font-weight: var(--viz-w-regular); } /* "Pythia-6.9B" in Regular, metric Light */

/* ---- Marks --------------------------------------------------------------- */
.viz .viz-line   { fill: none; stroke-width: var(--viz-stroke-line); stroke-linecap: round; stroke-linejoin: round; }
.viz .viz-fit    { fill: none; stroke-width: var(--viz-stroke-fit); stroke-dasharray: var(--viz-dash-fit); }
.viz .viz-point  { stroke: var(--viz-field); stroke-width: var(--viz-marker-edge); }
.viz .viz-ref    { fill: var(--viz-ref-fill); stroke: var(--viz-ref-edge); stroke-width: 1.5px; }
.viz .viz-err    { stroke-width: var(--viz-stroke-err); opacity: 0.5; }
.viz .viz-band   { opacity: 1; }  /* fill with a *-tint token, never alpha over the grid */
.viz .viz-name   { font-size: var(--viz-fs-name); font-weight: var(--viz-w-regular); }
.viz .viz-note   { font-size: var(--viz-fs-note); font-weight: var(--viz-w-light); fill: var(--viz-ink-2); }

/* Semantic stroke/fill helpers (SVG). On <text> they set only the fill, in
   the hue's text shade (600 light / 300 dark): a stroke on glyphs would
   outline them (faux bold). */
.viz .is-rosetta     { stroke: var(--viz-rosetta);    fill: var(--viz-rosetta); }
.viz .is-nonrosetta  { stroke: var(--viz-nonrosetta); fill: var(--viz-nonrosetta); }
.viz .is-null        { stroke: var(--viz-null);       fill: var(--viz-null); }
.viz .is-set-1 { stroke: var(--viz-set-1); fill: var(--viz-set-1); }
.viz .is-set-2 { stroke: var(--viz-set-2); fill: var(--viz-set-2); }
.viz .is-set-3 { stroke: var(--viz-set-3); fill: var(--viz-set-3); }
.viz .is-set-4 { stroke: var(--viz-set-4); fill: var(--viz-set-4); }
.viz .is-set-5 { stroke: var(--viz-set-5); fill: var(--viz-set-5); }
.viz .viz-line.is-rosetta, .viz .viz-line.is-nonrosetta, .viz .viz-line.is-null,
.viz .viz-fit[class*="is-"], .viz .viz-line[class*="is-set-"] { fill: none; }
.viz text.is-rosetta,    .viz tspan.is-rosetta    { fill: var(--viz-rosetta-text);    stroke: none; }
.viz text.is-nonrosetta, .viz tspan.is-nonrosetta { fill: var(--viz-nonrosetta-text); stroke: none; }
.viz text.is-null,       .viz tspan.is-null       { fill: var(--viz-null-text);       stroke: none; }
.viz text.is-set-1, .viz tspan.is-set-1 { fill: var(--viz-set-1-text); stroke: none; }
.viz text.is-set-2, .viz tspan.is-set-2 { fill: var(--viz-set-2-text); stroke: none; }
.viz text.is-set-3, .viz tspan.is-set-3 { fill: var(--viz-set-3-text); stroke: none; }
.viz text.is-set-4, .viz tspan.is-set-4 { fill: var(--viz-set-4-text); stroke: none; }
.viz text.is-set-5, .viz tspan.is-set-5 { fill: var(--viz-set-5-text); stroke: none; }
.viz .viz-name.is-rosetta    { fill: var(--viz-rosetta-text);    stroke: none; }
.viz .viz-name.is-nonrosetta { fill: var(--viz-nonrosetta-text); stroke: none; }
.viz .viz-name.is-null       { fill: var(--viz-null-text);       stroke: none; }
.viz .viz-name.is-set-1 { fill: var(--viz-set-1-text); stroke: none; }
.viz .viz-name.is-set-2 { fill: var(--viz-set-2-text); stroke: none; }
.viz .viz-name.is-set-3 { fill: var(--viz-set-3-text); stroke: none; }
.viz .viz-name.is-set-4 { fill: var(--viz-set-4-text); stroke: none; }
.viz .viz-name.is-set-5 { fill: var(--viz-set-5-text); stroke: none; }

/* HTML text helpers (prose, captions, controls) */
.viz-t-rosetta    { color: var(--viz-rosetta-text); }
.viz-t-nonrosetta { color: var(--viz-nonrosetta-text); }
.viz-t-null       { color: var(--viz-null-text); }

/* ==========================================================================
   Chip: the one token chip. IBM Plex Mono in ink on a light fill, ink edge,
   soft shadow falling down-right. Used for literal tokens as the model sees
   them (\n\nOkay, ' JavaScript'), neuron ids (L16/U11168), and code.
   ========================================================================== */
.viz-chip {
  display: inline-block;
  font-family: var(--viz-font-mono);
  font-weight: 400;
  font-size: var(--viz-fs-chip);
  line-height: 1;
  color: var(--viz-chip-text);
  background: var(--viz-chip-fill);
  border: 1px solid var(--viz-chip-edge);
  border-radius: var(--viz-radius-chip);
  padding: 0.33em 0.39em 0.3em;
  box-shadow: var(--viz-chip-shadow);
  white-space: pre;
  vertical-align: 0.08em;
}
/* A highlighted chip: edge in the hue's mark, fill in its tint, text ink. */
.viz-chip--rosetta    { border-color: var(--viz-rosetta);    background: var(--viz-rosetta-tint); }
.viz-chip--nonrosetta { border-color: var(--viz-nonrosetta); background: var(--viz-nonrosetta-tint); }
.viz-chip--act        { border-color: var(--viz-act-edge);   background: rgba(var(--viz-act-rgb), 0.25); }
.viz-chip--quiet      { box-shadow: none; border-color: var(--viz-ctl-edge); } /* chips in dense lists */

/* ==========================================================================
   Token strip: a run of tokens with per-token activation (teaser,
   neuron viewer, JavaScript filter). JS sets --a (0..1) on each .viz-tok.
   ========================================================================== */
.viz-tokens {
  font-family: var(--viz-font-mono);
  font-size: var(--viz-fs-chip);
  line-height: 2.0;
  color: var(--viz-ink);
  white-space: pre-wrap;
  word-break: break-word;
}
.viz-tok {
  --a: 0;
  border-radius: 3px;
  padding: 0.12em 0.05em;
  background: rgba(var(--viz-act-rgb), calc(var(--a) * var(--viz-act-max)));
  transition: background-color var(--viz-dur-hover) var(--viz-ease-in);
}
.viz-tok.is-max { box-shadow: 0 0 0 1.5px var(--viz-act-edge); }

/* ==========================================================================
   Cards: legend (last resort) and tooltip. Square, hairline ink edge.
   ========================================================================== */
.viz-legend, .viz-tooltip {
  font-family: var(--viz-font-sans);
  font-size: var(--viz-fs-tooltip);
  font-weight: var(--viz-w-light);
  color: var(--viz-ink);
  background: var(--viz-card-fill);
  border: 1px solid var(--viz-card-edge);
  border-radius: var(--viz-radius-card);
  padding: 4px 7px;
  line-height: 1.35;
}
.viz-tooltip {
  position: absolute;
  pointer-events: none;
  box-shadow: var(--viz-card-shadow);
  opacity: 0;
  transition: opacity var(--viz-dur-hover) var(--viz-ease-in);
  max-width: 280px;
  z-index: 5;
}
.viz-tooltip.is-on { opacity: 1; }
.viz-tooltip b, .viz-legend b { font-weight: var(--viz-w-regular); }
.viz-tooltip .viz-num { font-weight: var(--viz-w-regular); }

/* ==========================================================================
   Controls: segmented toggle (Language | Vision), stepper/replay buttons,
   slider. Quiet by default, ink when active.
   ========================================================================== */
.viz-controls {
  display: flex; flex-wrap: wrap; align-items: center;
  gap: 8px 16px; margin: 0 0 10px;
  font-family: var(--viz-font-sans); font-size: var(--viz-fs-ui); font-weight: var(--viz-w-light);
  color: var(--viz-ink);
}
.viz-seg { display: inline-flex; border: 1px solid var(--viz-ctl-edge); border-radius: 999px; overflow: hidden; background: var(--viz-ctl-fill); }
.viz-seg button, .viz-btn {
  font: inherit; color: var(--viz-ink); background: transparent; border: 0;
  padding: 5px 12px; cursor: pointer; min-height: 32px;
  transition: background-color var(--viz-dur-hover) var(--viz-ease-in), color var(--viz-dur-hover) var(--viz-ease-in), border-color var(--viz-dur-hover) var(--viz-ease-in);
}
.viz-seg button[aria-pressed="true"] { background: var(--viz-ctl-on-fill); color: var(--viz-ctl-on-text); font-weight: var(--viz-w-regular); }
.viz-btn { border: 1px solid var(--viz-ctl-edge); border-radius: 999px; background: var(--viz-ctl-fill); }
.viz-btn:hover, .viz-seg:hover { border-color: var(--viz-ctl-edge-hover); }
.viz-seg button:focus-visible, .viz-btn:focus-visible, .viz-range:focus-visible {
  outline: 2px solid var(--viz-focus); outline-offset: 2px;
}
.viz-range { accent-color: var(--viz-ink); width: 100%; max-width: 320px; }
.viz-readout { font-variant-numeric: tabular-nums; font-weight: var(--viz-w-regular); min-width: 4ch; display: inline-block; }

/* ==========================================================================
   Motion helpers
   ========================================================================== */
.viz-fade        { transition: opacity var(--viz-dur-fade) var(--viz-ease-in); }
.viz-move        { transition: transform var(--viz-dur-move) var(--viz-ease-move); }
.viz-hidden      { opacity: 0; }

@media (prefers-reduced-motion: reduce) {
  :root {
    --viz-dur-fade: 0ms; --viz-dur-move: 0ms; --viz-dur-draw: 0ms;
    --viz-dur-pause: 0ms; --viz-dur-hold: 0ms; --viz-dur-stagger: 0ms;
    --viz-dur-hover: 0ms;
  }
  .viz *, .viz-tok, .viz-tooltip { transition: none !important; animation: none !important; }
}
