:root {
  --bg: #0f1115;
  --bg-2: #161922;
  --panel: #1b1f2a;
  --panel-2: #222633;
  --border: #2a2f3d;
  --text: #e7e9ee;
  --muted: #9aa3b2;
  --accent: #5b8cff;
  --accent-700: #3f6fe0;
  --danger: #ef4444;
  --ok: #22c55e;
  --warn: #f59e0b;
  --info: #38bdf8;
  --radius: 12px;
  --shadow: 0 8px 30px rgba(0, 0, 0, 0.35);
  --maxw: 1200px;
  font-synthesis: none;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  font-size: 15px;
  line-height: 1.45;
  -webkit-text-size-adjust: 100%;
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
.muted { color: var(--muted); }
.loading { padding: 60px; text-align: center; color: var(--muted); }
.spacer { flex: 1; }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }

/* ---- inputs & buttons ---- */
input, select, textarea, button {
  font: inherit;
  color: var(--text);
  background: var(--bg-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 10px;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
select { cursor: pointer; }

button {
  cursor: pointer;
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
  font-weight: 600;
  transition: filter .12s ease;
}
button:hover { filter: brightness(1.08); }
button:disabled { opacity: .6; cursor: default; }
button.ghost { background: transparent; color: var(--text); border-color: var(--border); }
button.ghost:hover { background: var(--panel-2); }
button.danger { background: transparent; color: var(--danger); border-color: transparent; padding: 6px 8px; }
button.danger:hover { background: rgba(239, 68, 68, .12); }
button.small { padding: 6px 10px; font-size: 13px; }

/* ---- auth pages ---- */
.auth-body { display: grid; place-items: center; min-height: 100dvh; padding: 20px; background: radial-gradient(1200px 600px at 50% -10%, #1c2333, var(--bg)); }
.auth-card { width: 100%; max-width: 380px; background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); padding: 28px; display: flex; flex-direction: column; gap: 14px; }
.auth-card.wide { max-width: 460px; }
.auth-logo { font-size: 40px; text-align: center; }
.auth-card h1 { margin: 0; text-align: center; font-size: 22px; }
.auth-card > p.muted { margin: 0 0 6px; text-align: center; }
.auth-card label { display: flex; flex-direction: column; gap: 6px; font-size: 13px; color: var(--muted); }
.auth-card input { font-size: 16px; } /* ≥16px stops iOS zoom-on-focus */
.auth-card button { padding: 11px; font-size: 15px; }
.auth-error { color: var(--danger); background: rgba(239,68,68,.1); border: 1px solid rgba(239,68,68,.3); border-radius: 8px; padding: 8px 10px; margin: 0; font-size: 13px; }
.totp-box { display: flex; gap: 14px; align-items: center; background: var(--bg-2); border: 1px solid var(--border); border-radius: 10px; padding: 12px; }
.totp-box .qr { width: 180px; height: 180px; display: grid; place-items: center; background: #fff; border-radius: 8px; flex: 0 0 auto; }
.totp-box .qr canvas { display: block; }
.totp-meta { flex: 1; min-width: 0; }
.secret { display: inline-block; margin-top: 6px; padding: 6px 8px; background: var(--panel-2); border-radius: 6px; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 13px; letter-spacing: .5px; word-break: break-all; }

/* ---- app chrome ---- */
.topbar { position: sticky; top: 0; z-index: 20; display: flex; align-items: center; gap: 12px; padding: 12px 18px; background: rgba(15,17,21,.85); backdrop-filter: blur(8px); border-bottom: 1px solid var(--border); }
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; cursor: pointer; }
.brand .logo { font-size: 20px; }
.container { max-width: var(--maxw); margin: 0 auto; padding: 18px; }

.crumbs { display: flex; align-items: center; gap: 8px; color: var(--muted); margin-bottom: 14px; font-size: 14px; }
.page-head { display: flex; align-items: flex-start; gap: 12px; flex-wrap: wrap; margin-bottom: 18px; }
.page-head h1 { margin: 0; font-size: 24px; }

/* ---- stat cards ---- */
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px; margin-bottom: 18px; }
.stat { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; }
.stat .k { font-size: 12px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
.stat .v { font-size: 22px; font-weight: 700; margin-top: 4px; }
.stat .v.sm { font-size: 18px; }

/* ---- finished-units panel ---- */
.panelcard { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; margin-bottom: 18px; }
.panelcard-head { font-weight: 700; margin-bottom: 12px; }
.finished-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px; align-items: end; }
.finished-grid .field input { background: var(--bg-2); font-size: 16px; }
.stat.avail .v { color: var(--ok); }
.stat.avail.neg .v { color: var(--danger); }

/* ---- assembly panel (BOM ↔ Inventory) ---- */
.assemble-grid { display: grid; grid-template-columns: minmax(160px, 220px) 1fr; gap: 14px; align-items: stretch; }
.assemble-do { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; background: var(--bg-2); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px; }
.assemble-do button { padding: 10px 16px; }
.assemble-do .field input { background: var(--panel); font-size: 16px; }
table.asm-table th, table.asm-table td { padding: 7px 12px; }
table.asm-table tr.st-short td { background: rgba(245, 158, 11, .08); }
.synced { display: inline-block; margin-top: 4px; font-size: 11px; font-weight: 600; color: #4ade80; background: rgba(34,197,94,.12); border-radius: 99px; padding: 1px 8px; cursor: help; }
@media (max-width: 620px) { .assemble-grid { grid-template-columns: 1fr; } }

/* ---- parts of interest ---- */
details.interest { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); padding: 2px 16px 14px; margin-top: 16px; }
details.interest > summary { cursor: pointer; padding: 12px 0; font-weight: 700; }
.interest-add { margin-top: 4px; margin-bottom: 12px; }
.interest-list { display: flex; flex-direction: column; gap: 8px; }
.ii { display: flex; align-items: center; gap: 12px; padding: 10px 12px; background: var(--bg-2); border: 1px solid var(--border); border-radius: 10px; }
.ii-main { flex: 1 1 auto; min-width: 0; }
.ii-cost { flex: 0 0 auto; font-variant-numeric: tabular-nums; white-space: nowrap; }
.ii-act { display: flex; align-items: center; gap: 8px; flex: 0 0 auto; }

/* ---- docs (per-project documentation: svg / link / note) ---- */
details.docs { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); padding: 2px 16px 14px; margin-top: 16px; }
details.docs > summary { cursor: pointer; padding: 12px 0; font-weight: 700; }
.docs-toolbar { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; margin: 4px 0 12px; }
.docs-list { display: flex; flex-direction: column; gap: 12px; }
.doccard { background: var(--bg-2); border: 1px solid var(--border); border-radius: 10px; padding: 12px 14px; }
.doccard-head { display: flex; align-items: center; gap: 10px; margin-bottom: 10px; }
.doccard-head .doc-title { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.doc-kind.badge { color: var(--info); background: rgba(56,189,248,.14); }
.doc-kind.badge::before { display: none; }
/* Inline SVG diagram: responsive, centered, on a light card so dark strokes read. */
.doc-svg { background: #f6f8fc; border: 1px solid var(--border); border-radius: 8px; padding: 10px; overflow-x: auto; text-align: center; }
.doc-svg svg { max-width: 100%; height: auto; display: inline-block; }
.doc-link { display: inline-block; word-break: break-all; }
.doc-note { white-space: normal; line-height: 1.5; }
.doc-caption { margin-top: 8px; font-size: 13px; }

/* ---- progress ---- */
.progress { height: 8px; border-radius: 99px; background: var(--panel-2); overflow: hidden; margin-top: 8px; }
.progress > span { display: block; height: 100%; background: var(--ok); }

/* ---- project cards (home) ---- */
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 14px; }
.card { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; cursor: pointer; transition: border-color .12s, transform .12s; display: flex; flex-direction: column; gap: 10px; }
.card:hover { border-color: var(--accent); transform: translateY(-2px); }
.card h3 { margin: 0; font-size: 17px; }
.card .desc { color: var(--muted); font-size: 13px; min-height: 18px; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.card .cardstats { display: flex; justify-content: space-between; gap: 8px; font-size: 13px; color: var(--muted); }
.card .cardstats b { color: var(--text); font-size: 15px; display: block; }
.empty { text-align: center; color: var(--muted); padding: 60px 20px; border: 1px dashed var(--border); border-radius: var(--radius); }

/* ---- badges ---- */
.badge { display: inline-flex; align-items: center; gap: 6px; font-size: 12px; font-weight: 600; padding: 3px 9px; border-radius: 99px; border: 1px solid transparent; white-space: nowrap; }
.badge::before { content: ""; width: 7px; height: 7px; border-radius: 99px; background: currentColor; }
.s-not_ordered { color: #9aa3b2; background: rgba(154,163,178,.12); }
.s-ordered     { color: #7ab7ff; background: rgba(91,140,255,.14); }
.s-in_transit  { color: #fbbf24; background: rgba(245,158,11,.14); }
.s-delivered   { color: #4ade80; background: rgba(34,197,94,.14); }
.p-planning    { color: #9aa3b2; background: rgba(154,163,178,.12); }
.p-in-progress { color: #fbbf24; background: rgba(245,158,11,.14); }
.p-complete    { color: #4ade80; background: rgba(34,197,94,.14); }

/* ---- toolbar / filters ---- */
.toolbar { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; margin-bottom: 12px; }
.toolbar input[type=search] { min-width: 220px; flex: 1; }

/* ---- items table ---- */
.table-wrap { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); overflow-x: auto; }
table.items { width: 100%; border-collapse: collapse; table-layout: fixed; }
/* Pin the small columns narrow; the Item column (no width) absorbs the rest. */
.col-vendor { width: 156px; }
.col-cost { width: 100px; }
.col-qty { width: 62px; }
.col-line { width: 80px; text-align: right; }
.col-status { width: 126px; }
table.items th { text-align: left; font-size: 12px; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); padding: 10px 12px; border-bottom: 1px solid var(--border); background: var(--bg-2); }
table.items td { padding: 8px 12px; border-bottom: 1px solid var(--border); vertical-align: middle; }
table.items tr:last-child td { border-bottom: none; }
table.items input, table.items select { width: 100%; background: var(--bg-2); border: 1px solid var(--border); padding: 6px 8px; border-radius: 6px; }
table.items input:hover, table.items select:hover { border-color: var(--muted); }
table.items input:focus, table.items select:focus { border-color: var(--accent); outline: none; }
/* Strip number spinners in the table — they clipped values and ate clicks. */
table.items input[type=number]::-webkit-outer-spin-button,
table.items input[type=number]::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }
table.items input[type=number] { -moz-appearance: textfield; appearance: textfield; }
table.items .col-cost input, table.items .col-qty input { text-align: right; }
table.items input[data-field="name"] { min-width: 150px; }
table.items input[data-field="vendor"] { min-width: 100px; }
table.items tr.hint td { text-align: center; color: var(--muted); padding: 18px 12px; font-size: 13px; }
table.items input::placeholder, table.items textarea::placeholder { color: var(--muted); opacity: 1; }

/* ---- add-item bar (sits above the table; flex, never collapses) ---- */
.additem { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px; margin-bottom: 12px; }
.additem input, .additem select { background: var(--bg-2); border: 1px solid var(--border); font-size: 16px; }
.additem input::placeholder { color: var(--muted); opacity: 1; }
.additem #qa_name { flex: 3 1 200px; min-width: 160px; }
.additem #qa_vendor { flex: 2 1 130px; }
.additem #qa_link { flex: 2 1 130px; }
.additem #qa_cost { width: 96px; }
.additem #qa_qty, .additem #qa_moq, .additem #ip_qty, .additem #ip_moq { width: 72px; }
.additem #ip_cost { width: 96px; }
.additem #qa_status { flex: 0 0 auto; }
.additem #qa_add { flex: 0 0 auto; white-space: nowrap; }
.linecell { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; font-weight: 600; }
.linkcell { text-align: center; width: 50px; }
.qty-tiny { width: 64px; }
.cost-tiny { width: 90px; }
.statuspick { border-radius: 99px !important; font-weight: 600; }
.col-actions { text-align: right; width: 48px; }
.t-foot { padding: 12px; display: flex; justify-content: space-between; gap: 10px; font-weight: 700; background: var(--bg-2); border-top: 1px solid var(--border); }

.field { display: flex; flex-direction: column; gap: 5px; font-size: 12px; color: var(--muted); }
.field input, .field select, .field textarea { background: var(--bg-2); }

/* ---- modal ---- */
.modal-bg { position: fixed; inset: 0; background: rgba(0,0,0,.55); display: grid; place-items: center; padding: 16px; z-index: 50; }
.modal { width: 100%; max-width: 480px; background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); padding: 20px; display: flex; flex-direction: column; gap: 12px; max-height: 90dvh; overflow: auto; }
.modal h2 { margin: 0 0 4px; }
.modal .actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 6px; }

/* ---- toast ---- */
#toast { position: fixed; bottom: 18px; left: 50%; transform: translateX(-50%); background: var(--panel-2); border: 1px solid var(--border); color: var(--text); padding: 10px 16px; border-radius: 99px; box-shadow: var(--shadow); opacity: 0; transition: opacity .2s, transform .2s; pointer-events: none; z-index: 60; font-size: 14px; }
#toast.show { opacity: 1; transform: translateX(-50%) translateY(-4px); }
#toast.err { border-color: var(--danger); color: #fecaca; }

/* ---- mobile: items table → stacked cards ---- */
@media (max-width: 760px) {
  table.items thead { display: none; }
  table.items, table.items tbody, table.items tr, table.items td { display: block; width: 100%; }
  table.items tr { border-bottom: 1px solid var(--border); padding: 8px 4px; }
  table.items td { border: none; padding: 5px 10px; display: grid; grid-template-columns: 96px 1fr; align-items: center; gap: 8px; }
  table.items td::before { content: attr(data-label); font-size: 12px; color: var(--muted); }
  table.items td.col-actions { justify-content: end; grid-template-columns: 1fr; }
  table.items th { position: static; }
  .qty-tiny, .cost-tiny { width: 100%; }
  table.items input { min-width: 0; }
}

/* ---- component specs (physical catalog) ---- */
.specs-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(250px, 1fr)); gap: 12px; }
.speccard { background: var(--bg-2); border: 1px solid var(--border); border-radius: 10px; padding: 12px; display: flex; flex-direction: column; gap: 9px; }
.speccard.nosize { opacity: .75; }
.spec-head { display: flex; align-items: center; gap: 8px; }
.spec-head b { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 13.5px; }
.chipbtn { background: none; border: 1px solid var(--border); color: var(--muted); border-radius: 999px; padding: 3px 10px; font-size: 11px; cursor: pointer; white-space: nowrap; }
.chipbtn.on { color: var(--warn); border-color: rgba(245, 158, 11, .45); }
.spec-dims { display: flex; gap: 8px; }
.spec-dims.dim { opacity: .45; }
.spec-dims label { display: flex; align-items: center; gap: 5px; font-size: 11.5px; color: var(--muted); flex: 1; }
.spec-dims input { width: 100%; min-width: 0; background: var(--panel); border: 1px solid var(--border); border-radius: 7px; color: var(--text); padding: 6px 8px; font-size: 13px; }
.spec-photos { display: flex; gap: 8px; }
.spec-photo { position: relative; flex: 1; aspect-ratio: 4/3; border: 1px dashed var(--border); border-radius: 8px; overflow: hidden; cursor: pointer; background: var(--panel); display: flex; align-items: center; justify-content: center; }
.spec-photo img { width: 100%; height: 100%; object-fit: cover; display: block; }
.spec-photo .spec-pl { position: absolute; bottom: 4px; left: 6px; font-size: 10.5px; color: var(--muted); background: rgba(15, 17, 21, .72); padding: 1px 7px; border-radius: 6px; }
.spec-photo.empty .spec-pl { position: static; background: none; font-size: 12.5px; }
.spec-photo.busy { opacity: .5; pointer-events: none; }
.spec-photo:hover { border-color: var(--accent); }
.spec-notes { background: var(--panel); border: 1px solid var(--border); border-radius: 7px; color: var(--text); padding: 6px 9px; font-size: 12px; width: 100%; }
.spec-name { font-weight: 700; font-size: 13.5px; line-height: 1.3; overflow-wrap: anywhere; }
