:root {
  --bg: #f4f6f8;
  --surface: #ffffff;
  --surface-2: #f8fafb;
  --border: #e3e8ec;
  --border-strong: #cfd7de;
  --text: #17212b;
  --text-2: #4a5866;
  --muted: #7a8794;
  --primary: #0f766e;
  --primary-hover: #0d645d;
  --primary-soft: #e3f3f1;
  --good: #15803d;
  --good-soft: #e7f6ec;
  --warn: #b45309;
  --warn-soft: #fdf3e3;
  --bad: #b91c1c;
  --bad-soft: #fdecec;
  --info: #1d4ed8;
  --info-soft: #e8eefc;
  --chart: #14b8a6;
  --chart-2: #94a3b8;
  --shadow: 0 1px 2px rgba(16, 24, 40, .05), 0 1px 3px rgba(16, 24, 40, .06);
  --shadow-lg: 0 20px 50px rgba(16, 24, 40, .18);
  --radius: 10px;
  --sidebar: #0f2a2e;
  --sidebar-text: #b9cfd0;
  --sidebar-active: #ffffff;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e1418;
    --surface: #151d22;
    --surface-2: #1a242a;
    --border: #26323a;
    --border-strong: #33414a;
    --text: #e6edf1;
    --text-2: #b3c0c8;
    --muted: #82929c;
    --primary: #2dd4bf;
    --primary-hover: #5eead4;
    --primary-soft: #133a37;
    --good: #4ade80;
    --good-soft: #14301f;
    --warn: #fbbf24;
    --warn-soft: #3a2c10;
    --bad: #f87171;
    --bad-soft: #3b1717;
    --info: #93b4ff;
    --info-soft: #1a2747;
    --chart: #2dd4bf;
    --chart-2: #4b5b66;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3);
    --shadow-lg: 0 20px 50px rgba(0, 0, 0, .5);
    --sidebar: #0a1013;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, Roboto, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}
a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 22px; margin: 0; letter-spacing: -.01em; }
h2 { font-size: 15px; margin: 0; }
h3 { font-size: 13px; margin: 0 0 8px; color: var(--text-2); text-transform: uppercase; letter-spacing: .04em; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.nowrap { white-space: nowrap; }
.good { color: var(--good); }
.warn { color: var(--warn); }
.bad { color: var(--bad); }
.strong { font-weight: 600; }

/* layout */
.app { display: flex; min-height: 100vh; }
.sidebar {
  width: 236px; flex-shrink: 0; background: var(--sidebar); color: var(--sidebar-text);
  position: sticky; top: 0; height: 100vh; overflow-y: auto; padding: 18px 12px;
}
.brand { display: flex; gap: 10px; align-items: center; padding: 4px 8px 18px; }
.logo { width: 36px; height: 36px; border-radius: 9px; background: #14b8a6; color: #fff; display: grid; place-items: center; }
.brand-name { color: #fff; font-weight: 700; letter-spacing: .06em; }
.brand-sub { font-size: 11.5px; opacity: .8; max-width: 160px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.nav { display: flex; flex-direction: column; gap: 2px; }
.nav-label { font-size: 10.5px; text-transform: uppercase; letter-spacing: .1em; opacity: .55; padding: 14px 10px 6px; }
.nav a {
  display: flex; align-items: center; gap: 10px; padding: 8px 10px; border-radius: 8px;
  color: var(--sidebar-text); font-weight: 500;
}
.nav a:hover { background: rgba(255, 255, 255, .06); text-decoration: none; color: #fff; }
.nav a.active { background: rgba(20, 184, 166, .18); color: var(--sidebar-active); }
.nav .i { width: 18px; text-align: center; opacity: .85; }
.pill-count { margin-left: auto; background: #f59e0b; color: #1f1300; border-radius: 99px; font-size: 11px; padding: 0 7px; line-height: 18px; }
.pill-count:empty { display: none; }
.main-wrap { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.topbar {
  height: 52px; display: flex; align-items: center; gap: 12px; padding: 0 24px;
  border-bottom: 1px solid var(--border); background: var(--surface); position: sticky; top: 0; z-index: 5;
}
.topbar-title { font-weight: 600; }
.topbar-right { margin-left: auto; }
.menu-btn { display: none; }
.main { padding: 24px; max-width: 1480px; width: 100%; }
.loading { padding: 40px; color: var(--muted); }

.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 16px; margin-bottom: 20px; flex-wrap: wrap; }
.page-head .sub { margin: 4px 0 0; color: var(--muted); }
.actions { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }

/* cards */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 14px 16px; border-bottom: 1px solid var(--border); }
.card-body { padding: 16px; }
.card-body.flush { padding: 0; }
.grid2 { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px; }
.grid3 { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 16px; }
.stack { display: flex; flex-direction: column; gap: 16px; }

.kpis { display: grid; grid-template-columns: repeat(auto-fill, minmax(210px, 1fr)); gap: 12px; margin-bottom: 16px; }
.kpi { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; box-shadow: var(--shadow); position: relative; overflow: hidden; }
.kpi::before { content: ""; position: absolute; left: 0; top: 0; bottom: 0; width: 3px; background: var(--border-strong); }
.kpi.good::before { background: var(--good); }
.kpi.warn::before { background: #f59e0b; }
.kpi.bad::before { background: var(--bad); }
.kpi.info::before { background: var(--primary); }
.kpi-label { font-size: 12px; color: var(--muted); font-weight: 500; }
.kpi-value { font-size: 24px; font-weight: 650; margin-top: 4px; letter-spacing: -.02em; font-variant-numeric: tabular-nums; }
.kpi-sub { font-size: 12px; color: var(--text-2); margin-top: 2px; }
a.kpi { color: inherit; display: block; }
a.kpi:hover { text-decoration: none; border-color: var(--border-strong); }

/* buttons & inputs */
.btn {
  display: inline-flex; align-items: center; gap: 6px; height: 34px; padding: 0 14px; border-radius: 8px;
  border: 1px solid var(--border-strong); background: var(--surface); color: var(--text); font: inherit; font-weight: 550;
  cursor: pointer; white-space: nowrap;
}
.btn:hover { background: var(--surface-2); text-decoration: none; }
.btn.primary { background: var(--primary); border-color: var(--primary); color: #fff; }
@media (prefers-color-scheme: dark) { .btn.primary { color: #042f2b; } }
.btn.primary:hover { background: var(--primary-hover); }
.btn.danger { color: var(--bad); }
.btn.danger:hover { background: var(--bad-soft); }
.btn.sm { height: 28px; padding: 0 10px; font-size: 12.5px; }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.ghost:hover { background: var(--surface-2); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.icon-btn { border: 0; background: transparent; font-size: 18px; cursor: pointer; color: var(--text); }

input, select, textarea {
  font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--border-strong);
  border-radius: 8px; padding: 7px 10px; min-height: 34px; width: 100%;
}
textarea { min-height: 70px; resize: vertical; }
input:focus, select:focus, textarea:focus { outline: 2px solid color-mix(in srgb, var(--primary) 35%, transparent); border-color: var(--primary); }
input[type=checkbox] { width: 16px; height: 16px; min-height: 0; accent-color: var(--primary); }
input.sm, select.sm { min-height: 28px; padding: 3px 8px; font-size: 13px; }
.toolbar { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; padding: 12px 16px; border-bottom: 1px solid var(--border); }
.toolbar input[type=search] { max-width: 280px; }
.toolbar select { width: auto; }
.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px 16px; }
.form-grid .full { grid-column: 1 / -1; }
.field label { display: block; font-size: 12px; font-weight: 600; color: var(--text-2); margin-bottom: 4px; }
.field .hint { font-size: 11.5px; color: var(--muted); margin-top: 3px; }

/* tabs */
.tabs { display: flex; gap: 4px; padding: 8px 12px 0; border-bottom: 1px solid var(--border); overflow-x: auto; }
.tab { padding: 8px 12px; border: 0; background: none; font: inherit; font-weight: 550; color: var(--muted); cursor: pointer; border-bottom: 2px solid transparent; white-space: nowrap; }
.tab:hover { color: var(--text); }
.tab.active { color: var(--primary); border-bottom-color: var(--primary); }
.tab .c { font-size: 11px; background: var(--surface-2); border: 1px solid var(--border); border-radius: 99px; padding: 0 6px; margin-left: 4px; }

/* tables */
.table-wrap { 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; }
th { font-size: 11.5px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); font-weight: 600; background: var(--surface-2); position: sticky; top: 0; white-space: nowrap; }
th.sortable { cursor: pointer; user-select: none; }
th.sortable:hover { color: var(--text); }
tbody tr:last-child td { border-bottom: 0; }
tbody tr.click { cursor: pointer; }
tbody tr.click:hover td { background: var(--surface-2); }
tfoot td { font-weight: 600; background: var(--surface-2); border-top: 1px solid var(--border-strong); border-bottom: 0; }
td .sub { display: block; font-size: 12px; color: var(--muted); }
.empty { padding: 32px; text-align: center; color: var(--muted); }

.badge { display: inline-flex; align-items: center; gap: 4px; font-size: 11.5px; font-weight: 600; padding: 2px 8px; border-radius: 99px; white-space: nowrap; background: var(--surface-2); color: var(--text-2); border: 1px solid var(--border); }
.badge.good { background: var(--good-soft); color: var(--good); border-color: transparent; }
.badge.warn { background: var(--warn-soft); color: var(--warn); border-color: transparent; }
.badge.bad { background: var(--bad-soft); color: var(--bad); border-color: transparent; }
.badge.info { background: var(--info-soft); color: var(--info); border-color: transparent; }
.badge.primary { background: var(--primary-soft); color: var(--primary); border-color: transparent; }

/* price matrix */
.matrix td:first-child, #tbl td:first-child { min-width: 230px; }
.matrix td.cell { text-align: right; cursor: pointer; font-variant-numeric: tabular-nums; }
.matrix td.cell:hover { outline: 1px solid var(--primary); outline-offset: -1px; }
.matrix td.best { background: var(--good-soft); color: var(--good); font-weight: 650; }
.matrix td.worst { background: var(--bad-soft); color: var(--bad); }
.matrix td.none { color: var(--muted); }
.legend { display: flex; gap: 14px; flex-wrap: wrap; font-size: 12px; color: var(--muted); }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 5px; vertical-align: -1px; }

/* bars */
.hbars { display: flex; flex-direction: column; gap: 10px; }
.hbar-row { display: grid; grid-template-columns: 150px 1fr 90px; gap: 10px; align-items: center; font-size: 13px; }
.hbar-track { height: 10px; background: var(--surface-2); border-radius: 99px; overflow: hidden; border: 1px solid var(--border); }
.hbar-fill { height: 100%; background: var(--chart); border-radius: 99px; }
.chart svg { display: block; width: 100%; height: auto; }
.chart text { fill: var(--muted); font-size: 11px; }
.chart .bar { fill: var(--chart); }
.chart .bar:hover { opacity: .8; }
.chart .grid { stroke: var(--border); }

.meter { display: inline-block; width: 60px; height: 6px; border-radius: 99px; background: var(--surface-2); border: 1px solid var(--border); overflow: hidden; vertical-align: middle; margin-right: 6px; }
.meter > span { display: block; height: 100%; background: var(--good); }

.callout { padding: 10px 12px; border-radius: 8px; background: var(--info-soft); color: var(--info); font-size: 13px; }
.callout.warn { background: var(--warn-soft); color: var(--warn); }
.callout.good { background: var(--good-soft); color: var(--good); }
.callout.bad { background: var(--bad-soft); color: var(--bad); }

.supplier-cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(320px, 1fr)); gap: 16px; }
.sup-card { padding: 16px; display: flex; flex-direction: column; gap: 12px; }
.sup-card .stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.stat { background: var(--surface-2); border-radius: 8px; padding: 8px 10px; }
.stat .l { font-size: 11px; color: var(--muted); }
.stat .v { font-weight: 650; font-variant-numeric: tabular-nums; }
.stars { color: #f59e0b; letter-spacing: 1px; }

/* modal */
.modal-backdrop { position: fixed; inset: 0; background: rgba(10, 20, 25, .45); display: flex; align-items: flex-start; justify-content: center; padding: 5vh 16px; z-index: 50; overflow-y: auto; animation: fade .12s ease-out; }
.modal { background: var(--surface); border-radius: 12px; box-shadow: var(--shadow-lg); width: 100%; max-width: 560px; border: 1px solid var(--border); animation: pop .14s ease-out; }
.modal.wide { max-width: 980px; }
.modal.xwide { max-width: 1200px; }
.modal-head { display: flex; align-items: center; justify-content: space-between; padding: 16px 20px; border-bottom: 1px solid var(--border); gap: 12px; }
.modal-body { padding: 20px; }
.modal-foot { display: flex; justify-content: flex-end; gap: 8px; padding: 14px 20px; border-top: 1px solid var(--border); background: var(--surface-2); border-radius: 0 0 12px 12px; flex-wrap: wrap; }
.modal-foot .left { margin-right: auto; display: flex; gap: 8px; }
@keyframes fade { from { opacity: 0; } }
@keyframes pop { from { transform: translateY(8px) scale(.99); opacity: 0; } }

.toasts { position: fixed; bottom: 20px; right: 20px; display: flex; flex-direction: column; gap: 8px; z-index: 100; }
.toast { background: var(--text); color: var(--bg); padding: 10px 14px; border-radius: 8px; box-shadow: var(--shadow-lg); max-width: 380px; animation: pop .15s ease-out; }
.toast.error { background: var(--bad); color: #fff; }

.section-title { display: flex; align-items: center; justify-content: space-between; margin: 20px 0 10px; }
.kv { display: grid; grid-template-columns: 140px 1fr; gap: 6px 12px; font-size: 13px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; }
.mt { margin-top: 16px; }
.mb { margin-bottom: 16px; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.spacer { flex: 1; }

@media (max-width: 1000px) {
  .grid2, .grid3 { grid-template-columns: 1fr; }
}
@media (max-width: 800px) {
  .sidebar { position: fixed; left: -260px; z-index: 40; transition: left .2s; }
  .sidebar.open { left: 0; box-shadow: var(--shadow-lg); }
  .menu-btn { display: block; }
  .main { padding: 16px; }
  .topbar { padding: 0 16px; }
  .form-grid { grid-template-columns: 1fr; }
  .hbar-row { grid-template-columns: 100px 1fr 80px; }
}

/* stock count */
.print-only { display: none; }
.count-table tr.group td { background: var(--bg, transparent); border-top: 2px solid var(--border-strong); padding-top: 10px; }
.count-table tr.counted td { background: var(--good-soft); }

@media print {
  .sidebar, .topbar, .toolbar, .actions, .btn { display: none !important; }
  .print-only { display: block; margin: 0 0 10px; font-size: 13px; }
  .page-head p, .page-head .sub { display: none; }
  .count-table { font-size: 11px; }
  .count-table td, .count-table th { padding: 4px 6px; }
  .count-table input { border: 1px solid #999; background: #fff; color: #000; height: 22px; }
  .count-table input[type=date]::-webkit-calendar-picker-indicator { display: none; }
  .count-table tr { break-inside: avoid; }
  .count-table tr.counted td { background: none; }
  .main { padding: 0; }
  .card { box-shadow: none; }
}

/* product search in order editor */
.psearch { position: relative; }
.psearch input { width: 100%; font-size: 15px; padding: 10px 12px; }
.psearch-list { position: absolute; left: 0; right: 0; top: calc(100% + 4px); z-index: 50; max-height: 360px; overflow-y: auto;
  background: var(--surface); border: 1px solid var(--border-strong); border-radius: var(--radius); box-shadow: var(--shadow-lg); }
.psearch-item { display: flex; gap: 12px; align-items: center; padding: 8px 12px; cursor: pointer; border-bottom: 1px solid var(--border); }
.psearch-item:last-child { border-bottom: 0; }
.psearch-item.active, .psearch-item:hover { background: var(--primary-soft); }
.psearch-main { flex: 1; min-width: 0; }
.psearch-price { text-align: right; white-space: nowrap; }
.psearch-price .sub, .psearch-main .sub { display: block; }
.psearch-empty { padding: 12px; color: var(--muted); }

/* supplier order codes */
.code { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12.5px; letter-spacing: .2px; }

/* quote status on order lines */
tr.not-quoted td:first-child { box-shadow: inset 3px 0 0 var(--warn); }
tr.not-quoted { background: var(--warn-soft); }
.oqs { margin-top: 4px; display: flex; flex-wrap: wrap; gap: 4px 10px; font-size: 11.5px; }
.oq.yes { color: var(--good); }
.oq.no { color: var(--warn); }
.oq.yes::before { content: "✓ "; }
.oq.no::before { content: "✕ "; }
.oq.alt { color: var(--info); }
.oq.alt::before { content: "≈ "; }

/* barcode scanning */
.scanbox-row { display: flex; gap: 8px; align-items: center; }
.scan-ico { font-size: 20px; color: var(--primary); }
.scan-input { flex: 1; font-size: 16px; padding: 10px 12px; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; }
.scan-input:focus { outline: 2px solid var(--primary); outline-offset: 1px; }
.scan-cam { position: relative; margin-top: 10px; max-width: 520px; border-radius: 10px; overflow: hidden; background: #000; }
.scan-cam video { width: 100%; display: block; }
.scan-cam .scan-frame { position: absolute; top: 22%; bottom: 22%; left: 12%; right: 12%; border: 2px solid rgba(255,255,255,.85); border-radius: 8px; pointer-events: none; }
.scan-cam [data-camoff] { position: absolute; right: 8px; bottom: 8px; }
.scan-hit { display: flex; gap: 16px; align-items: flex-start; justify-content: space-between; flex-wrap: wrap; padding: 12px; border: 1px solid var(--border); border-radius: 10px; background: var(--primary-soft); }
.scan-name { font-size: 16px; }
.scan-stats { display: flex; gap: 18px; align-items: center; flex-wrap: wrap; }
.scan-stats .sub { display: block; }
.bc-list { display: flex; flex-wrap: wrap; gap: 6px; }
.bc-chip { display: inline-flex; align-items: center; gap: 6px; padding: 3px 4px 3px 10px; border: 1px solid var(--border); border-radius: 99px; background: var(--surface-2); }
.bc-chip .icon-btn { width: 22px; height: 22px; font-size: 11px; }
tr.flash td { background: var(--good-soft) !important; transition: background .3s; }
@media print { .no-print { display: none !important; } }

/* sign-in and user menu */
.login-wrap { min-height: 100vh; display: grid; place-items: center; padding: 16px; background: var(--bg); }
.login-card { width: 100%; max-width: 380px; padding: 28px; }
.login-card .field { margin-bottom: 12px; }
.login-card input { width: 100%; }
.usermenu { position: relative; }
.usermenu .btn { display: inline-flex; align-items: center; gap: 8px; }
.avatar { width: 26px; height: 26px; border-radius: 50%; display: inline-grid; place-items: center; background: var(--primary-soft); color: var(--primary); font-size: 11px; font-weight: 700; }
.um-pop { position: absolute; right: 0; top: calc(100% + 6px); min-width: 220px; z-index: 60; padding: 4px 0; }
.um-item { display: block; width: 100%; text-align: left; padding: 9px 12px; border: 0; background: none; font: inherit; color: var(--text); cursor: pointer; }
.um-item:hover { background: var(--surface-2); }
@media (max-width: 600px) { .um-name, .um-role { display: none; } }
.login-card .brand-name { color: var(--text); }
.swbtn { margin-top: 4px; padding: 2px 8px; font-size: 12px; color: var(--primary); }
.rt-radio { display: inline-flex; align-items: center; gap: 6px; cursor: pointer; }
.rt-radio input { width: auto; height: auto; margin: 0; padding: 0; box-shadow: none; }
.field label.rt-radio { display: inline-flex; margin: 0; font-weight: 500; }
