:root {
  --bg: #f4f4f2;
  --surface: #ffffff;
  --line: #e2e1dd;
  --line-strong: #cbc9c3;
  --text: #23231f;
  --muted: #6f6e68;
  --accent: #185fa5;
  --accent-bg: #e6f1fb;
  --danger: #a32d2d;
  --danger-bg: #fcebeb;
  --warn-bg: #faeeda;
  --warn-text: #633806;
  --ok-bg: #e1f5ee;
  --ok-text: #085041;
  --radius: 8px;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.55 -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

/* --- шапка --- */

.top {
  display: flex; align-items: center; gap: 28px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
  padding: 0 20px; height: 54px;
  position: sticky; top: 0; z-index: 10;
}
.brand { font-weight: 600; letter-spacing: .04em; color: var(--text); }
.brand span { color: var(--muted); font-weight: 400; margin-left: 6px; }
.top nav { display: flex; gap: 4px; margin-right: auto; }
.top nav a {
  padding: 6px 12px; border-radius: var(--radius);
  color: var(--muted); font-size: 14px;
}
.top nav a:hover { background: var(--bg); text-decoration: none; }
.top nav a.on { background: var(--accent-bg); color: var(--accent); }
.me { display: flex; align-items: center; gap: 12px; font-size: 13px; color: var(--muted); }

main { padding: 20px; max-width: 1600px; margin: 0 auto; }

/* --- общие элементы --- */

h1 { font-size: 20px; font-weight: 600; margin: 0 0 16px; }
h2 { font-size: 15px; font-weight: 600; margin: 0 0 12px; }

.bar { display: flex; align-items: center; gap: 12px; margin-bottom: 16px; }
.bar h1 { margin: 0; margin-right: auto; }

button, .btn {
  font: inherit; font-size: 14px;
  padding: 7px 14px; border-radius: var(--radius);
  border: 1px solid var(--line-strong); background: var(--surface);
  color: var(--text); cursor: pointer;
}
button:hover, .btn:hover { background: var(--bg); text-decoration: none; }
button.primary, .btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
button.primary:hover, .btn.primary:hover { opacity: .9; background: var(--accent); }
button.link { border: none; background: none; color: var(--muted); padding: 4px 6px; }
button.tiny { padding: 3px 8px; font-size: 12px; }
button.danger { background: var(--danger-bg); border-color: #f09595; color: var(--danger); }
button.danger:hover { background: #f7c1c1; }

input, select, textarea {
  font: inherit; font-size: 14px; width: 100%;
  padding: 7px 10px; border: 1px solid var(--line-strong);
  border-radius: var(--radius); background: var(--surface); color: var(--text);
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent-bg); border-color: var(--accent); }
textarea { min-height: 68px; resize: vertical; }
label { display: block; font-size: 13px; color: var(--muted); margin-bottom: 4px; }

.card {
  background: var(--surface); border: 1px solid var(--line);
  border-radius: 12px; padding: 18px; margin-bottom: 16px;
}

.pill {
  display: inline-block; padding: 2px 9px; border-radius: 11px;
  font-size: 12px; background: var(--bg); color: var(--muted);
}
.pill.warn { background: var(--warn-bg); color: var(--warn-text); }
.pill.ok { background: var(--ok-bg); color: var(--ok-text); }
.pill.stalled { background: var(--danger-bg); color: var(--danger); }

.muted { color: var(--muted); }
.empty { color: var(--muted); padding: 28px; text-align: center; }
.error { background: var(--danger-bg); color: var(--danger); padding: 10px 14px; border-radius: var(--radius); margin-bottom: 14px; font-size: 14px; }
.ok-note { background: var(--ok-bg); color: var(--ok-text); padding: 10px 14px; border-radius: var(--radius); margin-bottom: 14px; font-size: 14px; }
.hint { font-size: 13px; color: var(--muted); margin: -6px 0 16px; }

/* --- таблица --- */

/* Тот же приём, что и на канбане: таблица живёт в окне высотой с экран,
   поэтому горизонтальная полоса всегда под рукой, а не в конце страницы. */
.table-wrap {
  background: var(--surface); border: 1px solid var(--line); border-radius: 12px;
  max-height: calc(100vh - 146px); overflow: auto;
}
.table-wrap::-webkit-scrollbar { width: 12px; height: 12px; }
.table-wrap::-webkit-scrollbar-track { background: var(--line); border-radius: 6px; }
.table-wrap::-webkit-scrollbar-thumb { background: var(--line-strong); border-radius: 6px; }
.table-wrap::-webkit-scrollbar-thumb:hover { background: var(--muted); }
.table-wrap::-webkit-scrollbar-corner { background: var(--line); }

table { border-collapse: separate; border-spacing: 0; width: 100%; font-size: 14px; }
th {
  text-align: left; font-weight: 500; color: var(--muted); font-size: 13px;
  padding: 8px 12px; border-bottom: 1px solid var(--line); white-space: nowrap;
  position: sticky; top: 0; background: var(--surface); z-index: 2;
}
td { padding: 5px 12px; border-bottom: 1px solid var(--line); white-space: nowrap; }
tbody tr:last-child td { border-bottom: none; }
tbody tr:hover td { background: var(--bg); }
td.num { font-variant-numeric: tabular-nums; }

/* Колонка ЗН остаётся на месте при прокрутке вбок — иначе на восемнадцати
   колонках непонятно, чью строку смотришь. */
.orders tbody td:first-child, .orders thead th:first-child {
  position: sticky; left: 0; background: var(--surface); z-index: 1;
}
.orders thead th:first-child { z-index: 3; }
.orders tbody tr:hover td:first-child { background: var(--bg); }

/* Списки внутри таблицы компактнее обычных полей — строка не должна расти. */
table select { padding: 2px 26px 2px 8px; font-size: 13px; height: 26px; }

/* --- цветные метки стадий и статусов --- */

.pill.c-gray   { background: #f1efe8; color: #444441; }
.pill.c-red    { background: #fcebeb; color: #a32d2d; }
.pill.c-amber  { background: #faeeda; color: #633806; }
.pill.c-blue   { background: #e6f1fb; color: #0c447c; }
.pill.c-teal   { background: #e1f5ee; color: #085041; }
.pill.c-purple { background: #eeedfe; color: #3c3489; }
.pill.c-green  { background: #eaf3de; color: #27500a; }

/* Выпадающий список, покрашенный в цвет выбранного значения — стадия и статус
   проекта читаются с той же скоростью, что и метки в списке заказов.
   Отступ справа обязателен: без него стрелка встаёт прямо на буквы
   у длинных значений вроде «КП / согласование». */
select.tinted { font-weight: 500; padding-right: 26px; }
select.tinted.c-gray   { background: #f1efe8; color: #444441; border-color: #cbc9c3; }
select.tinted.c-red    { background: #fcebeb; color: #a32d2d; border-color: #f09595; }
select.tinted.c-amber  { background: #faeeda; color: #633806; border-color: #ef9f27; }
select.tinted.c-blue   { background: #e6f1fb; color: #0c447c; border-color: #85b7eb; }
select.tinted.c-teal   { background: #e1f5ee; color: #085041; border-color: #5dcaa5; }
select.tinted.c-purple { background: #eeedfe; color: #3c3489; border-color: #afa9ec; }
select.tinted.c-green  { background: #eaf3de; color: #27500a; border-color: #97c459; }

.dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 7px; vertical-align: 1px; }
.dot.c-gray { background: #888780; }
.dot.c-red { background: #e24b4a; }
.dot.c-amber { background: #ef9f27; }
.dot.c-blue { background: #378add; }
.dot.c-teal { background: #1d9e75; }
.dot.c-purple { background: #7f77dd; }
.dot.c-green { background: #639922; }

/* --- канбан --- */

/* Доска занимает высоту экрана: горизонтальная полоса прокрутки всегда на виду,
   а карточки внутри колонки крутятся сами. Иначе, чтобы прокрутить доску вбок,
   приходится сначала уезжать в самый низ страницы. */
.board {
  display: flex; gap: 12px;
  height: calc(100vh - 148px); min-height: 320px;
  overflow-x: auto; overflow-y: hidden;
  padding-bottom: 4px;
}
.board::-webkit-scrollbar { height: 12px; }
.board::-webkit-scrollbar-track { background: var(--line); border-radius: 6px; }
.board::-webkit-scrollbar-thumb { background: var(--line-strong); border-radius: 6px; }
.board::-webkit-scrollbar-thumb:hover { background: var(--muted); }

.col {
  background: var(--surface); border: 1px solid var(--line); border-radius: 12px;
  padding: 10px; min-width: 224px; flex: 1 0 224px;
  display: flex; flex-direction: column; max-height: 100%;
}
.col > h2 {
  display: flex; justify-content: space-between; align-items: center;
  font-size: 13px; color: var(--muted); font-weight: 500; flex: none;
}
.col .cards { overflow-y: auto; overflow-x: hidden; flex: 1 1 auto; min-height: 0; padding-right: 2px; }
.col .cards::-webkit-scrollbar { width: 8px; }
.col .cards::-webkit-scrollbar-thumb { background: var(--line-strong); border-radius: 4px; }
.tile {
  display: block; background: var(--bg); border-radius: var(--radius);
  padding: 9px 10px; margin-bottom: 8px; color: var(--text);
}
/* Рамки рисуем внутренней тенью, а не outline: outline лежит снаружи границы
   элемента и обрезается колонкой с прокруткой — у просроченных карточек
   пропадал левый край. */
.tile:hover { text-decoration: none; box-shadow: inset 0 0 0 1px var(--line-strong); }
/* Длинное имя клиента переносится, а не растягивает колонку. */
.tile b { display: block; font-weight: 500; font-size: 14px; overflow-wrap: anywhere; }
.tile small { display: block; color: var(--muted); font-size: 12px; overflow-wrap: anywhere; }
.tile small .pill { max-width: 100%; }
.tile.stalled { box-shadow: inset 0 0 0 1px var(--danger); }
.tile.stalled:hover { box-shadow: inset 0 0 0 2px var(--danger); }

/* Список стадий по умолчанию раздаётся до ширины самого длинного варианта
   («КП / согласование») и вылезает за край колонки. Ширину задаём сами. */
.move { display: flex; margin-top: 6px; min-width: 0; }
.move select {
  font-size: 12px; padding: 3px 22px 3px 6px; height: 24px;
  width: 100%; min-width: 0; max-width: 100%;
}

/* --- карточка заказа --- */

.grid2 { display: grid; grid-template-columns: minmax(0, 2fr) minmax(0, 1fr); gap: 16px; align-items: start; }
.fields { display: grid; grid-template-columns: 170px minmax(0, 1fr); gap: 10px 14px; align-items: center; }
.fields .lab { font-size: 13px; color: var(--muted); }
.fields .val { font-size: 14px; }
.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 14px; }

.files { list-style: none; padding: 0; margin: 0; }
.files li { display: flex; align-items: center; gap: 10px; padding: 7px 0; border-bottom: 1px solid var(--line); font-size: 14px; }
.files li:last-child { border-bottom: none; }
.files .size { color: var(--muted); font-size: 12px; margin-left: auto; }

.history { list-style: none; padding: 0; margin: 0; font-size: 13px; }
.history li { padding: 7px 0; border-bottom: 1px solid var(--line); }
.history li:last-child { border-bottom: none; }
.history .when { color: var(--muted); font-size: 12px; }

/* --- матрица прав --- */

.matrix tbody td:first-child, .matrix thead th:first-child {
  position: sticky; left: 0; background: var(--surface); z-index: 1;
}
.matrix thead th:first-child { z-index: 3; }
.matrix tbody tr:hover td:first-child { background: var(--bg); }
.matrix .group td { position: sticky; left: 0; }
.matrix select { font-size: 13px; padding: 4px 6px; min-width: 104px; }
.matrix .group td { background: var(--bg); font-weight: 500; color: var(--muted); font-size: 13px; }

/* --- аналитика --- */

.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 12px; margin-bottom: 16px; }
.tile-stat { background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 14px 16px; }
.tile-stat .lab { font-size: 13px; color: var(--muted); }
.tile-stat .val { font-size: 26px; font-weight: 500; margin-top: 2px; font-variant-numeric: tabular-nums; }
.tile-stat .val.alarm { color: var(--danger); }

.blocks { display: grid; grid-template-columns: repeat(auto-fit, minmax(330px, 1fr)); gap: 16px; align-items: start; }
.bars { display: grid; grid-template-columns: minmax(88px, auto) minmax(0, 1fr) auto; gap: 9px 10px; align-items: center; font-size: 13px; }
.bars .bl { color: var(--muted); }
.bars .bt { display: block; width: 100%; }
.bars .bf { display: block; height: 15px; border-radius: 3px; }
.bars .bv { text-align: right; font-variant-numeric: tabular-nums; }

.bf.c-gray { background: #888780; }
.bf.c-red { background: #e24b4a; }
.bf.c-amber { background: #ef9f27; }
.bf.c-blue { background: #378add; }
.bf.c-teal { background: #1d9e75; }
.bf.c-purple { background: #7f77dd; }
.bf.c-green { background: #639922; }

/* --- второй фактор --- */

.qr { background: #fff; padding: 12px; border-radius: var(--radius); display: inline-block; }
.qr svg { display: block; width: 200px; height: 200px; }

code.secret {
  display: inline-block; font-family: var(--font-mono, monospace);
  font-size: 15px; letter-spacing: .08em; background: var(--bg);
  padding: 8px 12px; border-radius: var(--radius); user-select: all;
}

.codes {
  display: grid; grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
  gap: 8px; margin: 14px 0;
}
.codes span {
  font-family: var(--font-mono, monospace); font-size: 15px;
  background: var(--bg); padding: 9px 12px; border-radius: var(--radius);
  text-align: center; user-select: all;
}

/* --- вход --- */

.login { max-width: 340px; margin: 12vh auto; }
.login .card { padding: 24px; }
.login h1 { text-align: center; margin-bottom: 20px; }
.login label { margin-top: 12px; }
.login button { width: 100%; margin-top: 20px; }

/* --------------------------------------------------------------------------
   Телефон и узкие экраны
   -------------------------------------------------------------------------- */

@media (max-width: 760px) {

  main { padding: 12px; }
  h1 { font-size: 18px; }

  /* Шапка перестаёт быть одной строкой: на 375 пикселях меню в неё не влезало
     и растягивало страницу вбок — содержимое занимало 701 пиксель. */
  .top {
    height: auto; flex-wrap: wrap; gap: 8px 14px;
    padding: 10px 12px; align-items: center;
  }
  .top nav { order: 3; width: 100%; margin-right: 0; gap: 2px; }
  .top nav a { flex: 1 1 auto; text-align: center; padding: 7px 8px; }
  .me { margin-left: auto; font-size: 12px; }
  .me a { max-width: 46vw; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

  .bar { flex-wrap: wrap; gap: 8px; }

  /* Список заказов раскладывается карточками: девятнадцать колонок на телефоне
     читать невозможно, а горизонтальная прокрутка на каждой строке мучительна. */
  .table-wrap { max-height: none; overflow: visible; border: none; background: none; }
  .orders, .orders tbody, .orders tr, .orders td { display: block; width: 100%; }
  .orders thead { display: none; }

  .orders tr {
    background: var(--surface); border: 1px solid var(--line);
    border-radius: 12px; margin-bottom: 10px; padding: 4px 0;
  }
  .orders tbody tr:hover td { background: none; }

  .orders td {
    display: flex; align-items: center; justify-content: space-between;
    gap: 12px; white-space: normal; text-align: right;
    padding: 7px 14px; border: none;
    position: static !important;
  }
  .orders td + td { border-top: 1px solid var(--line); }
  .orders td::before {
    content: attr(data-label);
    color: var(--muted); font-size: 13px; text-align: left; flex: 0 0 auto;
  }
  /* Первая ячейка — заголовок карточки: подпись «ЗН» тут лишняя. */
  .orders td:first-child {
    font-weight: 500; font-size: 16px; text-align: left;
    padding-top: 11px; padding-bottom: 11px;
  }
  .orders td:first-child::before { content: none; }
  .orders td select { max-width: 60%; }

  /* На телефоне карточка заказа показывает только главное: девятнадцать строк
     на один заказ превращали список в простыню. Остальное — в карточке заказа. */
  .orders td.wide-only { display: none; }


  /* Канбан: колонка почти во весь экран, листается вбок пальцем. */
  .board { height: auto; max-height: none; overflow-x: auto; padding-bottom: 10px; }
  .col { min-width: 82vw; flex: 0 0 82vw; max-height: 70vh; }

  /* Карточка заказа в одну колонку. */
  .grid2 { grid-template-columns: 1fr; }
  .fields { grid-template-columns: 1fr; gap: 3px 0; }
  .fields .lab { margin-top: 10px; }
  .blocks { grid-template-columns: 1fr; }
  .tiles { grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); }

  /* Матрица прав на телефоне остаётся таблицей — иначе её не сопоставить. */
  .matrix { font-size: 13px; }
  .table-wrap:has(.matrix) {
    overflow: auto; max-height: 70vh;
    border: 1px solid var(--line); background: var(--surface);
  }

  .login { margin: 6vh auto; }
  .codes { grid-template-columns: repeat(2, 1fr); }
  .qr svg { width: 168px; height: 168px; }
}

/* Установленное на главный экран приложение: учитываем вырез и нижнюю полосу. */
@media (display-mode: standalone) {
  .top { padding-top: calc(10px + env(safe-area-inset-top)); }
  main { padding-bottom: calc(12px + env(safe-area-inset-bottom)); }
}
