/* Interactive diagrams. Loaded only on pages that use them; everything is
   scoped under .viz and keyed to the site tokens in style.css. */

:root {
  /* Categorical (servers, series). Order is fixed; never cycle. */
  --viz-s1: #2a78d6; --viz-s2: #eb6834; --viz-s3: #1baf7a;
  --viz-s4: #eda100; --viz-s5: #e87ba4; --viz-s6: #008300;
  /* Status: always paired with a text label, never color alone. */
  --viz-good: #0ca30c;
  --viz-bad: #d03b3b;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --viz-s1: #3987e5; --viz-s2: #d95926; --viz-s3: #199e70;
    --viz-s4: #c98500; --viz-s5: #d55181; --viz-s6: #008300;
  }
}
:root[data-theme="dark"] {
  --viz-s1: #3987e5; --viz-s2: #d95926; --viz-s3: #199e70;
  --viz-s4: #c98500; --viz-s5: #d55181; --viz-s6: #008300;
}

.viz {
  margin: 2rem 0;
  padding: 1rem 1.125rem;
  border: 1px solid var(--rule);
  border-radius: 8px;
  font-size: .875rem;
  line-height: 1.5;
}
.viz * { box-sizing: border-box; }
.viz-fallback { color: var(--muted); margin: 0; }

.viz-head { display: flex; justify-content: space-between; align-items: baseline; gap: 1rem; margin-bottom: 1rem; }
.viz-title { font-weight: 500; }
.viz-meta, .viz-label { color: var(--muted); font-variant-numeric: tabular-nums; }
.viz-label { font-size: .75rem; }

.viz-caption { color: var(--muted); min-height: 3em; margin: 1rem 0 0; }
.viz-caption b { color: var(--fg); font-weight: 500; }
.viz-caption code { background: var(--subtle); padding: .05em .3em; border-radius: 3px; }

.viz-controls {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: .5rem;
  margin-top: .875rem;
  padding-top: .875rem;
  border-top: 1px solid var(--rule);
}
.viz-controls .viz-spacer { flex: 1; }

.viz-btn, .viz-seg button, .viz-chip {
  font: inherit;
  color: var(--fg);
  background: none;
  cursor: pointer;
}
.viz-btn {
  border: 1px solid var(--rule);
  border-radius: 6px;
  padding: .2rem .65rem;
  min-height: 2rem;
  display: inline-flex;
  align-items: center;
  gap: .35rem;
}
.viz-btn:hover:not(:disabled) { border-color: var(--muted); }
.viz-btn:disabled { opacity: .4; cursor: default; }
.viz-btn svg { width: 12px; height: 12px; fill: currentColor; }

.viz-seg { display: inline-flex; padding: 2px; border: 1px solid var(--rule); border-radius: 6px; }
.viz-seg button { border: 0; border-radius: 4px; padding: .15rem .6rem; color: var(--muted); }
.viz-seg button:hover { color: var(--fg); }
.viz-seg button[aria-checked="true"] { background: var(--subtle); color: var(--fg); }

.viz button:focus-visible { outline: 1px solid var(--muted); outline-offset: 1px; }

/* ---- cache ---- */
.viz-stage { overflow-x: auto; padding-bottom: .25rem; }
.viz-access { display: flex; align-items: center; gap: .75rem; margin-bottom: 1rem; }
.viz-key { font-family: var(--mono); font-size: 1.125rem; min-width: 1.5ch; }
.viz-badge::before {
  content: "";
  display: inline-block;
  width: 8px; height: 8px;
  border-radius: 50%;
  margin-right: .4em;
  background: var(--muted);
}
.viz-badge:empty::before { display: none; }
.viz-badge.hit::before { background: var(--viz-good); }
.viz-badge.miss::before { background: var(--viz-bad); }

.viz-slots { position: relative; height: 44px; }
.viz-slot, .viz-cell {
  position: absolute;
  top: 0; left: 0;
  width: 44px; height: 44px;
  border-radius: 6px;
}
.viz-slot { border: 1px dashed var(--rule); }
.viz-cell {
  display: grid;
  place-items: center;
  font-family: var(--mono);
  font-size: 1rem;
  background: var(--bg);
  border: 1px solid var(--muted);
  transition: transform .45s cubic-bezier(.2, .7, .2, 1), opacity .35s, border-color .2s, background-color .2s;
}
.viz-cell.is-new { border-color: var(--fg); box-shadow: 0 0 0 1px var(--fg); }
.viz-cell.is-hit { border-color: var(--viz-good); box-shadow: 0 0 0 1px var(--viz-good); }
.viz-cell.is-exit { border-color: var(--viz-bad); color: var(--viz-bad); }
.viz-ends { display: flex; justify-content: space-between; margin: .4rem 0 1rem; }

.viz-seq { display: flex; flex-wrap: wrap; gap: 4px; margin-top: .35rem; }
.viz-chip {
  font-family: var(--mono);
  font-size: .8125rem;
  min-width: 1.75rem;
  padding: .1rem .4rem;
  border: 1px solid var(--rule);
  border-radius: 4px;
  color: var(--muted);
}
.viz-chip.past { opacity: .45; }
.viz-chip.now { border-color: var(--fg); color: var(--fg); }

/* ---- ring ---- */
.viz-ring { display: block; width: 100%; max-width: 300px; height: auto; margin: 0 auto; overflow: visible; }
.viz-ring text { font: 500 12px var(--font); fill: var(--fg); }
.viz-ring .moved { fill: none; stroke: var(--fg); stroke-width: 1.5; animation: viz-in .5s ease-out; }
@keyframes viz-in { from { opacity: 0; } to { opacity: 1; } }

.viz-legend { display: flex; flex-wrap: wrap; gap: .25rem 1.25rem; list-style: none; margin: .75rem 0 0; padding: 0; justify-content: center; }
.viz-legend li { display: inline-flex; align-items: center; gap: .4rem; }
.viz-legend .sw { width: 10px; height: 10px; border-radius: 3px; }
.viz-legend .n { color: var(--muted); font-variant-numeric: tabular-nums; }

@media (prefers-reduced-motion: reduce) {
  .viz *, .viz *::before { transition: none !important; animation: none !important; }
}

/* Server-side diagram errors (malformed ```viz blocks) stay visible. */
.viz--error { border-color: var(--viz-bad); color: var(--viz-bad); }
.viz--error p { margin: 0; }
