:root {
  --bg: #14161a; --panel: #1b1e24; --line: #2b3038; --text: #e6e9ef;
  --muted: #8b94a3; --accent: #5aa9ff; --good: #4ec98a; --bad: #ff6b6b; --warn: #ffb454;
  color-scheme: dark;
}
* { box-sizing: border-box; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font: 14px/1.45 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
h2 { font-size: 15px; margin: 22px 0 6px; font-weight: 600; }
a { color: var(--accent); }
.muted { color: var(--muted); font-weight: 400; }
.hint { color: var(--muted); font-size: 12px; margin: 0 0 8px; }
kbd {
  background: #262b33; border: 1px solid var(--line); border-radius: 3px;
  padding: 0 4px; font-size: 11px; color: var(--muted);
}

header {
  display: flex; align-items: center; gap: 6px; padding: 8px 14px;
  border-bottom: 1px solid var(--line); background: var(--panel);
  position: sticky; top: 0; z-index: 5;
}
header .spacer { flex: 1; }
#tabs button { background: none; border: 1px solid transparent; }
#tabs button.active { background: #262b33; border-color: var(--line); color: var(--text); }

button {
  background: #2a303a; color: var(--text);
  border: 1px solid var(--line); border-radius: 4px; padding: 5px 10px;
  cursor: pointer; font: inherit; font-size: 13px;
}
button:hover { border-color: var(--accent); }
button.ghost { background: none; color: var(--muted); }
button.ghost:hover { color: var(--text); }
button.primary { background: #23445f; border-color: #2f5f87; }
button.danger:hover { border-color: var(--bad); color: var(--bad); }

input, select, textarea {
  background: #0f1115; color: var(--text); border: 1px solid var(--line);
  border-radius: 4px; padding: 5px 8px; font: inherit; font-size: 13px;
}
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--accent); }
/* Дата не сегодняшняя — видно сразу, чтобы не занести свежее событие прошлым числом. */
input[type="date"].past { border-color: var(--warn); color: var(--warn); }
input.wide { flex: 1; min-width: 120px; }

main { padding: 14px; max-width: 1180px; }

form { display: flex; gap: 6px; flex-wrap: wrap; align-items: flex-end; margin-bottom: 10px; }
form input { width: 130px; }

/* Подпись над полем: что именно сюда писать, без наведения мышью. */
form label { display: flex; flex-direction: column; gap: 2px; font-size: 11px; color: var(--muted); }
form label.grow { flex: 1; min-width: 150px; }
form label.grow input { width: 100%; }
form label .btns { display: flex; gap: 6px; }
#filters { align-items: center; }

.toolbar { display: flex; gap: 6px; align-items: center; margin-bottom: 10px; }
.who { display: flex; align-items: center; gap: 5px; font-size: 11px; color: var(--muted); }
.who select { padding: 3px 6px; }
.owner { font-size: 11px; color: var(--muted); white-space: nowrap; }

.counters { display: flex; gap: 28px; align-items: baseline; margin-bottom: 16px; }
.counter b { font-size: 44px; line-height: 1; display: block; }
.counter.small b { font-size: 22px; color: var(--muted); }
.counter span { color: var(--muted); font-size: 12px; }
.outbox-owner { margin: 20px 0 6px; font-size: 13px; font-weight: 600; color: var(--muted); letter-spacing: .02em; }
.outbox-owner:first-child { margin-top: 0; }
/* Сравнение с прошлой неделей под счётчиком: «▲ +2 · было 3»; с чем сравниваем — подписью .cmp-note. */
.counter .cmp { display: block; font-style: normal; font-size: 11.5px; color: var(--muted); margin-top: 3px; white-space: nowrap; cursor: help; }
.cmp-note { color: var(--muted); font-size: 11px; margin: -8px 0 14px; }
/* Итоги вчера и прошлой недели отделены от текущих цифр чертой. */
.counter.past { border-left: 1px solid var(--line); padding-left: 18px; }
.counter.past + .counter.past { border-left: none; padding-left: 0; }
.counter.past .cmp { cursor: default; }
.cmp .up { color: var(--good); }
.cmp .down { color: var(--bad); }

.rows { display: flex; flex-direction: column; gap: 3px; }
.row {
  display: flex; align-items: center; gap: 10px; padding: 6px 8px;
  background: var(--panel); border: 1px solid transparent; border-radius: 4px;
}
.row.sel { border-color: var(--accent); }
.row .name { min-width: 190px; font-weight: 600; }
.row .meta { color: var(--muted); font-size: 12px; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row .actions { display: flex; gap: 4px; }
.row.overdue .due { color: var(--warn); }

table { border-collapse: collapse; width: 100%; font-size: 13px; }
th, td { text-align: left; padding: 5px 8px; border-bottom: 1px solid var(--line); white-space: nowrap; }
th { color: var(--muted); font-weight: 500; font-size: 12px; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
tbody tr:hover { background: var(--panel); cursor: pointer; }

.badge { font-size: 11px; padding: 1px 6px; border-radius: 3px; background: #262b33; color: var(--muted); }
.badge.won { background: #1e4433; color: var(--good); }
.badge.lost { background: #452424; color: var(--bad); }
.badge.new { background: #23364d; color: var(--accent); }
.badge.postponed { background: #4a3a1c; color: var(--warn); }

.thin { color: var(--warn); font-size: 11px; }
.grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(340px, 1fr)); gap: 0 28px; align-items: start; }
.grid > div { min-width: 0; }
.scroll { overflow-x: auto; }
.period { display: flex; gap: 6px; align-items: center; margin-bottom: 4px; }
.period button.active { background: #23445f; border-color: #2f5f87; }
#owner-filter { display: inline-flex; gap: 4px; }
.period-nav, .period-custom { display: inline-flex; gap: 6px; align-items: center; }
.period-nav[hidden], .period-custom[hidden], #period-now[hidden] { display: none; }
.period-nav button[data-shift="-1"], .period-nav button[data-shift="1"] { min-width: 34px; font-size: 16px; }
.period-nav button:disabled { opacity: .35; cursor: default; }
.period-custom input[type="date"] { padding: 6px 8px; color-scheme: dark; }
.kv { display: flex; justify-content: space-between; gap: 16px; padding: 3px 0; border-bottom: 1px dotted var(--line); }
.kv b { font-variant-numeric: tabular-nums; }
.note { color: var(--muted); font-size: 12px; margin-top: 10px; line-height: 1.5; }

/* id-селектор ниже перебил бы браузерное [hidden]{display:none} — возвращаем его явно. */
#overlay[hidden] { display: none; }
#overlay {
  position: fixed; inset: 0; background: #000a; display: flex;
  justify-content: center; align-items: flex-start; padding: 40px 16px; overflow: auto; z-index: 10;
}
#card {
  background: var(--panel); border: 1px solid var(--line); border-radius: 6px;
  padding: 16px; width: 100%; max-width: 720px;
}
#card h3 { margin: 0 0 2px; font-size: 17px; }
#card textarea { width: 100%; min-height: 60px; margin-top: 4px; }
.ev { display: flex; gap: 10px; padding: 4px 0; border-bottom: 1px dotted var(--line); font-size: 13px; }
.ev time { color: var(--muted); white-space: nowrap; }
.ev .t { min-width: 78px; }
.ev .x { margin-left: auto; color: var(--muted); cursor: pointer; background: none; border: 0; padding: 0 4px; }
.ev .x:hover { color: var(--bad); }

#toast {
  position: fixed; bottom: 16px; left: 50%; transform: translateX(-50%);
  background: #23445f; border: 1px solid #2f5f87; padding: 8px 14px;
  border-radius: 4px; z-index: 20; max-width: 80vw;
}

/* Классы вместо инлайновых стилей: строгий CSP запрещает style="" и onclick="". */
.card-head { display: flex; gap: 10px; align-items: center; }
.gap { flex: 1; }
.ev-buttons { display: flex; gap: 5px; flex-wrap: wrap; margin: 10px 0; }
.w-hours { width: 70px; }
.report-text { min-height: 60vh; font-size: 12px; width: 100%; }
.bulk-text { width: 100%; min-height: 180px; }
.bulk-fields { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 8px; }

/* ───────── страница входа ───────── */
.login-page { display: flex; align-items: center; justify-content: center; min-height: 100vh; }
.login-box {
  /* align-items и flex-wrap перебиваем явно: общее правило form{} прижимает
     содержимое к правому краю и ломает колонку. */
  display: flex; flex-direction: column; align-items: stretch; flex-wrap: nowrap;
  gap: 10px; width: 320px; max-width: 100%;
  background: var(--panel); border: 1px solid var(--line); border-radius: 8px; padding: 24px;
  margin: 0;
}
.login-box h1 { margin: 0; font-size: 20px; }
.login-box label { display: flex; flex-direction: column; gap: 3px; font-size: 11px; color: var(--muted); }
.login-box input { width: 100%; padding: 7px 9px; }
.login-box button { margin-top: 6px; padding: 8px; }
.login-error { color: var(--bad); font-size: 12px; margin: 0; }

/* Кто вошёл — одна кнопка с именем; логин, «на кого записывать», пароль и выход — в её меню. */
.account { position: relative; }
.account-chip {
  display: inline-flex; align-items: center; gap: 7px; padding: 3px 10px 3px 3px; border-radius: 999px;
  background: #222731; color: var(--text); font-weight: 600;
}
.account-chip .avatar {
  display: inline-grid; place-items: center; width: 22px; height: 22px; border-radius: 50%;
  background: var(--accent); color: #0f1115; font-size: 12px; font-weight: 700;
}
.account-chip .caret { color: var(--muted); font-size: 10px; }
.account-menu {
  position: absolute; right: 0; top: calc(100% + 6px); z-index: 8; min-width: 250px; padding: 12px;
  display: grid; gap: 12px; background: var(--panel); border: 1px solid var(--line); border-radius: 8px;
  box-shadow: 0 10px 28px rgba(0, 0, 0, 0.4);
}
.account-menu[hidden], .who-badge[hidden] { display: none; } /* display: grid/inline would beat [hidden] */
.account-head { margin: 0; display: grid; gap: 2px; }
.account-head .muted { font-size: 12px; }
.account-menu .who { flex-direction: column; align-items: stretch; gap: 4px; font-size: 12px; }
.account-menu .who select { width: 100%; padding: 6px 8px; }
.account-actions { display: flex; justify-content: space-between; gap: 6px; padding-top: 10px; border-top: 1px solid var(--line); }
/* Работа пишется не на себя: плашка в шапке, крестик возвращает своё имя. */
.who-badge { border: 1px solid var(--warn); border-radius: 999px; padding: 3px 10px; background: none; color: var(--warn); font-size: 12px; }
.who-badge:hover { border-color: var(--warn); background: rgba(255, 180, 84, 0.1); }

/* Работа заносится не на того, кто вошёл — предупреждаем цветом. */
#who.foreign { border-color: var(--warn); color: var(--warn); }

/* Картинка воронки: холст 2400×1350, на экране — по ширине колонки. */
.funnel-pic { display: block; width: 100%; max-width: 1200px; height: auto; border: 1px solid var(--line); border-radius: 8px; }
.pic-bar { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; margin: 8px 0 4px; }

/* Меню «⋯» и кнопка фильтров нужны только на телефоне; на десктопе кнопки стоят в шапке как раньше. */
.header-actions { display: contents; }
.more, .filters-toggle { display: none; }

/*
  Телефон (iPhone 17 Pro Max — 440×956, и всё уже 700 px).
  Вкладки уезжают в нижнюю панель под большой палец, редкие кнопки шапки — в меню «⋯»,
  таблица контактов становится карточками. Поля — 16 px: при меньшем шрифте Safari
  приближает страницу на каждом вводе. Отступы учитывают вырез и полоску «домой».
*/
@media (max-width: 700px) {
  body { font-size: 15px; -webkit-text-size-adjust: 100%; }
  kbd, .keys { display: none; }
  input, select, textarea, button { font-size: 16px; }
  button { min-height: 40px; padding: 8px 12px; border-radius: 8px; }
  input, select { min-height: 40px; padding: 7px 10px; border-radius: 8px; }

  header {
    padding: calc(6px + env(safe-area-inset-top)) calc(12px + env(safe-area-inset-right)) 6px calc(12px + env(safe-area-inset-left));
    gap: 8px;
  }
  #account-name { max-width: 30vw; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .account-chip { min-height: 40px; padding-right: 12px; }
  .account-menu { position: fixed; left: 12px; right: 12px; top: calc(56px + env(safe-area-inset-top)); min-width: 0; }
  .who-badge { max-width: 38vw; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .more { display: inline-flex; align-items: center; justify-content: center; min-width: 44px; min-height: 44px; font-size: 22px; line-height: 1; color: var(--text); }
  .header-actions {
    display: none; position: absolute; right: 8px; top: calc(100% + 4px); z-index: 7;
    flex-direction: column; gap: 2px; min-width: 210px; padding: 6px;
    background: var(--panel); border: 1px solid var(--line); border-radius: 10px; box-shadow: 0 12px 32px #000a;
  }
  .header-actions.open { display: flex; }
  .header-actions button { text-align: left; min-height: 44px; color: var(--text); }

  #tabs {
    position: fixed; left: 0; right: 0; bottom: 0; z-index: 6;
    display: grid; grid-template-columns: repeat(5, 1fr); gap: 2px;
    padding: 4px calc(4px + env(safe-area-inset-right)) calc(4px + env(safe-area-inset-bottom)) calc(4px + env(safe-area-inset-left));
    background: var(--panel); border-top: 1px solid var(--line);
  }
  #tabs button { min-height: 50px; padding: 4px 2px; font-size: 12px; line-height: 1.2; white-space: normal; }

  main { padding: 12px calc(12px + env(safe-area-inset-right)) calc(84px + env(safe-area-inset-bottom)) calc(12px + env(safe-area-inset-left)); }
  #toast { bottom: calc(76px + env(safe-area-inset-bottom)); max-width: 92vw; }

  .counters { flex-wrap: wrap; gap: 12px 22px; margin-bottom: 12px; }
  .counter b { font-size: 34px; }

  /* Формы — сетка в две колонки, длинные поля и кнопки на всю ширину. */
  form { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; align-items: end; }
  form input, form select, form label.grow input { width: 100%; min-width: 0; }
  form label { min-width: 0; font-size: 12px; }
  form label.grow, form .wide, form label:has(.btns) { grid-column: 1 / -1; }
  form label .btns { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
  form > button { grid-column: 1 / -1; }

  .toolbar { flex-wrap: wrap; }
  .filters-toggle { display: inline-block; }
  #filters:not(.open) { display: none; }

  /* Строки «Сегодня», «К отправке», «Часы»: имя и статус сверху, детали ниже, кнопки внизу. */
  .row { flex-wrap: wrap; gap: 6px 10px; padding: 10px 12px; border-radius: 10px; }
  .row .name { min-width: 0; flex: 1 1 auto; font-size: 15px; }
  .row .meta { flex-basis: 100%; white-space: normal; font-size: 13px; line-height: 1.45; }
  .row .actions { flex-basis: 100%; }
  .row .actions button { flex: 1; min-height: 44px; }

  /* Контакты: таблица превращается в карточки. */
  #contacts-table thead { display: none; }
  #contacts-table table, #contacts-table tbody { display: block; }
  #contacts-table tr {
    display: flex; flex-wrap: wrap; align-items: center; gap: 4px 12px;
    background: var(--panel); border-radius: 10px; padding: 10px 12px; margin-bottom: 6px;
  }
  #contacts-table td { display: block; border: 0; padding: 0; white-space: normal; font-size: 13px; }
  #contacts-table td:first-child { flex-basis: 100%; font-weight: 600; font-size: 15px; }
  #contacts-table td:empty { display: none; }
  #contacts-table td[data-label]::before { content: attr(data-label) " "; color: var(--muted); font-size: 11px; }
  #contacts-table td.num { text-align: left; }

  /* Карточка клиента — на весь экран. */
  #overlay { padding: 0; align-items: stretch; }
  #card {
    max-width: none; min-height: 100%; border: 0; border-radius: 0;
    padding: calc(12px + env(safe-area-inset-top)) calc(14px + env(safe-area-inset-right)) calc(28px + env(safe-area-inset-bottom)) calc(14px + env(safe-area-inset-left));
  }
  .card-head { flex-wrap: wrap; gap: 8px 10px; }
  .card-head h3 { flex-basis: 100%; font-size: 19px; }
  .card-head .gap { display: none; }
  .ev-buttons { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
  .ev-buttons button { min-height: 46px; }
  .card-head button, .ev .x { min-height: 44px; min-width: 44px; }
  .ev { flex-wrap: wrap; }

  /* Метрики. */
  .period { flex-wrap: wrap; }
  #owner-filter { flex-wrap: wrap; }
  .grid { grid-template-columns: 1fr; }
  .scroll { -webkit-overflow-scrolling: touch; }

  .login-page { padding: calc(16px + env(safe-area-inset-top)) 16px calc(16px + env(safe-area-inset-bottom)); }
}
