/* Вариант B — раскладка с фасетным сайдбаром.
   Подключается после styles-stats.css и styles-stats-map.css поверх stats-b.html. */

:root {
    /* Карточка «Объекты» по умолчанию показывает 6 рядов плиток:
       плитка 106 px + зазор 1 px → 6*106 + 5*1 = 641 px.
       Значение читает getTreeMaxHeight(), поэтому кнопка «Показать ещё»
       остаётся согласованной с этой высотой. */
    --tree-max-height: 641px;
    /* Ширина сайдбара: плавающая строка активных фильтров начинается
       сразу за ним и занимает всю оставшуюся ширину окна */
    --sidebar-width: 300px;
}

.app-layout {
    display: flex;
    align-items: flex-start;
    min-height: 100vh;
}

/* На ПК страница целиком не прокручивается: сайдбар закреплён слева,
   а прокручиваются только основная область и дерево в сайдбаре */
@media (min-width: 769px) {
    html,
    body {
        overflow: hidden;
    }

    .app-layout {
        height: 100vh;
    }

    .app-layout .main-area {
        height: 100vh;
        overflow-y: auto;
        overflow-x: hidden;
    }
}

/* ------------------------------------------------------------------ сайдбар */

.app-layout .sidebar {
    width: var(--sidebar-width);
    flex: none;
    background: #fff;
    border-right: 1px solid #ddd;
    display: flex;
    flex-direction: column;
    position: sticky;
    top: 0;
    height: 100vh;
    box-sizing: border-box;
}

/* Шапка сайдбара на десктопе не нужна — на мобильном это заголовок нижнего листа */
.app-layout .sidebar-head {
    display: none;
    padding: 10px 12px;
    border-bottom: 1px solid #eee;
    flex: none;
    position: relative;
}

.app-layout .sidebar-title {
    display: block;
    font-weight: bold;
    font-size: 14px;
}

.app-layout .sidebar-close {
    position: absolute;
    right: 8px;
    top: 8px;
    width: 26px;
    height: 26px;
    border: 1px solid #ddd;
    background: #fff;
    border-radius: 4px;
    cursor: pointer;
    font-size: 12px;
    color: #555;
}

.app-layout .sidebar-body {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    padding: 0 10px 30px;
    -webkit-overflow-scrolling: touch;
}

/* ------------------------------------------------------------------ секции */

.app-layout .side-section {
    border-bottom: 1px solid #f0f0f0;
}

/* секция наследует класс tree-container ради JS-контракта,
   но вид карточки ей не нужен */
.app-layout .side-section.tree-container {
    background: transparent;
    padding: 0;
    border: 0;
    border-radius: 0;
    box-shadow: none;
}

/* Заголовок раздела — липкая шапка с фоном и разделителем:
   при прокрутке сайдбара остаётся наверху и чётко отделяет дерево */
.app-layout .side-section-head {
    position: sticky;
    top: 0;
    z-index: 6;
    display: flex;
    align-items: center;
    gap: 7px;
    padding: 9px 4px;
    cursor: pointer;
    font-size: 13px;
    user-select: none;
    background: #fff;
    border-bottom: 1px solid #e9ecef;
    margin-bottom: 4px;
}

.app-layout .side-section-head:hover {
    color: #007cba;
    background: #f6f9fb;
}

.app-layout .side-caret {
    width: 20px;
    height: 22px;
    flex: none;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    color: #59636d;
}

.app-layout .side-caret svg {
    display: block;
    transition: transform .15s ease;
}

.app-layout .side-section.is-open .side-caret {
    color: #007cba;
}

.app-layout .side-section.is-open .side-caret svg {
    transform: rotate(90deg);
}

.app-layout .side-name {
    font-weight: bold;
}

/* Открытый раздел — название выделено цветом, шапка подсвечена */
.app-layout .side-section.is-open .side-name {
    color: #007cba;
}

.app-layout .side-section.is-open .side-section-head {
    background: #eef6fb;
}

/* Счётчик — пилюля, а не серое число */
.app-layout .side-count {
    margin-left: auto;
    font-size: 10.5px;
    color: #667;
    background: #eef1f5;
    border-radius: 9px;
    padding: 1px 7px;
}

.app-layout .side-section-tools {
    display: none;
    align-items: center;
    gap: 5px;
    flex-wrap: wrap;
    padding: 0 2px 7px;
}

.app-layout .side-section.is-open .side-section-tools {
    display: flex;
}

.app-layout .side-section-tools .sort-select {
    flex: 1 1 auto;
    min-width: 0;
    width: auto;
    max-width: 100%;
}

.app-layout .side-section-tools .toggle-all-btn {
    flex: none;
}

/* Поиск в разделе (есть только у части конфигов, например у «Места находок»
   в пространстве config2) занимает отдельную строку */
.app-layout .side-section-tools .search-container {
    flex: 1 1 100%;
    display: flex;
    gap: 4px;
}

.app-layout .side-section-tools .tree-search {
    flex: 1;
    min-width: 0;
    height: 22px;
}

.app-layout .side-section-tools .clear-search-btn {
    flex: none;
}

/* Дерево в сайдбаре не обрезается по --tree-max-height — прокручивается весь сайдбар */
.app-layout .side-section .tree-content {
    max-height: none !important;
    overflow: visible !important;
    position: relative;
    padding-left: 14px;
}

/* Вертикальная направляющая: дерево отделено от шапки и читается как ветка */
.app-layout .side-section .tree-content::before {
    content: '';
    position: absolute;
    left: 4px;
    top: 0;
    bottom: 8px;
    width: 1px;
    background: #d9dfe5;
}

/* Закрытая секция не показывает дерево */
.app-layout .side-section:not(.is-open) .tree-content {
    display: none;
}

/* Кнопка «Показать ещё» в сайдбаре скрыта, но остаётся в DOM:
   updateExpandButtonVisibility() и toggleAllNodes() ищут её как nextElementSibling */
.app-layout .side-expand-btn {
    display: none !important;
}

/* ------------------------------------------------------------- основная часть */

.app-layout .main-area {
    flex: 1;
    min-width: 0;
    padding: 0 12px 40px;
}

.app-layout .main-topbar {
    position: sticky;
    top: 0;
    z-index: 100;
    background: #f5f5f5;
    padding: 6px 0;
    margin-bottom: 4px;
    border-bottom: 1px solid #dcdcdc;
}

/* Строка с синей кнопкой нужна только в мобильном режиме */
.app-layout .main-topbar-row {
    display: none;
    align-items: center;
    gap: 8px;
    margin-bottom: 4px;
}

.app-layout .mobile-filter-btn {
    display: none;
    border: 1px solid #007cba;
    background: #007cba;
    color: #fff;
    border-radius: 18px;
    padding: 5px 12px;
    font-size: 12px;
    font-weight: bold;
    cursor: pointer;
}

.app-layout .mobile-filter-btn .n {
    background: rgba(255, 255, 255, .28);
    border-radius: 9px;
    padding: 0 6px;
    font-size: 11px;
}

/* Панель активных фильтров — строка во всю ширину рабочей области:
   чипы прокручиваются по горизонтали, кнопка «Очистить все фильтры»
   остаётся на своём месте справа */
.app-layout .active-filters {
    position: static;
    bottom: auto;
    right: auto;
    width: 100%;
    max-width: none;
    background: transparent;
    border: 0;
    border-radius: 0;
    box-shadow: none;
    padding: 0;
    display: flex;
    align-items: center;
    gap: 8px;
    flex-wrap: nowrap;
}

.app-layout .active-filters:not(.has-filters) {
    display: flex;
}

.app-layout .active-filters::before {
    content: "Фильтры";
    position: static;
    font-size: 11px;
    font-weight: bold;
    color: #666;
    text-shadow: none;
}

/* Прокручиваемая часть строки: сами чипы.
   Прокрутки нет — если чипы не помещаются, они переносятся на вторую строку. */
.app-layout .filters-scroll {
    position: relative;
    flex: 1 1 auto;
    min-width: 0;
}

.app-layout .filters-list {
    max-width: none;
    flex: 1 1 auto;
    flex-wrap: wrap;
    overflow: visible;
}

/* Чипы не сжимаются: иначе подписи в чипах обрезаются вместо переноса */
.app-layout .filters-list .filter-tag {
    flex: 0 1 auto;
}

/* Стрелки-подсказки прокрутки строки активных фильтров: нужны только
   на мобильном, где строка прокручивается по горизонтали */
.app-layout .scroll-hint {
    display: none;
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    width: 22px;
    height: 22px;
    align-items: center;
    justify-content: center;
    font-size: 18px;
    font-weight: bold;
    color: #6b767f;
    background: rgba(255, 255, 255, .92);
    border-radius: 50%;
    box-shadow: 0 1px 3px rgba(0, 0, 0, .18);
    pointer-events: none;
    z-index: 2;
}

.app-layout .scroll-hint-left {
    left: 2px;
}

.app-layout .scroll-hint-right {
    right: 2px;
}

/* Группа кнопок строки активных фильтров */
.app-layout .active-filters .filter-actions {
    display: flex;
    align-items: center;
    gap: 4px;
    flex: none;
}

.app-layout .active-filters .clear-all-btn {
    flex: none;
}

/* На ПК строка активных фильтров плавает поверх содержимого в правом нижнем
   углу. Пока фильтров мало, панель по размеру содержимого; когда их много —
   растягивается на всю доступную ширину (за сайдбаром), и чипы переносятся
   на вторую строку. */
@media (min-width: 769px) {
    .app-layout .main-topbar {
        position: static;
        background: none;
        border-bottom: 0;
        padding: 0;
        margin: 0;
    }

    .app-layout .active-filters {
        position: fixed;
        right: 10px;
        bottom: 10px;
        left: auto;
        z-index: 1000;
        width: max-content;
        max-width: calc(100vw - var(--filters-left, 300px) - 20px);
        max-height: none;
        overflow: visible;
        padding: 6px 8px;
        gap: 5px;
        background: rgba(255, 0, 0, .5);
        border: 1px solid rgba(221, 221, 221, .8);
        border-radius: 8px;
        box-shadow: 0 2px 12px rgba(0, 0, 0, .15);
        align-items: center;
        justify-content: flex-end;
    }

    .app-layout .active-filters::before {
        content: "Фильтры";
        position: static;
        font-size: 10px;
        font-weight: bold;
        color: #fff;
        text-shadow: 0 1px 2px rgba(0, 0, 0, .3);
        flex: none;
        /* подпись всегда у левого края панели: когда чипы переносятся
           на вторую строку, она остаётся на уровне первой */
        align-self: flex-start;
        margin-top: 9px;
    }

    /* Содержимое строки прижато к правому краю панели */
    .app-layout .filters-list {
        justify-content: flex-end;
    }

    .app-layout .active-filters:not(.has-filters) {
        display: none;
    }

    /* содержимое не должно уезжать под плавающей строкой */
    .app-layout .main-area {
        padding-bottom: 56px;
    }
}

/* ------------------------------------------------------------------- сетка */

.app-layout .main-grid {
    display: grid;
    grid-template-columns: minmax(0, 2fr) minmax(320px, 1fr);
    gap: 10px;
    align-items: start;
}

/* Высота карты на ПК — фиксированная константа, как на оригинальном
   сайте (defaultHeight 467 в конфиге), но увеличенная на 200 px.
   На мобильном остаётся исходная высота из конфига. */
@media (min-width: 769px) {
    .app-layout .main-grid .map-container:not(.fullscreen) .map-content {
        height: 667px !important;
    }
}

/* Зона за пределами мира (за полюсами) — цветом воды, а не серым фоном Leaflet */
.app-layout .main-grid .map-content.leaflet-container {
    background: #a8d3e0;
}

/* В основной области ширина задана раскладкой, а не прежними множителями */
.app-layout .main-grid .width-multiplier-1,
.app-layout .main-grid .width-multiplier-2 {
    grid-column: span 1;
}

.app-layout .main-grid .width-multiplier-4 {
    grid-column: span 2;
}

.app-layout .main-grid .map-container,
.app-layout .main-grid .tree-container {
    min-width: 0;
}

/* На узком экране (но ещё не мобильном) карточка объектов и карта не помещаются
   рядом: карта уходит под объекты, иначе объекты сжимаются в узкую полосу. */
@media (min-width: 769px) and (max-width: 1279px) {
    .app-layout .main-grid {
        grid-template-columns: minmax(0, 1fr);
    }

    .app-layout .main-grid .width-multiplier-1,
    .app-layout .main-grid .width-multiplier-2,
    .app-layout .main-grid .width-multiplier-4 {
        grid-column: span 1;
    }
}

/* Кнопка «%» на узком экране лишняя: проценты в дереве занимают место,
   а места на экране и так мало. */
@media (max-width: 1279px) {
    .app-layout .percent-btn-global {
        display: none;
    }
}

/* Подпись фильтра в карточке статистики — обычный текст, а не ссылка:
   на узком экране клик по ней включал фильтр, которого рядом не видно,
   и курсор руки вводил в заблуждение */
.app-layout .filter-label-static {
    cursor: default;
}

/* --------------------------------------------------------------- мобильный */

.app-layout .sidebar-backdrop {
    display: none;
}

@media (max-width: 768px) {
    .app-layout {
        display: block;
    }

    .app-layout .main-area {
        padding: 0 10px 30px;
    }

    /* Сайдбар превращается в лист на весь экран:
       прокручивается всё содержимое — шапка с числовыми фильтрами и деревья */
    .app-layout .sidebar {
        position: fixed;
        left: 0;
        right: 0;
        top: 0;
        bottom: 0;
        width: auto;
        height: 100vh;
        height: 100dvh;
        z-index: 1200;
        border-right: 0;
        border-radius: 0;
        box-shadow: none;
        transform: translateY(100%);
        transition: transform .22s ease;
    }

    .app-layout .sidebar.is-open {
        transform: translateY(0);
    }

    .app-layout .sidebar-head {
        display: block;
        padding: 12px 14px;
    }

    .app-layout .sidebar-close {
        display: block;
        width: 32px;
        height: 32px;
        font-size: 15px;
    }

    .app-layout .sidebar-backdrop {
        display: block;
        position: fixed;
        inset: 0;
        background: rgba(20, 28, 34, .5);
        z-index: 1100;
        opacity: 0;
        visibility: hidden;
        transition: opacity .2s ease;
    }

    .app-layout .sidebar-backdrop.is-open {
        opacity: 1;
        visibility: visible;
    }

    body.sidebar-open {
        overflow: hidden;
    }

    .app-layout .main-topbar-row {
        display: flex;
    }

    .app-layout .mobile-filter-btn {
        display: inline-flex;
        align-items: center;
        gap: 6px;
    }

    /* Подпись «Фильтры» рядом с синей кнопкой не нужна */
    .app-layout .active-filters::before {
        display: none;
    }

    /* На мобильном строка активных фильтров прокручивается по горизонтали,
       кнопка «Очистить все фильтры» остаётся на своём месте */
    .app-layout .filters-list {
        flex-wrap: nowrap;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }

    .app-layout .filters-list .filter-tag {
        flex: none;
    }

    .app-layout .filters-list::-webkit-scrollbar {
        display: none;
    }

    /* Затемнение у края, к которому есть что прокручивать, и стрелка */
    .app-layout .filters-scroll.can-scroll-right .filters-list {
        -webkit-mask-image: linear-gradient(to right, #000 calc(100% - 28px), transparent);
        mask-image: linear-gradient(to right, #000 calc(100% - 28px), transparent);
    }

    .app-layout .filters-scroll.can-scroll-left .filters-list {
        -webkit-mask-image: linear-gradient(to right, transparent, #000 28px);
        mask-image: linear-gradient(to right, transparent, #000 28px);
    }

    .app-layout .filters-scroll.can-scroll-left.can-scroll-right .filters-list {
        -webkit-mask-image: linear-gradient(to right, transparent, #000 28px, #000 calc(100% - 28px), transparent);
        mask-image: linear-gradient(to right, transparent, #000 28px, #000 calc(100% - 28px), transparent);
    }

    .app-layout .filters-scroll.can-scroll-left .scroll-hint-left,
    .app-layout .filters-scroll.can-scroll-right .scroll-hint-right {
        display: flex;
    }

    .app-layout .active-filters .clear-all-btn,
    .app-layout .active-filters .controls-panel {
        flex: none;
    }

    /* Статистика — горизонтальная лента вместо трёх рядов */
    .app-layout .stats-section {
        display: flex;
        align-items: center;
        gap: 6px;
    }

    .app-layout .stats-grid {
        display: flex;
        overflow-x: auto;
        gap: 4px;
        -webkit-overflow-scrolling: touch;
        padding-bottom: 2px;
    }

    .app-layout .stats-grid::-webkit-scrollbar {
        display: none;
    }

    .app-layout .stat-card {
        flex: 0 0 96px;
        border-radius: 4px;
    }

    .app-layout .stat-number {
        font-size: 14px;
    }

    /* Числовые фильтры в карточках должны быть читаемы */
    .app-layout .stat-card.range-card .range-filters select {
        font-size: 11px;
        height: 22px;
    }

    .app-layout .controls-panel {
        flex-direction: row;
        height: auto;
        margin-bottom: 6px;
    }

    /* Числовые фильтры из панели статистики — в шапку листа, в две колонки */
    .app-layout .sidebar-numeric-filters {
        display: grid;
        grid-template-columns: repeat(2, minmax(0, 1fr));
        column-gap: 12px;
        row-gap: 4px;
        padding: 8px 0 2px;
    }

    /* Фильтры с диапазонами (мин/макс) занимают всю ширину */
    .app-layout .sidebar-numeric-filters .sheet-num-row-wide {
        grid-column: 1 / -1;
    }

    .app-layout .sidebar-numeric-filters .sheet-num-row {
        display: flex;
        align-items: center;
        gap: 8px;
        margin-bottom: 6px;
    }

    .app-layout .sidebar-numeric-filters .sheet-num-row .filter-label {
        flex: 0 0 92px;
        color: #666;
        font-size: 11px;
    }

    .app-layout .sidebar-numeric-filters .sheet-num-row input[type=checkbox] {
        width: 17px;
        height: 17px;
        flex: none;
    }

    .app-layout .sidebar-numeric-filters .range-filters {
        display: flex;
        gap: 4px;
        flex: 1;
        min-width: 0;
    }

    .app-layout .sidebar-numeric-filters select {
        flex: 1;
        min-width: 0;
        height: 30px;
        font-size: 12px;
        border: 1px solid #ddd;
        border-radius: 4px;
        background: #fff;
    }

    .app-layout .main-grid {
        grid-template-columns: minmax(0, 1fr);
    }

    .app-layout .main-grid .width-multiplier-4 {
        grid-column: span 1;
    }

    /* Три плитки в ряд — на телефоне и в ужатом окне десктопа */
    .app-layout .thumbnails-grid {
        grid-template-columns: repeat(3, minmax(0, 1fr));
    }

    .app-layout .thumbnail-container {
        width: auto;
        height: auto;
        aspect-ratio: 1;
    }

    /* Подписи на плитках мельче, иначе наезжают друг на друга */
    .app-layout .thumbnail-name-overlay,
    .app-layout .thumbnail-name-overlay2 {
        font-size: 9px;
        padding: 1px 2px;
    }

    /* ------------------------------------------------- крупный текст в листе */

    :root {
        --tree-count-font-size: 13px;
    }

    .app-layout .sidebar-head {
        padding: 12px 14px;
    }

    .app-layout .sidebar-title {
        font-size: 17px;
    }

    .app-layout .sidebar-close {
        width: 32px;
        height: 32px;
        top: 10px;
        right: 10px;
        font-size: 15px;
    }

    /* Числовые фильтры в шапке листа */
    .app-layout .sidebar-numeric-filters .sheet-num-row .filter-label {
        flex: 0 0 92px;
        color: #333;
        font-size: 14px;
    }

    .app-layout .sidebar-numeric-filters .sheet-num-row input[type=checkbox] {
        width: 20px;
        height: 20px;
    }

    .app-layout .sidebar-numeric-filters select {
        height: 36px;
        font-size: 14px;
    }

    /* Заголовки разделов */
    .app-layout .side-section-head {
        font-size: 16px;
        padding: 12px 4px;
        gap: 8px;
    }

    .app-layout .side-caret {
        width: 24px;
        height: 26px;
    }

    .app-layout .side-count {
        font-size: 13px;
        padding: 2px 9px;
    }

    /* Инструменты раздела: поиск, сортировка, раскрытие */
    .app-layout .side-section-tools .sort-select,
    .app-layout .side-section-tools .tree-search {
        font-size: 14px;
        height: 34px;
    }

    .app-layout .side-section-tools .toggle-all-btn {
        height: 30px;
        width: 36px;
        font-size: 14px;
    }

    .app-layout .side-section-tools .clear-search-btn {
        font-size: 30px;
    }

    /* Дерево */
    .app-layout .sidebar-body .tree-label-text {
        font-size: 14px;
    }

    .app-layout .sidebar-body .tree-bar {
        height: 26px;
    }

    .app-layout .sidebar-body .tree-toggle {
        width: 20px;
        height: 22px;
        font-size: 20px;
    }

    /* Чипы активных фильтров в шапке страницы */
    .app-layout .active-filters .filter-tag {
        font-size: 12px;
        padding: 5px 8px;
        max-width: 170px;
    }

    .app-layout .active-filters .clear-all-btn {
        font-size: 13px;
        padding: 6px 10px;
    }

    /* ------------------------------------- лента статистики: вид и прокрутка */

    .app-layout .stats-section {
        position: relative;
    }

    .app-layout .stat-card {
        flex: 0 0 68px;
        min-width: 0;
        box-sizing: border-box;
        padding: 4px 6px;
    }

    /* В карточках с числом и подписью подпись идёт под числом, а не рядом */
    .app-layout .stat-card.range-card .stat-row {
        flex-direction: column;
        gap: 1px;
    }

    .app-layout .stat-label {
        font-size: 11px;
    }

    /* Край, к которому есть что прокручивать, помечается затуханием и стрелкой */
    .app-layout .stats-section.can-scroll-right .stats-grid {
        -webkit-mask-image: linear-gradient(to right, #000 calc(100% - 28px), transparent);
        mask-image: linear-gradient(to right, #000 calc(100% - 28px), transparent);
    }

    .app-layout .stats-section.can-scroll-left .stats-grid {
        -webkit-mask-image: linear-gradient(to right, transparent, #000 28px);
        mask-image: linear-gradient(to right, transparent, #000 28px);
    }

    .app-layout .stats-section.can-scroll-right::after,
    .app-layout .stats-section.can-scroll-left::before {
        position: absolute;
        top: 50%;
        transform: translateY(-50%);
        width: 22px;
        height: 22px;
        display: flex;
        align-items: center;
        justify-content: center;
        pointer-events: none;
        font-size: 18px;
        font-weight: bold;
        color: #6b767f;
        background: rgba(255, 255, 255, .92);
        border-radius: 50%;
        box-shadow: 0 1px 3px rgba(0, 0, 0, .18);
    }

    .app-layout .stats-section.can-scroll-right::after {
        content: '›';
        right: 2px;
    }

    .app-layout .stats-section.can-scroll-left::before {
        content: '‹';
        left: 2px;
    }

    /* ------------------------- кнопки «%» и «ссылка» — рядом с кнопкой «Фильтры» */

    .app-layout .main-topbar-row .controls-panel {
        display: flex;
        flex-direction: row;
        gap: 4px;
        height: auto;
        margin: 0 0 0 auto;
    }

    .app-layout .main-topbar-row .percent-btn-global,
    .app-layout .main-topbar-row .shareFiltersBtn {
        font-size: 13px;
        height: 28px;
        width: 32px;
        min-width: 32px;
    }
}
