/* NCA-LM demo — light, sharp, compact.
 *
 * The page is a control surface for one dark object: the automaton's grid. Everything
 * else is white panels, hairline rules and monospaced numbers, so the only saturated
 * thing on screen is the thing that is actually computing. Corners are square by
 * design — 2px at most — because the subject is a lattice.
 */

:root {
  /* ink + paper */
  --ink:       #0a0a0c;
  --ink-2:     #3b3d46;
  --dim:       #767a86;
  --faint:     #a2a6b1;
  --paper:     #f2f2f5;
  --panel:     #ffffff;
  --panel-2:   #f7f7fa;
  --line:      #e2e2e9;
  --line-2:    #cdced8;

  /* one accent, three signals — no other colour is allowed in the chrome */
  --acc:       #4b21ef;
  --acc-ink:   #3512c4;
  --acc-wash:  #eeeaff;
  --good:      #0a8f5b;
  --bad:       #d92d3c;
  --warn:      #b46a00;

  --mono: ui-monospace, "SF Mono", "JetBrains Mono", "Fira Code", Menlo, Consolas, monospace;
  --sans: "Inter", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;

  --pad: 14px;
  --gap: 10px;
  --max: 1560px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  color: var(--ink);
  background: var(--paper);
  font: 13.5px/1.5 var(--sans);
  font-feature-settings: "tnum" 1, "cv05" 1;
  -webkit-font-smoothing: antialiased;
}

/* Graph paper, barely there. Techno without being loud. */
body {
  background-image:
    linear-gradient(to right,  rgba(10,10,12,0.035) 1px, transparent 1px),
    linear-gradient(to bottom, rgba(10,10,12,0.035) 1px, transparent 1px);
  background-size: 34px 34px;
  background-position: -1px -1px;
}

b, strong { font-weight: 650; }
em { font-style: normal; font-weight: 650; }

.mono { font-family: var(--mono); }
.dim  { color: var(--dim); }

/* ── top bar ──────────────────────────────────────────────────────────────── */

.topbar {
  max-width: var(--max);
  margin: 0 auto;
  padding: 18px 18px 12px;
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  align-items: center;
  gap: 22px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 9px;
}

/* Four cells of a lattice, one of them lit: the logo is the model. */
.mark {
  width: 22px;
  height: 22px;
  flex: none;
  background:
    linear-gradient(var(--acc), var(--acc)) 0 0 / 10px 10px no-repeat,
    linear-gradient(var(--ink), var(--ink)) 12px 0 / 10px 10px no-repeat,
    linear-gradient(var(--ink), var(--ink)) 0 12px / 10px 10px no-repeat,
    linear-gradient(var(--line-2), var(--line-2)) 12px 12px / 10px 10px no-repeat;
}

.wordmark {
  font-size: 20px;
  font-weight: 800;
  letter-spacing: -0.045em;
  line-height: 1;
}

.wordmark .slash { color: var(--acc); margin: 0 -1px; }

.tag {
  font-family: var(--mono);
  font-size: 9.5px;
  text-transform: uppercase;
  letter-spacing: 0.14em;
  color: var(--dim);
  border: 1px solid var(--line-2);
  border-radius: 2px;
  padding: 3px 6px 2px;
  margin-left: 4px;
}

.claim {
  margin: 0;
  max-width: 82ch;
  font-size: 13px;
  color: var(--ink-2);
}

.claim .neg {
  font-family: var(--mono);
  font-size: 11.5px;
  color: var(--acc-ink);
  background: var(--acc-wash);
  padding: 1px 5px;
  border-radius: 2px;
  white-space: nowrap;
}

.topstats {
  display: flex;
  gap: 0;
  border: 1px solid var(--line);
  background: var(--panel);
}

.stat {
  padding: 7px 13px 6px;
  border-right: 1px solid var(--line);
  min-width: 92px;
}

.stat:last-child { border-right: 0; }

.stat .k {
  display: block;
  font-family: var(--mono);
  font-size: 9px;
  text-transform: uppercase;
  letter-spacing: 0.13em;
  color: var(--faint);
}

.stat b {
  font-family: var(--mono);
  font-size: 15px;
  font-weight: 700;
  letter-spacing: -0.02em;
}

.stat b.good { color: var(--good); }
.stat b.bad  { color: var(--bad); }
.stat b.warn { color: var(--warn); }

/* ── app shell ────────────────────────────────────────────────────────────── */

.app {
  max-width: var(--max);
  margin: 0 auto;
  padding: 0 18px;
  display: grid;
  grid-template-columns: 274px minmax(420px, 1fr) minmax(300px, 400px);
  gap: var(--gap);
  align-items: start;
}

.wide {
  max-width: var(--max);
  margin: var(--gap) auto 0;
  padding: 0 18px;
}

.col {
  display: flex;
  flex-direction: column;
  gap: var(--gap);
  min-width: 0;
}

/* The automaton stays put while text streams next to it. */
.stage { position: sticky; top: var(--gap); }

@media (max-width: 1240px) {
  .app { grid-template-columns: 260px minmax(0, 1fr); }
  .out { grid-column: 1 / -1; flex-direction: row; }
  .out .block { flex: 1 1 0; min-width: 0; }
  .stage { position: static; }
}

@media (max-width: 860px) {
  .app { grid-template-columns: minmax(0, 1fr); }
  .out { flex-direction: column; }
  .topbar { grid-template-columns: minmax(0, 1fr); gap: 12px; }
  .topstats { overflow-x: auto; }
}

/* ── panels ───────────────────────────────────────────────────────────────── */

.block {
  background: var(--panel);
  border: 1px solid var(--line);
  padding: var(--pad);
  min-width: 0;
}

.block.flush { padding: var(--pad) var(--pad) 10px; }
.block.grow { flex: 1 1 auto; }

.blocktitle {
  margin: 0 0 10px;
  font-family: var(--mono);
  font-size: 9.5px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.15em;
  color: var(--ink);
  padding-bottom: 7px;
  border-bottom: 1px solid var(--line);
}

.blocktitle.row-title {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 12px;
}

.blocktitle .dim {
  font-weight: 500;
  letter-spacing: 0.06em;
  text-transform: none;
  font-size: 10px;
  color: var(--dim);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* ── form controls ────────────────────────────────────────────────────────── */

label {
  display: block;
  font-family: var(--mono);
  font-size: 9.5px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: var(--dim);
  margin-bottom: 5px;
}

output {
  font-family: var(--mono);
  color: var(--acc-ink);
  font-weight: 700;
  letter-spacing: -0.01em;
}

select, textarea, button, input {
  font-family: inherit;
  border-radius: 2px;
}

select {
  width: 100%;
  background: var(--panel-2);
  color: var(--ink);
  border: 1px solid var(--line-2);
  padding: 8px 30px 8px 9px;
  font-family: var(--mono);
  font-size: 11.5px;
  appearance: none;
  cursor: pointer;
  background-image:
    linear-gradient(45deg, transparent 50%, var(--ink) 50%),
    linear-gradient(135deg, var(--ink) 50%, transparent 50%);
  background-position: calc(100% - 15px) 15px, calc(100% - 11px) 15px;
  background-size: 4px 4px, 4px 4px;
  background-repeat: no-repeat;
}

select:hover { border-color: var(--acc); }

textarea {
  width: 100%;
  background: var(--panel-2);
  color: var(--ink);
  border: 1px solid var(--line-2);
  padding: 9px 10px;
  font-family: var(--mono);
  font-size: 12.5px;
  line-height: 1.45;
  resize: vertical;
}

textarea:focus, select:focus, button:focus-visible, input:focus-visible {
  outline: 2px solid var(--acc);
  outline-offset: 1px;
  border-color: var(--acc);
}

.row {
  display: flex;
  gap: 14px;
  margin-top: 12px;
  flex-wrap: wrap;
}

.field { flex: 1 1 130px; min-width: 120px; }

/* Square-thumbed sliders: the rest of the page is a lattice, so are these. */
input[type="range"] {
  width: 100%;
  height: 14px;
  appearance: none;
  background: transparent;
  cursor: pointer;
}

input[type="range"]::-webkit-slider-runnable-track {
  height: 3px;
  background: var(--line-2);
}
input[type="range"]::-moz-range-track {
  height: 3px;
  background: var(--line-2);
}
input[type="range"]::-webkit-slider-thumb {
  appearance: none;
  width: 10px;
  height: 14px;
  margin-top: -5.5px;
  background: var(--acc);
  border: 0;
  border-radius: 1px;
}
input[type="range"]::-moz-range-thumb {
  width: 10px;
  height: 14px;
  background: var(--acc);
  border: 0;
  border-radius: 1px;
}
input[type="range"]:disabled { opacity: 0.35; cursor: default; }
input[type="range"]:disabled::-webkit-slider-thumb { background: var(--faint); }
input[type="range"]:disabled::-moz-range-thumb { background: var(--faint); }

label.inline {
  display: inline;
  font-size: 9.5px;
  text-transform: none;
  letter-spacing: 0.02em;
  color: var(--faint);
  margin-left: 6px;
  cursor: pointer;
}
label.inline input { accent-color: var(--acc); vertical-align: -1px; }

.buttons {
  display: flex;
  gap: 8px;
  margin-top: 14px;
  align-items: center;
  flex-wrap: wrap;
}

button {
  background: var(--panel);
  color: var(--ink);
  border: 1px solid var(--line-2);
  padding: 8px 15px;
  font-size: 12.5px;
  font-weight: 650;
  letter-spacing: -0.005em;
  cursor: pointer;
}

button:hover:not(:disabled) { border-color: var(--ink); }
button:disabled { opacity: 0.36; cursor: default; }

button.primary {
  background: var(--acc);
  border-color: var(--acc);
  color: #fff;
  padding-inline: 20px;
  box-shadow: 0 1px 0 var(--acc-ink);
}
button.primary:hover:not(:disabled) { background: var(--acc-ink); border-color: var(--acc-ink); }

.hint {
  flex: 1 1 220px;
  min-width: 200px;
  font-size: 11px;
  line-height: 1.4;
  color: var(--dim);
}
.hint .mono { color: var(--ink-2); }

/* ── the grid itself ──────────────────────────────────────────────────────── */

.canvas-wrap {
  position: relative;
  width: 100%;
  aspect-ratio: var(--aspect, 1);
  background: #0b0416;
  border: 1px solid var(--ink);
}

canvas#grid {
  display: block;
  width: 100%;
  height: 100%;
  image-rendering: pixelated;
}

.stage-foot {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  margin-top: 9px;
  font-size: 10.5px;
  color: var(--dim);
}

.legend { display: flex; gap: 14px; flex-wrap: wrap; }

.swatch {
  display: inline-block;
  width: 9px;
  height: 9px;
  margin-right: 5px;
  vertical-align: -1px;
}

/* The write and read regions, drawn as exact cell outlines by drawRegions().
   Two canvases rather than one so each can be flashed on its own. */
.region {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  image-rendering: auto;
}

.region.write { filter: drop-shadow(0 0 3px rgba(167, 139, 255, 0.8)); }
.region.read  { filter: drop-shadow(0 0 3px rgba(22, 224, 142, 0.6)); }

/* Flash cues. A token write is a discontinuous overwrite of the stamped cells and
   a read is an instantaneous sample, so both are single events the animation cannot
   otherwise show. Driven by CSS animation rather than the render loop: they then
   run on the compositor and stay smooth even when a frame is slow. */
.region.write.flash { animation: writePulse var(--flash-ms, 180ms) ease-out; }
.region.read.flash  { animation: readPulse  var(--flash-ms, 180ms) ease-out; }

@keyframes writePulse {
  0%   { filter: drop-shadow(0 0 10px #fff) drop-shadow(0 0 22px rgba(167, 139, 255, 1))
                 brightness(2.4); }
  40%  { filter: drop-shadow(0 0 7px rgba(167, 139, 255, 0.95)) brightness(1.7); }
  100% { filter: drop-shadow(0 0 3px rgba(167, 139, 255, 0.8)); }
}

@keyframes readPulse {
  0%   { filter: drop-shadow(0 0 12px #16e08e) drop-shadow(0 0 24px rgba(22, 224, 142, 0.9))
                 brightness(2.4); }
  40%  { filter: drop-shadow(0 0 7px rgba(22, 224, 142, 0.9)) brightness(1.6); }
  100% { filter: drop-shadow(0 0 3px rgba(22, 224, 142, 0.6)); }
}

.swatch.write { background: #7c5cf0; }
.swatch.read  { background: #0aa86a; }

#regionInfo { color: var(--faint); font-size: 10px; }

/* ── output ───────────────────────────────────────────────────────────────── */

.text-out {
  background: var(--panel-2);
  border: 1px solid var(--line);
  padding: 11px 12px;
  /* Fixed, not min/max: a box that grows as tokens arrive reflows the page and
     shifts the grid on every step. */
  height: 214px;
  overflow-y: auto;
  font-family: var(--mono);
  font-size: 12.5px;
  line-height: 1.55;
  white-space: pre-wrap;
  word-break: break-word;
}

.text-out .prompt { color: var(--faint); }
.text-out .gen { color: var(--ink); font-weight: 550; }
.text-out .cursor { background: var(--acc); color: #fff; }
.text-out .placeholder { color: var(--faint); font-style: italic; }

.dist {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-family: var(--mono);
  font-size: 11.5px;
  /* 8 rows of 14px plus 7 gaps of 4px — reserved up front so the panel is the
     same height empty as it is full. */
  height: 140px;
}

.bar-row {
  display: grid;
  grid-template-columns: 10ch 1fr 5.5ch;
  gap: 8px;
  align-items: center;
  min-height: 14px;
}

.bar-row .tok {
  color: var(--ink-2);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.bar-track {
  background: var(--panel-2);
  border: 1px solid var(--line);
  height: 14px;
  overflow: hidden;
}

.bar-fill {
  height: 100%;
  background: var(--line-2);
}

.bar-row.chosen .tok { color: var(--acc-ink); font-weight: 700; }
.bar-row.chosen .bar-fill { background: var(--acc); }
.bar-row .pct { color: var(--faint); text-align: right; }

/* ── training curve ───────────────────────────────────────────────────────── */

#curve {
  display: block;
  width: 100%;
  height: 300px;
  background: var(--panel-2);
  border: 1px solid var(--line);
  cursor: crosshair;
}

.curve-legend {
  display: flex;
  flex-wrap: wrap;
  gap: 6px 16px;
  margin-top: 9px;
  font-size: 10.5px;
  color: var(--dim);
}

.curve-legend b { color: var(--ink); font-weight: 700; font-family: var(--mono); }
.curve-legend .hover {
  margin-left: auto;
  font-family: var(--mono);
  color: var(--acc-ink);
  font-weight: 650;
}

.swatch.train { background: #c9bffa; }
.swatch.val   { background: var(--acc); }
.swatch.uni   { background: var(--bad); }
.swatch.bi    { background: var(--good); }
.swatch.tri   { background: var(--warn); }

/* ── console ──────────────────────────────────────────────────────────────── */

.log {
  font-family: var(--mono);
  font-size: 10.5px;
  line-height: 1.6;
  color: var(--dim);
  white-space: pre-wrap;
  height: 84px;
  overflow-y: auto;
}

.log .err { color: var(--bad); font-weight: 600; }
.log .ok  { color: var(--good); }

footer {
  max-width: var(--max);
  margin: 0 auto;
  padding: 12px 18px 28px;
  font-family: var(--mono);
  font-size: 10.5px;
  color: var(--faint);
}

/* ── experiment note + spec table ─────────────────────────────────────────── */

.note {
  margin: 9px 0 0;
  font-size: 11.5px;
  line-height: 1.45;
  color: var(--ink-2);
  min-height: 3.2em;
  border-left: 2px solid var(--acc);
  padding-left: 9px;
}

/* renderSpecs() emits `<div>key<br><b>value</b></div>`; the <br> is suppressed so
   each pair reads as one row of a table without the JS having to know about it. */
.specs { font-family: var(--mono); font-size: 10.5px; }
.specs br { display: none; }

.specs > div {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 10px;
  padding: 4px 0;
  border-bottom: 1px dotted var(--line-2);
  color: var(--dim);
}

.specs > div:last-child { border-bottom: 0; }
.specs b {
  color: var(--ink);
  font-weight: 700;
  text-align: right;
  overflow-wrap: anywhere;
}

.specs .good { color: var(--good); }
.specs .bad  { color: var(--bad); }
.specs .warn { color: var(--warn); }

/* ── scrollbars: thin, square, out of the way ─────────────────────────────── */

.text-out::-webkit-scrollbar, .log::-webkit-scrollbar { width: 9px; }
.text-out::-webkit-scrollbar-thumb, .log::-webkit-scrollbar-thumb {
  background: var(--line-2);
  border: 3px solid var(--panel);
}
.text-out::-webkit-scrollbar-track, .log::-webkit-scrollbar-track { background: transparent; }
