/* ============== Tokens ============== */
:root {
  --bg:        #fafaf9;
  --bg-elev:   #ffffff;
  --surface:   #ffffff;
  --surface-2: #f4f4f3;
  --border:    #e7e5e4;
  --border-2:  #d6d3d1;
  --text:      #0c0a09;
  --text-muted:#78716c;
  --text-dim:  #a8a29e;

  --accent:    #4f46e5;
  --accent-2:  #7c3aed;
  --accent-contrast: #ffffff;
  --accent-ghost: #eef2ff;

  --income:    #059669;
  --income-ghost: #d1fae5;
  --expense:   #dc2626;
  --expense-ghost: #fee2e2;

  --warn:      #d97706;

  --radius:    14px;
  --radius-lg: 20px;
  --radius-pill: 999px;

  --shadow-sm: 0 1px 2px rgba(10,10,10,0.04), 0 1px 3px rgba(10,10,10,0.06);
  --shadow:    0 4px 12px rgba(10,10,10,0.06), 0 2px 4px rgba(10,10,10,0.04);
  --shadow-lg: 0 24px 48px rgba(10,10,10,0.12), 0 8px 16px rgba(10,10,10,0.08);

  --safe-top:  env(safe-area-inset-top, 0px);
  --safe-bot:  env(safe-area-inset-bottom, 0px);
  --nav-h:     72px;
}

:root:not([data-theme="light"]) { } /* placeholder for override */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg:        #0a0a0a;
    --bg-elev:   #141414;
    --surface:   #171717;
    --surface-2: #1f1f1f;
    --border:    #262626;
    --border-2:  #333333;
    --text:      #fafafa;
    --text-muted:#a3a3a3;
    --text-dim:  #71717a;
    --accent-ghost: #1e1b4b;
    --income-ghost: #022c22;
    --expense-ghost:#450a0a;
    --shadow-sm: 0 1px 2px rgba(0,0,0,0.3);
    --shadow:    0 4px 12px rgba(0,0,0,0.4);
    --shadow-lg: 0 24px 48px rgba(0,0,0,0.6);
  }
}
:root[data-theme="dark"] {
  --bg:        #0a0a0a;
  --bg-elev:   #141414;
  --surface:   #171717;
  --surface-2: #1f1f1f;
  --border:    #262626;
  --border-2:  #333333;
  --text:      #fafafa;
  --text-muted:#a3a3a3;
  --text-dim:  #71717a;
  --accent-ghost: #1e1b4b;
  --income-ghost: #022c22;
  --expense-ghost:#450a0a;
}

/* ============== Reset ============== */
*, *::before, *::after { box-sizing: border-box; }
html, body { margin: 0; padding: 0; height: 100%; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Inter", "Segoe UI", Roboto, system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  font-size: 16px;
  line-height: 1.45;
  overscroll-behavior-y: contain;
}
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; background: none; border: 0; padding: 0; }
input, select, textarea { background: transparent; border: 0; outline: 0; }
h1, h2, h3, h4 { margin: 0; font-weight: 600; letter-spacing: -0.015em; }
h1 { font-size: 28px; }
h2 { font-size: 17px; }
.muted { color: var(--text-muted); }
.small { font-size: 13px; }
.center { text-align: center; }
[hidden] { display: none !important; }

/* ============== Auth ============== */
.view-auth {
  min-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px 20px calc(24px + var(--safe-bot));
  background: radial-gradient(1200px 600px at 50% -10%, rgba(79,70,229,0.14), transparent 60%), var(--bg);
}
.auth-wrap { width: 100%; max-width: 420px; }
.auth-brand { text-align: center; margin-bottom: 28px; }
.brand-mark {
  width: 56px; height: 56px; margin: 0 auto 14px;
  border-radius: 16px;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  box-shadow: var(--shadow);
  position: relative;
}
.brand-mark::before {
  content: "€"; position: absolute; inset: 0;
  display: grid; place-items: center;
  color: white; font-size: 26px; font-weight: 600;
}
.auth-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 20px;
  box-shadow: var(--shadow);
}
.auth-tabs {
  display: flex;
  background: var(--surface-2);
  border-radius: var(--radius-pill);
  padding: 4px;
  margin-bottom: 20px;
}
.auth-tabs .tab {
  flex: 1; padding: 10px; border-radius: var(--radius-pill);
  color: var(--text-muted); font-weight: 500; font-size: 14px;
  transition: all .18s ease;
}
.auth-tabs .tab.active {
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow-sm);
}
.auth-form { display: none; flex-direction: column; gap: 14px; }
.auth-form.active { display: flex; }
.auth-form label { display: flex; flex-direction: column; gap: 6px; font-size: 13px; color: var(--text-muted); font-weight: 500; }
.auth-form input, .modal input[type="text"], .modal input[type="email"], .modal input[type="password"], .modal input[type="date"], .modal input[type="color"], .list-filters select {
  background: var(--surface-2);
  border: 1px solid var(--border);
  padding: 12px 14px;
  border-radius: var(--radius);
  color: var(--text);
  font-size: 16px;
  transition: border-color .15s ease, background .15s ease;
}
.auth-form input:focus, .modal input:focus {
  border-color: var(--accent);
  background: var(--surface);
}
.auth-form .row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.auth-form .row label { min-width: 0; }
.auth-form .row input { width: 100%; min-width: 0; }
.auth-error { color: var(--expense); font-size: 13px; min-height: 18px; margin: 0; }
.auth-hint { text-align: center; margin-top: 16px; font-size: 13px; }

/* ============== Buttons ============== */
.btn {
  display: inline-flex; align-items: center; justify-content: center;
  gap: 8px; padding: 10px 16px; border-radius: var(--radius);
  font-weight: 500; font-size: 15px;
  transition: transform .08s ease, background .15s ease, color .15s ease, border-color .15s ease;
  border: 1px solid transparent;
}
.btn:active { transform: scale(0.97); }
.btn-primary { background: var(--accent); color: var(--accent-contrast); }
.btn-primary:hover { background: color-mix(in oklab, var(--accent) 92%, white); }
.btn-ghost { background: transparent; color: var(--text); border-color: var(--border); }
.btn-ghost:hover { background: var(--surface-2); }
.btn-danger { background: transparent; color: var(--expense); border-color: var(--expense); }
.btn-danger:hover { background: var(--expense-ghost); }
.btn-lg { padding: 14px 20px; font-size: 16px; }
.btn-sm { padding: 8px 12px; font-size: 13px; }
.btn.full { width: 100%; }
.icon-btn {
  width: 40px; height: 40px; border-radius: 50%;
  display: grid; place-items: center;
  color: var(--text); background: transparent;
  transition: background .15s ease;
}
.icon-btn:hover { background: var(--surface-2); }

/* ============== App shell ============== */
.app {
  min-height: 100dvh;
  padding-top: var(--safe-top);
  padding-bottom: calc(var(--nav-h) + var(--safe-bot) + 12px);
  max-width: 560px;
  margin: 0 auto;
}
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 16px;
  position: sticky; top: 0; z-index: 10;
  background: color-mix(in oklab, var(--bg) 92%, transparent);
  backdrop-filter: saturate(140%) blur(12px);
  -webkit-backdrop-filter: saturate(140%) blur(12px);
  border-bottom: 1px solid var(--border);
}
.topbar-title { text-align: center; }
.month-label { font-weight: 600; font-size: 17px; }

.view { display: none; padding: 16px; animation: fade .22s ease; }
.view.active { display: block; }
@keyframes fade { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }

.section-head {
  display: flex; align-items: baseline; justify-content: space-between;
  margin: 22px 4px 10px;
}

/* ============== Balance card ============== */
.balance-card {
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  color: white;
  border-radius: var(--radius-lg);
  padding: 20px;
  box-shadow: var(--shadow);
  position: relative;
  overflow: hidden;
}
.balance-card::before {
  content: "";
  position: absolute; top: -60px; right: -60px;
  width: 200px; height: 200px;
  background: radial-gradient(circle, rgba(255,255,255,0.18), transparent 60%);
  pointer-events: none;
}
.balance-label { font-size: 13px; opacity: 0.85; text-transform: uppercase; letter-spacing: 0.06em; }
.balance-amount {
  font-size: 40px; font-weight: 600; margin-top: 6px;
  letter-spacing: -0.03em;
  font-variant-numeric: tabular-nums;
}
.balance-split {
  display: grid; grid-template-columns: 1fr 1fr; gap: 12px;
  margin-top: 18px;
  padding-top: 14px;
  border-top: 1px solid rgba(255,255,255,0.18);
}
.split-item { }
.split-label { font-size: 12px; opacity: 0.85; display: flex; align-items: center; gap: 6px; }
.split-value { font-size: 20px; font-weight: 500; margin-top: 2px; font-variant-numeric: tabular-nums; }
.dot { width: 8px; height: 8px; border-radius: 50%; display: inline-block; }
.dot-income { background: #a7f3d0; }
.dot-expense { background: #fecaca; }

/* ============== Cards ============== */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  overflow: hidden;
}
.list-card .row-item {
  display: grid;
  grid-template-columns: 40px 1fr auto;
  align-items: center;
  gap: 12px;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
}
.list-card .row-item:last-child { border-bottom: 0; }
.cat-icon {
  width: 40px; height: 40px;
  display: grid; place-items: center;
  border-radius: 12px;
  font-size: 20px;
}
.row-main { min-width: 0; }
.row-title { font-weight: 500; }
.row-sub { font-size: 12.5px; color: var(--text-muted); margin-top: 2px; }
.row-amount { font-weight: 600; font-variant-numeric: tabular-nums; }

.progress {
  height: 6px; background: var(--surface-2);
  border-radius: 3px; overflow: hidden; margin-top: 6px;
}
.progress-fill { height: 100%; background: var(--accent); transition: width .4s ease; }
.progress-fill.danger { background: var(--expense); }
.progress-fill.warn { background: var(--warn); }

/* ============== By-member bars ============== */
#by-member { padding: 14px 16px; display: flex; flex-direction: column; gap: 14px; }
.member-row { display: flex; align-items: center; gap: 12px; }
.member-avatar {
  width: 36px; height: 36px; border-radius: 50%;
  display: grid; place-items: center;
  color: white; font-weight: 600; font-size: 14px;
  flex-shrink: 0;
}
.member-main { flex: 1; min-width: 0; }
.member-head { display: flex; justify-content: space-between; font-size: 14px; }

/* ============== Chart ============== */
.chart-card { padding: 16px; }
.chart-card svg { width: 100%; display: block; }

/* ============== Lists (gastos) ============== */
.list { display: flex; flex-direction: column; gap: 8px; margin-top: 10px; }
.small-list { gap: 6px; }
.expense-item {
  display: grid;
  grid-template-columns: 40px 1fr auto;
  gap: 12px;
  align-items: center;
  padding: 12px 14px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  transition: transform .08s ease;
  cursor: pointer;
}
.expense-item:active { transform: scale(0.99); }
.expense-main { min-width: 0; }
.expense-title { font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.expense-sub { font-size: 12.5px; color: var(--text-muted); margin-top: 2px; display: flex; gap: 8px; align-items: center; }
.pill {
  display: inline-flex; align-items: center;
  padding: 2px 8px; border-radius: 999px;
  font-size: 11.5px; font-weight: 500;
  background: var(--surface-2);
  color: var(--text-muted);
}
.pill.pill-member { color: white; }
.expense-amount { font-weight: 600; font-variant-numeric: tabular-nums; color: var(--expense); }

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

.list-filters { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-bottom: 12px; }
.list-filters select { font-size: 14px; padding: 10px 12px; }

.empty { padding: 32px 20px; text-align: center; font-size: 14px; }

/* ============== Bottom nav ============== */
.bottom-nav {
  position: fixed; left: 0; right: 0; bottom: 0;
  height: calc(var(--nav-h) + var(--safe-bot));
  padding-bottom: var(--safe-bot);
  display: grid;
  grid-template-columns: 1fr 1fr 80px 1fr 1fr;
  align-items: center;
  background: color-mix(in oklab, var(--bg-elev) 96%, transparent);
  border-top: 1px solid var(--border);
  backdrop-filter: saturate(140%) blur(12px);
  -webkit-backdrop-filter: saturate(140%) blur(12px);
  z-index: 20;
}
.nav-btn {
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  color: var(--text-muted);
  padding: 6px;
  height: 100%;
  transition: color .15s ease;
}
.nav-btn svg { width: 22px; height: 22px; }
.nav-btn span { font-size: 11px; }
.nav-btn.active { color: var(--accent); }
.nav-fab {
  justify-self: center;
  width: 56px; height: 56px;
  margin-top: -18px;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  color: white;
  box-shadow: 0 10px 20px rgba(79,70,229,0.4), 0 4px 8px rgba(0,0,0,0.1);
  display: grid; place-items: center;
  transition: transform .12s ease;
}
.nav-fab:active { transform: scale(0.94); }
.nav-fab svg { width: 24px; height: 24px; }

/* ============== Modal ============== */
.modal {
  position: fixed; inset: 0; z-index: 100;
  display: flex; flex-direction: column; justify-content: flex-end;
}
.modal-backdrop {
  position: absolute; inset: 0;
  background: rgba(0,0,0,0.4);
  backdrop-filter: blur(4px);
  animation: fadeBg .18s ease;
}
@keyframes fadeBg { from { opacity: 0; } to { opacity: 1; } }
.modal-sheet {
  position: relative;
  background: var(--bg-elev);
  border-top-left-radius: 24px;
  border-top-right-radius: 24px;
  padding: 10px 20px calc(20px + var(--safe-bot));
  max-height: 92dvh;
  overflow-y: auto;
  animation: slideUp .25s cubic-bezier(.2,.9,.3,1);
  box-shadow: var(--shadow-lg);
  max-width: 560px;
  margin: 0 auto;
  width: 100%;
}
@keyframes slideUp { from { transform: translateY(100%); } to { transform: none; } }
.sheet-handle {
  width: 42px; height: 5px;
  background: var(--border-2);
  border-radius: 3px;
  margin: 4px auto 14px;
}
.modal-sheet h3 { margin-bottom: 16px; font-size: 18px; }
.modal-sheet form { display: flex; flex-direction: column; gap: 14px; }
.modal-sheet label { display: flex; flex-direction: column; gap: 6px; font-size: 13px; color: var(--text-muted); font-weight: 500; }
.modal-sheet input[type="text"],
.modal-sheet input[type="email"],
.modal-sheet input[type="password"],
.modal-sheet input[type="date"] {
  background: var(--surface-2);
  border: 1px solid var(--border);
  padding: 12px 14px;
  border-radius: var(--radius);
  color: var(--text);
  font-size: 16px;
}
.modal-sheet input[type="color"] {
  width: 56px; height: 40px;
  padding: 2px; border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  cursor: pointer;
}

.amount-field {
  display: flex; align-items: center; gap: 4px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px;
}
.amount-field .currency { color: var(--text-muted); font-size: 24px; font-weight: 500; }
.amount-field input {
  flex: 1;
  background: transparent;
  font-size: 28px; font-weight: 600;
  letter-spacing: -0.02em;
  color: var(--text);
  font-variant-numeric: tabular-nums;
}
.amount-field:focus-within { border-color: var(--accent); }
.amount-field-income:focus-within { border-color: var(--income); }

.chip-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(100px, 1fr));
  gap: 6px;
}
.chip-grid.cols-2 { grid-template-columns: 1fr 1fr; }
.chip {
  display: flex; align-items: center; gap: 6px;
  padding: 10px 12px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 14px;
  transition: all .15s ease;
  cursor: pointer;
}
.chip-icon { font-size: 18px; }
.chip.selected {
  border-color: var(--accent);
  background: var(--accent-ghost);
  color: var(--accent);
  font-weight: 500;
}

.sheet-actions { display: flex; gap: 10px; margin-top: 8px; }
.sheet-actions .btn { flex: 1; }

/* ============== Toast ============== */
.toast {
  position: fixed; bottom: calc(var(--nav-h) + var(--safe-bot) + 16px); left: 50%;
  transform: translateX(-50%);
  background: var(--text);
  color: var(--bg);
  padding: 10px 16px;
  border-radius: var(--radius-pill);
  font-size: 14px;
  z-index: 200;
  box-shadow: var(--shadow-lg);
  animation: fadeInDown .2s ease;
}
@keyframes fadeInDown { from { opacity: 0; transform: translate(-50%, 6px); } to { opacity: 1; transform: translate(-50%, 0); } }

/* ============== Safe tweaks ============== */
@supports (padding-top: env(safe-area-inset-top)) {
  .topbar { padding-top: calc(12px + var(--safe-top)); }
}

/* ============== Segment control (auth) ============== */
.seg {
  display: flex;
  background: var(--surface-2);
  border-radius: var(--radius-pill);
  padding: 4px;
  margin-bottom: 14px;
  gap: 4px;
}
.seg-btn {
  flex: 1;
  background: transparent;
  border: 0;
  padding: 10px 12px;
  border-radius: var(--radius-pill);
  font-size: 13px;
  font-weight: 600;
  color: var(--text-muted);
  cursor: pointer;
  transition: all .15s ease;
}
.seg-btn.active {
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow-sm);
}

/* ============== Invite / household box ============== */
.invite-box { padding: 16px; }
.invite-label {
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-muted);
  margin-bottom: 10px;
}
.invite-row {
  display: flex;
  align-items: center;
  gap: 12px;
}
.invite-row code {
  font-size: 22px;
  font-weight: 700;
  letter-spacing: 3px;
  background: var(--accent-ghost);
  color: var(--accent);
  padding: 10px 16px;
  border-radius: var(--radius);
  font-family: 'SF Mono', Menlo, Consolas, monospace;
  flex: 1;
  text-align: center;
}

/* ============== Member row: income vs expense ============== */
.member-row-balance { padding: 14px 16px; }
.member-breakdown {
  display: flex;
  gap: 8px;
  margin-top: 6px;
  flex-wrap: wrap;
}
.tag {
  display: inline-flex;
  align-items: center;
  font-size: 12px;
  font-weight: 600;
  padding: 3px 10px;
  border-radius: var(--radius-pill);
}
.tag-income { background: var(--income-ghost); color: var(--income); }
.tag-expense { background: var(--expense-ghost); color: var(--expense); }

/* ============== Scope toggle (Mês/Ano) ============== */
.scope-toggle {
  display: inline-flex;
  background: var(--surface-2);
  border-radius: var(--radius-pill);
  padding: 3px;
  gap: 2px;
  margin-top: 4px;
}
.scope-btn {
  background: transparent;
  border: 0;
  padding: 4px 12px;
  border-radius: var(--radius-pill);
  font-size: 11px;
  font-weight: 600;
  color: var(--text-muted);
  cursor: pointer;
  transition: all .15s ease;
}
.scope-btn.active { background: var(--surface); color: var(--text); box-shadow: var(--shadow-sm); }

/* ============== Project avatar (dashboard) ============== */
.project-avatar {
  width: 40px; height: 40px;
  display: flex; align-items: center; justify-content: center;
  font-size: 20px;
  border-radius: var(--radius);
  flex-shrink: 0;
}

.expense-amount-strong { color: var(--expense); }
.pill-warn { background: var(--warn); color: white; }

/* ============== Importer modal ============== */
.modal-sheet-wide { max-width: 720px; width: 95%; }
#import-raw {
  width: 100%;
  min-height: 160px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-family: 'SF Mono', Menlo, Consolas, monospace;
  font-size: 11px;
  background: var(--surface-2);
  color: var(--text);
  resize: vertical;
  box-sizing: border-box;
}
.import-summary {
  padding: 8px 12px;
  background: var(--accent-ghost);
  color: var(--accent);
  border-radius: var(--radius);
  margin-bottom: 10px;
  font-weight: 500;
}
.import-preview {
  max-height: 55vh;
  overflow-y: auto;
  border: 1px solid var(--border);
  border-radius: var(--radius);
}
.import-row {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
}
.import-row:last-child { border-bottom: 0; }
.import-row.needs-review { background: color-mix(in srgb, var(--warn) 8%, transparent); }
.import-row input[type=checkbox] { margin-top: 6px; }
.import-main { flex: 1; min-width: 0; }
.import-desc { font-weight: 500; font-size: 13px; margin-bottom: 4px; }
.import-meta { display: flex; gap: 6px; flex-wrap: wrap; font-size: 11px; color: var(--text-muted); align-items: center; margin-bottom: 6px; }
.import-selects { display: flex; gap: 6px; }
.import-selects select { flex: 1; font-size: 11px; padding: 4px 6px; border: 1px solid var(--border); border-radius: 6px; background: var(--surface); color: var(--text); }
.import-amount { font-weight: 700; font-variant-numeric: tabular-nums; font-size: 14px; white-space: nowrap; color: var(--expense); }
.import-amount.income-amount { color: var(--income); }

.check-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 12px 0 4px;
  font-size: 13px;
  cursor: pointer;
}
.check-row input[type=checkbox] { width: 16px; height: 16px; cursor: pointer; }
