:root {
  --bg: #0f1216;
  --panel: #181d24;
  --line: #28303a;
  --text: #e6edf3;
  --muted: #8b97a5;
  --accent: #2f81f7;
  --good: #3fb950;
  --bad: #f85149;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
}

header {
  padding: 2rem 1.5rem 1rem;
  border-bottom: 1px solid var(--line);
}

header h1 { margin: 0; font-size: 1.6rem; }
header .ph {
  color: var(--accent);
  font-weight: 800;
}
.tagline { margin: 0.25rem 0 0; color: var(--muted); font-size: 0.9rem; }

main { max-width: 820px; margin: 0 auto; padding: 1.5rem; }

.controls { margin-bottom: 1.5rem; }
.controls label { color: var(--muted); margin-right: 0.5rem; }
select {
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0.5rem 0.75rem;
  font-size: 1rem;
}

.cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 1rem;
  margin-bottom: 2rem;
}

.card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 1.25rem;
}
.card h3 { margin: 0 0 0.5rem; font-size: 0.8rem; text-transform: uppercase; letter-spacing: 0.05em; color: var(--muted); }
.big { font-size: 1.6rem; font-weight: 700; margin: 0; }
.sub { margin: 0.4rem 0 0; color: var(--muted); font-size: 0.85rem; }

.verdict { font-size: 1.4rem; font-weight: 700; margin: 0; text-transform: capitalize; }
.verdict.surplus { color: var(--good); }
.verdict.deficit { color: var(--bad); }
.verdict.break-even { color: var(--muted); }

.health { font-size: 1.3rem; font-weight: 700; margin: 0; }
.health.ok { color: var(--good); }
.health.bad { color: var(--bad); }

h2 { font-size: 1.1rem; margin: 1.5rem 0 0.75rem; }

table { width: 100%; border-collapse: collapse; }
th, td { padding: 0.6rem 0.5rem; text-align: left; border-bottom: 1px solid var(--line); }
th { color: var(--muted); font-size: 0.8rem; text-transform: uppercase; letter-spacing: 0.05em; }
.num { text-align: right; font-variant-numeric: tabular-nums; }

.empty { color: var(--muted); }
.empty code { background: var(--panel); padding: 0.15rem 0.4rem; border-radius: 5px; }

.hidden { display: none; }

/* ---- Tabs ---- */
.tabs { margin-top: 1rem; display: flex; gap: 0.5rem; }
.tab {
  background: transparent;
  color: var(--muted);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0.4rem 1rem;
  font-size: 0.9rem;
  cursor: pointer;
}
.tab.active { color: var(--text); border-color: var(--accent); background: var(--panel); }

/* ---- Form ---- */
fieldset {
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 1.25rem;
  margin-bottom: 1.25rem;
  background: var(--panel);
}
legend { padding: 0 0.5rem; color: var(--text); font-weight: 600; }
legend .muted, .muted { color: var(--muted); font-weight: 400; }

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 0.75rem;
}
label { display: flex; flex-direction: column; gap: 0.25rem; font-size: 0.85rem; color: var(--muted); }

input, .row select {
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0.5rem 0.6rem;
  font-size: 0.95rem;
}
input:focus, select:focus { outline: 1px solid var(--accent); border-color: var(--accent); }

.row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  align-items: center;
  margin-bottom: 0.6rem;
}
.row input { flex: 1 1 120px; min-width: 0; }
.row select { flex: 0 0 auto; }
.bank-row { padding-bottom: 0.6rem; border-bottom: 1px dashed var(--line); }
.splits { width: 100%; padding-left: 1.5rem; margin-top: 0.4rem; }
.split-row input { flex: 1 1 140px; }

button.ghost {
  background: transparent;
  color: var(--muted);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0.4rem 0.7rem;
  font-size: 0.85rem;
  cursor: pointer;
}
button.ghost:hover { color: var(--text); border-color: var(--accent); }

button.primary {
  background: var(--accent);
  color: white;
  border: none;
  border-radius: 8px;
  padding: 0.6rem 1.4rem;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
}

.form-actions { display: flex; align-items: center; gap: 1rem; margin-top: 0.5rem; }
.form-msg { font-size: 0.9rem; }
.form-msg.ok { color: var(--good); }
.form-msg.bad { color: var(--bad); }

/* Import view */
.import-hint { margin: 0 0 0.75rem; line-height: 1.45; }

/* Per-document upload cards */
.upload-card {
  border: 1px solid var(--border, #2a2f3a);
  border-radius: 10px;
  padding: 0.85rem 1rem;
  margin: 0.75rem 0;
  background: rgba(255, 255, 255, 0.02);
}
.upload-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  flex-wrap: wrap;
}
.upload-label { display: flex; align-items: center; gap: 0.5rem; flex-wrap: wrap; }
.upload-label input[type="file"] { margin-left: 0.5rem; }
.badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 1.4rem;
  height: 1.4rem;
  border-radius: 50%;
  background: var(--accent, #3b82f6);
  color: #fff;
  font-size: 0.8rem;
  font-weight: 700;
}
.upload-status { font-size: 0.85rem; color: var(--muted); white-space: nowrap; }
.upload-status.ok { color: var(--good); }
.upload-status.bad { color: var(--bad); }
.extract { margin-top: 0.75rem; }
.extract-summary { margin: 0 0 0.4rem; font-size: 0.85rem; color: var(--muted); }
.extract-table { width: 100%; border-collapse: collapse; font-size: 0.88rem; }
.extract-table th, .extract-table td {
  text-align: left;
  padding: 0.3rem 0.5rem;
  border-bottom: 1px solid var(--border, #2a2f3a);
}
.extract-table th.num, .extract-table td.num { text-align: right; }
.extract-table tr.extract-section td { font-weight: 600; padding-top: 0.5rem; }
.extract-table tr.extract-section td.num { color: #34d399; }
.extract-table td.indent { padding-left: 1.5rem; color: var(--muted); }
.extract-table tr.extract-total td {
  font-weight: 700;
  border-top: 2px solid var(--border, #2a2f3a);
}
.extract-table tr.extract-total td.num { color: #34d399; }
.cat-input {
  width: 100%;
  min-width: 110px;
  padding: 0.2rem 0.4rem;
  font-size: 0.82rem;
  border: 1px solid var(--border, #2a2f3a);
  border-radius: 5px;
  background: var(--input-bg, #1b1f27);
  color: inherit;
}
.cat-input:focus { outline: 2px solid var(--accent, #4f8cff); outline-offset: -1px; }
.edit-input {
  width: 100%;
  padding: 0.2rem 0.4rem;
  font-size: 0.82rem;
  border: 1px solid var(--border, #2a2f3a);
  border-radius: 5px;
  background: var(--input-bg, #1b1f27);
  color: inherit;
}
.edit-input.amt { text-align: right; min-width: 90px; }
.edit-input:focus { outline: 2px solid var(--accent, #4f8cff); outline-offset: -1px; }

#edit-month {
  margin: 1rem 0;
  border: 1px solid var(--border, #2a2f3a);
  border-radius: 8px;
  background: var(--input-bg, #1b1f27);
}
#edit-month > summary {
  cursor: pointer;
  padding: 0.6rem 0.9rem;
  font-weight: 600;
  user-select: none;
}
#edit-month .edit-form { padding: 0 0.9rem 0.9rem; }
#edit-month .payslip-edit {
  margin-top: 0.6rem;
  border: 1px solid var(--border, #2a2f3a);
  border-radius: 6px;
  padding: 0.4rem 0.8rem 0.8rem;
}
button.danger {
  background: #b5202f;
  border: 1px solid #d23;
  color: #fff;
}
button.danger:hover { background: #d23; }

.trend-wrap { overflow-x: auto; }
.trend-table { border-collapse: collapse; min-width: 100%; }
.trend-table th,
.trend-table td {
  padding: 0.4rem 0.7rem;
  white-space: nowrap;
  border-bottom: 1px solid var(--border, #2a2f3a);
}
.trend-table thead th { text-align: left; }
.trend-table th.num,
.trend-table td.num { text-align: right; }
.trend-table tbody td:first-child,
.trend-table thead th:first-child,
.trend-table tfoot th:first-child {
  position: sticky;
  left: 0;
  background: var(--bg, #11141a);
  text-align: left;
}
.trend-table tfoot th { border-top: 2px solid var(--border, #2a2f3a); font-weight: 700; }
.trend-table tbody tr:hover td { background: rgba(255, 255, 255, 0.03); }
.trend-table tr.trend-section td {
  font-weight: 700;
  text-transform: uppercase;
  font-size: 0.72rem;
  letter-spacing: 0.04em;
  color: var(--muted, #8a93a6);
  padding-top: 0.9rem;
}
.trend-table tr.trend-subtotal td {
  font-weight: 700;
  border-top: 1px solid var(--border, #2a2f3a);
}
.trend-table tr.trend-income td { font-weight: 600; }
.trend-table tr.trend-income-amt td.num { color: #34d399; }
.trend-table tr.trend-spend-amt td.num { color: #f87171; }
.trend-table tfoot tr.trend-balance th {
  border-top: none;
  font-weight: 600;
  color: var(--muted, #8a93a6);
}
.trend-table tfoot tr.trend-balance th.num { color: #34d399; }
.trend-table .surplus { color: #34d399; }
.trend-table .deficit { color: #f87171; }
.warnings {
  background: rgba(255, 196, 0, 0.08);
  border: 1px solid rgba(255, 196, 0, 0.35);
  border-radius: 8px;
  padding: 0.75rem 1rem;
  margin: 1rem 0;
  font-size: 0.9rem;
}
.warnings ul { margin: 0.4rem 0 0; padding-left: 1.2rem; }
.payslip-line { color: var(--muted); font-size: 0.92rem; margin: 0.5rem 0 0; }
tr.needs-review { background: rgba(255, 196, 0, 0.06); }
.review {
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: #b58900;
  border: 1px solid rgba(181, 137, 0, 0.5);
  border-radius: 4px;
  padding: 0 4px;
  margin-left: 6px;
}
button:disabled { opacity: 0.5; cursor: not-allowed; }

/* ---- Auth screen ---- */
/* While locked (pre-auth), the app behind the overlay stays hidden. */
body.app-locked > header,
body.app-locked > main { display: none; }

.auth-screen {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1.5rem;
  background:
    radial-gradient(1200px 600px at 50% -10%, rgba(47, 129, 247, 0.18), transparent 60%),
    var(--bg);
}
.auth-screen.hidden { display: none; }

.auth-card {
  width: 100%;
  max-width: 380px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 2rem 1.75rem;
  box-shadow: 0 18px 50px rgba(0, 0, 0, 0.45);
}
.auth-brand { margin: 0; font-size: 1.7rem; text-align: center; }
.auth-brand .ph { color: var(--accent); font-weight: 800; }
.auth-sub {
  margin: 0.35rem 0 1.5rem;
  text-align: center;
  color: var(--muted);
  font-size: 0.9rem;
}

.auth-toggle {
  display: flex;
  gap: 0.4rem;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 0.25rem;
  margin-bottom: 1.5rem;
}
.auth-tab {
  flex: 1;
  background: transparent;
  color: var(--muted);
  border: none;
  border-radius: 8px;
  padding: 0.5rem;
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
}
.auth-tab.active { background: var(--panel); color: var(--text); }

.auth-form { display: flex; flex-direction: column; gap: 1rem; }
.auth-form.hidden { display: none; }
.auth-form label { gap: 0.35rem; }
.auth-form input { font-size: 1rem; padding: 0.6rem 0.7rem; }
.auth-submit { width: 100%; margin-top: 0.25rem; }

.auth-msg { margin: 0; font-size: 0.85rem; min-height: 1.1em; }
.auth-msg.bad { color: var(--bad); }
.auth-msg.ok { color: var(--good); }

.auth-fineprint {
  margin: 1.25rem 0 0;
  text-align: center;
  color: var(--muted);
  font-size: 0.8rem;
}

/* ---- Header user chip ---- */
.header-top {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 1rem;
}
.user-chip {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  font-size: 0.85rem;
  color: var(--muted);
}
.user-chip.hidden { display: none; }
.user-email { max-width: 220px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.logout-btn {
  background: transparent;
  color: var(--muted);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0.35rem 0.8rem;
  font-size: 0.82rem;
  cursor: pointer;
}
.logout-btn:hover { color: var(--text); border-color: var(--accent); }

/* ---- Paywall ---- */
.paywall {
  border: 1px solid var(--accent);
  border-radius: 12px;
  padding: 1.5rem;
  margin-bottom: 1.5rem;
  background:
    radial-gradient(600px 200px at 0% 0%, rgba(47, 129, 247, 0.12), transparent 70%),
    var(--panel);
}
.paywall.hidden { display: none; }
.paywall-title { margin: 0 0 0.5rem; font-size: 1.15rem; }
.paywall-body { margin: 0 0 1rem; color: var(--muted); line-height: 1.5; }
.paywall-actions { display: flex; align-items: center; gap: 1rem; }

/* When the free limit is hit, dim the form behind the paywall. */
#import-form.locked {
  opacity: 0.4;
  pointer-events: none;
  filter: grayscale(0.4);
}

/* ---- Welcome / thank-you modal ---- */
.modal-overlay {
  position: fixed;
  inset: 0;
  z-index: 200;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1.5rem;
  background: rgba(0, 0, 0, 0.6);
  animation: modal-fade 0.18s ease-out;
}
.modal-overlay.hidden { display: none; }

.modal-card {
  width: 100%;
  max-width: 420px;
  text-align: center;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 2.25rem 2rem 2rem;
  box-shadow: 0 18px 50px rgba(0, 0, 0, 0.5);
  animation: modal-pop 0.22s cubic-bezier(0.18, 0.89, 0.32, 1.28);
}
.modal-emoji { font-size: 3rem; line-height: 1; margin-bottom: 0.5rem; }
.modal-title { margin: 0 0 0.6rem; font-size: 1.5rem; }
.modal-body {
  margin: 0 0 1.5rem;
  color: var(--muted);
  line-height: 1.55;
  font-size: 0.95rem;
}
.modal-actions { display: flex; justify-content: center; }
.modal-actions .primary { min-width: 200px; }

@keyframes modal-fade {
  from { opacity: 0; }
  to { opacity: 1; }
}
@keyframes modal-pop {
  from { opacity: 0; transform: translateY(12px) scale(0.96); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}
