/* ==========================================================================
   The research track: how the model was trained, stage by stage, and what its
   layers learned. The prose is the essay's; this file styles the charts.

   The charts follow the line charts of Jasper Lu's "Training Search Agents
   with GRPO" (jasperlu.com/blog/training-search-agents-grpo), as the author
   asked: small panels in a row, a hairline grid, mono ticks, a faint line for
   the logged values under a solid line for their running mean, one crosshair
   and one readout for every series, and a legend whose keys switch series off.
   The series colours are that post's own (--plot-* in styles.css). Hairlines,
   inks and surfaces are this page's tokens, so a chart sits on this paper the
   way the original sits on its own.

   Type inside a chart never falls below 13px on screen. A panel is a drawing
   that scales with its column, so its type size is held in drawing units
   (--fs); research.js keeps that at 13px as the width changes, and the rows
   below never let a panel shrink under 288px, the width the margins are cut
   for. Without the script the size is the one right for the widest layout.

   Defines no tokens.
   ========================================================================== */

/* ---------- Tones: one custom property carries a series' colour ---------- */

.t-lead { --c: var(--plot-lead); }
.t-dose1 { --c: var(--plot-dose1); }
.t-dose2 { --c: var(--plot-dose2); }
.t-dose3 { --c: var(--plot-dose3); }
.t-bad { --c: var(--plot-bad); }
.t-good { --c: var(--plot-good); }
.t-quiet { --c: var(--plot-quiet); }

/* ---------- A chart: rows of panels, then one legend ---------- */

.chart {
  container-type: inline-size;
  margin: 0;
}

.chart__row {
  display: grid;
  grid-template-columns: repeat(var(--cols, 3), minmax(0, var(--panel-max, 21.25rem)));
  justify-content: center;
  align-items: start;
  gap: 1.5rem;
}

.chart__row + .chart__row {
  margin-top: 1.5rem;
}

/* three across needs the whole figure (3 x 288px and two gaps); below that, two;
   under 2 x 288px and a gap, one, as wide as the essay's phone drawings */
@container (width < 57rem) {
  .chart__row {
    grid-template-columns: repeat(min(var(--cols, 3), 2), minmax(0, var(--panel-max, 21.25rem)));
  }
}

@container (width < 37.5rem) {
  .chart__row {
    grid-template-columns: minmax(0, 26rem);
    gap: 1.25rem;
  }
}

/* ---------- A panel ---------- */

.lc {
  min-width: 0;
}

.lc__title {
  margin: 0 0 0.25rem;
  color: var(--muted);
  font: 400 0.8125rem/1.4 var(--mono);
  font-variant-ligatures: none;
  letter-spacing: 0.05em;
  text-transform: uppercase;
}

.lc__plot {
  position: relative;
  border-radius: 3px;
}

.lc__svg {
  display: block;
  width: 100%;
  height: auto;
  overflow: visible;
  touch-action: pan-y;
}

.lc__svg text {
  font-family: var(--mono);
  font-size: var(--fs, 12.25px);
  font-variant-ligatures: none;
  font-variant-numeric: tabular-nums;
  fill: var(--muted);
}

.lc__grid {
  stroke: var(--line);
  stroke-width: 1;
}

.lc__grid.is-zero {
  stroke: var(--line-strong);
}

.lc__ty { text-anchor: end; }
.lc__tx { text-anchor: middle; }

/* a series: the logged values faint, their running mean solid */
.lc__ln {
  fill: none;
  stroke: var(--c);
  stroke-width: 1.75;
  stroke-linejoin: round;
  stroke-linecap: round;
}

.lc__ln.is-raw {
  opacity: 0.2;
}

.lc__ln.is-dashed {
  stroke-dasharray: 5 4;
}

.lc__dt {
  fill: var(--c);
  stroke: var(--paper);
  stroke-width: 1;
}

.lc__dt.is-raw {
  opacity: 0.25;
}

/* an interval round a point, and a band round a line */
.lc__bar {
  stroke: var(--c);
  stroke-width: 1;
}

.lc__band {
  fill: var(--c);
  opacity: 0.12;
}

/* a value that stands on the floor of the plot: a stem and its point */
.lc__stem {
  stroke: var(--c);
  stroke-width: 1.75;
  stroke-linecap: round;
}

/* a point that is named: a ring in the series colour, its name in ink beside it */
.lc__mark circle {
  fill: var(--paper);
  stroke: var(--c);
  stroke-width: 1.75;
}

.lc__svg .lc__mark text {
  fill: var(--ink-soft);
}

.lc__mark.is-off {
  display: none;
}

/* a reference: a level or a moment, dashed, with its name */
.lc__ref line {
  stroke: var(--faint);
  stroke-width: 1;
  stroke-dasharray: 3 3;
}

.lc__ref text {
  text-anchor: end;
}

.lc__ref.is-start text {
  text-anchor: start;
}

/* a series switched off by its key */
.lc__s.is-off {
  display: none;
}

/* ---------- The readout: a crosshair, a marker on every line, one card ---------- */

.lc__cross {
  stroke: var(--line-strong);
  stroke-width: 1;
  pointer-events: none;
}

.lc__mk {
  fill: var(--c);
  stroke: var(--paper);
  stroke-width: 1;
  pointer-events: none;
}

.lc__tip {
  position: absolute;
  top: 4px;
  z-index: 4;
  display: flex;
  flex-direction: column;
  gap: 1px;
  padding: 6px 9px;
  border: 1px solid var(--line-strong);
  border-radius: 2px;
  background: var(--paper-raised);
  color: var(--muted);
  font: 400 0.8125rem/1.5 var(--mono);
  font-variant-ligatures: none;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  pointer-events: none;
}

/* a card too large to stand inside the plot goes under it, not over it */
.lc__tip.is-below {
  top: calc(100% + 4px);
}

.lc__tip[hidden] {
  display: none;
}

.lc__tip-x {
  margin-bottom: 2px;
  color: var(--ink);
  font-weight: 500;
}

.lc__tip-row {
  display: flex;
  align-items: center;
  gap: 6px;
}

.lc__tip-row b {
  margin-left: auto;
  padding-left: 12px;
  color: var(--ink);
  font-weight: 500;
}

.lc__tip-row i {
  flex: none;
  width: 12px;
  height: 2px;
  background: var(--c);
}

.lc__tip-row i.is-dashed {
  height: 0;
  border-top: 2px dashed var(--c);
  background: none;
}

.lc__tip-row small {
  margin-left: 6px;
  color: var(--muted);
  font-size: inherit;
}

.lc__plot:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 4px;
}

/* ---------- The legend: every key is a switch ---------- */

.chart__legend {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 0.25rem 1rem;
  margin-top: 1rem;
  color: var(--muted);
  font: 400 0.8125rem/1.4 var(--mono);
  font-variant-ligatures: none;
}

.chart__key {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-height: 1.75rem;
  padding: 2px 4px;
  border: 0;
  border-radius: 3px;
  background: none;
  color: inherit;
  font: inherit;
  cursor: pointer;
  transition: opacity 0.15s ease-out, color 0.15s ease-out;
}

button.chart__key:hover {
  color: var(--ink);
}

span.chart__key {
  cursor: default;
}

.chart__key i {
  flex: none;
  width: 14px;
  height: 2.5px;
  background: var(--c);
}

.chart__key i.is-dashed {
  height: 0;
  border-top: 2.5px dashed var(--c);
  background: none;
}

.chart__key[aria-pressed="false"] {
  opacity: 0.4;
}

.chart__key[aria-pressed="false"] i {
  background: var(--faint);
  border-top-color: var(--faint);
}

/* a line of plain words under the legend: what the faint and the solid lines are */
.chart__how {
  max-width: var(--measure);
  margin: 0.5rem auto 0;
  color: var(--muted);
  font: 400 0.8125rem/1.5 var(--mono);
  font-variant-ligatures: none;
  text-align: center;
  text-wrap: balance;
}

/* ---------- A chart of named arms: one row each, a point and its interval ---------- */

.dg .pl-name {
  fill: var(--ink-soft);
  font: 400 13px var(--sans);
}

.dg .pl-name.is-ours {
  fill: var(--ink);
  font-weight: 600;
}

.dg .pl-group {
  fill: var(--muted);
  font: 400 13px var(--mono);
  letter-spacing: 0.05em;
  text-transform: uppercase;
}

.dg .pl-tick,
.dg .pl-val {
  fill: var(--muted);
  font: 400 13px var(--mono);
  font-variant-numeric: tabular-nums;
}

.dg .pl-val {
  fill: var(--ink-soft);
}

.dg .pl-val.is-ours {
  fill: var(--ink);
  font-weight: 500;
}

.dg--tall :is(.pl-name, .pl-group, .pl-tick, .pl-val) {
  font-size: 14.5px;
}

.dg .pl-grid {
  stroke: var(--line);
  stroke-width: 1;
}

.dg .pl-grid.is-zero {
  stroke: var(--line-strong);
}

.dg .pl-whisker {
  stroke: var(--c);
  stroke-width: 1.5;
  stroke-linecap: round;
}

.dg .pl-dot {
  fill: var(--c);
  stroke: var(--paper);
  stroke-width: 1.5;
}

.dg :is(.pl-dot, .pl-whisker).is-off {
  display: none;
}

.dg .pl-stem {
  stroke: var(--c);
  stroke-width: 1.75;
  stroke-linecap: round;
}

.dg .pl-band {
  fill: var(--paper-sunk);
}

/* several points on one row: a hairline across their range */
.dg .pl-span {
  stroke: var(--line-strong);
  stroke-width: 1;
}

/* before and after: the earlier value quiet, a line to the later one */
.dg .pl-link {
  stroke: var(--c);
  stroke-width: 1.75;
  stroke-linecap: round;
  opacity: 0.45;
}

.dg .pl-dot.is-from {
  fill: var(--paper);
  stroke: var(--c);
  stroke-width: 1.5;
}

/* a value beyond the scale: an arrow at the edge it lies past */
.dg .pl-off {
  fill: none;
  stroke: var(--c);
  stroke-width: 1.5;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.dg .pl-row-hit {
  fill: transparent;
}

.dg .pl-row:hover .pl-row-hit,
.dg .pl-row:focus-visible .pl-row-hit {
  fill: var(--paper-sunk);
}

.dg .pl-row:focus-visible {
  outline: none;
}

.dg .pl-row:focus-visible .pl-name {
  fill: var(--ink);
  text-decoration: underline;
}

/* a chart of arms is drawn across only where its 880 units are 880px or more, so its 13
   units of type are 13px; narrower, the tall drawings take over (one per measure) */
@container (width < 55rem) {
  .figure--arms .dg--wide {
    display: none;
  }

  .figure--arms .dg--tall {
    display: block;
  }
}

.figure--arms .dg--tall + .dg--tall {
  margin-top: 1.75rem;
}

.figure--arms .figure__canvas + .chart .chart__legend {
  margin-top: 0.75rem;
}

/* a row of panels above a chart of arms, in one figure */
.chart .chart__row + .figure__canvas {
  margin-top: 2rem;
}

/* ---------- The frame of a chart figure ---------- */

/* the notes under a chart's title carry facts (hours, epochs, units): the floor is 13px */
.figure--chart .figure__sub {
  font-size: 0.8125rem;
}

.figure--chart .figure__data {
  margin-top: 1rem;
}

/* ---------- Stage heads inside the essay ---------- */

.prose h4.stage {
  max-width: var(--measure);
  margin: 2.6em auto 0.6em;
  font: 500 clamp(1.2rem, 1.08rem + 0.45vw, 1.4rem)/1.25 var(--display);
  letter-spacing: -0.006em;
  text-wrap: balance;
}

.prose h4.stage .stage__no {
  display: block;
  margin-bottom: 0.4em;
  color: var(--accent);
  font: 600 0.71875rem/1 var(--sans);
  letter-spacing: 0.14em;
  text-transform: uppercase;
}

/* what a stage was, in one line of facts under its head */
.prose .stage__facts {
  max-width: var(--measure);
  margin: 0 auto 1.1em;
  color: var(--muted);
  font: 400 0.8125rem/1.6 var(--mono);
  font-variant-ligatures: none;
}

/* ---------- Motion and print ---------- */

@media (prefers-reduced-motion: reduce) {
  .chart__key {
    transition: none;
  }
}

@media print {
  .lc__tip,
  .lc__cross,
  .lc__mk {
    display: none !important;
  }

  .chart__key[aria-pressed="false"] {
    opacity: 1;
  }
}
