/* Invoice Management (staff) — see public/invoices.js. Uses the portal's own
   tokens from index.html (:root): --indigo is the brand red, --border,
   --text-muted, etc. Everything here is prefixed inv- so nothing leaks into
   the other screens. */

/* The invoice table needs more room than the 880px every other screen uses. */
main:has(#screen-invoices.active) { max-width: 1360px; }

.inv-card { padding: 28px 28px 32px; }
.inv-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 16px; flex-wrap: wrap; }
.inv-head .subtitle { margin-bottom: 22px; }

.inv-views { display: inline-flex; background: var(--bg); border: 1px solid var(--border); border-radius: 10px; padding: 3px; }
.inv-view-btn {
  border: 0; background: transparent; font: 600 13px 'Inter', sans-serif; color: var(--text-muted);
  padding: 8px 16px; border-radius: 8px; cursor: pointer;
}
.inv-view-btn.active { background: var(--surface); color: var(--text); box-shadow: 0 1px 3px rgba(16,26,51,0.08); }

/* --- Drop zone --------------------------------------------------------- */
.inv-drop {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 6px;
  border: 1.5px dashed #D9C9CA; border-radius: 14px; background: var(--bg);
  padding: 34px 20px; text-align: center; cursor: pointer; color: var(--text-muted);
  transition: border-color .15s, background .15s; margin: 0;
}
.inv-drop:hover, .inv-drop:focus-visible, .inv-drop.dragging { border-color: var(--indigo); background: var(--indigo-light); color: var(--indigo); outline: none; }
.inv-drop-title { font-weight: 600; font-size: 15px; color: var(--text); }
.inv-drop-sub { font-size: 12.5px; font-weight: 400; }
.inv-drop-link { color: var(--indigo); font-weight: 600; text-decoration: underline; }

/* --- Upload jobs ------------------------------------------------------- */
.inv-job { border: 1px solid var(--border); border-radius: 14px; margin-top: 18px; overflow: hidden; }
.inv-job-head { display: flex; align-items: center; gap: 12px; padding: 14px 16px; flex-wrap: wrap; }
.inv-job-name { font-weight: 600; font-size: 14px; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 420px; }
.inv-job-status { font-size: 12.5px; color: var(--text-muted); }
.inv-job-counts { display: flex; gap: 6px; flex-wrap: wrap; margin-left: auto; }
.inv-progress { height: 4px; background: var(--border); }
.inv-progress > span { display: block; height: 100%; background: var(--indigo); transition: width .3s ease; }
.inv-job.done .inv-progress > span { background: var(--success); }
.inv-job.failed .inv-progress > span { background: #DC2626; }
.inv-job-error { padding: 0 16px 14px; }
.inv-job-body { border-top: 1px solid var(--border); }

/* --- Pills, badges ----------------------------------------------------- */
.inv-pill {
  display: inline-flex; align-items: center; gap: 4px; font-size: 11px; font-weight: 600;
  padding: 3px 9px; border-radius: 20px; white-space: nowrap; background: var(--bg); color: var(--text-muted);
}
.inv-pill-good { background: var(--success-light); color: #15803D; }
.inv-pill-bad { background: #FEF2F2; color: #B91C1C; }
.inv-pill-warn { background: var(--amber-light); color: #B45309; }
.inv-pill-dup { background: #B91C1C; color: #fff; }

/* --- Filters ----------------------------------------------------------- */
.inv-filters { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; margin-bottom: 12px; }
.inv-filters select { width: auto; min-width: 150px; padding: 9px 12px; font-size: 13px; }
.inv-search { position: relative; flex: 1 1 280px; }
.inv-search svg { position: absolute; left: 12px; top: 50%; transform: translateY(-50%); color: var(--text-dim); }
.inv-search input[type="text"] { padding: 9px 12px 9px 34px; font-size: 13px; }
.inv-check { display: inline-flex; align-items: center; gap: 6px; font-size: 13px; font-weight: 500; margin: 0; cursor: pointer; white-space: nowrap; }
.inv-summary { font-size: 12.5px; color: var(--text-muted); margin: 4px 0 10px; min-height: 18px; }

/* --- Table ------------------------------------------------------------- */
.inv-table-wrap { overflow-x: auto; }
table.inv-table { width: 100%; border-collapse: collapse; font-size: 12.8px; }
table.inv-table th {
  text-align: left; font-size: 10.5px; text-transform: uppercase; letter-spacing: 0.04em; font-weight: 600;
  color: var(--text-dim); padding: 9px 10px; border-bottom: 1.5px solid var(--border); white-space: nowrap; background: var(--surface);
}
table.inv-table td { padding: 10px; border-bottom: 1px solid var(--border); vertical-align: middle; white-space: nowrap; }
table.inv-table td.num, table.inv-table th.num { text-align: right; font-variant-numeric: tabular-nums; }
table.inv-table tbody tr.inv-row { cursor: pointer; }
table.inv-table tbody tr.inv-row:hover td { background: var(--bg); }
table.inv-table tbody tr.inv-row:focus-visible td { background: var(--indigo-light); outline: none; }
.inv-client { max-width: 240px; overflow: hidden; text-overflow: ellipsis; }
.inv-client-sub { display: block; font-size: 11px; color: var(--text-dim); margin-top: 1px; }
.inv-missing { color: #B91C1C; font-style: italic; }
/* A cell the reader flagged: red outline, reason on hover (title). */
td.inv-flag { box-shadow: inset 0 0 0 1.5px #F2A7A7; background: #FFF7F7; }
tr.inv-row.has-issues td:first-child { box-shadow: inset 3px 0 0 #DC2626; }
tr.inv-row-unsaved td { background: #FFF5F5; cursor: default; }
tr.inv-row-unsaved td:first-child { box-shadow: inset 3px 0 0 #B91C1C; }
tr.inv-note-row td { background: #FFF5F5; color: #991B1B; font-size: 12px; padding-top: 0; white-space: normal; }
/* Actions stay in view however far the table scrolls sideways. */
table.inv-table th:last-child, table.inv-table td:last-child {
  position: sticky; right: 0; background: var(--surface); box-shadow: -8px 0 8px -8px rgba(32,23,23,0.18);
}
table.inv-table tbody tr.inv-row:hover td:last-child { background: var(--bg); }
tr.inv-row-unsaved td:last-child { background: #FFF5F5; }
tr.inv-note-row td:last-child { position: static; box-shadow: none; }
.inv-actions { display: flex; gap: 6px; justify-content: flex-end; }
.inv-actions .icon-btn { width: 28px; height: 28px; }
.inv-empty { text-align: center; color: var(--text-muted); padding: 28px 12px; font-size: 13.5px; }

/* --- Edit drawer ------------------------------------------------------- */
.inv-drawer-overlay {
  position: fixed; inset: 0; z-index: 120; background: rgba(16,26,51,0.45);
  display: none; justify-content: flex-end;
}
.inv-drawer-overlay.open { display: flex; }
.inv-drawer {
  background: var(--surface); width: min(1180px, 100%); height: 100%;
  display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 520px); grid-template-rows: auto minmax(0, 1fr) auto;
  box-shadow: -12px 0 40px rgba(16,26,51,0.2); animation: invSlide .25s cubic-bezier(0.16,1,0.3,1) both;
}
@keyframes invSlide { from { transform: translateX(40px); opacity: 0; } to { transform: none; opacity: 1; } }
.inv-drawer-head {
  grid-column: 1 / -1; display: flex; align-items: center; gap: 12px; padding: 16px 22px;
  border-bottom: 1px solid var(--border);
}
.inv-drawer-title { font: 600 18px 'Space Grotesk', sans-serif; margin: 0; }
.inv-drawer-head .inv-pill { margin-left: 2px; }
.inv-drawer-close { margin-left: auto; }
.inv-drawer-pdf { background: #F3F0EF; border-right: 1px solid var(--border); min-height: 0; display: flex; }
.inv-drawer-pdf iframe { border: 0; width: 100%; height: 100%; }
.inv-drawer-pdf .inv-empty { margin: auto; }
.inv-form { overflow-y: auto; padding: 18px 22px 24px; min-height: 0; }
.inv-drawer-foot { position: relative; grid-column: 1 / -1; display: flex; gap: 10px; padding: 14px 22px; border-top: 1px solid var(--border); align-items: center; }
.inv-drawer-foot .spacer { flex: 1; }
.inv-drawer-foot .inv-save-status { font-size: 12.5px; color: var(--text-muted); }

.inv-section { margin-bottom: 20px; }
.inv-section-title { font-size: 11px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--text-dim); font-weight: 600; margin: 0 0 10px; }
.inv-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px 14px; }
.inv-grid .full { grid-column: 1 / -1; }
.inv-field label { font-size: 12px; margin-bottom: 5px; }
.inv-field input[type="text"], .inv-field input[type="date"], .inv-field select { padding: 9px 12px; font-size: 13.5px; }
.inv-field input[readonly] { background: #F4F1F0; color: var(--text-muted); cursor: default; }
.inv-field.flagged input, .inv-field.flagged select { border-color: #EF4444; background: #FFF7F7; }
.inv-field-note { font-size: 11.5px; color: #B91C1C; margin-top: 4px; }
.inv-field-hint { font-size: 11.5px; color: var(--text-dim); margin-top: 4px; }
.inv-derived { display: flex; gap: 10px; align-items: center; padding: 10px 12px; background: var(--bg); border-radius: 10px; font-size: 13px; flex-wrap: wrap; }
.inv-derived strong { font-variant-numeric: tabular-nums; }
.inv-issues { border: 1px solid #FECACA; background: #FEF2F2; color: #991B1B; border-radius: 10px; padding: 10px 14px; margin-bottom: 16px; font-size: 12.8px; }
.inv-issues ul { margin: 6px 0 0; padding-left: 18px; }
.inv-issues li { margin: 2px 0; }
.inv-issues.clear { border-color: #BBF7D0; background: var(--success-light); color: #15803D; }
.inv-links { display: flex; flex-direction: column; gap: 4px; font-size: 12.5px; }
.inv-links a { color: var(--indigo); word-break: break-all; }
.inv-links span { color: var(--text-muted); word-break: break-all; }

/* --- Search dropdown (client + staff pickers) -------------------------- */
.inv-combo { position: relative; }
.inv-combo-input-wrap { position: relative; }
.inv-combo-input-wrap svg { position: absolute; left: 11px; top: 50%; transform: translateY(-50%); color: var(--text-dim); pointer-events: none; }
.inv-combo-input-wrap input { padding-left: 32px !important; padding-right: 30px !important; }
.inv-combo-clear {
  position: absolute; right: 6px; top: 50%; transform: translateY(-50%); border: 0; background: transparent;
  color: var(--text-dim); cursor: pointer; font-size: 16px; line-height: 1; padding: 4px 6px; border-radius: 6px;
}
.inv-combo-clear:hover { color: var(--indigo); background: var(--indigo-light); }
.inv-combo-list {
  position: absolute; left: 0; top: calc(100% + 6px); z-index: 20;
  width: max(100%, 340px); max-width: calc(100vw - 32px);
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px;
  box-shadow: 0 12px 32px rgba(16,26,51,0.14); padding: 6px; max-height: 320px; overflow-y: auto;
}
.inv-combo-head { font-size: 11.5px; color: var(--text-muted); padding: 6px 8px 4px; display: flex; gap: 6px; align-items: center; }
.inv-combo-head .count { color: var(--indigo); font-weight: 600; }
.inv-combo-item { display: flex; gap: 10px; align-items: center; padding: 8px; border-radius: 8px; cursor: pointer; }
.inv-combo-item[aria-selected="true"], .inv-combo-item:hover { background: var(--bg); }
.inv-combo-avatar {
  width: 30px; height: 30px; border-radius: 50%; flex-shrink: 0; display: flex; align-items: center; justify-content: center;
  background: var(--client-avatar-bg); color: var(--client-avatar-text); font: 600 11.5px 'Space Grotesk', sans-serif;
}
.inv-combo-main { min-width: 0; flex: 1; }
.inv-combo-name { font-size: 13.5px; font-weight: 500; color: var(--text); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.inv-combo-name mark { background: none; color: var(--indigo); font-weight: 700; }
.inv-combo-tag { color: var(--text-dim); font-weight: 400; margin-left: 6px; }
.inv-combo-meta { font-size: 11.5px; color: var(--text-dim); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.inv-combo-empty { padding: 12px 8px; font-size: 12.5px; color: var(--text-muted); }
/* A dropdown in the right-hand column opens leftwards so it stays on screen. */
.inv-grid > .inv-combo:nth-child(even) .inv-combo-list { left: auto; right: 0; }
.inv-picked {
  display: flex; gap: 10px; align-items: center; padding: 10px 12px; border: 1px solid var(--border);
  border-radius: 10px; background: var(--bg); margin-top: 8px;
}
.inv-picked .inv-combo-meta { white-space: normal; }

/* --- Totals cards (All Invoices) ---------------------------------------- */
.inv-totals { margin: 6px 0 14px; transition: opacity .15s; }
.inv-totals:empty { display: none; }
.inv-totals.loading { opacity: .45; }
.inv-totals-scope { font-size: 12.5px; color: var(--text-muted); margin-bottom: 8px; }
.inv-totals-scope strong { color: var(--text); font-weight: 600; }
.inv-totals-grid { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 12px; }
.inv-total {
  background: var(--bg); border: 1px solid var(--border); border-radius: 12px; padding: 14px 16px;
  border-top: 3px solid var(--border); min-width: 0;
}
.inv-total-label { font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: .04em; color: var(--text-dim); }
.inv-total-value {
  font: 600 22px 'Space Grotesk', 'Inter', sans-serif; font-variant-numeric: tabular-nums;
  /* Never cut a figure off with "…": a long amount wraps instead. */
  margin: 4px 0 2px; overflow-wrap: anywhere;
}
.inv-total-sub { font-size: 12px; color: var(--text-muted); }
.inv-total-revenue { border-top-color: var(--indigo); }
.inv-total-gst { border-top-color: #64748B; }
.inv-total-up { border-top-color: var(--success); }
.inv-total-up .inv-total-value { color: #15803D; }
.inv-total-off { border-top-color: #DC2626; }
.inv-total-off .inv-total-value { color: #B91C1C; }
.inv-total.is-zero .inv-total-value { color: var(--text-muted); }
@media (max-width: 900px) { .inv-totals-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); } }
@media (max-width: 480px) { .inv-total { padding: 12px; } .inv-total-value { font-size: 19px; } }

/* --- Small screens ----------------------------------------------------- */
@media (max-width: 900px) {
  .inv-drawer { grid-template-columns: minmax(0, 1fr); grid-template-rows: auto minmax(0, 1fr) auto; }
  .inv-drawer-pdf { display: none; }
}
@media (max-width: 640px) {
  .inv-card { padding: 20px 16px 24px; }
  .inv-grid { grid-template-columns: minmax(0, 1fr); }
  .inv-filters select { flex: 1 1 140px; min-width: 0; }
  .inv-job-name { max-width: 100%; }
  .inv-drawer-head, .inv-form, .inv-drawer-foot { padding-left: 16px; padding-right: 16px; }
  .inv-drawer-head { gap: 8px; }
  .inv-drawer-title { font-size: 16px; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  .inv-drawer-head .btn, .inv-drawer-foot .btn { white-space: nowrap; padding: 8px 12px; }
  .inv-drawer-foot { gap: 8px; }
  .inv-drawer-foot .inv-save-status { display: none; }
  .inv-drawer-foot .inv-save-status:not(:empty) { display: block; position: absolute; bottom: 64px; left: 16px; right: 16px; background: #FEF2F2; color: #B91C1C; padding: 8px 10px; border-radius: 8px; }
}

/* Shown only when the server runs with INVOICE_PREVIEW=1. */
.alert.inv-preview-banner { background: var(--amber-light); color: #92400E; border: 1px solid #FCD34D; display: block; }

/* Keap Contacts fields: shown, never edited — they change with the client. */
.inv-section-note { text-transform: none; letter-spacing: 0; font-weight: 400; color: var(--text-dim); margin-left: 6px; }
.inv-keap {
  display: grid; grid-template-columns: max-content minmax(0, 1fr); gap: 7px 16px; margin: 0;
  padding: 12px 14px; background: var(--bg); border: 1px solid var(--border); border-radius: 10px; font-size: 13px;
}
.inv-keap dt { color: var(--text-muted); }
.inv-keap dd { margin: 0; word-break: break-word; }
.inv-keap dd a { color: var(--indigo); }
.inv-keap-empty { color: var(--text-dim); }
.inv-keap-pending { grid-column: 1 / -1; font-size: 11.5px; color: #B45309; margin-top: 2px; }

/* Locked fields (invoice date read from the PDF, province from Keap). */
.inv-field input:disabled, .inv-field select:disabled { background: #F4F1F0; color: var(--text-muted); cursor: not-allowed; opacity: 1; }

.inv-history { list-style: none; margin: 0; padding: 0; font-size: 12.8px; }
.inv-history li { padding: 7px 0; border-bottom: 1px solid var(--border); }
.inv-history li:last-child { border-bottom: 0; }
.inv-history-fields { display: block; color: var(--text-dim); font-size: 11.5px; margin-top: 2px; }

/* Pop-up: pages rejected because their invoice number already exists. */
.inv-dup-overlay {
  position: fixed; inset: 0; z-index: 130; background: rgba(16,26,51,0.45);
  display: flex; align-items: center; justify-content: center; padding: 16px;
}
.inv-dup-dialog {
  background: var(--surface); border-radius: 16px; width: min(520px, 100%); max-height: 80vh;
  display: flex; flex-direction: column; box-shadow: 0 24px 64px rgba(16,26,51,0.25);
  border-top: 4px solid #DC2626; animation: fadeUp .25s ease both;
}
.inv-dup-head { display: flex; align-items: center; gap: 10px; padding: 18px 20px 4px; }
.inv-dup-head h2 { font: 600 18px 'Space Grotesk', sans-serif; margin: 0; }
.inv-dup-icon {
  width: 26px; height: 26px; border-radius: 50%; background: #FEF2F2; color: #DC2626;
  display: flex; align-items: center; justify-content: center; font-weight: 700; flex-shrink: 0;
}
.inv-dup-close {
  margin-left: auto; border: 0; background: transparent; font-size: 24px; line-height: 1;
  color: var(--text-muted); cursor: pointer; padding: 2px 8px; border-radius: 8px;
}
.inv-dup-close:hover, .inv-dup-close:focus-visible { background: var(--indigo-light); color: var(--indigo); outline: none; }
.inv-dup-sub { margin: 4px 20px 12px; font-size: 13px; color: var(--text-muted); }
.inv-dup-list { overflow-y: auto; padding: 0 20px 18px; }
.inv-dup-file { font-size: 11px; text-transform: uppercase; letter-spacing: .05em; color: var(--text-dim); font-weight: 600; margin: 10px 0 6px; }
.inv-dup-list ul { list-style: none; margin: 0; padding: 0; }
.inv-dup-list li { padding: 9px 12px; border: 1px solid #FECACA; background: #FFF7F7; border-radius: 10px; margin-bottom: 8px; font-size: 13px; }
.inv-dup-list li span { display: block; color: #991B1B; font-size: 12px; margin-top: 3px; }

/* Delete confirmation (reuses the pop-up shell above). */
.inv-confirm-actions { display: flex; justify-content: flex-end; gap: 10px; padding: 4px 20px 18px; }

/* --- AM / Province missing in Keap -------------------------------------- */
/* The whole row goes red: this can only be fixed in Keap, then Refresh. */
table.inv-table tbody tr.inv-row.inv-row-keap td { background: #FEF2F2; }
table.inv-table tbody tr.inv-row.inv-row-keap td:first-child { box-shadow: inset 4px 0 0 #DC2626; }
table.inv-table tbody tr.inv-row.inv-row-keap:hover td { background: #FDE8E8; }
.inv-section-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 10px; }
.inv-section-head .btn { flex: none; margin-top: -4px; }
.inv-keap-alert { margin: 0 0 12px; font-size: 12.5px; line-height: 1.45; }
.inv-keap-status { font-size: 12px; color: var(--text-muted); margin-top: 8px; }
.inv-keap-status:empty { display: none; }
.inv-keap-note { color: #B91C1C; margin-top: 3px; }

/* --- Export ------------------------------------------------------------- */
.inv-filters .inv-export { margin-left: auto; white-space: nowrap; }
@media (max-width: 720px) { .inv-filters .inv-export { margin-left: 0; flex: 1 1 100%; } }

/* --- Reconciled months (read-only) --------------------------------------- */
.inv-locked-alert { background: var(--blue-light); color: #1E40AF; border: 1px solid #BFDBFE; margin-bottom: 12px; font-size: 13px; }
.inv-lock-icon { color: var(--text-dim); cursor: default; }
