/* ==========================================================================
   SproCRM design system
   Mobile-first. No CSS framework — the legacy app shipped fixed pixel widths
   (1500px grids, 858px inputs) and no viewport meta, so nothing here assumes
   a desktop.
   ========================================================================== */

:root {
    /* Brand */
    --brand-600: #1d4ed8;
    --brand-500: #2563eb;
    --brand-400: #3b82f6;
    --brand-50:  #eff6ff;

    /* Surfaces */
    --bg:          #f6f7f9;
    --surface:     #ffffff;
    --surface-2:   #f9fafb;
    --surface-3:   #f3f4f6;
    --border:      #e5e7eb;
    --border-strong: #d1d5db;

    /* Text */
    --text:        #111827;
    --text-muted:  #6b7280;
    --text-subtle: #9ca3af;
    --text-invert: #ffffff;

    /* Status */
    --ok:      #059669;
    --ok-bg:   #ecfdf5;
    --warn:    #b45309;
    --warn-bg: #fffbeb;
    --danger:  #dc2626;
    --danger-bg: #fef2f2;
    --info:    #0369a1;
    --info-bg: #f0f9ff;

    /* Cost buckets */
    --ems:  #0d9488;
    --rb:   #7c3aed;
    --unspec: #a16207;

    --radius:    10px;
    --radius-sm: 6px;
    --radius-lg: 16px;

    --shadow-sm: 0 1px 2px rgb(16 24 40 / .06);
    --shadow:    0 1px 3px rgb(16 24 40 / .1), 0 1px 2px rgb(16 24 40 / .06);
    --shadow-md: 0 4px 12px rgb(16 24 40 / .08);
    --shadow-lg: 0 12px 32px rgb(16 24 40 / .12);

    --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    --mono: ui-monospace, SFMono-Regular, "Cascadia Mono", Consolas, monospace;

    --header-h: 60px;
}

@media (prefers-color-scheme: dark) {
    :root:not([data-theme="light"]) {
        --bg:        #0b0f17;
        --surface:   #131a24;
        --surface-2: #182130;
        --surface-3: #1e2937;
        --border:    #253044;
        --border-strong: #334155;

        --text:        #e8edf5;
        --text-muted:  #9aa8bd;
        --text-subtle: #6b7b93;

        --brand-50: #16233d;
        --ok-bg:     #052e21;
        --warn-bg:   #2a1e05;
        --danger-bg: #2d0f0f;
        --info-bg:   #04283c;

        --shadow-sm: 0 1px 2px rgb(0 0 0 / .4);
        --shadow:    0 1px 3px rgb(0 0 0 / .5);
        --shadow-md: 0 4px 12px rgb(0 0 0 / .45);
        --shadow-lg: 0 12px 32px rgb(0 0 0 / .55);
    }
}

*, *::before, *::after { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
    margin: 0;
    font-family: var(--font);
    font-size: 15px;
    line-height: 1.5;
    color: var(--text);
    background: var(--bg);
    -webkit-font-smoothing: antialiased;
}

h1, h2, h3, h4 { margin: 0 0 .5rem; line-height: 1.25; font-weight: 650; letter-spacing: -.01em; }
h1 { font-size: 1.5rem; }
h2 { font-size: 1.175rem; }
h3 { font-size: 1rem; }

a { color: var(--brand-500); text-decoration: none; }
a:hover { text-decoration: underline; }

:focus-visible {
    outline: 2px solid var(--brand-500);
    outline-offset: 2px;
    border-radius: 3px;
}

/* ---------- Layout ---------- */
.shell { min-height: 100vh; display: flex; flex-direction: column; }

.masthead {
    position: sticky; top: 0; z-index: 50;
    height: var(--header-h);
    display: flex; align-items: center; gap: 1rem;
    padding: 0 1rem;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    box-shadow: var(--shadow-sm);
}

.brand {
    display: flex; align-items: center; gap: .55rem;
    font-weight: 700; font-size: 1.0625rem; letter-spacing: -.02em;
    color: var(--text); flex-shrink: 0;
}
.brand:hover { text-decoration: none; }

/* The logo is cropped to its content, so it can fill the header height. */
.brand-logo { height: 42px; width: auto; display: block; }
.auth-brand { justify-content: center; margin-bottom: .75rem; }
.auth-brand .brand-logo { height: 92px; }

.nav { display: flex; gap: .125rem; margin-left: .5rem; flex: 1; min-width: 0; }
.nav a {
    padding: .45rem .7rem; border-radius: var(--radius-sm);
    color: var(--text-muted); font-weight: 550; font-size: .875rem;
    white-space: nowrap;
}
.nav a:hover { background: var(--surface-3); color: var(--text); text-decoration: none; }
.nav a.active { background: var(--brand-50); color: var(--brand-600); }

.masthead-right { display: flex; align-items: center; gap: .5rem; margin-left: auto; flex-shrink: 0; }

.who {
    display: flex; align-items: center; gap: .5rem;
    font-size: .8125rem; color: var(--text-muted);
}
.avatar {
    width: 28px; height: 28px; border-radius: 50%;
    background: var(--surface-3); color: var(--text);
    display: grid; place-items: center;
    font-size: .75rem; font-weight: 700;
}

.main { flex: 1; width: 100%; max-width: 1360px; margin: 0 auto; padding: 1.25rem 1rem 3rem; }

.page-head {
    display: flex; flex-wrap: wrap; align-items: flex-start; gap: .75rem;
    margin-bottom: 1.25rem;
}
.page-head .grow { flex: 1; min-width: 220px; }
.page-head p { margin: .125rem 0 0; color: var(--text-muted); font-size: .875rem; }

/* ---------- Buttons ---------- */
.btn {
    display: inline-flex; align-items: center; justify-content: center; gap: .4rem;
    min-height: 40px; padding: .5rem .9rem;
    border: 1px solid var(--border-strong); border-radius: var(--radius-sm);
    background: var(--surface); color: var(--text);
    font: inherit; font-size: .875rem; font-weight: 600;
    cursor: pointer; white-space: nowrap;
    transition: background .12s, border-color .12s, transform .04s;
}
.btn:hover { background: var(--surface-3); text-decoration: none; }
.btn:active { transform: translateY(1px); }
.btn[disabled], .btn:disabled { opacity: .5; cursor: not-allowed; }

.btn-primary {
    background: var(--brand-600); border-color: var(--brand-600); color: #fff;
    box-shadow: var(--shadow-sm);
}
.btn-primary:hover { background: var(--brand-500); border-color: var(--brand-500); }

.btn-danger { color: var(--danger); border-color: var(--border-strong); }
.btn-danger:hover { background: var(--danger-bg); border-color: var(--danger); }

.btn-ghost { border-color: transparent; background: transparent; }
.btn-ghost:hover { background: var(--surface-3); }

.btn-sm { min-height: 32px; padding: .3rem .6rem; font-size: .8125rem; }
.btn-block { width: 100%; }

/* ---------- Cards / panels ---------- */
.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow-sm);
}
.card-head {
    display: flex; align-items: center; gap: .5rem;
    padding: .85rem 1rem; border-bottom: 1px solid var(--border);
}
.card-head h2, .card-head h3 { margin: 0; }
.card-body { padding: 1rem; }
.card-body.tight { padding: .5rem; }

/* ---------- Stat tiles ---------- */
.stats {
    display: grid; gap: .75rem; margin-bottom: 1.25rem;
    grid-template-columns: repeat(auto-fit, minmax(155px, 1fr));
}
.stat {
    background: var(--surface); border: 1px solid var(--border);
    border-radius: var(--radius); padding: .9rem 1rem;
    box-shadow: var(--shadow-sm);
}
.stat .label {
    font-size: .6875rem; font-weight: 800; letter-spacing: .06em;
    text-transform: uppercase; color: var(--text);
}
.stat .value {
    font-size: 1.625rem; font-weight: 700; letter-spacing: -.02em;
    margin-top: .2rem; font-variant-numeric: tabular-nums;
}
.stat .hint { font-size: .75rem; color: var(--text-muted); margin-top: .1rem; }
.stat.accent .value { color: var(--brand-600); }
.stat.ok .value { color: var(--ok); }
.stat.warn .value { color: var(--warn); }

/* Status tiles double as filters for the dashboard list. */
a.stat { display: block; color: inherit; transition: border-color .12s, box-shadow .12s; }
a.stat:hover {
    text-decoration: none;
    border-color: var(--brand-400);
    box-shadow: var(--shadow-md);
}
a.stat.selected {
    border-color: var(--brand-600);
    box-shadow: 0 0 0 2px var(--brand-50), var(--shadow-sm);
}
a.stat.selected .hint { color: var(--brand-600); font-weight: 700; }

/* ---------- Filter bar ---------- */
.filters {
    display: flex; flex-wrap: wrap; gap: .5rem; align-items: center;
    padding: .75rem; margin-bottom: 1rem;
    background: var(--surface); border: 1px solid var(--border);
    border-radius: var(--radius); box-shadow: var(--shadow-sm);
}
.filters .search { flex: 1 1 260px; display: flex; gap: .4rem; }
.filters .search input { flex: 1; min-width: 0; }

/* ---------- Forms ---------- */
label, .label {
    display: block; font-size: .8125rem; font-weight: 700;
    color: var(--text); margin-bottom: .3rem;
}
input[type=text], input[type=search], input[type=password], input[type=email],
input[type=tel], input[type=number], input[type=date], select, textarea {
    width: 100%; min-height: 40px; padding: .5rem .65rem;
    font: inherit; font-size: .9375rem; color: var(--text);
    background: var(--surface);
    border: 1px solid var(--border-strong); border-radius: var(--radius-sm);
    transition: border-color .12s, box-shadow .12s;
}
textarea { min-height: 88px; resize: vertical; line-height: 1.5; }
input:focus, select:focus, textarea:focus {
    outline: none; border-color: var(--brand-500);
    box-shadow: 0 0 0 3px rgb(37 99 235 / .15);
}
input::placeholder, textarea::placeholder { color: var(--text-subtle); }
select { appearance: none; padding-right: 2rem;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath fill='%236b7280' d='M2.6 4.2h6.8L6 8.4z'/%3E%3C/svg%3E");
    background-repeat: no-repeat; background-position: right .6rem center; }

.field { margin-bottom: .9rem; }
.field-grid {
    display: grid; gap: .9rem;
    grid-template-columns: repeat(auto-fit, minmax(230px, 1fr));
}
fieldset { border: 0; padding: 0; margin: 0 0 1.5rem; }
legend {
    padding: 0; margin-bottom: .75rem; font-size: .75rem; font-weight: 800;
    letter-spacing: .06em; text-transform: uppercase; color: var(--text);
    width: 100%; border-bottom: 1px solid var(--border-strong); padding-bottom: .4rem;
}

.radio-row { display: flex; flex-wrap: wrap; gap: .4rem; }
.radio-chip { position: relative; }
.radio-chip input { position: absolute; opacity: 0; inset: 0; cursor: pointer; }
.radio-chip span {
    display: block; padding: .4rem .75rem; font-size: .8125rem; font-weight: 600;
    color: var(--text-muted); background: var(--surface);
    border: 1px solid var(--border-strong); border-radius: 999px; cursor: pointer;
}
.radio-chip input:checked + span {
    background: var(--brand-50); border-color: var(--brand-500); color: var(--brand-600);
}
.radio-chip input:focus-visible + span { outline: 2px solid var(--brand-500); outline-offset: 2px; }

.validation-summary-errors, .field-validation-error, .text-danger {
    color: var(--danger); font-size: .8125rem;
}
.validation-summary-errors ul { margin: 0; padding-left: 1.1rem; }
input.input-validation-error, textarea.input-validation-error, select.input-validation-error {
    border-color: var(--danger);
}

/* ---------- Alerts ---------- */
.alert {
    display: flex; gap: .6rem; align-items: flex-start;
    padding: .7rem .85rem; margin-bottom: 1rem;
    border: 1px solid var(--border); border-left-width: 3px;
    border-radius: var(--radius-sm); font-size: .875rem;
}
.alert-ok     { background: var(--ok-bg);     border-left-color: var(--ok);     color: var(--ok); }
.alert-warn   { background: var(--warn-bg);   border-left-color: var(--warn);   color: var(--warn); }
.alert-danger { background: var(--danger-bg); border-left-color: var(--danger); color: var(--danger); }
.alert-info   { background: var(--info-bg);   border-left-color: var(--info);   color: var(--info); }

/* ---------- Badges ---------- */
.badge {
    display: inline-flex; align-items: center; gap: .3rem;
    padding: .15rem .5rem; border-radius: 999px;
    font-size: .75rem; font-weight: 700; letter-spacing: .01em;
    white-space: nowrap;
}
.badge::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: currentColor; }
.badge-open   { background: var(--ok-bg);     color: var(--ok); }
.badge-closed { background: var(--surface-3); color: var(--text-muted); }
.badge-ems    { background: var(--info-bg);   color: var(--ems); }
.badge-rb     { background: var(--brand-50);  color: var(--rb); }
.badge-unspec { background: var(--warn-bg);   color: var(--unspec); }
.badge::before { flex-shrink: 0; }

/* ---------- Tables (desktop) ---------- */
.table-wrap {
    /* Horizontal scroll is confined here; the page body never scrolls sideways. */
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    border-radius: var(--radius);
}
table.data { width: 100%; border-collapse: collapse; font-size: .875rem; }
table.data th, table.data td {
    padding: .6rem .7rem; text-align: left; vertical-align: middle;
    border-bottom: 1px solid var(--border); white-space: nowrap;
}
table.data thead th {
    position: sticky; top: 0; z-index: 1;
    background: var(--surface-2); color: var(--text);
    font-size: .6875rem; font-weight: 800; letter-spacing: .05em; text-transform: uppercase;
    border-bottom: 1px solid var(--border-strong);
}
table.data thead th a { color: inherit; }
table.data tbody tr:hover { background: var(--surface-2); }
table.data tbody tr:last-child td { border-bottom: 0; }
table.data td.wrap { white-space: normal; min-width: 200px; }
table.data td.num { text-align: right; font-variant-numeric: tabular-nums; }
.sort-ind { opacity: .5; font-size: .7rem; }

/* ---------- Mobile card list ---------- */
.job-cards { display: none; }
.job-card {
    display: block; padding: .85rem;
    background: var(--surface); border: 1px solid var(--border);
    border-radius: var(--radius); box-shadow: var(--shadow-sm);
    margin-bottom: .6rem; color: var(--text);
}
.job-card:hover { text-decoration: none; border-color: var(--border-strong); }
.job-card .row1 { display: flex; align-items: flex-start; gap: .5rem; }
.job-card .addr { font-weight: 650; flex: 1; min-width: 0; overflow-wrap: anywhere; }
.job-card .meta { margin-top: .4rem; font-size: .8125rem; color: var(--text-muted); }
.job-card .meta div { margin-top: .12rem; }
.job-card .jobid {
    font-family: var(--mono); font-size: .75rem; color: var(--text-subtle);
}

/* ---------- Pager ---------- */
.pager {
    display: flex; flex-wrap: wrap; align-items: center; gap: .5rem;
    margin-top: 1rem; font-size: .8125rem; color: var(--text-muted);
}
.pager .spacer { flex: 1; }

/* ---------- Timeline (job log) ---------- */
.timeline { list-style: none; margin: 0; padding: 0; }
.timeline li {
    position: relative; padding: 0 0 .9rem 1.15rem;
    border-left: 2px solid var(--border);
}
.timeline li:last-child { border-left-color: transparent; padding-bottom: 0; }
.timeline li::before {
    content: ""; position: absolute; left: -5px; top: .35rem;
    width: 8px; height: 8px; border-radius: 50%;
    background: var(--brand-400); box-shadow: 0 0 0 3px var(--surface);
}
.timeline .when { font-size: .75rem; color: var(--text-subtle); font-variant-numeric: tabular-nums; }
.timeline .what { overflow-wrap: anywhere; }

/* ---------- Definition grid (job detail) ---------- */
.dl { display: grid; gap: .1rem 1rem; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); }
.dl > div { padding: .45rem 0; border-bottom: 1px solid var(--border); min-width: 0; }
.dl dt { font-size: .6875rem; font-weight: 800; letter-spacing: .05em;
         text-transform: uppercase; color: var(--text); margin: 0; }
.dl dd { margin: .1rem 0 0; overflow-wrap: anywhere; }
.dl dd.empty { color: var(--text-subtle); }

/* ---------- Cost summary ---------- */
.cost-grid { display: grid; gap: .6rem; grid-template-columns: repeat(auto-fit, minmax(130px, 1fr)); }
.cost-box {
    padding: .7rem .8rem; border: 1px solid var(--border);
    border-radius: var(--radius-sm); background: var(--surface-2);
}
.cost-box .k { font-size: .6875rem; font-weight: 800; letter-spacing: .05em;
               text-transform: uppercase; color: var(--text); }
.cost-box .v { font-size: 1.125rem; font-weight: 700; font-variant-numeric: tabular-nums; margin-top: .15rem; }
.cost-box.total { background: var(--brand-50); border-color: var(--brand-400); }
.cost-box.total .v { color: var(--brand-600); }

/* ---------- Empty state ---------- */
.empty { padding: 2.5rem 1rem; text-align: center; color: var(--text-muted); }
.empty .big { font-size: 2rem; opacity: .35; }
.empty p { margin: .4rem 0 0; font-size: .875rem; }

/* ---------- Auth ---------- */
.auth-wrap { min-height: 100vh; display: grid; place-items: center; padding: 1.25rem; }
.auth-card {
    width: 100%; max-width: 400px; padding: 1.75rem;
    background: var(--surface); border: 1px solid var(--border);
    border-radius: var(--radius-lg); box-shadow: var(--shadow-lg);
}
.auth-card .brand { justify-content: center; margin-bottom: .35rem; font-size: 1.25rem; }
.auth-card .sub { text-align: center; color: var(--text-muted); font-size: .875rem; margin: 0 0 1.5rem; }

.muted { color: var(--text-muted); }
.subtle { color: var(--text-subtle); }
.small { font-size: .8125rem; }
.mono { font-family: var(--mono); }
.num { font-variant-numeric: tabular-nums; }
.right { text-align: right; }
.stack { display: flex; flex-direction: column; gap: 1.25rem; }
.row-actions { display: flex; gap: .4rem; flex-wrap: wrap; }
.grid-2 { display: grid; gap: 1.25rem; grid-template-columns: 1fr; }
.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;
}
.skip-link {
    position: absolute; left: -9999px; top: 0; z-index: 100;
    padding: .6rem 1rem; background: var(--brand-600); color: #fff;
}
.skip-link:focus { left: 0; }

/* ==========================================================================
   Responsive
   ========================================================================== */

/* Phones: swap the data table for a tappable card list. */
@media (max-width: 767px) {
    .table-wrap { display: none; }
    .job-cards { display: block; }
    .main { padding: 1rem .75rem 2.5rem; }
    .nav { display: none; }
    .nav.mobile-open {
        display: flex; flex-direction: column; gap: .15rem;
        position: fixed; inset: var(--header-h) 0 auto 0;
        padding: .6rem; margin: 0;
        background: var(--surface); border-bottom: 1px solid var(--border);
        box-shadow: var(--shadow-md); z-index: 49;
    }
    .nav.mobile-open a { padding: .7rem .8rem; font-size: .9375rem; }
    .stats { grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); }
    .stat .value { font-size: 1.375rem; }
    .who .name { display: none; }
    h1 { font-size: 1.25rem; }
}

@media (min-width: 768px) {
    .nav-toggle { display: none; }
}

@media (min-width: 1024px) {
    .grid-2 { grid-template-columns: 1.6fr 1fr; }
    .main { padding: 1.5rem 1.5rem 3rem; }
}

.nav-toggle {
    min-height: 40px; min-width: 40px;
    display: grid; place-items: center;
    border: 1px solid var(--border-strong); border-radius: var(--radius-sm);
    background: var(--surface); color: var(--text); cursor: pointer;
}

@media print {
    .masthead, .filters, .row-actions, .pager, .nav-toggle { display: none !important; }
    body { background: #fff; }
    .card { box-shadow: none; border-color: #ccc; }
}

@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after { animation: none !important; transition: none !important; }
}
