/**
 * Fleet CRM Apex v3 — Mobile & Tablet Responsive Architecture
 * Full Obsidian Glass Touch & Scroll Engine for Mobile & Desktop
 */

/* ====================================================
   DESKTOP / MOBILE VISIBILITY UTILITIES
   ==================================================== */
.desktop-only {
    display: flex;
}
.mobile-only {
    display: none;
}
.client-col-mobile-compact {
    display: none;
}
.client-col-desktop {
    display: table-cell;
}

/* ====================================================
   TABLETS & MOBILE (<992px) CORE ARCHITECTURE
   ==================================================== */
@media screen and (max-width: 992px) {
    .desktop-only {
        display: none !important;
    }
    .mobile-only {
        display: flex !important;
    }

    /* 1. ROOT VIEWPORT & GESTURE SYSTEM */
    html, body {
        position: fixed !important;
        inset: 0 !important;
        width: 100vw !important;
        height: 100dvh !important;
        max-height: 100dvh !important;
        overflow: hidden !important;
        touch-action: manipulation;
    }

    .apex-shell {
        position: relative !important;
        width: 100vw !important;
        height: 100dvh !important;
        max-height: 100dvh !important;
        display: flex !important;
        overflow: hidden !important;
    }

    /* 2. OFF-CANVAS SIDEBAR ON MOBILE */
    aside.apex-sidebar {
        position: fixed !important;
        top: 0 !important;
        bottom: 0 !important;
        left: -290px !important;
        width: 280px !important;
        max-width: 86vw !important;
        height: 100dvh !important;
        max-height: 100dvh !important;
        display: flex !important;
        flex-direction: column !important;
        justify-content: space-between !important;
        z-index: 99999 !important;
        box-shadow: 10px 0 35px rgba(0, 0, 0, 0.8) !important;
        transition: left var(--transition-smooth) !important;
        overflow: hidden !important;
    }
    aside.apex-sidebar.mobile-open {
        left: 0 !important;
    }

    aside.apex-sidebar .apex-nav-list {
        flex: 1 1 0% !important;
        min-height: 0 !important;
        overflow-y: auto !important;
        overflow-x: hidden !important;
        -webkit-overflow-scrolling: touch !important;
        overscroll-behavior-y: contain !important;
        padding-bottom: 14px !important;
    }

    aside.apex-sidebar .apex-sidebar-user {
        flex-shrink: 0 !important;
        margin-top: auto !important;
        position: sticky !important;
        bottom: 0 !important;
        z-index: 20 !important;
        display: flex !important;
        visibility: visible !important;
        background: rgba(9, 14, 26, 0.98) !important;
        border-top: 1px solid rgba(255, 255, 255, 0.1) !important;
        padding: 12px 14px !important;
        padding-bottom: max(14px, env(safe-area-inset-bottom, 14px)) !important;
    }

    /* Sidebar Mobile Backdrop */
    .apex-sidebar-backdrop {
        position: fixed !important;
        inset: 0 !important;
        background: rgba(0, 0, 0, 0.72) !important;
        backdrop-filter: blur(6px) !important;
        -webkit-backdrop-filter: blur(6px) !important;
        z-index: 140 !important;
        opacity: 0 !important;
        pointer-events: none !important;
        transition: opacity var(--transition-fast) !important;
    }
    .apex-sidebar-backdrop.active {
        opacity: 1 !important;
        pointer-events: auto !important;
    }

    /* 3. MAIN WORKSPACE & SCROLL ENGINE */
    main.apex-main {
        flex: 1 1 0% !important;
        min-width: 0 !important;
        min-height: 0 !important;
        width: 100vw !important;
        height: 100dvh !important;
        max-height: 100dvh !important;
        display: flex !important;
        flex-direction: column !important;
        overflow: hidden !important;
        position: relative !important;
        padding-bottom: 0 !important; /* Critical: No double subtraction */
    }

    header.apex-header {
        height: 56px !important;
        padding: 0 14px !important;
        flex-shrink: 0 !important;
    }

    .apex-header-search {
        width: 180px !important;
    }

    /* UNIFIED DYNAMIC SCROLL CONTAINER */
    #apex-content {
        flex: 1 1 0% !important;
        min-height: 0 !important;
        height: 100% !important;
        max-height: none !important;
        overflow-y: auto !important;
        overflow-x: hidden !important;
        -webkit-overflow-scrolling: touch !important;
        overscroll-behavior-y: contain !important;
        touch-action: pan-y !important;
        padding: 14px 14px calc(76px + env(safe-area-inset-bottom, 14px)) 14px !important;
        gap: 14px !important;
    }

    #apex-content > * {
        flex-shrink: 0 !important;
        min-height: fit-content !important;
        width: 100% !important;
        max-width: 100% !important;
    }

    /* 4. PREVENT ALL NESTED VERTICAL SCROLL TRAPS */
    .apex-table-container,
    .settings-table-container,
    .apex-table-scrollable {
        max-height: none !important;
        overflow-y: visible !important;
        overflow-x: auto !important;
        -webkit-overflow-scrolling: touch !important;
        touch-action: pan-x pan-y !important;
        width: 100% !important;
        max-width: 100% !important;
        border-radius: var(--radius-md) !important;
    }

    /* Standard Tabular Data (Reports, Analytics, Security, Settings, Tasks, Finance) */
    table.apex-table,
    table.tasks-table {
        display: table !important;
        min-width: 850px !important;
        width: 100% !important;
        border-collapse: collapse !important;
    }
    table.apex-table thead,
    table.tasks-table thead {
        display: table-header-group !important;
    }
    table.apex-table tbody,
    table.tasks-table tbody {
        display: table-row-group !important;
    }
    table.apex-table tr,
    table.tasks-table tr {
        display: table-row !important;
        background: transparent !important;
        border: none !important;
        border-bottom: 1px solid rgba(255, 255, 255, 0.06) !important;
        box-shadow: none !important;
        padding: 0 !important;
        margin: 0 !important;
    }
    table.apex-table th,
    table.apex-table td,
    table.tasks-table th,
    table.tasks-table td {
        display: table-cell !important;
        white-space: nowrap !important;
        padding: 10px 14px !important;
        font-size: 12.5px !important;
    }
    table.apex-table td::before,
    table.tasks-table td::before {
        display: none !important;
    }

    /* 5. TABLE-TO-CARD SWITCHER (Clients, Shifts, Finance, Leads, Flags, Balance, Dashboard) */
    table.apex-table-cards {
        display: block !important;
        width: 100% !important;
        border-collapse: separate !important;
        border-spacing: 0 10px !important;
    }
    table.apex-table-cards thead {
        display: none !important;
    }
    table.apex-table-cards,
    table.apex-table-cards tbody,
    table.apex-table-cards tr,
    table.apex-table-cards td {
        display: block !important;
        width: 100% !important;
        min-width: 0 !important;
        white-space: normal !important;
        box-sizing: border-box !important;
    }
    table.apex-table-cards tr {
        background: rgba(13, 19, 34, 0.88) !important;
        border: 1px solid rgba(255, 255, 255, 0.08) !important;
        border-radius: 12px !important;
        margin-bottom: 12px !important;
        padding: 14px 16px !important;
        box-shadow: 0 4px 16px rgba(0, 0, 0, 0.35) !important;
        display: flex !important;
        flex-direction: column !important;
        gap: 8px !important;
        width: 100% !important;
    }
    table.apex-table-cards td {
        display: flex !important;
        align-items: center !important;
        justify-content: space-between !important;
        padding: 4px 0 !important;
        border: none !important;
        font-size: 13px !important;
    }
    table.apex-table-cards td::before {
        content: attr(data-label);
        font-size: 11.5px;
        font-weight: 700;
        color: var(--text-sub);
        text-transform: uppercase;
        letter-spacing: 0.5px;
    }
    table.apex-table-cards td.card-primary-row {
        justify-content: flex-start !important;
        font-size: 15px !important;
        font-weight: 800 !important;
        color: #fff !important;
        border-bottom: 1px solid rgba(255, 255, 255, 0.06) !important;
        padding-bottom: 8px !important;
        margin-bottom: 4px !important;
    }
    table.apex-table-cards td.card-primary-row::before {
        display: none !important;
    }
    table.apex-table-cards td.card-actions-row {
        border-top: 1px solid rgba(255, 255, 255, 0.06) !important;
        padding-top: 10px !important;
        margin-top: 6px !important;
        justify-content: flex-end !important;
        gap: 8px !important;
    }
    table.apex-table-cards td.card-actions-row::before {
        display: none !important;
    }

    /* Compact Driver Format on Mobile (<992px) */
    table.apex-table-cards tr.client-row-compact {
        padding: 9px 12px !important;
        margin-bottom: 7px !important;
        border-radius: 11px !important;
        gap: 0 !important;
        background: rgba(15, 23, 42, 0.88) !important;
        border: 1px solid rgba(255, 255, 255, 0.08) !important;
        box-shadow: 0 2px 10px rgba(0, 0, 0, 0.25) !important;
    }
    table.apex-table-cards tr.client-row-compact:hover {
        border-color: rgba(56, 189, 248, 0.3) !important;
    }
    table.apex-table-cards tr.client-row-compact td.client-col-mobile-compact {
        display: block !important;
        width: 100% !important;
        padding: 0 !important;
    }
    table.apex-table-cards tr.client-row-compact td.client-col-mobile-compact::before {
        display: none !important;
    }
    table.apex-table-cards tr.client-row-compact td.client-col-desktop {
        display: none !important;
    }

    /* 6. SINGLE-COLUMN RESPONSIVE GRIDS */
    .settings-grid,
    .balance-grid,
    .dashboard-split-grid,
    .analytics-charts-grid,
    #tasks-list,
    #parks-list-container,
    #studio-tab-agents,
    .sec-charts-grid,
    #sec-pane-analytics > div:first-child {
        grid-template-columns: 1fr !important;
        display: flex !important;
        flex-direction: column !important;
        gap: 14px !important;
    }

    /* KPI METRIC CARDS (2 columns on mobile, 1 on small screens) */
    .kpi-grid,
    .lead-kpi-grid,
    div[style*="grid-template-columns:repeat(auto-fit, minmax(200px, 1fr))"],
    div[style*="grid-template-columns:repeat(auto-fit, minmax(220px, 1fr))"],
    div[style*="grid-template-columns:repeat(auto-fit, minmax(240px, 1fr))"],
    div[style*="grid-template-columns:repeat(auto-fit, minmax(320px, 1fr))"],
    div[style*="grid-template-columns:repeat(auto-fit, minmax(180px, 1fr))"] {
        grid-template-columns: repeat(2, 1fr) !important;
        gap: 10px !important;
    }

    /* 7. HORIZONTALLY SWIPEABLE TABS & PILL BARS */
    .view-toggle-group,
    .task-kpi-row,
    .flag-kpi-pills-row,
    .flag-filter-card > div:first-child,
    .analytics-tab-btn-row,
    .analytics-wrapper .glass-card > div:first-child,
    .yandex-tab-btn-row,
    .yandex-settings-wrapper > div:nth-child(2),
    .sec-tab-btn-row,
    .ai-studio-container > div:nth-child(3),
    .shift-period-btn-group,
    .shift-filters-container > div:first-child > div:first-child,
    #shift-stats-container,
    #balance-recent-callsigns {
        display: flex !important;
        overflow-x: auto !important;
        overflow-y: hidden !important;
        flex-wrap: nowrap !important;
        -webkit-overflow-scrolling: touch !important;
        touch-action: pan-x pan-y !important;
        scrollbar-width: none !important;
        -ms-overflow-style: none !important;
        padding-bottom: 4px !important;
        gap: 8px !important;
        max-width: 100% !important;
    }

    .view-toggle-group::-webkit-scrollbar,
    .task-kpi-row::-webkit-scrollbar,
    .flag-kpi-pills-row::-webkit-scrollbar,
    .shift-filters-container::-webkit-scrollbar,
    #shift-stats-container::-webkit-scrollbar,
    #balance-recent-callsigns::-webkit-scrollbar {
        display: none !important;
        height: 0 !important;
    }

    .view-toggle-btn,
    .task-kpi-pill,
    .flag-kpi-pill,
    .shift-period-btn,
    .analytics-tab-btn,
    .yandex-tab-btn,
    .apex-tab-btn,
    .filter-pill,
    .sec-filter-pill {
        white-space: nowrap !important;
        flex-shrink: 0 !important;
    }

    /* 8. TOUCH CAROUSEL KANBAN BOARD */
    .kanban-board {
        display: flex !important;
        overflow-x: auto !important;
        overflow-y: hidden !important;
        gap: 12px !important;
        padding-bottom: 14px !important;
        scroll-snap-type: x mandatory !important;
        -webkit-overflow-scrolling: touch !important;
        touch-action: pan-x pan-y !important;
        scrollbar-width: thin !important;
    }
    .kanban-column {
        flex: 0 0 84vw !important;
        max-width: 84vw !important;
        min-width: 270px !important;
        scroll-snap-align: start !important;
    }

    /* 9. BALANCE TERMINAL GESTURE OPTIMIZATION */
    .balance-slider-track {
        touch-action: pan-y !important;
    }
    .balance-slider-thumb {
        touch-action: none !important;
    }

    /* 10. CARDS & WRAPPER ADJUSTMENTS */
    .clients-table-card,
    .shifts-table-card {
        background: transparent !important;
        border: none !important;
        box-shadow: none !important;
        overflow: visible !important;
    }
    .clients-table-card .apex-card-header,
    .shifts-table-card .apex-card-header {
        background: rgba(13, 19, 34, 0.85) !important;
        border: 1px solid var(--border) !important;
        border-radius: var(--radius-md) !important;
        margin-bottom: 12px !important;
        padding: 12px 14px !important;
    }

    .clients-pagination-bar {
        margin-top: 14px !important;
        margin-bottom: 24px !important;
        padding-bottom: 12px !important;
        width: 100% !important;
    }
}

/* ====================================================
   MOBILE BOTTOM NAVIGATION BAR
   ==================================================== */
nav.apex-mobile-bottom-nav {
    display: none;
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    height: 64px;
    padding-bottom: env(safe-area-inset-bottom, 0px);
    background: rgba(9, 14, 26, 0.95);
    backdrop-filter: blur(20px) saturate(180%);
    -webkit-backdrop-filter: blur(20px) saturate(180%);
    border-top: 1px solid rgba(255, 255, 255, 0.08);
    box-shadow: 0 -4px 25px rgba(0, 0, 0, 0.5);
    z-index: 120;
    padding-left: 4px;
    padding-right: 4px;
    box-sizing: content-box;
}

@media screen and (max-width: 992px) {
    nav.apex-mobile-bottom-nav {
        display: grid;
        grid-template-columns: repeat(4, 1fr);
        align-items: center;
    }
}

.apex-bottom-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 64px;
    padding: 7px 2px 5px;
    color: #94a3b8;
    text-decoration: none;
    cursor: pointer;
    position: relative;
    transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
    user-select: none;
    -webkit-tap-highlight-color: transparent;
}

.apex-bottom-item i {
    font-size: 19px;
    margin-bottom: 3px;
    transition: transform 0.2s ease, color 0.2s ease, filter 0.2s ease;
}

.apex-bottom-item span {
    font-size: 11px;
    font-weight: 600;
    letter-spacing: -0.2px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 100%;
    line-height: 1.2;
    transition: color 0.2s ease, font-weight 0.2s ease;
}

.apex-bottom-item:hover {
    color: #e2e8f0;
}
.apex-bottom-item:active {
    transform: scale(0.93);
}

.apex-bottom-item.active {
    color: #38bdf8;
}
.apex-bottom-item.active i {
    color: #38bdf8;
    transform: translateY(-2px) scale(1.08);
    filter: drop-shadow(0 2px 8px rgba(56, 189, 248, 0.55));
}
.apex-bottom-item.active span {
    color: #ffffff;
    font-weight: 800;
}

.apex-bottom-item.active::before {
    content: '';
    position: absolute;
    top: 0;
    left: 50%;
    transform: translateX(-50%);
    width: 28px;
    height: 3px;
    border-radius: 0 0 3px 3px;
    background: linear-gradient(90deg, #38bdf8, #818cf8);
    box-shadow: 0 2px 10px rgba(56, 189, 248, 0.75);
}

.apex-bottom-item.active::after {
    content: '';
    position: absolute;
    inset: 6px 4px;
    background: rgba(56, 189, 248, 0.07);
    border-radius: 10px;
    z-index: -1;
}

.apex-bottom-badge {
    position: absolute;
    top: 5px;
    right: 18%;
    background: #ef4444;
    color: #fff;
    font-size: 10px;
    font-weight: 900;
    min-width: 17px;
    height: 17px;
    padding: 0 4px;
    border-radius: 9px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    line-height: 1;
    box-shadow: 0 2px 6px rgba(239, 68, 68, 0.4);
}

/* ====================================================
   NATIVE BOTTOM SHEET MODALS & DRAWERS (<992px)
   ==================================================== */
@media screen and (max-width: 992px) {
    .apex-drawer-overlay {
        z-index: 290 !important;
    }

    .apex-drawer {
        width: 100vw !important;
        height: 92vh !important;
        height: 92dvh !important;
        max-height: 92vh !important;
        max-height: 92dvh !important;
        top: auto !important;
        bottom: -100vh !important;
        right: 0 !important;
        border-radius: 20px 20px 0 0 !important;
        border-left: none !important;
        border-top: 1px solid var(--border-highlight) !important;
        transition: bottom var(--transition-smooth) !important;
        z-index: 300 !important;
        display: flex !important;
        flex-direction: column !important;
        overflow: hidden !important;
        box-shadow: 0 -10px 40px rgba(0, 0, 0, 0.8) !important;
    }
    .apex-drawer.active {
        bottom: 0 !important;
        right: 0 !important;
    }

    .apex-drawer-header {
        position: relative !important;
        padding: 14px 20px 10px 20px !important;
        padding-top: 22px !important;
        flex-shrink: 0 !important;
    }
    .apex-drawer-header::before {
        content: '' !important;
        position: absolute !important;
        top: 8px !important;
        left: 50% !important;
        transform: translateX(-50%) !important;
        width: 42px !important;
        height: 4px !important;
        border-radius: 4px !important;
        background: rgba(255, 255, 255, 0.3) !important;
    }

    .apex-drawer-body {
        flex: 1 1 0% !important;
        min-height: 0 !important;
        height: 100% !important;
        overflow-y: auto !important;
        overflow-x: hidden !important;
        -webkit-overflow-scrolling: touch !important;
        overscroll-behavior-y: contain !important;
        touch-action: pan-y !important;
        padding: 12px 16px 45px 16px !important;
        gap: 10px !important;
    }

    /* Standard Modals -> Bottom Sheets */
    .apex-modal-overlay,
    .settings-modal-overlay {
        padding: 0 !important;
        align-items: flex-end !important;
        justify-content: flex-end !important;
    }

    .apex-modal-card,
    .apex-pop-modal,
    .settings-modal-overlay > div,
    .lead-filter-popover {
        width: 100vw !important;
        max-width: 100vw !important;
        margin: 0 !important;
        border-radius: 20px 20px 0 0 !important;
        border-bottom: none !important;
        max-height: 92dvh !important;
        z-index: 310 !important;
        box-shadow: 0 -10px 40px rgba(0, 0, 0, 0.8) !important;
    }

    .apex-pop-modal,
    .apex-modal-card {
        position: relative !important;
        padding-top: 14px !important;
    }
    .apex-pop-modal::before,
    .apex-modal-card::before {
        content: '' !important;
        position: absolute !important;
        top: 8px !important;
        left: 50% !important;
        transform: translateX(-50%) !important;
        width: 40px !important;
        height: 4px !important;
        border-radius: 4px !important;
        background: rgba(255, 255, 255, 0.3) !important;
    }

    /* Shift WebRTC Live Camera Viewport */
    .shift-camera-viewport {
        width: 100% !important;
        height: clamp(300px, 46dvh, 500px) !important;
        min-height: 260px !important;
        max-height: 520px !important;
        border-radius: 16px !important;
    }
}

/* ====================================================
   SMARTPHONES (<768px) HEADERS & TOOLBARS
   ==================================================== */
@media screen and (max-width: 768px) {
    /* Header Stacking */
    .settings-header,
    .clients-header,
    .shifts-header,
    .finance-header,
    .leads-wrapper > div:first-child,
    .analytics-wrapper > div:first-child,
    .security-wrapper > div:first-child,
    .yandex-settings-wrapper > div:first-child,
    .tasks-wrapper > div:first-child,
    .flags-page-wrapper > div:first-child,
    .ai-studio-header,
    .dashboard-wrapper > div:nth-child(2) {
        flex-direction: column !important;
        align-items: stretch !important;
        gap: 12px !important;
    }

    .settings-header > div:last-child,
    .clients-header > div:last-child,
    .shifts-header > div:last-child,
    .finance-header > div:last-child,
    .leads-wrapper > div:first-child > div:last-child,
    .analytics-wrapper > div:first-child > div:last-child,
    .security-wrapper > div:first-child > div:last-child,
    .yandex-settings-wrapper > div:first-child > div:last-child,
    .tasks-wrapper > div:first-child > div:last-child,
    .ai-studio-header > div:last-child {
        display: flex !important;
        width: 100% !important;
        gap: 8px !important;
        flex-wrap: wrap !important;
    }

    .settings-header > div:last-child > button,
    .clients-header > div:last-child > button,
    .shifts-header > div:last-child > button,
    .finance-header > div:last-child > button {
        flex: 1 1 auto !important;
        justify-content: center !important;
        padding: 8px 12px !important;
        font-size: 12px !important;
    }

    /* Filter Toolbars */
    .task-filter-bar,
    .clients-filter-bar,
    .finance-filters-container,
    .shift-filters-container,
    .flag-filter-card {
        flex-direction: column !important;
        align-items: stretch !important;
        gap: 10px !important;
        padding: 12px 14px !important;
    }

    .task-filter-input,
    .clients-search-box,
    .clients-service-select,
    .apex-pop-input,
    #filter-lead-search,
    #balance-search-input,
    #balance-amount-input {
        width: 100% !important;
        max-width: 100% !important;
        min-width: 0 !important;
        box-sizing: border-box !important;
    }

    .finance-filters-container > div:first-child,
    .finance-filters-container > div:last-child {
        flex-direction: column !important;
        align-items: stretch !important;
        gap: 10px !important;
    }

    .fin-date-range-box {
        width: 100% !important;
        justify-content: space-between !important;
    }
    .fin-date-range-box input[type="date"] {
        flex: 1 1 auto !important;
        min-width: 100px !important;
    }
}

/* ====================================================
   COMPACT SCREENS (<480px) FINE-TUNING
   ==================================================== */
@media screen and (max-width: 480px) {
    #apex-content {
        padding: 10px 10px calc(76px + env(safe-area-inset-bottom, 14px)) 10px !important;
        gap: 10px !important;
    }

    /* Single column KPI cards on very small phones */
    .kpi-grid,
    div[style*="grid-template-columns:repeat(auto-fit, minmax(200px, 1fr))"],
    div[style*="grid-template-columns:repeat(auto-fit, minmax(220px, 1fr))"],
    div[style*="grid-template-columns:repeat(auto-fit, minmax(240px, 1fr))"],
    div[style*="grid-template-columns:repeat(auto-fit, minmax(320px, 1fr))"],
    div[style*="grid-template-columns:repeat(auto-fit, minmax(180px, 1fr))"] {
        grid-template-columns: 1fr !important;
    }

    .apex-bottom-item span {
        font-size: 10px !important;
    }
    .apex-bottom-item i {
        font-size: 18px !important;
    }

    /* Calendar cell compact layout */
    #modal-calendar-grid, 
    #modal-operator-calendar div[style*="repeat(7, 1fr)"] {
        gap: 3px !important;
    }
    .modal-cal-cell {
        padding: 4px 1px !important;
        min-height: 48px !important;
        border-radius: 5px !important;
    }
    .modal-cal-cell .cal-day-num {
        font-size: 12px !important;
    }
    .modal-cal-cell .cal-status-text {
        font-size: 7.5px !important;
        letter-spacing: 0 !important;
    }
    .modal-cal-cell .cal-time-text {
        font-size: 8.5px !important;
    }
}
