:root {
  --bg: #0b1220;
  --surface: #141c2b;
  --surface-2: #1b2538;
  --border: #2b3a52;
  --text: #e8eef8;
  --muted: #93a4bd;
  --accent: #3b82f6;
  --ex: #f59e0b;
  --em: #38bdf8;
  --tr: #94a3b8;
  --danger: #ef4444;
  --ok: #22c55e;
}

* { box-sizing: border-box; }
body {
  margin: 0;
  min-height: 100vh;
  font-family: "Segoe UI", system-ui, sans-serif;
  background: radial-gradient(circle at top left, #172554 0%, var(--bg) 42%);
  color: var(--text);
  overflow: hidden;
}
.hidden { display: none !important; }
.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 1rem;
  padding: 0.75rem 1rem;
  border-bottom: 1px solid var(--border);
  background: rgba(11, 18, 32, 0.92);
}
.brand h1 { margin: 0.15rem 0 0; font-size: 1.05rem; }
.ver { color: var(--muted); font-weight: 400; font-size: 0.85rem; }
.back { color: var(--accent); text-decoration: none; font-size: 0.85rem; }
.topbar-actions { display: flex; gap: 0.5rem; align-items: center; flex-wrap: wrap; }
.badge {
  padding: 0.3rem 0.55rem;
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--muted);
  font-size: 0.75rem;
}
.badge.auth { color: var(--ok); background: rgba(34,197,94,0.12); }

.layout {
  display: grid;
  grid-template-columns: 280px 1fr 320px;
  height: calc(100vh - 64px);
}
.sidebar {
  overflow: auto;
  border-right: 1px solid var(--border);
  background: var(--surface);
  padding: 0.75rem;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}
.sidebar.right { border-right: none; border-left: 1px solid var(--border); }
.panel {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 0.65rem;
  display: grid;
  gap: 0.45rem;
}
.panel h2 {
  margin: 0;
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
}
.meta, .status { color: var(--muted); font-size: 0.82rem; margin: 0; }
.status { padding: 0 0.2rem; }

.btn {
  border: 1px solid transparent;
  border-radius: 8px;
  padding: 0.4rem 0.7rem;
  font: inherit;
  cursor: pointer;
  background: var(--surface-2);
  color: var(--text);
}
.btn:disabled { opacity: 0.45; cursor: not-allowed; }
.btn-primary { background: var(--accent); color: white; }
.btn-ghost { background: transparent; border-color: var(--border); color: var(--muted); }
.btn-ghost:hover:not(:disabled) { color: var(--text); }
.btn-danger { background: transparent; border-color: rgba(239,68,68,0.4); color: var(--danger); }
.btn-secondary { background: var(--surface); border-color: var(--border); }

select, input, textarea {
  width: 100%;
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.4rem 0.5rem;
  font: inherit;
}
#system-select { height: 160px; }
.panel-hint {
  margin: 0 0 0.45rem;
  color: var(--muted);
  font-size: 0.78rem;
  line-height: 1.35;
}
.panel-sub {
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 500;
}
.field-label {
  display: block;
  margin: 0.35rem 0 0.2rem;
  color: var(--muted);
  font-size: 0.75rem;
}
.panel-channels.locked {
  opacity: 0.72;
}
.panel-channels #filter-set-select { margin-bottom: 0.45rem; }
.channel-list, .device-groups, .catalog-results, .intensity-list, .fluor-list {
  display: grid;
  gap: 0.3rem;
  max-height: 220px;
  overflow: auto;
}
.channel-set-heading {
  margin-top: 0.35rem;
  padding: 0.2rem 0.35rem;
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.03em;
  text-transform: uppercase;
}
.channel-set-heading:first-child { margin-top: 0; }
.channels-empty {
  margin: 0.4rem 0;
  color: var(--muted);
  font-size: 0.82rem;
}
.channel-item, .device-item, .catalog-item, .fluor-item {
  padding: 0.4rem 0.5rem;
  border-radius: 8px;
  border: 1px solid transparent;
  background: var(--bg);
  cursor: pointer;
  font-size: 0.85rem;
}
.channel-item.active, .device-item.active {
  border-color: var(--accent);
  background: rgba(59,130,246,0.12);
}
.row-actions { display: flex; gap: 0.35rem; flex-wrap: wrap; }
.chk { display: flex; gap: 0.35rem; align-items: center; color: var(--muted); font-size: 0.82rem; }
.chk input { width: auto; }

.canvas-wrap {
  position: relative;
  min-width: 0;
  background:
    linear-gradient(rgba(255,255,255,0.03) 1px, transparent 1px),
    linear-gradient(90deg, rgba(255,255,255,0.03) 1px, transparent 1px),
    #0a101c;
  background-size: 24px 24px, 24px 24px, auto;
}
#schematic { width: 100%; height: 100%; display: block; cursor: grab; touch-action: none; }
#schematic:active { cursor: grabbing; }
.canvas-toolbar {
  position: absolute;
  z-index: 2;
  top: 0.6rem;
  left: 0.6rem;
  right: 0.6rem;
  display: flex;
  justify-content: space-between;
  gap: 0.75rem;
  pointer-events: none;
}
.canvas-toolbar > * { pointer-events: auto; }
.toolbar-right { display: flex; gap: 0.6rem; align-items: center; flex-wrap: wrap; justify-content: flex-end; }
#system-title {
  background: rgba(11,18,32,0.85);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.35rem 0.55rem;
  font-size: 0.85rem;
}
.view-controls {
  display: flex;
  align-items: center;
  gap: 0.2rem;
  background: rgba(11,18,32,0.85);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.15rem 0.25rem;
}
.btn-icon {
  min-width: 1.8rem;
  padding: 0.2rem 0.4rem;
  font-size: 1rem;
  line-height: 1;
}
.zoom-label {
  min-width: 3.2rem;
  text-align: center;
  font-size: 0.78rem;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}
.empty {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  color: var(--muted);
  margin: 0;
  pointer-events: none;
  z-index: 1;
}
.empty[hidden],
.empty.hidden {
  display: none !important;
}
.group-box {
  stroke: #475569;
  stroke-width: 1.25;
  rx: 10;
}
.group-label {
  fill: #cbd5e1;
  font-size: 12px;
  font-weight: 700;
  pointer-events: none;
}
.disk-l {
  fill: #334155;
  stroke: #94a3b8;
  stroke-width: 1.5;
}
.disk-l.on-path {
  fill: #3f4f6a;
  stroke: #fbbf24;
  stroke-width: 2.4;
}
.disk-l.selected { stroke: var(--accent); stroke-width: 2.6; }
.disk-l.dimmed { opacity: 0.28; }
.disk-arm-label { font-size: 9px; font-weight: 700; }
.disk-laser-label { font-size: 10px; font-weight: 700; }
.station-box { fill: #1e293b; stroke: #64748b; stroke-width: 1.5; rx: 6; }
.station-box.selected { stroke: var(--accent); stroke-width: 2.5; }
.station-box.on-path {
  fill: #243b5c;
  stroke: #fbbf24;
  stroke-width: 2.6;
}
.station-box.dimmed { opacity: 0.28; }
.station-label { fill: var(--text); font-size: 11px; pointer-events: none; }
.station-label.dimmed { fill: #64748b; }
.laser-chip { stroke: #cbd5e1; stroke-width: 1; fill: #f8fafc; }
.laser-chip.on { stroke: #1e293b; stroke-width: 2; }
.laser-chip.off { fill: #0f172a; stroke: #475569; }
.laser-chip.dimmed { fill: #1e293b; stroke: #334155; }
.laser-chip-label { font-size: 8px; font-weight: 700; pointer-events: none; }
.laser-chip-label.on { fill: #0f172a; }
.laser-chip-label.off { fill: #64748b; }
.laser-chip-label.dimmed { fill: #475569; }
.beam { fill: none; stroke-width: 2.2; opacity: 0.55; }
.beam.excitation { stroke: var(--ex); }
.beam.emission { stroke: var(--em); }
.beam.transmitted { stroke: var(--tr); }
.beam.dimmed { opacity: 0.18; stroke-width: 1.6; }
.beam.on-path { opacity: 1; stroke-width: 4; filter: drop-shadow(0 0 3px currentColor); }
.beam-arrow-head { fill: #94a3b8; }
.beam-label {
  font-size: 9px;
  font-weight: 600;
  pointer-events: none;
  paint-order: stroke;
  stroke: rgba(11, 18, 32, 0.85);
  stroke-width: 3px;
}
.beam-label.excitation { fill: #fbbf24; }
.beam-label.emission { fill: #7dd3fc; }
.beam-label.transmitted { fill: #cbd5e1; }
.beam-label.on-path { font-size: 10px; font-weight: 700; }
.beam-label.dimmed { fill: #64748b; opacity: 0.85; font-weight: 500; }

#spectrum-canvas {
  width: 100%;
  background: #0a101c;
  border-radius: 8px;
  border: 1px solid var(--border);
}
.preview-row { display: flex; gap: 0.5rem; }
.preview-row canvas {
  border-radius: 8px;
  border: 1px solid var(--border);
  background: #000;
  image-rendering: pixelated;
}
.inspector { font-size: 0.82rem; color: var(--muted); line-height: 1.4; }
.inspector strong { color: var(--text); }

dialog {
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
  color: var(--text);
  padding: 1rem;
  width: min(560px, 94vw);
  max-height: 90vh;
  overflow: auto;
}
dialog.dialog-wide { width: min(720px, 96vw); }
dialog::backdrop { background: rgba(0,0,0,0.55); }
dialog form { display: grid; gap: 0.55rem; }
dialog label { display: grid; gap: 0.25rem; font-size: 0.85rem; color: var(--muted); }
dialog fieldset {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.55rem 0.65rem 0.7rem;
  margin: 0;
}
dialog legend { color: var(--text); font-size: 0.82rem; padding: 0 0.25rem; }
.dialog-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.55rem;
}
.dialog-grid .full { grid-column: 1 / -1; }
.chk-block { display: flex !important; flex-direction: row; align-items: center; gap: 0.4rem; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 0.5rem; margin-top: 0.5rem; }
.positions-grid, .device-fields { display: grid; gap: 0.4rem; }
.positions-grid .pos-row { display: grid; gap: 0.2rem; }
.laser-checks, .build-kinds { display: grid; gap: 0.3rem; }
.build-channels {
  display: grid;
  gap: 0.25rem;
  max-height: 280px;
  overflow: auto;
}
.build-cam-row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.45rem;
  align-items: center;
  padding: 0.15rem 0 0.35rem 0.35rem;
}
.btn-tiny {
  padding: 0.15rem 0.45rem;
  font-size: 0.75rem;
}
.dimmed-opt { opacity: 0.45; }
.sets-list { display: grid; gap: 0.4rem; margin-bottom: 0.5rem; }
.set-row {
  display: grid;
  grid-template-columns: 1fr auto auto;
  gap: 0.4rem;
  align-items: center;
}
.error { color: var(--danger); font-size: 0.85rem; margin: 0; }

.dialog-path { width: min(960px, 98vw); }
.path-track {
  display: flex;
  align-items: stretch;
  gap: 0.35rem;
  overflow-x: auto;
  padding: 0.35rem 0 0.75rem;
  min-height: 200px;
}
.path-arrow {
  display: grid;
  place-items: center;
  color: var(--muted);
  font-size: 1.25rem;
  flex: 0 0 auto;
  padding: 0 0.15rem;
}
.path-card {
  flex: 0 0 158px;
  background: #1e293b;
  border: 1.5px solid #64748b;
  border-radius: 8px;
  padding: 0.45rem 0.5rem 0.4rem;
  display: grid;
  gap: 0.2rem;
  align-content: start;
}
.path-card.phase-ex { border-color: #f59e0b; background: #2a2114; }
.path-card.phase-sample { border-color: #34d399; background: #14261c; }
.path-card.phase-em { border-color: #38bdf8; background: #14222b; }
.path-card.phase-cam { border-color: #94a3b8; background: #1b2230; }
.path-role {
  font-size: 0.72rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  color: var(--muted);
}
.path-title { font-size: 0.82rem; font-weight: 700; color: var(--text); line-height: 1.25; }
.path-detail { font-size: 0.72rem; color: var(--muted); min-height: 1.6em; }
.path-spec {
  width: 100%;
  height: 64px;
  border-radius: 4px;
  border: 1px solid var(--border);
  margin-top: 0.2rem;
}
.sample-fluor-list {
  display: grid;
  gap: 0.25rem;
  max-height: 320px;
  overflow: auto;
  margin: 0.5rem 0;
  padding: 0.35rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
}

@media (max-width: 700px) {
  .dialog-grid { grid-template-columns: 1fr; }
}

@media (max-width: 1100px) {
  body { overflow: auto; }
  .layout {
    grid-template-columns: 1fr;
    height: auto;
  }
  .canvas-wrap { min-height: 55vh; }
  .sidebar { max-height: none; }
}
