:root {
  --bg: #eef3f8;
  --text: #102333;
  --muted: #65788b;
  --accent: #0f6a8a;
  --ok: #0f8a52;
  --bnc: #202a35;
  --lwl: #f07820;
  --meter: #1f6fd2;
  --board-width: 2600px;
  --board-height: 2400px;
  --lab-scale: 1;
  --scaled-board-width: 2600px;
  --scaled-board-height: 2400px;
}

* { box-sizing: border-box; }
html, body { min-height: 100%; }
body {
  margin: 0;
  min-width: 0;
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  background: var(--bg);
  color: var(--text);
}
button { font: inherit; }

.app-header {
  display: flex;
  align-items: center;
  padding: 12px 18px;
  background: linear-gradient(135deg, #123a52, #0f6a8a);
  color: white;
  box-shadow: 0 2px 16px rgba(12, 43, 62, .22);
}
h1 { margin: 0 0 2px; font-size: 21px; }
p { margin: 0; }
.app-header p { opacity: .86; font-size: 13px; }

.workspace {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 210px;
  gap: 10px;
  padding: 10px;
  align-items: start;
  height: calc(100vh - 64px);
  min-height: 0;
}

.lab-board {
  position: relative;
  width: 100%;
  height: 100%;
  min-height: 0;
  overflow: auto;
  border: 1px solid #cbd9e6;
  border-radius: 18px;
  background:
    radial-gradient(circle at 22px 22px, rgba(15,106,138,.13) 1px, transparent 1.5px) 0 0 / 28px 28px,
    linear-gradient(135deg, #f9fcff, #e5eef7);
  box-shadow: inset 0 0 0 1px rgba(255,255,255,.8), 0 16px 38px rgba(16,35,51,.08);
  touch-action: none;
  overscroll-behavior: contain;
}

.lab-content {
  position: relative;
  width: var(--scaled-board-width);
  height: var(--scaled-board-height);
  min-width: var(--scaled-board-width);
  min-height: var(--scaled-board-height);
}


.device-layer,
.cable-layer,
.cable-overlay-layer,
.plug-layer {
  position: absolute;
  left: 0;
  top: 0;
  width: var(--board-width);
  height: var(--board-height);
  min-width: var(--board-width);
  min-height: var(--board-height);
  transform: scale(var(--lab-scale));
  transform-origin: top left;
}

.cable-layer { z-index: 8; overflow: visible; pointer-events: none; }
.device-layer { z-index: 10; pointer-events: none; }
.cable-overlay-layer { z-index: 80; overflow: visible; pointer-events: none; }
.plug-layer { z-index: 100; pointer-events: none; }

.device-unit {
  position: absolute;
  overflow: visible;
  background: transparent;
  border: 0;
  border-radius: 0;
  box-shadow: none;
  pointer-events: auto;
}
.device-unit.dragging { filter: drop-shadow(0 14px 28px rgba(0,0,0,.24)); }

.lab-board.is-interacting {
  cursor: grabbing;
}

.lab-board.is-interacting .device-host,
.device-unit.dragging .device-host,
.device-unit.resizing .device-host {
  pointer-events: none;
}

.device-layer,
.cable-layer,
.cable-overlay-layer,
.plug-layer {
  will-change: transform;
}

.device-host {
  width: 100%;
  height: 100%;
  display: block;
  border: 0;
  background: transparent;
  overflow: visible;
}


.device-unit,
.device-host,
.device-host *,
.coupling-unit,
.coupling-unit * {
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-rendering: geometricPrecision;
}

.device-unit,
.device-host {
  transform: translateZ(0);
  backface-visibility: hidden;
}

.device-host svg,
.device-host path,
.device-host line,
.device-host polyline,
.device-host circle,
.device-host rect,
.device-host text {
  shape-rendering: geometricPrecision;
  text-rendering: geometricPrecision;
}

.coupling-unit {
  border-radius: 18px;
  background: linear-gradient(180deg, #f4f8fb 0%, #dce5ed 100%);
  border: 1px solid rgba(112, 127, 140, 0.72);
  box-shadow: 0 12px 28px rgba(24, 38, 52, 0.20);
  overflow: visible;
}

.coupling-unit .move-handle {
  left: 8px;
  top: 6px;
  width: calc(100% - 16px);
  height: 18px;
  border: 0;
  border-radius: 0;
  background: transparent;
  box-shadow: none;
  color: #30404f;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  font-weight: 900;
}

.coupling-unit .move-handle:hover {
  color: #1f4f70;
}

.coupling-card {
  position: absolute;
  inset: 0;
  padding: 26px 8px 8px;
  display: grid;
}

.coupling-body {
  display: grid;
  grid-template-columns: 40px 1fr 40px;
  gap: 8px;
  align-items: center;
  justify-items: center;
}

.coupling-port {
  position: relative;
  width: 36px;
  height: 36px;
  border: none;
  border-radius: 50%;
  background: linear-gradient(180deg, #4f5a66 0%, #212a34 100%);
  box-shadow:
    inset 0 3px 8px rgba(255,255,255,0.14),
    0 8px 14px rgba(0,0,0,0.20);
}

.coupling-port::before {
  content: "";
  position: absolute;
  inset: 7px;
  border-radius: 50%;
  background:
    radial-gradient(circle at center,
      #c7ced5 0 14%,
      #616a73 15% 34%,
      #232a31 35% 70%,
      #0a0d10 71% 100%);
  box-shadow: inset 0 0 0 2px rgba(255,255,255,0.12);
}

.coupling-center {
  width: 100%;
  height: 34px;
  display: grid;
  place-items: center;
  border-radius: 12px;
  background: linear-gradient(180deg, #f5f9fc 0%, #cfd9e3 100%);
  border: 1px solid #b8c6d2;
  color: #30404f;
  font-size: 12px;
  font-weight: 900;
  box-shadow: inset 0 1px 0 rgba(255,255,255,0.85);
}

.coupling-port.connected {
  box-shadow:
    inset 0 3px 8px rgba(255,255,255,0.14),
    0 0 0 4px rgba(255,255,255,0.35),
    0 0 12px rgba(72, 186, 255, 0.45);
}

.coupling-unit > .coupling-port-overlay {
  opacity: 0 !important;
  background: transparent !important;
  border: none !important;
  box-shadow: none !important;
  width: 28px;
  height: 28px;
}

.move-handle {
  position: absolute;
  z-index: 30;
  left: -4px;
  top: -4px;
  width: 28px;
  height: 28px;
  border-radius: 999px;
  border: 1px solid rgba(16, 35, 51, .3);
  background: rgba(255,255,255,.88);
  box-shadow: 0 4px 12px rgba(0,0,0,.16);
  color: #24475d;
  font-weight: 900;
  cursor: grab;
  user-select: none;
}
.move-handle:active { cursor: grabbing; }

.device-resize-handle {
  display: none;
  position: absolute;
  z-index: 32;
  right: -6px;
  bottom: -6px;
  width: 22px;
  height: 22px;
  border: 1px solid rgba(16, 35, 51, .34);
  border-radius: 7px 3px 8px 3px;
  background:
    linear-gradient(135deg, transparent 0 45%, rgba(36, 71, 93, .42) 45% 52%, transparent 52%),
    linear-gradient(135deg, transparent 0 62%, rgba(36, 71, 93, .56) 62% 69%, transparent 69%),
    rgba(255,255,255,.9);
  box-shadow: 0 4px 12px rgba(0,0,0,.16);
  cursor: nwse-resize;
  user-select: none;
  touch-action: none;
}

.device-resize-handle:hover { background-color: #ffffff; }
.device-unit.resizing { filter: drop-shadow(0 14px 28px rgba(0,0,0,.24)); }

.port {
  position: absolute;
  z-index: 28;
  transform: translate(-50%, -50%);
  border-radius: 999px;
  border: 2px solid rgba(255,255,255,.95);
  box-shadow: 0 0 0 2px rgba(16,35,51,.42), 0 4px 10px rgba(0,0,0,.18);
  background: transparent;
  cursor: crosshair;
  opacity: .82;
  pointer-events: none;
}
.port-bnc { width: 24px; height: 24px; background: rgba(32,42,53,.28); }
.port-lwl { width: 20px; height: 20px; background: rgba(240,120,32,.32); }
.port-meter { width: 18px; height: 18px; background: rgba(31,111,210,.32); }
.coupling-port-overlay { width: 18px; height: 18px; }
.port.connected {
  opacity: 1;
  box-shadow: 0 0 0 3px rgba(15,138,82,.85), 0 4px 12px rgba(0,0,0,.22);
}
.port.snap-target {
  opacity: 1;
  animation: pulse .7s infinite alternate;
}
@keyframes pulse {
  from { box-shadow: 0 0 0 2px rgba(16,35,51,.35), 0 0 0 0 rgba(15,138,82,.28); }
  to { box-shadow: 0 0 0 2px rgba(16,35,51,.35), 0 0 0 10px rgba(15,138,82,.28); }
}

.cable-path {
  fill: none;
  stroke-linecap: round;
  stroke-linejoin: round;
  opacity: .86;
  filter: drop-shadow(0 2px 3px rgba(0,0,0,.18));
  pointer-events: none;
}
.cable-path.bnc { stroke: var(--bnc); stroke-width: 5.2; }
.cable-path.lwl { stroke: var(--lwl); stroke-width: 5.2; }
.cable-path.meter { stroke: var(--meter); stroke-width: 3.7; }
.cable-path.crossing-device {
  opacity: .34;
  filter: none;
  mix-blend-mode: multiply;
}
.cable-path.crossing-device.bnc { stroke-width: 3.2; }
.cable-path.crossing-device.lwl { stroke-width: 3.2; }
.cable-path.crossing-device.meter { stroke-width: 2.4; }
.cable-path.incomplete { opacity: .48; stroke-dasharray: 12 10; }
.cable-path.crossing-device.incomplete { opacity: .24; }

.cable-plug {
  pointer-events: auto;
  position: absolute;
  z-index: 110;
  width: 24px;
  height: 24px;
  border-radius: 999px;
  border: 3px solid white;
  box-shadow: 0 0 0 2px rgba(16,35,51,.38), 0 7px 16px rgba(0,0,0,.25);
  transform: translate(-50%, -50%);
  cursor: grab;
  touch-action: none;
}

.cable-plug:active { cursor: grabbing; }
.cable-plug.bnc { background: var(--bnc); }
.cable-plug.lwl { background: var(--lwl); }
.cable-plug.meter { background: var(--meter); }
.cable-plug.snapped::after {
  content: "";
  position: absolute;
  inset: -8px;
  border: 2px solid rgba(15,138,82,.65);
  border-radius: 999px;
}

.cable-shelf {
  position: sticky;
  top: 10px;
  align-self: start;
  background: rgba(255,255,255,.94);
  border: 1px solid #d6e0ea;
  border-radius: 16px;
  padding: 12px;
  box-shadow: 0 10px 24px rgba(16, 35, 51, .08);
}
.cable-shelf h2 { margin: 0 0 6px; font-size: 17px; color: #15384f; }
.cable-shelf p { color: var(--muted); font-size: 12px; line-height: 1.34; margin-bottom: 10px; }
.cable-tools { display: grid; gap: 7px; }
.cable-tools button,
.secondary {
  width: 100%;
  border: 0;
  border-radius: 12px;
  padding: 9px 10px;
  color: white;
  background: linear-gradient(180deg, #16799d, #0f5d78);
  font-weight: 800;
  cursor: pointer;
}
.secondary {
  margin-top: 7px;
  color: #173449;
  background: #e6eef6;
  border: 1px solid #cbd9e6;
}
.cable-list {
  margin: 10px 0 0;
  padding-left: 18px;
  display: grid;
  gap: 5px;
  max-height: 240px;
  overflow: auto;
  color: var(--muted);
  font-size: 12px;
}
.cable-list li.connected { color: var(--ok); font-weight: 700; }
.cable-list button {
  margin-left: 5px;
  border: 0;
  border-radius: 999px;
  width: 18px;
  height: 18px;
  background: #e64848;
  color: white;
  font-weight: 900;
  line-height: 1;
  cursor: pointer;
}

@media (max-width: 900px) {
  .workspace {
    grid-template-columns: 1fr;
    height: auto;
  }

  .lab-board {
    height: 72vh;
    min-height: 420px;
  }

  .cable-shelf {
    position: static;
  }
}


.coupling-unit > .coupling-port-overlay.connected,
.coupling-unit > .coupling-port-overlay.snap-target {
  opacity: 0 !important;
  box-shadow: none !important;
  animation: none !important;
}

.device-image-button {
  position: absolute;
  z-index: 31;
  left: 30px;
  top: -4px;
  width: 30px;
  height: 30px;
  padding: 0;
  border-radius: 10px;
  border: 1px solid rgba(16, 35, 51, .16);
  background: rgba(255,255,255,.94);
  box-shadow: 0 6px 16px rgba(0,0,0,.12);
  color: #24475d;
  cursor: pointer;
  user-select: none;
  display: grid;
  place-items: center;
  touch-action: manipulation;
  transition: transform .12s ease, box-shadow .18s ease, background-color .18s ease, border-color .18s ease;
}

.device-image-icon {
  width: 15px;
  height: 15px;
  display: block;
}

.device-image-icon path,
.device-image-icon circle {
  fill: none;
  stroke: currentColor;
  stroke-width: 1.75;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.device-image-button:hover,
.device-image-button:focus-visible {
  background: #fff;
  border-color: rgba(15, 106, 138, .28);
  box-shadow: 0 8px 18px rgba(0,0,0,.14);
  outline: 2px solid rgba(15, 106, 138, .22);
  outline-offset: 2px;
}

.device-image-button:active {
  transform: translateY(1px);
  box-shadow: 0 4px 10px rgba(0,0,0,.12);
}

.coupling-unit > .device-image-button {
  left: auto;
  right: -4px;
  top: -4px;
}

.device-image-modal[hidden] {
  display: none !important;
}

.device-image-modal {
  position: fixed;
  inset: 0;
  z-index: 10000;
  display: grid;
  place-items: center;
  padding: 22px;
}

.device-image-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(5, 15, 24, .62);
  backdrop-filter: blur(3px);
}

.device-image-dialog {
  position: relative;
  z-index: 1;
  width: min(960px, 94vw);
  max-height: 92vh;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  overflow: hidden;
  border-radius: 20px;
  border: 1px solid rgba(203, 217, 230, .95);
  background: rgba(255,255,255,.98);
  box-shadow: 0 28px 80px rgba(0, 0, 0, .38);
}

.device-image-header {
  display: flex;
  align-items: start;
  justify-content: space-between;
  gap: 18px;
  padding: 16px 18px;
  background: linear-gradient(180deg, #ffffff, #eef5fb);
  border-bottom: 1px solid #d6e0ea;
}

.device-image-header h2 {
  margin: 0 0 4px;
  color: #123a52;
  font-size: 19px;
}

.device-image-header p {
  margin: 0;
  color: var(--muted);
  font-size: 12px;
}

.device-image-close {
  flex: 0 0 auto;
  width: 34px;
  height: 34px;
  border: 0;
  border-radius: 999px;
  background: #e6eef6;
  color: #173449;
  font-size: 24px;
  font-weight: 900;
  line-height: 1;
  cursor: pointer;
}

.device-image-close:hover,
.device-image-close:focus-visible {
  background: #d5e4f1;
  outline: 2px solid rgba(15, 106, 138, .35);
  outline-offset: 2px;
}

.device-image-viewer {
  min-height: 260px;
  max-height: calc(92vh - 84px);
  padding: 18px;
  display: grid;
  place-items: center;
  background:
    linear-gradient(45deg, rgba(16,35,51,.045) 25%, transparent 25% 75%, rgba(16,35,51,.045) 75%),
    linear-gradient(45deg, rgba(16,35,51,.045) 25%, transparent 25% 75%, rgba(16,35,51,.045) 75%);
  background-position: 0 0, 12px 12px;
  background-size: 24px 24px;
}

.device-image-viewer img {
  max-width: 100%;
  max-height: calc(92vh - 150px);
  object-fit: contain;
  border-radius: 14px;
  box-shadow: 0 14px 36px rgba(16, 35, 51, .22);
  background: #fff;
}

.device-image-status {
  padding: 12px 14px;
  border-radius: 14px;
  background: rgba(255,255,255,.94);
  border: 1px solid #d6e0ea;
  color: #173449;
  text-align: center;
  line-height: 1.45;
  box-shadow: 0 10px 24px rgba(16, 35, 51, .10);
}

.device-image-status.is-error {
  color: #7a2d16;
  background: #fff7f2;
  border-color: #f2c6ad;
}

.device-image-status code {
  padding: 1px 5px;
  border-radius: 6px;
  background: rgba(16,35,51,.08);
}

body.device-image-modal-open {
  overflow: hidden;
}

.device-unit, .device-host { pointer-events: none; }
.device-unit .device-host { overflow: visible; }
.coupling-unit .coupling-card, .coupling-unit .coupling-card * { pointer-events: auto; }
.device-unit[hidden] { display: none !important; }
.device-drag-strip {
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 10%;
  min-height: 12px;
  z-index: 33;
  cursor: grab;
  touch-action: none;
  pointer-events: auto;
  background: transparent;
}
.device-drag-strip:active { cursor: grabbing; }
.device-remove-button {
  position: absolute;
  top: 3px;
  right: 3px;
  z-index: 44;
  pointer-events: auto;
  background: #fff6f4;
  color: #b7352c;
  font-size: 19px;
  font-weight: 800;
  border: 1px solid #dfaaa4;
  border-radius: 50%;
  width: 26px;
  height: 26px;
  padding: 0;
  line-height: 1;
  cursor: pointer;
  box-shadow: 0 2px 8px #17344924;
}
.device-remove-button:hover { background: #ffe0dd; }
.device-image-button { top: 3px; left: 3px; z-index: 40; pointer-events: auto; }
.coupling-unit > .device-image-button { left: 3px; right: auto; }
.device-unit .device-host:focus-within { outline: none; }

.workspace { grid-template-columns: minmax(0,1fr) 260px; }
.cable-shelf { max-height: calc(100vh - 92px); overflow-y: auto; }
.shelf-heading { display:flex; flex-wrap:wrap; justify-content:space-between; gap:6px; align-items:center; margin-bottom:6px; }
.shelf-heading h2 { margin:0; }
.settings-launch { background:#eaf2f8; border:1px solid #bad0df; border-radius:8px; cursor:pointer; font-size:12px; color:#164a69; padding:5px 8px; }
.cable-shelf h3 { color:#173449; border-top:1px solid #dae3eb; padding-top:10px; margin:14px 0 7px; font-size:13px; }
.shelf-items { display:grid; gap:6px; }
.shelf-device { text-align:left; width:100%; padding:8px; border:1px solid #c7d9e5; border-radius:8px; background:#eff5fa; color:#183e56; font-size:12px; font-weight:700; cursor:pointer; }
.shelf-device:hover { background:#dcecf8; }
.empty-shelf { font-size:12px; color:#7b8c99; }
.lock-symbol { font-size:12px; }
.cable-list { max-height:210px; }
.v8-dialog { width:min(430px,calc(100vw - 36px)); background:white; border:1px solid #c4d7e6; border-radius:16px; box-shadow:0 25px 80px #15293c5c; color:#173449; padding:22px; }
.v8-dialog::backdrop { background:#10223199; }
.v8-dialog h2 { font-size:19px; margin:0 0 10px; }
.v8-dialog p { line-height:1.45; color:#63798c; font-size:13px; margin:0 0 16px; }
.v8-dialog label { display:block; font-size:13px; font-weight:750; margin:10px 0 5px; }
.v8-dialog input { width:100%; padding:10px 12px; background:#f7fbff; border:1px solid #b8ccd9; border-radius:8px; font-size:15px; }
.v8-dialog input:focus { outline:2px solid #6aabc9; outline-offset:1px; }
.v8-dialog .dialog-actions { margin-top:18px; display:flex; gap:8px; justify-content:flex-end; }
.v8-dialog .dialog-actions button { border-radius:9px; border:1px solid #c3d5e0; padding:9px 12px; background:#edf3f8; color:#173449; font-weight:700; cursor:pointer; }
.v8-dialog .dialog-actions button.primary { color:white; background:#0f6a8a; border-color:#0f6a8a; }
.v8-dialog .form-feedback:not(:empty) { padding:7px 0; color:#a5322b; font-weight:750; margin:5px 0; }
.v8-dialog .security-note { margin-top:18px; font-size:11px; }
@media (max-width:900px) { .workspace {grid-template-columns:1fr;} .cable-shelf {max-height:none;} }

.scope-unit,
.scope-unit .device-host {
  transform: none;
  backface-visibility: visible;
}
