* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  min-height: 100vh;
  font-family: Inter, system-ui, sans-serif;
  background: #0b1220;
  color: #e2e8f0;
}

.topbar {
  border-bottom: 1px solid #334155;
  padding: 12px 20px;
  background: #111827;
}

.topbar h1 {
  margin: 0;
  font-size: 20px;
}

.topbar p {
  margin: 4px 0 0;
  color: #94a3b8;
  font-size: 13px;
}

.layout {
  display: grid;
  grid-template-columns: minmax(340px, 1fr) minmax(520px, 2fr);
  gap: 16px;
  padding: 16px;
  height: calc(100vh - 70px);
}

.panel {
  background: #111827;
  border: 1px solid #334155;
  border-radius: 12px;
  padding: 14px;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

h2 {
  margin: 0 0 10px;
  font-size: 16px;
  flex-shrink: 0;
}

.hint {
  margin: 0 0 8px;
  color: #94a3b8;
  font-size: 12px;
  flex-shrink: 0;
}

/* ---------- CodeMirror overrides ---------- */
.CodeMirror {
  width: 100%;
  min-height: 280px;
  flex: 1 1 auto;
  border-radius: 8px;
  border: 1px solid #475569;
  font-family: "JetBrains Mono", ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 13px;
  line-height: 1.5;
}

.CodeMirror-gutters {
  border-radius: 8px 0 0 8px;
}

/* fallback if CodeMirror not loaded */
#sourceCode {
  width: 100%;
  min-height: 280px;
  flex: 1 1 auto;
  resize: vertical;
  border-radius: 8px;
  border: 1px solid #475569;
  background: #020617;
  color: #f8fafc;
  font-family: "JetBrains Mono", ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 13px;
  line-height: 1.4;
  padding: 10px;
}

.actions {
  margin-top: 10px;
  display: flex;
  gap: 8px;
  flex-shrink: 0;
}

button {
  border: 0;
  border-radius: 8px;
  padding: 8px 12px;
  font-weight: 600;
  cursor: pointer;
  color: #0b1220;
  background: #22d3ee;
}

button.secondary {
  background: #cbd5e1;
}

.status {
  margin-top: 10px;
  font-size: 13px;
  flex-shrink: 0;
}

#log {
  min-height: 60px;
  max-height: 100px;
  margin: 10px 0 0;
  border-radius: 8px;
  border: 1px solid #1e293b;
  background: #020617;
  color: #a5f3fc;
  padding: 10px;
  overflow: auto;
  flex-shrink: 0;
  font-size: 12px;
}

/* ---------- Circuit toolbar ---------- */
.circuit-toolbar {
  display: flex;
  gap: 8px;
  justify-content: space-between;
  margin-bottom: 10px;
  flex-shrink: 0;
  flex-wrap: wrap;
}

.tool-group {
  display: flex;
  gap: 4px;
}

.tool-btn {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 5px 10px;
  font-size: 12px;
  font-weight: 500;
  border-radius: 6px;
  border: 1px solid #475569;
  background: #1e293b;
  color: #cbd5e1;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s;
}

.tool-btn:hover {
  background: #334155;
  border-color: #64748b;
}

.tool-btn.active {
  background: #0e7490;
  border-color: #22d3ee;
  color: #fff;
}

.tool-btn svg {
  flex-shrink: 0;
}

/* ---------- Circuit workspace ---------- */
.circuit-workspace {
  display: flex;
  gap: 10px;
  flex: 1 1 auto;
  min-height: 0;
}

/* ---------- Component palette ---------- */
.component-palette {
  width: 130px;
  flex-shrink: 0;
  background: #0f172a;
  border: 1px solid #334155;
  border-radius: 10px;
  padding: 10px;
  overflow-y: auto;
}

.component-palette h3 {
  margin: 0 0 10px;
  font-size: 12px;
  color: #94a3b8;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.palette-item {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 8px;
  border-radius: 6px;
  cursor: grab;
  font-size: 11px;
  color: #e2e8f0;
  transition: background 0.15s;
  margin-bottom: 4px;
  user-select: none;
}

.palette-item:hover {
  background: #1e293b;
}

.palette-item:active {
  cursor: grabbing;
}

.palette-icon {
  width: 28px;
  height: 28px;
  border-radius: 6px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 13px;
  flex-shrink: 0;
}

.palette-icon--arduino { background: #0284c7; color: #fff; }
.palette-icon--led-red { background: #ef4444; border-radius: 50%; box-shadow: 0 0 6px #ef4444; }
.palette-icon--led-green { background: #22c55e; border-radius: 50%; box-shadow: 0 0 6px #22c55e; }
.palette-icon--led-yellow { background: #eab308; border-radius: 50%; box-shadow: 0 0 6px #eab308; }
.palette-icon--led-blue { background: #3b82f6; border-radius: 50%; box-shadow: 0 0 6px #3b82f6; }
.palette-icon--resistor { background: #78716c; color: #fff; }
.palette-icon--button { background: #6366f1; color: #fff; }
.palette-icon--buzzer { background: #a855f7; color: #fff; }
.palette-icon--servo { background: #f97316; color: #fff; }
.palette-icon--pot { background: #14b8a6; color: #fff; }
.palette-icon--lcd { background: #065f46; color: #6ee7b7; font-size: 9px; }
.palette-icon--neo { background: linear-gradient(135deg, #ef4444, #22c55e, #3b82f6); border-radius: 50%; }

/* ---------- Canvas container ---------- */
.canvas-container {
  flex: 1 1 auto;
  min-width: 0;
  background: #f8fafc;
  border-radius: 10px;
  border: 1px solid #e2e8f0;
  overflow: auto;
  position: relative;
}

.canvas-scroll {
  position: relative;
  width: 3000px;
  height: 3000px;
  min-width: 100%;
  min-height: 100%;
}

#componentsLayer {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 1;
}

#wiresOverlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 2;
  pointer-events: none;
}

/* ---------- Placed components ---------- */
.placed-component {
  position: absolute;
  cursor: move;
  user-select: none;
  z-index: 10;
}

.placed-component.selected {
  outline: 2px dashed #3b82f6;
  outline-offset: 4px;
  border-radius: 4px;
}

.placed-component .comp-label {
  position: absolute;
  bottom: -18px;
  left: 50%;
  transform: translateX(-50%);
  font-size: 10px;
  color: #475569;
  white-space: nowrap;
  pointer-events: none;
}

/* ---------- Pin indicators ---------- */
.pin-dot {
  pointer-events: auto;
  cursor: crosshair;
  transition: r 0.1s;
}

.pin-dot:hover {
  r: 6;
}

/* ---------- Wires ---------- */
.wire-line {
  pointer-events: auto;
  stroke: #ef4444;
  stroke-width: 2.5;
  stroke-linecap: round;
  stroke-linejoin: round;
  fill: none;
  cursor: pointer;
  transition: stroke 0.15s;
}

.wire-line:hover {
  stroke: #f97316;
  stroke-width: 3.5;
}

.wire-line.selected {
  stroke: #3b82f6;
  stroke-width: 3.5;
  stroke-dasharray: 6,3;
}

/* ---------- Responsive ---------- */
@media (max-width: 980px) {
  .layout {
    grid-template-columns: 1fr;
    height: auto;
  }

  .circuit-workspace {
    flex-direction: column;
  }

  .component-palette {
    width: 100%;
    flex-direction: row;
    flex-wrap: wrap;
    display: flex;
    gap: 4px;
  }

  .component-palette h3 {
    width: 100%;
  }

  .canvas-container {
    min-height: 500px;
  }
}
