/**
 * Mobile Optimizations for iLearnZed Platform
 * Comprehensive mobile-friendly styles and performance optimizations
 */

/* ============================================
   FORCE MOBILE VIEW ON MOBILE DEVICES
   ============================================ */

/* Apply mobile styles when is-mobile-device class is present */
html.is-mobile-device {
    font-size: 16px !important;
}

html.is-mobile-device body {
    font-size: 16px !important;
}

/* Prevent desktop view on mobile */
@media (max-width: 768px) {
    html {
        font-size: 16px !important;
        -webkit-text-size-adjust: 100% !important;
        -ms-text-size-adjust: 100% !important;
        overflow-x: hidden !important;
        max-width: 100vw !important;
    }
    
    body {
        width: 100vw !important;
        max-width: 100vw !important;
        overflow-x: hidden !important;
        position: relative !important;
    }
    
    /* Force mobile container widths */
    .container,
    .container-fluid,
    .container-lg,
    .container-md,
    .container-sm {
        width: 100% !important;
        max-width: 100% !important;
        padding-left: 1rem !important;
        padding-right: 1rem !important;
    }
    
}

/* Additional mobile styles for is-mobile-device class */
.is-mobile-device,
.is-mobile-device body {
    width: 100vw !important;
    max-width: 100vw !important;
    overflow-x: hidden !important;
}

/* ============================================
   PERFORMANCE OPTIMIZATIONS
   ============================================ */

/* Reduce repaints and reflows on mobile */
* {
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    text-rendering: optimizeLegibility;
}

/* Optimize scrolling on mobile */
html {
    -webkit-overflow-scrolling: touch;
    scroll-behavior: smooth;
}

body {
    -webkit-text-size-adjust: 100%;
    -webkit-tap-highlight-color: rgba(0, 166, 81, 0.1);
    touch-action: manipulation;
}

/* ============================================
   TOUCH TARGETS
   ============================================ */

/* Ensure all interactive elements meet minimum touch target size (44x44px) */
button,
.btn,
a.btn,
input[type="button"],
input[type="submit"],
input[type="reset"],
.nav-link,
.dropdown-item,
.pagination .page-link,
.form-check-input,
.form-check-label {
    min-height: 44px;
    min-width: 44px;
    touch-action: manipulation;
}

/* Larger touch targets on mobile */
@media (max-width: 768px) {
    button,
    .btn,
    a.btn {
        min-height: 48px;
        padding: 0.75rem 1.5rem;
    }
    
    .btn-sm {
        min-height: 44px;
        padding: 0.5rem 1rem;
    }
    
    .btn-icon-only {
        min-width: 48px;
        min-height: 48px;
    }
}

/* ============================================
   FORM OPTIMIZATIONS
   ============================================ */

/* Prevent zoom on iOS when focusing inputs */
@media (max-width: 768px) {
    input[type="text"],
    input[type="email"],
    input[type="password"],
    input[type="url"],
    input[type="number"],
    input[type="tel"],
    input[type="search"],
    textarea,
    select {
        font-size: 16px !important; /* Prevents iOS zoom */
        min-height: 44px;
        padding: 0.75rem;
        border-radius: 8px;
        touch-action: manipulation;
    }
    
    /* Better spacing for form groups */
    .form-group,
    .form-field-wrapper {
        margin-bottom: 1.25rem;
    }
    
    /* Full-width inputs on mobile */
    .form-control,
    .form-select {
        width: 100%;
    }
    
    /* Stack form buttons */
    .btn-group {
        flex-direction: column;
        width: 100%;
    }
    
    .btn-group .btn {
        width: 100%;
        margin-bottom: 0.5rem;
        border-radius: 8px !important;
    }
}

/* ============================================
   LAYOUT OPTIMIZATIONS
   ============================================ */

@media (max-width: 768px) {
    /* Full-width containers */
    .container,
    .container-fluid {
        padding-left: 0.75rem;
        padding-right: 0.75rem;
    }
    
    /* Stack columns */
    .row > * {
        margin-bottom: 1rem;
    }
    
    /* Reduce padding */
    .card {
        margin-bottom: 1rem;
        border-radius: 12px;
    }
    
    .card-header {
        padding: 0.75rem;
        font-size: 1rem;
    }
    
    .card-body {
        padding: 0.75rem;
    }
    
    /* Optimize typography */
    h1 { font-size: 1.75rem; }
    h2 { font-size: 1.5rem; }
    h3 { font-size: 1.25rem; }
    h4 { font-size: 1.125rem; }
    h5 { font-size: 1rem; }
    h6 { font-size: 0.875rem; }
    
    /* Reduce margins */
    .mb-4 { margin-bottom: 1rem !important; }
    .mb-5 { margin-bottom: 1.5rem !important; }
    .mt-4 { margin-top: 1rem !important; }
    .mt-5 { margin-top: 1.5rem !important; }
}

/* ============================================
   IMAGE OPTIMIZATIONS
   ============================================ */

img {
    max-width: 100%;
    height: auto;
    display: block;
}

@media (max-width: 768px) {
    img {
        width: 100%;
        object-fit: cover;
    }
    
    /* Lazy loading for images */
    img[loading="lazy"] {
        opacity: 0;
        transition: opacity 0.3s;
    }
    
    img[loading="lazy"].loaded {
        opacity: 1;
    }
}

/* ============================================
   TABLE OPTIMIZATIONS
   ============================================ */

@media (max-width: 768px) {
    .table-responsive {
        -webkit-overflow-scrolling: touch;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }
    
    table {
        font-size: 0.875rem;
        min-width: 600px; /* Prevent squishing */
    }
    
    /* Stack table cells on very small screens */
    @media (max-width: 480px) {
        .table-mobile-stack {
            display: block;
        }
        
        .table-mobile-stack thead {
            display: none;
        }
        
        .table-mobile-stack tbody,
        .table-mobile-stack tr,
        .table-mobile-stack td {
            display: block;
            width: 100%;
        }
        
        .table-mobile-stack td {
            text-align: left;
            padding: 0.5rem;
            border-bottom: 1px solid #dee2e6;
        }
        
        .table-mobile-stack td:before {
            content: attr(data-label) ": ";
            font-weight: bold;
            display: inline-block;
            width: 40%;
        }
    }
}

/* ============================================
   NAVIGATION OPTIMIZATIONS
   ============================================ */

@media (max-width: 768px) {
    .navbar {
        padding: 0.5rem 0;
    }
    
    .navbar-nav {
        padding: 0.5rem 0;
    }
    
    .navbar-nav .nav-link {
        padding: 0.75rem 1rem;
        border-bottom: 1px solid rgba(0, 0, 0, 0.05);
    }
    
    .dropdown-menu {
        width: 100%;
        border: none;
        box-shadow: none;
        border-radius: 0;
    }
}

/* ============================================
   MODAL OPTIMIZATIONS
   ============================================ */

@media (max-width: 768px) {
    .modal-dialog {
        margin: 0.5rem;
        max-width: calc(100% - 1rem);
    }
    
    .modal-content {
        border-radius: 12px;
    }
    
    .modal-header,
    .modal-body,
    .modal-footer {
        padding: 1rem;
    }
    
    .modal-title {
        font-size: 1.25rem;
    }
}

/* ============================================
   ANIMATION OPTIMIZATIONS
   ============================================ */

/* Reduce animations on mobile for better performance */
@media (max-width: 768px) {
    *,
    *::before,
    *::after {
        animation-duration: 0.2s !important;
        transition-duration: 0.2s !important;
    }
    
    /* Disable hover effects on touch devices */
    @media (hover: none) {
        .btn:hover,
        .card:hover,
        .nav-link:hover {
            transform: none !important;
            box-shadow: none !important;
        }
    }
}

/* ============================================
   VIDEO OPTIMIZATIONS
   ============================================ */

@media (max-width: 768px) {
    .video-container {
        margin: 0 -0.75rem;
        border-radius: 0;
    }
    
    .video-container iframe,
    .video-container video {
        width: 100%;
        height: auto;
        min-height: 200px;
        border-radius: 0;
    }
}

/* ============================================
   UTILITY CLASSES
   ============================================ */

/* Hide on mobile */
@media (max-width: 768px) {
    .hide-mobile {
        display: none !important;
    }
}

/* Show only on mobile */
.show-mobile {
    display: none !important;
}

@media (max-width: 768px) {
    .show-mobile {
        display: block !important;
    }
}

/* Full width on mobile */
@media (max-width: 768px) {
    .full-width-mobile {
        width: 100% !important;
        margin-left: 0 !important;
        margin-right: 0 !important;
    }
}

/* ============================================
   LOADING OPTIMIZATIONS
   ============================================ */

/* Skeleton loading for better perceived performance */
.skeleton {
    background: linear-gradient(90deg, #f0f0f0 25%, #e0e0e0 50%, #f0f0f0 75%);
    background-size: 200% 100%;
    animation: loading 1.5s infinite;
}

@keyframes loading {
    0% { background-position: 200% 0; }
    100% { background-position: -200% 0; }
}

/* ============================================
   ACCESSIBILITY
   ============================================ */

/* Ensure focus indicators are visible on mobile */
@media (max-width: 768px) {
    *:focus {
        outline: 2px solid var(--zambia-blue);
        outline-offset: 2px;
    }
    
    /* Larger focus areas for touch */
    button:focus,
    .btn:focus,
    a:focus {
        outline-width: 3px;
    }
}

/* ============================================
   PERFORMANCE OPTIMIZATIONS
   ============================================ */

/* Use GPU acceleration for transforms */
@media (max-width: 768px) {
    .card,
    .btn,
    .modal-content,
    .navbar {
        will-change: transform;
        transform: translateZ(0);
    }
}

/* Optimize repaints */
@media (max-width: 768px) {
    * {
        backface-visibility: hidden;
        -webkit-backface-visibility: hidden;
    }
}

/* Reduce layout shifts */
img,
video,
iframe {
    aspect-ratio: attr(width) / attr(height);
}

/* ============================================
   LAZY LOADING ENHANCEMENTS
   ============================================ */

img[data-src] {
    opacity: 0;
    transition: opacity 0.3s ease-in-out;
    background: #f0f0f0;
    min-height: 200px;
}

img[data-src].loaded {
    opacity: 1;
}

/* Skeleton loader for images */
img[data-src]::before {
    content: '';
    display: block;
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: linear-gradient(90deg, #f0f0f0 25%, #e0e0e0 50%, #f0f0f0 75%);
    background-size: 200% 100%;
    animation: loading 1.5s infinite;
}

/* ============================================
   MOBILE-SPECIFIC LAYOUTS
   ============================================ */

@media (max-width: 768px) {
    /* Stack columns on mobile */
    .row {
        flex-direction: column !important;
        width: 100% !important;
    }
    
    .col-md-6,
    .col-lg-4,
    .col-lg-6,
    .col-lg-8,
    .col-lg-3,
    .col-lg-9 {
        width: 100% !important;
        max-width: 100% !important;
        flex: 0 0 100% !important;
    }
    
    /* Full-width cards */
    .card {
        width: 100% !important;
        margin-left: 0 !important;
        margin-right: 0 !important;
    }
    
    /* Optimize spacing */
    .container {
        padding-left: 1rem !important;
        padding-right: 1rem !important;
        width: 100% !important;
        max-width: 100% !important;
    }
    
    /* Reduce padding in cards */
    .card-body {
        padding: 1rem !important;
    }
    
    /* Stack buttons */
    .btn-group {
        flex-direction: column !important;
    }
    
    .btn-group .btn {
        width: 100% !important;
        margin-bottom: 0.5rem !important;
        border-radius: 8px !important;
    }
    
    /* Force mobile tables */
    table {
        width: 100% !important;
        display: block !important;
        overflow-x: auto !important;
    }
}

/* ============================================
   NETWORK-AWARE STYLES
   ============================================ */

.slow-connection img:not(.essential) {
    display: none;
}

.slow-connection video:not(.essential) {
    display: none;
}

.slow-connection .lazy-content {
    display: none;
}

/* ============================================
   TOUCH OPTIMIZATIONS
   ============================================ */

@media (max-width: 768px) {
    /* Larger hit areas */
    a, button, .btn, .nav-link {
        min-height: 44px;
        min-width: 44px;
        display: inline-flex;
        align-items: center;
        justify-content: center;
    }
    
    /* Prevent text selection on buttons */
    button, .btn {
        -webkit-user-select: none;
        -moz-user-select: none;
        -ms-user-select: none;
        user-select: none;
    }
    
    /* Better tap feedback */
    button:active,
    .btn:active,
    a:active {
        opacity: 0.7;
        transform: scale(0.98);
    }
}

/* ============================================
   SCROLL OPTIMIZATIONS
   ============================================ */

@media (max-width: 768px) {
    /* Smooth scrolling */
    html {
        scroll-behavior: smooth;
        -webkit-overflow-scrolling: touch;
    }
    
    /* Optimize scroll containers */
    .scroll-container {
        -webkit-overflow-scrolling: touch;
        overflow-y: auto;
        will-change: scroll-position;
    }
    
    /* Reduce scroll jank */
    * {
        -webkit-transform: translateZ(0);
        transform: translateZ(0);
    }
}

/* ============================================
   DASHBOARD OPTIMIZATIONS
   ============================================ */

@media (max-width: 768px) {
    .dashboard-container {
        padding: 0.5rem;
    }
    
    .dashboard-card {
        margin-bottom: 1rem;
        padding: 1rem;
    }
    
    .dashboard-stats {
        flex-direction: column;
        gap: 1rem;
    }
    
    .dashboard-stats .stat-card {
        width: 100%;
    }
}

/* ============================================
   NEWSFEED OPTIMIZATIONS
   ============================================ */

@media (max-width: 768px) {
    .newsfeed-container {
        padding: 0.5rem;
    }
    
    .post-card {
        margin-bottom: 1rem;
        border-radius: 12px;
    }
    
    .post-actions {
        flex-wrap: wrap;
        gap: 0.5rem;
    }
    
    .post-actions button {
        flex: 1;
        min-width: calc(50% - 0.25rem);
    }
}

/* ============================================
   QUIZ/MODULE OPTIMIZATIONS
   ============================================ */

@media (max-width: 768px) {
    .quiz-container,
    .module-container {
        padding: 1rem;
    }
    
    .quiz-question {
        font-size: 1.125rem;
        margin-bottom: 1.5rem;
    }
    
    .quiz-option {
        padding: 1rem;
        margin-bottom: 0.75rem;
        min-height: 60px;
        display: flex;
        align-items: center;
    }
    
    .module-content {
        font-size: 1rem;
        line-height: 1.6;
    }
}

/* ============================================
   LIVE CLASS OPTIMIZATIONS
   ============================================ */

@media (max-width: 768px) {
    .meet-container {
        flex-direction: column;
    }
    
    .video-grid {
        grid-template-columns: 1fr !important;
        gap: 0.5rem;
    }
    
    .controls-bar {
        flex-wrap: wrap;
        padding: 0.5rem;
    }
    
    .control-button {
        min-width: 48px;
        min-height: 48px;
        font-size: 1.25rem;
    }
    
    .meet-sidebar {
        width: 100%;
        max-height: 50vh;
    }
}

/* ============================================
   STUDY GROUPS OPTIMIZATIONS
   ============================================ */

@media (max-width: 768px) {
    .study-group-card {
        width: 100%;
        margin-bottom: 1rem;
    }
    
    .study-group-members {
        flex-wrap: wrap;
    }
    
    .study-group-actions {
        flex-direction: column;
        width: 100%;
    }
    
    .study-group-actions .btn {
        width: 100%;
        margin-bottom: 0.5rem;
    }
}

/* ============================================
   LOADING STATES
   ============================================ */

.loading-spinner {
    display: inline-block;
    width: 20px;
    height: 20px;
    border: 3px solid rgba(0, 166, 81, 0.3);
    border-radius: 50%;
    border-top-color: #00A651;
    animation: spin 1s ease-in-out infinite;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

/* ============================================
   CRITICAL ABOVE-THE-FOLD OPTIMIZATIONS
   ============================================ */

@media (max-width: 768px) {
    /* Prioritize above-the-fold content */
    .above-fold {
        will-change: contents;
    }
    
    /* Defer below-the-fold content */
    .below-fold {
        content-visibility: auto;
        contain-intrinsic-size: 200px;
    }
}

/* ============================================
   FORCE MOBILE VIEW - NUCLEAR OPTION
   ============================================ */

/* Critical fix for phones showing desktop view */
@media (max-width: 991.98px) {
    /* Hide ALL desktop navigation elements */
    .desktop-nav,
    .navbar-nav.d-none.d-lg-flex,
    .d-none.d-lg-block,
    .d-none.d-lg-flex,
    .d-none.d-lg-inline,
    .d-none.d-lg-inline-block,
    nav .d-lg-flex:not(#mobile-menu-toggle) {
        display: none !important;
        visibility: hidden !important;
        opacity: 0 !important;
        pointer-events: none !important;
        position: absolute !important;
        left: -9999px !important;
    }
    
    /* FORCE mobile toggle to display */
    #mobile-menu-toggle,
    .navbar-toggler,
    .d-lg-none {
        display: flex !important;
        visibility: visible !important;
        opacity: 1 !important;
        pointer-events: auto !important;
    }
    
    /* Force mobile viewport */
    html {
        width: 100vw !important;
        max-width: 100vw !important;
        overflow-x: hidden !important;
        -webkit-text-size-adjust: 100% !important;
    }
    
    body {
        width: 100vw !important;
        max-width: 100vw !important;
        overflow-x: hidden !important;
    }
    
    /* Force all elements to respect viewport */
    * {
        max-width: 100vw !important;
    }
    
    /* Force containers */
    .container,
    .container-fluid,
    .container-lg,
    .container-md,
    .container-sm,
    .container-xl {
        width: 100% !important;
        max-width: 100% !important;
        padding-left: 1rem !important;
        padding-right: 1rem !important;
        margin-left: auto !important;
        margin-right: auto !important;
    }
    
    /* Force navbar mobile mode */
    .navbar {
        width: 100% !important;
        padding: 0.75rem 1rem !important;
    }
    
    .navbar-brand {
        max-width: 70% !important;
    }
    
    /* CRITICAL: Force single column layouts for ALL content */
    .row,
    div[class*="row"] {
        display: flex !important;
        flex-direction: column !important;
        width: 100% !important;
        margin-left: 0 !important;
        margin-right: 0 !important;
    }
    
    /* Force ALL Bootstrap columns to full width */
    [class*="col-"],
    .col, .col-1, .col-2, .col-3, .col-4, .col-5, .col-6,
    .col-7, .col-8, .col-9, .col-10, .col-11, .col-12,
    .col-sm, .col-sm-1, .col-sm-2, .col-sm-3, .col-sm-4, .col-sm-5, .col-sm-6,
    .col-sm-7, .col-sm-8, .col-sm-9, .col-sm-10, .col-sm-11, .col-sm-12,
    .col-md, .col-md-1, .col-md-2, .col-md-3, .col-md-4, .col-md-5, .col-md-6,
    .col-md-7, .col-md-8, .col-md-9, .col-md-10, .col-md-11, .col-md-12,
    .col-lg, .col-lg-1, .col-lg-2, .col-lg-3, .col-lg-4, .col-lg-5, .col-lg-6,
    .col-lg-7, .col-lg-8, .col-lg-9, .col-lg-10, .col-lg-11, .col-lg-12,
    .col-xl, .col-xl-1, .col-xl-2, .col-xl-3, .col-xl-4, .col-xl-5, .col-xl-6,
    .col-xl-7, .col-xl-8, .col-xl-9, .col-xl-10, .col-xl-11, .col-xl-12,
    .col-xxl, .col-xxl-1, .col-xxl-2, .col-xxl-3, .col-xxl-4, .col-xxl-5, .col-xxl-6,
    .col-xxl-7, .col-xxl-8, .col-xxl-9, .col-xxl-10, .col-xxl-11, .col-xxl-12 {
        width: 100% !important;
        max-width: 100% !important;
        flex: 0 0 100% !important;
        flex-basis: 100% !important;
        padding-left: 0.75rem !important;
        padding-right: 0.75rem !important;
        margin-bottom: 1rem !important;
    }
    
    /* Force cards to stack vertically */
    .card,
    .course-card,
    .post-card,
    .earnings-card,
    .stat-card,
    .summary-card,
    div[class*="card"] {
        width: 100% !important;
        max-width: 100% !important;
        margin-left: 0 !important;
        margin-right: 0 !important;
        margin-bottom: 1rem !important;
    }
    
    /* Force flexbox layouts to column */
    .d-flex:not(.mobile-nav-link):not(.navbar):not([class*="btn"]),
    div[class*="flex"]:not(.mobile-nav-link):not(.navbar) {
        flex-direction: column !important;
    }
    
    /* Force grid layouts to single column */
    .d-grid,
    [style*="display: grid"],
    [style*="grid-template-columns"] {
        grid-template-columns: 1fr !important;
        display: grid !important;
    }
    
    /* Exception: teacher dashboard keeps Quick Actions sidebar on the right (2fr 1fr) */
    .teacher-dashboard-wrap .dashboard-grid {
        grid-template-columns: 2fr 1fr !important;
        display: grid !important;
    }
    .teacher-dashboard-wrap .dashboard-sidebar {
        display: flex !important;
        visibility: visible !important;
        min-width: 260px !important;
    }
    
    /* Force dashboard cards to full width */
    .dashboard-container,
    .dashboard-stats,
    .stat-cards,
    .earnings-cards {
        display: flex !important;
        flex-direction: column !important;
        width: 100% !important;
    }
    
    /* Force course grid to 2 columns on mobile */
    .courses-grid,
    div.courses-grid,
    .courses-page .courses-grid,
    .courses-content .courses-grid {
        display: grid !important;
        grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
        gap: 0.5rem !important;
    }
    
    .course-list {
        display: flex !important;
        flex-direction: column !important;
    }
}

/* Critical fix for phones showing desktop view */
@media (max-width: 991.98px) {
    /* SHOW mobile elements on mobile */
    #mobile-menu-toggle,
    .navbar-toggler {
        display: flex !important;
        visibility: visible !important;
    }
    
    /* HIDE desktop nav on mobile */
    .desktop-nav,
    .d-none.d-lg-flex,
    .d-none.d-lg-block {
        display: none !important;
        visibility: hidden !important;
    }
}

