/**
 * Mobile Responsive CSS
 * V8.2: Optimized for all screen sizes
 * @version 8.2
 */

/* ==========================================================================
   CSS Variables for Responsive Design
   ========================================================================== */

:root {
    --sidebar-width: 240px;
    --sidebar-collapsed-width: 72px;
    --header-height: 64px;
    --mobile-header-height: 56px;
    --touch-target-min: 44px;
    --spacing-mobile: 12px;
    --spacing-tablet: 16px;
    --spacing-desktop: 24px;
}

/* ==========================================================================
   Mobile First Base Styles
   ========================================================================== */

/* Touch-friendly elements */
.btn,
.nav-item,
.tab-btn,
.form-select,
.form-input {
    min-height: var(--touch-target-min);
}

/* Smooth scrolling */
html {
    scroll-behavior: smooth;
    -webkit-overflow-scrolling: touch;
}

/* Prevent horizontal scroll */
body {
    overflow-x: hidden;
}

/* ==========================================================================
   Small Mobile (< 480px)
   ========================================================================== */

@media screen and (max-width: 479px) {
    :root {
        font-size: 14px;
    }

    .app {
        display: flex;
        flex-direction: column;
    }

    /* Sidebar - Full screen overlay on mobile */
    .sidebar {
        position: fixed;
        top: 0;
        left: -100%;
        width: 100%;
        height: 100vh;
        z-index: 1000;
        transition: left 0.3s ease;
        background: var(--bg-secondary);
    }

    .sidebar.open {
        left: 0;
    }

    .sidebar-header {
        padding: 16px;
    }

    .sidebar-logo h1 {
        font-size: 1.25rem;
    }

    .nav-item {
        padding: 14px 16px;
        font-size: 1rem;
    }

    /* Main content */
    .main {
        margin-left: 0;
        width: 100%;
        min-height: 100vh;
    }

    /* Header - Optimized for mobile with better touch targets */
    .header {
        padding: 12px 16px;
        height: var(--mobile-header-height);
        gap: 12px;
        min-height: 56px;
    }

    .header-left {
        gap: 12px;
        flex: 1;
        min-width: 0;
    }

    .header-title {
        font-size: 1.05rem;
        font-weight: 600;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        flex: 1;
        min-width: 0;
    }

    #mobileMenuToggle {
        display: flex !important;
        min-width: 44px;
        min-height: 44px;
        border-radius: 10px;
        background: rgba(15, 118, 110, 0.1);
        justify-content: center;
        align-items: center;
        transition: background 0.2s ease;
    }

    #mobileMenuToggle:active {
        background: rgba(15, 118, 110, 0.2);
    }

    #monthYearSelect {
        width: 110px;
        font-size: 0.8rem;
        padding: 8px 10px;
        border-radius: 8px;
        background: var(--bg-tertiary);
        border: 1px solid var(--border);
    }

    .header-right {
        gap: 6px;
    }

    .header-actions .btn {
        min-width: 44px;
        min-height: 44px;
        padding: 8px;
        border-radius: 10px;
    }

    /* Content */
    .content {
        padding: var(--spacing-mobile);
    }

    /* Cards */
    .card {
        border-radius: 12px;
        margin-bottom: 12px;
    }

    .card-header {
        padding: 12px 16px;
    }

    .card-body {
        padding: 12px 16px;
    }

    /* Stat Grid - Single column on very small screens */
    .stat-grid {
        grid-template-columns: 1fr;
        gap: 8px;
    }

    .stat-card {
        padding: 16px;
        display: flex;
        align-items: center;
        gap: 14px;
        min-height: 80px;
    }

    .stat-icon {
        font-size: 1.75rem;
        min-width: 48px;
        height: 48px;
        display: flex;
        align-items: center;
        justify-content: center;
        border-radius: 12px;
        flex-shrink: 0;
    }

    .stat-content {
        flex: 1;
        min-width: 0;
    }

    .stat-value {
        font-size: 1.5rem;
        font-weight: 700;
        line-height: 1.2;
        margin-bottom: 4px;
    }

    .stat-label {
        font-size: 0.875rem;
        opacity: 0.85;
        line-height: 1.3;
    }

    /* Tables */
    .table-container {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }

    table {
        min-width: 600px;
    }

    th, td {
        padding: 10px 8px;
        font-size: 0.8rem;
    }

    /* Forms - Optimized for mobile input */
    .form-group {
        margin-bottom: 16px;
    }

    .form-input,
    .form-select,
    .form-textarea {
        font-size: 16px; /* Prevents iOS zoom on focus */
        padding: 14px 16px;
        min-height: 48px;
        border-radius: 10px;
        border-width: 1.5px;
    }
    
    .form-label {
        font-size: 0.9rem;
        margin-bottom: 8px;
        font-weight: 600;
    }

    /* Buttons - Better touch targets */
    .btn {
        padding: 14px 20px;
        font-size: 0.95rem;
        min-height: 48px;
        width: 100%;
        justify-content: center;
        border-radius: 12px;
    }

    .button-group {
        flex-direction: column;
        gap: 12px;
    }
    
    .btn-icon {
        min-width: 48px;
        min-height: 48px;
        width: 48px;
        height: 48px;
    }

    /* Modals */
    .modal-content {
        width: 95%;
        max-height: 90vh;
        margin: 5vh auto;
        border-radius: 16px;
    }

    /* AI Chat Widget */
    .ai-chat-panel {
        width: 100%;
        height: 70vh;
        right: 0;
        bottom: 0;
        border-radius: 16px 16px 0 0;
    }

    .ai-chat-button {
        width: 50px;
        height: 50px;
        right: 16px;
        bottom: 16px;
    }

    /* Filters */
    .dispatch-filters,
    .debt-filters,
    .finance-toolbar {
        flex-direction: column;
        gap: 8px;
    }

    .dispatch-filters .filter-group,
    .debt-filters .filter-group {
        max-width: 100%;
        width: 100%;
    }

    /* Headers with actions */
    .dispatch-header,
    .debt-header,
    .assistant-header {
        flex-direction: column;
        align-items: flex-start;
        gap: 12px;
    }

    .header-actions {
        width: 100%;
        flex-wrap: wrap;
    }

    .header-actions .btn {
        flex: 1;
        min-width: calc(50% - 4px);
    }

    /* Stats grids */
    .debt-stats-grid,
    .dispatch-stats {
        flex-direction: column;
    }

    /* Tabs */
    .debt-tabs,
    .finance-tab {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }

    .debt-tab,
    .finance-tab {
        white-space: nowrap;
        flex-shrink: 0;
    }

    /* Upload area */
    .upload-area {
        padding: 30px 16px;
    }

    .upload-icon {
        font-size: 2.5rem;
    }

    .upload-text {
        font-size: 0.9rem;
    }

    /* Zoom toolbar hidden on mobile */
    .content-zoom-toolbar {
        display: none;
    }
}

/* ==========================================================================
   Large Mobile / Small Tablet (480px - 767px)
   ========================================================================== */

@media screen and (min-width: 480px) and (max-width: 767px) {
    :root {
        font-size: 15px;
    }

    .sidebar {
        position: fixed;
        left: -240px;
        width: 240px;
        height: 100vh;
        z-index: 1000;
        transition: left 0.3s ease;
    }

    .sidebar.open {
        left: 0;
    }

    /* Overlay when sidebar is open */
    .sidebar-overlay {
        position: fixed;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        background: rgba(0, 0, 0, 0.5);
        z-index: 999;
        opacity: 0;
        visibility: hidden;
        transition: opacity 0.3s;
    }

    .sidebar.open ~ .sidebar-overlay {
        opacity: 1;
        visibility: visible;
    }

    .main {
        margin-left: 0;
        width: 100%;
    }

    #mobileMenuToggle {
        display: flex !important;
    }

    .content {
        padding: var(--spacing-mobile);
    }

    .stat-grid {
        grid-template-columns: repeat(2, 1fr);
    }

    .stat-card {
        padding: 14px;
    }

    .button-group {
        flex-wrap: wrap;
    }

    .button-group .btn {
        flex: 1 1 auto;
        min-width: calc(50% - 6px);
    }

    .dispatch-filters,
    .debt-filters {
        flex-wrap: wrap;
    }

    .dispatch-filters .filter-group,
    .debt-filters .filter-group {
        flex: 1 1 calc(50% - 6px);
        max-width: calc(50% - 6px);
    }

    .debt-stats-grid {
        grid-template-columns: repeat(2, 1fr);
    }
}

/* ==========================================================================
   Tablet (768px - 1023px)
   ========================================================================== */

@media screen and (min-width: 768px) and (max-width: 1023px) {
    :root {
        font-size: 15px;
    }

    .sidebar {
        width: var(--sidebar-collapsed-width);
        transition: width 0.3s ease;
    }

    .sidebar:hover,
    .sidebar.expanded {
        width: var(--sidebar-width);
    }

    .sidebar:not(:hover):not(.expanded) .nav-item span:not(.icon),
    .sidebar:not(:hover):not(.expanded) .nav-section-title,
    .sidebar:not(:hover):not(.expanded) .sidebar-logo h1 {
        opacity: 0;
        width: 0;
        overflow: hidden;
    }

    .main {
        margin-left: var(--sidebar-collapsed-width);
        transition: margin-left 0.3s ease;
    }

    .sidebar:hover ~ .main,
    .sidebar.expanded ~ .main {
        margin-left: var(--sidebar-width);
    }

    .content {
        padding: var(--spacing-tablet);
    }

    .stat-grid {
        grid-template-columns: repeat(3, 1fr);
    }

    .debt-stats-grid {
        grid-template-columns: repeat(2, 1fr);
    }

    .dispatch-filters .filter-group {
        flex: 1 1 calc(33% - 8px);
        max-width: calc(33% - 8px);
    }

    #mobileMenuToggle {
        display: none !important;
    }
}

/* ==========================================================================
   Small Desktop (1024px - 1279px)
   ========================================================================== */

@media screen and (min-width: 1024px) and (max-width: 1279px) {
    .sidebar {
        width: var(--sidebar-width);
    }

    .main {
        margin-left: var(--sidebar-width);
    }

    .content {
        padding: var(--spacing-desktop);
    }

    .stat-grid {
        grid-template-columns: repeat(4, 1fr);
    }

    .debt-stats-grid {
        grid-template-columns: repeat(4, 1fr);
    }

    #mobileMenuToggle {
        display: none !important;
    }
}

/* ==========================================================================
   Large Desktop (1280px+)
   ========================================================================== */

@media screen and (min-width: 1280px) {
    .content {
        padding: var(--spacing-desktop);
        max-width: 1800px;
        margin: 0 auto;
    }

    .stat-grid {
        grid-template-columns: repeat(6, 1fr);
    }

    .debt-stats-grid {
        grid-template-columns: repeat(4, 1fr);
    }

    .aging-grid {
        grid-template-columns: repeat(2, 1fr);
    }
}

/* ==========================================================================
   Touch Device Optimizations
   ========================================================================== */

@media (hover: none) and (pointer: coarse) {
    /* Remove hover effects for touch devices */
    .btn:hover,
    .nav-item:hover,
    .card:hover {
        transform: none;
    }

    /* Larger touch targets */
    .btn-sm {
        min-height: 40px;
        padding: 10px 14px;
    }

    /* Scrollbar hidden on touch */
    ::-webkit-scrollbar {
        width: 0;
        height: 0;
    }

    /* Tap highlight */
    * {
        -webkit-tap-highlight-color: rgba(0, 0, 0, 0.1);
    }
}

/* ==========================================================================
   Landscape Mode Adjustments
   ========================================================================== */

@media screen and (max-height: 500px) and (orientation: landscape) {
    .sidebar {
        height: 100vh;
        overflow-y: auto;
    }

    .modal-content {
        max-height: 95vh;
    }

    .ai-chat-panel {
        height: 90vh;
    }

    .stat-grid {
        grid-template-columns: repeat(6, 1fr);
    }

    .stat-card {
        padding: 10px;
    }

    .stat-value {
        font-size: 1rem;
    }
}

/* ==========================================================================
   Print Styles
   ========================================================================== */

@media print {
    .sidebar,
    .ai-chat-widget,
    .content-zoom-toolbar,
    #mobileMenuToggle,
    .btn,
    button {
        display: none !important;
    }

    .main {
        margin-left: 0 !important;
        width: 100% !important;
    }

    .card {
        break-inside: avoid;
        box-shadow: none;
        border: 1px solid #ddd;
    }

    body {
        font-size: 12pt;
        color: #000;
        background: #fff;
    }

    a {
        text-decoration: none;
        color: #000;
    }
}

/* ==========================================================================
   Accessibility - Reduced Motion
   ========================================================================== */

@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
        scroll-behavior: auto !important;
    }
}

/* ==========================================================================
   Dark Mode Responsive Adjustments
   ========================================================================== */

@media (prefers-color-scheme: dark) {
    /* Auto dark mode for system preference */
    :root:not([data-theme="light"]) {
        --bg-primary: #0f172a;
        --bg-secondary: #1e293b;
        --bg-tertiary: #334155;
        --text-primary: #f8fafc;
        --text-secondary: #94a3b8;
        --border: #475569;
    }
}

/* ==========================================================================
   Safe Area Insets (Notched Phones)
   ========================================================================== */

@supports (padding: max(0px)) {
    .header {
        padding-top: max(12px, env(safe-area-inset-top));
    }

    .sidebar {
        padding-left: max(0px, env(safe-area-inset-left));
    }

    .content {
        padding-bottom: max(16px, env(safe-area-inset-bottom));
    }

    .ai-chat-button {
        bottom: max(16px, calc(env(safe-area-inset-bottom) + 16px));
    }
}

/* ==========================================================================
   High Contrast Mode
   ========================================================================== */

@media (forced-colors: active) {
    .btn {
        border: 2px solid currentColor;
    }

    .card {
        border: 1px solid currentColor;
    }

    .nav-item.active {
        border-left: 4px solid currentColor;
    }
}

/* ==========================================================================
   V8.2 Audit: Bottom Navigation Bar for Driver Mobile
   Quick access to key driver views on small screens
   ========================================================================== */

.mobile-bottom-nav {
    display: none;
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    background: var(--bg-secondary, #1e293b);
    border-top: 1px solid var(--border, #475569);
    z-index: 1001;
    padding-bottom: env(safe-area-inset-bottom, 0);
}

.mobile-bottom-nav-inner {
    display: flex;
    justify-content: space-around;
    align-items: center;
    height: 56px;
}

.mobile-bottom-nav-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 4px;
    padding: 8px 12px;
    color: var(--text-secondary, #94a3b8);
    text-decoration: none;
    font-size: 0.7rem;
    font-weight: 500;
    transition: all 0.2s ease;
    cursor: pointer;
    border: none;
    background: none;
    min-height: 60px;
    min-width: 60px;
    border-radius: 12px;
    flex: 1;
    position: relative;
}

.mobile-bottom-nav-item .nav-icon {
    font-size: 1.4rem;
    line-height: 1;
    transition: transform 0.2s ease;
}

.mobile-bottom-nav-item.active {
    color: var(--primary, #0f766e);
    background: rgba(15, 118, 110, 0.1);
}

.mobile-bottom-nav-item:active {
    transform: scale(0.95);
    background: rgba(15, 118, 110, 0.15);
}

.mobile-bottom-nav-item.active .nav-icon {
    transform: translateY(-2px);
}

/* Active indicator dot */
.mobile-bottom-nav-item.active::after {
    content: '';
    position: absolute;
    bottom: 4px;
    width: 4px;
    height: 4px;
    border-radius: 50%;
    background: var(--primary, #0f766e);
}

@media screen and (max-width: 767px) {
    .mobile-bottom-nav {
        display: block;
    }

    /* Push content above bottom nav */
    .content {
        padding-bottom: calc(56px + env(safe-area-inset-bottom, 0) + 16px) !important;
    }

    /* Hide sidebar footer on mobile (bottom nav replaces it) */
    .sidebar-footer {
        display: none;
    }
}

/* ==========================================================================
   V8.2 Audit: Pull-to-refresh indicator
   ========================================================================== */

.pull-to-refresh-indicator {
    display: none;
    position: fixed;
    top: 0;
    left: 50%;
    transform: translateX(-50%);
    padding: 8px 16px;
    background: var(--primary, #0f766e);
    color: white;
    border-radius: 0 0 12px 12px;
    font-size: 0.8rem;
    z-index: 2000;
    opacity: 0;
    transition: opacity 0.3s;
}

.pull-to-refresh-indicator.visible {
    opacity: 1;
}

@media screen and (max-width: 767px) {
    .pull-to-refresh-indicator {
        display: block;
    }
}

/* ==========================================================================
   V8.2 Audit: Finance Table Mobile Card View
   Transform tables to card layout on mobile for better readability
   ========================================================================== */

@media screen and (max-width: 479px) {
    .finance-list .table-responsive {
        overflow-x: visible;
    }

    .finance-mobile-card {
        background: var(--bg-primary);
        border: 1px solid var(--border);
        border-radius: 10px;
        padding: 14px;
        margin-bottom: 8px;
    }

    .finance-mobile-card .card-row {
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 4px 0;
    }

    .finance-mobile-card .card-row-label {
        color: var(--text-secondary);
        font-size: 0.8rem;
    }

    .finance-mobile-card .card-row-value {
        font-weight: 600;
        font-size: 0.9rem;
    }

    .finance-mobile-card .card-row-value.income {
        color: #10b981;
    }

    .finance-mobile-card .card-row-value.expense {
        color: #ef4444;
    }
}
