/* Палитра берётся из темы Telegram, поэтому приложение выглядит родным
   и в светлой, и в тёмной. Фолбэки нужны для открытия в обычном браузере. */
:root {
  --bg: var(--tg-theme-secondary-bg-color, #f2f2f7);
  --surface: var(--tg-theme-bg-color, #ffffff);
  --text: var(--tg-theme-text-color, #14161a);
  --muted: var(--tg-theme-hint-color, #7d8894);
  --accent: var(--tg-theme-button-color, #2f7cf6);
  --accent-text: var(--tg-theme-button-text-color, #ffffff);
  --link: var(--tg-theme-link-color, #2f7cf6);
  --positive: #1fa971;
  --negative: #e0503a;
  --line: color-mix(in srgb, var(--muted) 22%, transparent);
  --radius: 14px;
  --gap: 12px;
}

/* Если Telegram цвета не передал (открыли в браузере), опираемся на
   системную тему - иначе в тёмном клиенте страница слепила бы белым. */
@media (prefers-color-scheme: dark) {
  :root {
    --bg: var(--tg-theme-secondary-bg-color, #0e1116);
    --surface: var(--tg-theme-bg-color, #171d24);
    --text: var(--tg-theme-text-color, #eaeff5);
    --muted: var(--tg-theme-hint-color, #8b97a5);
    --accent: var(--tg-theme-button-color, #4a90f0);
    --positive: #35c48b;
    --negative: #ef6a52;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

/* Экраны и шторка скрываются атрибутом hidden, а собственный display их
   правил перебивает браузерное [hidden]{display:none} - без этой строки
   скрытые блоки остаются на экране и перехватывают нажатия. */
[hidden] { display: none !important; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  padding-bottom: calc(64px + env(safe-area-inset-bottom));
}

h1, h2, h3 { margin: 0; font-weight: 650; letter-spacing: -0.01em; }

/* -- загрузка и фатальная ошибка ---------------------------------------- */

.boot, .fatal {
  min-height: 70vh;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 14px; padding: 24px; text-align: center;
}
.boot-spinner {
  width: 26px; height: 26px; border-radius: 50%;
  border: 2.5px solid var(--line); border-top-color: var(--accent);
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.boot-text, .fatal-text { color: var(--muted); margin: 0; max-width: 34ch; }
.fatal-title { font-size: 19px; }

/* -- шапка --------------------------------------------------------------- */

.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  padding: 10px 14px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
}
.cabinet-switch {
  display: inline-flex; align-items: center; gap: 6px;
  background: none; border: 0; padding: 4px 0;
  color: var(--text); font-size: 17px; font-weight: 650; letter-spacing: -0.01em;
  cursor: pointer; max-width: 60vw;
}
.cabinet-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chevron { color: var(--muted); font-size: 12px; }
.sub-badge {
  font-size: 12px; font-weight: 600; padding: 4px 9px; border-radius: 999px;
  background: color-mix(in srgb, var(--accent) 14%, transparent); color: var(--accent);
  white-space: nowrap;
}
.sub-badge.is-warning { background: color-mix(in srgb, var(--negative) 14%, transparent); color: var(--negative); }

/* -- периоды ------------------------------------------------------------- */

.periods {
  display: flex; gap: 7px; padding: 11px 14px 3px;
  overflow-x: auto; scrollbar-width: none;
}
.periods::-webkit-scrollbar { display: none; }
.chip {
  flex: 0 0 auto; border: 0; cursor: pointer;
  padding: 7px 13px; border-radius: 999px;
  background: var(--surface); color: var(--muted);
  font-size: 13.5px; font-weight: 550;
}
.chip.is-active { background: var(--accent); color: var(--accent-text); }

.custom-range {
  display: flex; gap: 8px; align-items: center; flex-wrap: wrap;
  padding: 10px 14px 0;
}
.custom-range label { display: inline-flex; align-items: center; gap: 6px; color: var(--muted); font-size: 13px; }
.custom-range input {
  border: 1px solid var(--line); background: var(--surface); color: var(--text);
  border-radius: 9px; padding: 7px 9px; font-size: 14px; font-family: inherit;
}

/* -- экраны и карточки --------------------------------------------------- */

.screen { padding: 14px; display: flex; flex-direction: column; gap: var(--gap); }

.freshness { color: var(--muted); font-size: 12.5px; min-height: 16px; }
.freshness button {
  background: none; border: 0; color: var(--link); font: inherit; font-size: 12.5px;
  cursor: pointer; padding: 0 0 0 6px; text-decoration: underline;
}

.card { background: var(--surface); border-radius: var(--radius); padding: 14px; }
.card-head { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; }
.card-title { font-size: 15px; }
.card-note { color: var(--muted); font-size: 12px; text-align: right; }
.hint { color: var(--muted); font-size: 12.5px; margin: 6px 0 10px; }

/* -- KPI ----------------------------------------------------------------- */

.kpi-grid { display: grid; grid-template-columns: 1fr 1fr; gap: var(--gap); }
.kpi {
  background: var(--surface); border-radius: var(--radius); padding: 13px 14px;
  display: flex; flex-direction: column; gap: 3px; min-width: 0;
}
.kpi-primary, .kpi-wide { grid-column: 1 / -1; }
.kpi-label { color: var(--muted); font-size: 12.5px; }
.kpi-value {
  font-size: 22px; font-weight: 700; letter-spacing: -0.02em;
  font-variant-numeric: tabular-nums; overflow-wrap: anywhere;
}
.kpi-primary .kpi-value { font-size: 30px; }
.kpi-value.is-negative { color: var(--negative); }
.kpi-delta, .kpi-sub { font-size: 12.5px; color: var(--muted); font-variant-numeric: tabular-nums; }
.kpi-delta.up { color: var(--positive); }
.kpi-delta.down { color: var(--negative); }

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

.chart-wrap { margin-top: 10px; }
#chart { width: 100%; height: 152px; display: block; overflow: visible; }
.bar { fill: var(--accent); opacity: 0.85; }
.bar.is-negative { fill: var(--negative); }
.axis-label { fill: var(--muted); font-size: 9.5px; }
.zero-line { stroke: var(--line); stroke-width: 1; }

/* -- разбивка расходов --------------------------------------------------- */

.breakdown { list-style: none; margin: 8px 0 0; padding: 0; display: flex; flex-direction: column; }
.breakdown li {
  display: flex; justify-content: space-between; align-items: baseline; gap: 10px;
  padding: 9px 0; border-bottom: 1px solid var(--line); font-size: 14px;
}
.breakdown li:last-child { border-bottom: 0; }
.breakdown li.is-total { font-weight: 700; padding-top: 12px; }
.breakdown .name { color: var(--muted); }
.breakdown li.is-total .name { color: var(--text); }
.breakdown .value { font-variant-numeric: tabular-nums; white-space: nowrap; }
.breakdown .value.is-negative { color: var(--negative); }

.warnings { list-style: none; margin: 8px 0 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.warnings li { font-size: 13px; color: var(--muted); padding-left: 16px; position: relative; }
.warnings li::before { content: "•"; position: absolute; left: 4px; color: var(--negative); }

/* -- товары -------------------------------------------------------------- */

.list-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.products { display: flex; flex-direction: column; gap: 8px; }
.product {
  background: var(--surface); border-radius: var(--radius); padding: 12px 14px;
  display: flex; flex-direction: column; gap: 5px;
}
.product-top { display: flex; justify-content: space-between; gap: 10px; align-items: baseline; }
.product-article { font-weight: 600; font-size: 14px; overflow-wrap: anywhere; }
.product-revenue { font-variant-numeric: tabular-nums; font-weight: 650; white-space: nowrap; }
.product-meta { color: var(--muted); font-size: 12.5px; display: flex; gap: 10px; flex-wrap: wrap; }
.product-margin.up { color: var(--positive); }
.product-margin.down { color: var(--negative); }

/* -- расходы ------------------------------------------------------------- */

.expenses { display: flex; flex-direction: column; gap: 8px; }
/* Названия расходов бывают длинными, поэтому строка складывается в два
   яруса: название сверху во всю ширину, сумма и действия под ним. */
.expense {
  background: var(--surface); border-radius: var(--radius); padding: 12px 14px;
  display: flex; flex-direction: column; gap: 8px;
}
.expense-bottom { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.expense-main { min-width: 0; }
.expense-name { font-weight: 600; font-size: 14px; overflow-wrap: anywhere; }
.expense-meta { color: var(--muted); font-size: 12px; }
.expense-amount { font-variant-numeric: tabular-nums; font-weight: 650; white-space: nowrap; }
.expense-actions { display: flex; gap: 6px; }

/* -- себестоимость и факты ---------------------------------------------- */

.costs { display: flex; flex-direction: column; margin-top: 8px; max-height: 320px; overflow-y: auto; }
.cost-row {
  display: flex; justify-content: space-between; gap: 10px;
  padding: 8px 0; border-bottom: 1px solid var(--line); font-size: 13.5px;
}
.cost-row:last-child { border-bottom: 0; }
.cost-row .article { color: var(--muted); overflow-wrap: anywhere; }
.cost-row .amount { font-variant-numeric: tabular-nums; white-space: nowrap; }

.facts { margin: 8px 0 0; display: grid; grid-template-columns: auto 1fr; gap: 8px 14px; font-size: 13.5px; }
.facts dt { color: var(--muted); }
.facts dd { margin: 0; text-align: right; font-variant-numeric: tabular-nums; overflow-wrap: anywhere; }

/* -- элементы управления -------------------------------------------------- */

.row { display: flex; align-items: center; gap: 8px; }
.row input[type="number"] {
  flex: 1; min-width: 0; border: 1px solid var(--line); background: var(--bg);
  color: var(--text); border-radius: 9px; padding: 9px 11px; font-size: 15px; font-family: inherit;
}
.suffix { color: var(--muted); }

input[type="search"], .sheet input, .sheet select {
  width: 100%; border: 1px solid var(--line); background: var(--bg); color: var(--text);
  border-radius: 9px; padding: 10px 12px; font-size: 15px; font-family: inherit;
}

.btn {
  border: 0; border-radius: 10px; padding: 11px 16px; cursor: pointer;
  font: inherit; font-weight: 600; font-size: 15px;
  background: var(--bg); color: var(--text);
}
.btn-primary { background: var(--accent); color: var(--accent-text); }
.btn-small { padding: 7px 12px; font-size: 13.5px; border-radius: 9px; }
.btn-ghost { background: none; color: var(--link); padding: 6px 8px; }
.btn-danger { background: none; color: var(--negative); padding: 6px 8px; }
.btn[disabled] { opacity: 0.5; cursor: default; }

.empty { color: var(--muted); font-size: 13.5px; text-align: center; padding: 26px 14px; }

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

.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 30;
  display: grid; grid-template-columns: repeat(4, 1fr);
  background: var(--surface); border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom);
}
.tab {
  border: 0; background: none; cursor: pointer;
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  padding: 8px 4px 9px; color: var(--muted); font-size: 11px; font-weight: 550; font-family: inherit;
}
.tab-icon { font-size: 17px; line-height: 1; }
.tab.is-active { color: var(--accent); }

/* -- шторка -------------------------------------------------------------- */

.sheet { position: fixed; inset: 0; z-index: 40; display: flex; align-items: flex-end; }
.sheet-backdrop { position: absolute; inset: 0; background: rgba(0, 0, 0, 0.45); }
.sheet-body {
  position: relative; width: 100%; background: var(--surface);
  border-radius: 18px 18px 0 0; padding: 18px 16px calc(20px + env(safe-area-inset-bottom));
  max-height: 88vh; overflow-y: auto;
  animation: rise 0.22s ease-out;
}
@keyframes rise { from { transform: translateY(14px); opacity: 0.6; } }
.sheet-title { font-size: 17px; margin-bottom: 14px; }
.sheet .field { display: flex; flex-direction: column; gap: 6px; margin-bottom: 13px; }
.sheet .field span { color: var(--muted); font-size: 12.5px; }
.sheet-actions { display: flex; gap: 8px; margin-top: 6px; }
.sheet-actions .btn { flex: 1; }
.sheet-error { color: var(--negative); font-size: 13px; margin: 0 0 10px; }
.sheet-list { display: flex; flex-direction: column; gap: 2px; }
.sheet-option {
  display: flex; justify-content: space-between; align-items: center; gap: 10px;
  padding: 13px 4px; border: 0; background: none; color: var(--text);
  font: inherit; text-align: left; cursor: pointer; border-bottom: 1px solid var(--line);
}
.sheet-option:last-child { border-bottom: 0; }
.sheet-option.is-active { color: var(--accent); font-weight: 600; }
