/*
 * Dense, functional, dark by default to match KiCad's PCB editor. Legibility of
 * the panel over decoration.
 *
 * The token block and the component rules are deltaKi's, carried over verbatim
 * so the two tools read as one suite. What is new here is the panel canvas, the
 * panel tabs, and the inspector forms.
 */

:root {
  --bg: #1b1b1f;
  --bg-panel: #232329;
  --bg-raised: #2b2b33;
  --bg-input: #17171b;
  --fg: #e4e4ea;
  --fg-dim: #9a9aa8;
  --fg-faint: #6c6c7a;
  --border: #3a3a45;
  --accent: #4a9eff;

  --pass: #3fb950;
  --fail: #f85149;
  --fail-bg: #2a1416;
  --warn: #e3b341;
  --warn-bg: #2a2312;

  /* Panel object colours, carried over from the original tool's canvas. */
  --board: #58c878;
  --board-fill: #2b7d3f66;
  --rail: #9aa0ad;
  --rail-fill: #6a6f7a55;
  --tab: #e3a341;
  --tab-fill: #e3a34155;
  --cut: #f85149;
  --sel: #ff9d3c;

  --mono: ui-monospace, "SF Mono", "Cascadia Mono", "JetBrains Mono", Menlo, Consolas, monospace;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;

  --panel-head-h: 33px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--fg);
  font-family: var(--sans);
  font-size: 13px;
  line-height: 1.45;
  overflow: hidden;
}

button, input, select, textarea { font: inherit; color: inherit; }

.app {
  display: grid;
  grid-template-columns: 300px 1fr 330px;
  grid-template-rows: 100%;
  height: 100vh;
  position: relative;
}

@media (max-width: 1200px) {
  .app { grid-template-columns: 260px 1fr 290px; }
}

/* --- columns and pinned section headers ------------------------------------------ */

/*
 * The column scrolls as one document, and every section heading is pinned.
 * `display: contents` on the section wrapper is what makes this work: a sticky
 * element is confined to its parent's box, so a heading wrapped in its own
 * section would vanish the moment that section scrolled past. With the wrapper
 * generating no box, the heading's containing block is the scrolling column.
 */
.col {
  overflow-y: auto;
  overflow-x: hidden;
  background: var(--bg-panel);
  border-right: 1px solid var(--border);
}
.col > .panel { display: contents; }
.col > .pinned-top { position: sticky; top: 0; z-index: 5; }
.col > .panel > .panel-content { border-bottom: 1px solid var(--border); }

.col-right { border-right: 0; border-left: 1px solid var(--border); }
.col-main {
  background: var(--bg);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  min-width: 0;
}

.panel > h2 {
  position: sticky;
  top: calc(var(--stack-top-offset, 0px) + var(--panel-above, 0) * var(--panel-head-h));
  bottom: calc(var(--panel-below, 0) * var(--panel-head-h));
  z-index: 3;
  height: var(--panel-head-h);
  box-sizing: border-box;
  box-shadow: 0 1px 0 var(--border);
  margin: 0;
  padding: 8px 12px 8px 8px;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--fg-dim);
  background: var(--bg-raised);
  display: flex;
  align-items: center;
  gap: 6px;
  cursor: pointer;
  user-select: none;
}
.panel > h2:hover { color: var(--fg); background: #32323c; }
.panel > h2 .panel-title { flex: 1 1 auto; }
.panel > h2 .count {
  color: var(--fg-faint);
  font-weight: 400;
  letter-spacing: 0;
  text-transform: none;
  flex: 0 0 auto;
}
.panel-body { padding: 10px 12px; }
.panel-body > * + * { margin-top: 8px; }

/* --- brand header ---------------------------------------------------------------- */

.title-bar {
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-raised);
  display: flex;
  align-items: center;
  gap: 10px;
}
.title-bar .brand-mark { width: 40px; height: 40px; flex: 0 0 auto; }
.title-bar .brand-text { flex: 1 1 auto; min-width: 0; }
/* Typewriter mono, matching the wordmark drawn into the logo itself. */
.title-bar h1 {
  margin: 0;
  font-family: 'Courier New', Courier, 'JetBrains Mono', var(--mono);
  font-size: 26px;
  font-weight: 700;
  line-height: 1.05;
  letter-spacing: 0.02em;
}
.title-bar .tagline {
  margin: 3px 0 0;
  font-family: 'Courier New', Courier, var(--mono);
  font-size: 9.5px;
  letter-spacing: 0.2em;
  color: var(--fg-faint);
}
.title-bar .about-button { flex: 0 0 auto; align-self: center; }

/* --- controls -------------------------------------------------------------------- */

button {
  background: var(--bg-raised);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 5px 10px;
  cursor: pointer;
}
button:hover:not(:disabled) { border-color: var(--accent); }
button:disabled { opacity: 0.4; cursor: not-allowed; }
button.primary { background: var(--accent); border-color: var(--accent); color: #06121f; font-weight: 600; }
button.danger { background: var(--fail); border-color: var(--fail); color: #1a0808; font-weight: 600; }
button.sm { padding: 3px 7px; font-size: 11px; }

input[type="text"], input[type="number"], select, textarea {
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 4px 6px;
  width: 100%;
}
input:focus, select:focus, textarea:focus { outline: 1px solid var(--accent); border-color: var(--accent); }
input::placeholder { color: var(--fg-faint); }

label { display: block; font-size: 11px; color: var(--fg-dim); }
.row { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.row > * { flex: 0 0 auto; }
.row.fill > * { flex: 1 1 0; }

.checkbox { display: flex; gap: 6px; align-items: flex-start; font-size: 12px; color: var(--fg); }
.checkbox input { margin-top: 2px; flex: 0 0 auto; width: auto; }

.seg { display: inline-flex; border: 1px solid var(--border); border-radius: 4px; overflow: hidden; }
.seg button { border: 0; border-radius: 0; border-right: 1px solid var(--border); }
.seg button:last-child { border-right: 0; }
.seg button.on { background: var(--accent); color: #06121f; font-weight: 600; }

.dropzone {
  border: 1px dashed var(--border);
  border-radius: 5px;
  padding: 10px;
  text-align: center;
  cursor: pointer;
  background: var(--bg-input);
}
.dropzone:hover, .dropzone.over { border-color: var(--accent); background: #1c2430; }
.dropzone .note { font-size: 11px; color: var(--fg-dim); }

/* --- sources --------------------------------------------------------------------- */

.src-card { border: 1px solid var(--border); border-radius: 5px; overflow: hidden; }
.src-card > header {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 5px 7px;
  background: var(--bg-raised);
  font-family: var(--mono);
  font-size: 11px;
  word-break: break-all;
}
.src-card > header .grow { flex: 1 1 auto; min-width: 0; }
.src-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 4px 7px;
  cursor: grab;
  border-top: 1px solid var(--border);
}
.src-item:hover { background: var(--bg-raised); }
.src-item svg { flex: 0 0 auto; background: var(--bg-input); border-radius: 3px; }
.src-item .name { flex: 1 1 auto; min-width: 0; font-size: 12px; overflow: hidden; text-overflow: ellipsis; }
.src-item .size { font-size: 10px; color: var(--fg-faint); font-family: var(--mono); }

/* --- object list ----------------------------------------------------------------- */

.obj-group { font-size: 10px; text-transform: uppercase; letter-spacing: 0.08em; color: var(--fg-faint); margin-top: 6px; }
.obj-row {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 2px 6px;
  border-radius: 3px;
  cursor: pointer;
  font-size: 12px;
}
.obj-row:hover { background: var(--bg-raised); }
.obj-row.sel { background: #33445c; color: #fff; }
.obj-row .label { flex: 1 1 auto; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.obj-row .del { opacity: 0; padding: 0 4px; background: none; border: 0; color: var(--fg-dim); }
.obj-row:hover .del { opacity: 1; }
.obj-row .del:hover { color: var(--fail); }

/* --- panel tabs ------------------------------------------------------------------ */

.panel-tabs {
  display: flex;
  align-items: stretch;
  gap: 2px;
  padding: 4px 6px 0;
  background: var(--bg-panel);
  border-bottom: 1px solid var(--border);
  overflow-x: auto;
  flex: 0 0 auto;
}
.panel-tab {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 4px 8px;
  border: 1px solid var(--border);
  border-bottom: 0;
  border-radius: 4px 4px 0 0;
  background: var(--bg-input);
  color: var(--fg-dim);
  cursor: pointer;
  white-space: nowrap;
  font-size: 12px;
}
.panel-tab.on { background: var(--bg); color: var(--fg); border-color: var(--accent); }
.panel-tab .close { opacity: 0.5; padding: 0 2px; }
.panel-tab .close:hover { opacity: 1; color: var(--fail); }
.panel-tab .dirty { color: var(--sel); }
.panel-tabs .add { align-self: center; margin-left: 4px; }

/* --- canvas ---------------------------------------------------------------------- */

.map-toolbar {
  display: flex;
  gap: 8px;
  align-items: center;
  padding: 6px 10px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-panel);
  flex-wrap: wrap;
  flex: 0 0 auto;
}
.map-toolbar .snap { width: 4.5em; }
.map-toolbar .spacer { flex: 1 1 auto; }

.layer-bar {
  display: flex;
  gap: 4px;
  align-items: center;
  padding: 5px 10px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-panel);
  overflow-x: auto;
  white-space: nowrap;
  flex: 0 0 auto;
}
.layer-bar .chip {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 2px 7px;
  font-size: 11px;
  border-radius: 10px;
  background: var(--bg-input);
  border: 1px solid var(--border);
  color: var(--fg-faint);
  flex: 0 0 auto;
  cursor: pointer;
}
.layer-bar .chip.on { color: var(--fg); border-color: var(--fg-faint); background: var(--bg-raised); }
.layer-bar .chip .dot { width: 8px; height: 8px; border-radius: 50%; display: inline-block; opacity: 0.35; }
.layer-bar .chip.on .dot { opacity: 1; }

.map-wrap { position: relative; flex: 1 1 auto; min-height: 0; }
canvas { display: block; width: 100%; height: 100%; cursor: grab; }
canvas.dragging { cursor: grabbing; }
canvas.tool { cursor: crosshair; }

.map-note {
  position: absolute;
  left: 10px;
  bottom: 10px;
  background: #000000bb;
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 6px 8px;
  font-size: 11px;
  color: var(--fg-dim);
  max-width: 460px;
  pointer-events: none;
}

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

.view-footer {
  display: flex;
  gap: 14px;
  align-items: center;
  flex-wrap: wrap;
  padding: 5px 10px;
  border-top: 1px solid var(--border);
  background: var(--bg-panel);
  font-size: 11px;
  flex: 0 0 auto;
}
.view-footer .credit { margin-left: auto; opacity: 0.55; }
.view-footer .credit a { color: inherit; text-decoration: none; border-bottom: 1px dotted currentColor; }
.view-footer .credit a:hover { color: var(--accent); }
.view-footer .status.bad { color: var(--fail); }
.view-footer .status.warn { color: var(--warn); }

/* --- drawer ---------------------------------------------------------------------- */

.drawer { border-top: 1px solid var(--border); background: var(--bg-panel); flex: 0 0 auto; }
.drawer-head { display: flex; gap: 6px; align-items: center; padding: 4px 8px; }
.drawer-head .spacer { flex: 1 1 auto; }
.drawer-body {
  height: 170px;
  overflow: auto;
  border-top: 1px solid var(--border);
  background: var(--bg-input);
}
.drawer.closed .drawer-body { display: none; }
.drawer-body pre {
  margin: 0;
  padding: 8px;
  font-family: var(--mono);
  font-size: 11px;
  white-space: pre-wrap;
  word-break: break-word;
}
.log-line.bad { color: var(--fail); }
.log-line.warn { color: var(--warn); }

/* --- inspector ------------------------------------------------------------------- */

.field { margin-bottom: 7px; }
.field > label { margin-bottom: 2px; }
.field .vec { display: flex; gap: 5px; }
.field .ro {
  display: block;
  font-family: var(--mono);
  font-size: 11px;
  color: var(--fg-dim);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.insp-error {
  color: var(--fail);
  background: var(--fail-bg);
  border-radius: 4px;
  padding: 6px 8px;
  font-size: 11px;
  margin-top: 6px;
}
.rawyaml { font-family: var(--mono); font-size: 11px; min-height: 70px; resize: vertical; }
.sub-head {
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--fg-faint);
  margin: 10px 0 4px;
  display: flex;
  align-items: center;
  gap: 6px;
}
.sub-head input { width: auto; }

.empty { color: var(--fg-faint); font-size: 12px; font-style: italic; }
.note { font-size: 11px; color: var(--fg-dim); }
.note.warn { color: var(--warn); }
.note.bad { color: var(--fail); }
.mono { font-family: var(--mono); }

kbd {
  font-family: var(--mono);
  font-size: 10px;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-bottom-width: 2px;
  border-radius: 3px;
  padding: 0 4px;
}

/* --- branding -------------------------------------------------------------------- */

/*
 * The logo stands in for the panel while there is nothing to draw, so the canvas
 * reads as "waiting for a board" rather than as a rendering failure.
 */
.watermark {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 65%;
  height: 65%;
  object-fit: contain;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.35s ease;
}
.watermark.on { opacity: 0.16; }

.splash {
  position: absolute;
  inset: 0;
  z-index: 200;
  display: flex;
  align-items: center;
  justify-content: center;
  /* Nearly opaque: the interface behind is a hint of what is coming, not a
     competing thing to read. */
  background: #12121aef;
  cursor: pointer;
  transition: opacity 0.35s ease;
}
.splash.leaving { opacity: 0; }
.splash-logo {
  width: min(46vh, 34vw);
  height: auto;
  animation: splash-in 0.45s ease-out;
}
@keyframes splash-in {
  from { opacity: 0; transform: scale(0.96); }
  to { opacity: 1; transform: none; }
}
@media (prefers-reduced-motion: reduce) {
  .splash, .splash-logo, .watermark { transition: none; animation: none; }
}

/* --- dialog ---------------------------------------------------------------------- */

.dialog-backdrop {
  position: absolute;
  inset: 0;
  z-index: 300;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #0b0b10cc;
  padding: 24px;
}
.dialog {
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: 0 16px 50px #00000088;
  max-width: 520px;
  width: 100%;
  max-height: 100%;
  overflow: auto;
}
.dialog-head {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 16px 18px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-raised);
}
.dialog-logo { width: 52px; height: 52px; flex: 0 0 auto; }
.dialog-head h2 { margin: 0; font-size: 20px; font-weight: 700; }
.dialog-head .tagline {
  margin: 2px 0 0;
  font-family: var(--mono);
  font-size: 9.5px;
  letter-spacing: 0.22em;
  color: var(--fg-faint);
}
.dialog-close { margin-left: auto; align-self: flex-start; font-size: 14px; line-height: 1; }
.dialog-body { padding: 16px 18px; font-size: 12.5px; }
.dialog-body > * + * { margin-top: 12px; }
.dialog-body p { margin: 0; }
.dialog-body a { color: var(--accent); text-decoration: none; }
.dialog-body a:hover { text-decoration: underline; }
.dialog-link {
  display: inline-block;
  padding: 5px 10px;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: var(--bg-raised);
}
.dialog-link:hover { border-color: var(--accent); text-decoration: none; }
.dialog-body .disclaimer {
  font-size: 11.5px;
  color: var(--fg-dim);
  border-left: 3px solid var(--warn);
  padding-left: 10px;
}
.dialog-body .preview-note {
  font-size: 11.5px;
  color: var(--fg-dim);
  border-left: 3px solid var(--accent);
  padding-left: 10px;
}

/* --- missing board files --------------------------------------------------------- */

/*
 * A loaded spec names its boards by filename and the tool cannot fetch them, so
 * this says which are still needed and takes them on the spot.
 */
.missing {
  border: 1px solid var(--warn);
  background: var(--warn-bg);
  border-radius: 5px;
  padding: 8px 10px;
  margin-bottom: 8px;
}
.missing-head { font-size: 11.5px; color: var(--warn); margin-bottom: 5px; }
.missing-list {
  margin: 0 0 8px;
  padding-left: 18px;
  font-size: 11.5px;
  color: var(--fg);
}
.missing .dropzone { padding: 7px; }

/* --- the spec button ------------------------------------------------------------- */

/*
 * Once a spec is loaded this carries its filename, so the panel on screen is
 * identified rather than merely editable, with a dot when it has moved on from
 * what is on disk.
 */
.spec-button { display: inline-flex; align-items: center; gap: 5px; max-width: 22em; }
.spec-button.loaded { border-color: var(--fg-faint); }
.spec-button .mono { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.spec-button .dirty { color: var(--sel); flex: 0 0 auto; }

/* --- busy overlay ---------------------------------------------------------------- */

/*
 * Shown while something blocking runs. It only helps if the browser has painted
 * it *before* the work starts — see nextPaint() — otherwise the tab just freezes
 * with no overlay, which is what this replaces. Parsing a 33 MB board takes over
 * a second of held thread.
 */
.overlay {
  position: absolute;
  inset: 0;
  display: none;
  align-items: center;
  justify-content: center;
  background: #0e0e12cc;
  z-index: 100;
  cursor: progress;
}
.overlay.on { display: flex; }

.overlay-card {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 20px;
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: 6px;
  box-shadow: 0 8px 30px #00000066;
  max-width: 70%;
}
.overlay-text { font-size: 13px; color: var(--fg); }

.spinner {
  width: 16px;
  height: 16px;
  flex: 0 0 auto;
  border: 2px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

@media (prefers-reduced-motion: reduce) {
  .spinner { animation-duration: 2.5s; }
}

.layer-bar .layer-hint { flex: 0 0 auto; margin-right: 4px; opacity: 0.7; }
.layer-bar .chip { cursor: pointer; }
.layer-bar .chip .dot { font-style: normal; }

/* Layer chips restack the board by dragging sideways: rightmost draws on top. */
.layer-bar .chip { cursor: grab; touch-action: none; user-select: none; }
.layer-bar .chip.dragging { cursor: grabbing; opacity: 0.7; border-color: var(--accent); }

/* Where a dragged chip will land. */
.layer-bar .chip.drop-before { box-shadow: -3px 0 0 var(--accent); }
.layer-bar .chip.drop-after { box-shadow: 3px 0 0 var(--accent); }
.layer-bar .chip.dragging { z-index: 2; position: relative; }

/* A standalone toggle in the toolbar, matching the segmented controls. */
.map-toolbar > button.on { background: var(--accent); border-color: var(--accent); color: #06121f; font-weight: 600; }

/* Both canvases fill the wrap; the pointer layer sits over the board. */
.map-wrap canvas { position: absolute; inset: 0; }
.map-wrap canvas.pointer-layer { pointer-events: none; }

.view-footer .coords {
  font-size: 11px;
  color: var(--fg-dim);
  min-width: 13em;
  font-variant-numeric: tabular-nums;
}

.log-line .stamp {
  color: var(--fg-faint);
  margin-right: 8px;
  font-variant-numeric: tabular-nums;
}

/* --- first-run notice ------------------------------------------------------------ */

.dialog-foot {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  padding: 12px 18px;
  border-top: 1px solid var(--border);
  background: var(--bg-raised);
}
.dialog-body .steps {
  margin: 0;
  padding-left: 20px;
  font-size: 12.5px;
}
.dialog-body .steps li + li { margin-top: 6px; }
.dialog-body .steps .mono {
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: 3px;
  padding: 0 4px;
}
