/* "See it in action" drawer, shared by every technique (assets/sim_engine.js + recipes).
   Colour code inside the drawer: blue = a value flowing forward (rose when negative),
   amber = a gradient flowing backward, magenta = the error slope p − y that leaves the loss,
   violet = a weight, teal = training data,
   red = the loss, green = the update. The chapter colour (--ph-c) follows the phase. */

:root {
  --sim-blue: #2457e6;
  --sim-pos: #2f6bff;
  --sim-neg: #e0356a;
  --sim-grad: #e08a00;
  --sim-grad-ink: #9a5800;
  --sim-grad-bg: #fff3dc;
  --sim-err: #c026d3;
  --sim-err-ink: #9d1aa8;
  --sim-err-bg: #fbe8fd;
  --sim-on: #ffffff;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --sim-blue: #7aa3ff;
    --sim-pos: #4f86ff;
    --sim-neg: #ff5c8a;
    --sim-grad: #ffb02e;
    --sim-grad-ink: #ffc560;
    --sim-grad-bg: #33260f;
    --sim-err: #e879f9;
    --sim-err-ink: #f0a6fb;
    --sim-err-bg: #3b1640;
    --sim-on: #071226;
  }
}
:root[data-theme="dark"] {
  --sim-blue: #7aa3ff;
  --sim-pos: #4f86ff;
  --sim-neg: #ff5c8a;
  --sim-grad: #ffb02e;
  --sim-grad-ink: #ffc560;
  --sim-grad-bg: #33260f;
  --sim-err: #e879f9;
  --sim-err-ink: #f0a6fb;
  --sim-err-bg: #3b1640;
  --sim-on: #071226;
}

/* ---------- the button on the page ---------- */
.sim-cta { display: flex; flex-wrap: wrap; align-items: center; gap: 10px 20px; margin-top: 6px; }
.sim-cta p { max-width: 46ch; }
.sim-btn {
  display: inline-flex;
  align-items: center;
  gap: 14px;
  padding: 13px 28px 13px 13px;
  border: 0;
  border-radius: 18px;
  background: linear-gradient(135deg, #4a86ff 0%, #2457e6 52%, #1a3fbf 100%);
  color: #fff;
  font-family: var(--f-display);
  font-weight: 700;
  font-size: 1.16rem;
  line-height: 1.15;
  text-align: left;
  cursor: pointer;
  box-shadow: 0 14px 30px -12px rgb(36 87 230 / 0.75), inset 0 1px 0 rgb(255 255 255 / 0.28);
  transition: transform 0.18s ease, box-shadow 0.2s ease, filter 0.2s ease;
}
.sim-btn:hover { transform: translateY(-2px); filter: brightness(1.06); box-shadow: 0 20px 36px -14px rgb(36 87 230 / 0.85), inset 0 1px 0 rgb(255 255 255 / 0.28); }
.sim-btn:active { transform: translateY(0); }
.sim-btn:focus-visible { outline: 3px solid #9dbcff; outline-offset: 3px; }
.sim-btn small { display: block; margin-top: 3px; font-family: var(--f-body); font-weight: 500; font-size: 0.8rem; opacity: 0.86; }
.sim-btn .ico {
  position: relative;
  flex: none;
  width: 42px;
  height: 42px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  background: rgb(255 255 255 / 0.2);
}
.sim-btn .ico svg { width: 20px; height: 20px; margin-left: 2px; }
.sim-btn .ico::after {
  content: "";
  position: absolute;
  inset: -3px;
  border-radius: 50%;
  border: 2px solid rgb(255 255 255 / 0.6);
  animation: simRing 2.4s ease-out infinite;
}
.sim-btn.sm { padding: 8px 18px 8px 8px; font-size: 0.98rem; border-radius: 14px; gap: 10px; }
.sim-btn.sm .ico { width: 30px; height: 30px; }
.sim-btn.sm .ico svg { width: 16px; height: 16px; margin: 0; }
.sim-btn.sm .ico::after { display: none; }

/* ---------- drawer frame ---------- */
.sim-scrim {
  position: fixed;
  inset: 0;
  z-index: 90;
  background: rgb(5 9 15 / 0.52);
  backdrop-filter: blur(2px);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.35s ease;
}
.sim-scrim.show { opacity: 1; pointer-events: auto; }
.sim-drawer {
  --ph-c: var(--sim-blue);
  --spd: 1;
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  z-index: 91;
  width: 70vw;
  display: grid;
  grid-template-rows: auto auto minmax(0, 1fr) auto;
  background: var(--paper);
  border-left: 1px solid var(--rule);
  box-shadow: -30px 0 70px -24px rgb(0 0 0 / 0.45);
  color: var(--ink);
  outline: none;
  transform: translateX(104%);
  visibility: hidden;
  transition: transform 0.5s cubic-bezier(0.2, 0.8, 0.2, 1), visibility 0s linear 0.5s;
}
.sim-drawer.open { transform: none; visibility: visible; transition: transform 0.5s cubic-bezier(0.2, 0.8, 0.2, 1), visibility 0s; }
.sim-drawer[data-ph="data"] { --ph-c: var(--answer); }
.sim-drawer[data-ph="loss"] { --ph-c: var(--bad); }
.sim-drawer[data-ph="bwd"] { --ph-c: var(--sim-grad); }
.sim-drawer[data-ph="upd"] { --ph-c: var(--good); }
.sim-drawer[data-ph="gpu"] { --ph-c: var(--teacher); }
/* Below 1280px, 70% leaves the diagram less than the 530px a row of 9 logits needs, so the
   drawer takes the whole width. */
@media (max-width: 1279px) { .sim-drawer { width: 100vw; } }

.sim-head { display: flex; flex-wrap: wrap; align-items: flex-start; justify-content: space-between; gap: 10px 16px; padding: 16px 20px 10px; background: var(--surface); }
.sim-head-c { display: flex; flex-wrap: wrap; align-items: center; justify-content: flex-end; gap: 10px; margin-left: auto; }
.sim-speed.sim-var button { font-family: var(--f-display); }
.sim-layer {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 6px 12px 6px 8px;
  border-radius: 999px;
  border: 1px solid var(--rule);
  background: var(--surface);
  font-family: var(--f-display);
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--ink-2);
  cursor: pointer;
}
.sim-switch { position: relative; width: 30px; height: 17px; border-radius: 999px; background: var(--well); outline: 1px solid var(--rule); transition: background-color 0.2s; }
.sim-switch::after { content: ""; position: absolute; top: 2px; left: 2px; width: 13px; height: 13px; border-radius: 50%; background: var(--ink-3); transition: transform 0.2s, background-color 0.2s; }
.sim-layer[aria-pressed="true"] { color: var(--ink); border-color: var(--sim-blue); }
.sim-layer[aria-pressed="true"] .sim-switch { background: color-mix(in srgb, var(--sim-blue) 35%, var(--surface)); }
.sim-layer[aria-pressed="true"] .sim-switch::after { transform: translateX(13px); background: var(--sim-blue); }
.sim-head .eyebrow { color: var(--sim-blue); }
.sim-head h2 { font-size: 1.4rem; margin-top: 2px; }

.sim-rail {
  display: flex;
  gap: 6px;
  padding: 2px 20px 12px;
  overflow-x: auto;
  scrollbar-width: none;
  background: var(--surface);
  border-bottom: 1px solid var(--rule);
}
.sim-rail::-webkit-scrollbar { display: none; }
.sim-chip {
  --cc: var(--sim-blue);
  flex: none;
  padding: 5px 13px;
  border-radius: 999px;
  border: 1px solid var(--rule);
  background: var(--surface);
  color: var(--ink-3);
  font-family: var(--f-display);
  font-weight: 600;
  font-size: 0.8rem;
  cursor: pointer;
  transition: background-color 0.25s, color 0.25s, border-color 0.25s;
}
.sim-chip[data-ph="data"] { --cc: var(--answer); }
.sim-chip[data-ph="loss"] { --cc: var(--bad); }
.sim-chip[data-ph="bwd"] { --cc: var(--sim-grad); }
.sim-chip[data-ph="upd"] { --cc: var(--good); }
.sim-chip[data-ph="gpu"] { --cc: var(--teacher); }
.sim-chip.done { color: var(--ink); border-color: color-mix(in srgb, var(--cc) 45%, var(--rule)); }
.sim-chip.cur { background: var(--cc); border-color: var(--cc); color: var(--sim-on); }
.sim-chip.cur[data-ph="bwd"] { color: #241600; }
.sim-chip:hover { border-color: var(--cc); }

.sim-body { display: grid; grid-template-columns: minmax(0, 1.5fr) minmax(330px, 1fr); min-height: 0; }
.sim-body > * { min-height: 0; }
@media (max-width: 860px) {
  .sim-body { grid-template-columns: minmax(0, 1fr); grid-template-rows: minmax(0, 1.05fr) minmax(0, 1fr); }
  .sim-ex { border-left: 0 !important; border-top: 1px solid var(--rule); }
  /* On a phone every pixel of height goes to the diagram and the explanation. */
  .sim-head { padding: 10px 14px 6px; }
  .sim-head h2 { font-size: 1.1rem; }
  .sim-rail { padding: 0 14px 8px; }
  .sim-legend .sim-tip { display: none; }
  .sim-legend { padding: 5px 14px; }
  /* The colour key is the first thing to go: the sequence tabs in the same bar must stay. */
  .sim-legend > span:not(.sim-tip) { display: none; }
}

/* ---------- controls ---------- */
.sim-foot { display: flex; flex-wrap: wrap; align-items: center; gap: 10px 16px; padding: 10px 20px 12px; background: var(--surface); border-top: 1px solid var(--rule); }
.sim-btns { flex: none; display: flex; align-items: center; gap: 8px; }
.sim-ctl {
  flex: none;
  width: 38px;
  height: 38px;
  display: grid;
  place-items: center;
  border-radius: 10px;
  border: 1px solid var(--rule);
  background: var(--surface);
  color: var(--ink);
  cursor: pointer;
  transition: border-color 0.2s, transform 0.15s;
}
.sim-ctl svg { width: 20px; height: 20px; }
.sim-ctl:hover { border-color: var(--ink-2); }
.sim-ctl:active { transform: scale(0.95); }
.sim-ctl.play {
  width: 52px;
  height: 52px;
  border: 0;
  border-radius: 50%;
  color: #fff;
  background: linear-gradient(135deg, #4a86ff, #1f4fdc);
  box-shadow: 0 10px 22px -10px rgb(36 87 230 / 0.9);
}
.sim-ctl.play svg { width: 24px; height: 24px; }
.sim-ctl.close { flex: none; }
.sim-count { font-family: var(--f-mono); font-size: 0.8rem; font-weight: 600; color: var(--ink-2); min-width: 9.5ch; font-variant-numeric: tabular-nums; }
.sim-speed { display: inline-flex; border: 1px solid var(--rule); border-radius: 9px; overflow: hidden; }
.sim-speed button {
  border: 0;
  border-right: 1px solid var(--rule);
  background: var(--surface);
  padding: 6px 10px;
  font-family: var(--f-mono);
  font-size: 0.76rem;
  font-weight: 600;
  color: var(--ink-2);
  cursor: pointer;
}
.sim-speed button:last-child { border-right: 0; }
.sim-speed button[aria-pressed="true"] { background: var(--ink); color: var(--surface); }
.sim-scrub { flex: 1 1 260px; display: grid; grid-template-columns: repeat(var(--n), minmax(0, 1fr)); gap: 3px; min-width: 200px; align-items: center; }
.sim-tick {
  --tc: var(--sim-blue);
  position: relative;
  height: 10px;
  padding: 0;
  border: 0;
  border-radius: 3px;
  background: var(--well);
  overflow: hidden;
  cursor: pointer;
}
.sim-tick[data-ph="data"] { --tc: var(--answer); }
.sim-tick[data-ph="loss"] { --tc: var(--bad); }
.sim-tick[data-ph="bwd"] { --tc: var(--sim-grad); }
.sim-tick[data-ph="upd"] { --tc: var(--good); }
.sim-tick[data-ph="gpu"] { --tc: var(--teacher); }
.inline-tok.pad { background: transparent; color: var(--ink-3); outline: 1px dashed var(--rule); outline-offset: -1px; }
.sim-tick i { position: absolute; inset: 0 auto 0 0; width: 0; background: var(--tc); }
.sim-tick.cur { height: 14px; outline: 2px solid var(--tc); outline-offset: 1px; }
.sim-tick:hover { background: color-mix(in srgb, var(--tc) 30%, var(--well)); }
.sim-tick:focus-visible { outline: 3px solid var(--tc); outline-offset: 2px; }

/* ---------- the network diagram ---------- */
.sim-diagram {
  position: relative;
  overflow: auto;
  overscroll-behavior: contain;
  background-color: var(--paper);
  background-image: radial-gradient(color-mix(in srgb, var(--ink-3) 26%, transparent) 1px, transparent 1.3px);
  background-size: 18px 18px;
  background-attachment: local;
}
.sim-legend {
  position: sticky;
  /* left too: on a narrow pane the diagram scrolls sideways to the focused node. */
  top: 0;
  left: 0;
  z-index: 6;
  display: flex;
  flex-wrap: wrap;
  gap: 4px 16px;
  padding: 7px 18px;
  font-family: var(--f-display);
  font-size: 0.76rem;
  color: var(--ink-2);
  background: color-mix(in srgb, var(--paper) 86%, transparent);
  backdrop-filter: blur(6px);
  border-bottom: 1px solid var(--rule);
}
.sim-legend span { display: inline-flex; align-items: center; gap: 6px; }
.sim-legend i { width: 13px; height: 13px; border-radius: 3px; display: inline-block; }
.sim-legend .sw-pos { background: color-mix(in srgb, var(--sim-pos) 45%, var(--surface)); }
.sim-legend .sw-neg { background: color-mix(in srgb, var(--sim-neg) 45%, var(--surface)); }
.sim-legend .sw-grad { background: color-mix(in srgb, var(--sim-grad) 45%, var(--surface)); outline: 1px solid var(--sim-grad); }
.sim-legend .sw-err { background: color-mix(in srgb, var(--sim-err) 45%, var(--surface)); outline: 1px solid var(--sim-err); }
.sim-legend .sw-focus { background: var(--surface); box-shadow: 0 0 0 2px var(--ph-c); }
.sim-legend .sim-tip { margin-left: auto; font-style: italic; color: var(--ink-3); }
.sim-passes { display: inline-flex; flex-wrap: wrap; align-items: center; gap: 4px; }
.sim-passes > span { color: var(--ink-3); }
.sim-passes button {
  padding: 2px 9px;
  border-radius: 999px;
  border: 1px solid var(--rule);
  background: var(--surface);
  font-family: var(--f-display);
  font-size: 0.74rem;
  color: var(--ink-2);
  cursor: pointer;
}
.sim-passes button[aria-pressed="true"] { background: var(--ink); border-color: var(--ink); color: var(--surface); }
.sim-net [data-n] { cursor: pointer; }
.sim-net .sim-n:hover { box-shadow: 0 0 0 1.5px var(--ink-2); }
.sim-sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

/* isolation: the link layer (z 1) must sit between the card backgrounds and the nodes (z 2). */
/* min-width keeps a row of 9 logits or 9 positions from overlapping; narrower panes scroll sideways instead. */
.sim-net { position: relative; isolation: isolate; display: grid; gap: 22px; padding: 16px 12px 56px; min-width: 520px; }
.sim-links { position: absolute; left: 0; top: 0; z-index: 1; pointer-events: none; overflow: visible; }

.sim-card {
  position: relative;
  display: grid;
  gap: 12px;
  padding: 12px 14px 14px;
  background: var(--surface);
  border: 1px solid var(--rule);
  border-radius: 14px;
  transition: border-color 0.35s, box-shadow 0.35s;
}
.sim-card + .sim-card::before {
  content: "";
  position: absolute;
  left: 50%;
  top: -21px;
  height: 20px;
  border-left: 2px dotted color-mix(in srgb, var(--ink-3) 60%, transparent);
}
.sim-card.cur { border-color: color-mix(in srgb, var(--ph-c) 75%, transparent); box-shadow: 0 0 0 4px color-mix(in srgb, var(--ph-c) 14%, transparent), var(--shadow); }
.sim-card-h { display: flex; align-items: baseline; gap: 10px; }
.sim-card-n {
  flex: none;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  align-self: center;
  background: var(--well);
  color: var(--ink-2);
  font-family: var(--f-mono);
  font-size: 0.72rem;
  font-weight: 700;
  transition: background-color 0.3s, color 0.3s;
}
.sim-card.cur .sim-card-n { background: var(--ph-c); color: var(--sim-on); }
.sim-drawer[data-ph="bwd"] .sim-card.cur .sim-card-n { color: #241600; }
.sim-card-h h4 { font-size: 0.95rem; }
.sim-card-h p { font-size: 0.78rem; color: var(--ink-2); line-height: 1.35; }
.sim-card[data-card="after"]:not(.live) > :not(.sim-card-h),
.sim-card[data-card="loss"]:not(.live) > :not(.sim-card-h) { opacity: 0.18; filter: blur(3px); }
.sim-card[data-card="loss"]:not(.live)::after,
.sim-card[data-card="after"]:not(.live)::after {
  content: "Runs after the update";
  position: absolute;
  inset: 44px 0 0;
  display: grid;
  place-items: center;
  font-family: var(--f-display);
  font-size: 0.85rem;
  color: var(--ink-3);
}
.sim-card[data-card="loss"]:not(.live)::after { content: "Computed after the forward pass"; }
.sim-card-h { flex-wrap: wrap; }
.sim-card-h > div { flex: 1 1 auto; min-width: 0; }

.sim-rl { font-family: var(--f-display); font-size: 0.76rem; font-weight: 600; color: var(--ink-2); line-height: 1.2; }
.sim-tgrid { display: grid; gap: 8px; }
.sim-trow { display: grid; grid-template-columns: 66px repeat(var(--T, 5), minmax(46px, 1fr)); gap: 6px; align-items: center; }
.sim-tc, .sim-slot { display: flex; justify-content: center; min-width: 0; }
.sim-pos { font-family: var(--f-mono); font-size: 0.66rem; color: var(--ink-3); }
.sim-vec { display: grid; gap: 3px; }
.sim-na { color: var(--ink-3); opacity: 0.5; font-family: var(--f-mono); }

.sim-tok {
  position: relative;
  z-index: 2;
  padding: 4px 9px;
  border-radius: 7px;
  border: 1px solid transparent;
  font-family: var(--f-mono);
  font-size: 0.8rem;
  opacity: 0.4;
  font-variant-ligatures: none;
  transition: opacity 0.4s, box-shadow 0.3s, transform 0.3s;
}
.sim-tok.known { opacity: 1; }
.sim-tok.p { background: var(--prompt-bg); color: var(--ink); }
.sim-tok.a { background: var(--answer-bg); border-color: var(--answer); color: var(--ink); font-weight: 700; }
.sim-tok.w { background: var(--tag-bg); border-color: var(--tag); color: var(--tag); font-size: 0.7rem; font-weight: 700; }
.sim-tok.focus { box-shadow: 0 0 0 2px var(--ph-c); }
.sim-tok.op { box-shadow: 0 0 0 1.5px var(--answer); }

.sim-vocab { display: grid; grid-template-columns: 66px minmax(0, 1fr); gap: 6px; align-items: center; padding-top: 10px; border-top: 1px dashed var(--rule); }
.sim-voc-list { display: flex; flex-wrap: wrap; gap: 5px; }
.sim-voc {
  position: relative;
  z-index: 2;
  padding: 3px 9px;
  border-radius: 6px;
  background: var(--well);
  color: var(--ink-2);
  font-family: var(--f-mono);
  font-size: 0.74rem;
  transition: box-shadow 0.3s, background-color 0.3s;
}
.sim-voc b { color: var(--ink-3); margin-right: 6px; font-weight: 700; }
.sim-voc.op { background: color-mix(in srgb, var(--sim-blue) 18%, var(--surface)); color: var(--ink); box-shadow: 0 0 0 1.5px var(--sim-blue); }
.sim-voc.w { background: var(--tag-bg); color: var(--tag); }

/* ---------- a node: one number ---------- */
.sim-n {
  position: relative;
  z-index: 2;
  display: inline-grid;
  place-items: center;
  min-width: 48px;
  height: 22px;
  padding: 0 5px;
  border-radius: 6px;
  border: 1px dashed color-mix(in srgb, var(--ink-3) 45%, transparent);
  background: var(--surface);
  color: var(--ink-3);
  font-family: var(--f-mono);
  font-size: 0.66rem;
  font-variant-numeric: tabular-nums;
  font-variant-ligatures: none;
  line-height: 1;
  white-space: nowrap;
  transition: background-color 0.45s, border-color 0.45s, color 0.45s, box-shadow 0.3s, opacity 0.45s, transform 0.3s;
}
.sim-n.known { border: 1px solid transparent; color: var(--ink); background: color-mix(in srgb, var(--hc, var(--well)) calc(var(--h, 0) * 1%), var(--surface)); }
.sim-n.pos { --hc: var(--sim-pos); }
.sim-n.neg { --hc: var(--sim-neg); }
.sim-n.int { background: var(--well); }
.sim-n.grad { --hc: var(--sim-grad); color: var(--sim-grad-ink); border-color: color-mix(in srgb, var(--sim-grad) 65%, transparent); font-weight: 700; }
/* The logits' gradient is the error slope p − y: where the target enters the backward pass. */
.sim-n.grad.err { --hc: var(--sim-err); color: var(--sim-err-ink); border-color: color-mix(in srgb, var(--sim-err) 65%, transparent); }
.sim-n.sm { min-width: 40px; }
.sim-n.big { min-width: 40px; height: 28px; padding: 0 3px; border-radius: 14px; font-size: 0.62rem; }
.sim-n.loss { min-width: 120px; height: 42px; border-radius: 12px; font-size: 1.15rem; font-weight: 700; }
/* A light, fixed tint: red text on a strong red fill would not be readable. */
.sim-n.loss.pos, .sim-n.loss.neg { --hc: var(--bad); --h: 14 !important; color: var(--bad); border-color: color-mix(in srgb, var(--bad) 55%, transparent); }
.sim-n.focus { z-index: 4; box-shadow: 0 0 0 2px var(--ph-c), 0 0 0 6px color-mix(in srgb, var(--ph-c) 22%, transparent); }
.sim-n.focus:not(.main) { box-shadow: 0 0 0 1.5px var(--ph-c); }
.sim-n.focus.main { z-index: 5; transform: scale(1.1); box-shadow: 0 0 0 2.5px var(--ph-c), 0 0 0 8px color-mix(in srgb, var(--ph-c) 26%, transparent); }
.sim-n.op { box-shadow: 0 0 0 1.5px color-mix(in srgb, var(--ph-c) 70%, transparent); }
.sim-n.trail { outline: 1.5px dashed var(--sim-grad); outline-offset: 2px; }
.sim-n.dead { opacity: 0.5; }
.sim-n.dead .sim-v { text-decoration: line-through; }
.sim-n.stale { opacity: 0.4; filter: grayscale(0.7); }
.sim-n.masked { opacity: 0.45; border-style: dotted; background: transparent; }
.sim-n.sim-rn { min-width: 0; color: var(--ink); background: var(--well); border: 1px solid transparent; }
.sim-n.pop { animation: simPop 0.6s cubic-bezier(0.2, 0.9, 0.3, 1.3) both; }
/* Pills in a row of 8 or 9 sit close: a full-size pop would make neighbours collide. */
.sim-n.big.pop { animation-name: simPopSm; }
.sim-n.changed { animation: simFlash 1.6s ease both; animation-delay: calc(var(--d, 0) * 1ms / var(--spd, 1)); }

/* the position the attention, MLP and output cards follow */
.sim-pospick { display: inline-flex; flex-wrap: wrap; align-items: center; gap: 3px; margin-left: auto; font-family: var(--f-display); font-size: 0.72rem; color: var(--ink-3); }
.sim-pospick > span { margin-right: 3px; }
.sim-pospick button {
  min-width: 24px;
  height: 22px;
  padding: 0 5px;
  border-radius: 6px;
  border: 1px solid var(--rule);
  background: var(--surface);
  font-family: var(--f-mono);
  font-size: 0.7rem;
  color: var(--ink-2);
  cursor: pointer;
}
.sim-pospick button.graded { border-color: var(--answer); color: var(--answer); font-weight: 700; }
.sim-pospick button[aria-pressed="true"] { background: var(--ph-c); border-color: var(--ph-c); color: var(--sim-on); }

/* attention weight bars */
.sim-alw { display: grid; justify-items: center; gap: 3px; }
.sim-albar { display: block; width: 48px; height: 5px; border-radius: 3px; background: var(--well); overflow: hidden; }
.sim-albar i { display: block; height: 100%; width: 0; background: var(--sim-blue); transition: width 0.7s cubic-bezier(0.2, 0.8, 0.2, 1); }

/* neuron rows (MLP, logits, target) */
.sim-ngrid { display: grid; gap: 22px; padding: 4px 0; }
.sim-nrow { display: grid; grid-template-columns: 76px minmax(0, 1fr); gap: 8px; align-items: center; }
.sim-nodes { display: flex; justify-content: space-around; gap: 3px; min-width: 0; }
.sim-nw { display: grid; justify-items: center; gap: 3px; min-width: 0; }
.sim-nw small { font-family: var(--f-mono); font-size: 0.62rem; color: var(--ink-3); }

/* weight matrices */
/* Above the link layer: a link between two cards passes behind a weight panel, not over its numbers. */
.sim-mats { position: relative; z-index: 3; display: flex; flex-wrap: wrap; gap: 14px 24px; align-items: flex-start; padding-top: 12px; border-top: 1px dashed var(--rule); background: var(--surface); }
.sim-mats.top { padding: 0 0 12px; border-top: 0; border-bottom: 1px dashed var(--rule); }
.sim-rl small { display: block; font-weight: 400; color: var(--ink-3); }
.sim-mat { display: grid; gap: 5px; min-width: 0; }
.sim-mat-h { font-family: var(--f-display); font-size: 0.74rem; color: var(--ink-2); }
.sim-mat-h b { color: var(--teacher); }
.sim-mat.gview .sim-mat-h::after { content: " · gradients"; color: var(--sim-grad-ink); font-weight: 700; }
.sim-mat.nview .sim-mat-h::after { content: " · updated"; color: var(--good); font-weight: 700; }
.sim-grid { display: grid; grid-template-columns: auto repeat(var(--cols), minmax(36px, auto)); gap: 2px; align-items: center; }
.sim-clab, .sim-rlab { font-family: var(--f-mono); font-size: 0.6rem; color: var(--ink-3); text-align: center; white-space: nowrap; }
.sim-rlab { text-align: right; padding-right: 5px; }
.sim-n.sim-mc { min-width: 36px; height: 18px; padding: 0 3px; border-radius: 4px; font-size: 0.6rem; }
.sim-n.sim-mc.focus { transform: scale(1.18); }
.sim-n.big.focus.main { transform: scale(1.05); }

/* softmax bars */
.sim-bars { display: grid; gap: 5px; }
.sim-bar { display: grid; grid-template-columns: 46px minmax(0, 1fr) 52px; gap: 8px; align-items: center; }
.sim-bw { font-family: var(--f-mono); font-size: 0.74rem; color: var(--ink-2); }
.sim-bar.tgt .sim-bw { color: var(--answer); font-weight: 700; }
.sim-btrack { position: relative; height: 14px; border-radius: 4px; background: var(--well); overflow: hidden; }
.sim-btrack i { position: absolute; inset: 0 auto 0 0; width: 0; border-radius: 4px; background: var(--sim-blue); transition: width 0.7s cubic-bezier(0.2, 0.8, 0.2, 1); }
.sim-bar.tgt .sim-btrack i { background: var(--answer); }
.sim-pred { font-family: var(--f-display); font-size: 0.8rem; color: var(--ink-2); }
.sim-pred b { font-family: var(--f-mono); color: var(--ink); }
.sim-lossrow { display: flex; flex-wrap: wrap; align-items: center; justify-content: center; gap: 16px; padding-top: 6px; }

/* after the update */
.sim-again { display: flex; flex-wrap: wrap; gap: 10px; }
.sim-ro { display: grid; gap: 2px; padding: 8px 12px; border-radius: 10px; background: var(--well); }
.sim-ro span { font-family: var(--f-display); font-size: 0.72rem; color: var(--ink-2); }
.sim-ro b { font-family: var(--f-mono); font-size: 0.95rem; }
.sim-charts { display: grid; grid-template-columns: repeat(auto-fit, minmax(210px, 1fr)); gap: 12px; }
.sim-chart { background: var(--well); border-radius: 10px; }
.sim-cline { fill: none; stroke-width: 2.6; stroke-linecap: round; stroke-linejoin: round; stroke-dasharray: 1; stroke-dashoffset: 1; }
.sim-cline.a { stroke: var(--bad); }
.sim-cline.b { stroke: var(--answer); }
.sim-cline.c { stroke: var(--sim-blue); }
.sim-cline.d { stroke: var(--teacher); }
.sim-cdot { opacity: 0; }
.sim-cdot.a { fill: var(--bad); }
.sim-cdot.b { fill: var(--answer); }
.sim-cdot.c { fill: var(--sim-blue); }
.sim-cdot.d { fill: var(--teacher); }
.sim-chart text.sim-clab-t { font-family: var(--f-display); font-size: 10.5px; font-weight: 700; }
.sim-chart text.sim-clab-t.a { fill: var(--bad); }
.sim-chart text.sim-clab-t.b { fill: var(--answer); }
.sim-chart text.sim-clab-t.c { fill: var(--sim-blue); }
.sim-chart text.sim-clab-t.d { fill: var(--teacher); }
.sim-card.charted .sim-cline { animation: simDraw 2.6s ease forwards; }
.sim-card.charted .sim-cdot { opacity: 1; transition: opacity 0.4s 0.4s; }

/* links drawn between nodes */
.sim-mesh { fill: none; stroke: color-mix(in srgb, var(--ink-3) 32%, transparent); stroke-width: 1; }
.sim-mesh.g { stroke: color-mix(in srgb, var(--sim-grad) 42%, transparent); }
.sim-mesh.on { stroke: color-mix(in srgb, var(--ph-c) 85%, transparent); stroke-width: 1.5; }
.sim-mesh.res { stroke-dasharray: 3 5; }
.sim-arc { fill: none; stroke: color-mix(in srgb, var(--sim-blue) 40%, transparent); stroke-linecap: round; }
.sim-arc.g { stroke: color-mix(in srgb, var(--sim-grad) 50%, transparent); }
.sim-link { fill: none; stroke: var(--sim-blue); stroke-width: 2; stroke-dasharray: 6 6; animation: simFlow 0.8s linear infinite; }
.sim-link.g { stroke: var(--sim-grad); }
.sim-drawer[data-ph="loss"] .sim-link { stroke: var(--bad); }
.sim-arrow { fill: var(--sim-blue); }
.sim-arrow.g { fill: var(--sim-grad); }

/* ---------- the explanation panel ---------- */
.sim-ex { overflow: auto; overscroll-behavior: contain; background: var(--surface); border-left: 1px solid var(--rule); }
.sim-ex-in { display: grid; gap: 14px; padding: 22px 26px 36px; animation: simIn 0.4s ease both; }
.sim-chap { display: flex; align-items: center; gap: 9px; font-family: var(--f-display); font-size: 0.74rem; font-weight: 700; letter-spacing: 0.08em; text-transform: uppercase; color: var(--ph-c); }
.sim-chap i { width: 8px; height: 8px; border-radius: 50%; background: var(--ph-c); box-shadow: 0 0 0 4px color-mix(in srgb, var(--ph-c) 22%, transparent); }
.sim-drawer.playing .sim-chap i { animation: simBeat 1.4s ease-in-out infinite; }
.sim-ex h3 { font-size: 1.42rem; letter-spacing: -0.01em; }
.sim-text { display: grid; gap: 0.7em; font-size: 0.95rem; line-height: 1.62; }
.sim-text p.muted { font-size: 0.86rem; }
.sim-tex { padding: 8px 14px; border-radius: 10px; background: var(--well); overflow-x: auto; }
.sim-tex .katex-display { margin: 0.2em 0; }
.sim-tex .katex-display > .katex { font-size: 1.08em; }
.sim-rule {
  display: grid;
  gap: 4px;
  padding: 10px 14px;
  border-radius: 10px;
  border: 1px solid color-mix(in srgb, var(--sim-grad) 60%, transparent);
  background: var(--sim-grad-bg);
  font-size: 0.88rem;
  line-height: 1.5;
}
.sim-rule-k { font-family: var(--f-display); font-size: 0.7rem; font-weight: 700; letter-spacing: 0.08em; text-transform: uppercase; color: var(--sim-grad-ink); }
.sim-rule-gen { color: var(--ink-2); font-size: 0.82rem; }
.sim-rule-tex { overflow-x: auto; padding: 2px 0; }
.sim-rule-tex .katex-display { margin: 0.25em 0; }
/* The slope that leaves the loss (the error p − y): the one number each technique builds its own
   way, so the steps that build it carry this note, in the error slope's own colour. */
.sim-err {
  display: grid;
  gap: 6px;
  padding: 12px 14px;
  border-radius: 10px;
  border: 1.5px solid var(--sim-err);
  background: var(--sim-err-bg);
  font-size: 0.88rem;
  line-height: 1.55;
}
.sim-err-k { font-family: var(--f-display); font-size: 0.72rem; font-weight: 800; letter-spacing: 0.08em; text-transform: uppercase; color: var(--sim-err-ink); }
.sim-err b { color: var(--sim-err-ink); }
/* "What each symbol means": one row per symbol of the step's formula. */
.sim-vwrap { overflow-x: auto; }
.sim-vars { border-collapse: collapse; width: 100%; font-size: 0.82rem; line-height: 1.45; }
.sim-vars caption { caption-side: top; text-align: left; padding: 0 0 4px; font-family: var(--f-display); font-size: 0.7rem; font-weight: 700; letter-spacing: 0.08em; text-transform: uppercase; color: var(--ink-2); }
.sim-vars td { padding: 4px 8px; vertical-align: top; border-bottom: 1px solid color-mix(in srgb, var(--rule) 60%, transparent); }
.sim-vars td.sim-vsym { white-space: nowrap; width: 1%; padding-left: 0; }
.sim-calc {
  list-style: none;
  margin: 0;
  padding: 12px 14px;
  display: grid;
  gap: 8px;
  border-radius: 10px;
  background: var(--well);
  font-family: var(--f-mono);
  font-size: 0.78rem;
  line-height: 1.75;
  font-variant-ligatures: none;
  overflow-x: auto;
}
.sim-calc li { animation: simIn 0.5s ease both; animation-delay: calc(var(--i) * 420ms / var(--spd, 1)); }
.sim-calc .t { white-space: nowrap; }
.sim-calc .muted { color: var(--ink-3); }
/* "The whole step, slope by slope": one fixed-width label per part, so the five parts line up. */
.sim-part { display: inline-block; min-width: 6.4em; font-weight: 700; color: var(--ink); }
.sim-eq { padding: 1px 5px; border-radius: 5px; background: color-mix(in srgb, var(--ph-c) 18%, transparent); font-weight: 700; }
.sim-off { color: var(--sim-neg); font-weight: 700; }
.sim-up { color: var(--good); }
.sim-down { color: var(--bad); }
.sim-road { display: inline; line-height: 2; }
.sim-tab { border-collapse: collapse; width: 100%; font-size: 0.74rem; }
.sim-tab th { padding: 3px 8px; text-align: left; font-family: var(--f-display); font-size: 0.7rem; font-weight: 600; color: var(--ink-2); border-bottom: 1px solid var(--rule); white-space: nowrap; }
.sim-tab td { padding: 3px 8px; border-bottom: 1px solid color-mix(in srgb, var(--rule) 60%, transparent); white-space: nowrap; }
.sim-tab .r { text-align: right; }
.sim-tab tr.hl td { background: color-mix(in srgb, var(--ph-c) 12%, transparent); }

.k-act { color: var(--sim-blue); font-weight: 600; }
.k-w { color: var(--teacher); font-weight: 600; }
.k-g { color: var(--sim-grad-ink); font-weight: 700; }
.k-d { color: var(--answer); font-weight: 700; }
.k-l { color: var(--bad); font-weight: 700; }
.k-e { color: var(--sim-err-ink); font-weight: 700; }

.sim-keys { display: flex; flex-wrap: wrap; gap: 8px 18px; padding: 10px 14px; border: 1px dashed var(--rule); border-radius: 10px; font-size: 0.82rem; color: var(--ink-2); }
.sim-keys > span > span { font-family: var(--f-mono); margin-right: 4px; }

.sim-roadmap { display: flex; flex-wrap: wrap; gap: 5px; align-items: center; }
.sim-stop { display: inline-flex; align-items: center; gap: 5px; padding: 3px 8px; border-radius: 999px; background: var(--well); color: var(--ink-3); font-family: var(--f-mono); font-size: 0.68rem; }
.sim-stop.done { color: var(--ink-2); }
.sim-stop b { color: var(--sim-grad-ink); }
.sim-stop.cur { background: var(--sim-grad); color: #241600; box-shadow: 0 0 0 3px color-mix(in srgb, var(--sim-grad) 25%, transparent); }
.sim-stop.cur b { color: inherit; }
.sim-stop:not(:last-child)::after { content: "→"; margin-left: 2px; color: var(--ink-3); }
.sim-stop.cur:not(:last-child)::after { color: #241600; }

.sim-recap { margin: 0; padding-left: 1.3em; display: grid; gap: 0.55em; }
.sim-end { display: flex; flex-wrap: wrap; align-items: center; gap: 12px; padding-top: 4px; }

/* ---------- motion ---------- */
@keyframes simIn { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }
/* No 100% keyframe: the pop settles on whatever scale the node's focus rule gives it. */
@keyframes simPop { 0% { transform: scale(0.82); } 55% { transform: scale(1.2); } }
@keyframes simPopSm { 0% { transform: scale(0.9); } 55% { transform: scale(1.06); } }
@keyframes simFlow { to { stroke-dashoffset: -24; } }
@keyframes simDraw { to { stroke-dashoffset: 0; } }
@keyframes simRing { 0% { transform: scale(0.88); opacity: 0.9; } 100% { transform: scale(1.4); opacity: 0; } }
@keyframes simBeat { 50% { box-shadow: 0 0 0 7px color-mix(in srgb, var(--ph-c) 8%, transparent); } }
@keyframes simFlash {
  0% { box-shadow: 0 0 0 0 transparent; }
  35% { box-shadow: 0 0 0 3px var(--good); background: var(--good-bg); color: var(--good); }
  100% { box-shadow: 0 0 0 0 transparent; }
}
/* The page turns animations off for reduced motion; the training curves must still show. */
@media (prefers-reduced-motion: reduce) {
  .sim-card.charted .sim-cline { stroke-dashoffset: 0; }
  /* The flash is the only cue for which weights moved: keep it as a static ring. */
  .sim-n.changed { animation: none; box-shadow: 0 0 0 2px var(--good); }
}

/* ---------- content of the cards a technique adds (data, loss) ---------- */
.sim-xrows { display: grid; gap: 8px; }
.sim-xrow { display: flex; flex-wrap: wrap; align-items: center; gap: 5px 6px; padding: 7px 9px; border-radius: 9px; background: var(--well); border: 1px solid transparent; }
.sim-xrow > b { font-family: var(--f-display); font-size: 0.72rem; color: var(--ink-2); min-width: 64px; }
.sim-xrow .inline-tok { font-size: 0.74rem; }
.sim-xrow.good { border-color: color-mix(in srgb, var(--good) 55%, transparent); background: color-mix(in srgb, var(--good-bg) 55%, var(--surface)); }
.sim-xrow.bad { border-color: color-mix(in srgb, var(--bad) 45%, transparent); background: color-mix(in srgb, var(--bad-bg) 45%, var(--surface)); }
.sim-xrow.off { opacity: 0.55; }
.sim-xrow.off .inline-tok { text-decoration: line-through; }
.sim-xrow .sim-rn { margin-left: auto; }
.sim-pill { display: inline-flex; align-items: center; gap: 4px; padding: 1px 8px; border-radius: 999px; font-family: var(--f-display); font-size: 0.68rem; font-weight: 700; background: var(--surface); color: var(--ink-2); border: 1px solid var(--rule); }
.sim-pill.good { color: var(--good); border-color: var(--good); }
.sim-pill.bad { color: var(--bad); border-color: var(--bad); }
.sim-pill.tea { color: var(--teacher); border-color: var(--teacher); }
.sim-arrowtxt { color: var(--ink-3); font-family: var(--f-mono); font-size: 0.8rem; }
.sim-card .sim-tab { font-family: var(--f-mono); }
.sim-card .sim-tab td .sim-rn { font-size: 0.66rem; height: 20px; }
.sim-note { font-size: 0.78rem; color: var(--ink-2); line-height: 1.45; }
.sim-mbars { display: grid; gap: 4px; }
.sim-mbar { display: grid; grid-template-columns: 52px minmax(0, 1fr) 46px; gap: 8px; align-items: center; font-family: var(--f-mono); font-size: 0.72rem; color: var(--ink-2); }
.sim-mbar i { display: block; height: 10px; border-radius: 3px; background: var(--teacher); }
.sim-mbar span:last-child { text-align: right; }
.sim-two { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 10px; }
.sim-two > div { display: grid; gap: 6px; align-content: start; }
.sim-two h5 { font-size: 0.78rem; color: var(--ink-2); font-family: var(--f-display); }
.sim-lossbig { display: flex; flex-wrap: wrap; align-items: center; justify-content: center; gap: 14px; padding-top: 4px; font-family: var(--f-display); font-size: 0.8rem; color: var(--ink-2); }
.sim-lossbig .sim-rn { min-width: 110px; height: 40px; font-size: 1.1rem; font-weight: 700; color: var(--bad); border: 1px solid color-mix(in srgb, var(--bad) 55%, transparent); background: color-mix(in srgb, var(--bad) 10%, var(--surface)); border-radius: 12px; }

/* ---------- the batch card: rows × positions ---------- */
.sim-bgrid { display: grid; grid-template-columns: max-content repeat(var(--T), minmax(60px, 1fr)); gap: 4px; align-items: stretch; font-family: var(--f-mono); }
.sim-bpos { font-size: 0.62rem; color: var(--ink-3); text-align: center; align-self: end; }
.sim-brl { display: flex; align-items: center; max-width: 150px; padding-right: 6px; overflow: hidden; font-family: var(--f-display); font-size: 0.7rem; color: var(--ink-3); white-space: nowrap; text-overflow: ellipsis; }
.sim-brl b { overflow: hidden; text-overflow: ellipsis; }
.sim-brl.cur { color: var(--ink); }
.sim-bc { display: grid; gap: 3px; justify-items: center; align-content: start; min-width: 0; padding: 5px 3px 4px; border: 1px solid var(--rule); border-radius: 7px; background: var(--surface); color: var(--ink-2); font: inherit; font-size: 0.62rem; cursor: pointer; transition: box-shadow 0.25s, background-color 0.25s; }
.sim-bc:hover { border-color: var(--ink-3); }
.sim-bc:focus-visible { outline: 2px solid var(--ph-c); outline-offset: 1px; }
.sim-bc.graded { border-color: color-mix(in srgb, var(--answer) 60%, var(--rule)); background: color-mix(in srgb, var(--answer-bg) 55%, var(--surface)); }
.sim-bc.pad { display: grid; place-items: center; border-style: dashed; background: transparent; color: var(--ink-3); cursor: default; }
.sim-bc.cur { box-shadow: 0 0 0 2px var(--ph-c); }
/* rows with no gradient this step (frozen, dropped, another stage) fade once the backward pass starts */
.sim-bc.out { opacity: 0.45; }
.sim-bt { max-width: 100%; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.sim-bt i { margin: 0 1px; font-style: normal; color: var(--ink-3); }
.sim-bv { min-height: 1.25em; font-weight: 700; color: var(--ink); }
.sim-bc.up .sim-bv { color: var(--good); }
.sim-bc.down .sim-bv { color: var(--bad); }
.sim-bbar { width: 100%; height: 4px; overflow: hidden; border-radius: 2px; background: var(--well); }
.sim-bbar i { display: block; width: 0; height: 100%; background: var(--sim-grad); transition: width 0.6s ease; }
.sim-bnote { margin: 8px 0 0; font-size: 0.74rem; color: var(--ink-3); }
.sim-card[data-card="batch"].flash .sim-bc:not(.pad) { animation: simBFlash calc(0.9s / var(--spd, 1)) ease; }
@keyframes simBFlash { 35% { border-color: var(--sim-blue); background: color-mix(in srgb, var(--sim-blue) 32%, var(--surface)); } }

/* ---------- where the starting 1 goes ---------- */
.sim-tree, .sim-tree ul { margin: 0; padding: 0; list-style: none; }
.sim-tree ul { margin-left: 6px; padding-left: 14px; border-left: 1px dashed var(--rule); }
.sim-tree li { padding: 3px 0; }

/* ---------- training vs writing, and one weight's contributions ---------- */
.sim-tl { display: grid; gap: 8px; }
.sim-tl-r { display: flex; flex-wrap: wrap; align-items: center; gap: 5px; }
.sim-tl-r > b { flex: 0 0 136px; font-family: var(--f-display); font-size: 0.72rem; color: var(--ink-2); }
.sim-tl-b { padding: 3px 8px; border: 1px solid color-mix(in srgb, var(--sim-blue) 50%, var(--rule)); border-radius: 6px; background: color-mix(in srgb, var(--sim-blue) 20%, var(--surface)); font-size: 0.68rem; animation: simTl 0.5s ease both; animation-delay: calc(var(--k, 0) * 650ms / var(--spd, 1)); }
.sim-tl-b.bwd { border-color: color-mix(in srgb, var(--sim-grad) 55%, var(--rule)); background: color-mix(in srgb, var(--sim-grad) 20%, var(--surface)); }
.sim-tl-b.gen { border-color: color-mix(in srgb, var(--answer) 50%, var(--rule)); background: color-mix(in srgb, var(--answer) 16%, var(--surface)); }
@keyframes simTl { from { opacity: 0; transform: translateX(-6px); } }
.sim-cbars { display: grid; gap: 4px; }
.sim-cb { display: grid; grid-template-columns: minmax(96px, max-content) minmax(0, 1fr); gap: 8px; align-items: center; font-size: 0.68rem; color: var(--ink-2); }
.sim-cbt { position: relative; height: 10px; border-radius: 3px; background: var(--well); }
.sim-cbt::after { content: ""; position: absolute; top: -2px; bottom: -2px; left: 50%; width: 1px; background: var(--ink-3); }
.sim-cbt i { position: absolute; top: 0; bottom: 0; border-radius: 3px; }
.sim-cbt i.up { left: 50%; background: var(--sim-pos); }
.sim-cbt i.down { right: 50%; background: var(--sim-neg); }
.sim-cb.total .sim-cbt i { box-shadow: 0 0 0 1.5px var(--ink); }

/* which parts every technique shares, and which this one adds */
.sim-scope { justify-self: start; margin-top: -6px; padding: 2px 10px; border: 1px solid var(--rule); border-radius: 999px; background: var(--surface); font-family: var(--f-display); font-size: 0.68rem; font-weight: 700; color: var(--ink-3); }
.sim-scope:not(.same) { border-color: color-mix(in srgb, var(--ph-c) 55%, var(--rule)); color: var(--ph-c); }
