/* ==========================================================================
   Money Manager - Design tokens
   ========================================================================== */
:root {
  --bg: #F4F6F8;
  --surface: #FFFFFF;
  --surface-2: #F0F3F6;
  --surface-3: #E4E9ED;
  --border: #E3E8EC;
  --text: #172029;
  --text-muted: #64707B;
  --text-faint: #9AA6B0;

  --income: #1CA875;
  --income-dim: #E3F6ED;
  --expense: #E1473D;
  --expense-dim: #FCEAE8;
  --warning: #C0810F;
  --warning-dim: #FBF0D9;
  --premium: #4C6FEA;
  --premium-dim: #EAEFFE;
  --loan: #9A5FC7;
  --loan-dim: #F4EBFB;

  --radius-sm: 10px;
  --radius-md: 16px;
  --radius-lg: 22px;
  --radius-pill: 999px;

  --shadow-1: 0 2px 10px rgba(23,32,41,0.06);
  --shadow-2: 0 8px 30px rgba(23,32,41,0.12);

  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;

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

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: none;
}
h1, h2, h3, h4 { margin: 0; font-weight: 700; letter-spacing: -0.01em; }
p { margin: 0; }
button { font-family: inherit; }
input, select, textarea { font-family: inherit; }
a { color: inherit; text-decoration: none; }
::selection { background: var(--income-dim); }

.num { font-variant-numeric: tabular-nums; font-feature-settings: "tnum"; }

/* ==========================================================================
   Auth screens (login / setup)
   ========================================================================== */
.auth-wrap {
  min-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background:
    radial-gradient(circle at 20% 0%, rgba(62,207,142,0.10), transparent 55%),
    radial-gradient(circle at 100% 100%, rgba(124,158,255,0.08), transparent 50%),
    var(--bg);
}
.auth-card {
  width: 100%;
  max-width: 380px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 32px 26px;
  box-shadow: var(--shadow-2);
}
.auth-mark {
  width: 52px; height: 52px; border-radius: 16px;
  background: var(--surface-2);
  border: 1.5px solid var(--income);
  display: flex; align-items: center; justify-content: center;
  margin-bottom: 18px;
}
.auth-mark svg { width: 26px; height: 26px; }
.auth-card h1 { font-size: 22px; margin-bottom: 4px; }
.auth-card .sub { color: var(--text-muted); font-size: 14px; margin-bottom: 24px; display: block; }
.field { margin-bottom: 14px; }
.field label { display: block; font-size: 12.5px; color: var(--text-muted); margin-bottom: 6px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.04em; }
.field input, .field select, .field textarea {
  width: 100%;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 13px 14px;
  color: var(--text);
  font-size: 15.5px;
  outline: none;
  transition: border-color .15s ease;
}
.field input:focus, .field select:focus, .field textarea:focus { border-color: var(--income); }
.field textarea { resize: vertical; min-height: 70px; }
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  border: none; border-radius: var(--radius-sm);
  padding: 13px 18px; font-size: 15px; font-weight: 700;
  cursor: pointer; transition: transform .08s ease, opacity .15s ease, background .15s ease;
  width: 100%;
}
.btn:active { transform: scale(0.98); }
.btn-primary { background: var(--income); color: #FFFFFF; }
.btn-primary:hover { opacity: 0.92; }
.btn-secondary { background: var(--surface-2); color: var(--text); border: 1px solid var(--border); }
.btn-ghost { background: transparent; color: var(--text-muted); border: 1px solid var(--border); }
.btn-danger { background: var(--expense-dim); color: var(--expense); }
.btn-block-row { display: flex; gap: 10px; }
.btn-icon { width: 42px; height: 42px; border-radius: 12px; flex: 0 0 auto; padding: 0; }
.error-text { color: var(--expense); font-size: 13.5px; margin-top: 10px; min-height: 18px; }
.hint-text { color: var(--text-faint); font-size: 12.5px; margin-top: 14px; text-align: center; }

/* ==========================================================================
   App shell
   ========================================================================== */
#app { display: none; min-height: 100dvh; flex-direction: column; }
#app.ready { display: flex; }

.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(14px + var(--safe-top)) 18px 14px;
  background: rgba(255,255,255,0.85);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
}
.topbar h1 { font-size: 18px; }
.topbar .topbar-actions { display: flex; gap: 10px; }
.icon-btn {
  width: 38px; height: 38px; border-radius: 12px;
  display: flex; align-items: center; justify-content: center;
  background: var(--surface-2); border: 1px solid var(--border); color: var(--text-muted);
  cursor: pointer;
}
.icon-btn svg { width: 18px; height: 18px; }
.icon-btn.has-dot::after {
  content: ''; position: absolute; width: 8px; height: 8px; border-radius: 50%;
  background: var(--expense); margin-left: 18px; margin-top: -22px;
}

main.view {
  flex: 1; overflow-y: auto;
  padding: 16px 16px calc(var(--nav-h) + 28px + var(--safe-bottom));
}

.bottomnav {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 30;
  display: flex;
  background: rgba(255,255,255,0.90);
  backdrop-filter: blur(14px);
  border-top: 1px solid var(--border);
  padding-bottom: var(--safe-bottom);
}
.navitem {
  flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 4px; padding: 10px 0 8px; color: var(--text-faint); cursor: pointer; border: none; background: none;
  font-size: 11px; font-weight: 600;
}
.navitem svg { width: 22px; height: 22px; }
.navitem.active { color: var(--income); }
.navitem .fab-slot { display: none; }

.fab {
  position: fixed; right: 18px; bottom: calc(var(--nav-h) + 16px + var(--safe-bottom)); z-index: 25;
  width: 56px; height: 56px; border-radius: 50%;
  background: var(--income); color: #FFFFFF; border: none;
  display: flex; align-items: center; justify-content: center;
  box-shadow: 0 10px 24px rgba(62,207,142,0.35);
  cursor: pointer;
}
.fab svg { width: 26px; height: 26px; }

/* ==========================================================================
   Cards & generic layout blocks
   ========================================================================== */
.section-title {
  display: flex; align-items: center; justify-content: space-between;
  margin: 22px 0 10px;
}
.section-title h2 { font-size: 14px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--text-muted); }
.section-title .link { font-size: 13px; color: var(--income); font-weight: 600; cursor: pointer; }

.card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius-md); padding: 16px;
}
.card + .card { margin-top: 12px; }

.balance-hero {
  background: linear-gradient(155deg, var(--surface-2) 0%, var(--surface) 65%);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 22px 20px;
  position: relative;
  overflow: hidden;
}
.balance-hero .label { color: var(--text-muted); font-size: 13px; font-weight: 600; }
.balance-hero .amount { font-size: 34px; font-weight: 800; margin-top: 6px; letter-spacing: -0.02em; }
.balance-hero .amount small { font-size: 18px; color: var(--text-muted); font-weight: 700; }
.hero-split { display: flex; gap: 10px; margin-top: 18px; }
.hero-pill {
  flex: 1; background: rgba(23,32,41,0.03); border: 1px solid var(--border);
  border-radius: var(--radius-sm); padding: 10px 12px;
}
.hero-pill .t { font-size: 11.5px; color: var(--text-muted); font-weight: 700; text-transform: uppercase; letter-spacing: 0.04em; display: flex; align-items: center; gap: 6px; }
.hero-pill .v { font-size: 17px; font-weight: 800; margin-top: 4px; }
.hero-pill.inc .t { color: var(--income); }
.hero-pill.exp .t { color: var(--expense); }
.dot { width: 7px; height: 7px; border-radius: 50%; display: inline-block; }

.accounts-scroll { display: flex; gap: 10px; overflow-x: auto; padding: 2px 2px 6px; margin: 0 -2px; scrollbar-width: none; }
.accounts-scroll::-webkit-scrollbar { display: none; }
.account-chip {
  flex: 0 0 auto; min-width: 148px;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-md);
  padding: 14px;
}
.account-chip .name { font-size: 12.5px; color: var(--text-muted); font-weight: 600; }
.account-chip .bal { font-size: 18px; font-weight: 800; margin-top: 6px; }
.account-chip.add-chip {
  display: flex; align-items: center; justify-content: center; color: var(--text-faint);
  border-style: dashed; cursor: pointer; min-width: 90px;
}

.chart-card { padding: 16px 12px 10px; }
.chart-card .chart-head { display: flex; justify-content: space-between; align-items: center; padding: 0 6px 6px; }
.chart-card .chart-head h3 { font-size: 14px; }
.chart-legend { display: flex; flex-wrap: wrap; gap: 8px 14px; padding: 10px 6px 2px; }
.chart-legend .item { display: flex; align-items: center; gap: 6px; font-size: 12.5px; color: var(--text-muted); }

.list-row {
  display: flex; align-items: center; gap: 12px;
  padding: 12px 4px;
  border-bottom: 1px solid var(--border);
}
.list-row:last-child { border-bottom: none; }
.list-icon {
  width: 40px; height: 40px; border-radius: 12px; flex: 0 0 auto;
  display: flex; align-items: center; justify-content: center; font-size: 16px; font-weight: 800;
}
.list-main { flex: 1; min-width: 0; }
.list-main .title { font-size: 14.5px; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.list-main .sub { font-size: 12.5px; color: var(--text-muted); margin-top: 2px; }
.list-amount { text-align: right; font-weight: 800; font-size: 15px; flex: 0 0 auto; }
.list-amount.inc { color: var(--income); }
.list-amount.exp { color: var(--expense); }

.empty-state {
  text-align: center; padding: 38px 16px; color: var(--text-faint);
}
.empty-state svg { width: 40px; height: 40px; margin-bottom: 10px; opacity: 0.6; }
.empty-state .t { font-size: 14.5px; color: var(--text-muted); font-weight: 600; margin-bottom: 4px; }
.empty-state .s { font-size: 13px; }

/* ==========================================================================
   Tabs / filter chips
   ========================================================================== */
.tabbar {
  display: flex; gap: 6px; background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius-pill); padding: 4px; margin-bottom: 14px;
}
.tabbar button {
  flex: 1; border: none; background: transparent; color: var(--text-muted);
  padding: 9px 10px; border-radius: var(--radius-pill); font-size: 13px; font-weight: 700; cursor: pointer;
}
.tabbar button.active { background: var(--surface-3); color: var(--text); }

.chiprow { display: flex; gap: 8px; overflow-x: auto; padding-bottom: 4px; margin-bottom: 12px; scrollbar-width: none; }
.chiprow::-webkit-scrollbar { display: none; }
.chip {
  flex: 0 0 auto; padding: 8px 14px; border-radius: var(--radius-pill);
  background: var(--surface); border: 1px solid var(--border); color: var(--text-muted);
  font-size: 13px; font-weight: 600; cursor: pointer; white-space: nowrap;
}
.chip.active { background: var(--income-dim); border-color: var(--income); color: var(--income); }

/* ==========================================================================
   Progress bars (bills / premiums / loans)
   ========================================================================== */
.progress-track {
  height: 8px; background: var(--surface-3); border-radius: var(--radius-pill); overflow: hidden; margin-top: 10px;
}
.progress-fill { height: 100%; border-radius: var(--radius-pill); transition: width .25s ease; }
.progress-fill.ok { background: var(--income); }
.progress-fill.warn { background: var(--warning); }
.progress-fill.bad { background: var(--expense); }

.status-pill {
  display: inline-flex; align-items: center; gap: 5px;
  font-size: 11px; font-weight: 800; text-transform: uppercase; letter-spacing: 0.03em;
  padding: 4px 9px; border-radius: var(--radius-pill);
}
.status-pill.pending { background: var(--surface-3); color: var(--text-muted); }
.status-pill.partial { background: var(--warning-dim); color: var(--warning); }
.status-pill.overdue { background: var(--expense-dim); color: var(--expense); }
.status-pill.paid { background: var(--income-dim); color: var(--income); }
.status-pill.closed { background: var(--surface-3); color: var(--text-faint); }
.status-pill.active { background: var(--premium-dim); color: var(--premium); }

.obligation-card { margin-bottom: 12px; }
.obligation-card .row1 { display: flex; justify-content: space-between; align-items: flex-start; gap: 10px; }
.obligation-card .name { font-size: 15px; font-weight: 700; }
.obligation-card .meta { font-size: 12.5px; color: var(--text-muted); margin-top: 3px; }
.obligation-card .amt { text-align: right; }
.obligation-card .amt .due { font-size: 16px; font-weight: 800; }
.obligation-card .amt .of { font-size: 11.5px; color: var(--text-faint); }
.obligation-card .actions { display: flex; gap: 8px; margin-top: 12px; }
.obligation-card .actions .btn { padding: 10px; font-size: 13px; }

/* ==========================================================================
   Modal / bottom sheet
   ========================================================================== */
.sheet-backdrop {
  position: fixed; inset: 0; background: rgba(0,0,0,0.55); z-index: 50;
  opacity: 0; pointer-events: none; transition: opacity .2s ease;
}
.sheet-backdrop.open { opacity: 1; pointer-events: auto; }
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 51;
  background: var(--surface); border-top: 1px solid var(--border);
  border-radius: 22px 22px 0 0;
  max-height: 88dvh; overflow-y: auto;
  padding: 10px 20px calc(24px + var(--safe-bottom));
  transform: translateY(100%); transition: transform .25s cubic-bezier(.32,.72,0,1);
}
.sheet.open { transform: translateY(0); }
.sheet-handle { width: 40px; height: 4px; border-radius: 3px; background: var(--surface-3); margin: 8px auto 16px; }
.sheet-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 18px; }
.sheet-head h2 { font-size: 17px; }
.sheet-close { color: var(--text-muted); cursor: pointer; padding: 4px; }
.segmented { display: flex; background: var(--surface-2); border-radius: var(--radius-sm); padding: 4px; margin-bottom: 16px; }
.segmented button {
  flex: 1; border: none; background: transparent; padding: 10px; border-radius: 8px;
  color: var(--text-muted); font-weight: 700; font-size: 13.5px; cursor: pointer;
}
.segmented button.active.inc { background: var(--income-dim); color: var(--income); }
.segmented button.active.exp { background: var(--expense-dim); color: var(--expense); }
.segmented button.active { background: var(--surface-3); color: var(--text); }
.field-row { display: flex; gap: 10px; }
.field-row .field { flex: 1; }
.toggle-row { display: flex; align-items: center; justify-content: space-between; padding: 14px 2px; border-bottom: 1px solid var(--border); }
.toggle-row:last-child { border-bottom: none; }
.toggle-row .t { font-size: 14.5px; font-weight: 600; }
.toggle-row .s { font-size: 12.5px; color: var(--text-muted); margin-top: 2px; }
.switch { position: relative; width: 46px; height: 27px; flex: 0 0 auto; }
.switch input { opacity: 0; width: 0; height: 0; }
.switch .track { position: absolute; inset: 0; background: var(--surface-3); border-radius: var(--radius-pill); transition: background .15s ease; cursor: pointer; }
.switch .track::before { content: ''; position: absolute; width: 21px; height: 21px; left: 3px; top: 3px; background: #fff; border-radius: 50%; transition: transform .15s ease; }
.switch input:checked + .track { background: var(--income); }
.switch input:checked + .track::before { transform: translateX(19px); }

/* ==========================================================================
   Toast
   ========================================================================== */
.toast-wrap { position: fixed; top: calc(14px + var(--safe-top)); left: 0; right: 0; z-index: 80; display: flex; flex-direction: column; align-items: center; gap: 8px; pointer-events: none; }
.toast {
  background: var(--surface-2); border: 1px solid var(--border); color: var(--text);
  padding: 11px 18px; border-radius: var(--radius-pill); font-size: 13.5px; font-weight: 600;
  box-shadow: var(--shadow-2); opacity: 0; transform: translateY(-12px); transition: all .2s ease;
}
.toast.show { opacity: 1; transform: translateY(0); }
.toast.error { border-color: var(--expense); color: var(--expense); }
.toast.success { border-color: var(--income); color: var(--income); }

/* ==========================================================================
   Settings page specifics
   ========================================================================== */
.profile-block { display: flex; align-items: center; gap: 14px; padding: 6px 2px 20px; }
.avatar-circle {
  width: 52px; height: 52px; border-radius: 50%; background: var(--surface-2);
  display: flex; align-items: center; justify-content: center; font-weight: 800; font-size: 18px; color: var(--income);
  border: 1.5px solid var(--income);
}
.settings-group { margin-bottom: 8px; }
.settings-group h3 { font-size: 12.5px; text-transform: uppercase; letter-spacing: 0.05em; color: var(--text-muted); margin: 20px 4px 8px; }

.loader { display: flex; align-items: center; justify-content: center; padding: 40px; color: var(--text-faint); font-size: 13px; }
.spin { width: 20px; height: 20px; border: 2.5px solid var(--surface-3); border-top-color: var(--income); border-radius: 50%; animation: spin .7s linear infinite; margin-right: 10px; }
@keyframes spin { to { transform: rotate(360deg); } }

/* form-only extras */
.person-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin-bottom: 4px; }
.person-grid button {
  padding: 10px 4px; border-radius: var(--radius-sm); border: 1px solid var(--border);
  background: var(--surface-2); color: var(--text-muted); font-size: 12.5px; font-weight: 700; cursor: pointer;
}
.person-grid button.active { border-color: var(--premium); background: var(--premium-dim); color: var(--premium); }

@media (min-width: 720px) {
  main.view { max-width: 640px; margin: 0 auto; }
  .topbar > div:first-child, .topbar { max-width: 640px; margin: 0 auto; width: 100%; }
  .bottomnav { max-width: 640px; margin: 0 auto; border-left: 1px solid var(--border); border-right: 1px solid var(--border); border-radius: 18px 18px 0 0; }
  .fab { right: calc(50% - 320px + 18px); }
  svg { width: 20px; height: 20px; flex: 0 0 auto; display: block; }
}
svg { width: 20px; height: 20px; flex: 0 0 auto; display: block; }