:root {
  --bg: #0b1220;
  --bg-2: #0e1628;
  --panel: #121a2b;
  --panel-2: #162033;
  --border: #1e2a3f;
  --border-hi: #2a3a55;
  --text: #e8eef7;
  --muted: #8b9bb4;
  --accent: #2dd4bf;
  --accent-dim: rgba(45, 212, 191, 0.12);
  --ok: #22c55e;
  --warn: #f59e0b;
  --danger: #ef4444;
  --ai: #38bdf8;
  --matlab: #f59e0b;
  --c: var(--accent);
  --n: var(--bg);
  --p: var(--panel);
  --d: var(--danger);
  --w: var(--warn);
  --font: "IBM Plex Sans", system-ui, sans-serif;
  --mono: "IBM Plex Mono", ui-monospace, monospace;
  --hdr: 56px;
  --ops-h: min(72vh, 820px);
  --gap: 12px;
  --radius: 10px;
  --fs: 14px;
  --fs-sm: 12px;
  --fs-lg: 16px;
  --fs-xl: 22px;
}

* { box-sizing: border-box; margin: 0; padding: 0; }
html { -webkit-text-size-adjust: 100%; scroll-behavior: smooth; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: var(--fs);
  line-height: 1.5;
  min-height: 100vh; min-height: 100dvh;
  overflow-x: hidden;
  overflow-y: auto;
}

#app {
  display: flex;
  flex-direction: column;
  min-height: 100dvh;
}

.topbar {
  position: sticky; top: 0;
  display: flex; flex-direction: column; flex: 0 0 auto;
  border-bottom: 1px solid var(--border);
  background: linear-gradient(180deg, #101828 0%, rgba(11,18,32,.96) 100%);
  backdrop-filter: blur(10px);
  z-index: 50;
}

.workspace {
  display: flex; flex-direction: column; gap: var(--gap);
  padding: var(--gap); padding-bottom: 28px;
  flex: 1; min-width: 0;
}

/* ===== Header ===== */
.hdr {
  display: flex; align-items: center; gap: 14px;
  padding: 0 14px;
  min-height: var(--hdr);
}
.brand { display: flex; align-items: baseline; gap: 7px; flex: 0 0 auto; }
.brand-mark { font-weight: 700; letter-spacing: 0.08em; color: var(--accent); font-size: 18px; }
.brand-name { font-weight: 600; letter-spacing: 0.12em; font-size: 18px; }
.brand-tag {
  font-family: var(--mono); font-size: 10px; font-weight: 600;
  color: var(--muted); border: 1px solid var(--border); border-radius: 3px;
  padding: 2px 6px; letter-spacing: 0.06em;
}
.hdr-status { display: flex; align-items: center; gap: 8px; flex: 1; min-width: 0; }
.hdr-src, .hdr-actions { display: flex; align-items: center; gap: 4px; flex-wrap: wrap; }

.pill {
  font-family: var(--mono); font-size: var(--fs-sm); padding: 5px 10px;
  border-radius: 999px; border: 1px solid; white-space: nowrap;
}
.pill.on { color: var(--ok); border-color: rgba(34,197,94,.45); background: rgba(34,197,94,.1); }
.pill.off { color: var(--muted); border-color: var(--border); }

.live {
  font-family: var(--mono); font-size: var(--fs-sm); font-weight: 600;
  letter-spacing: 0.08em; padding: 5px 9px; border-radius: 4px;
  border: 1px solid var(--border);
}
.live.on { color: var(--danger); border-color: rgba(239,68,68,.5); background: rgba(239,68,68,.12);
  animation: pulse-live 1.6s ease-in-out infinite; }
.live.off { color: var(--muted); }
@keyframes pulse-live {
  0%, 100% { opacity: 1; } 50% { opacity: .65; }
}

.cnt {
  display: none; background: var(--danger); color: #fff; border-radius: 999px;
  padding: 3px 8px; font-size: var(--fs-sm); font-weight: 700; font-family: var(--mono);
}

/* ===== Buttons ===== */
.b {
  font-family: var(--font); font-weight: 600; font-size: var(--fs-sm);
  padding: 8px 13px; min-height: 34px; border: 1px solid var(--border-hi);
  border-radius: 7px; background: var(--panel-2); color: var(--text);
  cursor: pointer; touch-action: manipulation; transition: background .15s, border-color .15s, color .15s;
}
.b:hover { border-color: var(--accent); color: var(--accent); }
.b.on { background: var(--accent-dim); border-color: var(--accent); color: var(--accent); }
.b:disabled { opacity: .45; cursor: not-allowed; }
.b.sm { padding: 5px 10px; min-height: 30px; font-size: 12px; }
.b.ghost { background: transparent; }
.b.gold { border-color: var(--ai); color: var(--ai); }
#bMute.muted { background: rgba(127,29,29,.45); border-color: var(--danger); color: #fecaca; }

/* ===== Tools drawer ===== */
.tools {
  border-top: 1px solid var(--border); background: var(--bg-2);
  padding: 8px 14px; display: flex; flex-direction: column; gap: 6px;
}
.tools[hidden] { display: none !important; }
.tools-row { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; }
.tools-label {
  font-family: var(--mono); font-size: 9px; letter-spacing: 0.08em;
  color: var(--muted); text-transform: uppercase; min-width: 72px;
}
.tools-meta { font-size: 10px; color: var(--muted); font-family: var(--mono); }
#wsurl {
  flex: 1; min-width: 180px; max-width: 420px;
  background: var(--panel); color: var(--text); border: 1px solid var(--border);
  border-radius: 6px; padding: 5px 8px; font-family: var(--mono); font-size: 11px;
}
#st { font-size: 10px; color: var(--muted); max-height: 90px; overflow: auto;
  background: var(--panel); padding: 6px 8px; border-radius: 6px; display: none; }
#st.show { display: block; }

/* ===== Main ops ===== */
.ops {
  display: grid; grid-template-columns: minmax(0, 1.35fr) minmax(300px, 0.9fr);
  gap: var(--gap); min-height: var(--ops-h); height: var(--ops-h); overflow: hidden;
}
.rail {
  display: grid; grid-template-rows: minmax(200px, 42%) minmax(0, 1fr);
  gap: var(--gap); min-height: 0;
}
.deck {
  display: flex; flex-direction: column; gap: var(--gap);
}
.deck > .panel {
  min-height: 220px;
}
#panel-ev-stats { min-height: 0; }
#panel-ev-chart { min-height: 240px; }
#panel-ev-list { min-height: 360px; max-height: none; }
#panel-cam { min-height: 280px; }
#panel-sys { min-height: 0; }
#panel-iqa, #panel-iqa-warn { min-height: 0; }
.iqa-panel-body {
  font-size: var(--fs) !important;
  line-height: 1.65;
  min-height: 120px;
}
#panel-iqa-warn .panel-hd h3 { color: var(--warn); }

.panel {
  background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius);
  display: flex; flex-direction: column; min-height: 0; overflow: hidden;
}
.panel-hd {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  padding: 10px 14px; border-bottom: 1px solid var(--border); flex: 0 0 auto;
}
.panel-hd h3 {
  font-size: 13px; font-weight: 700; letter-spacing: 0.07em;
  text-transform: uppercase; color: var(--text);
}
.panel-pad { padding: 12px 14px; }
.panel-help {
  padding: 0 14px 12px; font-size: var(--fs-sm); color: var(--muted); line-height: 1.5;
}
.panel-hd-actions {
  display: flex; align-items: center; justify-content: flex-end; gap: 6px;
  flex-wrap: wrap; min-width: 0;
}
.zoom-btn {
  font-size: 12px; line-height: 1; min-width: 28px; padding: 3px 7px;
  color: var(--muted); border-color: var(--border);
}
.zoom-btn:hover { color: var(--accent); border-color: rgba(45,212,191,.35); }
.panel.panel-zoom {
  position: fixed; inset: 10px; z-index: 900;
  width: auto !important; height: auto !important;
  max-width: none; max-height: none;
  border-radius: 10px;
  box-shadow: 0 0 0 1px var(--border), 0 24px 80px rgba(0,0,0,.55);
  background: var(--panel);
}
body.panel-zoom-open { overflow: hidden; }
body.panel-zoom-open::before {
  content: ""; position: fixed; inset: 0; z-index: 890;
  background: rgba(5, 8, 15, .72); pointer-events: none;
}
.panel.panel-zoom .panel-scroll,
.panel.panel-zoom.metrics-stack { overflow: auto; }
.chip {
  font-family: var(--mono); font-size: 11px; font-weight: 600;
  padding: 3px 8px; border-radius: 5px; background: var(--accent-dim);
  color: var(--accent); border: 1px solid rgba(45,212,191,.25);
}
.chip.muted { background: transparent; color: var(--muted); border-color: var(--border); }

.wall .kv {
  position: relative; flex: 1; min-height: 0; background: #05080f;
  margin: 0; border: none; border-radius: 0; aspect-ratio: auto; max-width: none;
}
.kv video, .kv #ov { width: 100%; height: 100%; display: block; }
.kv video { object-fit: contain; background: #000; }
#ov { position: absolute; inset: 0; pointer-events: none; transition: clip-path .1s; }

#cb {
  position: absolute; top: 10px; right: 10px; z-index: 20;
  background: var(--danger); color: #fff; font-family: var(--mono);
  font-size: 11px; font-weight: 700; padding: 5px 10px; border-radius: 4px;
  display: none; animation: blink 1s infinite;
}
@keyframes blink { 0%,100%{opacity:1} 50%{opacity:.55} }
#kt {
  position: absolute; bottom: 10px; left: 50%; transform: translateX(-50%);
  padding: 6px 14px; border-radius: 6px; font-weight: 700; font-size: 11px;
  background: rgba(0,0,0,.88); border: 1px solid; display: none; z-index: 20; text-align: center;
}
#mode { /* chip in panel-hd; keep id for JS */ }
#badge {
  position: absolute; top: 10px; left: 10px; z-index: 100;
  font-family: var(--mono); font-size: 10px; font-weight: 600;
  padding: 4px 8px; border-radius: 4px; background: #334155; color: #fff; display: none;
}
#bp {
  position: absolute; top: 42px; left: 10px; z-index: 100;
  background: rgba(0,0,0,.88); border: 1px solid var(--ai); color: var(--ai);
  padding: 6px 10px; border-radius: 4px; font-family: var(--mono); font-size: 9px;
  line-height: 1.5; display: none;
}
#bp.show { display: block; }
#bp .bp-title { font-weight: 700; margin-bottom: 2px; }
#bp .br { display: flex; justify-content: space-between; gap: 12px; }
#bp .bv { color: var(--ok); font-weight: 700; }
#cc { position: absolute; inset: 0; display: none; z-index: 15; pointer-events: none; }
#cc.show { display: block; }
#cs {
  position: absolute; top: 0; bottom: 0; width: 3px; background: var(--accent);
  cursor: ew-resize; box-shadow: 0 0 12px rgba(45,212,191,.5); pointer-events: auto; touch-action: none;
}
#cs::before {
  content: "◀ ▶"; position: absolute; top: 50%; left: 50%; transform: translate(-50%,-50%);
  background: var(--accent); color: #041016; padding: 3px 8px; border-radius: 12px;
  font-size: 10px; font-weight: 700; white-space: nowrap;
}
#cl, #cr {
  position: absolute; top: 10px; padding: 3px 8px; border-radius: 4px;
  font-size: 10px; font-weight: 700; font-family: var(--mono);
}
#cl { left: 10px; background: rgba(239,68,68,.85); }
#cr { right: 10px; background: rgba(34,197,94,.85); }

/* 3D */
#R { position: relative; flex: 1; min-height: 0; background: #000; }
#c3d { width: 100%; height: 100%; }
#c3dLabel { max-width: 55%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.metrics-stack {
  display: grid; grid-template-columns: 1fr 1fr; gap: 6px; padding: 0 0 6px;
  overflow: auto; border: none; background: transparent;
  align-content: start;
}
.metrics-stack > .panel-hd {
  grid-column: 1 / -1; margin: 0; border-radius: 0;
  background: var(--panel); border: 1px solid var(--border); border-bottom: 1px solid var(--border);
}
.metrics-stack > .metric-card { margin: 0 6px; }
.metrics-stack > .metric-card.span2 { margin: 0 6px; }
.panel.panel-zoom.metrics-stack {
  display: grid; background: var(--bg, #0b1220); border: 1px solid var(--border);
  padding-bottom: 10px;
}
.metric-card {
  background: var(--panel); border: 1px solid var(--border); border-radius: 8px;
  padding: 10px 11px; font-size: var(--fs-sm); min-height: 0;
}
.metric-card.span2 { grid-column: span 2; }
.metric-card h4 {
  font-size: 11px; font-weight: 700; letter-spacing: 0.05em; text-transform: uppercase;
  color: var(--muted); margin-bottom: 6px; padding-bottom: 4px; border-bottom: 1px solid var(--border);
}
.metric-card.warn-card h4 { color: var(--warn); }
.metric-card.matlab-card h4 { color: var(--matlab); }
.metric-body { font-family: var(--mono); font-size: var(--fs-sm); line-height: 1.5; }
.metric-body.det { font-size: var(--fs-lg); font-weight: 600; color: var(--accent); }
.row { display: flex; justify-content: space-between; gap: 8px; margin: 2px 0; }
#scc { width: 100%; height: 72px; display: block; }

/* ===== Deck panels (scroll) ===== */
.panel-scroll { flex: 1; min-height: 0; overflow: auto; padding: 12px 14px; }

.camtip { font-size: var(--fs-sm); color: var(--muted); margin: 0 0 10px; line-height: 1.5; }
.camgrid { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 12px; }
.cam {
  background: var(--panel-2); border: 1px solid var(--border); border-radius: 6px;
  overflow: hidden; cursor: pointer;
}
.cam:hover { border-color: var(--border-hi); }
.cam.sel { border-color: var(--accent); box-shadow: 0 0 0 1px rgba(45,212,191,.35); }
.camimg { position: relative; background: #000; min-height: 90px; }
.camimg img { width: 100%; height: auto; display: block; }
.zdraw { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; }
.zdraw .zp { fill: rgba(239,68,68,.25); stroke: #ef4444; stroke-width: .004; }
.zdraw .zn { fill: rgba(250,204,21,.2); stroke: #facc15; stroke-width: .005; stroke-dasharray: .01; }
.zdraw .zc { fill: #facc15; }
.cam.editing .camimg { cursor: crosshair; outline: 2px dashed #facc15; }
.camcfg { margin-top: 8px; font-size: 11px; }
.camcfg .zrow { display: flex; gap: 6px; align-items: center; margin: 3px 0; flex-wrap: wrap; }
.camcfg .zrow input {
  background: var(--bg); color: var(--text); border: 1px solid var(--border);
  border-radius: 4px; padding: 2px 4px; font-family: var(--font);
}
.camcfg .zrow input[type=number] { width: 50px; }
.camcfg .rules { display: flex; flex-wrap: wrap; gap: 4px 12px; margin: 8px 0; }
.camh {
  display: flex; justify-content: space-between; align-items: center; gap: 6px;
  padding: 8px 10px 0; font-size: var(--fs);
}
.camh span { margin-left: auto; font-size: var(--fs-sm); font-family: var(--mono); }
.camm { padding: 4px 10px 10px; font-size: 11px; color: var(--muted); line-height: 1.5; word-break: break-all; }

.evbar { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; }
.evbar select, .evbar input {
  background: var(--bg); color: var(--text); border: 1px solid var(--border);
  border-radius: 6px; padding: 5px 8px; font-size: var(--fs-sm); font-family: var(--font);
}
.evbar .chk { font-size: var(--fs-sm); color: var(--muted); display: flex; align-items: center; gap: 4px; }
.evbar a.b { text-decoration: none; display: inline-flex; align-items: center; }
.stats {
  display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 10px;
}
.stat {
  background: var(--panel-2); border: 1px solid var(--border); border-radius: 8px;
  padding: 12px 14px; min-height: 78px;
}
.stat h4 {
  font-size: var(--fs-sm); color: var(--muted); margin-bottom: 6px; border: none; padding: 0;
  text-transform: none; letter-spacing: 0; font-weight: 600;
}
.stat .big { font-family: var(--mono); font-size: var(--fs-xl); font-weight: 700; line-height: 1.2; }
.chart-wrap {
  flex: 1; min-height: 180px; display: flex; flex-direction: column;
}
#evChart {
  width: 100%; height: 180px; flex: 1;
  background: rgba(0,0,0,.35); border-radius: 8px; display: none;
}
.ev-gallery {
  display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 12px; align-content: start;
}
.ev {
  display: flex; flex-direction: column; gap: 0; background: var(--panel-2);
  border: 1px solid var(--border); border-top: 3px solid var(--danger);
  border-radius: 8px; overflow: hidden; font-size: var(--fs-sm);
}
.ev.done { border-top-color: var(--ok); opacity: .88; }
.thumb {
  width: 100%; height: 150px; object-fit: cover; border-radius: 0;
  cursor: zoom-in; background: #05080f; flex: 0 0 auto; display: block;
}
.thumb.none {
  display: flex; align-items: center; justify-content: center; color: #475569; cursor: default;
  font-size: var(--fs); height: 110px;
}
.evb { flex: 1; min-width: 0; line-height: 1.5; padding: 10px 12px 4px; }
.evm { color: var(--muted); font-size: 12px; margin-top: 2px; }
.tag {
  color: #fff; border-radius: 4px; padding: 2px 7px; font-size: 11px;
  font-weight: 700; margin-right: 6px; font-family: var(--mono);
}
.eva {
  display: flex; flex-wrap: wrap; gap: 6px; align-items: center;
  padding: 8px 12px 12px;
}
.eva .ok { color: var(--ok); font-size: var(--fs-sm); white-space: nowrap; }
#evMore { text-align: center; padding: 10px 0 14px; }

.sys-grid {
  display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); gap: 8px;
  padding: 12px 14px; flex: 1; align-content: start;
}
.sys-item {
  background: var(--panel-2); border: 1px solid var(--border); border-radius: 8px;
  padding: 10px 12px; display: flex; flex-direction: column; gap: 4px;
}
.sys-item .k { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; }
.sys-item .v { font-family: var(--mono); font-size: var(--fs); font-weight: 600; color: var(--text); }
.sys-item .v.ok { color: var(--ok); }
.sys-item .v.warn { color: var(--warn); }
.sys-item .v.bad { color: var(--danger); }
.sys-dbg {
  font-family: var(--mono); font-size: 11px; color: var(--ok);
  border-top: 1px solid var(--border); padding: 8px 14px; white-space: nowrap;
  overflow: hidden; text-overflow: ellipsis; flex: 0 0 auto; background: rgba(0,0,0,.25);
}
.sys-dbg.err { color: var(--danger); }

.hint {
  background: var(--panel-2); border: 1px dashed var(--border-hi); border-radius: 8px;
  padding: 16px 18px; font-size: var(--fs); line-height: 1.65; color: var(--muted);
}
.hint > b, .feature-guide > b {
  color: var(--text); font-size: var(--fs-lg); display: block; margin-bottom: 8px;
}
.hint li b { color: var(--text); font-size: inherit; display: inline; margin: 0; }
.hint ul { margin: 8px 0 14px 1.1em; }
.hint li { margin: 4px 0; }
.hint code {
  background: var(--bg); color: var(--accent); padding: 1px 6px; border-radius: 4px;
  font-family: var(--mono); word-break: break-all; font-size: var(--fs-sm);
}
.feature-guide .guide-actions { margin-top: 4px; }

/* ===== Loading ===== */
#ld {
  position: fixed; inset: 0; background: var(--bg); z-index: 99999;
  display: flex; flex-direction: column; justify-content: center; align-items: center;
  transition: opacity .4s;
}
#ld.hide { opacity: 0; pointer-events: none; }
.ld-mark {
  font-weight: 700; letter-spacing: 0.2em; color: var(--accent); font-size: 22px;
}
.ld-sub {
  font-family: var(--mono); font-size: 11px; color: var(--muted);
  letter-spacing: 0.12em; margin: 6px 0 18px; text-transform: uppercase;
}
#lds { width: min(320px, 80vw); }
#ld .s { color: var(--muted); font-size: 12px; margin: 3px 0; font-family: var(--mono); }
#ld .s.ok { color: var(--ok); }
#ld .s.err { color: var(--danger); }
#ld .bar {
  width: min(320px, 80vw); height: 3px; background: var(--border);
  border-radius: 2px; margin-top: 16px; overflow: hidden;
}
#ld .bar div { height: 100%; background: var(--accent); transition: width .3s; width: 0%; }
#ap {
  margin-top: 10px; width: min(320px, 80vw); height: 4px; background: var(--border);
  border-radius: 2px; overflow: hidden; display: none;
}
#ap div { height: 100%; background: var(--ai); width: 0%; transition: width .3s; }

/* ===== Toasts & viewer ===== */
#toasts {
  position: fixed; top: 12px; right: 12px; z-index: 10000;
  display: flex; flex-direction: column; gap: 6px; width: min(320px, 90vw); pointer-events: none;
}
.toast {
  pointer-events: auto; cursor: pointer; padding: 9px 11px; border-radius: 6px;
  font-size: 11px; line-height: 1.45; color: #fff;
  box-shadow: 0 8px 24px rgba(0,0,0,.45); animation: tin .25s ease-out;
  font-family: var(--font);
}
.toast.crit { background: rgba(127,29,29,.96); border: 1px solid var(--danger); }
.toast.warn { background: rgba(120,53,15,.96); border: 1px solid var(--warn); }
.toast.info { background: rgba(6,78,59,.96); border: 1px solid var(--ok); }
.toast .d { color: #fde68a; }
@keyframes tin { from { transform: translateX(24px); opacity: 0; } to { transform: none; opacity: 1; } }
#imgv {
  position: fixed; inset: 0; background: rgba(0,0,0,.88); z-index: 10001;
  display: flex; flex-direction: column; align-items: center; justify-content: center; cursor: zoom-out;
}
#imgv[hidden] { display: none; }
#imgv img { max-width: 94vw; max-height: 86vh; border: 1px solid var(--border); border-radius: 6px; }
#imgv .cap { margin-top: 6px; font-size: 11px; color: var(--muted); }
#imgv .cap a { color: var(--accent); }

/* ===== Responsive ===== */
@media (max-width: 1200px) {
  .ops { grid-template-columns: 1fr 1fr; height: auto; min-height: 560px; }
  .stats { grid-template-columns: repeat(3, 1fr); }
  .sys-grid { grid-template-columns: repeat(3, minmax(0, 1fr)); }
}
@media (max-width: 900px) {
  .ops, .rail { grid-template-columns: 1fr; grid-template-rows: none; height: auto; min-height: 0; }
  .wall .kv { min-height: 260px; aspect-ratio: 4/3; }
  #R { height: min(55vw, 360px); }
  .hdr { flex-wrap: wrap; height: auto; padding: 10px 12px; }
  .metrics-stack .metric-card.span2 { grid-column: span 2; }
  .sys-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
@media (max-width: 600px) {
  .metrics-stack { grid-template-columns: 1fr; }
  .metric-card.span2 { grid-column: span 1; }
  .stats { grid-template-columns: repeat(2, 1fr); }
  .sys-grid { grid-template-columns: 1fr; }
  .ev-gallery { grid-template-columns: 1fr; }
  .thumb { height: 140px; }
}
@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; transition: none !important; }
}

/* Legacy hooks still referenced by JS */
.card { background: var(--panel-2); padding: 6px; border-radius: 6px; border: 1px solid var(--border); font-size: 10px; }
.card h4 { color: var(--accent); margin-bottom: 3px; border-bottom: 1px solid var(--border); padding-bottom: 2px; font-size: 11px; }
.hop { display: flex; flex-wrap: wrap; align-items: center; gap: 4px; }
.top, .tabs, .tab, .pane, .kb, .grid { /* removed layout; keep harmless */ }
