/* Каркас приложения и общие детали: шапка, навигация, карточки, списки,
 * кнопки, поля, суммы. Всё, что переиспользуют ЭКРАНЫ, живёт здесь; всё, что
 * нужно одному экрану, — в его собственном .css.
 *
 * Этот файл правит только владелец каркаса. Экраны его не трогают — иначе
 * параллельные правки затирают друг друга (урок lotscout: восемь агентов и
 * один общий файл).
 */

/* ── Сброс ──────────────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; }

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

body {
  margin: 0;
  background: var(--canvas);
  color: var(--ink);
  font-family: var(--font);
  font-size: var(--t-body);
  line-height: var(--lh-body);
  /* Приложение, добавленное на экран, не должно «оттягиваться» как страница:
   * резиновый скролл всего документа выдаёт браузер с головой. Скроллится
   * контент, а не body. */
  overscroll-behavior-y: none;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }

/* Подсветка тапа в iOS — синий прямоугольник поверх скруглений. Гасим и
 * рисуем нажатие сами (`:active` на строках и кнопках). */
* { -webkit-tap-highlight-color: transparent; }

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

/* ── Каркас ─────────────────────────────────────────────────────────── */
#app {
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
}

/* Шапка. На телефоне — липкая, компактная; крупный заголовок живёт ВНУТРИ
 * контента и сворачивается в неё при прокрутке (см. app.js). */
#top {
  position: sticky;
  top: 0;
  z-index: 30;
  display: flex;
  align-items: center;
  gap: var(--s3);
  height: calc(var(--header-h) + var(--safe-t));
  padding: var(--safe-t) var(--s4) 0;
  background: color-mix(in srgb, var(--surface) 82%, transparent);
  /* Размытие под шапкой — та самая деталь, по которой iOS-приложение узнают.
   * Без него липкая шапка выглядит как приклеенная плашка. */
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  backdrop-filter: saturate(180%) blur(20px);
  border-bottom: .5px solid transparent;
  transition: border-color .2s linear;
}

#top.scrolled { border-bottom-color: var(--hairline); }

#top-title {
  flex: 1;
  min-width: 0;
  font-size: var(--t-headline);
  font-weight: 600;
  letter-spacing: -.01em;
  text-align: center;
  opacity: 0;
  transform: translateY(6px);
  transition: opacity .18s linear, transform .18s var(--ease);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

#top.scrolled #top-title { opacity: 1; transform: none; }

#top-left, #top-actions {
  display: flex;
  align-items: center;
  gap: var(--s2);
  min-width: 44px;
}
#top-actions { justify-content: flex-end; }

/* Контент. `padding-bottom` учитывает таб-бар и вырез — иначе последняя
 * строка списка навсегда прячется под панелью. */
#screen {
  flex: 1;
  width: 100%;
  max-width: var(--content-max);
  margin: 0 auto;
  padding: var(--s3) var(--s4) calc(var(--tabbar-h) + var(--safe-b) + var(--s7));
}

#screen.wide { max-width: var(--wide-max); }

/* Крупный заголовок экрана — прокручивается вместе с контентом. */
.screen-head {
  padding: var(--s2) 0 var(--s4);
}
.screen-head h1 {
  margin: 0;
  font-size: var(--t-large);
  font-weight: 700;
  letter-spacing: -.022em;
  line-height: var(--lh-tight);
}
.screen-head .sub {
  margin: var(--s1) 0 0;
  color: var(--ink-2);
  font-size: var(--t-subhead);
}

/* ── Навигация ──────────────────────────────────────────────────────── */
/* Телефон: нижняя панель. Пять разделов — предел, дальше подписи не влезают
 * и по ним промахиваются. */
#nav {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  z-index: 40;
  display: flex;
  padding-bottom: var(--safe-b);
  background: color-mix(in srgb, var(--surface) 82%, transparent);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  backdrop-filter: saturate(180%) blur(20px);
  box-shadow: var(--shadow-bar);
}

.nav-item {
  flex: 1;
  min-width: 0;
  height: var(--tabbar-h);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  padding: 0 2px;
  border: 0;
  background: none;
  color: var(--ink-3);
  font-size: var(--t-caption2);
  font-weight: 500;
  letter-spacing: -.01em;
  transition: color .15s linear;
}

.nav-item .ico {
  width: 26px;
  height: 26px;
  display: block;
  stroke: currentColor;
  fill: none;
  stroke-width: 1.7;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.nav-item[aria-current="true"] { color: var(--accent); }
.nav-item:active .ico { transform: scale(.9); }
.nav-item .ico { transition: transform .12s var(--ease); }

/* Значок «требует внимания» на разделе. Число, а не точка: точка сообщает
 * «что-то есть», число — «сколько», и второе решает, идти туда сейчас или нет. */
.nav-badge {
  position: absolute;
  transform: translate(14px, -14px);
  min-width: 17px;
  height: 17px;
  padding: 0 5px;
  border-radius: var(--r-pill);
  background: var(--expense);
  color: #fff;
  font-size: 10px;
  font-weight: 700;
  line-height: 17px;
  text-align: center;
}
.nav-item { position: relative; }

/* Планшет и десктоп: боковая панель. Разделы разворачиваются в полный
 * список — прятать «Ещё» на широком экране незачем. */
#nav-wide { display: none; }

@media (min-width: 900px) {
  #nav { display: none; }

  #app {
    flex-direction: row;
    align-items: stretch;
  }

  #nav-wide {
    display: flex;
    flex-direction: column;
    gap: var(--s1);
    position: sticky;
    top: 0;
    align-self: flex-start;
    width: var(--sidebar-w);
    height: 100dvh;
    padding: var(--s5) var(--s3);
    background: var(--surface);
    border-right: .5px solid var(--hairline);
    overflow-y: auto;
  }

  .brand {
    display: flex;
    align-items: center;
    gap: var(--s3);
    padding: 0 var(--s3) var(--s5);
  }
  .brand-mark {
    width: 30px; height: 30px;
    border-radius: 8px;
    background: var(--accent);
    color: #fff;
    display: grid; place-items: center;
    font-size: 13px; font-weight: 800; letter-spacing: -.03em;
  }
  .brand-name { font-size: var(--t-headline); font-weight: 700; letter-spacing: -.02em; }

  .side-item {
    display: flex;
    align-items: center;
    gap: var(--s3);
    padding: 9px var(--s3);
    border: 0;
    border-radius: var(--r-row);
    background: none;
    color: var(--ink);
    font-size: var(--t-callout);
    font-weight: 500;
    text-align: left;
  }
  .side-item .ico { width: 20px; height: 20px; stroke: currentColor; fill: none;
                    stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round;
                    color: var(--ink-3); flex: none; }
  .side-item:hover { background: var(--surface-2); }
  .side-item[aria-current="true"] { background: var(--accent-soft); color: var(--accent-text); }
  .side-item[aria-current="true"] .ico { color: var(--accent); }
  .side-item .count {
    margin-left: auto;
    font-size: var(--t-footnote);
    font-weight: 600;
    color: var(--ink-3);
    font-variant-numeric: tabular-nums;
  }
  .side-item .count.alert { color: var(--expense); }

  .side-group {
    margin: var(--s4) 0 var(--s1);
    padding: 0 var(--s3);
    font-size: var(--t-caption2);
    font-weight: 600;
    letter-spacing: .06em;
    text-transform: uppercase;
    color: var(--ink-3);
  }

  #main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
  #top { padding-left: var(--s6); padding-right: var(--s6); }
  #top-title { text-align: left; }
  #screen { padding: var(--s4) var(--s6) var(--s8); }
}

/* ── Карточки и сгруппированные списки ──────────────────────────────── */
.card {
  background: var(--surface);
  border-radius: var(--r-card);
  padding: var(--s4);
  margin-bottom: var(--s3);
}
.card.flush { padding: 0; overflow: hidden; }

.card-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: var(--s3);
  margin-bottom: var(--s3);
}
.card-title {
  font-size: var(--t-headline);
  font-weight: 600;
  letter-spacing: -.01em;
}
.card-note { font-size: var(--t-footnote); color: var(--ink-3); }

/* Заголовок группы над карточкой — как в системных «Настройках». */
.group-label {
  margin: var(--s5) var(--s2) var(--s2);
  font-size: var(--t-footnote);
  font-weight: 500;
  color: var(--ink-2);
}

/* Строка списка. Разделитель рисуется ПСЕВДОЭЛЕМЕНТОМ с отступом слева —
 * так он не доходит до края и не режет иконку, как в iOS. */
.row {
  position: relative;
  display: flex;
  align-items: center;
  gap: var(--s3);
  width: 100%;
  padding: var(--s3) var(--s4);
  border: 0;
  background: none;
  text-align: left;
  color: inherit;
}
.row + .row::before {
  content: "";
  position: absolute;
  top: 0; left: var(--s4); right: 0;
  height: .5px;
  background: var(--hairline);
}
button.row:active, a.row:active { background: var(--surface-3); }

.row-main { flex: 1; min-width: 0; }
.row-title {
  font-size: var(--t-callout);
  font-weight: 500;
  letter-spacing: -.01em;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.row-sub {
  margin-top: 1px;
  font-size: var(--t-footnote);
  color: var(--ink-3);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.row-side { flex: none; text-align: right; }

.chevron { flex: none; width: 8px; height: 13px; color: var(--ink-3); opacity: .7; }

/* ── Деньги ─────────────────────────────────────────────────────────────
 * Главная типографическая работа приложения. Табличные цифры обязательны:
 * в колонке сумм разряды обязаны стоять друг под другом, иначе столбец
 * невозможно читать взглядом сверху вниз. */
.money {
  font-variant-numeric: tabular-nums lining-nums;
  font-feature-settings: "tnum" 1, "lnum" 1;
  white-space: nowrap;
  letter-spacing: -.02em;
}
.money .cur {
  margin-left: .18em;
  font-size: .62em;
  font-weight: 500;
  color: var(--ink-3);
  letter-spacing: 0;
}
.money.hero { font-size: var(--m-hero); font-weight: 700; letter-spacing: -.035em; }
.money.lg   { font-size: var(--m-lg);   font-weight: 700; letter-spacing: -.03em; }
.money.md   { font-size: var(--m-md);   font-weight: 600; }
.money.sm   { font-size: var(--m-sm);   font-weight: 500; }

.money.income  { color: var(--income); }
.money.expense { color: var(--expense); }
.money.neutral { color: var(--neutral); }
.money.muted   { color: var(--ink-3); }

/* Подпись-надзаголовок над суммой: откуда взялась цифра.
 *
 * ЭТО НЕ УКРАШЕНИЕ. В findash две несводимые картины денег — банк (касса,
 * факт) и ручной журнал (начисления, с наличными). Их регулярно путали, и
 * каждый раз это стоило разбора на полдня. Источник подписан у КАЖДОЙ суммы,
 * которую можно перепутать. */
.source {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: var(--t-caption2);
  font-weight: 600;
  letter-spacing: .07em;
  text-transform: uppercase;
  color: var(--ink-3);
}
.source::before {
  content: "";
  width: 5px; height: 5px;
  border-radius: 50%;
  background: currentColor;
  opacity: .55;
}
.source.bank { color: var(--accent-text); }
.source.journal { color: var(--warn); }

/* ── Плашки ─────────────────────────────────────────────────────────── */
.tag {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 2px 8px;
  border-radius: var(--r-pill);
  background: var(--neutral-soft);
  color: var(--ink-2);
  font-size: var(--t-caption);
  font-weight: 600;
  white-space: nowrap;
}
.tag.income  { background: var(--income-soft);  color: var(--income); }
.tag.expense { background: var(--expense-soft); color: var(--expense); }
.tag.warn    { background: var(--warn-soft);    color: var(--warn); }
.tag.accent  { background: var(--accent-soft);  color: var(--accent-text); }

/* ── Кнопки ─────────────────────────────────────────────────────────── */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--s2);
  /* 44px — минимальная цель для пальца по HIG. Ниже начинают промахиваться. */
  min-height: 44px;
  padding: 0 var(--s5);
  border: 0;
  border-radius: var(--r-control);
  background: var(--surface-2);
  color: var(--accent-text);
  font-size: var(--t-callout);
  font-weight: 600;
  letter-spacing: -.01em;
  transition: transform .1s var(--ease), opacity .12s linear;
}
.btn:active { transform: scale(.97); }
.btn:disabled { opacity: .4; pointer-events: none; }

/* Иконка внутри кнопки. У SVG без явного размера ширина схлопывается в ноль,
   и кнопка выглядит как один текст — а на кнопках, где иконка ЕДИНСТВЕННОЕ
   содержимое (обновить, закрыть), не остаётся вообще ничего. */
.btn .ico, .btn svg {
  width: 18px;
  height: 18px;
  flex: none;
  stroke: currentColor;
  fill: none;
  stroke-width: 1.9;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.btn.small .ico, .btn.small svg { width: 16px; height: 16px; }

.btn.primary { background: var(--accent-fill); color: var(--ink-on-accent); }
.btn.danger  { background: var(--expense-soft); color: var(--expense); }
.btn.ghost   { background: none; }
.btn.wide    { width: 100%; }
.btn.small   { min-height: 34px; padding: 0 var(--s3); font-size: var(--t-footnote); }

/* Сегментированный переключатель — системный элемент выбора одного из 2–4. */
.segmented {
  display: inline-flex;
  padding: 2px;
  border-radius: var(--r-control);
  background: var(--surface-2);
  gap: 2px;
  width: 100%;
}
.segmented button {
  flex: 1;
  min-height: 32px;
  padding: 0 var(--s3);
  border: 0;
  border-radius: 10px;
  background: none;
  color: var(--ink-2);
  font-size: var(--t-footnote);
  font-weight: 600;
  white-space: nowrap;
  transition: background .16s var(--ease), color .16s linear;
}
.segmented button[aria-pressed="true"] {
  background: var(--surface);
  color: var(--ink);
  box-shadow: 0 1px 3px rgba(0, 0, 0, .1);
}

/* Горизонтальная лента фильтров — прокручивается, не переносится. */
.chips {
  display: flex;
  gap: var(--s2);
  overflow-x: auto;
  scrollbar-width: none;
  padding-bottom: var(--s1);
  margin: 0 calc(var(--s4) * -1);
  padding-left: var(--s4);
  padding-right: var(--s4);
}
.chips::-webkit-scrollbar { display: none; }
.chip {
  flex: none;
  padding: 6px var(--s3);
  border: 0;
  border-radius: var(--r-pill);
  background: var(--surface);
  color: var(--ink-2);
  font-size: var(--t-footnote);
  font-weight: 600;
  white-space: nowrap;
}
.chip[aria-pressed="true"] { background: var(--accent); color: #fff; }

/* ── Поля ───────────────────────────────────────────────────────────── */
.field { margin-bottom: var(--s3); }
.field > label {
  display: block;
  margin-bottom: 6px;
  font-size: var(--t-footnote);
  font-weight: 500;
  color: var(--ink-2);
}
.input, .select, .textarea {
  width: 100%;
  min-height: 44px;
  padding: 10px var(--s3);
  border: .5px solid var(--hairline-strong);
  border-radius: var(--r-control);
  background: var(--surface);
  color: var(--ink);
  font-size: var(--t-callout);
  /* 16px+ обязательны: Safari на iOS зумит страницу при фокусе в поле с
   * меньшим шрифтом, и вернуть масштаб потом нечем. */
  transition: border-color .15s linear, box-shadow .15s linear;
}
.input:focus, .select:focus, .textarea:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-ring);
}
.textarea { min-height: 88px; resize: vertical; line-height: var(--lh-body); }
.input.num { font-variant-numeric: tabular-nums; text-align: right; }
.select {
  appearance: none;
  background-image: linear-gradient(45deg, transparent 50%, var(--ink-3) 50%),
                    linear-gradient(135deg, var(--ink-3) 50%, transparent 50%);
  background-position: calc(100% - 17px) 20px, calc(100% - 12px) 20px;
  background-size: 5px 5px, 5px 5px;
  background-repeat: no-repeat;
  padding-right: var(--s7);
}
.hint { margin-top: 6px; font-size: var(--t-footnote); color: var(--ink-3); }
.hint.error { color: var(--expense); }

.switch { display: flex; align-items: center; justify-content: space-between; gap: var(--s4); }

/* ── Состояния ──────────────────────────────────────────────────────── */
.skel {
  background: linear-gradient(90deg, var(--surface-2) 25%, var(--surface-3) 37%, var(--surface-2) 63%);
  background-size: 400% 100%;
  animation: shimmer 1.3s ease-in-out infinite;
  border-radius: var(--r-row);
}
.skel.row-skel { height: 46px; margin-bottom: var(--s2); }
.skel.block { height: 92px; margin-bottom: var(--s3); }
@keyframes shimmer { from { background-position: 100% 0; } to { background-position: 0 0; } }

.empty {
  padding: var(--s7) var(--s4);
  text-align: center;
  color: var(--ink-2);
}
.empty .big { font-size: var(--t-title3); font-weight: 600; color: var(--ink); margin-bottom: 6px; }
.empty .btn { margin-top: var(--s4); }

.error-box {
  padding: var(--s4);
  border-radius: var(--r-card);
  background: var(--expense-soft);
  color: var(--expense);
  font-size: var(--t-subhead);
}

.muted { color: var(--ink-3); }
.tiny  { font-size: var(--t-footnote); }
.nowrap { white-space: nowrap; }
.right { text-align: right; }
.stack { display: grid; gap: var(--s3); }
.rowline { display: flex; align-items: center; gap: var(--s3); }
.spread { display: flex; align-items: center; justify-content: space-between; gap: var(--s3); }
.grow { flex: 1; min-width: 0; }

/* ── Всплывающее уведомление ────────────────────────────────────────── */
#toast {
  position: fixed;
  left: 50%;
  bottom: calc(var(--tabbar-h) + var(--safe-b) + var(--s4));
  z-index: 60;
  transform: translate(-50%, 20px);
  max-width: min(420px, calc(100vw - var(--s7)));
  padding: 11px var(--s4);
  border-radius: var(--r-pill);
  background: var(--ink);
  color: var(--canvas);
  font-size: var(--t-subhead);
  font-weight: 500;
  text-align: center;
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s linear, transform .26s var(--ease);
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }
#toast.bad { background: var(--expense); color: #fff; }

@media (min-width: 900px) {
  #toast { bottom: var(--s5); }
}

/* ── Модальный лист ─────────────────────────────────────────────────────
 * На телефоне выезжает снизу (как системный sheet), на десктопе — окно по
 * центру. Один компонент, две подачи: разные компоненты разъезжаются. */
#sheet-host {
  position: fixed;
  inset: 0;
  z-index: 70;
  display: none;
}
#sheet-host.open { display: block; }
.sheet-backdrop {
  position: absolute;
  inset: 0;
  background: var(--overlay);
  -webkit-backdrop-filter: blur(2px);
  backdrop-filter: blur(2px);
  animation: fade .2s linear;
}
.sheet {
  position: absolute;
  left: 0; right: 0; bottom: 0;
  max-height: 92dvh;
  display: flex;
  flex-direction: column;
  background: var(--canvas);
  border-radius: 20px 20px 0 0;
  animation: slide-up .32s var(--ease);
  padding-bottom: var(--safe-b);
}
.sheet-head {
  display: flex;
  align-items: center;
  gap: var(--s3);
  padding: var(--s4);
  border-bottom: .5px solid var(--hairline);
}
.sheet-title { flex: 1; font-size: var(--t-headline); font-weight: 600; text-align: center; }
.sheet-body { flex: 1; overflow-y: auto; padding: var(--s4); -webkit-overflow-scrolling: touch; }
.sheet-foot { padding: var(--s3) var(--s4) var(--s4); border-top: .5px solid var(--hairline); }
.sheet-grab {
  width: 36px; height: 5px;
  margin: var(--s2) auto 0;
  border-radius: 3px;
  background: var(--hairline-strong);
}
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }
@keyframes slide-up { from { transform: translateY(100%); } to { transform: none; } }

@media (min-width: 700px) {
  .sheet {
    top: 50%; left: 50%; right: auto; bottom: auto;
    transform: translate(-50%, -50%);
    width: min(560px, calc(100vw - var(--s7)));
    max-height: 84dvh;
    border-radius: 18px;
    box-shadow: var(--shadow-float);
    animation: pop .22s var(--ease);
    padding-bottom: 0;
  }
  .sheet-grab { display: none; }
  @keyframes pop {
    from { transform: translate(-50%, -46%) scale(.97); opacity: 0; }
    to { transform: translate(-50%, -50%) scale(1); opacity: 1; }
  }
}

/* ── Таблицы ────────────────────────────────────────────────────────────
 * Широкая таблица прокручивается ВНУТРИ своего блока. Горизонтальный скролл
 * всей страницы на телефоне ломает вертикальный жест и ощущается поломкой. */
.table-wrap {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  border-radius: var(--r-card);
  background: var(--surface);
}
table.grid {
  width: 100%;
  border-collapse: collapse;
  font-size: var(--t-footnote);
}
table.grid th, table.grid td {
  padding: 9px var(--s3);
  text-align: right;
  white-space: nowrap;
  border-bottom: .5px solid var(--hairline);
}
table.grid th {
  position: sticky;
  top: 0;
  background: var(--surface-2);
  font-weight: 600;
  color: var(--ink-2);
  font-size: var(--t-caption);
  letter-spacing: .01em;
  z-index: 1;
}
table.grid th:first-child, table.grid td:first-child {
  text-align: left;
  position: sticky;
  left: 0;
  background: var(--surface);
  font-weight: 500;
  z-index: 2;
}
table.grid th:first-child { background: var(--surface-2); z-index: 3; }
table.grid tbody tr:last-child td { border-bottom: 0; }
table.grid tr.total td { font-weight: 700; background: var(--surface-2); }
table.grid td.num { font-variant-numeric: tabular-nums; }
table.grid td.pos { color: var(--income); }
table.grid td.neg { color: var(--expense); }
