html,
body {
    width: 100%;
    height: 100%;
    padding: 0;
    margin: 0;
    overflow: hidden;
    -webkit-tap-highlight-color: transparent;
}
*, *::before, *::after {
    box-sizing: border-box;
}

/* ===========================================================================
   Балун — стандартный вид Яндекс.Карт (без кастомных стилей)
   =========================================================================== */

/* Контролы карты (Пробки, Слои и т.д.) — поверх сайдбара */
[class*="-controls-pane"] {
    z-index: 99999 !important;
}
/* Выпадающие панели контролов — тоже поверх */
[class*="-popup"] {
    z-index: 99999 !important;
}

#map-wrapper {
    position: relative;
    width: 100%;
    height: 100%;
}
#map {
    width: 100%;
    height: 100%;
}

/* ===========================================================================
   Overlay для сайдбара на мобильных (скрыт по умолчанию)
   =========================================================================== */
#sidebar-overlay {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0,0,0,0.4);
    z-index: 9998;
    opacity: 0;
    transition: opacity 0.3s ease;
}
#sidebar-overlay.visible {
    display: block;
    opacity: 1;
}

/* ===========================================================================
   Кнопка переключателя сайдбара (скрыта на десктопе)
   =========================================================================== */
#sidebar-toggle-btn {
    display: none;
    position: fixed;
    top: 50px;
    left: 12px;
    padding: 8px 14px;
    background: #fff;
    color: #333;
    border: none;
    border-radius: 6px;
    font-size: 14px;
    font-family: Arial, sans-serif;
    cursor: pointer;
    z-index: 10000;
    box-shadow: 0 2px 8px rgba(0,0,0,0.3);
    min-height: 40px;
    align-items: center;
}

#sidebar {
    position: absolute;
    top: 10px;
    right: 34px;
    width: 260px;
    max-height: calc(100% - 10px);
    background: #f5f5f7;
    border-radius: 0;
    box-shadow: 0 2px 12px rgba(0,0,0,0.3);
    display: flex;
    flex-direction: column;
    font-family: Arial, sans-serif;
    font-size: 13px;
    opacity: 1;
    transform: translateX(0);
    transition: transform 0.3s ease, opacity 0.3s ease;
}
/* Симметричная полоса справа (единое целое с сайдбаром) */
#sidebar::before {
    content: "";
    position: absolute;
    right: -24px;
    top: 0;
    bottom: 0;
    width: 24px;
    background: #f5f5f7;
    border-radius: 0 8px 8px 0;
    box-shadow: 2px 0 8px -4px rgba(0,0,0,0.3);
    pointer-events: none;
}

#sidebar.collapsed {
    transform: translateX(calc(100% - 2px));
    opacity: 1;
    pointer-events: auto;
    cursor: pointer;
    overflow: hidden;
    border-radius: 0 8px 8px 0;
}
#sidebar.collapsed::before {
    display: none;
}
/* При свёрнутом сайдбаре скрываем внутреннее содержимое, оставляем только полосу */
#sidebar.collapsed #sidebar-header h3,
#sidebar.collapsed #sidebar-controls,
#sidebar.collapsed #checkbox-list {
    opacity: 0;
}
#sidebar.collapsed #sidebar-collapse-btn {
    display: none;
}
/* Стрелка ❯ по центру свёрнутой полосы */
#sidebar.collapsed::after {
    content: "❮";
    position: absolute;
    left: 12px;
    top: 50%;
    transform: translateY(-50%);
    font-size: 20px;
    font-weight: bold;
    color: #888;
    pointer-events: none;
}

/* Кнопка-полоса сворачивания сайдбара (слева, единое целое с сайдбаром) */
#sidebar-collapse-btn {
    position: absolute;
    left: -24px;
    top: 0;
    bottom: 0;
    width: 24px;
    background: #f5f5f7;
    border: none;
    cursor: pointer;
    font-size: 16px;
    font-weight: bold;
    color: #888;
    border-radius: 8px 0 0 8px;
    box-shadow: -2px 0 8px -4px rgba(0,0,0,0.3);
    transition: background 0.2s, color 0.2s;
    z-index: 1;
    display: flex;
    align-items: center;
    justify-content: center;
}
#sidebar-collapse-btn:hover {
    background: #e8e8ea;
    color: #333;
}

#sidebar-header {
    padding: 12px 16px 8px;
    border-bottom: 1px solid #ddd;
    flex-shrink: 0;
}
#sidebar-header-top {
    display: flex;
    align-items: center;
    justify-content: center;
    margin-bottom: 8px;
}
#sidebar h3 {
    margin: 0;
    font-size: 13px;
    color: #333;
    border-bottom: 2px solid #e21429;
    padding-bottom: 6px;
    flex: 1;
}
#sidebar-controls {
    display: flex;
    gap: 8px;
}
#sidebar-controls button {
    flex: 1;
    padding: 5px 8px;
    cursor: pointer;
    border: 1px solid #ddd;
    border-radius: 4px;
    font-family: Arial, sans-serif;
    font-size: 12px;
    font-weight: bold;
    line-height: 1.3;
    text-align: center;
    transition: background 0.2s, color 0.2s;
}
#sidebar-controls button:first-child,
#sidebar-controls button:last-child {
    background: #f5f5f5;
    color: #333;
}
#sidebar-controls button:first-child:hover,
#sidebar-controls button:last-child:hover {
    background: #807979;
    color: #fff;
}
#btn-zones-all {
    background: rgba(24, 141, 11, 0.7);
    color: #fff;
    border-color: rgba(24, 141, 11, 0.7);
}
#btn-zones-all:hover {
    background: rgba(20, 122, 9, 0.85);
    color: #fff;
}
#btn-zones-all.on {
    background: rgba(226, 20, 41, 0.7);
    color: #fff;
    border-color: rgba(226, 20, 41, 0.7);
}
#btn-zones-all.on:hover {
    background: rgba(196, 16, 36, 0.85);
    color: #fff;
}
#checkbox-list {
    flex: 1;
    overflow-y: auto;
    padding: 8px 12px 12px;
    -webkit-overflow-scrolling: touch;
}
.checkbox-group {
    margin-bottom: 4px;
}
.checkbox-group label {
    display: flex;
    align-items: flex-start;
    gap: 8px;
    padding: 6px 8px;
    border-radius: 4px;
    cursor: pointer;
    transition: background 0.15s;
}
.checkbox-group label:hover {
    background: #807979;
}
.checkbox-group input[type="checkbox"] {
    margin-top: 2px;
    flex-shrink: 0;
    width: 16px;
    height: 16px;
    cursor: pointer;
}
.checkbox-group .color-dot {
    width: 12px;
    height: 12px;
    border-radius: 50%;
    flex-shrink: 0;
    margin-top: 3px;
}
.checkbox-group .obj-name {
    line-height: 1.3;
}
.checkbox-group .obj-type {
    display: block;
    font-size: 11px;
    color: #807979;
}
#sidebar::-webkit-scrollbar {
    width: 6px;
}
#sidebar::-webkit-scrollbar-thumb {
    background: #ccc;
    border-radius: 3px;
}
/* ===========================================================================
   Нижняя панель кнопок (Проект + Обратная связь)
   =========================================================================== */
#bottom-buttons {
    position: absolute;
    bottom: 60px;
    left: 20px;
    z-index: 9999;
    display: inline-flex;
    border-radius: 8px;
    overflow: hidden;
    box-shadow: 0 2px 10px rgba(0,0,0,0.3);
    opacity: 0;
    transform: translateX(-30px);
    animation: btnSlideIn 0.5s ease forwards;
}
#bottom-buttons button {
    padding: 10px 20px;
    font-size: 14px;
    font-family: Arial, sans-serif;
    border: none;
    cursor: pointer;
    transition: background 0.2s;
    color: #fff;
    text-decoration: none;
    display: inline-block;
    text-align: center;
    position: relative;
}
#bottom-buttons button:first-child {
    background: #188d0b;
    padding-right: 16px;
}
#bottom-buttons button:first-child:hover {
    background: #147a09;
}
#bottom-buttons button:last-child {
    background: #e21429;
    padding-left: 16px;
}
#bottom-buttons button:last-child:hover {
    background: #c41024;
}
/* Разделитель между кнопками */
#bottom-buttons button:first-child::after {
    content: "";
    position: absolute;
    right: 0;
    top: 20%;
    bottom: 20%;
    width: 1px;
    background: rgba(255,255,255,0.3);
}
@keyframes btnSlideIn {
    to {
        opacity: 1;
        transform: translateX(0);
    }
}

/* Панель просмотра проекта */
#project-panel {
    position: fixed;
    top: 0;
    left: 0;
    width: 50%;
    height: 100%;
    background: #fff;
    z-index: 10000;
    box-shadow: 2px 0 12px rgba(0,0,0,0.3);
    transform: translateX(-100%);
    transition: transform 0.3s ease;
    display: flex;
    flex-direction: column;
}
#project-panel.open {
    transform: translateX(0);
}
#btn-close-project {
    position: absolute;
    top: 10px;
    right: 10px;
    padding: 8px 14px;
    background: #e21429;
    color: #fff;
    border: none;
    border-radius: 4px;
    font-size: 14px;
    cursor: pointer;
    z-index: 10001;
    box-shadow: 0 2px 6px rgba(0,0,0,0.2);
    transition: background 0.2s;
}
#btn-close-project:hover {
    background: #c41024;
}
#project-iframe {
    flex: 1;
    width: 100%;
    border: none;
}

/* ===========================================================================
    Сайдбар — категории
    =========================================================================== */
.sidebar-category {
    margin-bottom: 6px;
    border: 1px solid #ddd;
    border-radius: 4px;
    overflow: hidden;
}
.sidebar-category-header {
    font-weight: bold;
    font-size: 14px;
    padding: 10px 12px;
    background: #f5f5f5;
    cursor: pointer;
    display: flex;
    justify-content: space-between;
    align-items: center;
    user-select: none;
    transition: background 0.2s;
}
.sidebar-category-header:hover {
    background: #807979;
}
.sidebar-category-header.expanded {
    background: #e8e8e8;
}
.sidebar-category-arrow {
    transition: transform 0.3s;
    font-size: 12px;
}
.sidebar-category-arrow.rotated {
    transform: rotate(180deg);
}
.sidebar-category-body {
    max-height: 0;
    overflow: hidden;
    transition: max-height 0.4s ease;
    background: #fff;
}
.sidebar-category-body.visible {
    max-height: none !important;
}

/* ===========================================================================
    Кнопка "Выбрать все"
    =========================================================================== */
.sidebar-select-all-btn {
    display: block;
    width: 100%;
    padding: 6px 12px;
    margin-bottom: 6px;
    background: rgba(24, 141, 11, 0.7);
    color: #fff;
    border: none;
    border-radius: 4px;
    font-size: 12px;
    cursor: pointer;
    transition: background 0.2s;
    opacity: 0;
    transform: translateY(-10px);
}
.sidebar-select-all-btn:hover {
    background: rgba(20, 122, 9, 0.85);
}
.sidebar-select-all-btn.hidden {
    opacity: 0;
    transform: translateY(-10px);
}
.sidebar-select-all-btn.visible {
    opacity: 1;
    transform: translateY(0);
}
.sidebar-select-all-btn.selected {
    background: rgba(204, 204, 204, 0.7);
    color: #333;
}

/* ===========================================================================
    Кнопка зон охвата (внутри категории)
    =========================================================================== */
.sidebar-zone-toggle {
    display: block;
    width: 100%;
    padding: 6px 12px;
    margin-bottom: 6px;
    background: rgba(204, 204, 204, 0.7);
    color: #333;
    border: none;
    border-radius: 4px;
    font-size: 12px;
    cursor: pointer;
    transition: background 0.2s;
    opacity: 0;
    transform: translateY(-10px);
}
.sidebar-zone-toggle:hover {
    background: rgba(180, 180, 180, 0.85);
}
.sidebar-zone-toggle.hidden {
    opacity: 0;
    transform: translateY(-10px);
}
.sidebar-zone-toggle.visible {
    opacity: 1;
    transform: translateY(0);
}
.sidebar-zone-toggle.on {
    background: rgba(226, 20, 41, 0.7);
    color: #fff;
}
.sidebar-zone-toggle.on:hover {
    background: rgba(196, 16, 36, 0.85);
}

/* ===========================================================================
    Под-объекты (П-166М ТКС, ББР СГС-22-М, П166М КПУ)
    =========================================================================== */
.sub-items-list {
    margin-top: 6px;
    padding: 4px 4px 4px 28px;
    background: #f8f9fa;
    border-radius: 6px;
    border: 1px solid #e9ecef;
}
.sub-item-row {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 11px;
    color: #333;
    padding: 5px 8px;
    margin: 2px 0;
    border-radius: 4px;
    border-left: 3px solid #101110;
    background: #fff;
    transition: background 0.15s, box-shadow 0.15s, transform 0.1s;
}
.sub-item-row:hover {
    background: #eef4fb;
    box-shadow: 0 1px 4px rgba(0,0,0,0.08);
    transform: translateX(2px);
}
.sub-item-row input[type="checkbox"] {
    width: 14px;
    height: 14px;
    margin: 0;
    flex-shrink: 0;
    cursor: pointer;
    accent-color: #1a5fb4;
}
.sub-item-icon {
    font-size: 12px;
    flex-shrink: 0;
    line-height: 1;
}
.sub-item-link {
    cursor: pointer;
    color: #1a5fb4;
    font-weight: 600;
    transition: color 0.15s;
}
.sub-item-link:hover {
    color: #0d4a9e;
}
.sub-item-text {
    color: #555;
    font-weight: 500;
}

/* ===========================================================================
    Динамические стили (через CSS-переменные, задаются из JS)
    =========================================================================== */
.checkbox-group {
    --border-left-color: #101110;
    --transition-delay: 0s;
}
.sub-items-list {
    padding-left: var(--sub-list-padding-left);
}
.sub-item-row {
    border-left: 3px solid var(--border-left-color);
    cursor: pointer;
    user-select: none;
}
.checkbox-group {
    opacity: 0;
    transform: translateY(-10px);
    transition: opacity 0.3s ease, transform 0.3s ease;
    transition-delay: var(--transition-delay);
}
.checkbox-group.visible {
    opacity: 1;
    transform: translateY(0);
}

/* ===========================================================================
    Счётчики объектов (внизу сайдбара)
    =========================================================================== */
.sidebar-counter-separator {
    height: 3px;
    background: #e21429;
    margin: 10px 0;
    border-radius: 2px;
}
.sidebar-counter {
    font-weight: bold;
    font-size: 14px;
    color: #333;
    text-align: left;
    padding-left: 12px;
}
.sidebar-counter-with-margin {
    margin-top: 6px;
}

/* ===========================================================================
    АДАПТИВНОСТЬ ДЛЯ ПЛАНШЕТОВ (<= 1024px)
    =========================================================================== */
@media screen and (max-width: 1024px) {
    /* Сайдбар скрыт за пределами экрана, открывается по кнопке */
    #sidebar {
        position: fixed;
        top: 0;
        right: 0;
        width: clamp(180px, 25vw, 250px);
        height: 100%;
        max-height: 100%;
        z-index: 9999;
        transform: translateX(100%);
        transition: transform 0.3s ease;
    }
    #sidebar.open {
        transform: translateX(0);
    }
    /* На планшетах нет сворачивания до полоски */
    #sidebar.collapsed {
        transform: translateX(100%);
    }
    /* Скрываем декоративные элементы desktop-версии */
    #sidebar::before,
    #sidebar-collapse-btn {
        display: none !important;
    }
    /* Показываем кнопку переключателя */
    #sidebar-toggle-btn {
        display: flex;
        align-items: center;
        gap: 6px;
        top: 50px;
    }
    /* Кнопки сайдбара — компактные, не растягиваются */
    #sidebar-controls button {
        padding: 5px 8px;
        font-size: 11px;
        max-height: 44px;
        white-space: normal;
        overflow: visible;
        text-overflow: clip;
        line-height: 1.2;
    }
    #project-panel {
        width: 70%;
    }
}

/* ===========================================================================
    АДАПТИВНОСТЬ ДЛЯ МОБИЛЬНЫХ УСТРОЙСТВ (<= 768px)
    =========================================================================== */
@media screen and (max-width: 768px) {
    html, body {
        overflow: hidden;
        overscroll-behavior: none; /* предотвращает "резиновый" скролл на iOS */
    }

    /* --- Сайдбар: выезжает справа как drawer --- */
    #sidebar {
        display: flex;
        position: fixed;
        top: 0;
        left: auto;
        right: 0;
        width: clamp(180px, 25vw, 250px);
        height: 100%;
        max-height: 100%;
        border-radius: 0;
        z-index: 9999;
        opacity: 1;
        transform: translateX(100%);
        animation: none;
        transition: transform 0.3s ease;
        overflow: hidden;
    }
    #sidebar.open {
        transform: translateX(0);
    }
    /* На мобильных нет состояния "свёрнутой полоски" */
    #sidebar.collapsed {
        transform: translateX(100%);
    }
    /* Скрываем декоративные элементы desktop-версии */
    #sidebar::before,
    #sidebar-collapse-btn {
        display: none !important;
    }
    /* Внутренние элементы не вылезают за ширину сайдбара */
    #sidebar-header,
    #sidebar-controls,
    #checkbox-list {
        min-width: 0;
        max-width: 100%;
    }
    #sidebar-controls {
        flex-wrap: wrap;
    }
    #sidebar-controls button {
        min-width: 0;
        word-break: break-word;
        white-space: normal;
    }
    .checkbox-group label {
        flex-wrap: wrap;
        min-width: 0;
    }

    /* --- Кнопки внизу: компактные плавающие снизу-слева --- */
    #bottom-buttons {
        position: fixed;
        bottom: 16px;
        left: 12px;
        border-radius: 8px;
        overflow: hidden;
        box-shadow: 0 2px 10px rgba(0,0,0,0.3);
    }
    #bottom-buttons button {
        padding: 10px 14px;
        font-size: 13px;
    }

    /* --- Панель проекта: на весь экран --- */
    #project-panel {
        width: 100%;
    }

    /* --- Чекбоксы крупнее для тач-управления --- */
    .checkbox-group label {
        padding: 10px 8px;
        min-height: 44px;
    }
    .checkbox-group input[type="checkbox"] {
        width: 20px;
        height: 20px;
        margin-top: 1px;
    }
    .checkbox-group .color-dot {
        width: 14px;
        height: 14px;
        margin-top: 3px;
    }
    .checkbox-group .obj-name {
        font-size: 14px;
        line-height: 1.4;
    }

    /* --- Заголовки и кнопки сайдбара крупнее --- */
    #sidebar h3 {
        font-size: 13px;
    }
    #sidebar-controls button {
        padding: 5px 8px;
        font-size: 11px;
        max-height: 44px;
        white-space: normal;
        overflow: visible;
        text-overflow: clip;
        line-height: 1.2;
    }
    #sidebar {
        font-size: 14px;
    }
}

/* ===========================================================================
   Контекстное меню карты (правый клик)
   =========================================================================== */
#map-context-menu {
    position: fixed;
    z-index: 10001;
    min-width: 200px;
    background: #fff;
    border-radius: 8px;
    box-shadow: 0 4px 16px rgba(0,0,0,0.25);
    font-family: Arial, sans-serif;
    font-size: 13px;
    overflow: hidden;
    display: none;
    animation: ctxFadeIn 0.12s ease;
}
#map-context-menu.visible {
    display: block;
}
@keyframes ctxFadeIn {
    from { opacity: 0; transform: scale(0.95); }
    to   { opacity: 1; transform: scale(1); }
}
#map-context-menu .ctx-coords {
    padding: 10px 14px 8px;
    color: #888;
    font-size: 11px;
    border-bottom: 1px solid #eee;
    word-break: break-all;
    white-space: pre-line;
    cursor: default;
}
#map-context-menu .ctx-item {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 10px 14px;
    cursor: pointer;
    transition: background 0.15s;
    color: #333;
}
#map-context-menu .ctx-item:hover {
    background: #f0f0f3;
}
#map-context-menu .ctx-item .ctx-icon {
    font-size: 15px;
    width: 18px;
    text-align: center;
    flex-shrink: 0;
}
#map-context-menu .ctx-separator {
    height: 1px;
    background: #eee;
    margin: 0;
}

/* ===========================================================================
   Очень маленькие экраны (<= 380px)
   =========================================================================== */
@media screen and (max-width: 380px) {
    #bottom-buttons button {
        padding: 8px 10px;
        font-size: 12px;
    }
    #map-context-menu {
        min-width: 170px;
        font-size: 12px;
    }
}
