:root {
    --line: #d9dee5; --bg: #f6f8fa; --ink: #1f2a37; --muted: #6b7480;
    --ok: #e9f7ef; --soon: #fff6e0; --overdue: #fdecec;
    --soon-b: #e0a800; --overdue-b: #d23c3c; --brand: #2b5fac;
}
* { box-sizing: border-box; }
body { margin: 0; font: 14px/1.45 -apple-system, "Segoe UI", Roboto, sans-serif; color: var(--ink); background: var(--bg); }
a { color: var(--brand); text-decoration: none; }
a:hover { text-decoration: underline; }

.topbar { display: flex; align-items: center; gap: 18px; padding: 10px 18px; background: #fff; border-bottom: 1px solid var(--line); }
.brand { font-weight: 700; color: var(--ink); }
.topbar nav { display: flex; gap: 14px; }
.topbar nav a.active { font-weight: 700; color: var(--ink); border-bottom: 2px solid var(--brand); padding-bottom: 2px; }
.topbar .search { margin-left: auto; margin-bottom: 0; }
.topbar .search input { width: 230px; padding: 5px 9px; border: 1px solid var(--line); border-radius: 14px; font-size: 13px; }
.topbar .user { color: var(--muted); display: flex; align-items: center; gap: 8px; }
.inline { display: inline-flex; flex-wrap: wrap; gap: 6px; align-items: center; margin: 0; }
button.link, .link { background: none; border: none; color: var(--brand); cursor: pointer; padding: 0; font-size: inherit; }

main { padding: 18px; }
h1 { font-size: 22px; margin: 0 0 14px; }
h2 { font-size: 16px; margin: 0 0 10px; }
h3 { font-size: 14px; margin: 14px 0 8px; }

.card { background: #fff; border: 1px solid var(--line); border-radius: 8px; padding: 16px; margin-bottom: 16px; }
.cols { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; align-items: start; }
/* Грид-элементы по умолчанию имеют min-width:auto и не сжимаются уже контента —
   из-за широких таблиц/форм это даёт горизонтальную прокрутку всей страницы. */
.cols > *, .cols3 > * { min-width: 0; }
.login { max-width: 320px; margin: 60px auto; }
.login label, .stack label { display: block; margin-bottom: 10px; }
.stack { display: flex; flex-direction: column; gap: 8px; }
input, select, textarea, button { font: inherit; padding: 6px 8px; border: 1px solid var(--line); border-radius: 6px; }
button[type=submit] { background: var(--brand); color: #fff; border-color: var(--brand); cursor: pointer; }
textarea { min-height: 60px; }

.filters { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 14px; }
.messages { list-style: none; padding: 0; margin: 0 18px; }
.msg { padding: 8px 12px; border-radius: 6px; margin-top: 8px; }
.msg.error { background: var(--overdue); color: var(--overdue-b); }

.table-wrap { overflow-x: auto; }
table { border-collapse: collapse; width: 100%; background: #fff; }
.matrix th, .matrix td { border: 1px solid var(--line); padding: 6px 8px; text-align: left; vertical-align: top; font-size: 13px; }
.matrix th.agency { white-space: normal; vertical-align: bottom; min-width: 88px; max-width: 110px; font-size: 12px; line-height: 1.2; }
/* Колонка «Объект» прилипает при горизонтальной прокрутке. При
   border-collapse: collapse рамки принадлежат таблице и рисуются НИЖЕ липкой
   ячейки — уезжающие под неё ячейки просвечивали полоской по краю колонки.
   Поэтому у матрицы раздельные рамки (по одной на ячейку) — сетка та же. */
.matrix { border-collapse: separate; border-spacing: 0; }
.matrix th, .matrix td { border-width: 0 1px 1px 0; }
.matrix tr > :first-child { border-left-width: 1px; }
.matrix thead tr > * { border-top-width: 1px; }
/* z-index обязателен: без него содержимое (текст статуса, 📎) уезжающих
   колонок рисуется поверх названия и адреса объекта. */
.sticky { position: sticky; left: 0; z-index: 2; background: #fff; min-width: 200px; }
.matrix thead th.sticky { z-index: 3; }
.matrix td.sticky small { display: block; color: var(--muted); }
.matrix td.sticky small.firstdate { color: var(--brand); font-size: 11px; margin-top: 2px; }
.cell { min-width: 84px; }
.cell .att { font-size: 14px; margin-left: 4px; vertical-align: middle; white-space: nowrap; }
.cell .att.ok { opacity: 0.9; }
.list th, .list td { border-bottom: 1px solid var(--line); padding: 6px 8px; text-align: left; }
dl { display: grid; grid-template-columns: 130px 1fr; gap: 4px 10px; margin: 0; }
dt { color: var(--muted); }

.state-ok { background: var(--ok); }
.state-soon { background: var(--soon); }
.state-overdue { background: var(--overdue); color: var(--overdue-b); font-weight: 600; }
.badge { display: inline-block; font-size: 11px; padding: 1px 6px; background: var(--bg); border: 1px solid var(--line); border-radius: 10px; margin-top: 2px; }
/* Бейдж явного срока ведомства (продление / override) */
.badge.due-overdue { background: var(--overdue); border-color: var(--overdue-b); color: var(--overdue-b); font-weight: 600; }
.badge.due-soon { background: var(--soon); border-color: var(--soon-b); color: #8a6d00; }
/* Легенда цветов статусов */
.legend-toggle, details.catalog { margin: 0 0 14px; }
.legend-toggle > summary, details.catalog > summary { cursor: pointer; color: var(--muted); font-size: 13px; padding: 2px 0; }
.legend-toggle[open] > summary, details.catalog[open] > summary { margin-bottom: 8px; }
.statuslegend { display: flex; flex-wrap: wrap; gap: 6px; margin: 0 0 14px; }
.statuslegend .chip { font-size: 11px; padding: 2px 8px; border: 1px solid var(--line); border-radius: 10px; color: var(--ink); }

.case-head { display: flex; gap: 32px; flex-wrap: wrap; }
.case-head .label { display: block; color: var(--muted); font-size: 12px; }

.timeline { list-style: none; padding: 0; margin: 0; }
.timeline li { display: flex; gap: 12px; padding: 10px 0; border-bottom: 1px solid var(--line); }
.timeline .dot { width: 10px; height: 10px; border-radius: 50%; margin-top: 5px; flex: none; background: var(--muted); }
.timeline .dir-outgoing .dot { background: var(--brand); }
.timeline .dir-incoming .dot { background: var(--soon-b); }
.timeline p { margin: 4px 0 0; }
.timeline small { color: var(--muted); margin-left: 6px; }
.hint { font-size: 12px; color: var(--muted); }
.suggestion { display: block; background: var(--soon); padding: 6px 8px; border-radius: 6px; font-size: 13px; }
.attachments { margin-top: 6px; display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.attachments input[type=file] { font-size: 12px; }
.list form { margin: 0; }
.cols3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 16px; }
.cols3 h3 { padding: 4px 6px; border-radius: 6px; }
.cols3 div > div { padding: 3px 0; border-bottom: 1px solid var(--line); font-size: 13px; }
.csv { font-size: 12px; font-weight: normal; margin-left: 8px; }
.btn-new { font-size: 13px; font-weight: normal; padding: 5px 10px; background: var(--brand); color: #fff; border-radius: 6px; margin-left: 10px; vertical-align: middle; }
.btn-new:hover { text-decoration: none; opacity: .9; }

/* Сводка-легенда над реестром */
.summary { display: flex; flex-wrap: wrap; gap: 18px; align-items: center; margin: 0 0 12px; font-size: 13px; color: var(--muted); }
.summary b { color: var(--ink); }
.summary .lg i { display: inline-block; width: 10px; height: 10px; border-radius: 50%; margin-right: 5px; vertical-align: middle; }
.summary .overdue i { background: var(--overdue-b); }
.summary .soon i { background: var(--soon-b); }
.summary .ok i { background: #cfe8d6; }

/* Индикаторы состояния в ячейке (иконка + цвет, не только цвет) */
.ind { font-size: 10px; margin-right: 3px; vertical-align: middle; }
.ind.overdue { color: var(--overdue-b); }
.ind.soon { color: var(--soon-b); }

/* Подсветка строки при наведении */
.matrix tbody tr:hover td { background-color: #eef3fb; }
.matrix tbody tr:hover td.state-overdue { background-color: #f9dede; }
.matrix tbody tr:hover td.state-soon { background-color: #fdeec6; }

/* Аккордеон форм на карточке дела */
details.card > summary { cursor: pointer; font-weight: 600; list-style: none; }
details.card > summary::-webkit-details-marker { display: none; }
details.card[open] > summary { margin-bottom: 12px; }
details.card > form { margin: 0; }
.copybox textarea { width: 100%; box-sizing: border-box; font-family: ui-monospace, "SF Mono", Menlo, monospace; font-size: 13px; line-height: 1.5; padding: 8px; border: 1px solid var(--line); border-radius: 6px; margin: 8px 0; resize: vertical; }

/* Сортировка, пагинация, счётчик найденного */
.matrix th.sticky .sort { color: var(--muted); font-size: 10px; text-decoration: none; margin-left: 2px; }
.matrix th.sticky .sort.on { color: var(--brand); }
.found { color: var(--muted); font-size: 13px; margin-left: 6px; }
.pager { display: flex; gap: 16px; align-items: center; justify-content: center; margin: 14px 0; color: var(--muted); }
.presets { color: var(--muted); font-size: 13px; margin-bottom: 14px; }
.plan-done td { color: var(--muted); text-decoration: line-through; }
.plan-done td a { color: var(--muted); }
.plan-summary { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; margin-bottom: 14px; font-size: 13px; color: var(--muted); }
.plan-summary b { color: var(--ink); }
.plan-summary .badge { background: var(--ok); border-color: #cfe8d6; }

/* Адаптив: на узких экранах фильтры в столбик, таблица скроллится */
@media (max-width: 700px) {
    main { padding: 12px; }
    .filters { flex-direction: column; align-items: stretch; }
    .filters select, .filters input { width: 100%; }
    .cols, .cols3 { grid-template-columns: 1fr; }
    .case-head { gap: 16px; }
    .topbar { flex-wrap: wrap; gap: 8px 14px; }
    .topbar .user { margin-left: 0; width: 100%; }
    .topbar .search { margin-left: 0; width: 100%; }
    .topbar .search input { width: 100%; }
    .matrix th.agency { min-width: 70px; font-size: 11px; }
    .sticky { min-width: 150px; }
}
tr.grp-positive td:first-child { color: #1c7c3e; }
tr.grp-negative td:first-child { color: var(--overdue-b); }
