/**
 * MOBILE OPTIMIZER - Universal Mobile CSS Framework
 * Ensures clean, efficient mobile layouts across all pages
 * No overlapping elements, optimized visual space
 */

/* ============================================
   MOBILE BASE RESETS
   ============================================ */

@media (max-width: 768px) {
    /* Prevent horizontal scroll */
    body {
        overflow-x: hidden;
        max-width: 100vw;
    }

    /* Box sizing for all elements */
    *, *::before, *::after {
        box-sizing: border-box;
    }

    /* Remove default margins that cause overflow */
    html, body {
        margin: 0;
        padding: 0;
        width: 100%;
    }

    /* Ensure containers don't exceed viewport */
    .container {
        max-width: 100%;
        padding-left: 1rem;
        padding-right: 1rem;
        margin-left: auto;
        margin-right: auto;
    }

    /* ============================================
       NAVIGATION & HEADERS
       ============================================ */

    /* Fixed headers should not block content */
    header, .header, nav, .nav, .navbar {
        position: relative;
        z-index: 100;
        width: 100%;
        padding: 0.75rem 1rem;
    }

    /* Breadcrumb navigation */
    .breadcrumb-nav, .nav-bar {
        overflow-x: auto;
        white-space: nowrap;
        -webkit-overflow-scrolling: touch;
        padding: 0.5rem 1rem;
    }

    .breadcrumb-nav::-webkit-scrollbar {
        height: 2px;
    }

    /* ============================================
       BUTTONS & CONTROLS
       ============================================ */

    /* Ensure buttons don't overlap */
    .btn, button, .button, a.button {
        min-height: 44px; /* Touch-friendly size */
        padding: 0.75rem 1.5rem;
        margin: 0.5rem 0.25rem;
        font-size: 0.95rem;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }

    /* Button groups */
    .button-group, .btn-group {
        display: flex;
        flex-direction: column;
        gap: 0.75rem;
        width: 100%;
    }

    /* Floating action buttons */
    .floating-btn, .fab, .control-btn {
        position: fixed !important;
        z-index: 1000;
        width: 50px;
        height: 50px;
    }

    /* Position floating buttons to avoid overlap */
    .floating-btn:nth-child(1), .control-btn:nth-child(1) {
        bottom: 2rem;
        right: 2rem;
    }

    .floating-btn:nth-child(2), .control-btn:nth-child(2) {
        bottom: 2rem;
        right: calc(2rem + 60px);
    }

    /* Skip/close buttons top-left */
    .skip-btn, .close-btn, .back-btn {
        top: 1rem;
        left: 1rem;
        position: fixed;
        z-index: 10001;
    }

    /* ============================================
       MODALS & OVERLAYS
       ============================================ */

    .modal, .overlay, .popup {
        position: fixed;
        inset: 0;
        width: 100vw;
        height: 100vh;
        padding: 1rem;
        overflow-y: auto;
        z-index: 9999;
    }

    .modal-content, .overlay-content {
        max-width: calc(100vw - 2rem);
        max-height: calc(100vh - 2rem);
        margin: auto;
        padding: 1.5rem;
        overflow-y: auto;
    }

    /* ============================================
       TYPOGRAPHY & TEXT
       ============================================ */

    h1 {
        font-size: clamp(1.75rem, 5vw, 2.5rem);
        line-height: 1.2;
        margin: 1rem 0;
    }

    h2 {
        font-size: clamp(1.5rem, 4vw, 2rem);
        line-height: 1.3;
        margin: 0.875rem 0;
    }

    h3 {
        font-size: clamp(1.25rem, 3.5vw, 1.75rem);
        line-height: 1.4;
        margin: 0.75rem 0;
    }

    p {
        font-size: clamp(0.95rem, 3vw, 1.1rem);
        line-height: 1.6;
        margin: 0.75rem 0;
    }

    /* Prevent text overflow */
    h1, h2, h3, h4, h5, h6, p, span, div {
        word-wrap: break-word;
        overflow-wrap: break-word;
        hyphens: auto;
    }

    /* ============================================
       GRIDS & LAYOUTS
       ============================================ */

    /* Convert multi-column grids to single column */
    .grid, .grid-container, .row {
        display: flex;
        flex-direction: column;
        gap: 1rem;
        width: 100%;
    }

    /* Specific grid classes */
    .grid-2, .grid-3, .grid-4, .grid-5, .grid-6,
    .col-2, .col-3, .col-4, .col-5, .col-6 {
        grid-template-columns: 1fr !important;
        display: grid;
        gap: 1rem;
    }

    .flex-row {
        flex-direction: column;
    }

    /* ============================================
       CARDS & CONTAINERS
       ============================================ */

    .card, .box, .panel {
        width: 100%;
        max-width: 100%;
        margin: 0.75rem 0;
        padding: 1rem;
        border-radius: 12px;
    }

    /* Prevent card overflow */
    .card-content, .box-content {
        max-width: 100%;
        overflow-x: auto;
    }

    /* ============================================
       TABLES
       ============================================ */

    table {
        width: 100%;
        display: block;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }

    thead {
        display: none; /* Hide headers on mobile */
    }

    tbody, tr, td {
        display: block;
        width: 100%;
    }

    tr {
        margin-bottom: 1rem;
        padding: 1rem;
        border: 1px solid #e5e7eb;
        border-radius: 8px;
    }

    td {
        padding: 0.5rem 0;
        text-align: left;
    }

    td::before {
        content: attr(data-label);
        font-weight: 600;
        display: inline-block;
        width: 40%;
        margin-right: 1rem;
    }

    /* ============================================
       FORMS & INPUTS
       ============================================ */

    input, textarea, select {
        width: 100%;
        max-width: 100%;
        font-size: 16px; /* Prevent zoom on iOS */
        padding: 0.875rem;
        margin: 0.5rem 0;
        border-radius: 8px;
    }

    label {
        display: block;
        margin-bottom: 0.25rem;
        font-weight: 600;
    }

    form {
        width: 100%;
        padding: 0;
    }

    /* ============================================
       IMAGES & MEDIA
       ============================================ */

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

    .video-container, .embed-container {
        position: relative;
        padding-bottom: 56.25%; /* 16:9 aspect ratio */
        height: 0;
        overflow: hidden;
        max-width: 100%;
    }

    .video-container iframe,
    .embed-container iframe {
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
    }

    /* ============================================
       ASSESSMENT & QUIZ COMPONENTS
       ============================================ */

    .assessment-container, .quiz-container {
        width: 100%;
        padding: 1rem;
    }

    .question-card {
        width: 100%;
        margin: 1rem 0;
        padding: 1rem;
    }

    .answer-options {
        display: flex;
        flex-direction: column;
        gap: 0.75rem;
    }

    .answer-option {
        min-height: 44px;
        padding: 1rem;
        border-radius: 8px;
        cursor: pointer;
    }

    /* ============================================
       BOOK READER & TOC
       ============================================ */

    .book-reader-layout {
        flex-direction: column;
    }

    .toc-sidebar {
        position: static !important;
        width: 100%;
        max-height: 400px;
        order: 2;
        margin-top: 1rem;
    }

    .book-content-wrapper {
        order: 1;
        width: 100%;
    }

    .chapter-navigation {
        display: flex;
        flex-wrap: wrap;
        justify-content: space-between;
        gap: 0.5rem;
        padding: 1rem 0;
    }

    .chapter-btn {
        flex: 1;
        min-width: 100px;
    }

    /* ============================================
       VOICE CONTROLS & AUDIO
       ============================================ */

    .voice-controls, .audio-controls {
        position: fixed;
        bottom: 1.5rem;
        right: 1.5rem;
        display: flex;
        flex-direction: column;
        gap: 0.75rem;
        z-index: 100;
    }

    .voice-btn, .audio-btn {
        width: 50px;
        height: 50px;
        border-radius: 50%;
        padding: 0;
    }

    /* ============================================
       DASHBOARD & STATS
       ============================================ */

    .dashboard-grid {
        grid-template-columns: 1fr !important;
        gap: 1rem;
    }

    .stat-card, .metric-card {
        width: 100%;
        padding: 1.25rem;
    }

    .progress-bar {
        width: 100%;
        height: 8px;
        border-radius: 4px;
    }

    /* ============================================
       HERO SECTIONS
       ============================================ */

    .hero-section, .hero {
        padding: 2rem 1rem;
        min-height: auto;
    }

    .hero-title, .hero h1 {
        font-size: clamp(2rem, 6vw, 3rem);
        line-height: 1.1;
    }

    .hero-subtitle {
        font-size: clamp(1rem, 3vw, 1.25rem);
        margin-top: 1rem;
    }

    /* ============================================
       PRICING & OFFERINGS
       ============================================ */

    .offerings-grid, .pricing-grid {
        grid-template-columns: 1fr !important;
        gap: 1.5rem;
    }

    .offering-card, .pricing-card {
        width: 100%;
        transform: none !important; /* Remove desktop scale effects */
    }

    .offering-card.featured {
        transform: none !important;
        order: -1; /* Show featured first */
    }

    /* ============================================
       FLOATING WIDGETS
       ============================================ */

    .floating-help-widget {
        bottom: 1.5rem;
        right: 1.5rem;
    }

    .help-widget-button {
        width: 50px;
        height: 50px;
    }

    .help-widget-menu {
        right: -10px;
        bottom: 65px;
        min-width: 280px;
        max-width: calc(100vw - 2rem);
    }

    /* ============================================
       CINEMATIC INTRO
       ============================================ */

    .cinematic-container {
        width: 100vw;
        height: 100vh;
    }

    .scene {
        padding: 2rem 1rem;
    }

    .scene-question {
        font-size: clamp(1.5rem, 5vw, 2.5rem);
    }

    .scene-answer {
        font-size: clamp(1rem, 3vw, 1.25rem);
        line-height: 1.6;
    }

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

    .hide-mobile {
        display: none !important;
    }

    .show-mobile {
        display: block !important;
    }

    .mobile-stack {
        flex-direction: column !important;
    }

    .mobile-full-width {
        width: 100% !important;
    }

    .mobile-center {
        text-align: center !important;
    }

    /* ============================================
       SPACING UTILITIES
       ============================================ */

    .p-mobile-sm { padding: 0.5rem !important; }
    .p-mobile-md { padding: 1rem !important; }
    .p-mobile-lg { padding: 1.5rem !important; }

    .m-mobile-sm { margin: 0.5rem !important; }
    .m-mobile-md { margin: 1rem !important; }
    .m-mobile-lg { margin: 1.5rem !important; }

    /* ============================================
       SAFE AREAS (iOS Notch)
       ============================================ */

    @supports (padding: max(0px)) {
        body {
            padding-top: max(0px, env(safe-area-inset-top));
            padding-bottom: max(0px, env(safe-area-inset-bottom));
            padding-left: max(0px, env(safe-area-inset-left));
            padding-right: max(0px, env(safe-area-inset-right));
        }

        .fixed-top {
            top: max(1rem, calc(env(safe-area-inset-top) + 1rem));
        }

        .fixed-bottom {
            bottom: max(1rem, calc(env(safe-area-inset-bottom) + 1rem));
        }
    }

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

    /* Reduce animations on mobile for performance */
    * {
        animation-duration: 0.3s !important;
        transition-duration: 0.2s !important;
    }

    /* Simplify shadows */
    .card, .box, .modal, .overlay {
        box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1) !important;
    }

    /* Optimize transforms */
    .transform-3d {
        transform: none !important;
    }
}

/* ============================================
   EXTRA SMALL DEVICES (<480px)
   ============================================ */

@media (max-width: 480px) {
    .container {
        padding-left: 0.75rem;
        padding-right: 0.75rem;
    }

    h1 {
        font-size: 1.75rem;
    }

    h2 {
        font-size: 1.5rem;
    }

    h3 {
        font-size: 1.25rem;
    }

    p {
        font-size: 0.95rem;
    }

    .btn, button {
        font-size: 0.9rem;
        padding: 0.65rem 1.25rem;
    }

    .modal-content {
        padding: 1rem;
    }

    .card, .box {
        padding: 0.875rem;
    }
}
