/* =====================================================
   FLEET TRACKER — MOBILE RESPONSIVE STYLESHEET
   Activates on screens <= 768px.
   Desktop layout (>= 769px) is NOT modified here.
   ===================================================== */

/* =====================================================
   MOBILE BOTTOM NAVIGATION BAR
   Always in DOM; hidden on desktop via media query.
   ===================================================== */
#mob-nav {
    display: none; /* shown only on mobile */
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    height: 60px;
    background: #0b0d10;
    border-top: 1px solid #1f2228;
    z-index: 2000;
    align-items: stretch;
    justify-content: space-around;
}

#mob-nav button {
    flex: 1;
    background: transparent;
    border: none;
    color: #6b7280;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 3px;
    padding: 0;
    cursor: pointer;
    min-height: 0;
    border-radius: 0;
    width: auto;
    transition: color 0.15s;
}

#mob-nav button:hover,
#mob-nav button.mob-active {
    color: #3b82f6;
    background: transparent;
}

.mob-nav-icon {
    font-size: 20px;
    line-height: 1;
}

.mob-nav-label {
    font-size: 9px;
    font-weight: 600;
    letter-spacing: 0.3px;
    text-transform: uppercase;
}

/* =====================================================
   MAP FULLSCREEN TOGGLE BUTTON
   Overlaid on top-right of map on mobile.
   ===================================================== */
#mob-fullscreen-btn {
    display: none; /* shown only on mobile */
    position: absolute;
    top: 8px;
    right: 8px;
    z-index: 450;
    background: rgba(11, 13, 16, 0.85);
    border: 1px solid #2d3748;
    color: #9ca3af;
    width: 36px;
    height: 36px;
    border-radius: 8px;
    cursor: pointer;
    font-size: 16px;
    align-items: center;
    justify-content: center;
    transition: background 0.15s, color 0.15s;
    padding: 0;
    min-height: 0;
    line-height: 1;
}

#mob-fullscreen-btn.mob-fs-active {
    background: rgba(37, 99, 235, 0.75);
    color: #fff;
    border-color: #3b82f6;
}

/* =====================================================
   MOBILE BREAKPOINT — ALL OVERRIDES BELOW 768px
   ===================================================== */
@media (max-width: 768px) {

    /* ----- BOTTOM NAV: activate ----- */
    #mob-nav {
        display: flex;
    }

    #mob-fullscreen-btn {
        display: flex;
    }

    /* ----- VIEWPORT / SCROLL FIX ----- */
    html {
        /* Use dvh where supported; fall back to 100% */
        height: 100%;
    }

    body {
        min-height: 100%;
        /* Allow body scroll to be controlled per-section */
    }

    /* ----- TOP BAR: fixed, full-width ----- */
    #top {
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        z-index: 1000;
        height: 52px;
        padding: 0 12px;
    }

    /* Hide verbose company label to save space */
    #company_label {
        display: none;
    }

    /* Shrink logout button */
    #logout_btn {
        padding: 6px 10px;
        font-size: 11px;
    }

    /* ----- SIDEBAR: hidden on mobile ----- */
    #sidebar {
        display: none !important;
    }

    /* ----- MAIN LAYOUT: fill space between topbar and bottom nav ----- */
    #layout {
        display: block;
        position: fixed;
        top: 52px;
        left: 0;
        right: 0;
        bottom: 60px;
        overflow: hidden;
        height: auto;
        width: auto;
    }

    /* ----- MAIN DASHBOARD: scrollable column ----- */
    #mainDashboard {
        position: absolute;
        inset: 0;
        padding: 0;
        gap: 0;
        overflow-y: auto;
        overflow-x: hidden;
        -webkit-overflow-scrolling: touch;
        display: flex;
        flex-direction: column;
        margin-left: 0 !important; /* override panel-open shift */
    }

    /* ----- STAT CARDS: 3-column compact grid ----- */
    .home-cards {
        display: grid;
        grid-template-columns: repeat(3, 1fr);
        gap: 6px;
        padding: 10px 10px 6px;
        flex-shrink: 0;
        overflow-x: visible;
    }

    .gcard {
        min-height: 60px;
        padding: 7px 6px;
        border-radius: 10px;
        min-width: 0;
    }

    .gicon  { font-size: 13px; }
    .gtitle { font-size: 7px; letter-spacing: 0.4px; }
    .gvalue { font-size: 15px; font-weight: 700; margin-top: 1px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
    .gsub   { font-size: 8px; }

    /* ----- MIDDLE: map + vehicle list stacked ----- */
    .dash-middle {
        display: flex;
        flex-direction: column;
        flex: 1;
        min-height: 0;
        gap: 10px;
        padding: 0 12px 12px;
        overflow: hidden;
    }

    /* Map area fills remaining space */
    .map-area {
        flex: 1;
        min-height: 0;
        padding: 0;
        border-radius: 12px;
        overflow: hidden;
        border: 1px solid #2d3748;
        position: relative; /* for fullscreen button overlay */
    }

    .map-area h2 {
        display: none; /* too much space on mobile */
    }

    .map-area .legend {
        position: absolute;
        bottom: 42px;
        left: 8px;
        background: rgba(11, 13, 16, 0.85);
        padding: 5px 10px;
        border-radius: 8px;
        z-index: 400;
        margin: 0;
        gap: 10px;
    }

    .map-area .last-update {
        position: absolute;
        bottom: 8px;
        left: 8px;
        background: rgba(11, 13, 16, 0.8);
        padding: 3px 8px;
        border-radius: 6px;
        z-index: 400;
        margin: 0;
        font-size: 10px;
    }

    .map-area #map {
        height: 100%;
        min-height: 200px;
        border-radius: 0;
    }

    #map {
        height: 100%;
        position: relative;
    }

    /* Vehicle list: compact scrollable panel */
    .list-area {
        background: #1a1f2e;
        border-radius: 12px;
        max-height: 200px;
        min-height: 80px;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
        padding: 8px;
        flex-shrink: 0;
    }

    /* ----- MAP FULLSCREEN STATE ----- */
    body.mob-map-fs .home-cards {
        display: none;
    }

    body.mob-map-fs .dash-middle {
        padding: 0;
        gap: 0;
    }

    body.mob-map-fs .map-area {
        border-radius: 0;
        border: none;
    }

    body.mob-map-fs .list-area {
        display: none;
    }

    /* ----- SIDE PANELS: fullscreen on mobile ----- */
    /* z-index: 800 puts panels above ALL Leaflet panes (max 700 for popupPane) */
    #page_manage,
    #page_list,
    #page_telemetry,
    #page_history,
    #page_reports {
        position: fixed !important;
        left: 0 !important;
        right: 0 !important;
        top: 52px !important;
        bottom: 60px !important;
        width: 100% !important;
        padding: 16px !important;
        overflow-y: auto !important;
        -webkit-overflow-scrolling: touch !important;
        z-index: 800 !important;
        border-right: none !important;
        background: #0f1115 !important;  /* solid bg prevents map bleeding through */
    }

    /* ----- HIDE MAP when a side panel is open ----- */
    /* display:none fully removes Leaflet from the paint pipeline, preventing
       tiles/markers from bleeding through on mobile browsers. Leaflet recovers
       via map.invalidateSize() when returning to home. */
    #mainDashboard.panel-open {
        display: none !important;
    }

    /* ----- FORMS: single-column on mobile ----- */
    .form-row,
    .form-row-3 {
        grid-template-columns: 1fr;
        gap: 10px;
    }

    .history-row {
        flex-direction: column;
        gap: 10px;
    }

    /* ----- TOUCH-FRIENDLY INPUTS (prevent iOS zoom) ----- */
    .input-dark,
    .input-big {
        height: 48px;
        font-size: 16px;
        padding: 0 14px;
        border-radius: 10px;
    }

    input[type="text"],
    input[type="number"],
    input[type="date"],
    input[type="time"],
    input[type="month"],
    input[type="search"] {
        height: 48px;
        font-size: 16px;
        padding: 0 12px;
        border-radius: 10px;
    }

    /* ----- TOUCH-FRIENDLY BUTTONS (Apple HIG 44px minimum) ----- */
    button {
        min-height: 44px;
    }

    .btn-big {
        height: 48px;
        font-size: 16px;
    }

    .icon-btn {
        width: 44px;
        height: 44px;
    }

    .btn-primary {
        padding: 14px;
        font-size: 15px;
    }

    /* ----- DEBUG GRID ----- */
    .debug-grid {
        grid-template-columns: 1fr 1fr;
        gap: 8px;
    }

    .debug-grid button {
        height: 48px;
        font-size: 13px;
    }

    /* ----- VEHICLE CARDS: better touch targets ----- */
    .vehicle-card,
    .list-card,
    .f-item,
    .fleet-item-card {
        padding: 14px;
    }

    /* ----- CHARTS: prevent overflow ----- */
    #speedChart {
        max-width: 100%;
    }

    /* ----- HISTORY VEHICLE LIST: larger height on mobile ----- */
    #history_vehicle_list {
        max-height: 180px;
    }

    /* ----- HIDE SCROLLBARS on mobile (keep scroll, hide bar) ----- */
    ::-webkit-scrollbar {
        width: 0;
        height: 0;
    }
}

/* =====================================================
   FLEET.HTML MOBILE OVERRIDES
   fleet.html uses inline <style>, so we override here.
   ===================================================== */
@media (max-width: 768px) {

    /* Fleet body: allow scrolling */
    body.fleet-page {
        overflow-y: auto;
        height: auto;
        min-height: 100vh;
    }

    /* Fleet top bar: fixed */
    body.fleet-page #top {
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        z-index: 1000;
    }

    /* Fleet layout: column, below top bar, above bottom nav */
    #fleet_layout {
        flex-direction: column !important;
        overflow-y: auto !important;
        -webkit-overflow-scrolling: touch !important;
        height: auto !important;
        min-height: calc(100vh - 52px - 60px) !important;
        margin-top: 52px; /* offset for fixed top bar */
        padding-bottom: 60px; /* offset for fixed bottom nav */
    }

    /* Fleet sidebar nav: horizontal scrollable pill tabs */
    #fleet_nav {
        width: 100% !important;
        height: auto !important;
        flex-direction: row !important;
        overflow-x: auto !important;
        overflow-y: hidden !important;
        padding: 8px 10px !important;
        gap: 6px !important;
        border-right: none !important;
        border-bottom: 1px solid #1f2228 !important;
        flex-shrink: 0 !important;
        flex-wrap: nowrap !important;
        -webkit-overflow-scrolling: touch !important;
        /* Hide horizontal scrollbar */
        scrollbar-width: none;
    }

    #fleet_nav::-webkit-scrollbar {
        display: none;
    }

    /* Nav buttons: pill-shaped, auto-width */
    .fnav-btn {
        flex-shrink: 0 !important;
        white-space: nowrap !important;
        width: auto !important;
        padding: 8px 16px !important;
        font-size: 12px !important;
        border-radius: 20px !important;
        justify-content: center !important;
        border-left: none !important;
    }

    .fnav-btn.active {
        border-left: none !important;
        padding-left: 16px !important;
        background: #1e3a5f !important;
        border: 1px solid #2563eb !important;
        color: #fff !important;
    }

    .fnav-sep {
        display: none !important;
    }

    /* Fleet content: scrollable */
    #fleet_content {
        flex: 1 !important;
        overflow-y: visible !important;
        min-height: 0 !important;
    }

    /* Fleet sections: normal flow */
    .f-section {
        padding: 14px !important;
        overflow-y: visible !important;
        min-height: 0 !important;
    }

    /* Split layout: single column */
    .f-split {
        grid-template-columns: 1fr !important;
        gap: 14px !important;
        overflow: visible !important;
        height: auto !important;
    }

    .f-col-left,
    .f-col-right {
        overflow: visible !important;
        min-height: 0 !important;
    }

    .f-list {
        max-height: 320px !important;
        overflow-y: auto !important;
        -webkit-overflow-scrolling: touch !important;
    }

    /* Item cards: larger touch targets */
    .f-item {
        padding: 14px !important;
        margin-bottom: 10px !important;
    }

    /* Fleet mini cards: 2-column on mobile */
    .f-mini-cards {
        grid-template-columns: repeat(2, 1fr) !important;
        gap: 8px !important;
    }

    /* Fleet map: shorter on mobile */
    #fleet_map {
        height: 200px !important;
    }

    /* Register / forms grid: single column */
    .f-forms-grid {
        grid-template-columns: 1fr !important;
    }

    /* Form boxes: full width */
    .f-form-box {
        padding: 16px !important;
    }

    /* History 3-col inline grid → single column */
    #fs_history > div {
        grid-template-columns: 1fr !important;
        overflow: visible !important;
        height: auto !important;
    }

    /* Reports 4-col inline grid → single column */
    #fs_reports > div {
        grid-template-columns: 1fr !important;
        overflow: visible !important;
        height: auto !important;
    }
}

/* =====================================================
   ADMIN PAGES MOBILE
   Admin pages use separate templates with full-page
   layouts; just ensure they scroll.
   ===================================================== */
@media (max-width: 768px) {
    /* Admin pages body must scroll */
    body.admin-page {
        overflow-y: auto !important;
        height: auto !important;
    }

    /* Admin table overflow */
    .admin-table,
    table {
        display: block;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        white-space: nowrap;
    }
}

/* =====================================================
   LEAFLET MOBILE ENHANCEMENTS
   ===================================================== */
@media (max-width: 768px) {

    /* Larger zoom control buttons for touch */
    .leaflet-control-zoom a {
        width: 38px !important;
        height: 38px !important;
        line-height: 38px !important;
        font-size: 20px !important;
    }

    /* Smooth pan on touch devices */
    .leaflet-container {
        -webkit-overflow-scrolling: touch;
        touch-action: pan-x pan-y;
    }

    /* Larger, more readable popups */
    .leaflet-popup-content {
        font-size: 14px !important;
        min-width: 180px !important;
        line-height: 1.6 !important;
    }

    .leaflet-popup-content-wrapper {
        border-radius: 12px !important;
        box-shadow: 0 6px 28px rgba(0, 0, 0, 0.6) !important;
    }

    /* Bigger close button */
    .leaflet-popup-close-button {
        width: 28px !important;
        height: 28px !important;
        font-size: 20px !important;
        line-height: 28px !important;
    }

    /* Attribution: smaller on mobile */
    .leaflet-control-attribution {
        font-size: 9px !important;
    }
}

/* =====================================================
   UTILITY: MOBILE-ONLY VISIBILITY
   ===================================================== */
.mob-only {
    display: none;
}

@media (max-width: 768px) {
    .mob-only {
        display: block;
    }
    .desk-only {
        display: none !important;
    }
}
