:root {
  --bg: #f4f5f9;
  --surface: #ffffff;
  --border: #e3e5ee;
  --text: #1a1d2b;
  --muted: #6b7085;
  --accent: #3d5af1;
  --accent-soft: #e9edff;
  --green: #0e9f6e;
  --green-soft: #e3f5ee;
  --red: #d64545;
  --red-soft: #fbe9e9;
  --amber: #b7791f;
  --amber-soft: #fdf3e1;
  --violet: #7c3aed;
  --violet-soft: #efe7fd;
  /* derived surfaces (so dark mode can flip them in one place) */
  --surface-2: #fafbfe;     /* subtle bg: table headers, footers */
  --hover: #f7f8fd;         /* row / item hover */
  --field: #fbfbfd;         /* input background */
  --field-focus: #ffffff;   /* input bg on hover/focus */
  --chip: #eef0f6;          /* grey badge / segmented control */
  --border-soft: #f0f1f7;   /* row separators */
  --border-strong: #c9cddd; /* button hover border */
  --line-new-bg: #f0fdf6;   /* newly-added line */
  --dirty-bg: #fff8e6;      /* edited cell */
  --dirty-border: #ecc94b;
  --radius: 10px;
  --shadow: 0 1px 3px rgba(20, 24, 50, .07), 0 4px 16px rgba(20, 24, 50, .05);
  color-scheme: light;
  font-size: 15px;
}

/* ---------------- dark mode (follows the OS / browser setting) ---------------- */
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1115;
    --surface: #181b22;
    --surface-2: #1f232c;
    --hover: #20242e;
    --field: #1f232c;
    --field-focus: #262b36;
    --border: #2c313c;
    --border-soft: #242832;
    --border-strong: #3a4150;
    --chip: #2a2f3a;
    --text: #e6e8f0;
    --muted: #9aa1b4;
    --accent: #7c8cff;
    --accent-soft: #232a4a;
    --green: #34d399;  --green-soft: #16291f;
    --red: #f87171;    --red-soft: #3a2222;
    --amber: #e0b24a;  --amber-soft: #332a16;
    --violet: #b794f6; --violet-soft: #2c2440;
    --line-new-bg: #15291f;
    --dirty-bg: #3a3320; --dirty-border: #8a7320;
    --shadow: 0 1px 3px rgba(0, 0, 0, .4), 0 6px 20px rgba(0, 0, 0, .35);
    color-scheme: dark;
  }
}
* { box-sizing: border-box; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font-family: -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}
button { font: inherit; cursor: pointer; }
input, select { font: inherit; color: inherit; }

/* ---------- top bar ---------- */
.topbar {
  display: flex; align-items: center; gap: 28px;
  background: var(--surface); border-bottom: 1px solid var(--border);
  padding: 0 22px; height: 56px; position: sticky; top: 0; z-index: 50;
}
.brand { display: flex; align-items: center; gap: 10px; cursor: pointer; font-size: 16px; }
.brand strong { font-weight: 700; }
.brand-mark {
  width: 28px; height: 28px; border-radius: 7px; background: var(--accent);
  color: #fff; font-weight: 800; display: flex; align-items: center; justify-content: center;
}
.topbar nav { display: flex; gap: 4px; }
.topbar nav a {
  text-decoration: none; color: var(--muted); padding: 7px 14px;
  border-radius: 8px; font-weight: 500;
}
.topbar nav a.active { color: var(--accent); background: var(--accent-soft); }
/* embedded as a Halo custom tab: drop the whole top bar (it's per-client, no
   nav needed) — and the sign-in screen — so there's no dead space up top. */
body.embedded .topbar,
body.signedout .topbar { display: none; }
body.embedded main { padding-top: 14px; }
.page-head:empty { display: none; }
.topbar-right { margin-left: auto; display: flex; gap: 8px; align-items: center; }
.whoami { display: inline-flex; align-items: center; gap: 7px; font-size: 13px; color: var(--muted); margin-right: 4px; }
.whoami:empty { display: none; }
.who-ini {
  width: 24px; height: 24px; border-radius: 50%; background: var(--accent-soft); color: var(--accent);
  font-size: 11px; font-weight: 700; display: inline-flex; align-items: center; justify-content: center;
}
.who-name { font-weight: 500; color: var(--text); }

/* ---------- buttons ---------- */
.btn {
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  border-radius: 8px; padding: 7px 14px; font-weight: 500;
}
.btn:hover { border-color: var(--border-strong); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { background: #3049d6; }
.btn.primary:disabled { background: #a4b0f5; border-color: #a4b0f5; cursor: default; }
.btn.danger { color: var(--red); }
.btn.danger:hover { background: var(--red-soft); border-color: var(--red); }
.btn.ghost { border-color: transparent; color: var(--muted); }
.btn.ghost:hover { background: var(--accent-soft); color: var(--accent); }
.btn.sm { padding: 4px 9px; font-size: 13px; border-radius: 6px; }
.btn.icon { padding: 4px 7px; font-size: 13px; line-height: 1; }

/* ---------- layout ---------- */
main { max-width: 1320px; margin: 0 auto; padding: 22px; }
.page-head { display: flex; align-items: center; gap: 14px; margin-bottom: 16px; flex-wrap: wrap; }
.page-head h1 { font-size: 21px; margin: 0; }
.page-head .sub { color: var(--muted); font-size: 13.5px; }
.spacer { flex: 1; }

.cards { display: flex; gap: 14px; margin-bottom: 18px; flex-wrap: wrap; }
.kpi {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 14px 18px; min-width: 170px; box-shadow: var(--shadow);
}
.kpi .label { color: var(--muted); font-size: 12.5px; text-transform: uppercase; letter-spacing: .04em; }
.kpi .value { font-size: 22px; font-weight: 700; margin-top: 3px; }
.kpi .hint { color: var(--muted); font-size: 12px; margin-top: 2px; }

/* ---------- filter bar ---------- */
.filters {
  display: flex; gap: 10px; align-items: center; flex-wrap: wrap;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 10px 12px; margin-bottom: 14px; box-shadow: var(--shadow);
}
.filters input[type=search], .filters input[type=text], .filters select, .filters input[type=number] {
  border: 1px solid var(--border); border-radius: 8px; padding: 7px 11px; background: var(--field);
  min-width: 130px;
}
.filters input[type=search]:focus, .filters select:focus { outline: 2px solid var(--accent-soft); border-color: var(--accent); }
.filters .grow { flex: 1; min-width: 220px; }
.filters label.chk { display: flex; align-items: center; gap: 6px; color: var(--muted); font-size: 13.5px; }

/* ---------- tables ---------- */
.tablecard {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow); overflow: auto;
}
table { width: 100%; border-collapse: collapse; font-size: 14px; }
thead th {
  text-align: left; color: var(--muted); font-size: 12px; text-transform: uppercase;
  letter-spacing: .05em; padding: 10px 12px; border-bottom: 1px solid var(--border);
  background: var(--surface-2); position: sticky; top: 0; cursor: pointer; user-select: none; white-space: nowrap;
}
thead th.sorted { color: var(--accent); }
tbody td { padding: 9px 12px; border-bottom: 1px solid var(--border-soft); vertical-align: middle; }
tbody tr:hover { background: var(--hover); }
tbody tr.clickable { cursor: pointer; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
.badge {
  display: inline-block; border-radius: 20px; font-size: 12px; font-weight: 600;
  padding: 2px 10px; white-space: nowrap;
}
.badge.green { background: var(--green-soft); color: var(--green); }
.badge.grey { background: var(--chip); color: var(--muted); }
.badge.red { background: var(--red-soft); color: var(--red); }
.badge.amber { background: var(--amber-soft); color: var(--amber); }
.badge.blue { background: var(--accent-soft); color: var(--accent); }
.badge.violet { background: var(--violet-soft); color: var(--violet); }
.mono { font-variant-numeric: tabular-nums; }
.muted { color: var(--muted); }
.small { font-size: 12.5px; }
td.pos { color: var(--green); }
td.neg { color: var(--red); }

/* segmented control (reconcile tabs) */
.seg { display: inline-flex; gap: 4px; background: var(--chip); border-radius: 10px; padding: 4px; margin-bottom: 14px; }
.seg-btn {
  border: none; background: transparent; cursor: pointer; padding: 7px 14px; border-radius: 7px;
  font-weight: 600; font-size: 13px; color: var(--muted);
}
.seg-btn:hover { color: var(--text); }
.seg-btn.active { background: var(--surface); color: var(--accent); box-shadow: var(--shadow); }
.badge.tiny { font-size: 10.5px; padding: 1px 6px; margin-left: 5px; vertical-align: middle; }

/* ---------- column profiles ---------- */
.colbar { display: flex; align-items: center; gap: 8px; }
.colbar .cb-lbl { color: var(--muted); font-size: 12.5px; text-transform: uppercase; letter-spacing: .04em; }
.colbar select { border: 1px solid var(--border); border-radius: 8px; padding: 6px 10px; background: var(--field); }
.cp-rows { display: flex; flex-direction: column; gap: 2px; margin-bottom: 12px; }
.cp-row {
  display: flex; align-items: center; gap: 8px; padding: 5px 8px; border-radius: 7px;
}
.cp-row:hover { background: var(--hover); }
.cp-row label.chk { display: flex; align-items: center; gap: 8px; flex: 1; cursor: pointer; }
.cp-actions { display: flex; gap: 8px; flex-wrap: wrap; margin: 6px 0 4px; }

/* ---------- per-line config: cog + selection ---------- */
.item-cell { white-space: normal; }
.line-actions { white-space: nowrap; }
.btn.cog { font-size: 14px; line-height: 1; padding: 4px 7px; }
.btn.cog.has-cfg { color: var(--accent); border-color: var(--accent-soft); background: var(--accent-soft); }
.btn.cog:hover { border-color: var(--accent); }
tr.line.editing > td { background: var(--accent-soft) !important; }
.d-selbar {
  display: flex; align-items: center; gap: 14px; margin: 2px 2px 12px;
  color: var(--muted); font-size: 13.5px;
}
.d-selbar label.chk { display: inline-flex; align-items: center; gap: 7px; cursor: pointer; }

/* ---------- right slide-over drawer ---------- */
#drawer-wrap { position: fixed; inset: 0; z-index: 120; }
#drawer-wrap.hidden { display: none; }
.drawer-backdrop { position: absolute; inset: 0; background: rgba(15, 18, 35, .35); animation: fadein .15s ease-out; }
.drawer {
  position: absolute; top: 0; right: 0; height: 100%; width: min(480px, 94vw);
  background: var(--surface); box-shadow: -12px 0 40px rgba(20, 24, 50, .25);
  display: flex; flex-direction: column; animation: slidein-r .2s ease-out;
}
@keyframes fadein { from { opacity: 0; } }
@keyframes slidein-r { from { transform: translateX(30px); opacity: .4; } }
.dw-head {
  display: flex; align-items: flex-start; gap: 12px; padding: 16px 18px;
  border-bottom: 1px solid var(--border);
}
.dw-head .dw-title { font-size: 16px; font-weight: 700; line-height: 1.3; }
.dw-head .dw-x { margin-left: auto; }
.dw-body { flex: 1; overflow: auto; padding: 4px 18px 18px; }
.dw-foot {
  display: flex; align-items: center; gap: 12px; padding: 12px 18px;
  border-top: 1px solid var(--border); background: var(--surface-2);
}
.dw-foot .btn { margin-left: auto; }
.dw-foot .muted { margin-right: auto; }

.dw-sec { padding: 16px 0; border-bottom: 1px solid var(--border-soft); }
.dw-sec:last-child { border-bottom: none; }
.dw-sec h3 {
  margin: 0 0 12px; font-size: 12.5px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted);
}
.dw-row { display: flex; align-items: center; gap: 10px; margin-bottom: 10px; flex-wrap: wrap; }
.dw-lbl { color: var(--muted); font-size: 13px; min-width: 120px; }
.dw-sec select, .dw-sec input[type=date], .dw-sec input[type=number] {
  border: 1px solid var(--border); border-radius: 8px; padding: 7px 10px; background: var(--field-focus); font: inherit;
}
.dw-qtytype { font-weight: 600; }
.dw-lock { margin: -2px 0 12px; color: var(--amber); }
.dw-note { color: var(--muted); line-height: 1.55; margin: 6px 0 0; }
.dw-kv { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 5px 0; font-size: 13.5px; }
.dw-kv > span { color: var(--muted); }
.dw-kv.col { flex-direction: column; align-items: flex-start; gap: 4px; }
.dw-kv.col input { width: 120px; }
.dw-check {
  display: flex; align-items: flex-start; gap: 10px; cursor: pointer;
  background: var(--surface-2); border: 1px solid var(--border); border-radius: 9px; padding: 11px 13px; margin-bottom: 12px;
}
.dw-check input { margin-top: 3px; }

.dw-groupbar {
  display: flex; gap: 8px; align-items: center; flex-wrap: wrap;
  background: var(--accent-soft); border-radius: 9px; padding: 8px 10px; margin-bottom: 12px;
}
.dw-groupbar select { flex: 1; min-width: 0; }
.dw-groupmatch { margin: -4px 0 12px; color: var(--violet); font-weight: 600; font-size: 12.5px; }
.dw-groupmatch.exact { color: var(--green); }
.qg-rows { display: flex; flex-direction: column; gap: 8px; }
.qg-row { display: flex; align-items: center; gap: 8px; border: 1px solid var(--border); border-radius: 9px; padding: 10px 12px; }
.qg-row .qg-info { flex: 1; min-width: 0; }
.bulk-sep { width: 1px; align-self: stretch; background: #3a3f5c; margin: 0 2px; }
.dw-lics { display: flex; flex-direction: column; gap: 9px; margin-bottom: 10px; }
.dw-lic { border: 1px solid var(--border); border-left: 3px solid var(--green); border-radius: 9px; padding: 10px 12px; }
.dw-lic-top { display: flex; align-items: center; gap: 8px; }
.dw-lic-name { font-weight: 600; flex: 1; }
.dw-lic-opts { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; margin-top: 9px; }
.dw-lic-opts label { display: inline-flex; align-items: center; gap: 5px; color: var(--muted); font-size: 12.5px; }
.dw-lic-opts input[type=number] { width: 52px; padding: 3px 6px; }
.dw-lic-opts select { padding: 3px 7px; }
.dw-add { margin-bottom: 8px; }
.dw-add select { width: 100%; }
.dw-prfields { gap: 18px; }
.dw-kv.col input[type=date] { width: 150px; }
.d-filters { margin-bottom: 10px; }
.d-filters #d-filtersum { margin-left: auto; }
.dw-prtable { margin-top: 8px; max-height: 260px; overflow: auto; border: 1px solid var(--border); border-radius: 9px; }
.dw-prtable table { font-size: 12px; }
.dw-prtable thead th { padding: 7px 9px; position: sticky; top: 0; }
.dw-prtable tbody td { padding: 6px 9px; }

/* ---------- detail page ---------- */
.detail-head {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 16px 20px; margin-bottom: 16px;
  display: flex; gap: 26px; align-items: center; flex-wrap: wrap;
}
.detail-head .title h1 { margin: 0; font-size: 19px; }
.detail-head .title .sub { color: var(--muted); font-size: 13px; margin-top: 3px; }
.stat { min-width: 110px; }
.stat .label { color: var(--muted); font-size: 11.5px; text-transform: uppercase; letter-spacing: .04em; }
.stat .value { font-weight: 700; font-size: 16.5px; margin-top: 2px; font-variant-numeric: tabular-nums; }
.stat .value.changed { color: var(--accent); }

.group {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); margin-bottom: 14px; overflow: hidden;
}
.group.drag-over { outline: 2px dashed var(--accent); outline-offset: -2px; }
.group-head {
  display: flex; align-items: center; gap: 10px; padding: 10px 14px;
  background: var(--hover); border-bottom: 1px solid var(--border);
}
.group-head .drag { cursor: grab; color: var(--muted); font-size: 15px; padding: 2px 4px; }
.group-head input.gname {
  border: 1px solid transparent; background: transparent; font-weight: 700; font-size: 15px;
  border-radius: 6px; padding: 4px 8px; flex: 0 1 420px;
}
.group-head input.gname:hover { border-color: var(--border); background: var(--field-focus); }
.group-head input.gname:focus { border-color: var(--accent); background: var(--field-focus); outline: none; }
.group-head .gsum { margin-left: auto; color: var(--muted); font-size: 13px; font-variant-numeric: tabular-nums; }
.group table { font-size: 13.8px; }
.group tbody tr.line.dragging { opacity: .4; }
.group tbody tr.line.drag-over td { border-top: 2px solid var(--accent); }
.group tbody tr.selected { background: var(--accent-soft); }
.group tbody td { vertical-align: top; }
.group .drag-cell { cursor: grab; color: var(--muted); width: 26px; text-align: center; padding-top: 11px; }
.group .drag-cell:active { cursor: grabbing; }
.desc-cell { min-width: 280px; }

.cell-edit {
  width: 90px; border: 1px solid transparent; background: transparent; border-radius: 6px;
  padding: 4px 7px; text-align: right; font-variant-numeric: tabular-nums;
}
.cell-edit.wide { width: 100%; min-width: 220px; text-align: left; }
.cell-edit:hover { border-color: var(--border); background: var(--field-focus); }
.cell-edit:focus { border-color: var(--accent); background: var(--field-focus); outline: none; }
.cell-edit.dirty { background: var(--dirty-bg); border-color: var(--dirty-border); }
/* description editor: wraps + auto-grows so the whole line is visible */
textarea.cell-edit.desc {
  display: block; resize: none; overflow: hidden; min-height: 30px;
  font: inherit; line-height: 1.4; white-space: pre-wrap; word-break: break-word;
}

tr.line-new td { background: var(--line-new-bg); }
tr.line-del td { background: var(--red-soft); text-decoration: line-through; opacity: .65; }
tr.line-del td .btn { text-decoration: none; }

.group-foot { padding: 8px 14px; border-top: 1px solid var(--border-soft); }

/* ---------- bulk bar / save bar ---------- */
.actionbar {
  position: sticky; bottom: 14px; z-index: 40; display: flex; gap: 10px; align-items: center;
  background: #20243a; color: #fff; border-radius: 12px; padding: 10px 16px;
  box-shadow: 0 8px 30px rgba(20,24,50,.35); margin-top: 18px; flex-wrap: wrap;
}
.actionbar .info { font-size: 13.5px; }
.actionbar input, .actionbar select {
  border: 1px solid #3a3f5c; background: #2b3050; color: #fff; border-radius: 7px; padding: 6px 9px;
}
.actionbar .btn { border-color: #3a3f5c; background: #2b3050; color: #fff; }
.actionbar .btn.primary { background: var(--accent); border-color: var(--accent); }
.actionbar .btn.ghost { background: transparent; border-color: transparent; color: #aab0d0; }

/* ---------- modal / toast ---------- */
#modal-wrap {
  position: fixed; inset: 0; background: rgba(15, 18, 35, .45);
  display: flex; align-items: flex-start; justify-content: center; padding-top: 9vh; z-index: 100;
}
#modal-wrap.hidden { display: none; }
.modal {
  background: var(--surface); border-radius: 14px; box-shadow: 0 20px 60px rgba(0,0,0,.3);
  width: min(640px, 92vw); max-height: 78vh; display: flex; flex-direction: column; overflow: hidden;
}
.modal header { padding: 14px 18px; border-bottom: 1px solid var(--border); display: flex; align-items: center; }
.modal header h2 { margin: 0; font-size: 16px; }
.modal header .btn { margin-left: auto; }
.modal .body { padding: 14px 18px; overflow: auto; }
.modal .body input[type=search] {
  width: 100%; border: 1px solid var(--border); border-radius: 8px; padding: 9px 12px; margin-bottom: 10px;
}
.item-row {
  display: flex; align-items: center; gap: 12px; padding: 9px 10px; border-radius: 8px; cursor: pointer;
}
.item-row:hover { background: var(--accent-soft); }
.item-row .price { margin-left: auto; font-variant-numeric: tabular-nums; font-weight: 600; }

#toast-wrap { position: fixed; right: 18px; bottom: 18px; z-index: 200; display: flex; flex-direction: column; gap: 8px; }
.toast {
  background: #20243a; color: #fff; border-radius: 10px; padding: 11px 16px; font-size: 14px;
  box-shadow: 0 8px 30px rgba(0,0,0,.3); animation: slidein .18s ease-out;
}
.toast.err { background: #8c2f2f; }
.toast.ok { background: #14694b; }
@keyframes slidein { from { transform: translateY(8px); opacity: 0; } }

.loading { text-align: center; color: var(--muted); padding: 60px 0; font-size: 15px; }
.spin { display: inline-block; animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* richer loading + error states */
.loading.big { display: flex; flex-direction: column; align-items: center; gap: 10px; padding: 72px 0; }
.loading.big .spin { font-size: 26px; color: var(--accent); }
.loading .load-title { font-size: 17px; font-weight: 600; color: var(--text); }
.load-track {
  width: min(360px, 70vw); height: 8px; background: var(--border);
  border-radius: 20px; overflow: hidden; margin-top: 4px;
}
.load-bar { height: 100%; width: 0; background: var(--accent); border-radius: 20px; transition: width .4s ease; }
.load-text { min-height: 18px; }
.err-mark {
  width: 44px; height: 44px; border-radius: 50%; background: var(--red-soft); color: var(--red);
  font-size: 26px; font-weight: 800; display: flex; align-items: center; justify-content: center;
}

/* help modal */
.help-body h3 { font-size: 14px; margin: 16px 0 6px; }
.help-body h3:first-child { margin-top: 0; }
.help-body p, .help-body li { line-height: 1.55; color: var(--text); }
.help-body ul { margin: 4px 0; padding-left: 20px; }
.help-body ul.keys { list-style: none; padding-left: 0; }
.help-body kbd {
  display: inline-block; min-width: 18px; text-align: center; padding: 1px 7px; margin-right: 6px;
  border: 1px solid var(--border); border-bottom-width: 2px; border-radius: 6px;
  background: var(--surface-2); font: 12px ui-monospace, monospace;
}
