:root {
  --bg: #09090b;
  --panel: #111114;
  --panel-2: #18181c;
  --border: #27272a;
  --text: #f4f4f5;
  --muted: #a1a1aa;
  --accent: #38bdf8;
  --error: #ef4444;
  --warning: #f59e0b;
  --info: #60a5fa;
  --ok: #22c55e;

  --pin-power: #ef4444;
  --pin-ground: #94a3b8;
  --pin-signal: #38bdf8;
  --pin-analog: #a78bfa;
  --pin-motor: #f97316;
  --pin-passive: #d4d4d8;

  --cat-controller: #38bdf8;
  --cat-actuator: #f97316;
  --cat-sensor: #22c55e;
  --cat-power: #ef4444;
  --cat-passive: #a1a1aa;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 14px;
}

button, input { font: inherit; color: inherit; }

/* ── Layout ─────────────────────────────────────────────────── */

.robowire-shell {
  display: grid;
  grid-template-columns: 240px 1fr 340px;
  grid-template-rows: auto 1fr;
  grid-template-areas: "top top top" "side canvas panel";
  height: 100vh;
  overflow: hidden;
}

.toolbar {
  grid-area: top;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 12px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
}

.brand { font-weight: 700; font-size: 16px; white-space: nowrap; }
.logo { color: var(--accent); }

.project-name {
  background: transparent;
  border: 1px solid transparent;
  border-radius: 6px;
  padding: 5px 8px;
  min-width: 160px;
  flex: 0 1 220px;
}
.project-name:hover, .project-name:focus { border-color: var(--border); outline: none; background: var(--panel-2); }

.tool-group { display: flex; gap: 4px; }
.stats { margin-left: auto; display: flex; gap: 14px; color: var(--muted); white-space: nowrap; }
.stats strong { color: var(--text); }

.btn {
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 5px 10px;
  cursor: pointer;
  line-height: 1.2;
}
.btn:hover:not(:disabled) { border-color: #3f3f46; background: #222227; }
.btn:disabled { opacity: 0.4; cursor: default; }
.btn.icon { width: 32px; padding: 5px 0; text-align: center; }
.btn.danger { color: #fca5a5; border-color: #7f1d1d; margin-top: 12px; }
.btn.danger:hover { background: #2a1111; }

/* ── Sidebar ────────────────────────────────────────────────── */

.sidebar {
  grid-area: side;
  display: flex;
  flex-direction: column;
  background: var(--panel);
  border-right: 1px solid var(--border);
  min-height: 0;
}

.search {
  margin: 10px;
  padding: 7px 10px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--panel-2);
}
.search:focus { outline: none; border-color: var(--accent); }

.component-list { flex: 1; overflow-y: auto; padding: 0 10px 10px; }

.cat-title {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
  margin: 14px 2px 6px;
}

.card {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px;
  margin-bottom: 6px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--panel-2);
  cursor: grab;
  user-select: none;
}
.card:hover { border-color: #3f3f46; }
.card:active { cursor: grabbing; }
.card-name { font-weight: 600; font-size: 13px; }
.card-sub { font-size: 11px; color: var(--muted); }

.sidebar-hint { margin: 0; padding: 10px; font-size: 12px; color: var(--muted); border-top: 1px solid var(--border); }

/* ── Canvas ─────────────────────────────────────────────────── */

.canvas {
  grid-area: canvas;
  position: relative;
  overflow: hidden;
  cursor: grab;
  background-color: var(--bg);
  background-image: radial-gradient(circle, #27272a 1px, transparent 1px);
  touch-action: none;
}
.canvas.panning { cursor: grabbing; }
.canvas.wiring-mode, .canvas.wiring-mode * { cursor: crosshair !important; }

.world { position: absolute; left: 0; top: 0; transform-origin: 0 0; }

.wires {
  position: absolute;
  left: 0;
  top: 0;
  width: 1px;
  height: 1px;
  overflow: visible;
  pointer-events: none;
}

.wire-line { fill: none; stroke-width: 2.5; stroke-linecap: round; }
.wire-hit { fill: none; stroke: transparent; stroke-width: 14; pointer-events: stroke; cursor: pointer; }
.wire.selected .wire-line { stroke-width: 4; filter: drop-shadow(0 0 4px rgba(255, 255, 255, 0.6)); }
.wire.issue-warning .wire-line { stroke-dasharray: 8 5; }
.wire.issue-error .wire-line { stroke: var(--error) !important; stroke-dasharray: 8 5; animation: dash 0.8s linear infinite; }
@keyframes dash { to { stroke-dashoffset: -26; } }

#temp-wire { fill: none; stroke: var(--accent); stroke-width: 2; stroke-dasharray: 6 4; pointer-events: none; }

.node {
  position: absolute;
  min-width: 150px;
  background: var(--panel);
  border: 1.5px solid var(--border);
  border-radius: 10px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.4);
  user-select: none;
  cursor: move;
}
.node.selected { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(56, 189, 248, 0.25), 0 4px 16px rgba(0, 0, 0, 0.4); }
.node.issue-warning { border-color: var(--warning); }
.node.issue-error { border-color: var(--error); }

.node-head {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 7px 10px;
  border-bottom: 1px solid var(--border);
}
.node-name { font-weight: 600; font-size: 13px; white-space: nowrap; }

.node-icon {
  flex: none;
  font-size: 10px;
  font-weight: 700;
  font-family: ui-monospace, Menlo, monospace;
  padding: 3px 5px;
  border-radius: 4px;
  min-width: 36px;
  text-align: center;
  background: rgba(255, 255, 255, 0.06);
}
.cat-controller .node-icon { color: var(--cat-controller); }
.cat-actuator .node-icon { color: var(--cat-actuator); }
.cat-sensor .node-icon { color: var(--cat-sensor); }
.cat-power .node-icon { color: var(--cat-power); }
.cat-passive .node-icon { color: var(--cat-passive); }
.node.cat-controller { border-top: 3px solid var(--cat-controller); }
.node.cat-actuator { border-top: 3px solid var(--cat-actuator); }
.node.cat-sensor { border-top: 3px solid var(--cat-sensor); }
.node.cat-power { border-top: 3px solid var(--cat-power); }
.node.cat-passive { border-top: 3px solid var(--cat-passive); }

.node-body { display: flex; justify-content: space-between; gap: 16px; padding: 6px 0; }
.pins { display: flex; flex-direction: column; }
.pins:empty { display: none; }

.pin {
  display: flex;
  align-items: center;
  gap: 6px;
  height: 18px;
  font-size: 11px;
  color: #d4d4d8;
  white-space: nowrap;
}
.pin-left { margin-left: -6px; padding-right: 8px; }
.pin-right { flex-direction: row-reverse; margin-right: -6px; padding-left: 8px; }
.pin.hover .dot { transform: scale(1.5); box-shadow: 0 0 0 3px rgba(255, 255, 255, 0.25); }

.dot {
  display: inline-block;
  width: 11px;
  height: 11px;
  border-radius: 50%;
  border: 2px solid var(--bg);
  background: var(--pin-signal);
  cursor: crosshair;
  transition: transform 0.1s;
  flex: none;
}
.pin .dot:hover { transform: scale(1.4); }
.dot.inline { width: 9px; height: 9px; margin-right: 5px; vertical-align: middle; cursor: default; }
.type-power { background: var(--pin-power); }
.type-ground { background: var(--pin-ground); }
.type-gpio, .type-pwm, .type-trigger, .type-echo { background: var(--pin-signal); }
.type-analog { background: var(--pin-analog); }
.type-motor { background: var(--pin-motor); }
.type-passive { background: var(--pin-passive); }

.empty-hint {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: var(--muted);
  pointer-events: none;
  text-align: center;
}
.empty-hint[hidden] { display: none; }
.empty-hint h2 { color: var(--text); margin: 0 0 6px; }

.zoom-controls, .legend {
  position: absolute;
  bottom: 12px;
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  cursor: default;
}
.zoom-controls { left: 12px; }
#zoom-level { min-width: 42px; text-align: center; font-size: 12px; color: var(--muted); }
.legend { right: 12px; gap: 12px; font-size: 11px; color: var(--muted); padding: 8px 10px; }
.legend span { display: flex; align-items: center; gap: 5px; }
.legend .dot { width: 9px; height: 9px; border: 0; cursor: default; }

/* ── Right panel ────────────────────────────────────────────── */

.panel {
  grid-area: panel;
  display: flex;
  flex-direction: column;
  background: var(--panel);
  border-left: 1px solid var(--border);
  min-height: 0;
}

.tabs { display: flex; border-bottom: 1px solid var(--border); }
.tab {
  flex: 1;
  background: none;
  border: 0;
  border-bottom: 2px solid transparent;
  padding: 10px 4px;
  cursor: pointer;
  color: var(--muted);
}
.tab:hover { color: var(--text); }
.tab.active { color: var(--text); border-bottom-color: var(--accent); }

.badge { display: inline-block; min-width: 18px; padding: 0 5px; border-radius: 9px; font-size: 11px; line-height: 18px; color: #fff; }
.badge.ok { background: #14532d; }
.badge.warn { background: #92400e; }
.badge.bad { background: #991b1b; }

.panel-body { flex: 1; overflow-y: auto; padding: 12px; }
.muted { color: var(--muted); }
.pad { padding: 8px; }

.all-good { padding: 12px; border-radius: 8px; background: rgba(34, 197, 94, 0.1); color: #86efac; border: 1px solid #14532d; }

.issue {
  display: block;
  width: 100%;
  text-align: left;
  padding: 10px;
  margin-bottom: 8px;
  border-radius: 8px;
  border: 1px solid var(--border);
  border-left-width: 4px;
  background: var(--panel-2);
  cursor: pointer;
}
.issue:hover { background: #1f1f24; }
.issue .sev { display: block; font-size: 10px; text-transform: uppercase; letter-spacing: 0.06em; font-weight: 700; margin-bottom: 3px; }
.issue .msg { display: block; }
.issue .tip { display: block; margin-top: 4px; font-size: 12px; color: var(--muted); }
.sev-error { border-left-color: var(--error); }
.sev-error .sev { color: var(--error); }
.sev-warning { border-left-color: var(--warning); }
.sev-warning .sev { color: var(--warning); }
.sev-info { border-left-color: var(--info); }
.sev-info .sev { color: var(--info); }

.wiring { margin: 0; padding-left: 22px; }
.wiring li { padding: 6px 0; border-bottom: 1px solid var(--border); cursor: pointer; font-size: 13px; }
.wiring li:hover { color: var(--accent); }
.wiring .arrow { color: var(--muted); margin: 0 4px; }
.swatch { display: inline-block; width: 10px; height: 10px; border-radius: 2px; margin-right: 6px; vertical-align: middle; }

.code-actions { display: flex; gap: 6px; margin-bottom: 8px; }
.code {
  margin: 0;
  padding: 10px;
  border-radius: 8px;
  background: #050507;
  border: 1px solid var(--border);
  font-size: 12px;
  line-height: 1.5;
  overflow-x: auto;
  white-space: pre;
}

.field { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--muted); }
.field input { padding: 6px 8px; border-radius: 6px; border: 1px solid var(--border); background: var(--panel-2); font-size: 14px; }
.field input:focus { outline: none; border-color: var(--accent); }
.part-title { margin: 14px 0 4px; font-size: 15px; }
.panel-body p { margin: 6px 0; line-height: 1.45; }

.pin-table { width: 100%; border-collapse: collapse; margin-top: 10px; font-size: 12px; }
.pin-table th { text-align: left; color: var(--muted); font-weight: 500; border-bottom: 1px solid var(--border); padding: 4px; }
.pin-table td { padding: 3px 4px; border-bottom: 1px solid #1c1c20; }

.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translate(-50%, 20px);
  background: var(--panel-2);
  border: 1px solid var(--border);
  padding: 8px 14px;
  border-radius: 8px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s, transform 0.2s;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ── Small screens ──────────────────────────────────────────── */

@media (max-width: 900px) {
  .robowire-shell {
    grid-template-columns: 1fr;
    grid-template-rows: auto 160px 1fr 260px;
    grid-template-areas: "top" "side" "canvas" "panel";
  }
  .sidebar { border-right: 0; border-bottom: 1px solid var(--border); }
  .sidebar-hint { display: none; }
  .panel { border-left: 0; border-top: 1px solid var(--border); }
  .legend { display: none; }
  .stats { margin-left: 0; }
}
