/* ─────────────────────────────────────────────
   Sparky editor — main stylesheet
   A light shell around a dark 3D viewport.
───────────────────────────────────────────── */

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --shell:        #fbfaf7;
  --shell-2:      #f3f1ec;
  --shell-3:      #ebe8e1;
  --line:         #e6e2d9;
  --line-2:       #d6d0c4;
  --ink:          #1b1915;
  --ink-2:        #57524a;
  --ink-3:        #8c867c;
  --accent:       #d97706;
  --accent-hover: #b45309;
  --accent-soft:  #fdf2e2;
  --go:           #16a34a;
  --go-hover:     #15803d;
  --stop:         #dc2626;
  --canvas:       #23262b;
  --glass:        rgba(22, 24, 29, 0.74);
  --glass-line:   rgba(255, 255, 255, 0.10);
  --glass-ink:    #eef0f3;
  --glass-ink-2:  #a9afb9;
  --topbar-h:     56px;
  --sidebar-w:    236px;
  --radius:       10px;
  --shadow-1:     0 1px 2px rgba(27, 25, 21, 0.06);
  --shadow-2:     0 6px 24px rgba(0, 0, 0, 0.18);
  --focus:        0 0 0 3px rgba(217, 119, 6, 0.28);
}

html, body {
  height: 100%;
  overflow: hidden;
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  font-size: 13px;
  color: var(--ink);
  background: var(--canvas);
  -webkit-font-smoothing: antialiased;
}

button { font-family: inherit; }
:focus-visible { outline: none; box-shadow: var(--focus); }

/* ── APP SHELL ──────────────────────────────── */

#app { display: flex; flex-direction: column; height: 100vh; }

/* ── TOP BAR ────────────────────────────────── */

#topbar {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  height: var(--topbar-h);
  padding: 0 14px;
  background: var(--shell);
  border-bottom: 1px solid var(--line);
  flex-shrink: 0;
  z-index: 50;
}

.topbar-left-group { display: flex; align-items: center; gap: 10px; min-width: 0; }

.topbar-brand {
  display: flex;
  align-items: center;
  gap: 9px;
  font-family: 'Space Grotesk', sans-serif;
  font-weight: 700;
  font-size: 15px;
  letter-spacing: -0.3px;
  color: var(--ink);
  text-decoration: none;
  white-space: nowrap;
  flex-shrink: 0;
  border-radius: 8px;
  padding: 3px 4px 3px 3px;
}
.brand-mark {
  width: 26px;
  height: 26px;
  border-radius: 7px;
  display: grid;
  place-items: center;
  color: #fff;
  background: linear-gradient(145deg, #f59e0b, #d97706);
  box-shadow: 0 1px 0 rgba(255,255,255,0.3) inset, 0 2px 6px rgba(217,119,6,0.35);
}
.topbar-name-sep { color: var(--line-2); font-size: 16px; flex-shrink: 0; }

#circuit-name-field {
  font-size: 13px;
  font-weight: 500;
  color: var(--ink);
  outline: none;
  border: 1px solid transparent;
  border-radius: 6px;
  padding: 4px 8px;
  min-width: 60px;
  max-width: 240px;
  white-space: nowrap;
  overflow: hidden;
  cursor: text;
  transition: border-color 0.15s, background 0.15s;
}
#circuit-name-field:hover { border-color: var(--line); background: var(--shell-2); }
#circuit-name-field:focus { border-color: var(--accent); background: #fff; box-shadow: var(--focus); }

.topbar-center { display: flex; align-items: center; gap: 10px; }
.topbar-actions { display: flex; align-items: center; gap: 6px; justify-content: flex-end; }
.topbar-divider { width: 1px; height: 22px; background: var(--line); margin: 0 4px; }

/* segmented tool switch */
.seg {
  display: inline-flex;
  padding: 3px;
  gap: 2px;
  background: var(--shell-2);
  border: 1px solid var(--line);
  border-radius: 9px;
}
.mode-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 5px 11px;
  border: none;
  background: transparent;
  border-radius: 6px;
  font-size: 12.5px;
  font-weight: 500;
  color: var(--ink-2);
  cursor: pointer;
  transition: background 0.12s, color 0.12s, box-shadow 0.12s;
}
.mode-btn:hover { color: var(--ink); }
.mode-btn.active { background: #fff; color: var(--ink); box-shadow: var(--shadow-1), 0 0 0 1px var(--line); }

.icon-btn {
  width: 32px;
  height: 32px;
  display: grid;
  place-items: center;
  border: 1px solid transparent;
  background: transparent;
  border-radius: 8px;
  color: var(--ink-2);
  cursor: pointer;
  transition: background 0.12s, color 0.12s, border-color 0.12s;
}
.icon-btn:hover { background: var(--shell-2); border-color: var(--line); color: var(--ink); }

.btn-secondary {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  height: 32px;
  padding: 0 12px;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 8px;
  font-size: 12.5px;
  font-weight: 500;
  color: var(--ink);
  cursor: pointer;
  box-shadow: var(--shadow-1);
  transition: background 0.12s, border-color 0.12s;
}
.btn-secondary:hover { background: var(--shell-2); border-color: var(--line-2); }

.btn-danger {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 5px 12px;
  background: transparent;
  border: 1px solid var(--line-2);
  border-radius: 8px;
  font-size: 12px;
  color: var(--stop);
  cursor: pointer;
}
.btn-danger:hover { background: #fff1f1; border-color: var(--stop); }

.btn-simulate, .btn-stop {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  height: 34px;
  padding: 0 16px;
  border: none;
  border-radius: 999px;
  font-family: 'Space Grotesk', sans-serif;
  font-size: 13px;
  font-weight: 600;
  color: #fff;
  cursor: pointer;
  transition: background 0.12s, transform 0.1s, box-shadow 0.15s;
}
.btn-simulate { background: var(--go); box-shadow: 0 2px 8px rgba(22,163,74,0.28), 0 1px 0 rgba(255,255,255,0.25) inset; }
.btn-simulate:hover { background: var(--go-hover); }
.btn-simulate:active, .btn-stop:active { transform: scale(0.97); }
.btn-stop { background: var(--stop); box-shadow: 0 2px 8px rgba(220,38,38,0.3); }
.btn-stop:hover { background: #b91c1c; }

/* ── BODY ───────────────────────────────────── */

#body { display: flex; flex: 1; overflow: hidden; }

/* ── PARTS LIBRARY ──────────────────────────── */

#sidebar {
  width: var(--sidebar-w);
  background: var(--shell);
  border-right: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 12px 10px;
  overflow-y: auto;
  flex-shrink: 0;
}

.sidebar-section-label {
  font-size: 10.5px;
  font-weight: 600;
  letter-spacing: 0.7px;
  text-transform: uppercase;
  color: var(--ink-3);
  padding: 4px 6px 6px;
}

.comp-item {
  display: flex;
  align-items: center;
  gap: 11px;
  width: 100%;
  padding: 6px;
  background: transparent;
  border: 1px solid transparent;
  border-radius: 11px;
  cursor: pointer;
  text-align: left;
  transition: background 0.12s, border-color 0.12s, transform 0.12s;
}
.comp-item:hover { background: var(--shell-2); border-color: var(--line); }
.comp-item:active { transform: scale(0.99); }
.comp-item.active { background: var(--accent-soft); border-color: rgba(217,119,6,0.45); }

.comp-item-icon {
  width: 46px;
  height: 46px;
  border-radius: 9px;
  flex-shrink: 0;
  display: grid;
  place-items: center;
  background: radial-gradient(120% 120% at 30% 20%, #3a3e46 0%, #24272d 70%);
  border: 1px solid rgba(0,0,0,0.25);
  box-shadow: 0 1px 0 rgba(255,255,255,0.06) inset;
  overflow: hidden;
}
.comp-item-icon img { width: 100%; height: 100%; display: block; object-fit: contain; }
.comp-item.active .comp-item-icon { box-shadow: 0 0 0 2px rgba(217,119,6,0.55); }

.comp-item-text { display: flex; flex-direction: column; min-width: 0; }
.comp-item-name { font-size: 13px; font-weight: 600; color: var(--ink); }

.sidebar-divider { height: 1px; background: var(--line); margin: 6px 0; }

.color-swatches { display: flex; gap: 7px; flex-wrap: wrap; padding: 4px 8px 8px 63px; }
.swatch {
  width: 20px;
  height: 20px;
  border-radius: 50%;
  border: 2px solid #fff;
  box-shadow: 0 0 0 1px var(--line-2);
  cursor: pointer;
  transition: transform 0.1s, box-shadow 0.1s;
}
.swatch:hover { transform: scale(1.12); }
.swatch.active { box-shadow: 0 0 0 2px var(--ink); }


kbd {
  display: inline-block;
  min-width: 22px;
  padding: 1px 5px;
  font-family: inherit;
  font-size: 10px;
  font-weight: 600;
  text-align: center;
  background: #fff;
  border: 1px solid var(--line-2);
  border-bottom-width: 2px;
  border-radius: 5px;
  color: var(--ink);
}

/* ── CANVAS ─────────────────────────────────── */

#canvas-wrap { flex: 1; position: relative; overflow: hidden; background: var(--canvas); }
canvas {
  display: block;
  position: absolute;
  inset: 0;
  touch-action: none;    /* gestures belong to the editor, not the page */
}

/* Overlays on the viewport are dark glass, so they sit on the scene. */
.glass {
  background: var(--glass);
  border: 1px solid var(--glass-line);
  color: var(--glass-ink);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
}

#hint-box {
  position: absolute;
  top: 14px;
  left: 50%;
  transform: translateX(-50%);
  max-width: calc(100% - 40px);
  padding: 7px 14px;
  font-size: 12px;
  color: var(--glass-ink);
  background: var(--glass);
  border: 1px solid var(--glass-line);
  border-radius: 999px;
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  pointer-events: none;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  transition: opacity 0.2s;
}
.hint-hidden { opacity: 0; }
.sim-open #hint-box { left: 14px; transform: none; max-width: calc(100% - 336px); }

#hole-label {
  position: absolute;
  bottom: 14px;
  left: 14px;
  padding: 5px 10px;
  font: 500 11.5px ui-monospace, 'SF Mono', Menlo, Consolas, monospace;
  color: var(--glass-ink);
  background: var(--glass);
  border: 1px solid var(--glass-line);
  border-radius: 8px;
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  pointer-events: none;
  display: none;
}

#rotate-badge {
  position: absolute;
  bottom: 14px;
  left: 50%;
  transform: translateX(-50%);
  padding: 5px 10px;
  font-size: 11px;
  color: var(--glass-ink-2);
  background: var(--glass);
  border: 1px solid var(--glass-line);
  border-radius: 999px;
  pointer-events: none;
  display: none;
}
#rotate-badge kbd { background: rgba(255,255,255,0.1); color: var(--glass-ink); border-color: rgba(255,255,255,0.18); }

.view-tools {
  position: absolute;
  bottom: 14px;
  right: 14px;
  display: flex;
  gap: 8px;
}
#clear-all-btn, #reset-cam-btn {
  display: flex;
  align-items: center;
  gap: 6px;
  height: 30px;
  padding: 0 11px;
  font-size: 11.5px;
  font-weight: 500;
  color: var(--glass-ink);
  background: var(--glass);
  border: 1px solid var(--glass-line);
  border-radius: 8px;
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  cursor: pointer;
  transition: background 0.12s, color 0.12s, border-color 0.12s;
}
#reset-cam-btn:hover { background: rgba(40,44,52,0.85); }
#clear-all-btn { color: #fca5a5; }
#clear-all-btn:hover { background: rgba(127,29,29,0.55); border-color: rgba(252,165,165,0.35); color: #fee2e2; }

/* ── SIMULATION RESULTS ─────────────────────── */

#sim-results {
  position: absolute;
  top: 14px;
  right: 14px;
  width: 290px;
  max-height: calc(100% - 80px);
  overflow-y: auto;
  padding: 10px 14px 12px;
  font-size: 12px;
  line-height: 1.55;
  color: var(--glass-ink);
  background: var(--glass);
  border: 1px solid var(--glass-line);
  border-radius: 12px;
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  box-shadow: var(--shadow-2);
  display: none;
}
.sim-head {
  display: flex;
  align-items: center;
  gap: 8px;
  font-family: 'Space Grotesk', sans-serif;
  font-weight: 600;
  font-size: 12px;
  letter-spacing: 0.3px;
  text-transform: uppercase;
  color: var(--glass-ink-2);
  padding-bottom: 7px;
  margin-bottom: 6px;
  border-bottom: 1px solid var(--glass-line);
}
.sim-dot { width: 8px; height: 8px; border-radius: 50%; background: #22c55e; box-shadow: 0 0 0 0 rgba(34,197,94,0.6); animation: sim-pulse 1.6s infinite; }
@keyframes sim-pulse { 0% { box-shadow: 0 0 0 0 rgba(34,197,94,0.55); } 70% { box-shadow: 0 0 0 7px rgba(34,197,94,0); } 100% { box-shadow: 0 0 0 0 rgba(34,197,94,0); } }
.sim-line { padding: 2px 0; }
.sim-info { color: var(--glass-ink); }
.sim-path { color: var(--glass-ink-2); padding-left: 8px; }
.sim-on   { color: #4ade80; font-weight: 600; }
.sim-warn { color: #fbbf24; }
.sim-err  { color: #f87171; font-weight: 600; }

/* ── LOAD PREVIEW MODAL ─────────────────────── */

#load-preview-modal {
  position: fixed;
  inset: 0;
  background: rgba(20,18,15,0.5);
  backdrop-filter: blur(4px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 200;
}
.lpm-card {
  background: var(--shell);
  border: 1px solid var(--line);
  border-radius: 14px;
  box-shadow: 0 16px 48px rgba(0,0,0,0.28);
  width: 360px;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}
.lpm-thumb { width: 100%; aspect-ratio: 16/9; background: var(--canvas); display: flex; align-items: center; justify-content: center; overflow: hidden; }
.lpm-thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
#lpm-placeholder { font-size: 12px; color: #8b919b; }
.lpm-name { font-family: 'Space Grotesk', sans-serif; font-size: 16px; font-weight: 700; padding: 14px 16px 2px; letter-spacing: -0.2px; }
.lpm-meta { font-size: 12px; color: var(--ink-3); padding: 0 16px 14px; }
.lpm-btns { display: flex; gap: 8px; padding: 0 16px 16px; }
.lpm-btns .btn-simulate, .lpm-btns .btn-secondary { flex: 1; justify-content: center; }

/* ── SCROLLBARS ─────────────────────────────── */

::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--line-2); border-radius: 3px; }

/* ── RESPONSIVE ─────────────────────────────── */
/* Below this width the side panels no longer fit beside the canvas, so they
   stack under it. The canvas is the product: it keeps whatever height the
   stacked panels do not need. */
@media (max-width: 1100px) {
  .btn-secondary { padding: 0 9px; }
  .seg .mode-btn { padding: 5px 9px; }
}

@media (max-width: 1024px) and (min-height: 600px) {
  #body { flex-direction: column; }
  #canvas-wrap { order: -1; flex: 1 1 auto; min-height: 0; }
  #sidebar {
    width: 100%;
    flex-direction: row;
    align-items: center;
    gap: 6px;
    padding: 8px;
    border-right: none;
    border-top: 1px solid var(--line);
    overflow-x: auto;
    overflow-y: hidden;
  }
  .comp-item { width: auto; flex-shrink: 0; padding: 5px 10px 5px 5px; }
  .comp-item-icon { width: 38px; height: 38px; }
  #wire-color-row { flex-shrink: 0; }
  .color-swatches { flex-wrap: nowrap; padding: 0 6px; }
  .sidebar-section-label, .sidebar-divider { display: none; }
}

@media (max-width: 760px) {
  #topbar { grid-template-columns: auto 1fr auto; padding: 0 10px; gap: 8px; }
  .topbar-name-sep, #circuit-name-field, .topbar-divider, .topbar-actions .btn-secondary { display: none; }
  .seg { display: none; }
  .comp-item-text { display: none; }
  .comp-item { padding: 5px; }
}

/* status marks for simulator lines */
.sim-line.sim-on::before, .sim-line.sim-warn::before, .sim-line.sim-err::before {
  content: '';
  display: inline-block;
  width: 7px;
  height: 7px;
  border-radius: 50%;
  margin-right: 7px;
  vertical-align: 1px;
}
.sim-line.sim-on::before   { background: #4ade80; box-shadow: 0 0 8px rgba(74,222,128,0.7); }
.sim-line.sim-warn::before { background: #fbbf24; }
.sim-line.sim-err::before  { background: #f87171; }

/* ── INSPECTOR (selected part) ──────────────── */
#inspector {
  position: absolute;
  top: 14px;
  left: 14px;
  width: 280px;
  padding: 12px 14px;
  color: var(--glass-ink);
  background: var(--glass);
  border: 1px solid var(--glass-line);
  border-radius: 12px;
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  box-shadow: var(--shadow-2);
  display: none;
  z-index: 5;
}
.insp-head { display: flex; flex-direction: column; gap: 2px; }
.insp-title { font-family: 'Space Grotesk', sans-serif; font-weight: 700; font-size: 14px; }
.insp-id { font: 500 11px ui-monospace, 'SF Mono', Menlo, Consolas, monospace; color: var(--glass-ink-2); }
.insp-about { font-size: 12px; line-height: 1.5; color: var(--glass-ink-2); margin: 8px 0 4px; }
.insp-label { font-size: 10.5px; font-weight: 600; letter-spacing: 0.6px; text-transform: uppercase; color: var(--glass-ink-2); margin: 10px 0 6px; }
.insp-chips { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.insp-chip {
  height: 26px;
  padding: 0 9px;
  border-radius: 7px;
  border: 1px solid var(--glass-line);
  background: rgba(255,255,255,0.06);
  color: var(--glass-ink);
  font-size: 11.5px;
  font-weight: 500;
  cursor: pointer;
}
.insp-chip:hover { background: rgba(255,255,255,0.12); }
.insp-chip.on { background: var(--accent); border-color: var(--accent); color: #fff; }
.insp-swatch {
  width: 22px;
  height: 22px;
  border-radius: 50%;
  border: 2px solid rgba(255,255,255,0.18);
  background: var(--sw);
  cursor: pointer;
}
.insp-swatch.on { border-color: #fff; box-shadow: 0 0 0 2px var(--accent); }
.insp-note { font-size: 11px; color: var(--glass-ink-2); margin-left: 4px; }
.insp-actions { display: flex; justify-content: flex-end; margin-top: 12px; }
.insp-delete {
  height: 28px;
  padding: 0 11px;
  border-radius: 7px;
  border: 1px solid rgba(248,113,113,0.35);
  background: rgba(127,29,29,0.25);
  color: #fecaca;
  font-size: 11.5px;
  font-weight: 600;
  cursor: pointer;
}
.insp-delete:hover { background: rgba(127,29,29,0.5); }

/* ── EMPTY BOARD ─────────────────────────────── */
#empty-state {
  position: absolute;
  left: 50%;
  bottom: 64px;
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  gap: 14px;
  max-width: calc(100% - 40px);
  padding: 10px 10px 10px 18px;
  white-space: nowrap;
  color: var(--glass-ink);
  background: var(--glass);
  border: 1px solid var(--glass-line);
  border-radius: 14px;
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
}
.empty-title { font-family: 'Space Grotesk', sans-serif; font-weight: 700; font-size: 15px; }
#empty-state .btn-secondary { margin: 0; }
