:root {
  color-scheme: light dark;
  --bg: var(--tg-theme-bg-color, #f4f5f9);
  --surface: var(--tg-theme-secondary-bg-color, #ffffff);
  --surface-2: #edf0f5;
  --surface-2: color-mix(in srgb, var(--surface) 88%, var(--text) 12%);
  --text: var(--tg-theme-text-color, #141625);
  --muted: var(--tg-theme-hint-color, #777b8e);
  --accent: var(--tg-theme-button-color, #5b67f1);
  --accent-text: var(--tg-theme-button-text-color, #ffffff);
  --link: var(--tg-theme-link-color, #5865e8);
  --positive: #137a49;
  --negative: #c7364d;
  --transfer: #216ac1;
  --warning: #f19a38;
  --border: rgba(20, 22, 37, .11);
  --border: color-mix(in srgb, var(--text) 11%, transparent);
  --shadow: 0 10px 30px rgba(18, 22, 42, .08);
  --safe-top: max(8px, env(safe-area-inset-top, 0px), var(--tg-safe-area-inset-top, 0px), var(--tg-content-safe-area-inset-top, 0px));
  --safe-right: max(0px, env(safe-area-inset-right, 0px), var(--tg-safe-area-inset-right, 0px), var(--tg-content-safe-area-inset-right, 0px));
  --safe-bottom: max(8px, env(safe-area-inset-bottom, 0px), var(--tg-safe-area-inset-bottom, 0px), var(--tg-content-safe-area-inset-bottom, 0px));
  --safe-left: max(0px, env(safe-area-inset-left, 0px), var(--tg-safe-area-inset-left, 0px), var(--tg-content-safe-area-inset-left, 0px));
  --app-height: var(--tg-viewport-stable-height, var(--browser-stable-height, 100dvh));
  --visual-height: var(--tg-viewport-height, var(--browser-visual-height, 100dvh));
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; min-height: 100%; background: var(--bg); color: var(--text); overflow-x: clip; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  font-size: 16px;
  min-height: 100vh;
  min-height: var(--app-height);
  overscroll-behavior: none;
}
button, input, select, textarea { font: inherit; color: inherit; }
button { border: 0; cursor: pointer; }
button:focus-visible, input:focus-visible, select:focus-visible, textarea:focus-visible { outline: 3px solid var(--accent); outline: 3px solid color-mix(in srgb, var(--accent) 42%, transparent); outline-offset: 2px; }
.hidden { display: none !important; }

.app { min-height: 100vh; min-height: var(--app-height); max-width: 720px; margin: 0 auto; overflow-x: clip; }
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; justify-content: space-between;
  gap: 12px; min-height: 76px;
  padding: var(--safe-top) calc(18px + var(--safe-right)) 10px calc(18px + var(--safe-left));
  background: var(--bg);
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  -webkit-backdrop-filter: blur(18px);
  backdrop-filter: blur(18px);
}
.topbar > div { min-width: 0; }
.eyebrow { margin: 0 0 2px; color: var(--muted); font-size: 12px; font-weight: 650; letter-spacing: .035em; }
.topbar h1 { margin: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 24px; letter-spacing: -.025em; }
.icon-btn { flex: 0 0 44px; width: 44px; height: 44px; border-radius: 50%; background: var(--surface); font-size: 24px; box-shadow: 0 3px 14px rgba(0,0,0,.05); }
.icon-btn.spinning { animation: spin .75s linear infinite; }
.view { padding: 8px calc(16px + var(--safe-right)) calc(102px + var(--safe-bottom)) calc(16px + var(--safe-left)); outline: none; }

.balance-card {
  position: relative; overflow: hidden; padding: 22px;
  border-radius: 24px; color: #fff;
  background: linear-gradient(135deg, #5361ec 0%, #7955d9 58%, #a750c6 100%);
  box-shadow: 0 18px 36px rgba(87, 81, 219, .25);
}
.balance-card::after { content: ""; position: absolute; width: 160px; height: 160px; border-radius: 50%; right: -60px; top: -70px; pointer-events: none; background: rgba(255,255,255,.11); }
.balance-card p { margin: 0 0 5px; opacity: .8; font-size: 14px; }
.big-money { max-width: 100%; margin: 0; overflow-wrap: anywhere; font-size: clamp(26px, 8vw, 42px); line-height: 1.1; letter-spacing: -.04em; font-variant-numeric: tabular-nums; }
.month-nav { display: flex; align-items: center; gap: 8px; margin-top: 20px; }
.month-nav button { width: 44px; height: 44px; border-radius: 14px; color: #fff; background: rgba(255,255,255,.14); }
.month-nav span { flex: 1; text-align: center; font-size: 14px; font-weight: 650; text-transform: capitalize; }

.quick-actions { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin: 14px 0 20px; }
.quick-action { min-height: 58px; padding: 9px 6px; border-radius: 16px; background: var(--surface); box-shadow: var(--shadow); font-weight: 650; }
.quick-action span { display: block; font-size: 20px; margin-bottom: 3px; }
.quick-action.expense span { color: var(--negative); }
.quick-action.income span { color: var(--positive); }
.quick-action.transfer span { color: var(--transfer); }

.section { margin: 22px 0; }
.section-head { display: flex; min-width: 0; align-items: center; justify-content: space-between; gap: 10px; margin: 0 3px 10px; }
.section-head h2 { min-width: 0; margin: 0; overflow-wrap: anywhere; font-size: 18px; letter-spacing: -.015em; }
.text-btn { padding: 8px 0; color: var(--link); background: transparent; font-size: 14px; font-weight: 650; }
.card { background: var(--surface); border-radius: 19px; padding: 16px; box-shadow: 0 3px 18px rgba(0,0,0,.035); }
.stats-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 1px; padding: 0; overflow: hidden; }
.stat { min-width: 0; padding: 16px 8px; text-align: center; }
.stat + .stat { border-left: 1px solid var(--border); }
.stat small { display: block; color: var(--muted); margin-bottom: 7px; }
.stat strong { display: block; min-width: 0; overflow-wrap: anywhere; font-size: clamp(11px, 3.45vw, 15px); letter-spacing: -.025em; font-variant-numeric: tabular-nums; }
.positive { color: var(--positive) !important; }
.negative { color: var(--negative) !important; }
.transfer-color { color: var(--transfer) !important; }

.category-row, .budget-row { margin: 14px 0; }
.row-line { display: flex; min-width: 0; align-items: center; justify-content: space-between; gap: 12px; }
.row-line > :first-child { min-width: 0; overflow-wrap: anywhere; }
.row-line strong { text-align: right; }
.category-row .row-line > :first-child, .budget-row .row-line > :first-child { flex: 1 1 auto; }
.category-row .row-line > strong, .budget-row .row-line > strong { flex: 0 1 auto; max-width: 58%; overflow-wrap: anywhere; }
.progress { height: 7px; margin-top: 8px; border-radius: 10px; overflow: hidden; background: var(--surface-2); }
.progress > i { display: block; height: 100%; border-radius: inherit; background: var(--accent); }
.progress.warning > i { background: var(--warning); }
.progress.danger > i { background: var(--negative); }

.operation-list { overflow: hidden; padding: 0; }
.operation-row { display: grid; grid-template-columns: 42px minmax(0, 1fr); grid-template-areas: "icon main" "icon side"; align-items: center; column-gap: 11px; row-gap: 3px; width: 100%; min-height: 82px; padding: 11px 14px; text-align: left; background: transparent; }
.operation-row + .operation-row { border-top: 1px solid var(--border); }
.op-icon { grid-area: icon; display: grid; place-items: center; width: 42px; height: 42px; overflow: hidden; border-radius: 14px; background: var(--surface-2); font-size: 20px; }
.op-main { grid-area: main; min-width: 0; }
.op-main strong, .op-main small { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.op-main small, .op-side small { color: var(--muted); font-size: 12px; margin-top: 4px; }
.op-side { grid-area: side; display: flex; min-width: 0; align-items: baseline; justify-content: space-between; gap: 8px; text-align: left; }
.op-side strong { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 14px; font-variant-numeric: tabular-nums; }
.op-side small { flex: 0 0 auto; margin-top: 0; white-space: nowrap; }
.date-label { margin: 20px 4px 8px; color: var(--muted); font-size: 13px; font-weight: 700; text-transform: uppercase; letter-spacing: .04em; }

.empty { padding: 36px 20px; text-align: center; }
.empty-icon { font-size: 42px; }
.empty h3 { margin: 12px 0 6px; overflow-wrap: anywhere; }
.empty p { color: var(--muted); line-height: 1.45; }

.account-card { width: 100%; margin-bottom: 10px; padding: 17px; overflow: hidden; text-align: left; background: var(--surface); border-radius: 18px; box-shadow: 0 3px 14px rgba(0,0,0,.03); }
.account-card .row-line + .row-line { margin-top: 10px; }
.account-card.archived { opacity: .78; border: 1px dashed var(--border); box-shadow: none; }
.account-name { flex: 1 1 auto; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.account-balance { flex: 0 0 auto; max-width: 58%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-variant-numeric: tabular-nums; }
.badge { display: inline-block; flex: 0 0 auto; padding: 4px 8px; border-radius: 20px; color: var(--accent); background: rgba(91, 103, 241, .12); background: color-mix(in srgb, var(--accent) 12%, transparent); font-size: 11px; font-weight: 700; white-space: nowrap; }
.total-line { margin: 8px 2px 18px; }
.total-line small { color: var(--muted); }
.total-line strong { display: block; max-width: 100%; margin-top: 4px; overflow-wrap: anywhere; font-size: clamp(26px, 8vw, 30px); line-height: 1.15; letter-spacing: -.035em; font-variant-numeric: tabular-nums; }

.menu-group { margin-bottom: 20px; }
.menu-group > h3 { margin: 0 5px 8px; color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: .05em; }
.menu-list { overflow: hidden; padding: 0; }
.menu-item { display: flex; align-items: center; gap: 12px; width: 100%; min-height: 58px; padding: 12px 15px; background: transparent; text-align: left; }
.menu-item + .menu-item { border-top: 1px solid var(--border); }
.menu-item > * { min-width: 0; }
.menu-item > span:not(.menu-emoji):not(.chevron) { flex: 1 1 auto; overflow-wrap: anywhere; }
.menu-item > strong { flex: 0 1 auto; max-width: 52%; text-align: right; overflow-wrap: anywhere; }
.menu-emoji { display: grid; flex: 0 0 34px; place-items: center; width: 34px; height: 34px; overflow: hidden; border-radius: 11px; background: var(--surface-2); }
.chevron { flex: 0 0 auto !important; color: var(--muted); font-size: 22px; }

.bottom-nav {
  position: fixed; z-index: 30; left: 0; right: 0; bottom: 0;
  display: grid; grid-template-columns: 1fr 1fr 68px 1fr 1fr; align-items: end;
  max-width: 720px; margin: auto; min-height: 72px;
  padding: 7px calc(8px + var(--safe-right)) var(--safe-bottom) calc(8px + var(--safe-left)); border-top: 1px solid var(--border);
  background: var(--surface);
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  -webkit-backdrop-filter: blur(22px); backdrop-filter: blur(22px);
}
.nav-item { min-width: 0; height: 54px; overflow: hidden; color: var(--muted); background: transparent; }
.nav-item span { display: block; height: 25px; font-size: 23px; line-height: 23px; }
.nav-item small { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 10px; font-weight: 650; }
.nav-item.active { color: var(--accent); }
.add-button { align-self: start; width: 58px; height: 58px; margin: -17px auto 0; border-radius: 21px; color: var(--accent-text); background: var(--accent); box-shadow: 0 9px 22px color-mix(in srgb, var(--accent) 38%, transparent); }
.add-button span { font-size: 34px; line-height: 1; font-weight: 300; }

.sheet-backdrop { position: fixed; inset: 0; z-index: 60; background: rgba(10, 12, 24, .42); -webkit-backdrop-filter: blur(3px); backdrop-filter: blur(3px); }
.sheet { position: fixed; z-index: 70; left: var(--safe-left); right: var(--safe-right); bottom: var(--keyboard-offset, 0px); display: flex; flex-direction: column; max-width: 720px; max-height: min(92vh, 900px); max-height: min(92dvh, var(--visual-height), 900px); margin: auto; padding-bottom: var(--safe-bottom); border-radius: 26px 26px 0 0; background: var(--bg); box-shadow: 0 -15px 50px rgba(0,0,0,.2); animation: rise .18s ease-out; }
@keyframes rise { from { transform: translateY(25px); opacity: .5; } }
.sheet-handle { flex: 0 0 auto; width: 38px; height: 4px; margin: 8px auto 2px; border-radius: 5px; background: var(--border); }
.sheet-head { display: flex; flex: 0 0 auto; min-width: 0; align-items: center; justify-content: space-between; gap: 12px; padding: 8px 16px; }
.sheet-head h2 { flex: 1 1 auto; min-width: 0; margin: 0; overflow-wrap: anywhere; font-size: 21px; line-height: 1.2; }
.sheet-head .icon-btn { box-shadow: none; background: var(--surface); }
.sheet-body { flex: 1 1 auto; min-height: 0; max-height: none; padding: 6px 16px 22px; overflow-y: auto; overscroll-behavior: contain; -webkit-overflow-scrolling: touch; }

.segment { display: grid; grid-template-columns: repeat(3, 1fr); gap: 4px; padding: 4px; margin-bottom: 18px; border-radius: 14px; background: var(--surface-2); }
.segment button { min-width: 0; min-height: 42px; padding: 7px 5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; border-radius: 11px; color: var(--muted); background: transparent; font-weight: 650; }
.segment button.active { color: var(--text); background: var(--surface); box-shadow: 0 2px 8px rgba(0,0,0,.07); }
.field { margin: 14px 0; }
.field label { display: block; margin: 0 3px 7px; color: var(--muted); font-size: 13px; font-weight: 650; }
.input, .select, .textarea { display: block; width: 100%; max-width: 100%; min-height: 52px; padding: 13px 14px; border: 1px solid transparent; outline: none; border-radius: 15px; background: var(--surface); }
.input:focus, .select:focus, .textarea:focus { border-color: var(--accent); }
.amount-input { height: 76px; text-align: center; font-size: clamp(29px, 9vw, 36px); font-weight: 750; letter-spacing: -.035em; font-variant-numeric: tabular-nums; }
.textarea { min-height: 76px; resize: none; }
.category-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px; }
.category-chip { min-height: 46px; padding: 8px; border-radius: 13px; background: var(--surface); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.category-chip.active { color: var(--accent-text); background: var(--accent); }
.primary-btn, .secondary-btn, .danger-btn { width: 100%; min-height: 54px; margin-top: 14px; padding: 12px 18px; border-radius: 16px; font-weight: 720; overflow-wrap: anywhere; }
.primary-btn { color: var(--accent-text); background: var(--accent); }
.secondary-btn { color: var(--text); background: var(--surface); }
.danger-btn { color: #fff; background: var(--negative); }
.primary-btn:disabled, .secondary-btn:disabled, .danger-btn:disabled { cursor: default; opacity: .48; }
.button-row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.hint { color: var(--muted); font-size: 13px; line-height: 1.45; }
.error-text { min-height: 18px; margin: 6px 3px 0; color: var(--negative); font-size: 13px; }
.divider { height: 1px; margin: 18px 0; background: var(--border); }
.sheet-amount { max-width: 100%; margin: 14px 0 22px; text-align: center; overflow-wrap: anywhere; font-size: clamp(28px, 8vw, 34px); line-height: 1.12; letter-spacing: -.035em; font-variant-numeric: tabular-nums; }
.detail-grid { display: grid; grid-template-columns: minmax(96px, .72fr) minmax(0, 1.28fr); gap: 12px; padding: 16px; border-radius: 16px; background: var(--surface); }
.detail-grid > * { min-width: 0; overflow-wrap: anywhere; }
.detail-grid span:nth-child(odd) { color: var(--muted); font-size: 14px; }

.toast { position: fixed; z-index: 100; left: calc(16px + var(--safe-left)); right: calc(16px + var(--safe-right)); bottom: calc(88px + var(--safe-bottom)); display: flex; align-items: center; gap: 12px; max-width: 540px; margin: auto; padding: 14px 16px; border-radius: 15px; color: #fff; background: #222637; box-shadow: 0 10px 30px rgba(0,0,0,.25); animation: toast-in .18s ease-out; }
.toast > span { flex: 1 1 auto; min-width: 0; overflow-wrap: anywhere; }
.toast > button { flex: 0 0 auto; margin: 0; padding: 8px 0; color: #aeb6ff; }
@keyframes toast-in { from { transform: translateY(12px); opacity: 0; } }
.loading-screen, .fatal-screen { min-height: 100vh; min-height: var(--app-height); display: grid; place-content: center; justify-items: center; padding: 24px; text-align: center; }
.brand-mark { display: grid; place-items: center; width: 72px; height: 72px; border-radius: 24px; color: #fff; background: linear-gradient(135deg, #5361ec, #a750c6); font-size: 34px; font-weight: 800; box-shadow: 0 16px 32px rgba(87,81,219,.25); }
.loader { width: 28px; height: 28px; margin: 24px; border: 3px solid var(--border); border-top-color: var(--accent); border-radius: 50%; animation: spin .75s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.loading-screen p, .fatal-screen p { color: var(--muted); }
.fatal-icon { font-size: 50px; }

@media (min-width: 521px) {
  .operation-row { grid-template-columns: 42px minmax(0, 1fr) auto; grid-template-areas: "icon main side"; row-gap: 0; min-height: 70px; }
  .op-side { display: block; text-align: right; }
  .op-side strong, .op-side small { display: block; }
  .op-side small { margin-top: 4px; }
}

@media (max-width: 430px) {
  .stats-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .stats-grid .stat:nth-child(3) { grid-column: 1 / -1; border-top: 1px solid var(--border); border-left: 0; }
  .stat strong { font-size: 13px; }
}

@media (max-width: 360px) {
  .quick-actions { gap: 7px; }
  .quick-action { font-size: 14px; }
  .sheet-head, .sheet-body { padding-left: 14px; padding-right: 14px; }
}

@media (prefers-color-scheme: dark) {
  :root {
    --surface-2: #272a36;
    --surface-2: color-mix(in srgb, var(--surface) 88%, var(--text) 12%);
    --border: rgba(255, 255, 255, .12);
    --border: color-mix(in srgb, var(--text) 11%, transparent);
    --positive: #4ed394;
    --negative: #ff7587;
    --transfer: #69a8ff;
  }
}

@media (orientation: landscape) and (max-height: 500px) {
  .topbar { min-height: 54px; padding-top: max(4px, var(--safe-top)); padding-bottom: 5px; }
  .eyebrow { display: none; }
  .topbar h1 { font-size: 20px; }
  .icon-btn { flex-basis: 44px; width: 44px; height: 44px; }
  .view { padding-top: 4px; padding-bottom: calc(72px + var(--safe-bottom)); }
  .balance-card { padding: 16px 20px; }
  .month-nav { margin-top: 12px; }
  .bottom-nav { min-height: 58px; padding-top: 3px; }
  .nav-item { height: 46px; }
  .nav-item span { height: 21px; font-size: 20px; line-height: 20px; }
  .add-button { width: 50px; height: 50px; margin-top: -12px; border-radius: 17px; }
  .sheet { max-height: 100vh; max-height: var(--visual-height); border-radius: 18px 18px 0 0; }
  .sheet-handle { margin-top: 5px; }
  .sheet-head { padding-top: 4px; padding-bottom: 5px; }
  .sheet-head h2 { font-size: 19px; }
  .sheet-body { padding-top: 2px; padding-bottom: 12px; }
  .segment { margin-bottom: 10px; }
  .field { margin: 10px 0; }
}

@media (min-width: 721px) {
  body { background: color-mix(in srgb, var(--bg) 82%, #222 18%); }
  .app { background: var(--bg); box-shadow: 0 0 40px rgba(0,0,0,.12); }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; scroll-behavior: auto !important; }
}
