/* CWR Office — mobile-first base, desktop sidebar in @media. Light + dark via variables. */
:root {
    --bg: #f4f6f8;
    --surface: #ffffff;
    --surface-2: #f0f2f5;
    --border: #dce1e7;
    --text: #1c2530;
    --text-muted: #6b7785;
    --brand: #17683f;
    --brand-ink: #ffffff;
    --accent: #1f8a54;
    --ok-bg: #e3f4ea; --ok-text: #17683f;
    --muted-bg: #eceff2; --muted-text: #6b7785;
    --err-bg: #fdecec; --err-text: #b02a2a;
    --u-fresh: #1f8a54;   --u-fresh-bg: #e3f4ea;
    --u-aging: #b8860b;   --u-aging-bg: #f9efd6;
    --u-overdue: #c0392b; --u-overdue-bg: #fbe4e1;
    --radius: 8px;
    --radius-lg: 12px;
    --shadow: 0 1px 2px rgba(16,24,32,.06), 0 4px 16px rgba(16,24,32,.05);
}
@media (prefers-color-scheme: dark) {
    :root {
        --bg: #10151b; --surface: #1a222c; --surface-2: #212b36; --border: #2c3846;
        --text: #e7edf3; --text-muted: #97a4b2; --brand: #2fae6d; --accent: #38c07c;
        --ok-bg: #14301f; --ok-text: #6fe0a3; --muted-bg: #222c37; --muted-text: #97a4b2;
        --err-bg: #3a1d1d; --err-text: #ff9c9c;
        --u-fresh: #38c07c;   --u-fresh-bg: #14301f;
        --u-aging: #e0b34a;   --u-aging-bg: #372c12;
        --u-overdue: #f0776b; --u-overdue-bg: #3a1d1d;
        --shadow: 0 1px 2px rgba(0,0,0,.4), 0 6px 20px rgba(0,0,0,.35);
    }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    color: var(--text);
    background: var(--bg);
    font-size: 15px;
    line-height: 1.45;
}

/* ---- App shell (mobile: stacked) ---- */
.app { display: flex; flex-direction: column; min-height: 100vh; }
.sidebar {
    background: var(--brand);
    color: var(--brand-ink);
    padding: 12px 16px;
}
.brand { font-weight: 700; font-size: 18px; letter-spacing: .2px; }
.brand-mark {
    display: inline-block; background: rgba(255,255,255,.16);
    padding: 2px 7px; border-radius: 6px; margin-right: 6px; font-weight: 800;
}
.nav { display: flex; gap: 4px; overflow-x: auto; margin-top: 10px; }
.nav-link {
    color: rgba(255,255,255,.82); text-decoration: none;
    padding: 8px 12px; border-radius: 6px; white-space: nowrap; font-weight: 500;
    min-height: 44px; display: flex; align-items: center;
}
.nav-link:hover { background: rgba(255,255,255,.1); color: #fff; }
.nav-link.is-active { background: rgba(255,255,255,.2); color: #fff; }

.main { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.topbar {
    display: flex; justify-content: space-between; align-items: center; gap: 12px;
    padding: 10px 16px; background: var(--surface); border-bottom: 1px solid var(--border);
}
.scope { font-weight: 600; color: var(--text); font-size: 14px; }
.account { display: flex; align-items: center; gap: 10px; }
.account-name { color: var(--text-muted); font-size: 13px; }
.logout { margin: 0; }

.content { padding: 16px; }

/* ---- Flash ---- */
.flash { padding: 10px 14px; border-radius: var(--radius); margin: 12px 16px 0; font-size: 14px; }
.flash-ok { background: var(--ok-bg); color: var(--ok-text); }
.flash-error { background: var(--err-bg); color: var(--err-text); }

/* ---- Page head ---- */
.page-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; margin-bottom: 16px; }
.page-title { font-size: 22px; margin: 0; }
.empty { color: var(--text-muted); }

/* ---- Buttons ---- */
.btn {
    display: inline-flex; align-items: center; justify-content: center;
    min-height: 40px; padding: 0 16px; border-radius: var(--radius);
    border: 1px solid transparent; font-size: 14px; font-weight: 600;
    cursor: pointer; text-decoration: none; background: var(--surface-2); color: var(--text);
}
.btn:hover { filter: brightness(.97); }
.btn-primary { background: var(--brand); color: #fff; }
.btn-ghost { background: transparent; border-color: var(--border); color: var(--text); }
.btn-block { width: 100%; }

/* ---- Stat grid ---- */
.stat-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 12px; }
.stat-card {
    background: var(--surface); border: 1px solid var(--border);
    border-radius: var(--radius-lg); padding: 16px; box-shadow: var(--shadow);
}
.stat-value { font-size: 30px; font-weight: 700; line-height: 1; }
.stat-label { color: var(--text-muted); font-size: 13px; margin-top: 6px; }

/* ---- Tables ---- */
.table-wrap { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-lg); overflow-x: auto; box-shadow: var(--shadow); }
.table { width: 100%; border-collapse: collapse; font-size: 14px; }
.table th, .table td { text-align: left; padding: 12px 14px; border-bottom: 1px solid var(--border); }
.table th { color: var(--text-muted); font-weight: 600; font-size: 12px; text-transform: uppercase; letter-spacing: .4px; }
.table tbody tr:last-child td { border-bottom: none; }

.badge { display: inline-block; padding: 3px 9px; border-radius: 999px; font-size: 12px; font-weight: 600; }
.badge-ok { background: var(--ok-bg); color: var(--ok-text); }
.badge-muted { background: var(--muted-bg); color: var(--muted-text); }
/* Outstanding but not ignored — a part-paid or issued invoice reads as neither settled nor
   unissued. Reuses the urgency ramp so both themes are already covered. */
.badge-warn { background: var(--u-aging-bg); color: var(--u-aging); }

/* ---- Forms ---- */
.form-card, .auth-form { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-lg); padding: 20px; max-width: 480px; box-shadow: var(--shadow); }
.field { display: block; margin-bottom: 14px; }
.field-label { display: block; font-size: 13px; color: var(--text-muted); margin-bottom: 5px; font-weight: 600; }
.input { width: 100%; padding: 10px 12px; border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); color: var(--text); font-size: 15px; min-height: 44px; }
.input:focus { outline: 2px solid var(--accent); outline-offset: 1px; border-color: var(--accent); }
.form-actions { margin-top: 6px; }

/* ---- Auth page ---- */
.auth-body { display: flex; align-items: center; justify-content: center; min-height: 100vh; padding: 20px; }
.auth-card { width: 100%; max-width: 380px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-lg); padding: 28px; box-shadow: var(--shadow); }
.auth-brand { font-size: 22px; font-weight: 700; }
.auth-sub { color: var(--text-muted); margin: 4px 0 18px; font-size: 14px; }
.auth-card .flash { margin: 0 0 14px; }

/* ---- Desktop: sidebar layout ---- */
@media (min-width: 800px) {
    .app { flex-direction: row; }
    .sidebar { width: 224px; min-height: 100vh; padding: 20px 16px; }
    .nav { flex-direction: column; gap: 4px; overflow: visible; margin-top: 20px; }
    .content { padding: 28px; }
    .page-title { font-size: 28px; }
    .stat-grid { grid-template-columns: repeat(4, 1fr); gap: 20px; }
    .stat-value { font-size: 36px; }
    .flash { margin: 16px 28px 0; }
}

/* ============ Dispatch + Map ============ */
.head-meta {
    font-size: 13px; color: var(--text-muted); font-weight: 600;
    background: var(--surface-2); padding: 5px 12px; border-radius: 999px;
}
.empty-card {
    background: var(--surface); border: 1px dashed var(--border); border-radius: var(--radius-lg);
    padding: 40px 20px; text-align: center;
}
.empty-mark { font-size: 34px; color: var(--text-muted); opacity: .5; line-height: 1; }

/* Dispatch board */
.dispatch-grid { display: grid; grid-template-columns: 1fr; gap: 12px; }
.wo-card {
    position: relative; background: var(--surface); border: 1px solid var(--border);
    border-radius: var(--radius-lg); padding: 16px 16px 14px 20px; box-shadow: var(--shadow);
    overflow: hidden;
}
.wo-card::before {
    content: ""; position: absolute; left: 0; top: 0; bottom: 0; width: 5px; background: var(--u-fresh);
}
.wo-card.u-aging::before   { background: var(--u-aging); }
.wo-card.u-overdue::before { background: var(--u-overdue); }

.wo-top { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; }
.wo-basin-label { display: block; font-size: 11px; text-transform: uppercase; letter-spacing: .6px; color: var(--text-muted); }
.wo-basin-num { font-size: 24px; font-weight: 800; letter-spacing: -.3px; }
.wo-days { text-align: right; line-height: 1.05; }
.wo-days-num { font-size: 26px; font-weight: 800; color: var(--u-fresh); }
.u-aging .wo-days-num   { color: var(--u-aging); }
.u-overdue .wo-days-num { color: var(--u-overdue); }
.wo-days-unit { display: block; font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: .4px; color: var(--text-muted); margin-top: 2px; }

.wo-where { margin: 12px 0 14px; }
.wo-customer { font-weight: 700; font-size: 15px; }
.wo-site { color: var(--text-muted); font-size: 13px; margin-top: 2px; }

.wo-foot { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; padding-top: 12px; border-top: 1px solid var(--border); }
.wo-chip { display: inline-flex; align-items: center; gap: 6px; font-size: 13px; font-weight: 600; background: var(--surface-2); padding: 5px 10px; border-radius: 999px; }
.chip-dot { width: 7px; height: 7px; border-radius: 50%; background: var(--accent); }
.wo-metric { font-size: 13px; color: var(--text-muted); font-weight: 600; }
.wo-weight { margin-left: auto; color: var(--text); font-variant-numeric: tabular-nums; }

/* Map */
.map-panel { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-lg); overflow: hidden; box-shadow: var(--shadow); }
.basin-map { height: 62vh; min-height: 360px; width: 100%; background: var(--surface-2); z-index: 0; }
.map-legend { display: flex; gap: 16px; flex-wrap: wrap; padding: 12px 16px; border-top: 1px solid var(--border); font-size: 13px; color: var(--text-muted); }
.legend-item { display: inline-flex; align-items: center; gap: 7px; font-weight: 600; }
.legend-dot { width: 12px; height: 12px; border-radius: 50%; }
.legend-dot.u-fresh { background: var(--u-fresh); }
.legend-dot.u-aging { background: var(--u-aging); }
.legend-dot.u-overdue { background: var(--u-overdue); }

/* Map pins (Leaflet divIcon) */
.pin {
    display: flex; align-items: center; justify-content: center;
    width: 34px; height: 34px; border-radius: 50% 50% 50% 0; transform: rotate(-45deg);
    background: var(--u-fresh); color: #fff; box-shadow: 0 2px 6px rgba(0,0,0,.4); border: 2px solid #fff;
}
.pin.u-aging { background: var(--u-aging); }
.pin.u-overdue { background: var(--u-overdue); }
.pin b { transform: rotate(45deg); font-size: 10px; font-weight: 800; }

@media (min-width: 800px) {
    .dispatch-grid { grid-template-columns: repeat(auto-fill, minmax(320px, 1fr)); gap: 18px; }
    .wo-basin-num { font-size: 26px; }
}

/* ============ Dispatch assignment board ============ */
.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0; }
.head-actions { display: flex; align-items: center; gap: 10px; }
.field-hint { font-weight: 400; color: var(--text-muted); text-transform: none; letter-spacing: 0; }
.btn-sm { min-height: 32px; padding: 0 10px; font-size: 13px; }

.board { display: flex; gap: 14px; overflow-x: auto; padding-bottom: 8px; align-items: flex-start; }
.board-col {
    flex: 0 0 260px; background: var(--surface-2); border: 1px solid var(--border);
    border-radius: var(--radius-lg); display: flex; flex-direction: column; max-height: 74vh;
}
.board-col-queue { background: var(--surface); border-style: dashed; }
.board-col.is-over { outline: 2px dashed var(--accent); outline-offset: -2px; }
.col-head { display: flex; align-items: center; justify-content: space-between; padding: 12px 14px; border-bottom: 1px solid var(--border); }
.col-name { font-weight: 700; font-size: 14px; }
.col-count { background: var(--muted-bg); color: var(--muted-text); font-size: 12px; font-weight: 700; padding: 2px 9px; border-radius: 999px; }
.col-body { padding: 12px; display: flex; flex-direction: column; gap: 10px; overflow-y: auto; min-height: 60px; }

.job-card {
    background: var(--surface); border: 1px solid var(--border); border-left: 4px solid var(--text-muted);
    border-radius: var(--radius); padding: 12px; box-shadow: var(--shadow); cursor: grab;
}
.job-card:active { cursor: grabbing; }
.job-card.is-dragging { opacity: .45; }
.job-card.jt-delivery { border-left-color: #2b7cd3; }
.job-card.jt-pickup   { border-left-color: #c98a12; }
.job-card.jt-dump     { border-left-color: #8a5cd1; }

.job-top { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 6px; }
.job-type { font-size: 11px; font-weight: 800; text-transform: uppercase; letter-spacing: .5px; padding: 2px 8px; border-radius: 999px; color: #fff; background: var(--text-muted); }
.jt-delivery .job-type { background: #2b7cd3; }
.jt-pickup .job-type   { background: #c98a12; }
.jt-dump .job-type     { background: #8a5cd1; }
.job-basin { font-weight: 800; font-size: 14px; }
.job-customer { font-weight: 700; font-size: 14px; }
.job-site { color: var(--text-muted); font-size: 13px; margin-top: 1px; }
.job-foot { margin-top: 8px; }
.job-due { font-size: 12px; font-weight: 600; color: var(--text-muted); background: var(--surface-2); padding: 3px 8px; border-radius: 999px; }

.assign-form { display: flex; gap: 6px; margin-top: 10px; padding-top: 10px; border-top: 1px solid var(--border); }
.assign-select { flex: 1; min-height: 34px; padding: 4px 8px; border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); color: var(--text); font-size: 13px; }

/* ============ Billing / invoices ============ */
.page-sub { color: var(--text-muted); font-size: 14px; margin-top: 2px; }
.inline-bar { display: flex; gap: 10px; margin-bottom: 16px; flex-wrap: wrap; }
.inline-select { max-width: 380px; flex: 1; min-width: 220px; }
.row-link { cursor: pointer; }
.table .num { text-align: right; font-variant-numeric: tabular-nums; }
.tfoot-label { color: var(--text-muted); font-weight: 600; }
.table tfoot td { padding: 8px 14px; border-bottom: none; }
.tfoot-total td { font-size: 16px; font-weight: 800; border-top: 2px solid var(--border); padding-top: 12px; }

.invoice-grid { display: grid; grid-template-columns: 1fr; gap: 16px; }
.invoice-side { display: flex; flex-direction: column; gap: 14px; }
.side-card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-lg); padding: 16px; box-shadow: var(--shadow); }
.side-title { font-weight: 700; margin-bottom: 10px; }
.side-row { display: flex; justify-content: space-between; padding: 5px 0; font-size: 14px; }
.side-row span { color: var(--text-muted); }
.side-balance { border-top: 1px solid var(--border); margin-top: 6px; padding-top: 10px; font-size: 17px; }
.side-balance strong { color: var(--brand); }
.side-form { margin: 0; }

.input-prefix, .input-suffix { display: flex; align-items: center; }
.input-prefix .prefix, .input-suffix .suffix { padding: 0 10px; color: var(--text-muted); font-weight: 700; }
.input-prefix .input { border-top-left-radius: 0; border-bottom-left-radius: 0; }
.input-prefix .prefix { border: 1px solid var(--border); border-right: none; border-radius: var(--radius) 0 0 var(--radius); min-height: 44px; display: flex; align-items: center; background: var(--surface-2); }
.input-suffix .input { border-top-right-radius: 0; border-bottom-right-radius: 0; }
.input-suffix .suffix { border: 1px solid var(--border); border-left: none; border-radius: 0 var(--radius) var(--radius) 0; min-height: 44px; display: flex; align-items: center; background: var(--surface-2); }
.form-note { color: var(--text-muted); font-size: 13px; }

@media (min-width: 800px) {
    .invoice-grid { grid-template-columns: 2fr 1fr; align-items: start; }
}

/* Billing settings */
.section-title { font-size: 18px; margin: 28px 0 12px; }
.field-row { display: flex; gap: 14px; }
.field-row .field { flex: 1; }
textarea.input { min-height: 60px; resize: vertical; }
input[type="color"].input { padding: 4px; height: 44px; }

/* Reports */
.report-cols { display: grid; grid-template-columns: 1fr; gap: 8px; margin-top: 8px; }
@media (min-width: 900px) { .report-cols { grid-template-columns: 1fr 1fr; gap: 24px; } }

/* Read-only payment link on the invoice page. Monospace + full width so a long token is
   selectable and visibly complete rather than silently truncated. */
.pay-link-input {
  width: 100%;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: .8rem;
  padding: .45rem .5rem;
  border: 1px solid #cfd8e3;
  border-radius: 6px;
  background: #f8fafc;
  color: #16202b;
}
.pay-link-input:focus { outline: 2px solid #1f6feb; outline-offset: 1px; }

/* ---- Payment history ----
   A reversed payment stays on screen: deleting it would make the money look like it never
   arrived. It is dimmed rather than hidden so the row is legible as history, not as an
   outstanding entry someone might chase. */
.muted { color: var(--text-muted); }
.ledger-row { padding: 8px 0; border-bottom: 1px solid var(--border); }
.ledger-row:last-child { border-bottom: none; }
.ledger-row.is-reversed .side-row strong { text-decoration: line-through; color: var(--text-muted); }
.ledger-note { color: var(--text-muted); font-size: 12px; margin-top: 2px; }
.ledger-warn { color: var(--u-overdue); font-size: 12px; margin: 6px 0 0; }
.ledger-reverse { display: flex; gap: 6px; margin-top: 8px; flex-wrap: wrap; }
.ledger-reverse .input-sm { flex: 1; min-width: 130px; }
.input-sm { min-height: 32px; padding: 5px 8px; font-size: 13px; }

/* Ad-hoc line entry: description takes the room, qty and price stay small. */
.line-form { max-width: none; margin-top: 16px; }
.line-form .field-row { flex-wrap: wrap; }
.line-desc { flex: 3 1 220px; }
.line-qty { flex: 0 1 90px; }
.line-unit { flex: 1 1 140px; }
