:root {
    --bg: #f5f6f8;
    --panel: #ffffff;
    --line: #dfe3e8;
    --text: #1b1f24;
    --muted: #6b7480;
    --accent: #1f6feb;
    --accent-soft: #e8f0fe;
    --ok: #157f3d;
    --warn: #9a6700;
}

* { box-sizing: border-box; }

body {
    margin: 0;
    background: var(--bg);
    color: var(--text);
    font: 14px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, sans-serif;
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

.top {
    display: flex;
    align-items: baseline;
    gap: 16px;
    padding: 12px 20px;
    background: var(--panel);
    border-bottom: 1px solid var(--line);
    position: sticky;
    top: 0;
    z-index: 10;
}
.top h1 { font-size: 17px; margin: 0; font-weight: 600; }
.top .muted { color: var(--muted); font-size: 13px; }

.layout { display: flex; align-items: flex-start; gap: 16px; padding: 16px 20px 40px; }

.filters {
    width: 290px;
    flex: 0 0 290px;
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: 8px;
    padding: 14px;
    position: sticky;
    top: 60px;
    max-height: calc(100vh - 80px);
    overflow-y: auto;
}
.filters h2 { font-size: 13px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); margin: 0 0 10px; }
.filter { border-top: 1px solid var(--line); padding: 10px 0; }
.filter:first-of-type { border-top: 0; }
.filter > label, .filter > .title { display: block; font-weight: 600; margin-bottom: 6px; font-size: 13px; }
.filter .options { max-height: 190px; overflow-y: auto; }
.filter .option { display: flex; align-items: center; gap: 6px; padding: 2px 0; cursor: pointer; }
.filter .option input { margin: 0; }
.filter .option .count { margin-left: auto; color: var(--muted); font-size: 12px; }
.filter .row { display: flex; gap: 6px; }
.filter input[type="text"], .filter input[type="number"], .filter input[type="date"], .filter select {
    width: 100%;
    padding: 5px 7px;
    border: 1px solid var(--line);
    border-radius: 5px;
    font: inherit;
    background: #fff;
}
.quick { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 6px; }
.quick button {
    border: 1px solid var(--line);
    background: #fff;
    border-radius: 12px;
    padding: 2px 9px;
    font-size: 12px;
    cursor: pointer;
    color: var(--muted);
}
.quick button:hover { border-color: var(--accent); color: var(--accent); }
.actions { display: flex; gap: 8px; margin-top: 12px; }
button.primary {
    background: var(--accent); color: #fff; border: 0; border-radius: 6px;
    padding: 7px 14px; font: inherit; cursor: pointer;
}
button.link { background: none; border: 0; color: var(--muted); cursor: pointer; font: inherit; padding: 7px 0; }
button.link:hover { color: var(--accent); }

.results { flex: 1; min-width: 0; }
.summary {
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: 8px;
    padding: 10px 14px;
    margin-bottom: 12px;
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 18px;
}
.summary .found { font-size: 16px; font-weight: 600; }
.summary .metric { color: var(--muted); font-size: 13px; }
.summary .metric b { color: var(--text); font-weight: 600; }
.summary .controls { margin-left: auto; display: flex; gap: 8px; align-items: center; }
.summary select { padding: 5px 7px; border: 1px solid var(--line); border-radius: 5px; font: inherit; background: #fff; }

table { width: 100%; border-collapse: collapse; background: var(--panel); border: 1px solid var(--line); border-radius: 8px; overflow: hidden; }
th, td { padding: 8px 10px; text-align: left; border-bottom: 1px solid var(--line); vertical-align: top; }
th { font-size: 12px; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); font-weight: 600; white-space: nowrap; }
tbody tr { cursor: pointer; }
tbody tr:hover { background: var(--accent-soft); }
td.num { text-align: right; white-space: nowrap; font-variant-numeric: tabular-nums; }
td .title { display: block; max-width: 380px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
td .sub { color: var(--muted); font-size: 12px; }
.badge { display: inline-block; padding: 1px 7px; border-radius: 10px; font-size: 12px; background: #eef1f4; color: var(--muted); white-space: nowrap; }
.badge.active { background: #e6f4ea; color: var(--ok); }
.badge.wait { background: #fdf3e2; color: var(--warn); }

.pager { display: flex; gap: 8px; align-items: center; justify-content: center; padding: 14px 0; }
.pager button { border: 1px solid var(--line); background: #fff; border-radius: 6px; padding: 5px 12px; cursor: pointer; font: inherit; }
.pager button[disabled] { opacity: .45; cursor: default; }
.state { padding: 30px; text-align: center; color: var(--muted); background: var(--panel); border: 1px solid var(--line); border-radius: 8px; }

.card { background: var(--panel); border: 1px solid var(--line); border-radius: 8px; padding: 18px 20px; margin-bottom: 14px; }
.card h2 { font-size: 14px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); margin: 0 0 12px; }
.card dl { display: grid; grid-template-columns: 230px 1fr; gap: 8px 16px; margin: 0; }
.card dt { color: var(--muted); }
.card dd { margin: 0; }
.card .price { font-size: 22px; font-weight: 600; }
details pre { background: #0f1720; color: #e6edf3; padding: 12px; border-radius: 6px; overflow-x: auto; font-size: 12px; line-height: 1.4; }

@media (max-width: 900px) {
    .layout { flex-direction: column; padding: 12px; }
    .filters { width: 100%; flex: none; position: static; max-height: none; }
    .card dl { grid-template-columns: 1fr; gap: 2px 0; }
    .card dd { margin-bottom: 8px; }
    table { display: block; overflow-x: auto; }
}

/* Карта (§18–21 ТЗ). Точка ГИС — геокод адреса, поэтому подпись под картой обязательна. */
#map { height: 420px; border: 1px solid var(--line); border-radius: 7px; }
/* position: relative нужен оверлею затуманивания: вешать его на сам #map нельзя, Leaflet
   ставит своему контейнеру собственное позиционирование, а его панели живут до z-index 800. */
#map-panel { margin-bottom: 14px; position: relative; }
.map-note { margin: 7px 2px 0; font-size: 12px; color: var(--muted); line-height: 1.5; }
.map-note #map-count { color: var(--text); font-weight: 600; margin-right: 4px; }
.summary .views { display: flex; border: 1px solid var(--line); border-radius: 5px; overflow: hidden; }
.summary .views button {
    padding: 5px 10px; border: 0; background: #fff; font: inherit; color: var(--muted); cursor: pointer;
}
.summary .views button.on { background: var(--accent, #2f6fd0); color: #fff; }
.popup { font-size: 13px; line-height: 1.5; }
.popup p { margin: 8px 0 0; }
.leaflet-popup-content { margin: 10px 12px; }

/* Знаки на карте. Кластер и одиночная точка — одна гамма (синий акцент): отличаются размером
   и числом, а не цветом. Зелёный остаётся только у точки «сейчас на торгах». Цвета обязаны
   совпадать с styleFor() в nspd.js: Leaflet красит SVG-контуры атрибутами, а div-значки — стилями. */
.parcel-dot, .parcel-cluster { cursor: pointer; }

.parcel-dot { border-radius: 50%; background: rgba(31, 111, 235, .22); }
.parcel-dot::after {
    content: ''; display: block; box-sizing: border-box;
    width: 14px; height: 14px; margin: 4px;
    border-radius: 50%;
    background: var(--accent);
    border: 2px solid #fff;
    box-shadow: 0 1px 2px rgba(27, 31, 36, .35);
}
.parcel-dot.on-torgi { background: rgba(26, 127, 55, .22); }
.parcel-dot.on-torgi::after { background: #1a7f37; }

.parcel-cluster { border-radius: 50%; background: rgba(31, 111, 235, .22); }
.parcel-cluster span {
    display: flex; align-items: center; justify-content: center;
    width: calc(100% - 10px); height: calc(100% - 10px); margin: 5px;
    border-radius: 50%;
    background: var(--accent);
    border: 2px solid #fff;
    color: #fff; font-weight: 600; font-size: 12px; white-space: nowrap;
    box-shadow: 0 1px 2px rgba(27, 31, 36, .35);
}
.parcel-cluster-lg span { font-size: 13px; }
.parcel-cluster.on-torgi { background: rgba(26, 127, 55, .22); }
.parcel-cluster.on-torgi span { background: #1a7f37; }

/* Легенда: зелёный цвет точки иначе ничего не объясняет. */
.map-legend { display: inline-flex; flex-wrap: wrap; align-items: center; gap: 4px 12px; margin-left: 10px; }
.map-legend > span { display: inline-flex; align-items: center; gap: 6px; }
.map-legend i {
    width: 12px; height: 12px; border-radius: 50%;
    background: var(--accent); border: 2px solid #fff;
    box-shadow: 0 0 0 3px rgba(31, 111, 235, .22);
}
.map-legend i.on-torgi { background: #1a7f37; box-shadow: 0 0 0 3px rgba(26, 127, 55, .22); }
.map-legend i.group.on-torgi { background: #1a7f37; box-shadow: 0 0 0 3px rgba(26, 127, 55, .22); }
.map-legend i.group {
    display: inline-flex; align-items: center; justify-content: center;
    width: 18px; height: 18px;
    background: var(--accent);
    box-shadow: 0 0 0 3px rgba(31, 111, 235, .22);
    color: #fff; font-size: 10px; font-weight: 600; font-style: normal; line-height: 1;
}

/* ---------- Вход и пользователи ---------- */

.auth-page { min-height: 100vh; display: flex; }
.auth {
    flex: 1; display: flex; align-items: center; justify-content: center; padding: 24px;
    background: linear-gradient(160deg, #eef2f8 0%, #f7f8fa 55%, #eaf0f9 100%);
}
.auth-card {
    width: 100%; max-width: 380px; background: var(--panel); border: 1px solid var(--line);
    border-radius: 12px; padding: 28px 28px 22px; box-shadow: 0 12px 36px rgba(24, 39, 75, .10);
}
.auth-card h1 { margin: 0; font-size: 20px; font-weight: 650; letter-spacing: -.01em; }
.auth-sub { margin: 5px 0 22px; color: var(--muted); font-size: 13px; }
.auth-card label { display: block; margin: 0 0 5px; font-size: 13px; color: var(--muted); }
.auth-card input[type="text"], .auth-card input[type="password"] {
    width: 100%; padding: 9px 11px; margin-bottom: 15px; border: 1px solid var(--line);
    border-radius: 7px; font: inherit; background: #fff;
}
.auth-card input:focus { outline: 2px solid var(--accent-soft); border-color: var(--accent); }
.auth-card button.primary { width: 100%; padding: 10px; font-size: 14px; }
.auth-error {
    margin: 0 0 16px; padding: 9px 12px; border-radius: 7px; font-size: 13px;
    background: #fdeceb; color: #a01a13; border: 1px solid #f6cdc9;
}
.auth-note { margin: 18px 0 0; font-size: 12px; color: var(--muted); text-align: center; }

.top h1 a { color: inherit; }
.nav { display: flex; gap: 14px; margin-left: 8px; }
.nav a { color: var(--muted); font-size: 13px; }
.nav a.on { color: var(--text); font-weight: 600; }
.top .who { margin-left: auto; font-size: 13px; color: var(--muted); }
.top .tag { font-size: 11px; background: var(--accent-soft); color: var(--accent); border-radius: 9px; padding: 1px 7px; }
.top .logout { margin: 0; }
.top .logout button { padding: 0; }
.subbar { display: flex; gap: 10px; align-items: center; padding: 8px 20px 0; }

.layout.narrow { display: block; max-width: 980px; margin: 0 auto; }
.notice { border-left: 3px solid var(--accent); }
.notice.error { border-left-color: #c0392b; }
.ok-note { color: var(--ok); margin: 0 0 12px; }
.secret { margin: 10px 0 4px; }
.secret code {
    font-size: 15px; background: #f1f4f8; border: 1px solid var(--line);
    border-radius: 6px; padding: 3px 8px; user-select: all;
}
.card dl.pairs { grid-template-columns: 200px 1fr; }
.stack { display: flex; flex-direction: column; max-width: 340px; }
.stack label { font-size: 13px; color: var(--muted); margin-bottom: 4px; }
.stack input { padding: 8px 10px; margin-bottom: 12px; border: 1px solid var(--line); border-radius: 7px; font: inherit; }
.stack .muted { font-size: 12px; margin: 0 0 12px; }
.stack button { align-self: flex-start; }

.grid-form { display: flex; flex-wrap: wrap; gap: 12px; align-items: flex-end; }
.grid-form label { display: flex; flex-direction: column; font-size: 13px; color: var(--muted); gap: 4px; }
.grid-form input, .grid-form select { padding: 8px 10px; border: 1px solid var(--line); border-radius: 7px; font: inherit; }

table.users { width: 100%; border-collapse: collapse; }
table.users th { text-align: left; font-size: 12px; color: var(--muted); font-weight: 500; padding: 6px 10px 6px 0; }
table.users td { padding: 7px 10px 7px 0; border-top: 1px solid var(--line); vertical-align: middle; }
table.users tr.off { opacity: .55; }
table.users .sub { display: block; font-size: 11px; color: var(--muted); }
.inline { display: inline-flex; gap: 5px; align-items: center; margin: 0; }
.inline input[type="password"], .inline input[type="text"], .inline select { padding: 4px 7px; border: 1px solid var(--line); border-radius: 6px; font: inherit; }
button.link.danger { color: #c0392b; }

/* Данные НСПД в карточке лота (ТЗ №2 §12, §21, §88) */
.notice-line { margin: 0 0 12px; padding: 8px 10px; border-radius: 4px; background: var(--accent-soft); font-size: 13px; }
.card .actions { margin-top: 12px; display: flex; gap: 10px; align-items: center; }
.card dl + dl { margin-top: 14px; padding-top: 14px; border-top: 1px solid var(--line); }

/* ---------- Подбор по эталону ---------- */

.reference {
    margin: -14px -14px 0;
    padding: 12px 14px 14px;
    background: var(--accent-soft);
    border-bottom: 1px solid var(--line);
}
.ref + .ref { margin-top: 14px; padding-top: 12px; border-top: 1px solid #cfdcf7; }
.ref-head { display: flex; align-items: baseline; gap: 8px; }
.ref-label {
    font-size: 11px; text-transform: uppercase; letter-spacing: .05em;
    color: var(--accent); font-weight: 700;
}
.ref-head a { font-size: 13px; font-weight: 600; }
.ref-title { font-size: 12px; color: var(--muted); margin-top: 2px; }
.ref-facts {
    display: grid; grid-template-columns: auto 1fr; gap: 2px 10px;
    margin: 10px 0 0; font-size: 12px;
}
.ref-facts dt { color: var(--muted); }
.ref-facts dd { margin: 0; }
.ref-search { margin-top: 12px; }
.ref-search > .title {
    display: block; font-weight: 600; font-size: 12px; margin-bottom: 5px;
}
.ref-search .muted { font-size: 12px; margin: 6px 0 0; }
.ref-option { align-items: flex-start; font-size: 12px; }
.ref-option.nested { padding-left: 18px; }
.ref-option input { margin-top: 3px; }
.ref-option input:disabled + span { opacity: .5; }
.ref-option .sub { display: block; color: var(--muted); }
.ref-search button.primary { margin-top: 10px; padding: 6px 12px; }
.ref-count { margin-left: 8px; font-size: 12px; }

/* ---------- «База НСПД»: эталонная раскладка ---------- */

/* Вид «оба»: карта сверху, список под ней. Правил под .with-map в проекте не было вовсе —
   класс навешивался вхолостую, и «оба» ничем не отличалось от «списка с картой». */
.results.with-map #map { height: 340px; }
.results.map-only #map { height: calc(100vh - 200px); }

.ref-criteria {
    margin: 8px 0 0;
    font-size: 12px;
    line-height: 1.5;
    color: var(--text);
    font-weight: 600;
}
/* Переключатель торгов — тот же вид, что и у переключателя «Список / Карта / Оба». */
.ref-torgi { display: flex; margin-top: 10px; border: 1px solid var(--line); border-radius: 5px; overflow: hidden; }
.ref-torgi button {
    flex: 1; padding: 5px 6px; border: 0; background: #fff;
    font: inherit; font-size: 12px; color: var(--muted); cursor: pointer;
}
.ref-torgi button + button { border-left: 1px solid var(--line); }
.ref-torgi button.on { background: var(--accent); color: #fff; }
.districts { max-height: 320px; overflow-y: auto; }
.districts .option { font-size: 13px; }
.districts .option .count { margin-left: auto; color: var(--muted); font-size: 12px; }

.more-filters { border-top: 1px solid var(--line); margin-top: 10px; }
.more-filters > summary {
    cursor: pointer;
    padding: 10px 0;
    font-weight: 600;
    font-size: 13px;
    color: var(--muted);
    list-style: none;
}
.more-filters > summary::-webkit-details-marker { display: none; }
.more-filters > summary::before { content: '▸ '; }
.more-filters[open] > summary::before { content: '▾ '; }
.more-filters > summary:hover { color: var(--accent); }

/* ---------- Карточка окном в окне ---------- */

/* Нативный <dialog>: top layer снимает войну z-index с панелями Leaflet, а ловушка фокуса,
   Esc и ::backdrop достаются бесплатно. */
dialog.modal {
    width: min(980px, calc(100vw - 48px));
    max-height: calc(100vh - 48px);
    padding: 0;
    border: 0;
    border-radius: 10px;
    background: var(--bg);
    box-shadow: 0 24px 64px rgba(16, 24, 40, .28);
    overflow: hidden;
}
dialog.modal::backdrop { background: rgba(16, 24, 40, .45); }

/* Прокрутка на теле, а не на самом <dialog>: иначе не работает sticky у шапки карточки. */
.modal-body {
    max-height: calc(100vh - 48px);
    overflow-y: auto;
    overscroll-behavior: contain;
    padding: 0 16px 16px;
}
.modal-body .state { margin-top: 16px; }

.card-head {
    position: sticky;
    top: 0;
    z-index: 2;
    display: flex;
    align-items: center;
    gap: 10px;
    flex-wrap: wrap;
    padding: 14px 0 12px;
    margin-bottom: 4px;
    background: var(--bg);
    border-bottom: 1px solid var(--line);
}
.card-head .card-title { font-size: 16px; font-weight: 600; margin: 0; text-transform: none; letter-spacing: 0; }
.card-actions { margin-left: auto; display: flex; align-items: center; gap: 12px; }
.card-standalone { font-size: 12px; }
.card-close {
    border: 0; background: none; cursor: pointer; font-size: 24px; line-height: 1;
    color: var(--muted); padding: 0 4px;
}
.card-close:hover { color: var(--text); }

/* Не id="map": на странице уже есть своя большая карта с этим id, и L.map() взял бы её. */
.card-map { height: 360px; border: 1px solid var(--line); border-radius: 7px; }

/* На отдельной странице карточки шапка окна не нужна — её кнопки скрыты атрибутом hidden. */
.layout .card-doc .card-head { position: static; background: none; }

@media (max-width: 900px) {
    dialog.modal {
        width: 100vw; max-width: none; height: 100dvh; max-height: none;
        inset: 0; margin: 0; border-radius: 0;
    }
    .modal-body { max-height: 100dvh; }
    .card-map { height: 240px; }
}
.notice-line.fresh { background: #e6f4ea; color: var(--ok); }

/* Избранное: звезда в списке и в шапке карточки. */
.favorite {
    border: 0; background: none; cursor: pointer; padding: 0 2px;
    font-size: 15px; line-height: 1; color: var(--line);
}
.favorite:hover { color: var(--warn); }
.favorite.on { color: #e3a008; }
.favorite[disabled] { opacity: .5; cursor: default; }

/* ---------- Индикатор загрузки ---------- */

/* Первая анимация в проекте. Полоска висит над всем, кроме модального окна: <dialog> живёт
   в top layer и перекрывает любой z-index, поэтому у окна полоска своя (.progress-inline). */
.progress {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    height: 3px;
    z-index: 30;          /* у шапки 10 — это самый большой z-index в остальном проекте */
    pointer-events: none;
    opacity: 0;
    transition: opacity .2s ease;
}
.progress.on { opacity: 1; }
.progress-line {
    height: 100%;
    width: 0;
    background: linear-gradient(90deg, var(--accent), #5fa8ff);
    box-shadow: 0 0 8px rgba(31, 111, 235, .6);
    transition: width .25s ease-out;
}
/* Первый кадр — без перехода, иначе полоска каждый раз «выезжала» бы из нуля. */
.progress.instant .progress-line { transition: none; }

.progress-inline { position: sticky; z-index: 3; border-radius: 3px 3px 0 0; }

/* Затуманивание карты: видно, что устарели именно точки на ней. */
#map-panel.busy #map { filter: saturate(.5) opacity(.55); transition: filter .2s ease; }
#map-panel.busy::after {
    content: 'Обновляю карту…';
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    z-index: 900;         /* панели Leaflet доходят до 800 */
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 14px 8px 34px;
    border-radius: 18px;
    background: var(--panel);
    border: 1px solid var(--line);
    box-shadow: 0 4px 16px rgba(16, 24, 40, .12);
    font-size: 12px;
    color: var(--muted);
}
#map-panel.busy::before {
    content: '';
    position: absolute;
    top: 50%;
    left: 50%;
    z-index: 901;
    width: 13px;
    height: 13px;
    margin-top: -6px;
    /* Сдвиг влево от центра плашки: её ширина задана содержимым, поэтому отступ подобран
       под левый паддинг ::after, а не вычисляется. */
    margin-left: -74px;
    border: 2px solid var(--line);
    border-top-color: var(--accent);
    border-radius: 50%;
    animation: spin .7s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

/* Движение — приятная мелочь, но не необходимость: кому оно мешает, тому остаётся статика. */
@media (prefers-reduced-motion: reduce) {
    .progress-line, .progress, #map-panel.busy #map { transition: none; }
    #map-panel.busy::before { animation: none; }
}
