/* NamTrack KB — mobile first, high contrast, readable in sunlight. */

:root {
  /* NamTrack brand (namtrack.com / logo) */
  --brand-orange: #dc6a38;
  --brand-blue: #036cc9;
  --brand-navy: #102136;

  --bg: #ffffff;
  --fg: var(--brand-navy);
  --muted: #4d5d6d;
  --line: #c5cdd5;
  --card: #f4f6f8;
  --accent: var(--brand-blue);   /* links, buttons: 5.3:1 on white */
  --accent-fg: #ffffff;
  --highlight: var(--brand-orange);
  --topbar: #ffffff;
  --bottombar: var(--brand-navy);
  --ok: #1b6e20;
  --warn: #9a4a00;
  --housing: #e7ebee;
  --tap: 48px;
  color-scheme: light;
}

/* data-theme is set by the inline script in base.html: the user's choice
   (Auto / Light / Dark, remembered per device), resolved against the OS. */
:root[data-theme="dark"] {
  --bg: #0b1726;
  --fg: #f2f5f8;
  --muted: #b3c0cd;
  --line: #2c3e55;
  --card: #13243a;
  --accent: #5cb0ff;
  --accent-fg: #0b1726;
  --highlight: #f08a55;
  --topbar: #13243a;
  --bottombar: #13243a;
  --ok: #7bd88f;
  --warn: #ffb35c;
  --housing: #1e3048;
  color-scheme: dark;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html { -webkit-text-size-adjust: 100%; scroll-padding-top: 64px; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--fg);
  font: 17px/1.45 Roboto, system-ui, -apple-system, "Segoe UI", sans-serif;
  padding-bottom: calc(var(--tap) + 16px + env(safe-area-inset-bottom));
}

a { color: var(--accent); }
h1 { font-size: 1.6rem; line-height: 1.2; margin: 16px 0 4px; }
h2 { font-size: 1.3rem; margin: 32px 0 12px; padding-top: 6px; border-top: 3px solid var(--highlight); }
h3 { font-size: 1.15rem; margin: 0 0 6px; display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
h4 { font-size: 1rem; margin: 16px 0 8px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }

.wrap { max-width: 760px; margin: 0 auto; padding: 0 16px; }

/* --- bars ---------------------------------------------------------------- */

.bar {
  display: flex; align-items: center; gap: 8px;
  padding: 0 16px; min-height: 56px;
}
.bar a { color: inherit; text-decoration: none; font-weight: 700; }
.bar.top {
  justify-content: space-between;
  background: var(--topbar); color: var(--fg);
  border-bottom: 3px solid var(--highlight);
  padding-top: env(safe-area-inset-top);
}
.brand { display: inline-flex; align-items: center; gap: 10px; min-height: var(--tap); }
.brand span { font-size: .95rem; font-weight: 700; color: var(--muted); letter-spacing: .02em; }
.logo { display: block; height: 40px; width: auto; }
.brand.small .logo { height: 30px; }
/* the logo is drawn for a white background */
[data-theme="dark"] .logo { background: #fff; border-radius: 6px; padding: 2px 4px; box-sizing: content-box; }

.theme-toggle {
  flex: none; display: inline-flex; align-items: center; justify-content: center;
  width: var(--tap); height: var(--tap); margin-right: -8px; padding: 0;
  border: 0; background: none; color: var(--fg); cursor: pointer;
}
.theme-toggle svg { width: 26px; height: 26px; display: none; }
:root[data-theme-pref="auto"] .theme-toggle .i-auto,
:root[data-theme-pref="light"] .theme-toggle .i-light,
:root[data-theme-pref="dark"] .theme-toggle .i-dark { display: block; }
.bar-end { display: inline-flex; align-items: center; gap: 4px; }
:root:not([data-theme-pref]) .theme-toggle { display: none; } /* no JS */
.back {
  display: inline-flex; align-items: center; min-height: var(--tap);
  padding-right: 12px; font-size: 1.05rem; color: var(--accent) !important;
}

.bar.bottom {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 10;
  background: var(--bottombar); color: #fff; border-top: 3px solid var(--highlight);
  justify-content: space-around;
  padding-bottom: env(safe-area-inset-bottom);
}
.bar.bottom a { flex: 1; text-align: center; min-height: var(--tap); line-height: var(--tap); }

/* --- search -------------------------------------------------------------- */

.search { display: flex; gap: 8px; margin: 16px 0; position: relative; }
.search input {
  flex: 1; min-width: 0; min-height: 52px;
  font-size: 1.1rem; padding: 0 14px;
  border: 2px solid var(--fg); border-radius: 10px;
  background: var(--bg); color: var(--fg);
}
.search input:focus { outline: 3px solid var(--accent); outline-offset: 1px; }
.search button, .button {
  display: inline-flex; align-items: center; justify-content: center;
  min-height: 52px; padding: 0 18px;
  font: inherit; font-weight: 700;
  border: 0; border-radius: 10px;
  background: var(--accent); color: var(--accent-fg); text-decoration: none;
}
.search.htmx-request::after {
  content: ""; position: absolute; left: 0; right: 0; bottom: -6px; height: 3px;
  background: var(--highlight); animation: load 1s linear infinite;
}
@keyframes load { from { transform: scaleX(0); transform-origin: left; } to { transform: scaleX(1); transform-origin: left; } }

/* --- lists and cards ----------------------------------------------------- */

.list { list-style: none; margin: 0; padding: 0; display: grid; gap: 10px; }
.card {
  display: block; background: var(--card);
  border: 1px solid var(--line); border-radius: 12px;
  padding: 14px; margin-bottom: 12px;
}
.card.link { color: inherit; text-decoration: none; min-height: var(--tap); margin: 0; }
.card.link:active { background: var(--line); }
.card .title { display: block; font-size: 1.15rem; font-weight: 700; }
.meta { color: var(--muted); margin: 2px 0; }
.card .meta { display: block; }
.empty { color: var(--muted); font-style: italic; }
.notes { white-space: pre-line; margin: 6px 0; }
.where { font-weight: 600; }
.warn { color: var(--warn); font-weight: 600; }
.mono, .code { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }
.code { color: var(--muted); font-size: .9rem; margin: 0 0 8px; }
.unit { font-weight: 400; color: var(--muted); }
.prov { color: var(--muted); font-size: .85rem; margin: 10px 0 0; }

.chip, .badge, .role {
  display: inline-block; font-size: .8rem; font-weight: 700;
  padding: 2px 8px; border-radius: 999px;
  border: 1px solid var(--fg); white-space: nowrap; vertical-align: middle;
}
.badge { border-color: var(--muted); color: var(--muted); font-weight: 600; }
.badge.ok { border-color: var(--ok); color: var(--ok); }
.badge.warn { border-color: var(--warn); color: var(--warn); }
.card .chip { margin-top: 6px; }

.kv { display: grid; grid-template-columns: max-content 1fr; gap: 4px 12px; margin: 8px 0; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; overflow-wrap: anywhere; }

/* --- jump nav ------------------------------------------------------------ */

.jump {
  position: sticky; top: 0; z-index: 5;
  display: flex; gap: 6px; overflow-x: auto; scrollbar-width: none;
  margin: 16px -16px 0; padding: 8px 16px;
  background: var(--bg); border-bottom: 1px solid var(--line);
}
.jump a {
  flex: none; display: inline-flex; align-items: center; gap: 6px;
  min-height: 40px; padding: 0 12px; border-radius: 999px;
  border: 2px solid var(--fg); color: var(--fg); text-decoration: none; font-weight: 600;
}
.jump b { font-weight: 800; }

/* --- connection points --------------------------------------------------- */

.points { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.points > li {
  display: grid; grid-template-columns: 7.2em 1fr; gap: 10px; align-items: start;
  background: var(--bg); border: 1px solid var(--line); border-radius: 10px; padding: 10px;
}
.points > li:target, .pins tr:target { outline: 3px solid var(--accent); }
.points .role { text-align: center; white-space: normal; }
.pinref { display: inline-block; min-height: 32px; padding-top: 4px; font-weight: 600; }

/* role colours, shared by chips and connector pins */
.role { background: var(--bg); color: var(--fg); }
.r-permanent_plus { --rc: #c62828; --rt: #fff; }
.r-ground         { --rc: #3e2723; --rt: #fff; }
.r-ignition       { --rc: #f9a825; --rt: #000; }
.r-accessory      { --rc: #fdd835; --rt: #000; }
.r-can_h          { --rc: #e65100; --rt: #fff; }
.r-can_l          { --rc: #2e7d32; --rt: #fff; }
.r-k_line, .r-l_line { --rc: #6a1b9a; --rt: #fff; }
.r-signal         { --rc: #1565c0; --rt: #fff; }
.r-shield         { --rc: #757575; --rt: #fff; }
[data-theme="dark"] .r-ground { --rc: #8d6e63; }
.role[class*="r-"] { background: var(--rc, var(--bg)); color: var(--rt, var(--fg)); border-color: var(--rc, var(--fg)); }

/* --- wire colours -------------------------------------------------------- */

.wire { display: inline-flex; align-items: center; gap: 6px; font-weight: 600; }
.swatch {
  position: relative; display: inline-block; width: 34px; height: 16px;
  border-radius: 8px; border: 1px solid var(--fg); overflow: hidden;
  background: var(--c, transparent);
}
.swatch[class*=" s-"]::after {
  content: ""; position: absolute; left: 0; right: 0; top: 5px; height: 5px; background: var(--s);
}
.c-none { background: repeating-linear-gradient(45deg, transparent 0 4px, var(--line) 4px 8px); }
.c-black { --c: #111; }      .s-black { --s: #111; }
.c-brown { --c: #6d4c41; }   .s-brown { --s: #6d4c41; }
.c-red { --c: #d32f2f; }     .s-red { --s: #d32f2f; }
.c-orange { --c: #f57c00; }  .s-orange { --s: #f57c00; }
.c-yellow { --c: #fdd835; }  .s-yellow { --s: #fdd835; }
.c-green { --c: #2e7d32; }   .s-green { --s: #2e7d32; }
.c-blue { --c: #1565c0; }    .s-blue { --s: #1565c0; }
.c-violet { --c: #7b1fa2; }  .s-violet { --s: #7b1fa2; }
.c-grey { --c: #9e9e9e; }    .s-grey { --s: #9e9e9e; }
.c-white { --c: #fafafa; }   .s-white { --s: #fafafa; }
.c-pink { --c: #f48fb1; }    .s-pink { --s: #f48fb1; }
.c-turquoise { --c: #26c6da; } .s-turquoise { --s: #26c6da; }
.c-gold { --c: #c9a227; }    .s-gold { --s: #c9a227; }
.c-silver { --c: #c0c0c0; }  .s-silver { --s: #c0c0c0; }

/* --- connector drawings -------------------------------------------------- */

.connview { margin: 12px 0; }
.connview > input { position: absolute; opacity: 0; pointer-events: none; }
.seg { display: flex; border: 2px solid var(--fg); border-radius: 10px; overflow: hidden; }
.seg label {
  flex: 1; text-align: center; min-height: 44px; line-height: 40px;
  font-weight: 700; cursor: pointer; user-select: none;
}
.connview > input:nth-of-type(1):checked ~ .seg label:nth-child(1),
.connview > input:nth-of-type(2):checked ~ .seg label:nth-child(2) {
  background: var(--fg); color: var(--bg);
}
.connview > input:focus-visible ~ .seg { outline: 3px solid var(--accent); outline-offset: 2px; }
.connview figure { margin: 10px 0 0; }
.connview .side-b { display: none; }
.connview > input:nth-of-type(2):checked ~ .side-a { display: none; }
.connview > input:nth-of-type(2):checked ~ .side-b { display: block; }
.connview figcaption { color: var(--muted); font-size: .9rem; margin-top: 4px; }

svg.conn { display: block; width: 100%; max-width: 520px; height: auto; margin: 0 auto; }
svg.conn .housing { fill: var(--housing); stroke: var(--fg); stroke-width: 3; }
svg.conn .pin circle { fill: var(--bg); stroke: var(--muted); stroke-width: 2; }
svg.conn .pin[class*=" r-"]:not(.on) circle { stroke-dasharray: 4 3; stroke: var(--rc); }
svg.conn .pin.on circle { fill: var(--rc, var(--fg)); stroke: var(--fg); stroke-width: 3; }
svg.conn .pin text {
  font: 700 15px system-ui, sans-serif; text-anchor: middle; fill: var(--fg); pointer-events: none;
}
svg.conn .pin.on text { fill: var(--rt, var(--bg)); }

.pins { width: 100%; border-collapse: collapse; margin: 12px 0; }
.pins th { text-align: left; font-size: .85rem; color: var(--muted); border-bottom: 2px solid var(--fg); padding: 4px 6px; }
.pins td { border-bottom: 1px solid var(--line); padding: 8px 6px; vertical-align: top; }
.pinno { font-weight: 800; font-size: 1.1rem; width: 3em; }

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

.sources { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.sources > li { background: var(--bg); border: 1px solid var(--line); border-radius: 10px; padding: 10px; }
.srchead { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.chip.src { background: var(--fg); color: var(--bg); }
.prio { color: var(--muted); font-size: .9rem; }

/* --- photos -------------------------------------------------------------- */

.photos { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 8px; margin: 10px 0; }
.photo { display: block; color: inherit; text-decoration: none; }
.photo .frame { position: relative; display: block; }
.photo img {
  display: block; width: 100%; height: auto; aspect-ratio: 4 / 3; object-fit: cover;
  border-radius: 8px; border: 1px solid var(--line); background: var(--card);
}
.photo .cap { display: block; font-size: .85rem; color: var(--muted); margin-top: 2px; }
.badge-marks {
  position: absolute; right: 6px; bottom: 6px; padding: 2px 8px; border-radius: 999px;
  background: var(--brand-orange); color: #fff; font-size: .8rem; font-weight: 700;
}

/* --- buttons and fields -------------------------------------------------- */

.button.big { min-height: 64px; font-size: 1.1rem; gap: 10px; flex: 1; cursor: pointer; }
.button.big svg { width: 28px; height: 28px; }
.button.small { min-height: 44px; padding: 0 14px; font-size: .95rem; cursor: pointer; }
.button.secondary { background: var(--bg); color: var(--accent); border: 2px solid var(--accent); }
.button.danger { background: #c62828; color: #fff; }
.button:disabled { opacity: .5; }
.linkish { font: inherit; font-weight: 700; color: inherit; background: none; border: 0; text-decoration: underline; padding: 8px; min-height: 40px; cursor: pointer; }

.field { display: block; margin: 12px 0; }
.field > span { display: block; font-weight: 600; margin-bottom: 4px; }
.field small { font-weight: 400; color: var(--muted); }
.field input, .field select {
  width: 100%; min-height: 48px; padding: 0 12px; font: inherit;
  border: 2px solid var(--fg); border-radius: 10px; background: var(--bg); color: var(--fg);
}
.field input:focus, .field select:focus { outline: 3px solid var(--accent); outline-offset: 1px; }

.addphoto {
  display: inline-flex; align-items: center; min-height: 44px; padding: 0 14px; margin-top: 6px;
  border: 2px dashed var(--accent); border-radius: 10px; font-weight: 700; text-decoration: none;
}
.marked { display: block; margin-top: 6px; font-size: .9rem; color: var(--muted); }
.marked a { display: inline-block; min-height: 32px; padding: 4px 4px 0; font-weight: 600; }

/* --- upload page --------------------------------------------------------- */

.pickers { display: flex; gap: 10px; margin: 16px 0; flex-wrap: wrap; }
.uploads { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.upload {
  display: grid; grid-template-columns: 88px 1fr; gap: 10px; align-items: start;
  background: var(--card); border: 1px solid var(--line); border-radius: 10px; padding: 8px;
}
.upload.preparing { display: block; font-weight: 600; }
.upload img { width: 88px; height: 66px; object-fit: cover; border-radius: 6px; }
.upload .cap { font-weight: 700; }
.upload .status { font-size: .9rem; color: var(--muted); }
.upload.done .status { color: var(--ok); font-weight: 700; }
.upload.failed .status { color: var(--warn); font-weight: 700; }
.upload progress { width: 100%; height: 10px; accent-color: var(--brand-orange); }
.upload .actions { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 6px; }
.hint { color: var(--muted); font-size: .9rem; }

.upload-banner {
  position: fixed; left: 8px; right: 8px; z-index: 20;
  bottom: calc(var(--tap) + 14px + env(safe-area-inset-bottom));
  background: var(--brand-orange); color: #fff; font-weight: 700;
  border-radius: 10px; padding: 8px 12px; box-shadow: 0 4px 16px rgba(0,0,0,.25);
}

/* --- photo viewer -------------------------------------------------------- */

.photo-page figure { margin: 12px -16px 0; }
svg.annot { display: block; width: 100%; height: auto; background: #000; }
.sr-toggle { position: absolute; opacity: 0; pointer-events: none; }
.sr-toggle:not(:checked) ~ figure .shapes { display: none; }
.switch { display: inline-flex; align-items: center; gap: 10px; min-height: var(--tap); font-weight: 600; cursor: pointer; }
.switch .track { width: 46px; height: 26px; border-radius: 13px; background: var(--line); position: relative; }
.switch .track::after {
  content: ""; position: absolute; top: 3px; left: 3px; width: 20px; height: 20px;
  border-radius: 50%; background: #fff; box-shadow: 0 1px 3px rgba(0,0,0,.4); transition: transform .15s;
}
.sr-toggle:checked ~ .switch .track { background: var(--accent); }
.sr-toggle:checked ~ .switch .track::after { transform: translateX(20px); }
.sr-toggle:focus-visible ~ .switch { outline: 3px solid var(--accent); }
.shape.dim { opacity: .3; }
.shape.hl { animation: pulse 1.2s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: .45; } }

.legend { list-style: none; margin: 8px 0; padding: 0; display: grid; gap: 6px; }
.legend li { display: flex; gap: 10px; align-items: center; min-height: 40px; }
.legend li.dim { opacity: .5; }
.legend .key {
  flex: none; width: 32px; height: 32px; border-radius: 50%; display: inline-flex; align-items: center; justify-content: center;
  background: var(--k); color: #000; font-weight: 800; border: 2px solid var(--fg);
}
.legend .key:not(.k-marker) { border-radius: 6px; }
.legend .lbl { font-weight: 600; margin-right: 6px; }
.legend a { font-weight: 600; }
.caption-form { margin-top: 16px; }

.bar.bottom a.primary { background: var(--accent); color: var(--accent-fg); }

/* --- annotation editor --------------------------------------------------- */

.editor {
  position: fixed; inset: 0; display: flex; flex-direction: column;
  background: #000; color: #fff; z-index: 50;
}
.ed-top {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: env(safe-area-inset-top) 12px 0; min-height: 56px; background: var(--brand-navy);
  border-bottom: 3px solid var(--brand-orange);
}
.ed-top .back { color: #fff !important; }
.ed-status { flex: 1; text-align: center; font-size: .9rem; font-weight: 600; }
.ed-status.ok { color: #7bd88f; }
.ed-status.err { color: #ffb35c; }
.ed-wrap { position: relative; flex: 1; overflow: hidden; touch-action: none; user-select: none; -webkit-user-select: none; }
.ed-wrap[data-tool="select"] { cursor: grab; }
.ed-wrap:not([data-tool="select"]) { cursor: crosshair; }
.ed-stage { position: absolute; left: 0; top: 0; transform-origin: 0 0; }
.ed-stage svg { display: block; }
.ed-stage .shape { cursor: pointer; }
.ed-banner {
  position: absolute; left: 8px; right: 8px; top: 8px; padding: 8px 12px; border-radius: 10px;
  background: var(--brand-orange); color: #fff; font-weight: 700;
}
.ed-panel { background: var(--brand-navy); padding: 8px 12px; border-top: 1px solid #2c3e55; }
.ed-panel .field { margin: 6px 0; }
.ed-panel .field > span { color: #b3c0cd; font-size: .9rem; }
.ed-panel input, .ed-panel select { background: #0b1726; color: #fff; border-color: #b3c0cd; }
.ed-panel-head { display: flex; align-items: center; gap: 8px; }
.ed-panel-head b { flex: 1; }
.ed-tools { background: var(--brand-navy); padding: 4px 4px calc(4px + env(safe-area-inset-bottom)); }
.ed-row { display: flex; justify-content: space-between; gap: 2px; }
.ed-row button {
  flex: 1; min-width: 0; min-height: 56px; padding: 4px 0;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  background: none; border: 0; border-radius: 8px; color: #fff; font: 600 .72rem/1 Roboto, system-ui, sans-serif; cursor: pointer;
}
.ed-row button svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.ed-row button[aria-pressed="true"] { background: var(--brand-orange); }
.ed-row button:disabled { opacity: .35; }
.ed-row2 { align-items: center; }
.ed-colours { flex: 3; display: flex; justify-content: space-around; }
.ed-colours button {
  flex: none; width: 40px; height: 40px; min-height: 40px; border-radius: 50%;
  background: var(--c); border: 3px solid #4d5d6d;
}
.ed-colours button[aria-checked="true"] { border-color: #fff; box-shadow: 0 0 0 3px var(--brand-orange); }

/* --- edit forms ---------------------------------------------------------- */

form.edit { padding-bottom: 8px; }
fieldset.field { border: 0; padding: 0; min-width: 0; }
fieldset.field > legend { font-weight: 600; margin-bottom: 6px; padding: 0; }
.field textarea {
  width: 100%; padding: 10px 12px; font: inherit; resize: vertical;
  border: 2px solid var(--fg); border-radius: 10px; background: var(--bg); color: var(--fg);
}
.field textarea:focus { outline: 3px solid var(--accent); outline-offset: 1px; }
.field input[aria-invalid], .field:has(.ferr) input, .field:has(.ferr) textarea { border-color: var(--warn); }
.ferr { display: block; color: var(--warn); font-weight: 700; font-size: .9rem; margin-top: 4px; }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 10px; }

.warnbox {
  border: 2px solid var(--warn); border-radius: 10px; padding: 10px 12px; margin: 12px 0;
  color: var(--fg); background: var(--card);
}
.warnbox .similar { margin: 6px 0; padding-left: 20px; }
.warnbox .similar a { display: inline-block; min-height: 36px; padding-top: 6px; font-weight: 600; }
.draftbar { background: var(--card); border-left: 4px solid var(--highlight); padding: 6px 10px; margin: 8px 0; }

.check {
  display: flex; align-items: center; gap: 10px; min-height: var(--tap); margin: 8px 0; font-weight: 600; cursor: pointer;
}
.check input { width: 24px; height: 24px; accent-color: var(--accent); flex: none; }

.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip-radio { position: relative; cursor: pointer; }
.chip-radio input { position: absolute; opacity: 0; pointer-events: none; }
.chip-radio > span {
  display: inline-flex; align-items: center; min-height: 44px; padding: 0 14px;
  border: 2px solid var(--line); border-radius: 999px; font-weight: 600; background: var(--bg);
}
.chip-radio input:checked + span { border-color: var(--fg); background: var(--fg); color: var(--bg); box-shadow: 0 0 0 3px var(--highlight); }
.chip-radio input:focus-visible + span { outline: 3px solid var(--accent); outline-offset: 2px; }
.chips.roles .chip-radio > span.role { font-size: .95rem; border-width: 2px; }
.chips.roles .chip-radio input:not(:checked) + span.role { opacity: .55; }
.chips.roles .chip-radio input:checked + span.role {
  background: var(--rc, var(--fg)); color: var(--rt, var(--bg)); border-color: var(--fg); opacity: 1;
}

.wirepick .swatches { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 6px; }
.wirepick .sub { display: block; color: var(--muted); font-size: .9rem; margin: 4px 0; }
.wirepick .sw {
  width: 44px; height: 44px; border-radius: 50%; border: 2px solid var(--fg);
  background: var(--c, transparent); cursor: pointer; padding: 0;
}
.wirepick .sw.c-none { background: repeating-linear-gradient(45deg, transparent 0 5px, var(--line) 5px 10px); }
.wirepick .sw[aria-pressed="true"] { box-shadow: 0 0 0 3px var(--bg), 0 0 0 6px var(--highlight); }

.who-known { margin: 12px 0; color: var(--muted); }

.formbar {
  position: sticky; bottom: 0; z-index: 5; display: flex; flex-direction: row-reverse; gap: 8px;
  margin: 16px -16px 0; padding: 10px 16px calc(10px + env(safe-area-inset-bottom));
  background: var(--bg); border-top: 3px solid var(--highlight);
}
.formbar .button { flex: 1; min-height: 52px; cursor: pointer; }

.actions-row { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }

.toast {
  position: fixed; left: 50%; top: 70px; transform: translateX(-50%); z-index: 60;
  background: var(--ok); color: #fff; font-weight: 700; padding: 10px 18px; border-radius: 999px;
  box-shadow: 0 4px 16px rgba(0,0,0,.25); transition: opacity .4s;
}
.toast.out { opacity: 0; }

/* --- function sources: wires to tap -------------------------------------- */

.taps { display: grid; gap: 6px; margin: 8px 0; }
.wireref {
  display: grid; grid-template-columns: 7.2em 1fr; gap: 10px; align-items: start;
  padding: 8px; border: 1px solid var(--line); border-radius: 10px;
  background: var(--card); color: inherit; text-decoration: none; min-height: var(--tap);
}
.wireref .role { text-align: center; white-space: normal; }
.wireref .body { display: flex; flex-direction: column; gap: 2px; }
.wireref:active { background: var(--line); }
.mono.big { font-size: 1.15rem; font-weight: 700; }
.mono small { font-family: Roboto, system-ui, sans-serif; font-weight: 400; color: var(--muted); }
.prio.best { color: var(--ok); font-weight: 700; }

/* --- source / bus forms: show only what applies --------------------------- */

.row3 { display: grid; grid-template-columns: 1.2fr 1fr 1fr; gap: 0 8px; }
.hint { display: block; color: var(--muted); font-size: .85rem; margin-top: 4px; }
.hint code, .mono { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }
.field input.mono { font-size: 1.1rem; letter-spacing: .03em; }
.chip-radio > span small { font-weight: 400; color: inherit; opacity: .75; margin-left: 4px; }
.sigdbc { border-top: 1px solid var(--line); padding-top: 8px; }
.if-can, .if-wired { border-left: 4px solid var(--highlight); padding-left: 12px; margin: 12px 0; }

/* Without :has() support every section simply stays visible. */
@supports selector(:has(a)) {
  .source-form .if-custom, .source-form .if-can, .source-form .if-wired,
  .source-form .if-existing, .source-form .if-newsig, .source-form .if-pulses,
  .source-form .if-formula, .bus-form .if-newprofile { display: none; }
  .source-form:has(select[name="function"] option[value="_custom"]:checked) .if-custom { display: block; }
  .source-form:has(input[name="source_type"][value="can"]:checked) .if-can { display: block; }
  .source-form:has(input[name="source_type"]:checked):not(:has(input[name="source_type"][value="can"]:checked)) .if-wired { display: block; }
  .source-form:has(input[name="signal_mode"][value="existing"]:checked) .if-existing { display: block; }
  .source-form:has(input[name="signal_mode"][value="new"]:checked) .if-newsig { display: block; }
  .source-form:has(input[name="source_type"][value="pwm"]:checked, input[name="source_type"][value="frequency"]:checked) .if-pulses { display: block; }
  .source-form:has(input[name="source_type"][value="pwm"]:checked, input[name="source_type"][value="frequency"]:checked, input[name="source_type"][value="analog"]:checked) .if-formula { display: block; }
  .bus-form:has(select[name="profile_id"] option[value="_new"]:checked) .if-newprofile { display: block; }
}

/* --- delete ---------------------------------------------------------------- */

.delete-link { margin: 20px 0 4px; text-align: center; }
.delete-link a { display: inline-block; min-height: 44px; padding: 10px 16px; color: #c62828; font-weight: 700; }
[data-theme="dark"] .delete-link a { color: #ff8a80; }
.delete-name { font-size: 1.15rem; font-weight: 700; margin: 0 0 12px; }
.effects { padding-left: 20px; margin: 8px 0; }
.effects li { margin: 6px 0; }
.formbar a.button { flex: 1; min-height: 52px; }
.addphoto.danger { border-color: #c62828; color: #c62828; }
[data-theme="dark"] .addphoto.danger { border-color: #ff8a80; color: #ff8a80; }

/* --- connector library and editor ---------------------------------------- */

.ctype-preview svg.conn { max-width: 320px; margin: 8px 0; }
.chips.stacked { flex-direction: column; align-items: stretch; }
.chips.stacked .chip-radio > span { border-radius: 12px; padding: 8px 14px; display: block; font-weight: 400; }

.layout-editor { display: grid; gap: 10px; }
.le-row { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; }
.le-row b { min-width: 2ch; text-align: center; font-size: 1.1rem; }
.step {
  width: 44px; height: 44px; border-radius: 10px; border: 2px solid var(--fg);
  background: var(--bg); color: var(--fg); font-size: 1.4rem; font-weight: 700; cursor: pointer;
}
.le-shape button, .le-mode button {
  min-height: 40px; padding: 0 12px; border-radius: 999px; border: 2px solid var(--line);
  background: var(--bg); color: var(--fg); font: inherit; font-weight: 600; cursor: pointer;
}
.le-shape button[aria-pressed="true"], .le-mode button[aria-pressed="true"] { border-color: var(--fg); background: var(--fg); color: var(--bg); }
.le-grid {
  display: grid; gap: 4px; padding: 14px; overflow-x: auto;
  background: var(--housing); border: 3px solid var(--fg); border-radius: 12px;
}
.le-grid.shape-trapezoid { clip-path: polygon(0 0, 100% 0, 92% 100%, 8% 100%); padding-left: 10%; padding-right: 10%; border-radius: 0; }
.le-grid.shape-round { border-radius: 40px; }
.le-cell {
  aspect-ratio: 1; min-width: 38px; border-radius: 50%; border: 2px solid var(--fg);
  background: var(--bg); color: var(--fg); font: 700 .95rem Roboto, system-ui, sans-serif; cursor: pointer; padding: 0;
}
.le-cell.blank { border-style: dashed; border-color: var(--muted); background: transparent; opacity: .6; }

.pinlist { list-style: none; margin: 12px 0; padding: 0; display: grid; gap: 6px; }
.pinlist a {
  display: grid; grid-template-columns: 3em 1fr; gap: 10px; align-items: center; min-height: var(--tap);
  padding: 6px 10px; border: 1px solid var(--line); border-radius: 10px; background: var(--card);
  color: inherit; text-decoration: none;
}
.pinlist .pinno { font-size: 1.2rem; font-weight: 800; text-align: center; }
.pinlist .body { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.linkish.danger { color: #c62828; }
[data-theme="dark"] .linkish.danger { color: #ff8a80; }
.ctype-preview svg.conn { max-height: 130px; width: auto; max-width: 100%; margin: 8px 0; }
.le-size { justify-content: space-between; row-gap: 8px; }
.le-step { display: inline-flex; align-items: center; gap: 6px; white-space: nowrap; }

/* --- logins and roles ------------------------------------------------------ */

/* Viewers do not see edit controls (the server refuses the changes anyway). */
.role-viewer .addphoto, .role-viewer .delete-link, .role-viewer .edit-only { display: none !important; }

.secretbox { border: 3px solid var(--highlight); border-radius: 12px; padding: 10px 14px; margin: 16px 0; background: var(--card); }
.secretbox .secret { font-size: 1.4rem; font-weight: 800; letter-spacing: .04em; user-select: all; overflow-wrap: anywhere; margin: 6px 0; }
.okbox { border: 2px solid var(--ok); border-radius: 10px; padding: 10px 12px; color: var(--fg); background: var(--card); }
.logout { margin: 28px 0; }
.login { max-width: 460px; }
.login .hint { text-align: center; }
.token-new { display: grid; gap: 4px; margin-top: 8px; }
.list li.card { display: grid; gap: 2px; }
.list li.card form { margin: 0; }

/* --- CAN library ------------------------------------------------------------ */

.frame h3 small { font-family: Roboto, system-ui, sans-serif; font-weight: 400; color: var(--muted); }
.siglist { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.siglist li { background: var(--bg); border: 1px solid var(--line); border-radius: 10px; padding: 8px 10px; }
.siglist li:target { outline: 3px solid var(--accent); }
.signame { font-weight: 700; overflow-wrap: anywhere; }
.formula { font-size: 1.1rem; font-weight: 700; }
.import-summary { display: flex; flex-wrap: wrap; gap: 6px; }
.chip.warn { border-color: var(--warn); color: var(--warn); }
.import-table td { overflow-wrap: anywhere; }
.import-table tr.act-skip td, .import-table tr.act-keep td { opacity: .7; }
.import-table tr.act-error td { background: color-mix(in srgb, var(--warn) 12%, transparent); }

/* --- side menu and floating button ------------------------------------------ */

.menu-btn {
  flex: none; display: inline-flex; align-items: center; justify-content: center;
  width: var(--tap); height: var(--tap); margin-left: -8px; color: var(--fg) !important;
}
/* Header: ☰, then title/back on the left; extras pushed to the right. */
.bar.top { justify-content: flex-start; }
.bar.top .bar-end, .bar.top > .theme-toggle { margin-left: auto; }
.menu-btn svg, .drawer svg, .fab svg { fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.menu-btn svg { width: 28px; height: 28px; }

.drawer {
  position: fixed; top: 0; left: 0; bottom: 0; z-index: 101;
  width: min(86vw, 340px); overflow-y: auto; overscroll-behavior: contain;
  display: flex; flex-direction: column;
  background: var(--bg); color: var(--fg);
  box-shadow: 8px 0 24px rgba(0, 0, 0, .3);
  padding: env(safe-area-inset-top) 0 env(safe-area-inset-bottom);
  transform: translateX(-105%); visibility: hidden;
  transition: transform .2s ease, visibility 0s linear .2s;
}
.menu-open .drawer, .drawer:target {
  transform: none; visibility: visible; transition: transform .2s ease;
}
.drawer-backdrop {
  position: fixed; inset: 0; z-index: 100; background: rgba(0, 0, 0, .45);
  opacity: 0; pointer-events: none; transition: opacity .2s;
}
.menu-open .drawer-backdrop { opacity: 1; pointer-events: auto; }
.menu-open, .menu-open body { overflow: hidden; }
@media (prefers-reduced-motion: reduce) { .drawer, .drawer-backdrop { transition: none !important; } }

.drawer-head {
  display: flex; align-items: center; justify-content: space-between;
  min-height: 56px; padding: 0 8px 0 16px; border-bottom: 3px solid var(--highlight);
}
.drawer-close { display: inline-flex; align-items: center; justify-content: center; width: var(--tap); height: var(--tap); color: var(--fg); }
.drawer-close svg { width: 26px; height: 26px; }
.drawer-user { display: flex; align-items: center; gap: 12px; padding: 16px; border-bottom: 1px solid var(--line); }
.drawer-user .avatar {
  flex: none; width: 48px; height: 48px; border-radius: 50%;
  display: inline-flex; align-items: center; justify-content: center;
  background: var(--brand-orange); color: #fff; font-size: 1.4rem; font-weight: 800;
}
.drawer-user b { display: block; font-size: 1.1rem; }
.drawer-user small { display: block; color: var(--muted); }
.drawer-links { list-style: none; margin: 8px 0; padding: 0; }
.drawer-links a, .drawer-logout button {
  display: flex; align-items: center; gap: 16px; width: 100%; min-height: 52px; padding: 0 20px;
  color: var(--fg); text-decoration: none; font: 600 1.05rem Roboto, system-ui, sans-serif;
  background: none; border: 0; cursor: pointer; text-align: left;
}
.drawer-links a:active, .drawer-logout button:active { background: var(--card); }
.drawer-links a[aria-current="page"] { background: var(--card); box-shadow: inset 4px 0 0 var(--highlight); }
.drawer-links svg, .drawer-logout svg { width: 24px; height: 24px; flex: none; color: var(--accent); }
.drawer-row {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  margin-top: auto; padding: 8px 12px 8px 20px; border-top: 1px solid var(--line); font-weight: 600;
}
.drawer-theme { display: inline-flex; align-items: center; gap: 4px; color: var(--muted); }
.drawer-theme .theme-toggle { margin: 0; }
.drawer-logout { border-top: 1px solid var(--line); }
.drawer-logout button { color: #c62828; }
.drawer-logout svg { color: #c62828; }
[data-theme="dark"] .drawer-logout button, [data-theme="dark"] .drawer-logout svg { color: #ff8a80; }

.fab {
  position: fixed; right: 16px; bottom: calc(18px + env(safe-area-inset-bottom)); z-index: 40;
  width: 64px; height: 64px; border-radius: 50%;
  display: inline-flex; align-items: center; justify-content: center;
  background: var(--accent); color: var(--accent-fg);
  box-shadow: 0 6px 16px rgba(0, 0, 0, .3), 0 0 0 3px var(--bg);
}
.fab svg { width: 32px; height: 32px; stroke-width: 2.6; }
.fab:active { transform: scale(.96); }

/* Tablet and desktop: the menu is a permanent sidebar. */
@media (min-width: 768px) {
  :root { --sidebar: 272px; }
  body:not(.role-none) { padding-left: var(--sidebar); }
  body:not(.role-none) .drawer {
    width: var(--sidebar); transform: none; visibility: visible; transition: none;
    box-shadow: none; border-right: 1px solid var(--line); z-index: 30;
  }
  body:not(.role-none) .menu-btn, body:not(.role-none) .drawer-close,
  body:not(.role-none) .drawer-backdrop { display: none; }
  body:not(.role-none) .drawer-head { justify-content: flex-start; }
  body:not(.role-none) .bar.bottom, body:not(.role-none) .upload-banner { left: var(--sidebar); }
  body:not(.role-none) .upload-banner { left: calc(var(--sidebar) + 8px); }
  .menu-open, .menu-open body { overflow: auto; }
  /* the sidebar shows the logo already */
  body:not(.role-none) .bar.top .logo { display: none; }
}
/* The annotation editor takes the whole screen, also over the sidebar. */
.editor { z-index: 200; }
