/* Make the HTML `hidden` attribute reliably hide elements even when author
   CSS gives them an explicit display (e.g. .pdf-chosen uses display:flex,
   which otherwise ties the UA stylesheet on specificity and wins by source
   order, leaving a `hidden` element visible). */
[hidden] { display: none !important; }

:root {
    --bg:           #eef2f7;
    --surface:      #f7faff;
    --ink:          #1a1a1a;
    --ink-soft:     #4a4a4a;
    --ink-muted:    #7a8590;
    --rule:         #c8d4e2;
    --rule-soft:    #dde6f0;
    --accent:       #1f4e8b;
    --db-bg:        #dbe4ed;
    --db-bg-alt:    #cfd9e6;
    --db-ink:       #485866;
    --row-alt:      #eef3f9;
    --focus:        #2766b8;
    --header-bg:    #1a1a1a;
    --header-ink:   #eef2f7;
}

* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; }

body {
    background: var(--bg);
    color: var(--ink);
    font-family: 'Inter', -apple-system, sans-serif;
    font-size: 13px;
    line-height: 1.4;
    padding: 20px 20px 0;
    display: flex;
    flex-direction: column;
}

/* ── Header ─────────────────────────────────────────────────────── */
header {
    /* Title block on the left, user-strip on the right. The filter metrics
       live in the view-tabs now, not here. */
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 20px;
    padding-bottom: 14px;
    border-bottom: 2px solid var(--ink);
    flex-shrink: 0;
}

.title-block { position: relative; }
.title-block .eyebrow {
    font-family: 'JetBrains Mono', monospace;
    font-size: 10px;
    letter-spacing: 0.18em;
    text-transform: uppercase;
    color: var(--ink-muted);
    margin-bottom: 4px;
    user-select: none;   /* shift-click to open settings — don't start a selection */
}

/* Hidden settings menu — revealed by shift-click on the eyebrow */
.settings-menu {
    position: absolute;
    top: 100%;
    left: 0;
    z-index: 50;
    min-width: 340px;
    margin-top: 4px;
    padding: 14px 16px;
    background: var(--surface);
    border: 1px solid var(--ink);
    box-shadow: 0 12px 28px -12px rgba(0,0,0,0.3);
}
.settings-menu label + label { margin-top: 10px; }
.settings-menu h3 {
    font-family: 'JetBrains Mono', monospace;
    font-size: 10px;
    letter-spacing: 0.14em;
    text-transform: uppercase;
    color: var(--ink-muted);
    margin-bottom: 10px;
}
.settings-menu label {
    display: flex;
    flex-direction: column;
    gap: 4px;
    font-size: 11px;
    font-family: 'JetBrains Mono', monospace;
    letter-spacing: 0.04em;
}
.settings-menu select {
    font-family: 'Inter', sans-serif;
    font-size: 13px;
    padding: 6px 8px;
    border: 1px solid var(--rule);
    background: var(--bg);
    color: var(--ink);
}
.settings-hint {
    margin-top: 10px;
    font-size: 10px;
    color: var(--ink-muted);
    font-style: italic;
}

.title-block h1 {
    font-family: 'Fraunces', serif;
    font-weight: 600;
    font-size: 28px;
    letter-spacing: -0.02em;
    line-height: 1;
    font-style: italic;
}

/* Legacy .meta-row is no longer used in the header, but keep the typographic
   defaults in case stray usages remain. */
.meta-row {
    font-family: 'JetBrains Mono', monospace;
    font-size: 10px;
    color: var(--ink-muted);
    letter-spacing: 0.05em;
}

.legend {
    display: inline-flex;
    align-items: center;
    gap: 6px;
}

.legend-swatch {
    display: inline-block;
    width: 10px;
    height: 10px;
    border: 1px solid var(--rule);
}

.legend-swatch.db    { background: var(--db-bg); }
.legend-swatch.input { background: var(--surface); }

.row-count {
    color: var(--ink-soft);
    font-weight: 500;
}

/* Signed-in strip — right-side item of the header flex row. */
header .user-strip {
    font-family: 'JetBrains Mono', monospace;
    font-size: 10px;
    letter-spacing: 0.08em;
    color: var(--ink-muted);
    white-space: nowrap;
}
.user-strip strong { color: var(--ink); font-weight: 500; letter-spacing: 0.02em; }
.user-strip .sep   { margin: 0 6px; color: var(--rule); }
.user-strip .sign-out-link {
    color: var(--focus);
    text-decoration: none;
    font-weight: 500;
}
.user-strip .sign-out-link:hover { text-decoration: underline; }

/* Live search count — sits inline next to the search input in the toolbar. */
.toolbar .search { display: flex; align-items: center; gap: 10px; }
.toolbar .search-count {
    font-family: 'JetBrains Mono', monospace;
    font-size: 10px;
    letter-spacing: 0.08em;
    color: var(--ink-muted);
    white-space: nowrap;
}

/* Dashboard — three metric links in the header that filter the Open view.
   Replaces the old DB/Input legend. Count under label, both centered. */
.dashboard {
    display: flex;
    gap: 6px;
}
.dashboard .metric {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 2px;
    min-width: 72px;
    padding: 6px 14px 7px;
    text-decoration: none;
    color: var(--ink-muted);
    background: var(--surface);
    border: 1px solid var(--rule);
    transition: border-color 0.15s ease, color 0.15s ease, background 0.15s ease;
}
.dashboard .metric:hover {
    border-color: var(--focus);
    color: var(--ink);
}
.dashboard .metric.active {
    background: var(--ink);
    border-color: var(--ink);
    color: var(--surface);
    cursor: default;
}
.metric-label {
    font-family: 'JetBrains Mono', monospace;
    font-size: 9px;
    letter-spacing: 0.14em;
    text-transform: uppercase;
    line-height: 1;
}
.metric-value {
    font-family: 'Fraunces', serif;
    font-weight: 600;
    font-size: 20px;
    font-style: italic;
    line-height: 1;
    font-variant-numeric: tabular-nums;
}

/* ── View switcher tabs ─────────────────────────────────────────── */
.view-tabs {
    display: flex;
    gap: 2px;
    margin: 8px 0 0;
    border-bottom: 1px solid var(--rule);
    flex-shrink: 0;
}
.view-tab {
    font-family: 'Inter', sans-serif;
    font-size: 12px;
    font-weight: 500;
    letter-spacing: 0.02em;
    padding: 8px 18px;
    color: var(--ink-soft);
    text-decoration: none;
    border: 1px solid transparent;
    border-bottom: none;
    position: relative;
    bottom: -1px;
    transition: color 0.15s ease, background 0.15s ease;
}
.view-tab:hover { color: var(--ink); background: var(--surface); }
.view-tab.active {
    color: var(--ink);
    background: var(--surface);
    border-color: var(--rule);
}
.view-count {
    color: var(--ink-muted);
    font-weight: 500;
    font-size: 13px;
    margin-left: 4px;
}
/* Match the ratio-low red from the main grid so the Low Ratio tab's count
   signals the margin-review threshold at a glance. */
.view-tab[href*="filter=lowratio"] .view-count { color: #b91c1c; }

/* ── Toolbar ────────────────────────────────────────────────────── */
.toolbar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 10px 0;
    gap: 16px;
    flex-shrink: 0;
}

.search {
    flex: 1;
    max-width: 320px;
    position: relative;
}

.search input {
    width: 100%;
    padding: 7px 12px 7px 30px;
    font-family: 'Inter', sans-serif;
    font-size: 12px;
    background: var(--surface);
    border: 1px solid var(--rule);
    outline: none;
    transition: border-color 0.15s ease;
}

.search input:focus { border-color: var(--focus); }

.search::before {
    content: '⌕';
    position: absolute;
    left: 10px;
    top: 50%;
    transform: translateY(-50%);
    color: var(--ink-muted);
    font-size: 14px;
}

.btn-group { display: flex; gap: 8px; }

button {
    font-family: 'Inter', sans-serif;
    font-size: 12px;
    font-weight: 500;
    letter-spacing: 0.02em;
    padding: 7px 16px;
    border: 1px solid var(--ink);
    cursor: pointer;
    transition: all 0.15s ease;
    background: transparent;
    color: var(--ink);
}

button.primary {
    background: var(--ink);
    color: var(--surface);
}

button:hover { background: var(--accent); border-color: var(--accent); color: var(--surface); }

/* Disabled buttons shouldn't react to hover; semi-transparent and
   not-allowed cursor make the state obvious. */
button:disabled,
button:disabled:hover {
    opacity: 0.4;
    cursor: not-allowed;
    background: transparent;
    border-color: var(--rule);
    color: var(--ink);
}
button.primary:disabled,
button.primary:disabled:hover {
    background: var(--ink);
    border-color: var(--ink);
    color: var(--surface);
    opacity: 0.4;
}

/* ── Table Wrapper ──────────────────────────────────────────────── */
.table-wrap {
    flex: 1;
    overflow: auto;
    border: 1px solid var(--rule);
    background: var(--surface);
    box-shadow: 0 1px 0 rgba(0,0,0,0.02), 0 16px 32px -20px rgba(0,0,0,0.08);
    margin-bottom: 16px;
}

table {
    border-collapse: separate;
    border-spacing: 0;
    font-size: 13px;
    width: max-content;
    min-width: 100%;
}

/* ── Header row ─────────────────────────────────────────────────── */
thead th {
    position: sticky;
    top: 0;
    z-index: 3;
    background: var(--header-bg);
    color: var(--header-ink);
    font-family: 'JetBrains Mono', monospace;
    font-size: 10px;
    font-weight: 500;
    letter-spacing: 0.12em;
    text-transform: uppercase;
    padding: 10px 10px;
    text-align: left;
    white-space: nowrap;          /* ← key fix: headers always one line */
    border-right: 1px solid #2c2c2c;
    user-select: none;
}

thead th:last-child { border-right: none; }

/* Sortable column headers */
thead th { cursor: pointer; }
thead th.sort-asc::after  { content: ' ▲'; opacity: 0.85; font-size: 9px; margin-left: 2px; }
thead th.sort-desc::after { content: ' ▼'; opacity: 0.85; font-size: 9px; margin-left: 2px; }

thead th.db-col {
    background: #1c2630;
    color: #bccad9;
}

thead th .marker {
    display: inline-block;
    width: 4px;
    height: 4px;
    background: var(--accent);
    border-radius: 50%;
    margin-right: 6px;
    vertical-align: middle;
    opacity: 0.8;
}

/* Sticky first column (QC Number) for horizontal scroll context */
thead th.sticky-col,
tbody td.sticky-col {
    position: sticky;
    left: 0;
    z-index: 2;
}

thead th.sticky-col { z-index: 4; }

tbody td.sticky-col {
    background: var(--db-bg);
    box-shadow: 1px 0 0 var(--rule);
}

tbody tr:nth-child(even) td.sticky-col {
    background: var(--db-bg-alt);
}

/* ── Reconciled view: batch grouping ────────────────────────────── */
tr.batch-header > td {
    background: #1c2630;
    color: #bccad9;
    padding: 10px 14px;
    font-family: 'JetBrains Mono', monospace;
    font-size: 11px;
    letter-spacing: 0.08em;
    cursor: pointer;
    user-select: none;
    position: sticky;
    left: 0;
    border-bottom: 1px solid var(--rule);
}
tr.batch-header:hover > td { background: #243040; }
.batch-chevron {
    display: inline-block;
    margin-right: 10px;
    color: var(--focus);
    transition: transform 0.15s ease;
    width: 10px;
    text-align: center;
}
tbody.batch.collapsed .batch-chevron { transform: rotate(-90deg); }
tbody.batch.collapsed > tr.data-row { display: none; }

.batch-label { font-weight: 500; }
.batch-count {
    margin-left: 16px;
    color: #8a95a3;
    text-transform: uppercase;
    font-size: 10px;
    letter-spacing: 0.12em;
}
.batch-actions {
    float: right;
    display: inline-flex;
    gap: 8px;
}
.batch-actions button {
    font-family: 'Inter', sans-serif;
    font-size: 11px;
    padding: 3px 10px;
    border: 1px solid #4a5a66;
    background: transparent;
    color: #d6dde5;
    cursor: pointer;
    transition: background 0.15s, color 0.15s, border-color 0.15s;
}
.batch-actions button:hover {
    background: var(--focus);
    border-color: var(--focus);
    color: var(--surface);
}

/* Right-align money columns in reconciled read-only view */
tbody tr.data-row td.num {
    text-align: right;
    font-variant-numeric: tabular-nums;
    padding: 8px 10px;
}
tbody tr.data-row td { padding: 8px 10px; }

/* ── Body cells ─────────────────────────────────────────────────── */
/* content-visibility lets the browser skip rendering rows that aren't
   on screen — huge initial-paint win for ~950-row tables. The intrinsic
   size hint keeps scrollbar geometry sensible until each row paints. */
tbody tr {
    content-visibility: auto;
    contain-intrinsic-size: auto 33px;
}
tbody tr:nth-child(even) { background: var(--row-alt); }
tbody tr:hover { background: #dde7f0; }
tbody tr:hover td.sticky-col { background: #c2cfdb; }

tbody td {
    padding: 0;
    border-right: 1px solid var(--rule-soft);
    border-bottom: 1px solid var(--rule-soft);
    white-space: nowrap;
    vertical-align: middle;
}

tbody td.db-col {
    background: var(--db-bg);
    color: var(--db-ink);
    font-family: 'JetBrains Mono', monospace;
    font-size: 13px;
    padding: 8px 10px;
}

tbody tr:nth-child(even) td.db-col {
    background: var(--db-bg-alt);
}

/* Inputs inside cells */
tbody td input[type="text"],
tbody td input[type="date"],
tbody td input[type="number"],
tbody td select {
    font-family: 'Inter', sans-serif;
    font-size: 13px;
    color: var(--ink);
    background: transparent;
    border: none;
    outline: none;
    padding: 8px 10px;
    width: 100%;
    min-width: 90px;
}

tbody td input[type="number"] { text-align: right; font-variant-numeric: tabular-nums; }
tbody td input[type="date"]   { min-width: 130px; }

tbody td input:focus,
tbody td select:focus {
    background: #fff;
    box-shadow: inset 0 0 0 1.5px var(--focus);
}

tbody td select {
    appearance: none;
    background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='8' height='5' viewBox='0 0 8 5'><path d='M1 1l3 3 3-3' stroke='%238a8580' stroke-width='1.2' fill='none'/></svg>");
    background-repeat: no-repeat;
    background-position: right 8px center;
    padding-right: 22px;
    cursor: pointer;
}

tbody td select.yesno { min-width: 70px; }
tbody td input.notes { min-width: 180px; }

/* Column-specific widths */
.col-edit      { min-width: 32px; width: 32px; max-width: 32px; }
.col-qc        { min-width: 90px; }
.col-acct      { min-width: 100px; }
.col-vendor    { min-width: 110px; }
.col-date      { min-width: 130px; }
.col-aff       { min-width: 130px; }
.col-money     { min-width: 100px; }
.col-buid      { min-width: 80px; }
.col-inv       { min-width: 100px; }
.col-ratio     { min-width: 70px; }
.col-yesno     { min-width: 80px; }
.col-notes     { min-width: 200px; }

/* ── Add-Invoice dialog ─────────────────────────────────────────── */
dialog.add-dialog {
    border: 1px solid var(--ink);
    background: var(--bg);
    color: var(--ink);
    padding: 0;
    width: min(1012px, 95vw);
    max-height: 90vh;
    box-shadow: 0 24px 60px -20px rgba(0,0,0,0.35);
}
/* When a PDF is attached, dialog widens and lays out as [form | viewer]
   with the viewer getting most of the width so staff can actually read
   the invoice while verifying auto-filled fields. Form is fixed to 740px
   — enough breathing room for the 3- and 4-column fieldsets at the
   trimmed input widths (~110px). */
dialog.add-dialog.with-viewer {
    width: min(1620px, 97vw);
    display: grid;
    grid-template-columns: 740px minmax(0, 1fr);
    grid-template-rows: 90vh;
}
dialog.add-dialog.with-viewer > form {
    min-width: 0;
    display: flex;
    flex-direction: column;
}
dialog.add-dialog.with-viewer > form > .dialog-body {
    flex: 1 1 auto;
    max-height: none;   /* grid row cap handles height instead */
}

/* PDF viewer panel */
.pdf-viewer-panel {
    display: flex;
    flex-direction: column;
    border-left: 1px solid var(--rule);
    background: var(--surface);
    min-height: 0;
}
.pdf-viewer-head {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 10px;
    padding: 14px 16px;
    border-bottom: 1px solid var(--rule);
    background: var(--bg);
}
.pdf-viewer-head h3 {
    font-family: 'JetBrains Mono', monospace;
    font-size: 10px;
    letter-spacing: 0.14em;
    text-transform: uppercase;
    color: var(--ink-muted);
    margin: 0;
}
.pdf-viewer-filename {
    font-family: 'JetBrains Mono', monospace;
    font-size: 10px;
    color: var(--ink-muted);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.pdf-viewer-body { flex: 1 1 auto; overflow: hidden; min-height: 0; }
.pdf-viewer-body iframe { width: 100%; height: 100%; border: 0; display: block; }

/* Narrow screens — stack the viewer below the form instead of beside it.
   With a 740px form column, side-by-side needs at least ~1240px of width
   before the viewer gets too cramped to be useful. */
@media (max-width: 1240px) {
    dialog.add-dialog.with-viewer {
        grid-template-columns: 1fr;
        grid-template-rows: auto 50vh;
        width: min(1012px, 95vw);
    }
    .pdf-viewer-panel { border-left: 0; border-top: 1px solid var(--rule); }
}
dialog.add-dialog::backdrop { background: rgba(20, 18, 14, 0.45); }

.dialog-head, .dialog-foot {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 14px 20px;
    background: var(--surface);
}
.dialog-head { border-bottom: 1px solid var(--rule); }
.dialog-foot { border-top: 1px solid var(--rule); gap: 8px; }
.dialog-foot { justify-content: flex-end; }

.dialog-head h2 {
    font-family: 'Fraunces', serif;
    font-weight: 600;
    font-style: italic;
    font-size: 22px;
    letter-spacing: -0.01em;
}
.dialog-close {
    border: none;
    background: transparent;
    font-size: 22px;
    line-height: 1;
    cursor: pointer;
    padding: 0 6px;
    color: var(--ink-muted);
}
.dialog-close:hover { color: var(--accent); background: transparent; }

.dialog-body {
    padding: 16px 20px;
    overflow-y: auto;
    max-height: calc(90vh - 120px);
}

.dialog-body fieldset {
    border: 1px solid var(--rule);
    padding: 12px 14px 14px;
    margin-bottom: 14px;
    display: grid;
    /* minmax(0, 1fr) — otherwise 1fr won't shrink below the inputs'
       intrinsic width (default size=20 ≈ 180px) and the narrow form
       side gets a horizontal scrollbar when the PDF viewer is open. */
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 10px 14px;
}
.dialog-body fieldset.cols-4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.dialog-body legend {
    font-family: 'JetBrains Mono', monospace;
    font-size: 10px;
    letter-spacing: 0.14em;
    text-transform: uppercase;
    color: var(--ink-muted);
    padding: 0 6px;
}
.dialog-body label {
    display: flex;
    flex-direction: column;
    gap: 4px;
    font-size: 11px;
    color: var(--ink-soft);
    font-family: 'JetBrains Mono', monospace;
    letter-spacing: 0.04em;
    min-width: 0;   /* let the grid cell shrink below its content size */
}
.dialog-body label.span2 { grid-column: span 2; }
.dialog-body label.span3 { grid-column: span 3; }
.dialog-body label.row-start { grid-column-start: 1; }  /* force this label onto a new row */

.dialog-body input,
.dialog-body select {
    font-family: 'Inter', sans-serif;
    font-size: 13px;
    color: var(--ink);
    background: var(--surface);
    border: 1px solid var(--rule);
    outline: none;
    padding: 7px 10px;
    transition: border-color 0.15s ease;
    /* Short-value fields (identifiers, money, dates, Yes/No) are capped
       at 150px so they fill most of their grid cell without blowing out
       the narrow form side. The 4-column CRT fieldset's cells are ~157px
       at the current form width, so 150px fits snugly in all layouts. */
    width: 100%;
    max-width: 150px;
    min-width: 0;
    box-sizing: border-box;
}
/* Full-column width for the free-text fields that benefit from room:
   Affiliate (long business names), Notes (span-3 label), and the
   multi-match QC picker (long "branch | date | code | …" option labels). */
.dialog-body input[list="affiliate-list"],
.dialog-body label.span2 input,
.dialog-body label.span2 select,
.dialog-body label.span3 input,
.dialog-body label.span3 select,
.dialog-body #qcMatchPicker {
    max-width: 100%;
}
.dialog-body input:focus,
.dialog-body select:focus { border-color: var(--focus); }

/* Edit column — first column of every row, opens the full dialog. */
tbody td.edit-cell {
    text-align: center;
    padding: 2px;
    background: var(--surface);
}
tbody td.edit-cell .row-edit-btn {
    border: none;
    background: transparent;
    cursor: pointer;
    font-size: 13px;
    padding: 2px 4px;
    opacity: 0.55;
    line-height: 1;
}
tbody td.edit-cell .row-edit-btn:hover { opacity: 1; }

/* Yes/No columns — checkboxes centered in each cell. */
tbody td.yesno-cell {
    text-align: center;
    padding: 0;
}
tbody td.yesno-cell input[type="checkbox"] {
    cursor: pointer;
    width: 16px;
    height: 16px;
    margin: 6px;
    accent-color: var(--accent);
}

/* Edit mode on the dialog — hide the PDF drop zone only when a PDF
   is already attached. If the row has no pdf_path, we show the dropzone
   so staff can attach one after the fact. (Replacing an existing PDF
   is deliberately not supported here — would need a confirm-and-replace
   flow to avoid silent overwrite.) */
dialog.add-dialog.edit-mode.has-existing-pdf .pdf-dropzone { display: none; }

/* Refresh CRT Data button — only shown in Edit mode (new-row flow uses
   the automatic lookup on QC change). */
.refresh-crt-btn {
    display: none;
    margin-top: 6px;
    padding: 4px 10px;
    font-family: 'Inter', sans-serif;
    font-size: 11px;
    font-weight: 500;
    border: 1px solid var(--rule);
    background: var(--surface);
    color: var(--ink);
    cursor: pointer;
    align-self: flex-start;
    transition: background 0.15s ease, border-color 0.15s ease;
}
.refresh-crt-btn:hover { background: var(--focus); border-color: var(--focus); color: var(--surface); }
dialog.add-dialog.edit-mode .refresh-crt-btn { display: inline-block; }

/* Vendor inv # cell with attached-PDF link */
tbody td.vendor-inv-cell {
    display: flex;
    align-items: center;
    padding: 0;
}
tbody td.vendor-inv-cell input { flex: 1; min-width: 70px; }
tbody td.vendor-inv-cell .pdf-link {
    display: inline-block;
    padding: 0 8px;
    text-decoration: none;
    font-size: 13px;
    opacity: 0.7;
    transition: opacity 0.15s ease, transform 0.1s ease;
}
tbody td.vendor-inv-cell .pdf-link:hover { opacity: 1; transform: scale(1.15); }

/* Manage Affiliates dialog */
.affiliate-new {
    display: flex;
    flex-direction: column;
    gap: 10px;
    margin-bottom: 16px;
    padding: 12px;
    background: var(--surface);
    border: 1px solid var(--rule);
}
.affiliate-new .row-top    { display: grid; grid-template-columns: 1.5fr 2fr 1fr;              gap: 10px; }
.affiliate-new .row-bottom { display: grid; grid-template-columns: 0.8fr 0.8fr 0.8fr auto;     gap: 10px; }
.affiliate-new input,
.affiliate-new select {
    font-family: 'Inter', sans-serif;
    font-size: 12px;
    padding: 7px 10px;
    border: 1px solid var(--rule);
    background: #fff;
    outline: none;
    transition: border-color 0.15s ease;
}
.affiliate-new input:focus,
.affiliate-new select:focus { border-color: var(--focus); }
.affiliate-new button {
    font-family: 'Inter', sans-serif;
    font-size: 12px;
    font-weight: 500;
    padding: 7px 16px;
    border: 1px solid var(--ink);
    background: var(--ink);
    color: var(--surface);
    cursor: pointer;
    transition: background 0.15s, border-color 0.15s;
}
.affiliate-new button:hover { background: var(--accent); border-color: var(--accent); }

.affiliates-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 12px;
    margin-bottom: 14px;
}
.affiliates-table th {
    background: var(--surface);
    padding: 8px 10px;
    text-align: left;
    font-family: 'JetBrains Mono', monospace;
    font-size: 10px;
    text-transform: uppercase;
    letter-spacing: 0.12em;
    color: var(--ink-muted);
    border-bottom: 1px solid var(--rule);
}
.affiliates-table th.num     { text-align: right; }
.affiliates-table th.actions { text-align: right; }
.affiliates-table td {
    padding: 4px 6px;
    border-bottom: 1px solid var(--rule-soft);
    vertical-align: middle;
}
.affiliates-table td input,
.affiliates-table td select {
    width: 100%;
    font-family: 'Inter', sans-serif;
    font-size: 12px;
    padding: 5px 8px;
    border: 1px solid transparent;
    background: transparent;
    outline: none;
    transition: border-color 0.15s, background 0.15s;
}
.affiliates-table td input:focus,
.affiliates-table td select:focus { border-color: var(--focus); background: #fff; }
.affiliates-table td input.dirty,
.affiliates-table td select.dirty { background: #fff8e1; }

/* Dim rows marked Inactive */
.affiliates-table tr.inactive-row td input,
.affiliates-table tr.inactive-row td select { color: var(--ink-muted); }
.affiliates-table tr.inactive-row td.num    { color: var(--ink-muted); }
.affiliates-table td.num {
    text-align: right;
    font-variant-numeric: tabular-nums;
    color: var(--ink-soft);
    padding-right: 14px;
}
.affiliates-table td.actions { white-space: nowrap; text-align: right; }
.affiliates-table .affiliate-save,
.affiliates-table .affiliate-delete {
    font-family: 'Inter', sans-serif;
    font-size: 11px;
    padding: 3px 10px;
    border: 1px solid var(--ink);
    background: transparent;
    cursor: pointer;
    margin-left: 4px;
    transition: background 0.15s, color 0.15s, border-color 0.15s;
}
.affiliates-table .affiliate-save:hover,
.affiliates-table .affiliate-delete:hover:not(:disabled) {
    background: var(--accent);
    border-color: var(--accent);
    color: var(--surface);
}
.affiliates-table .affiliate-delete:disabled {
    opacity: 0.35;
    cursor: not-allowed;
}
.affiliates-loading { text-align: center; padding: 24px; color: var(--ink-muted); font-style: italic; }

.affiliates-note {
    font-family: 'Inter', sans-serif;
    font-size: 11px;
    color: var(--ink-muted);
    line-height: 1.5;
    padding: 10px 12px;
    border-left: 3px solid var(--rule);
    background: var(--bg);
}
.affiliates-note code {
    font-family: 'JetBrains Mono', monospace;
    font-size: 10px;
    background: var(--surface);
    padding: 0 4px;
}

/* Export History dialog */
.history-table {
    width: 100%;
    border-collapse: collapse;
    font-family: 'JetBrains Mono', monospace;
    font-size: 12px;
    margin-bottom: 14px;
}
.history-table th,
.history-table td {
    padding: 8px 12px;
    border-bottom: 1px solid var(--rule-soft);
    text-align: left;
}
.history-table th {
    font-size: 10px;
    letter-spacing: 0.12em;
    text-transform: uppercase;
    color: var(--ink-muted);
    border-bottom: 1px solid var(--rule);
}
.history-table td.history-count { text-align: right; color: var(--ink-soft); }
.history-table td.history-actions { text-align: right; white-space: nowrap; }
.history-table .reexport-batch,
.history-table .undo-batch {
    font-family: 'Inter', sans-serif;
    font-size: 11px;
    padding: 4px 12px;
    border: 1px solid var(--ink);
    background: transparent;
    cursor: pointer;
    margin-left: 6px;
}
.history-table .reexport-batch:hover,
.history-table .undo-batch:hover { background: var(--accent); color: var(--surface); border-color: var(--accent); }
.history-table .reexport-batch[disabled] { opacity: 0.5; cursor: not-allowed; }
.history-note {
    font-family: 'Inter', sans-serif;
    font-size: 11px;
    color: var(--ink-muted);
    line-height: 1.5;
    padding: 10px 12px;
    border-left: 3px solid var(--rule);
    background: var(--bg);
}
.history-note code {
    font-family: 'JetBrains Mono', monospace;
    font-size: 10px;
    background: var(--surface);
    padding: 0 4px;
}

/* PDF drop zone in the AFFILIATE section */
.pdf-dropzone {
    margin-top: 6px;
    padding: 18px 14px;
    border: 1.5px dashed var(--rule);
    background: var(--surface);
    text-align: center;
    cursor: pointer;
    transition: border-color 0.15s ease, background 0.15s ease;
    font-family: 'JetBrains Mono', monospace;
    font-size: 11px;
    color: var(--ink-muted);
    letter-spacing: 0.04em;
}
.pdf-dropzone:hover,
.pdf-dropzone.drag-over {
    border-color: var(--focus);
    background: #e6eef9;
    color: var(--ink);
}
.pdf-dropzone .pdf-icon { font-size: 16px; margin-right: 8px; vertical-align: -2px; }
.pdf-chosen {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 10px;
    color: var(--ink);
    font-size: 12px;
}
.pdf-chosen .pdf-size { color: var(--ink-muted); font-size: 10px; }
.pdf-remove {
    border: 1px solid var(--rule);
    background: var(--bg);
    color: var(--ink-muted);
    font-size: 14px;
    line-height: 1;
    cursor: pointer;
    padding: 1px 7px 3px;
    margin-left: 4px;
}
.pdf-remove:hover { background: var(--accent); color: var(--surface); border-color: var(--accent); }

/* Auto-fill button (on-demand mode only) */
.pdf-autofill-btn {
    border: 1px solid var(--focus);
    background: var(--focus);
    color: var(--surface);
    font-family: 'Inter', sans-serif;
    font-size: 12px;
    padding: 4px 10px;
    cursor: pointer;
    letter-spacing: 0.02em;
}
.pdf-autofill-btn:hover { background: var(--ink); border-color: var(--ink); }

/* Parsing state on the dropzone */
.pdf-dropzone.parsing {
    border-color: var(--focus);
    background: #eef3fb;
    color: var(--focus);
    animation: parsingPulse 1.2s ease-in-out infinite;
}
@keyframes parsingPulse { 0%,100% { opacity: 0.9; } 50% { opacity: 0.6; } }

/* Toast-style confidence summary under the Affiliate fieldset — the Affiliate
   fieldset is a 3-column grid, so span the full row instead of sitting in
   the first column and pushing the layout. */
.parse-summary {
    grid-column: 1 / -1;
    margin: 6px 0 10px;
    padding: 8px 12px;
    border-left: 3px solid var(--focus);
    background: #eef3fb;
    font-size: 11px;
    color: var(--ink);
    line-height: 1.4;
    font-family: 'JetBrains Mono', monospace;
    letter-spacing: 0.02em;
}
.parse-summary.warn   { border-left-color: #c88a2a; background: #fbf3e3; }
.parse-summary.err    { border-left-color: var(--accent); background: #fbe9e4; }
.parse-summary .notes { color: var(--ink-muted); display: block; margin-top: 4px; font-style: italic; }

/* Shared-QC badge in the Ratio cell — flags rows whose ratio has been
   pro-rated across sibling invoices on the same QC. Subtle but visible. */
tbody td.ratio-cell { white-space: nowrap; }

/* Red Ratio values below 3:1 — accounting threshold for margin review. */
tbody td.ratio-cell.ratio-low { color: #b91c1c; font-weight: 600; }

/* Dialog delete button — destructive, pushed to the far left so it's
   visually separated from Cancel / Save on the right. */
.dialog-foot button.danger {
    background: #b91c1c;
    border: 1px solid #b91c1c;
    color: var(--surface);
    margin-right: auto;    /* flex-end default on .dialog-foot */
}
.dialog-foot button.danger:hover {
    background: #991818;
    border-color: #991818;
}
.dialog-foot button.danger:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

/* Affiliate input + "+" button for quick-create flow. Wraps the input and
   the add-button in a flex row inside the label without disturbing the
   fieldset's grid cell. */
.input-with-add {
    display: flex;
    gap: 4px;
    align-items: stretch;
    min-width: 0;
}
.input-with-add input {
    flex: 1 1 auto;
    min-width: 0;
    max-width: none !important;  /* beat the generic .dialog-body input max-width: 150 */
}
.affiliate-add-btn {
    flex: 0 0 auto;
    padding: 0 10px;
    background: var(--surface);
    border: 1px solid var(--rule);
    color: var(--ink);
    font-family: 'Inter', sans-serif;
    font-size: 13px;
    font-weight: 600;
    line-height: 1;
    cursor: pointer;
    white-space: nowrap;
}
.affiliate-add-btn:hover {
    background: var(--focus);
    color: var(--surface);
    border-color: var(--focus);
}

/* Narrower sub-dialog for the quick-create form. */
dialog.quick-affiliate-dialog { width: min(560px, 95vw); }
.quick-affiliate-dialog .banner.error { margin: 0 0 10px; }

/* ── Batch Add dialog ───────────────────────────────────────────────────── */
dialog.batch-dialog {
    /* Big dialog for a wide review grid + side PDF viewer.
       NOTE: no `display:` rule on the base selector — the UA stylesheet
       hides a closed dialog via `dialog:not([open]) { display: none }`,
       and setting display here would clobber that and make it always
       visible. We only override display when [open]. */
    width: min(1600px, 97vw);
    max-width: 97vw;
    padding: 0;
}
/* When open, the dialog is a vertical flex container so the header stays
   pinned at the top, footer at the bottom, and the active content section
   fills the middle and manages its own scroll — no more footer-overlaps-
   content when the review grid is tall. */
dialog.batch-dialog[open] {
    display: flex;
    flex-direction: column;
    max-height: 90vh;
}
.batch-dialog > header.dialog-head,
.batch-dialog > footer.batch-foot { flex: 0 0 auto; }

/* Content sections are hidden by default; the state machine turns the
   active one on with the right display mode AND flex-growth so it fills
   the space between header and footer. */
.batch-dialog .batch-setup,
.batch-dialog .batch-parsing,
.batch-dialog .batch-review { display: none; }
.batch-dialog .batch-setup {
    display: block;
    flex: 1 1 auto;
    min-height: 0;
    overflow-y: auto;
    padding: 20px 24px;
}
.batch-dialog.parsing .batch-setup { display: none; }
.batch-dialog.parsing .batch-parsing {
    display: block;
    flex: 1 1 auto;
    min-height: 0;
    overflow-y: auto;
    padding: 20px 24px;
}
.batch-dialog.reviewing .batch-setup { display: none; }
.batch-dialog.reviewing .batch-review {
    display: grid;
    grid-template-columns: minmax(0, 1fr) 680px;   /* wider PDF side than the initial 520px */
    gap: 16px;
    padding: 16px 20px;
    flex: 1 1 auto;
    min-height: 0;
    overflow: hidden;   /* grid-wrap and the iframe handle their own scroll */
}
.batch-dialog.parsing .batch-foot .primary,
.batch-dialog.parsing .batch-foot #batchCancelBtn { visibility: hidden; }

/* Step 1 — vendor + drop zone */
.batch-vendor-row {
    display: flex;
    flex-direction: column;
    gap: 6px;
    margin-bottom: 14px;
}
.batch-vendor-row > span {
    font-family: 'JetBrains Mono', monospace;
    font-size: 11px;
    letter-spacing: 0.04em;
    color: var(--ink-soft);
}
.batch-vendor-row > span small { color: var(--ink-muted); font-size: 10px; }
.batch-vendor-row input {
    font-family: 'Inter', sans-serif;
    font-size: 14px;
    padding: 9px 12px;
    border: 1px solid var(--rule);
    background: var(--surface);
    color: var(--ink);
    outline: none;
}
.batch-vendor-row input:focus { border-color: var(--focus); }

.batch-dropzone {
    padding: 36px 20px;
    border: 2px dashed var(--rule);
    background: var(--surface);
    text-align: center;
    cursor: pointer;
    font-family: 'JetBrains Mono', monospace;
    font-size: 12px;
    color: var(--ink-muted);
    letter-spacing: 0.04em;
    transition: border-color 0.15s ease, background 0.15s ease;
}
.batch-dropzone:hover,
.batch-dropzone.drag-over {
    border-color: var(--focus);
    background: #eef3fb;
    color: var(--ink);
}
.batch-dropzone .pdf-icon { font-size: 22px; margin-right: 10px; vertical-align: -3px; }

/* Step 2 — parsing progress */
.batch-progress-row {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    font-family: 'JetBrains Mono', monospace;
    font-size: 12px;
    color: var(--ink);
    margin-bottom: 8px;
}
.batch-cost { color: var(--ink-muted); font-size: 11px; }
.batch-progress-track {
    height: 8px;
    background: var(--surface);
    border: 1px solid var(--rule);
    overflow: hidden;
}
.batch-progress-bar {
    height: 100%;
    background: var(--focus);
    width: 0%;
    transition: width 0.2s ease;
}
.batch-progress-note {
    margin-top: 10px;
    font-size: 10px;
    color: var(--ink-muted);
    font-style: italic;
}

/* Step 3 — review grid + side PDF viewer */
.batch-review-grid-wrap { overflow: auto; border: 1px solid var(--rule); min-width: 0; }
.batch-review-grid { width: 100%; border-collapse: collapse; font-size: 11px; }
.batch-review-grid thead th {
    position: sticky;
    top: 0;
    z-index: 1;
    background: var(--header-bg);
    color: var(--surface);
    text-align: left;
    padding: 8px 6px;
    font-family: 'JetBrains Mono', monospace;
    font-size: 9px;
    letter-spacing: 0.1em;
    text-transform: uppercase;
    white-space: nowrap;
}
.batch-review-grid tbody tr { border-top: 1px solid var(--rule); }
.batch-review-grid tbody tr.selected { background: #eef3fb; }
.batch-review-grid tbody tr.excluded { opacity: 0.45; }
.batch-review-grid tbody tr.duplicate td.flags { color: #b91c1c; font-weight: 600; }
.batch-review-grid td { padding: 4px 6px; vertical-align: middle; }
.batch-review-grid td.file {
    font-family: 'JetBrains Mono', monospace;
    font-size: 10px;
    color: var(--ink-soft);
    max-width: 170px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    cursor: pointer;
}
.batch-review-grid td.file:hover { color: var(--focus); text-decoration: underline; }
.batch-review-grid td input[type="text"],
.batch-review-grid td input[type="date"],
.batch-review-grid td input[type="number"] {
    width: 100%;
    min-width: 60px;
    font-size: 11px;
    padding: 3px 5px;
    border: 1px solid transparent;
    background: transparent;
    color: var(--ink);
    outline: none;
    box-sizing: border-box;
}
.batch-review-grid td input:focus { border-color: var(--focus); background: var(--surface); }
.batch-review-grid td.readonly input { color: var(--ink-muted); cursor: not-allowed; }
.batch-review-grid td.ratio { font-variant-numeric: tabular-nums; }
.batch-review-grid td.ratio.ratio-low { color: #b91c1c; font-weight: 600; }
.batch-review-grid td.flags { font-size: 10px; }
.batch-review-grid td.billed-cell { text-align: center; }
.batch-review-grid th.billed-head {
    white-space: nowrap;
    text-align: center;
}
.batch-review-grid th.billed-head input { margin-right: 4px; vertical-align: -1px; }

/* Notes column — wider by default; header carries a replicate input + button. */
.batch-review-grid th.notes-head { min-width: 220px; }
.batch-review-grid td.notes-cell input { min-width: 180px; }
.notes-replicate-row {
    display: flex;
    gap: 4px;
    margin-top: 4px;
    /* Override the uppercase/letterspacing thead style for the input + button. */
    text-transform: none;
    letter-spacing: 0;
}
.notes-replicate-row input {
    flex: 1 1 auto;
    min-width: 0;
    font-family: 'Inter', sans-serif;
    font-size: 11px;
    font-weight: 400;
    padding: 3px 6px;
    border: 1px solid var(--rule);
    background: var(--bg);
    color: var(--ink);
    outline: none;
}
.notes-replicate-row input:focus { border-color: var(--focus); }
.notes-replicate-row button {
    flex: 0 0 auto;
    font-family: 'Inter', sans-serif;
    font-size: 10px;
    font-weight: 500;
    padding: 2px 8px;
    background: var(--bg);
    border: 1px solid var(--rule);
    color: var(--ink);
    cursor: pointer;
    white-space: nowrap;
}
.notes-replicate-row button:hover {
    background: var(--focus);
    border-color: var(--focus);
    color: var(--surface);
}

.batch-review-grid td.flags .f-dup { color: #b91c1c; }
.batch-review-grid td.flags .f-conf-low { color: #b91c1c; }
.batch-review-grid td.flags .f-conf-med { color: #c88a2a; }
.batch-review-grid td.flags .f-no-crt { color: #c88a2a; }
.batch-review-grid td.include { text-align: center; }

.batch-pdf-side {
    display: flex;
    flex-direction: column;
    border: 1px solid var(--rule);
    background: var(--surface);
    min-height: 0;
}
.batch-pdf-head {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 10px;
    padding: 10px 14px;
    border-bottom: 1px solid var(--rule);
    background: var(--bg);
    font-family: 'JetBrains Mono', monospace;
    font-size: 10px;
    letter-spacing: 0.1em;
    text-transform: uppercase;
}
.batch-pdf-head-label { color: var(--ink-muted); }
.batch-pdf-head-name {
    text-transform: none;
    letter-spacing: 0;
    color: var(--ink-soft);
    font-size: 10px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.batch-pdf-side iframe { flex: 1 1 auto; width: 100%; border: 0; }

.batch-foot { padding: 12px 20px; display: flex; align-items: center; gap: 10px; }
.batch-foot .spacer { flex: 1; }
.batch-foot-summary {
    font-family: 'JetBrains Mono', monospace;
    font-size: 11px;
    color: var(--ink-muted);
}
.shared-qc-badge {
    display: inline-block;
    margin-left: 4px;
    font-size: 11px;
    cursor: help;
    opacity: 0.75;
    vertical-align: 1px;
}
.shared-qc-badge:hover { opacity: 1; }

/* Field-level confidence hints — subtle left-tint on auto-filled inputs */
.dialog-body input.conf-high   { box-shadow: inset 3px 0 0 #4a7a3f; }
.dialog-body input.conf-medium { box-shadow: inset 3px 0 0 #c88a2a; }
.dialog-body input.conf-low    { box-shadow: inset 3px 0 0 var(--accent); }
/* Clear hint as soon as the user edits the field — they've taken ownership. */
.dialog-body input.conf-dirty  { box-shadow: none; }

/* Readonly fields in the dialog (locked until QC lookup runs) */
.dialog-body input[readonly] {
    background: #e6ebf2;
    color: var(--ink-muted);
    cursor: not-allowed;
}
.dialog-body input[readonly]:focus { border-color: var(--rule); }

/* Status pill below the QC input */
.lookup-status {
    margin-top: 4px;
    font-size: 10px;
    letter-spacing: 0.06em;
    text-transform: uppercase;
    min-height: 12px;
}
.lookup-status.ok   { color: #4a5a32; }
.lookup-status.warn { color: #8b2d1f; }

/* Multi-match picker shown under the QC input when more than one order
   matches the entered QC Number. */
.qc-match-picker {
    margin-top: 4px;
    width: 100%;
    font-family: 'JetBrains Mono', monospace;
    font-size: 11px;
    padding: 5px 8px;
    border: 1px solid var(--focus);
    background: #fff8e1;
    color: var(--ink);
    cursor: pointer;
}
.qc-match-picker:focus { outline: 1px solid var(--focus); }

/* Just-added row flash */
@keyframes flash-row {
    0%   { background: #fff6c4; }
    100% { background: transparent; }
}
tbody tr.flash > td { animation: flash-row 2.5s ease-out; }

/* Dirty row indicator — thin accent stripe on the sticky first column */
tbody tr[data-dirty] td.sticky-col {
    box-shadow: inset 3px 0 0 var(--accent), 1px 0 0 var(--rule);
}

/* Status banner */
.banner {
    margin: 10px 0 0;
    padding: 8px 14px;
    font-family: 'JetBrains Mono', monospace;
    font-size: 11px;
    letter-spacing: 0.05em;
    border: 1px solid var(--rule);
    background: var(--surface);
}
.banner.ok    { border-color: #6a7a4a; color: #4a5a32; background: #eef0e0; }
.banner.error { border-color: #b53d2f; color: #8b2d1f;        background: #fbe4e3; }

/* Custom scrollbar */
.table-wrap::-webkit-scrollbar { height: 12px; width: 12px; }
.table-wrap::-webkit-scrollbar-track { background: var(--bg); }
.table-wrap::-webkit-scrollbar-thumb {
    background: var(--rule);
    border: 2px solid var(--bg);
    border-radius: 6px;
}
.table-wrap::-webkit-scrollbar-thumb:hover { background: var(--ink-muted); }
