: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 chooser: shown once on load so the choice is never hidden
   behind a small button colleagues might not notice, especially on a
   phone. Plain in-flow flex, not a fixed/absolute overlay — that keeps it
   immune to the mobile viewport-height and stacking-context quirks a
   `position: fixed` layer can hit inside an embedded viewer. Before a
   choice is made, body.chosen is absent, which hides the app and shows
   just the chooser (see the .main-grid rule below); clicking an option
   adds body.chosen and swaps which one is visible. */
.layout-chooser {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  background: var(--bg);
}
body.chosen .layout-chooser {
  display: none;
}
body:not(.chosen) .main-grid {
  display: none;
}

.chooser-option {
  flex: 1 1 50%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: 40px 24px;
  background: var(--panel-bg);
  border: none;
  border-left: 1px solid var(--panel-border);
  color: var(--text);
  font-family: inherit;
  cursor: pointer;
  text-align: center;
}
.chooser-option:first-child { border-left: none; }
.chooser-option:hover { background: var(--row-hover); }

.chooser-icon {
  display: flex;
  color: var(--accent);
}
.chooser-title { font-size: 1.3rem; font-weight: 700; }
.chooser-desc {
  max-width: 320px;
  font-size: 0.9rem;
  color: var(--text-dim);
  line-height: 1.4;
}

@media (max-width: 640px), (max-aspect-ratio: 4/5) {
  .layout-chooser { flex-direction: column; }
  .chooser-option:first-child {
    border-left: none;
    border-bottom: 1px solid var(--panel-border);
  }
}

.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);
}

/* Default (desktop) layout — board and signal list side by side, the
   active-pairs banner spans the full width underneath:
     +-------------------+-----------+
     |       board       |  signals  |
     +-------------------+-----------+
     |             pairs             |
     +--------------------------------+
   "Phone layout" (for a landscape phone, where width is generous but
   height is tight) swaps to a full-height board on the left with the
   signal list and pairs stacked on the right:
     +-------------------+-----------+
     |                   |  signals  |
     |       board       +-----------+
     |                   |   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;
}

body.phone-layout .main-grid {
  grid-template-rows: minmax(0, 1fr) minmax(0, 1fr);
  /* pairs on top, signal list below — on a phone you want the result of
     your last tap to stay put near the top while you scroll the (often
     longer) signal list underneath it looking for the next one */
  grid-template-areas:
    "board banner"
    "board side";
}

/* 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. This
   applies no matter which chooser option was picked — the desktop/phone
   split is about arranging *columns*, which only makes sense once
   there's enough width to have columns at all. */
@media (max-width: 700px) {
  .main-grid,
  body.phone-layout .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;
}

/* Tools live in the margin the board's own aspect ratio leaves empty
   rather than floating over it — see the SVG's preserveAspectRatio
   (xMaxYMid), which pins the board to the right so this column reliably
   has room instead of guessing at a dynamic letterboxed gap. */
.board-toolbar {
  flex: 0 0 auto;
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 8px;
}

.tool-btn,
.split-btn,
.theme-switch {
  flex: 0 0 auto;
  width: 104px;
  height: 44px;
  border: 1px solid var(--panel-border);
  border-radius: 8px;
  cursor: pointer;
  padding: 0;
}

.tool-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  background: var(--row-bg);
  color: var(--text);
}
.tool-btn:hover { background: var(--row-hover); }
.tool-btn.active,
.tool-btn.copied {
  background: var(--accent);
  color: #ffffff;
  border-color: var(--accent);
}
.tool-btn-label {
  font-size: 0.78rem;
  font-family: inherit;
  white-space: nowrap;
}

.split-btn {
  display: flex;
  padding: 0;
  overflow: hidden;
}
.split-btn-half {
  flex: 1 1 50%;
  align-self: stretch;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  background: transparent;
  color: var(--text);
  border: none;
  cursor: pointer;
  padding: 0;
}
.split-btn-half:hover { background: var(--row-hover); }
.split-btn-half:first-child { border-right: 1px solid var(--panel-border); }
.split-btn-half:disabled {
  color: var(--panel-border);
  cursor: not-allowed;
  background: transparent;
}
.split-btn-label {
  font-size: 0.62rem;
  font-family: inherit;
}

/* --- theme switch: a real slider, not an icon that changes --- */
.theme-switch {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 5px;
  background: var(--row-bg);
  color: var(--text);
  font-family: inherit;
}
.theme-switch:hover { background: var(--row-hover); }
.theme-switch-label {
  font-size: 0.68rem;
  white-space: nowrap;
}
.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); }

.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;
}

.clear-all-btn {
  background: var(--danger-bg);
  color: var(--danger-text);
  border: 1px solid var(--danger-border);
  border-radius: 6px;
  padding: 5px 10px;
  font-size: 0.78rem;
  cursor: pointer;
}
.clear-all-btn:hover { background: var(--danger-hover); }

.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;
  transition: fill 0.1s;
}
.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: 10;
  stroke-linecap: round;
  fill: none;
  opacity: 0.9;
}
.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: space-between;
  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);
}
