/*
 * Adaptación responsiva de la aplicación MACOV.
 *
 * Se carga después de los estilos históricos para corregir dimensiones rígidas
 * y conservar una experiencia utilizable con zoom del navegador y pantallas
 * táctiles. El escritorio de más de 1400 px mantiene la composición original.
 */

.mobile-header-menu,
.mobile-map-switcher {
    display: none;
}

/* Escritorios con zoom o portátiles: liberar espacio del imagotipo institucional. */
@media (max-width: 1400px) {
    .app-header-inner {
        grid-template-columns: minmax(0, 1fr) auto;
        gap: 1rem;
        padding-inline: 1.25rem;
    }

    .banner-logo {
        display: none;
    }

    .header-title-text h1 {
        font-size: clamp(1.1rem, 2vw, 1.45rem);
    }

    .header-actions .hb-label {
        display: none;
    }
}

/* Al apilar los dos mapas, flex:1 ya no puede anular su altura. */
@media (min-width: 1025px) and (max-width: 1200px) {
    .dual-map-container {
        flex-direction: column;
        height: auto;
    }

    .map-panel {
        flex: 0 0 auto;
        width: 100%;
        height: clamp(480px, 62svh, 720px);
        min-height: 480px;
    }
}

/* Tabletas, teléfonos y escritorios cuyo zoom reduce el viewport efectivo. */
@media (max-width: 1024px) {
    html {
        scroll-padding-top: 58px;
    }

    body {
        min-width: 0;
        overflow-x: clip;
    }

    #mainAppContent,
    .container,
    .container-fluid,
    .row,
    .row > * {
        min-width: 0;
        max-width: 100%;
    }

    /* Encabezado compacto con navegación bajo demanda. */
    .app-header {
        min-height: 0;
        overflow: visible;
    }

    .app-header-inner {
        display: grid;
        grid-template-columns: minmax(0, 1fr) 44px;
        grid-template-rows: auto auto;
        align-items: center;
        gap: 8px;
        padding: 9px 12px;
        text-align: left;
    }

    .header-title {
        width: 100%;
        min-width: 0;
        gap: 9px;
    }

    .header-logo-macov {
        flex: 0 0 auto;
        height: 38px;
    }

    .header-title-text {
        min-width: 0;
    }

    .header-title-text h1 {
        overflow: hidden;
        font-size: clamp(1rem, 4vw, 1.25rem);
        line-height: 1.15;
        white-space: nowrap;
        text-overflow: ellipsis;
    }

    .header-title-text h1 small {
        font-size: 0.58em;
    }

    .mobile-header-menu {
        display: inline-flex;
        width: 44px;
        height: 44px;
        align-items: center;
        justify-content: center;
        justify-self: end;
        border: 1px solid rgba(255, 255, 255, 0.35);
        border-radius: 9px;
        background: rgba(13, 59, 50, 0.72);
        color: #fff;
        font-size: 1rem;
        cursor: pointer;
        touch-action: manipulation;
    }

    .mobile-header-menu:focus-visible,
    .mobile-map-tab:focus-visible {
        outline: 3px solid var(--profepa-beige);
        outline-offset: 2px;
    }

    .header-actions {
        display: none;
        grid-column: 1 / -1;
        grid-template-columns: repeat(5, minmax(44px, 1fr));
        width: 100%;
        gap: 6px;
        padding-top: 2px;
    }

    .app-header.mobile-menu-open .header-actions {
        display: grid;
    }

    .header-actions > .header-btn {
        width: 100%;
        min-width: 44px;
        min-height: 44px;
        justify-content: center;
        padding: 8px;
        font-size: 0.95rem;
        touch-action: manipulation;
    }

    .header-actions .hb-label {
        display: none;
    }

    #navUserArea {
        grid-column: 1 / -1;
        width: 100%;
        min-width: 0;
        flex-wrap: wrap;
        justify-content: center;
    }

    #navUserArea .btn-admin-nav,
    #navUserArea .btn-logout {
        min-height: 44px;
        padding: 8px 12px;
        touch-action: manipulation;
    }

    #navUserArea .navbar-user {
        min-width: 0;
        min-height: 44px;
        flex: 1 1 180px;
        justify-content: center;
    }

    #navUserArea .user-name {
        min-width: 0;
        overflow: hidden;
        white-space: nowrap;
        text-overflow: ellipsis;
    }

    /* En móvil se trabaja con un mapa a la vez. */
    .mobile-map-switcher {
        position: sticky;
        top: 0;
        z-index: 1450;
        display: grid;
        grid-template-columns: repeat(2, minmax(0, 1fr));
        gap: 6px;
        padding: 8px 10px;
        border-bottom: 1px solid rgba(74, 157, 127, 0.35);
        background: rgba(8, 31, 27, 0.96);
        box-shadow: 0 5px 16px rgba(0, 0, 0, 0.35);
        backdrop-filter: blur(10px);
    }

    .mobile-map-tab {
        display: inline-flex;
        min-width: 0;
        min-height: 44px;
        align-items: center;
        justify-content: center;
        gap: 8px;
        padding: 8px 10px;
        border: 1px solid rgba(255, 255, 255, 0.2);
        border-radius: 9px;
        background: rgba(255, 255, 255, 0.06);
        color: rgba(255, 255, 255, 0.8);
        font-size: 0.82rem;
        font-weight: 600;
        cursor: pointer;
        touch-action: manipulation;
    }

    .mobile-map-tab span {
        min-width: 0;
        overflow: hidden;
        white-space: nowrap;
        text-overflow: ellipsis;
    }

    .mobile-map-tab.active {
        border-color: var(--profepa-beige);
        background: linear-gradient(135deg, rgba(30, 91, 79, 0.95), rgba(0, 47, 42, 0.95));
        color: #fff;
        box-shadow: 0 0 0 1px rgba(230, 209, 148, 0.25);
    }

    .dual-map-container {
        display: block;
        height: auto;
        padding: 8px;
        background: var(--color-fondo);
    }

    .dual-map-container .map-panel {
        display: none;
        flex: none;
        width: 100%;
        height: clamp(420px, 62svh, 680px);
        min-height: 420px;
        border-radius: 12px;
    }

    .dual-map-container .map-panel.is-mobile-active {
        display: block;
    }

    .dual-map-container .map-panel[hidden] {
        display: none !important;
    }

    .dual-map-container.fs-left,
    .dual-map-container.fs-right {
        display: block;
        height: 100dvh;
    }

    .dual-map-container.fs-left .map-panel:first-child,
    .dual-map-container.fs-right .map-panel:nth-child(2) {
        display: block;
        height: 100dvh;
        min-height: 0;
    }

    .map-panel-header {
        min-height: 56px;
        align-items: flex-start;
        gap: 8px;
        padding: 8px 9px;
    }

    .map-panel-header h5 {
        min-width: 0;
        gap: 6px;
        padding-top: 6px;
        font-size: 0.8rem;
    }

    .map-panel-header .map-panel-title {
        overflow: hidden;
        white-space: nowrap;
        text-overflow: ellipsis;
    }

    #analysisMapPanel .map-panel-header .badge.bg-success,
    #syncStatusBadge {
        display: none;
    }

    .map-header-tools {
        flex: 0 0 auto;
        flex-wrap: wrap;
        justify-content: flex-end;
        gap: 4px;
    }

    .map-header-tools .map-opacity-ctl {
        display: none;
    }

    .map-panel-header .map-header-tools button {
        width: 44px;
        min-width: 44px;
        height: 44px;
        padding: 0;
        touch-action: manipulation;
    }

    #map,
    #satelliteMap {
        min-height: inherit;
    }

    .leaflet-control-container .leaflet-top.leaflet-left,
    .leaflet-control-container .leaflet-top.leaflet-right {
        padding-top: 60px;
    }

    .leaflet-control-geosearch.leaflet-geosearch-bar,
    .leaflet-geosearch-bar {
        width: min(330px, calc(100% - 124px)) !important;
        min-width: 170px;
    }

    .leaflet-control a,
    .leaflet-draw-toolbar a {
        min-width: 44px !important;
        min-height: 44px !important;
        line-height: 44px !important;
    }

    .leaflet-control-layers-expanded {
        max-width: calc(100vw - 36px);
        max-height: 55svh;
        overflow: auto;
        overscroll-behavior: contain;
    }

    .leaflet-control-attribution {
        max-width: 74%;
        overflow: hidden;
        font-size: 8px !important;
        white-space: nowrap;
        text-overflow: ellipsis;
    }

    /* Control temporal y categorías sin dimensiones de escritorio. */
    .satellite-controller {
        width: auto;
        max-width: none;
        margin: 8px;
        padding: 16px;
        border-radius: 12px;
    }

    .timeline-container,
    .satellite-controls {
        min-width: 0;
    }

    .timeline-header {
        min-height: 44px;
        flex-wrap: wrap;
        gap: 8px;
    }

    .timeline-header > span {
        min-width: 160px;
        flex: 1 1 auto;
        font-size: 0.88rem;
        font-weight: 600;
    }

    #timelineSettingsBtn {
        min-height: 40px;
        margin-left: 0 !important;
        touch-action: manipulation;
    }

    .timeline-label {
        display: flex;
        min-width: 0;
        flex-wrap: wrap;
        gap: 8px;
        font-size: 0.95rem;
    }

    #monthSelector {
        width: auto !important;
        min-width: min(220px, 100%);
        flex: 1 1 220px;
        min-height: 42px;
        margin-left: 0 !important;
    }

    #openMonthImagesBtn {
        min-height: 42px;
        margin-left: 0 !important;
    }

    .timeline-slider {
        margin-top: 10px;
    }

    .satellite-tabs {
        min-width: 0;
        margin: 0 !important;
    }

    .tab-header {
        min-height: 58px;
        padding: 11px 12px;
        touch-action: manipulation;
    }

    .tab-header-content,
    .tab-text {
        min-width: 0;
    }

    .tab-subtitle {
        overflow-wrap: anywhere;
    }

    .tab-content.active {
        max-height: none;
        overflow: visible;
        padding: 12px;
    }

    .tab-buttons {
        grid-template-columns: minmax(0, 1fr);
    }

    .layer-pick {
        min-width: 0;
    }

    .layer-pick .btn-title,
    .layer-pick .btn-desc {
        overflow: visible;
        white-space: normal;
        text-overflow: clip;
    }

    .layer-info-btn {
        min-width: 44px;
        min-height: 44px;
        touch-action: manipulation;
    }

    .control-panel,
    .chart-container,
    .analysis-card {
        min-width: 0;
        max-width: 100%;
    }

    .control-panel {
        padding: 1rem;
    }

    .table-responsive,
    #statisticsTables,
    .analysis-table-container {
        display: block;
        width: 100%;
        max-width: 100%;
        overflow-x: auto;
        overscroll-behavior-inline: contain;
        -webkit-overflow-scrolling: touch;
    }

    canvas {
        max-width: 100% !important;
    }

    .modal-dialog {
        max-width: calc(100vw - 20px);
        margin-inline: auto;
    }

    .modal-content,
    .modal-body,
    .modal-footer {
        min-width: 0;
        max-width: 100%;
    }

    .modal-footer {
        flex-wrap: wrap;
    }
}

@media (max-width: 600px) {
    .header-title-text h1 small {
        display: none;
    }

    .leaflet-control-geosearch.leaflet-geosearch-bar,
    .leaflet-geosearch-bar {
        margin-top: 62px !important;
    }

    .dual-map-container .map-panel {
        height: clamp(400px, 62svh, 590px);
        min-height: 400px;
    }

    .mobile-map-tab {
        font-size: 0.76rem;
    }

    .satellite-controller {
        margin: 7px;
        padding: 12px;
    }

    .timeline-labels {
        display: grid;
        grid-template-columns: repeat(4, minmax(0, 1fr));
        align-items: center;
    }

    .timeline-labels span {
        display: none;
        text-align: center;
    }

    .timeline-labels span:first-child,
    .timeline-labels span:nth-child(3),
    .timeline-labels span:nth-child(5),
    .timeline-labels span:last-child {
        display: block;
    }

    .timeline-options .row > [class*="col-"] {
        width: 100%;
        margin-bottom: 8px;
    }

    .tab-icon-wrapper {
        width: 38px;
        height: 38px;
        flex: 0 0 38px;
    }

    .modal-dialog {
        width: 100%;
        max-width: none;
        min-height: 100dvh;
        margin: 0;
    }

    .modal-dialog-centered {
        min-height: 100dvh;
    }

    .modal-content {
        min-height: 100dvh;
        border-radius: 0 !important;
    }

    .modal-footer > .btn {
        min-height: 44px;
        flex: 1 1 130px;
    }
}

@media (max-width: 380px) {
    .app-header-inner {
        padding-inline: 8px;
    }

    .header-logo-macov {
        height: 34px;
    }

    .header-title-text h1 {
        font-size: 1rem;
    }

    .mobile-map-switcher {
        padding-inline: 7px;
    }

    .mobile-map-tab {
        gap: 5px;
        padding-inline: 6px;
    }

    .map-panel-header .map-panel-title {
        max-width: 115px;
    }
}

@media (max-width: 1024px) and (orientation: landscape) and (max-height: 600px) {
    .dual-map-container .map-panel {
        height: max(330px, calc(100dvh - 70px));
        min-height: 330px;
    }
}

@media (prefers-reduced-motion: reduce) {
    .mobile-map-tab,
    .mobile-header-menu {
        transition: none;
    }
}
