/* ================== THEME / LAYOUT ================== */
:root {
    color-scheme: light;
    --bg: #f7f8fa;
    --ink: #223042;
    --muted: #7b8794;
    --card: #ffffff;
    --accent: #1f4b5a;
    --accent-soft: #e7f6ff;
    --border: #e6ebf1;
    --shadow-sm: 0 2px 6px rgba(28, 35, 45, .06);
    --shadow-md: 0 6px 14px rgba(28, 35, 45, .08);
    --font-s: .94rem;
    --font-xs: .86rem;
    --radius-xs: 6px;
    --radius-sm: 8px;
    --radius-md: 10px;
    --nav-h: 68px;
    --side-w: 320px;
    --brand-green: #63b231;
}

html,
body {
    height: 100%
}

body {
    font-family: system-ui, -apple-system, Segoe UI, Roboto, "Helvetica Neue", Arial;
    overflow: hidden;
    background: var(--bg);
    color: var(--ink);
}

/* ===== Header ===== */
#header {
    position: fixed;
    left: 8px;
    right: 8px;
    top: 8px;
    display: grid;
    gap: 12px;
    align-items: center;
    grid-template-columns: 44px max-content 1fr;
    /* menu | título | busca */
    padding: 8px 10px;
    background: transparent;
    border-radius: var(--radius-md);
    z-index: 5000;
}

#menu-icon {
    width: 44px;
    height: 44px;
    display: grid;
    place-items: center;
    cursor: pointer;
    border-radius: var(--radius-sm);
    background: #fff;
    color: var(--accent);
    font-size: 18px;
}

#app-title {
    display: inline-flex;
    align-items: center;
    gap: 10px;
    white-space: nowrap;
    min-width: 150px;
    margin: 0;
    padding: 0 12px;
    height: 44px;
    line-height: 44px;
    font-size: 16px;
    font-weight: 900;
    letter-spacing: .3px;
    color: var(--ink);
    border-radius: var(--radius-sm);
}

#app-title .app-logo {
    width: 60px;
    height: 60px;
    object-fit: contain;
    display: block;
}

.search-controls {
    position: relative;
    width: 100%;
    justify-self: end;
    max-width: 50%;
}

#search-input {
    height: 44px;
    width: 100%;
    background: #fff;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 10px 14px;
    box-shadow: var(--shadow-sm);
    font-size: var(--font-s);
}

.search-dropdown {
    position: absolute;
    left: 0;
    right: 0;
    top: calc(100% + 6px);
    width: 100%;
    max-height: 300px;
    overflow-y: auto;
    background: #fff;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    box-shadow: var(--shadow-md);
    z-index: 6000;
}

/* "x" do search com cursor 👆 */
#search-input::-webkit-search-cancel-button {
    cursor: pointer;
}

#search-input::-webkit-search-cancel-button:hover {
    filter: brightness(.9);
    cursor: pointer;
}

#search-input::-ms-clear {
    cursor: pointer !important;
}

/* ===== Sidebar ===== */
#sidebar {
    position: fixed;
    top: calc(var(--nav-h) + 16px);
    left: 8px;
    bottom: 8px;
    width: var(--side-w);
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    box-shadow: var(--shadow-md);
    overflow: hidden;
    transform: translateX(calc(-100% - 24px));
    opacity: 0;
    transition: transform .18s ease, opacity .14s ease;
    z-index: 4000;
}

#sidebar.open {
    transform: translateX(0);
    opacity: 1;
}

/* rolagem ok */
#sidebar .scroll {
    position: relative;
    height: 100%;
    box-sizing: border-box;
    overflow: auto;
    padding: 10px;
    padding-bottom: 96px;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
    touch-action: pan-y;
}

/* >>> POR PADRÃO (DESKTOP): oculto <<< */
.sidebar-app-title {
    display: none;
    align-items: center;
    gap: 8px;
    position: sticky;
    top: 0;
    z-index: 1;
    padding: 10px;
    margin: -10px -10px 10px;
    background: #fff;
    border-bottom: 1px solid var(--border);
}

.sidebar-app-title .badge {
    display: inline-grid;
    place-items: center;
    width: 55px;
    height: 55px;
    border-radius: var(--radius-sm);
    background: #fff;
    padding-left: 13px;
    font-weight: 800;
    color: var(--accent);
}

.sidebar-app-title .app-logo {
    width: 50px;
    height: 50px;
    object-fit: contain;
}

.sidebar-app-title h2 {
    margin: 0;
    font-size: 16px;
    font-weight: 900;
    color: var(--ink);
    letter-spacing: .3px;
    display: inline-flex;
    padding-left: 13px;
    align-items: center;
    gap: 8px;
}

/* Backdrop */
#backdrop {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, .28);
    backdrop-filter: blur(2px);
    display: none;
    z-index: 3500;
}

/* ===== Mapa ===== */
#map {
    position: absolute;
    inset: 0;
    top: calc(var(--nav-h) + 15px);
}

.leaflet-top,
.leaflet-bottom,
.leaflet-control-container {
    z-index: 1200;
}

#sidebar.open~#map .leaflet-top.leaflet-left {
    left: calc(8px + min(var(--side-w), 100vw - 16px) + 8px) !important;
}

/* ===== Ferramentas ===== */
.tools-right {
    position: fixed;
    right: 8px;
    top: calc(var(--nav-h) + 24px);
    display: flex;
    flex-direction: column;
    gap: 8px;
    z-index: 4400;
}

.tools-right.is-hidden {
    display: none !important;
}

.icon-button {
    min-width: 44px;
    min-height: 44px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: #fff;
    box-shadow: var(--shadow-sm);
    cursor: pointer;
    font-size: 16px;
}

/* Accordion */
.section-title {
    background: #f6fafc;
    border: 1px solid var(--border);
    color: var(--accent);
    padding: 8px 10px;
    margin: 8px 0 6px;
    border-radius: var(--radius-sm);
    font-weight: 800;
    font-size: .95rem;
    display: flex;
    align-items: center;
    justify-content: space-between;
    cursor: pointer;
}

.toggle-arrow {
    font-size: 12px;
    color: var(--muted);
}

.layer-group {
    background: #fbfcfe;
    border: 1px dashed var(--border);
    border-radius: var(--radius-sm);
    padding: 6px 8px;
    margin: 0 0 10px;
    display: none;
}

.layer-group.open {
    display: block;
}

.layer-option {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px 12px;
    margin: 6px 0;
    background: #fff;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
}

.layer-option input[type="checkbox"] {
    width: 22px;
    height: 22px;
    accent-color: var(--accent);
}

.layer-option label {
    margin: 0;
    font-size: clamp(.9rem, .9rem + .2vw, 1rem);
    line-height: 1.25;
    word-break: break-word;
}

.sidebar-highlight {
    outline: 2px solid rgba(31, 75, 90, .22);
}

/* ===== Mobile (≤ 991px) ===== */
@media (max-width: 991px) {
    :root {
        --nav-h: 56px;
    }

    #header {
        grid-template-columns: 44px 1fr;
    }

    /* >>> MOBILE: esconder título/logo do header <<< */
    #app-title {
        display: none;
    }

    .search-controls {
        max-width: 100%;
        justify-self: stretch;
    }

    #search-input {
        height: 42px;
    }

    /* >>> MOBILE: mostrar título/logo no sidebar <<< */
    .sidebar-app-title {
        display: flex;
    }

    #map {
        top: calc(var(--nav-h) + 16px);
    }

    .tools-right {
        left: 8px;
        right: 8px;
        bottom: 8px;
        top: auto;
        display: block;
        overflow-x: auto;
        overflow-y: hidden;
        white-space: nowrap;
        padding: 6px;
        background: rgba(255, 255, 255, .92);
        border: 1px solid var(--border);
        border-radius: var(--radius-md);
        box-shadow: var(--shadow-md);
    }

    .tools-right .icon-button {
        display: inline-flex;
        margin-right: 6px;
    }
}

/* Scrollbar sutil (webkit) */
#sidebar .scroll::-webkit-scrollbar {
    width: 10px
}

#sidebar .scroll::-webkit-scrollbar-thumb {
    background: #dfe7ee;
    border-radius: var(--radius-xs);
    border: 3px solid #fff;
}

/* ================== PRINT: SOMENTE O MAPA ================== */
@media print {
    @page {
        size: A4 landscape;
        margin: 0;
    }

    html,
    body {
        height: 100%;
        margin: 0 !important;
        padding: 0 !important;
        background: #fff !important;
    }

    body * {
        visibility: hidden !important;
    }

    #map,
    #map * {
        visibility: visible !important;
    }

    #header,
    #sidebar,
    #backdrop,
    .tools-right,
    .leaflet-control-container {
        display: none !important;
    }

    #map {
        position: static !important;
        height: 100vh !important;
        width: 100vw !important;
        inset: auto !important;
        page-break-inside: avoid;
    }

    /* comente se quiser ver os controles no papel */
    .leaflet-control-container {
        display: none !important;
    }
}