:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --border: #dde1e7;
  --text: #1b1f24;
  --muted: #5f6b7a;
  --accent: #1f5fd1;
  --accent-text: #ffffff;
  --accent-soft: #e6eefc;
  --danger: #b42318;
  --danger-soft: #fdecea;
  --ok: #117a43;
  --ok-soft: #e4f5ec;
  --warn: #9a5b00;
  --warn-soft: #fff3dc;
  --gray-soft: #eceef1;
  --radius: 8px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .08);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #111418; --surface: #1a1e24; --surface-2: #222730; --border: #2f3540;
    --text: #e8ebef; --muted: #9aa5b3; --accent: #6b9cff; --accent-text: #0b1220; --accent-soft: #1e2a44;
    --danger: #ff8a80; --danger-soft: #3a1d1b; --ok: #6fd39d; --ok-soft: #173024;
    --warn: #ffc266; --warn-soft: #3a2c12; --gray-soft: #2a2f38;
    --shadow: none; color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font: 15px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 1.5rem; margin: 0 0 .25rem; }
h2 { font-size: 1.1rem; margin: 1.5rem 0 .6rem; }
h3 { font-size: 1rem; margin: 0 0 .5rem; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }
.nowrap { white-space: nowrap; }

/* ---- top bar ---- */
.topbar {
  background: var(--surface); border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 20;
}
.topbar-inner {
  max-width: 1200px; margin: 0 auto; padding: 8px 16px;
  display: flex; align-items: center; gap: 12px; flex-wrap: wrap;
}
.brand { display: flex; align-items: baseline; gap: 8px; color: var(--text); white-space: nowrap; }
.brand:hover { text-decoration: none; }
.brand .logo { height: 19px; width: auto; align-self: center; }
.brand-sub { font-weight: 600; color: var(--accent); font-size: .95rem; letter-spacing: .01em; }
/* Black wordmark on light backgrounds, white on dark. */
.on-dark { display: none; }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .on-light { display: none; }
  :root:not([data-theme="light"]) .on-dark { display: inline; }
}
:root[data-theme="dark"] .on-light { display: none; }
:root[data-theme="dark"] .on-dark { display: inline; }
.scanbar { flex: 1 1 260px; display: flex; gap: 6px; min-width: 0; }
.scanbar input { flex: 1; min-width: 0; font-family: ui-monospace, Menlo, monospace; }
.usermenu { display: flex; align-items: center; gap: 10px; font-size: .9rem; white-space: nowrap; }
.usermenu form { margin: 0; }
.nav { background: var(--surface); border-bottom: 1px solid var(--border); }
.nav-inner {
  max-width: 1200px; margin: 0 auto; padding: 0 16px;
  display: flex; gap: 4px; overflow-x: auto; scrollbar-width: none;
}
.nav a {
  padding: 10px 10px; color: var(--muted); white-space: nowrap; border-bottom: 2px solid transparent; font-weight: 500;
}
.nav a:hover { color: var(--text); text-decoration: none; }
.nav a.on { color: var(--accent); border-bottom-color: var(--accent); }

main { max-width: 1200px; margin: 0 auto; padding: 20px 16px 60px; }
.page-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; flex-wrap: wrap; margin-bottom: 16px; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }

/* ---- cards / grid ---- */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow); }
.card + .card { margin-top: 16px; }
.grid { display: grid; gap: 16px; }
.grid-2 { grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); }
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 12px; }
.stat { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px 14px; color: var(--text); }
.stat:hover { text-decoration: none; border-color: var(--accent); }
.stat .n { font-size: 1.6rem; font-weight: 700; font-variant-numeric: tabular-nums; }
.stat .l { color: var(--muted); font-size: .85rem; }
dl.kv { display: grid; grid-template-columns: max-content 1fr; gap: 6px 16px; margin: 0; }
dl.kv dt { color: var(--muted); }
dl.kv dd { margin: 0; overflow-wrap: anywhere; }

/* ---- forms ---- */
label { display: block; font-weight: 600; font-size: .9rem; margin-bottom: 4px; }
.field { margin-bottom: 14px; }
.row { display: grid; gap: 12px; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); }
input[type=text], input[type=email], input[type=password], input[type=number], input[type=search],
input[type=datetime-local], input[type=date], select, textarea {
  width: 100%; padding: 9px 10px; font: inherit; color: var(--text); background: var(--surface);
  border: 1px solid var(--border); border-radius: 6px;
}
input:focus, select:focus, textarea:focus, button:focus-visible, .btn:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 1px;
}
textarea { min-height: 80px; resize: vertical; }
.check { display: flex; gap: 8px; align-items: center; font-weight: 500; }
.check input { width: 18px; height: 18px; }
.hint { color: var(--muted); font-size: .82rem; margin-top: 4px; font-weight: 400; }
.req::after { content: " *"; color: var(--danger); }

.btn, button {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 9px 14px; font: inherit; font-weight: 600; border-radius: 6px; cursor: pointer;
  border: 1px solid var(--border); background: var(--surface); color: var(--text); white-space: nowrap;
}
.btn:hover, button:hover { text-decoration: none; background: var(--surface-2); }
.btn-primary, button.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn-primary:hover, button.btn-primary:hover { background: var(--accent); filter: brightness(1.08); }
.btn-danger { color: var(--danger); }
.btn-sm { padding: 5px 10px; font-size: .85rem; }
.btn-lg { padding: 12px 18px; font-size: 1rem; }
.linkbtn { background: none; border: none; padding: 0; color: var(--accent); font-weight: 500; }
.linkbtn:hover { background: none; text-decoration: underline; }

.alert { padding: 10px 14px; border-radius: 6px; margin-bottom: 14px; }
.alert ul { margin: 4px 0 0; padding-left: 20px; }
.alert-error { background: var(--danger-soft); color: var(--danger); }
.alert-ok { background: var(--ok-soft); color: var(--ok); }
.alert-warn { background: var(--warn-soft); color: var(--warn); }

/* ---- tables ---- */
.table-wrap { overflow-x: auto; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 9px 12px; border-bottom: 1px solid var(--border); vertical-align: top; }
th { font-size: .8rem; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); background: var(--surface-2); white-space: nowrap; }
tr:last-child td { border-bottom: none; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
tbody tr:hover { background: var(--surface-2); }

.badge { display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: .78rem; font-weight: 600; white-space: nowrap; }
.st-in_vault { background: var(--ok-soft); color: var(--ok); }
.st-checked_out { background: var(--warn-soft); color: var(--warn); }
.st-in_transit { background: var(--accent-soft); color: var(--accent); }
.st-delivered { background: var(--gray-soft); color: var(--muted); }
.st-destroyed { background: var(--danger-soft); color: var(--danger); }
.act { background: var(--gray-soft); color: var(--text); }

.filters { display: flex; gap: 8px; flex-wrap: wrap; align-items: flex-end; margin-bottom: 14px; }
.filters .field { margin: 0; flex: 1 1 160px; }
.pager { display: flex; justify-content: space-between; align-items: center; margin-top: 12px; }

/* ---- timeline ---- */
.timeline { list-style: none; margin: 0; padding: 0; }
.timeline li { padding: 12px 0 12px 22px; border-left: 2px solid var(--border); position: relative; margin-left: 6px; }
.timeline li::before {
  content: ""; position: absolute; left: -7px; top: 16px; width: 12px; height: 12px; border-radius: 50%;
  background: var(--surface); border: 2px solid var(--accent);
}
.timeline .when { color: var(--muted); font-size: .85rem; }
.photos { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 8px; }
.photos a img, .photo-thumb img { width: 84px; height: 84px; object-fit: cover; border-radius: 6px; border: 1px solid var(--border); display: block; }

/* ---- autocomplete ---- */
.ac { position: relative; }
.ac-list {
  position: absolute; left: 0; right: 0; top: 100%; z-index: 30; margin: 2px 0 0; padding: 4px 0; list-style: none;
  background: var(--surface); border: 1px solid var(--border); border-radius: 6px; box-shadow: 0 6px 18px rgba(0,0,0,.12);
  max-height: 280px; overflow-y: auto;
}
.ac-list li { padding: 8px 10px; cursor: pointer; }
.ac-list li.on, .ac-list li:hover { background: var(--accent-soft); }
.ac.ok input[type=text] { border-color: var(--ok); }

/* ---- photo picker ---- */
.photo-picker .buttons { display: flex; gap: 8px; flex-wrap: wrap; }
.photo-thumb { position: relative; }
.photo-thumb button {
  position: absolute; top: -6px; right: -6px; width: 22px; height: 22px; padding: 0; border-radius: 50%;
  font-size: 13px; line-height: 1; background: var(--danger); color: #fff; border: none;
}

/* ---- scanning ---- */
.scan-hero { text-align: center; padding: 24px 16px; }
.scan-hero input { font-size: 1.3rem; padding: 12px; text-align: center; max-width: 420px; font-family: ui-monospace, Menlo, monospace; }
#reader { max-width: 480px; margin: 0 auto; }
.batch-list td { vertical-align: middle; }

.barcode-img { background: #fff; padding: 8px 12px; border-radius: 6px; border: 1px solid var(--border); display: inline-block; }
.barcode-img img { height: 56px; width: 220px; display: block; }

.login-wrap { max-width: 380px; margin: 10vh auto; padding: 0 16px; }
.logo-login { width: 200px; height: auto; display: block; margin-bottom: 14px; }
.login-wrap h1 { font-size: 1.25rem; font-weight: 600; color: var(--muted); letter-spacing: .02em; }

@media (max-width: 640px) {
  .usermenu .who { display: none; }
  h1 { font-size: 1.3rem; }
  th, td { padding: 8px; }
  .hide-sm { display: none; }
}

/* ---- security UI ---- */
.idle-warning {
  position: fixed; left: 50%; transform: translateX(-50%); bottom: 20px; z-index: 100;
  background: var(--warn-soft); color: var(--warn); border: 1px solid var(--warn);
  border-radius: 8px; padding: 10px 14px; display: flex; gap: 12px; align-items: center;
  box-shadow: 0 8px 24px rgba(0,0,0,.18); font-weight: 600; max-width: calc(100vw - 32px);
}
.idle-warning button { padding: 6px 12px; }
.qr-wrap { background: #fff; padding: 12px; border-radius: 8px; display: inline-block; border: 1px solid var(--border); }
.qr-wrap svg { width: 200px; height: 200px; display: block; }
.codes { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 8px; margin: 14px 0;
  background: var(--surface-2); padding: 14px; border-radius: 8px; user-select: all; font-size: 1.05rem; }
