/* =========================================================================
   Админ-панель. Без фреймворков: только переменные, флексы и гриды.
   Тёмная тема подхватывается из настроек системы.
   ========================================================================= */

:root {
  color-scheme: light dark;

  --bg: #f7f6f4;
  --surface: #ffffff;
  --surface-soft: #fbfaf9;
  --line: #e8e5e0;
  --line-soft: #f0edE8;

  --text: #1a1a1c;
  --text-soft: #5f5f68;
  --text-faint: #8d8d96;

  --accent: #1c6b47;
  --accent-soft: #eaf3ee;
  --danger: #a32b2b;
  --danger-soft: #fbeeee;
  --warn: #8a6412;
  --warn-soft: #fbf4e5;

  --radius: 14px;
  --radius-sm: 9px;
  --shadow: 0 1px 2px rgba(20, 20, 25, .04), 0 8px 24px -16px rgba(20, 20, 25, .18);

  --gap: 20px;
  --pad: 22px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121214;
    --surface: #1a1a1d;
    --surface-soft: #202024;
    --line: #2c2c31;
    --line-soft: #26262a;

    --text: #ecebe9;
    --text-soft: #a3a3ac;
    --text-faint: #74747e;

    --accent: #4fb185;
    --accent-soft: #172a22;
    --danger: #e08282;
    --danger-soft: #2c1c1c;
    --warn: #d7ad5c;
    --warn-soft: #2a2416;

    --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 8px 24px -16px rgba(0, 0, 0, .7);
  }
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.55 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
        "Helvetica Neue", Arial, sans-serif;
  font-variant-numeric: tabular-nums;
  -webkit-font-smoothing: antialiased;
}

a { color: inherit; text-decoration-color: color-mix(in srgb, currentColor 35%, transparent); }
a:hover { text-decoration-color: currentColor; }

/* ---------------- шапка ---------------- */

header {
  position: sticky;
  top: 0;
  z-index: 10;
  background: color-mix(in srgb, var(--surface) 88%, transparent);
  backdrop-filter: saturate(180%) blur(12px);
  border-bottom: 1px solid var(--line);
}

.header-inner {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 24px;
  display: flex;
  align-items: center;
  gap: 28px;
  min-height: 58px;
}

/* На широком экране обёртка не создаёт своей строки: название и «Выйти»
   встают прямо в общий ряд. Порядок задаём явно, потому что в разметке
   «Выйти» лежит рядом с названием. */
.header-top { display: contents; }
nav { order: 1; }
.logout { order: 2; margin-left: auto; }

.brand {
  font-weight: 600;
  letter-spacing: -.01em;
  white-space: nowrap;
  display: inline-flex;
  align-items: center;
  gap: 9px;
}
/* Точка вместо логотипа: даёт шапке опору для взгляда и отделяет название
   проекта от пунктов меню. */
.brand::before {
  content: "";
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--accent);
  flex: 0 0 auto;
}

/* Меню — сегментированный переключатель: пункты лежат в одной подложке,
   выбранный приподнят. Так видно, что это один блок выбора, а не россыпь
   ссылок. Без переноса: на узком экране лента прокручивается, а не
   разваливается на три строки. */
nav {
  display: flex;
  gap: 2px;
  flex: 0 0 auto;
  flex-wrap: nowrap;
  /* Подложка на тон «глубже» фона карточек, выбранный пункт — на тон выше.
     В светлой теме это серая подложка и белая пилюля, в тёмной наоборот:
     почти чёрная подложка и пилюля посветлее. Обе темы читаются одинаково. */
  background: var(--bg);
  border: 1px solid var(--line-soft);
  border-radius: 11px;
  padding: 3px;
}

nav a {
  padding: 6px 13px;
  border-radius: 8px;
  color: var(--text-soft);
  text-decoration: none;
  font-size: 14px;
  transition: background .15s, color .15s, box-shadow .15s;
}
nav a:hover { color: var(--text); }
nav a.active {
  background: var(--surface);
  color: var(--text);
  font-weight: 500;
  box-shadow: 0 1px 2px rgba(20, 20, 25, .07);
}

.logout {
  color: var(--text-faint);
  font-size: 13px;
  text-decoration: none;
  white-space: nowrap;
  padding: 6px 10px;
  border-radius: 8px;
  transition: background .15s, color .15s;
}
.logout:hover { color: var(--text); background: var(--surface-soft); }

main { max-width: 1200px; margin: 0 auto; padding: 28px 24px 64px; }

/* ---------------- заголовки ---------------- */

.page-head { margin-bottom: 22px; }

h1 {
  font-size: 24px;
  font-weight: 600;
  letter-spacing: -.02em;
  margin: 0;
  display: flex;
  align-items: baseline;
  gap: 10px;
  flex-wrap: wrap;
}
h1 .count { color: var(--text-faint); font-weight: 400; font-size: 17px; }

h2 {
  font-size: 13px;
  font-weight: 600;
  letter-spacing: .04em;
  text-transform: uppercase;
  color: var(--text-faint);
  margin: 32px 0 12px;
}

.subtitle { color: var(--text-soft); font-size: 14px; margin: 6px 0 0; }

/* ---------------- карточки и плитки ---------------- */

.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: var(--pad);
  box-shadow: var(--shadow);
}

.tiles {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 12px;
  margin-bottom: var(--gap);
}

.tile {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 16px 18px;
  box-shadow: var(--shadow);
}
.tile .label {
  color: var(--text-faint);
  font-size: 12px;
  letter-spacing: .03em;
  text-transform: uppercase;
}
.tile .value {
  font-size: 27px;
  font-weight: 600;
  letter-spacing: -.02em;
  margin-top: 6px;
  line-height: 1.1;
}
.tile .value.accent { color: var(--accent); }
.tile .sub { color: var(--text-faint); font-size: 12px; margin-top: 4px; }

/* ---------------- воронка ---------------- */

.funnel { display: flex; flex-direction: column; }

.step {
  display: grid;
  grid-template-columns: minmax(150px, 1.1fr) 64px minmax(120px, 3fr) minmax(120px, 1fr);
  align-items: center;
  gap: 16px;
  padding: 11px 0;
  border-bottom: 1px solid var(--line-soft);
}
.step:last-child { border-bottom: 0; }

.step .name { font-size: 14px; }
.step .count { font-size: 18px; font-weight: 600; text-align: right; letter-spacing: -.02em; }

.step .track {
  background: var(--line-soft);
  border-radius: 999px;
  height: 8px;
  overflow: hidden;
}
.step .fill {
  background: var(--accent);
  height: 100%;
  border-radius: 999px;
  min-width: 2px;
  transition: width .4s ease;
}
.step .conv { font-size: 13px; color: var(--text-faint); text-align: right; }
.step .lost { color: var(--danger); margin-left: 6px; }

@media (max-width: 720px) {
  .step { grid-template-columns: 1fr auto; gap: 6px 12px; }
  .step .track { grid-column: 1 / -1; }
  .step .conv { grid-column: 1 / -1; text-align: left; }
}

/* ---------------- таблицы ---------------- */

.table-wrap {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: var(--shadow);
}
.table-scroll { overflow-x: auto; }

table { width: 100%; border-collapse: collapse; font-size: 14px; }

th {
  text-align: left;
  font-weight: 500;
  font-size: 12px;
  letter-spacing: .03em;
  text-transform: uppercase;
  color: var(--text-faint);
  padding: 11px 16px;
  border-bottom: 1px solid var(--line);
  white-space: nowrap;
  background: var(--surface-soft);
}
td { padding: 12px 16px; border-bottom: 1px solid var(--line-soft); vertical-align: top; }
tbody tr:last-child td { border-bottom: 0; }
tbody tr { transition: background .12s; }
tbody tr:hover { background: var(--surface-soft); }

td.num, th.num { text-align: right; }
td.tight { white-space: nowrap; }

.cell-title { font-weight: 500; }
.cell-sub { color: var(--text-faint); font-size: 12.5px; margin-top: 2px; }

.empty { padding: 36px 16px; text-align: center; color: var(--text-faint); font-size: 14px; }

/* вертикальная таблица «ключ — значение» в карточке */
.props { width: 100%; font-size: 14px; }
.props th {
  background: none;
  border: 0;
  text-transform: none;
  letter-spacing: 0;
  font-size: 13px;
  color: var(--text-faint);
  padding: 7px 0;
  width: 45%;
}
.props td { border: 0; padding: 7px 0; }

/* ---------------- бейджи ---------------- */

.badge {
  display: inline-block;
  padding: 2px 9px;
  border-radius: 999px;
  font-size: 12px;
  line-height: 1.5;
  white-space: nowrap;
  background: var(--surface-soft);
  border: 1px solid var(--line);
  color: var(--text-soft);
}
.badge.ok { color: var(--accent); background: var(--accent-soft); border-color: transparent; }
.badge.warn { color: var(--warn); background: var(--warn-soft); border-color: transparent; }
.badge.err { color: var(--danger); background: var(--danger-soft); border-color: transparent; }
.badge.mute { color: var(--text-faint); }

/* ---------------- формы ---------------- */

.toolbar {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  align-items: flex-end;
  margin-bottom: 16px;
}
.toolbar .spacer { flex: 1; }

label.field {
  display: flex;
  flex-direction: column;
  gap: 5px;
  font-size: 12px;
  letter-spacing: .02em;
  color: var(--text-faint);
}

input[type="text"], input[type="date"], input[type="password"], select, textarea {
  padding: 8px 11px;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  font: inherit;
  font-size: 14px;
  background: var(--surface);
  color: var(--text);
  transition: border-color .15s, box-shadow .15s;
}
input:focus, select:focus, textarea:focus {
  outline: 0;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}
input::placeholder { color: var(--text-faint); }

textarea {
  width: 100%;
  min-height: 190px;
  resize: vertical;
  line-height: 1.6;
}

button, .btn {
  padding: 9px 16px;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  background: var(--text);
  color: var(--bg);
  font: inherit;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  gap: 7px;
  white-space: nowrap;
  transition: opacity .15s, background .15s;
}
button:hover, .btn:hover { opacity: .85; }

.btn.ghost {
  background: var(--surface);
  color: var(--text);
  border-color: var(--line);
}
.btn.ghost:hover { background: var(--surface-soft); opacity: 1; }

.segmented { display: inline-flex; gap: 3px; padding: 3px; background: var(--surface-soft); border: 1px solid var(--line); border-radius: var(--radius-sm); }
.segmented a {
  padding: 6px 12px;
  border-radius: 7px;
  font-size: 13px;
  color: var(--text-soft);
  text-decoration: none;
  transition: background .15s, color .15s;
}
.segmented a:hover { color: var(--text); }
.segmented a.active { background: var(--surface); color: var(--text); font-weight: 500; box-shadow: var(--shadow); }

.check {
  display: flex;
  align-items: flex-start;
  gap: 9px;
  font-size: 14px;
  color: var(--text-soft);
  cursor: pointer;
  line-height: 1.45;
}
.check input { margin: 2px 0 0; accent-color: var(--accent); }

/* ---------------- сообщения ---------------- */

.notice {
  padding: 12px 15px;
  border-radius: var(--radius-sm);
  margin-bottom: 16px;
  font-size: 14px;
  border: 1px solid transparent;
}
.notice.ok { background: var(--accent-soft); color: var(--accent); }
.notice.err { background: var(--danger-soft); color: var(--danger); }

/* Ручное подтверждение оплаты в карточке человека. */
.confirm-pay {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px 10px;
  margin-top: 8px;
}
.confirm-pay label { display: inline-flex; align-items: center; gap: 5px; color: var(--text-soft); }
.small-btn { padding: 5px 10px; font-size: 12.5px; }
.notice.info { background: var(--surface); border-color: var(--line); color: var(--text-soft); }

/* Баннер о ненадёжном хранилище — держим заметным, но не кричащим. */
.alert {
  padding: 13px 16px;
  border-radius: var(--radius-sm);
  margin-bottom: var(--gap);
  font-size: 14px;
  line-height: 1.5;
  background: var(--warn-soft);
  color: var(--warn);
  border: 1px solid color-mix(in srgb, var(--warn) 25%, transparent);
}
.alert strong { font-weight: 600; }

/* ---------------- прочее ---------------- */

.muted { color: var(--text-faint); }
.small { font-size: 13px; }
.mono {
  font-family: ui-monospace, SFMono-Regular, "SF Mono", Consolas, "Liberation Mono", monospace;
  font-size: 13px;
}
.hint { color: var(--text-faint); font-size: 13px; margin-top: 14px; line-height: 1.6; }

.back-link {
  display: inline-block;
  color: var(--text-faint);
  font-size: 13px;
  text-decoration: none;
  margin-bottom: 12px;
}
.back-link:hover { color: var(--text); }

.pager {
  display: flex;
  gap: 10px;
  align-items: center;
  justify-content: center;
  margin-top: 18px;
  font-size: 13px;
  color: var(--text-faint);
}

.cols { display: grid; grid-template-columns: repeat(auto-fit, minmax(330px, 1fr)); gap: var(--gap); }

.stack { display: flex; flex-direction: column; gap: var(--gap); }

/* ---------------- лента событий ---------------- */

.timeline { list-style: none; padding: 0; margin: 0; }
.timeline li {
  display: grid;
  grid-template-columns: 145px minmax(140px, 200px) 1fr;
  gap: 14px;
  padding: 9px 0;
  border-bottom: 1px solid var(--line-soft);
  font-size: 14px;
}
.timeline li:last-child { border-bottom: 0; }
.timeline .when { color: var(--text-faint); font-size: 12.5px; font-variant-numeric: tabular-nums; }
.timeline .what { font-weight: 500; }
.timeline .meta { color: var(--text-faint); font-size: 12.5px; word-break: break-word; }

@media (max-width: 720px) {
  .timeline li { grid-template-columns: 1fr; gap: 3px; }
}

/* ---------------- страница входа ---------------- */

body.login {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  padding: 24px;
}
body.login .card { max-width: 350px; width: 100%; padding: 30px; }
body.login h1 { font-size: 20px; display: block; }
body.login .subtitle { margin-bottom: 24px; }
body.login label.field { margin-bottom: 14px; }
body.login input { width: 100%; }
body.login button { width: 100%; justify-content: center; padding: 11px; }

/* ---------------- телефон ---------------- */

@media (max-width: 720px) {
  /* На телефоне всё заметно плотнее: экран узкий, и интерфейс, рассчитанный
     на мышь, занимает на нём вдвое больше места, чем нужно. Уменьшаем базовый
     шрифт и отступы, но не поля ввода — их трогать нельзя (см. ниже). */
  body { font-size: 13.5px; }

  /* Шапка: две ясные строки вместо каши из переносов.
     Сверху название и выход, под ними меню лентой с прокруткой вбок. */
  .header-inner {
    flex-direction: column;
    align-items: stretch;
    gap: 0;
    padding: 0 12px;
    min-height: 0;
  }
  .header-top {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    min-height: 36px;
  }
  .brand {
    font-size: 14px;
    min-width: 0;
    gap: 7px;
  }
  /* Длинное название проекта обрезаем, а не выталкиваем «Выйти» за экран. */
  .brand > * { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .brand::before { width: 6px; height: 6px; }

  .logout {
    order: 0;
    font-size: 12px;
    padding: 5px 10px;
    border: 1px solid var(--line);
    margin-left: 0;
  }

  nav {
    order: 0;
    gap: 2px;
    margin: 0 0 6px;
    padding: 2px;
    border-radius: 9px;
    overflow-x: auto;
    scrollbar-width: none;
    -webkit-overflow-scrolling: touch;
  }
  nav::-webkit-scrollbar { display: none; }
  nav a {
    flex: 1 1 auto;
    text-align: center;
    white-space: nowrap;
    padding: 6px 9px;
    font-size: 12.5px;
    border-radius: 7px;
  }

  main { padding: 14px 12px 40px; }
  h1 { font-size: 17px; }
  .page-head { margin-bottom: 14px; }
  h2 { font-size: 15px; }

  /* Фильтры: каждое поле на всю ширину, кнопки крупные и нажимаемые пальцем. */
  .toolbar { gap: 10px; }
  .toolbar label.field { flex: 1 1 100%; }
  .toolbar .spacer { display: none; }
  .toolbar button,
  .toolbar .btn { flex: 1 1 100%; text-align: center; justify-content: center; }

  input[type="text"], input[type="date"], input[type="password"], select, textarea {
    width: 100%;
    /* 16px обязательны: на меньшем размере iOS увеличивает всю страницу,
       как только коснёшься поля, и вернуть масштаб потом непросто. */
    font-size: 16px;
    padding: 8px 11px;
  }
  button, .btn { padding: 9px 14px; font-size: 14px; }
  label.field { font-size: 11.5px; }

  /* Таблицы шире экрана: прокручиваем их саму, а не всю страницу. */
  .table-scroll { -webkit-overflow-scrolling: touch; }
  td, th { padding: 7px 10px; white-space: nowrap; font-size: 13px; }
  th { font-size: 11px; }
  td .cell-sub, td .cell-title { white-space: normal; }

  /* Списки людей и платежей на телефоне становятся карточками.
     Восемь колонок иначе пришлось бы листать вбок, чтобы прочитать одну
     строку целиком: половина данных всё время остаётся за краем экрана. */
  table.cardify thead { display: none; }
  table.cardify,
  table.cardify tbody,
  table.cardify tr,
  table.cardify td { display: block; width: auto; }

  table.cardify tr {
    padding: 10px 12px;
    border-bottom: 1px solid var(--line);
  }
  table.cardify tr:last-child { border-bottom: 0; }

  table.cardify td {
    border: 0;
    padding: 1px 0;
    white-space: normal;
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: 12px;
    text-align: right;
    font-size: 12.5px;
    line-height: 1.35;
  }
  /* Второстепенное в карточку не пускаем: она и так длинная, а эти поля
     есть в карточке человека и в выгрузке. */
  table.cardify td.col-secondary { display: none; }
  /* Подпись колонки берём из заголовка, который спрятали. */
  table.cardify td::before {
    content: attr(data-label);
    color: var(--text-faint);
    font-size: 12px;
    letter-spacing: .02em;
    text-align: left;
    flex: 0 0 auto;
  }
  /* Заглавная ячейка карточки — имя человека. Помечена в разметке явно,
     а не выбрана по позиции: в платежах первым идёт номер, который на
     телефоне не нужен. */
  table.cardify td.cell-main {
    display: block;
    text-align: left;
    padding-bottom: 5px;
  }
  table.cardify td.cell-main::before { content: none; }
  table.cardify .cell-title { font-size: 13.5px; }
  table.cardify .cell-sub { font-size: 11.5px; }
  table.cardify td::before { font-size: 11px; }
  table.cardify td.empty { text-align: center; padding: 24px 0; }
  table.cardify td.empty::before { content: none; }
  table.cardify td.num { text-align: right; }

  .tiles { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; margin-bottom: 14px; }
  .tile { padding: 10px 12px; }
  .tile .label { font-size: 10.5px; }
  .tile .value { font-size: 19px; margin-top: 3px; }
  .tile .sub { font-size: 11px; margin-top: 2px; }

  /* Воронка: шаги плотнее, полоса тоньше. */
  .step { padding: 7px 0; gap: 4px 10px; }
  .step .conv { font-size: 11.5px; }
  .step .track { height: 5px; }
  .step .name { font-size: 13px; }
  .step .count { font-size: 14px; }
  .badge { font-size: 11px; padding: 2px 7px; }
  .card, .panel { padding: 14px; }

  .pager { flex-wrap: wrap; gap: 8px; }
  .pager .btn { flex: 1 1 40%; text-align: center; }
}

/* Узкий телефон: уплотняем меню, чтобы все четыре пункта помещались целиком.
   Подрезанный последний пункт читается как поломка, а не как «листай вбок». */
@media (max-width: 430px) {
  nav a { padding: 7px 6px; font-size: 12px; }
}

@media (max-width: 400px) {
  .tiles { grid-template-columns: 1fr; }
}
