/* Mapa fullscreen — layout independiente del sidebar */

.map-layout {
    margin: 0;
    background: var(--bg, #0f172a);
    color: var(--text, #f8fafc);
    font-family: system-ui, -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
    font-size: 16px;
    line-height: 1.5;
    height: 100dvh;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

/* Barra superior — por encima del mapa, por debajo de drawers/modales */
.map-header-wrap {
    flex-shrink: 0;
    position: relative;
    z-index: 200;
    background: var(--bg-panel, #1e293b);
    border-bottom: 1px solid var(--border, #475569);
}

.map-header-row,
.map-header-shell,
.map-topbar {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 8px 12px;
    min-height: 52px;
}

.map-logo {
    color: var(--text, #f8fafc);
    text-decoration: none;
    font-size: 1.1rem;
    font-weight: 600;
    white-space: nowrap;
    flex-shrink: 0;
}
.map-logo strong { color: #38bdf8; }

.map-nav {
    display: flex;
    gap: 4px;
    flex: 1;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
}
.map-nav a {
    color: var(--text-soft, #cbd5e1);
    text-decoration: none;
    padding: 8px 12px;
    border-radius: 8px;
    font-size: 0.9375rem;
    font-weight: 500;
    white-space: nowrap;
}
.map-nav a:hover, .map-nav a.active {
    background: var(--nav-hover, #334155);
    color: var(--text, #fff);
}

.map-topbar-actions {
    display: flex;
    align-items: center;
    gap: 6px;
    flex-shrink: 0;
}

.map-menu-btn {
    display: none;
    flex-shrink: 0;
    width: 44px;
    height: 44px;
    padding: 0;
    border: 1px solid var(--border, #475569);
    border-radius: 10px;
    background: var(--bg-panel, #334155);
    color: var(--text, #fff);
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
}
.map-menu-btn[aria-expanded="true"] {
    background: #0284c7;
    border-color: #0284c7;
}
.map-menu-icon,
.map-menu-icon::before,
.map-menu-icon::after {
    display: block;
    width: 20px;
    height: 2px;
    background: currentColor;
    border-radius: 2px;
    position: relative;
}
.map-menu-icon { margin: 0 auto; }
.map-menu-icon::before,
.map-menu-icon::after {
    content: "";
    position: absolute;
    left: 0;
}
.map-menu-icon::before { top: -6px; }
.map-menu-icon::after { top: 6px; }
.map-menu-btn[aria-expanded="true"] .map-menu-icon { background: transparent; }
.map-menu-btn[aria-expanded="true"] .map-menu-icon::before {
    top: 0;
    transform: rotate(45deg);
}
.map-menu-btn[aria-expanded="true"] .map-menu-icon::after {
    top: 0;
    transform: rotate(-45deg);
}

.map-nav-backdrop {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, .45);
    z-index: 190;
    pointer-events: none;
}
.map-nav-backdrop.show {
    display: block;
    pointer-events: auto;
}

body.map-nav-open .map-body,
body.map-nav-open #agromaps-map,
body.map-nav-open .map-fallback {
    pointer-events: none !important;
}

.map-nav-mobile-actions {
    display: none;
    gap: 8px;
    padding-top: 4px;
    margin-top: 4px;
    border-top: 1px solid #475569;
}
.map-nav-action { flex: 1; }

/* Botones mapa */
.map-layout .btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-height: 36px;
    padding: 0 14px;
    border-radius: 8px;
    font-size: 0.9375rem;
    font-weight: 600;
    cursor: pointer;
    border: none;
    text-decoration: none;
    white-space: nowrap;
}
.map-layout .btn-primary { background: #0284c7; color: #fff; }
.map-layout .btn-primary:hover { background: #0369a1; }
.map-layout .btn-outline {
    background: var(--bg-input, #334155);
    color: var(--text, #f8fafc);
    border: 1px solid var(--border, #64748b);
}
.map-layout .btn-outline:hover { background: var(--nav-hover, #475569); }
.map-layout .btn-sm { min-height: 36px; padding: 0 12px; font-size: 0.875rem; }
.map-btn-badge {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 18px;
    height: 18px;
    margin-left: 6px;
    padding: 0 5px;
    border-radius: 999px;
    background: #ef4444;
    color: #fff;
    font-size: 0.6875rem;
    font-weight: 700;
    line-height: 1;
}
.map-btn-badge[hidden] { display: none !important; }

.btn-refresh {
    font-size: 1.125rem;
    line-height: 1;
    min-width: 36px;
    padding: 0 10px;
}
.btn-refresh.refreshing {
    animation: map-refresh-spin 0.7s linear infinite;
    opacity: 0.85;
}
@keyframes map-refresh-spin {
    to { transform: rotate(360deg); }
}

/* Cuerpo del mapa — CRÍTICO: flex 1 + min-height 0 */
.map-body {
    flex: 1 1 auto;
    min-height: 0;
    position: relative;
    background: var(--radar-map-body, var(--bg-panel, #1e293b));
    width: 100%;
}

#agromaps-map {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 10;
    background: #cbd5e1;
}

#agromaps-map.leaflet-container,
#agromaps-map .leaflet-container {
    width: 100% !important;
    height: 100% !important;
    font-family: inherit !important;
}

.map-fallback {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    border: 0;
    z-index: 5;
}
.map-fallback.hidden { visibility: hidden; pointer-events: none; }

.map-loading {
    position: absolute;
    inset: 0;
    z-index: 20;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 12px;
    background: rgba(15, 23, 42, 0.75);
    color: #f8fafc;
    font-size: 1rem;
    pointer-events: none;
}
.map-loading.hidden { display: none; }

.spinner {
    width: 40px;
    height: 40px;
    border: 4px solid #475569;
    border-top-color: #38bdf8;
    border-radius: 50%;
    animation: spin .7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* Stats inferior */
.map-stats {
    flex-shrink: 0;
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: center;
    gap: 16px 24px;
    padding: 10px 12px;
    background: var(--bg-panel, #1e293b);
    border-top: 1px solid var(--border, #475569);
    font-size: 0.875rem;
    color: var(--text-soft, #cbd5e1);
    z-index: 200;
}
.map-stats div {
    display: flex;
    flex-direction: column;
    align-items: center;
    min-width: 64px;
}
.map-stats strong {
    font-size: 1.125rem;
    color: var(--accent-strong, #0284c7);
    line-height: 1.2;
}
.map-stats span { font-size: 0.75rem; color: var(--text-muted, #94a3b8); }
.map-badge {
    font-size: 0.75rem;
    color: var(--text-muted, #94a3b8);
    padding: 4px 8px;
    background: var(--bg-input, #0f172a);
    border: 1px solid var(--border, transparent);
    border-radius: 6px;
}

/* Drawers — por encima del header del mapa */
.drawer-backdrop {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,.5);
    z-index: 800;
}
.drawer-backdrop.show { display: block; }

.drawer {
    position: fixed;
    top: 0;
    bottom: 0;
    left: 0;
    width: min(360px, 100%);
    max-width: 100%;
    background: var(--bg-panel, #1e293b);
    border-right: 1px solid var(--border, #475569);
    color: var(--text, #f8fafc);
    z-index: 810;
    transform: translateX(-100%);
    transition: transform .25s ease;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}
.drawer.open { transform: translateX(0); }
.drawer-right {
    left: auto;
    right: 0;
    border-right: none;
    border-left: 1px solid var(--border, #475569);
    transform: translateX(100%);
}
.drawer-right.open { transform: translateX(0); }

.drawer-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 14px 16px;
    border-bottom: 1px solid var(--border, #475569);
    flex-shrink: 0;
}
.drawer-header h2 { font-size: 1.0625rem; font-weight: 700; color: var(--text, #f8fafc); }
.drawer-close {
    width: 36px;
    height: 36px;
    border: 1px solid var(--border, #64748b);
    border-radius: 8px;
    background: var(--bg-input, #334155);
    color: var(--text, #f8fafc);
    font-size: 1.25rem;
    cursor: pointer;
}

.drawer-body {
    padding: 16px;
    overflow-y: auto;
    flex: 1;
    -webkit-overflow-scrolling: touch;
}

.drawer-section-title {
    font-size: 0.8125rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: .04em;
    color: var(--text-muted, #94a3b8);
    margin: 16px 0 8px;
}
.drawer-section-title:first-child { margin-top: 0; }

.chip-group { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
    padding: 8px 14px;
    border-radius: 20px;
    border: 1px solid var(--border, #64748b);
    background: var(--bg-input, #334155);
    color: var(--text, #f8fafc);
    font-size: 0.875rem;
    font-weight: 500;
    cursor: pointer;
}
.chip.active {
    background: #0284c7;
    border-color: #0284c7;
    color: #fff;
}

.legend-list { list-style: none; }
.legend-list li {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 6px 0;
    font-size: 0.9375rem;
    color: var(--text-soft, #e2e8f0);
}
.legend-list .dot {
    width: 14px;
    height: 14px;
    border-radius: 50%;
    flex-shrink: 0;
}

.field-label {
    display: block;
    font-size: 0.875rem;
    font-weight: 600;
    color: var(--field-label, var(--text-soft, #e2e8f0));
    margin: 12px 0 6px;
}
.field-input {
    width: 100%;
    min-height: 44px;
    padding: 10px 12px;
    font-size: 1rem;
    color: var(--text, #f8fafc);
    background: var(--bg-input, #0f172a);
    border: 1px solid var(--border, #64748b);
    border-radius: 8px;
}

.alert-item {
    padding: 14px;
    margin-bottom: 10px;
    background: var(--bg-input, #0f172a);
    border: 1px solid var(--border, #475569);
    border-left: 4px solid #eab308;
    border-radius: 8px;
    cursor: pointer;
}
.alert-item.sev-4, .alert-item.sev-5 { border-left-color: #ef4444; }
.alert-item h3 { font-size: 1rem; font-weight: 700; color: var(--text, #f8fafc); margin-bottom: 6px; line-height: 1.35; }
.alert-item p { font-size: 0.9375rem; color: var(--text-soft, #cbd5e1); line-height: 1.45; margin-bottom: 6px; }
.alert-item time { font-size: 0.8125rem; color: var(--text-muted, #94a3b8); }
.alert-type-badge {
    display: inline-block;
    margin-bottom: 8px;
    padding: 2px 8px;
    border-radius: 999px;
    background: var(--nav-hover, #334155);
    color: var(--accent-strong, #0284c7);
    font-size: 0.6875rem;
    font-weight: 700;
    letter-spacing: .03em;
    text-transform: uppercase;
}
.alert-go-map {
    display: inline-block;
    margin-top: 8px;
    color: var(--accent-strong, #0284c7);
    font-size: 0.8125rem;
    font-weight: 600;
}
.muted { color: var(--text-muted, #94a3b8); font-size: 0.9375rem; }

/* Modal detalle reporte en mapa */
.map-layout .modal {
    display: none;
    position: fixed;
    inset: 0;
    z-index: 2400;
    background: var(--overlay, rgba(0,0,0,.6));
    align-items: center;
    justify-content: center;
    padding: 16px;
}
.map-layout .modal.open { display: flex; }
.predict-modal { align-items: center; justify-content: center; padding: 16px; }
.modal-box {
    background: var(--bg-panel, #1e293b);
    border: 1px solid var(--border, #475569);
    border-radius: 12px;
    padding: 20px;
    width: min(480px, 100%);
    max-height: 85vh;
    overflow-y: auto;
    position: relative;
    color: var(--text, #f8fafc);
}
.modal-x {
    position: absolute;
    top: 12px;
    right: 12px;
    width: 36px;
    height: 36px;
    border: none;
    border-radius: 8px;
    background: #334155;
    color: #fff;
    font-size: 1.25rem;
    cursor: pointer;
}

.modal-detail-title {
    margin: 0 0 8px;
    padding-right: 40px;
    font-size: 1.125rem;
    font-weight: 700;
    color: #f8fafc;
}
.modal-detail-desc {
    margin: 0 0 14px;
    color: #cbd5e1;
    line-height: 1.55;
    font-size: 0.9375rem;
}
.modal-detail-meta {
    display: grid;
    gap: 10px;
    margin: 0;
}
.modal-detail-meta div {
    display: flex;
    justify-content: space-between;
    gap: 12px;
    padding: 8px 0;
    border-top: 1px solid #475569;
}
.modal-detail-meta dt {
    color: #94a3b8;
    font-size: 0.8125rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: .03em;
}
.modal-detail-meta dd {
    margin: 0;
    color: #f8fafc;
    font-weight: 600;
    text-align: right;
}
.modal-sev-badge {
    display: inline-block;
    padding: 2px 10px;
    border-radius: 6px;
    font-size: 0.875rem;
}
.modal-sev-badge.risk-low { background: #14532d; color: #bbf7d0; }
.modal-sev-badge.risk-mid { background: #713f12; color: #fef08a; }
.modal-sev-badge.risk-high { background: #7c2d12; color: #fed7aa; }
.modal-sev-badge.risk-critical { background: #7f1d1d; color: #fecaca; }

/* Modal predicción IA */
.predict-modal { z-index: 2450; }
.predict-modal-box {
    width: min(400px, calc(100% - 32px));
    padding: 28px 24px 24px;
    text-align: center;
}
.predict-modal-head {
    padding: 0 8px 20px;
    border-bottom: 1px solid #475569;
    margin-bottom: 20px;
}
.predict-modal-icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 52px;
    height: 52px;
    margin-bottom: 12px;
    border-radius: 50%;
    background: linear-gradient(135deg, #0284c7 0%, #6366f1 100%);
    font-size: 1.5rem;
    line-height: 1;
    box-shadow: 0 8px 24px rgba(2, 132, 199, .35);
}
.predict-modal-head h3 {
    margin: 0 0 6px;
    font-size: 1.25rem;
    font-weight: 700;
    color: #f8fafc;
}
.predict-modal-sub {
    margin: 0;
    font-size: 0.875rem;
    color: #94a3b8;
}
.predict-modal-body {
    padding: 8px 12px 4px;
}
.predict-probability {
    font-size: 2.75rem;
    font-weight: 800;
    line-height: 1.1;
    color: #38bdf8;
    margin-bottom: 16px;
    letter-spacing: -.02em;
}
.predict-probability.risk-low { color: #22c55e; }
.predict-probability.risk-mid { color: #eab308; }
.predict-probability.risk-high { color: #f97316; }
.predict-probability.risk-critical { color: #ef4444; }
.predict-summary {
    margin: 0;
    padding: 14px 16px;
    background: #0f172a;
    border: 1px solid #475569;
    border-radius: 12px;
    font-size: 0.9375rem;
    line-height: 1.55;
    color: #e2e8f0;
    text-align: left;
}
.predict-modal-hint {
    margin: 18px 0 0;
    padding: 0 8px;
    font-size: 0.8125rem;
    color: #64748b;
    line-height: 1.45;
}

.drawer-hint {
    margin: 0 0 12px;
    padding: 10px 12px;
    background: #0f172a;
    border: 1px solid #475569;
    border-radius: 8px;
    font-size: 0.8125rem;
    line-height: 1.45;
    color: #94a3b8;
}

/* Leaflet popups legibles */
.leaflet-popup-content-wrapper {
    background: #1e293b !important;
    color: #f8fafc !important;
    border: 1px solid #475569 !important;
    border-radius: 8px !important;
}
.leaflet-popup-content { font-size: 0.9375rem !important; margin: 12px 14px !important; }
.leaflet-popup-tip { background: #1e293b !important; }
.leaflet-control-zoom a {
    background: #1e293b !important;
    color: #f8fafc !important;
    border-color: #64748b !important;
}

/* Google Maps InfoWindow — texto legible sobre fondo blanco */
.gm-style .gm-style-iw-c { padding: 0 !important; }
.gm-style .gm-style-iw-d { overflow: hidden !important; color: #0f172a !important; }
.gm-style .gm-style-iw-d * { color: inherit; }
.leaflet-popup-content.predict-popup,
.leaflet-container .predict-popup .leaflet-popup-content {
    color: #0f172a !important;
}

/* Responsive — desktop */
@media (min-width: 1367px) {
    .map-header-wrap {
        display: flex;
        align-items: center;
        gap: 8px;
        padding-right: 12px;
    }

    .map-header-row {
        flex: 0 0 auto;
        padding-right: 0;
    }

    .map-menu-btn {
        display: none !important;
    }

    .map-nav-backdrop {
        display: none !important;
    }

    .map-nav {
        display: flex !important;
        position: static;
        flex: 1;
        flex-direction: row;
        padding: 8px 0;
        border: none;
        box-shadow: none;
        max-height: none;
        overflow-x: auto;
        pointer-events: auto;
    }

    .map-nav-mobile-actions {
        display: none !important;
    }

    .map-topbar-actions {
        margin-left: auto;
    }
}

/* Responsive — móvil y tablets, incl. horizontal (≤1366px) */
.map-report-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 4px;
}

.map-report-btn .map-btn-icon {
    font-size: 1.05rem;
    font-weight: 700;
    line-height: 1;
}

.map-report-btn .map-btn-label,
.map-report-btn .report-btn-text {
    display: inline;
    font-weight: 700;
}

@media (max-width: 1366px) {
    .map-header-row,
    .map-header-shell,
    .map-topbar {
        padding: 8px 10px;
        padding-top: max(8px, env(safe-area-inset-top));
        gap: 8px;
        flex-wrap: nowrap;
    }

    .map-logo {
        flex: 0 1 auto;
        min-width: 0;
        max-width: 42vw;
        font-size: 0.875rem;
        overflow: hidden;
        text-overflow: ellipsis;
    }

    .map-menu-btn {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        flex-shrink: 0;
    }

    .map-header-wrap > .map-nav,
    .map-nav {
        display: none !important;
        position: fixed;
        top: calc(52px + env(safe-area-inset-top, 0px));
        left: 0;
        right: 0;
        flex: none;
        background: #1e293b;
        border-bottom: 1px solid #475569;
        padding: 12px;
        padding-bottom: max(12px, env(safe-area-inset-bottom));
        z-index: 201;
        flex-direction: column;
        gap: 4px;
        box-shadow: 0 12px 32px rgba(0, 0, 0, .35);
        max-height: calc(100dvh - 52px - env(safe-area-inset-top, 0px));
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
        pointer-events: auto;
    }

    .map-nav.mobile-open {
        display: flex !important;
    }

    .map-nav.mobile-open a,
    .map-nav.mobile-open button {
        pointer-events: auto;
        touch-action: manipulation;
        position: relative;
        z-index: 1;
    }

    .map-nav.mobile-open a {
        display: flex;
        align-items: center;
        min-height: 48px;
        padding: 12px 14px;
        font-size: 1rem;
        border-radius: 10px;
    }

    .map-nav-mobile-actions {
        display: flex;
    }

    .map-topbar-actions {
        flex-shrink: 0;
        gap: 6px;
        margin-left: auto;
    }

    .map-topbar-actions #btn-layers {
        display: none;
    }

    .map-topbar-actions #btn-refresh {
        min-width: 40px;
        padding: 0;
        font-size: 1.25rem;
    }

    .map-topbar-actions .btn-sm {
        min-height: 40px;
        padding: 0 10px;
        font-size: 0.8125rem;
    }

    .map-topbar-actions #btn-alerts::before {
        content: "A";
        font-size: 0.8125rem;
        font-weight: 800;
        line-height: 1;
    }

    .map-topbar-actions #btn-alerts {
        display: inline-flex;
        align-items: center;
        gap: 4px;
        min-width: 40px;
        padding: 0 8px;
    }

    .map-topbar-actions #btn-alerts .map-btn-label {
        display: none;
    }

    .map-topbar-actions .map-report-btn {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        min-width: auto;
        padding: 0 12px;
        font-size: inherit;
        gap: 4px;
    }

    .map-topbar-actions .map-report-btn::after {
        content: none;
        display: none;
    }

    .map-topbar-actions .map-report-btn .map-btn-icon {
        display: none;
    }

    .map-topbar-actions .map-report-btn .map-btn-label,
    .map-topbar-actions .map-report-btn .report-btn-text {
        display: inline;
        font-size: 0.8125rem;
        font-weight: 700;
        line-height: 1.2;
    }

    .drawer { width: 100%; }
    .drawer-header {
        padding-top: max(14px, env(safe-area-inset-top));
    }
    .map-stats { gap: 10px 16px; padding: 8px; padding-bottom: max(8px, env(safe-area-inset-bottom)); }
    .predict-modal-box { padding: 24px 20px 20px; }
}
