:root {
  --bg: var(--tg-theme-secondary-bg-color, #efeff4);
  --card: var(--tg-theme-section-bg-color, var(--tg-theme-bg-color, #ffffff));
  --text: var(--tg-theme-text-color, #111111);
  --muted: var(--tg-theme-hint-color, #8a8a8f);
  --accent: var(--tg-theme-button-color, #2a78d6);
  --accent-text: var(--tg-theme-button-text-color, #ffffff);
  --danger: var(--tg-theme-destructive-text-color, #d93b3b);
  --line: rgba(128, 128, 128, .22);
  --press: rgba(128, 128, 128, .18);
  /* Палитра графиков (светлая тема) */
  --s1: #2a78d6; --s2: #eb6834; --s3: #1baf7a; --s4: #eda100;
  --s5: #e87ba4; --s6: #008300; --s7: #6250d6; --s-other: #9a9a96;
  --safe-b: max(env(safe-area-inset-bottom, 0px), var(--tg-safe-area-inset-bottom, 0px));
}
:root[data-theme="dark"] {
  --bg: var(--tg-theme-secondary-bg-color, #0f0f10);
  --card: var(--tg-theme-section-bg-color, var(--tg-theme-bg-color, #1c1c1e));
  --text: var(--tg-theme-text-color, #f2f2f2);
  --muted: var(--tg-theme-hint-color, #98989e);
  --s1: #3987e5; --s2: #d95926; --s3: #199e70; --s4: #c98500;
  --s5: #d55181; --s6: #008300; --s7: #9085e9; --s-other: #6f6f6b;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; overflow: hidden; }
body {
  background: var(--bg); color: var(--text);
  font: 15px/1.35 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  -webkit-user-select: none; user-select: none; overscroll-behavior: none;
}
button { font: inherit; color: inherit; border: 0; background: none; padding: 0; cursor: pointer; }
input, select { font: inherit; color: var(--text); }

#app {
  height: var(--tg-viewport-stable-height, 100dvh);
  max-width: 560px; margin: 0 auto;
  display: flex; flex-direction: column;
}
.view { display: none; flex: 1; min-height: 0; flex-direction: column; }
.view.active { display: flex; }
.view.scroll.active { display: block; }
.view.scroll { overflow-y: auto; padding: 12px 12px 24px; -webkit-overflow-scrolling: touch; }

/* ---------- Вкладки ---------- */
.tabs {
  display: flex; background: var(--card); border-top: 1px solid var(--line);
  padding-bottom: var(--safe-b); flex: none;
}
.tabs button { flex: 1; padding: 6px 0 7px; color: var(--muted); display: flex; flex-direction: column; align-items: center; gap: 1px; }
.tabs button i { font-style: normal; font-size: 19px; line-height: 22px; }
.tabs button span { font-size: 11px; }
.tabs button.on { color: var(--accent); }

/* ---------- Сегменты и чипы ---------- */
.seg { display: flex; background: var(--press); border-radius: 10px; padding: 2px; }
.seg button { flex: 1; padding: 7px 10px; border-radius: 8px; font-size: 14px; color: var(--text); }
.seg button.on { background: var(--card); font-weight: 600; box-shadow: 0 1px 3px rgba(0, 0, 0, .12); }
.seg.small button { padding: 5px 12px; font-size: 13px; }
.chips { display: flex; gap: 6px; overflow-x: auto; padding: 2px 0; scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }
.chip { flex: none; padding: 6px 12px; border-radius: 16px; background: var(--card); font-size: 14px; white-space: nowrap; border: 1px solid transparent; }
.chip.on { background: var(--accent); color: var(--accent-text); }

/* ---------- Экран ввода ---------- */
.add-top { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 10px 12px 0; flex: none; }
.summary { font-size: 12px; color: var(--muted); text-align: right; line-height: 1.3; }
.summary b { color: var(--text); font-weight: 600; }
.amount {
  flex: none; text-align: center; padding: 6px 12px 0; font-size: 46px; font-weight: 700;
  letter-spacing: -.5px; line-height: 1.15; white-space: nowrap; overflow: hidden; font-variant-numeric: tabular-nums;
}
.amount.zero #amount { color: var(--muted); }
.amount .cur { font-size: 26px; font-weight: 500; color: var(--muted); margin-left: 6px; }
.amount.shake { animation: shake .3s; }
@keyframes shake { 25% { transform: translateX(-8px); } 75% { transform: translateX(8px); } }
.hint { flex: none; text-align: center; font-size: 12px; color: var(--muted); padding: 0 12px 6px; min-height: 22px; }

.cats {
  flex: 1; min-height: 0; overflow-y: auto; padding: 2px 10px 8px;
  display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; align-content: start;
}
.cat {
  background: var(--card); border-radius: 12px; padding: 6px 2px 6px; min-height: 74px;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  transition: transform .08s, opacity .15s;
}
.cat:active { transform: scale(.94); background: var(--press); }
.cat .sum { font-size: 11px; font-weight: 600; line-height: 14px; min-height: 14px; max-width: 100%; white-space: nowrap; overflow: hidden; font-variant-numeric: tabular-nums; }
.cat .sum.left { color: var(--accent); }
.cat .sum.left.over { color: var(--danger); }
.limits { margin-top: 6px; }
.limits .row { padding: 7px 0; }
.limits input {
  width: 108px; flex: none; padding: 8px 10px; border-radius: 10px; border: 1px solid var(--line); background: var(--bg);
  text-align: right; outline: none; -webkit-user-select: text; user-select: text; font-variant-numeric: tabular-nums;
}
.limits input:focus { border-color: var(--accent); }
.limits .cur { color: var(--muted); width: 22px; flex: none; }
.cat .e { font-size: 24px; line-height: 28px; }
.cat .n { font-size: 11px; max-width: 100%; padding: 0 3px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.cats.idle .cat { opacity: .55; }

.keypad {
  flex: none; display: grid; grid-template-columns: repeat(3, 1fr); gap: 1px;
  background: var(--line); border-top: 1px solid var(--line);
}
.keypad button { background: var(--card); height: 50px; font-size: 23px; font-weight: 500; }
.keypad button:active { background: var(--press); }
@media (max-height: 560px) {
  .keypad button { height: 42px; }
  .amount { font-size: 36px; }
  .cat { min-height: 64px; padding-top: 4px; }
  .cat .e { font-size: 21px; line-height: 24px; }
}

/* ---------- Карточки, списки, таблицы ---------- */
.card { background: var(--card); border-radius: 14px; padding: 12px; margin-top: 10px; }
.card h4 { margin: 0 0 8px; font-size: 13px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: .3px; }
.day-head { display: flex; justify-content: space-between; padding: 14px 6px 5px; font-size: 13px; color: var(--muted); }
.day-head:first-child { padding-top: 2px; }
.list { background: var(--card); border-radius: 14px; overflow: hidden; }
.row { display: flex; align-items: center; gap: 10px; padding: 9px 12px; width: 100%; text-align: left; }
.row + .row { border-top: 1px solid var(--line); }
button.row:active { background: var(--press); }
.row .e { font-size: 22px; width: 28px; text-align: center; flex: none; }
.row .mid { flex: 1; min-width: 0; }
.row .t { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row .s { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row .a { font-weight: 600; white-space: nowrap; font-variant-numeric: tabular-nums; }
.row .a.inc::before { content: "+"; }
.badge { font-size: 11px; color: var(--muted); border: 1px solid var(--line); border-radius: 8px; padding: 1px 6px; margin-left: 6px; }
.empty { text-align: center; color: var(--muted); padding: 36px 16px; }

table.tbl { width: 100%; border-collapse: collapse; font-size: 14px; }
.tbl th { text-align: right; font-weight: 500; font-size: 12px; color: var(--muted); padding: 4px 0 4px 8px; }
.tbl th:first-child { text-align: left; padding-left: 0; }
.tbl td { padding: 7px 0 7px 8px; border-top: 1px solid var(--line); text-align: right; white-space: nowrap; font-variant-numeric: tabular-nums; }
.tbl td:first-child { text-align: left; padding-left: 0; white-space: normal; }
.tbl tr.sel td { font-weight: 600; }
.tbl .pct { color: var(--muted); width: 44px; }
.dot { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 7px; vertical-align: 0; }

.btn { display: block; width: 100%; padding: 12px; border-radius: 12px; background: var(--accent); color: var(--accent-text); font-weight: 600; text-align: center; margin-top: 10px; }
.btn.ghost { background: var(--press); color: var(--text); }
.btn.danger { background: none; color: var(--danger); }
.btn:active { opacity: .7; }
.link { color: var(--accent); padding: 4px 0; }

/* ---------- Аналитика ---------- */
.pernav { display: flex; align-items: center; justify-content: space-between; margin: 8px 0 6px; }
.pernav button { width: 40px; height: 34px; font-size: 24px; border-radius: 10px; color: var(--accent); }
.pernav button:disabled { color: var(--muted); opacity: .4; }
.pernav span { font-weight: 600; font-size: 16px; }
.tiles { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 8px; margin-top: 10px; }
.tile { background: var(--card); border-radius: 14px; padding: 10px; min-width: 0; }
.tile .l { font-size: 12px; color: var(--muted); }
.tile .v { font-size: 16px; font-weight: 700; margin-top: 2px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-variant-numeric: tabular-nums; }
.tile .d { font-size: 11px; color: var(--muted); margin-top: 2px; line-height: 1.25; }
.chart { position: relative; }
.chart svg { display: block; width: 100%; overflow: visible; }
.chart text { fill: var(--muted); font-size: 10px; }
.chart .grid { stroke: var(--line); stroke-width: 1; }
.chart .bar { transition: opacity .12s; }
.chart.hover .bar { opacity: .4; }
.chart.hover .bar.on { opacity: 1; }
.tip {
  position: absolute; pointer-events: none; transform: translate(-50%, -100%); white-space: nowrap;
  background: var(--text); color: var(--card); font-size: 12px; padding: 4px 8px; border-radius: 8px; z-index: 2;
}
.tip b { font-weight: 600; }
.donut { position: relative; width: 170px; margin: 4px auto 10px; }
.donut svg { display: block; width: 100%; }
.donut circle { transition: opacity .12s; cursor: pointer; }
.donut.hover circle { opacity: .3; }
.donut.hover circle.on { opacity: 1; }
.donut .center { position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; pointer-events: none; text-align: center; padding: 0 34px; }
.donut .center .l { font-size: 11px; color: var(--muted); max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.donut .center .v { font-size: 16px; font-weight: 700; white-space: nowrap; }

/* ---------- Формы, шторка, тост ---------- */
.field { display: block; margin-top: 10px; }
.field > span { display: block; font-size: 12px; color: var(--muted); margin-bottom: 4px; }
.field input, .field select {
  width: 100%; padding: 10px 12px; border-radius: 10px; border: 1px solid var(--line);
  background: var(--bg); outline: none; -webkit-user-select: text; user-select: text; min-height: 42px;
}
.field input:focus { border-color: var(--accent); }
.inline { display: flex; gap: 8px; align-items: flex-end; }
.inline .field { flex: 1; }
.inline .btn { width: auto; padding: 11px 16px; margin: 0; }
.code { font: 600 20px/1 ui-monospace, Menlo, monospace; letter-spacing: 2px; -webkit-user-select: text; user-select: text; }
.note { font-size: 12px; color: var(--muted); margin-top: 6px; }

.overlay { position: fixed; inset: 0; background: rgba(0, 0, 0, .45); z-index: 20; display: flex; align-items: flex-end; justify-content: center; }
.sheet {
  width: 100%; max-width: 560px; max-height: 88%; overflow-y: auto; background: var(--card);
  border-radius: 18px 18px 0 0; padding: 16px 14px calc(16px + var(--safe-b)); animation: up .18s ease-out;
}
@keyframes up { from { transform: translateY(40px); opacity: 0; } }
.sheet h3 { margin: 0 0 4px; font-size: 17px; }
.sheet .chips { flex-wrap: wrap; overflow: visible; }
.sheet .chip { background: var(--bg); }
.sheet .chip.on { background: var(--accent); }

.toast {
  position: fixed; left: 50%; transform: translateX(-50%); top: calc(8px + env(safe-area-inset-top, 0px)); z-index: 30;
  background: var(--text); color: var(--card); border-radius: 14px; padding: 10px 14px;
  display: flex; align-items: center; gap: 14px; max-width: calc(100% - 24px); font-size: 14px;
  box-shadow: 0 6px 20px rgba(0, 0, 0, .25); animation: drop .18s ease-out;
}
@keyframes drop { from { margin-top: -30px; opacity: 0; } }
.toast span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.toast button { color: inherit; font-weight: 600; text-decoration: underline; flex: none; }
.fatal { position: fixed; inset: 0; z-index: 50; background: var(--bg); display: flex; align-items: center; justify-content: center; text-align: center; padding: 32px; color: var(--muted); }
[hidden] { display: none !important; }
