/**
 * components.css: buttons, top bar, panels, table of contents, viewer controls, log, code
 * drawer, legend, counters, and the role colors for SVG elements and legend swatches.
 * Uses theme and role tokens from nord.css only.
 */

/* Top bar */
.topbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.6rem 1rem;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
}
.brand {
  font-size: 1.05rem;
}
.crumbs {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 0.5rem;
}
.crumbs .sep {
  color: var(--muted);
}
.topbar-actions {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.4rem;
}

/* Buttons */
.api-panel {
  margin-bottom: 0.8rem;
  min-width: 0;
}
.api-panel select {
  display: block;
  width: 100%;
  max-width: 100%;
  margin-top: 0.4rem;
  font: inherit;
}
.api-args label {
  display: block;
  margin: 0.5rem 0;
}
.api-args input,
.api-args textarea {
  display: block;
  width: 100%;
  box-sizing: border-box;
  font: inherit;
}
.api-phase {
  display: inline-block;
  margin: 0.7rem 0.7rem 0 0;
  font-weight: bold;
}
#api-result,
#api-contract {
  overflow-wrap: anywhere;
  white-space: pre-wrap;
}

.btn {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.35rem 0.7rem;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: var(--panel-2);
  color: var(--text);
  font: inherit;
  text-decoration: none;
  cursor: pointer;
}
.btn:hover {
  border-color: var(--accent);
}
.btn:disabled {
  opacity: 0.45;
  cursor: default;
}
.btn-primary {
  background: var(--accent-strong);
  color: var(--on-accent);
  border-color: var(--accent-strong);
}
.btn-ghost {
  background: transparent;
}
.btn-small {
  padding: 0.15rem 0.5rem;
  font-size: 0.85rem;
}
.icon {
  /* Nerd Font glyphs can be wider than one cell; reserve room so they never touch the label. */
  display: inline-block;
  min-width: 1.3em;
  text-align: center;
  font-size: 1.05em;
  line-height: 1;
}

/* Theme toggle shows the icon of the theme you would switch to. */
:root[data-theme='dark'] .icon-moon,
:root[data-theme='light'] .icon-sun {
  display: none;
}

/* Panels */
.panel {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.7rem 0.8rem;
}
.panel h2 {
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--accent);
  margin-bottom: 0.4rem;
}
.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
}
.muted {
  color: var(--muted);
}

/* Table of contents */
.toc-intro {
  max-width: 70ch;
}
.catalog-filters {
  display: flex;
  align-items: end;
  flex-wrap: wrap;
  gap: 0.8rem;
}
.catalog-filters label {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  font-size: 0.85rem;
}
.catalog-search {
  flex: 1 1 18rem;
  min-width: 0;
}
.catalog-filters input[type='search'],
.catalog-filters select {
  width: 100%;
  min-width: 0;
  padding: 0.4rem 0.5rem;
  border: 1px solid var(--border);
  border-radius: 4px;
  color: var(--text);
  background: var(--panel);
  font: inherit;
}
.catalog-filters .catalog-checkbox {
  flex-direction: row;
  align-items: center;
  padding: 0.4rem 0;
}
.toc-chapter-title {
  display: flex;
  align-items: baseline;
  gap: 0.6rem;
  margin: 1.4rem 0 0.4rem;
  font-size: 1.15rem;
}
.toc-num {
  color: var(--accent);
}
.toc-progress {
  margin-left: auto;
  font-size: 0.8rem;
  color: var(--muted);
}
.toc-list {
  list-style: none;
  margin: 0;
  padding: 0;
  border-top: 1px solid var(--border);
}
.toc-entry {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 0 1rem;
  padding: 0.3rem 0.4rem;
  border-bottom: 1px solid var(--border);
}
.toc-link {
  display: grid;
  grid-template-columns: 3.5rem minmax(0, 1fr) auto;
  gap: 0.8rem;
  flex: 1 1 32rem;
  color: var(--text);
  text-decoration: none;
}
a.toc-link:hover .toc-title {
  color: var(--accent);
  text-decoration: underline;
}
.toc-sec {
  color: var(--accent);
}
.toc-source,
.toc-note {
  color: var(--muted);
  font-size: 0.85rem;
}
/* Notes always sit on their own line, aligned with the title column. */
.toc-note {
  flex-basis: 100%;
  padding-left: 4.3rem;
}
/* Dim the whole row, so a title never looks fainter than its own note. */
.toc-entry:not(.status-done) {
  opacity: 0.6;
}
@media (max-width: 600px) {
  .toc-link {
    grid-template-columns: 2.5rem minmax(0, 1fr);
    gap: 0.25rem 0.6rem;
  }
  .toc-source {
    grid-column: 2;
    overflow-wrap: anywhere;
  }
  .toc-note {
    padding-left: 3.1rem;
  }
}

/* Viewer: stage, counters, legend */
.stage-wrap {
  position: relative;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.5rem;
}
.stage {
  display: block;
  width: calc(100% * var(--diagram-zoom, 1));
  height: auto;
  min-height: 12rem;
}
.counters {
  display: flex;
  flex-wrap: wrap;
  gap: 1rem;
  font-size: 0.85rem;
  color: var(--muted);
}
.counters b {
  color: var(--text);
}
.stage-toolbar,
.diagram-tools {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 0.4rem;
}
.stage-toolbar {
  justify-content: space-between;
  margin-bottom: 0.5rem;
}
.diagram-tools {
  font-size: 0.85rem;
}
#zoom-level {
  min-width: 4ch;
  text-align: center;
}
.stage-viewport {
  overflow: auto;
  max-height: 70vh;
}
.stage-col:fullscreen,
.stage-col.is-expanded {
  padding: 1rem;
  background: var(--bg);
  width: 100%;
  height: 100%;
  overflow: auto;
}
.stage-col.is-expanded {
  position: fixed;
  inset: 0;
  z-index: 10;
}
.diagram-expanded {
  overflow: hidden;
}
.stage-col:fullscreen .stage-wrap,
.stage-col.is-expanded .stage-wrap {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-height: 0;
}
.stage-col:fullscreen .stage-viewport,
.stage-col.is-expanded .stage-viewport {
  max-height: none;
  min-height: 0;
  flex: 1;
}
.stage-col:fullscreen .stage,
.stage-col.is-expanded .stage {
  height: calc(100% * var(--diagram-zoom, 1));
  min-height: 0;
}
.legend {
  display: flex;
  flex-wrap: wrap;
  gap: 0.3rem 1.1rem;
  list-style: none;
  margin: 0.5rem 0;
  padding: 0;
  font-size: 0.85rem;
}
.legend li {
  display: flex;
  align-items: center;
  gap: 0.4rem;
}
.swatch {
  width: 0.9rem;
  height: 0.9rem;
  border-radius: 2px;
  border: 1px solid var(--role-stroke);
}

/* Viewer: controls */
.controls {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.4rem;
  padding: 0.5rem;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 6px;
}
.scrubber {
  flex: 1 1 8rem;
  accent-color: var(--accent);
}
.step-label {
  min-width: 9ch;
  font-size: 0.85rem;
  color: var(--muted);
}
.speed select {
  font: inherit;
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 4px;
}

/* Viewer: saved-run controls */
.remember-run,
.run-import {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  font-size: 0.85rem;
}
.run-import {
  flex-direction: column;
  align-items: start;
}
.run-import input {
  width: 100%;
  min-width: 0;
  color: var(--text);
  font: inherit;
}
.saved-run-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin: 0.75rem 0;
}
#saved-run-status {
  font-size: 0.85rem;
  overflow-wrap: anywhere;
}

/* Viewer: shortcut guide */
.shortcut-guide {
  padding: 0.4rem 0.5rem;
  color: var(--muted);
  font-size: 0.85rem;
}
.shortcut-guide summary {
  cursor: pointer;
}
.shortcut-list {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem 1.25rem;
  list-style: none;
  padding: 0;
}
.shortcut-guide p {
  margin-bottom: 0;
}
kbd {
  padding: 0.05rem 0.25rem;
  border: 1px solid var(--border);
  border-radius: 3px;
  background: var(--panel-2);
  color: var(--text);
  font: inherit;
}

/* Viewer: narration log */
.log {
  position: relative; /* offsetParent for the scroll-into-view math in playback.js */
  list-style: none;
  margin: 0;
  padding: 0;
  max-height: 50vh;
  overflow-y: auto;
}
.log-line {
  display: flex;
  gap: 0.6rem;
  width: 100%;
  padding: 0.2rem 0.3rem;
  border: 0;
  border-bottom: 1px solid var(--panel-2);
  background: none;
  color: var(--muted);
  font: inherit;
  font-size: 0.85rem;
  text-align: left;
  cursor: pointer;
}
.log-n {
  min-width: 3ch;
  text-align: right;
  opacity: 0.7;
}
.log li[aria-current='step'] .log-line {
  background: var(--panel-2);
  color: var(--text);
  box-shadow: inset 3px 0 0 var(--accent);
}

/* Viewer: code drawer */
.code {
  margin: 0;
  padding: 0.5rem 0;
  overflow-x: auto;
  background: var(--code-bg);
  border-radius: 4px;
  font-size: 0.82rem;
  line-height: 1.5;
  font-variant-ligatures: contextual; /* Fira Code ligatures for <=, !=, && */
}
.code-line {
  display: block;
  padding: 0 0.7rem;
  white-space: pre;
}
.code-line.is-current {
  background: var(--panel-2);
  box-shadow: inset 3px 0 0 var(--role-compare);
}
.api-mode .code {
  position: relative;
  max-height: min(70vh, 42rem);
  overflow: auto;
}
.tree-reference,
.tree-detached {
  fill: var(--text);
  font-size: 11px;
}

/* Viewer: input panel */
.chips {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
}
.chip[aria-pressed='true'] {
  background: var(--accent-strong);
  color: var(--on-accent);
  border-color: var(--accent-strong);
}
.seed-form,
.custom-form {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.4rem;
  margin-top: 0.5rem;
}
.custom-form textarea,
.seed-form input {
  font: inherit;
  background: var(--code-bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 0.3rem 0.4rem;
}
.custom-form textarea {
  width: 100%;
}
.error {
  color: var(--error);
  margin: 0.4rem 0 0;
}
.viewer-error {
  max-width: 60ch;
  margin: 3rem auto;
  padding: 1rem 1.2rem;
  background: var(--panel);
  border: 1px solid var(--error);
  border-radius: 6px;
}

/* Role colors for SVG elements and legend swatches */
.role-default {
  fill: var(--role-default);
  background: var(--role-default);
}
.role-progress {
  fill: var(--role-progress);
  background: var(--role-progress);
}
.role-active {
  fill: var(--role-active);
  background: var(--role-active);
}
.role-compare {
  fill: var(--role-compare);
  background: var(--role-compare);
}
.role-final {
  fill: var(--role-final);
  background: var(--role-final);
}
.role-special {
  fill: var(--role-special);
  background: var(--role-special);
}
.role-reject {
  fill: var(--role-reject);
  background: var(--role-reject);
}
.role-path {
  fill: var(--role-path);
  background: var(--role-path);
}

/* Bars: role colors plus a shape cue so no state relies on color alone. */
.bar {
  stroke: var(--role-stroke);
  stroke-width: 1;
}
.bar.role-default {
  stroke: var(--default-stroke);
  stroke-opacity: 0.35;
}
.bar.role-active {
  stroke: var(--text);
  stroke-width: 2;
}
.bar.role-compare {
  stroke: var(--text);
  stroke-width: 2;
  stroke-dasharray: 4 3;
}
.bar-label {
  fill: var(--text);
  font-size: 13px;
}
.bar-label.is-marked {
  font-weight: 700;
}
/* Non-color cues: dotted outline for "in progress", underlined label for "final". */
.bar.role-progress {
  stroke: var(--text);
  stroke-width: 1.5;
  stroke-dasharray: 1 3;
}
.bar-label.label-final {
  text-decoration: underline;
  text-decoration-thickness: 2px;
}
.slot-empty,
.cell.cell-empty {
  fill: none;
  stroke: var(--muted);
  stroke-opacity: 0.6;
  stroke-dasharray: 3 3;
}
.is-dim {
  opacity: 0.3;
}
.pointer {
  fill: var(--accent);
  font-size: 11px;
  font-weight: 700;
}
.badge,
.row-label {
  fill: var(--muted);
  font-size: 12px;
}

/* Heap renderer */
.heap-edge {
  stroke: var(--border);
  stroke-width: 1.5;
}
.heap-node {
  stroke: var(--role-stroke);
  stroke-width: 1;
}
.heap-node.role-default {
  stroke: var(--default-stroke);
  stroke-opacity: 0.5;
}
.heap-node.role-active,
.heap-node.role-compare,
.cell.role-active,
.cell.role-compare {
  stroke: var(--text);
  stroke-width: 2;
}
.heap-node.role-compare,
.cell.role-compare {
  stroke-dasharray: 4 3;
}
.heap-key,
.cell-text {
  fill: var(--text);
  font-size: 12px;
  font-weight: 700;
}
.heap-index,
.cell-index {
  fill: var(--muted);
  font-size: 9px;
}
.cell {
  stroke: var(--role-stroke);
}

/* Call stack of recursive sorts */
.stack {
  display: flex;
  flex-wrap: wrap;
  gap: 0.3rem;
  list-style: none;
  margin: 0;
  padding: 0;
  font-size: 0.8rem;
  color: var(--muted);
}
.stack li {
  padding: 0.05rem 0.4rem;
  border: 1px solid var(--border);
  border-radius: 3px;
}
.stack li:last-child {
  color: var(--text);
  border-color: var(--accent);
}

/* Footer */
.footer {
  padding: 1.5rem 1rem;
  color: var(--muted);
  font-size: 0.8rem;
  text-align: center;
}

/* The independent-status note under the footer text. */
.footer-note {
  max-width: 46rem;
  margin: 0.5rem auto 0;
}

/* Linked lists */
.node-link,
.null-link {
  stroke: var(--muted);
  stroke-width: 1.5;
  marker-end: none;
}
.node-arrow {
  fill: var(--muted);
}
.null-label,
.null-pointer {
  fill: var(--muted);
  font-size: 11px;
}

/* Doubling-experiment chart */
.axis {
  stroke: var(--muted);
  stroke-width: 1;
}
.axis-label,
.axis-tick {
  fill: var(--muted);
  font-size: 11px;
}
.chart-line {
  fill: none;
  stroke: var(--role-progress);
  stroke-width: 2;
}
.chart-point {
  fill: var(--role-active);
  stroke: var(--text);
  stroke-width: 1;
}

/* Union-find forest */
.site-link {
  stroke: var(--muted);
  stroke-width: 1.5;
}

/* Binary search trees */
.tree-link {
  stroke: var(--muted);
  stroke-width: 1.5;
}
.tree-link.link-red {
  stroke: var(--role-reject);
  stroke-width: 4;
}
.tree-size {
  fill: var(--accent);
  font-size: 9px;
}

/* String sorts */
.str-row,
.str-count {
  fill: var(--muted);
  font-size: 11px;
}
.str-char {
  fill: var(--text);
  font-size: 13px;
}
.str-char-d {
  font-weight: 700;
  text-decoration: underline;
}
/* Interior labels carry their shape's role; labels outside shapes use theme text colors. */
text[data-role] {
  fill: var(--on-role);
}
text[data-role='default'] {
  fill: var(--on-role-default);
}
.str-bracket {
  fill: none;
  stroke: var(--role-active);
  stroke-width: 2;
}

/* Collision system */
.particle-box {
  fill: none;
  stroke: var(--muted);
  stroke-width: 2;
}
.particle {
  stroke: var(--role-stroke);
  stroke-width: 1;
  fill-opacity: 0.85;
}
.particle.role-active,
.particle.role-compare {
  stroke-width: 4;
}
/* The halo keeps tiny particle labels readable even beyond their custom-colored circles. */
.particle-label {
  fill: var(--on-role-default);
  stroke: var(--contrast-ink);
  stroke-width: 3;
  stroke-linejoin: round;
  paint-order: stroke fill;
}

/* Tries and TSTs */
.tree-empty {
  fill: var(--muted);
  font-size: 12px;
}
.tree-value {
  fill: var(--accent);
  font-size: 9px;
}
.tree-link.link-left,
.tree-link.link-right {
  stroke-dasharray: 4 3;
}
.tree-link.link-mid {
  stroke-width: 2.5;
}

/* Graphs */
.edge {
  fill: none;
  stroke: var(--muted);
  stroke-opacity: 0.6;
  stroke-width: 1.5;
}
.edge.role-edge-final,
.edge.role-edge-path,
.edge.role-edge-compare,
.edge.role-edge-active,
.edge.role-edge-reject,
.edge.role-edge-special,
.edge.role-edge-progress {
  stroke-opacity: 1;
  stroke-width: 4;
}
.role-edge-final {
  stroke: var(--role-final);
  fill: var(--role-final);
}
.role-edge-path {
  stroke: var(--role-path);
  fill: var(--role-path);
}
.role-edge-compare {
  stroke: var(--role-compare);
  fill: var(--role-compare);
}
.role-edge-active {
  stroke: var(--role-active);
  fill: var(--role-active);
}
.role-edge-reject {
  stroke: var(--role-reject);
  fill: var(--role-reject);
}
.role-edge-special {
  stroke: var(--role-special);
  fill: var(--role-special);
}
.role-edge-progress {
  stroke: var(--role-progress);
  fill: var(--role-progress);
}
.edge-arrow.role-edge-default {
  fill: var(--muted);
  stroke: none;
}
.edge-arrow {
  stroke: none;
}
.edge-weight {
  fill: var(--muted);
  font-size: 10px;
  /* A halo in the panel color keeps a label readable where it crosses an edge. */
  paint-order: stroke;
  stroke: var(--panel);
  stroke-width: 3px;
}

/* Geometry (points renderer) */
.geo-strip {
  fill: var(--role-compare);
  fill-opacity: 0.12;
}
.geo-divider {
  stroke: var(--muted);
  stroke-dasharray: 5 4;
  stroke-width: 1.5;
}
.geo-label {
  fill: var(--muted);
  font-size: 10px;
}
