/* ============================================================
   cl-spikeviz — Cortical Labs instrument console
   "ink on lab paper" — neutral lab paper, warm ink, vermilion accent
   ============================================================ */

:root {
  /* ---- paper theme (default) ---- */
  --bg:            #e7e8e1;
  --bg-grain:      rgba(23, 24, 20, 0.02);
  --panel:         #f4f4ee;
  --panel-2:       #ecede6;
  --inset:         #e5e6de;   /* canvas plotting bg */
  --rail:          #efefea;

  --ink:           #1b1710;
  --ink-2:         #4a4338;
  --muted:         #837868;
  --faint:         #a99d89;

  --line:          rgba(27, 23, 16, 0.13);
  --line-2:        rgba(27, 23, 16, 0.22);
  --line-canvas:   rgba(27, 23, 16, 0.10);

  --accent:        #cf4524;   /* Cortical vermilion — spikes / live */
  --accent-soft:   rgba(207, 69, 36, 0.12);
  --accent-line:   rgba(207, 69, 36, 0.42);
  --stim:          #2f4a8c;   /* ink blue — stims */
  --stim-soft:     rgba(47, 74, 140, 0.13);
  --good:          #2f7d4f;
  --warn:          #9a6a18;
  --bad:           #b23218;

  /* heatmap ramp (low -> high) */
  --heat-0:  #e5e6de;
  --heat-1:  #d8d2bf;
  --heat-2:  #d89a65;
  --heat-3:  #d45e2f;
  --heat-4:  #9c2c12;

  --radius:    3px;
  --radius-lg: 5px;
  --rail-w:    314px;

  --mono:    "IBM Plex Mono", ui-monospace, "SFMono-Regular", Menlo, monospace;
  --sans:    "IBM Plex Sans", system-ui, -apple-system, sans-serif;
  --display: "Space Grotesk", var(--sans);

  color-scheme: light;
}

body.dark {
  --bg:            #110f0c;
  --bg-grain:      rgba(255, 250, 240, 0.018);
  --panel:         #1a1712;
  --panel-2:       #221d16;
  --inset:         #16130f;
  --rail:          #18150f;

  --ink:           #efe7d8;
  --ink-2:         #c4bba9;
  --muted:         #8d8271;
  --faint:         #5f574a;

  --line:          rgba(239, 231, 216, 0.12);
  --line-2:        rgba(239, 231, 216, 0.20);
  --line-canvas:   rgba(239, 231, 216, 0.09);

  --accent:        #f2643f;
  --accent-soft:   rgba(242, 100, 63, 0.16);
  --accent-line:   rgba(242, 100, 63, 0.5);
  --stim:          #7d9bee;
  --stim-soft:     rgba(125, 155, 238, 0.16);
  --good:          #5bbd81;
  --warn:          #d6a24a;
  --bad:           #ef6a52;

  --heat-0:  #16130f;
  --heat-1:  #4d2e1c;
  --heat-2:  #9a4a22;
  --heat-3:  #db6a36;
  --heat-4:  #ffb27a;

  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  background:
    linear-gradient(180deg, rgba(255,255,255,0.32), transparent 34%),
    var(--bg);
  color: var(--ink);
  font-family: var(--sans);
  font-size: 14px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
  letter-spacing: 0.005em;
}

/* faint paper grain overlay */
body::before {
  content: "";
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 9999;
  opacity: 0.5;
  background-image:
    repeating-linear-gradient(0deg, var(--bg-grain) 0 1px, transparent 1px 3px);
  mix-blend-mode: multiply;
}
body.dark::before { mix-blend-mode: screen; }

button, input, select { font: inherit; color: inherit; }
button { touch-action: manipulation; }

::selection { background: var(--accent-soft); }

/* ---------- shell ---------- */
.app {
  height: 100vh;
  display: grid;
  grid-template-rows: auto 1fr;
  grid-template-columns: 100%;
  overflow: hidden;
}

/* ============================================================
   COMMAND BAR
   ============================================================ */
.cmdbar {
  display: flex;
  align-items: stretch;
  height: 56px;
  border-bottom: 1px solid var(--line-2);
  background: var(--panel);
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 0 18px;
  border-right: 1px solid var(--line);
  width: var(--rail-w);
  flex: none;
}
.brand .word b   { white-space: nowrap; }
.brand .word span { white-space: nowrap; }

.brand .mark {
  width: 24px; height: 24px;
  flex: none;
  position: relative;
}
.brand .mark svg { display: block; width: 100%; height: 100%; }

.brand .word {
  display: flex;
  flex-direction: column;
  line-height: 1;
}
.brand .word b {
  font-family: var(--display);
  font-weight: 600;
  font-size: 15px;
  letter-spacing: 0.02em;
}
.brand .word span {
  font-family: var(--mono);
  font-size: 9.5px;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--muted);
  margin-top: 3px;
}

.cmdbar .live-cluster {
  display: flex;
  align-items: center;
  gap: 22px;
  padding: 0 20px;
  flex: 1;
  min-width: 0;
  overflow: hidden;
}

.statline {
  display: flex;
  align-items: center;
  gap: 9px;
  font-family: var(--mono);
  font-size: 12px;
  letter-spacing: 0.02em;
  white-space: nowrap;
}
.statline .dot {
  width: 8px; height: 8px; border-radius: 50%;
  background: var(--faint);
  flex: none;
  position: relative;
}
.statline .dot.live    { background: var(--accent); }
.statline .dot.live::after {
  content: ""; position: absolute; inset: -4px;
  border-radius: 50%;
  border: 1px solid var(--accent);
  opacity: 0;
  animation: ping 1.8s ease-out infinite;
}
.statline .dot.waiting { background: var(--warn); }
.statline .dot.error   { background: var(--bad); }
@keyframes ping {
  0%        { transform: scale(0.7); opacity: 0.7; }
  80%, 100% { transform: scale(2.1); opacity: 0; }
}
.statline .lab { color: var(--muted); }
.statline .val { color: var(--ink); }

.cmdbar .clock {
  font-family: var(--mono);
  font-size: 12px;
  color: var(--muted);
  letter-spacing: 0.04em;
}
.cmdbar .clock b { color: var(--ink); font-weight: 500; }

.viewtabs {
  display: flex;
  align-items: center;
  padding: 0 12px;
  gap: 4px;
  border-left: 1px solid var(--line);
  flex: none;
}
.viewtabs button {
  height: 32px;
  padding: 0 13px;
  border: 1px solid transparent;
  border-radius: var(--radius);
  background: transparent;
  color: var(--muted);
  font-family: var(--mono);
  font-size: 11.5px;
  letter-spacing: 0.03em;
  cursor: pointer;
  transition: color .12s, background .12s, border-color .12s;
  white-space: nowrap;
}
.viewtabs button:hover { color: var(--ink); }
.viewtabs button[aria-pressed="true"],
.viewtabs button.is-active {
  background: var(--ink);
  color: var(--panel);
  border-color: var(--ink);
}

/* ============================================================
   BODY: rail + stage
   ============================================================ */
.body {
  display: grid;
  grid-template-columns: var(--rail-w) 1fr;
  min-height: 0;
  overflow: hidden;
}

/* ---------- control rail ---------- */
.rail {
  border-right: 1px solid var(--line-2);
  background: var(--rail);
  overflow-y: auto;
  overflow-x: hidden;
  display: flex;
  flex-direction: column;
}
.rail::-webkit-scrollbar { width: 9px; }
.rail::-webkit-scrollbar-thumb {
  background: var(--line-2); border-radius: 9px;
  border: 3px solid transparent; background-clip: padding-box;
}

.section { border-bottom: 1px solid var(--line); padding: 16px 18px 18px; }
.section:last-child { border-bottom: 0; flex: 1; }

.section > h3 {
  margin: 0 0 14px;
  font-family: var(--mono);
  font-size: 10px;
  font-weight: 500;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: var(--muted);
  display: flex;
  align-items: center;
  gap: 8px;
}
.section > h3::before {
  content: "";
  width: 5px; height: 5px;
  background: var(--accent);
  flex: none;
}

.field { margin-bottom: 12px; }
.field:last-child { margin-bottom: 0; }
.field > label,
.field > .lbl {
  display: block;
  font-family: var(--mono);
  font-size: 10px;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--muted);
  margin-bottom: 6px;
}
.field .lbl-row {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  margin-bottom: 6px;
}
.field .lbl-row .val {
  font-family: var(--mono);
  font-size: 11px;
  color: var(--accent);
}

.row2      { display: grid; grid-template-columns: 1fr 84px; gap: 8px; }
.row-split { display: grid; grid-template-columns: 1fr 1fr;  gap: 8px; }

input[type="text"], input[type="number"], select, .inp {
  width: 100%;
  height: 34px;
  border: 1px solid var(--line-2);
  border-radius: var(--radius);
  background: var(--panel);
  color: var(--ink);
  padding: 0 10px;
  font-family: var(--mono);
  font-size: 12.5px;
  outline: none;
  transition: border-color .12s, box-shadow .12s;
}
input::placeholder { color: var(--faint); }
input:focus, select:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}
select {
  appearance: none;
  background-image:
    linear-gradient(45deg, transparent 50%, var(--muted) 50%),
    linear-gradient(135deg, var(--muted) 50%, transparent 50%);
  background-position: calc(100% - 16px) 14px, calc(100% - 11px) 14px;
  background-size: 5px 5px, 5px 5px;
  background-repeat: no-repeat;
  cursor: pointer;
}

input[type="range"] {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  height: 4px;
  border-radius: 2px;
  background: var(--line-2);
  outline: none;
  margin: 9px 0 4px;
  cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 14px; height: 14px;
  border-radius: 50%;
  background: var(--accent);
  border: 2px solid var(--panel);
  box-shadow: 0 0 0 1px var(--accent);
  cursor: grab;
}
input[type="range"]::-moz-range-thumb {
  width: 14px; height: 14px; border-radius: 50%;
  background: var(--accent); border: 2px solid var(--panel);
  box-shadow: 0 0 0 1px var(--accent); cursor: grab;
}

/* buttons */
.btn {
  height: 34px;
  border: 1px solid var(--line-2);
  border-radius: var(--radius);
  background: var(--panel);
  color: var(--ink);
  font-family: var(--mono);
  font-size: 11.5px;
  letter-spacing: 0.04em;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  padding: 0 12px;
  white-space: nowrap;
  transition: border-color .12s, background .12s, color .12s, transform .04s;
}
.btn:hover  { border-color: var(--line-2); background: var(--panel-2); }
.btn:active { transform: translateY(1px); }
.btn .ic    { width: 13px; height: 13px; flex: none; }
button:focus-visible,
.btn:focus-visible,
.chips button:focus-visible,
.viewtabs button:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-primary:hover { filter: brightness(1.06); }

.btn[aria-pressed="true"],
.btn.is-active {
  background: var(--ink);
  border-color: var(--ink);
  color: var(--panel);
}

.btn-row   { display: grid; grid-template-columns: 1fr 1fr;     gap: 8px; }
.btn-row-3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 8px; }
.btn-full  { width: 100%; }
.file-input { position: absolute; width: 1px; height: 1px; opacity: 0; pointer-events: none; }

.btn-ghost { background: transparent; border-color: var(--line); color: var(--muted); }
.btn-ghost:hover { color: var(--ink); border-color: var(--line-2); background: transparent; }

/* chips */
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chips button {
  height: 27px;
  padding: 0 10px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: transparent;
  color: var(--muted);
  font-family: var(--mono);
  font-size: 10.5px;
  letter-spacing: 0.03em;
  cursor: pointer;
  transition: color .12s, background .12s, border-color .12s;
}
.chips button:hover { color: var(--ink); border-color: var(--line-2); }

/* diagnostics */
.diag { display: flex; flex-direction: column; gap: 8px; }
.endpoint {
  display: flex;
  align-items: center;
  gap: 9px;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--panel);
}
.endpoint .ep-dot { width: 7px; height: 7px; border-radius: 50%; background: var(--faint); flex: none; }
.endpoint.live            .ep-dot { background: var(--good); }
.endpoint.waiting         .ep-dot { background: var(--warn); }
.endpoint.ended           .ep-dot { background: var(--faint); }
.endpoint.error           .ep-dot,
.endpoint.protocol-error  .ep-dot { background: var(--bad); }
.endpoint .ep-name  { font-family: var(--mono); font-size: 11px; letter-spacing: 0.02em; flex: 1; }
.endpoint .ep-state { font-family: var(--mono); font-size: 10px; letter-spacing: 0.04em; text-transform: uppercase; color: var(--muted); }
.endpoint.live .ep-state  { color: var(--good); }

.metrics {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1px;
  background: var(--line);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  overflow: hidden;
  margin-top: 2px;
}
.metrics .m { background: var(--panel); padding: 8px 10px; }
.metrics .m .k {
  font-family: var(--mono);
  font-size: 9px; letter-spacing: 0.1em; text-transform: uppercase; color: var(--muted);
}
.metrics .m .v { font-family: var(--mono); font-size: 14px; color: var(--ink); margin-top: 2px; }
.metrics .m .v.accent { color: var(--accent); }

.hint { margin: 12px 0 0; font-size: 11.5px; line-height: 1.5; color: var(--muted); }
.hint.error { color: var(--bad); }
.hint code {
  font-family: var(--mono); font-size: 11px;
  color: var(--ink); background: var(--panel-2); padding: 1px 5px; border-radius: 3px;
}

.protocol-tag {
  font-family: var(--mono); font-size: 10px; letter-spacing: 0.04em; color: var(--muted);
  display: flex; align-items: center; gap: 7px;
  margin-top: 12px; padding-top: 12px; border-top: 1px dashed var(--line-2);
}
.protocol-tag b { color: var(--ink); font-weight: 500; }

/* ============================================================
   STAGE (viz)
   ============================================================ */
.stage {
  min-width: 0; min-height: 0;
  padding: 14px;
  display: grid;
  gap: 14px;
  grid-template-columns: minmax(0, 1fr) minmax(280px, 30%);
  grid-template-rows: minmax(0, 1.32fr) minmax(0, 1fr);
  overflow: hidden;
}

.panel {
  min-width: 0; min-height: 0;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  background: var(--panel);
  border: 1px solid var(--line-2);
  border-radius: var(--radius-lg);
  overflow: hidden;
}

.p-raster  { grid-column: 1; grid-row: 1; }
.p-wave    { grid-column: 1; grid-row: 2; }
.p-heatmap { grid-column: 2; grid-row: 1 / span 2; }
.p-grid    { display: none; }
.p-iso     { display: none; }

.phead {
  display: flex; align-items: center; justify-content: space-between;
  gap: 12px; padding: 11px 15px;
  border-bottom: 1px solid var(--line);
  background: var(--panel-2);
}
.phead .ttl { display: flex; align-items: baseline; gap: 10px; min-width: 0; flex: 1 1 auto; }
.phead h2 {
  margin: 0; font-family: var(--mono); font-size: 11px; font-weight: 500;
  letter-spacing: 0.16em; text-transform: uppercase; color: var(--ink); white-space: nowrap;
}
.phead .sub {
  font-family: var(--mono); font-size: 11px; color: var(--muted); letter-spacing: 0.01em;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; min-width: 0;
}
.phead .metric {
  font-family: var(--mono); font-size: 11px; color: var(--accent);
  white-space: nowrap; flex: none; letter-spacing: 0.02em;
}

.canvas-wrap { position: relative; min-width: 0; min-height: 0; background: var(--inset); }
canvas.viz   { display: block; width: 100%; height: 100%; }

.legend {
  position: absolute; left: 14px; bottom: 11px;
  display: flex; gap: 14px;
  font-family: var(--mono); font-size: 10px; letter-spacing: 0.04em;
  color: var(--muted); pointer-events: none;
}
.legend i { display: inline-flex; align-items: center; gap: 6px; }
.legend i::before { content: ""; width: 8px; height: 8px; border-radius: 1px; }
.legend .lg-spk::before { background: var(--accent); }
.legend .lg-stm::before { background: var(--stim); }
.grid-legend span {
  display: inline-flex;
  align-items: center;
  color: var(--muted);
}

.heat-scale {
  position: absolute; right: 13px; top: 13px;
  display: flex; align-items: center; gap: 8px;
  font-family: var(--mono); font-size: 9.5px; letter-spacing: 0.06em;
  color: var(--muted); pointer-events: none;
}
.heat-scale .ramp {
  width: 84px; height: 7px; border-radius: 2px;
  background: linear-gradient(90deg, var(--heat-0), var(--heat-1), var(--heat-2), var(--heat-3), var(--heat-4));
  border: 1px solid var(--line);
}

/* view: 3D */
body.view-3d .p-raster,
body.view-3d .p-wave,
body.view-3d .p-heatmap,
body.view-3d .p-grid { display: none; }
body.view-3d .stage { grid-template-columns: 1fr; grid-template-rows: 1fr; }
body.view-3d .p-iso { display: grid; grid-column: 1; grid-row: 1; }

/* view: electrode grid */
body.view-grid .p-raster,
body.view-grid .p-wave,
body.view-grid .p-heatmap,
body.view-grid .p-iso { display: none; }
body.view-grid .stage { grid-template-columns: 1fr; grid-template-rows: 1fr; }
body.view-grid .p-grid { display: grid; grid-column: 1; grid-row: 1; }

/* view: split */
body.view-split .stage { grid-template-columns: minmax(0, 1fr) minmax(340px, 42%); }
body.view-split .p-heatmap,
body.view-split .p-grid { display: none; }
body.view-split .p-iso { display: grid; grid-column: 2; grid-row: 1 / span 2; }

.sel-banner {
  position: absolute; right: 13px; bottom: 11px;
  font-family: var(--mono); font-size: 10px; color: var(--muted);
  letter-spacing: 0.03em; pointer-events: none;
}
.sel-banner b { color: var(--accent); }

/* toast */
.toast {
  position: fixed; bottom: 18px; left: 50%;
  transform: translateX(-50%) translateY(8px);
  background: var(--ink); color: var(--panel);
  font-family: var(--mono); font-size: 12px; letter-spacing: 0.03em;
  padding: 9px 16px; border-radius: var(--radius);
  opacity: 0; pointer-events: none;
  transition: opacity .18s, transform .18s;
  z-index: 100;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

.drop-zone {
  position: fixed;
  inset: 12px;
  z-index: 90;
  display: grid;
  place-items: center;
  border: 1px dashed var(--accent);
  border-radius: var(--radius-lg);
  background: var(--panel);
  color: var(--ink);
  opacity: 0;
  pointer-events: none;
  transition: opacity .12s;
}
.drop-zone div {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  padding: 16px 20px;
  border: 1px solid var(--line-2);
  border-radius: var(--radius-lg);
  background: var(--panel);
  font-family: var(--mono);
  box-shadow: 0 10px 40px rgba(0,0,0,0.16);
}
.drop-zone b { font-size: 12px; letter-spacing: 0.12em; text-transform: uppercase; }
.drop-zone span { font-size: 11px; color: var(--muted); }
body.is-dragging-file .drop-zone { opacity: 1; pointer-events: auto; }

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}

kbd {
  font-family: var(--mono); font-size: 10px;
  border: 1px solid var(--line-2); border-bottom-width: 2px;
  border-radius: 3px; padding: 1px 5px; color: var(--muted); background: var(--panel);
}
.kbd-help {
  display: flex; flex-wrap: wrap; gap: 6px 12px;
  font-family: var(--mono); font-size: 10.5px; color: var(--muted); margin-top: 4px;
}
.kbd-help span { display: inline-flex; align-items: center; gap: 5px; }

/* responsive */
@media (max-width: 1280px) {
  .cmdbar .clock { display: none; }
  .cmdbar .live-cluster { gap: 16px; }
}
@media (max-width: 1120px) {
  .live-cluster .statline:nth-child(3) { display: none; }
}
@media (max-width: 1080px) {
  .body { grid-template-columns: 1fr; grid-template-rows: auto 1fr; }
  .rail { border-right: 0; border-bottom: 1px solid var(--line-2); max-height: 42vh; }
  .brand { width: auto; min-width: 0; }
}
@media (max-width: 760px) {
  .cmdbar {
    height: auto;
    flex-wrap: wrap;
  }
  .brand {
    min-height: 56px;
    flex: 1 1 auto;
  }
  .cmdbar .live-cluster {
    display: none;
  }
  .viewtabs {
    order: 2;
    width: 100%;
    border-left: 0;
    border-top: 1px solid var(--line);
    overflow-x: auto;
    padding: 6px 12px;
  }
  .viewtabs button {
    flex: 1 0 auto;
  }
  .stage { grid-template-columns: 1fr; grid-template-rows: repeat(3, minmax(220px, 1fr)); }
  .p-raster  { grid-column: 1; grid-row: 1; }
  .p-wave    { grid-column: 1; grid-row: 3; }
  .p-heatmap { grid-column: 1; grid-row: 2; }
  .cmdbar .clock { display: none; }
}
