/* ============================================================
   PerfBoard Studio — styles
   ============================================================ */

:root {
  --bg: #0b0e13;
  --panel: #141821;
  --panel-2: #1a1f2b;
  --panel-3: #212735;
  --border: #27303f;
  --border-soft: #1e2530;
  --text: #e8ecf3;
  --muted: #8b95a8;
  --faint: #5c6678;
  --accent: #f5a83c;
  --accent-soft: rgba(245, 168, 60, 0.14);
  --accent-2: #ffc86b;
  --mono: 'JetBrains Mono', ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  --sans: 'Inter', system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  font-family: var(--sans);
  font-size: 14px;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}

.mono { font-family: var(--mono); }

::selection { background: rgba(245, 168, 60, 0.35); }

button { font-family: inherit; }

/* ---------- App shell ---------- */

.app {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  min-height: 0;
}

/* ---------- Topbar ---------- */

.topbar {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 10px 20px;
  background: linear-gradient(180deg, #161b26, #10141d);
  border-bottom: 1px solid var(--border);
}

.brand { display: flex; align-items: center; gap: 12px; }

.brand svg { flex: 0 0 auto; filter: drop-shadow(0 2px 6px rgba(245,168,60,.25)); }

.brand-text h1 {
  margin: 0;
  font-size: 15px;
  font-weight: 700;
  letter-spacing: 0.02em;
}

.brand-text h1 span { color: var(--accent); }

.brand-text p {
  margin: 1px 0 0;
  font-size: 11px;
  color: var(--muted);
  letter-spacing: 0.06em;
  text-transform: uppercase;
}

.topbar-right { display: flex; align-items: center; gap: 12px; }

.unit-badge {
  font-family: var(--mono);
  font-size: 11px;
  color: var(--accent-2);
  border: 1px solid rgba(245, 168, 60, 0.35);
  background: var(--accent-soft);
  padding: 3px 8px;
  border-radius: 6px;
}

.top-stats {
  font-size: 12px;
  color: var(--muted);
  white-space: nowrap;
}

/* ---------- Layout ---------- */

.layout {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: 330px 1fr;
}

/* ---------- Sidebar ---------- */

.sidebar {
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  background: var(--panel);
  border-right: 1px solid var(--border);
}

.sidebar::-webkit-scrollbar { width: 10px; }
.sidebar::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 8px;
  border: 3px solid var(--panel);
}
.sidebar::-webkit-scrollbar-track { background: transparent; }

.card {
  background: var(--panel-2);
  border: 1px solid var(--border-soft);
  border-radius: 12px;
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.card-title {
  margin: 0;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--muted);
  display: flex;
  align-items: center;
  gap: 8px;
}

.card-title .idx {
  font-family: var(--mono);
  font-size: 10px;
  color: var(--accent);
  background: var(--accent-soft);
  border: 1px solid rgba(245, 168, 60, 0.25);
  border-radius: 5px;
  padding: 1px 5px;
}

/* ---------- Fields ---------- */

.field-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.field { display: flex; flex-direction: column; gap: 6px; }

.field-label {
  font-size: 12px;
  color: var(--muted);
  display: flex;
  align-items: baseline;
  gap: 6px;
}

.field-label em {
  font-style: normal;
  font-size: 10px;
  color: var(--faint);
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

.input-wrap {
  display: flex;
  align-items: center;
  gap: 6px;
  background: var(--panel-3);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 6px 10px;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

.input-wrap:focus-within {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(245, 168, 60, 0.15);
}

.input-wrap input {
  width: 100%;
  min-width: 0;
  background: transparent;
  border: none;
  outline: none;
  color: var(--text);
  font-family: var(--mono);
  font-size: 13px;
  padding: 0;
}

.suffix {
  font-size: 10px;
  color: var(--faint);
  font-family: var(--mono);
  text-transform: uppercase;
  white-space: nowrap;
}

/* ---------- Presets ---------- */

.presets { display: flex; flex-direction: column; gap: 8px; }

.preset-label {
  font-size: 11px;
  color: var(--faint);
  text-transform: uppercase;
  letter-spacing: 0.08em;
}

.chip-row { display: flex; flex-wrap: wrap; gap: 6px; }

.chip {
  font-family: var(--mono);
  font-size: 12px;
  color: var(--muted);
  background: var(--panel-3);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 5px 10px;
  cursor: pointer;
  transition: all 0.15s ease;
}

.chip:hover {
  color: var(--text);
  border-color: var(--faint);
  transform: translateY(-1px);
}

.chip.active {
  color: #141414;
  background: var(--accent);
  border-color: var(--accent);
  font-weight: 600;
  box-shadow: 0 2px 8px rgba(245, 168, 60, 0.35);
}

/* ---------- Board orientation toolbar (floating, top-right of board) ---------- */

.board-tools {
  position: absolute;
  top: 16px;
  right: 16px;
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 3px;
  padding: 5px;
  background: rgba(20, 24, 33, 0.86);
  border: 1px solid var(--border);
  border-radius: 10px;
  backdrop-filter: blur(6px);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.45);
  /* Only the buttons themselves capture pointer events, so hover
     tooltips and drag-routing pass through the toolbar's padding/gaps. */
  pointer-events: none;
}

.tbtn-group {
  display: flex;
  align-items: center;
  gap: 3px;
}

.tbtn-sep {
  width: 1px;
  height: 20px;
  background: var(--border);
  margin: 0 3px;
}

.tbtn {
  width: 32px;
  height: 32px;
  display: grid;
  place-items: center;
  font-size: 15px;
  line-height: 1;
  color: var(--muted);
  background: transparent;
  border: 1px solid transparent;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.15s ease;
  pointer-events: auto;
}

.tbtn:hover {
  color: var(--accent-2);
  background: var(--accent-soft);
  border-color: rgba(245, 168, 60, 0.35);
  transform: translateY(-1px);
}

.tbtn:active {
  transform: translateY(0) scale(0.94);
}

.tbtn:focus-visible {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(245, 168, 60, 0.25);
}

/* Zoom percentage readout: a clickable reset button, a touch wider than
   the icon buttons so "100%" fits without the label jumping around. */
.tbtn.zoom-pct {
  width: auto;
  min-width: 46px;
  padding: 0 6px;
  font-family: var(--mono);
  font-size: 11px;
  font-weight: 600;
  letter-spacing: -0.2px;
}

/* Undo / redo: grey out while there's nothing to step back/forward to */
.tbtn:disabled {
  color: var(--muted-dim, #5a6372);
  opacity: 0.45;
  cursor: default;
  pointer-events: none;
  transform: none;
  box-shadow: none;
  background: transparent;
  border-color: transparent;
}

/* Bottom-right twin of the top-right orientation toolbar, anchored to the
   board stage so it floats over the board rather than the status footer. */
.board-history {
  top: auto;
  bottom: 16px;
  right: 16px;
}

/* ---------- Swatches ---------- */

.swatches {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.swatch {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--panel-3);
  border: 1px solid var(--border);
  border-radius: 9px;
  color: var(--muted);
  font-size: 12px;
  padding: 8px 10px;
  cursor: pointer;
  transition: all 0.15s ease;
}

.swatch i {
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: var(--c);
  border: 2px solid rgba(255, 255, 255, 0.18);
  box-shadow: inset 0 -2px 4px rgba(0, 0, 0, 0.25);
}

.swatch:hover {
  color: var(--text);
  border-color: var(--faint);
  transform: translateY(-1px);
}

.swatch.active {
  color: var(--text);
  border-color: var(--accent);
  box-shadow: 0 0 0 1px var(--accent);
}

/* ---------- Toggle rows ---------- */

.toggle-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  font-size: 13px;
  color: var(--text);
  cursor: pointer;
  padding: 2px 0;
}

.hint-inline {
  display: block;
  font-style: normal;
  font-size: 10px;
  color: var(--faint);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin-top: 1px;
}

.switch {
  position: relative;
  flex: 0 0 auto;
  width: 40px;
  height: 22px;
}

.switch input {
  position: absolute;
  opacity: 0;
  width: 100%;
  height: 100%;
  margin: 0;
  cursor: pointer;
  z-index: 1;
}

.switch i {
  position: absolute;
  inset: 0;
  background: var(--panel-3);
  border: 1px solid var(--border);
  border-radius: 999px;
  transition: background 0.18s ease, border-color 0.18s ease;
  pointer-events: none;
}

.switch i::after {
  content: '';
  position: absolute;
  top: 2px;
  left: 2px;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: var(--muted);
  transition: transform 0.18s cubic-bezier(0.2, 0.9, 0.3, 1.2), background 0.18s ease;
}

.switch input:checked + i {
  background: var(--accent-soft);
  border-color: var(--accent);
}

.switch input:checked + i::after {
  transform: translateX(18px);
  background: var(--accent);
}

.switch input:focus-visible + i { box-shadow: 0 0 0 3px rgba(245, 168, 60, 0.25); }

/* ---------- Placement tool switch ---------- */

.tool-switch {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 4px;
  padding: 4px;
  background: var(--panel-3);
  border: 1px solid var(--border);
  border-radius: 10px;
}

.tool-btn {
  font-family: inherit;
  font-size: 12px;
  font-weight: 600;
  color: var(--muted);
  background: transparent;
  border: 1px solid transparent;
  border-radius: 7px;
  padding: 6px 8px;
  cursor: pointer;
  transition: all 0.15s ease;
}

.tool-btn:hover { color: var(--text); }

.tool-btn.active {
  color: #141414;
  background: var(--accent);
  box-shadow: 0 1px 6px rgba(245, 168, 60, 0.35);
}

.tool-btn:focus-visible {
  outline: none;
  box-shadow: 0 0 0 3px rgba(245, 168, 60, 0.25);
}

/* Routed wires, pin headers and resize handles: never intercept pointer
   events (hit-testing is manual). */
#wires, #headers, #handles, #preview { pointer-events: none; }

.wire, .header { transition: filter 0.12s ease, opacity 0.12s ease; }
.wire.hl, .header.hl { filter: brightness(1.5) drop-shadow(0 0 3px rgba(255, 255, 255, 0.4)); }

/* Resize handles on the selected wire/header: a white ring with a dark
   core so they read as grab points over pads, solder and plastic alike. */
.handle {
  animation: handleIn 0.16s ease;
  transform-box: fill-box;
  transform-origin: center;
}
.handle circle {
  transition: r 0.12s ease, opacity 0.12s ease;
}
@keyframes handleIn {
  from { opacity: 0; transform: scale(0.6); }
  to { opacity: 1; transform: scale(1); }
}

.wire-label {
  font-family: var(--mono);
  font-size: 1.3px;
  font-weight: 600;
  fill: #fff;
  stroke: rgba(8, 10, 14, 0.85);
  stroke-width: 0.45px;
  stroke-linejoin: round;
  paint-order: stroke;
}

#board { cursor: crosshair; touch-action: none; }

/* ---------- Reset / notes ---------- */

.reset-btn {
  display: block;
  width: 100%;
  font-size: 13px;
  font-weight: 600;
  color: var(--muted);
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px;
  cursor: pointer;
  transition: all 0.15s ease;
}

.reset-btn:hover {
  color: var(--accent-2);
  border-color: rgba(245, 168, 60, 0.4);
  background: var(--accent-soft);
}

.sidebar-note {
  margin: 0;
  font-size: 11px;
  color: var(--faint);
  text-align: center;
  line-height: 1.5;
}

/* ---------- Board area ---------- */

.board-area {
  position: relative;
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  background-color: #0a0d11;
  background-image: radial-gradient(rgba(255, 255, 255, 0.05) 1px, transparent 1.4px);
  background-size: 22px 22px;
  overflow: hidden;
}

.board-area::before {
  content: '';
  position: absolute;
  inset: 0;
  pointer-events: none;
  background: radial-gradient(60% 50% at 50% 42%, rgba(245, 168, 60, 0.05), transparent 70%);
}

.board-stage {
  position: relative; /* anchors the bottom-right undo/redo toolbar */
  flex: 1;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 26px 26px 10px;
}

#board {
  display: block;
  width: 100%;
  height: 100%;
  filter: drop-shadow(0 26px 42px rgba(0, 0, 0, 0.55));
}

/* Pads: hover micro-interaction */
.pad { cursor: pointer; }

.padcopper, .padhole {
  transform-box: fill-box;
  transform-origin: center;
  transition: transform 0.12s ease;
}

.pad:hover .padcopper { transform: scale(1.08); filter: brightness(1.28); }
.pad:hover .padhole { transform: scale(1.08); }

#pads { animation: pop 0.42s cubic-bezier(0.22, 0.9, 0.3, 1.15) both; }

.labels { font-family: var(--mono); }

@keyframes pop {
  from { opacity: 0; transform: scale(0.985); }
  to   { opacity: 1; transform: scale(1); }
}

/* ---------- Board status bar ---------- */

.board-status {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  padding: 9px 18px;
  background: linear-gradient(180deg, rgba(20, 24, 33, 0.9), rgba(16, 20, 29, 0.95));
  border-top: 1px solid var(--border-soft);
  backdrop-filter: blur(6px);
}

#statusText {
  font-size: 11.5px;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.status-badges { display: flex; gap: 8px; flex: 0 0 auto; }

.badge {
  font-family: var(--mono);
  font-size: 10px;
  color: var(--accent-2);
  background: var(--accent-soft);
  border: 1px solid rgba(245, 168, 60, 0.25);
  border-radius: 6px;
  padding: 3px 8px;
  white-space: nowrap;
}

/* ---------- Tooltip ---------- */

.tooltip {
  position: fixed;
  z-index: 50;
  font-family: var(--mono);
  font-size: 12px;
  font-weight: 600;
  color: #141414;
  background: var(--accent-2);
  border-radius: 7px;
  padding: 4px 9px;
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.45);
  pointer-events: none;
  transform: translate(-50%, -50%);
  animation: tipIn 0.12s ease;
}

.tooltip::after {
  content: 'HOLE';
  display: block;
  font-size: 8px;
  font-weight: 600;
  letter-spacing: 0.1em;
  opacity: 0.55;
  text-align: center;
}

@keyframes tipIn {
  from { opacity: 0; transform: translate(-50%, -50%) scale(0.92); }
  to   { opacity: 1; transform: translate(-50%, -50%) scale(1); }
}

/* ---------- Wire context menu (recolor / remove) ---------- */

.wire-menu {
  position: fixed;
  z-index: 60;
  min-width: 168px;
  display: flex;
  flex-direction: column;
  gap: 9px;
  padding: 10px;
  background: rgba(22, 27, 38, 0.97);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: 0 16px 40px rgba(0, 0, 0, 0.55);
  backdrop-filter: blur(8px);
  animation: menuIn 0.13s ease;
}

.wire-menu[hidden] { display: none; }

/* The hidden attribute must beat the author display rules for the
   context-menu sections we toggle (recolor palette for headers). */
#wireMenuColors[hidden], .wire-menu-label[hidden], .wire-menu-sex[hidden] { display: none; }

.wire-menu-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.wire-menu-title {
  font-family: var(--mono);
  font-size: 11px;
  font-weight: 600;
  color: var(--accent-2);
  letter-spacing: 0.03em;
}

.wire-menu-delete {
  font-family: inherit;
  font-size: 12px;
  font-weight: 600;
  color: #ff6b6b;
  background: rgba(255, 107, 107, 0.1);
  border: 1px solid rgba(255, 107, 107, 0.35);
  border-radius: 8px;
  padding: 4px 9px;
  cursor: pointer;
  transition: all 0.15s ease;
}

.wire-menu-delete:hover {
  color: #fff;
  background: rgba(255, 107, 107, 0.35);
  border-color: #ff6b6b;
}

.wire-menu-delete:active { transform: scale(0.96); }

.wire-menu-delete:focus-visible {
  outline: none;
  border-color: #ff6b6b;
  box-shadow: 0 0 0 3px rgba(255, 107, 107, 0.3);
}

.wire-menu-sep {
  height: 1px;
  background: var(--border);
}

.wire-menu-label {
  font-size: 10px;
  font-weight: 600;
  color: var(--faint);
  text-transform: uppercase;
  letter-spacing: 0.1em;
}

.wire-menu-sex {
  font-family: inherit;
  font-size: 12px;
  font-weight: 600;
  color: var(--text);
  background: var(--panel-3);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 7px 10px;
  cursor: pointer;
  text-align: left;
  transition: all 0.15s ease;
}

.wire-menu-sex:hover {
  color: var(--accent-2);
  border-color: rgba(245, 168, 60, 0.4);
  background: var(--accent-soft);
}

.wire-menu-sex:active { transform: scale(0.97); }

.wire-menu-sex:focus-visible {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(245, 168, 60, 0.25);
}

.wire-menu-colors {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(24px, 1fr));
  gap: 7px;
}

.menu-swatch {
  width: 26px;
  height: 26px;
  padding: 0;
  border-radius: 50%;
  background: var(--wc);
  border: 2px solid rgba(255, 255, 255, 0.16);
  box-shadow: inset 0 -2px 4px rgba(0, 0, 0, 0.35);
  cursor: pointer;
  transition: transform 0.12s ease, box-shadow 0.12s ease, border-color 0.12s ease;
}

.menu-swatch:hover {
  transform: scale(1.14);
  border-color: rgba(255, 255, 255, 0.5);
}

.menu-swatch:active { transform: scale(0.92); }

.menu-swatch:focus-visible {
  outline: none;
  box-shadow: 0 0 0 3px rgba(245, 168, 60, 0.45);
}

.menu-swatch.active {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px var(--accent), inset 0 -2px 4px rgba(0, 0, 0, 0.35);
}

@keyframes menuIn {
  from { opacity: 0; transform: translateY(-4px) scale(0.97); }
  to   { opacity: 1; transform: translateY(0) scale(1); }
}

/* ---------- Responsive ---------- */

@media (max-width: 900px) {
  .app { height: auto; min-height: 100dvh; }
  .layout {
    display: flex;
    flex-direction: column;
  }
  .sidebar {
    overflow: visible;
    border-right: none;
    border-bottom: 1px solid var(--border);
    display: grid;
    grid-template-columns: 1fr 1fr;
    align-items: stretch;
  }
  .sidebar .card:first-child { grid-column: 1 / -1; }
  .sidebar .reset-btn, .sidebar .sidebar-note { grid-column: 1 / -1; }
  .board-area { min-height: 62vh; flex: 1 1 auto; }
  .top-stats { display: none; }
}

@media (max-width: 560px) {
  .sidebar { grid-template-columns: 1fr; }
  .board-status { flex-direction: column; align-items: flex-start; gap: 6px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.001s !important;
    transition-duration: 0.001s !important;
  }
}
