:root {
  color-scheme: light dark;
  font-family: Inter, system-ui, -apple-system, sans-serif;
  --canvas: var(--tg-theme-secondary-bg-color, light-dark(#f2f4f5, #121820));
  --surface: var(--tg-theme-bg-color, light-dark(#fff, #1c242f));
  --ink: var(--tg-theme-text-color, light-dark(#182132, #eaf0fa));
  --muted: var(--tg-theme-hint-color, light-dark(#586579, #a8b7cb));
  --line: color-mix(in srgb, var(--ink) 13%, transparent);
  --accent: var(--tg-theme-button-color, light-dark(#3155b4, #829fff));
  --accent-text: var(--tg-theme-button-text-color, light-dark(#fff, #121820));
  --tint: color-mix(in srgb, var(--accent) 10%, var(--surface));
  --positive: light-dark(#08745b, #76dfbd);
  --negative: light-dark(#ab3934, #ff9b8f);
  --warning: light-dark(#986307, #f2ba63);
  background: var(--canvas);
  color: var(--ink);
  accent-color: var(--accent);
  font-synthesis: none;
}
*, *::before, *::after { box-sizing: border-box; }
body { margin: 0; }
[hidden] { display: none !important; }
a { color: inherit; text-decoration: none; }
button, input, select, textarea { font: inherit; }
button { cursor: pointer; }
button:disabled { cursor: wait; opacity: .55; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 3px; }
h1, h2, h3, p { margin: 0; }
h1 { font-size: clamp(1.65rem, 6vw, 2.4rem); letter-spacing: -.045em; line-height: 1.1; }
h2 { font-size: 1.06rem; letter-spacing: -.015em; }
h3 { font-size: 1rem; }
p, small, strong, .pill { overflow-wrap: anywhere; }
small { color: var(--muted); font-size: .83rem; }
.auth { display: grid; min-height: 100svh; place-items: center; padding: 1.25rem; }
.auth-card {
  display: grid;
  gap: 1rem;
  width: min(100%, 28rem);
  padding: clamp(1.5rem, 7vw, 2.5rem);
  border: 1px solid var(--line);
  border-radius: 1.4rem;
  background: var(--surface);
}
.auth-card p { min-height: 1.5rem; line-height: 1.5; }
.auth-card button { justify-self: start; }
.header { background: var(--surface); border-bottom: 1px solid var(--line); padding-top: env(safe-area-inset-top); }
.header-inner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: .75rem;
  max-width: 65rem;
  min-height: 66px;
  padding-inline: clamp(1rem, 4vw, 2rem);
  margin: auto;
}
.brand { display: inline-flex; align-items: center; gap: .65rem; font-weight: 730; letter-spacing: -.035em; }
.brand-icon, .avatar {
  display: inline-grid;
  width: 40px;
  height: 40px;
  flex-shrink: 0;
  place-items: center;
  border-radius: 12px;
  background: var(--tint);
  color: var(--accent);
  font-weight: 760;
  font-variant-numeric: tabular-nums;
}
.avatar { border-radius: 50%; width: 44px; height: 44px; }
.content {
  max-width: 65rem;
  min-height: 48vh;
  margin: auto;
  padding: clamp(1.2rem, 4vw, 2.25rem) clamp(1rem, 4vw, 2rem) calc(95px + env(safe-area-inset-bottom));
}
.page { min-width: 0; display: grid; gap: 1rem; }
.page-heading { display: flex; justify-content: space-between; align-items: center; flex-wrap: wrap; gap: .8rem; margin-bottom: .2rem; }
.actions, .cluster { display: flex; align-items: center; flex-wrap: wrap; gap: .6rem; min-width: 0; }
.actions a, .actions button { max-width: 100%; }
.primary, .secondary, .subtle, .danger {
  display: inline-flex;
  min-height: 44px;
  align-items: center;
  justify-content: center;
  padding: .57rem .87rem;
  border: 1px solid transparent;
  border-radius: .75rem;
  line-height: 1.25;
  font-weight: 670;
  text-align: center;
}
.primary { background: var(--accent); color: var(--accent-text); }
.secondary { background: var(--tint); color: var(--accent); }
.subtle { color: var(--ink); border-color: var(--line); background: var(--surface); }
.danger { background: color-mix(in srgb, var(--negative) 9%, var(--surface)); color: var(--negative); }
.panel { min-width: 0; padding: 1.1rem; background: var(--surface); border: 1px solid var(--line); border-radius: 1.05rem; }
.panel > h2 { margin-bottom: .85rem; }
.grid-two, .grid-three { display: grid; gap: 1rem; min-width: 0; }
.hero {
  display: grid;
  gap: .95rem;
  padding: clamp(1.25rem, 6vw, 2rem);
  min-width: 0;
  border-radius: 1.4rem;
  background: light-dark(#1d2c53, #253968);
  color: #f7f9ff;
}
.hero small { color: #bdcbe5; }
.hero-balance { font-size: clamp(1.8rem, 9vw, 3.55rem); letter-spacing: -.07em; line-height: 1.1; overflow-wrap: anywhere; }
.hero-values { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: .8rem; border-top: 1px solid #ffffff32; padding-top: .82rem; }
.hero-values strong { display: block; margin-top: .35rem; font-size: clamp(1rem, 5vw, 1.45rem); }
.hero .positive { color: #a9e4ca; }
.hero .negative { color: #ffc0bb; }
.month-control { display: inline-grid; align-items: center; grid-template-columns: 44px 1fr 44px; gap: .25rem; border: 1px solid var(--line); border-radius: .75rem; background: var(--surface); text-align: center; white-space: nowrap; font-size: .93rem; font-weight: 650; }
.month-arrow { min-width: 44px; min-height: 44px; padding: 0; border: 0; border-radius: .68rem; background: transparent; color: var(--accent); font-size: 1.55rem; }
.month-arrow:hover { background: var(--tint); }
.rows { display: grid; }
.row { display: flex; gap: .75rem; align-items: center; justify-content: space-between; min-width: 0; min-height: 52px; padding: .72rem 0; border: 0; background: transparent; color: inherit; text-align: left; }
.row + .row { border-top: 1px solid var(--line); }
.row-copy { flex: 1 1 auto; min-width: 0; display: grid; gap: .22rem; }
.row-value { flex: 0 1 auto; text-align: right; font-variant-numeric: tabular-nums; white-space: normal; overflow-wrap: anywhere; font-weight: 690; }
.row-arrow { color: var(--accent); font-size: 1.3rem; }
.row-button:hover, .row-button:focus-visible { color: var(--accent); }
.positive { color: var(--positive); }
.negative { color: var(--negative); }
.warning { color: var(--warning); }
.pill { background: var(--tint); padding: .26rem .5rem; border-radius: .55rem; font-size: .78rem; font-weight: 650; }
.empty, .loading, .muted { color: var(--muted); }
.empty, .loading { line-height: 1.55; }
.progress-card { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: .5rem; align-items: center; padding-block: .6rem; }
.progress-track { grid-column: 1 / -1; height: .5rem; border-radius: 2rem; background: var(--line); overflow: hidden; }
.progress-track > span { height: 100%; display: block; border-radius: inherit; background: currentColor; }
.progress-card small { grid-column: 1 / -1; }
.form, .form-stack { display: grid; gap: .9rem; }
.form .actions, .form > button { justify-self: start; }
.field { min-width: 0; display: grid; align-content: start; gap: .35rem; font-weight: 620; font-size: .91rem; }
.field > input, .field > select, .field > textarea {
  width: 100%;
  min-width: 0;
  min-height: 46px;
  padding: .6rem .72rem;
  color: var(--ink);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: .68rem;
  font-size: 16px;
  font-weight: 450;
}
.field > textarea { min-height: 100px; resize: vertical; }
.toggle { display: flex; align-items: center; gap: .6rem; min-height: 44px; color: var(--ink); }
.toggle input { width: 1.1rem; height: 1.1rem; flex-shrink: 0; }
.filter-fields, .fields { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 145px), 1fr)); gap: .85rem; }
fieldset { min-width: 0; display: grid; gap: .35rem; padding: .8rem; border: 1px solid var(--line); border-radius: .7rem; }
legend { font-weight: 650; color: var(--muted); font-size: .87rem; }
.form-error, .table-error { color: var(--negative); font-size: .88rem; }
details > summary { cursor: pointer; min-height: 44px; padding-block: .75rem; font-weight: 670; }
details[open] > summary { margin-bottom: .6rem; }
.card-list { display: grid; gap: .55rem; }
.history-group { margin-bottom: .65rem; }
.history-group h3 { margin: .7rem 0 .25rem; color: var(--muted); font-size: .85rem; }
.table { width: 100%; table-layout: auto; border-collapse: collapse; font-size: .9rem; overflow-wrap: anywhere; }
.table caption { text-align: left; font-weight: 650; padding-bottom: .65rem; }
.table th, .table td { padding: .55rem; border-bottom: 1px solid var(--line); text-align: left; }
.table th { color: var(--muted); }
.report-list { display: grid; gap: .6rem; }
.report-list dl { margin: 0; padding: .6rem 0; display: grid; gap: .35rem; }
.report-list dl + dl { border-top: 1px solid var(--line); }
.report-list dl > div { display: flex; align-items: baseline; gap: .45rem; justify-content: space-between; min-width: 0; }
.report-list dt, .report-list dd { overflow-wrap: anywhere; min-width: 0; }
.report-list dd { margin: 0; text-align: right; }
.nav { background: var(--surface); border-top: 1px solid var(--line); position: fixed; z-index: 5; inset: auto 0 0; padding-bottom: env(safe-area-inset-bottom); }
.nav-inner { display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); max-width: 34rem; margin: auto; padding: .38rem .35rem .42rem; }
.nav-item { display: grid; grid-template-rows: 26px auto; align-items: center; justify-items: center; gap: .25rem; min-width: 0; min-height: 56px; padding: .18rem .05rem; border-radius: .65rem; text-align: center; color: var(--muted); font-size: clamp(.65rem, 3vw, .78rem); }
.nav-item.current { color: var(--accent); background: var(--tint); font-weight: 720; }
.nav-icon { width: 20px; height: 20px; display: grid; place-items: center; position: relative; }
.icon-overview { border: 1.5px solid currentColor; border-radius: 5px; }
.icon-overview::before { content: ""; width: 2px; height: 8px; background: currentColor; box-shadow: 4px -2px currentColor, -4px 2px currentColor; }
.icon-ledger { border: 1.5px solid currentColor; border-radius: 4px; }
.icon-ledger::before { content: ""; width: 10px; height: 1.5px; background: currentColor; box-shadow: 0 4px currentColor, 0 -4px currentColor; }
.icon-plus { width: 25px; height: 25px; color: var(--accent-text); background: var(--accent); border-radius: 8px; }
.icon-plus::before, .icon-plus::after { content: ""; width: 13px; height: 1.5px; background: currentColor; grid-area: 1 / 1; }
.icon-plus::after { transform: rotate(90deg); }
.icon-plan { border-radius: 50%; border: 1.5px solid currentColor; }
.icon-plan::before { content: ""; width: 9px; height: 9px; border-radius: 50%; border: 1.5px solid currentColor; }
.icon-more::before { content: ""; width: 3px; height: 3px; border-radius: 50%; background: currentColor; box-shadow: -6px 0 currentColor, 6px 0 currentColor; }
#toast { position: fixed; z-index: 10; inset: auto 1rem calc(84px + env(safe-area-inset-bottom)); margin: auto; max-width: 34rem; padding: .8rem 1rem; border-radius: .8rem; background: var(--ink); color: var(--surface); text-align: center; opacity: 0; pointer-events: none; transition: opacity .15s ease; }
#toast.visible { opacity: 1; }
.error-panel { display: grid; justify-items: start; gap: .9rem; }
.receipt-photo { max-inline-size: 100%; max-block-size: 22rem; object-fit: contain; border-radius: .75rem; }
.chart { display: grid; gap: .6rem; }
.chart-value { display: grid; grid-template-columns: 6.4rem minmax(0, 1fr); gap: .65rem; align-items: center; }
.chart-track { display: grid; gap: .25rem; }
.chart-bar { height: .44rem; border-radius: 2rem; background: var(--positive); }
.chart-bar.expense { background: var(--negative); }
.chart-bar.savings { background: var(--accent); }
@media (min-width: 640px) {
  .grid-two { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .grid-three { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .panel { padding: 1.25rem; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition-duration: 0.001ms !important; }
}
.import-row { display: grid; gap: .2rem; padding-block: .55rem; border-bottom: 1px solid var(--line); }
.import-row .row { min-height: 0; padding: .3rem 0; }
.category-node + .category-node, .rule-entry + .rule-entry { border-top: 1px solid var(--line); }
.category-children { padding-left: 1rem; border-left: 1px solid var(--line); }
.rule-entry > summary { overflow-wrap: anywhere; }
