/* ===================================
   MOBILE RESPONSIVE CSS - ULTRA HIGH PRIORITY
   This file MUST load AFTER all other CSS
   =================================== */

/* Prevent horizontal scroll and zooming */
html {
    -webkit-text-size-adjust: 100%;
    -ms-text-size-adjust: 100%;
    overflow-x: hidden !important;
}

body {
    max-width: 100vw !important;
    overflow-x: hidden !important;
}

/* ========== TABLET: 1024px ========== */
@media screen and (max-width: 1024px) {

    .test-grid,
    [class*="grid-cols"] {
        grid-template-columns: repeat(2, 1fr) !important;
    }
}

/* ========== MOBILE: 768px - MAIN BREAKPOINT ========== */
@media screen and (max-width: 768px) {

    /* === CRITICAL: Prevent overflow === */
    body.cbt-wrapper,
    #cbtScreen,
    .cbt-wrapper {
        width: 100vw !important;
        max-width: 100vw !important;
        overflow-x: hidden !important;
    }

    /* === TEST PAGE HEADER === */
    .cbt-header {
        height: auto !important;
        min-height: 56px !important;
        padding: 8px 12px !important;
        flex-wrap: wrap !important;
        gap: 8px !important;
    }

    .header-left,
    .header-right {
        gap: 8px !important;
    }

    .test-timer {
        font-size: 0.85rem !important;
        padding: 6px 10px !important;
    }

    .btn-back {
        font-size: 0.75rem !important;
        padding: 6px 10px !important;
    }

    /* === MAIN LAYOUT TRANSFORMATION === */
    .cbt-main {
        flex-direction: column !important;
        height: auto !important;
        overflow: visible !important;
    }

    /* === QUESTION AREA === */
    .question-area {
        width: 100% !important;
        height: auto !important;
        min-height: 0 !important;
        border-right: none !important;
        overflow-y: visible !important;
    }

    /* Section tabs - horizontal scroll */
    .section-tabs-container {
        padding: 0 8px !important;
        overflow-x: auto !important;
        -webkit-overflow-scrolling: touch;
        scrollbar-width: none;
    }

    .section-tabs-container::-webkit-scrollbar {
        display: none;
    }

    .section-tabs {
        display: inline-flex !important;
        min-width: min-content !important;
        gap: 4px !important;
    }

    .section-tab {
        padding: 12px 16px !important;
        font-size: 0.8rem !important;
        white-space: nowrap !important;
    }

    /* Question header */
    .question-header {
        padding: 12px 16px !important;
        flex-direction: row !important;
        flex-wrap: wrap !important;
        gap: 8px !important;
    }

    .question-info,
    .question-meta {
        flex: 1 1 auto !important;
    }

    .question-label {
        font-size: 1rem !important;
    }

    .question-type {
        font-size: 0.7rem !important;
        padding: 4px 10px !important;
    }

    /* Question content */
    .question-content {
        padding: 16px !important;
        max-height: none !important;
        overflow-y: visible !important;
    }

    .question-images {
        max-width: 100% !important;
        gap: 12px !important;
    }

    .question-images img {
        max-height: 400px !important;
    }

    /* Answer options */
    .answer-options {
        padding: 16px !important;
        margin: 20px 0 0 0 !important;
        max-width: 100% !important;
    }

    .answer-option {
        padding: 12px 14px !important;
        margin-bottom: 8px !important;
    }

    .option-letter {
        width: 26px !important;
        height: 26px !important;
        font-size: 0.8rem !important;
    }

    /* NAT Input */
    .nat-input {
        max-width: 100% !important;
        font-size: 1.1rem !important;
    }

    /* === NAVIGATION CONTROLS - CRITICAL === */
    .navigation-controls {
        position: sticky !important;
        bottom: 0 !important;
        left: 0 !important;
        right: 0 !important;
        width: 100% !important;
        background: white !important;
        z-index: 999 !important;
        box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.1) !important;
        padding: 10px !important;
        flex-direction: column !important;
        gap: 8px !important;
        border-top: 2px solid #e2e8f0 !important;
    }

    .nav-left,
    .nav-right {
        width: 100% !important;
        display: flex !important;
        gap: 8px !important;
    }

    .nav-left {
        flex-direction: column !important;
    }

    .nav-left .btn,
    .nav-right .btn {
        flex: 1 !important;
        padding: 12px 8px !important;
        font-size: 0.8rem !important;
        white-space: nowrap !important;
        min-height: 44px !important;
    }

    /* === QUESTION PALETTE - MOVE TO BOTTOM === */
    .question-palette {
        width: 100% !important;
        height: auto !important;
        max-height: 400px !important;
        position: relative !important;
        border-top: 2px solid #e2e8f0 !important;
        border-left: none !important;
        order: 10 !important;
    }

    .palette-header {
        padding: 12px 16px !important;
    }

    .palette-title {
        font-size: 0.85rem !important;
    }

    .palette-section {
        font-size: 0.95rem !important;
    }

    /* Status legend - 2 column grid */
    .status-legend {
        display: grid !important;
        grid-template-columns: repeat(2, 1fr) !important;
        gap: 6px !important;
        padding: 10px 12px !important;
    }

    .legend-item {
        font-size: 0.7rem !important;
        gap: 4px !important;
    }

    .legend-text {
        font-size: 0.7rem !important;
    }

    .status-icon {
        width: 22px !important;
        height: 22px !important;
        font-size: 0.7rem !important;
        min-width: 22px !important;
    }

    /* Question grid */
    .question-grid-container {
        padding: 12px 16px !important;
        max-height: 300px !important;
        overflow-y: auto !important;
    }

    .question-grid {
        grid-template-columns: repeat(auto-fill, minmax(44px, 1fr)) !important;
        gap: 6px !important;
    }

    .question-number {
        min-height: 44px !important;
        font-size: 0.85rem !important;
        aspect-ratio: 1 !important;
    }

    /* Submit button */
    .submit-container {
        padding: 12px 16px !important;
    }

    .btn-submit {
        width: 100% !important;
        padding: 14px !important;
        font-size: 1rem !important;
        min-height: 48px !important;
    }

    /* === MODALS === */
    #summaryModal {
        padding: 10px !important;
    }

    #summaryModal .bg-white {
        width: 95% !important;
        max-width: 95% !important;
        margin: 10px auto !important;
        max-height: 85vh !important;
        overflow-y: auto !important;
    }

    #summaryModal .p-6 {
        padding: 1rem !important;
    }

    #summaryModal .p-4 {
        padding: 0.875rem !important;
    }

    #summaryModal h2 {
        font-size: 1.25rem !important;
    }

    #summaryModal .text-3xl {
        font-size: 1.5rem !important;
    }

    /* Summary modal grids */
    #summaryModal .grid-cols-2,
    #summaryModal .grid-cols-4,
    #summaryModal .md\:grid-cols-4 {
        grid-template-columns: repeat(2, 1fr) !important;
        gap: 10px !important;
    }

    /* Modal buttons */
    #summaryModal button {
        padding: 12px 16px !important;
        font-size: 0.9rem !important;
    }

    /* === DASHBOARD PAGE === */
    .scorex-header {
        padding: 1rem 1rem !important;
    }

    .main-content {
        padding: 1.5rem 1rem !important;
        margin-left: 0 !important;
    }

    .max-w-7xl,
    .max-w-6xl,
    .max-w-5xl,
    .max-w-4xl {
        padding-left: 1rem !important;
        padding-right: 1rem !important;
    }

    /* Welcome banner */
    .welcome-banner {
        padding: 1.5rem !important;
        margin-bottom: 1.5rem !important;
        border-radius: 16px !important;
    }

    .welcome-title {
        font-size: 1.5rem !important;
    }

    .welcome-subtitle {
        font-size: 0.95rem !important;
    }

    /* Brand elements */
    .brand-name {
        font-size: 1rem !important;
    }

    .brand-mark {
        width: 36px !important;
        height: 36px !important;
    }

    /* Tab navigation */
    .tab-nav-container {
        overflow-x: auto !important;
        -webkit-overflow-scrolling: touch;
        margin-bottom: 1.5rem !important;
    }

    .tab-nav {
        padding: 0.4rem !important;
        display: inline-flex !important;
        min-width: min-content !important;
    }

    .tab-btn {
        padding: 0.7rem 1.1rem !important;
        font-size: 0.8rem !important;
        white-space: nowrap !important;
    }

    /* Category filters */
    .category-filters {
        overflow-x: auto !important;
        -webkit-overflow-scrolling: touch;
        gap: 0.5rem !important;
    }

    .category-pill {
        padding: 0.5rem 0.9rem !important;
        font-size: 0.75rem !important;
        white-space: nowrap !important;
    }

    /* Test grid */
    .test-grid,
    .series-grid,
    .grid-cols-4,
    .grid-cols-3,
    .grid-cols-2,
    .md\:grid-cols-4,
    .md\:grid-cols-3,
    .md\:grid-cols-2 {
        grid-template-columns: 1fr !important;
        gap: 1.25rem !important;
    }

    .test-card,
    .series-card {
        padding: 1.25rem !important;
    }

    /* === RESULTS PAGE === */
    .results-container {
        padding: 1.5rem 1rem !important;
    }

    .results-header {
        padding: 1.5rem 1rem !important;
        flex-direction: column !important;
        align-items: flex-start !important;
        gap: 1rem !important;
    }

    .results-header h1 {
        font-size: 1.5rem !important;
    }

    .card-glass {
        padding: 1rem !important;
    }

    /* Score card */
    .card.md\:col-span-2 {
        padding: 1.5rem !important;
    }

    .text-6xl {
        font-size: 2.5rem !important;
    }

    .text-5xl {
        font-size: 2rem !important;
    }

    .text-4xl {
        font-size: 1.75rem !important;
    }

    .text-3xl {
        font-size: 1.5rem !important;
    }

    .text-2xl {
        font-size: 1.25rem !important;
    }

    .text-xl {
        font-size: 1.1rem !important;
    }

    /* Stat cards */
    .stat-card {
        padding: 1.25rem !important;
    }

    .grid.grid-rows-3 {
        grid-template-rows: auto !important;
        grid-template-columns: 1fr !important;
    }

    /* Tables */
    .review-table-container {
        overflow-x: auto !important;
        -webkit-overflow-scrolling: touch;
        margin: 0 -1rem !important;
        padding: 0 1rem !important;
    }

    .review-table {
        min-width: 600px !important;
    }

    .review-table th,
    .review-table td {
        padding: 10px 12px !important;
        font-size: 0.85rem !important;
    }

    /* === ADMIN PAGE === */
    .sidebar {
        position: relative !important;
        width: 100% !important;
        min-height: auto !important;
        border-bottom: 2px solid #e2e8f0;
    }

    .sidebar .p-6 {
        padding: 1.25rem !important;
    }

    .sidebar nav {
        display: flex !important;
        overflow-x: auto !important;
        -webkit-overflow-scrolling: touch;
        margin-top: 0 !important;
    }

    .nav-item {
        white-space: nowrap !important;
        padding: 12px 16px !important;
    }

    /* === FORMS & INPUTS === */
    input[type="text"],
    input[type="email"],
    input[type="password"],
    input[type="number"],
    input[type="tel"],
    textarea,
    select,
    .input-field {
        font-size: 16px !important;
        padding: 12px 14px !important;
    }

    /* === BUTTONS === */
    .btn {
        padding: 10px 16px !important;
        font-size: 0.9rem !important;
        min-height: 44px !important;
    }

    .btn-sm {
        padding: 8px 12px !important;
        font-size: 0.85rem !important;
        min-height: 40px !important;
    }

    .scorex-btn {
        padding: 0.8em 1.2em !important;
        font-size: 0.85rem !important;
    }

    /* === HIDE DECORATIVE ELEMENTS === */
    .bg-text,
    .bg-text-1,
    .bg-text-2,
    .diagonal-stripe {
        display: none !important;
    }

    /* === HERO SECTION === */
    .hero-gradient {
        padding: 6rem 1rem 3rem !important;
    }

    .hero-title {
        font-size: 2rem !important;
    }

    .stats-grid {
        grid-template-columns: repeat(2, 1fr) !important;
        gap: 1rem !important;
    }

    .features-grid {
        grid-template-columns: 1fr !important;
    }

    .footer-content {
        flex-direction: column !important;
        text-align: center !important;
    }

    /* Utility padding/margin overrides */
    .px-4 {
        padding-left: 1rem !important;
        padding-right: 1rem !important;
    }

    .px-6 {
        padding-left: 1rem !important;
        padding-right: 1rem !important;
    }

    .px-8 {
        padding-left: 1rem !important;
        padding-right: 1rem !important;
    }

    .py-8 {
        padding-top: 1.5rem !important;
        padding-bottom: 1.5rem !important;
    }

    .py-12 {
        padding-top: 2rem !important;
        padding-bottom: 2rem !important;
    }

    .gap-6 {
        gap: 1.25rem !important;
    }

    .gap-8 {
        gap: 1.5rem !important;
    }
}

/* ========== SMALL MOBILE: 480px ========== */
@media screen and (max-width: 480px) {

    body {
        font-size: 14px !important;
    }

    /* Header elements */
    .cbt-header {
        padding: 6px 10px !important;
    }

    .test-timer {
        font-size: 0.75rem !important;
        padding: 5px 8px !important;
    }

    .btn-back {
        font-size: 0.7rem !important;
        padding: 5px 8px !important;
    }

    /* Brand */
    .brand-name {
        font-size: 0.9rem !important;
    }

    .brand-mark {
        width: 32px !important;
        height: 32px !important;
    }

    /* Typography */
    .welcome-title,
    .hero-title {
        font-size: 1.35rem !important;
    }

    .text-6xl {
        font-size: 2rem !important;
    }

    .text-5xl {
        font-size: 1.75rem !important;
    }

    .text-4xl {
        font-size: 1.5rem !important;
    }

    .text-3xl {
        font-size: 1.25rem !important;
    }

    .text-2xl {
        font-size: 1.1rem !important;
    }

    /* Section tabs */
    .section-tab {
        padding: 10px 12px !important;
        font-size: 0.75rem !important;
    }

    /* Question grid */
    .question-grid {
        grid-template-columns: repeat(auto-fill, minmax(40px, 1fr)) !important;
        gap: 5px !important;
    }

    .question-number {
        min-height: 40px !important;
        font-size: 0.75rem !important;
    }

    /* Navigation buttons - full width stack */
    .nav-right {
        flex-direction: column !important;
    }

    .nav-right .btn {
        width: 100% !important;
    }

    /* Summary modal - single column */
    #summaryModal .grid-cols-2,
    #summaryModal .grid-cols-4 {
        grid-template-columns: 1fr !important;
    }

    /* Stats grid - single column */
    .stats-grid {
        grid-template-columns: 1fr !important;
    }

    /* Tab navigation */
    .tab-nav {
        width: 100% !important;
        justify-content: space-around !important;
        flex-wrap: wrap !important;
    }

    .tab-btn {
        flex: 1 !important;
        min-width: 110px !important;
        padding: 0.65rem 0.9rem !important;
    }

    /* Cards */
    .welcome-banner,
    .test-card,
    .card {
        padding: 1rem !important;
        border-radius: 12px !important;
    }

    /* Main content */
    .main-content {
        padding: 1rem 0.75rem !important;
    }
}

/* ========== EXTRA SMALL: 390px ========== */
@media screen and (max-width: 390px) {

    .brand-name {
        font-size: 0.85rem !important;
        letter-spacing: 0.03em !important;
    }

    .logout-btn span,
    .btn-back span,
    .hidden {
        display: none !important;
    }

    .question-grid {
        grid-template-columns: repeat(auto-fill, minmax(36px, 1fr)) !important;
    }

    .question-number {
        min-height: 36px !important;
        font-size: 0.7rem !important;
    }

    .answer-option {
        padding: 10px 12px !important;
    }

    .option-letter {
        width: 24px !important;
        height: 24px !important;
        font-size: 0.75rem !important;
    }
}

/* ========== TOUCH DEVICES ========== */
@media (hover: none) and (pointer: coarse) {

    /* Disable hover effects */
    .question-number:hover,
    .test-card:hover,
    .series-card:hover,
    .stat-card:hover,
    .card:hover,
    .btn:hover {
        transform: none !important;
    }

    /* Add active/touch states */
    .btn:active,
    .question-number:active {
        opacity: 0.7 !important;
        transform: scale(0.97) !important;
    }

    /* Ensure all interactive elements have proper touch targets */
    a,
    button,
    .btn,
    .question-number,
    .section-tab,
    .tab-btn,
    .category-pill,
    input[type="radio"]+label,
    input[type="checkbox"]+label {
        min-height: 44px !important;
        min-width: 44px !important;
    }
}

/* ========== LANDSCAPE MOBILE ========== */
@media screen and (max-width: 900px) and (orientation: landscape) {

    .cbt-header {
        height: 48px !important;
        min-height: 48px !important;
        padding: 6px 12px !important;
    }

    .question-area {
        min-height: calc(100vh - 48px) !important;
    }

    .question-content {
        padding: 12px !important;
    }

    .question-grid-container {
        max-height: 250px !important;
    }

    .navigation-controls {
        padding: 8px !important;
    }

    .welcome-banner {
        padding: 1.25rem !important;
    }

    .hero-gradient {
        padding: 4rem 1rem 2rem !important;
    }
}

/* ========== PRINT ========== */
@media print {

    .cbt-header,
    .navigation-controls,
    .question-palette,
    .btn,
    .theme-toggle,
    .logout-btn,
    .sidebar,
    .status-legend,
    .submit-container {
        display: none !important;
    }

    .question-area {
        border: none !important;
    }

    .main-content {
        margin-left: 0 !important;
    }
}