:root {
  /* TDK blue, sampled directly from the logo, plus a light neutral scheme
     built around it — see README.md for the full palette rationale.
     body.dark-theme below redefines every one of these for dark mode;
     nothing outside these two blocks should hardcode a color. */
  --bg: #e7eef7;
  --panel-bg: #ffffff;
  --panel-border: #d3e1f0;
  --row-bg: #f3f7fc;
  --row-hover: #e7f0fa;
  --text: #16232f;
  --text-dim: #5c7086;
  --accent: #0a57a0;
  --accent-hover: #084785;
  --pending: #e8a93b;
  --warning: #b45309;
  --danger-bg: #fce9e9;
  --danger-text: #b23b3b;
  --danger-border: #f0c4c4;
  --danger-hover: #f7d6d6;
  --pad: #f0b429;
  --pad-stroke: #8a6216;
  /* the board schematic itself */
  --board-fill: #ffffff;
  --board-stroke: #0a57a0;
  --outline-color: #14181c;
  --pcb-label-color: #16232f;
  --pcb-num-color: #5c7086;
  --pcb-title-color: #0a57a0;
  font-family: -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
}

body.dark-theme {
  --bg: #1c1f22;
  --panel-bg: #24282c;
  --panel-border: #383e44;
  --row-bg: #2c3136;
  --row-hover: #333a41;
  --text: #e8eaed;
  --text-dim: #9aa2ab;
  --accent: #4da3ff;
  --accent-hover: #3d8fe0;
  --pending: #ffcc33;
  --warning: #f2b84f;
  --danger-bg: #3a2323;
  --danger-text: #f2b3b3;
  --danger-border: #5a3131;
  --danger-hover: #4a2929;
  --pad: #f0b429;
  --pad-stroke: #8a6216;
  --board-fill: #20242b;
  --board-stroke: #4da3ff;
  --outline-color: #6b7684;
  --pcb-label-color: #e8eaed;
  --pcb-num-color: #9aa2ab;
  --pcb-title-color: #4da3ff;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
}

body {
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.layout-toggle-btn {
  flex: 0 0 auto;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: var(--row-bg);
  color: var(--text);
  border: 1px solid var(--panel-border);
  border-radius: 6px;
  padding: 6px 10px;
  font-size: 0.78rem;
  font-family: inherit;
  cursor: pointer;
  white-space: nowrap;
}
.layout-toggle-btn .btn-icon { flex: 0 0 auto; }
.layout-toggle-btn:hover { background: var(--row-hover); }
.layout-toggle-btn.active,
.layout-toggle-btn.copied {
  background: var(--accent);
  color: #ffffff;
  border-color: var(--accent);
}

/* Board and signal list side by side, the active-pairs banner spans the
   full width underneath:
     +-------------------+-----------+
     |       board       |  signals  |
     +-------------------+-----------+
     |             pairs             |
     +--------------------------------+ */
.main-grid {
  flex: 1 1 auto;
  min-height: 0;
  display: grid;
  grid-template-columns: 1fr 380px;
  /* the banner row is a fixed size (not "auto"), so the board never grows
     or shrinks as pairs are added/removed — only the banner's own
     internal scrolling changes, the layout around it stays put */
  grid-template-rows: minmax(0, 1fr) 22vh;
  grid-template-areas:
    "board side"
    "banner banner";
  gap: 16px;
  padding: 16px;
}

/* Medium widths (landscape phones, small tablets): keep the two-column
   grid, but a 380px sidebar is too greedy at this size — shrink it. */
@media (max-width: 1000px) {
  .main-grid {
    grid-template-columns: 1fr 260px;
  }
}

/* Narrow widths (a phone held in portrait): there's no sensible way to
   fit a board next to a 260-380px sidebar, so give up on columns
   entirely and stack board / signals / pairs vertically instead. */
@media (max-width: 700px) {
  .main-grid {
    display: flex;
    flex-direction: column;
    overflow-y: auto;
    padding: 12px;
  }
  /* pairs above the signal list, same reasoning as the two-column phone
     layout above: the result of your last tap should stay near the top. */
  .board-area { order: 1; }
  .pairs-banner { order: 2; }
  .side-panel { order: 3; }
  .board-area {
    height: 42vh;
    min-height: 260px;
    margin-bottom: 12px;
  }
  .pairs-banner {
    margin-bottom: 12px;
    max-height: 40vh;
  }
  .side-panel {
    height: 280px;
  }
}

.board-area {
  grid-area: board;
  min-width: 0;
  min-height: 0;
  display: flex;
  flex-direction: row;
  gap: 10px;
  background: var(--panel-bg);
  border: 1px solid var(--panel-border);
  border-radius: 10px;
  padding: 12px;
}

/* --- small icon-only tool buttons: live next to "Signal Names" (theme,
   layout) and next to "Active jumper pairs" (import, copy, undo/redo,
   clear) instead of taking up a whole dedicated toolbar column. --- */
.icon-btn,
.icon-split-btn {
  flex: 0 0 auto;
  width: 30px;
  height: 30px;
  border: 1px solid var(--panel-border);
  border-radius: 7px;
  cursor: pointer;
  padding: 0;
}

.icon-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--row-bg);
  color: var(--text);
}
.icon-btn:hover { background: var(--row-hover); }
.icon-btn.active,
.icon-btn.copied {
  background: var(--accent);
  color: #ffffff;
  border-color: var(--accent);
}
.danger-icon-btn {
  background: var(--danger-bg);
  color: var(--danger-text);
  border-color: var(--danger-border);
}
.danger-icon-btn:hover { background: var(--danger-hover); }

.icon-split-btn {
  display: flex;
  padding: 0;
  overflow: hidden;
}
.icon-split-btn-half {
  flex: 1 1 50%;
  align-self: stretch;
  display: flex;
  align-items: center;
  justify-content: center;
  background: transparent;
  color: var(--text);
  border: none;
  cursor: pointer;
  padding: 0;
}
.icon-split-btn-half:hover { background: var(--row-hover); }
.icon-split-btn-half:first-child { border-right: 1px solid var(--panel-border); }
.icon-split-btn-half:disabled {
  color: var(--panel-border);
  cursor: not-allowed;
  background: transparent;
}

.panel-header-actions,
.pairs-banner-actions {
  display: flex;
  gap: 6px;
  flex: 0 0 auto;
}

/* --- theme switch: a real slider, not an icon that changes --- */
.theme-switch {
  flex: 0 0 auto;
  width: 46px;
  height: 30px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 1px solid var(--panel-border);
  border-radius: 7px;
  cursor: pointer;
  padding: 0;
  background: var(--row-bg);
}
.theme-switch:hover { background: var(--row-hover); }
.switch-track {
  flex: 0 0 auto;
  width: 34px;
  height: 18px;
  border-radius: 999px;
  background: var(--panel-border);
  position: relative;
  transition: background 0.15s;
}
.switch-thumb {
  position: absolute;
  top: 2px;
  left: 2px;
  width: 14px;
  height: 14px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  background: #ffffff;
  /* fixed regardless of theme — the thumb itself stays a white disc in
     both modes, so its icon needs its own always-legible color */
  color: #5c7086;
  transition: transform 0.15s;
}
.theme-switch.active .switch-track { background: var(--accent); }
.theme-switch.active .switch-thumb { transform: translateX(16px); }

/* --- rectangular labeled buttons next to "Active jumper pairs" --- */
.pairs-tool-btn,
.pairs-split-btn {
  flex: 0 0 auto;
  height: 30px;
  border: 1px solid var(--panel-border);
  border-radius: 7px;
  cursor: pointer;
  padding: 0;
}
.pairs-tool-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 0 10px;
  background: var(--row-bg);
  color: var(--text);
}
.pairs-tool-btn:hover { background: var(--row-hover); }
.pairs-tool-btn.copied {
  background: var(--accent);
  color: #ffffff;
  border-color: var(--accent);
}
.danger-tool-btn {
  background: var(--danger-bg);
  color: var(--danger-text);
  border-color: var(--danger-border);
}
.danger-tool-btn:hover { background: var(--danger-hover); }
.pairs-tool-btn-label {
  font-size: 0.74rem;
  font-family: inherit;
  white-space: nowrap;
}

.pairs-split-btn {
  display: flex;
  padding: 0;
  overflow: hidden;
}
.pairs-split-btn-half {
  flex: 0 0 auto;
  align-self: stretch;
  display: flex;
  align-items: center;
  gap: 5px;
  padding: 0 9px;
  background: transparent;
  color: var(--text);
  border: none;
  cursor: pointer;
}
.pairs-split-btn-half:hover { background: var(--row-hover); }
.pairs-split-btn-half:first-child { border-right: 1px solid var(--panel-border); }
.pairs-split-btn-half:disabled {
  color: var(--panel-border);
  cursor: not-allowed;
  background: transparent;
}
.pairs-split-btn-label {
  font-size: 0.7rem;
  font-family: inherit;
}

.board-scroll {
  flex: 1 1 auto;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
  display: flex;
  border-radius: 6px;
}

#board-svg {
  width: 100%;
  height: 100%;
  display: block;
}

.side-panel {
  grid-area: side;
  min-width: 0;
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: 16px;
  overflow-y: auto;
}

.panel-block {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
  background: var(--panel-bg);
  border: 1px solid var(--panel-border);
  border-radius: 10px;
  padding: 14px;
}

.panel-block-header {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 6px 8px;
  margin-bottom: 4px;
}

.panel-block h2 {
  font-size: 1rem;
  margin: 0;
}

.panel-hint {
  margin: 0 0 8px;
  font-size: 0.82rem;
  color: var(--text-dim);
}

.signal-list {
  list-style: none;
  margin: 0;
  padding: 0;
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.signal-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 7px 10px;
  border-radius: 6px;
  border: 1px solid transparent;
  background: var(--row-bg);
  cursor: pointer;
  font-size: 0.88rem;
  user-select: none;
  transition: background 0.12s, border-color 0.12s;
}

.signal-item:hover {
  background: var(--row-hover);
}

.signal-item.selected-pending {
  border-color: var(--pending);
  background: #fdf1dc;
  box-shadow: 0 0 0 1px var(--pending);
}

.signal-item .swatch {
  width: 11px;
  height: 11px;
  border-radius: 50%;
  flex: 0 0 auto;
  background: #b8c4d1;
}

.signal-item.has-pairs .swatch-group {
  display: flex;
  gap: 2px;
}

.remove-btn {
  background: transparent;
  color: var(--text-dim);
  border: 1px solid var(--panel-border);
  border-radius: 5px;
  width: 24px;
  height: 24px;
  line-height: 1;
  cursor: pointer;
  flex: 0 0 auto;
}
.remove-btn:hover {
  background: var(--danger-bg);
  color: var(--danger-text);
  border-color: var(--danger-border);
}

/* --- SVG board --- */
.pcb-pad {
  fill: var(--pad);
  stroke: var(--pad-stroke);
  stroke-width: 1;
  cursor: pointer;
  transition: fill 0.1s, stroke 0.1s, stroke-width 0.1s;
}
.pcb-pad:hover {
  stroke: var(--accent);
  stroke-width: 2;
}
.pcb-label {
  fill: var(--pcb-label-color);
  font-size: 22px;
  font-weight: 600;
  font-family: inherit;
}
.pcb-num {
  fill: var(--pcb-num-color);
  font-size: 16px;
  text-anchor: middle;
  font-family: inherit;
}
.pcb-jumper-bridge {
  stroke-width: 4;
  fill-opacity: 0.16;
  opacity: 0.95;
}
.pcb-title-text {
  fill: var(--pcb-title-color);
  font-size: 19px;
  font-weight: 600;
  font-family: inherit;
}

.pcb-board {
  fill: var(--board-fill);
  stroke: var(--board-stroke);
  stroke-width: 3;
}

.pcb-outline {
  fill: none;
  stroke: var(--outline-color);
  stroke-width: 2;
}

/* --- pairs banner: the big, hard-to-miss readout of active pairs --- */
.pairs-banner {
  grid-area: banner;
  min-width: 0;
  min-height: 0;
  padding: 14px 20px 18px;
  overflow-y: auto;
  background: var(--panel-bg);
  border: 1px solid var(--panel-border);
  border-radius: 10px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.pairs-banner-header {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: flex-start;
  gap: 8px;
}

.pairs-banner-label {
  font-size: 0.72rem;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--text-dim);
}

.pairs-banner-list {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
}

.pairs-banner-chip {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 10px 14px 10px 20px;
  border-radius: 10px;
  background: var(--row-bg);
  border-left: 7px solid var(--chip-color, #8899aa);
}

.pairs-banner-chip .chip-main {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.pairs-banner-chip .chip-text {
  font-size: clamp(1.15rem, 1.9vw, 1.6rem);
  font-weight: 700;
  line-height: 1.25;
  white-space: nowrap;
}

.pairs-banner-chip .chip-sub {
  font-size: 0.82rem;
  color: var(--text-dim);
}

.pairs-banner-chip .no-slot {
  color: var(--warning);
}

.chip-actions {
  display: flex;
  align-items: center;
  gap: 4px;
  flex: 0 0 auto;
}

.copy-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  background: transparent;
  color: var(--text-dim);
  border: 1px solid var(--panel-border);
  border-radius: 5px;
  width: 24px;
  height: 24px;
  cursor: pointer;
  flex: 0 0 auto;
}
.copy-btn:hover {
  background: var(--row-hover);
  color: var(--accent);
  border-color: var(--accent);
}
.copy-btn.copied {
  background: var(--accent);
  color: #ffffff;
  border-color: var(--accent);
}

.pairs-banner-empty {
  margin: 0;
  color: var(--text-dim);
  font-size: 1rem;
}

/* --- import modal: paste a "Copy All" list to load a whole scheme --- */
.import-modal {
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(22, 35, 47, 0.45);
  padding: 20px;
}
.import-modal.hidden {
  display: none;
}

.import-modal-box {
  width: 100%;
  max-width: 440px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  background: var(--panel-bg);
  border: 1px solid var(--panel-border);
  border-radius: 12px;
  padding: 20px;
}
.import-modal-box h3 {
  margin: 0;
  font-size: 1.05rem;
}
.import-modal-box code {
  background: var(--row-bg);
  border-radius: 3px;
  padding: 1px 5px;
  font-size: 0.85em;
}

#import-textarea {
  width: 100%;
  min-height: 140px;
  resize: vertical;
  font-family: ui-monospace, "SFMono-Regular", Consolas, monospace;
  font-size: 0.85rem;
  padding: 8px 10px;
  border-radius: 6px;
  border: 1px solid var(--panel-border);
  background: var(--row-bg);
  color: var(--text);
}

.import-error {
  margin: 0;
  min-height: 1.1em;
  font-size: 0.8rem;
  color: var(--danger-text);
}

.import-modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}

.primary-btn {
  background: var(--accent);
  color: #ffffff;
  border: 1px solid var(--accent);
  border-radius: 6px;
  padding: 6px 16px;
  font-size: 0.85rem;
  font-family: inherit;
  cursor: pointer;
}
.primary-btn:hover {
  background: var(--accent-hover);
  border-color: var(--accent-hover);
}
