/* Оформление опирается на переменные темы Telegram, чтобы приложение выглядело
   родным и в светлой, и в темной теме. Значения после запятой - запасные,
   на случай запуска в обычном браузере при локальной разработке. */

:root {
  --bg: var(--tg-theme-bg-color, #ffffff);
  --secondary-bg: var(--tg-theme-secondary-bg-color, #f1f1f4);
  --text: var(--tg-theme-text-color, #1a1a1a);
  --hint: var(--tg-theme-hint-color, #8a8a8e);
  --link: var(--tg-theme-link-color, #2481cc);
  --button: var(--tg-theme-button-color, #2481cc);
  --button-text: var(--tg-theme-button-text-color, #ffffff);
  --section-bg: var(--tg-theme-section-bg-color, var(--bg));
  --separator: var(--tg-theme-section-separator-color, rgba(128, 128, 128, 0.25));

  --income: #2e9e5b;
  --expense: #d1453b;
  --warning: #e08b2f;

  --radius: 14px;
  --gap: 12px;
  --tabbar-height: 58px;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  padding: 0 0 calc(var(--tabbar-height) + env(safe-area-inset-bottom, 0px));
  background: var(--secondary-bg);
  color: var(--text);
  font: 15px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
}

/* --- Шапка --- */

.topbar {
  position: sticky;
  top: 0;
  z-index: 5;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 16px;
  background: var(--bg);
  border-bottom: 1px solid var(--separator);
}

.topbar h1 {
  flex: 1;
  margin: 0;
  font-size: 18px;
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.iconbtn {
  width: 28px;
  height: 28px;
  padding: 0;
  border: none;
  border-radius: 8px;
  background: var(--secondary-bg);
  color: var(--link);
  font-size: 22px;
  line-height: 1;
  cursor: pointer;
}

.period {
  padding: 6px 10px;
  border: 1px solid var(--separator);
  border-radius: 8px;
  background: var(--secondary-bg);
  color: var(--text);
  font-size: 14px;
}

/* --- Каркас --- */

.content {
  padding: var(--gap);
  display: flex;
  flex-direction: column;
  gap: var(--gap);
  animation: fade 0.18s ease-out;
}

@keyframes fade {
  from { opacity: 0; transform: translateY(4px); }
  to { opacity: 1; transform: none; }
}

@media (prefers-reduced-motion: reduce) {
  .content { animation: none; }
}

.card {
  padding: 14px 16px;
  border-radius: var(--radius);
  background: var(--section-bg);
}

.card h2 {
  margin: 0 0 10px;
  font-size: 15px;
  font-weight: 600;
}

.hint {
  margin: 0;
  color: var(--hint);
  font-size: 14px;
}

.error {
  margin: 0;
  padding: 14px 16px;
  border-radius: var(--radius);
  background: var(--section-bg);
  color: var(--expense);
}

/* Скелетоны загрузки вместо «Загружаю данные» */
.skeleton {
  height: 74px;
  border-radius: var(--radius);
  background: linear-gradient(90deg, var(--section-bg) 25%, var(--secondary-bg) 50%, var(--section-bg) 75%);
  background-size: 200% 100%;
  animation: shimmer 1.2s infinite linear;
}

.skeleton.tall { height: 150px; }

@keyframes shimmer {
  from { background-position: 200% 0; }
  to { background-position: -200% 0; }
}

@media (prefers-reduced-motion: reduce) {
  .skeleton { animation: none; }
}

/* --- Строки и числа --- */

.row {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: var(--gap);
  padding: 7px 0;
  border-bottom: 1px solid var(--separator);
}

.row:last-child { border-bottom: none; }

.row .label {
  color: var(--hint);
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* Подпись под остатком: на какую дату он верен */
.stale-note {
  font-size: 12px;
  opacity: 0.75;
}

.row .value {
  font-variant-numeric: tabular-nums;
  font-weight: 600;
  white-space: nowrap;
}

.income { color: var(--income); }
.expense { color: var(--expense); }
.warning { color: var(--warning); }

.totals {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: var(--gap);
}

.total {
  padding: 12px;
  border-radius: 10px;
  background: var(--secondary-bg);
}

.total .caption {
  color: var(--hint);
  font-size: 13px;
}

.total .amount {
  margin-top: 2px;
  font-size: 19px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

/* --- Списки с долями --- */

.bar-item {
  padding: 9px 0;
  border-bottom: 1px solid var(--separator);
  cursor: pointer;
}

.bar-item:last-child { border-bottom: none; }
.bar-item:active { opacity: 0.6; }

.bar-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: var(--gap);
}

.bar-head .share {
  color: var(--hint);
  font-size: 13px;
}

.bar-track {
  height: 6px;
  margin-top: 6px;
  border-radius: 3px;
  background: var(--secondary-bg);
  overflow: hidden;
}

.bar-fill {
  height: 100%;
  border-radius: 3px;
  background: var(--button);
  transition: width 0.3s ease-out;
}

.bar-fill.over { background: var(--expense); }
.bar-fill.near { background: var(--warning); }

/* --- Операции --- */

.tx {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: var(--gap);
  padding: 10px 0;
  border-bottom: 1px solid var(--separator);
  cursor: pointer;
}

.tx:last-child { border-bottom: none; }
.tx:active { opacity: 0.6; }

.tx-main { min-width: 0; }

.tx-desc {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.tx-meta {
  margin-top: 2px;
  color: var(--hint);
  font-size: 13px;
}

.tx-amount {
  font-variant-numeric: tabular-nums;
  font-weight: 600;
  white-space: nowrap;
}

/* --- Поля и кнопки --- */

.filters {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.filters input,
.filters select {
  flex: 1;
  min-width: 120px;
  padding: 9px 12px;
  border: 1px solid var(--separator);
  border-radius: 10px;
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
}

.chips {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 10px;
}

.chip {
  padding: 8px 12px;
  border: none;
  border-radius: 999px;
  background: var(--secondary-bg);
  color: var(--text);
  font-size: 14px;
  cursor: pointer;
}

.chip:active { opacity: 0.6; }

.chip.primary {
  background: var(--button);
  color: var(--button-text);
}

.chip.wide {
  width: 100%;
  text-align: center;
}

.classify-card .tx-desc {
  white-space: normal;
  font-weight: 600;
}

.badge {
  display: inline-block;
  min-width: 17px;
  margin-left: 3px;
  padding: 0 5px;
  border-radius: 9px;
  background: var(--expense);
  color: #fff;
  font-size: 11px;
  line-height: 17px;
  text-align: center;
}

.hidden { display: none; }

/* --- Нижняя навигация --- */

.tabbar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 5;
  display: flex;
  height: calc(var(--tabbar-height) + env(safe-area-inset-bottom, 0px));
  padding-bottom: env(safe-area-inset-bottom, 0px);
  background: var(--bg);
  border-top: 1px solid var(--separator);
}

.tab {
  flex: 1;
  min-width: 0;
  border: none;
  background: none;
  color: var(--hint);
  font-size: 11px;
  cursor: pointer;
}

.tab.is-active {
  color: var(--link);
  font-weight: 600;
}

/* --- Графики --- */

.chart {
  display: block;
  width: 100%;
  height: auto;
}

.chart .bar { fill: var(--button); }
.chart .axis { fill: var(--hint); font-size: 9px; }

.donut-wrap {
  display: flex;
  align-items: center;
  gap: 16px;
}

.donut { flex: 0 0 128px; width: 128px; height: 128px; }

.donut-center {
  fill: var(--text);
  font-size: 13px;
  font-weight: 700;
}

.legend {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.legend-item {
  display: flex;
  align-items: center;
  gap: 7px;
  font-size: 13px;
  min-width: 0;
}

.legend-dot {
  flex: 0 0 9px;
  width: 9px;
  height: 9px;
  border-radius: 50%;
}

.legend-name {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.legend-share {
  color: var(--hint);
  font-variant-numeric: tabular-nums;
}

/* --- Дельты сравнения --- */

.delta {
  font-size: 13px;
  font-variant-numeric: tabular-nums;
}
