/* BXFLW — light drafting-sheet theme per the octilinear pipe design tokens */
/* Space Grotesk, self-hosted (variable weight file, latin subset) — external
   font CDNs hang the preview pane and fail offline; local is also what the
   etch pipeline wants */
@font-face {
  font-family: "Space Grotesk";
  font-style: normal;
  font-weight: 300 700;
  font-display: swap;
  src: url("fonts/space-grotesk-latin.woff2") format("woff2");
}
@font-face {
  font-family: "Archivo Black"; /* tile nameplates - blocky display face */
  font-style: normal;
  font-weight: 400;
  font-display: swap;
  src: url("fonts/archivo-black-latin.woff2") format("woff2");
}
:root {
  --canvas: oklch(0.97 0.004 90);
  --panel: #ffffff;
  --panel-2: #f2f0ea;
  --line: #d8d4c8;
  --ink: oklch(0.22 0.01 260);
  --ink-dim: oklch(0.5 0.02 250);
  --accent: oklch(0.42 0.09 220);
  --danger: oklch(0.55 0.14 25);
  --wood: #d7b083;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  height: 100%;
  overflow: hidden;
  background: var(--canvas);
  color: var(--ink);
  font-family: "Space Grotesk", "Segoe UI", system-ui, sans-serif;
  font-size: 14px;
}

#app { display: flex; flex-direction: column; height: 100vh; }

/* ---------- Header ---------- */
header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 14px;
  background: var(--panel);
  border-bottom: 2px solid var(--ink);
  flex-wrap: wrap;
}

.logo { font-weight: 700; letter-spacing: 3px; font-size: 16px; }
.logo span { color: var(--accent); }
.logo em {
  font-style: normal; font-weight: 500; letter-spacing: 1px;
  font-size: 10px; color: var(--ink-dim); margin-left: 8px; text-transform: uppercase;
}

#patch-name {
  background: var(--panel-2);
  border: 1px solid var(--line);
  color: var(--ink);
  border-radius: 6px;
  padding: 6px 10px;
  width: 220px;
  font-family: inherit;
}

header button, header select {
  background: var(--panel-2);
  border: 1px solid var(--line);
  color: var(--ink);
  border-radius: 6px;
  padding: 6px 12px;
  cursor: pointer;
  font-family: inherit;
  font-weight: 600;
}
header button:hover { border-color: var(--accent); }
header button.danger:hover { border-color: var(--danger); color: var(--danger); }
header button.etch { border-color: var(--ink); }
header button#btn-etch-view.active { background: var(--wood); border-color: var(--ink); }

.spacer { flex: 1; }
.hint { color: var(--ink-dim); font-size: 11.5px; }

/* ---------- Main layout ---------- */
main { display: flex; flex: 1; min-height: 0; }

/* ---------- Drawer ---------- */
#drawer {
  width: 250px;
  background: var(--panel);
  border-right: 1px solid var(--line);
  overflow-y: auto;
  transition: width 0.18s ease;
  flex-shrink: 0;
}
#drawer.collapsed { width: 0; border-right: none; overflow: hidden; }

#drawer-toggle {
  position: absolute;
  left: 0;
  top: 50%;
  transform: translateY(-50%);
  z-index: 10;
  background: var(--panel);
  border: 1px solid var(--line);
  border-left: none;
  color: var(--ink-dim);
  border-radius: 0 8px 8px 0;
  padding: 14px 4px;
  cursor: pointer;
  font-size: 12px;
}
#drawer-toggle:hover { color: var(--ink); border-color: var(--accent); }

.drawer-cat {
  padding: 10px 12px 4px;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 1px;
  color: var(--ink-dim);
  font-weight: 600;
}

.drawer-item {
  display: flex;
  align-items: center;
  gap: 10px;
  margin: 3px 8px;
  padding: 7px 9px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel-2);
  cursor: grab;
  user-select: none;
}
.drawer-item:hover { border-color: var(--accent); }
.drawer-item.placed { opacity: 0.4; }
.drawer-item .thumb { flex-shrink: 0; }
.drawer-item .meta { min-width: 0; }
.drawer-item .name { font-size: 12px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.drawer-item .dims { font-size: 11px; color: var(--ink-dim); }

/* ---------- Canvas ---------- */
#canvas-wrap { position: relative; flex: 1; min-width: 0; }
svg#canvas { width: 100%; height: 100%; display: block; cursor: default; }
svg#canvas, #drawer, #legend, #scale-note, .hint { user-select: none; -webkit-user-select: none; }
svg#canvas.panning { cursor: grabbing; }

.gear-group { cursor: move; }
.port-hit { cursor: crosshair; }
.pipe-hit { fill: none; stroke: transparent; stroke-width: 0.4; cursor: pointer; }

/* ---------- Tooltip / toast / legend ---------- */
#tooltip {
  position: absolute;
  display: none;
  background: var(--ink);
  color: #f5f4ef;
  border-radius: 6px;
  padding: 6px 10px;
  font-size: 12px;
  pointer-events: none;
  z-index: 30;
  max-width: 280px;
}
#tooltip .t-type { color: #b9c0cc; }

#toast {
  position: absolute;
  bottom: 52px;
  left: 50%;
  transform: translateX(-50%);
  background: #fbeeee;
  border: 1.5px solid var(--danger);
  color: #7c2d1e;
  border-radius: 8px;
  padding: 8px 16px;
  display: none;
  z-index: 30;
  font-size: 13px;
  font-weight: 600;
}
#toast.ok { background: #edf6ef; border-color: #3f7d5a; color: #1e5238; }

#legend {
  position: absolute;
  bottom: 12px;
  left: 14px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  background: #f8f4ec;
  border: 1.5px solid #3a322a;
  border-top-width: 5px;            /* the heavy rule rides the TOP */
  border-radius: 0;
  padding: 7px 10px;
  z-index: 20;
}
.legend-row { display: flex; align-items: baseline; gap: 5px; } /* tag glyphs share the bullet letters' baseline */
.legend-bullet {
  width: 17px;
  height: 17px;
  border-radius: 50%;
  color: #f8f4ec;
  font: 700 9.4px/17px "Helvetica Neue", Helvetica, Arial, sans-serif;
  text-align: center;
  letter-spacing: -0.2px;
  user-select: none;
  flex-shrink: 0;
}
.legend-name {
  font: 700 9.4px/1 "Helvetica Neue", Helvetica, Arial, sans-serif; /* matches the bullet type */
  color: #3a322a;
}
.legend-arrow {
  position: absolute;               /* to the RIGHT of the card, small gap; */
  top: -5px;                        /* spans the card's FULL outer height */
  left: calc(100% + 11px);          /* (border to border, no built-in margin) */
  height: calc(100% + 6.5px);       /* padding box + both borders */
  aspect-ratio: 1 / 1;
}
.rot-ico { width: 17px; height: 12px; vertical-align: -2px; } /* solid Standards-style turn glyph */

#scale-note {
  position: absolute;
  top: 10px;
  right: 14px;
  color: var(--ink-dim);
  font-size: 12px;
  z-index: 20;
  background: rgba(255, 255, 255, 0.9);
  padding: 4px 10px;
  border-radius: 6px;
  border: 1px solid var(--line);
}

/* ---------- Add-a-box dialog ---------- */
dialog#custom-dlg {
  border: 1.5px solid #3a322a;
  border-left-width: 6px;           /* the heavy rule, card-style */
  border-radius: 0;
  background: #f8f4ec;
  color: #3a322a;
  padding: 18px 20px 16px;
  min-width: 460px;
  max-width: 540px;
}
dialog#custom-dlg::backdrop { background: rgba(58, 50, 42, 0.4); }
#custom-form h3 {
  font: 400 18px/1 "Archivo Black", "Space Grotesk", sans-serif;
  margin: 0 0 12px;
}
.cd-title-row { display: flex; align-items: center; gap: 8px; margin-bottom: 12px; }
.cd-title-row span, .cd-ports-head span {
  font: 600 11px "Space Grotesk", sans-serif;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}
.cd-title-row input { flex: 1; }
#custom-form input[type="text"], #custom-form select {
  font: 500 13px "Space Grotesk", sans-serif;
  color: #3a322a;
  background: #fff;
  border: 1px solid #3a322a;
  border-radius: 0;
  padding: 5px 7px;
}
.cd-ports-head {
  display: flex; align-items: center; justify-content: space-between;
  border-top: 3px solid #3a322a;    /* card top rule */
  padding-top: 8px;
}
#custom-form button {
  font: 600 12px "Space Grotesk", sans-serif;
  color: #3a322a;
  background: #fff;
  border: 1px solid #3a322a;
  border-radius: 0;
  padding: 4px 10px;
  cursor: pointer;
}
#custom-form button:hover { background: #efe8db; }
#cd-save { background: #3a322a; color: #f8f4ec; }
#cd-save:hover { background: #1660c4; border-color: #1660c4; }
.cd-port {
  display: grid;
  grid-template-columns: 1fr 148px auto auto;
  gap: 6px;
  align-items: center;
  margin-top: 6px;
}
.cd-pair {
  display: flex; align-items: center; gap: 3px;
  font: 500 11px "Space Grotesk", sans-serif;
  user-select: none;
}
.cd-pair.off { visibility: hidden; }
.cd-del { line-height: 1; }
.cd-hint {
  font: 400 11px/1.45 "Space Grotesk", sans-serif;
  color: #7a6f60;
  margin: 10px 0 0;
}
.cd-actions {
  display: flex; justify-content: flex-end; gap: 8px;
  margin-top: 14px;
}

/* ---------- Dark mode: warm dark grey, never black ---------- */
body.dark {
  --canvas: #2e2a25;
  --panel: #262320;
  --panel-2: #322d27;
  --line: #4a443b;
  --ink: #e9e1d2;
  --ink-dim: #a89d8c;
  --wood: #6b5738;
}
body.dark header button#btn-dark.active { background: #e9e1d2; color: #262320; border-color: #e9e1d2; }
body.dark .drawer-item { background: #322d27; }
body.dark #legend { background: #38332c; border-color: #e9e1d2; }
body.dark .legend-name { color: #e9e1d2; }
body.dark .legend-bullet { color: #38332c; }
body.dark #scale-note { background: rgba(38, 35, 32, 0.92); }
body.dark #tooltip { background: #e9e1d2; color: #2e2a25; }
body.dark #toast { background: #4a2a22; border-color: #f08238; color: #f3d9c8; }
body.dark #toast.ok { background: #26382c; border-color: #55a374; color: #cfe8d8; }
body.dark dialog#custom-dlg { background: #38332c; border-color: #e9e1d2; color: #e9e1d2; }
body.dark #custom-form input[type="text"], body.dark #custom-form select {
  background: #262320; color: #e9e1d2; border-color: #a89d8c;
}
body.dark .cd-ports-head { border-top-color: #e9e1d2; }
body.dark #custom-form button { background: #322d27; color: #e9e1d2; border-color: #a89d8c; }
body.dark #custom-form button:hover { background: #453f36; }
body.dark #cd-save { background: #e9e1d2; color: #262320; }
body.dark #cd-save:hover { background: #1660c4; border-color: #1660c4; color: #f8f4ec; }
body.dark .cd-hint { color: #a89d8c; }

.via-hit { cursor: grab; }

/* recovery code + patch book dialogs share the card chrome */
dialog#code-dlg, dialog#book-dlg {
  border: 1.5px solid #3a322a; border-top-width: 6px; border-radius: 0;
  background: #f8f4ec; color: #3a322a; padding: 18px 20px 16px; min-width: 420px; max-width: 560px;
}
dialog#code-dlg::backdrop, dialog#book-dlg::backdrop { background: rgba(58, 50, 42, 0.4); }
dialog#code-dlg h3, dialog#book-dlg h3 { font: 400 18px/1 "Archivo Black", "Space Grotesk", sans-serif; margin: 0 0 10px; }
#code-value {
  font: 700 22px/1.2 "Space Grotesk", monospace; letter-spacing: 1.5px;
  background: #fff; border: 1px solid #3a322a; padding: 12px 14px; margin: 12px 0 4px;
  text-align: center; user-select: all;
}
#book-list { max-height: 50vh; overflow-y: auto; margin-top: 6px; }
.book-row {
  display: flex; align-items: center; gap: 8px;
  border-top: 1px solid #d8d0c0; padding: 7px 2px;
  font: 500 13px "Space Grotesk", sans-serif;
}
.book-row .bname { flex: 1; font-weight: 600; }
.book-row .bwhere { color: #7a6f60; font-size: 11px; }
.book-row button {
  font: 600 12px "Space Grotesk", sans-serif; color: #3a322a; background: #fff;
  border: 1px solid #3a322a; border-radius: 0; padding: 3px 9px; cursor: pointer;
}
.book-row button.danger { border-color: #b3261e; color: #b3261e; }
dialog#book-dlg .cd-actions button, dialog#code-dlg .cd-actions button {
  font: 600 12px "Space Grotesk", sans-serif; color: #3a322a; background: #fff;
  border: 1px solid #3a322a; border-radius: 0; padding: 4px 10px; cursor: pointer;
}
body.dark dialog#code-dlg, body.dark dialog#book-dlg { background: #38332c; border-color: #e9e1d2; color: #e9e1d2; }
body.dark #code-value { background: #262320; border-color: #a89d8c; color: #e9e1d2; }
body.dark .book-row { border-top-color: #4a443b; }
body.dark .book-row button, body.dark dialog#book-dlg .cd-actions button, body.dark dialog#code-dlg .cd-actions button {
  background: #322d27; color: #e9e1d2; border-color: #a89d8c;
}

.title-hit { cursor: move; }

header button#btn-patch.active { background: #0e8c46; border-color: #0e8c46; color: #f8f4ec; }
