/* ==========================================================================
   JLEARN MASTER MOBILE-RESPONSIVE & TOUCH UI SYSTEM v3.0.97
   Comprehensive mobile UX, touch targets, responsive layouts, drawers,
   tab switchers, bottom sheets, and DepEd table scroll containers.
   ========================================================================== */

/* --------------------------------------------------------------------------
   1. GLOBAL MOBILE RESETS & PERFECT VERTICAL SCROLLING (ANDROID / IOS / ALL BROWSERS)
   -------------------------------------------------------------------------- */
html {
    -webkit-text-size-adjust: 100%;
    text-size-adjust: 100%;
    height: 100%;
    min-height: 100%;
    overflow-x: clip; /* clip prevents horizontal breakout without creating a scroll container trap on Android */
    overflow-y: auto !important;
    touch-action: pan-y;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior-y: auto;
    scroll-behavior: smooth;
}

body {
    min-height: 100%;
    height: auto !important;
    width: 100%;
    min-width: 0;
    margin: 0;
    padding: 0;
    position: relative;
    overflow-x: clip;
    overflow-y: auto !important;
    touch-action: pan-y;
    -webkit-overflow-scrolling: touch;
    -webkit-tap-highlight-color: rgba(0, 92, 48, 0.12);
}

@supports not (overflow-x: clip) {
    html {
        overflow-x: hidden;
    }
    body {
        overflow-x: hidden;
    }
}

/* Safe Area Insets for iOS Notch & Home Bar / Android Navigation Bar */
:root {
    --sat: env(safe-area-inset-top, 0px);
    --sar: env(safe-area-inset-right, 0px);
    --sab: env(safe-area-inset-bottom, 0px);
    --sal: env(safe-area-inset-left, 0px);
    --mobile-nav-height: 60px;
    --touch-min-height: 44px;
}

/* --------------------------------------------------------------------------
   2. TOUCH TARGETS & FORM INPUTS (PREVENT IOS AUTO-ZOOM & PRESERVE TOUCH SCROLL)
   -------------------------------------------------------------------------- */
@media (max-width: 768px) {
    /* Critical: 16px minimum font-size on mobile inputs prevents iOS Safari auto-zoom */
    input[type="text"],
    input[type="password"],
    input[type="email"],
    input[type="number"],
    input[type="search"],
    input[type="tel"],
    input[type="url"],
    input[type="date"],
    select,
    textarea {
        font-size: 16px !important;
        min-height: var(--touch-min-height);
        border-radius: 8px;
        touch-action: pan-y;
    }

    /* All clickable elements touch target enlargement + vertical scroll pass-through */
    button, 
    .btn, 
    .tab-btn, 
    .nav-link, 
    .tool-tab-btn,
    .mobile-menu-toggle,
    .dropdown-item,
    .quiz-palette-item {
        min-height: var(--touch-min-height);
        touch-action: manipulation;
    }

    /* Active touch feedback */
    button:active, 
    .btn:active, 
    .tool-tab-btn:active,
    .role-card:active {
        transform: scale(0.98);
        transition: transform 0.1s ease;
    }
}

/* --------------------------------------------------------------------------
   3. MOBILE NAVIGATION DRAWER & HAMBURGER MENU (INDEX.HTML & GLOBAL)
   -------------------------------------------------------------------------- */
.mobile-menu-toggle {
    display: none;
    background: transparent;
    border: none;
    font-size: 1.35rem;
    color: var(--primary-color, #005c30);
    cursor: pointer;
    padding: 8px 12px;
    border-radius: 8px;
    align-items: center;
    justify-content: center;
    z-index: 110;
}

.mobile-menu-toggle:focus {
    outline: 2px solid var(--secondary-color, #34d399);
}

.mobile-menu-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(15, 23, 42, 0.6);
    backdrop-filter: blur(4px);
    -webkit-backdrop-filter: blur(4px);
    z-index: 998;
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.3s cubic-bezier(0.4, 0, 0.2, 1), visibility 0.3s;
}

.mobile-menu-overlay.active {
    opacity: 1;
    visibility: visible;
}

.mobile-nav-drawer {
    position: fixed;
    top: 0;
    right: -320px;
    width: 300px;
    max-width: 85vw;
    height: 100%;
    height: 100dvh;
    background: var(--surface-color, #ffffff);
    box-shadow: -4px 0 25px rgba(0, 0, 0, 0.2);
    z-index: 999;
    padding: calc(var(--sat) + 20px) 20px calc(var(--sab) + 20px) 20px;
    display: flex;
    flex-direction: column;
    gap: 16px;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    transition: transform 0.35s cubic-bezier(0.4, 0, 0.2, 1);
}

.mobile-nav-drawer.active {
    transform: translateX(-320px);
}

.mobile-nav-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding-bottom: 12px;
    border-bottom: 1px solid var(--border-color, #e2e8f0);
}

.mobile-nav-header .drawer-title {
    font-size: 1.1rem;
    font-weight: 700;
    color: var(--primary-color, #005c30);
    display: flex;
    align-items: center;
    gap: 8px;
}

.mobile-nav-close {
    background: var(--hover-bg, #f1f5f9);
    border: none;
    width: 36px;
    height: 36px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--text-secondary, #64748b);
    font-size: 1.1rem;
    cursor: pointer;
}

.mobile-nav-links {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.mobile-nav-link {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px 16px;
    border-radius: 10px;
    color: var(--text-color, #1e293b);
    text-decoration: none;
    font-weight: 500;
    font-size: 0.95rem;
    transition: background 0.2s ease, color 0.2s ease;
}

.mobile-nav-link:hover,
.mobile-nav-link.active {
    background: var(--primary-light, #ecfdf5);
    color: var(--primary-color, #005c30);
}

.mobile-nav-link i {
    width: 20px;
    text-align: center;
    font-size: 1.1rem;
}

/* --------------------------------------------------------------------------
   4. DUAL-COLUMN GENERATOR TOOLS RESPONSIVE LAYOUT & MOBILE TAB SWITCHER
   (ILAW Lesson Plan, Lesson Exemplar, PPT Generator, C.O. Maker)
   -------------------------------------------------------------------------- */
.mobile-view-tabs {
    display: none;
    position: sticky;
    top: calc(var(--sat) + 56px);
    z-index: 85;
    background: var(--surface-color, #ffffff);
    padding: 8px 12px;
    border-bottom: 1px solid var(--border-color, #e2e8f0);
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.04);
}

.mobile-view-tabs .tabs-segmented {
    display: flex;
    background: var(--bg-secondary, #f1f5f9);
    border-radius: 10px;
    padding: 4px;
    gap: 4px;
    width: 100%;
}

.mobile-view-tabs .tab-segment-btn {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    padding: 8px 12px;
    font-size: 0.875rem;
    font-weight: 600;
    color: var(--text-secondary, #64748b);
    background: transparent;
    border: none;
    border-radius: 8px;
    cursor: pointer;
    transition: all 0.2s ease;
}

.mobile-view-tabs .tab-segment-btn.active {
    background: var(--surface-color, #ffffff);
    color: var(--primary-color, #005c30);
    box-shadow: 0 1px 4px rgba(0, 0, 0, 0.1);
}

@media (max-width: 1023px) {
    .mobile-view-tabs {
        display: block;
    }

    /* Never display generator mobile tabs on student portal or login pages */
    #student-login-page .mobile-view-tabs,
    #student-portal-container .mobile-view-tabs,
    .student-portal-wrapper .mobile-view-tabs,
    body:not(.generator-page) .mobile-view-tabs {
        display: none !important;
    }

    /* Ensure all main application containers can vertically scroll naturally */
    html,
    body {
        height: auto !important;
        min-height: 100% !important;
        overflow-x: clip !important;
        overflow-y: auto !important;
        touch-action: pan-y !important;
        -webkit-overflow-scrolling: touch !important;
    }

    #app,
    .page,
    .page.active,
    .generator-app,
    .generator-container,
    .main-container,
    .dashboard-container,
    .app-layout-2col,
    main,
    aside,
    section {
        height: auto !important;
        min-height: 0 !important;
        max-height: none !important;
        overflow-y: visible !important;
        overflow-x: visible !important;
        touch-action: pan-y !important;
        -webkit-overflow-scrolling: touch !important;
    }

    /* Dual-column generators stack on tablet & mobile */
    .generator-app-grid,
    .exemplar-grid,
    .ilaw-grid,
    .ppt-grid,
    .co-maker-grid,
    #app > .generator-container,
    .app-layout-2col {
        display: flex !important;
        flex-direction: column !important;
        gap: 16px !important;
        height: auto !important;
        overflow: visible !important;
    }

    /* Generator panels with mobile-tab awareness */
    .generator-form-panel,
    .generator-preview-panel,
    #exemplar-form-panel,
    #exemplar-preview-panel,
    #ilaw-form-panel,
    #ilaw-preview-panel,
    #ppt-form-panel,
    #ppt-preview-panel,
    #ppt-input-panel,
    #ppt-result-panel,
    #co-form-panel,
    #co-right-panel,
    #co-preview-panel {
        width: 100% !important;
        max-width: 100% !important;
        height: auto !important;
        min-height: 0 !important;
        overflow-y: visible !important;
        overflow-x: visible !important;
    }

    /* When mobile tabs are active, hide the inactive panel */
    body.mobile-view-preview-active .generator-form-panel,
    body.mobile-view-preview-active #exemplar-form-panel,
    body.mobile-view-preview-active #ilaw-form-panel,
    body.mobile-view-preview-active #ppt-form-panel,
    body.mobile-view-preview-active #ppt-input-panel,
    body.mobile-view-preview-active #co-form-panel {
        display: none !important;
    }

    body.mobile-view-form-active .generator-preview-panel,
    body.mobile-view-form-active #exemplar-preview-panel,
    body.mobile-view-form-active #ilaw-preview-panel,
    body.mobile-view-form-active #ppt-preview-panel,
    body.mobile-view-form-active #ppt-result-panel,
    body.mobile-view-form-active #co-right-panel,
    body.mobile-view-form-active #co-preview-panel {
        display: none !important;
    }
}

/* --------------------------------------------------------------------------
   5. RESPONSIVE DEPED TABLES & OUTPUT CONTAINERS
   -------------------------------------------------------------------------- */
.table-responsive {
    width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    margin-bottom: 1rem;
    border-radius: 8px;
    border: 1px solid var(--border-color, #e2e8f0);
}

.table-responsive::-webkit-scrollbar {
    height: 6px;
}

.table-responsive::-webkit-scrollbar-thumb {
    background: #cbd5e1;
    border-radius: 3px;
}

/* Slide Deck responsive layout for PPT Generator */
@media (max-width: 768px) {
    .ppt-slide-card,
    .slide-preview-container {
        width: 100% !important;
        min-height: auto !important;
        padding: 16px !important;
    }

    .ppt-slide-deck-list {
        display: flex;
        flex-direction: column;
        gap: 16px;
    }

    .ppt-thumbnails-carousel {
        display: flex;
        overflow-x: auto;
        scroll-snap-type: x mandatory;
        gap: 10px;
        padding-bottom: 10px;
    }

    .ppt-thumbnail-item {
        scroll-snap-align: start;
        flex-shrink: 0;
        width: 140px;
    }
}

/* --------------------------------------------------------------------------
   6. STICKY MOBILE ACTION BARS (FAB)
   -------------------------------------------------------------------------- */
.mobile-sticky-action-bar {
    display: none;
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    background: var(--surface-color, #ffffff);
    padding: 10px 16px calc(var(--sab) + 10px) 16px;
    border-top: 1px solid var(--border-color, #e2e8f0);
    box-shadow: 0 -4px 16px rgba(0, 0, 0, 0.08);
    z-index: 90;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
}

@media (max-width: 768px) {
    .mobile-sticky-action-bar {
        display: flex;
    }
    
    /* Ensure content doesn't get clipped under the sticky bar */
    .has-mobile-action-bar {
        padding-bottom: calc(var(--sab) + 75px) !important;
    }
}

/* --------------------------------------------------------------------------
   7. CHIP ROWS & HORIZONTAL SCROLLING PICKERS
   -------------------------------------------------------------------------- */
.horizontal-chip-picker {
    display: flex;
    overflow-x: auto;
    white-space: nowrap;
    gap: 8px;
    padding: 6px 2px;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
}

.horizontal-chip-picker::-webkit-scrollbar {
    display: none;
}

.chip-option {
    display: inline-flex;
    align-items: center;
    padding: 8px 14px;
    border-radius: 50px;
    border: 1px solid var(--border-color, #e2e8f0);
    background: var(--surface-color, #ffffff);
    font-size: 0.85rem;
    font-weight: 500;
    color: var(--text-color, #1e293b);
    cursor: pointer;
    flex-shrink: 0;
    transition: all 0.2s ease;
}

.chip-option.active {
    background: var(--primary-color, #005c30);
    color: #ffffff;
    border-color: var(--primary-color, #005c30);
    box-shadow: 0 2px 6px rgba(0, 92, 48, 0.25);
}

/* --------------------------------------------------------------------------
   8. MOBILE RESPONSIVENESS: DASHBOARDS & PORTALS
   -------------------------------------------------------------------------- */
@media (max-width: 768px) {
    /* Navbar compact sizing */
    .navbar {
        padding: 8px 0;
    }

    .navbar .container {
        padding: 0 16px;
    }

    .mobile-menu-toggle {
        display: flex;
    }

    .nav-links,
    .desktop-nav-actions {
        display: none !important;
    }

    /* Container padding */
    .container {
        padding: 0 14px;
    }

    /* Hero section */
    .hero-section {
        padding: 2.5rem 0 1.5rem 0;
        text-align: center;
    }

    .hero-title {
        font-size: clamp(1.6rem, 6vw, 2.2rem) !important;
        line-height: 1.25 !important;
    }

    .hero-subtitle {
        font-size: 0.95rem !important;
    }

    .hero-actions {
        flex-direction: column;
        gap: 10px;
        width: 100%;
    }

    .hero-actions .btn {
        width: 100%;
    }

    /* Role selection cards & Grid items */
    .role-grid,
    .features-grid,
    .tools-grid,
    .stats-grid,
    .dashboard-grid {
        grid-template-columns: 1fr !important;
        gap: 14px !important;
    }

    /* Quick stats 2-col */
    .stats-summary-grid {
        grid-template-columns: repeat(2, 1fr) !important;
        gap: 10px !important;
    }

    /* Card styling */
    .card, 
    .feature-card, 
    .role-card,
    .tool-card {
        padding: 16px !important;
    }

    /* Modals become full-width bottom sheets */
    .modal-content,
    .dialog-box {
        width: 95% !important;
        max-width: 95% !important;
        margin: auto;
        padding: 18px !important;
        border-radius: 16px !important;
        max-height: 90vh !important;
        overflow-y: auto !important;
    }

    /* Quiz / Assessment Sticky Header & Sticky Buttons */
    .assessment-header,
    #assessment-header {
        position: sticky !important;
        top: 0 !important;
        top: env(safe-area-inset-top, 0px) !important;
        z-index: 1040 !important;
        padding: 0.5rem 0.65rem !important;
        margin-bottom: 0.85rem !important;
        display: flex !important;
        flex-direction: row !important;
        align-items: center !important;
        justify-content: space-between !important;
        gap: 0.4rem !important;
        border-radius: 12px !important;
        box-shadow: 0 4px 16px rgba(0, 0, 0, 0.1) !important;
        background: rgba(255, 255, 255, 0.96) !important;
        backdrop-filter: blur(12px) !important;
        -webkit-backdrop-filter: blur(12px) !important;
    }

    [data-theme="dark"] .assessment-header,
    [data-theme="dark"] #assessment-header {
        background: rgba(15, 23, 42, 0.96) !important;
    }

    /* Timer Button - Upper Left Corner */
    #sticky-timer,
    .quiz-timer {
        padding: 6px 12px !important;
        font-size: 0.88rem !important;
        font-weight: 700 !important;
        min-height: 36px !important;
        border-radius: 50px !important;
        background: linear-gradient(135deg, #2563eb, #1d4ed8) !important;
        color: white !important;
        box-shadow: 0 2px 6px rgba(37, 99, 235, 0.3) !important;
        flex-shrink: 0 !important;
        margin: 0 !important;
        gap: 4px !important;
    }

    /* Violation Counter - Upper Right Corner */
    #violation-counter-container {
        display: inline-flex !important;
        align-items: center !important;
        justify-content: flex-end !important;
        flex-shrink: 0 !important;
        margin: 0 !important;
    }

    #violation-counter-main {
        padding: 6px 12px !important;
        font-size: 0.88rem !important;
        font-weight: 700 !important;
        min-height: 36px !important;
        border-radius: 50px !important;
        background: linear-gradient(135deg, #ef4444, #dc2626) !important;
        color: white !important;
        box-shadow: 0 2px 6px rgba(239, 68, 68, 0.3) !important;
        flex-shrink: 0 !important;
        margin: 0 !important;
        cursor: pointer !important;
    }

    #violation-counter-collapsed {
        padding: 0 !important;
        gap: 4px !important;
        font-size: 0.88rem !important;
    }

    /* Assessment Header Title - Compact / Centered */
    .assessment-header-title {
        display: flex !important;
        align-items: center !important;
        justify-content: center !important;
        flex: 1 1 auto !important;
        min-width: 0 !important;
    }

    .assessment-header-title h3 {
        font-size: 0.9rem !important;
        margin: 0 !important;
        font-weight: 700 !important;
        white-space: nowrap !important;
        overflow: hidden !important;
        text-overflow: ellipsis !important;
    }

    /* Question Progress (Quiz Palette) - Scrollable & Compact */
    .quiz-palette-container {
        padding: 0.65rem 0.75rem !important;
        margin-bottom: 1rem !important;
        border-radius: 10px !important;
    }

    .quiz-palette-header {
        margin-bottom: 0.45rem !important;
    }

    .quiz-palette-grid {
        display: flex !important;
        flex-wrap: wrap !important;
        gap: 5px !important;
        max-height: 105px !important;
        overflow-y: auto !important;
        -webkit-overflow-scrolling: touch !important;
        overscroll-behavior-y: contain !important;
        padding: 3px 1px !important;
    }

    .quiz-palette-item {
        width: 35px !important;
        height: 35px !important;
        min-width: 35px !important;
        font-size: 0.82rem !important;
        border-radius: 6px !important;
    }

    .question-card {
        scroll-margin-top: 65px !important;
    }

    .question-options-list .option-item {
        padding: 12px 14px !important;
        font-size: 0.95rem !important;
    }

    /* Footer collapse */
    .footer-grid {
        grid-template-columns: 1fr !important;
        gap: 20px !important;
        text-align: center;
    }

    .footer-links {
        align-items: center;
    }
}

/* --------------------------------------------------------------------------
   9. EXTRA SMALL PHONES (< 380px)
   -------------------------------------------------------------------------- */
@media (max-width: 380px) {
    .hero-title {
        font-size: 1.45rem !important;
    }
    
    .stats-summary-grid {
        grid-template-columns: 1fr !important;
    }

    .quiz-palette-grid {
        gap: 4px !important;
        max-height: 95px !important;
    }
}

/* --------------------------------------------------------------------------
   10. PRINT STYLES - PRESERVE CRISP PRINTING
   -------------------------------------------------------------------------- */
@media print {
    .mobile-menu-toggle,
    .mobile-menu-overlay,
    .mobile-nav-drawer,
    .mobile-view-tabs,
    .mobile-sticky-action-bar {
        display: none !important;
    }
}
