:root {
  --bg: #0f1115;
  --panel: #171a21;
  --panel-2: #1e222b;
  --border: #262b36;
  --text: #e6e8ee;
  --muted: #8a93a6;
  --accent: #6ee7b7;
  --accent-2: #60a5fa;
  --danger: #f87171;
  --warn: #fbbf24;
  --income: #6ee7b7;
  --expense: #f87171;
  --radius: 10px;
}

* { box-sizing: border-box; }
html, body {
  margin: 0; padding: 0; min-height: 100dvh;
  background: var(--bg); color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, Helvetica, Arial, sans-serif;
  font-size: 14px; line-height: 1.5;
  padding-top: env(safe-area-inset-top);
  padding-bottom: env(safe-area-inset-bottom);
}

.hidden { display: none !important; }
.centered { justify-content: center; }
.small { font-size: 12px; }
.muted { color: var(--muted); }
.err { color: var(--danger); }

/* ---------- Gate (setup / login) ---------- */
.gate {
  position: fixed; inset: 0; z-index: 40;
  background: radial-gradient(1200px 800px at 30% 10%, #11161e 0%, #0b0d12 60%, #080a0e 100%);
  display: flex; align-items: center; justify-content: center; padding: 24px;
}
.gate-card {
  background: var(--panel); border: 1px solid var(--border); border-radius: 14px;
  padding: 28px; width: 100%; max-width: 460px;
  box-shadow: 0 10px 30px rgba(0,0,0,0.4);
}
.gate-card .brand { margin-bottom: 18px; font-size: 17px; }
.gate-form h2 { margin-top: 16px; }
.gate-form label { display: flex; flex-direction: column; gap: 4px; margin-top: 12px; color: var(--muted); font-size: 12px; }
.gate-form input, .gate-form select {
  background: var(--panel-2); border: 1px solid var(--border); color: var(--text);
  padding: 10px 12px; border-radius: 6px; font: inherit;
}
.gate-form .grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.gate-form button { width: 100%; margin-top: 16px; padding: 11px; }
.gate-form hr { margin-top: 18px; border: 0; border-top: 1px solid var(--border); }

/* ---------- Topbar ---------- */
.topbar {
  display: flex; align-items: center; gap: 14px; flex-wrap: wrap;
  padding: 12px 20px; border-bottom: 1px solid var(--border);
  background: linear-gradient(180deg, #11141b 0%, #0f1115 100%);
  position: sticky; top: 0; z-index: 10;
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 600; letter-spacing: 0.3px; }
.brand .logo { color: var(--accent); font-size: 18px; }
.entity-switcher label { display: flex; align-items: center; gap: 6px; color: var(--muted); font-size: 12px; }
.entity-switcher select {
  background: var(--panel-2); border: 1px solid var(--border); color: var(--text);
  padding: 6px 10px; border-radius: 6px; font: inherit;
}
.user-area { display: flex; align-items: center; gap: 10px; margin-left: auto; }

.tabs { display: flex; gap: 4px; flex-wrap: wrap; }
.tab {
  background: transparent; color: var(--muted); border: 1px solid transparent;
  padding: 8px 14px; border-radius: 8px; cursor: pointer; font-size: 14px;
  transition: background 0.15s, color 0.15s;
}
.tab:hover { color: var(--text); background: var(--panel-2); }
.tab.active { color: var(--text); background: var(--panel-2); border-color: var(--border); }
.tab .chip { margin-left: 6px; }

main { max-width: 1100px; margin: 24px auto; padding: 0 20px 64px; }

h1 { font-size: 22px; margin: 0 0 18px; font-weight: 600; }
h2 { font-size: 15px; margin: 0 0 12px; font-weight: 600; color: var(--text); }

.view.hidden { display: none; }

.panel {
  background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 18px; margin-bottom: 18px;
}

.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 14px; margin-bottom: 18px; }
.card {
  background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 16px;
}
.card .label { color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: 0.6px; }
.card .value { font-size: 22px; font-weight: 600; margin-top: 6px; }
.card .value.income { color: var(--income); }
.card .value.expense { color: var(--expense); }

.grid {
  display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 12px; align-items: end;
}
.grid label { display: flex; flex-direction: column; gap: 4px; color: var(--muted); font-size: 12px; }
.grid .wide { grid-column: span 2; }
.grid input, .grid select, .toolbar input, .toolbar select, .entity-switcher select {
  background: var(--panel-2); border: 1px solid var(--border); color: var(--text);
  padding: 8px 10px; border-radius: 6px; font: inherit;
}
.grid input:focus, .grid select:focus { outline: 2px solid var(--accent-2); outline-offset: 1px; }

button, .linklike {
  background: var(--accent); color: #0b1015;
  border: 0; padding: 9px 14px; border-radius: 6px; cursor: pointer; font-weight: 600;
}
button.ghost { background: transparent; color: var(--muted); border: 1px solid var(--border); font-weight: 500; }
button.danger { background: var(--danger); color: #0b1015; }
button:hover { filter: brightness(1.05); }
.linklike { background: transparent; color: var(--accent-2); padding: 0; font-weight: 500; text-decoration: underline; }

.toolbar { display: flex; flex-wrap: wrap; gap: 10px; align-items: end; margin-bottom: 12px; }
.toolbar label { display: flex; flex-direction: column; gap: 4px; color: var(--muted); font-size: 12px; }

table { width: 100%; border-collapse: collapse; }
th, td { padding: 10px 8px; border-bottom: 1px solid var(--border); text-align: left; vertical-align: top; }
th { color: var(--muted); font-weight: 500; font-size: 12px; text-transform: uppercase; letter-spacing: 0.5px; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
tr:hover { background: var(--panel-2); }

.amount.income { color: var(--income); }
.amount.expense { color: var(--expense); }

.chip { display: inline-block; padding: 2px 8px; border-radius: 999px; background: var(--panel-2); color: var(--muted); font-size: 11px; }
.chip.warn { background: rgba(251, 191, 36, 0.15); color: var(--warn); }
.chip.ok { background: rgba(110, 231, 183, 0.12); color: var(--income); }

.dropzone {
  border: 2px dashed var(--border); border-radius: var(--radius);
  padding: 28px; text-align: center; color: var(--muted);
  transition: border-color 0.15s, background 0.15s;
}
.dropzone.drag { border-color: var(--accent); background: rgba(110, 231, 183, 0.05); color: var(--text); }
.dropzone p { margin: 0; }

#upload-status .row {
  display: flex; justify-content: space-between; gap: 8px;
  padding: 6px 0; border-bottom: 1px dashed var(--border); font-size: 12px;
}
#upload-status .row:last-child { border-bottom: 0; }

.footer {
  display: flex; align-items: center; justify-content: center; gap: 10px;
  color: var(--muted); font-size: 12px; padding: 20px 0 36px;
  padding-bottom: calc(36px + env(safe-area-inset-bottom));
}
.footer code { background: var(--panel-2); padding: 2px 6px; border-radius: 4px; }

.actions button { padding: 4px 10px; font-size: 12px; font-weight: 500; }
.actions .del { background: transparent; color: var(--danger); border: 1px solid var(--border); }
.actions .view { background: transparent; color: var(--accent-2); border: 1px solid var(--border); }

/* Goal bars */
.goal-row { margin: 10px 0; }
.goal-row .title { display: flex; justify-content: space-between; align-items: baseline; }
.bar-wrap { background: var(--panel-2); height: 10px; border-radius: 5px; overflow: hidden; margin-top: 4px; }
.bar { height: 100%; background: var(--accent); transition: width 0.3s; }
.bar.expense { background: var(--expense); }
.bar.income { background: var(--income); }
.bar.under { background: var(--warn); }

/* Entity dashboard tiles */
.entity-tile {
  background: var(--panel-2); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 14px; margin-bottom: 10px;
}
.entity-tile .header { display: flex; justify-content: space-between; align-items: baseline; }
.entity-tile .kv { display: flex; gap: 18px; margin-top: 6px; flex-wrap: wrap; }
.entity-tile .kv span { color: var(--muted); font-size: 12px; }
.entity-tile .kv b { color: var(--text); font-weight: 600; font-size: 14px; margin-left: 4px; }

/* Inbox cards */
.inbox-card {
  display: grid; grid-template-columns: 72px 1fr auto; gap: 12px; align-items: center;
  padding: 10px; border: 1px solid var(--border); border-radius: 8px; margin-bottom: 8px;
  background: var(--panel-2);
}
.inbox-card .thumb {
  width: 72px; height: 72px; border-radius: 6px; background: var(--border);
  display: flex; align-items: center; justify-content: center; overflow: hidden;
  color: var(--muted); font-size: 11px;
}
.inbox-card .thumb img { width: 100%; height: 100%; object-fit: cover; }
.inbox-card .meta { min-width: 0; }
.inbox-card .meta .name { font-weight: 600; text-overflow: ellipsis; white-space: nowrap; overflow: hidden; }
.inbox-card .meta .sub { color: var(--muted); font-size: 12px; }
.inbox-card .actions { display: flex; gap: 6px; }

/* Bank reconcile */
.bank-row {
  display: grid; grid-template-columns: 92px 1fr 110px 160px 180px auto; gap: 10px;
  align-items: center; padding: 8px 6px; border-bottom: 1px solid var(--border);
}
.bank-row.matched { background: rgba(110, 231, 183, 0.05); }
.bank-row.suggested { background: rgba(125, 211, 252, 0.06); }
.bank-row.needs-attn { background: rgba(251, 191, 36, 0.07); }
.bank-row.ignored { opacity: 0.5; }
.bank-row .amount { text-align: right; font-variant-numeric: tabular-nums; }
.bank-row select { background: var(--panel-2); border: 1px solid var(--border); color: var(--text); padding: 4px 6px; border-radius: 4px; font: inherit; max-width: 100%; }
.bank-row select.category { border-left: 3px solid transparent; }
.bank-row.suggested select.category { border-left-color: #38bdf8; }
.bank-row.needs-attn select.category { border-left-color: #fbbf24; }

/* Modal */
.modal {
  position: fixed; inset: 0; z-index: 30;
  background: rgba(0,0,0,0.6); display: flex; align-items: center; justify-content: center;
  padding: 16px;
}
.modal.hidden { display: none; }
.modal-card {
  background: var(--panel); border: 1px solid var(--border); border-radius: 12px;
  padding: 20px; width: 100%; max-width: 680px; position: relative;
  max-height: 92vh; overflow: auto;
}
.modal-card .close {
  position: absolute; top: 8px; right: 8px; background: transparent; color: var(--muted);
  border: 0; font-size: 22px; padding: 4px 10px; cursor: pointer; font-weight: 400;
}
.modal-preview {
  width: 100%; min-height: 180px; background: var(--panel-2); border-radius: 8px;
  display: flex; align-items: center; justify-content: center; color: var(--muted); overflow: hidden;
  margin-bottom: 14px;
}
.modal-preview img { max-width: 100%; max-height: 320px; object-fit: contain; }
.modal-preview iframe { width: 100%; height: 320px; border: 0; background: #fff; }

/* Toast */
.toast {
  position: fixed; right: 16px; bottom: 16px;
  background: var(--panel-2); border: 1px solid var(--border); color: var(--text);
  padding: 10px 14px; border-radius: 8px; font-size: 13px;
  box-shadow: 0 4px 14px rgba(0,0,0,0.35); opacity: 0; transition: opacity 0.2s;
  z-index: 50; display: inline-flex; align-items: center; gap: 8px; max-width: min(560px, calc(100vw - 32px));
}
.toast.show { opacity: 1; }
.toast.err { border-color: var(--danger); color: var(--danger); }
.toast-close {
  background: transparent; border: 0; color: inherit; cursor: pointer;
  font-size: 16px; line-height: 1; padding: 0 4px; opacity: 0.8;
}
.toast-close:hover { opacity: 1; }

/* Receipts inside the transaction edit modal */
.tx-receipts { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 10px; }
.receipt-item {
  display: grid; grid-template-columns: 84px 1fr; gap: 10px; align-items: start;
  padding: 8px; border: 1px solid var(--border); border-radius: 8px; background: var(--panel-2);
}
.receipt-link { display: block; width: 84px; height: 84px; border-radius: 6px; overflow: hidden; background: var(--border); position: relative; }
.receipt-link img { width: 100%; height: 100%; object-fit: cover; display: block; }
.receipt-pdf {
  width: 100%; height: 100%; display: flex; align-items: center; justify-content: center;
  color: var(--muted); font-weight: 600;
}
.receipt-link .receipt-iframe {
  width: 100%; height: 100%; border: 0; background: var(--panel);
  pointer-events: none; /* don't capture clicks — the wrapping <a> owns "open in new tab" */
}
.receipt-meta { min-width: 0; font-size: 12px; }
.receipt-meta > div:first-child { font-weight: 600; color: var(--text); text-overflow: ellipsis; overflow: hidden; white-space: nowrap; }
.receipt-actions { display: flex; gap: 8px; margin-top: 6px; flex-wrap: wrap; align-items: center; }
.receipt-actions a { color: var(--accent-2); text-decoration: none; }
.receipt-actions a:hover { text-decoration: underline; }
.btn-link { color: var(--accent-2); text-decoration: none; padding: 6px 10px; border: 1px solid var(--border); border-radius: 6px; font-size: 12px; }
.btn-link:hover { background: var(--panel-2); }

/* Mobile tweaks */
@media (max-width: 640px) {
  .topbar { padding: 10px 12px; }
  main { padding: 0 12px 48px; }
  .tabs { gap: 2px; }
  .tab { padding: 6px 10px; font-size: 13px; }
  .bank-row { grid-template-columns: 1fr 1fr; grid-auto-flow: row; gap: 6px; }
  .bank-row .amount { text-align: left; }

  /* Modal goes near-full-screen on phones so the classify form is the
     focus, not the page chrome behind it. */
  .modal { padding: 0; align-items: stretch; }
  .modal-card { max-width: none; max-height: 100vh; border-radius: 0; padding: 16px; }
  .modal-card .close { top: 4px; right: 4px; font-size: 28px; padding: 6px 12px; }
  .modal-preview { min-height: 140px; }
  .modal-preview img, .modal-preview iframe { max-height: 200px; height: 200px; }
  /* Bigger amount input on phones — easier to hit with a thumb and the
     iOS / Android numeric keypad is more usable above 16px. */
  .amount-label input { font-size: 18px; padding: 10px; }
  /* Save / submit buttons full-width on phones so the primary action is
     impossible to miss. */
  .modal-card button[type=submit] { width: 100%; padding: 12px; font-size: 16px; }
}

/* PDF parser diagnostics panel — shown when an import fails with 0 rows. */
.pdf-diag { margin-top: 12px; }
.diag-panel { border-left: 3px solid #d18a2a; }
.diag-stats { width: auto; margin: 6px 0 10px; }
.diag-stats td { padding: 2px 10px 2px 0; font-size: 13px; }
.diag-stats td.num { text-align: right; font-variant-numeric: tabular-nums; }
.diag-pre {
  background: rgba(255,255,255,0.03);
  border: 1px solid rgba(255,255,255,0.08);
  border-radius: 6px;
  padding: 8px 10px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 12px;
  white-space: pre-wrap;
  word-break: break-word;
  max-height: 260px;
  overflow: auto;
  margin: 4px 0 0;
}
.diag-panel details > summary { cursor: pointer; font-size: 13px; margin-top: 8px; color: var(--muted); }

/* ---- All-receipts gallery ---- */
.gallery-head { display: flex; justify-content: space-between; align-items: flex-end; gap: 12px; flex-wrap: wrap; }
.gallery-filters { display: flex; gap: 10px; flex-wrap: wrap; align-items: flex-end; }
.gallery-filters label.inline { display: flex; flex-direction: column; font-size: 11px; color: var(--muted); gap: 2px; }
.gallery-filters select, .gallery-filters input[type=search] {
  background: var(--panel); color: var(--text); border: 1px solid var(--border);
  border-radius: 6px; padding: 6px 8px; font-size: 13px;
}
.gallery-grid {
  display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 12px; margin-top: 10px;
}
.gallery-card {
  background: var(--panel-2); border: 1px solid var(--border); border-radius: 10px; overflow: hidden;
  display: flex; flex-direction: column;
}
.gallery-thumb {
  display: block; width: 100%; height: 140px; background: var(--border); overflow: hidden;
  position: relative;
}
.gallery-thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.gallery-thumb .gallery-iframe { width: 100%; height: 100%; border: 0; pointer-events: none; background: var(--panel); }
.gallery-body { padding: 8px 10px; display: flex; flex-direction: column; gap: 2px; }
.gallery-title { font-weight: 600; font-size: 14px; display: flex; align-items: center; gap: 6px; }
.gallery-body .receipt-actions { margin-top: 6px; }

@media (max-width: 640px) {
  .gallery-grid { grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); }
  .gallery-thumb { height: 110px; }
}

/* ---- Multi-currency (v0.3.6) ----
   USD is a rare guest in a mostly-CAD ledger. These styles keep the
   foreign amount visible without stealing focus from the CAD figure that
   actually reconciles against the bank.
*/
.currency-select {
  background: var(--panel); color: var(--text); border: 1px solid var(--border);
  border-radius: 6px; padding: 6px 8px; font-size: 13px; min-width: 72px;
}
.cad-hint {
  display: flex; align-items: center; gap: 6px;
  font-size: 12px; color: var(--muted);
  padding: 4px 2px 0;
}
.cad-hint .cad-est { font-weight: 600; color: var(--text); }
.cad-hint .fx-rate-display { opacity: 0.8; }
.amount-label { font-size: 11px; color: var(--muted); }
.foreign-hint {
  font-size: 11px; color: var(--muted); font-weight: 400;
}
.tx-table .foreign-hint {
  display: block; margin-top: 1px;
}
