/**
 * CPWE AI Mobile Optimization Enhancements
 * Improves mobile performance scores without breaking existing logic
 */

/* ========================================
   TOUCH TARGET OPTIMIZATION (44x44px minimum)
   ======================================== */
@media (max-width: 768px) {
    /* Button elements only - safe to use inline-flex */
    /* Exclude dropdown toggles to preserve Bootstrap functionality */
    button:not(.dropdown-toggle),
    input[type="button"],
    input[type="submit"],
    .btn:not(.dropdown-toggle),
    .btn-primary:not(.dropdown-toggle),
    .btn-secondary:not(.dropdown-toggle),
    .btn-success:not(.dropdown-toggle),
    .btn-danger:not(.dropdown-toggle),
    .btn-warning:not(.dropdown-toggle),
    .btn-info:not(.dropdown-toggle),
    .btn-light:not(.dropdown-toggle),
    .btn-dark:not(.dropdown-toggle),
    .btn-outline-primary:not(.dropdown-toggle),
    .btn-outline-secondary:not(.dropdown-toggle),
    .navbar-toggler,
    [role="button"]:not(.nav-link):not(.dropdown-item):not(.dropdown-toggle) {
        min-width: 44px;
        min-height: 44px;
        padding: 12px 16px;
        display: inline-flex;
        align-items: center;
        justify-content: center;
    }
    
    /* Dropdown toggles - preserve Bootstrap dropdown functionality */
    .dropdown-toggle {
        min-height: 44px;
        padding-top: 10px;
        padding-bottom: 10px;
        /* Do NOT change display - Bootstrap needs default behavior */
    }
    
    /* Navigation and dropdown items - preserve Bootstrap layout */
    /* Only add touch target sizing, keep block display */
    .nav-link,
    .dropdown-item,
    .pagination .page-link {
        min-height: 44px;
        padding-top: 10px;
        padding-bottom: 10px;
        /* Do NOT change display property - let Bootstrap handle it */
    }
    
    /* Navbar toggler needs proper touch target */
    .navbar-toggler {
        min-width: 48px;
        min-height: 48px;
        padding: 12px;
    }
    
    /* Form controls */
    input,
    select,
    textarea {
        min-height: 48px;
        font-size: 16px; /* Prevents zoom on iOS */
        padding: 12px;
    }
    
    /* Card actions */
    .card-footer button,
    .card-footer a {
        margin: 8px 4px;
        min-width: 48px;
    }
}

/* ========================================
   TYPOGRAPHY & READABILITY
   ======================================== */
@media (max-width: 768px) {
    body {
        font-size: 16px; /* Minimum for mobile readability */
        line-height: 1.6;
    }
    
    h1 {
        font-size: 1.75rem;
        line-height: 1.3;
    }
    
    h2 {
        font-size: 1.5rem;
        line-height: 1.3;
    }
    
    h3 {
        font-size: 1.25rem;
        line-height: 1.4;
    }
    
    p {
        font-size: 1rem;
        line-height: 1.6;
        margin-bottom: 1rem;
    }
    
    /* Prevent text from being too small */
    small,
    .small {
        font-size: 0.875rem; /* Minimum 14px */
    }
}

/* ========================================
   LAYOUT & SPACING
   ======================================== */
@media (max-width: 768px) {
    /* Reduce excessive padding on mobile */
    .container,
    .container-fluid {
        padding-left: 16px;
        padding-right: 16px;
    }
    
    /* Stack elements vertically on mobile */
    .row {
        margin-left: -8px;
        margin-right: -8px;
    }
    
    .row > [class*="col-"] {
        padding-left: 8px;
        padding-right: 8px;
        margin-bottom: 16px;
    }
    
    /* Cards with proper spacing */
    .card {
        margin-bottom: 16px;
        border-radius: 8px;
    }
    
    .card-body {
        padding: 16px;
    }
    
    /* Sections */
    .section,
    section {
        padding: 24px 0;
    }
    
    /* Hero sections */
    .hero-section,
    .jumbotron {
        min-height: auto;
        padding: 32px 16px;
    }
}

/* ========================================
   NAVIGATION & HEADER
   ======================================== */
@media (max-width: 768px) {
    /* Sticky header optimization */
    .navbar {
        position: sticky;
        top: 0;
        z-index: 1030;
        backdrop-filter: blur(10px);
    }
    
    /* Hamburger menu */
    .navbar-toggler {
        min-width: 48px;
        min-height: 48px;
        padding: 12px;
        border: none;
    }
    
    /* Mobile menu */
    .navbar-collapse {
        max-height: calc(100vh - 60px);
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
    }
    
    /* Dropdown menus */
    .dropdown-menu {
        position: static !important;
        transform: none !important;
        box-shadow: none;
        border: none;
        border-top: 1px solid rgba(255, 255, 255, 0.1);
    }
}

/* ========================================
   IMAGES & MEDIA
   ======================================== */
@media (max-width: 768px) {
    /* Responsive images */
    img {
        max-width: 100%;
        height: auto;
        display: block;
    }
    
    /* Image containers */
    .img-container,
    .image-wrapper {
        position: relative;
        width: 100%;
        overflow: hidden;
    }
    
    /* Video embeds */
    iframe,
    video {
        max-width: 100%;
        height: auto;
    }
    
    /* Charts and graphs */
    .chart-container,
    canvas {
        max-width: 100%;
        height: auto !important;
    }
}

/* ========================================
   TABLES & DATA DISPLAY
   ======================================== */
@media (max-width: 768px) {
    /* Responsive tables */
    .table-responsive {
        display: block;
        width: 100%;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }
    
    /* Table styling */
    table {
        font-size: 0.875rem;
    }
    
    th,
    td {
        padding: 8px;
        white-space: nowrap;
    }
    
    /* Card-based table alternative */
    .table-mobile-cards tbody tr {
        display: block;
        margin-bottom: 16px;
        border: 1px solid rgba(255, 255, 255, 0.1);
        border-radius: 8px;
        padding: 12px;
    }
    
    .table-mobile-cards td {
        display: block;
        text-align: right;
        padding: 8px 0;
        border: none;
    }
    
    .table-mobile-cards td:before {
        content: attr(data-label);
        float: left;
        font-weight: 600;
    }
}

/* ========================================
   MODALS & OVERLAYS
   ======================================== */
@media (max-width: 768px) {
    /* Full-screen modals on mobile */
    .modal-dialog {
        margin: 0;
        max-width: 100%;
        height: 100vh;
    }
    
    .modal-content {
        height: 100%;
        border: none;
        border-radius: 0;
    }
    
    .modal-body {
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
        flex: 1;
    }
    
    /* Modal close button */
    .modal-header .btn-close {
        min-width: 44px;
        min-height: 44px;
        padding: 12px;
    }
}

/* ========================================
   FORMS & INPUTS
   ======================================== */
@media (max-width: 768px) {
    /* Form groups */
    .form-group,
    .mb-3 {
        margin-bottom: 16px;
    }
    
    /* Labels */
    label {
        font-size: 0.875rem;
        font-weight: 600;
        margin-bottom: 8px;
        display: block;
    }
    
    /* Input groups */
    .input-group {
        flex-wrap: wrap;
    }
    
    .input-group > * {
        flex: 1 1 100%;
        min-width: 0;
    }
    
    /* Checkboxes and radios */
    .form-check {
        min-height: 48px;
        padding-left: 48px;
        display: flex;
        align-items: center;
    }
    
    .form-check-input {
        min-width: 24px;
        min-height: 24px;
        margin-left: -48px;
    }
}

/* ========================================
   BUTTONS & ACTIONS
   ======================================== */
@media (max-width: 768px) {
    /* Button sizes */
    .btn {
        font-size: 1rem;
        padding: 12px 24px;
        border-radius: 8px;
    }
    
    .btn-sm {
        min-height: 36px;
        padding: 8px 16px;
        font-size: 0.875rem;
    }
    
    .btn-lg {
        min-height: 56px;
        padding: 16px 32px;
        font-size: 1.125rem;
    }
    
    /* Button groups */
    .btn-group {
        display: flex;
        flex-wrap: wrap;
        gap: 8px;
    }
    
    .btn-group > .btn {
        flex: 1 1 auto;
        margin: 0;
    }
    
    /* Full-width buttons */
    .btn-block,
    .d-grid .btn {
        width: 100%;
    }
}

/* ========================================
   PERFORMANCE OPTIMIZATIONS
   ======================================== */
@media (max-width: 768px) {
    /* Disable parallax effects on mobile - they're heavy */
    [data-parallax],
    .parallax {
        background-attachment: scroll !important;
        transform: none !important;
    }
    
    /* Optimize shadows - use simpler shadows on mobile */
    .shadow-lg {
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1) !important;
    }
    
    /* Respect user's motion preferences */
    @media (prefers-reduced-motion: reduce) {
        *,
        *::before,
        *::after {
            animation-duration: 0.01ms !important;
            transition-duration: 0.01ms !important;
        }
    }
}

/* ========================================
   ACCESSIBILITY
   ======================================== */
@media (max-width: 768px) {
    /* Focus states */
    :focus,
    :focus-visible {
        outline: 2px solid #00ffff !important;
        outline-offset: 2px;
    }
    
    /* Skip links */
    .skip-to-content {
        position: absolute;
        top: -40px;
        left: 0;
        background: #000;
        color: #fff;
        padding: 12px 16px;
        text-decoration: none;
        z-index: 9999;
    }
    
    .skip-to-content:focus {
        top: 0;
    }
}

/* ========================================
   UTILITY CLASSES
   ======================================== */
@media (max-width: 768px) {
    /* Show/hide on mobile */
    .mobile-only {
        display: block !important;
    }
    
    .desktop-only {
        display: none !important;
    }
    
    /* Text alignment */
    .text-mobile-center {
        text-align: center !important;
    }
    
    .text-mobile-left {
        text-align: left !important;
    }
    
    /* Spacing utilities */
    .mb-mobile-3 {
        margin-bottom: 1rem !important;
    }
    
    .mt-mobile-3 {
        margin-top: 1rem !important;
    }
    
    .p-mobile-2 {
        padding: 0.5rem !important;
    }
}

/* ========================================
   LANDSCAPE ORIENTATION
   ======================================== */
@media (max-width: 768px) and (orientation: landscape) {
    /* Reduce header height in landscape */
    .navbar {
        min-height: 48px;
    }
    
    /* Adjust hero sections */
    .hero-section {
        min-height: auto;
        padding: 24px 16px;
    }
}

/* ========================================
   DARK MODE MOBILE OPTIMIZATION
   ======================================== */
@media (max-width: 768px) and (prefers-color-scheme: dark) {
    /* Reduce contrast for better readability */
    body {
        background-color: #0a0a0a;
        color: #e0e0e0;
    }
    
    .card {
        background-color: #1a1a1a;
        border-color: rgba(255, 255, 255, 0.1);
    }
}

/* ========================================
   PRINT OPTIMIZATION
   ======================================== */
@media print {
    /* Hide non-essential elements */
    .navbar,
    .btn,
    .no-print {
        display: none !important;
    }
    
    /* Optimize for print */
    body {
        font-size: 12pt;
        line-height: 1.5;
        color: #000;
        background: #fff;
    }
    
    a {
        text-decoration: underline;
        color: #000;
    }
    
    img {
        max-width: 100%;
        page-break-inside: avoid;
    }
}
