:root {
  --bg: #f4f6f3;
  --surface: #ffffff;
  --surface-2: #eef1ec;
  --text: #1a221c;
  --text-dim: #6a766e;
  --line: #e2e7df;
  --accent: #2f9e6e;
  --accent-soft: #d8f0e4;
  --in: #2f9e6e;
  --out: #d96a4a;
  --in-soft: #d8f0e4;
  --out-soft: #f8e2da;
  --shadow: 0 4px 24px rgba(26, 34, 28, 0.08);
  --shadow-lg: 0 12px 48px rgba(26, 34, 28, 0.18);
  --radius: 18px;
  --radius-sm: 12px;
  --ease: cubic-bezier(.22,.61,.36,1);
}

[data-theme="dark"] {
  --bg: #0f1611;
  --surface: #18211b;
  --surface-2: #202b24;
  --text: #e8efe9;
  --text-dim: #8a988f;
  --line: #2a352d;
  --accent: #44c489;
  --accent-soft: #173127;
  --in: #44c489;
  --out: #e8855f;
  --in-soft: #143026;
  --out-soft: #34221b;
  --shadow: 0 4px 24px rgba(0, 0, 0, 0.4);
  --shadow-lg: 0 12px 48px rgba(0, 0, 0, 0.55);
}

* { box-sizing: border-box; margin: 0; padding: 0; }

body {
  font-family: 'Lexend', system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  transition: background .3s var(--ease), color .3s var(--ease);
}

.app {
  max-width: 540px;
  margin: 0 auto;
  padding: max(env(safe-area-inset-top), 16px) 18px calc(120px + env(safe-area-inset-bottom));
  min-height: 100vh;
}

.hidden { display: none !important; }

/* ===== HEADER ===== */
.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 0 22px;
}
.brand { display: flex; align-items: center; gap: 11px; }
.brand-mark {
  width: 26px; height: 26px; border-radius: 8px;
  background: conic-gradient(from 140deg, var(--accent), #7fd9b3, var(--accent));
  box-shadow: inset 0 0 0 4px var(--bg);
}
.brand h1 { font-size: 1.18rem; font-weight: 600; letter-spacing: -.02em; }
.header-actions { display: flex; align-items: center; gap: 8px; }

.btn-icon {
  width: 38px; height: 38px; border-radius: 11px;
  border: 1px solid var(--line); background: var(--surface);
  color: var(--text); font-size: 1.1rem; cursor: pointer;
  transition: transform .15s var(--ease), background .2s;
}
.btn-icon:active { transform: scale(.92); }

.btn-ghost {
  border: 1px solid var(--line); background: transparent;
  color: var(--text); padding: 9px 15px; border-radius: 11px;
  font-family: inherit; font-size: .9rem; cursor: pointer;
  transition: background .2s, border-color .2s;
}
.btn-ghost:hover { background: var(--surface-2); }

/* ===== SUMMARY ===== */
.summary { margin-bottom: 26px; }
.balance-card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 22px 22px 18px;
  box-shadow: var(--shadow);
  position: relative;
  overflow: hidden;
}
.balance-card::after {
  content: ''; position: absolute; right: -40px; top: -40px;
  width: 140px; height: 140px; border-radius: 50%;
  background: radial-gradient(circle, var(--accent-soft), transparent 70%);
  opacity: .6;
}
.balance-label {
  display: block; font-size: .78rem; color: var(--text-dim);
  text-transform: uppercase; letter-spacing: .08em; font-weight: 500;
}
.balance-value {
  display: block; font-size: 2.5rem; font-weight: 600;
  letter-spacing: -.03em; margin: 6px 0 4px; font-variant-numeric: tabular-nums;
}
.balance-meta { display: flex; }
.period-toggle {
  font-size: .82rem; color: var(--accent); font-weight: 500;
  cursor: pointer; user-select: none; padding: 3px 0;
}
.period-toggle::after { content: ' ▾'; font-size: .7rem; }

.stat-pair { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-top: 12px; }
.stat {
  background: var(--surface); border: 1px solid var(--line);
  border-radius: var(--radius-sm); padding: 14px 15px;
  display: flex; align-items: center; gap: 11px;
}
.stat-arrow {
  width: 34px; height: 34px; border-radius: 10px;
  display: grid; place-items: center; font-size: 1.05rem; font-weight: 700; flex-shrink: 0;
}
.stat-in .stat-arrow { background: var(--in-soft); color: var(--in); }
.stat-out .stat-arrow { background: var(--out-soft); color: var(--out); }
.stat-label { display: block; font-size: .74rem; color: var(--text-dim); }
.stat-value { display: block; font-size: 1.05rem; font-weight: 600; font-variant-numeric: tabular-nums; }

/* ===== BREAKDOWN ===== */
.breakdown { margin-bottom: 28px; }
.breakdown-head, .tx-head {
  display: flex; align-items: center; justify-content: space-between; margin-bottom: 14px;
}
.breakdown h2, .transactions h2 { font-size: 1rem; font-weight: 600; letter-spacing: -.01em; }

.seg { display: inline-flex; background: var(--surface-2); border-radius: 10px; padding: 3px; }
.seg-btn {
  border: 0; background: transparent; color: var(--text-dim);
  font-family: inherit; font-size: .8rem; font-weight: 500;
  padding: 6px 14px; border-radius: 8px; cursor: pointer; transition: all .2s var(--ease);
}
.seg-btn.active { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }

.bar-list { list-style: none; display: flex; flex-direction: column; gap: 13px; }
.bar-row .bar-top {
  display: flex; justify-content: space-between; font-size: .88rem; margin-bottom: 6px;
}
.bar-row .bar-cat { font-weight: 500; }
.bar-row .bar-amt { color: var(--text-dim); font-variant-numeric: tabular-nums; }
.bar-track { height: 8px; background: var(--surface-2); border-radius: 99px; overflow: hidden; }
.bar-fill { height: 100%; border-radius: 99px; transition: width .6s var(--ease); }
.breakdown-empty { color: var(--text-dim); font-size: .88rem; padding: 6px 0; }

/* ===== TRANSACTIONS ===== */
.filter-select {
  border: 1px solid var(--line); background: var(--surface); color: var(--text);
  font-family: inherit; font-size: .82rem; padding: 7px 10px; border-radius: 10px; cursor: pointer;
}
.tx-list { list-style: none; display: flex; flex-direction: column; gap: 9px; }
.tx-item {
  display: flex; align-items: center; gap: 13px;
  background: var(--surface); border: 1px solid var(--line);
  border-radius: var(--radius-sm); padding: 13px 15px;
  cursor: pointer; transition: transform .12s var(--ease), border-color .2s;
}
.tx-item:active { transform: scale(.98); }
.tx-icon {
  width: 40px; height: 40px; border-radius: 12px; flex-shrink: 0;
  display: grid; place-items: center; font-size: 1.15rem;
}
.tx-body { flex: 1; min-width: 0; }
.tx-cat { font-weight: 500; font-size: .95rem; }
.tx-note { font-size: .8rem; color: var(--text-dim); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tx-right { text-align: right; flex-shrink: 0; }
.tx-amt { font-weight: 600; font-variant-numeric: tabular-nums; }
.tx-amt.in { color: var(--in); }
.tx-amt.out { color: var(--out); }
.tx-date { font-size: .75rem; color: var(--text-dim); }

.empty-state { text-align: center; color: var(--text-dim); font-size: .9rem; padding: 36px 16px; }

/* ===== FAB ===== */
.fab {
  position: fixed; right: max(22px, calc(50% - 270px + 22px));
  bottom: calc(26px + env(safe-area-inset-bottom));
  width: 58px; height: 58px; border-radius: 19px; border: 0;
  background: var(--accent); color: #fff; font-size: 1.9rem; font-weight: 300;
  cursor: pointer; box-shadow: var(--shadow-lg); z-index: 30;
  display: grid; place-items: center; line-height: 1;
  transition: transform .15s var(--ease);
}
.fab:active { transform: scale(.9); }

/* ===== SHEET ===== */
.sheet-backdrop {
  position: fixed; inset: 0; background: rgba(0,0,0,.45);
  z-index: 40; animation: fade .25s var(--ease);
}
@keyframes fade { from { opacity: 0; } }
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 50;
  background: var(--surface); border-radius: 26px 26px 0 0;
  padding: 12px 22px calc(26px + env(safe-area-inset-bottom));
  max-width: 540px; margin: 0 auto;
  box-shadow: var(--shadow-lg);
  animation: slideUp .32s var(--ease);
}
@keyframes slideUp { from { transform: translateY(100%); } }
.sheet-grip { width: 40px; height: 4px; border-radius: 99px; background: var(--line); margin: 4px auto 16px; }
.sheet-title { font-size: 1.15rem; font-weight: 600; margin-bottom: 16px; }

.flow-toggle { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-bottom: 18px; }
.flow-btn {
  border: 1px solid var(--line); background: var(--surface-2); color: var(--text-dim);
  font-family: inherit; font-weight: 500; font-size: .92rem; padding: 12px; border-radius: 13px;
  cursor: pointer; transition: all .2s var(--ease);
}
.flow-btn[data-flow="in"].active { background: var(--in-soft); color: var(--in); border-color: var(--in); }
.flow-btn[data-flow="out"].active { background: var(--out-soft); color: var(--out); border-color: var(--out); }

.field { display: block; margin-bottom: 14px; }
.field > span { display: block; font-size: .82rem; color: var(--text-dim); margin-bottom: 6px; font-weight: 500; }
.field em { font-style: normal; opacity: .7; }
.field input, .field select {
  width: 100%; font-family: inherit; font-size: 1rem; color: var(--text);
  background: var(--surface-2); border: 1px solid var(--line);
  border-radius: 13px; padding: 13px 15px; outline: none;
  transition: border-color .2s;
}
.field input:focus, .field select:focus { border-color: var(--accent); }
.amount-input { position: relative; }
.amount-input .cur {
  position: absolute; left: 15px; top: 50%; transform: translateY(-50%);
  color: var(--text-dim); font-size: 1rem; pointer-events: none;
}
.amount-input input { padding-left: 32px; font-size: 1.3rem; font-weight: 600; }

.sheet-actions { display: grid; grid-template-columns: 1fr 1.6fr; gap: 10px; margin-top: 20px; }
.btn-primary {
  border: 0; background: var(--accent); color: #fff; font-family: inherit;
  font-weight: 600; font-size: 1rem; padding: 14px; border-radius: 14px; cursor: pointer;
  transition: transform .12s var(--ease), opacity .2s;
}
.btn-primary:active { transform: scale(.97); }
.sheet-actions .btn-ghost { padding: 14px; border-radius: 14px; }

/* ===== TOAST ===== */
.toast {
  position: fixed; left: 50%; transform: translateX(-50%);
  bottom: calc(96px + env(safe-area-inset-bottom)); z-index: 60;
  background: var(--text); color: var(--bg);
  padding: 12px 16px; border-radius: 14px; box-shadow: var(--shadow-lg);
  display: flex; align-items: center; gap: 16px; font-size: .88rem;
  animation: slideUp .25s var(--ease); width: max-content; max-width: 90vw;
}
.toast button {
  border: 0; background: transparent; color: var(--accent);
  font-family: inherit; font-weight: 600; font-size: .88rem; cursor: pointer;
}