* { margin: 0; padding: 0; box-sizing: border-box; }

:root {
  --bg: #f4f2f8;
  --surface: #ffffff;
  --text: #1f1b2e;
  --muted: #7a7590;
  --primary: #7b2cbf;
  --primary-dark: #5a189a;
  --green: #1b9e63;
  --red: #d63d5f;
  --border: #e6e2ef;
  --shadow: 0 1px 3px rgba(31, 27, 46, 0.08);
}

html { -webkit-text-size-adjust: 100%; } /* iOS: не увеличивать шрифт в альбомной ориентации */

body {
  font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  -webkit-tap-highlight-color: transparent; /* без серой вспышки при тапе */
}

header {
  background: linear-gradient(135deg, var(--primary-dark), var(--primary));
  color: #fff;
  padding: 16px 24px;
  box-shadow: var(--shadow);
}

.header-inner {
  max-width: 1200px;
  margin: 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 12px;
}

h1 { font-size: 22px; font-weight: 700; }

/* Название контрагента (продавца) под заголовком — грузится из /api/seller-info */
.seller-name { font-size: 13px; opacity: 0.85; margin-top: 2px; }

.header-controls { display: flex; gap: 8px; align-items: center; }

select {
  padding: 8px 12px;
  border-radius: 8px;
  border: none;
  font-size: 14px;
  background: rgba(255, 255, 255, 0.95);
  color: var(--text);
  cursor: pointer;
}

.custom-range { display: inline-flex; align-items: center; gap: 6px; }

.custom-range.hidden { display: none; }

.custom-range input[type="date"] {
  padding: 7px 10px;
  border-radius: 8px;
  border: none;
  font-size: 13px;
  background: rgba(255, 255, 255, 0.95);
  color: var(--text);
  font-family: inherit;
}

.range-sep { color: rgba(255, 255, 255, 0.7); }

.btn {
  padding: 8px 16px;
  border-radius: 8px;
  border: 1px solid rgba(255, 255, 255, 0.4);
  background: rgba(255, 255, 255, 0.12);
  color: #fff;
  font-size: 14px;
  cursor: pointer;
  transition: background 0.15s;
}

.btn:hover { background: rgba(255, 255, 255, 0.25); }
.btn:active { transform: scale(0.97); }

.btn.primary {
  background: #fff;
  color: var(--primary-dark);
  border: none;
  font-weight: 600;
}

.btn.primary:hover { background: #f0e8fa; }

.token-panel {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 20px 24px;
}

.token-inner { max-width: 1200px; margin: 0 auto; }

.token-inner label { font-weight: 600; font-size: 14px; display: block; margin-bottom: 8px; }

.token-row { display: flex; gap: 8px; }

.token-row input {
  flex: 1;
  padding: 10px 14px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 14px;
}

.token-panel .btn.primary { background: var(--primary); color: #fff; }
.token-panel .btn.primary:hover { background: var(--primary-dark); }

.hint { font-size: 12px; color: var(--muted); margin-top: 8px; }

.autosync-label {
  display: flex;
  gap: 8px;
  align-items: flex-start;
  font-size: 13px;
  color: var(--text);
  margin-top: 12px;
  cursor: pointer;
}

.autosync-label input { margin-top: 3px; }

/* Раскрывающаяся инструкция «где взять токен» */
.token-help {
  margin-top: 14px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg);
}
.token-help summary {
  cursor: pointer;
  padding: 12px 14px;
  font-weight: 600;
  font-size: 14px;
  color: var(--primary-dark);
  list-style: none;
}
.token-help summary::-webkit-details-marker { display: none; }
.token-help[open] summary { border-bottom: 1px solid var(--border); }
.token-help-body { padding: 12px 16px 14px; font-size: 13px; color: var(--text); line-height: 1.55; }
.token-help-body ol { margin: 0 0 0 18px; }
.token-help-body ol > li { margin-bottom: 7px; }
.token-help-body ul { margin: 6px 0 2px 18px; }
.token-help-body ul li { margin-bottom: 3px; color: var(--muted); }
.token-help-note {
  margin-top: 10px;
  padding: 9px 12px;
  background: rgba(27, 158, 99, 0.10);
  border-radius: 8px;
  font-size: 12.5px;
  color: var(--text);
}

/* Блок «где хранятся данные» — снимает страх вводить токен */
.token-safe {
  margin-top: 14px;
  border: 1px solid var(--border);
  border-left: 4px solid var(--green);
  border-radius: 10px;
  background: rgba(27, 158, 99, 0.06);
  padding: 12px 16px;
}
.token-safe-title { font-weight: 600; font-size: 14px; margin-bottom: 8px; }
.token-safe ul { margin: 0 0 0 18px; padding: 0; }
.token-safe li { font-size: 13px; color: var(--text); line-height: 1.55; margin-bottom: 7px; }
.token-safe li:last-child { margin-bottom: 0; }
.token-safe code {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 1px 5px;
  font-size: 12px;
}

.history-info {
  margin-top: 24px;
  font-size: 12px;
  color: var(--muted);
  text-align: center;
}

.status {
  max-width: 1200px;
  margin: 16px auto 0;
  padding: 12px 18px;
  border-radius: 10px;
  font-size: 14px;
}

.status.error { background: #fde8ed; color: var(--red); border: 1px solid #f6c4d0; }
.status.info { background: #ede4f7; color: var(--primary-dark); border: 1px solid #d9c8ee; }

main { max-width: 1200px; margin: 0 auto; padding: 24px; }

.demo-banner {
  background: #fff7e0;
  border: 1px solid #f0dfa8;
  color: #8a6d1a;
  border-radius: 10px;
  padding: 10px 16px;
  font-size: 13px;
  margin-bottom: 18px;
}

.cards {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
  gap: 14px;
  margin-bottom: 28px;
}

.card {
  background: var(--surface);
  border-radius: 14px;
  padding: 18px 20px;
  box-shadow: var(--shadow);
  border-left: 4px solid var(--muted);
}

.card.income { border-left-color: var(--green); }
.card.expense { border-left-color: var(--red); }
.card.total { border-left-color: var(--primary); background: linear-gradient(135deg, #faf7ff, #f1e8fc); }

.card-label { font-size: 13px; color: var(--muted); margin-bottom: 6px; }

.card-value { font-size: 24px; font-weight: 700; }

.card.income .card-value { color: var(--green); }
.card.expense .card-value { color: var(--red); }
.card.total .card-value { color: var(--primary-dark); font-size: 28px; }

.card-sub { font-size: 12px; color: var(--muted); margin-top: 4px; }

/* Сравнение с прошлым периодом */
.card-delta { margin-top: 6px; font-size: 12px; }

.delta { font-weight: 700; }
.delta.good { color: var(--green); }
.delta.bad { color: var(--red); }

.delta-hint { color: var(--muted); font-size: 11px; }

/* Карточки показателей эффективности */
.card.metric { border-left-color: var(--primary); }
.card.metric .card-value { color: var(--primary-dark); }

.tax-sub input {
  width: 54px;
  padding: 3px 6px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 13px;
  font-family: inherit;
}

.op-section { margin-bottom: 28px; }

.op-section h2 { font-size: 16px; margin-bottom: 12px; }

.op-cards { margin-bottom: 0; }

.card.op { border-left-color: var(--primary); }
.card.op .card-value { color: var(--primary-dark); }

.op-note {
  background: #ede4f7;
  color: var(--primary-dark);
  border: 1px solid #d9c8ee;
  border-radius: 10px;
  padding: 10px 16px;
  font-size: 13px;
  margin-bottom: 12px;
}

.op-note.hidden { display: none; }

.charts {
  display: grid;
  grid-template-columns: 2fr 1fr;
  gap: 14px;
  margin-bottom: 28px;
}

.chart-box {
  background: var(--surface);
  border-radius: 14px;
  padding: 20px;
  box-shadow: var(--shadow);
  min-width: 0; /* иначе грид-ячейка не даёт канвасу Chart.js ужиматься при сужении экрана */
}

.chart-box h2 { font-size: 16px; margin-bottom: 14px; }

/* Высота графиков задаётся контейнером (maintainAspectRatio: false),
   иначе на узком экране Chart.js сплющивает график до нечитаемого */
.chart-wrap { position: relative; height: 320px; }

.table-section {
  background: var(--surface);
  border-radius: 14px;
  padding: 20px;
  box-shadow: var(--shadow);
}

.table-section h2 { font-size: 16px; margin-bottom: 14px; }

table { width: 100%; border-collapse: collapse; font-size: 14px; }

th, td { text-align: left; padding: 10px 12px; border-bottom: 1px solid var(--border); }

th { color: var(--muted); font-weight: 600; font-size: 12px; text-transform: uppercase; letter-spacing: 0.4px; }

/* Значок «?» с всплывающей подсказкой (текст — в data-tip, показ — в app.js).
   Не путать с .hint — это обычный серый поясняющий абзац. */
.tip {
  display: inline-flex; align-items: center; justify-content: center;
  width: 16px; height: 16px; border: 1.5px solid currentColor; border-radius: 50%;
  font-size: 10px; font-weight: 700; line-height: 1;
  cursor: help; user-select: none; vertical-align: middle;
  text-transform: none; letter-spacing: 0; opacity: 0.75;
}
.tip:hover { opacity: 1; }

#hint-pop {
  position: fixed; z-index: 300; max-width: 340px;
  background: #33343d; color: #fff; border-radius: 10px;
  padding: 12px 14px; font-size: 13px; line-height: 1.5;
  box-shadow: 0 8px 28px rgba(0, 0, 0, 0.3);
  white-space: pre-line; text-transform: none; letter-spacing: normal;
  text-align: left; font-weight: 400;
}
#hint-pop.hidden { display: none; }

td.num, th.num { text-align: right; }

tbody tr:hover { background: #faf8fd; }

.empty-state { display: flex; justify-content: center; padding: 80px 24px; }

.empty-state.hidden, .hidden { display: none; }

.empty-inner { text-align: center; max-width: 480px; }

.empty-icon { font-size: 56px; margin-bottom: 16px; }

.empty-inner h2 { font-size: 22px; margin-bottom: 10px; }

.empty-inner p { color: var(--muted); margin-bottom: 22px; }

.empty-actions { display: flex; gap: 10px; justify-content: center; }

.empty-actions .btn { border: 1px solid var(--border); background: var(--surface); color: var(--text); }
.empty-actions .btn.primary { background: var(--primary); color: #fff; border: none; }
.empty-actions .btn.primary:hover { background: var(--primary-dark); }

.features {
  list-style: none;
  text-align: left;
  margin-top: 28px;
  display: grid;
  gap: 10px;
}

.features li {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 14px;
  font-size: 13px;
  color: var(--text);
}

.site-footer {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px 24px 28px;
  font-size: 12px;
  color: var(--muted);
  text-align: center;
}

.h2-hint { font-size: 12px; font-weight: 400; color: var(--muted); }

tbody tr.clickable { cursor: pointer; }

/* Миниатюры товаров */
.prod-cell { display: flex; align-items: center; gap: 10px; }

.prod-thumb {
  width: 36px;
  height: 48px;
  border-radius: 8px;
  object-fit: cover;
  background: var(--bg);
  border: 1px solid var(--border);
  flex-shrink: 0;
}

.prod-thumb.emoji {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
}

.prod-thumb.big { width: 64px; height: 85px; border-radius: 10px; }

.prod-thumb.emoji.big { font-size: 36px; }

.modal-head-left { display: flex; align-items: center; gap: 14px; }

/* Товары в минусе + расшифровка удержаний */
.two-col { display: flex; gap: 14px; flex-wrap: wrap; margin-top: 28px; }

.two-col > div { flex: 1 1 380px; }

.table-section.losers h2 { color: var(--red); }

.table-section .hint { margin-top: 12px; }

td.pos { color: var(--green); font-weight: 600; }
td.neg { color: var(--red); font-weight: 600; }

/* Модальное окно товара */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(31, 27, 46, 0.55);
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: 40px 16px;
  z-index: 100;
  overflow-y: auto;
}

.modal-overlay.hidden { display: none; }

.modal {
  background: var(--surface);
  border-radius: 16px;
  padding: 24px;
  max-width: 860px;
  width: 100%;
  box-shadow: 0 12px 40px rgba(31, 27, 46, 0.3);
}

.modal-head {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 12px;
  margin-bottom: 18px;
}

.modal-head h2 { font-size: 20px; }

.modal-sub { font-size: 13px; color: var(--muted); margin-top: 4px; }

.modal-close {
  border: none;
  background: var(--bg);
  color: var(--muted);
  width: 34px;
  height: 34px;
  border-radius: 50%;
  font-size: 15px;
  cursor: pointer;
  flex-shrink: 0;
}

.modal-close:hover { background: var(--border); color: var(--text); }

.modal-cards {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(170px, 1fr));
  gap: 10px;
  margin-bottom: 20px;
}

.mini-card {
  background: var(--bg);
  border-radius: 10px;
  padding: 12px 14px;
}

.mini-card.total { background: linear-gradient(135deg, #faf7ff, #f1e8fc); border: 1px solid #d9c8ee; }

.mini-label { font-size: 11px; color: var(--muted); margin-bottom: 4px; }

.mini-value { font-size: 15px; font-weight: 700; }

.mini-card.pos .mini-value { color: var(--green); }
.mini-card.neg .mini-value { color: var(--red); }

.modal-columns {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 14px;
  margin-bottom: 20px;
}

@media (max-width: 700px) {
  .modal-columns { grid-template-columns: 1fr; }
}

.modal-breakdown {
  background: var(--bg);
  border-radius: 12px;
  padding: 16px 18px;
}

.modal-breakdown.unit { border: 1px solid #d9c8ee; background: linear-gradient(135deg, #faf7ff, #f3ecfb); }

.modal-breakdown h3 { font-size: 15px; margin-bottom: 10px; }

.bd-row {
  display: flex;
  justify-content: space-between;
  padding: 7px 0;
  font-size: 14px;
  border-bottom: 1px dashed var(--border);
}

.bd-row:last-child { border-bottom: none; }

.bd-label { color: var(--muted); }

.bd-value { font-weight: 600; font-variant-numeric: tabular-nums; }

.bd-value.pos { color: var(--green); }
.bd-value.neg { color: var(--red); }

.bd-total {
  margin-top: 6px;
  padding-top: 12px;
  border-top: 2px solid var(--primary);
  border-bottom: none;
}

.bd-total .bd-label { color: var(--text); font-weight: 700; }
.bd-total .bd-value { font-size: 17px; }

.modal-chart h3 { font-size: 15px; margin-bottom: 12px; }

.modal-chart { position: relative; height: 320px; }
.modal-chart canvas { max-height: 280px; }

/* Умный аналитик: автоматические выводы по цифрам */
.insights { display: grid; gap: 8px; }

.insight {
  background: var(--surface);
  border-radius: 10px;
  border-left: 3px solid var(--muted);
  padding: 10px 14px;
  font-size: 13px;
  line-height: 1.5;
  box-shadow: var(--shadow);
}

.insight.good { border-left-color: var(--green); }
.insight.bad { border-left-color: var(--red); }
.insight.info { border-left-color: #5a6acf; }
.insight.reco { border-left-color: var(--primary); background: linear-gradient(135deg, #faf7ff, #f5eefc); }

.insight b { font-weight: 700; }

/* ABC-анализ: вклад товара в выручку */
.abc {
  display: inline-block;
  min-width: 24px;
  text-align: center;
  padding: 2px 7px;
  border-radius: 6px;
  font-size: 11px;
  font-weight: 700;
}

.abc.a { background: #e3f5ec; color: var(--green); }
.abc.b { background: #fdf3dc; color: #a87b12; }
.abc.c { background: #f0eef5; color: var(--muted); }

/* Шапка секции с кнопкой (экспорт и т.п.) */
.section-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 10px;
  flex-wrap: wrap;
  margin-bottom: 14px;
}

.section-head h2 { margin-bottom: 0; }

.btn.ghost { border: 1px solid var(--border); background: var(--surface); color: var(--text); }
.btn.ghost:hover { background: var(--bg); }

/* Цель по прибыли на месяц */
.goal-bar {
  height: 6px;
  background: var(--border);
  border-radius: 3px;
  margin-top: 8px;
  overflow: hidden;
}

#goal-bar-fill {
  height: 100%;
  width: 0;
  background: linear-gradient(90deg, var(--primary), var(--green));
  border-radius: 3px;
  transition: width 0.3s;
}

.tax-sub input#goal-input { width: 86px; }

/* Себестоимость в модалке товара */
.modal-cost {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 16px 18px;
  margin-bottom: 20px;
}

.modal-cost h3 { font-size: 15px; margin-bottom: 10px; }

.cost-inputs { display: flex; gap: 18px; flex-wrap: wrap; font-size: 13px; color: var(--muted); }

.cost-inputs input {
  width: 80px;
  padding: 5px 8px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 13px;
  font-family: inherit;
}

.modal-cost .modal-breakdown { background: var(--surface); margin-top: 12px; padding: 12px 14px; }

.modal-cost .hint { margin-top: 10px; }

/* Широкие таблицы на узких экранах прокручиваются внутри своей карточки */
.table-section { overflow-x: auto; -webkit-overflow-scrolling: touch; }
#products-table { min-width: 800px; }
#losers-table { min-width: 460px; }
/* Приглушённая пометка в ячейке остатков («+N в пути») */
.stock-cell .muted { color: #8a8f98; font-size: 0.85em; white-space: nowrap; font-weight: 400; }

/* Мобильный список товаров (заменяет таблицу на телефоне) — на десктопе скрыт */
.prod-list { display: none; }

@media (max-width: 800px) {
  .charts { grid-template-columns: 1fr; }
  .header-inner { flex-direction: column; align-items: flex-start; }
}

/* === Мобильная версия === */
@media (max-width: 600px) {
  header { padding: 12px 14px; }

  h1 { font-size: 19px; }

  .header-controls { width: 100%; flex-wrap: wrap; }

  .header-controls select { flex: 1 1 100%; min-height: 44px; }

  .custom-range { flex: 1 1 100%; }

  .custom-range input[type="date"] { flex: 1; min-height: 44px; }

  .header-controls .btn { flex: 1; min-height: 44px; padding: 8px 10px; }

  /* iOS увеличивает страницу при фокусе на поле со шрифтом меньше 16px —
     поэтому на телефоне ВСЕ поля ввода и селекты ровно 16px */
  select,
  input[type="date"],
  input[type="number"],
  input[type="password"],
  .token-row input,
  .tax-sub input,
  .cost-inputs input { font-size: 16px; }

  .tax-sub input { width: 64px; }
  .tax-sub input#goal-input { width: 110px; }

  main { padding: 14px 12px; }

  .demo-banner { font-size: 12px; padding: 9px 12px; margin-bottom: 14px; }

  /* Карточки — в две колонки, компактнее */
  .cards {
    grid-template-columns: repeat(2, 1fr);
    gap: 10px;
    margin-bottom: 22px;
  }

  .card { padding: 13px 14px; border-radius: 12px; }

  .card-label { font-size: 12px; }

  .card-value { font-size: 19px; }

  .card.total .card-value { font-size: 22px; }

  .card-sub { font-size: 11px; }

  /* Итоговая карточка «Чистый доход» — на всю ширину */
  .card.total { grid-column: 1 / -1; }

  .op-section { margin-bottom: 22px; }

  .op-section h2, .chart-box h2, .table-section h2 { font-size: 15px; }

  .h2-hint { display: block; margin-top: 2px; }

  .charts { gap: 10px; margin-bottom: 22px; }

  .chart-box { padding: 14px; }

  /* Графики на телефоне ниже, но всё ещё читаемые */
  .chart-wrap { height: 240px; }

  .table-section { padding: 14px; }

  th, td { padding: 8px 9px; font-size: 13px; }

  th { font-size: 11px; }

  /* Товары: вместо широкой таблицы с прокруткой — карточки */
  #products-table { display: none; }

  .prod-list { display: grid; gap: 10px; }

  .prod-item {
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 12px;
    cursor: pointer;
  }

  .prod-item:active { background: var(--bg); }

  .prod-item-head { display: flex; align-items: center; gap: 10px; }

  .prod-item-title { min-width: 0; } /* иначе длинное название не даёт ужаться */

  .prod-item-name {
    font-size: 14px;
    font-weight: 600;
    line-height: 1.3;
    overflow: hidden;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
  }

  .prod-item-sub {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 11px;
    color: var(--muted);
    margin-top: 3px;
  }

  .prod-item-net {
    margin-left: auto;
    font-size: 15px;
    font-weight: 700;
    white-space: nowrap;
    align-self: flex-start;
  }

  .prod-item-net.pos { color: var(--green); }
  .prod-item-net.neg { color: var(--red); }

  .prod-item-stats {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 5px 12px;
    margin-top: 10px;
    font-size: 12.5px;
  }

  .prod-item-stats .stat-label { color: var(--muted); }

  .prod-item-stats b { font-weight: 600; }

  /* «Товары в минусе»: без колонки «Логистика» и фото влезает без прокрутки */
  #losers-table { min-width: 0; }

  #losers-table th:nth-child(4),
  #losers-table td:nth-child(4) { display: none; }

  #losers-table th, #losers-table td { padding: 8px 6px; }

  #losers-table .prod-thumb { display: none; }

  /* Расшифровка удержаний: длинные причины переносятся, сумма не сжимается */
  .bd-row { gap: 10px; }
  .bd-label { min-width: 0; }
  .bd-value { flex-shrink: 0; }

  .two-col { gap: 10px; margin-top: 22px; }

  /* Токен-панель: поле и кнопка друг под другом */
  .token-panel { padding: 16px 14px; }

  .token-row { flex-direction: column; }

  .token-row .btn { min-height: 44px; }

  .status { margin: 12px 12px 0; font-size: 13px; }

  /* Стартовый экран */
  .empty-state { padding: 40px 16px; }

  .empty-icon { font-size: 44px; }

  .empty-inner h2 { font-size: 19px; }

  .empty-actions { flex-direction: column; }

  .empty-actions .btn { min-height: 44px; }

  .features { margin-top: 22px; }

  /* Модалка товара — почти во весь экран, крестик крупнее под палец */
  .modal-overlay { padding: 8px 6px; }

  .modal { padding: 16px 14px; border-radius: 14px; }

  .modal-head h2 { font-size: 17px; }

  .modal-close { width: 42px; height: 42px; font-size: 17px; }

  .modal-cards { grid-template-columns: repeat(2, 1fr); gap: 8px; }

  .modal-chart { height: 260px; }

  .modal-chart canvas { max-height: 220px; }

  .cost-inputs { flex-direction: column; gap: 8px; }

  .section-head .btn.ghost { flex: 0 1 auto; width: 100%; min-height: 44px; }

  .site-footer { padding: 16px 14px 22px; }
}

/* Совсем узкие экраны — карточки в одну колонку */
@media (max-width: 360px) {
  .cards { grid-template-columns: 1fr; }
}
