/* RETAIN demo widgets (static/demo.js). Builds on the page tokens in page.html :root.
   Demo-only tokens: memory-grid states and the ground-truth colour, in both themes. */
:root {
  --d-empty: #ebe7de;
  --d-fresh: #3f7046;          /* just written (sage, dark end) */
  --d-old: #d3e0cf;            /* not written for a long time (sage, pale end) */
  --d-add: #d29a12;            /* new slot claimed (sand, saturated) */
  --d-update: #3a6aa6;         /* slot refreshed (slate) */
  --d-evict: #b3261e;          /* slot evicted */
  --d-gt: #2b8a3e;             /* ground truth, dashed */
  --d-base1: #7b8794;          /* offline baselines in the storage chart */
  --d-base2: #3f4a56;
  --d-on: #ffffff;             /* text on accent / success / failure fills */
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --d-empty: #262b31; --d-fresh: #9ccc98; --d-old: #34463a; --d-add: #e8b93a;
    --d-update: #74a6e0; --d-evict: #f06d61; --d-gt: #63d17c; --d-base1: #8a96a3; --d-base2: #c3ccd5;
    --d-on: #15181c;
  }
}
:root[data-theme="dark"] {
  --d-empty: #262b31; --d-fresh: #9ccc98; --d-old: #34463a; --d-add: #e8b93a;
  --d-update: #74a6e0; --d-evict: #f06d61; --d-gt: #63d17c; --d-base1: #8a96a3; --d-base2: #c3ccd5;
  --d-on: #15181c;
}

.demo-panel {
  background: var(--surface); border: 1px solid var(--rule); border-radius: 10px;
  padding: 16px; margin-top: 6px; min-width: 0;
}
.demo-panel :is(select, input, button, canvas, [tabindex]):focus-visible {
  outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 4px;
}
.d-status { color: var(--muted); font-size: 14.5px; margin: 0; }
.d-error { color: var(--d-evict); font-size: 14.5px; margin: 0; }
.d-row { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 12px; }
.d-row label, .d-label { font: 500 12.5px/1.2 var(--mono); letter-spacing: .04em; text-transform: uppercase; color: var(--muted); }
.d-select {
  font: 400 15px/1.3 var(--body); color: var(--fg); background: var(--bg);
  border: 1px solid var(--rule); border-radius: 6px; padding: 6px 8px; max-width: 100%; min-width: 0;
}
.d-btn {
  font: 500 14px/1 var(--body); color: var(--fg); background: var(--bg); cursor: pointer;
  border: 1px solid var(--rule); border-radius: 6px; padding: 8px 12px; min-height: 36px;
  display: inline-flex; align-items: center; gap: 6px;
}
.d-btn:hover:not(:disabled) { border-color: var(--muted); }
.d-btn:disabled { opacity: .45; cursor: default; }
.d-btn[aria-pressed="true"] { background: var(--fg); color: var(--bg); border-color: var(--fg); }
.d-btn.primary { background: var(--accent); border-color: var(--accent); color: var(--d-on); }
.d-note { color: var(--muted); font-size: 14.5px; line-height: 1.5; margin: 8px 0 0; }
.d-num { font-family: var(--mono); font-variant-numeric: tabular-nums; }
.d-swatch { display: inline-block; width: 11px; height: 11px; border-radius: 2px; vertical-align: -1px; margin-right: 5px; }

/* ---------- memory explorer ---------- */
.mx-layout { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1.3fr); gap: 18px; margin-top: 14px; }
.mx-frame { margin: 0; min-width: 0; }
.mx-frame-inner { position: relative; border-radius: 6px; overflow: hidden; background: var(--code-bg); }
.mx-frame-inner img, .mx-frame-inner canvas { display: block; width: 100%; height: auto; }
.mx-frame-inner svg { position: absolute; inset: 0; width: 100%; height: 100%; overflow: visible; }
.mx-boxes rect { fill: none; stroke-width: 2px; vector-effect: non-scaling-stroke; }
.mx-boxes .add { stroke: var(--d-add); }
.mx-boxes .update { stroke: var(--d-update); }
.mx-boxes .pin { stroke: var(--fg); stroke-width: 3.5px; }
.mx-memory { min-width: 0; }
.mx-grid { display: block; width: 100%; aspect-ratio: 2 / 1; cursor: crosshair; background: var(--surface); touch-action: manipulation; }
.mx-legend { display: flex; flex-wrap: wrap; gap: 4px 14px; font-size: 13px; color: var(--muted); margin-top: 8px; }
.mx-ramp { display: inline-block; width: 46px; height: 11px; border-radius: 2px; vertical-align: -1px; margin: 0 5px;
  background: linear-gradient(90deg, var(--d-fresh), var(--d-old)); }
.mx-info { display: grid; grid-template-columns: 64px minmax(0, 1fr); gap: 12px; align-items: start; margin-top: 12px;
  min-height: 92px; padding: 10px; border-radius: 8px; background: var(--memory); font-size: 14px; line-height: 1.45; }
.mx-info.empty { display: block; color: var(--muted); }
.mx-info .crop { width: 64px; height: 64px; border-radius: 4px; background: var(--d-empty); object-fit: cover; }
.mx-info b { font-weight: 600; }
.mx-info dl { display: grid; grid-template-columns: auto minmax(0, 1fr); gap: 1px 10px; margin: 4px 0 0; }
.mx-info dt { color: var(--muted); }
.mx-info dd { margin: 0; font-family: var(--mono); font-size: 13px; }
.mx-timeline { margin-top: 16px; }
.mx-time-row { display: flex; align-items: center; gap: 10px; }
.mx-time-row input[type="range"] { flex: 1; min-width: 0; accent-color: var(--accent); }
.mx-time { font: 400 13px/1 var(--mono); color: var(--muted); white-space: nowrap; min-width: 9ch; text-align: right; }
.mx-spark { display: block; width: 100%; height: 56px; margin-top: 6px; cursor: pointer; touch-action: pan-y; }
.mx-spark-cap { display: flex; justify-content: space-between; gap: 10px; flex-wrap: wrap; font-size: 13px; color: var(--muted); }
.d-stats { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 10px; margin-top: 14px; }
.d-stat { background: var(--bg); border-radius: 8px; padding: 8px 10px; min-width: 0; }
.d-stat b { display: block; font: 600 21px/1.15 var(--display); font-variant-numeric: tabular-nums; }
.d-stat span { font-size: 12.5px; color: var(--muted); }

/* ---------- storage chart ---------- */
.st-chart svg { display: block; width: 100%; height: auto; overflow: visible; }
.st-chart text { font: 400 11.5px var(--mono); fill: var(--muted); }
.st-chart .lbl { font: 500 12.5px var(--body); }
.st-chart .grid { stroke: var(--rule); stroke-width: 1; }
.st-chart polyline.line { fill: none; stroke-width: 2.5; }
.st-readout .ours-num { color: var(--accent); }
.st-chart .ours { stroke: var(--accent); fill: var(--accent); }
.st-chart .b1 { stroke: var(--d-base1); fill: var(--d-base1); }
.st-chart .b2 { stroke: var(--d-base2); fill: var(--d-base2); }
.st-chart text.ours, .st-chart text.b1, .st-chart text.b2 { stroke: none; }
.st-chart .cursor { stroke: var(--fg); stroke-dasharray: 3 3; stroke-width: 1; }
.st-controls { display: flex; align-items: center; gap: 12px; margin-top: 10px; flex-wrap: wrap; }
.st-controls input[type="range"] { flex: 1; min-width: 160px; accent-color: var(--accent); }
.st-readout { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 10px; margin-top: 12px; }

/* ---------- query walkthrough ---------- */
.qw-steps { list-style: none; display: flex; flex-wrap: wrap; gap: 6px; padding: 0; margin: 14px 0 0; counter-reset: qs; }
.qw-steps button { font: 500 13.5px/1 var(--body); padding: 7px 10px; border-radius: 999px; border: 1px solid var(--rule);
  background: var(--bg); color: var(--muted); cursor: pointer; min-height: 32px; }
.qw-steps button[aria-current="step"] { background: var(--read); color: var(--fg); border-color: var(--muted); }
.qw-nav { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 12px; }
.qw-cards { display: grid; gap: 12px; margin-top: 12px; }
.qw-card { background: var(--read); border-radius: 8px; padding: 14px; min-width: 0; }
.qw-card h4 { font: 600 17px/1.3 var(--display); margin: 0 0 4px; }
.qw-card > p { font-size: 14.5px; color: var(--muted); margin: 0 0 10px; }
.qw-qcrop { width: 128px; height: 128px; object-fit: cover; border-radius: 6px; background: var(--d-empty); }
.qw-list { display: grid; gap: 6px; }
.qw-item { display: grid; grid-template-columns: 40px minmax(0, 1fr) 3.6rem; gap: 10px; align-items: center; font-size: 14px; }
.qw-item img, .qw-item .ph { width: 40px; height: 40px; border-radius: 4px; object-fit: cover; background: var(--d-empty); }
.qw-item .val { font: 400 13px var(--mono); text-align: right; }
.qw-item.target { font-weight: 600; }
.qw-track { position: relative; height: 8px; border-radius: 4px; background: var(--surface); margin-top: 4px; overflow: hidden; }
.qw-fill { position: absolute; top: 0; bottom: 0; left: 0; border-radius: 4px; background: var(--d-base1); }
.qw-item.target .qw-fill { background: var(--d-gt); }
.qw-track.div::after { content: ""; position: absolute; left: 50%; top: -2px; bottom: -2px; width: 1px; background: var(--muted); }
.qw-badge { display: inline-block; font: 500 11.5px/1 var(--mono); padding: 3px 6px; border-radius: 4px; margin-left: 6px;
  background: var(--surface); color: var(--fg); vertical-align: 1px; }
.qw-badge.ok { background: var(--d-gt); color: var(--d-on); }
.qw-badge.bad { background: var(--d-evict); color: var(--d-on); }
.qw-badge.ours { background: var(--accent); color: var(--d-on); }
.qw-answer { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.qw-answer figure { margin: 0; min-width: 0; }
.qw-answer .gt rect { stroke: var(--d-gt); stroke-dasharray: 6 4; }
.qw-answer .raw rect { stroke: var(--accent); stroke-dasharray: 2 3; stroke-width: 1.5px; }
.qw-answer .ref rect { stroke: var(--accent); stroke-width: 2.5px; }
.qw-answer rect { fill: none; stroke-width: 2.5px; vector-effect: non-scaling-stroke; }
.qw-timebar { margin-top: 12px; display: grid; gap: 4px; font-size: 12.5px; color: var(--muted); }
.qw-tb-row { display: grid; grid-template-columns: 7rem minmax(0, 1fr); gap: 8px; align-items: center; }
.qw-tb-track { position: relative; height: 10px; border-radius: 3px; background: var(--surface); }
.qw-tb-band { position: absolute; top: 0; bottom: 0; min-width: 3px; border-radius: 3px; }
.qw-tb-band.gt { background: var(--d-gt); }
.qw-tb-band.ours { background: var(--accent); }
.qw-tb-axis { display: flex; justify-content: space-between; margin-left: calc(7rem + 8px); font: 400 11px var(--mono); }
.qw-meta { display: flex; flex-wrap: wrap; gap: 6px 18px; margin-top: 10px; font-size: 14px; }
.qw-lat { font: 400 12.5px var(--mono); color: var(--muted); margin-top: 6px; }

/* ---------- shared chips ---------- */
.mx-chips { display: flex; flex-wrap: wrap; gap: 6px; }
.d-chip { font: 500 13px/1 var(--body); padding: 6px 10px; min-height: 30px; border-radius: 999px; cursor: pointer;
  border: 1px solid var(--rule); background: var(--bg); color: var(--muted); }
.d-chip[aria-pressed="true"] { background: var(--fg); color: var(--bg); border-color: var(--fg); }

/* ---------- memory explorer: tabs, preview, mosaic ---------- */
.mx-tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--rule); margin-bottom: 10px; }
.mx-tab { font: 500 13.5px/1 var(--body); color: var(--muted); background: none; border: 0; border-bottom: 2px solid transparent;
  padding: 8px 10px; margin-bottom: -1px; cursor: pointer; min-height: 34px; }
.mx-tab[aria-selected="true"] { color: var(--fg); border-bottom-color: var(--accent); }
.mx-tab:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; border-radius: 4px; }
.mx-frame-inner.peek { outline: 2px dashed var(--muted); outline-offset: 2px; }
.mx-boxes .peek { stroke: var(--fg); stroke-width: 3px; stroke-dasharray: 5 3; }
.mx-mosaic-head { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 6px 12px;
  font-size: 13.5px; color: var(--muted); margin-bottom: 8px; }
.mx-mosaic-head .d-select { font-size: 13.5px; padding: 4px 6px; }
.mx-mosaic-scroll { max-height: 380px; overflow-y: auto; overflow-x: hidden; border-radius: 6px; background: var(--surface);
  overscroll-behavior: contain; }
.mx-mosaic-sizer { position: relative; }
.mx-mosaic-canvas { display: block; width: 100%; position: sticky; top: 0; cursor: pointer; touch-action: manipulation; }
.mx-lifeline .inc:focus-visible { outline: none; stroke: var(--accent); stroke-width: 3px; vector-effect: non-scaling-stroke; }

/* ---------- memory explorer: write log and lifetimes ---------- */
.mx-lower { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 18px; margin-top: 18px; }
.mx-lower > * { min-width: 0; }
.mx-lower h4 { font: 600 16px/1.3 var(--display); margin: 0 0 8px; }
.mx-life h4 + * { margin-top: 0; }
.mx-life h4:not(:first-child) { margin-top: 16px; }
.mx-log-list { list-style: none; padding: 0; margin: 10px 0 0; display: grid; gap: 3px; }
.mx-log-line { display: grid; grid-template-columns: 3.6rem 28px minmax(0, 1fr); gap: 8px; align-items: center; width: 100%;
  text-align: left; font: 400 13.5px/1.3 var(--body); color: var(--fg); background: var(--bg); cursor: pointer;
  border: 1px solid transparent; border-left: 3px solid var(--rule); border-radius: 4px; padding: 3px 8px 3px 6px; min-height: 36px; }
.mx-log-line:hover { border-color: var(--rule); }
.mx-log-line.add { border-left-color: var(--d-add); }
.mx-log-line.update { border-left-color: var(--d-update); }
.mx-log-line.evict { border-left-color: var(--d-evict); }
.mx-log-line.pin { border-color: var(--fg); font-weight: 600; }
.mx-log-t { font-size: 12.5px; color: var(--muted); }
.mx-log-crop { width: 28px; height: 28px; border-radius: 3px; background: var(--d-empty); }
.mx-log-text { overflow-wrap: anywhere; }
.mx-lifeline svg { display: block; width: 100%; height: 34px; }
.mx-lifeline .track { fill: var(--d-empty); }
.mx-lifeline .inc { cursor: pointer; }
.mx-lifeline .inc.a { fill: var(--d-fresh); }
.mx-lifeline .inc.b { fill: var(--d-update); }
.mx-lifeline .ev { fill: var(--d-evict); }
.mx-lifeline .wa { fill: var(--d-add); }
.mx-lifeline .wu { fill: var(--muted); }
.mx-lifeline .cur { fill: var(--accent); }
.mx-lifeline-axis { display: flex; justify-content: space-between; font: 400 11px var(--mono); color: var(--muted); }
.mx-hist { display: grid; grid-template-columns: repeat(9, minmax(0, 1fr)); gap: 4px; align-items: end; height: 132px; }
.mx-hist-col { display: grid; grid-template-rows: 16px minmax(0, 1fr) 30px; height: 100%; min-width: 0; text-align: center; }
.mx-hist-n { font-size: 10.5px; color: var(--muted); white-space: nowrap; overflow: hidden; }
.mx-hist-track { position: relative; }
.mx-hist-bar { position: absolute; left: 0; right: 0; bottom: 0; min-height: 1px; border-radius: 2px 2px 0 0; background: var(--d-base1); }
.mx-hist-col.hit .mx-hist-bar { background: var(--accent); }
.mx-hist-lbl { font-size: 10.5px; line-height: 1.15; color: var(--muted); padding-top: 3px; overflow-wrap: anywhere; }

/* ---------- what if K were smaller ---------- */
.ks-krow { margin-top: 10px; }
.ks-h { font: 600 16px/1.3 var(--display); margin: 18px 0 6px; }
.ks-chart .ks-line { stroke-width: 1.6; opacity: .75; }
.ks-chart .ks-line.sel { stroke-width: 3; opacity: 1; }
.ks-bars { display: grid; gap: 4px; }
.ks-bar { display: grid; grid-template-columns: 5.6rem minmax(0, 1fr) 4.6rem; grid-template-areas: "k bar v" ". sub sub";
  gap: 2px 10px; align-items: center; width: 100%; text-align: left; font: 400 13.5px/1.3 var(--body); color: var(--fg);
  background: none; border: 1px solid transparent; border-radius: 6px; padding: 5px 8px; cursor: pointer; }
.ks-bar:hover { border-color: var(--rule); }
.ks-bar.sel { background: var(--accent-soft); border-color: var(--accent); }
.ks-bar .ks-k { grid-area: k; font-size: 13px; }
.ks-bar .qw-track { grid-area: bar; display: block; margin: 0; }
.ks-bar.sel .qw-fill { background: var(--accent); }
.ks-bar .ks-v { grid-area: v; text-align: right; font-size: 13px; }
.ks-bar .ks-sub { grid-area: sub; font-size: 12.5px; color: var(--muted); }
.ks-matrix { min-width: 0; font-size: 14px; }
.ks-matrix th, .ks-matrix td { padding: 6px 8px; text-align: center; }
.ks-matrix th:first-child { text-align: left; white-space: normal; font: 400 14px/1.3 var(--body); color: var(--fg); letter-spacing: 0; }
.ks-matrix thead th { white-space: nowrap; }
.ks-matrix th.sel { color: var(--accent); }
.ks-matrix td { font-family: var(--mono); font-size: 13px; }
.ks-matrix td.sel { box-shadow: inset 2px 0 0 var(--accent), inset -2px 0 0 var(--accent); }
.ks-matrix td.alive, .ks-key.alive { background: var(--memory); }
.ks-matrix td.lost, .ks-key.lost { background: var(--accent-soft); color: var(--d-evict); }
.ks-matrix td.na { color: var(--muted); }
.ks-key { display: inline-block; padding: 1px 6px; border-radius: 4px; font-size: 12.5px; }
.ks-top1 { color: var(--d-gt); }

/* ---------- query walkthrough: memory line and baselines ---------- */
.qw-memline { font-size: 14px; margin-top: 6px; padding: 6px 8px; border-radius: 6px; background: var(--memory); max-width: 52ch; }
.qw-memline.lost { background: var(--accent-soft); }
.qw-base { margin-top: 14px; }
.qw-base h5 { font: 600 14.5px/1.3 var(--body); margin: 0 0 8px; }
.qw-base-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 10px; }
.qw-base-grid figure { margin: 0; min-width: 0; }
.qw-base-grid figcaption { font-size: 13px; margin-top: 6px; }
.qw-base-grid .base rect { fill: none; stroke: var(--d-base2); stroke-width: 2.5px; vector-effect: non-scaling-stroke; }
.qw-noframe { aspect-ratio: 16 / 9; display: grid; place-items: center; border-radius: 6px; background: var(--code-bg);
  color: var(--muted); font-size: 13px; }
.qw-tb-band.base { background: var(--d-base1); }

@media (max-width: 820px) {
  .mx-layout, .qw-answer { grid-template-columns: 1fr; }
  .d-stats, .st-readout { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .mx-lower { grid-template-columns: 1fr; }
}
@media (max-width: 480px) {
  .demo-panel { padding: 12px; }
  .mx-info { grid-template-columns: 48px minmax(0, 1fr); }
  .mx-info .crop { width: 48px; height: 48px; }
  .mx-time-row { flex-wrap: wrap; }
  .mx-time-row input[type="range"] { order: -1; flex-basis: 100%; }
  .mx-time { margin-left: auto; }
  .mx-hist-lbl, .mx-hist-n { font-size: 9.5px; }
  .ks-bar { grid-template-columns: 4.8rem minmax(0, 1fr) 3.8rem; }
  .ks-matrix th, .ks-matrix td { padding: 5px 4px; }
}
@media (prefers-reduced-motion: reduce) {
  .demo-panel * { transition: none !important; animation: none !important; }
}

/* ---------- videos (static/videos/*.mp4, 16:9, rendered by viz/site_video) ---------- */
.vid-hero { margin: 28px 0 0; background: var(--surface); border: 1px solid var(--rule); border-radius: 10px; padding: 12px; }
.vid-hero video, .vid-card video {
  display: block; width: 100%; height: auto; aspect-ratio: 16 / 9; border-radius: 6px; background: #15181c;
}
.vid-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 16px; margin-top: 6px; }
.vid-card { margin: 0; min-width: 0; background: var(--surface); border: 1px solid var(--rule); border-radius: 10px; padding: 10px; }
.vid-card figcaption { margin-top: 8px; font-size: 13.5px; }
.vid-card figcaption b { color: var(--fg); font-weight: 600; }
@media (max-width: 820px) { .vid-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); } }
@media (max-width: 520px) {
  .vid-grid { grid-template-columns: 1fr; }
  .vid-hero { padding: 8px; }
}
