:root {
  --bg: #f5f7f9;
  --surface: #ffffff;
  --text: #17212b;
  --muted: #5d6b78;
  --border: #d9e0e6;
  --accent: #146c94;
  --accent-text: #ffffff;
  --row-hover: #eef4f8;
  --low-bg: #fff5e6;
  --low-edge: #d98a00;
  --danger: #b42318;
  --radius: 8px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f151b;
    --surface: #17202a;
    --text: #e3e9ee;
    --muted: #93a2b0;
    --border: #2a3744;
    --accent: #4fb3e0;
    --accent-text: #08202c;
    --row-hover: #1d2935;
    --low-bg: #2e2412;
    --low-edge: #e8a33a;
    --danger: #f2877d;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
h1 { font-size: 20px; margin: 0; }
h2 { font-size: 16px; margin: 0 0 8px; }
.muted { color: var(--muted); }
.error { color: var(--danger); min-height: 1em; margin: 8px 0 0; }

button, .button {
  font: inherit;
  padding: 6px 12px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  text-decoration: none;
  white-space: nowrap;
}
button:hover, .button:hover { border-color: var(--accent); }
button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); font-weight: 600; }
button.danger { color: var(--danger); }
button.ghost, .button.ghost { background: transparent; border-color: transparent; color: inherit; }
button.ghost:hover, .button.ghost:hover { border-color: rgba(255, 255, 255, .4); }
button.small { padding: 2px 8px; font-size: 12px; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* top bar */
.topbar {
  display: flex; align-items: center; gap: 16px; flex-wrap: wrap;
  padding: 8px 16px; background: #10384d; color: #fff;
  position: sticky; top: 0; z-index: 5;
}
.brand { color: #fff; font-weight: 700; text-decoration: none; }
#tabs { display: flex; gap: 2px; flex-wrap: wrap; flex: 1; }
#tabs a { color: #cfe3ee; text-decoration: none; padding: 6px 10px; border-radius: 6px; }
#tabs a:hover { background: rgba(255, 255, 255, .1); color: #fff; }
#tabs a[aria-current="page"] { background: rgba(255, 255, 255, .18); color: #fff; font-weight: 600; }
.topbar-end { display: flex; gap: 4px; align-items: center; }
#tz { width: auto; background: transparent; color: #fff; border-color: transparent; cursor: pointer; }
#tz:hover { border-color: rgba(255, 255, 255, .4); }
#tz option, #system option { background: var(--surface); color: var(--text); }
#system {
  width: auto; max-width: 240px; font-weight: 600; color: #fff; cursor: pointer;
  background: rgba(255, 255, 255, .12); border-color: rgba(255, 255, 255, .3);
}

/* per-system scope */
.scope { font-size: 13px; font-weight: 600; color: var(--accent); border: 1px solid var(--accent); border-radius: 999px; padding: 1px 8px; margin-left: 6px; vertical-align: middle; }
.data th.sys { color: var(--accent); }
h3.section { font-size: 13px; margin: 16px 0 8px; text-transform: uppercase; letter-spacing: .03em; }
h3.section:first-of-type { margin-top: 4px; }
.empty-state { max-width: 560px; margin: 48px auto; text-align: center; }

/* sign-in and accounts */
.signed-out #tabs, .signed-out #system, .signed-out .topbar-end { visibility: hidden; }
.login { max-width: 360px; margin: 64px auto; display: flex; flex-direction: column; gap: 12px; }
.login h1 { margin-bottom: 0; }
.login button { align-self: stretch; padding: 8px; }
.banner {
  display: flex; align-items: center; gap: 12px; flex-wrap: wrap; margin-bottom: 12px; padding: 8px 12px;
  border-radius: var(--radius); background: var(--low-bg); border: 1px solid var(--low-edge);
}
.data tr.inactive td { color: var(--muted); text-decoration: line-through; }
[hidden] { display: none !important; }

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

.toolbar { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; margin-bottom: 12px; }
.toolbar h1 { margin-right: auto; }
.count { font-size: 13px; font-weight: 400; color: var(--muted); margin-left: 6px; }
input[type=search] { width: min(320px, 100%); }
input, select, textarea {
  font: inherit; color: var(--text); background: var(--surface);
  border: 1px solid var(--border); border-radius: 6px; padding: 6px 8px; width: 100%;
}
.chk { display: inline-flex; align-items: center; gap: 6px; white-space: nowrap; }
.chk input { width: auto; }

/* tables */
.table-wrap { overflow-x: auto; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); }
table.data { border-collapse: collapse; width: 100%; }
.data th, .data td { padding: 7px 10px; text-align: left; border-bottom: 1px solid var(--border); vertical-align: top; }
.data th { position: sticky; top: 0; background: var(--surface); font-size: 12px; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); cursor: pointer; user-select: none; white-space: nowrap; }
.data tbody tr { cursor: pointer; }
.data tbody tr:hover { background: var(--row-hover); }
.data tr.low { background: var(--low-bg); box-shadow: inset 3px 0 0 var(--low-edge); }
.data .num { text-align: right; font-variant-numeric: tabular-nums; }
.data td.act { text-align: right; width: 1%; white-space: nowrap; }
.data .empty { text-align: center; color: var(--muted); padding: 24px; cursor: default; }
.badge { display: inline-block; font-size: 11px; font-weight: 600; padding: 0 5px; border-radius: 4px; background: var(--low-edge); color: #fff; }

/* dialog */
dialog {
  width: min(720px, calc(100vw - 32px)); border: 1px solid var(--border); border-radius: 12px;
  background: var(--surface); color: var(--text); padding: 20px;
}
dialog.wide { width: min(1100px, calc(100vw - 32px)); }
dialog::backdrop { background: rgba(10, 20, 30, .45); }
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 12px; }
.field { display: flex; flex-direction: column; gap: 4px; }
.field > span { font-size: 12px; color: var(--muted); }
.field.wide { grid-column: 1 / -1; }
.field.chk { flex-direction: row; align-self: end; padding-bottom: 6px; }
.actions { display: flex; gap: 8px; margin-top: 16px; }
.spacer { flex: 1; }
.history { list-style: none; padding: 0; margin: 0 0 12px; max-height: 60vh; overflow: auto; border: 1px solid var(--border); border-radius: 6px; }
.history li { display: grid; grid-template-columns: 11rem 10rem 1fr; gap: 12px; padding: 8px 10px; border-bottom: 1px solid var(--border); }
.history li:last-child { border-bottom: 0; }
.history li.empty { display: block; color: var(--muted); }
.history time { color: var(--muted); white-space: nowrap; }
.history b { overflow-wrap: anywhere; }
@media (max-width: 640px) {
  .history li { grid-template-columns: 1fr; gap: 2px; }
}

/* lists */
.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 16px; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; }
.card-head { display: flex; justify-content: space-between; align-items: center; }
.pills { list-style: none; padding: 0; margin: 0; display: flex; flex-wrap: wrap; gap: 6px; }

/* reports */
.seg { display: inline-flex; }
.seg button { border-radius: 0; margin-left: -1px; }
.seg button:first-child { border-radius: 6px 0 0 6px; }
.seg button:last-child { border-radius: 0 6px 6px 0; }
.seg button[aria-pressed="true"] { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
.report { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 24px; }
.report-head { border-bottom: 2px solid var(--text); margin-bottom: 16px; }
.report-head p { margin: 4px 0 12px; color: var(--muted); }
.report .data tbody tr { cursor: default; }
.supplier { margin-bottom: 28px; break-inside: avoid; }
.data tfoot td { font-weight: 600; border-bottom: 0; }
.grand { text-align: right; font-size: 16px; }

#toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%) translateY(20px);
  background: var(--text); color: var(--bg); padding: 8px 16px; border-radius: 6px;
  opacity: 0; transition: opacity .2s, transform .2s; pointer-events: none;
}
#toast.show { opacity: 1; transform: translateX(-50%); }

@media (max-width: 640px) {
  .topbar { gap: 8px; }
  .toolbar h1 { width: 100%; }
}

@media print {
  :root { --bg: #fff; --surface: #fff; --text: #000; --muted: #444; --border: #bbb; --low-bg: #fff; }
  .topbar, .no-print, #toast { display: none; }
  main { padding: 0; }
  .report { border: 0; padding: 0; }
  .data tr.low { box-shadow: none; font-weight: 600; }
}
