/*
 * Responsive source of truth.
 * Mobile: viewport width <= 768px.
 * Compact phones: viewport width <= 370px.
 * Desktop and intermediate desktop/tablet rules remain in the core styles.
 */

.mobile-legend-toggle {
    display: none;
}

@media (max-width: 768px) {
    :root {
        --sidebar-width: 0px;
        --ui-padding: 10px;
        --ui-gap: 10px;
        --mobile-edge: 10px;
        --mobile-top-offset: 66px;
        --mobile-drawer-width: min(420px, calc(100vw - 16px));
        --perfil-card-max-height: 336px;
    }

    .ui-container {
        display: block;
        padding: 0;
        overflow: visible;
        pointer-events: none;
        z-index: var(--z-drawer);
    }

    .ui-panel {
        pointer-events: auto;
    }

    .mobile-nav-toggle {
        display: inline-flex;
        position: fixed;
        top: var(--mobile-edge);
        z-index: var(--z-mobile-navigation);
        min-width: 44px;
        min-height: 44px;
        align-items: center;
        justify-content: center;
        gap: 7px;
        padding: 0 12px;
        border: 1px solid rgba(255, 255, 255, 0.16);
        border-radius: 10px;
        background: rgba(15, 23, 42, 0.94);
        color: #f8fafc;
        font-family: var(--font-sans);
        font-size: 0.78rem;
        font-weight: 800;
        letter-spacing: 0.01em;
        box-shadow: var(--shadow);
        cursor: pointer;
        pointer-events: auto;
    }

    .mobile-sidebar-toggle {
        left: var(--mobile-edge);
    }

    .mobile-layers-toggle {
        right: 58px;
    }

    body.mobile-sidebar-open .mobile-nav-toggle,
    body.mobile-layers-open .mobile-nav-toggle {
        opacity: 0;
        pointer-events: none;
    }

    .mobile-nav-toggle:focus-visible,
    .mobile-sidebar-close:focus-visible,
    .drawer-close:focus-visible,
    .mobile-legend-toggle:focus-visible,
    .leaflet-control-layers input:focus-visible,
    .map-selector-control select:focus-visible,
    #map-year-slider:focus-visible {
        outline: 3px solid #38bdf8;
        outline-offset: 2px;
    }

    .mobile-overlay-backdrop {
        position: fixed;
        inset: 0;
        z-index: var(--z-backdrop);
        width: 100vw;
        height: 100dvh;
        padding: 0;
        border: 0;
        background: rgba(2, 6, 23, 0.48);
        backdrop-filter: blur(1px);
        -webkit-backdrop-filter: blur(1px);
        cursor: pointer;
    }

    body.mobile-sidebar-open .mobile-overlay-backdrop,
    body.mobile-layers-open .mobile-overlay-backdrop {
        display: block;
    }

    .citizen-panel {
        padding: 7px 12px;
        overflow-x: hidden;
    }

    .citizen-brand {
        gap: 8px;
        padding-bottom: 2px;
    }

    .citizen-eyebrow {
        display: none;
    }

    .citizen-panel h1 {
        font-size: 0.98rem;
        line-height: 1.12;
    }

    .citizen-national-badge {
        padding: 3px 6px;
        font-size: 0.56rem;
    }

    .citizen-question {
        display: none;
    }

    .citizen-intro-full { display: none; }

    .citizen-intro-mobile {
        display: block;
        margin: 3px 0 0;
        overflow: hidden;
        color: #52606b;
        font-size: 0.56rem;
        line-height: 1.15;
        text-overflow: ellipsis;
        white-space: nowrap;
    }

    .citizen-search-label {
        margin-top: 3px;
        margin-bottom: 3px;
    }

    .citizen-search-status:empty {
        display: none;
    }

    .citizen-map-context {
        margin-top: 4px;
        padding: 6px 8px;
    }

    .citizen-map-context strong {
        font-size: 0.7rem;
    }

    .citizen-map-context span {
        font-size: 0.68rem;
        line-height: 1.25;
    }

    .citizen-summary-metrics,
    .citizen-actions {
        grid-template-columns: 1fr 1fr;
    }

    .citizen-panel:not(.has-selection) .citizen-summary,
    .citizen-panel:not(.has-selection) #share-view-button,
    .citizen-panel:not(.has-selection) #download-summary-button {
        display: none;
    }

    .citizen-panel:not(.has-selection) .citizen-actions {
        grid-template-columns: 1fr 1fr;
        margin-top: 7px;
    }

    .institutional-entry {
        min-height: 44px;
        margin-top: 4px;
        padding-block: 7px;
    }

    .citizen-action:last-child {
        grid-column: 1 / -1;
    }

    body.profile-selection-active .citizen-panel {
        visibility: hidden;
        opacity: 0;
        pointer-events: none;
    }

    .sidebar {
        position: fixed;
        top: 0;
        left: 0;
        bottom: 0;
        width: var(--mobile-drawer-width);
        height: 100dvh;
        max-height: 100dvh;
        padding: 16px;
        overflow-x: hidden;
        overflow-y: auto;
        z-index: var(--z-drawer);
        border-radius: 0 12px 12px 0;
        transform: translateX(calc(-100% - 18px));
        opacity: 0;
        pointer-events: none;
        box-shadow: 20px 0 48px rgba(0, 0, 0, 0.42);
        transition: transform 0.24s ease, opacity 0.24s ease;
        scrollbar-gutter: stable;
        overscroll-behavior: contain;
    }

    .sidebar > *,
    .sidebar canvas {
        min-width: 0;
        max-width: 100%;
    }

    body.mobile-sidebar-open .sidebar {
        transform: translateX(0);
        opacity: 1;
        pointer-events: auto;
    }

    /* Header pegajoso para asegurar botón de cierre (×) visible durante el scroll en paneles móviles.
       Cualquier nuevo panel móvil futuro debe seguir este mismo patrón sticky por defecto. */
    .drawer-header {
        position: sticky;
        top: -12px;
        z-index: var(--z-sticky-content);
        margin: -12px -12px 12px;
        padding: 12px 12px 12px;
        background: #18212a;
    }

    .citizen-panel-topbar {
        position: sticky;
        top: -7px;
        z-index: var(--z-sticky-content);
        margin: -7px -12px 6px;
        padding: 7px 12px 6px;
        background: var(--public-surface, #fff);
    }

    .mobile-sidebar-topbar {
        display: flex;
        position: sticky;
        top: -16px;
        z-index: var(--z-sticky-content);
        align-items: center;
        justify-content: space-between;
        gap: 12px;
        margin: -4px -4px 14px;
        padding: 4px 4px 10px;
        border-bottom: 1px solid var(--border-glass);
        background: #172235;
        color: var(--text-primary);
        font-size: 0.76rem;
        font-weight: 800;
        text-transform: uppercase;
        letter-spacing: 0.05em;
    }

    .mobile-sidebar-close {
        position: relative;
        z-index: 2;
        display: inline-flex;
        width: 44px;
        height: 44px;
        flex: 0 0 44px;
        align-items: center;
        justify-content: center;
        border: 1px solid rgba(255, 255, 255, 0.16);
        border-radius: 8px;
        background: rgba(255, 255, 255, 0.08);
        color: var(--text-primary);
        font-size: 1.35rem;
        line-height: 1;
        cursor: pointer;
        pointer-events: auto;
    }

    .sidebar .detail-period-control {
        top: 64px;
        z-index: calc(var(--z-sticky-content) - 1);
        margin-top: 12px;
    }

    .technical-toggle {
        display: none;
    }

    .institutional-modal {
        padding: 0;
    }

    .institutional-dialog {
        width: 100%;
        height: 100dvh;
        border: 0;
        border-radius: 0;
    }

    .institutional-header {
        padding: 11px 12px 9px;
    }

    .institutional-header h2 {
        font-size: 1rem;
    }

    .institutional-tabs {
        padding-inline: 4px;
    }

    .institutional-tabs button {
        min-height: 48px;
        padding: 7px 4px;
        font-size: 0.69rem;
        line-height: 1.25;
    }

    .institutional-panel {
        padding: 12px;
    }

    .institutional-content {
        padding: 12px;
    }

    .ranking-heading {
        gap: 8px;
    }

    .ranking-heading h3,
    .institutional-reading h3 {
        font-size: 1rem;
    }

    .ranking-period {
        padding: 4px 5px;
        font-size: 0.61rem;
    }

    .ranking-search-wrap {
        grid-template-columns: 1fr;
        gap: 4px;
    }

    .ranking-table-wrap {
        max-height: calc(100dvh - 340px);
    }

    .ranking-table {
        font-size: 0.67rem;
    }

    .ranking-table th,
    .ranking-table td {
        padding: 7px 5px;
    }

    .ranking-table thead th:nth-child(1) {
        width: 58px;
    }

    .ranking-table thead th:nth-child(2) {
        width: 34%;
    }

    .ranking-table thead th:nth-child(4) {
        width: 78px;
    }

    .ranking-table thead button {
        min-height: 44px;
        padding: 6px 4px;
        font-size: 0.65rem;
    }

    .trust-principles {
        grid-template-columns: 1fr;
        gap: 14px;
        margin-top: 16px;
    }

    .citation-block {
        margin-top: 14px;
        padding: 13px;
        font-size: 0.78rem;
    }

    .technical-drawer {
        width: var(--mobile-drawer-width);
        padding: 12px 12px 20px;
        overflow-x: hidden;
        overflow-y: auto;
        overscroll-behavior: contain;
        scrollbar-gutter: stable;
    }

    .technical-controls-slot .map-selector-control {
        order: 0;
    }

    .infrastructure-disclosure {
        order: 1;
    }

    .technical-controls-slot .leaflet-control-layers-list label {
        display: flex;
        min-height: 44px;
        align-items: center;
        gap: 9px;
        margin: 0;
        line-height: 1.25;
    }

    .technical-controls-slot .leaflet-control-layers-selector {
        width: 22px;
        height: 22px;
        flex: 0 0 22px;
        margin: 0;
    }

    .map-selector-control select,
    #map-year-slider,
    .period-mode-segments button,
    .detail-period-segments button,
    .territory-level-segments button {
        min-height: 44px;
    }

    .map-selector-control select {
        padding: 10px 8px;
        font-size: 0.82rem;
    }

    .leaflet-top.leaflet-right .leaflet-control-zoom {
        margin-top: 72px;
    }

    .zoom-control-container {
        display: none;
    }

    .legend-panel {
        width: auto;
        min-width: 0;
        max-height: none;
        padding: 0;
        overflow: visible;
        border-radius: 8px;
        font-size: 0.68rem;
    }

    .mobile-legend-toggle {
        display: flex;
        min-width: 126px;
        min-height: 44px;
        align-items: center;
        justify-content: space-between;
        gap: 10px;
        padding: 0 12px;
        border: 0;
        border-radius: 8px;
        background: transparent;
        color: var(--text-primary);
        font: inherit;
        font-weight: 800;
        cursor: pointer;
    }

    .mobile-legend-state {
        color: var(--accent-hover);
        font-size: 0.62rem;
        font-weight: 700;
    }

    .legend-content {
        display: none;
    }

    .legend-panel.mobile-legend-open {
        width: min(280px, calc(100vw - 20px));
        max-height: min(52dvh, 360px);
        overflow: hidden;
    }

    .legend-panel.mobile-legend-open .mobile-legend-toggle {
        width: 100%;
        border-bottom: 1px solid var(--border-glass);
        border-radius: 8px 8px 0 0;
    }

    .legend-panel.mobile-legend-open .legend-content {
        display: block;
        max-height: calc(min(52dvh, 360px) - 44px);
        padding: 10px;
        overflow-x: hidden;
        overflow-y: auto;
        overscroll-behavior: contain;
    }

    .legend-panel.mobile-legend-open .legend-title {
        display: none;
    }

    body.mobile-legend-open .perfil-fallecidos-card,
    body.mobile-sidebar-open .perfil-fallecidos-card,
    body.mobile-layers-open .perfil-fallecidos-card {
        visibility: hidden;
    }

    .perfil-fallecidos-card {
        left: var(--perfil-card-left);
        right: auto;
        width: var(--perfil-card-width);
        max-height: var(--perfil-card-max-height);
    }

    .perfil-card-grid {
        display: block;
    }

    .perfil-card-section {
        padding-right: 0;
        border-right: 0;
        padding-bottom: 12px;
        margin-bottom: 12px;
        border-bottom: 1px solid rgba(255, 255, 255, 0.08);
    }

    .perfil-card-section:last-child {
        border-bottom: 0;
        margin-bottom: 0;
    }

    .perfil-fallecidos-card {
        overflow-x: hidden;
    }

    .profile-card-header {
        flex-wrap: wrap;
        align-items: flex-start;
    }

    #hover-card-title {
        flex: 1 1 100%;
        min-width: 0;
        overflow-wrap: anywhere;
    }

    .profile-card-header-actions {
        display: grid;
        grid-template-columns: minmax(0, 1fr) auto auto;
        width: 100%;
        min-width: 0;
    }

    .profile-period-segments {
        width: auto;
        min-width: 0;
    }

    .profile-card-close {
        width: 44px;
        height: 44px;
    }

    .leaflet-control-attribution {
        max-width: 100vw;
        font-size: 9px;
        line-height: 1.2;
        white-space: normal;
    }
}

@media (max-width: 370px) {
    .citizen-national-badge {
        display: none;
    }

    .mobile-nav-toggle {
        padding-inline: 10px;
    }
}
