:root {
  --bg: #f1f5f4;
  --surface: #ffffff;
  --surface-2: #f6f8f7;
  --primary: #16a34a;
  --primary-dark: #15803d;
  --primary-light: #dcfce7;
  --text: #111827;
  --text-2: #4b5563;
  --text-3: #9ca3af;
  --border: #e5e7eb;
  --income: #16a34a;
  --expense: #ef4444;
  --warn: #f59e0b;
  --danger: #dc2626;
  --shadow: 0 1px 3px rgba(15, 23, 42, .08), 0 4px 12px rgba(15, 23, 42, .06);
  --shadow-lg: 0 8px 30px rgba(15, 23, 42, .16);
  --radius: 16px;
  --header-h: 64px;
  --nav-h: 64px;
  --sat: env(safe-area-inset-top, 0px);
  --sab: env(safe-area-inset-bottom, 0px);
  --ease: cubic-bezier(.2, .8, .2, 1);
}

[data-theme="dark"] {
  --bg: #0f1412;
  --surface: #1a211e;
  --surface-2: #222b27;
  --primary: #34d399;
  --primary-dark: #10b981;
  --primary-light: #123324;
  --text: #f3f4f6;
  --text-2: #c3cbc6;
  --text-3: #6b7280;
  --border: #2a332f;
  --income: #34d399;
  --expense: #f87171;
  --warn: #fbbf24;
  --danger: #f87171;
  --shadow: 0 1px 3px rgba(0, 0, 0, .4), 0 4px 12px rgba(0, 0, 0, .35);
  --shadow-lg: 0 8px 30px rgba(0, 0, 0, .5);
}

* { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; }

html, body { height: 100%; }

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
  transition: background .3s var(--ease), color .3s var(--ease);
}

#app {
  max-width: 480px;
  margin: 0 auto;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  position: relative;
}

/* ---------- Header ---------- */
#app-header {
  background: linear-gradient(135deg, var(--primary-dark), var(--primary));
  color: #fff;
  padding-top: calc(var(--sat) + 10px);
  padding-bottom: 10px;
  flex-shrink: 0;
  box-shadow: var(--shadow);
  position: relative;
  z-index: 10;
}

.header-inner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  height: calc(var(--header-h) - var(--sat) - 10px);
}

.brand { display: flex; align-items: center; gap: 12px; }

.brand-icon {
  width: 40px; height: 40px;
  background: rgba(255, 255, 255, .18);
  border-radius: 12px;
  display: flex; align-items: center; justify-content: center;
}
.brand-icon svg { width: 24px; height: 24px; }

#header-title { font-size: 17px; font-weight: 700; letter-spacing: .2px; }
.subtitle { font-size: 12px; opacity: .85; }

.icon-btn {
  width: 40px; height: 40px;
  border: none; background: none;
  color: inherit;
  border-radius: 50%;
  display: inline-flex; align-items: center; justify-content: center;
  cursor: pointer;
}
.icon-btn svg { width: 22px; height: 22px; }
.icon-btn:active { background: rgba(255, 255, 255, .2); }
.header-actions .icon-btn { color: #fff; }

/* ---------- Content ---------- */
main {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
  padding: 16px 16px calc(var(--nav-h) + var(--sab) + 20px);
}

.view { display: none; }
.view.active { display: block; animation: fadeIn .25s var(--ease); }

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: translateY(0); }
}

/* ---------- Balance card ---------- */
.balance-card {
  background: linear-gradient(135deg, #1f9d55, #16a34a 55%, #34b95c);
  border-radius: 20px;
  padding: 24px 20px;
  color: #fff;
  box-shadow: 0 10px 24px rgba(22, 163, 74, .35);
  margin-bottom: 20px;
}
.balance-label { font-size: 13px; opacity: .85; letter-spacing: .4px; text-transform: uppercase; }
.balance-value {
  font-size: 40px;
  font-weight: 800;
  letter-spacing: -.5px;
  margin: 6px 0 14px;
  font-variant-numeric: tabular-nums;
}
.balance-legend { display: flex; gap: 18px; }
.legend-item { display: inline-flex; align-items: center; gap: 6px; font-size: 13px; }
.legend-item .dot { width: 8px; height: 8px; border-radius: 50%; display: inline-block; }
.legend-item.income .dot { background: #bbf7d0; }
.legend-item.expense .dot { background: #fecaca; }
.legend-item span { font-weight: 700; font-variant-numeric: tabular-nums; }

/* ---------- Sections ---------- */
.section-head {
  display: flex; align-items: center; justify-content: space-between;
  margin: 20px 2px 10px;
}
.section-head h2 { font-size: 16px; font-weight: 700; }
.link-btn {
  border: none; background: none;
  color: var(--primary); font-weight: 600; font-size: 13px;
  padding: 6px 8px; cursor: pointer; border-radius: 8px;
}
.link-btn:active { background: var(--primary-light); }
.month-label { font-size: 13px; color: var(--text-3); font-weight: 500; }

/* ---------- Stat row ---------- */
.stat-row { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin-bottom: 16px; }
.stat-card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 14px 12px;
  box-shadow: var(--shadow);
}
.stat-label { display: block; font-size: 11px; color: var(--text-3); text-transform: uppercase; letter-spacing: .4px; margin-bottom: 6px; }
.stat-value { font-size: 17px; font-weight: 700; font-variant-numeric: tabular-nums; }
.stat-value.positive { color: var(--income); }
.stat-value.negative { color: var(--expense); }

/* ---------- Chart card ---------- */
.chart-card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 16px 16px 10px;
  box-shadow: var(--shadow);
}
.chart-legend {
  display: flex; flex-wrap: wrap; gap: 6px 14px;
  margin-bottom: 8px;
}
.legend-chip {
  display: inline-flex; align-items: center; gap: 6px;
  font-size: 12px; color: var(--text-2);
}
.legend-chip i { width: 9px; height: 9px; border-radius: 3px; display: inline-block; }
#expense-chart { width: 100%; height: 160px; display: block; }
.chart-empty {
  text-align: center; color: var(--text-3); font-size: 13px;
  padding: 30px 0;
}

/* ---------- Budget & savings preview ---------- */
.budget-preview, .savings-preview { display: flex; flex-direction: column; gap: 10px; }
.bp-card, .sg-card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 14px 16px;
  box-shadow: var(--shadow);
}
.bp-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 8px; }
.bp-name { display: flex; align-items: center; gap: 8px; font-weight: 600; font-size: 14px; }
.bp-amount { font-size: 13px; color: var(--text-2); font-variant-numeric: tabular-nums; }
.progress {
  height: 8px; background: var(--surface-2);
  border-radius: 99px; overflow: hidden; position: relative;
}
.progress > span {
  display: block; height: 100%; border-radius: 99px;
  background: var(--primary);
  transition: width .4s var(--ease);
}
.progress.over > span { background: var(--expense); }
.progress.warn > span { background: var(--warn); }
.bp-foot { display: flex; justify-content: space-between; margin-top: 6px; font-size: 12px; color: var(--text-3); }
.bp-foot b { color: var(--text-2); }
.bp-foot.over b { color: var(--expense); }

.sg-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 8px; }
.sg-name { font-weight: 600; font-size: 14px; display: flex; align-items: center; gap: 8px; }
.sg-amount { font-size: 13px; color: var(--text-2); font-variant-numeric: tabular-nums; }
.sg-foot { display: flex; justify-content: space-between; margin-top: 6px; font-size: 12px; color: var(--text-3); }
.sg-foot b { color: var(--text-2); }

/* ---------- Transaction list ---------- */
.tx-list { display: flex; flex-direction: column; gap: 8px; }
.tx-item {
  display: flex; align-items: center; gap: 12px;
  background: var(--surface);
  border-radius: 14px;
  padding: 12px 14px;
  box-shadow: var(--shadow);
  cursor: pointer;
}
.tx-item:active { background: var(--surface-2); }
.tx-icon {
  width: 42px; height: 42px; border-radius: 12px;
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
  color: #fff;
}
.tx-icon svg { width: 20px; height: 20px; }
.tx-body { flex: 1; min-width: 0; }
.tx-name { font-weight: 600; font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tx-meta { font-size: 12px; color: var(--text-3); margin-top: 2px; }
.tx-amount { font-weight: 700; font-size: 15px; font-variant-numeric: tabular-nums; flex-shrink: 0; }
.tx-amount.income { color: var(--income); }
.tx-amount.expense { color: var(--expense); }

.empty-state {
  text-align: center; color: var(--text-3);
  padding: 36px 16px; font-size: 14px;
}
.empty-state p { line-height: 1.6; }

/* ---------- Month nav & filter ---------- */
.month-nav { display: flex; align-items: center; justify-content: space-between; margin: 4px 0 12px; }
.month-nav .month-label { font-size: 15px; font-weight: 700; color: var(--text); }
.month-nav .icon-btn { color: var(--text-2); }
.month-nav .icon-btn:active { background: var(--surface-2); }

.seg-control {
  display: flex; background: var(--surface-2);
  border-radius: 12px; padding: 3px;
  margin-bottom: 12px;
}
.seg-btn {
  flex: 1; border: none; background: none;
  padding: 9px 6px; border-radius: 10px;
  font-size: 13px; font-weight: 600; color: var(--text-2);
  cursor: pointer;
  transition: all .2s var(--ease);
}
.seg-btn.active { background: var(--surface); color: var(--primary); box-shadow: var(--shadow); }

.month-totals {
  display: flex; justify-content: space-between;
  background: var(--surface);
  border-radius: 12px; padding: 12px 16px;
  font-size: 13px; color: var(--text-2);
  margin-bottom: 12px; box-shadow: var(--shadow);
}
.month-totals b { font-variant-numeric: tabular-nums; }
.positive { color: var(--income); }
.negative { color: var(--expense); }

/* ---------- Budget list ---------- */
.budget-list { display: flex; flex-direction: column; gap: 10px; }
.budget-list .bp-card { cursor: pointer; }
.budget-list .bp-card:active { background: var(--surface-2); }

/* ---------- Goal list ---------- */
.goal-list { display: flex; flex-direction: column; gap: 12px; }
.goal-card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--shadow);
}
.goal-card.done { border: 2px solid var(--income); }
.goal-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 10px; }
.goal-title { font-weight: 700; font-size: 15px; display: flex; align-items: center; gap: 8px; }
.goal-badge {
  font-size: 10px; font-weight: 700; text-transform: uppercase;
  background: var(--primary-light); color: var(--primary);
  padding: 3px 8px; border-radius: 99px;
}
.goal-actions { display: flex; gap: 4px; }
.goal-actions .icon-btn { color: var(--text-2); width: 34px; height: 34px; }
.goal-actions .icon-btn svg { width: 18px; height: 18px; }
.goal-actions .icon-btn:active { background: var(--surface-2); }
.goal-progress { margin: 4px 0 8px; }
.goal-meta { display: flex; justify-content: space-between; font-size: 13px; color: var(--text-2); }
.goal-meta b { font-variant-numeric: tabular-nums; }
.goal-meta .g-left { display: flex; align-items: center; gap: 6px; }

.add-inline {
  color: var(--primary);
  background: var(--primary-light);
  border-radius: 10px;
}
.add-inline svg { width: 20px; height: 20px; }

/* ---------- Settings ---------- */
.settings-group {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 8px 16px;
  box-shadow: var(--shadow);
  margin-bottom: 16px;
}
.settings-group h3 {
  font-size: 12px; text-transform: uppercase; letter-spacing: .6px;
  color: var(--text-3); padding: 14px 0 4px;
}
.setting-row {
  display: flex; align-items: center; justify-content: space-between;
  gap: 12px; padding: 12px 0;
  border-bottom: 1px solid var(--border);
}
.setting-row:last-child { border-bottom: none; }
.setting-row label { font-size: 14px; font-weight: 500; }
.setting-row select, .setting-row input {
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  border-radius: 10px;
  padding: 9px 10px;
  font-size: 14px;
  max-width: 55%;
  font-family: inherit;
}
.btn-outline, .btn-danger {
  display: block; width: 100%;
  margin: 8px 0; padding: 12px;
  border-radius: 12px; font-size: 14px; font-weight: 600;
  cursor: pointer; font-family: inherit;
  border: 1.5px solid var(--primary); background: none; color: var(--primary);
}
.btn-outline:active { background: var(--primary-light); }
.btn-danger {
  border-color: var(--danger); color: var(--danger); background: none;
}
.btn-danger:active { background: rgba(239, 68, 68, .1); }
.hint { font-size: 12px; color: var(--text-3); line-height: 1.6; padding: 6px 0 14px; }
.about p { padding: 4px 0; font-size: 14px; }
.about p.hint { font-size: 12px; }

/* ---------- Bottom nav ---------- */
#bottom-nav {
  position: absolute; left: 0; right: 0; bottom: 0;
  height: calc(var(--nav-h) + var(--sab));
  padding-bottom: var(--sab);
  background: var(--surface);
  border-top: 1px solid var(--border);
  display: flex;
  align-items: stretch;
  z-index: 20;
}
.nav-item {
  flex: 1;
  border: none; background: none;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px;
  color: var(--text-3);
  font-size: 10.5px; font-weight: 500;
  cursor: pointer;
}
.nav-item svg { width: 22px; height: 22px; }
.nav-item.active { color: var(--primary); }
.nav-item.active svg { stroke-width: 2.4; }

.nav-fab-wrap {
  flex: 1; position: relative; display: flex; align-items: center; justify-content: center;
}
.fab {
  width: 56px; height: 56px;
  border-radius: 50%;
  border: none;
  background: var(--primary);
  color: #fff;
  box-shadow: 0 6px 16px rgba(22, 163, 74, .45);
  display: flex; align-items: center; justify-content: center;
  position: absolute; top: -22px;
  cursor: pointer;
  transition: transform .15s var(--ease);
}
.fab svg { width: 26px; height: 26px; }
.fab:active { transform: scale(.92); }

/* ---------- Modal ---------- */
.modal { position: fixed; inset: 0; z-index: 100; display: flex; }
.modal[hidden] { display: none; }
.modal-backdrop { position: absolute; inset: 0; background: rgba(15, 23, 42, .5); backdrop-filter: blur(2px); }
.modal-sheet {
  position: relative;
  margin-top: auto;
  background: var(--surface);
  border-radius: 20px 20px 0 0;
  padding: 8px 20px calc(20px + var(--sab));
  max-height: 88dvh;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  animation: sheetUp .3s var(--ease);
}
.modal-center {
  position: relative; margin: auto 20px;
  background: var(--surface);
  border-radius: 20px;
  padding: 22px 20px 16px;
  max-width: 320px;
  animation: sheetUp .25s var(--ease);
}
@keyframes sheetUp {
  from { transform: translateY(40px); opacity: 0; }
  to { transform: translateY(0); opacity: 1; }
}
.modal-handle {
  width: 40px; height: 4px; border-radius: 99px;
  background: var(--border); margin: 4px auto 12px;
}
.modal-sheet h2, .modal-center h2 { font-size: 18px; margin-bottom: 14px; }
.modal-center p { font-size: 14px; color: var(--text-2); line-height: 1.6; margin-bottom: 18px; }

.type-toggle { margin-bottom: 16px; }
.type-toggle .seg-btn.expense.active { color: var(--expense); }
.type-toggle .seg-btn.income.active { color: var(--income); }

.field { margin-bottom: 14px; }
.field label { display: block; font-size: 13px; font-weight: 600; color: var(--text-2); margin-bottom: 7px; }
.field label .opt { font-weight: 400; color: var(--text-3); }
.field input[type="text"], .field input[type="number"], .field input[type="date"] {
  width: 100%;
  border: 1.5px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  border-radius: 12px;
  padding: 13px 14px;
  font-size: 15px;
  font-family: inherit;
  transition: border-color .2s;
}
.field input:focus { outline: none; border-color: var(--primary); }
.amount-input { display: flex; align-items: center; }
.amount-input .amount-prefix {
  font-size: 15px; font-weight: 600; color: var(--text-2);
  background: var(--surface-2);
  border: 1.5px solid var(--border);
  border-right: none;
  border-radius: 12px 0 0 12px;
  padding: 13px 0 13px 14px;
}
.amount-input input { border-radius: 0 12px 12px 0 !important; }

.category-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 10px;
}
.cat-option {
  border: 1.5px solid var(--border);
  background: var(--surface-2);
  border-radius: 12px;
  padding: 10px 4px;
  display: flex; flex-direction: column; align-items: center; gap: 5px;
  cursor: pointer;
  font-size: 11px; color: var(--text-2); font-weight: 500;
  transition: all .15s var(--ease);
}
.cat-option .cat-ico {
  width: 34px; height: 34px; border-radius: 10px;
  display: flex; align-items: center; justify-content: center;
  color: #fff;
}
.cat-option .cat-ico svg { width: 18px; height: 18px; }
.cat-option.selected {
  border-color: var(--primary);
  background: var(--primary-light);
  color: var(--primary);
}
.cat-option.selected .cat-ico { transform: scale(1.05); }
.cat-option:active { transform: scale(.96); }

.modal-actions { display: flex; gap: 10px; margin-top: 6px; }
.modal-actions .btn-ghost, .modal-actions .btn-primary, .modal-actions .btn-danger-ghost {
  flex: 1; padding: 13px; border-radius: 12px;
  font-size: 15px; font-weight: 600; cursor: pointer; font-family: inherit;
}
.btn-ghost { border: 1.5px solid var(--border); background: none; color: var(--text-2); }
.btn-danger-ghost { border: 1.5px solid var(--danger); background: none; color: var(--danger); }
.btn-danger-ghost:active { background: rgba(239, 68, 68, .1); }
.btn-primary {
  border: none; background: var(--primary); color: #fff;
}
.btn-primary:active { background: var(--primary-dark); }

/* ---------- Budget form (modal) ---------- */
.budget-form { display: flex; flex-direction: column; gap: 12px; }
.budget-row {
  display: flex; align-items: center; gap: 12px;
  background: var(--surface-2);
  border-radius: 12px;
  padding: 8px 12px;
}
.budget-row .cat-ico { width: 34px; height: 34px; border-radius: 10px; display: flex; align-items: center; justify-content: center; color: #fff; flex-shrink: 0; }
.budget-row .cat-ico svg { width: 18px; height: 18px; }
.budget-row label { flex: 1; font-size: 14px; font-weight: 500; }
.budget-row .amount-input { width: 130px; }
.budget-row input {
  width: 100%;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  border-radius: 0 10px 10px 0;
  padding: 10px;
  font-size: 14px;
  font-family: inherit;
}
.budget-row .amount-prefix {
  padding: 10px 0 10px 12px;
  font-size: 13px;
  border-radius: 10px 0 0 10px;
  background: var(--surface);
}
.budget-row input:focus { outline: none; border-color: var(--primary); }

/* ---------- Toast ---------- */
.toast {
  position: fixed; left: 50%; bottom: calc(var(--nav-h) + var(--sab) + 16px);
  transform: translateX(-50%);
  background: #111827; color: #f9fafb;
  padding: 11px 18px; border-radius: 12px;
  font-size: 13.5px; font-weight: 500;
  box-shadow: var(--shadow-lg);
  z-index: 200;
  max-width: 85%;
  text-align: center;
  animation: toastIn .25s var(--ease);
}
.toast[hidden] { display: none; }
@keyframes toastIn {
  from { opacity: 0; transform: translateX(-50%) translateY(12px); }
  to { opacity: 1; transform: translateX(-50%) translateY(0); }
}

/* ---------- Scrollbar (desktop) ---------- */
main::-webkit-scrollbar { width: 6px; }
main::-webkit-scrollbar-thumb { background: var(--border); border-radius: 99px; }
