:root {
  --bg: #f7f7f8;
  --surface: #ffffff;
  --text: #1c1c22;
  --muted: #6b6b78;
  --border: #e2e2e8;
  --accent: #4f46e5;
  --accent-soft: #eef0fe;
  --plus: #2563eb;
  --minus: #dc2626;
  --bar: #6366f1;
  --bar-track: #edeef4;
  --shadow: 0 1px 3px rgba(0, 0, 0, 0.06), 0 8px 24px rgba(0, 0, 0, 0.04);
  --radius: 14px;

  /* Mechanism viz series (validated: dataviz palette, all-pairs, light surface).
     Aqua sits below 3:1 on the light surface — legal because every point carries
     a visible direct label (the relief rule). */
  --viz-term: #2a78d6;
  --viz-result: #eb6834;
  --viz-neighbor: #1baf7a;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f0f14;
    --surface: #1a1a22;
    --text: #ececf1;
    --muted: #9a9aab;
    --border: #2a2a35;
    --accent: #8b83ff;
    --accent-soft: #23233a;
    --plus: #6ea8ff;
    --minus: #ff7a7a;
    --bar: #7c78ff;
    --bar-track: #23232e;
    --shadow: 0 1px 3px rgba(0, 0, 0, 0.4), 0 12px 32px rgba(0, 0, 0, 0.3);

    /* Same three hues re-stepped for the dark surface (validated separately —
       dark mode is selected, not an automatic flip). */
    --viz-term: #3987e5;
    --viz-result: #d95926;
    --viz-neighbor: #199e70;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
}

.app {
  max-width: 1120px; /* room for the three §4d columns */
  margin: 0 auto;
  padding: 3rem 1.25rem 4rem;
}

/* The masthead reads best at prose width; the builder row spans the app width,
   with the card at prose width on the left and the presets rail beside it. */
.masthead {
  max-width: 720px;
  margin-left: auto;
  margin-right: auto;
}
.builder-row {
  display: flex;
  align-items: flex-start;
  gap: 2rem;
  margin-bottom: 1.5rem;
}

.masthead h1 {
  margin: 0 0 1.5rem;
  font-size: 2.4rem;
  letter-spacing: -0.02em;
}

code {
  background: var(--accent-soft);
  color: var(--accent);
  padding: 0.1em 0.4em;
  border-radius: 6px;
  font-size: 0.92em;
}

/* ---- builder ---- */

.builder {
  flex: 0 1 720px;
  min-width: 0;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1.25rem;
}

.equation {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.4rem;
  min-height: 2.75rem;
  font-size: 1.25rem;
}

.equation-empty {
  color: var(--muted);
  font-size: 1rem;
}

.op {
  border: 1px solid transparent;
  background: transparent;
  font-size: 1.3rem;
  font-weight: 700;
  cursor: pointer;
  padding: 0 0.3rem;
  line-height: 1.4;
  border-radius: 7px;
}
.op-plus { color: var(--plus); }
.op-minus { color: var(--minus); }
.op:hover { background: var(--accent-soft); border-color: var(--border); }

.chip {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  background: var(--accent-soft);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 0.3rem 0.55rem 0.3rem 0.8rem;
}
.chip-word { font-weight: 600; }
.chip-x {
  border: none;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  font-size: 1.1rem;
  line-height: 1;
  padding: 0 0.1rem;
  border-radius: 50%;
}
.chip-x:hover { color: var(--minus); }

.add-term {
  display: flex;
  align-items: stretch;
  gap: 0.5rem;
  margin-top: 1rem;
}

.sign-toggle {
  flex: 0 0 auto;
  width: 3.1rem;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  font-size: 1.5rem;
  font-weight: 700;
  line-height: 1;
  cursor: pointer;
  transition: color 0.12s, border-color 0.12s, background 0.12s;
}
.sign-toggle.plus { color: var(--plus); }
.sign-toggle.minus { color: var(--minus); border-color: var(--minus); }
.sign-toggle:hover { background: var(--accent-soft); }

.input-wrap {
  position: relative;
  flex: 1 1 auto;
}

#word-input {
  width: 100%;
  padding: 0.7rem 0.9rem;
  font-size: 1rem;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg);
  color: var(--text);
}

.hint {
  margin: 0.7rem 0 0;
  color: var(--muted);
  font-size: 0.85rem;
}
.hint-pm {
  font-weight: 700;
  color: var(--text);
}
#word-input:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

.suggestions {
  position: absolute;
  z-index: 10;
  left: 0;
  right: 0;
  margin: 0.35rem 0 0;
  padding: 0.3rem;
  list-style: none;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: var(--shadow);
  max-height: 260px;
  overflow-y: auto;
}
.suggestion {
  padding: 0.45rem 0.6rem;
  border-radius: 7px;
  cursor: pointer;
}
.suggestion:hover,
.suggestion.active { background: var(--accent-soft); }

.status {
  margin: 0.75rem 0 0;
  color: var(--muted);
  font-size: 0.9rem;
  min-height: 1.2em;
}

/* ---- presets (the "Try:" rail just right of the builder card — saves
        vertical space so the viz stays above the fold) ---- */

.presets {
  flex: 0 0 auto;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 0.45rem;
}
.presets-label { color: var(--muted); font-size: 0.9rem; }
.preset-list {
  display: grid;
  grid-template-columns: repeat(2, auto);
  justify-items: start;
  gap: 0.45rem;
}
.preset {
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  border-radius: 999px;
  padding: 0.3rem 0.7rem;
  font-size: 0.85rem;
  cursor: pointer;
  white-space: nowrap;
}
.preset:hover { border-color: var(--accent); color: var(--accent); }

/* Narrow screens: the rail folds back under the card as a wrapping row. */
@media (max-width: 900px) {
  .builder-row { flex-direction: column; gap: 1rem; }
  .presets { flex-direction: row; align-items: center; flex-wrap: wrap; }
  .preset-list { display: flex; flex-wrap: wrap; }
}

/* ---- results / comparison columns (SPEC §4d) ---- */

.compare-bar {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 1rem;
  margin: 0 0 0.75rem;
}
.compare-bar h2 {
  font-size: 0.85rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
  margin: 0;
}
/* Always one column per registry entry, side by side (SPEC §4d: comparison is
   the experience). --corpus-count is set from CORPORA by renderColumns, so the
   track count has one source of truth. Below the minimum column width the row
   scrolls horizontally — it never wraps. */
.columns {
  display: grid;
  grid-template-columns: repeat(var(--corpus-count, 3), minmax(300px, 1fr));
  gap: 1rem;
  overflow-x: auto;
  padding-bottom: 0.5rem; /* keep the scrollbar off the column shadows */
}

.column {
  display: flex;
  flex-direction: column;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 0.9rem 1rem 1rem;
  min-width: 0; /* let grid tracks shrink below content width */
}
/* Bottom-align the lists: viz heights vary per column (content-adaptive), so
   pinning the lists to the card bottom keeps the three rank #1 rows level. */
.column .results-list { margin-top: auto; }
.column-head {
  display: flex;
  align-items: baseline;
  gap: 0.5rem;
  margin-bottom: 0.6rem;
}
.column-name { font-weight: 600; }
.column-tokens { color: var(--muted); font-size: 0.85rem; }
.column-status {
  margin: 0.5rem 0;
  color: var(--muted);
  font-size: 0.85rem;
}
.column-viz { margin-bottom: 0.75rem; }

.results-list { list-style: none; margin: 0; padding: 0; }
.result {
  display: grid;
  grid-template-columns: 8.5rem 1fr 3.5rem;
  align-items: center;
  gap: 0.75rem;
  padding: 0.35rem 0;
}
.result-word {
  color: var(--text);
  font-size: 1.05rem;
  font-weight: 600;
  padding: 0.15rem 0;
}
.result:first-child .result-word { color: var(--accent); }
.result-bar {
  height: 8px;
  background: var(--bar-track);
  border-radius: 999px;
  overflow: hidden;
}
.result-fill {
  height: 100%;
  background: var(--bar);
  border-radius: 999px;
  transition: width 0.2s ease;
}
.result-score {
  font-variant-numeric: tabular-nums;
  color: var(--muted);
  font-size: 0.9rem;
  text-align: right;
}

/* ---- mechanism viz (SPEC §4c, one instance per column) ---- */

.viz-canvas { position: relative; width: 100%; } /* anchors the tooltip to the SVG's own origin */
.viz-svg { display: block; width: 100%; height: auto; }

/* marks — data wears the series color */
.viz-arrow-term   { stroke: var(--viz-term);   stroke-width: 2; stroke-linecap: round; }
.viz-arrow-result { stroke: var(--viz-result); stroke-width: 2; stroke-linecap: round; }
.viz-head-term    { fill: var(--viz-term); }
.viz-head-result  { fill: var(--viz-result); }

.viz-dot { stroke: var(--surface); stroke-width: 2; } /* 2px surface ring */
.viz-dot-result   { fill: var(--viz-result); }
.viz-dot-neighbor { fill: var(--viz-neighbor); }

/* chrome — recessive hairlines, never data-weight */
.viz-connector   { stroke: var(--muted); stroke-width: 1.5; stroke-dasharray: 4 4; }
.viz-origin-line { stroke: var(--muted); stroke-width: 1; }

/* text wears text tokens, never the series color */
.viz-label {
  fill: var(--muted);
  font-size: 13px;
  font-family: inherit;
  /* Surface-colored halo (the text version of the dots' 2px surface ring), so
     labels stay legible where they cross arrows, dots, or other labels. */
  paint-order: stroke;
  stroke: var(--surface);
  stroke-width: 3px;
  stroke-linejoin: round;
}
.viz-label-strong { fill: var(--text); font-weight: 600; }
.viz-label-step { font-style: italic; } /* movements, not places — see arrowLabel() */
.viz-label-approx { fill: var(--muted); font-size: 15px; }

.viz-hit {
  fill: transparent;
  cursor: default;
  outline: none;
}
.viz-hit:focus-visible { stroke: var(--accent); stroke-width: 2; }

/* `display: flex` would beat the UA's `[hidden] { display: none }` and leave an
   empty pill floating in the corner — restate hidden explicitly. */
.viz-tooltip[hidden] { display: none; }

.viz-tooltip {
  position: absolute;
  transform: translate(-50%, calc(-100% - 14px));
  display: flex;
  flex-direction: column;
  align-items: center;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: var(--shadow);
  padding: 0.35rem 0.6rem;
  pointer-events: none;
  white-space: nowrap;
  z-index: 5;
}
.viz-tooltip-value { font-weight: 600; color: var(--text); }
.viz-tooltip-detail { font-size: 0.8rem; color: var(--muted); }

.viz-legend {
  display: flex;
  flex-wrap: wrap;
  gap: 1rem;
}
.viz-legend[hidden] { display: none; } /* flex would beat the UA [hidden] rule */
.viz-key {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  font-size: 0.85rem;
  color: var(--text);
}
.viz-swatch {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  flex: 0 0 auto;
}
.viz-swatch-line {
  width: 14px;
  height: 2px;
  border-radius: 1px;
}
.viz-key-term     { background: var(--viz-term); }
.viz-key-result   { background: var(--viz-result); }
.viz-key-neighbor { background: var(--viz-neighbor); }

.column-desc {
  margin: 0.9rem 0 0;
  padding-top: 0.75rem;
  border-top: 1px solid var(--border);
  color: var(--muted);
  font-size: 0.8rem;
  line-height: 1.45;
}
/* In a corpus column with no results list — still loading / assets missing
   (status + desc) or ready with an empty equation (head + desc) — the
   description still sits at the card bottom. When the list exists, its own
   margin-top: auto wins and the description follows it. */
.column-status + .column-desc,
.column-head + .column-desc { margin-top: auto; }

@media (max-width: 520px) {
  .result { grid-template-columns: 6.5rem 1fr 3rem; }
  .masthead h1 { font-size: 2rem; }
}
