/* Финансист: Mini App и сайт. Без сборки, системные шрифты. */

:root {
  color-scheme: light dark;

  /* Базовые цвета (светлая тема). Контраст текста на поверхностях не ниже AA. */
  --c-bg: #f3f4f7;
  --c-surface: #ffffff;
  --c-surface-2: #eceef3;
  --c-text: #14181f;
  --c-muted: #4d5767;
  --c-line: #d5d9e1;
  --c-primary: #1a5bd0;
  --c-on-primary: #ffffff;
  --c-danger: #b3202a;
  --c-on-danger: #ffffff;
  --c-success: #146c39;
  --c-warn: #7d5200;
  --c-focus: #1a5bd0;
  --c-bar: #1a5bd0;
  --c-bar-track: #e2e5ec;

  /* Тема Telegram имеет приоритет внутри Mini App. */
  --bg: var(--tg-theme-secondary-bg-color, var(--c-bg));
  --surface: var(--tg-theme-bg-color, var(--c-surface));
  --surface-2: var(--c-surface-2);
  --text: var(--tg-theme-text-color, var(--c-text));
  --muted: var(--tg-theme-hint-color, var(--c-muted));
  --line: var(--c-line);
  --primary: var(--tg-theme-button-color, var(--c-primary));
  --on-primary: var(--tg-theme-button-text-color, var(--c-on-primary));
  --danger: var(--c-danger);
  --on-danger: var(--c-on-danger);
  --success: var(--c-success);
  --warn: var(--c-warn);
  --focus: var(--c-focus);

  --radius: 14px;
  --gutter: 16px;
  --tap: 44px;
  --nav-h: 60px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans", sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --c-bg: #0e1116;
    --c-surface: #171c24;
    --c-surface-2: #212833;
    --c-text: #e9edf3;
    --c-muted: #a6b0bf;
    --c-line: #313a48;
    --c-primary: #7aaeff;
    --c-on-primary: #08111f;
    --c-danger: #ff8d93;
    --c-on-danger: #1c0507;
    --c-success: #62d58f;
    --c-warn: #f0c36a;
    --c-focus: #9cc4ff;
    --c-bar: #7aaeff;
    --c-bar-track: #2a3240;
  }
}

* { box-sizing: border-box; }

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 var(--font);
  min-height: 100vh;
  overflow-wrap: anywhere;
}

h1, h2, h3 { margin: 0; line-height: 1.2; }
h1 { font-size: 1.5rem; }
h2 { font-size: 1.15rem; }
h3 { font-size: 1rem; }
p { margin: 0; }
ul { margin: 0; padding: 0; list-style: none; }
a { color: var(--primary); }
button, input, select, textarea { font: inherit; color: inherit; }

:focus-visible {
  outline: 3px solid var(--focus);
  outline-offset: 2px;
}

.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0;
}

.muted { color: var(--muted); }
.small { font-size: 0.875rem; }
.num { font-variant-numeric: tabular-nums; white-space: nowrap; }
.pos { color: var(--success); }
.neg { color: var(--text); }

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

.app { min-height: 100vh; }

.sidebar { display: none; }

.main {
  width: 100%;
  max-width: 760px;
  margin: 0 auto;
  padding: 16px var(--gutter) calc(var(--nav-h) + var(--safe-b) + 24px);
}

.screen { display: grid; gap: 16px; }

.screen-head {
  display: flex; align-items: center; justify-content: space-between;
  gap: 12px; flex-wrap: wrap;
}

.bottomnav {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
  display: grid; grid-auto-flow: column; grid-auto-columns: 1fr;
  background: var(--surface);
  border-top: 1px solid var(--line);
  padding-bottom: var(--safe-b);
}

.nav-item {
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 2px; min-height: var(--nav-h); min-width: 44px;
  padding: 6px 4px; text-decoration: none; color: var(--muted);
  font-size: 0.75rem; font-weight: 500;
}
.nav-item svg { width: 24px; height: 24px; }
.nav-item[aria-current="page"] { color: var(--primary); font-weight: 700; }
.nav-item.accent .nav-plus {
  display: grid; place-items: center; width: 44px; height: 44px; border-radius: 50%;
  background: var(--primary); color: var(--on-primary);
}
.nav-item.accent { color: var(--text); }

@media (min-width: 900px) {
  .app { display: grid; grid-template-columns: 250px minmax(0, 1fr); }
  .sidebar {
    display: flex; flex-direction: column; gap: 4px;
    position: sticky; top: 0; height: 100vh; overflow-y: auto;
    padding: 20px 12px;
    background: var(--surface); border-right: 1px solid var(--line);
  }
  .sidebar .brand { padding: 4px 12px 16px; font-size: 1.25rem; font-weight: 800; }
  .sidebar .nav-item {
    flex-direction: row; justify-content: flex-start; gap: 12px;
    min-height: 48px; padding: 8px 12px; border-radius: 12px;
    font-size: 1rem;
  }
  .sidebar .nav-item[aria-current="page"] { background: var(--surface-2); }
  .bottomnav { display: none; }
  .main { max-width: 900px; padding: 28px 32px 48px; margin: 0; }
}

/* ---------- карточки и списки ---------- */

.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 16px;
  display: grid; gap: 12px;
}
.card > h2, .card > h3 { margin: 0; }

.kpi-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 12px; }
.kpi { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; display: grid; gap: 4px; }
.kpi .label { color: var(--muted); font-size: 0.85rem; }
.kpi .value { font-size: 1.25rem; font-weight: 700; font-variant-numeric: tabular-nums; }
.kpi.big .value { font-size: 1.7rem; }

.list { display: grid; gap: 0; }
.list > li { border-top: 1px solid var(--line); }
.list > li:first-child { border-top: 0; }

.row {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  min-height: var(--tap); padding: 10px 0;
  color: inherit; text-decoration: none;
}
a.row, button.row { cursor: pointer; }
.row .grow { min-width: 0; flex: 1; display: grid; gap: 2px; }
.row .title { font-weight: 600; }
.row .sub { color: var(--muted); font-size: 0.85rem; }
.row .amount { font-weight: 700; text-align: right; font-variant-numeric: tabular-nums; }
.row.reversed .title, .row.reversed .amount { text-decoration: line-through; color: var(--muted); }

.group-title { color: var(--muted); font-size: 0.85rem; font-weight: 600; margin: 8px 0 0; }

.badge {
  display: inline-block; padding: 2px 8px; border-radius: 999px;
  font-size: 0.75rem; font-weight: 600;
  background: var(--surface-2); color: var(--text); border: 1px solid var(--line);
}
.badge.danger { color: var(--danger); border-color: var(--danger); background: transparent; }
.badge.ok { color: var(--success); border-color: var(--success); background: transparent; }
.badge.warn { color: var(--warn); border-color: var(--warn); background: transparent; }

dl.facts { margin: 0; display: grid; grid-template-columns: minmax(90px, 35%) 1fr; gap: 8px 12px; }
dl.facts dt { color: var(--muted); }
dl.facts dd { margin: 0; }

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

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: var(--tap); padding: 10px 18px;
  border-radius: 12px; border: 1px solid var(--line);
  background: var(--surface); color: var(--text);
  font-weight: 600; text-decoration: none; cursor: pointer;
}
.btn:disabled, .btn[aria-disabled="true"] { opacity: 0.55; cursor: not-allowed; }
.btn-primary { background: var(--primary); border-color: var(--primary); color: var(--on-primary); }
.btn-danger { background: var(--danger); border-color: var(--danger); color: var(--on-danger); }
.btn-ghost { background: transparent; }
.btn-block { width: 100%; }
.btn-row { display: flex; gap: 8px; flex-wrap: wrap; }
.btn-row > .btn { flex: 1 1 140px; }
.btn-small { min-height: var(--tap); padding: 6px 12px; font-size: 0.9rem; }

.form { display: grid; gap: 14px; }
.field { display: grid; gap: 6px; min-width: 0; }
.field > span.lbl { font-size: 0.9rem; font-weight: 600; }
.field .hint { color: var(--muted); font-size: 0.8rem; }
.field-row { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px; }

input[type="text"], input[type="search"], input[type="number"], input[type="date"],
input[type="time"], input[type="file"], select, textarea {
  width: 100%; min-height: var(--tap); padding: 10px 12px;
  background: var(--surface); color: var(--text);
  border: 1px solid var(--muted); border-radius: 10px;
}
textarea { min-height: 88px; resize: vertical; }
input[aria-invalid="true"] { border-color: var(--danger); border-width: 2px; }
.field-error { color: var(--danger); font-size: 0.85rem; }

.check {
  display: flex; align-items: center; gap: 12px; min-height: var(--tap); cursor: pointer;
}
.check input { width: 24px; height: 24px; flex: none; accent-color: var(--primary); }

.choice {
  display: flex; gap: 12px; align-items: flex-start; padding: 12px;
  border: 1px solid var(--line); border-radius: 12px; cursor: pointer;
  background: var(--surface); min-height: var(--tap);
}
.choice input { width: 22px; height: 22px; margin-top: 2px; flex: none; accent-color: var(--primary); }
.choice:has(input:checked) { border: 2px solid var(--primary); padding: 11px; }
.choice .t { font-weight: 700; display: block; }
.choice .d { color: var(--muted); font-size: 0.875rem; }

/* вкладки и чипы */
.tabs { display: flex; gap: 6px; overflow-x: auto; padding-bottom: 2px; scrollbar-width: thin; }
.tab, .chip {
  flex: none; min-height: var(--tap); padding: 8px 14px; border-radius: 999px;
  border: 1px solid var(--line); background: var(--surface); color: var(--text);
  font-weight: 600; cursor: pointer;
}
.tab[aria-selected="true"], .chip[aria-pressed="true"] {
  background: var(--primary); border-color: var(--primary); color: var(--on-primary);
}

/* ---------- состояния ---------- */

.state { display: grid; gap: 12px; justify-items: center; text-align: center; padding: 32px 12px; color: var(--muted); }
.state h2 { color: var(--text); }
.state.error { color: var(--text); }
.spinner {
  width: 28px; height: 28px; border-radius: 50%;
  border: 3px solid var(--line); border-top-color: var(--primary);
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { .spinner { animation-duration: 3s; } }

.notice {
  border: 1px solid var(--line); border-left: 4px solid var(--primary);
  background: var(--surface); border-radius: 10px; padding: 12px 14px;
}
.notice.danger { border-left-color: var(--danger); }
.notice.warn { border-left-color: var(--warn); }

#toasts {
  position: fixed; left: 0; right: 0; z-index: 60;
  bottom: calc(var(--nav-h) + var(--safe-b) + 12px);
  display: grid; gap: 8px; justify-items: center; padding: 0 var(--gutter);
  pointer-events: none;
}
@media (min-width: 900px) { #toasts { bottom: 24px; } }
.toast {
  pointer-events: auto; max-width: 520px; width: 100%;
  background: var(--text); color: var(--bg);
  padding: 12px 16px; border-radius: 12px; font-weight: 500;
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.25);
}
.toast.error { background: var(--danger); color: var(--on-danger); }

dialog {
  width: min(92vw, 420px); padding: 20px; border: 1px solid var(--line);
  border-radius: 16px; background: var(--surface); color: var(--text);
}
dialog::backdrop { background: rgba(0, 0, 0, 0.55); }
dialog .dlg { display: grid; gap: 14px; }

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

.bars { display: grid; gap: 12px; }
.bar-item { display: grid; gap: 4px; }
.bar-head { display: flex; justify-content: space-between; gap: 12px; }
.bar-track {
  height: 10px; border-radius: 999px; background: var(--c-bar-track); overflow: hidden;
}
.bar-fill { height: 100%; border-radius: 999px; background: var(--c-bar); min-width: 2px; }
.bar-fill.warn { background: var(--warn); }
.bar-fill.over { background: var(--danger); }

.spark { width: 100%; height: 90px; display: block; }
.spark .line { fill: none; stroke: var(--c-bar); stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; vector-effect: non-scaling-stroke; }
.spark .area { fill: var(--c-bar); opacity: 0.12; }
.spark .axis { stroke: var(--line); stroke-width: 1; vector-effect: non-scaling-stroke; }

.table-scroll { overflow-x: auto; border: 1px solid var(--line); border-radius: 10px; }
table { border-collapse: collapse; width: 100%; font-size: 0.85rem; }
th, td { text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--line); white-space: nowrap; }
th { color: var(--muted); font-weight: 600; }
tr:last-child td { border-bottom: 0; }

/* ---------- вход и первый запуск ---------- */

.center-page {
  min-height: 100vh; display: grid; place-items: center; padding: 24px var(--gutter);
}
.center-card { width: 100%; max-width: 440px; display: grid; gap: 16px; }
.brand-mark { font-size: 1.6rem; font-weight: 800; }
.widget-box { min-height: 48px; display: grid; justify-items: center; }

/* Работа с чужими данными по выданному доступу: плашка всегда на виду, чтобы не перепутать, чьи данные открыты. */
.actas { position: sticky; top: 0; z-index: 30; display: flex; flex-wrap: wrap; gap: 8px 12px; align-items: center; justify-content: space-between;
  padding: 8px 16px; background: var(--warn); color: #1a1300; font-weight: 600; }
.actas[hidden] { display: none; }
