/* Mobile UI Improvements - Cross-browser tested */
/* Optimized for iOS Safari, Chrome, Firefox Mobile */

/* =========================
   ENHANCED TOUCH TARGETS
   ========================= */

@media (max-width: 600px) {
    /* Minimum 44x44px touch targets per Apple HIG and Material Design */
    .icon-btn, .pill-btn, .zoom-btn {
        min-width: 44px;
        min-height: 44px;
        padding: 10px;
    }
    
    .footer-icon-btn {
        width: 44px;
        height: 44px;
        min-width: 44px;
        min-height: 44px;
    }
    
    .sel-icon-btn {
        width: 48px;
        height: 48px;
        min-width: 48px;
        min-height: 48px;
    }
    
    /* Larger FAB for easier thumb reach */
    .fab-btn {
        width: 64px;
        height: 64px;
    }
    
    .fab-btn .material-symbols-outlined {
        font-size: 28px;
    }
}

/* =========================
   IMPROVED SPACING & LAYOUT
   ========================= */

@media (max-width: 600px) {
    /* Better thumb zone positioning - keep UI in reachable areas */
    #ui-menu {
        top: max(env(safe-area-inset-top, 0px) + 12px, 12px);
        left: 12px;
    }
    
    #ui-mode {
        top: max(env(safe-area-inset-top, 0px) + 12px, 12px);
        /* Slightly smaller on mobile to fit */
        transform: translateX(-50%) scale(0.95);
    }
    
    #ui-history {
        top: max(env(safe-area-inset-top, 0px) + 12px, 12px);
        right: 12px;
    }
    
    #ui-tools {
        top: max(env(safe-area-inset-top, 0px) + 72px, 72px);
        right: 12px;
    }
    
    /* Zoom controls - better bottom positioning */
    #ui-zoom {
        bottom: max(env(safe-area-inset-bottom, 0px) + 20px, 20px);
        left: 50%;
        transform: translateX(-50%);
        padding: 6px 10px;
        max-width: calc(100vw - 24px);
    }
    
    /* Sync indicator - avoid FAB collision */
    #sync-indicator {
        bottom: max(env(safe-area-inset-bottom, 0px) + 90px, 90px);
        right: 20px;
        width: 50px;
        height: 50px;
    }
    
    /* Selection bar - better spacing from bottom */
    #selection-bar {
        bottom: max(env(safe-area-inset-bottom, 0px) + 25px, 25px);
        width: calc(100% - 32px);
        max-width: 380px;
    }
}

/* =========================
   IMPROVED READABILITY
   ========================= */

@media (max-width: 600px) {
    /* Slightly larger font sizes for better readability */
    .dashboard-header h1 {
        font-size: 1.5rem;
    }
    
    .card-title {
        font-size: 0.9rem !important; /* Compact title */
    }
    
    .card-meta {
        font-size: 0.75rem !important; /* Compact meta */
    }
    
    .prop-label {
        font-size: 0.7rem;
    }
    
    /* Better contrast for small text */
    .picker-file-meta,
    .stat-label,
    .queue-item-time {
        font-size: 0.75rem;
        color: #999;
    }
}

/* =========================
   OPTIMIZED SIDEBAR
   ========================= */

@media (max-width: 600px) {
    /* Sidebar doesn't take full width - better for one-handed use */
    #sidebar {
        width: min(85vw, 320px);
        max-width: 320px;
    }
    
    /* Larger touch targets in sidebar */
    .btn {
        padding: 14px;
        font-size: 0.95rem;
        min-height: 48px;
    }
    
    .gallery-item {
        padding: 10px 12px;
        min-height: 56px;
    }
    
    .gallery-item img {
        width: 36px;
        height: 36px;
    }
    
    /* Better folder tap targets */
    summary {
        padding: 10px 8px;
        min-height: 44px;
    }
}

/* =========================
   MODAL & DIALOG IMPROVEMENTS
   ========================= */

@media (max-width: 600px) {
    /* Better dialog sizing */
    .dialog-box {
        width: 92%;
        max-width: 380px;
        padding: 20px;
        margin: 0 16px;
    }
    
    .dialog-title {
        font-size: 1.15rem;
        margin-bottom: 12px;
    }
    
    .dialog-message {
        font-size: 0.95rem;
        line-height: 1.6;
    }
    
    .dialog-input {
        padding: 14px;
        font-size: 1rem; /* Prevents iOS zoom on focus */
        border-radius: 8px;
    }
    
    .dialog-buttons {
        flex-direction: column;
        gap: 10px;
    }
    
    .dialog-btn {
        width: 100%;
        padding: 14px;
        font-size: 1rem;
    }
    
    /* Settings modal optimization */
    .modal-box {
        width: 92%;
        max-width: 380px;
        padding: 20px;
    }
    
    .setting-row {
        margin-bottom: 24px;
    }
}

/* =========================
   DASHBOARD MOBILE OPTIMIZATIONS
   ========================= */

@media (max-width: 600px) {
    .dashboard-header {
        padding: 16px;
        padding-top: max(env(safe-area-inset-top, 0px) + 16px, 16px);
        gap: 12px;
    }
    
    .dashboard-body {
        padding: 12px; /* Reduced padding for compact grid */
    }
    
    /* COMPACT GRID: 2 Columns */
    .projects-grid {
        grid-template-columns: repeat(2, 1fr) !important;
        gap: 10px !important;
    }
    
    .project-card {
        border-radius: 12px;
    }
    
    /* COMPACT THUMBNAIL: Shorter fixed height */
    .card-thumbnail {
        height: 100px !important;
        padding-top: 0 !important;
    }
    
    /* COMPACT BODY */
    .card-body {
        padding: 8px !important;
    }
    
    .card-actions {
        padding: 6px 8px !important;
        gap: 6px !important;
    }
    
    .btn-primary {
        padding: 6px 10px !important;
        font-size: 0.8rem !important;
        min-height: 32px !important;
    }
    
    .btn-icon {
        width: 32px !important;
        height: 32px !important;
        min-width: 32px !important;
    }
    
    /* Source tabs - better mobile layout */
    .source-tabs {
        gap: 0;
        margin-bottom: 15px;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        scrollbar-width: none;
    }
    
    .source-tabs::-webkit-scrollbar {
        display: none;
    }
    
    .source-tab {
        flex: 1;
        min-width: 90px;
        padding: 12px 10px;
        font-size: 0.85rem;
        justify-content: center;
        white-space: nowrap;
    }
    
    /* FAB positioning - thumb zone optimized */
    .dashboard-fab {
        bottom: max(env(safe-area-inset-bottom, 0px) + 20px, 20px);
        right: 20px;
    }
}

/* =========================
   ENHANCED TEXT INPUT
   ========================= */

@media (max-width: 600px) {
    .live-textarea {
        font-size: 16px; /* Prevents iOS zoom */
        padding: 8px;
        border-width: 2px;
        border-radius: 8px;
        min-width: 50px;
        min-height: 40px;
    }
    
    #ui-text-props {
        top: max(env(safe-area-inset-top, 0px) + 140px, 140px);
        right: 12px;
        width: min(90vw, 200px);
        padding: 12px;
    }
    
    .style-grp button {
        padding: 10px;
        min-height: 40px;
    }
    
    .swatch {
        width: 32px;
        height: 32px;
    }
}

/* =========================
   IMPROVED PICKER/FILE DIALOGS
   ========================= */

@media (max-width: 600px) {
    .enhanced-picker {
        width: 100%;
        max-width: 100%;
        height: 100vh;
        max-height: 100vh;
        border-radius: 0;
    }
    
    .picker-header {
        padding: 16px;
        padding-top: max(env(safe-area-inset-top, 0px) + 16px, 16px);
    }
    
    .picker-controls {
        padding: 14px 16px;
    }
    
    #picker-search {
        padding: 12px 12px 12px 42px;
        font-size: 1rem; /* Prevents zoom */
    }
    
    .picker-file-item {
        padding: 12px;
        margin-bottom: 10px;
        min-height: 68px;
    }
    
    .picker-file-thumbnail {
        width: 52px;
        height: 52px;
    }
    
    .picker-action-btn {
        width: 40px;
        height: 40px;
    }
    
    .picker-footer {
        padding: 14px 16px;
        padding-bottom: max(env(safe-area-inset-bottom, 0px) + 14px, 14px);
    }
}

/* =========================
   CONTEXT MENU MOBILE
   ========================= */

@media (max-width: 600px) {
    .context-menu {
        min-width: 220px;
        padding: 5px;
        border-radius: 16px;
        /* Ensure it's reachable */
        z-index: 2000 !important;
    }
    
    .menu-item {
        padding: 16px 20px;
        border-radius: 10px;
        font-size: 1rem;
        min-height: 50px;
    }
    
    .menu-item .material-symbols-outlined {
        font-size: 24px;
    }
}

/* =========================
   SYNC QUEUE PANEL MOBILE
   ========================= */

@media (max-width: 600px) {
    .sync-queue-panel {
        width: 100vw;
        right: -100vw;
        border-radius: 0;
    }
    
    .queue-header {
        padding: 16px;
        padding-top: max(env(safe-area-inset-top, 0px) + 16px, 16px);
    }
    
    .queue-content {
        padding: 16px;
    }
    
    .queue-actions {
        padding: 14px 16px;
        padding-bottom: max(env(safe-area-inset-bottom, 0px) + 14px, 14px);
    }
}

/* =========================
   IMPROVED LANDSCAPE MODE
   ========================= */

@media (max-width: 900px) and (orientation: landscape) {
    /* Compact UI for landscape */
    .float-pill {
        padding: 3px;
        gap: 3px;
    }
    
    .icon-btn, .pill-btn {
        width: 38px;
        height: 38px;
        min-width: 38px;
        min-height: 38px;
        padding: 7px;
    }
    
    #ui-mode {
        top: 10px;
    }
    
    #ui-zoom {
        bottom: 15px;
        padding: 4px 8px;
    }
    
    #sync-indicator {
        width: 44px;
        height: 44px;
        bottom: 15px;
        right: 15px;
    }
}

/* =========================
   REDUCED MOTION SUPPORT
   ========================= */

@media (prefers-reduced-motion: reduce) {
    * {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }
}

/* =========================
   HIGH CONTRAST MODE
   ========================= */

@media (prefers-contrast: high) {
    .project-card,
    .gallery-item,
    .btn {
        border-width: 2px;
    }
    
    .icon-btn,
    .pill-btn {
        border: 2px solid currentColor;
    }
}

/* =========================
   DARK MODE FIXES (if needed)
   ========================= */

@media (prefers-color-scheme: dark) {
    /* Already dark by default, but ensuring consistency */
    .dialog-box,
    .modal-box,
    .context-menu {
        background: var(--secondary);
        border-color: var(--border);
    }
}

/* =========================
   TABLET OPTIMIZATIONS
   ========================= */

@media (min-width: 601px) and (max-width: 1024px) {
    /* Tablet-specific adjustments */
    .projects-grid {
        grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
        gap: 20px;
    }
    
    #sidebar {
        width: 340px;
    }
    
    .dashboard-header {
        padding: 20px 24px;
    }
    
    .dashboard-body {
        padding: 20px 24px;
    }
}

/* =========================
   IPHONE NOTCH SUPPORT
   ========================= */

@supports (padding: max(0px)) {
    @media (max-width: 600px) {
        /* Additional notch support for modern iPhones */
        .dashboard-header,
        .picker-header,
        .queue-header {
            padding-top: max(env(safe-area-inset-top, 16px) + 16px, 16px);
        }
        
        .sidebar-footer,
        .picker-footer,
        .queue-actions {
            padding-bottom: max(env(safe-area-inset-bottom, 16px) + 16px, 16px);
        }
        
        #ui-zoom,
        #selection-bar,
        .dashboard-fab {
            bottom: max(env(safe-area-inset-bottom, 20px) + 20px, 20px);
        }
    }
}

/* =========================
   FOCUS VISIBLE (Accessibility)
   ========================= */

button:focus-visible,
input:focus-visible,
select:focus-visible {
    outline: 3px solid var(--accent);
    outline-offset: 2px;
}

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

@media (max-width: 600px) {
    /* Reduce blur on mobile for better performance */
    .float-pill,
    #sidebar,
    #dialog-overlay,
    #modal-overlay {
        -webkit-backdrop-filter: blur(6px);
        backdrop-filter: blur(6px);
    }
    
    /* Hardware acceleration hints */
    .project-card,
    .gallery-item,
    .icon-btn,
    .pill-btn {
        will-change: transform;
        transform: translateZ(0);
    }
}
