:root {
  color-scheme: light;
  --bg: #f6f6f4;
  --surface: #ffffff;
  --surface-2: #f9f9f7;
  --border: #e3e2dc;
  --border-strong: #cfcdc5;
  --text: #17171a;
  --text-2: #55545a;
  --muted: #8a8890;
  --accent: #2a64c9;
  --accent-hover: #1f53ad;
  --accent-soft: #e8effb;
  --danger: #c43c3c;
  --danger-soft: #fbeaea;
  --grid: #ecebe6;
  --series-1: #2a78d6;
  --series-2: #eb6834;
  --c1: #2a78d6; --c2: #eb6834; --c3: #1baf7a; --c4: #eda100; --c5: #e87ba4; --c6: #4a3aa7; --c-other: #b5b3ab;
  --st-progress-bg: #eef0f4; --st-progress: #4a5568;
  --st-pending-bg: #fff4dc; --st-pending: #8a5a00;
  --st-sent-bg: #e6effc; --st-sent: #1f56b3;
  --st-paid-bg: #e3f5e6; --st-paid: #1d6b2c;
  --st-nb-bg: #f0efec; --st-nb: #6b6a66;
  --st-overdue-bg: #fbe7e4; --st-overdue: #a83226;
  --shadow: 0 1px 2px rgba(0,0,0,.04), 0 4px 16px rgba(0,0,0,.06);
  --radius: 10px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #111112; --surface: #1a1a1b; --surface-2: #202022; --border: #2e2e31; --border-strong: #3d3d41;
    --text: #f2f2f0; --text-2: #c3c2bc; --muted: #8f8e93; --accent: #4d8ff0; --accent-hover: #6aa2f5; --accent-soft: #1d2a40;
    --danger: #ec6b6b; --danger-soft: #3a1f1f; --grid: #2a2a2c; --series-1: #3987e5; --series-2: #d95926; --c1: #3987e5; --c2: #d95926; --c3: #199e70; --c4: #c98500; --c5: #d55181; --c6: #9085e9; --c-other: #5c5b57;
    --st-progress-bg: #2a2d33; --st-progress: #c2c8d2; --st-pending-bg: #3a2f14; --st-pending: #f2c45c;
    --st-sent-bg: #1c2b45; --st-sent: #8bb6f7; --st-paid-bg: #17321e; --st-paid: #7fd48f;
    --st-nb-bg: #2a2a2a; --st-nb: #a9a8a3; --st-overdue-bg: #3d1f1b; --st-overdue: #f39a8c;
    --shadow: 0 1px 2px rgba(0,0,0,.3), 0 4px 16px rgba(0,0,0,.3);
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #111112; --surface: #1a1a1b; --surface-2: #202022; --border: #2e2e31; --border-strong: #3d3d41;
  --text: #f2f2f0; --text-2: #c3c2bc; --muted: #8f8e93; --accent: #4d8ff0; --accent-hover: #6aa2f5; --accent-soft: #1d2a40;
  --danger: #ec6b6b; --danger-soft: #3a1f1f; --grid: #2a2a2c; --series-1: #3987e5; --series-2: #d95926; --c1: #3987e5; --c2: #d95926; --c3: #199e70; --c4: #c98500; --c5: #d55181; --c6: #9085e9; --c-other: #5c5b57;
  --st-progress-bg: #2a2d33; --st-progress: #c2c8d2; --st-pending-bg: #3a2f14; --st-pending: #f2c45c;
  --st-sent-bg: #1c2b45; --st-sent: #8bb6f7; --st-paid-bg: #17321e; --st-paid: #7fd48f;
  --st-nb-bg: #2a2a2a; --st-nb: #a9a8a3; --st-overdue-bg: #3d1f1b; --st-overdue: #f39a8c;
  --shadow: 0 1px 2px rgba(0,0,0,.3), 0 4px 16px rgba(0,0,0,.3);
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { background: var(--bg); color: var(--text); font: 14px/1.45 var(--font); }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 22px; margin: 0; font-weight: 650; letter-spacing: -.01em; }
h2 { font-size: 15px; margin: 0 0 12px; font-weight: 600; }
.num, td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.nowrap { white-space: nowrap; }

/* layout */
.app { display: grid; grid-template-columns: 220px 1fr; min-height: 100vh; }
.sidebar { background: var(--surface); border-right: 1px solid var(--border); padding: 18px 12px; position: sticky; top: 0; height: 100vh; display: flex; flex-direction: column; gap: 4px; }
.brand { display: flex; align-items: center; gap: 10px; padding: 4px 10px 18px; font-weight: 700; font-size: 15px; }
.brand-mark { width: 28px; height: 28px; border-radius: 8px; background: var(--accent); color: #fff; display: grid; place-items: center; font-size: 13px; }
.nav a { display: flex; align-items: center; gap: 10px; padding: 8px 10px; border-radius: 8px; color: var(--text-2); font-weight: 500; }
.nav a:hover { background: var(--surface-2); text-decoration: none; color: var(--text); }
.nav a.active { background: var(--accent-soft); color: var(--accent); }
.nav svg { width: 17px; height: 17px; flex: none; }
.nav-count { margin-left: auto; font-size: 11px; background: var(--st-pending-bg); color: var(--st-pending); border-radius: 10px; padding: 0 7px; font-weight: 600; }
.sidebar-foot { margin-top: auto; padding: 8px 10px; }
.main { padding: 24px 28px 60px; min-width: 0; }
.page-head { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; margin-bottom: 18px; }
.page-head .spacer { flex: 1; }
.crumb { color: var(--muted); font-size: 13px; margin-bottom: 4px; }

/* cards */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 18px; }
.card + .card { margin-top: 16px; }
.grid { display: grid; gap: 16px; }
.kpis { grid-template-columns: repeat(4, minmax(0, 1fr)); margin-bottom: 16px; }
.kpi .label { color: var(--text-2); font-size: 13px; font-weight: 500; }
.kpi .value { font-size: 26px; font-weight: 650; margin: 6px 0 2px; letter-spacing: -.02em; }
.kpi .sub { color: var(--muted); font-size: 12px; }
.two { grid-template-columns: 2fr 1fr; }
.half { grid-template-columns: 1fr 1fr; }

/* buttons & inputs */
.btn { display: inline-flex; align-items: center; gap: 6px; border: 1px solid var(--border-strong); background: var(--surface); color: var(--text); padding: 7px 12px; border-radius: 8px; font: inherit; font-weight: 500; cursor: pointer; white-space: nowrap; }
.btn:hover { background: var(--surface-2); text-decoration: none; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { background: var(--accent-hover); }
.btn.danger { color: var(--danger); border-color: var(--border-strong); }
.btn.danger:hover { background: var(--danger-soft); }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.ghost:hover { background: var(--surface-2); }
.btn.sm { padding: 4px 9px; font-size: 13px; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn svg { width: 15px; height: 15px; }
input, select, textarea { font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--border-strong); border-radius: 8px; padding: 7px 10px; min-width: 0; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: var(--accent); }
textarea { resize: vertical; min-height: 64px; }
input[type="checkbox"] { width: 16px; height: 16px; accent-color: var(--accent); }
label.field { display: flex; flex-direction: column; gap: 5px; font-size: 13px; color: var(--text-2); font-weight: 500; }
label.field .hint { font-weight: 400; color: var(--muted); font-size: 12px; }
.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 14px; }
.form-grid .full { grid-column: 1 / -1; }
.form-grid.three { grid-template-columns: repeat(3, minmax(0, 1fr)); }

/* toolbar & filters */
.toolbar { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; margin-bottom: 12px; }
.toolbar input[type="search"] { width: 260px; }
.tabs { display: flex; gap: 4px; flex-wrap: wrap; margin-bottom: 12px; }
.tab { border: 1px solid var(--border); background: var(--surface); padding: 5px 11px; border-radius: 20px; cursor: pointer; color: var(--text-2); font: inherit; font-size: 13px; }
.tab .c { color: var(--muted); margin-left: 4px; font-variant-numeric: tabular-nums; }
.tab.active { background: var(--text); color: var(--surface); border-color: var(--text); }
.tab.active .c { color: inherit; opacity: .75; }
.bulkbar { display: flex; gap: 8px; align-items: center; background: var(--accent-soft); border: 1px solid var(--border); padding: 8px 12px; border-radius: 8px; margin-bottom: 12px; flex-wrap: wrap; }

/* tables */
.table-wrap { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); overflow: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { padding: 9px 10px; text-align: left; border-bottom: 1px solid var(--border); vertical-align: middle; }
th { font-size: 12px; font-weight: 600; color: var(--text-2); background: var(--surface-2); position: sticky; top: 0; z-index: 1; white-space: nowrap; }
th.sortable { cursor: pointer; user-select: none; }
th.sortable:hover { color: var(--text); }
tbody tr:hover { background: var(--surface-2); }
tbody tr.clickable { cursor: pointer; }
tr.selected { background: var(--accent-soft) !important; }
td.desc { max-width: 320px; }
td.desc .t { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
td.desc .s { color: var(--muted); font-size: 12px; }
tfoot td { font-weight: 600; background: var(--surface-2); border-bottom: none; }
.empty { padding: 40px; text-align: center; color: var(--muted); }
.more { padding: 12px; text-align: center; border-top: 1px solid var(--border); }
table.compact th, table.compact td { padding: 7px 10px; }

/* badges */
.badge { display: inline-flex; align-items: center; gap: 5px; padding: 2px 9px; border-radius: 20px; font-size: 12px; font-weight: 600; white-space: nowrap; }
.badge::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: currentColor; }
.b-progress { background: var(--st-progress-bg); color: var(--st-progress); }
.b-pending { background: var(--st-pending-bg); color: var(--st-pending); }
.b-sent { background: var(--st-sent-bg); color: var(--st-sent); }
.b-paid { background: var(--st-paid-bg); color: var(--st-paid); }
.b-nb { background: var(--st-nb-bg); color: var(--st-nb); }
.b-overdue { background: var(--st-overdue-bg); color: var(--st-overdue); }

/* modal */
.modal-back { position: fixed; inset: 0; background: rgba(10,10,12,.45); display: flex; align-items: flex-start; justify-content: center; padding: 6vh 16px; z-index: 50; overflow: auto; }
.modal { background: var(--surface); border-radius: 12px; box-shadow: var(--shadow); width: 100%; max-width: 640px; border: 1px solid var(--border); }
.modal.wide { max-width: 940px; }
.modal-head { display: flex; align-items: center; padding: 16px 20px; border-bottom: 1px solid var(--border); }
.modal-head h2 { margin: 0; flex: 1; font-size: 16px; }
.modal-body { padding: 20px; }
.modal-foot { display: flex; gap: 8px; padding: 14px 20px; border-top: 1px solid var(--border); background: var(--surface-2); border-radius: 0 0 12px 12px; }
.modal-foot .spacer { flex: 1; }

.spinner { width: 28px; height: 28px; margin: 0 auto; border: 3px solid var(--border); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* toast */
.toasts { position: fixed; right: 20px; bottom: 20px; display: flex; flex-direction: column; gap: 8px; z-index: 60; }
.toast { background: var(--text); color: var(--surface); padding: 10px 14px; border-radius: 8px; box-shadow: var(--shadow); max-width: 380px; animation: pop .15s ease-out; }
.toast.error { background: var(--danger); color: #fff; }
@keyframes pop { from { transform: translateY(6px); opacity: 0; } }

/* chart */
.legend { display: flex; gap: 16px; font-size: 12px; color: var(--text-2); margin-bottom: 8px; }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 6px; vertical-align: -1px; }
.chart { position: relative; }
.chart svg { display: block; width: 100%; height: auto; overflow: visible; }
.chart .tick { fill: var(--muted); font-size: 11px; font-variant-numeric: tabular-nums; }
.chart .gridline { stroke: var(--grid); stroke-width: 1; }
.chart .baseline { stroke: var(--border-strong); stroke-width: 1; }
.chart .hit { fill: transparent; cursor: default; }
.chart .hit:hover { fill: var(--text); fill-opacity: .04; }
.tooltip { position: absolute; pointer-events: none; background: var(--surface); border: 1px solid var(--border); box-shadow: var(--shadow); border-radius: 8px; padding: 8px 10px; font-size: 12px; min-width: 150px; z-index: 5; display: none; }
.tooltip .tt-title { font-weight: 600; margin-bottom: 4px; }
.tooltip .row { display: flex; gap: 10px; justify-content: space-between; }
.tooltip .row i { display: inline-block; width: 8px; height: 8px; border-radius: 2px; margin-right: 6px; }
.hbar { display: grid; grid-template-columns: minmax(0, 1fr) 90px; gap: 4px 10px; align-items: center; font-size: 13px; }
.hbar .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.hbar .track { grid-column: 1 / -1; height: 8px; background: var(--grid); border-radius: 4px; margin-bottom: 8px; }
.hbar .fill { height: 100%; background: var(--series-1); border-radius: 4px; }

/* invoice builder */
.lines-table { table-layout: fixed; }
.lines-table th:last-child { width: 44px; }
.lines-table input { width: 100%; padding: 6px 8px; }
.lines-table td { padding: 6px 6px; vertical-align: top; }
.lines-table .w-qty { width: 80px; }
.lines-table .w-rate { width: 110px; }
.lines-table .w-amt { width: 120px; }
.totals { margin-left: auto; width: 300px; margin-top: 12px; }
.totals .row { display: flex; justify-content: space-between; padding: 4px 0; }
.totals .row.grand { font-size: 17px; font-weight: 700; border-top: 1px solid var(--border); padding-top: 8px; margin-top: 4px; }
.avail { max-height: 420px; overflow: auto; }
.avail .item { display: flex; gap: 10px; padding: 8px 0; border-bottom: 1px solid var(--border); align-items: flex-start; }
.avail .item .grow { flex: 1; min-width: 0; }
.invoice-frame { width: 100%; border: 1px solid var(--border); border-radius: var(--radius); background: #fff; height: 1100px; }
.timeline { display: flex; gap: 0; margin: 4px 0 16px; }
.timeline .step { flex: 1; padding: 10px 12px; border-top: 3px solid var(--border); color: var(--muted); font-size: 13px; }
.timeline .step.done { border-color: var(--accent); color: var(--text); }
.timeline .step b { display: block; font-size: 13px; }

/* import */
.dropzone { border: 2px dashed var(--border-strong); border-radius: var(--radius); padding: 30px; text-align: center; background: var(--surface-2); }
.dropzone.drag { border-color: var(--accent); background: var(--accent-soft); }
.choice { display: flex; gap: 12px; flex-wrap: wrap; }
.choice label { flex: 1; min-width: 220px; border: 1px solid var(--border-strong); border-radius: var(--radius); padding: 14px; cursor: pointer; display: flex; gap: 10px; align-items: flex-start; background: var(--surface); }
.choice label:has(input:checked) { border-color: var(--accent); background: var(--accent-soft); }
.choice b { display: block; margin-bottom: 2px; }
.map-table select { width: 100%; }
.notice { padding: 10px 14px; border-radius: 8px; background: var(--accent-soft); color: var(--text); font-size: 13px; margin-bottom: 12px; }
.notice.warn { background: var(--st-pending-bg); }

@media (max-width: 1100px) {
  .kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .two, .half { grid-template-columns: 1fr; }
}
@media (max-width: 760px) {
  .app { grid-template-columns: 1fr; }
  .sidebar { position: static; height: auto; flex-direction: row; flex-wrap: wrap; padding: 10px; }
  .brand { padding: 4px 8px; width: 100%; }
  .nav { display: flex; flex-wrap: wrap; gap: 2px; }
  .sidebar-foot { margin: 0; padding: 4px 0; width: 100%; }
  .sidebar-foot [data-act="toggle-theme"] { display: none; }
  .main { padding: 16px; }
  .form-grid, .form-grid.three { grid-template-columns: 1fr; }
  .toolbar input[type="search"] { width: 100%; }
}

[hidden] { display: none !important; }

/* Signed-in person and view-only access */
.sidebar-foot .me { margin-bottom: 6px; padding: 0 4px; }
body.role-viewer #main .btn.primary, body.role-viewer #main .btn.danger,
body.role-viewer #modal-root .btn.primary, body.role-viewer #modal-root .btn.danger,
body.role-viewer [data-po-drop] { display: none !important; }
body.role-viewer [data-act="new-company"],
body.role-viewer [data-act="new-po"],
body.role-viewer [data-act="new-project"],
body.role-viewer [data-act="delete-company"],
body.role-viewer [data-act="delete-project"],
body.role-viewer [data-act="inv-delete"],
body.role-viewer [data-act="inv-paid"],
body.role-viewer [data-act="inv-revert"],
body.role-viewer [data-act="inv-sent"],
body.role-viewer [data-act="invoice-company"],
body.role-viewer [data-act="invoice-one"],
body.role-viewer [data-act="p-delete"],
body.role-viewer [data-act="p-invoice"],
body.role-viewer [data-act="p-status"],
body.role-viewer [data-act="p-sel"],
body.role-viewer [data-act="p-selall"],
body.role-viewer [data-act="p-clearsel"],
body.role-viewer [data-act="po-auto"],
body.role-viewer [data-act="po-delete"],
body.role-viewer [data-act="po-invoice"],
body.role-viewer [data-act="po-link"],
body.role-viewer [data-act="po-unlink"],
body.role-viewer [data-act="po-projects"],
body.role-viewer [data-act="po-line-project"],
body.role-viewer [data-act="b-add"],
body.role-viewer [data-act="b-addall"],
body.role-viewer [data-act="b-addline"],
body.role-viewer [data-act="b-remove"],
body.role-viewer [data-act="b-save"],
body.role-viewer [data-act="i-status"],
body.role-viewer [data-act="calc-cost"],
body.role-viewer a[href="#/invoices/new"] { display: none !important; }
body.role-viewer a[href$="/edit"], body.role-viewer a[href^="#/import"] { display: none !important; }
