/* Mobile Optimizations for Small Devices (Android Phones)
   Optimized for users in regions with limited computer access
   Designed for: Smartphones 320px-480px, Small tablets 480px-768px
*/

/* Base Mobile Reset */
@media (max-width: 768px) {
    /* Ensure proper touch targets */
    button, 
    a, 
    input, 
    select, 
    textarea,
    .btn,
    .clickable {
        min-height: 44px;
        min-width: 44px;
        -webkit-tap-highlight-color: rgba(52, 152, 219, 0.2);
    }

    /* Prevent zoom on form focus (iOS) */
    input, select, textarea {
        font-size: 16px !important;
    }

    /* Better touch scrolling */
    .scrollable,
    .overflow-scroll {
        -webkit-overflow-scrolling: touch;
        overflow-y: auto;
    }

    /* Prevent accidental zoom */
    * {
        touch-action: manipulation;
    }
}

/* Phone Portrait (Primary target: Android phones in Rwanda) */
@media (max-width: 480px) {
    /* Global spacing adjustments */
    .container,
    .wrapper,
    main,
    section {
        padding-left: 12px !important;
        padding-right: 12px !important;
    }

    /* Typography scaling */
    h1 { font-size: clamp(1.25rem, 5vw, 1.75rem) !important; }
    h2 { font-size: clamp(1.1rem, 4.5vw, 1.5rem) !important; }
    h3 { font-size: clamp(1rem, 4vw, 1.25rem) !important; }
    p, li, span { font-size: clamp(0.875rem, 3.5vw, 1rem); }

    /* Improve readability */
    body {
        line-height: 1.6;
        -webkit-text-size-adjust: 100%;
    }

    /* Form elements - Touch friendly */
    input[type="text"],
    input[type="number"],
    input[type="email"],
    input[type="password"],
    input[type="search"],
    select,
    textarea {
        min-height: 48px;
        padding: 12px;
        border-radius: 8px;
        border: 2px solid #ddd;
    }

    input[type="text"]:focus,
    input[type="number"]:focus,
    select:focus,
    textarea:focus {
        border-color: #3498db;
        outline: none;
    }

    /* Buttons - Large touch targets */
    button,
    .btn,
    input[type="submit"],
    input[type="button"] {
        min-height: 48px;
        padding: 12px 20px;
        font-size: 16px;
        border-radius: 8px;
        width: 100%;
        margin-bottom: 8px;
    }

    /* Range sliders - Easier to drag */
    input[type="range"] {
        height: 12px;
        -webkit-appearance: none;
        appearance: none;
        background: linear-gradient(to right, #e74c3c, #f39c12, #27ae60);
        border-radius: 6px;
    }

    input[type="range"]::-webkit-slider-thumb {
        -webkit-appearance: none;
        appearance: none;
        width: 28px;
        height: 28px;
        border-radius: 50%;
        background: #3498db;
        cursor: pointer;
        box-shadow: 0 2px 6px rgba(0,0,0,0.3);
    }

    /* Tables - Horizontal scroll with sticky header */
    table {
        display: block;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }

    /* Cards and panels - Full width */
    .card,
    .panel,
    .box {
        margin-left: -12px;
        margin-right: -12px;
        border-radius: 0;
    }

    /* Hide decorative elements to save bandwidth */
    .decoration,
    .ornament,
    .decorative-animation {
        display: none !important;
    }

    /* Plotly charts - Optimize for mobile */
    .js-plotly-plot,
    [class*="plotly"] {
        width: 100% !important;
    }

    /* Navigation - Mobile friendly */
    nav ul,
    .nav-list {
        flex-direction: column;
        gap: 8px;
    }

    nav li,
    .nav-item {
        width: 100%;
    }

    nav a,
    .nav-link {
        display: block;
        padding: 12px 16px;
        text-align: center;
    }
}

/* Very Small Screens (320px - older Android devices) */
@media (max-width: 360px) {
    .container,
    .wrapper,
    main,
    section {
        padding-left: 8px !important;
        padding-right: 8px !important;
    }

    h1 { font-size: 1.2rem !important; }
    h2 { font-size: 1.1rem !important; }
    h3 { font-size: 1rem !important; }

    button,
    .btn {
        padding: 10px 16px;
        font-size: 14px;
    }
}

/* Landscape phone optimization */
@media (max-width: 900px) and (orientation: landscape) {
    /* Reduce vertical spacing in landscape */
    .container,
    main {
        padding-top: 8px !important;
        padding-bottom: 8px !important;
    }

    /* Keep important content visible */
    header,
    .site-header {
        max-height: 50px;
    }
}

/* Performance optimizations for mobile */
@media (max-width: 768px) {
    /* Reduce animations for performance */
    *,
    *::before,
    *::after {
        animation-duration: 0.3s !important;
    }

    /* Optimize images */
    img {
        max-width: 100%;
        height: auto;
    }

    /* Reduce box-shadows for performance */
    .card,
    .panel,
    button,
    .btn {
        box-shadow: 0 2px 4px rgba(0,0,0,0.1);
    }
}

/* Respect user preferences */
@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
        scroll-behavior: auto !important;
    }
}

/* Dark mode for OLED screens (saves battery on AMOLED phones) */
@media (prefers-color-scheme: dark) {
    :root {
        --mobile-bg: #121212;
        --mobile-text: #e0e0e0;
        --mobile-surface: #1e1e1e;
    }
}

/* High contrast mode support */
@media (prefers-contrast: high) {
    button,
    .btn,
    a {
        border: 2px solid currentColor;
    }

    input,
    select,
    textarea {
        border: 2px solid #000;
    }
}

/* Print-friendly styles (for saving info) */
@media print {
    .no-print,
    nav,
    .nav,
    button,
    .btn,
    .controls {
        display: none !important;
    }

    body {
        font-size: 12pt;
        line-height: 1.4;
    }
}

/* Utility classes for mobile */
@media (max-width: 480px) {
    .hide-mobile { display: none !important; }
    .show-mobile { display: block !important; }
    .show-mobile-flex { display: flex !important; }
    .show-mobile-grid { display: grid !important; }
    
    .full-width-mobile { width: 100% !important; }
    .text-center-mobile { text-align: center !important; }
    
    .stack-mobile {
        flex-direction: column !important;
        align-items: stretch !important;
    }
}

@media (min-width: 481px) {
    .hide-desktop { display: none !important; }
    .show-desktop { display: block !important; }
}
/* =====================================================
   MAIN PAGE BOTTOM CONTROLS - ANDROID OPTIMIZATION
   ===================================================== */

/* Bottom controls layout for phones */
@media (max-width: 640px) {
    .bottom-controls {
        flex-direction: column;
        gap: 10px;
        padding: 10px;
        margin: 8px 0;
    }
    
    .bottom-left {
        width: 100%;
        justify-content: flex-start;
        gap: 8px;
    }
    
    /* Contact button moved to hamburger menu on phones */
    .bottom-left #contactBtn,
    .bottom-left .contact-trigger {
        display: none !important;
    }
    
    .bottom-right {
        width: 100%;
        flex-direction: column;
        align-items: stretch;
        gap: 8px;
    }
    
    /* Control cluster (Signal, Filter, Analyze, Ops) - grid layout */
    .bottom-controls .control-cluster {
        display: grid !important;
        grid-template-columns: repeat(4, 1fr);
        gap: 6px;
        width: 100%;
        padding: 0;
    }
    
    .bottom-controls .cc-group {
        width: 100%;
    }
    
    .bottom-controls .cc-btn {
        width: 100%;
        padding: 10px 6px;
        font-size: 11px;
        min-height: 44px;
        border-radius: 8px;
    }
    
    /* Files dropdown and utility buttons - row layout */
    .file-dropdown {
        display: none !important; /* Hide Files on mobile - not needed for phone users */
    }
    
    .file-dropdown .file-button {
        width: 100%;
        min-height: 44px;
    }
    
    .file-dropdown-content {
        width: 100%;
        left: 0;
        right: 0;
    }
    
    .file-dropdown-content button {
        width: 100%;
        min-height: 44px;
        padding: 12px;
    }
    
    /* Utility buttons row (Clear, Undo, Data Status) */
    .bottom-right > button.header-btn {
        flex: 1;
        min-height: 44px !important;
        font-size: 13px !important;
        padding: 10px 8px !important;
        width: 100%;
        box-sizing: border-box;
    }
    
    /* Make utility buttons (Clear, Undo, Data Status) in a row */
    .bottom-right {
        display: flex !important;
        flex-direction: column !important;
        gap: 8px;
    }
    
    /* Wrap utility buttons in a row */
    .utility-buttons-row {
        display: flex;
        gap: 6px;
        width: 100%;
    }
    
    .utility-buttons-row button {
        flex: 1;
    }
    
    /* CC Menu popup adjustments for mobile: open upward from tapped button */
    .bottom-controls .cc-menu {
        position: absolute !important;
        top: auto !important;
        bottom: calc(100% + 8px) !important;
        left: 50% !important;
        right: auto !important;
        transform: translateX(-50%) !important;
        max-width: 90vw;
        max-height: 58vh;
        overflow-y: auto;
        z-index: 10000;
    }

    /* Keep Signal panel fully inside phone viewport (override centered ID rules) */
    .bottom-controls .cc-group[data-group="signal"] .cc-menu,
    .bottom-controls #cc-signal-btn ~ .cc-menu {
        left: 0 !important;
        right: auto !important;
        transform: none !important;
        width: min(86vw, 320px);
        max-width: calc(100vw - 16px);
        max-height: 46vh;
        min-width: 0;
        padding: 8px 10px 10px;
    }

    /* Compact Filter panel for Android phones */
    .bottom-controls .cc-group[data-group="filter"] .cc-menu,
    .bottom-controls #cc-filter-btn ~ .cc-menu {
        left: 0 !important;
        right: auto !important;
        transform: none !important;
        width: min(78vw, 260px);
        max-width: calc(100vw - 16px);
        max-height: 42vh;
        min-width: 0;
        padding: 8px 10px 10px;
    }

    /* Compact Operations panel for Android phones */
    .bottom-controls .cc-group[data-group="ops"] .cc-menu,
    .bottom-controls #cc-ops-btn ~ .cc-menu {
        left: auto !important;
        right: 0 !important;
        transform: none !important;
        width: min(78vw, 260px);
        max-width: calc(100vw - 16px);
        max-height: 42vh;
        min-width: 0;
        padding: 8px 10px 10px;
    }
    
    /* Signal menu specific - better layout for form inputs */
    .cc-menu .cc-section {
        display: flex;
        flex-direction: column;
        gap: 8px;
    }
    
    .cc-menu .cc-section label {
        display: flex;
        justify-content: space-between;
        align-items: center;
        gap: 8px;
        font-size: 13px;
    }

    .bottom-controls .cc-group[data-group="signal"] .cc-section label {
        flex-direction: column;
        align-items: stretch;
        justify-content: flex-start;
        gap: 3px;
        margin-bottom: 4px;
        font-size: 12px;
    }
    
    .cc-menu .cc-section input,
    .cc-menu .cc-section select {
        flex: 1;
        min-height: 40px;
        padding: 8px;
        font-size: 14px;
        border-radius: 6px;
        border: 1px solid rgba(255,255,255,0.2);
        background: rgba(0,0,0,0.3);
        color: #fff;
    }

    .bottom-controls .cc-group[data-group="signal"] .cc-section input,
    .bottom-controls .cc-group[data-group="signal"] .cc-section select {
        width: 100%;
        max-width: 100%;
        min-height: 34px;
        padding: 6px 7px;
        font-size: 13px;
    }

    .bottom-controls .cc-group[data-group="signal"] .cc-section {
        gap: 5px;
    }

    .bottom-controls .cc-group[data-group="signal"] .cc-actions {
        gap: 6px;
        margin-top: 6px;
    }

    .bottom-controls .cc-group[data-group="signal"] .cc-actions button {
        min-height: 36px;
        padding: 6px 8px;
        font-size: 12px;
    }
    
    .cc-menu .cc-actions {
        display: flex;
        gap: 8px;
        margin-top: 8px;
    }
    
    .cc-menu .cc-actions button {
        flex: 1;
        min-height: 44px;
    }
    
    /* Filter, Analyze, Ops menu buttons - full width */
    .cc-menu button[role="menuitem"] {
        width: 100% !important;
        min-height: 44px;
        margin: 4px 0;
    }

    .bottom-controls .cc-group[data-group="filter"] .cc-menu button[role="menuitem"] {
        min-height: 36px;
        padding: 6px 8px;
        margin: 3px 0;
        font-size: 12px;
    }

    .bottom-controls .cc-group[data-group="ops"] .cc-menu button[role="menuitem"] {
        min-height: 36px;
        padding: 6px 8px;
        margin: 3px 0;
        font-size: 12px;
    }
}

/* Very small phones */
@media (max-width: 360px) {
    .bottom-controls .control-cluster {
        grid-template-columns: repeat(2, 1fr);
    }
    
    .bottom-controls .cc-btn {
        font-size: 10px;
        padding: 8px 4px;
    }
}

/* Landscape phone - more horizontal layout */
@media (max-width: 900px) and (orientation: landscape) {
    .bottom-controls {
        flex-direction: row;
        flex-wrap: wrap;
    }
    
    .bottom-right {
        flex-direction: row !important;
        flex-wrap: wrap;
    }
    
    .bottom-controls .control-cluster {
        display: flex !important;
        width: auto;
    }
    
    .bottom-controls .cc-menu {
        bottom: calc(100% + 8px) !important;
        top: auto !important;
        transform: translateX(-50%) !important;
    }

    .bottom-controls .cc-group[data-group="ops"] .cc-menu,
    .bottom-controls #cc-ops-btn ~ .cc-menu {
        left: auto !important;
        right: 0 !important;
        transform: none !important;
    }
}

/* Utility buttons row - all screens */
.utility-buttons-row {
    display: flex;
    gap: 6px;
    align-items: center;
}

.utility-buttons-row button.header-btn {
    font-size: 13px;
    padding: 4px 10px;
    white-space: nowrap;
}

/* Utility buttons row - mobile */
@media (max-width: 640px) {
    .utility-buttons-row {
        width: 100%;
    }
    
    .utility-buttons-row button.header-btn {
        flex: 1;
        min-height: 44px !important;
        padding: 10px 6px !important;
        font-size: 12px !important;
    }
}