:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --border: #dde1e7;
  --text: #1b2330;
  --muted: #5d6878;
  --accent: #1f5fbf;
  --accent-text: #ffffff;
  --accent-soft: #e6eefb;
  --good: #1e7a46;
  --good-soft: #e3f4ea;
  --warn: #9a5b00;
  --warn-soft: #fdf1dc;
  --bad: #b3261e;
  --bad-soft: #fbe5e3;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.06);
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #11151b;
    --surface: #181d25;
    --surface-2: #1f2530;
    --border: #2c3442;
    --text: #e6e9ef;
    --muted: #97a1b2;
    --accent: #6ea2f5;
    --accent-text: #0d1420;
    --accent-soft: #1d2b42;
    --good: #5cc98b;
    --good-soft: #17301f;
    --warn: #e6ad55;
    --warn-soft: #33270f;
    --bad: #f08880;
    --bad-soft: #3a1b19;
    --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: 14px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
h1, h2, h3 { margin: 0; line-height: 1.25; }
h2 { font-size: 16px; }
h3 { font-size: 14px; }
a { color: var(--accent); }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.mono { font-family: var(--mono); }
.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.hidden { display: none !important; }

/* ---------- header ---------- */
.topbar {
  position: sticky; top: 0; z-index: 10;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.topbar-inner {
  max-width: 1280px; margin: 0 auto; padding: 10px 16px;
  display: flex; align-items: center; gap: 16px; flex-wrap: wrap;
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 17px; }
.brand-mark {
  width: 28px; height: 28px; border-radius: 7px; background: var(--accent); color: var(--accent-text);
  display: grid; place-items: center; font-size: 14px; font-weight: 800;
}
.tabs { display: flex; gap: 4px; flex-wrap: wrap; }
.tab {
  border: 0; background: none; color: var(--muted); font: inherit; font-weight: 600;
  padding: 7px 12px; border-radius: 8px; cursor: pointer;
}
.tab:hover { background: var(--surface-2); color: var(--text); }
.tab[aria-selected="true"] { background: var(--accent-soft); color: var(--accent); }

main { max-width: 1280px; margin: 0 auto; padding: 16px; }

/* ---------- layout ---------- */
.grid-2 { display: grid; grid-template-columns: minmax(0, 5fr) minmax(0, 7fr); gap: 16px; align-items: start; }
@media (max-width: 980px) { .grid-2 { grid-template-columns: 1fr; } }
.stack { display: grid; gap: 16px; }
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 16px; min-width: 0;
}
.card-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; margin-bottom: 12px; flex-wrap: wrap; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.fields { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 10px 12px; }
.field { display: grid; gap: 4px; min-width: 0; }
.field.wide { grid-column: 1 / -1; }
.field > span { font-size: 12px; font-weight: 600; color: var(--muted); }
.check { display: flex; align-items: center; gap: 8px; font-weight: 500; }

input, select, textarea {
  font: inherit; color: var(--text); background: var(--surface);
  border: 1px solid var(--border); border-radius: 7px; padding: 6px 9px; width: 100%; min-width: 0;
}
input[type="checkbox"] { width: auto; }
input:focus, select:focus, textarea:focus, button:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
textarea { resize: vertical; min-height: 70px; }
input[type="file"] { padding: 5px; }

.btn {
  font: inherit; font-weight: 600; cursor: pointer; border-radius: 7px; padding: 7px 13px;
  border: 1px solid var(--border); background: var(--surface); color: var(--text); white-space: nowrap;
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { color: var(--bad); }
.btn.sm { padding: 3px 9px; font-size: 12px; }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.icon-btn { border: 0; background: none; color: var(--muted); cursor: pointer; font-size: 16px; padding: 2px 6px; border-radius: 6px; }
.icon-btn:hover { background: var(--surface-2); color: var(--bad); }

/* ---------- tables ---------- */
.table-wrap { overflow-x: auto; margin: 0 -4px; }
table { width: 100%; border-collapse: collapse; }
th, td { padding: 6px 8px; border-bottom: 1px solid var(--border); text-align: left; vertical-align: middle; }
th { font-size: 12px; color: var(--muted); font-weight: 600; white-space: nowrap; }
tbody tr:last-child td { border-bottom: 0; }
.pieces input { padding: 4px 6px; }
.pieces td { padding: 4px; }
tr.cat-row td { background: var(--surface-2); font-weight: 600; font-size: 12px; text-transform: uppercase; letter-spacing: 0.03em; color: var(--muted); }
tr.selected td { background: var(--accent-soft); }
tr.total-row td { font-weight: 700; border-top: 2px solid var(--border); }
td input.sell { width: 110px; text-align: right; padding: 3px 6px; }
td input.sell.overridden { border-color: var(--warn); background: var(--warn-soft); }

/* ---------- bits ---------- */
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(120px, 1fr)); gap: 8px; }
.stat { background: var(--surface-2); border-radius: 8px; padding: 8px 10px; min-width: 0; }
.stat .label { font-size: 11px; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: 0.03em; }
.stat .value { font-size: 18px; font-weight: 700; font-variant-numeric: tabular-nums; overflow-wrap: anywhere; }
.stat .sub { font-size: 12px; color: var(--muted); }

.badge { display: inline-block; font-size: 11px; font-weight: 700; padding: 1px 7px; border-radius: 99px; background: var(--surface-2); color: var(--muted); white-space: nowrap; }
.badge.good { background: var(--good-soft); color: var(--good); }
.badge.warn { background: var(--warn-soft); color: var(--warn); }
.badge.bad { background: var(--bad-soft); color: var(--bad); }
.badge.accent { background: var(--accent-soft); color: var(--accent); }
.mode { font-family: var(--mono); font-weight: 700; font-size: 12px; }

.notice { border-radius: 8px; padding: 8px 12px; font-size: 13px; }
.notice.warn { background: var(--warn-soft); color: var(--warn); }
.notice.bad { background: var(--bad-soft); color: var(--bad); }
.notice.info { background: var(--accent-soft); color: var(--accent); }
.notice ul { margin: 4px 0 0; padding-left: 18px; max-height: 180px; overflow: auto; }

.bar { height: 6px; background: var(--surface-2); border-radius: 99px; overflow: hidden; min-width: 60px; }
.bar > div { height: 100%; background: var(--accent); }

.empty { text-align: center; color: var(--muted); padding: 28px 12px; }

.hs-box { position: relative; }
.hs-results {
  position: absolute; left: 0; right: 0; top: calc(100% + 4px); z-index: 5; max-height: 280px; overflow: auto;
  background: var(--surface); border: 1px solid var(--border); border-radius: 8px; box-shadow: 0 8px 24px rgba(0,0,0,.15);
}
.hs-results button {
  display: block; width: 100%; text-align: left; border: 0; background: none; color: var(--text);
  font: inherit; padding: 7px 10px; cursor: pointer; border-bottom: 1px solid var(--border);
}
.hs-results button:hover, .hs-results button:focus { background: var(--accent-soft); }
.hs-results .code { font-family: var(--mono); font-weight: 700; margin-right: 6px; }

.toast {
  position: fixed; bottom: 16px; left: 50%; transform: translateX(-50%); z-index: 50;
  background: var(--text); color: var(--bg); padding: 9px 16px; border-radius: 8px; font-weight: 600;
  max-width: calc(100vw - 32px);
}
.logo-preview { max-height: 48px; max-width: 180px; display: block; }
.split { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
@media (max-width: 720px) { .split { grid-template-columns: 1fr; } }
footer { max-width: 1280px; margin: 0 auto; padding: 8px 16px 32px; color: var(--muted); font-size: 12px; }

/* ---------- print / PDF quote ---------- */
#print-area { display: none; }
@media print {
  @page { margin: 14mm; }
  :root { color-scheme: light; }
  body { background: #fff; color: #000; font-size: 11pt; }
  .topbar, main, footer, .toast { display: none !important; }
  #print-area { display: block; }
  .pq-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 24px; border-bottom: 2px solid #1f5fbf; padding-bottom: 10px; margin-bottom: 14px; }
  .pq-head img { max-height: 60px; max-width: 220px; }
  .pq-company { font-size: 9pt; color: #333; white-space: pre-line; }
  .pq-company strong { font-size: 13pt; color: #000; }
  .pq-title { text-align: right; }
  .pq-title h1 { font-size: 20pt; color: #1f5fbf; }
  .pq-meta { display: grid; grid-template-columns: 1fr 1fr; gap: 6px 24px; font-size: 10pt; margin-bottom: 14px; }
  .pq-meta > div { display: grid; grid-template-columns: 100px 1fr; gap: 8px; }
  .pq-meta div span { color: #555; }
  #print-area table { font-size: 10pt; }
  #print-area th, #print-area td { border-bottom: 1px solid #ccc; padding: 4px 6px; color: #000; }
  #print-area tr.cat-row td { background: #eef2f8; color: #333; }
  #print-area tr.total-row td { font-size: 12pt; }
  .pq-notes { margin-top: 14px; font-size: 9pt; color: #333; white-space: pre-line; }
  .pq-notes h3 { font-size: 10pt; margin-bottom: 2px; color: #000; }
  tr { break-inside: avoid; }
}

/* ---------- utilities (no inline styles: the CSP forbids them) ---------- */
.sub-h { margin: 16px 0 6px; }
.mt-0 { margin-top: 0; }
.mt-6 { margin-top: 6px; }
.mt-8 { margin-top: 8px; }
.mt-12 { margin-top: 12px; }
.mb-0 { margin-bottom: 0; }
.mb-8 { margin-bottom: 8px; }
.mb-12 { margin-bottom: 12px; }
.gap-8 { gap: 8px; }
.w-80 { width: 80px; }
.w-auto { width: auto; }
.w-240 { width: 240px; max-width: 100%; }
.w-260 { width: 260px; max-width: 100%; }
.mw-260 { max-width: 260px; }
td.bad { color: var(--bad); }
