*,
*::before,
*::after {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

:root {
    --accent: #8b8fa8;
    --accent-hover: #757990;
    --accent-soft: rgba(139, 143, 168, 0.18);
    --accent-border: rgba(139, 143, 168, 0.28);

    --bg-base: #111318;
    --bg-surface: rgba(22, 24, 32, 0.88);
    --bg-card: rgba(255, 255, 255, 0.035);
    --bg-card-hover: rgba(255, 255, 255, 0.06);

    --border: rgba(255, 255, 255, 0.07);
    --border-soft: rgba(255, 255, 255, 0.04);

    --text-primary: #d8dae8;
    --text-secondary: #848796;
    --text-muted: #50535f;

    --info: #7a9eb5;
    --info-soft: rgba(122, 158, 181, 0.15);
    --info-border: rgba(122, 158, 181, 0.25);

    --sidebar-w: 420px;
    --radius-lg: 22px;
    --radius-md: 14px;
    --radius-sm: 9px;
    --blur: blur(28px) saturate(1.2);
    --shadow: 0 8px 32px rgba(0, 0, 0, 0.35), 0 1px 0 rgba(255, 255, 255, 0.04) inset;
    --shadow-sm: 0 4px 16px rgba(0, 0, 0, 0.25);
    --trans: 0.35s cubic-bezier(0.22, 1, 0.36, 1);
    --trans-fast: 0.2s cubic-bezier(0.22, 1, 0.36, 1);
}

.hidden:not(#sidebar) {
    display: none !important;
}

html,
body {
    height: 100%;
    font-family: 'Inter', system-ui, sans-serif;
    background: var(--bg-base);
    color: var(--text-primary);
    overflow: hidden;
}

#app-container {
    display: flex;
    width: 100vw;
    height: 100vh;
    height: 100dvh;
    overflow: hidden;
}

#map-wrapper {
    flex-grow: 1;
    position: relative;
    height: 100%;
    transition: width var(--trans);
}

#map {
    width: 100%;
    height: 100%;
    z-index: 1;
    background: var(--bg-base);
}

.layer-controls {
    position: absolute;
    top: 24px;
    left: 24px;
    z-index: 500;
    display: flex;
    flex-direction: column;
    gap: 14px;
    background: var(--bg-surface);
    backdrop-filter: var(--blur);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    padding: 16px;
    box-shadow: var(--shadow);
    transition: box-shadow var(--trans), transform var(--trans), padding var(--trans), gap var(--trans), height var(--trans);
    overflow: hidden;
}

.layer-controls.collapsed {
    gap: 0;
    padding-bottom: 8px;
}

.layer-controls.collapsed>*:not(.layer-brand) {
    display: none !important;
}

.layer-controls.collapsed .collapse-btn {
    transform: rotate(180deg);
}

.collapse-btn {
    background: transparent;
    border: none;
    color: var(--text-muted);
    cursor: pointer;
    padding: 6px;
    border-radius: 6px;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: color var(--trans-fast), background var(--trans-fast), transform var(--trans);
    margin-left: auto;
}

.collapse-btn:hover {
    color: var(--text-primary);
    background: var(--bg-card-hover);
}

.layer-brand h2 {
    font-size: 0.95rem;
    font-weight: 600;
    color: var(--text-primary);
    letter-spacing: 0.01em;
}

.layer-brand span {
    font-size: 0.68rem;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.06em;
}

.layer-buttons {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.layer-btn {
    display: flex;
    align-items: center;
    gap: 11px;
    padding: 9px 14px;
    border-radius: var(--radius-sm);
    background: transparent;
    border: 1px solid transparent;
    color: var(--text-secondary);
    font-family: inherit;
    font-size: 0.875rem;
    cursor: pointer;
    transition: background var(--trans-fast), color var(--trans-fast), border-color var(--trans-fast);
    outline: none;
}

.layer-btn:hover {
    background: var(--bg-card-hover);
    color: var(--text-primary);
}

.layer-btn.active {
    background: var(--accent-soft);
    border-color: var(--accent-border);
    color: var(--text-primary);
}

#sidebar {
    width: var(--sidebar-w);
    height: 100%;
    background: var(--bg-surface);
    backdrop-filter: var(--blur);
    border-right: 1px solid var(--border);
    box-shadow: var(--shadow);
    z-index: 600;
    display: flex;
    flex-direction: column;
    transition: margin-left var(--trans);
    flex-shrink: 0;
}

#sidebar.hidden {
    margin-left: calc(var(--sidebar-w) * -1);
}

.sidebar-drag-handle {
    display: none;
}

.sidebar-header {
    padding: 22px 22px 14px;
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    border-bottom: 1px solid var(--border-soft);
}

.location-info h2 {
    font-size: 1.15rem;
    font-weight: 600;
    line-height: 1.35;
    margin-bottom: 4px;
    color: var(--text-primary);
}

.coords-text {
    font-size: 0.78rem;
    color: var(--text-muted);
    font-variant-numeric: tabular-nums;
}

#close-btn {
    background: transparent;
    border: none;
    color: var(--text-muted);
    cursor: pointer;
    padding: 4px;
    border-radius: 6px;
    transition: color var(--trans-fast), background var(--trans-fast);
    outline: none;
}

#close-btn:hover {
    color: var(--text-secondary);
    background: var(--bg-card-hover);
}

.sidebar-content {
    padding: 20px;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 16px;
}

.sidebar-content::-webkit-scrollbar {
    width: 3px;
}

.sidebar-content::-webkit-scrollbar-thumb {
    background: var(--border);
    border-radius: 2px;
}

.metric-card {
    background: var(--bg-card);
    border: 1px solid var(--border-soft);
    border-radius: var(--radius-md);
    padding: 16px;
    transition: background var(--trans-fast);
}

.metric-card:hover {
    background: var(--bg-card-hover);
}

.metric-header {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 10px;
    color: var(--text-muted);
}

.metric-icon svg {
    width: 16px;
    height: 16px;
    opacity: 0.7;
}

.metric-label {
    font-size: 0.75rem;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    font-weight: 500;
}

.metric-body {
    display: flex;
    align-items: baseline;
    gap: 6px;
}

.metric-value {
    font-size: 1.9rem;
    font-weight: 700;
    color: var(--text-primary);
    font-variant-numeric: tabular-nums;
}

.metric-unit {
    font-size: 0.85rem;
    color: var(--text-muted);
}

.ai-block {
    background: var(--bg-card);
    border: 1px solid var(--border-soft);
    border-radius: var(--radius-md);
    padding: 16px;
    transition: background var(--trans-fast);
}

.ai-block:hover {
    background: var(--bg-card-hover);
}

.complex-ai {
    background: var(--info-soft);
    border-color: var(--info-border);
}

.ai-header {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 10px;
    color: var(--text-muted);
}

.complex-ai .ai-header {
    color: var(--info);
}

.ai-header h3 {
    font-size: 0.82rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

.health-index-container {
    margin-top: 10px;
    margin-bottom: 14px;
    background: rgba(0, 0, 0, 0.2);
    border: 1px solid var(--border-soft);
    padding: 14px;
    border-radius: var(--radius-sm);
}

.health-index-header {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    margin-bottom: 10px;
}

.health-index-score {
    font-size: 1.5rem;
    font-weight: 700;
    color: var(--text-primary);
    font-variant-numeric: tabular-nums;
}

.health-index-label {
    font-size: 0.75rem;
    color: #92a3b0;
    font-weight: 500;
}

.health-index-bar-bg {
    width: 100%;
    height: 6px;
    background: var(--bg-card-hover);
    border-radius: 3px;
    overflow: hidden;
}

.health-index-fill {
    height: 100%;
    background: linear-gradient(90deg, #6b8cce, #8dc390);
    border-radius: 3px;
    transition: width 1s cubic-bezier(0.22, 1, 0.36, 1);
}

.ai-text {
    font-size: 0.855rem;
    color: var(--text-secondary);
    line-height: 1.65;
}

.typing-indicator {
    display: flex;
    gap: 4px;
    padding: 8px 0;
}

.typing-indicator span {
    width: 5px;
    height: 5px;
    background: var(--text-muted);
    border-radius: 50%;
    animation: bounce 1.4s infinite ease-in-out both;
}

.complex-ai .typing-indicator span {
    background: var(--info);
}

.typing-indicator span:nth-child(1) {
    animation-delay: -0.32s;
}

.typing-indicator span:nth-child(2) {
    animation-delay: -0.16s;
}

@keyframes bounce {

    0%,
    80%,
    100% {
        transform: scale(0);
        opacity: 0.4;
    }

    40% {
        transform: scale(1);
        opacity: 1;
    }
}

.btn-primary {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    width: 100%;
    padding: 13px;
    border-radius: var(--radius-sm);
    background: var(--accent-soft);
    color: var(--text-primary);
    border: 1px solid var(--accent-border);
    font-size: 0.875rem;
    font-weight: 500;
    font-family: inherit;
    cursor: pointer;
    transition: background var(--trans-fast), border-color var(--trans-fast), color var(--trans-fast);
    outline: none;
}

.btn-primary:hover {
    background: rgba(139, 143, 168, 0.26);
    border-color: rgba(139, 143, 168, 0.4);
}

.sci-marker {
    position: relative;
}

.sci-dot {
    width: 11px;
    height: 11px;
    background: #d8dae8;
    border: 2px solid var(--accent);
    border-radius: 50%;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    box-shadow: 0 0 6px rgba(139, 143, 168, 0.5);
    z-index: 2;
}

.sci-pulse {
    width: 36px;
    height: 36px;
    border: 1px solid rgba(139, 143, 168, 0.5);
    border-radius: 50%;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    animation: sci-pulse 2.4s cubic-bezier(0.1, 0.8, 0.3, 1) infinite;
    z-index: 1;
}

@keyframes sci-pulse {
    0% {
        width: 11px;
        height: 11px;
        opacity: 0.8;
    }

    100% {
        width: 52px;
        height: 52px;
        opacity: 0;
    }
}

.norm-card {
    display: flex;
    align-items: flex-start;
    gap: 12px;
    background: var(--info-soft);
    border: 1px solid var(--info-border);
    border-radius: var(--radius-sm);
    padding: 13px 14px;
    margin-bottom: -2px;
}

.norm-card.hidden {
    display: none !important;
}

.norm-icon {
    color: var(--info);
    flex-shrink: 0;
    margin-top: 2px;
    opacity: 0.8;
}

.norm-text {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.norm-label {
    font-size: 0.68rem;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    color: var(--text-muted);
    font-weight: 600;
}

#factor-norm {
    font-size: 0.84rem;
    color: var(--text-secondary);
    line-height: 1.45;
    font-weight: 400;
}

.layer-brand {
    display: flex;
    align-items: center;
    gap: 11px;
}

.brand-logo {
    width: 34px;
    height: 34px;
    object-fit: contain;
    border-radius: var(--radius-sm);
    opacity: 0.85;
}

.brand-text {
    display: flex;
    flex-direction: column;
    gap: 2px;
}

@media (max-width: 768px) {
    #app-container {
        flex-direction: column;
    }

    #map-wrapper {
        width: 100%;
        height: 100%;
    }

    /* ─── Панель слоёв ── прибита снизу с safe-area ─── */
    .layer-controls {
        top: auto;
        bottom: calc(16px + env(safe-area-inset-bottom, 8px));
        left: 10px;
        right: 10px;
        padding: 10px 10px 8px;
        flex-direction: column;
        gap: 8px;
    }

    .layer-brand {
        display: none !important;
    }

    /* Кнопки слоёв — горизонтальный ряд */
    .layer-buttons {
        flex-direction: row;
        width: 100%;
        gap: 4px;
        justify-content: space-between;
    }

    .layer-btn {
        flex-direction: column;
        flex-grow: 1;
        padding: 10px 4px 8px;
        gap: 4px;
        justify-content: center;
        align-items: center;
        min-width: 0;
        border-radius: 10px;
    }

    .btn-text {
        font-size: 0.62rem;
        letter-spacing: 0;
        line-height: 1;
    }

    /* Ползунок прозрачности — на всю ширину */
    .opacity-control {
        width: 100%;
        padding: 0 2px;
    }

    .opacity-control input[type="range"] {
        height: 36px;
        /* увеличенная тач-зона */
        cursor: pointer;
    }

    /* Кнопка «Объекты инфраструктуры» */
    #btn-toggle-zosh {
        width: 100% !important;
        min-height: 44px;
        font-size: 0.78rem !important;
        padding: 10px 12px !important;
        border-radius: 10px !important;
    }

    /* ─── Сайдбар — bottom sheet ─── */
    #sidebar {
        position: fixed;
        bottom: 0;
        left: 0;
        width: 100%;
        height: 86vh;
        border-right: none;
        border-top: 1px solid var(--border);
        border-radius: 20px 20px 0 0;
        margin-left: 0;
        transform: translateY(100%);
        transition: transform 0.45s cubic-bezier(0.22, 1, 0.36, 1);
        z-index: 1000;
        overflow: hidden;
    }

    #sidebar.hidden {
        transform: translateY(100%);
    }

    #sidebar.half-open {
        transform: translateY(52%);
    }

    #sidebar.full-open {
        transform: translateY(0);
    }

    /* Ручка для свайпа */
    .sidebar-drag-handle {
        display: block;
        width: 40px;
        height: 4px;
        background: rgba(255, 255, 255, 0.14);
        border-radius: 4px;
        margin: 12px auto 4px;
        flex-shrink: 0;
    }

    .sidebar-header {
        padding: 10px 18px 14px;
    }

    .location-info h2 {
        font-size: 1rem;
    }

    /* Контент сайдбара */
    .sidebar-content {
        padding: 14px 14px calc(14px + env(safe-area-inset-bottom, 0px));
        gap: 12px;
    }

    /* Покрупнее значения */
    .metric-value {
        font-size: 1.65rem;
    }

    .metric-card {
        padding: 14px;
    }

    /* ─── Health Index ─── */
    .health-index-container {
        padding: 12px;
        margin-top: 8px;
        margin-bottom: 10px;
    }

    .health-index-score {
        font-size: 1.3rem;
    }

    .health-index-bar-bg {
        height: 8px;
        /* толще — лучше видно */
    }

    /* ─── Practical Guides / Как использовать среду ─── */
    .guide-cards {
        gap: 8px !important;
    }

    .guide-card {
        background: var(--bg-card);
        border: 1px solid var(--border-soft);
        border-radius: var(--radius-sm);
        padding: 12px !important;
    }

    .guide-card-title {
        font-size: 0.68rem !important;
        margin-bottom: 6px !important;
        display: block;
    }

    .guide-card-text {
        font-size: 0.82rem !important;
        line-height: 1.55 !important;
    }

    /* ─── Кнопки действий ─── */
    .btn-primary,
    .btn-secondary {
        padding: 14px;
        font-size: 0.85rem;
        min-height: 48px;
    }

    #close-btn {
        padding: 8px;
        min-width: 40px;
        min-height: 40px;
        display: flex;
        align-items: center;
        justify-content: center;
    }

    /* ─── Leaflet попапы ZOZh-маркеров ─── */
    .leaflet-popup-content-wrapper {
        border-radius: 12px !important;
    }

    .leaflet-popup-content {
        font-size: 0.9rem !important;
        margin: 12px 16px !important;
        min-width: 160px !important;
    }

    /* ─── Статус-уведомления ─── */
    .backend-status {
        top: calc(env(safe-area-inset-top, 0px) + 10px);
        left: 10px;
        right: 10px;
        max-width: none;
        font-size: 0.78rem;
        padding: 9px 12px;
    }
}

.btn-secondary {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    width: 100%;
    margin-top: 12px;
    padding: 11px;
    border-radius: var(--radius-sm);
    background: transparent;
    color: var(--text-secondary);
    border: 1px solid var(--border);
    font-size: 0.82rem;
    font-weight: 500;
    font-family: inherit;
    cursor: pointer;
    transition: background var(--trans-fast),
        color var(--trans-fast),
        border-color var(--trans-fast);
    outline: none;
}

.btn-secondary:hover {
    background: var(--bg-card-hover);
    color: var(--text-primary);
    border-color: var(--accent-border);
}

#ai-advanced-text,
#complex-advanced-text {
    margin-top: 10px;
}

#complex-advanced-wrap {
    margin-top: 12px;
    border-top: 1px solid var(--border-soft);
    padding-top: 12px;
}

.backend-status {
    position: absolute;
    top: 22px;
    right: 24px;
    z-index: 700;
    max-width: min(460px, calc(100vw - 64px));
    padding: 11px 14px;
    border-radius: 10px;
    border: 1px solid var(--border);
    background: var(--bg-surface);
    color: var(--text-primary);
    font-size: 0.82rem;
    line-height: 1.45;
    box-shadow: var(--shadow-sm);
    backdrop-filter: var(--blur);
}

.backend-status.status-warning {
    background: rgba(179, 120, 53, 0.14);
    border-color: rgba(242, 180, 104, 0.45);
    color: #ffe1be;
}

.backend-status.status-error {
    background: rgba(163, 63, 63, 0.18);
    border-color: rgba(222, 109, 109, 0.5);
    color: #ffd2d2;
}

.backend-status.status-info {
    background: rgba(79, 109, 153, 0.17);
    border-color: rgba(120, 157, 213, 0.35);
    color: #d8e6ff;
}