/* ==========================================================================
   Mpox-237CM — Responsive / Mobile enhancements
   Loaded AFTER styles.css and each page's inline <style>, so it can safely
   override them. Breakpoints:
     ≤ 1024px  tablet landscape / small laptops
     ≤  768px  tablets portrait & phones  (sidebar becomes a drawer)
     ≤  480px  small phones
   ========================================================================== */

/* ---------- Global safety nets (all sizes) ------------------------------- */
html {
    -webkit-text-size-adjust: 100%;
    text-size-adjust: 100%;
    -webkit-tap-highlight-color: transparent;
}

img, svg, video, canvas {
    max-width: 100%;
}

/* A flex child with wide content (tables, canvases) must be allowed to shrink,
   otherwise the whole page gets a sideways scrollbar. */
.main-content {
    min-width: 0;
    max-width: 100%;
}

/* Hamburger + overlay are injected by js/common.js; hidden on desktop. */
.menu-toggle,
.sidebar-overlay,
.sidebar-close {
    display: none;
}

/* Scroll wrapper added around every table by common.js */
.table-scroll {
    width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    margin-bottom: 20px;
}
.table-scroll > table,
.table-scroll > .dataTables_wrapper {
    margin-bottom: 0;
}
.dataTables_wrapper {
    max-width: 100%;
}
.dataTables_wrapper .dataTables_scroll,
.dataTables_wrapper .dataTables_scrollBody {
    overflow-x: auto !important;
    -webkit-overflow-scrolling: touch;
}

/* Long unbroken strings (emails, URLs, place names) never push layout wide */
.card, .stat-card, .news-item, .media-house-card, .alert-level {
    overflow-wrap: anywhere;
    word-break: normal;
}

/* Keyboard users get a visible focus ring */
.menu-item:focus-visible,
.menu-toggle:focus-visible,
.sidebar-close:focus-visible,
button:focus-visible,
a:focus-visible {
    outline: 3px solid #69f0ae;
    outline-offset: 2px;
}

/* Touch devices: no sticky hover "lift" effects */
@media (hover: none) {
    .card:hover,
    .source-card:hover,
    .season-card:hover,
    .year-btn:hover,
    .refresh-btn:hover {
        transform: none;
    }
}

/* ---------- ≤ 1024px: tablets / small laptops ---------------------------- */
@media (max-width: 1024px) {
    :root { --sidebar-width: 210px; }

    .main-content { padding: 16px; }
    .page-title   { font-size: 1.5rem; }
    .map-container { height: 440px; }
}

/* ---------- ≤ 768px: phones & portrait tablets --------------------------- */
@media (max-width: 768px) {

    /* ----- Off-canvas navigation drawer ----- */
    body {
        display: block;               /* stop flex from squeezing the page */
        overflow-x: hidden;
    }

    .sidebar {
        width: min(280px, 82vw);
        transform: translateX(-100%);
        visibility: hidden;           /* keeps hidden links out of tab order */
        transition: transform .3s ease, visibility 0s linear .3s;
        z-index: 1001;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
        overscroll-behavior: contain;
        padding-bottom: env(safe-area-inset-bottom, 0);
        box-shadow: none;
    }
    .sidebar.active {
        transform: translateX(0);
        visibility: visible;
        transition: transform .3s ease, visibility 0s;
        box-shadow: 4px 0 24px rgba(0, 0, 0, .35);
    }

    .sidebar-header {
        padding: 16px 16px 16px 20px;
        position: sticky;
        top: 0;
        z-index: 1;
        background: #6a1b9a;
    }
    .sidebar-header .app-name { flex: 1; }

    .sidebar-close {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        width: 44px;
        height: 44px;
        margin-left: auto;
        border: 0;
        border-radius: 50%;
        background: rgba(255, 255, 255, .12);
        color: #fff;
        cursor: pointer;
    }
    .sidebar-close:active { background: rgba(255, 255, 255, .25); }

    .sidebar-menu { padding: 8px 0 24px; }
    .menu-item {
        min-height: 48px;             /* comfortable tap target */
        padding: 12px 20px;
        font-size: 1rem;
    }

    .sidebar-overlay {
        display: block;
        position: fixed;
        inset: 0;
        background: rgba(0, 0, 0, .5);
        opacity: 0;
        pointer-events: none;
        transition: opacity .3s ease;
        z-index: 1000;
        -webkit-backdrop-filter: blur(1px);
        backdrop-filter: blur(1px);
    }
    .sidebar-overlay.active {
        opacity: 1;
        pointer-events: auto;
    }
    body.nav-open { overflow: hidden; }   /* lock background scroll */

    /* ----- Main area & sticky top bar ----- */
    .main-content {
        margin-left: 0;
        width: 100%;
        padding: 0 12px 24px;
        padding-left:  max(12px, env(safe-area-inset-left));
        padding-right: max(12px, env(safe-area-inset-right));
        padding-bottom: max(24px, env(safe-area-inset-bottom));
    }

    .header {
        position: sticky;
        top: 0;
        z-index: 900;
        gap: 10px;
        margin: 0 -12px 16px;
        padding: 10px 12px;
        padding-top: max(10px, env(safe-area-inset-top));
        background: #fafafa;
        border-bottom: 1px solid #e0e0e0;
        box-shadow: 0 2px 6px rgba(0, 0, 0, .06);
    }

    .menu-toggle {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        flex: 0 0 auto;
        width: 44px;
        height: 44px;
        border: 0;
        border-radius: 12px;
        background: var(--primary-color);
        color: #fff;
        cursor: pointer;
        box-shadow: 0 2px 6px rgba(123, 31, 162, .35);
    }
    .menu-toggle:active { background: #4a148c; }
    .menu-toggle .material-icons { font-size: 26px; }

    .page-title {
        flex: 1;
        min-width: 0;
        font-size: 1.25rem;
        line-height: 1.2;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }

    .user-profile > span { display: none; }   /* keep avatar only */
    .user-avatar {
        margin-left: 0;
        width: 36px;
        height: 36px;
    }

    /* ----- Typography ----- */
    .hero h1 { font-size: 1.35rem; line-height: 1.25; }
    .hero p  { font-size: .95rem; line-height: 1.55; }
    .card-title { font-size: 1rem; }

    /* ----- Cards & grids ----- */
    .card { padding: 14px; border-radius: 10px; margin-bottom: 14px; }

    .stats-grid {
        grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
        gap: 12px;
        margin-bottom: 20px;
    }
    .stat-card { padding: 14px; }
    .stat-icon {
        width: 42px; height: 42px;
        margin-right: 12px;
        font-size: 1.25rem;
        flex-shrink: 0;
    }
    .stat-info h3 { font-size: 1.4rem; }
    .stat-info p  { font-size: .8rem; }

    /* Page-specific KPI strips: two per row */
    .hotspot-stats-strip,
    .analytics-stats-strip,
    .pred-stats-strip,
    .sentiment-summary-strip,
    .vacc-stats-strip,
    .alert-summary-grid,
    .news-stats-strip {
        grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
        gap: 10px !important;
    }
    [class*="stats-strip"] > *,
    [class*="alert-count"] > * { min-width: 0; }

    .sources-grid {
        grid-template-columns: repeat(2, minmax(0, 1fr));
        gap: 10px;
    }
    .source-card { padding: 14px 10px; }
    .source-card i { font-size: 2em; }
    .source-card .count { font-size: 1.4em; }

    .season-info-panel { padding: 16px; }
    .season-info-panel h3 { font-size: 1.2em; margin-bottom: 14px; }
    .season-cards { grid-template-columns: 1fr; gap: 14px; }
    .current-season-badge { margin: 6px 0 0 0; }

    .region-cards-grid {
        grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
    }

    /* Overview banner on dashboard: 2-column grid instead of a tall column */
    .overview-banner {
        display: grid;
        grid-template-columns: repeat(2, minmax(0, 1fr));
        gap: 14px;
        padding: 16px;
        justify-content: stretch;
    }
    .overview-stat { min-width: 0; }
    .overview-stat .value { font-size: 1.6em; }
    .overview-stat .label { font-size: .75em; }

    /* ----- Charts ----- */
    .chart-container {
        height: 260px;
        margin-bottom: 18px;
    }
    .timeline-container {
        height: 300px;
        margin-bottom: 18px;
    }
    #wordCloudCanvas { height: 280px !important; }

    /* ----- Map ----- */
    .map-container {
        height: 62vh;
        min-height: 320px;
        max-height: 520px;
        margin-bottom: 14px;
    }
    .leaflet-control-zoom a { width: 36px; height: 36px; line-height: 36px; }
    .info.legend { font-size: 12px; padding: 8px !important; max-width: 70vw; }

    /* ----- Filter tabs / pills: swipeable single row ----- */
    .year-filter-bar,
    .year-tabs,
    .cat-tabs {
        flex-wrap: nowrap;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        scrollbar-width: none;
        gap: 8px;
        margin-left: -2px;
        padding: 2px 2px 8px;
    }
    .year-filter-bar::-webkit-scrollbar,
    .year-tabs::-webkit-scrollbar,
    .cat-tabs::-webkit-scrollbar { display: none; }
    .year-tab, .cat-tab {
        flex: 0 0 auto;
        min-height: 40px;
        white-space: nowrap;
    }
    .year-btn { min-height: 44px; }

    .refresh-btn { margin-left: 0; min-height: 40px; }

    /* ----- Tables ----- */
    .data-table th, .data-table td,
    .outbreak-table th, .outbreak-table td {
        padding: 10px 10px;
        font-size: .85rem;
        white-space: nowrap;
    }

    /* DataTables controls: stack and centre */
    .dataTables_wrapper .dataTables_length,
    .dataTables_wrapper .dataTables_filter,
    .dataTables_wrapper .dataTables_info,
    .dataTables_wrapper .dataTables_paginate,
    .dt-buttons {
        float: none !important;
        display: block;
        text-align: center !important;
        margin: 8px 0 !important;
    }
    .dataTables_wrapper .dataTables_filter input {
        width: 100%;
        max-width: 100%;
        margin: 6px 0 0 !important;
        display: block;
    }
    .dataTables_wrapper .dataTables_length select { min-height: 40px; }
    .dataTables_wrapper .dataTables_paginate .paginate_button {
        min-width: 40px;
        min-height: 40px;
        padding: .5em .8em !important;
    }
    .dt-buttons { display: flex !important; flex-wrap: wrap; justify-content: center; gap: 6px; }
    .dt-buttons .dt-button { margin: 0 !important; min-height: 38px; }

    /* ----- Alerts / news / media cards ----- */
    .alert-level { padding: 12px 14px; gap: 10px; }
    .alert-icon  { font-size: 1.5em !important; }
    .news-item   { padding: 12px; }
    .news-item h3 { font-size: 1rem; }

    .media-house-card {
        flex-direction: column;
        align-items: flex-start;
        gap: 10px;
    }
    .media-house-stats { text-align: left; }

    /* ----- Forms (admin.html) ----- */
    .form-container { padding: 14px; }
    .form-row {
        flex-direction: column;
        gap: 0;
        margin-bottom: 0;
    }
    .form-field { margin-bottom: 15px; }
    .form-actions { justify-content: stretch; }
    .form-actions .btn-submit,
    .btn-submit {
        width: 100%;
        justify-content: center;
        min-height: 46px;
    }

    .table-selector {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        scrollbar-width: none;
    }
    .table-selector::-webkit-scrollbar { display: none; }
    .table-option {
        flex: 0 0 auto;
        min-width: 120px;
        padding: 12px;
    }

    /* 16px+ font prevents iOS Safari from zooming into inputs on focus */
    input, select, textarea,
    .dataTables_wrapper .dataTables_filter input,
    .dataTables_wrapper .dataTables_length select {
        font-size: 16px !important;
        min-height: 44px;
    }
    textarea { min-height: 100px; }

    .btn { min-height: 44px; }
}

/* ---------- ≤ 480px: small phones ---------------------------------------- */
@media (max-width: 480px) {
    .main-content { padding-left: max(10px, env(safe-area-inset-left)); padding-right: max(10px, env(safe-area-inset-right)); }
    .header { margin-left: -10px; margin-right: -10px; padding-left: 10px; padding-right: 10px; }

    .page-title { font-size: 1.1rem; }
    .hero h1    { font-size: 1.2rem; }

    /* Dashboard cards: one per row so numbers never get cramped */
    .stats-grid { grid-template-columns: 1fr; }

    .chart-container    { height: 240px; }
    .timeline-container { height: 270px; }
    .map-container      { height: 55vh; min-height: 300px; }

    .overview-stat .value { font-size: 1.4em; }

    .hstat .hval, .astat .aval, .pstat .pval,
    .ssum-card .sval, .alert-count-card .aval { font-size: 1.35em; }
    .hstat .hlbl, .astat .albl, .pstat .plbl,
    .ssum-card .slbl, .alert-count-card .albl { font-size: .72em; }

    .vacc-stat-card { padding: 12px; gap: 10px; }
    .vacc-stat-icon { width: 38px; height: 38px; }
    .vacc-stat-value { font-size: 1.1rem; }

    .region-cards-grid { grid-template-columns: 1fr; }
    .sources-grid { gap: 8px; }

    .alert-meta { gap: 6px; }
}

/* ---------- Very small phones (≤ 340px) ---------------------------------- */
@media (max-width: 340px) {
    .sources-grid,
    .overview-banner { grid-template-columns: 1fr; }
}

/* ---------- Landscape phones: avoid giant map / charts ------------------- */
@media (max-width: 900px) and (orientation: landscape) and (max-height: 500px) {
    .map-container { height: 80vh; }
    .chart-container, .timeline-container { height: 70vh; }
    .sidebar-menu { padding-bottom: 8px; }
}

/* ---------- Respect reduced-motion preference ---------------------------- */
@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after {
        animation-duration: .01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: .01ms !important;
        scroll-behavior: auto !important;
    }
}

/* ---------- Printing: hide chrome, show content -------------------------- */
@media print {
    .sidebar, .sidebar-overlay, .menu-toggle { display: none !important; }
    .main-content { margin-left: 0 !important; padding: 0 !important; }
    .header { position: static; box-shadow: none; }
    .card { box-shadow: none; break-inside: avoid; }
}
