/* Цвета берём из темы Telegram (переменные --tg-theme-* выставляет telegram-web-app.js).
   Значения после запятой — запасные, если страницу открыли не из Telegram. */
:root {
  --bg: var(--tg-theme-bg-color, #ffffff);
  --bg2: var(--tg-theme-secondary-bg-color, #f1f1f4);
  --card: var(--tg-theme-section-bg-color, var(--bg));
  --text: var(--tg-theme-text-color, #0b0b0b);
  --hint: var(--tg-theme-hint-color, #8a8983);
  --link: var(--tg-theme-link-color, #2a78d6);
  --accent: var(--tg-theme-button-color, #2a78d6);
  --accent-text: var(--tg-theme-button-text-color, #ffffff);
  --danger: var(--tg-theme-destructive-text-color, #d93a3a);
  --good: #1a9a5c;
  --line: color-mix(in srgb, var(--hint) 25%, transparent);
  --radius: 14px;
  color-scheme: light dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg2); color: var(--text); }
body {
  font: 15px/1.4 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
  padding-bottom: calc(72px + env(safe-area-inset-bottom));
}
button, input, select { font: inherit; color: inherit; }
.hidden { display: none !important; }

main { max-width: 640px; margin: 0 auto; padding: 12px 16px; }
h2 { font-size: 13px; font-weight: 600; text-transform: uppercase; letter-spacing: .04em; color: var(--hint); margin: 20px 4px 8px; }

.card { background: var(--card); border-radius: var(--radius); padding: 14px 16px; margin-bottom: 10px; }
.muted { color: var(--hint); }
.small { font-size: 13px; }
.num { font-variant-numeric: tabular-nums; white-space: nowrap; }
.good { color: var(--good); }
.bad { color: var(--danger); }
.row { display: flex; align-items: center; gap: 10px; }
.grow { flex: 1; min-width: 0; }
.ellipsis { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* Нижнее меню */
nav.tabs {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 10;
  display: flex; background: var(--bg); border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom);
}
nav.tabs button {
  flex: 1; border: 0; background: none; padding: 8px 0 10px; font-size: 11px; color: var(--hint);
  display: flex; flex-direction: column; align-items: center; gap: 2px; cursor: pointer;
}
nav.tabs button .icon { font-size: 20px; line-height: 1.2; }
nav.tabs button.active { color: var(--accent); }

/* Кнопки */
.btn {
  border: 0; border-radius: 10px; padding: 11px 14px; background: var(--accent); color: var(--accent-text);
  font-weight: 600; cursor: pointer; width: 100%;
}
.btn.secondary { background: var(--bg2); color: var(--link); }
.btn.danger { background: none; color: var(--danger); }
.btn.small { width: auto; padding: 7px 12px; font-size: 14px; }
.btn:disabled { opacity: .5; }
.link { border: 0; background: none; color: var(--link); padding: 0; cursor: pointer; }

/* Чипсы (фильтр периода и т.п.) */
.chips { display: flex; gap: 6px; overflow-x: auto; padding-bottom: 2px; scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }
.chip {
  flex: none; border: 0; border-radius: 999px; padding: 7px 13px; background: var(--card); color: var(--text);
  cursor: pointer; font-size: 14px;
}
.chip.active { background: var(--accent); color: var(--accent-text); }

/* Бюджет */
.hero { text-align: center; padding: 20px 16px 16px; }
.hero .label { color: var(--hint); font-size: 14px; }
.hero .big { font-size: 40px; font-weight: 700; letter-spacing: -.02em; margin: 2px 0 10px; }
.progress { height: 8px; border-radius: 99px; background: var(--bg2); overflow: hidden; }
.progress > div { height: 100%; border-radius: 99px; background: var(--accent); transition: width .3s; }
.progress.over > div { background: var(--danger); }
.limit-row { display: flex; justify-content: space-between; margin-top: 6px; font-size: 13px; color: var(--hint); }
.stale { background: color-mix(in srgb, #eda100 18%, var(--card)); }

/* Списки */
.list { background: var(--card); border-radius: var(--radius); overflow: hidden; margin-bottom: 10px; }
.item { display: flex; align-items: center; gap: 12px; padding: 11px 16px; border-bottom: 1px solid var(--line); cursor: pointer; }
.item:last-child { border-bottom: 0; }
.item .emoji { font-size: 20px; width: 28px; text-align: center; flex: none; }
.item .title { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item .sub { font-size: 13px; color: var(--hint); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item .amount { margin-left: auto; text-align: right; flex: none; }
.dot { width: 10px; height: 10px; border-radius: 50%; flex: none; }
.check {
  width: 24px; height: 24px; border-radius: 7px; border: 2px solid var(--hint); flex: none;
  display: flex; align-items: center; justify-content: center; color: var(--accent-text); font-size: 15px;
}
.check.on { background: var(--good); border-color: var(--good); }
.day-head { font-size: 13px; color: var(--hint); padding: 12px 4px 6px; display: flex; justify-content: space-between; }

/* Итоги */
.stats { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 10px; }
.stat { background: var(--card); border-radius: var(--radius); padding: 12px 14px; }
.stat .label { font-size: 13px; color: var(--hint); }
.stat .value { font-size: 20px; font-weight: 700; margin-top: 2px; }
.stat .delta { font-size: 12px; margin-top: 2px; }

.chart-box { position: relative; height: 220px; }
.chart-box.donut { height: 200px; }
.legend { display: flex; flex-wrap: wrap; gap: 6px 14px; margin-top: 10px; font-size: 13px; }
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.legend i { width: 10px; height: 10px; border-radius: 3px; display: inline-block; }

/* Формы */
label.field { display: block; margin-bottom: 12px; }
label.field > span { display: block; font-size: 13px; color: var(--hint); margin: 0 0 4px 2px; }
input[type=text], input[type=number], input[type=date], input[type=search], select {
  width: 100%; border: 1px solid var(--line); background: var(--bg); border-radius: 10px; padding: 10px 12px; outline: none;
  -webkit-appearance: none; appearance: none;
}
input:focus, select:focus { border-color: var(--accent); }
select {
  padding-right: 30px;
  background-image: linear-gradient(45deg, transparent 50%, var(--hint) 50%), linear-gradient(135deg, var(--hint) 50%, transparent 50%);
  background-position: calc(100% - 17px) 50%, calc(100% - 12px) 50%;
  background-size: 5px 5px;
  background-repeat: no-repeat;
}
.toggle { display: flex; align-items: center; justify-content: space-between; padding: 6px 0; }
.toggle input { width: 44px; height: 26px; accent-color: var(--accent); }
.seg { display: flex; background: var(--bg2); border-radius: 10px; padding: 3px; gap: 3px; margin-bottom: 12px; }
.seg button { flex: 1; border: 0; background: none; border-radius: 8px; padding: 7px 4px; cursor: pointer; font-size: 14px; }
.seg button.active { background: var(--card); font-weight: 600; box-shadow: 0 1px 3px rgba(0,0,0,.08); }
.filters { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-top: 8px; }
.dates { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-top: 8px; }

/* Нижняя шторка для форм */
.sheet-bg { position: fixed; inset: 0; background: rgba(0,0,0,.4); z-index: 20; }
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 21; background: var(--bg);
  border-radius: 18px 18px 0 0; padding: 18px 16px calc(18px + env(safe-area-inset-bottom));
  max-height: 88vh; overflow-y: auto; max-width: 640px; margin: 0 auto;
}
.sheet h3 { margin: 0 0 14px; font-size: 18px; }
.sheet .actions { display: grid; gap: 8px; margin-top: 6px; }

.empty { text-align: center; color: var(--hint); padding: 24px 16px; }
.error { background: color-mix(in srgb, var(--danger) 12%, var(--card)); color: var(--danger); }
.toast {
  position: fixed; left: 50%; bottom: calc(84px + env(safe-area-inset-bottom)); transform: translateX(-50%);
  background: var(--text); color: var(--bg); padding: 9px 16px; border-radius: 10px; z-index: 30; font-size: 14px;
  max-width: calc(100% - 32px);
}
