/*
 * Peremen Mini App — компоненты и раскладка.
 * Правила: только rem/em (текст масштабируется до 200%), никаких бесконечных анимаций,
 * красный — только ошибка, цели касания не меньше --p-target-min (44 px).
 * Цвета здесь не задаются: только переменные --p-* из tokens.css (палитра меняется в одном файле).
 */

*, *::before, *::after { box-sizing: border-box; }

/* iOS: размер текста из системных настроек (Dynamic Type) в WebView Telegram. Не проверено на устройстве. */
@supports (font: -apple-system-body) and (-webkit-touch-callout: none) {
  html { font: -apple-system-body; }
}

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
  background: var(--p-bg);
}

body {
  margin: 0;
  background: var(--p-bg);
  color: var(--p-text);
  font-family: var(--p-font);
  font-size: var(--p-text-base);
  line-height: var(--p-leading-body);
  -webkit-font-smoothing: antialiased;
  /* Длинные русские слова при крупном тексте переносятся, а не вылезают за край. */
  overflow-wrap: break-word;
}

h1, h2, h3, p, ul, ol, dl, dd, figure { margin: 0; }
ul, ol { padding: 0; }
button, input, select { font: inherit; color: inherit; }
img, svg { display: block; }

a { color: var(--p-link); text-underline-offset: .18em; }

:focus-visible {
  outline: 3px solid var(--p-focus);
  outline-offset: 2px;
  border-radius: .25rem;
}
:focus:not(:focus-visible) { outline: none; }

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

[hidden] { display: none !important; }

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

.app {
  container: app / inline-size;
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
}

.page {
  width: 100%;
  max-width: calc(var(--p-content-max) + 2 * var(--p-page-gutter));
  margin: 0 auto;
  padding-left: max(var(--p-page-gutter), calc(var(--p-safe-left) + var(--p-content-safe-left)));
  padding-right: max(var(--p-page-gutter), calc(var(--p-safe-right) + var(--p-content-safe-right)));
}

.topbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: var(--p-space-2) var(--p-space-4);
  padding-top: calc(var(--p-safe-top) + var(--p-content-safe-top) + var(--p-space-2));
  padding-bottom: var(--p-space-2);
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: var(--p-space-2);
  min-height: var(--p-target-min);
  color: var(--p-text);
  font-size: var(--p-text-lg);
  font-weight: var(--p-weight-strong);
  text-decoration: none;
}
.brand-mark { width: 1.75rem; height: 1.75rem; flex: none; }
.brand-word { font-family: var(--p-font-head); font-size: var(--p-brand-word); font-weight: 700; line-height: 1; letter-spacing: normal; }

.topbar-link {
  display: inline-flex;
  align-items: center;
  gap: var(--p-space-2);
  min-height: var(--p-target-min);
  padding: 0 var(--p-space-1);
  font-size: var(--p-text-sm);
  font-weight: var(--p-weight-medium);
  text-decoration: none;
}
.topbar-link svg { width: 1.25rem; height: 1.25rem; flex: none; }

main { flex: 1 0 auto; outline: none; padding-bottom: var(--p-space-6); }

.view { display: flex; flex-direction: column; gap: var(--p-space-6); }

/* ---------- Заголовки экрана ---------- */

.screen-head { display: flex; flex-direction: column; gap: var(--p-space-1); padding-top: var(--p-space-2); }
.eyebrow { color: var(--p-muted); font-size: var(--p-text-sm); }
/* Caveat — только крупные заголовки экранов (≥ 24 px), слово Peremen и иллюстративные пустые состояния. */
.screen-title {
  font-family: var(--p-font-head);
  font-size: var(--p-title-head);
  line-height: var(--p-leading-head);
  font-weight: 700;
  letter-spacing: normal;
  overflow-wrap: anywhere;
  outline: none;
}
.screen-title.is-long { font-size: var(--p-title-head-long); font-weight: 600; }
.lead { color: var(--p-muted); }

.back-link {
  align-self: flex-start;
  display: inline-flex;
  align-items: center;
  gap: var(--p-space-1);
  min-height: var(--p-target-min);
  margin-left: calc(-1 * var(--p-space-1));
  padding: 0 var(--p-space-2) 0 var(--p-space-1);
  font-weight: var(--p-weight-medium);
  text-decoration: none;
}
.back-link svg { width: 1.25rem; height: 1.25rem; flex: none; }

.section { display: flex; flex-direction: column; gap: var(--p-space-3); }
.section-title {
  font-size: var(--p-text-lg);
  line-height: var(--p-leading-title);
  font-weight: var(--p-weight-strong);
}
.section-note { color: var(--p-muted); font-size: var(--p-text-sm); }

/* ---------- Списки и строки ---------- */

.list {
  list-style: none;
  background: var(--p-surface);
  border: 1px solid var(--p-line);
  border-radius: var(--p-radius-card);
  overflow: hidden;
}
.list > li + li { border-top: 1px solid var(--p-line); }

.row {
  display: flex;
  align-items: center;
  gap: var(--p-space-3);
  min-height: var(--p-target-min);
  padding: var(--p-space-3) var(--p-space-4);
  color: var(--p-text);
  text-decoration: none;
}
a.row { transition: background-color var(--p-duration-fast) var(--p-ease); }
a.row:hover { background: var(--p-row-hover); }
a.row:focus-visible { outline-offset: -3px; border-radius: var(--p-radius-card); }

.row-lead {
  flex: none;
  min-width: 3.25rem;
  font-variant-numeric: tabular-nums;
  font-weight: var(--p-weight-medium);
}
.row-body { flex: 1 1 auto; min-width: 0; display: flex; flex-direction: column; gap: .125rem; }
.row-title { overflow-wrap: anywhere; }
.row-meta { color: var(--p-muted); font-size: var(--p-text-sm); overflow-wrap: anywhere; }
.row-trail { flex: none; margin-left: auto; text-align: right; font-variant-numeric: tabular-nums; font-weight: var(--p-weight-medium); }
.row-chevron { flex: none; width: 1.25rem; height: 1.25rem; color: var(--p-muted); }
.row-snippet {
  overflow-wrap: anywhere;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 3;
  line-clamp: 3;
  overflow: hidden;
}

.link-row { font-weight: var(--p-weight-medium); color: var(--p-link); }
.link-row .row-chevron { color: currentColor; }

/* Узкий контейнер или крупный текст: колонка времени уходит над названием. */
@container app (max-width: 18em) {
  .row.has-lead { flex-wrap: wrap; row-gap: 0; }
  .row.has-lead .row-lead { flex-basis: 100%; min-width: 0; }
  .row.has-lead .row-chevron { display: none; }
  .row-trail { flex-basis: 100%; text-align: left; }
}

/* ---------- Кнопки ---------- */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--p-space-2);
  min-height: var(--p-target-min);
  min-width: var(--p-target-min);
  padding: var(--p-space-2) var(--p-space-4);
  border: 1px solid transparent;
  border-radius: var(--p-radius-control);
  background: transparent;
  color: var(--p-link);
  font-weight: var(--p-weight-medium);
  line-height: 1.3;
  text-align: center;
  text-decoration: none;
  cursor: pointer;
  transition: background-color var(--p-duration-fast) var(--p-ease), border-color var(--p-duration-fast) var(--p-ease);
}
.btn svg { width: 1.25rem; height: 1.25rem; flex: none; }
.btn-primary { background: var(--p-action); color: var(--p-on-action); }
.btn-primary:hover { background: var(--p-action-hover); }
.btn-secondary { border-color: var(--p-control-border); color: var(--p-text); background: var(--p-surface); }
.btn-secondary:hover { background: var(--p-control-hover); }
.btn-quiet { padding-left: var(--p-space-2); padding-right: var(--p-space-2); }
.btn-quiet:hover { background: var(--p-quiet-hover); }
.btn-block { width: 100%; }
.btn:disabled { cursor: default; opacity: 1; }

.actions { display: flex; flex-wrap: wrap; gap: var(--p-space-3); align-items: center; }

/* ---------- Возврат в чат ---------- */

.chat-return { display: flex; flex-direction: column; gap: var(--p-space-2); padding-top: var(--p-space-8); }
.chat-hint { color: var(--p-muted); font-size: var(--p-text-sm); }

/* ---------- Нижняя навигация ---------- */

.tabbar {
  position: sticky;
  bottom: 0;
  z-index: 2;
  background: var(--p-secondary-bg);
  border-top: 1px solid var(--p-line);
  padding-bottom: calc(var(--p-safe-bottom) + var(--p-content-safe-bottom));
}
.tabbar ul {
  list-style: none;
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  max-width: calc(var(--p-content-max) + 2 * var(--p-page-gutter));
  margin: 0 auto;
  padding: var(--p-space-1) max(var(--p-space-2), var(--p-safe-left)) var(--p-space-1) max(var(--p-space-2), var(--p-safe-right));
}
.tab {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: .125rem;
  min-height: 3.25rem;
  padding: var(--p-space-1) var(--p-space-1);
  border-radius: var(--p-radius-control);
  color: var(--p-muted);
  font-size: var(--p-text-sm);
  line-height: 1.25;
  text-align: center;
  text-decoration: none;
  hyphens: auto;
  overflow-wrap: anywhere;
}
.tab svg { width: 1.5rem; height: 1.5rem; flex: none; }
.tab[aria-current="page"] { color: var(--p-text); font-weight: var(--p-weight-strong); }
.tab[aria-current="page"] svg { color: var(--p-brand); }
.tab[aria-current="page"] .tab-label {
  text-decoration: underline;
  text-decoration-thickness: 2px;
  text-underline-offset: .3em;
  text-decoration-color: var(--p-brand);
}
.tab:focus-visible { outline-offset: -3px; }

/* Крупный текст на узком экране: навигация перестаёт прилипать и не закрывает содержание. */
@container app (max-width: 18em) {
  .tabbar { position: static; }
  .tabbar ul { display: flex; flex-wrap: wrap; }
  .tabbar li { flex: 1 1 auto; }
  .tab { flex-direction: row; gap: var(--p-space-2); }
  .tab svg { width: 1.25rem; height: 1.25rem; }
}

/* ---------- Плашки состояния ---------- */

.notice {
  display: flex;
  gap: var(--p-space-3);
  align-items: flex-start;
  padding: var(--p-space-3) var(--p-space-4);
  border-radius: var(--p-radius-card);
  background: var(--p-brand-soft);
  color: var(--p-text);
  font-size: var(--p-text-sm);
}
.notice > svg { width: 1.25rem; height: 1.25rem; flex: none; margin-top: .125rem; color: var(--p-text); }
.notice-body { flex: 1 1 auto; min-width: 0; display: flex; flex-direction: column; gap: var(--p-space-2); }
.notice strong { font-weight: var(--p-weight-strong); }
.notice .btn { align-self: flex-start; margin-left: calc(-1 * var(--p-space-2)); }
.notice-plain { background: transparent; border: 1px solid var(--p-line); }

.banners { display: flex; flex-direction: column; gap: var(--p-space-3); }
.banners:empty { display: none; }
.banners + .view { margin-top: var(--p-space-4); }

/* ---------- Пустое состояние, ошибка, загрузка ---------- */

.state {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: var(--p-space-3);
  padding: var(--p-space-6) var(--p-space-5);
  border: 1px solid var(--p-line);
  border-radius: var(--p-radius-card);
  background: var(--p-surface);
}
.state-mark { width: 2.25rem; height: 3rem; color: var(--p-muted); }
.state-title { font-size: var(--p-text-lg); line-height: var(--p-leading-title); font-weight: var(--p-weight-strong); }
.state-empty .state-title { font-family: var(--p-font-head); font-size: var(--p-title-head-long); line-height: var(--p-leading-head); font-weight: 600; }
.state-text { color: var(--p-muted); max-width: 34rem; }
.state .actions { margin-top: var(--p-space-1); }
.state-icon { width: 1.5rem; height: 1.5rem; }
.state-error .state-icon { color: var(--p-danger); }
.state-error { border-color: var(--p-error-border); }

.inline-empty { color: var(--p-muted); }

.placeholder-list { list-style: none; background: var(--p-surface); border: 1px solid var(--p-line); border-radius: var(--p-radius-card); }
.placeholder-list > li { display: flex; flex-direction: column; gap: var(--p-space-2); padding: var(--p-space-4); }
.placeholder-list > li + li { border-top: 1px solid var(--p-line); }
.ph { display: block; height: .75rem; border-radius: .375rem; background: var(--p-placeholder); }
.ph-title { width: 62%; height: .95rem; }
.ph-meta { width: 38%; }
.ph-text { width: 90%; }
.ph-short { width: 45%; }
.status-line { display: flex; align-items: center; gap: var(--p-space-2); color: var(--p-muted); font-size: var(--p-text-sm); }

/* ---------- Детали ---------- */

.card {
  background: var(--p-surface);
  border: 1px solid var(--p-line);
  border-radius: var(--p-radius-card);
  padding: var(--p-space-4) var(--p-space-4);
}
.details { display: grid; grid-template-columns: minmax(0, max-content) minmax(0, 1fr); gap: var(--p-space-3) var(--p-space-4); }
.details dt { color: var(--p-muted); }
.details dd { overflow-wrap: anywhere; font-variant-numeric: tabular-nums; }
@container app (max-width: 20em) {
  .details { grid-template-columns: minmax(0, 1fr); gap: 0; }
  .details dd + dt { margin-top: var(--p-space-3); }
}

.note-text { white-space: pre-wrap; overflow-wrap: anywhere; font-size: var(--p-text-lg); line-height: 1.6; }
.note-meta { color: var(--p-muted); font-size: var(--p-text-sm); }
.tags { display: flex; flex-wrap: wrap; gap: var(--p-space-2); list-style: none; }
.tag { padding: .125rem var(--p-space-3); border: 1px solid var(--p-line); border-radius: 999px; font-size: var(--p-text-sm); color: var(--p-text); }

.task-status { display: flex; gap: var(--p-space-3); align-items: flex-start; }
.task-status svg { width: 1.5rem; height: 1.5rem; flex: none; color: var(--p-brand); }

.command {
  font-variant-numeric: tabular-nums;
  font-weight: var(--p-weight-medium);
  padding: .05em .35em;
  border-radius: .375rem;
  background: var(--p-brand-soft);
  user-select: all;
  -webkit-user-select: all;
  white-space: nowrap;
}

/* Подтверждение действия: локальная замена статуса за 140–180 мс. */
.confirm-in { animation: confirm-in var(--p-duration-normal) var(--p-ease) both; }
@keyframes confirm-in { from { opacity: 0; transform: translateY(.25rem); } to { opacity: 1; transform: none; } }
@media (prefers-reduced-motion: reduce) {
  .confirm-in { animation: none; }
  * { scroll-behavior: auto !important; transition-duration: 0ms !important; }
}

/* ---------- Поиск ---------- */

.search { display: flex; flex-direction: column; gap: var(--p-space-2); }
.search label { font-weight: var(--p-weight-medium); }
.search-row { display: flex; flex-wrap: wrap; gap: var(--p-space-2); }
.input {
  flex: 1 1 12rem;
  min-width: 0;
  min-height: var(--p-target-min);
  padding: var(--p-space-2) var(--p-space-3);
  border: 1px solid var(--p-control-border);
  border-radius: var(--p-radius-control);
  background: var(--p-secondary-bg);
  color: var(--p-text);
  font-size: max(1rem, 16px);
}
.input::placeholder { color: var(--p-muted); opacity: 1; }
.input:focus-visible { outline-offset: 1px; border-radius: var(--p-radius-control); }
.search-row .btn { flex: 0 0 auto; }

/* ---------- Выбор периода ---------- */

.segmented { border: 0; margin: 0; padding: 0; min-width: 0; }
.segmented legend { padding: 0; margin-bottom: var(--p-space-2); font-weight: var(--p-weight-medium); }
.segmented-options { display: flex; flex-wrap: wrap; gap: var(--p-space-2); }
.segmented input { position: absolute; opacity: 0; width: 1px; height: 1px; margin: 0; }
.segmented label {
  display: inline-flex;
  align-items: center;
  gap: var(--p-space-1);
  min-height: var(--p-target-min);
  padding: var(--p-space-2) var(--p-space-4);
  border: 1px solid var(--p-control-border);
  border-radius: 999px;
  background: var(--p-surface);
  cursor: pointer;
}
.segmented label svg { width: 1.125rem; height: 1.125rem; display: none; }
.segmented input:checked + label { background: var(--p-brand-soft); border-color: var(--p-brand); font-weight: var(--p-weight-strong); }
.segmented input:checked + label svg { display: block; }
.segmented input:focus-visible + label { outline: 3px solid var(--p-focus); outline-offset: 2px; }

/* ---------- Деньги ---------- */

.totals { display: flex; flex-direction: column; gap: var(--p-space-1); }
.totals-label { color: var(--p-muted); font-size: var(--p-text-sm); }
.money-lg { font-size: var(--p-title-sm); line-height: var(--p-leading-title); font-weight: var(--p-weight-strong); font-variant-numeric: tabular-nums; overflow-wrap: anywhere; }
.money-lg + .money-lg { font-size: var(--p-text-lg); }

.budget { display: flex; flex-direction: column; gap: var(--p-space-2); padding: var(--p-space-4); }
.budget-head { display: flex; flex-wrap: wrap; justify-content: space-between; gap: var(--p-space-1) var(--p-space-3); }
.budget-name { font-weight: var(--p-weight-strong); overflow-wrap: anywhere; }
.budget-main { font-variant-numeric: tabular-nums; }
.bar { position: relative; height: .5rem; border-radius: 999px; background: var(--p-track); overflow: hidden; }
.bar-fill { position: absolute; inset: 0 auto 0 0; width: 0; border-radius: inherit; background: var(--p-brand); }
.budget-meta { color: var(--p-muted); font-size: var(--p-text-sm); font-variant-numeric: tabular-nums; }

/* ---------- Режим разработчика ---------- */

.devpanel { margin-top: var(--p-space-2); border: 1px dashed var(--p-control-border); border-radius: var(--p-radius-card); font-size: var(--p-text-sm); }
.devpanel summary { min-height: var(--p-target-min); display: flex; align-items: center; padding: 0 var(--p-space-4); cursor: pointer; font-weight: var(--p-weight-medium); }
.devpanel-body { display: flex; flex-direction: column; gap: var(--p-space-4); padding: 0 var(--p-space-4) var(--p-space-4); }
.devpanel fieldset { border: 0; margin: 0; padding: 0; min-width: 0; }
.devpanel legend, .devpanel .dev-label { padding: 0; margin-bottom: var(--p-space-1); color: var(--p-muted); }
.dev-options { display: flex; flex-wrap: wrap; gap: var(--p-space-1) var(--p-space-2); }
.dev-options label { display: inline-flex; align-items: center; gap: var(--p-space-1); min-height: var(--p-target-min); padding-right: var(--p-space-2); cursor: pointer; }
.dev-options input { width: 1.125rem; height: 1.125rem; margin: 0; accent-color: var(--p-brand); }
.devpanel select { min-height: var(--p-target-min); width: 100%; padding: 0 var(--p-space-3); border: 1px solid var(--p-control-border); border-radius: var(--p-radius-control); background: var(--p-secondary-bg); font-size: max(1rem, 16px); }
.dev-meta { color: var(--p-muted); }

.row-icon { flex: none; width: 1.5rem; height: 1.5rem; color: var(--p-brand); }
.link-row .row-icon { color: currentColor; }

[data-dev-slot]:not(:empty) { margin-bottom: var(--p-space-4); }
[data-region] > .lead:first-child { margin-top: calc(-1 * var(--p-space-4)); }
.state .actions > .btn-quiet:first-child { margin-left: calc(-1 * var(--p-space-2)); }

/* Крупный текст на узком экране: иконки плашек уступают место словам. */
@container app (max-width: 19em) {
  .page, .tabbar { hyphens: auto; }
  /* Рукописный Caveat с переносом по слогам читается хуже — переносим только целыми словами. */
  .screen-title, .state-title, .brand-word { hyphens: manual; }
  .state { padding: var(--p-space-4) var(--p-space-3); }
  .state-mark { display: none; }
}
@container app (max-width: 18em) {
  .notice > svg { display: none; }
  .notice { padding-left: var(--p-space-3); padding-right: var(--p-space-3); }
}
[data-dev-note]:empty { display: none; }
