:root {
  --bg: #f4f4f2;
  --panel: #ffffff;
  --ink: #17191c;
  --muted: #646a73;
  --line: #e1e2df;
  --accent: #f2c200;
  --accent-ink: #17191c;
  --hit: rgba(242, 194, 0, .38);
  --hit-line: #c99f00;
  --sel: rgba(37, 110, 235, .28);
  --sel-line: #256eeb;
  --row-hit: #fff6cc;
  --row-sel: #dfe9fd;
  --ok: #1b7f3b;
  --warn: #a35a00;
  --bad: #c0392b;
  --mono: ui-monospace, "SFMono-Regular", "JetBrains Mono", Menlo, Consolas, monospace;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --radius: 10px;
  --gap: 14px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #111317; --panel: #1a1d22; --ink: #eceef1; --muted: #9aa1ab; --line: #2b3038;
    --hit: rgba(242, 194, 0, .30); --row-hit: #3a3313; --row-sel: #1b2b49;
    --sel: rgba(90, 150, 255, .30); --sel-line: #6ea2ff; --ok: #4cc172; --warn: #f0a24a; --bad: #ff7b6b;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #111317; --panel: #1a1d22; --ink: #eceef1; --muted: #9aa1ab; --line: #2b3038;
  --hit: rgba(242, 194, 0, .30); --row-hit: #3a3313; --row-sel: #1b2b49;
  --sel: rgba(90, 150, 255, .30); --sel-line: #6ea2ff; --ok: #4cc172; --warn: #f0a24a; --bad: #ff7b6b;
  color-scheme: dark;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; }
body { background: var(--bg); color: var(--ink); font: 15px/1.4 var(--sans); }
button { font: inherit; color: inherit; }
.pn { font-family: var(--mono); font-size: 12.5px; }
.was { color: var(--muted); font-size: 11.5px; }
.stock-ok { color: var(--ok); }
.stock-warn { color: var(--warn); }

/* ================= header ================= */
.top {
  position: sticky; top: 0; z-index: 20;
  display: grid; grid-template-columns: auto 1fr auto; gap: 8px 14px; align-items: center;
  padding: 10px 16px; padding-top: max(10px, env(safe-area-inset-top));
  background: #16181c; color: #f1f2f4; border-bottom: 3px solid var(--accent);
}
.brand { display: flex; align-items: center; gap: 8px; min-width: 0; }
.badge { background: var(--accent); color: var(--accent-ink); font-weight: 800; letter-spacing: .04em;
  padding: 3px 7px; border-radius: 5px; font-size: 13px; }
.searchbox { position: relative; }
.searchbox .mag { position: absolute; left: 11px; top: 50%; width: 18px; height: 18px; transform: translateY(-50%);
  fill: none; stroke: #8a9099; stroke-width: 2; stroke-linecap: round; }
#q { width: 100%; padding: 10px 12px 10px 36px; border-radius: 9px; border: 1px solid #3a3f47;
  background: #23272d; color: #fff; font-size: 16px; outline: none; }
#q:focus { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(242, 194, 0, .25); }
.chips { grid-column: 1 / -1; display: flex; gap: 6px; overflow-x: auto; scrollbar-width: none; }
.chips button { flex: none; border: 1px solid #3a3f47; background: #23272d; border-radius: 999px;
  padding: 3px 11px; font-size: 13px; cursor: pointer; color: #d8dbe0; }
.chips button[aria-pressed="true"] { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.summary { grid-column: 1 / -1; font-size: 13px; color: #a9afb8; min-height: 1.2em; }
.cartbtn { display: inline-flex; align-items: center; gap: 7px; border: 1px solid #3a3f47; background: #23272d;
  color: #f1f2f4; border-radius: 9px; padding: 8px 12px; cursor: pointer; font-weight: 600; }
.cartbtn svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.cartbtn .count { min-width: 22px; padding: 1px 6px; border-radius: 999px; background: #3a3f47; text-align: center; font-size: 13px; }
.cartbtn .count.on { background: var(--accent); color: var(--accent-ink); }
.cartbtn.bump { animation: bump .35s ease-out; }
@keyframes bump { 50% { transform: scale(1.08); } }
#cartname { max-width: 14ch; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ================= app layout: two panes that scroll on their own ================= */
.layout {
  display: grid; grid-template-columns: minmax(300px, 400px) 1fr; gap: var(--gap);
  padding: var(--gap) 16px; height: calc(100dvh - var(--top, 130px));
}
.pane { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); min-width: 0; min-height: 0; overflow: hidden; }
.list { overflow: auto; }
.foot { padding: 4px 16px 20px; font-size: 12px; color: var(--muted); }

/* ---- results / browse list ---- */
.group-h { position: sticky; top: 0; z-index: 1; background: var(--panel); padding: 8px 12px 6px;
  font-size: 12px; font-weight: 700; letter-spacing: .06em; text-transform: uppercase; color: var(--muted);
  border-bottom: 1px solid var(--line); }
.res { display: grid; grid-template-columns: 1fr auto; gap: 2px 10px; width: 100%; text-align: left;
  padding: 8px 12px; border-bottom: 1px solid var(--line); cursor: pointer; }
.res:hover, .res:focus-visible { background: var(--row-hit); outline: none; }
.res.active { background: var(--row-sel); }
.res .d { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.res .pa { display: flex; align-items: center; gap: 8px; }
.res .p { font-weight: 700; font-variant-numeric: tabular-nums; }
.res .m { grid-column: 1 / -1; font-size: 12.5px; color: var(--muted); display: flex; gap: 8px; flex-wrap: wrap; }
.dgm { display: flex; gap: 10px; align-items: center; width: 100%; padding: 6px 12px; border: 0;
  border-bottom: 1px solid var(--line); background: none; text-align: left; cursor: pointer; }
.dgm:hover { background: var(--row-hit); }
.dgm.active { background: var(--row-sel); }
.dgm img { width: 44px; height: 44px; object-fit: contain; background: #fff; border: 1px solid var(--line); border-radius: 5px; }
.dgm .n { flex: 1; font-weight: 600; }
.dgm .c { font-size: 12px; color: var(--muted); }
.empty { padding: 24px 14px; color: var(--muted); }
.more { width: 100%; padding: 10px; border: 0; background: none; color: var(--sel-line); cursor: pointer; }
.add { flex: none; border: 1px solid var(--line); background: var(--panel); color: var(--ink); border-radius: 7px;
  padding: 3px 9px; font-size: 12.5px; font-weight: 700; cursor: pointer; white-space: nowrap; }
.add:hover { border-color: var(--hit-line); }
.add.in { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }

/* ================= diagram viewer: image | splitter | parts (own scroll) ================= */
.viewer { display: flex; flex-direction: column; }
.welcome { padding: 28px 22px; color: var(--muted); overflow: auto; }
.welcome h2 { color: var(--ink); margin-top: 0; }
.vh { flex: none; display: flex; flex-wrap: wrap; align-items: center; gap: 4px 12px; padding: 8px 12px;
  border-bottom: 1px solid var(--line); }
.vh h2 { margin: 0; font-size: 18px; }
.vh .cat { color: var(--muted); font-size: 13px; }
.vh .ext { margin-left: auto; font-size: 13px; color: var(--sel-line); text-decoration: none; font-weight: 600; }
.back { display: none; border: 1px solid var(--line); background: var(--panel); border-radius: 7px; padding: 3px 10px; cursor: pointer; }

.fig { flex: 0 0 auto; position: relative; overflow: hidden; background: #fff; touch-action: none;
  user-select: none; -webkit-user-select: none; cursor: zoom-in; }
.fig.zoomed { cursor: grab; }
.fig.zoomed:active { cursor: grabbing; }
.stage { position: absolute; left: 0; top: 0; transform-origin: 0 0; will-change: transform; }
.stage img { display: block; width: 100%; height: 100%; pointer-events: none; }
.spot { position: absolute; border-radius: 4px; cursor: pointer; border: 2px solid transparent; padding: 0;
  background: transparent; min-width: 18px; min-height: 18px; }
.spot:hover { border-color: var(--sel-line); background: rgba(37,110,235,.10); }
.spot.hit { background: var(--hit); border-color: var(--hit-line); }
.spot.sel { background: var(--sel); border-color: var(--sel-line); animation: pulse 1.2s ease-out 2; }
@keyframes pulse { 0% { box-shadow: 0 0 0 0 rgba(37,110,235,.55); } 100% { box-shadow: 0 0 0 12px rgba(37,110,235,0); } }
@media (prefers-reduced-motion: reduce) { .spot.sel { animation: none; } .cartbtn.bump { animation: none; } }

.zctl { position: absolute; right: 8px; top: 8px; display: flex; flex-direction: column; gap: 4px; }
.zctl button { width: 36px; height: 36px; border-radius: 8px; border: 1px solid #d4d6d9; background: rgba(255,255,255,.94);
  color: #17191c; font-size: 18px; font-weight: 700; cursor: pointer; box-shadow: 0 1px 3px rgba(0,0,0,.15);
  display: grid; place-items: center; padding: 0; }
.zctl button svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.zctl button:hover { border-color: var(--sel-line); }
.zctl [data-z="fit"] { font-size: 11px; }
.fig:not(.zoomed) .zctl [data-z="fit"] { display: none; }
.zhint { position: absolute; left: 8px; bottom: 6px; font-size: 11.5px; color: #646a73; background: rgba(255,255,255,.88);
  padding: 2px 6px; border-radius: 5px; pointer-events: none; }

.split { flex: none; height: 14px; cursor: row-resize; touch-action: none; display: grid; place-items: center;
  border-top: 1px solid var(--line); border-bottom: 1px solid var(--line); background: var(--bg); }
.split::before { content: ""; width: 44px; height: 4px; border-radius: 4px; background: var(--muted); opacity: .55; }
.split:hover::before, .split.drag::before, .split:focus-visible::before { opacity: 1; background: var(--sel-line); }
.split:focus-visible { outline: none; }

.pickwrap { flex: none; padding: 6px 12px; border-bottom: 1px solid var(--line); }
.pick { display: flex; flex-direction: column; gap: 4px; min-height: 28px; justify-content: center; }
.pick.muted { color: var(--muted); font-size: 13px; }
.pk { display: flex; align-items: center; gap: 10px; }
.tagb { flex: none; min-width: 26px; height: 26px; padding: 0 5px; border-radius: 999px; display: grid; place-items: center;
  background: var(--sel-line); color: #fff; font-weight: 800; font-size: 12.5px; }
.pk .pd { flex: 1; min-width: 0; font-size: 13.5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pk .pd .pn, .pk .pd .st { margin-left: 8px; }
.pk .pp { font-weight: 800; font-variant-numeric: tabular-nums; }

.tblwrap { flex: 1 1 0; min-height: 0; overflow: auto; overscroll-behavior: contain; }
table.parts { width: 100%; border-collapse: collapse; table-layout: fixed; font-size: 13.5px; }
table.parts th { position: sticky; top: 0; z-index: 1; background: var(--panel); text-align: left; font-size: 11px;
  letter-spacing: .05em; text-transform: uppercase; color: var(--muted); padding: 6px 8px; border-bottom: 1px solid var(--line); }
table.parts td { padding: 5px 8px; border-bottom: 1px solid var(--line); vertical-align: middle; overflow-wrap: anywhere; }
table.parts .c-tag { width: 3.2em; text-align: center; font-weight: 700; white-space: nowrap; overflow-wrap: normal; padding-left: 4px; padding-right: 4px; }
table.parts .c-price { width: 6em; text-align: right; font-variant-numeric: tabular-nums; }
table.parts .c-stock { width: 8.5em; font-size: 12px; }
table.parts .c-act { width: 5.8em; text-align: right; padding-right: 10px; }
table.parts td.c-part .pn { margin-left: 8px; color: var(--muted); font-size: 12px; }
tr.part { cursor: pointer; scroll-margin-top: 30px; }
tr.part:hover td { background: var(--row-hit); }
tr.part.hit td { background: var(--row-hit); }
tr.part.sel td { background: var(--row-sel); }

/* expanded (full window) viewer */
.viewer.fs { position: fixed; inset: 0; z-index: 50; border-radius: 0; border: 0;
  padding-top: env(safe-area-inset-top); padding-bottom: env(safe-area-inset-bottom); }
body.fs-open { overflow: hidden; }

/* ================= cart drawer ================= */
.scrim { position: fixed; inset: 0; background: rgba(0,0,0,.35); z-index: 60; }
.drawer { position: fixed; top: 0; right: 0; bottom: 0; width: min(480px, 100vw); z-index: 61; background: var(--panel);
  border-left: 1px solid var(--line); display: flex; flex-direction: column; box-shadow: -8px 0 30px rgba(0,0,0,.2); }
.dh { display: flex; gap: 6px; align-items: center; padding: 12px; padding-top: max(12px, env(safe-area-inset-top));
  border-bottom: 1px solid var(--line); }
#listsel { flex: 1 1 100px; min-width: 0; padding: 7px 8px; border-radius: 7px; border: 1px solid var(--line);
  background: var(--panel); color: var(--ink); font: 600 15px var(--sans); }
.ghost { border: 1px solid var(--line); background: none; border-radius: 7px; padding: 6px 9px; cursor: pointer; font-size: 13px; }
.ghost:hover { border-color: var(--muted); }
.ghost.danger { color: var(--bad); }
.dbody { flex: 1; overflow: auto; padding: 4px 0 24px; }
.rg { border-bottom: 1px solid var(--line); padding-bottom: 10px; }
.rg h3 { margin: 0; padding: 12px 14px 6px; font-size: 13px; letter-spacing: .05em; text-transform: uppercase; color: var(--muted); }
.ci { display: grid; grid-template-columns: 1fr auto; gap: 3px 10px; padding: 9px 14px; border-top: 1px solid var(--line); }
.ci .d { font-weight: 600; }
.ci .m { font-size: 12.5px; color: var(--muted); }
.ci .m a { color: inherit; }
.ci .lp { text-align: right; font-weight: 700; font-variant-numeric: tabular-nums; }
.qty { display: inline-flex; align-items: center; border: 1px solid var(--line); border-radius: 7px; overflow: hidden; }
.qty button { border: 0; background: none; width: 30px; height: 28px; cursor: pointer; font-size: 16px; }
.qty button:hover { background: var(--row-hit); }
.qty span { min-width: 26px; text-align: center; font-variant-numeric: tabular-nums; font-weight: 700; }
.ci .ctl { display: flex; gap: 8px; align-items: center; }
.ci .rm { border: 0; background: none; color: var(--muted); cursor: pointer; font-size: 12.5px; text-decoration: underline; }
.tot { display: flex; justify-content: space-between; padding: 10px 14px 4px; font-weight: 700; font-size: 16px; }
.ship { padding: 0 14px 8px; font-size: 12.5px; color: var(--muted); }
.ship.ok { color: var(--ok); }
.export { margin: 6px 14px 0; width: calc(100% - 28px); padding: 11px; border-radius: 9px; border: 0; cursor: pointer;
  background: var(--accent); color: var(--accent-ink); font-weight: 800; font-size: 15px; }
.export[disabled] { opacity: .6; cursor: progress; }
.confirm { margin: 8px 14px 0; padding: 10px 12px; border-radius: 8px; background: var(--row-hit); font-size: 13.5px; }
.confirm .row { display: flex; gap: 8px; margin-top: 8px; }
.confirm .export { margin: 0; width: auto; flex: 1; }
.status { margin: 8px 14px 0; font-size: 13px; }
.status ul { margin: 6px 0 0; padding-left: 18px; }
.status .bad { color: var(--bad); }
.status .good { color: var(--ok); }
.alt { display: block; margin: 8px 14px 0; font-size: 12.5px; color: var(--muted); }

/* ================= phones / narrow windows: one pane at a time ================= */
@media (max-width: 900px) {
  :root { --gap: 8px; }
  .top { grid-template-columns: 1fr auto; padding: 8px 10px; }
  .brand { grid-row: 1; grid-column: 1; }
  .actions { grid-row: 1; grid-column: 2; }
  .searchbox { grid-row: 2; grid-column: 1 / -1; }
  #cartname { display: none; }
  body.show-viewer .chips, body.show-viewer .summary { display: none; }
  .layout { grid-template-columns: 1fr; padding: var(--gap); }
  body.show-viewer .list { display: none; }
  body:not(.show-viewer) .viewer { display: none; }
  .back { display: inline-block; }
  .vh { padding: 6px 8px; }
  .vh h2 { font-size: 16px; }
  .vh .ext { font-size: 12.5px; }
  .zctl button { width: 40px; height: 40px; }
  table.parts { font-size: 13px; }
  table.parts .c-stock { display: none; }
  table.parts td.c-part .pnl { display: block; }
  table.parts td.c-part .pn { margin-left: 0; }
  table.parts .c-act { width: 6em; padding-right: 8px; }
  .vh { flex-wrap: nowrap; }
  .vh h2 { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }
  .vh .cat { white-space: nowrap; }
  table.parts td { padding: 5px 6px; }
  .add { padding: 4px 8px; }
}

/* dim the white diagram paper in dark mode; highlights are overlays so they stay vivid */
:root { --fig-bg: #ffffff; --img-filter: none; }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) { --fig-bg: #c9cbce; --img-filter: brightness(.79) contrast(1.08); }
}
:root[data-theme="dark"] { --fig-bg: #c9cbce; --img-filter: brightness(.79) contrast(1.08); }
.fig { background: var(--fig-bg); }
.stage img, .dgm img { filter: var(--img-filter); }
.dgm img { background: var(--fig-bg); }

/* header actions: theme + cart */
.actions { display: flex; gap: 8px; align-items: center; }
.themebtn { display: grid; place-items: center; width: 40px; height: 38px; border: 1px solid #3a3f47; background: #23272d;
  color: #f1f2f4; border-radius: 9px; cursor: pointer; padding: 0; }
.themebtn:hover { border-color: var(--accent); }
.themebtn svg { width: 19px; height: 19px; fill: none; stroke: currentColor; stroke-width: 1.9; stroke-linecap: round; stroke-linejoin: round; }

/* price freshness */
.fresh { border: 0; background: none; color: var(--muted); font-size: 12.5px; cursor: pointer; padding: 2px 4px; border-radius: 6px; }
.fresh:hover { color: var(--ink); background: var(--row-hit); }
.fresh .stale { color: var(--warn); }
.spin { display: inline-block; animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { .spin { animation: none; } }
@media (max-width: 900px) { .vh .cat { display: none; } }

/* ---- bike picker ---- */
#bikebtn { display: inline-flex; align-items: center; gap: 6px; background: transparent; color: inherit;
  border: 1px solid #3a3f47; border-radius: 7px; padding: 6px 8px 6px 10px; font: 600 15px var(--sans);
  cursor: pointer; max-width: 60vw; min-width: 0; }
#bikebtn:hover { border-color: var(--accent); }
#bikename { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#bikebtn svg { flex: none; width: 16px; height: 16px; fill: none; stroke: currentColor; stroke-width: 2.2; stroke-linecap: round; }
.bikepick { position: fixed; z-index: 62; top: calc(var(--top, 60px) - 8px); left: 16px; width: min(520px, calc(100vw - 32px));
  max-height: min(70dvh, 620px); display: flex; flex-direction: column; background: var(--panel); color: var(--ink);
  border: 1px solid var(--line); border-radius: 12px; box-shadow: 0 16px 40px rgba(0,0,0,.3); }
.bpsearch { display: flex; gap: 6px; padding: 10px; border-bottom: 1px solid var(--line); }
#bikeq { flex: 1; min-width: 0; padding: 9px 11px; border-radius: 8px; border: 1px solid var(--line);
  background: var(--bg); color: var(--ink); font-size: 16px; outline: none; }
#bikeq:focus { border-color: var(--accent); }
.bpbody { overflow: auto; flex: 1; }
.bpfoot { padding: 7px 12px; font-size: 12px; color: var(--muted); border-top: 1px solid var(--line); }
.bp { display: grid; grid-template-columns: 1fr auto; gap: 1px 10px; width: 100%; padding: 8px 12px; border: 0;
  border-bottom: 1px solid var(--line); background: none; text-align: left; cursor: pointer; }
.bp:hover, .bp:focus-visible { background: var(--row-hit); outline: none; }
.bp.cur { background: var(--row-sel); }
.bp .n { font-weight: 650; }
.bp .t { font-size: 12px; color: var(--muted); }
.bp .st { grid-row: 1 / span 2; grid-column: 2; align-self: center; font-size: 12px; font-weight: 700; padding: 2px 8px;
  border-radius: 999px; background: var(--bg); color: var(--muted); white-space: nowrap; }
.bp .st.ready { color: var(--ok); }
.bp .st.copying, .bp .st.queued { color: var(--warn); }
.bp .st.new { color: var(--sel-line); }
.copybar { padding: 10px 12px; border-bottom: 1px solid var(--line); background: var(--row-hit); font-size: 13.5px; }
.copybar .bar { height: 6px; border-radius: 6px; background: var(--line); margin-top: 6px; overflow: hidden; }
.copybar .bar i { display: block; height: 100%; background: var(--accent); transition: width .4s; }
@media (max-width: 900px) { .bikepick { left: 8px; width: calc(100vw - 16px); top: 8px; max-height: 80dvh; } }

.confirm.setup ol { margin: 6px 0 0; padding-left: 20px; }
.confirm.setup li { margin: 4px 0; }
.confirm a, .alt a { color: var(--sel-line); }

/* account menu (only when sign-in is enabled) */
.usermenu { position: relative; }
#userbtn { font-weight: 800; font-size: 13px; letter-spacing: .02em; }
.userpop { position: absolute; right: 0; top: calc(100% + 6px); z-index: 70; min-width: 220px; background: var(--panel); color: var(--ink);
  border: 1px solid var(--line); border-radius: 10px; box-shadow: 0 12px 30px rgba(0,0,0,.3); padding: 8px 0; }
.userpop .un { padding: 4px 14px 0; font-weight: 700; }
.userpop .ue { padding: 0 14px 8px; font-size: 12.5px; color: var(--muted); border-bottom: 1px solid var(--line); margin-bottom: 4px; }
.userpop a { display: block; padding: 8px 14px; color: var(--ink); text-decoration: none; }
.userpop a:hover { background: var(--row-hit); }

.copybar.human { background: color-mix(in srgb, var(--warn) 22%, var(--panel)); }
.copybar.human a { color: var(--sel-line); font-weight: 700; }
