:root {
  --bg: #0e1116;
  --panel: #161b22;
  --panel-2: #1c2330;
  --line: #2a3341;
  --text: #e8edf5;
  --muted: #8b97a8;
  --accent: #e8a317;
  --accent-dim: rgba(232, 163, 23, 0.16);
  --live: #2fd67b;
  --live-dim: rgba(47, 214, 123, 0.18);
  --danger: #e85d4c;
  --wire: #9eb0c7;
  --frame: #243044;
  --shadow: 0 12px 40px rgba(0, 0, 0, 0.45);
  --radius: 10px;
  --font: "DM Sans", system-ui, sans-serif;
  --mono: "JetBrains Mono", ui-monospace, monospace;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: var(--font);
  color: var(--text);
  background:
    radial-gradient(1200px 600px at 10% -10%, rgba(232, 163, 23, 0.07), transparent 55%),
    radial-gradient(900px 500px at 100% 0%, rgba(47, 214, 123, 0.05), transparent 50%),
    var(--bg);
  overflow: hidden;
}

#app {
  display: grid;
  grid-template-columns: 280px minmax(0, 1fr) 260px;
  grid-template-rows: 58px minmax(0, 1fr);
  height: 100vh;
  gap: 0;
}

.topbar {
  grid-column: 1 / -1;
  display: flex;
  align-items: center;
  gap: 1rem;
  padding: 0 0.9rem;
  background: linear-gradient(180deg, #1a212c, #141a22);
  border-bottom: 1px solid var(--line);
  z-index: 5;
}

.brand { display: flex; align-items: center; gap: 0.65rem; min-width: 160px; }
.brand-mark {
  width: 28px; height: 28px; border-radius: 7px;
  background:
    linear-gradient(135deg, var(--accent), #c47b00 55%, #8a5600);
  box-shadow: inset 0 0 0 1px rgba(255,255,255,0.18), 0 0 0 1px rgba(0,0,0,0.35);
  position: relative;
}
.brand-mark::after {
  content: "";
  position: absolute; inset: 7px 6px 7px 13px;
  border-left: 2px solid rgba(20, 16, 8, 0.85);
  border-right: 2px solid rgba(20, 16, 8, 0.85);
}
.brand-text { display: flex; flex-direction: column; line-height: 1.05; }
.brand-text strong { font-size: 0.95rem; letter-spacing: 0.02em; }
.brand-text span { font-size: 0.72rem; color: var(--muted); text-transform: uppercase; letter-spacing: 0.12em; }

.top-actions {
  display: flex; flex-wrap: wrap; align-items: center; gap: 0.55rem;
  flex: 1;
}
.project-meta input {
  width: min(220px, 28vw);
  background: var(--panel-2);
  border: 1px solid var(--line);
  color: var(--text);
  border-radius: 8px;
  padding: 0.4rem 0.65rem;
  font: 500 0.85rem var(--font);
}

.field { display: flex; align-items: center; gap: 0.4rem; font-size: 0.75rem; color: var(--muted); }
.field select, .btn, .props input, .props select, .sidebar input {
  font: 500 0.82rem var(--font);
  color: var(--text);
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 8px;
}
.field select { padding: 0.38rem 0.5rem; }

.btn-group { display: inline-flex; gap: 0.28rem; }
.btn {
  padding: 0.4rem 0.7rem;
  cursor: pointer;
  transition: background .15s, border-color .15s, color .15s, transform .1s;
}
.btn:hover { border-color: #3d4b60; background: #222b38; }
.btn:active { transform: translateY(1px); }
.btn.active { background: var(--accent-dim); border-color: var(--accent); color: #ffd98a; }
.btn-sim { background: var(--live-dim); border-color: #2a8a55; color: #9af0c0; }
.btn-sim.active, body.simulating .btn-sim { background: rgba(47,214,123,.28); border-color: var(--live); }

.sidebar, .props {
  background: var(--panel);
  border-right: 1px solid var(--line);
  overflow: auto;
}
.props { border-right: 0; border-left: 1px solid var(--line); padding: 0.85rem; }
.props h2, .sidebar-head h1 {
  margin: 0 0 0.65rem;
  font-size: 0.72rem;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--muted);
  font-weight: 600;
}
.sidebar-head { padding: 0.85rem 0.85rem 0.4rem; position: sticky; top: 0; background: var(--panel); z-index: 2; }
.sidebar-head input { width: 100%; padding: 0.5rem 0.65rem; margin-bottom: 0.5rem; }
.categories { padding: 0 0.55rem 1rem; }

.cat { margin-bottom: 0.45rem; border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; background: var(--panel-2); }
.cat summary {
  list-style: none; cursor: pointer; padding: 0.55rem 0.7rem;
  font-size: 0.8rem; font-weight: 600; display: flex; justify-content: space-between; align-items: center;
  user-select: none;
}
.cat summary::-webkit-details-marker { display: none; }
.cat summary::after { content: "+"; color: var(--muted); font-family: var(--mono); }
.cat[open] summary::after { content: "–"; }
.cat-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.4rem;
  padding: 0 0.55rem 0.65rem;
}
.sym-card {
  display: flex; flex-direction: column; align-items: center; gap: 0.25rem;
  padding: 0.45rem 0.3rem 0.4rem;
  border: 1px solid transparent;
  border-radius: 8px;
  background: rgba(255,255,255,0.02);
  cursor: grab;
  color: var(--text);
  transition: border-color .15s, background .15s;
}
.sym-card:hover, .sym-card.active { border-color: var(--accent); background: var(--accent-dim); }
.sym-card svg { width: 54px; height: 36px; pointer-events: none; }
.sym-card span { font-size: 0.68rem; color: var(--muted); text-align: center; line-height: 1.2; }

.viewport-wrap {
  display: grid;
  grid-template-rows: minmax(0, 1fr) 32px;
  min-width: 0;
  min-height: 0;
  padding: 0.75rem 0.75rem 0;
}
.viewport-frame {
  min-height: 0;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  border: 1px solid var(--frame);
  border-radius: 14px;
  overflow: hidden;
  background:
    linear-gradient(180deg, #121820, #0c1016 40%),
    var(--bg);
  box-shadow: var(--shadow), inset 0 0 0 1px rgba(255,255,255,0.03);
}
.viewport-chrome {
  display: flex; align-items: center; gap: 0.45rem;
  padding: 0.45rem 0.7rem;
  background: #18202b;
  border-bottom: 1px solid var(--line);
  font-size: 0.72rem;
  color: var(--muted);
}
.viewport-chrome .dot {
  width: 8px; height: 8px; border-radius: 50%;
  background: #3a4556;
}
.viewport-chrome .dot:nth-child(1) { background: #e85d4c; }
.viewport-chrome .dot:nth-child(2) { background: var(--accent); }
.viewport-chrome .dot:nth-child(3) { background: var(--live); }
.chrome-label { font-family: var(--mono); color: #c9d4e4; margin-left: 0.35rem; }
.chrome-hint { margin-left: auto; opacity: 0.75; }

.canvas-host { position: relative; min-height: 0; overflow: hidden; cursor: crosshair; }
#board { width: 100%; height: 100%; display: block; background: #0b0f14; outline: none; }
#board.panning { cursor: grabbing; }
#board.wiring { cursor: crosshair; }

.symbol-node { cursor: move; }
.symbol-node .sym-body { stroke: #d7e0ec; fill: none; stroke-width: 1.7; }
.symbol-node .sym-fill { fill: #0b0f14; stroke: #d7e0ec; stroke-width: 1.7; }
.symbol-node .sym-text { fill: #c5d0de; font-family: var(--mono); font-size: 10px; }
.symbol-node.selected .hit { stroke: var(--accent); stroke-width: 1.4; stroke-dasharray: 4 3; }
.symbol-node.live .sym-body,
.symbol-node.live .sym-fill { stroke: var(--live); }
.symbol-node.live .glow { fill: var(--live); opacity: 0.35; }
.symbol-node .pin {
  fill: #0b0f14; stroke: #9eb0c7; stroke-width: 1.4; cursor: crosshair;
}
.symbol-node .pin:hover, .symbol-node .pin.hot { stroke: var(--accent); fill: var(--accent-dim); }

.wire-path { fill: none; stroke: var(--wire); stroke-width: 1.8; stroke-linecap: square; stroke-linejoin: miter; pointer-events: stroke; cursor: pointer; }
.wire-path.selected { stroke: var(--accent); stroke-width: 2.4; }
.wire-path.live { stroke: var(--live); filter: drop-shadow(0 0 3px rgba(47,214,123,.55)); }
.wire-path.preview { stroke: var(--accent); stroke-dasharray: 5 4; opacity: 0.85; pointer-events: none; }

.statusbar {
  display: flex; gap: 1.2rem; align-items: center;
  padding: 0 0.35rem;
  font-family: var(--mono);
  font-size: 0.7rem;
  color: var(--muted);
}
.statusbar span:last-child { margin-left: auto; }

.props-body { display: grid; gap: 0.65rem; }
.props label { display: grid; gap: 0.28rem; font-size: 0.75rem; color: var(--muted); }
.props input, .props select { padding: 0.45rem 0.55rem; width: 100%; }
.props .muted { color: var(--muted); font-size: 0.82rem; margin: 0; }
.kv { display: flex; justify-content: space-between; gap: 0.5rem; font-size: 0.78rem; padding: 0.35rem 0; border-bottom: 1px solid var(--line); }
.kv b { font-weight: 500; color: var(--text); font-family: var(--mono); font-size: 0.74rem; }

body.simulating .viewport-frame { box-shadow: var(--shadow), 0 0 0 1px rgba(47,214,123,.35); }

@media (max-width: 1100px) {
  #app { grid-template-columns: 240px minmax(0, 1fr); }
  .props { display: none; }
}
@media (max-width: 820px) {
  #app { grid-template-columns: 1fr; grid-template-rows: 58px auto minmax(0, 1fr); }
  .sidebar { max-height: 38vh; border-right: 0; border-bottom: 1px solid var(--line); }
  .chrome-hint { display: none; }
}
