/* Цвета поверхностей и текста берём из темы Telegram (--tg-theme-*): дашборд выглядит
   частью Telegram, светлый он или тёмный. Свои — только цвета данных (проверены на
   различимость при дальтонизме) и запасные значения для браузера вне Telegram. */
:root {
  color-scheme: light;
  --page: var(--tg-theme-secondary-bg-color, #f1f1f4);
  --section: var(--tg-theme-section-bg-color, var(--tg-theme-bg-color, #ffffff));
  --text: var(--tg-theme-text-color, #121214);
  --hint: var(--tg-theme-hint-color, #85858c);
  --header: var(--tg-theme-section-header-text-color, var(--tg-theme-hint-color, #6d6d72));
  --accent: var(--tg-theme-accent-text-color, var(--tg-theme-link-color, #2a78d6));
  --button: var(--tg-theme-button-color, #2a78d6);
  --button-text: var(--tg-theme-button-text-color, #ffffff);
  --separator: var(--tg-theme-section-separator-color, rgba(120, 120, 128, 0.22));
  --destructive: var(--tg-theme-destructive-text-color, #d03b3b);
  --track: rgba(120, 120, 128, 0.16);
  --tab-on: var(--section);

  --income: #2a78d6;
  --expense: #eb6834;
  --capital: #6250d6;
  --aside: #898781;
  --good: #1f8a3b;

  --radius: 14px;
  --gap: 12px;
  font-family: system-ui, -apple-system, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  font-size: 15px;
  line-height: 1.4;
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --income: #3987e5;
  --expense: #d95926;
  --capital: #9085e9;
  --good: #3fb95f;
  --track: rgba(160, 160, 168, 0.18);
  --tab-on: rgba(150, 150, 160, 0.42);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --page: var(--tg-theme-secondary-bg-color, #0f0f10);
    --section: var(--tg-theme-section-bg-color, var(--tg-theme-bg-color, #1c1c1e));
    --text: var(--tg-theme-text-color, #f2f2f5);
    --hint: var(--tg-theme-hint-color, #8d8d93);
    --income: #3987e5;
    --expense: #d95926;
    --capital: #9085e9;
    --good: #3fb95f;
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  background: var(--page);
  color: var(--text);
  -webkit-text-size-adjust: 100%;
}

body { padding-bottom: calc(24px + env(safe-area-inset-bottom)); }

button { font: inherit; color: inherit; }

:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 8px; }

/* --- шапка: разделы и период --- */

.top {
  position: sticky;
  top: 0;
  z-index: 5;
  padding: 10px 16px 8px;
  background: var(--page);
}

.tabs {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  padding: 3px;
  border-radius: 11px;
  background: var(--track);
}

.tabs button {
  border: 0;
  padding: 7px 0;
  border-radius: 8px;
  background: transparent;
  font-size: 14px;
  font-weight: 500;
  color: var(--text);
  cursor: pointer;
}

.tabs button[aria-selected="true"] {
  background: var(--tab-on);
  font-weight: 600;
  box-shadow: 0 0 0 0.5px var(--separator);
}

.periods {
  display: flex;
  gap: 6px;
  margin-top: 10px;
  overflow-x: auto;
  scrollbar-width: none;
}

.periods[hidden] { display: none; }

.periods button, .chips button {
  flex: none;
  border: 0;
  padding: 6px 12px;
  border-radius: 999px;
  background: var(--track);
  font-size: 14px;
  cursor: pointer;
}

.periods button[aria-checked="true"], .chips button[aria-checked="true"] {
  background: var(--button);
  color: var(--button-text);
}

main { padding: 4px 16px 0; transition: opacity 0.2s; }
main.loading { opacity: 0.55; }

@media (prefers-reduced-motion: reduce) {
  main { transition: none; }
}

.panel[hidden] { display: none; }

.notice {
  margin: 8px 0 12px;
  padding: 12px 14px;
  border-radius: var(--radius);
  background: var(--section);
  color: var(--text);
}

/* --- главный итог периода --- */

.hero { padding: 14px 4px 18px; }

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

.hero-value {
  margin: 2px 0 14px;
  font-size: 46px;
  line-height: 1.05;
  font-weight: 650;
  letter-spacing: -0.02em;
}

.hero-value.negative { color: var(--destructive); }

.meter {
  height: 8px;
  border-radius: 4px;
  background: var(--track);
  background: color-mix(in srgb, var(--income) 24%, transparent);
  overflow: hidden;
}

.meter-fill {
  height: 100%;
  width: 0;
  border-radius: 4px;
  background: var(--expense);
  transition: width 0.4s ease-out;
}

@media (prefers-reduced-motion: reduce) {
  .meter-fill { transition: none; }
}

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

.hero-pair {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin: 0;
}

.hero-pair dt { color: var(--hint); font-size: 14px; display: flex; align-items: center; gap: 6px; }
.hero-pair dd { margin: 2px 0 0; font-size: 19px; font-weight: 600; }

/* метка цвета рядом с текстом: сам текст всегда цвета текста */
.key { display: inline-block; width: 10px; height: 10px; border-radius: 3px; flex: none; }
.key-income { background: var(--income); }
.key-expense { background: var(--expense); }
.key-aside { background: var(--aside); }
.key-net { width: 9px; height: 9px; border-radius: 50%; background: var(--text); box-shadow: 0 0 0 2px var(--section); }

/* --- секции --- */

.block {
  margin: 0 0 var(--gap);
  padding: 14px 16px;
  border-radius: var(--radius);
  background: var(--section);
}

figure.block { margin-left: 0; margin-right: 0; }

.block-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 10px;
  font-size: 15px;
  font-weight: 600;
}

.link {
  border: 0;
  padding: 0;
  background: none;
  color: var(--accent);
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
}

.legend {
  display: flex;
  flex-wrap: wrap;
  gap: 4px 14px;
  margin: 0 0 8px;
  padding: 0;
  list-style: none;
  color: var(--hint);
  font-size: 13px;
}

.legend li { display: flex; align-items: center; gap: 6px; }

.chart-box { position: relative; height: 240px; }
.chart-box[hidden] { display: none; }

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

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

/* --- отчёт P&L --- */

.statement { margin: 0; }

.st-row {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 12px;
  padding: 7px 0;
}

.st-row + .st-row { border-top: 0.5px solid var(--separator); }

.st-row .amount { font-variant-numeric: tabular-nums; white-space: nowrap; }

.st-group { font-weight: 600; }
.st-group .name { display: flex; align-items: center; gap: 8px; }

.st-item .name { padding-left: 18px; color: var(--text); }
.st-item .amount { color: var(--hint); }

.st-total {
  margin-top: 4px;
  border-top: 1.5px solid var(--text) !important;
  font-weight: 650;
  font-size: 17px;
}

.st-total .amount.negative { color: var(--destructive); }

.st-subhead {
  padding: 16px 0 4px;
  color: var(--header);
  font-size: 13px;
}

/* --- категории полосками --- */

.bars { margin: 0; padding: 0; list-style: none; }

.bar-row { border-top: 0.5px solid var(--separator); }
.bar-row:first-child { border-top: 0; }

.bar-head {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 4px 12px;
  width: 100%;
  padding: 9px 0 10px;
  border: 0;
  background: none;
  text-align: left;
  cursor: pointer;
}

.bar-head .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bar-head .amount { font-variant-numeric: tabular-nums; white-space: nowrap; font-weight: 500; }
.bar-head .share { color: var(--hint); font-size: 13px; font-weight: 400; margin-left: 6px; }

.bar-track { grid-column: 1 / -1; height: 6px; border-radius: 3px; background: var(--track); }
.bar-fill { height: 100%; border-radius: 3px; background: var(--expense); min-width: 2px; }
.bar-fill.income { background: var(--income); }
.bar-fill.muted { background: var(--aside); }

.bar-detail { padding: 0 0 12px; }
.bar-detail .chart-box { height: 140px; }
.bar-detail .hint { margin-top: 6px; }

/* --- счета --- */

.chips {
  display: flex;
  gap: 6px;
  margin: 6px 0 12px;
  overflow-x: auto;
  scrollbar-width: none;
}

.kpis {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 14px 12px;
  margin: 0;
}

.kpis dt { color: var(--hint); font-size: 13px; }
.kpis dd { margin: 2px 0 0; font-size: 18px; font-weight: 600; }

.table-wrap { overflow-x: auto; margin: 0 -16px; padding: 0 16px; }
.table-wrap[hidden] { display: none; }

table { width: 100%; border-collapse: collapse; font-size: 14px; }
caption { caption-side: bottom; padding-top: 8px; text-align: left; color: var(--hint); font-size: 13px; }
th, td { padding: 7px 0 7px 12px; text-align: right; white-space: nowrap; }
th:first-child, td:first-child { padding-left: 0; text-align: left; }
thead th { color: var(--hint); font-weight: 500; font-size: 13px; }
tbody td { border-top: 0.5px solid var(--separator); font-variant-numeric: tabular-nums; }
tfoot td { border-top: 1.5px solid var(--text); font-weight: 600; font-variant-numeric: tabular-nums; }
td.negative { color: var(--destructive); }

/* --- загрузка --- */

.file-pick input { position: absolute; width: 1px; height: 1px; opacity: 0; }

.file-pick span, .primary, .secondary {
  display: block;
  width: 100%;
  padding: 12px;
  border: 0;
  border-radius: 12px;
  text-align: center;
  font-weight: 600;
  cursor: pointer;
}

.file-pick span { background: var(--track); color: var(--accent); }
.file-pick input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 2px; }

.primary { margin-top: 10px; background: var(--button); color: var(--button-text); }
.primary:disabled { opacity: 0.45; cursor: default; }
.secondary { margin-top: 8px; background: var(--track); color: var(--text); }

.file-list { margin: 8px 0 0; padding: 0; list-style: none; color: var(--hint); font-size: 14px; }
.file-list li { padding: 4px 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.check { color: var(--good); font-weight: 600; }
.error-text { color: var(--destructive); }

.imports { margin: 0; padding: 0; list-style: none; }
.imports li { display: flex; justify-content: space-between; gap: 12px; padding: 9px 0; border-top: 0.5px solid var(--separator); }
.imports li:first-child { border-top: 0; }
.imports .meta { color: var(--hint); font-size: 13px; }
.imports .link { color: var(--destructive); flex: none; }

.empty { margin: 0; color: var(--hint); }

.hint.outside { margin: -2px 4px 14px; }
.chart-box.small { height: 150px; }
.negative { color: var(--destructive); }
body.sheet-open { overflow: hidden; }

/* --- капитал: счета --- */

.accounts { margin: 0; padding: 0; list-style: none; }
.acc + .acc { border-top: 0.5px solid var(--separator); }

.acc-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  width: 100%;
  padding: 10px 0;
  border: 0;
  background: none;
  text-align: left;
  cursor: pointer;
}

.acc-name { display: flex; flex-direction: column; min-width: 0; }
.acc-name > span:first-child { font-weight: 500; }
.acc-sub { color: var(--hint); font-size: 13px; }
.acc-balance { font-size: 17px; font-weight: 600; white-space: nowrap; }

.acc-edit { padding: 0 0 12px; }
.acc-edit-row { display: flex; gap: 8px; }
.acc-edit input, .field select, .field input {
  flex: 1;
  min-width: 0;
  padding: 10px 12px;
  border: 0;
  border-radius: 10px;
  background: var(--track);
  color: var(--text);
  font: inherit;
}

.small-primary {
  flex: none;
  padding: 10px 14px;
  border: 0;
  border-radius: 10px;
  background: var(--button);
  color: var(--button-text);
  font-weight: 600;
  cursor: pointer;
}
.small-primary:disabled { opacity: 0.5; }

/* --- группы категорий --- */

.bars.sub { margin: -4px 0 6px; padding: 0 0 0 14px; list-style: none; border-left: 2px solid var(--track); }
.bar-head.child .name { font-size: 14px; }
.bar-head .name { display: flex; align-items: center; gap: 6px; }
.chev { display: inline-block; width: 12px; color: var(--hint); font-size: 18px; line-height: 1; transition: transform 0.15s; }
.chev.open { transform: rotate(90deg); }
@media (prefers-reduced-motion: reduce) { .chev { transition: none; } }

.st-row[role="button"] { cursor: pointer; }
.st-row[role="button"]:active { opacity: 0.6; }
.st-groupname .name { padding-left: 18px; color: var(--text); font-weight: 500; }
.st-groupname .amount { color: var(--text); }
.st-child .name { padding-left: 34px; color: var(--hint); font-size: 14px; }
.st-child .amount { font-size: 14px; }

/* --- экран категории --- */

.cv-head { padding-top: 10px; }
.link.back { display: block; margin: 0 0 8px; font-size: 15px; }
.cv-title { margin: 0; font-size: 22px; font-weight: 650; }
.cv-total { margin: 4px 0 2px; font-size: 34px; font-weight: 650; letter-spacing: -0.01em; }
.cv-head .hint { margin-top: 2px; }

.count { color: var(--hint); font-weight: 400; }
.link.more { margin-top: 8px; }

.ops .block-head { margin-bottom: 2px; }
.day-head {
  display: flex;
  justify-content: space-between;
  padding: 14px 0 6px;
  color: var(--header);
  font-size: 13px;
}
.day-head span:last-child { font-variant-numeric: tabular-nums; }

.op-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  width: 100%;
  padding: 9px 0;
  border: 0;
  border-top: 0.5px solid var(--separator);
  background: none;
  text-align: left;
  cursor: pointer;
}
.op-main { display: flex; flex-direction: column; min-width: 0; }
.op-title { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.op-sub { color: var(--hint); font-size: 13px; }
.op-amount { font-variant-numeric: tabular-nums; white-space: nowrap; font-weight: 500; }

/* --- карточка платежа --- */

.sheet-backdrop {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: flex;
  align-items: flex-end;
  background: rgba(0, 0, 0, 0.42);
}
.sheet-backdrop[hidden] { display: none; }

.sheet {
  width: 100%;
  max-height: 88vh;
  overflow-y: auto;
  padding: 8px 16px calc(16px + env(safe-area-inset-bottom));
  border-radius: 16px 16px 0 0;
  background: var(--section);
}

.sheet-grip { width: 36px; height: 5px; margin: 0 auto 12px; border-radius: 3px; background: var(--track); }
.sheet-amount { margin: 0; font-size: 30px; font-weight: 650; }
.sheet-title { margin: 2px 0 12px; font-size: 17px; font-weight: 500; }
.sheet-facts { display: grid; grid-template-columns: 1fr 1fr; gap: 8px 12px; margin: 0 0 10px; }
.sheet-facts dt { color: var(--hint); font-size: 13px; }
.sheet-facts dd { margin: 2px 0 0; }
.sheet-desc { margin: 0 0 14px; color: var(--hint); font-size: 13px; overflow-wrap: anywhere; }
.field { display: flex; flex-direction: column; gap: 6px; margin-bottom: 10px; color: var(--hint); font-size: 13px; }
.field select, .field input { font-size: 16px; }  /* 16px: iOS не приближает страницу при вводе */
.field select { appearance: auto; }
.check-row { display: flex; gap: 10px; align-items: flex-start; margin: 4px 0 8px; font-size: 14px; }
.check-row input { margin-top: 2px; width: 18px; height: 18px; accent-color: var(--button); }

/* --- места в категории --- */

.place-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  width: 100%;
  padding: 8px 0;
  border: 0;
  border-top: 0.5px solid var(--separator);
  background: none;
  text-align: left;
  cursor: pointer;
}
.block-head + .place-row { border-top: 0; }
.place-row .op-amount { color: var(--hint); font-weight: 400; }
.sheet-title.big { font-size: 22px; font-weight: 650; margin: 0 0 4px; }
.sheet-place { margin: 0 0 12px; font-size: 14px; color: var(--hint); }
.sheet-place .link { font-size: 14px; text-align: left; }
.sheet-title.big + .hint { margin: 0 0 14px; }
