:root {
  --bg: #f5f6f8;
  --panel: #ffffff;
  --text: #1d2330;
  --muted: #667085;
  --border: #dde1e7;
  --accent: #2563eb;
  --accent-text: #ffffff;
  --danger: #c0362c;
  --ok: #1d7a46;
  --row-hover: #f0f4ff;
  --chip: #eef1f5;
  --mark: #ffe58a;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14171c;
    --panel: #1c2027;
    --text: #e6e8ec;
    --muted: #98a1b0;
    --border: #2f3540;
    --accent: #4f8cff;
    --row-hover: #232a36;
    --danger: #ff6b5e;
    --ok: #4cc47e;
    --chip: #2a303a;
    --mark: #7a6212;
  }
}

* { box-sizing: border-box; }
body {
  margin: 0;
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
}
[hidden] { display: none !important; }

/* sign-in */
#login-screen { display: none; }
body.signed-out > :not(#login-screen):not(script):not(datalist) { display: none !important; }
body.signed-out #login-screen { display: flex; min-height: 100vh; align-items: center; justify-content: center; padding: 16px; }
.login-card {
  width: min(380px, 100%);
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 28px 24px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
}
.login-card h1 { margin-bottom: 2px; }
.login-card p { margin: 0; }
.login-card label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); }
.login-card input { color: var(--text); font-size: 16px; }
.login-card button { margin-top: 4px; padding: 10px; font-size: 16px; }
#account-btn::before { content: "👤 "; }
table.users td { padding: 6px 8px; }
.small { font-size: 13px; }
dialog form h3 { margin: 10px 0 0; font-size: 13px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }

header {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 18px 24px 12px;
  background: var(--panel);
}
h1 { margin: 0; font-size: 22px; }
.brand { display: flex; align-items: center; gap: 14px; }
#scan-btn { padding: 7px 16px; font-weight: 600; }
#scan-btn::before { content: "▦ "; }
.stats { display: flex; gap: 28px; }
.stats div { display: flex; flex-direction: column; align-items: flex-end; }
.stats span { font-size: 20px; font-weight: 600; font-variant-numeric: tabular-nums; }
.stats label { font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }

.tabs {
  display: flex;
  gap: 4px;
  padding: 0 16px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  overflow-x: auto;
}
.tabs button {
  border: none;
  border-bottom: 3px solid transparent;
  border-radius: 0;
  background: none;
  padding: 10px 14px;
  color: var(--muted);
  font-weight: 500;
}
.tabs button:hover { color: var(--text); }
.tabs button[aria-selected="true"] { color: var(--accent); border-bottom-color: var(--accent); }

main { max-width: 1300px; margin: 0 auto; padding: 20px 16px 40px; }

.toolbar { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 14px; }
.toolbar .search { flex: 1 1 260px; }

input, textarea, select, button, .button {
  font: inherit;
  color: var(--text);
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 8px 12px;
}
input[readonly] { background: var(--chip); }
textarea { resize: vertical; }
button, .button { cursor: pointer; text-decoration: none; white-space: nowrap; display: inline-block; }
button:hover:not(:disabled), .button:hover { border-color: var(--accent); }
button:disabled { opacity: .45; cursor: not-allowed; }
.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
.danger { color: var(--danger); }
input:focus, textarea:focus, select:focus { outline: 2px solid var(--accent); outline-offset: -1px; }

#message { padding: 10px 14px; border-radius: 6px; margin-bottom: 12px; background: var(--panel); border: 1px solid var(--border); }
#message.error, .error { color: var(--danger); }
.muted { color: var(--muted); }

.table-wrap { background: var(--panel); border: 1px solid var(--border); border-radius: 8px; overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { padding: 9px 12px; text-align: left; border-bottom: 1px solid var(--border); vertical-align: middle; }
tbody tr:last-child td { border-bottom: none; }
tfoot td { font-weight: 600; border-top: 2px solid var(--border); border-bottom: none; }
tbody tr:hover { background: var(--row-hover); }
tr.clickable { cursor: pointer; }
th { font-size: 12px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); user-select: none; white-space: nowrap; }
th[data-sort] { cursor: pointer; }
th.sorted-asc::after { content: " ▲"; }
th.sorted-desc::after { content: " ▼"; }
.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.pn { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-weight: 600; white-space: nowrap; }
td.desc { min-width: 180px; }
.low { color: var(--danger); font-weight: 600; }
.nowrap { white-space: nowrap; }
td.actions { white-space: nowrap; text-align: right; }
td.actions button { padding: 4px 9px; font-size: 13px; }
mark { background: var(--mark); color: inherit; border-radius: 2px; }
.empty { text-align: center; color: var(--muted); padding: 30px; margin: 0; }
a { color: var(--accent); }

.thumb-col { width: 52px; padding-right: 0; }
.thumb { width: 40px; height: 40px; border-radius: 6px; object-fit: cover; display: block; border: 1px solid var(--border); }
.thumb.none { background: var(--chip); }

.chips { min-width: 140px; }
.chip {
  display: inline-block;
  margin: 2px 4px 2px 0;
  padding: 1px 8px;
  border-radius: 999px;
  background: var(--chip);
  font-size: 13px;
  white-space: nowrap;
}
.tag {
  display: inline-block;
  padding: 1px 8px;
  border-radius: 4px;
  border: 1px solid var(--border);
  color: var(--muted);
  font-size: 12px;
  white-space: nowrap;
}
td.code, .code-chip { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; white-space: nowrap; }
.code-chip { font-size: 14px; font-weight: 500; padding: 2px 8px; border-radius: 4px; background: var(--chip); vertical-align: middle; }
.badge { display: inline-block; padding: 1px 9px; border-radius: 999px; font-size: 12px; font-weight: 600; vertical-align: middle; }
.badge.open { background: color-mix(in srgb, var(--ok) 18%, transparent); color: var(--ok); }
.badge.closed { background: var(--chip); color: var(--muted); }
.kind { font-size: 13px; white-space: nowrap; }
.pos { color: var(--ok); }
.neg { color: var(--danger); }

/* dialogs */
dialog {
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 0;
  width: min(460px, calc(100vw - 32px));
  max-height: calc(100vh - 32px);
  background: var(--panel);
  color: var(--text);
}
dialog.wide { width: min(980px, calc(100vw - 32px)); }
dialog.medium { width: min(680px, calc(100vw - 32px)); }
dialog::backdrop { background: rgba(0, 0, 0, .45); }
dialog form { display: flex; flex-direction: column; gap: 12px; padding: 20px; }
dialog h2 { margin: 0 0 4px; font-size: 19px; }
dialog form label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); }
dialog form label small { font-size: 12px; }
dialog form input, dialog form select, dialog form textarea { color: var(--text); }
dialog .actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 4px; }
.subject { display: flex; align-items: center; gap: 10px; margin: 0; padding: 8px 10px; background: var(--chip); border-radius: 8px; }
.subject b { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; margin-right: 4px; }

.label-list { display: flex; flex-direction: column; gap: 8px; max-height: 42vh; overflow: auto; padding: 4px; }
.label-item { display: flex !important; flex-direction: row !important; align-items: center; gap: 10px; }
.label-item canvas {
  height: 72px;
  max-width: 100%;
  object-fit: contain;
  object-position: left;
  image-rendering: pixelated;
  border: 1px solid #c8ccd2;
  border-radius: 3px;
  background: #fff;
}
#print-area { display: none; }
@media print {
  body { background: #fff; }
  body > :not(#print-area) { display: none !important; }
  #print-area { display: block; }
  .print-label { display: flex; align-items: center; break-after: page; overflow: hidden; }
  .print-label img { display: block; image-rendering: pixelated; }
}

.detail { position: relative; padding: 22px; }
.detail .close {
  position: absolute; top: 10px; right: 10px; z-index: 1;
  width: 36px; height: 36px; padding: 0; border-radius: 50%;
  background: var(--panel); font-size: 16px; color: var(--muted);
}
.detail h3 { margin: 24px 0 8px; font-size: 14px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
.detail-head { display: flex; gap: 22px; flex-wrap: wrap; }
.detail .info { flex: 1 1 320px; min-width: 0; }
.detail .info h2 { font-size: 22px; padding-right: 30px; }
.detail .description { margin: 6px 0 12px; white-space: pre-wrap; }
.detail dl { display: flex; flex-wrap: wrap; gap: 8px 28px; margin: 0 0 14px; }
.detail dt { font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.detail dd { margin: 0; font-size: 18px; font-weight: 600; font-variant-numeric: tabular-nums; }
.button-row { display: flex; flex-wrap: wrap; gap: 8px; }
.photo { flex: 0 0 220px; display: flex; flex-direction: column; gap: 8px; }
.photo img, .no-photo { width: 220px; height: 220px; border-radius: 8px; border: 1px solid var(--border); object-fit: cover; display: block; }
.no-photo { display: flex; align-items: center; justify-content: center; background: var(--chip); color: var(--muted); }
.photo-actions { display: flex; gap: 6px; }
.photo-actions button { flex: 1; padding: 6px 8px; font-size: 13px; }

@media (max-width: 640px) {
  header { padding: 14px 16px 10px; }
  .brand { width: 100%; justify-content: space-between; }
  .stats { gap: 14px; }
  .stats span { font-size: 16px; }
  .detail { padding: 16px; }
  .photo { flex-basis: 100%; }
  .photo { margin-top: 30px; }
  .photo img, .no-photo { width: 100%; height: auto; aspect-ratio: 4 / 3; }

  /* Parts lists become cards on phones */
  :is(#view-parts, .cards) thead { display: none; }
  :is(#view-parts, .cards) table, :is(#view-parts, .cards) tbody, table.cards, .cards tbody { display: block; }
  :is(#view-parts, .cards) tbody tr {
    display: grid;
    grid-template-columns: 48px 1fr auto;
    grid-template-areas: "thumb pn qty" "thumb desc desc" "thumb cat cat" "thumb chips chips" "thumb total actions";
    gap: 4px 10px;
    padding: 12px;
    border-bottom: 1px solid var(--border);
  }
  :is(#view-parts, .cards) tbody tr:last-child { border-bottom: none; }
  :is(#view-parts, .cards) td { border: none; padding: 0; min-width: 0; }
  :is(#view-parts, .cards) td.thumb-col { grid-area: thumb; }
  :is(#view-parts, .cards) td.pn { grid-area: pn; white-space: normal; overflow-wrap: anywhere; }
  :is(#view-parts, .cards) td.qty { grid-area: qty; }
  :is(#view-parts, .cards) td.qty::before { content: "Qty "; color: var(--muted); font-weight: 400; font-size: 13px; }
  :is(#view-parts, .cards) td.desc { grid-area: desc; }
  :is(#view-parts, .cards) td.chips { grid-area: chips; }
  :is(#view-parts, .cards) td.cat { grid-area: cat; }
  :is(#view-parts, .cards) td.cat:empty { display: none; }
  :is(#view-parts, .cards) td.chips:empty { display: none; }
  :is(#view-parts, .cards) td.unit { display: none; }
  :is(#view-parts, .cards) td.total { grid-area: total; text-align: left; align-self: center; color: var(--muted); }
  :is(#view-parts, .cards) td.actions { grid-area: actions; }
}
