/* ===================================================================
   ARG Invoicing — one component set for every screen.
   System fonts only (the desktop app must render offline). Every screen
   is built from these parts; no inline styling in the JS.
   =================================================================== */

:root {
  --ink: #161A22;
  --ink-2: #3A4150;
  --muted: #667085;
  --paper: #F3F4F6;
  --panel: #FFFFFF;
  --line: #E2E5EA;
  --line-2: #EEF0F3;
  --accent: #0E9D72;
  --accent-ink: #087A57;
  --accent-wash: #E6F6F0;
  --amber: #A15C07;
  --amber-wash: #FEF4E6;
  --red: #C0262D;
  --red-wash: #FDECEC;
  --blue: #2557A7;
  --blue-wash: #EAF1FB;
  --radius: 8px;
  --radius-lg: 12px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .05), 0 1px 3px rgba(16, 24, 40, .06);
  --shadow-lg: 0 16px 40px rgba(16, 24, 40, .18);
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body { margin: 0; height: 100%; background: var(--paper); color: var(--ink); font: 14px/1.45 var(--font); -webkit-font-smoothing: antialiased; }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
a { color: var(--accent-ink); }
.num, .tabular { font-variant-numeric: tabular-nums; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.mono { font-family: var(--mono); font-size: 12.5px; }
.nowrap { white-space: nowrap; }

/* ------------------------------------------------------------ app frame */
.app { display: grid; grid-template-rows: auto 1fr; height: 100vh; }
.topbar { display: flex; align-items: center; gap: 16px; padding: 10px 20px; background: var(--ink); color: #F5F6F8; min-height: 56px; }
.brand { font-weight: 700; font-size: 16px; letter-spacing: -.1px; white-space: nowrap; }
.brand small { display: block; font-weight: 400; font-size: 11px; color: #98A2B3; }
.company-switch { display: flex; align-items: center; gap: 8px; margin-left: 8px; min-width: 0; }
.company-switch label { font-size: 11.5px; color: #98A2B3; white-space: nowrap; }
.company-switch select { background: #242A36; color: #F5F6F8; border: 1px solid #3A4252; border-radius: var(--radius); padding: 6px 10px; max-width: 280px; }
.company-switch .company-name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 280px; }
.spacer { flex: 1; }
.mode-pill { display: inline-flex; align-items: center; gap: 6px; font-size: 12px; padding: 4px 10px; border-radius: 999px; white-space: nowrap; }
.mode-pill::before { content: ""; width: 7px; height: 7px; border-radius: 50%; background: currentColor; }
.mode-pill.sandbox { background: #33290F; color: #F4CF84; }
.mode-pill.production { background: #0C2B21; color: #6EE7B7; }
.mode-pill.none { background: #3A1A1C; color: #F4A9A9; }
.icon-btn { display: inline-grid; place-items: center; width: 34px; height: 34px; border-radius: var(--radius); border: 0; background: transparent; color: inherit; position: relative; }
.icon-btn:hover { background: rgba(255, 255, 255, .08); }
.icon-btn svg { width: 18px; height: 18px; }
.dot { position: absolute; top: 6px; right: 7px; width: 8px; height: 8px; border-radius: 50%; background: #F04438; border: 2px solid var(--ink); }
.topbar .btn.ghost { color: #D0D5DD; }
.topbar .btn.ghost:hover { background: rgba(255, 255, 255, .08); color: #fff; }

.body { display: grid; grid-template-columns: 216px minmax(0, 1fr); min-height: 0; }
.sidebar { background: #FAFAFB; border-right: 1px solid var(--line); padding: 14px 0; overflow-y: auto; }
.nav-heading { margin: 14px 18px 6px; font-size: 11px; font-weight: 600; color: var(--muted); }
.nav-heading:first-child { margin-top: 2px; }
.sidebar a { display: block; padding: 8px 18px; color: var(--ink-2); text-decoration: none; font-weight: 500; border-left: 3px solid transparent; }
.sidebar a:hover { background: var(--line-2); }
.sidebar a.active { background: var(--accent-wash); color: var(--accent-ink); border-left-color: var(--accent); font-weight: 600; }
.main { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.view-as { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 8px 28px; background: var(--amber-wash); color: #7A4304; border-bottom: 1px solid #F5D9AE; font-size: 13px; }
.view-as button { background: none; border: 0; color: #7A4304; font-weight: 600; text-decoration: underline; padding: 0; }
.content { flex: 1; overflow-y: auto; padding: 24px 28px 64px; }

/* --------------------------------------------------------- page header */
.page-header { display: flex; align-items: flex-start; justify-content: space-between; gap: 16px; margin-bottom: 18px; }
.page-header h1 { margin: 0; font-size: 22px; font-weight: 700; letter-spacing: -.3px; }
.page-header p { margin: 4px 0 0; color: var(--muted); max-width: 70ch; }
.page-header .actions { display: flex; gap: 8px; flex-shrink: 0; }

/* ---------------------------------------------------------------- cards */
.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius-lg); box-shadow: var(--shadow); padding: 18px 20px; margin-bottom: 16px; }
.card-header { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 12px; }
.card-header h2 { margin: 0; font-size: 15px; font-weight: 650; }
.card-header p { margin: 2px 0 0; color: var(--muted); font-size: 12.5px; }
.card.flush { padding: 0; }
.card.flush > .card-header { padding: 16px 20px 0; }

.stat-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 14px; margin-bottom: 16px; }
.stat { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius-lg); box-shadow: var(--shadow); padding: 14px 16px; }
.stat .label { color: var(--muted); font-size: 12.5px; }
.stat .value { font-size: 26px; font-weight: 700; letter-spacing: -.5px; margin-top: 2px; font-variant-numeric: tabular-nums; }
.stat.warn .value { color: var(--amber); }
.stat.bad .value { color: var(--red); }
.stat.good .value { color: var(--accent-ink); }

/* ---------------------------------------------------------------- forms */
.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px 18px; }
.form-grid.cols-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.form-grid .span-all { grid-column: 1 / -1; }
.form-grid .span-2 { grid-column: span 2; }
.field { display: flex; flex-direction: column; gap: 5px; min-width: 0; }
.field > label, .field-label { font-size: 12.5px; font-weight: 550; color: var(--ink-2); }
.field input, .field select, .field textarea, input.control, select.control {
  width: 100%; min-width: 0; padding: 8px 10px; border: 1px solid #D0D5DD; border-radius: var(--radius); background: #fff;
}
.field textarea { min-height: 64px; resize: vertical; }
.field input:focus, .field select:focus, .field textarea:focus, .control:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-wash); }
.field input[readonly] { background: var(--line-2); }
.field-help { font-size: 12px; color: var(--muted); }
.field-error { font-size: 12px; color: var(--red); }
.check { display: flex; align-items: flex-start; gap: 9px; }
.check input { margin-top: 3px; width: 16px; height: 16px; flex-shrink: 0; }
.check label { font-weight: 500; color: var(--ink); }
.check .field-help { display: block; }
.form-actions { display: flex; gap: 10px; align-items: center; margin-top: 16px; flex-wrap: wrap; }
.segmented { display: inline-flex; border: 1px solid #D0D5DD; border-radius: var(--radius); overflow: hidden; background: #fff; }
.segmented button { border: 0; background: none; padding: 7px 12px; font-weight: 500; color: var(--ink-2); border-right: 1px solid #D0D5DD; }
.segmented button:last-child { border-right: 0; }
.segmented button.on { background: var(--accent-wash); color: var(--accent-ink); font-weight: 600; }
.toolbar { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; margin-bottom: 12px; }
.toolbar input.control { max-width: 260px; }

/* -------------------------------------------------------------- buttons */
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; padding: 8px 14px; border-radius: var(--radius); border: 1px solid #D0D5DD; background: #fff; color: var(--ink); font-weight: 600; font-size: 13px; white-space: nowrap; }
.btn:hover { background: var(--line-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { background: var(--accent-ink); border-color: var(--accent-ink); }
.btn.danger { color: var(--red); }
.btn.danger.solid { background: var(--red); border-color: var(--red); color: #fff; }
.btn.ghost { border-color: transparent; background: transparent; color: var(--accent-ink); padding: 6px 8px; }
.btn.ghost:hover { background: var(--accent-wash); }
.btn.ghost.danger { color: var(--red); }
.btn.ghost.danger:hover { background: var(--red-wash); }
.btn.sm { padding: 5px 9px; font-size: 12.5px; }
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn .spinner { width: 13px; height: 13px; border: 2px solid currentColor; border-right-color: transparent; border-radius: 50%; animation: spin .7s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { .btn .spinner { animation: none; } }

/* --------------------------------------------------------------- tables */
.table-wrap { overflow-x: auto; border: 1px solid var(--line); border-radius: var(--radius); }
.card .table-wrap { margin: 0 -1px; }
table.data { width: 100%; border-collapse: collapse; background: #fff; }
table.data th { text-align: left; font-size: 12px; font-weight: 600; color: var(--muted); background: #F8F9FB; padding: 9px 12px; border-bottom: 1px solid var(--line); white-space: nowrap; }
table.data td { padding: 10px 12px; border-bottom: 1px solid var(--line-2); vertical-align: middle; }
table.data tr:last-child td { border-bottom: 0; }
table.data tbody tr:hover td { background: #FAFBFC; }
table.data .num { text-align: right; white-space: nowrap; }
table.data td.actions { white-space: nowrap; text-align: right; width: 1%; }
table.data td.actions .btn + .btn { margin-left: 2px; }
table.data tr.clickable { cursor: pointer; }
table.data .sub { display: block; font-size: 12px; color: var(--muted); }
table.lines td { padding: 6px 8px; }
table.lines input, table.lines select { width: 100%; min-width: 0; padding: 7px 8px; border: 1px solid #D0D5DD; border-radius: 6px; background: #fff; }
table.lines td.num input { text-align: right; }
table.lines .col-service { min-width: 180px; }
table.lines .col-desc { min-width: 160px; }
table.lines .col-qty input { min-width: 70px; }
table.lines .col-money input { min-width: 104px; }
table.data td.col-client { min-width: 150px; }
table.lines .calc { text-align: right; white-space: nowrap; color: var(--ink-2); min-width: 92px; }

/* ---------------------------------------------------------------- badges */
.badge { display: inline-block; font-size: 11.5px; font-weight: 600; padding: 2px 9px; border-radius: 999px; background: var(--line-2); color: var(--ink-2); white-space: nowrap; }
.badge.draft { background: var(--blue-wash); color: var(--blue); }
.badge.submitted, .badge.ok { background: var(--accent-wash); color: var(--accent-ink); }
.badge.failed, .badge.bad { background: var(--red-wash); color: var(--red); }
.badge.uncertain, .badge.submitting, .badge.warn { background: var(--amber-wash); color: var(--amber); }
.badge.cancelled, .badge.voided, .badge.muted { background: var(--line-2); color: var(--muted); }

/* -------------------------------------------------------------- banners */
.banner { display: flex; gap: 10px; align-items: flex-start; padding: 11px 14px; border-radius: var(--radius); border: 1px solid; margin-bottom: 14px; font-size: 13px; }
.banner > div { flex: 1; }
.banner b { font-weight: 650; }
.banner.info { background: var(--blue-wash); border-color: #C8D8F2; color: #1D3F7A; }
.banner.warn { background: var(--amber-wash); border-color: #F5D9AE; color: #6B3D04; }
.banner.error { background: var(--red-wash); border-color: #F4C7C7; color: #8E1C21; }
.banner.ok { background: var(--accent-wash); border-color: #BFE7D7; color: #075E44; }
.banner .btn { flex-shrink: 0; }

/* ---------------------------------------------------------------- totals */
.totals { display: flex; justify-content: space-between; align-items: flex-end; gap: 20px; flex-wrap: wrap; background: var(--ink); color: #E4E7EC; border-radius: var(--radius-lg); padding: 18px 22px; margin-bottom: 16px; }
.totals dl { display: grid; grid-template-columns: auto auto; gap: 3px 16px; margin: 0; font-size: 13px; }
.totals dt { color: #98A2B3; }
.totals dd { margin: 0; text-align: right; font-variant-numeric: tabular-nums; color: #fff; }
.totals .grand { text-align: right; }
.totals .grand .label { font-size: 12px; color: #98A2B3; }
.totals .grand .amount { font-size: 30px; font-weight: 700; color: #6EE7B7; letter-spacing: -.5px; font-variant-numeric: tabular-nums; }

/* ------------------------------------------------------------- empty/misc */
.empty { text-align: center; color: var(--muted); padding: 34px 16px; }
.empty .btn { margin-top: 10px; }
.kv { display: grid; grid-template-columns: max-content minmax(0, 1fr); gap: 6px 18px; margin: 0; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; overflow-wrap: anywhere; }
.plain-list { margin: 4px 0 0; padding-left: 18px; }
.plain-list li { margin-bottom: 8px; color: var(--ink-2); }
ol.plain-list.numbered { list-style: decimal; }
.kv.compact { margin: 8px 0 4px; gap: 3px 14px; font-size: 13px; }
.card.iris-check { border-color: #F5D9AE; }
pre.json { background: #0F1320; color: #D7DEEA; border-radius: var(--radius); padding: 12px; max-height: 260px; overflow: auto; font: 12px/1.45 var(--mono); white-space: pre-wrap; word-break: break-word; }
details.log { border: 1px solid var(--line); border-radius: var(--radius); padding: 8px 12px; margin-bottom: 8px; }
details.log summary { cursor: pointer; font-weight: 550; }
.recovery-codes { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; background: var(--line-2); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px; font: 14px var(--mono); text-align: center; }
.qr { display: block; width: 190px; height: 190px; margin: 10px auto; border: 1px solid var(--line); border-radius: var(--radius); }

/* ---------------------------------------------------------- notifications */
.notif-wrap { position: relative; }
.notif-panel { position: absolute; right: 0; top: 42px; width: 340px; max-height: 420px; overflow-y: auto; background: #fff; color: var(--ink); border: 1px solid var(--line); border-radius: var(--radius-lg); box-shadow: var(--shadow-lg); z-index: 40; }
.notif-head { display: flex; justify-content: space-between; align-items: center; padding: 10px 14px; border-bottom: 1px solid var(--line-2); font-weight: 600; }
.notif-item { padding: 10px 14px; border-bottom: 1px solid var(--line-2); font-size: 13px; cursor: pointer; }
.notif-item:last-child { border-bottom: 0; }
.notif-item.unread { background: #F5FBF8; }
.notif-item .t { font-weight: 600; }
.notif-item.error .t { color: var(--red); }
.notif-item.warning .t { color: var(--amber); }
.notif-item.success .t { color: var(--accent-ink); }
.notif-item .when { font-size: 11.5px; color: var(--muted); margin-top: 2px; }

/* ---------------------------------------------------------------- dialog */
dialog.modal { border: 0; padding: 0; border-radius: var(--radius-lg); box-shadow: var(--shadow-lg); width: min(560px, calc(100vw - 40px)); max-height: calc(100vh - 60px); color: var(--ink); }
dialog.modal.wide { width: min(880px, calc(100vw - 40px)); }
dialog.modal::backdrop { background: rgba(16, 20, 30, .45); }
.modal-form { display: flex; flex-direction: column; max-height: calc(100vh - 60px); }
.modal-head { padding: 16px 20px 0; }
.modal-head h2 { margin: 0; font-size: 17px; }
.modal-head p { margin: 4px 0 0; color: var(--muted); font-size: 13px; }
.modal-body { padding: 14px 20px; overflow-y: auto; }
.modal-foot { display: flex; justify-content: flex-end; gap: 8px; padding: 12px 20px 16px; border-top: 1px solid var(--line-2); }
.modal-error { color: var(--red); font-size: 13px; margin-top: 8px; min-height: 1px; }

/* ----------------------------------------------------------------- toast */
.toast-stack { position: fixed; right: 20px; bottom: 20px; display: flex; flex-direction: column; gap: 8px; z-index: 100; max-width: 400px; }
.toast { display: flex; gap: 10px; align-items: flex-start; background: var(--ink); color: #fff; padding: 11px 14px; border-radius: var(--radius); box-shadow: var(--shadow-lg); font-size: 13px; }
.toast.success { background: #06684A; }
.toast.error { background: #A3222A; }
.toast.warning { background: #8A4E05; }
.toast button { background: none; border: 0; color: inherit; opacity: .8; padding: 0 0 0 6px; font-size: 16px; line-height: 1; }

/* ----------------------------------------------------------------- login */
.auth-screen { min-height: 100vh; display: grid; place-items: center; background: linear-gradient(160deg, #161A22, #0B0E14); padding: 24px; }
.auth-card { width: min(400px, 100%); background: #fff; border-radius: var(--radius-lg); box-shadow: var(--shadow-lg); padding: 28px; }
.auth-card .brand { color: var(--ink); text-align: center; font-size: 19px; margin-bottom: 4px; }
.auth-card .brand small { color: var(--muted); }
.auth-card h2 { font-size: 16px; margin: 18px 0 4px; }
.auth-card p { color: var(--muted); font-size: 13px; margin: 0 0 14px; }
.auth-card .field { margin-bottom: 12px; }
.auth-card .btn { width: 100%; }
.auth-card .btn + .btn { margin-top: 8px; }
.auth-error { color: var(--red); font-size: 13px; min-height: 18px; margin-bottom: 8px; }

@media (max-width: 1180px) {
  .form-grid.cols-3 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .body { grid-template-columns: 190px minmax(0, 1fr); }
  .content { padding: 20px 20px 56px; }
}

.card.flush { overflow: hidden; }
.card.flush .table-wrap { border: 0; border-radius: 0; }
.card.flush > .empty { padding: 34px 16px; }

.top-actions { margin: 0 0 14px; }
#fbrLog:not(:empty) { margin-bottom: 14px; }
#checkResult:empty { display: none; }

.field > .btn { align-self: flex-start; }
.btn.icon-only { font-size: 18px; line-height: 1; padding: 4px 9px; }
.visually-hidden { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
@media (max-width: 1180px) {
  table.lines .col-tax { display: none; }
  table.lines .col-service { min-width: 160px; }
  table.lines .col-desc { min-width: 130px; }
  table.lines .col-money input { min-width: 92px; }
}

.notif-panel .btn.ghost { color: var(--accent-ink); }
.hs-results { display: flex; flex-direction: column; gap: 2px; max-height: 220px; overflow-y: auto; margin-top: 4px; }
.hs-results:empty { display: none; }
.hs-hit { text-align: left; background: var(--surface, #fff); border: 1px solid var(--line); border-radius: 6px; padding: 6px 10px; font: inherit; font-size: 13px; cursor: pointer; color: var(--ink-2); }
.hs-hit:hover, .hs-hit:focus-visible { border-color: var(--accent, #0F8A5F); }
.hs-hit b { color: var(--ink, #111); margin-right: 6px; font-variant-numeric: tabular-nums; }
.unit-check:empty { display: none; }
.unit-check.ok { color: #075E44; }
.unit-check.bad { color: #8E1C21; }
.unit-check .btn { margin: 4px 4px 0 0; }
