/* Цветовые токены: тёмная тема в стиле Telegram, все стили используют только их */
:root {
  --bg: #18222d;
  --surface: #212d3b;
  --surface-2: #17212b;
  --surface-3: #2b3847;
  --border: #2f3d4c;
  --text: #ffffff;
  --text-muted: #7f91a4;
  --accent: #5288c1;
  --accent-hover: #5f95ce;
  --accent-dim: #3a5573;
  --income: #4caf50;
  --danger: #e53935;
  --edit: #5aa0e6;
  /* Серии графиков: проверены на различимость (в т. ч. при дальтонизме) на фоне --surface */
  --chart-income: #43a047;
  --chart-expense: #5288c1;
  --chart-grid: #2f3d4c;
  --radius: 14px;
  --radius-sm: 10px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 14px;
  -webkit-tap-highlight-color: transparent;
}

body {
  min-height: 100vh;
  padding: 0 16px calc(16px + env(safe-area-inset-bottom));
}

button { font: inherit; color: inherit; }
[hidden] { display: none !important; }

svg {
  width: 20px;
  height: 20px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

/* Верхняя панель */
.toolbar {
  position: sticky;
  top: 0;
  z-index: 30;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 0;
  background: var(--bg);
}

.btn-pill {
  padding: 7px 14px;
  border: none;
  border-radius: 999px;
  background: var(--accent);
  color: #fff;
  font-weight: 600;
  font-size: 13px;
  cursor: pointer;
}
.btn-pill:hover { background: var(--accent-hover); }

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  padding: 0;
  border: none;
  border-radius: 8px;
  background: none;
  color: var(--text);
  cursor: pointer;
}
.icon-btn.small { width: 30px; height: 30px; color: var(--text-muted); }
.icon-btn.small svg { width: 18px; height: 18px; }
.icon-btn.active { color: var(--accent); }

#menu-btn .icon-close { display: none; }
#menu-btn.open .icon-open { display: none; }
#menu-btn.open .icon-close { display: block; }

/* Выпадающие меню */
.menu-wrap { position: relative; }

.dropdown {
  position: absolute;
  z-index: 40;
  padding: 6px 0;
  border-radius: var(--radius-sm);
  background: var(--surface);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.45);
}

.dropdown button {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  padding: 10px 16px;
  border: none;
  background: none;
  text-align: left;
  white-space: nowrap;
  cursor: pointer;
}
.dropdown button:hover { background: var(--surface-3); }
.dropdown button.selected { color: var(--accent); }

.main-menu { top: 42px; right: 0; min-width: 180px; }
.main-menu svg { color: var(--text-muted); }

.filter-menu { top: 34px; right: 0; min-width: 130px; }

/* Карточки */
.card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 16px;
}

/* Баланс */
.balance-card { text-align: center; padding-top: 12px; }

.period-wrap { position: relative; display: inline-block; }

.period-btn {
  padding: 6px 12px;
  border: none;
  border-radius: 999px;
  background: none;
  color: var(--text-muted);
  font-weight: 600;
  font-size: 13px;
  cursor: pointer;
}
.period-btn.open { background: var(--surface-3); }

.period-menu {
  top: 36px;
  left: 50%;
  transform: translateX(-50%);
  min-width: 190px;
  background: var(--surface-2);
}
.period-menu button { justify-content: center; }

.balance {
  margin: 14px 0 4px;
  font-size: 32px;
  font-weight: 700;
  color: var(--income);
  font-variant-numeric: tabular-nums;
}
.balance.negative { color: var(--danger); }

.balance-caption {
  margin: 0;
  color: var(--text-muted);
  font-size: 13px;
  font-weight: 600;
}

.balance-card hr {
  margin: 14px 0;
  border: none;
  border-top: 1px solid var(--border);
}

.totals { margin: 0; color: var(--text-muted); font-size: 12px; }

/* Список транзакций */
.section-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
}
.section-head h2 { margin: 0; font-size: 18px; font-weight: 600; }
.section-tools { display: flex; gap: 4px; }

.search-input { margin-bottom: 12px; }

.tx-list { display: flex; flex-direction: column; gap: 8px; }

.tx {
  padding: 12px;
  border-radius: var(--radius-sm);
  background: var(--surface-2);
}

.tx-top {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 8px;
}

.tx-name { font-weight: 600; font-size: 14px; overflow-wrap: anywhere; }
.tx-emoji { margin-right: 6px; }

.tx-amount {
  font-weight: 700;
  font-size: 15px;
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
}
.tx-amount.income { color: var(--income); }
.tx-amount.not-mine { color: var(--text-muted); font-weight: 600; }

.tx-desc {
  margin: 4px 0 0;
  color: var(--text-muted);
  font-size: 12px;
  overflow-wrap: anywhere;
}

.tx-bottom {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-top: 6px;
}
.tx-date { color: var(--text-muted); font-size: 11px; }

/* Доли разделённого расхода в списке */
.tx-splits { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.split-chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-height: 32px;
  padding: 4px 10px;
  border-radius: 999px;
  background: var(--surface-3);
  font-size: 12px;
}
.split-chip b { font-weight: 600; font-variant-numeric: tabular-nums; }
.split-chip.has-btn { padding-right: 3px; }
.split-chip.settled b { color: var(--income); }
.split-chip.owe b { color: var(--danger); }
.split-chip.payer { color: var(--text-muted); }
/* Кнопки у долей — крупные, чтобы легко попасть пальцем */
.chip-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  padding: 0;
  border: none;
  border-radius: 50%;
  background: var(--surface-2);
  color: var(--text-muted);
  font-size: 14px;
  line-height: 1;
  cursor: pointer;
}
.split-chip.settled .chip-btn { color: var(--income); }
.split-total { color: var(--text-muted); font-size: 11px; align-self: center; }

.tx-actions { display: flex; gap: 2px; }
.tx-actions button {
  display: inline-flex;
  padding: 4px;
  border: none;
  background: none;
  cursor: pointer;
}
.tx-actions svg { width: 16px; height: 16px; stroke: none; fill: currentColor; }
.tx-edit { color: var(--edit); }
.tx-delete { color: var(--danger); }

.empty {
  padding: 28px 12px;
  color: var(--text-muted);
  text-align: center;
  line-height: 1.5;
}

/* Пагинация */
.pager {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-top: 16px;
  color: var(--text-muted);
  font-size: 13px;
}

.btn-pager {
  padding: 8px 14px;
  border: none;
  border-radius: 8px;
  background: var(--accent);
  color: #fff;
  font-weight: 600;
  font-size: 13px;
  cursor: pointer;
}
.btn-pager:disabled { background: var(--accent-dim); color: var(--text-muted); cursor: default; }

/* Разделы-заглушки */
.page { display: none; }
.page.active { display: block; }

.page-head {
  display: flex;
  align-items: center;
  gap: 12px;
  margin: 4px 0 16px;
}
.page-head h2 { margin: 0; font-size: 17px; font-weight: 600; }

.back-link {
  padding: 0;
  border: none;
  background: none;
  color: var(--accent);
  font-size: 13px;
  cursor: pointer;
}

/* Поля ввода */
.input {
  width: 100%;
  height: 44px;
  padding: 0 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface-2);
  color: var(--text);
  font: inherit;
  color-scheme: dark;
  outline: none;
}
.input:focus { border-color: var(--accent); }

select.input {
  appearance: none;
  padding-right: 36px;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='6'%3E%3Cpath d='M0 0h10L5 6z' fill='%237f91a4'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 14px center;
}
.input::placeholder { color: var(--text-muted); }

/* Модальное окно */
.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background: rgba(0, 0, 0, 0.6);
}

.modal {
  width: 100%;
  max-width: 380px;
  max-height: 100%;
  overflow-y: auto;
  padding: 18px;
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.5);
}

.modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
}
.modal-head h3 { margin: 0; font-size: 16px; font-weight: 600; }

.modal-close {
  padding: 0 4px;
  border: none;
  background: none;
  color: var(--text-muted);
  font-size: 22px;
  line-height: 1;
  cursor: pointer;
}

.form-row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }

.field { display: block; margin-bottom: 14px; }
.field > span {
  display: block;
  margin-bottom: 6px;
  font-weight: 600;
  font-size: 13px;
}

/* Поле с кнопкой справа (ножницы у суммы) */
.input-group { display: flex; gap: 6px; }
.input-group .input { min-width: 0; }
.input-btn {
  flex: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 64px;
  height: 44px;
  border: 1px solid var(--accent-dim);
  border-radius: var(--radius-sm);
  background: var(--surface-3);
  color: var(--accent);
  cursor: pointer;
}
.input-btn svg { width: 28px; height: 28px; }
.input-btn.active { border-color: var(--accent); background: var(--accent); color: #fff; }

/* Разделение расхода */
.split-box {
  margin-bottom: 14px;
  padding: 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface-2);
}
.split-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 10px;
  font-weight: 600;
  font-size: 13px;
}
.stepper { display: flex; align-items: center; gap: 2px; }
.stepper .optional { margin-right: 4px; font-size: 12px; }
.split-count { min-width: 18px; text-align: center; }
.split-row {
  display: grid;
  grid-template-columns: 1fr 96px;
  gap: 8px;
  align-items: center;
  margin-bottom: 8px;
}
.split-row .input { height: 38px; background: var(--surface); }
.split-me { padding-left: 4px; font-weight: 600; }
.percent-wrap { position: relative; }
.percent-wrap .input { padding-right: 26px; text-align: right; }
.percent-wrap::after {
  content: "%";
  position: absolute;
  top: 50%;
  right: 10px;
  transform: translateY(-50%);
  color: var(--text-muted);
}
.split-percent[readonly] { color: var(--text-muted); }
.split-percent.bad { border-color: var(--danger); color: var(--danger); }
.split-preview { color: var(--text-muted); font-size: 12px; line-height: 1.6; }
.split-preview b { color: var(--text); font-weight: 600; }
.split-owe { color: var(--text); }
.split-payer {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 8px;
  align-items: center;
  margin-bottom: 10px;
  font-size: 13px;
}
.split-payer .input { height: 38px; background: var(--surface); }

/* Галочка «Повторять каждый месяц» */
.check-row { display: flex; align-items: flex-start; gap: 10px; margin-bottom: 14px; font-size: 13px; cursor: pointer; }
.check-row input,
.apply-check input { flex: none; width: 20px; height: 20px; margin: 0; accent-color: var(--accent); }

.form-error { margin: 0 0 12px; color: var(--danger); font-size: 13px; }

.modal-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  padding-top: 14px;
  border-top: 1px solid var(--border);
}

.btn {
  height: 40px;
  border: none;
  border-radius: 8px;
  font-weight: 600;
  cursor: pointer;
}
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-hover); }
.btn-primary:disabled { opacity: 0.6; cursor: default; }
.btn-secondary { background: var(--surface-3); color: var(--text); }

.optional { color: var(--text-muted); font-weight: 400; }
.hint { margin: 0 4px 16px; color: var(--text-muted); font-size: 12px; line-height: 1.5; }
.modal .hint { margin: -6px 0 14px; }

/* Полосы прогресса бюджета */
.budget-progress { margin-top: 12px; }
.budget-progress .totals { margin-top: 8px; }

.bar {
  display: block;
  height: 6px;
  border-radius: 3px;
  background: var(--surface-3);
  overflow: hidden;
}
.bar.small { width: 72px; height: 4px; margin-top: 6px; margin-left: auto; }
.bar-fill {
  display: block;
  height: 100%;
  width: 0;
  border-radius: inherit;
  background: #7d93ad;
}
.bar-fill.over { background: var(--danger); }

/* Настройки */
.section-label {
  margin: 0 4px 8px;
  color: var(--text-muted);
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.option-list { padding: 6px; }
.option {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  width: 100%;
  padding: 12px;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  background: none;
  text-align: left;
  cursor: pointer;
}
.option + .option { margin-top: 4px; }
.option.selected { border-color: var(--accent); background: var(--surface-2); }
.option-title { font-weight: 600; }
.option-example { color: var(--text-muted); font-size: 13px; white-space: nowrap; }
.option.selected .option-example { color: var(--accent); }

/* Категории */
.tabs { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-bottom: 12px; }
.tab {
  height: 36px;
  border: none;
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
  font-weight: 600;
  cursor: pointer;
}
.tab.active { background: var(--accent); color: #fff; }

.cat-list { display: flex; flex-direction: column; gap: 8px; margin: 8px 0; }

.cat-row {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  min-height: 52px;
  padding: 10px 12px;
  border: none;
  border-radius: var(--radius-sm);
  background: var(--surface-2);
  text-align: left;
  cursor: pointer;
  user-select: none;
}
.cat-row.dragging { opacity: 0.85; outline: 1px solid var(--accent); }

.drag-handle {
  display: inline-flex;
  padding: 6px 2px;
  color: var(--text-muted);
  cursor: grab;
  touch-action: none;
}
.drag-handle svg { width: 18px; height: 18px; }

.cat-emoji { flex: 0 0 26px; font-size: 18px; text-align: center; }
.cat-main { display: flex; flex-direction: column; flex: 1; min-width: 0; }
.cat-name { font-weight: 600; overflow-wrap: anywhere; }
.cat-sub { color: var(--text-muted); font-size: 11px; margin-top: 2px; }
.cat-amount-wrap { display: flex; flex-direction: column; align-items: flex-end; }
.cat-amount { font-size: 12px; color: var(--text-muted); white-space: nowrap; }
.cat-amount.income { color: var(--income); }
.cat-amount.over { color: var(--danger); }

.btn-dashed {
  width: 100%;
  height: 44px;
  border: 1px dashed var(--border);
  border-radius: var(--radius-sm);
  background: none;
  color: var(--accent);
  font-weight: 600;
  cursor: pointer;
}

.emoji-row { grid-template-columns: 76px 1fr; }
.emoji-input { text-align: center; font-size: 18px; }

.btn-link-danger {
  display: block;
  margin: 14px auto 0;
  padding: 4px 8px;
  border: none;
  background: none;
  color: var(--danger);
  font-size: 13px;
  cursor: pointer;
}

/* Главная: переходы в разделы */
.home-nav { display: flex; flex-direction: column; gap: 10px; }
.nav-tile {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  padding: 14px 16px;
  border: none;
  border-radius: var(--radius);
  background: var(--surface);
  text-align: left;
  cursor: pointer;
}
.nav-tile:hover { background: var(--surface-3); }
.nav-emoji { font-size: 22px; }
.nav-main { display: flex; flex-direction: column; flex: 1; min-width: 0; }
.nav-title { font-weight: 600; font-size: 15px; }
.nav-sub { color: var(--text-muted); font-size: 12px; margin-top: 2px; }
.nav-arrow { color: var(--text-muted); font-size: 22px; }

.section-period { color: var(--text-muted); font-weight: 600; font-size: 13px; }

/* Сводная таблица */
.month-switch {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  margin-bottom: 12px;
  font-weight: 600;
}
.month-switch .icon-btn { font-size: 22px; color: var(--text); }
.month-switch #month-label,
.month-switch .month-name { min-width: 130px; text-align: center; }

.sum-table { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
.sum-table th,
.sum-table td { padding: 7px 4px; text-align: right; white-space: nowrap; }
.sum-table th:first-child,
.sum-table td:first-child { text-align: left; white-space: normal; overflow-wrap: anywhere; }
.sum-table th { color: var(--text-muted); font-size: 11px; font-weight: 600; }
.sum-table tr.total td { font-weight: 700; border-bottom: 1px solid var(--border); }
.sum-table tr.total.income td:first-child { color: var(--income); }
.sum-table tr.total.expense td:first-child { color: var(--danger); }
.sum-table tbody tr:not(.total) td { font-size: 13px; }
.sum-table tbody tr:not(.total) td:last-child { color: var(--text-muted); }
.sum-table .good { color: var(--income); }
.sum-table .bad { color: var(--danger); }
.sum-table .muted { color: var(--text-muted); }
.sum-unit { margin: 0 0 8px; color: var(--text-muted); font-size: 11px; text-align: right; }
.sum-table tr.subhead th { padding-top: 12px; }

/* Настройки: месяц для записи */
.setting-text { margin: 0 0 12px; line-height: 1.5; }
.btn-wide { width: 100%; }

/* Отчёты */
.chart-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 10px;
}
.chart-title { font-weight: 600; font-size: 15px; }
.legend { display: flex; gap: 12px; color: var(--text-muted); font-size: 12px; }
.legend-item { display: inline-flex; align-items: center; gap: 5px; }
.swatch { width: 10px; height: 10px; border-radius: 3px; }
.swatch.income { background: var(--chart-income); }
.swatch.expense { background: var(--chart-expense); }

.chart { width: 100%; }
.chart-svg { display: block; width: 100%; height: auto; fill: initial; stroke: none; overflow: visible; }
.chart-svg .grid { stroke: var(--chart-grid); stroke-width: 1; }
.chart-svg .axis { fill: var(--text-muted); font-size: 10px; font-variant-numeric: tabular-nums; }
.chart-svg .col.income { fill: var(--chart-income); }
.chart-svg .col.expense { fill: var(--chart-expense); }
.chart-svg .band-hl { fill: var(--surface-3); }
.chart-svg .hit { fill: transparent; cursor: pointer; }
.chart-info { min-height: 18px; margin: 8px 0 12px; color: var(--text-muted); font-size: 12px; line-height: 1.5; }
.chart-info b { color: var(--text); }
.report-table td:first-child { white-space: nowrap; }

.hbar-list { display: flex; flex-direction: column; gap: 12px; }
.hbar-top {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 5px;
  font-size: 13px;
}
.hbar-name { overflow-wrap: anywhere; }
.hbar-value { font-weight: 600; white-space: nowrap; font-variant-numeric: tabular-nums; }
.hbar-share { color: var(--text-muted); font-weight: 400; font-size: 12px; }
.hbar-track { height: 8px; border-radius: 4px; background: var(--surface-2); }
.hbar-fill { height: 100%; border-radius: 0 4px 4px 0; }
.hbar-fill.expense { background: var(--chart-expense); }
.hbar-fill.income { background: var(--chart-income); }
.hbar-total { margin: 14px 0 0; color: var(--text-muted); font-size: 13px; text-align: right; }
.hbar-total b { color: var(--text); }

/* Помощь */
.help { line-height: 1.55; }
.help h3 { margin: 18px 0 6px; font-size: 15px; }
.help h3:first-child { margin-top: 0; }
.help p { margin: 0; color: var(--text-muted); }
.help ul { margin: 0; padding-left: 18px; color: var(--text-muted); }
.help li + li { margin-top: 4px; }
.help b { color: var(--text); }
.sum-table.report-table tbody tr td.good:last-child { color: var(--income); }
.sum-table.report-table tbody tr td.bad:last-child { color: var(--danger); }
.sum-table.report-table tbody td.muted { color: var(--text-muted); text-align: right; }
.card .tab { background: var(--surface-2); }
.card .tab.active { background: var(--accent); }

/* Главная: долги */
.debts-card .chart-head { margin-bottom: 4px; }
.link-btn { padding: 4px 0; border: none; background: none; color: var(--accent); font-size: 13px; cursor: pointer; }
.debt-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
}
.debt-side { display: flex; flex-direction: column; align-items: flex-end; gap: 6px; }
.debt-amount { font-weight: 700; white-space: nowrap; font-variant-numeric: tabular-nums; }
.debt-amount.plus { color: var(--income); }
.debt-amount.minus { color: var(--danger); }
.btn-chip {
  padding: 6px 12px;
  border: none;
  border-radius: 999px;
  background: var(--surface-3);
  color: var(--accent);
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
}
.btn-chip:disabled { opacity: 0.6; cursor: default; }
.debts-total { margin: 10px 0 0; color: var(--text-muted); font-size: 13px; }
.debts-total b { color: var(--text); }

/* Регулярные платежи */
#rec-apply-btn { margin-bottom: 12px; }
#page-recurring .cat-row { cursor: pointer; }
.apply-list { display: flex; flex-direction: column; gap: 8px; margin-bottom: 10px; }
.apply-row {
  display: grid;
  grid-template-columns: 1fr 110px;
  gap: 8px;
  align-items: center;
  padding: 6px 8px;
  border-radius: var(--radius-sm);
  background: var(--surface-2);
}
.apply-row .input { height: 38px; text-align: right; background: var(--surface); }
.apply-check { display: flex; align-items: center; gap: 10px; min-width: 0; cursor: pointer; }
.apply-name { display: flex; flex-direction: column; min-width: 0; overflow-wrap: anywhere; font-size: 14px; }
.apply-row.done .apply-name { color: var(--text-muted); }
.apply-total { margin: 0 0 12px; color: var(--text-muted); font-size: 13px; }
.apply-total b { color: var(--text); }
.debt-row:last-child { border-bottom: none; }
