* { box-sizing: border-box; }
:root {
    --bg: #cdd4dc;
    --surface: #ffffff;
    --border: #d8e2ec;
    --border-strong: #b6c6d8;
    --ink: #1e3a5f;
    --ink-soft: #2d4a6e;
    --muted: #6b7d92;

    /* Pastel header */
    --header-bg: #b3d4ec;
    --header-ink: #1e3a5f;
    --header-ink-soft: #2d4a6e;

    /* Pastel button family */
    --accent: #8b7cb5;         /* lavender — primary */
    --accent-hover: #6f5fa3;
    --accent-soft: #ece6f5;

    --peach: #f0c878;          /* warm secondary */
    --peach-hover: #e0b260;
    --mint: #88c8a4;           /* success */
    --mint-hover: #6cb38a;
    --coral: #e89b9b;          /* danger */
    --coral-hover: #d77878;
    --sky: #88c8d4;            /* info chip */

    --success: #2f7c4e;
    --danger: #a04848;

    --shadow-sm: 0 1px 2px rgba(30, 58, 95, 0.06);
    --shadow-md: 0 4px 14px rgba(30, 58, 95, 0.10);
    --radius: 10px;
    --radius-sm: 6px;
}
body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Inter", sans-serif;
    margin: 0;
    background: var(--bg);
    color: var(--ink);
    line-height: 1.5;
    font-size: 14px;
}
.container { width: 90%; margin: 0 auto; padding: 0 24px; }
header {
    background: var(--header-bg);
    color: var(--header-ink);
    padding: 14px 0;
    box-shadow: var(--shadow-sm);
    border-bottom: 1px solid #94bbd8;
}
header .container { display: flex; align-items: center; gap: 16px; }
header a.brand { color: var(--header-ink); text-decoration: none; font-weight: 700; font-size: 18px; letter-spacing: 0.2px; }
header nav { flex: 1; }
header nav a {
    color: var(--header-ink-soft); text-decoration: none; margin-left: 18px;
    font-size: 14px; font-weight: 500; transition: color 0.15s;
}
header nav a:hover { color: var(--header-ink); }
header .auth-chrome { display: flex; align-items: center; gap: 10px; color: var(--header-ink); }
header .auth-chrome .muted { color: var(--header-ink-soft); }

main { padding: 24px 0 48px; }
footer { padding: 24px 0; color: var(--muted); font-size: 13px; }

h1 { margin: 0 0 4px; font-size: 26px; letter-spacing: -0.2px; font-weight: 700; color: var(--ink); }
h2 { margin: 0 0 12px; font-size: 18px; font-weight: 600; color: var(--ink-soft); }
h3 { margin: 0 0 10px; font-size: 14px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: 0.6px; }

.page-header {
    display: flex; align-items: flex-end; justify-content: space-between;
    gap: 16px; flex-wrap: wrap; margin-bottom: 18px;
}
.page-meta { display: flex; gap: 8px; flex-wrap: wrap; }

.pill {
    display: inline-flex; align-items: center;
    background: var(--surface); color: var(--ink-soft);
    border: 1px solid var(--border);
    padding: 5px 12px; border-radius: 999px;
    font-size: 12px; font-weight: 500;
    box-shadow: var(--shadow-sm);
}
.pill-strong { background: var(--accent-soft); color: var(--accent); border-color: #d6cbe8; }

.card {
    background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
    padding: 20px; margin-bottom: 16px;
    box-shadow: var(--shadow-sm);
}
.filter-card > summary {
    cursor: pointer; font-weight: 500; color: var(--ink-soft);
    list-style: none; padding: 2px 0;
}
.filter-card > summary::marker { display: none; }
.filter-card > summary::-webkit-details-marker { display: none; }
.filter-card > summary::before { content: "▸ "; color: var(--muted); }
.filter-card[open] > summary::before { content: "▾ "; }

.muted { color: var(--muted); }
code {
    font-family: ui-monospace, SFMono-Regular, "SF Mono", Consolas, monospace;
    font-size: 0.9em;
    background: #f3f4f6;
    padding: 1px 6px;
    border-radius: 4px;
    color: var(--ink-soft);
}

.btn {
    display: inline-block;
    background: var(--accent); color: white; border: 0; border-radius: var(--radius-sm);
    padding: 8px 16px; font-size: 14px; font-weight: 600; cursor: pointer;
    text-decoration: none; letter-spacing: 0.2px;
    transition: background 0.15s, transform 0.05s, box-shadow 0.15s;
    box-shadow: 0 1px 2px rgba(30, 58, 95, 0.10);
}
.btn:hover { background: var(--accent-hover); box-shadow: 0 2px 6px rgba(30, 58, 95, 0.15); }
.btn:active { transform: translateY(1px); }
.btn.secondary { background: var(--peach); color: #5a3f10; }
.btn.secondary:hover { background: var(--peach-hover); }
.btn.success { background: var(--mint); color: #1f4d33; }
.btn.success:hover { background: var(--mint-hover); }
.btn.danger { background: var(--coral); color: #5a1f1f; }
.btn.danger:hover { background: var(--coral-hover); }
.btn[disabled] { background: #c7cdd4; color: #6b7d92; cursor: not-allowed; box-shadow: none; }

.status-ok { color: var(--success); font-weight: 600; }
.status-err { color: var(--danger); font-weight: 600; }

.alert { padding: 12px 16px; border-radius: var(--radius-sm); margin-bottom: 14px; font-size: 14px; }
.alert.success { background: #e3f3ea; color: #1f4d33; border: 1px solid #b9dec8; }
.alert.error { background: #fbe7e7; color: #6b2424; border: 1px solid #f0c5c5; }
.alert.info { background: #e6f1f8; color: #1e3a5f; border: 1px solid #bcd5e8; }

table.grid { width: 100%; border-collapse: collapse; background: var(--surface); }
table.grid th, table.grid td {
    border-bottom: 1px solid var(--border); padding: 10px 12px; text-align: left;
    font-size: 14px;
}
table.grid th {
    background: #eef3f8; font-weight: 600; color: var(--ink-soft);
    font-size: 12px; text-transform: uppercase; letter-spacing: 0.5px;
}
table.grid tr:last-child td { border-bottom: 0; }
table.grid tr:hover td { background: #f7fafd; }
table.grid tr.row-excluded td { color: #6b2424; background: #fbe7e7; }
table.grid tr.row-excluded:hover td { background: #f7d8d8; }

dl.kv { display: grid; grid-template-columns: 180px 1fr; gap: 6px 16px; margin: 0; }
dl.kv dt { color: var(--muted); }
dl.kv dd { margin: 0; }
form.inline { display: inline; }

/* -----------------------------------------------------------------------
   DB Code accordion
   ----------------------------------------------------------------------- */
.db-accordion {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    margin-bottom: 12px;
    overflow: hidden;
    box-shadow: var(--shadow-sm);
    transition: box-shadow 0.15s, border-color 0.15s;
}
.db-accordion:hover { border-color: var(--border-strong); }
.db-accordion[open] {
    box-shadow: var(--shadow-md);
    border-color: var(--border-strong);
}
.db-accordion.is-excluded {
    background: #fbe7e7;
    border-color: #f0c5c5;
}
.db-accordion.is-excluded:hover { border-color: #e89b9b; }
.db-accordion.is-excluded .db-code { background: var(--coral); color: #5a1f1f; }
.db-accordion.is-excluded .db-name { color: #6b2424; }
.db-accordion.is-excluded .db-body { background: #fdf0f0; }

.db-accordion.is-billing-excluded {
    background: #fbe7e7;
    border-color: #f0c5c5;
}
.db-accordion.is-billing-excluded:hover { border-color: #e89b9b; }
.db-accordion.is-billing-excluded .db-code { background: var(--coral); color: #5a1f1f; }
.db-accordion.is-billing-excluded .db-name { color: #6b2424; }
.db-accordion.is-billing-excluded .db-body { background: #fdf0f0; }

/* Needs-attention state: total bill = 0, OR primary line rate = 0,
   OR any non-primary line qty = 0. Same pale-red palette as the excluded
   states so the user sees "this needs work" in one glance. */
.db-accordion.is-zero-total {
    background: #fbe7e7;
    border-color: #f0c5c5;
}
.db-accordion.is-zero-total:hover { border-color: #e89b9b; }
.db-accordion.is-zero-total .db-name { color: #6b2424; }
.db-accordion.is-zero-total .db-body { background: #fdf0f0; }

.flat-lines tr.line-zero-qty td {
    background: #fbe7e7;
    color: #6b2424;
}
.flat-lines tr.line-zero-qty:hover td { background: #f7d8d8; }

.db-badge.billing-off {
    display: inline-block;
    padding: 2px 8px;
    background: #c95757;
    color: #fff;
    border-radius: 10px;
    font-size: 0.72rem;
    font-weight: 700;
    letter-spacing: 0.4px;
    text-transform: uppercase;
    box-shadow: 0 1px 2px rgba(120, 30, 30, 0.2);
}

.db-toggle-form {
    margin: 0;
    flex-shrink: 0;
}
.db-toggle-btn {
    background: transparent;
    border: 1px solid #c1a0a0;
    color: #6b2424;
    padding: 4px 10px;
    border-radius: var(--radius-sm);
    font-size: 0.78rem;
    font-weight: 600;
    cursor: pointer;
    transition: background 0.12s, border-color 0.12s;
}
.db-toggle-btn:hover { background: #f7d8d8; border-color: #c95757; }

.db-process-form { margin: 0; flex-shrink: 0; }
.db-process-btn {
    background: var(--mint); color: #1f4d33;
    border: 0;
    padding: 6px 14px;
    border-radius: var(--radius-sm);
    font-size: 0.85rem;
    font-weight: 700;
    cursor: pointer;
    box-shadow: 0 1px 2px rgba(31, 77, 51, 0.15);
    transition: background 0.12s, opacity 0.12s;
}
.db-process-btn:hover:not([disabled]) { background: #6fb38a; }
.db-process-btn[disabled] {
    background: #c7cdd4; color: #6b7d92;
    cursor: not-allowed; box-shadow: none;
    opacity: 0.7;
}
/* Set by JS when the Process form is submitted: shows a small spinning ring
   next to "Processing…" while the server pushes the invoice to QBO. */
.db-process-btn.is-loading {
    background: #6fb38a;
    color: #fff;
    cursor: wait;
    padding-left: 30px;
    position: relative;
}
.db-process-btn.is-loading::before {
    content: '';
    position: absolute;
    left: 10px;
    top: 50%;
    width: 12px; height: 12px;
    margin-top: -6px;
    border: 2px solid rgba(255, 255, 255, 0.45);
    border-top-color: #ffffff;
    border-radius: 50%;
    animation: db-spin 0.6s linear infinite;
}
@keyframes db-spin { to { transform: rotate(360deg); } }

.db-processed {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 3px 10px;
    background: #e3f3ea;
    color: #1f4d33;
    border: 1px solid #b9dec8;
    border-radius: var(--radius-sm);
    font-size: 0.75rem;
    flex-shrink: 0;
}
.db-processed-icon { font-weight: 700; }
.db-processed-meta { display: inline-flex; gap: 6px; align-items: center; }
.db-processed-date { font-variant-numeric: tabular-nums; }
.db-processed-doc {
    font-family: ui-monospace, SFMono-Regular, Consolas, monospace;
    font-weight: 700;
    padding-left: 6px;
    border-left: 1px solid #b9dec8;
}

/* Per-accordion PDF / Email buttons. Rendered only when LatestRealInvoice exists. */
.db-inv-form { margin: 0; flex-shrink: 0; }
.db-inv-btn {
    background: transparent;
    border: 1px solid #b3c4d4;
    color: #345070;
    padding: 4px 10px;
    border-radius: var(--radius-sm);
    font-size: 0.78rem;
    font-weight: 600;
    cursor: pointer;
    transition: background 0.12s, border-color 0.12s;
}
.db-inv-btn:hover { background: #e7f0f8; border-color: #5d7fa5; }

/* "Emailed at" chip — visible when InvoiceHistory.EmailedAtUtc IS NOT NULL. */
.db-emailed {
    display: inline-flex;
    align-items: center;
    gap: 5px;
    padding: 3px 8px;
    background: #e3f3ea;
    color: #1f4d33;
    border: 1px solid #b9dec8;
    border-radius: var(--radius-sm);
    font-size: 0.72rem;
    flex-shrink: 0;
}
.db-emailed-icon { font-weight: 700; }
.db-emailed-date { font-variant-numeric: tabular-nums; }
.db-accordion:not(.is-billing-excluded) .db-toggle-btn {
    border-color: #b3c4d4;
    color: #345070;
}
.db-accordion:not(.is-billing-excluded) .db-toggle-btn:hover {
    background: #e7f0f8;
    border-color: #5d7fa5;
}

.db-summary {
    list-style: none;
    cursor: pointer;
    padding: 14px 18px;
    display: flex;
    align-items: center;
    gap: 16px;
    user-select: none;
}
.db-summary::-webkit-details-marker { display: none; }
.db-summary::marker { display: none; }

.db-caret {
    display: inline-block;
    width: 10px; height: 10px;
    border-right: 2px solid var(--muted);
    border-bottom: 2px solid var(--muted);
    transform: rotate(-45deg);
    transition: transform 0.18s ease;
    flex-shrink: 0;
}
.db-accordion[open] .db-caret { transform: rotate(45deg); }

.db-code {
    font-family: ui-monospace, SFMono-Regular, Consolas, monospace;
    font-weight: 700; font-size: 15px;
    background: #5d7fa5; color: white;
    padding: 4px 10px; border-radius: var(--radius-sm);
    letter-spacing: 0.4px;
    flex-shrink: 0;
    box-shadow: 0 1px 2px rgba(30, 58, 95, 0.15);
}
.db-qbid {
    font-family: ui-monospace, SFMono-Regular, Consolas, monospace;
    font-weight: 700; font-size: 13px;
    background: var(--mint); color: #1f4d33;
    padding: 3px 9px; border-radius: var(--radius-sm);
    letter-spacing: 0.3px;
    flex-shrink: 0;
    box-shadow: 0 1px 2px rgba(31, 77, 51, 0.15);
}
.db-name { font-weight: 600; font-size: 15px; color: var(--ink); }
.db-spacer { flex: 1; }

.db-stat {
    display: flex; flex-direction: column; align-items: flex-end;
    min-width: 76px;
    border-left: 1px solid var(--border);
    padding-left: 16px;
}
.db-stat-num {
    font-weight: 700; font-size: 16px; color: var(--ink);
    font-variant-numeric: tabular-nums;
    line-height: 1.1;
}
.db-stat-label {
    font-size: 11px; color: var(--muted);
    text-transform: uppercase; letter-spacing: 0.5px;
    margin-top: 2px;
}
.db-stat-total { min-width: 100px; }
.db-stat-total .db-stat-num { font-size: 18px; color: var(--accent); }
/* Variance vs the rolling 5-month average. Driven by IsWithinTolerance:
   null  -> no class      (neutral lavender, default)
   true  -> .in-range     (within ±15%, green)
   false -> .out-range    (outside ±15%, red — investigate) */
.db-stat-total.in-range  .db-stat-num { color: #1f7a4d; }
.db-stat-total.out-range .db-stat-num { color: #c95757; }
.db-stat-avg { min-width: 90px; }
.db-stat-avg .db-stat-num { color: var(--muted); font-size: 15px; }

.db-body {
    border-top: 1px solid var(--border);
    padding: 18px;
    background: #e8eff6;
}
.db-section + .db-section { margin-top: 22px; }
.db-section-title {
    margin: 0 0 10px;
    font-size: 11px; font-weight: 600; color: var(--muted);
    text-transform: uppercase; letter-spacing: 0.7px;
}
.db-section table.grid {
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    overflow: hidden;
}

.placeholder {
    border: 1px dashed var(--border-strong);
    border-radius: var(--radius-sm);
    padding: 20px;
    text-align: center;
    background: var(--surface);
}

/* -----------------------------------------------------------------------
   Branch-block (one per branch inside an accordion)
   ----------------------------------------------------------------------- */
.branch-block {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    margin-bottom: 14px;
    overflow: hidden;
}
.branch-block.is-excluded {
    background: #fbe7e7;
    border-color: #f0c5c5;
}
.branch-block-head {
    display: flex; align-items: center; gap: 12px;
    padding: 10px 14px;
    background: #eef3f8;
    border-bottom: 1px solid var(--border);
    font-size: 13px;
}
.branch-block.is-excluded .branch-block-head {
    background: #f7d8d8;
    border-color: #f0c5c5;
}
.branch-code-chip {
    font-family: ui-monospace, SFMono-Regular, Consolas, monospace;
    font-weight: 700;
    background: var(--surface); color: var(--ink-soft);
    border: 1px solid var(--border);
    padding: 2px 8px; border-radius: 4px; font-size: 12px;
}
.branch-block-name { font-weight: 600; }
.branch-block-meta { color: var(--muted); font-size: 12px; }
.branch-block-spacer { flex: 1; }
.branch-block-total { font-weight: 700; color: var(--ink); }

/* Lines table */
.lines-table { border: 0; }
.lines-table th { background: var(--surface); padding: 8px 10px; }
.lines-table td { padding: 6px 10px; vertical-align: middle; }
.lines-table tr:hover td { background: transparent; }
.lines-table .line-item,
.lines-table .line-text,
.lines-table .line-num {
    width: 100%;
    padding: 5px 8px;
    font-size: 13px;
    border: 1px solid var(--border);
    border-radius: 4px;
    background: var(--surface);
    color: var(--ink);
    font-family: inherit;
}
.lines-table .line-num { text-align: right; font-variant-numeric: tabular-nums; }
.lines-table .line-item:focus,
.lines-table .line-text:focus,
.lines-table .line-num:focus {
    outline: none;
    border-color: var(--accent);
    box-shadow: 0 0 0 2px var(--accent-soft);
}
.lines-table .line-calc { color: var(--muted); font-variant-numeric: tabular-nums; }

.bill-capped {
    background: #fbe7e7; color: #6b2424;
    padding: 2px 8px; border-radius: 4px;
    font-weight: 700;
}
.row-x {
    background: transparent; border: 0;
    color: var(--muted); cursor: pointer;
    font-size: 18px; line-height: 1;
    padding: 2px 6px; border-radius: 4px;
}
.row-x:hover { background: #fbe7e7; color: var(--danger); }

.lines-actions {
    display: flex; gap: 8px;
    padding: 10px 14px;
    border-top: 1px solid var(--border);
    background: #f9fbfd;
}

/* Branch→Customer link form on /Customers */
.cust-select {
    width: 100%; max-width: 360px;
    padding: 5px 8px; font-size: 13px;
    border: 1px solid var(--border); border-radius: 4px;
    background: var(--surface); color: var(--ink);
    font-family: inherit;
}

/* -----------------------------------------------------------------------
   Flat lines table — one table per DB accordion, per-row save buttons
   ----------------------------------------------------------------------- */
.flat-lines { background: var(--surface); }
.flat-lines th { background: #eef3f8; font-size: 11px; padding: 8px 8px; }
.flat-lines td { padding: 6px 8px; vertical-align: middle; font-size: 13px; }
.flat-lines .qbcust-input {
    width: 100%;
    padding: 4px 8px; font-size: 13px;
    border: 1px solid var(--border); border-radius: 4px;
    background: var(--surface); color: var(--ink); font-family: inherit;
    font-weight: 600;
}
.flat-lines .qbcust-input:focus {
    outline: none; border-color: var(--accent);
    box-shadow: 0 0 0 2px var(--accent-soft);
}

/* Visual grouping: the first row of each branch gets a top border to separate it. */
.flat-lines .branch-first td { border-top: 2px solid var(--border-strong); }
.flat-lines .branch-cont td { border-top: 1px dotted var(--border); }
.flat-lines tr.add-row td {
    background: #aabac9;
    border-bottom: 2px solid #8a9aaa;
}
.flat-lines tr.add-row td * { opacity: 0.95; }
.flat-lines tr.add-row .line-item,
.flat-lines tr.add-row .line-text,
.flat-lines tr.add-row .line-num {
    background: #ffffff;
}

.save-btn {
    background: var(--mint); color: #1f4d33;
    border: 0; border-radius: 4px;
    width: 30px; height: 30px;
    cursor: pointer; font-size: 14px; font-weight: 700;
    box-shadow: 0 1px 2px rgba(30,58,95,0.10);
}
.save-btn:hover { background: var(--mint-hover); }
.save-btn.add-btn { background: var(--sky); color: #1e3a5f; }
.save-btn.add-btn:hover { background: #6aa9b5; }

.users-cell { white-space: nowrap; }
.users-frac { display: inline-block; font-variant-numeric: tabular-nums; margin-right: 6px; }
.users-tick { vertical-align: middle; }
.qty-num { width: 70px; }
