:root {
  --bg: var(--tg-theme-secondary-bg-color, #eef1f6);
  --card: var(--tg-theme-bg-color, #ffffff);
  --text: var(--tg-theme-text-color, #11151c);
  --hint: var(--tg-theme-hint-color, #8a93a2);
  --accent: var(--tg-theme-button-color, #5b7cfa);
  --accent-text: var(--tg-theme-button-text-color, #ffffff);
  --income: #25b86f;
  --expense: #ff5470;
  --grad-1: #6a5cff;
  --grad-2: #4d8bff;
  --radius: 20px;
  --shadow: 0 8px 24px rgba(20, 30, 60, 0.08);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body { margin: 0; padding: 0; }

body {
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro", "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  padding-bottom: 88px;
  -webkit-font-smoothing: antialiased;
}

#app { max-width: 640px; margin: 0 auto; padding: 0 14px 24px; }

/* ---------- Шапка ---------- */
.hero {
  margin: 12px 0 14px;
  padding: 18px 20px 20px;
  border-radius: 24px;
  background: linear-gradient(135deg, var(--grad-1), var(--grad-2));
  color: #fff;
  box-shadow: 0 12px 30px rgba(90, 90, 230, 0.35);
}
.hero-head { display: flex; justify-content: space-between; align-items: center; }
.hero-title { font-size: 16px; font-weight: 600; }
.hero-chip {
  font-size: 13px;
  background: rgba(255, 255, 255, 0.22);
  padding: 4px 12px;
  border-radius: 20px;
  backdrop-filter: blur(4px);
}
.hero-balance-label { font-size: 13px; opacity: 0.85; margin-top: 16px; }
.hero-balance { font-size: 36px; font-weight: 800; letter-spacing: -0.5px; margin-top: 2px; }

.hero-grid { display: flex; gap: 10px; margin-top: 18px; }
.hero-stat {
  flex: 1;
  display: flex;
  align-items: center;
  gap: 10px;
  background: rgba(255, 255, 255, 0.16);
  border-radius: 14px;
  padding: 10px 12px;
}
.hero-stat-ic {
  width: 30px; height: 30px;
  flex: none;
  border-radius: 10px;
  display: grid; place-items: center;
  font-size: 16px; font-weight: 700;
  background: rgba(255, 255, 255, 0.25);
}
.hero-stat.income .hero-stat-ic { color: #d7ffe9; }
.hero-stat.expense .hero-stat-ic { color: #ffdfe6; }
.hero-stat-label { font-size: 11px; opacity: 0.85; }
.hero-stat-val { font-size: 15px; font-weight: 700; margin-top: 1px; }

/* ---------- Период ---------- */
.periods {
  display: flex;
  gap: 6px;
  background: var(--card);
  padding: 5px;
  border-radius: 16px;
  margin-bottom: 14px;
  box-shadow: var(--shadow);
}
.periods button {
  flex: 1;
  border: none;
  background: transparent;
  color: var(--hint);
  padding: 9px 0;
  border-radius: 11px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.15s;
}
.periods button.active {
  background: linear-gradient(135deg, var(--grad-1), var(--grad-2));
  color: #fff;
  box-shadow: 0 4px 12px rgba(90, 90, 230, 0.3);
}

/* ---------- Карточки ---------- */
.card {
  background: var(--card);
  border-radius: var(--radius);
  padding: 18px;
  margin-bottom: 14px;
  box-shadow: var(--shadow);
}
.card-title { font-size: 15px; font-weight: 700; margin-bottom: 8px; }

.chart-wrap {
  position: relative;
  min-height: 230px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.state-empty { text-align: center; color: var(--hint); font-size: 14px; }
.state-emoji { font-size: 40px; margin-bottom: 8px; }

/* ---------- Список категорий ---------- */
.cat-list { list-style: none; margin: 0; padding: 0; }
.cat-list li {
  background: var(--card);
  border-radius: 16px;
  padding: 12px 14px;
  margin-bottom: 8px;
  display: flex;
  align-items: center;
  gap: 12px;
  box-shadow: var(--shadow);
}
.cat-dot { width: 10px; height: 10px; border-radius: 50%; flex: none; }
.cat-list .emoji { font-size: 20px; }
.cat-list .name { flex: 1; font-size: 15px; font-weight: 500; }
.cat-list .amount { font-weight: 700; font-size: 15px; }
.cat-list .pct {
  color: var(--hint);
  font-size: 12px;
  display: block;
  text-align: right;
  margin-top: 2px;
}

/* ---------- Добавить ---------- */
.seg {
  display: flex;
  background: var(--bg);
  border-radius: 14px;
  padding: 4px;
  margin-bottom: 18px;
}
.seg button {
  flex: 1;
  border: none;
  background: transparent;
  color: var(--hint);
  padding: 11px 0;
  border-radius: 11px;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.15s;
}
.seg button.active { background: var(--card); color: var(--text); box-shadow: 0 2px 8px rgba(0,0,0,.1); }
.seg button[data-type="expense"].active { color: var(--expense); }
.seg button[data-type="income"].active { color: var(--income); }

.amount-box {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  background: var(--bg);
  border-radius: 16px;
  padding: 18px;
  margin-bottom: 6px;
}
.amount-box input {
  border: none;
  background: transparent;
  color: var(--text);
  font-size: 40px;
  font-weight: 800;
  text-align: right;
  width: 60%;
  outline: none;
  padding: 0;
}
.amount-cur { font-size: 30px; font-weight: 800; color: var(--hint); }

.muted-label { font-size: 13px; color: var(--hint); margin: 16px 2px 8px; font-weight: 600; }

.cat-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.cat-grid button {
  border: 2px solid transparent;
  background: var(--bg);
  color: var(--text);
  border-radius: 14px;
  padding: 10px 2px;
  font-size: 11px;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  transition: all 0.15s;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  width: 100%;
}
.cat-grid button .e { font-size: 22px; }
.cat-grid button.active {
  border-color: var(--accent);
  background: color-mix(in srgb, var(--accent) 12%, var(--card));
}

.field {
  width: 100%;
  border: none;
  background: var(--bg);
  color: var(--text);
  border-radius: 14px;
  padding: 14px;
  font-size: 16px;
  outline: none;
}

.primary-btn {
  width: 100%;
  margin-top: 20px;
  border: none;
  background: linear-gradient(135deg, var(--grad-1), var(--grad-2));
  color: #fff;
  padding: 16px;
  border-radius: 16px;
  font-size: 16px;
  font-weight: 700;
  cursor: pointer;
  box-shadow: 0 8px 20px rgba(90, 90, 230, 0.3);
  transition: transform 0.1s;
}
.primary-btn:active { transform: scale(0.98); }
.primary-btn:disabled { opacity: 0.5; box-shadow: none; }
.hint { text-align: center; color: var(--hint); font-size: 13px; margin-top: 12px; min-height: 18px; }

/* ---------- История ---------- */
.tx-list { list-style: none; margin: 0; padding: 0; }
.tx-list li {
  background: var(--card);
  border-radius: 16px;
  padding: 12px 14px;
  margin-bottom: 8px;
  display: flex;
  align-items: center;
  gap: 12px;
  box-shadow: var(--shadow);
}
.tx-ava {
  width: 42px; height: 42px;
  flex: none;
  border-radius: 13px;
  display: grid; place-items: center;
  font-size: 21px;
  background: var(--bg);
}
.tx-meta { flex: 1; min-width: 0; }
.tx-meta .title { font-size: 15px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tx-meta .sub { font-size: 12px; color: var(--hint); margin-top: 2px; }
.tx-amt { font-weight: 700; font-size: 15px; white-space: nowrap; }
.tx-amt.income { color: var(--income); }
.tx-amt.expense { color: var(--expense); }

.state-block { text-align: center; color: var(--hint); padding: 50px 20px; }
.state-block .state-emoji { font-size: 44px; margin-bottom: 10px; }
.state-block button {
  margin-top: 14px; border: none; background: var(--accent); color: var(--accent-text);
  padding: 10px 20px; border-radius: 12px; font-weight: 600; cursor: pointer;
}

/* ---------- Нижняя навигация ---------- */
.tabbar {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  display: flex;
  justify-content: space-around;
  background: var(--card);
  border-top: 1px solid color-mix(in srgb, var(--hint) 22%, transparent);
  padding: 8px 0 calc(8px + env(safe-area-inset-bottom, 0));
  z-index: 50;
}
.tabbar button {
  border: none;
  background: transparent;
  color: var(--hint);
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
  font-size: 10px;
  font-weight: 600;
  flex: 1;
  padding: 0;
}
.tabbar button svg { width: 23px; height: 23px; fill: currentColor; }
.tabbar button.active { color: var(--accent); }
.tabbar .plus {
  width: 30px; height: 30px;
  border-radius: 10px;
  background: linear-gradient(135deg, var(--grad-1), var(--grad-2));
  color: #fff;
  display: grid; place-items: center;
  font-size: 22px; line-height: 1;
  box-shadow: 0 4px 12px rgba(90, 90, 230, 0.35);
}
.tabbar .tab-add.active { color: var(--accent); }

/* ---------- Управление категориями ---------- */
.row-between { display: flex; align-items: center; justify-content: space-between; }
.link-btn {
  border: none; background: transparent; color: var(--accent);
  font-size: 13px; font-weight: 600; cursor: pointer; padding: 4px;
}
.cat-grid button { position: relative; }
.cat-del {
  position: absolute; top: -5px; right: -5px;
  width: 20px; height: 20px; border-radius: 50%;
  background: var(--expense); color: #fff;
  display: grid; place-items: center; font-size: 13px; line-height: 1;
  border: 2px solid var(--card);
}
.cat-grid button.add-tile { border: 2px dashed color-mix(in srgb, var(--hint) 50%, transparent); color: var(--hint); }

.create-box { display: flex; gap: 8px; align-items: center; margin-top: 10px; }
.emoji-input {
  width: 52px; flex: none; text-align: center;
  border: none; background: var(--bg); color: var(--text);
  border-radius: 14px; padding: 14px 0; font-size: 22px; outline: none;
}
.mini-btn {
  border: none; background: var(--accent); color: var(--accent-text);
  border-radius: 14px; padding: 12px 18px; font-size: 15px; font-weight: 700; cursor: pointer;
  flex: none;
}
.mini-btn.ghost { background: var(--bg); color: var(--text); }
.row-buttons { display: flex; gap: 8px; margin-top: 12px; }
.row-buttons .mini-btn { flex: 1; }
.form-note { font-size: 12px; color: var(--hint); margin: 8px 2px 0; }

.primary-btn.ghost {
  background: var(--card); color: var(--accent);
  box-shadow: var(--shadow); margin-top: 0; margin-bottom: 14px;
  border: 2px dashed color-mix(in srgb, var(--accent) 40%, transparent);
}

/* ---------- Цели ---------- */
.goal-list { list-style: none; margin: 0 0 14px; padding: 0; }
.goal-card {
  background: var(--card);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 12px;
  box-shadow: var(--shadow);
}
.goal-top { display: flex; align-items: center; gap: 12px; }
.goal-ava {
  width: 44px; height: 44px; flex: none; border-radius: 13px;
  display: grid; place-items: center; font-size: 22px; background: var(--bg);
}
.goal-name { flex: 1; font-size: 16px; font-weight: 700; }
.goal-del {
  border: none; background: transparent; color: var(--hint);
  font-size: 18px; cursor: pointer; padding: 4px 6px;
}
.goal-amounts { display: flex; justify-content: space-between; font-size: 13px; margin: 14px 0 6px; }
.goal-amounts .saved { font-weight: 700; color: var(--income); }
.goal-amounts .target { color: var(--hint); }
.goal-bar { height: 10px; border-radius: 6px; background: var(--bg); overflow: hidden; }
.goal-bar > div {
  height: 100%; border-radius: 6px;
  background: linear-gradient(90deg, var(--grad-1), var(--grad-2));
  transition: width 0.4s;
}
.goal-bar.done > div { background: linear-gradient(90deg, #25b86f, #1fd18a); }
.goal-pct { font-size: 12px; color: var(--hint); margin-top: 6px; }
.goal-deposit { display: flex; gap: 8px; margin-top: 12px; }
.goal-deposit input {
  flex: 1; border: none; background: var(--bg); color: var(--text);
  border-radius: 12px; padding: 11px 14px; font-size: 15px; outline: none;
}
.goal-done-badge {
  display: inline-block; margin-top: 10px; font-size: 13px; font-weight: 700;
  color: var(--income);
}

/* ---------- Подписки ---------- */
.sub-total {
  background: linear-gradient(135deg, var(--grad-1), var(--grad-2));
  color: #fff;
  border-radius: var(--radius);
  padding: 16px 18px;
  font-weight: 700;
  font-size: 16px;
  margin-bottom: 14px;
  box-shadow: 0 8px 20px rgba(90, 90, 230, 0.3);
}
.sub-total small { display: block; font-weight: 500; font-size: 12px; opacity: 0.85; margin-bottom: 2px; }

.sub-list { list-style: none; margin: 0 0 14px; padding: 0; }
.sub-card {
  background: var(--card);
  border-radius: var(--radius);
  padding: 14px 16px;
  margin-bottom: 12px;
  box-shadow: var(--shadow);
  border-left: 4px solid transparent;
}
.sub-card.status-soon { border-left-color: #ff9f43; }
.sub-card.status-overdue { border-left-color: var(--expense); }
.sub-card.status-ok { border-left-color: var(--income); }

.sub-head { display: flex; align-items: center; gap: 12px; }
.sub-ava {
  width: 44px; height: 44px; flex: none; border-radius: 13px;
  display: grid; place-items: center; font-size: 22px; background: var(--bg);
}
.sub-main { flex: 1; min-width: 0; }
.sub-name { font-size: 16px; font-weight: 700; }
.sub-meta { font-size: 13px; color: var(--hint); margin-top: 2px; }
.sub-del { border: none; background: transparent; color: var(--hint); font-size: 17px; cursor: pointer; padding: 4px 6px; }

.sub-dates { display: flex; justify-content: space-between; gap: 8px; font-size: 12px; color: var(--hint); margin: 12px 0; }
.sub-next { text-align: right; }
.sub-next b { color: var(--text); }
.sub-next.overdue b { color: var(--expense); }
.sub-next.soon b { color: #ff9f43; }
.pay-btn { width: 100%; }

/* ---------- Настройки (напоминания) ---------- */
.settings-card { padding: 6px 16px; }
.settings-row {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 0; font-size: 15px;
}
.settings-row + .settings-row { border-top: 1px solid color-mix(in srgb, var(--hint) 18%, transparent); }
.mini-select {
  border: none; background: var(--bg); color: var(--text);
  border-radius: 10px; padding: 8px 10px; font-size: 14px; font-weight: 600;
}
.switch { position: relative; display: inline-block; width: 48px; height: 28px; }
.switch input { opacity: 0; width: 0; height: 0; }
.slider {
  position: absolute; cursor: pointer; inset: 0;
  background: color-mix(in srgb, var(--hint) 40%, transparent);
  border-radius: 28px; transition: 0.2s;
}
.slider::before {
  content: ""; position: absolute; height: 22px; width: 22px; left: 3px; bottom: 3px;
  background: #fff; border-radius: 50%; transition: 0.2s;
}
.switch input:checked + .slider { background: var(--income); }
.switch input:checked + .slider::before { transform: translateX(20px); }

/* кликабельные операции в истории */
.tx-list li { cursor: pointer; transition: transform 0.1s; }
.tx-list li:active { transform: scale(0.98); }

/* ---------- Модалка ---------- */
.modal { position: fixed; inset: 0; z-index: 100; display: flex; align-items: flex-end; justify-content: center; }
.modal[hidden] { display: none; }  /* иначе display:flex перебивает атрибут hidden */
.modal-backdrop { position: absolute; inset: 0; background: rgba(0, 0, 0, 0.5); }
.modal-card {
  position: relative; width: 100%; max-width: 640px;
  background: var(--card); color: var(--text);
  border-radius: 20px 20px 0 0; padding: 18px 16px calc(20px + env(safe-area-inset-bottom, 0));
  box-shadow: 0 -8px 30px rgba(0, 0, 0, 0.25);
  animation: slideUp 0.2s ease;
  max-height: 90vh; overflow-y: auto;
}
@keyframes slideUp { from { transform: translateY(100%); } to { transform: translateY(0); } }
.modal-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 14px; }
.modal-title { font-size: 17px; font-weight: 700; }
.modal-close { border: none; background: var(--bg); color: var(--hint); width: 32px; height: 32px; border-radius: 50%; font-size: 16px; cursor: pointer; }
.mini-btn.danger { background: color-mix(in srgb, var(--expense) 15%, var(--card)); color: var(--expense); }

@media (prefers-color-scheme: dark) {
  :root { --shadow: 0 8px 24px rgba(0, 0, 0, 0.25); }
}
