/* ==================================================================
   EJ Circuit — design system + application layout
   Two genuinely distinct layouts: desktop multi-panel, mobile sheets.
   ================================================================== */

:root {
  /* surfaces */
  --bg: #0b0e13;
  --bg-2: #10151c;
  --panel: #141a22;
  --panel-2: #1a222c;
  --panel-3: #212b37;
  --line: #253040;
  --line-soft: #1c2531;

  /* text */
  --tx: #e8eef6;
  --tx-2: #a9b7c8;
  --tx-3: #6f7f92;

  /* brand */
  --acc: #38bdf8;
  --acc-2: #0ea5e9;
  --acc-dim: rgba(56, 189, 248, .14);
  --vio: #a78bfa;
  --grn: #34d399;
  --amb: #fbbf24;
  --red: #f87171;
  --pink: #f472b6;

  /* wire / node colours (shared with canvas renderer) */
  --w-wire: #7dd3fc;
  --w-live: #fbbf24;

  --r-s: 6px;
  --r-m: 10px;
  --r-l: 14px;
  --sh-1: 0 1px 2px rgba(0, 0, 0, .4);
  --sh-2: 0 8px 24px rgba(0, 0, 0, .45);
  --sh-3: 0 18px 48px rgba(0, 0, 0, .55);

  --topbar-h: 52px;
  --status-h: 26px;
  --pal-w: 236px;
  --insp-w: 272px;
  --dock-h: 268px;

  --ff: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --fm: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace;

  --ease: cubic-bezier(.22, .61, .36, 1);
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--tx);
  font-family: var(--ff);
  font-size: 13px;
  line-height: 1.45;
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
  touch-action: none;
}
button, input, select, textarea { font: inherit; color: inherit; }
::selection { background: rgba(56, 189, 248, .3); }

/* scrollbars */
*::-webkit-scrollbar { width: 10px; height: 10px; }
*::-webkit-scrollbar-track { background: transparent; }
*::-webkit-scrollbar-thumb { background: #2a3644; border-radius: 8px; border: 3px solid transparent; background-clip: content-box; }
*::-webkit-scrollbar-thumb:hover { background: #3a4a5e; background-clip: content-box; border: 3px solid transparent; }

/* ---------------------------------------------------------------- */
/* App frame                                                         */
/* ---------------------------------------------------------------- */
#app {
  position: fixed;
  inset: 0;
  display: grid;
  grid-template-rows: var(--topbar-h) 1fr var(--status-h);
  grid-template-columns: var(--pal-w) 1fr var(--insp-w);
  grid-template-areas:
    "top  top  top"
    "pal  main insp"
    "stat stat stat";
  background: var(--bg);
}

/* ---------------------------------------------------------------- */
/* Top bar                                                           */
/* ---------------------------------------------------------------- */
.topbar {
  grid-area: top;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 0 10px 0 12px;
  background: linear-gradient(180deg, #161d26, #111821);
  border-bottom: 1px solid var(--line);
  z-index: 40;
}
.brand { display: flex; align-items: center; gap: 9px; min-width: 0; }
.brand .mark {
  width: 26px; height: 26px; border-radius: 7px; flex: none;
  background: radial-gradient(120% 120% at 30% 20%, #4cc9ff, #1d6fd1 60%, #123a72);
  display: grid; place-items: center;
  box-shadow: 0 0 0 1px rgba(120, 200, 255, .35), 0 4px 14px rgba(20, 120, 220, .35);
}
.brand .mark svg { width: 16px; height: 16px; }
.brand .name { font-weight: 700; letter-spacing: .2px; font-size: 13.5px; white-space: nowrap; }
.brand .name em { font-style: normal; color: var(--acc); }
.brand .sub { color: var(--tx-3); font-size: 10.5px; white-space: nowrap; }

.proj-name {
  background: transparent; border: 1px solid transparent; border-radius: var(--r-s);
  padding: 5px 8px; min-width: 90px; width: 168px; color: var(--tx);
  transition: border-color .15s, background .15s;
}
.proj-name:hover { border-color: var(--line); }
.proj-name:focus { outline: none; border-color: var(--acc-2); background: #0d141c; }

.tb-sep { width: 1px; height: 24px; background: var(--line); flex: none; }
.tb-group { display: flex; align-items: center; gap: 4px; }
.tb-spacer { flex: 1 1 auto; }

/* buttons */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  height: 32px; padding: 0 11px; border-radius: var(--r-s);
  background: var(--panel-2); border: 1px solid var(--line);
  color: var(--tx-2); cursor: pointer; white-space: nowrap;
  transition: background .14s var(--ease), color .14s, border-color .14s, transform .08s;
  user-select: none;
}
.btn:hover { background: var(--panel-3); color: var(--tx); border-color: #33415280; }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .38; cursor: not-allowed; transform: none; }
.btn svg { width: 15px; height: 15px; flex: none; }
.btn.icon { width: 32px; padding: 0; }
.btn.sm { height: 26px; padding: 0 8px; font-size: 12px; }
.btn.sm svg { width: 13px; height: 13px; }
.btn.pri { background: linear-gradient(180deg, #1a9ede, #0b7cb8); border-color: #2ab0ef80; color: #04121c; font-weight: 650; }
.btn.pri:hover { background: linear-gradient(180deg, #2cb0ee, #0e8ac9); color: #04121c; }
.btn.run { background: linear-gradient(180deg, #2fd39a, #14a97a); border-color: #3ee6b080; color: #04140e; font-weight: 700; }
.btn.run:hover { filter: brightness(1.08); color: #04140e; }
.btn.stop { background: linear-gradient(180deg, #f2766f, #cf4b45); border-color: #ff9d9780; color: #1a0605; font-weight: 700; }
.btn.ghost { background: transparent; border-color: transparent; }
.btn.ghost:hover { background: var(--panel-2); border-color: var(--line); }
.btn.on { background: var(--acc-dim); border-color: #38bdf866; color: var(--acc); }
.btn.danger:hover { background: rgba(248, 113, 113, .16); border-color: #f8717166; color: #fca5a5; }

select.sel {
  height: 32px; border-radius: var(--r-s); background: var(--panel-2);
  border: 1px solid var(--line); color: var(--tx-2); padding: 0 8px; cursor: pointer;
}
select.sel:hover { background: var(--panel-3); color: var(--tx); }
select.sel:focus { outline: none; border-color: var(--acc-2); }

/* tooltip */
[data-tip] { position: relative; }
[data-tip]:hover::after {
  content: attr(data-tip);
  position: absolute; top: calc(100% + 7px); left: 50%; transform: translateX(-50%);
  background: #05080c; color: var(--tx); border: 1px solid var(--line);
  padding: 4px 8px; border-radius: var(--r-s); font-size: 11px; white-space: nowrap;
  z-index: 900; pointer-events: none; box-shadow: var(--sh-2);
  animation: tipIn .12s var(--ease);
}
@keyframes tipIn { from { opacity: 0; transform: translateX(-50%) translateY(-3px); } }

/* ---------------------------------------------------------------- */
/* Palette (left)                                                    */
/* ---------------------------------------------------------------- */
.palette {
  grid-area: pal;
  background: var(--panel);
  border-right: 1px solid var(--line);
  display: flex; flex-direction: column; min-height: 0;
  z-index: 30;
}
.pal-head { padding: 9px 10px 7px; border-bottom: 1px solid var(--line-soft); }
.pal-title { font-size: 10.5px; text-transform: uppercase; letter-spacing: .09em; color: var(--tx-3); font-weight: 700; }
.search {
  margin-top: 7px; position: relative; display: flex; align-items: center;
}
.search svg { position: absolute; left: 8px; width: 14px; height: 14px; color: var(--tx-3); pointer-events: none; }
.search input {
  width: 100%; height: 30px; padding: 0 26px 0 27px; border-radius: var(--r-s);
  background: var(--bg-2); border: 1px solid var(--line); color: var(--tx);
}
.search input::placeholder { color: var(--tx-3); }
.search input:focus { outline: none; border-color: var(--acc-2); background: #0c1219; }
.search .clr { position: absolute; right: 5px; width: 20px; height: 20px; display: none; place-items: center; cursor: pointer; color: var(--tx-3); border-radius: 4px; }
.search .clr:hover { background: var(--panel-3); color: var(--tx); }
.search.has .clr { display: grid; }

.pal-list { overflow-y: auto; overflow-x: hidden; flex: 1; padding: 4px 6px 14px; }
.cat { margin-top: 4px; }
.cat-head {
  display: flex; align-items: center; gap: 6px; padding: 6px 6px;
  cursor: pointer; border-radius: var(--r-s); user-select: none;
  font-size: 11px; font-weight: 700; letter-spacing: .05em; text-transform: uppercase; color: var(--tx-2);
}
.cat-head:hover { background: var(--panel-2); color: var(--tx); }
.cat-head .chev { width: 12px; height: 12px; transition: transform .18s var(--ease); color: var(--tx-3); }
.cat.closed .chev { transform: rotate(-90deg); }
.cat-head .cnt { margin-left: auto; font-size: 10px; color: var(--tx-3); font-weight: 600; }
.cat-body { display: grid; grid-template-columns: 1fr 1fr; gap: 5px; padding: 2px 2px 6px; }
.cat.closed .cat-body { display: none; }

.part {
  position: relative;
  display: flex; flex-direction: column; align-items: center; gap: 3px;
  padding: 7px 3px 5px; border-radius: var(--r-m);
  background: var(--panel-2); border: 1px solid var(--line-soft);
  cursor: grab; user-select: none; overflow: hidden;
  transition: transform .1s var(--ease), border-color .14s, background .14s, box-shadow .14s;
}
.part:hover { background: var(--panel-3); border-color: #3a4a5e; transform: translateY(-1px); box-shadow: var(--sh-1); }
.part:active { cursor: grabbing; transform: scale(.97); }
.part canvas { width: 52px; height: 34px; display: block; pointer-events: none; }
.part .lbl {
  font-size: 9.5px; color: var(--tx-2); text-align: center; line-height: 1.2;
  max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.part:hover .lbl { color: var(--tx); }
.part .pin { position: absolute; top: 3px; right: 4px; width: 5px; height: 5px; border-radius: 50%; background: var(--acc); opacity: .55; }
.part.dragging { opacity: .35; }
.part.hidden { display: none; }
.pal-empty { padding: 22px 12px; text-align: center; color: var(--tx-3); font-size: 12px; }

/* ---------------------------------------------------------------- */
/* Stage (canvas + floating tools)                                   */
/* ---------------------------------------------------------------- */
.stage { grid-area: main; position: relative; min-width: 0; min-height: 0; background: var(--bg); overflow: hidden; }
#cv { position: absolute; inset: 0; width: 100%; height: 100%; display: block; touch-action: none; cursor: default; }
#cv.wiring { cursor: crosshair; }
#cv.panning { cursor: grabbing; }
#cv.moving { cursor: grabbing; }

.stage-tools {
  position: absolute; left: 10px; top: 10px; display: flex; gap: 4px;
  background: rgba(16, 21, 28, .82); backdrop-filter: blur(9px);
  border: 1px solid var(--line); border-radius: var(--r-m); padding: 4px;
  box-shadow: var(--sh-2); z-index: 12;
}
.stage-tools .btn { height: 30px; width: 30px; padding: 0; border-color: transparent; background: transparent; }
.stage-tools .btn:hover { background: var(--panel-3); }
.stage-tools .btn.on { background: var(--acc-dim); border-color: #38bdf855; }
.stage-tools .div { width: 1px; background: var(--line); margin: 3px 2px; }

.zoomer {
  position: absolute; right: 10px; bottom: 10px; display: flex; align-items: center; gap: 2px;
  background: rgba(16, 21, 28, .82); backdrop-filter: blur(9px);
  border: 1px solid var(--line); border-radius: var(--r-m); padding: 3px;
  box-shadow: var(--sh-2); z-index: 12;
}
.zoomer .btn { height: 28px; width: 28px; padding: 0; background: transparent; border-color: transparent; }
.zoomer .btn:hover { background: var(--panel-3); }
.zoomer .val {
  min-width: 52px; text-align: center; font-family: var(--fm); font-size: 11.5px;
  color: var(--tx-2); cursor: pointer; padding: 4px 2px; border-radius: 5px;
}
.zoomer .val:hover { background: var(--panel-3); color: var(--tx); }

.simbar {
  position: absolute; left: 50%; top: 10px; transform: translateX(-50%);
  display: flex; align-items: center; gap: 8px;
  background: rgba(16, 21, 28, .88); backdrop-filter: blur(10px);
  border: 1px solid var(--line); border-radius: 999px; padding: 4px 6px 4px 5px;
  box-shadow: var(--sh-2); z-index: 14; max-width: calc(100% - 24px);
}
.simbar .btn { height: 30px; border-radius: 999px; }
.simbar .btn.run, .simbar .btn.stop { padding: 0 14px; }
.simbar .t {
  font-family: var(--fm); font-size: 11.5px; color: var(--tx-2);
  padding: 0 6px; white-space: nowrap; min-width: 62px; text-align: center;
}
.simbar .led {
  width: 8px; height: 8px; border-radius: 50%; background: #3a4757; flex: none;
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, .4);
  transition: background .2s, box-shadow .2s;
}
.simbar .led.live { background: var(--grn); box-shadow: 0 0 9px #34d39988; }
.simbar .led.paused { background: var(--amb); box-shadow: 0 0 9px #fbbf2488; }
.simbar .spd { height: 28px; }

.hintline {
  position: absolute; left: 50%; bottom: 12px; transform: translateX(-50%);
  background: rgba(10, 14, 19, .9); border: 1px solid var(--line);
  padding: 6px 12px; border-radius: 999px; font-size: 11.5px; color: var(--tx-2);
  box-shadow: var(--sh-2); z-index: 15; pointer-events: none; white-space: nowrap;
  animation: hintIn .18s var(--ease);
}
@keyframes hintIn { from { opacity: 0; transform: translateX(-50%) translateY(6px); } }
.hintline kbd {
  font-family: var(--fm); font-size: 10.5px; background: var(--panel-3);
  border: 1px solid var(--line); border-bottom-width: 2px; border-radius: 4px; padding: 0 4px; color: var(--tx);
}

/* context menu */
.ctx {
  position: fixed; z-index: 500; min-width: 186px; padding: 5px;
  background: rgba(20, 26, 34, .97); backdrop-filter: blur(14px);
  border: 1px solid var(--line); border-radius: var(--r-m); box-shadow: var(--sh-3);
  animation: ctxIn .12s var(--ease);
}
@keyframes ctxIn { from { opacity: 0; transform: scale(.97) translateY(-4px); } }
.ctx button {
  display: flex; align-items: center; gap: 9px; width: 100%; padding: 7px 9px;
  background: transparent; border: 0; border-radius: var(--r-s); cursor: pointer;
  color: var(--tx-2); text-align: left; font-size: 12.5px;
}
.ctx button:hover:not(:disabled) { background: var(--acc-dim); color: var(--tx); }
.ctx button:disabled { opacity: .35; cursor: default; }
.ctx button svg { width: 14px; height: 14px; flex: none; opacity: .85; }
.ctx button .k { margin-left: auto; font-family: var(--fm); font-size: 10.5px; color: var(--tx-3); }
.ctx .sep { height: 1px; background: var(--line); margin: 4px 6px; }
.ctx .head { padding: 5px 9px 4px; font-size: 10px; text-transform: uppercase; letter-spacing: .08em; color: var(--tx-3); font-weight: 700; }
.ctx button.danger:hover { background: rgba(248, 113, 113, .16); color: #fca5a5; }

/* ---------------------------------------------------------------- */
/* Inspector (right)                                                 */
/* ---------------------------------------------------------------- */
.inspector {
  grid-area: insp; background: var(--panel); border-left: 1px solid var(--line);
  display: flex; flex-direction: column; min-height: 0; z-index: 30;
}
.insp-tabs { display: flex; border-bottom: 1px solid var(--line-soft); flex: none; }
.insp-tabs button {
  flex: 1; height: 34px; background: transparent; border: 0; border-bottom: 2px solid transparent;
  color: var(--tx-3); cursor: pointer; font-size: 11.5px; font-weight: 650; letter-spacing: .02em;
  transition: color .14s, border-color .14s, background .14s;
}
.insp-tabs button:hover { color: var(--tx-2); background: var(--panel-2); }
.insp-tabs button.on { color: var(--acc); border-bottom-color: var(--acc); }
.insp-body { overflow-y: auto; flex: 1; min-height: 0; }
.insp-pane { display: none; padding: 10px; }
.insp-pane.on { display: block; animation: fadeIn .16s var(--ease); }
@keyframes fadeIn { from { opacity: 0; transform: translateY(3px); } }

.sect { margin-bottom: 14px; }
.sect > h4 {
  margin: 0 0 7px; font-size: 10px; text-transform: uppercase; letter-spacing: .09em;
  color: var(--tx-3); font-weight: 700; display: flex; align-items: center; gap: 6px;
}
.sect > h4 .badge {
  margin-left: auto; text-transform: none; letter-spacing: 0; font-size: 10px;
  background: var(--panel-3); padding: 1px 6px; border-radius: 999px; color: var(--tx-2); font-weight: 600;
}

.comp-head { display: flex; align-items: center; gap: 9px; margin-bottom: 11px; }
.comp-head .ic {
  width: 40px; height: 30px; border-radius: var(--r-s); background: var(--bg-2);
  border: 1px solid var(--line); flex: none; display: block;
}
.comp-head .tt { min-width: 0; }
.comp-head .tt b { display: block; font-size: 13px; font-weight: 650; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.comp-head .tt span { font-size: 10.5px; color: var(--tx-3); font-family: var(--fm); }

.field { margin-bottom: 9px; }
.field > label { display: block; font-size: 11px; color: var(--tx-2); margin-bottom: 4px; font-weight: 550; }
.field .row { display: flex; gap: 6px; align-items: center; }
.inp {
  width: 100%; height: 30px; padding: 0 8px; border-radius: var(--r-s);
  background: var(--bg-2); border: 1px solid var(--line); color: var(--tx);
  transition: border-color .14s, background .14s;
}
.inp:hover { border-color: #33415280; }
.inp:focus { outline: none; border-color: var(--acc-2); background: #0c1219; box-shadow: 0 0 0 3px rgba(14, 165, 233, .12); }
.inp.mono { font-family: var(--fm); font-size: 12px; }
select.inp { cursor: pointer; padding-right: 4px; }
textarea.inp { height: auto; padding: 7px 8px; resize: vertical; line-height: 1.5; }
.unit { color: var(--tx-3); font-size: 11px; min-width: 16px; font-family: var(--fm); }
.row { display: flex; gap: 6px; align-items: center; }
.cbrow { display: flex; align-items: center; gap: 7px; font-size: 11.5px; color: var(--tx-2); cursor: pointer; padding: 3px 0; }
.cbrow input[type=checkbox] { width: 15px; height: 15px; accent-color: var(--acc); cursor: pointer; flex: none; }
button.btn.on { border-color: var(--acc-2); background: var(--acc-dim); color: var(--acc); }

input[type=range].rng {
  -webkit-appearance: none; appearance: none; width: 100%; height: 22px; background: transparent; cursor: pointer;
}
input[type=range].rng::-webkit-slider-runnable-track { height: 4px; border-radius: 3px; background: var(--panel-3); }
input[type=range].rng::-webkit-slider-thumb {
  -webkit-appearance: none; width: 15px; height: 15px; border-radius: 50%; margin-top: -5.5px;
  background: linear-gradient(180deg, #67d5ff, #1596d6); border: 1px solid #0b6f9f; box-shadow: 0 1px 5px rgba(0, 0, 0, .5);
}
input[type=range].rng::-moz-range-track { height: 4px; border-radius: 3px; background: var(--panel-3); }
input[type=range].rng::-moz-range-thumb {
  width: 14px; height: 14px; border-radius: 50%; border: 1px solid #0b6f9f;
  background: linear-gradient(180deg, #67d5ff, #1596d6);
}

.swatches { display: flex; flex-wrap: wrap; gap: 5px; }
.sw {
  width: 24px; height: 24px; border-radius: 6px; cursor: pointer; position: relative;
  border: 1px solid rgba(255, 255, 255, .14); transition: transform .1s var(--ease);
}
.sw:hover { transform: scale(1.12); }
.sw.on { box-shadow: 0 0 0 2px var(--bg), 0 0 0 3.5px var(--acc); }

.pinlist { display: flex; flex-direction: column; gap: 3px; }
.pinrow {
  display: flex; align-items: center; gap: 7px; padding: 5px 7px;
  background: var(--bg-2); border: 1px solid var(--line-soft); border-radius: var(--r-s);
  font-family: var(--fm); font-size: 11px; cursor: pointer;
}
.pinrow:hover { border-color: #33415280; background: #0e151d; }
.pinrow .dot { width: 7px; height: 7px; border-radius: 50%; flex: none; }
.pinrow .nm { color: var(--tx); font-weight: 600; }
.pinrow .vv { margin-left: auto; color: var(--tx-2); }
.pinrow.linked .vv { color: var(--acc); }

.readout {
  background: linear-gradient(180deg, #0d141b, #0a1016); border: 1px solid var(--line-soft);
  border-radius: var(--r-m); padding: 9px 10px; margin-bottom: 10px;
}
.readout .big { font-family: var(--fm); font-size: 19px; font-weight: 600; letter-spacing: -.02em; color: var(--acc); }
.readout .big.ok { color: var(--grn); }
.readout .big.warn { color: var(--amb); }
.readout .big.bad { color: var(--red); }
.readout .lbl { font-size: 10px; text-transform: uppercase; letter-spacing: .08em; color: var(--tx-3); font-weight: 700; margin-bottom: 2px; }
.kv { display: grid; grid-template-columns: auto 1fr; gap: 3px 10px; font-size: 11.5px; margin-top: 7px; }
.kv dt { color: var(--tx-3); }
.kv dd { margin: 0; font-family: var(--fm); color: var(--tx-2); text-align: right; }

.empty-state { text-align: center; padding: 34px 18px; color: var(--tx-3); }
.empty-state svg { width: 40px; height: 40px; opacity: .35; margin-bottom: 10px; }
.empty-state b { display: block; color: var(--tx-2); font-size: 12.5px; margin-bottom: 4px; font-weight: 600; }
.empty-state p { margin: 0; font-size: 11.5px; line-height: 1.6; }

.errlist { display: flex; flex-direction: column; gap: 5px; }
.erritem {
  display: flex; gap: 7px; padding: 7px 8px; border-radius: var(--r-s); font-size: 11.5px;
  background: rgba(248, 113, 113, .08); border: 1px solid rgba(248, 113, 113, .25); color: #fecaca;
  cursor: pointer;
}
.erritem:hover { background: rgba(248, 113, 113, .15); }
.erritem.warn { background: rgba(251, 191, 36, .08); border-color: rgba(251, 191, 36, .25); color: #fde68a; }
.erritem.warn:hover { background: rgba(251, 191, 36, .15); }
.erritem svg { width: 13px; height: 13px; flex: none; margin-top: 2px; }
.ok-box {
  display: flex; align-items: center; gap: 8px; padding: 10px; border-radius: var(--r-m);
  background: rgba(52, 211, 153, .09); border: 1px solid rgba(52, 211, 153, .25); color: #a7f3d0; font-size: 12px;
}
.ok-box svg { width: 16px; height: 16px; flex: none; }

/* ---------------------------------------------------------------- */
/* Dock (code / serial / stimulus)                                   */
/* ---------------------------------------------------------------- */
.dock {
  position: absolute; left: 0; right: 0; bottom: 0; height: var(--dock-h);
  background: var(--panel); border-top: 1px solid var(--line);
  display: flex; flex-direction: column; z-index: 20;
  transition: transform .26s var(--ease);
  box-shadow: 0 -10px 30px rgba(0, 0, 0, .35);
}
.dock.min { transform: translateY(calc(100% - 34px)); }
.dock-tabs {
  display: flex; align-items: center; gap: 2px; padding: 0 8px; height: 34px;
  border-bottom: 1px solid var(--line-soft); flex: none; cursor: ns-resize;
}
.dock-tabs button {
  height: 26px; padding: 0 11px; background: transparent; border: 0; border-radius: var(--r-s);
  color: var(--tx-3); cursor: pointer; font-size: 11.5px; font-weight: 650;
  display: flex; align-items: center; gap: 6px;
}
.dock-tabs button:hover { color: var(--tx-2); background: var(--panel-2); }
.dock-tabs button.on { color: var(--acc); background: var(--acc-dim); }
.dock-tabs button svg { width: 13px; height: 13px; }
.dock-tabs .badge {
  font-size: 9.5px; background: var(--red); color: #1a0605; border-radius: 999px;
  padding: 0 4px; font-weight: 800; min-width: 14px; text-align: center;
}
.dock-tabs .sp { flex: 1; }
.dock-body { flex: 1; min-height: 0; position: relative; }
.dock-pane { position: absolute; inset: 0; display: none; }
.dock-pane.on { display: flex; }

/* code editor */
.codewrap { flex: 1; display: flex; min-width: 0; position: relative; background: #0a0e14; }
.gutter {
  flex: none; width: 46px; padding: 10px 8px 10px 0; text-align: right;
  font-family: var(--fm); font-size: 12px; line-height: 19px; color: #3d4c5e;
  background: #0a0e14; user-select: none; overflow: hidden; border-right: 1px solid var(--line-soft);
}
.gutter div.cur { color: var(--acc); background: rgba(56, 189, 248, .1); border-radius: 3px; }
.gutter div.err { color: var(--red); }
.codearea { position: relative; flex: 1; min-width: 0; overflow: hidden; }
#codeHl, #codeTa {
  position: absolute; inset: 0; margin: 0; padding: 10px 12px;
  font-family: var(--fm); font-size: 12px; line-height: 19px;
  white-space: pre; overflow: auto; tab-size: 2; border: 0;
}
#codeHl { pointer-events: none; color: #dbe6f2; z-index: 1; }
#codeTa {
  background: transparent; color: transparent; caret-color: var(--acc); resize: none; z-index: 2;
  outline: none; -webkit-text-fill-color: transparent;
}
#codeTa::selection { background: rgba(56, 189, 248, .28); -webkit-text-fill-color: transparent; }
.tok-kw { color: #ff7ab2; } .tok-ty { color: #7ee0c3; } .tok-fn { color: #82cfff; }
.tok-num { color: #ffcf7d; } .tok-str { color: #b8e986; } .tok-com { color: #55677d; font-style: italic; }
.tok-pre { color: #c084fc; } .tok-const { color: #f0a35e; }
.code-side {
  width: 194px; flex: none; border-left: 1px solid var(--line-soft); background: var(--panel);
  overflow-y: auto; padding: 9px; display: flex; flex-direction: column; gap: 10px;
}
.code-side h5 { margin: 0 0 5px; font-size: 10px; text-transform: uppercase; letter-spacing: .09em; color: var(--tx-3); font-weight: 700; }
.demo {
  display: block; width: 100%; text-align: left; padding: 7px 8px; margin-bottom: 4px;
  background: var(--panel-2); border: 1px solid var(--line-soft); border-radius: var(--r-s);
  cursor: pointer; color: var(--tx-2); transition: all .14s var(--ease);
}
.demo:hover { background: var(--panel-3); color: var(--tx); border-color: #3a4a5e; transform: translateX(2px); }
.demo b { display: block; font-size: 11.5px; font-weight: 650; color: var(--tx); }
.demo span { font-size: 10px; color: var(--tx-3); }
.demo.on { border-color: var(--acc-2); background: var(--acc-dim); }

/* serial monitor */
.serial { flex: 1; display: flex; flex-direction: column; min-width: 0; background: #080b10; }
.serial-out {
  flex: 1; overflow-y: auto; padding: 9px 12px; font-family: var(--fm); font-size: 12px;
  line-height: 1.6; color: #b6f0d0; white-space: pre-wrap; word-break: break-word;
}
.serial-out .ln { display: block; min-height: 1.62em; }
.serial-out .ln::selection { background: rgba(125,232,195,.25); }
.serial-out .ln.tx { color: #7dd3fc; }
.serial-out .ln.err { color: #fca5a5; }
.serial-out .ln.sys { color: #6f7f92; font-style: italic; }
.serial-in { display: flex; gap: 6px; padding: 7px 9px; border-top: 1px solid var(--line-soft); flex: none; }
.serial-in input { flex: 1; height: 28px; padding: 0 9px; border-radius: var(--r-s); background: var(--bg-2); border: 1px solid var(--line); color: var(--tx); font-family: var(--fm); font-size: 12px; }
.serial-in input:focus { outline: none; border-color: var(--acc-2); }

/* stimulus panel */
.stimwrap { flex: 1; overflow-y: auto; padding: 11px; display: grid; grid-template-columns: repeat(auto-fill, minmax(258px, 1fr)); gap: 10px; align-content: start; }
.stimcard {
  background: var(--panel-2); border: 1px solid var(--line-soft); border-radius: var(--r-m); padding: 10px;
  display: flex; flex-direction: column; gap: 8px;
}
.stimcard .hd { display: flex; align-items: center; gap: 8px; }
.stimcard .hd .ic { width: 26px; height: 26px; border-radius: 7px; display: grid; place-items: center; flex: none; }
.stimcard .hd b { font-size: 12px; font-weight: 650; }
.stimcard .hd span { display: block; font-size: 10px; color: var(--tx-3); font-family: var(--fm); }
.stimcard .acts { display: flex; gap: 5px; flex-wrap: wrap; }
.stimcard .val { font-family: var(--fm); font-size: 11px; color: var(--acc); margin-left: auto; }

/* ---------------------------------------------------------------- */
/* Status bar                                                        */
/* ---------------------------------------------------------------- */
.statusbar {
  grid-area: stat; display: flex; align-items: center; gap: 12px; padding: 0 10px;
  background: var(--panel); border-top: 1px solid var(--line);
  font-size: 10.5px; color: var(--tx-3); font-family: var(--fm); z-index: 25;
  overflow: hidden; white-space: nowrap;
}
.statusbar .it { display: flex; align-items: center; gap: 4px; flex: none; }
.statusbar .it b { color: var(--tx-2); font-weight: 600; }
.statusbar .sp { flex: 1; }
.statusbar .msg { color: var(--tx-2); overflow: hidden; text-overflow: ellipsis; }
.statusbar .msg.bad { color: #fca5a5; }
.statusbar .msg.ok { color: #6ee7b7; }
.dotstat { width: 6px; height: 6px; border-radius: 50%; background: var(--tx-3); }
.dotstat.ok { background: var(--grn); box-shadow: 0 0 6px #34d39988; }
.dotstat.bad { background: var(--red); box-shadow: 0 0 6px #f8717188; }
.dotstat.run { background: var(--acc); box-shadow: 0 0 6px #38bdf888; animation: pulse 1.4s infinite; }
@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: .35; } }

/* ---------------------------------------------------------------- */
/* Modals / toasts                                                   */
/* ---------------------------------------------------------------- */
.modal-bg {
  position: fixed; inset: 0; background: rgba(4, 7, 11, .68); backdrop-filter: blur(4px);
  display: grid; place-items: center; z-index: 600; padding: 20px;
  animation: fadeIn .16s var(--ease);
}
.modal {
  width: min(560px, 100%); max-height: 84vh; overflow: hidden;
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--r-l);
  box-shadow: var(--sh-3); display: flex; flex-direction: column;
  animation: modalIn .2s var(--ease);
}
@keyframes modalIn { from { opacity: 0; transform: translateY(14px) scale(.98); } }
.modal header {
  display: flex; align-items: center; gap: 9px; padding: 13px 15px;
  border-bottom: 1px solid var(--line-soft); flex: none;
}
.modal header h3 { margin: 0; font-size: 14px; font-weight: 650; }
.modal header .x { margin-left: auto; }
.modal .body { padding: 15px; overflow-y: auto; }
.modal footer { padding: 12px 15px; border-top: 1px solid var(--line-soft); display: flex; gap: 8px; justify-content: flex-end; flex: none; }
.modal p { margin: 0 0 10px; color: var(--tx-2); font-size: 12.5px; line-height: 1.6; }

.kb-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 4px 18px; }
.kb-row { display: flex; align-items: center; gap: 8px; padding: 4px 0; font-size: 12px; color: var(--tx-2); border-bottom: 1px solid var(--line-soft); }
.kb-row .keys { margin-left: auto; display: flex; gap: 3px; }
.kb-row kbd {
  font-family: var(--fm); font-size: 10.5px; background: var(--panel-3); color: var(--tx);
  border: 1px solid var(--line); border-bottom-width: 2px; border-radius: 4px; padding: 1px 5px; min-width: 20px; text-align: center;
}

.toasts { position: fixed; right: 14px; bottom: 40px; z-index: 800; display: flex; flex-direction: column; gap: 8px; align-items: flex-end; pointer-events: none; }
.toast {
  display: flex; align-items: center; gap: 9px; padding: 9px 13px; border-radius: var(--r-m);
  background: rgba(20, 26, 34, .97); border: 1px solid var(--line); box-shadow: var(--sh-2);
  font-size: 12.5px; color: var(--tx); max-width: 340px; pointer-events: auto;
  animation: toastIn .22s var(--ease);
}
.toast.out { animation: toastOut .2s var(--ease) forwards; }
@keyframes toastIn { from { opacity: 0; transform: translateX(24px) scale(.96); } }
@keyframes toastOut { to { opacity: 0; transform: translateX(24px) scale(.96); } }
.toast .ic { width: 17px; height: 17px; flex: none; }
.toast.ok .ic { color: var(--grn); } .toast.err .ic { color: var(--red); }
.toast.warn .ic { color: var(--amb); } .toast.info .ic { color: var(--acc); }

/* mobile-only bits are hidden on desktop */
.monly { display: none !important; }
.sheet-bg { display: none; }

/* ---------------------------------------------------------------- */
/* MOBILE / TOUCH LAYOUT — a different structure, not a squeeze      */
/* ---------------------------------------------------------------- */
@media (max-width: 900px), (pointer: coarse) and (max-width: 1100px) {
  :root { --topbar-h: 48px; --status-h: 0px; --dock-h: 46vh; }

  #app {
    grid-template-rows: var(--topbar-h) 1fr;
    grid-template-columns: 1fr;
    grid-template-areas: "top" "main";
  }
  .statusbar { display: none; }
  .palette, .inspector { display: none; }
  .monly { display: flex !important; }
  .donly { display: none !important; }

  .topbar { padding: 0 8px; gap: 6px; }
  .brand .sub, .proj-name { display: none; }
  .brand .name { font-size: 12.5px; }
  .tb-group.mob-actions { gap: 2px; }
  .btn { height: 34px; }
  .btn.icon { width: 34px; }

  .stage-tools { left: 8px; top: 8px; padding: 3px; gap: 2px; }
  .stage-tools .btn { height: 36px; width: 36px; }
  .stage-tools .btn svg { width: 17px; height: 17px; }
  .zoomer { right: 8px; bottom: 8px; }
  .zoomer .btn { height: 34px; width: 34px; }

  .simbar { top: auto; bottom: 8px; left: 50%; transform: translateX(-50%); padding: 3px 5px; gap: 5px; }
  .simbar .btn { height: 34px; }
  .simbar .t { font-size: 11px; min-width: 54px; }
  .simbar .spd { display: none; }
  .stage.has-dock .simbar { bottom: calc(var(--dock-h) + 8px); }

  .hintline { bottom: auto; top: 54px; font-size: 11px; padding: 5px 10px; }

  /* bottom sheet dock */
  .dock {
    position: fixed; left: 0; right: 0; bottom: 0; height: var(--dock-h);
    border-radius: var(--r-l) var(--r-l) 0 0; border-top: 1px solid var(--line);
    transform: translateY(100%); z-index: 100;
  }
  .dock.open { transform: translateY(0); }
  .dock.min { transform: translateY(100%); }
  .dock-tabs { height: 40px; padding: 0 6px; }
  .dock-tabs::before {
    content: ''; position: absolute; top: 5px; left: 50%; transform: translateX(-50%);
    width: 34px; height: 4px; border-radius: 3px; background: var(--line);
  }
  .dock-tabs { position: relative; padding-top: 6px; }
  .dock-tabs button { height: 30px; font-size: 12px; padding: 0 10px; }
  .code-side { width: 150px; }
  .stimwrap { grid-template-columns: 1fr; }

  /* bottom nav */
  .mobnav {
    position: fixed; left: 0; right: 0; bottom: 0; height: 54px; z-index: 90;
    display: flex; align-items: stretch; background: rgba(16, 21, 28, .96);
    backdrop-filter: blur(12px); border-top: 1px solid var(--line);
    padding-bottom: env(safe-area-inset-bottom);
  }
  .mobnav button {
    flex: 1; background: transparent; border: 0; cursor: pointer; color: var(--tx-3);
    display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
    font-size: 9.5px; font-weight: 600; transition: color .14s;
  }
  .mobnav button svg { width: 19px; height: 19px; }
  .mobnav button.on { color: var(--acc); }
  .mobnav button:active { background: var(--panel-2); }
  .stage { padding-bottom: 54px; }

  /* side drawers for palette + inspector */
  .drawer {
    position: fixed; top: 0; bottom: 0; width: min(310px, 86vw); z-index: 200;
    background: var(--panel); display: flex; flex-direction: column;
    transition: transform .26s var(--ease); box-shadow: var(--sh-3);
  }
  .drawer.left { left: 0; transform: translateX(-102%); border-right: 1px solid var(--line); }
  .drawer.right { right: 0; transform: translateX(102%); border-left: 1px solid var(--line); }
  .drawer.open { transform: translateX(0); }
  .drawer .dh {
    display: flex; align-items: center; gap: 8px; padding: 11px 12px; height: var(--topbar-h);
    border-bottom: 1px solid var(--line-soft); flex: none;
  }
  .drawer .dh b { font-size: 13px; }
  .drawer .dh .x { margin-left: auto; }
  .drawer.left .pal-list, .drawer.right .insp-body { flex: 1; min-height: 0; }
  .drawer .cat-body { grid-template-columns: 1fr 1fr 1fr; }
  .sheet-bg {
    display: block; position: fixed; inset: 0; background: rgba(4, 7, 11, .55);
    z-index: 190; opacity: 0; pointer-events: none; transition: opacity .24s var(--ease);
  }
  .sheet-bg.on { opacity: 1; pointer-events: auto; }
  .ctx button { padding: 11px 10px; font-size: 13px; }
  .modal { width: 100%; max-height: 88vh; }
  .kb-grid { grid-template-columns: 1fr; }
  .toasts { right: 10px; left: 10px; bottom: 62px; align-items: stretch; }
  .toast { max-width: none; }
}

@media (max-width: 420px) {
  .code-side { display: none; }
  .brand .name span { display: none; }
}

/* reduced motion */
@media (prefers-reduced-motion: reduce) {
  * { animation-duration: .01ms !important; transition-duration: .01ms !important; }
}
