:root {
  --bg: #050816;
  --bg-elevated: #0b1020;
  --bg-elevated-soft: #10172f;
  --border-subtle: rgba(255, 255, 255, 0.08);
  --accent: #38bdf8;
  --accent-soft: rgba(56, 189, 248, 0.1);
  --accent-strong: #0ea5e9;
  --danger: #fb7185;
  --text: #e5e7eb;
  --text-soft: #9ca3af;
  --grid-line: rgba(148, 163, 184, 0.25);
  --pin: #facc15;
  --component-body: #1f2937;
  --wire: #e5e7eb;
  --wire-active: #38bdf8;
  --led-off: #1f2937;
  --led-on: #22c55e;
  --bulb-on: #facc15;
  --transition-fast: 150ms ease-out;
  --transition-med: 220ms ease-out;
  --radius-lg: 16px;
  --radius-md: 10px;
  --radius-sm: 999px;
  --shadow-soft: 0 18px 40px rgba(15, 23, 42, 0.9);
  --shadow-subtle: 0 10px 25px rgba(15, 23, 42, 0.8);
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
  height: 100%;
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  color: var(--text);
  background: radial-gradient(circle at top, #0f172a 0, var(--bg) 55%, #020617 100%);
}

body {
  display: flex;
}

.app-root {
  display: grid;
  grid-template-columns: 280px minmax(0, 1fr);
  grid-template-rows: 1fr;
  width: 100%;
  height: 100vh;
  max-width: none;
  margin: 0;
  padding: 12px;
  gap: 12px;
  min-height: 0;
}

@media (max-width: 900px) {
  .app-root {
    grid-template-columns: 1fr;
  }
}

.palette {
  background: linear-gradient(145deg, #020617, #020617 45%, #020617 100%);
  border-radius: var(--radius-lg);
  padding: 20px 18px 18px;
  box-shadow: var(--shadow-soft);
  border: 1px solid var(--border-subtle);
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.app-title {
  margin: 0;
  font-size: 1.4rem;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: #e5e7eb;
}

.app-subtitle {
  margin: 4px 0 0;
  font-size: 0.82rem;
  color: var(--text-soft);
}

.palette-section {
  margin-top: 14px;
  padding-top: 12px;
  border-top: 1px solid var(--border-subtle);
}

.palette-section-small {
  padding-top: 10px;
}

.palette-heading {
  margin: 0 0 8px;
  font-size: 0.9rem;
  font-weight: 600;
  color: var(--text-soft);
  text-transform: uppercase;
  letter-spacing: 0.08em;
}

.palette-details {
  margin-top: 10px;
  border: none;
}

.palette-details:first-of-type {
  margin-top: 0;
}

.palette-summary {
  font-size: 0.88rem;
  font-weight: 600;
  color: var(--text);
  cursor: pointer;
  list-style: none;
  padding: 6px 0;
  user-select: none;
}

.palette-summary::-webkit-details-marker {
  display: none;
}

.palette-summary::before {
  content: "▸ ";
  opacity: 0.7;
}

.palette-details[open] .palette-summary::before {
  content: "▾ ";
}

.palette-subheading {
  font-size: 0.75rem;
  color: var(--text-soft);
  margin: 8px 0 4px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.palette-buttons {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  margin-bottom: 4px;
}

.palette-item,
.secondary-button {
  appearance: none;
  border: 1px solid var(--border-subtle);
  border-radius: 999px;
  padding: 7px 12px;
  font-size: 0.85rem;
  background: rgba(15, 23, 42, 0.9);
  color: var(--text);
  cursor: pointer;
  margin: 2px 4px 4px 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  transition:
    background var(--transition-fast),
    border-color var(--transition-fast),
    transform var(--transition-fast),
    box-shadow var(--transition-fast),
    color var(--transition-fast);
}

.palette-item:hover,
.secondary-button:hover {
  background: var(--accent-soft);
  border-color: rgba(56, 189, 248, 0.6);
  box-shadow: 0 0 0 1px rgba(56, 189, 248, 0.3);
  transform: translateY(-1px);
}

.palette-item:active,
.secondary-button:active {
  transform: translateY(0);
  box-shadow: none;
}

.palette-item-disabled,
.palette-item-disabled:hover,
.palette-item-disabled:active {
  opacity: 0.4;
  cursor: default;
  transform: none;
  box-shadow: none;
  background: rgba(15, 23, 42, 0.9);
  border-color: var(--border-subtle);
}

.palette-item[data-active="true"] {
  background: var(--accent-strong);
  border-color: transparent;
  color: #0b1120;
  box-shadow: 0 0 0 1px rgba(15, 23, 42, 0.6), 0 12px 30px rgba(56, 189, 248, 0.45);
}

.secondary-button {
  width: 100%;
  justify-content: center;
  background: rgba(15, 23, 42, 0.95);
}

.board-container {
  background: rgba(15, 23, 42, 0.9);
  border-radius: var(--radius-lg);
  padding: 14px 14px 12px;
  box-shadow: var(--shadow-soft);
  border: 1px solid var(--border-subtle);
  display: flex;
  flex-direction: column;
  flex: 1;
  min-height: 0;
  gap: 10px;
}

.board-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 6px 4px 4px;
}

.board-title-group {
  display: flex;
  flex-direction: column;
}

.board-title {
  margin: 0;
  font-size: 1.05rem;
}

.board-hint {
  margin: 2px 0 0;
  font-size: 0.8rem;
  color: var(--text-soft);
}

.board-controls {
  display: flex;
  align-items: center;
  gap: 8px;
}

.board-run-button {
  padding-inline: 14px;
  font-size: 0.8rem;
  white-space: nowrap;
}

.board-zoom-controls {
  display: flex;
  align-items: center;
  gap: 4px;
}

.board-zoom-btn {
  width: 28px;
  height: 28px;
  padding: 0;
  font-size: 1.1rem;
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--radius-md);
}

.chip {
  border-radius: 999px;
  padding: 4px 10px;
  font-size: 0.8rem;
  border: 1px solid var(--border-subtle);
  background: rgba(15, 23, 42, 0.95);
  color: var(--text-soft);
}

.board-wrapper {
  position: relative;
  flex: 1;
  min-height: 0;
  border-radius: 16px;
  overflow: hidden;
  border: 1px solid var(--border-subtle);
  background: radial-gradient(circle at top left, #020617 0, #020617 40%, #020617 100%);
}

.board-svg {
  display: block;
  width: 100%;
  height: 100%;
  background: radial-gradient(circle at top, #020617 0, #020617 55%, #020617 100%);
  touch-action: none;
  cursor: default;
}

.io-panel {
  margin-top: 4px;
}

.io-panel details {
  border-radius: 12px;
  background: rgba(15, 23, 42, 0.95);
  border: 1px solid var(--border-subtle);
}

.io-panel summary {
  padding: 6px 10px;
  font-size: 0.8rem;
  cursor: pointer;
  color: var(--text-soft);
}

.io-panel-body {
  padding: 0 10px 10px;
}

.component-info {
  margin-top: 8px;
  padding: 8px 10px;
  border-radius: 12px;
  background: rgba(15, 23, 42, 0.95);
  border: 1px solid var(--border-subtle);
}

.component-info-title {
  margin: 0 0 4px;
  font-size: 0.9rem;
  font-weight: 600;
  color: var(--text-soft);
}

.component-info-body {
  font-size: 0.8rem;
  color: var(--text-soft);
}

.component-info-empty {
  margin: 2px 0 0;
}

.component-info-row {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  margin: 2px 0;
}

.component-info-row span {
  opacity: 0.7;
}

.component-info-row strong {
  font-weight: 600;
}

.component-info-slider {
  align-items: center;
}

.component-info-range {
  width: 100px;
  accent-color: var(--accent);
}

.component-info-select {
  min-width: 120px;
  padding: 4px 8px;
  border-radius: var(--radius-md);
  border: 1px solid var(--border-subtle);
  background: var(--bg-elevated);
  color: var(--text);
  font-size: 0.8rem;
  accent-color: var(--accent);
}

#json-textarea {
  width: 100%;
  min-height: 120px;
  border-radius: 10px;
  border: 1px solid var(--border-subtle);
  background: #020617;
  color: var(--text-soft);
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
  font-size: 0.8rem;
  padding: 8px;
  resize: vertical;
}

#json-textarea:focus {
  outline: none;
  border-color: rgba(56, 189, 248, 0.7);
  box-shadow: 0 0 0 1px rgba(56, 189, 248, 0.5);
}

.pin-hit-target {
  cursor: crosshair;
}

.pin-label {
  font-size: 11px;
  fill: var(--text-soft);
  pointer-events: none;
  user-select: none;
}

.junction-blob {
  filter: drop-shadow(0 0 2px rgba(0, 0, 0, 0.5));
}

.component-hit-target {
  cursor: grab;
}

.component-hit-target:active {
  cursor: grabbing;
}

.component-selected {
  filter: drop-shadow(0 0 10px rgba(56, 189, 248, 0.8));
}

.wire {
  stroke: var(--wire);
  stroke-width: 4;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.wire-hot {
  stroke: var(--wire-active);
}

.wire-selected {
  stroke-width: 4;
  stroke-dasharray: 6 4;
}

.wire-pos {
  stroke: #ef4444; /* red for positive */
}

.wire-neg {
  stroke: #3b82f6; /* blue for negative */
}

.wire-live.wire-pos {
  filter: drop-shadow(0 0 6px rgba(239, 68, 68, 0.9));
  stroke-width: 5;
}

.wire-live.wire-neg {
  filter: drop-shadow(0 0 6px rgba(59, 130, 246, 0.9));
  stroke-width: 5;
}

/* LED fill/stroke/filter set in JS per ledColor (white, red, blue, green, yellow) */
.led-shape {
  stroke-width: 1.6;
  transition:
    fill var(--transition-med),
    stroke var(--transition-med),
    filter var(--transition-med);
}

.led-broken {
  fill: #111827;
  stroke: #f97316;
  filter: drop-shadow(0 0 8px rgba(248, 113, 113, 0.9));
}

.bulb-shape {
  fill: #020617;
  stroke: #6b7280;
  stroke-width: 1.5;
  transition:
    fill var(--transition-med),
    stroke var(--transition-med),
    filter var(--transition-med);
}

.bulb-on {
  fill: var(--bulb-on);
  stroke: #fed7aa;
  filter: drop-shadow(0 0 10px rgba(250, 204, 21, 0.95));
}

.bulb-broken {
  fill: #111827;
  stroke: #f97316;
  filter: drop-shadow(0 0 8px rgba(248, 113, 113, 0.9));
}

.smoke {
  fill: rgba(148, 163, 184, 0.5);
  filter: blur(2px);
}

.switch-lever {
  transform-origin: center;
  transition: transform var(--transition-med);
}

.battery-body-live {
  stroke: #38bdf8;
  filter: drop-shadow(0 0 12px rgba(56, 189, 248, 0.9));
}

.hidden {
  display: none !important;
}

