:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --border: #e3e6ea;
  --text: #16191d;
  --muted: #6b7280;
  --accent: #3b5bdb;
  --accent-soft: #eef1fd;
  --good: #17803d;
  --bad: #b42318;
  --warn: #b54708;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .05), 0 1px 3px rgba(16, 24, 40, .06);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1115;
    --surface: #171a1f;
    --border: #272b33;
    --text: #e8eaed;
    --muted: #9aa3af;
    --accent: #7d92f5;
    --accent-soft: #1c2233;
    --good: #4ade80;
    --bad: #f87171;
    --warn: #fbbf24;
    --shadow: none;
  }
}

* { box-sizing: border-box; }

/* Several rules below (.view, .modal-overlay, .login-screen, .progress) set
   an explicit `display` on elements that are also toggled via the `hidden`
   attribute (login gating, tab switching, the Add User modal). Author CSS
   always wins over the user-agent stylesheet's `[hidden] { display: none }`
   default, regardless of specificity -- so without this rule, `el.hidden =
   true` stops visually hiding anything once a later rule sets `display` on
   that element. This keeps `hidden` authoritative no matter what else
   targets the element. */
[hidden] { display: none !important; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 14px 24px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
}

.brand { display: flex; align-items: center; gap: 10px; }
.brand h1 { font-size: 16px; font-weight: 600; margin: 0; letter-spacing: -.01em; }
.mark {
  width: 22px; height: 22px; border-radius: 6px;
  background: linear-gradient(135deg, var(--accent), #8b5cf6);
}

.status { display: flex; align-items: center; gap: 8px; font-size: 13px; color: var(--muted); }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); flex: none; }
.dot.ok { background: var(--good); }
.dot.err { background: var(--bad); }

main {
  max-width: 1240px;
  margin: 0 auto;
  padding: 24px;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.stats {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 12px;
}

.stat {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.stat-label { font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.stat-value { font-size: 22px; font-weight: 600; font-variant-numeric: tabular-nums; letter-spacing: -.02em; }

.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}

.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 10px;
  padding: 14px 18px;
  border-bottom: 1px solid var(--border);
}

.panel-head h2 { font-size: 14px; font-weight: 600; margin: 0; }
.hint { font-size: 12.5px; color: var(--muted); }

/* Dropzone */
.dropzone {
  margin: 18px;
  padding: 34px 20px;
  border: 1.5px dashed var(--border);
  border-radius: var(--radius);
  text-align: center;
  cursor: pointer;
  transition: border-color .15s, background .15s;
  outline: none;
}
.dropzone:hover, .dropzone:focus-visible { border-color: var(--accent); background: var(--accent-soft); }
.dropzone.dragging { border-color: var(--accent); background: var(--accent-soft); border-style: solid; }
.dropzone.busy { opacity: .55; pointer-events: none; }

.dz-icon { width: 30px; height: 30px; color: var(--muted); margin-bottom: 8px; }
.dz-title { margin: 0; font-weight: 550; font-size: 14.5px; }
.dz-sub { margin: 4px 0 0; font-size: 13px; color: var(--muted); }

.linkbtn {
  background: none; border: none; padding: 0;
  color: var(--accent); font: inherit; cursor: pointer; text-decoration: underline;
}

/* Manual entry form */
.entry-form {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 14px;
  padding: 18px;
}
.field { display: flex; flex-direction: column; gap: 5px; min-width: 0; }
.field.wide { grid-column: span 2; }
.field.actions { flex-direction: row; align-items: center; justify-content: flex-end; gap: 8px; }
.field label { font-size: 12.5px; color: var(--muted); }
.req { color: var(--bad); }
.field input, .field select {
  font: inherit;
  font-size: 13.5px;
  padding: 7px 10px;
  border: 1px solid var(--border);
  border-radius: 7px;
  background: var(--surface);
  color: var(--text);
  min-width: 0;
}
.field input:focus, .field select:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

.btn {
  background: var(--accent);
  color: #fff;
  border: none;
  padding: 8px 16px;
  border-radius: 7px;
  font: inherit;
  font-size: 13.5px;
  font-weight: 550;
  cursor: pointer;
  transition: opacity .12s;
}
.btn:hover:not(:disabled) { opacity: .9; }
.btn:disabled { opacity: .5; cursor: default; }

@media (max-width: 900px) {
  .entry-form { grid-template-columns: repeat(2, 1fr); }
  .field.wide { grid-column: span 2; }
}
@media (max-width: 560px) {
  .entry-form { grid-template-columns: 1fr; }
  .field.wide { grid-column: span 1; }
}

/* Transaction list filters */
.filter-bar {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 12px;
  padding: 14px 18px;
  border-bottom: 1px solid var(--border);
  background: var(--surface-2, transparent);
}
.filter-bar .field.actions { align-items: flex-end; justify-content: flex-start; }
@media (max-width: 700px) {
  .filter-bar { grid-template-columns: repeat(2, 1fr); }
}

/* Progress */
.progress { margin: 0 18px 18px; display: flex; align-items: center; gap: 10px; font-size: 13px; color: var(--muted); }
.progress .bar {
  flex: 1; height: 4px; border-radius: 2px; background: var(--border);
  position: relative; overflow: hidden;
}
.progress .bar::after {
  content: ""; position: absolute; inset: 0; width: 35%;
  background: var(--accent); border-radius: 2px;
  animation: slide 1.1s ease-in-out infinite;
}
@keyframes slide { 0% { left: -35%; } 100% { left: 100%; } }

/* Result */
.result { margin: 0 18px 18px; font-size: 13.5px; }
.result .summary { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 10px; }
.pill {
  padding: 3px 10px; border-radius: 20px; font-size: 12.5px; font-weight: 550;
  border: 1px solid var(--border);
}
.pill.ok { color: var(--good); border-color: color-mix(in srgb, var(--good) 35%, transparent); }
.pill.warn { color: var(--warn); border-color: color-mix(in srgb, var(--warn) 35%, transparent); }
.pill.bad { color: var(--bad); border-color: color-mix(in srgb, var(--bad) 35%, transparent); }

.result details { margin-top: 8px; }
.result summary { cursor: pointer; color: var(--muted); font-size: 13px; }
.result ul { margin: 8px 0 0; padding-left: 18px; color: var(--muted); max-height: 220px; overflow-y: auto; }
.result li { margin: 2px 0; }
.result .fail {
  color: var(--bad);
  border: 1px solid color-mix(in srgb, var(--bad) 30%, transparent);
  background: color-mix(in srgb, var(--bad) 7%, transparent);
  padding: 10px 12px; border-radius: 8px;
}

/* Table */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 13.5px; }
thead th {
  text-align: left; font-weight: 550; font-size: 12px; color: var(--muted);
  text-transform: uppercase; letter-spacing: .04em;
  padding: 10px 14px; border-bottom: 1px solid var(--border); white-space: nowrap;
}
tbody td { padding: 10px 14px; border-bottom: 1px solid var(--border); }
tbody tr:last-child td { border-bottom: none; }
tbody tr:hover { background: var(--accent-soft); }
.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
td.num.neg { color: var(--bad); }
td.desc { max-width: 320px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.center { text-align: center; }
.ghost.small { padding: 3px 9px; font-size: 12px; }
.row-error { display: block; font-size: 11px; color: var(--bad); margin-top: 3px; white-space: normal; }
.tag {
  display: inline-block; padding: 2px 8px; border-radius: 5px;
  background: var(--accent-soft); font-size: 12.5px; white-space: nowrap;
}
.tag.type-income { background: color-mix(in srgb, var(--good) 15%, transparent); color: var(--good); }
.tag.type-expense { background: color-mix(in srgb, var(--bad) 12%, transparent); color: var(--bad); }
.dash { color: var(--muted); }
tr.empty td { text-align: center; color: var(--muted); padding: 32px 14px; }

/* Recent transactions: fixed layout so long values (e.g. UPI refs) can't widen the table */
#txTable { table-layout: fixed; }
#txTable th, #txTable td { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#txTable td.desc { max-width: none; }
#txTable .tag { max-width: 100%; overflow: hidden; text-overflow: ellipsis; vertical-align: middle; }
#txTable tr.empty td { white-space: normal; }
#txTable .col-date { width: 112px; }
#txTable .col-type { width: 96px; }
#txTable .col-tag { width: 120px; }
#txTable .col-amount { width: 156px; }
#txTable .col-reviewed { width: 96px; }
#txTable .col-actions { width: 72px; }
/* Lead ID / Brand / Category / Purpose / Party: main is capped at 1240px, so there is
   never room for them alongside the rest; still visible in the Edit form. */
#txTable .col-secondary { display: none; }
@media (max-width: 1100px) {
  #txTable .col-md { display: none; }
}
@media (max-width: 860px) {
  #txTable .col-sm { display: none; }
}
@media (max-width: 640px) {
  #txTable .col-date { width: 96px; }
  #txTable .col-type { width: 84px; }
  #txTable .col-amount { width: 124px; }
  #txTable .col-reviewed { width: 64px; }
  #txTable .col-actions { width: 60px; }
}

/* Buttons */
.pager { display: flex; align-items: center; gap: 8px; }
.ghost {
  background: none; border: 1px solid var(--border); color: var(--text);
  padding: 5px 11px; border-radius: 7px; font: inherit; font-size: 13px; cursor: pointer;
  transition: background .12s, border-color .12s;
}
.ghost:hover:not(:disabled) { border-color: var(--accent); color: var(--accent); }
.ghost:disabled { opacity: .45; cursor: default; }

@media (max-width: 640px) {
  main { padding: 16px; }
  .topbar { padding: 12px 16px; }
  td.desc { max-width: 160px; }
}

/* Column mapping report (upload result) */
.mapping { list-style: none; margin: 8px 0 0; padding: 0; display: grid; gap: 4px; }
.mapping li { font-size: 13px; display: flex; align-items: baseline; gap: 2px; flex-wrap: wrap; }
.mapping code {
  background: var(--accent-soft); padding: 1px 6px; border-radius: 5px;
  font-size: 12.5px;
}
.map-exact, .map-alias { color: var(--muted); }
.map-fuzzy { color: var(--warn); font-weight: 500; }
.map-ignored { color: var(--muted); opacity: .75; }
.warn-line {
  margin-top: 8px; font-size: 13px; color: var(--warn);
  border-left: 2px solid var(--warn); padding-left: 10px;
}

/* ---------- Login screen ---------- */
.login-screen {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}
.login-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 28px 26px;
  width: 100%;
  max-width: 340px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}
.brand.center { justify-content: center; margin-bottom: 4px; }
.btn.wide { width: 100%; padding: 9px 16px; }

/* ---------- Top bar: nav + user menu ---------- */
.topbar { flex-wrap: wrap; row-gap: 10px; }
.topbar-right { display: flex; align-items: center; gap: 16px; }

.tabs { display: flex; gap: 4px; flex-wrap: wrap; }
.tab {
  background: none; border: 1px solid transparent; color: var(--muted);
  padding: 7px 12px; border-radius: 7px; font: inherit; font-size: 13px;
  font-weight: 550; cursor: pointer; white-space: nowrap;
  transition: background .12s, color .12s;
}
.tab:hover { color: var(--text); background: var(--accent-soft); }
.tab.active { color: var(--accent); background: var(--accent-soft); }

.user-menu { display: flex; align-items: center; gap: 10px; }
.user-chip { font-size: 13px; color: var(--muted); }

.view { display: flex; flex-direction: column; gap: 20px; }

/* ---------- Dashboard quick actions ---------- */
.quick-actions { display: flex; gap: 10px; flex-wrap: wrap; padding: 18px; }

/* ---------- Charts ---------- */
.chart-wrap { padding: 18px; }

/* ---------- Status pills for user roles/status ---------- */
.pill.role { color: var(--accent); border-color: color-mix(in srgb, var(--accent) 35%, transparent); }
select.role-select, select.months-select {
  font: inherit; font-size: 13px; padding: 5px 8px;
  border: 1px solid var(--border); border-radius: 6px;
  background: var(--surface); color: var(--text);
}
.status-toggle {
  border: none; border-radius: 20px; padding: 3px 12px; font-size: 12.5px;
  font-weight: 550; cursor: pointer; border: 1px solid var(--border);
}
.status-toggle.active { color: var(--good); border-color: color-mix(in srgb, var(--good) 35%, transparent); }
.status-toggle.suspended { color: var(--bad); border-color: color-mix(in srgb, var(--bad) 35%, transparent); }

/* ---------- Modal ---------- */
.modal-overlay {
  position: fixed; inset: 0; background: rgba(15, 17, 21, .45);
  display: flex; align-items: center; justify-content: center;
  padding: 20px; z-index: 100;
}
.modal {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow);
  width: 100%; max-width: 400px; max-height: 90vh; overflow-y: auto;
}
.modal .panel-head { border-bottom: 1px solid var(--border); }
.modal form { display: flex; flex-direction: column; gap: 14px; padding: 18px; }
.modal .field.actions { justify-content: flex-end; }

@media (max-width: 640px) {
  .tabs { order: 3; width: 100%; }
}

/* ---------- Duplicate detection ---------- */
.banner {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 10px 16px; border-radius: var(--radius); font-size: 13.5px;
}
.banner.warn {
  background: color-mix(in srgb, var(--warn) 12%, transparent);
  border: 1px solid color-mix(in srgb, var(--warn) 35%, transparent);
  color: var(--warn);
}
.banner .linkbtn { color: inherit; font-weight: 600; }

.dup-badge {
  display: inline-block; margin-bottom: 3px; padding: 1px 7px; border-radius: 5px;
  background: color-mix(in srgb, var(--warn) 15%, transparent); color: var(--warn);
  font-size: 11px; font-weight: 600; white-space: nowrap;
}
tr.duplicate-row { background: color-mix(in srgb, var(--warn) 6%, transparent); }

.modal.wide { max-width: 720px; }
.dup-toolbar { display: flex; align-items: center; gap: 8px; }
.btn.small { padding: 5px 11px; font-size: 12.5px; }
.dup-groups { padding: 18px; display: flex; flex-direction: column; gap: 18px; max-height: 70vh; overflow-y: auto; }
.dup-group { border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; }
.dup-group-head {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  padding: 10px 14px; background: var(--accent-soft); font-size: 12.5px;
  font-weight: 600; color: var(--muted);
}
.dup-compare { width: 100%; border-collapse: collapse; font-size: 13px; }
.dup-compare th, .dup-compare td { padding: 8px 12px; border-bottom: 1px solid var(--border); text-align: left; }
.dup-compare tr:last-child td { border-bottom: none; }
.dup-row-actions { display: flex; gap: 6px; }
.dup-row-actions button { white-space: nowrap; }
