:root {
  --canvas: #F7F6F3;
  --surface: #FFFFFF;
  --sunken: #F1EFEA;
  --line: #E8E5DF;
  --line2: #F0EDE7;
  --ink: #1A1917;
  --body: #3D3A35;
  --muted: #7A756C;
  --faint: #A39D92;
  --pos: #17795E;
  --posbg: #E7F3EE;
  --warn: #96631B;
  --warnbg: #FBF1E1;
  --neg: #A33B32;
  --negbg: #FAECEA;
  --info: #2C5C99;
  --infobg: #EAF1F9;
  --accent: #1A1917;
  --radius: 13px;
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { -webkit-font-smoothing: antialiased; }
body {
  margin: 0;
  font-family: 'Inter', ui-sans-serif, system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  background: var(--canvas);
  color: var(--ink);
  font-feature-settings: "cv05" 1, "ss01" 1;
}
a { color: inherit; }
.tnum { font-variant-numeric: tabular-nums; }
.wrap { max-width: 1120px; margin: 0 auto; padding: 0 20px; }

/* Top nav */
.topbar {
  height: 60px; display: flex; align-items: center; gap: 12px;
  border-bottom: 1px solid var(--line);
  background: rgba(247,246,243,.9); backdrop-filter: blur(8px);
  position: sticky; top: 0; z-index: 20;
}
.brand { display: flex; align-items: center; gap: 10px; text-decoration: none; }
.brand-mark {
  width: 30px; height: 30px; border-radius: 9px; display: block; flex-shrink: 0;
}
.brand-name { font-weight: 650; letter-spacing: -.01em; font-size: 15px; }
.brand-sub { font-size: 11px; color: var(--faint); }
.nav-links { display: flex; gap: 4px; margin-left: 12px; }
.nav-links a {
  text-decoration: none; font-size: 13px; font-weight: 500; color: var(--muted);
  padding: 7px 11px; border-radius: 9px;
}
.nav-links a:hover { background: var(--sunken); color: var(--ink); }

/* Buttons / inputs */
.btn {
  display: inline-flex; align-items: center; gap: 7px; height: 36px; padding: 0 15px;
  border-radius: 10px; font-size: 13.5px; font-weight: 550; cursor: pointer;
  border: 1px solid transparent; text-decoration: none; transition: all .15s;
  white-space: nowrap;
}
.btn-primary { background: var(--ink); color: #fff; }
.btn-primary:hover { filter: brightness(1.12); }
.btn-primary:disabled { opacity: .55; cursor: not-allowed; }
.btn-ghost { background: var(--surface); border-color: var(--line); color: var(--body); }
.btn-ghost:hover { background: var(--sunken); }
.btn-quiet { background: transparent; color: var(--muted); }
.btn-quiet:hover { background: var(--sunken); color: var(--ink); }
.btn-danger { background: var(--negbg); color: var(--neg); border-color: #F0D9D6; }
.inp, .sel {
  height: 38px; width: 100%; border-radius: 10px; border: 1px solid var(--line);
  background: var(--surface); padding: 0 12px; font-size: 13.5px; color: var(--ink); outline: none;
}
.inp:focus, .sel:focus { border-color: #C9C3B8; box-shadow: 0 0 0 3px rgba(26,25,23,.06); }
textarea.inp { height: auto; padding: 10px 12px; }
.lbl-field { display: block; font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: .07em; color: var(--faint); margin: 0 0 6px; }
.card {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: 0 1px 2px rgba(20,18,15,.04);
}
.card-pad { padding: 20px; }
.lbl { font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: .08em; color: var(--faint); }
.h2 { font-size: 15px; font-weight: 650; letter-spacing: -.01em; margin: 0; }
.muted { color: var(--muted); }
.small { font-size: 12.5px; }
.chip {
  display: inline-flex; align-items: center; gap: 4px; height: 22px; padding: 0 8px;
  border-radius: 6px; font-size: 11px; font-weight: 600; white-space: nowrap;
}
.chip-pos { background: var(--posbg); color: var(--pos); }
.chip-warn { background: var(--warnbg); color: var(--warn); }
.chip-neg { background: var(--negbg); color: var(--neg); }
.chip-info { background: var(--infobg); color: var(--info); }
.chip-neutral { background: var(--sunken); color: var(--muted); }

/* Hero */
.hero { padding: 72px 0 40px; text-align: left; }
.hero h1 { font-size: 46px; line-height: 1.04; letter-spacing: -.03em; margin: 0 0 16px; }
.hero h1 em { font-style: normal; color: var(--muted); }
.hero p.lead { font-size: 16.5px; color: var(--body); line-height: 1.6; max-width: 640px; margin: 0 0 24px; }
.hero-cta { display: flex; gap: 10px; flex-wrap: wrap; }
.grid-3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 14px; }
.grid-2 { display: grid; grid-template-columns: repeat(2, 1fr); gap: 14px; }
.grid-4 { display: grid; grid-template-columns: repeat(4, 1fr); gap: 14px; }
@media (max-width: 900px) { .grid-3, .grid-4 { grid-template-columns: 1fr 1fr; } .hero h1 { font-size: 34px; } }
@media (max-width: 600px) { .grid-3, .grid-4, .grid-2 { grid-template-columns: 1fr; } .nav-links { display: none; } }
.feature { padding: 18px; }
.feature .ico { font-size: 20px; }
.feature h3 { margin: 10px 0 6px; font-size: 14.5px; }
.feature p { margin: 0; font-size: 13px; color: var(--muted); line-height: 1.6; }
.steps { counter-reset: step; }
.step { display: flex; gap: 14px; padding: 16px 18px; }
.step-num {
  width: 28px; height: 28px; border-radius: 50%; background: var(--ink); color: #fff;
  display: grid; place-items: center; font-size: 13px; font-weight: 700; flex-shrink: 0;
}
.footer { border-top: 1px solid var(--line); margin-top: 56px; padding: 22px 0 40px; color: var(--faint); font-size: 12.5px; }

/* Auth */
.auth-wrap { min-height: calc(100vh - 60px); display: grid; place-items: center; padding: 32px 16px; }
.auth-card { width: 100%; max-width: 420px; padding: 28px; }
.auth-card h1 { font-size: 22px; letter-spacing: -.02em; margin: 0 0 6px; }
.auth-card .sub { font-size: 13.5px; color: var(--muted); margin: 0 0 20px; line-height: 1.55; }
.field { margin-bottom: 14px; }
.form-err {
  display: none; background: var(--negbg); color: var(--neg); border: 1px solid #F0D9D6;
  font-size: 13px; border-radius: 10px; padding: 10px 12px; margin-bottom: 14px; line-height: 1.5;
}
.form-ok {
  display: none; background: var(--posbg); color: var(--pos); border: 1px solid #CBE5D8;
  font-size: 13px; border-radius: 10px; padding: 10px 12px; margin-bottom: 14px; line-height: 1.5;
}
.auth-alt { margin-top: 16px; font-size: 13px; color: var(--muted); text-align: center; }
.auth-alt a { color: var(--ink); font-weight: 600; }
.divider { display: flex; align-items: center; gap: 10px; margin: 18px 0; color: var(--faint); font-size: 11px; text-transform: uppercase; letter-spacing: .08em; }
.divider::before, .divider::after { content: ''; flex: 1; height: 1px; background: var(--line); }
.google-btn { width: 100%; justify-content: center; }

/* Dashboard layout */
.dash { display: flex; min-height: calc(100vh - 60px); }
.side {
  width: 232px; flex-shrink: 0; border-right: 1px solid var(--line);
  background: rgba(255,255,255,.7); padding: 16px 12px; display: flex; flex-direction: column; gap: 2px;
}
.side .lbl { padding: 8px 10px 6px; }
.nav-i {
  display: flex; align-items: center; gap: 10px; height: 36px; padding: 0 10px;
  border-radius: 9px; font-size: 13.5px; font-weight: 500; color: var(--muted);
  cursor: pointer; border: none; background: none; width: 100%; text-align: left;
}
.nav-i:hover { background: var(--sunken); color: var(--ink); }
.nav-i.on { background: var(--sunken); color: var(--ink); }
.main { flex: 1; min-width: 0; padding: 24px; max-width: 1080px; }
.page-head { display: flex; align-items: flex-start; gap: 12px; margin-bottom: 18px; flex-wrap: wrap; }
.page-head h1 { font-size: 21px; letter-spacing: -.02em; margin: 0; }
.page-head p { margin: 4px 0 0; font-size: 13px; color: var(--muted); }
.stat-val { font-size: 26px; font-weight: 650; letter-spacing: -.025em; margin-top: 8px; }
.stat-sub { font-size: 12px; color: var(--muted); margin-top: 6px; }
.table-wrap { overflow-x: auto; border-radius: var(--radius); }
table.tbl { width: 100%; border-collapse: collapse; font-size: 13px; }
.tbl th {
  text-align: left; font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: .06em;
  color: var(--faint); padding: 10px 12px; white-space: nowrap; border-bottom: 1px solid var(--line);
}
.tbl td { padding: 11px 12px; border-top: 1px solid var(--line2); color: var(--body); vertical-align: middle; }
.tbl tr:first-child td { border-top: none; }
.tbl tr:hover td { background: #FBFAF8; }
.toolbar { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; margin-bottom: 14px; }
.toolbar .spacer { flex: 1; }
.toast {
  position: fixed; bottom: 18px; right: 18px; z-index: 60; display: flex; flex-direction: column; gap: 8px; width: 330px;
}
.toast-item {
  background: var(--ink); color: #fff; border-radius: 11px; padding: 12px 14px; font-size: 13px;
  box-shadow: 0 12px 32px -12px rgba(20,18,15,.4); animation: fade .18s ease-out;
}
.toast-item.err { background: var(--neg); }
@keyframes fade { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }
.empty { padding: 36px 20px; text-align: center; color: var(--faint); font-size: 13.5px; }
.kv { display: grid; grid-template-columns: 150px 1fr; gap: 6px 12px; font-size: 13px; }
.kv dt { color: var(--faint); }
.kv dd { margin: 0; color: var(--body); }
@media (max-width: 860px) { .side { display: none; } .main { padding: 16px; } }

.folder-tree { margin-top: 14px; border: 1px solid var(--line2); border-radius: 10px; padding: 12px; max-height: 460px; overflow: auto; }
.folder-tree ul { list-style: none; padding-left: 24px; margin: 0; }
.folder-tree > ul { padding-left: 0; }
.folder-row { display: flex; align-items: center; gap: 8px; min-height: 40px; border-radius: 6px; }
.folder-row:hover { background: var(--line2); }
.folder-toggle { width: 30px; height: 30px; border: 0; background: transparent; cursor: pointer; font-size: 18px; color: inherit; }
.folder-label { display: flex; align-items: center; gap: 10px; cursor: pointer; overflow-wrap: anywhere; }
.folder-label input { width: 17px; height: 17px; accent-color: #17795E; flex-shrink: 0; }
.selected-folders { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 12px; }

/* Scan progress + loaders */
.spinner {
  width: 14px; height: 14px; border-radius: 50%; display: inline-block; flex-shrink: 0;
  border: 2px solid rgba(255,255,255,.4); border-top-color: #fff;
  animation: spin .7s linear infinite; vertical-align: -2px;
}
.btn-ghost .spinner, .btn-quiet .spinner { border-color: rgba(26,25,23,.2); border-top-color: var(--ink); }
@keyframes spin { to { transform: rotate(360deg); } }
.progress-track {
  height: 10px; border-radius: 99px; background: var(--sunken); overflow: hidden; margin: 12px 0 8px;
}
.progress-fill {
  height: 100%; border-radius: 99px; background: var(--pos); transition: width .4s ease; min-width: 2%;
}
.progress-fill.indeterminate {
  width: 40% !important; animation: slide 1.2s ease-in-out infinite alternate;
}
@keyframes slide { from { margin-left: -10%; } to { margin-left: 70%; } }
.scan-live {
  border-left: 3px solid var(--pos); padding-left: 14px;
}
.tbl-loading td { text-align: center; color: var(--faint); padding: 28px !important; }

/* Overview stat cards — distinct tones per metric */
.stat-card { position: relative; overflow: hidden; border-top: 3px solid var(--line); }
.stat-card::before { content: ''; position: absolute; top: 0; left: 0; right: 0; height: 3px; }
.stat-top { display: flex; align-items: center; gap: 8px; }
.stat-ico {
  width: 26px; height: 26px; border-radius: 8px; display: grid; place-items: center; font-size: 14px; flex-shrink: 0;
}
.stat-blue { border-top-color: var(--info); background: linear-gradient(180deg, var(--infobg) 0%, var(--surface) 55%); }
.stat-blue .stat-ico { background: var(--infobg); }
.stat-green { border-top-color: var(--pos); background: linear-gradient(180deg, var(--posbg) 0%, var(--surface) 55%); }
.stat-green .stat-ico { background: var(--posbg); }
.stat-pos { border-top-color: var(--pos); background: linear-gradient(180deg, var(--posbg) 0%, var(--surface) 55%); }
.stat-pos .stat-ico { background: var(--pos); color: #fff; }
.stat-amber { border-top-color: var(--warn); background: linear-gradient(180deg, var(--warnbg) 0%, var(--surface) 55%); }
.stat-amber .stat-ico { background: var(--warnbg); }
.stat-red { border-top-color: var(--neg); background: linear-gradient(180deg, var(--negbg) 0%, var(--surface) 55%); }
.stat-red .stat-ico { background: var(--negbg); }
.stat-neutral { border-top-color: var(--line); }
.stat-neutral .stat-ico { background: var(--sunken); }

/* Modals / dialogs */
.modal-backdrop {
  position: fixed; inset: 0; z-index: 50; background: rgba(26,25,23,.45);
  display: flex; align-items: center; justify-content: center; padding: 20px;
}
.modal-backdrop[hidden] { display: none; }
.modal { background: var(--surface); border-radius: 14px; border: 1px solid var(--line); width: 100%; max-width: 560px; max-height: 90vh; display: flex; flex-direction: column; box-shadow: 0 24px 64px -16px rgba(20,18,15,.35); }
.modal-lg { max-width: 880px; }
.modal-xl { max-width: 1100px; width: calc(100vw - 48px); }
.modal-above { z-index: 60; }
.modal-head { display: flex; align-items: flex-start; gap: 10px; padding: 16px 18px 0; }
.modal-body { padding: 14px 18px 18px; overflow: auto; }
.tabs { display: flex; gap: 6px; padding: 12px 18px 0; border-bottom: 1px solid var(--line2); }
.tab-btn { border: none; background: none; padding: 9px 12px; font-size: 13px; font-weight: 600; color: var(--muted); cursor: pointer; border-bottom: 2px solid transparent; }
.tab-btn.on { color: var(--ink); border-bottom-color: var(--ink); }
pre.json { background: var(--sunken); border: 1px solid var(--line2); border-radius: 10px; padding: 12px; font-size: 12px; line-height: 1.55; overflow: auto; max-height: 260px; white-space: pre-wrap; word-break: break-word; margin: 0; }

/* File viewer */
.viewer-toolbar { display: flex; gap: 8px; align-items: center; padding: 10px 18px 0; flex-wrap: wrap; }
.viewer-toolbar .btn { height: 32px; padding: 0 12px; }
.viewer-stage {
  min-height: 420px; max-height: 70vh; overflow: auto; background: var(--sunken);
  border: 1px solid var(--line2); border-radius: 10px; margin-top: 10px;
  display: grid; place-items: center; padding: 12px;
}
.viewer-stage img.zoom-img { max-width: 100%; max-height: 66vh; object-fit: contain; border-radius: 8px; background: #fff; transform-origin: center center; transition: transform .15s ease; }
.viewer-stage iframe.viewer-frame { width: 100%; height: 68vh; min-height: 520px; border: 0; border-radius: 8px; background: #fff; }
.viewer-stage > * { grid-area: 1 / 1; }
.viewer-loading { display: flex; align-items: center; gap: 10px; z-index: 1; }
.viewer-loading .spinner { border-color: rgba(26,25,23,.2); border-top-color: var(--ink); }
.copy-btn { height: 28px; padding: 0 10px; font-size: 12px; }

/* Drive preview grid */
.file-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: 12px; }
.file-grid .empty { grid-column: 1 / -1; }
.file-card {
  background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 10px;
  display: flex; flex-direction: column; gap: 6px; cursor: pointer; text-align: left; font: inherit; color: inherit;
}
.file-card:hover { background: var(--sunken); border-color: #C9C3B8; }
.file-thumb { height: 110px; border-radius: 8px; background: var(--sunken); display: grid; place-items: center; overflow: hidden; }
.file-thumb img { width: 100%; height: 100%; object-fit: cover; }
.file-ico { font-size: 34px; }
.file-name { font-size: 12.5px; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.file-meta { font-size: 11px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* Connection status helpers */
.conn-ok { display: flex; gap: 10px; align-items: flex-start; background: var(--posbg); border: 1px solid #CBE5D8; border-radius: 10px; padding: 12px; }
.conn-err { background: var(--negbg); border: 1px solid #F0D9D6; border-radius: 10px; padding: 12px; color: var(--neg); }
.conn-dot { width: 10px; height: 10px; border-radius: 50%; background: var(--pos); margin-top: 4px; flex-shrink: 0; box-shadow: 0 0 0 4px rgba(23,121,94,.15); }

/* Invoice review keeps the original document beside payment candidates. */
#invoiceModal .modal { max-width: 1500px; }
.invoice-review-layout { display: grid; grid-template-columns: minmax(0, 1.45fr) minmax(330px, 1fr); min-height: 0; overflow: auto; }
.invoice-document { min-width: 0; }
.match-review-panel { padding: 18px; border-left: 1px solid var(--line, #ddd); overflow: auto; max-height: 75vh; }
.match-review-actions { display: flex; gap: 8px; flex-wrap: wrap; margin: 12px 0; }
.match-summary { padding: 12px; background: var(--surface, #f5f4f1); border-radius: 8px; }
.match-summary > strong { margin-left: 8px; }
.payment-candidate { border: 1px solid var(--line, #ddd); border-radius: 10px; padding: 14px; margin: 12px 0; }
.candidate-heading { display: flex; justify-content: space-between; gap: 8px; margin-bottom: 8px; }
.signal-list { display: flex; flex-wrap: wrap; gap: 6px; margin: 10px 0; }
.signal-match { color: var(--pos); font-size: 12px; }
.candidate-conflicts { color: var(--neg); padding-left: 18px; }
.candidate-json { white-space: pre-wrap; overflow-wrap: anywhere; max-height: 220px; overflow: auto; font-size: 11px; }
.payment-candidate > button { margin-top: 12px; width: 100%; }
@media (max-width: 850px) {
  .invoice-review-layout { display: block; }
  .match-review-panel { border-left: 0; border-top: 1px solid var(--line, #ddd); max-height: none; }
  #invoiceModal .viewer-stage iframe.viewer-frame { min-height: 360px; height: 48vh; }
}
