/* =========================================================
   Копилка — Telegram Mini App. Кастомные стили поверх Tailwind.
   Адаптация к темам Telegram через CSS-переменные.
   ========================================================= */

/* --- Система переменных (значения по умолчанию — светлая тема) --- */
:root {
  --tg-bg: #f4f4f6;
  --tg-surface: #ffffff;
  --tg-text: #17181c;
  --tg-hint: #8a8f98;
  --tg-button: #2f6bff;
  --tg-buttontext: #ffffff;
  --tg-accent: #2f6bff;
  --tg-destructive: #e5484d;

  /* Семантические цвета денег */
  --c-income: #0f9d63;
  --c-expense: #e5484d;

  /* Производные тона интерфейса.
     Первая строка — фолбэк для старых WebView без поддержки color-mix. */
  --hairline: rgba(0, 0, 0, 0.1);
  --hairline: color-mix(in srgb, var(--tg-text) 10%, transparent);
  --hairline-strong: rgba(0, 0, 0, 0.18);
  --hairline-strong: color-mix(in srgb, var(--tg-text) 18%, transparent);
  --surface-2: rgba(0, 0, 0, 0.045);
  --surface-2: color-mix(in srgb, var(--tg-text) 4%, var(--tg-surface));
  --shadow-card: 0 10px 30px -18px rgba(16, 20, 40, 0.35);
  --radius: 20px;
  --radius-sm: 14px;
  --tabbar-h: 64px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
}

/* Тёмная тема Telegram */
.theme-dark {
  --c-income: #34d399;
  --c-expense: #f87171;
  --shadow-card: 0 14px 34px -20px rgba(0, 0, 0, 0.9);
}

/* --- База --- */
* {
  -webkit-tap-highlight-color: transparent;
}

html,
body {
  margin: 0;
  min-height: 100%;
}

body {
  background: var(--tg-bg);
  color: var(--tg-text);
  font-family: "Manrope", system-ui, -apple-system, sans-serif;
  font-size: 15px;
  line-height: 1.45;
  overflow-x: hidden;
  transition: background-color 0.25s ease, color 0.25s ease;
}

/* Узкие числа — ровные по разрядам */
.tnum {
  font-variant-numeric: tabular-nums;
  font-feature-settings: "tnum" 1;
}

.app {
  max-width: 560px;
  margin: 0 auto;
  min-height: 100vh;
  position: relative;
}

/* --- Верхняя панель --- */
.app-header {
  position: sticky;
  top: 0;
  z-index: 30;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: calc(var(--safe-t) + 12px) 18px 12px;
  background: color-mix(in srgb, var(--tg-bg) 84%, transparent);
  backdrop-filter: saturate(1.4) blur(14px);
  -webkit-backdrop-filter: saturate(1.4) blur(14px);
}

.app-header__brand {
  display: flex;
  align-items: center;
  gap: 9px;
}

.app-header__mark {
  width: 16px;
  height: 16px;
  border-radius: 6px;
  background: linear-gradient(135deg, var(--tg-button), #7db0ff);
  box-shadow: 0 4px 12px -4px var(--tg-button);
}

.app-header__title {
  font-family: "Sora", sans-serif;
  font-weight: 600;
  letter-spacing: -0.01em;
  font-size: 16px;
}

.app-header__user {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  font-weight: 700;
  font-size: 13px;
  color: var(--tg-buttontext);
  background: linear-gradient(135deg, var(--tg-button), #7db0ff);
  flex: none;
}

/* --- Лента экранов --- */
.screen-root {
  padding: 6px 18px calc(var(--tabbar-h) + var(--safe-b) + 28px);
}

.screen[hidden] {
  display: none;
}

.screen {
  animation: rise 0.34s cubic-bezier(0.2, 0.7, 0.3, 1) both;
}

.screen-title {
  font-family: "Sora", sans-serif;
  font-size: 22px;
  font-weight: 600;
  letter-spacing: -0.02em;
  margin: 4px 0 16px;
}

/* --- Приветствие --- */
.greeting {
  padding: 10px 2px 16px;
}

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

.greeting__name {
  margin: 2px 0 0;
  font-family: "Sora", sans-serif;
  font-size: 24px;
  font-weight: 600;
  letter-spacing: -0.02em;
}

/* --- Переключатель периода --- */
.period-switch {
  display: inline-flex;
  gap: 4px;
  padding: 4px;
  border-radius: 999px;
  background: var(--surface-2);
  border: 1px solid var(--hairline);
  margin-bottom: 14px;
}

.period-switch__btn {
  border: 0;
  background: transparent;
  color: var(--tg-hint);
  font: inherit;
  font-weight: 600;
  font-size: 13px;
  padding: 8px 16px;
  min-height: 36px;
  border-radius: 999px;
  cursor: pointer;
  transition: color 0.18s ease, background-color 0.18s ease;
}

.period-switch__btn.is-active {
  background: var(--tg-surface);
  color: var(--tg-text);
  box-shadow: 0 2px 10px -6px rgba(0, 0, 0, 0.5);
}

/* --- Карточка баланса (сигнатурный блок) --- */
.balance-card {
  position: relative;
  overflow: hidden;
  border-radius: var(--radius);
  padding: 22px 20px 18px;
  color: #fff;
  background: linear-gradient(150deg, #2f6bff 0%, #5b3df5 100%);
  box-shadow: 0 22px 44px -26px rgba(64, 74, 255, 0.9);
  isolation: isolate;
}

/* Мягкая «сетчатая» подсветка и зернистость — глубина без шума */
.balance-card::before {
  content: "";
  position: absolute;
  inset: -40% -20% auto auto;
  width: 260px;
  height: 260px;
  border-radius: 50%;
  background: radial-gradient(circle, rgba(255, 255, 255, 0.34), transparent 62%);
  z-index: -1;
}

.balance-card::after {
  content: "";
  position: absolute;
  inset: 0;
  z-index: -1;
  opacity: 0.42;
  background-image: radial-gradient(
    rgba(255, 255, 255, 0.5) 0.5px,
    transparent 0.6px
  );
  background-size: 5px 5px;
  mix-blend-mode: overlay;
}

.balance-card__intro {
  display: flex;
  flex-direction: column;
  gap: 4px;
  margin-bottom: 18px;
}

.balance-card__label {
  font-size: 12.5px;
  letter-spacing: 0.02em;
  text-transform: uppercase;
  color: rgba(255, 255, 255, 0.78);
}

.balance-card__value {
  font-family: "Sora", sans-serif;
  font-variant-numeric: tabular-nums;
  font-size: 34px;
  font-weight: 700;
  letter-spacing: -0.03em;
  line-height: 1.1;
}

.balance-rows {
  display: grid;
  gap: 10px;
  margin: 0;
  padding-top: 16px;
  border-top: 1px solid rgba(255, 255, 255, 0.22);
}

.balance-rows__row {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
}

.balance-rows__key {
  display: flex;
  align-items: center;
  gap: 8px;
  color: rgba(255, 255, 255, 0.82);
  font-size: 13.5px;
}

.balance-rows__dot {
  width: 8px;
  height: 8px;
  border-radius: 3px;
  background: rgba(255, 255, 255, 0.65);
}

.balance-rows__val {
  margin: 0;
  font-family: "Sora", sans-serif;
  font-variant-numeric: tabular-nums;
  font-weight: 600;
  font-size: 15px;
}

/* --- Быстрые действия --- */
.quick-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  margin-top: 16px;
}

.quick-actions__btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 52px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--hairline);
  background: var(--tg-surface);
  color: var(--tg-text);
  font: inherit;
  font-weight: 600;
  font-size: 15px;
  cursor: pointer;
  box-shadow: var(--shadow-card);
  transition: transform 0.12s ease, border-color 0.18s ease;
}

.quick-actions__btn:active {
  transform: scale(0.97);
}

.quick-actions__btn--income {
  border-color: color-mix(in srgb, var(--c-income) 40%, transparent);
}
.quick-actions__btn--expense {
  border-color: color-mix(in srgb, var(--c-expense) 40%, transparent);
}

/* --- Фильтры --- */
.filters {
  display: flex;
  flex-direction: column;
  gap: 12px;
  margin-bottom: 16px;
}

.segmented {
  display: flex;
  gap: 4px;
  padding: 4px;
  border-radius: 14px;
  background: var(--surface-2);
  border: 1px solid var(--hairline);
}

.segmented__btn {
  flex: 1;
  border: 0;
  background: transparent;
  color: var(--tg-hint);
  font: inherit;
  font-weight: 600;
  font-size: 14px;
  min-height: 40px;
  padding: 8px 10px;
  border-radius: 10px;
  cursor: pointer;
  transition: color 0.18s ease, background-color 0.18s ease;
}

.segmented--lg .segmented__btn {
  min-height: 46px;
  font-size: 15px;
}

.segmented__btn.is-active {
  background: var(--tg-surface);
  color: var(--tg-text);
  box-shadow: 0 2px 10px -6px rgba(0, 0, 0, 0.5);
}

.chips {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  scrollbar-width: none;
  padding-bottom: 2px;
}

.chips::-webkit-scrollbar {
  display: none;
}

.chip {
  flex: none;
  border: 1px solid var(--hairline);
  background: var(--tg-surface);
  color: var(--tg-hint);
  font: inherit;
  font-weight: 600;
  font-size: 13px;
  min-height: 38px;
  padding: 8px 16px;
  border-radius: 999px;
  cursor: pointer;
  transition: color 0.18s ease, background-color 0.18s ease,
    border-color 0.18s ease;
}

.chip.is-active {
  background: var(--tg-button);
  border-color: var(--tg-button);
  color: var(--tg-buttontext);
}

/* --- История --- */
.history-list {
  display: grid;
  gap: 18px;
}

.history-group__head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin: 0 2px 8px;
}

.history-group__date {
  font-weight: 700;
  font-size: 14px;
}

.history-group__total {
  font-family: "Sora", sans-serif;
  font-variant-numeric: tabular-nums;
  font-size: 13px;
  color: var(--tg-hint);
}

.history-group__items {
  display: grid;
  background: var(--tg-surface);
  border: 1px solid var(--hairline);
  border-radius: var(--radius-sm);
  overflow: hidden;
  box-shadow: var(--shadow-card);
}

.tx {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 13px 14px;
  min-height: 60px;
}

.tx + .tx {
  border-top: 1px solid var(--hairline);
}

.tx__icon {
  width: 40px;
  height: 40px;
  flex: none;
  border-radius: 12px;
  display: grid;
  place-items: center;
  font-size: 17px;
  background: var(--surface-2);
}

.tx--income .tx__icon {
  background: color-mix(in srgb, var(--c-income) 16%, transparent);
}

.tx--expense .tx__icon {
  background: color-mix(in srgb, var(--c-expense) 15%, transparent);
}

.tx__body {
  min-width: 0;
  flex: 1;
}

.tx__title {
  font-weight: 600;
  font-size: 15px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.tx__comment {
  color: var(--tg-hint);
  font-size: 12.5px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-top: 1px;
}

.tx__amount {
  font-family: "Sora", sans-serif;
  font-variant-numeric: tabular-nums;
  font-weight: 600;
  font-size: 15px;
  flex: none;
  white-space: nowrap;
}

.tx--income .tx__amount {
  color: var(--c-income);
}

.tx--expense .tx__amount {
  color: var(--c-expense);
}

/* --- Копилки --- */
.piggies-list {
  display: grid;
  gap: 14px;
}

.piggy {
  background: var(--tg-surface);
  border: 1px solid var(--hairline);
  border-radius: var(--radius);
  padding: 18px 18px 16px;
  box-shadow: var(--shadow-card);
}

.piggy__head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 14px;
}

.piggy__name {
  font-family: "Sora", sans-serif;
  font-weight: 600;
  font-size: 17px;
  letter-spacing: -0.01em;
  margin: 0;
}

.piggy__interest {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  margin-top: 5px;
  font-size: 12px;
  font-weight: 600;
  color: var(--c-income);
  background: color-mix(in srgb, var(--c-income) 14%, transparent);
  padding: 3px 9px;
  border-radius: 999px;
}

.piggy__percent {
  font-family: "Sora", sans-serif;
  font-variant-numeric: tabular-nums;
  font-weight: 700;
  font-size: 18px;
  flex: none;
}

.progress-track {
  height: 10px;
  border-radius: 999px;
  background: var(--surface-2);
  overflow: hidden;
}

.progress-fill {
  height: 100%;
  border-radius: inherit;
  background: linear-gradient(90deg, var(--tg-button), #7db0ff);
  width: 0;
  transition: width 0.7s cubic-bezier(0.2, 0.7, 0.3, 1);
}

.piggy__amounts {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 10px;
  margin-top: 12px;
}

.piggy__current {
  font-family: "Sora", sans-serif;
  font-variant-numeric: tabular-nums;
  font-weight: 600;
  font-size: 16px;
}

.piggy__target {
  color: var(--tg-hint);
  font-size: 13px;
  font-variant-numeric: tabular-nums;
}

.piggy__action {
  margin-top: 16px;
}

/* --- Форма добавления --- */
.add-form {
  display: grid;
  gap: 16px;
}

.field {
  display: grid;
  gap: 7px;
}

.field__label {
  font-size: 13px;
  font-weight: 600;
  color: var(--tg-hint);
  padding-left: 2px;
}

.field__input {
  width: 100%;
  box-sizing: border-box;
  min-height: 50px;
  padding: 13px 15px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--hairline-strong);
  background: var(--tg-surface);
  color: var(--tg-text);
  font: inherit;
  font-size: 16px; /* ≥16px — не зумит iOS при фокусе */
  outline: none;
  transition: border-color 0.18s ease, box-shadow 0.18s ease;
  appearance: none;
}

.field__input:focus {
  border-color: var(--tg-button);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--tg-button) 22%, transparent);
}

.field__input--area {
  resize: vertical;
  min-height: 56px;
  line-height: 1.4;
}

.field__money {
  position: relative;
  display: flex;
  align-items: center;
}

.field__currency {
  position: absolute;
  left: 16px;
  color: var(--tg-hint);
  font-family: "Sora", sans-serif;
  font-weight: 600;
  pointer-events: none;
}

.field__input--money {
  padding-left: 40px;
  font-family: "Sora", sans-serif;
  font-variant-numeric: tabular-nums;
  font-weight: 600;
  font-size: 22px;
}

.field__error {
  margin: 0;
  color: var(--tg-destructive);
  font-size: 13px;
  font-weight: 600;
}

/* --- Кнопки --- */
.btn-primary {
  width: 100%;
  min-height: 52px;
  border: 0;
  border-radius: var(--radius-sm);
  background: var(--tg-button);
  color: var(--tg-buttontext);
  font: inherit;
  font-weight: 700;
  font-size: 16px;
  cursor: pointer;
  transition: transform 0.12s ease, opacity 0.18s ease;
}

.btn-primary:active {
  transform: scale(0.985);
}

.btn-primary:disabled {
  opacity: 0.5;
  cursor: default;
}

.btn-ghost {
  min-height: 44px;
  padding: 10px 18px;
  border-radius: 12px;
  border: 1px solid var(--hairline-strong);
  background: transparent;
  color: var(--tg-text);
  font: inherit;
  font-weight: 600;
  font-size: 14px;
  cursor: pointer;
}

.btn-small {
  min-height: 44px;
  padding: 10px 18px;
  border-radius: 12px;
  border: 0;
  background: var(--tg-button);
  color: var(--tg-buttontext);
  font: inherit;
  font-weight: 600;
  font-size: 14px;
  cursor: pointer;
}

/* --- Распределения после сохранения --- */
.allocations {
  margin-top: 20px;
  border-radius: var(--radius);
  border: 1px solid color-mix(in srgb, var(--c-income) 35%, transparent);
  background: color-mix(in srgb, var(--c-income) 8%, var(--tg-surface));
  padding: 16px 18px;
}

.allocations__title {
  font-family: "Sora", sans-serif;
  font-weight: 600;
  font-size: 14px;
  margin: 0 0 10px;
  color: var(--c-income);
}

.allocations__row {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  font-size: 14px;
  padding: 5px 0;
}

.allocations__row span:last-child {
  font-family: "Sora", sans-serif;
  font-variant-numeric: tabular-nums;
  font-weight: 600;
}

/* --- Нижняя навигация --- */
.tabbar {
  position: fixed;
  left: 50%;
  transform: translateX(-50%);
  bottom: 0;
  width: 100%;
  max-width: 560px;
  z-index: 40;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  padding: 6px 10px calc(var(--safe-b) + 6px);
  background: color-mix(in srgb, var(--tg-bg) 82%, transparent);
  backdrop-filter: saturate(1.4) blur(16px);
  -webkit-backdrop-filter: saturate(1.4) blur(16px);
  border-top: 1px solid var(--hairline);
}

.tabbar__btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  min-height: 52px;
  border: 0;
  background: transparent;
  color: var(--tg-hint);
  font: inherit;
  font-size: 11px;
  font-weight: 600;
  cursor: pointer;
  border-radius: 14px;
  transition: color 0.18s ease, background-color 0.18s ease;
}

.tabbar__icon {
  width: 24px;
  height: 24px;
}

.tabbar__btn.is-active {
  color: var(--tg-button);
}

.tabbar__btn--accent .tabbar__icon {
  width: 34px;
  height: 34px;
  padding: 6px;
  border-radius: 12px;
  background: var(--tg-button);
  color: var(--tg-buttontext);
  box-shadow: 0 8px 18px -8px var(--tg-button);
}

/* --- Модальное окно --- */
.modal-overlay {
  position: fixed;
  inset: 0;
  z-index: 60;
  background: rgba(8, 10, 16, 0.5);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  animation: fade 0.2s ease both;
}

.modal-sheet {
  width: 100%;
  max-width: 560px;
  background: var(--tg-surface);
  border-radius: 24px 24px 0 0;
  padding: 8px 20px calc(var(--safe-b) + 22px);
  animation: slide-up 0.28s cubic-bezier(0.2, 0.7, 0.3, 1) both;
  max-height: 88vh;
  overflow-y: auto;
}

.modal-sheet__grip {
  width: 40px;
  height: 4px;
  border-radius: 999px;
  background: var(--hairline-strong);
  margin: 6px auto 14px;
}

.modal-sheet__title {
  font-family: "Sora", sans-serif;
  font-weight: 600;
  font-size: 19px;
  margin: 0 0 4px;
}

.modal-sheet__sub {
  color: var(--tg-hint);
  font-size: 13px;
  margin: 0 0 18px;
}

.modal-sheet__row {
  display: flex;
  gap: 10px;
  margin-top: 16px;
}

.modal-sheet__row > * {
  flex: 1;
}

/* --- Тосты --- */
.toast-root {
  position: fixed;
  top: calc(var(--safe-t) + 10px);
  left: 50%;
  transform: translateX(-50%);
  z-index: 80;
  width: calc(100% - 32px);
  max-width: 500px;
  display: grid;
  gap: 8px;
  pointer-events: none;
}

.toast {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 13px 16px;
  border-radius: 14px;
  background: var(--tg-surface);
  border: 1px solid var(--hairline-strong);
  box-shadow: var(--shadow-card);
  font-size: 14px;
  font-weight: 500;
  animation: drop 0.28s cubic-bezier(0.2, 0.7, 0.3, 1) both;
  pointer-events: auto;
}

.toast__icon {
  flex: none;
  font-size: 16px;
}

.toast--success {
  border-color: color-mix(in srgb, var(--c-income) 45%, transparent);
}
.toast--error {
  border-color: color-mix(in srgb, var(--tg-destructive) 45%, transparent);
}

.toast--leaving {
  animation: fade-out 0.2s ease both;
}

/* --- Скелетоны загрузки --- */
.skeleton {
  border-radius: 12px;
  background: linear-gradient(
    100deg,
    var(--surface-2) 30%,
    color-mix(in srgb, var(--tg-text) 9%, var(--surface-2)) 50%,
    var(--surface-2) 70%
  );
  background-size: 220% 100%;
  animation: shimmer 1.3s ease-in-out infinite;
}

.skeleton--card {
  height: 180px;
  border-radius: var(--radius);
  margin-bottom: 16px;
}

.skeleton--row {
  height: 60px;
  margin-bottom: 8px;
}

.skeleton--line {
  height: 14px;
  width: 55%;
  margin: 4px 0;
}

/* --- Состояние ошибки экрана --- */
.screen-error {
  margin-top: 8px;
  padding: 16px;
  border-radius: var(--radius-sm);
  border: 1px solid color-mix(in srgb, var(--tg-destructive) 40%, transparent);
  background: color-mix(in srgb, var(--tg-destructive) 8%, var(--tg-surface));
  color: var(--tg-text);
  font-size: 14px;
}

.screen-error__text {
  margin: 0 0 12px;
}

/* --- Пустое состояние --- */
.empty {
  text-align: center;
  padding: 48px 20px;
  color: var(--tg-hint);
}

.empty__emoji {
  font-size: 34px;
  display: block;
  margin-bottom: 10px;
}

.empty__title {
  color: var(--tg-text);
  font-weight: 600;
  margin: 0 0 4px;
}

.empty__hint {
  margin: 0;
  font-size: 13.5px;
}

/* --- Анимации --- */
@keyframes rise {
  from {
    opacity: 0;
    transform: translateY(10px);
  }
  to {
    opacity: 1;
    transform: none;
  }
}

@keyframes drop {
  from {
    opacity: 0;
    transform: translateY(-12px);
  }
  to {
    opacity: 1;
    transform: none;
  }
}

@keyframes fade {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

@keyframes fade-out {
  to {
    opacity: 0;
  }
}

@keyframes slide-up {
  from {
    transform: translateY(100%);
  }
  to {
    transform: none;
  }
}

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

@media (prefers-reduced-motion: reduce) {
  * {
    animation-duration: 0.001ms !important;
    transition-duration: 0.001ms !important;
  }
}
