/**
 * GOD-LEVEL MOBILE OPTIMIZATION
 * Ultra-fast, smooth, native-like experience
 */

/* ===== PERFORMANCE OPTIMIZATIONS ===== */
* {
    -webkit-tap-highlight-color: transparent;
    -webkit-touch-callout: none;
}

html {
    -webkit-text-size-adjust: 100%;
    -ms-text-size-adjust: 100%;
    touch-action: manipulation;
}

body {
    overscroll-behavior-y: contain;
    -webkit-overflow-scrolling: touch;
}

/* Hardware acceleration for smooth scrolling */
.video-grid,
.video-card,
.thumbnail-container {
    transform: translateZ(0);
    will-change: transform;
    backface-visibility: hidden;
}

/* ===== MOBILE-FIRST LAYOUT ===== */
@media (max-width: 768px) {
    /* Optimize grid for mobile */
    .video-grid {
        grid-template-columns: repeat(2, 1fr) !important;
        gap: 8px !important;
        padding: 8px !important;
    }
    
    /* Larger touch targets */
    .video-card {
        min-height: 44px;
        cursor: pointer;
        -webkit-user-select: none;
        user-select: none;
    }
    
    /* Optimize thumbnail loading */
    .thumbnail-container {
        position: relative;
        aspect-ratio: 16/9;
        background: #1a1a1a;
        overflow: hidden;
    }
    
    .thumbnail-container img {
        width: 100%;
        height: 100%;
        object-fit: cover;
        loading: lazy;
        decoding: async;
    }
    
    /* Smooth transitions */
    .video-card:active {
        transform: scale(0.98);
        transition: transform 0.1s ease;
    }
    
    /* Optimize text rendering */
    .video-title {
        font-size: 13px !important;
        line-height: 1.3 !important;
        max-height: 2.6em !important;
        overflow: hidden;
        text-overflow: ellipsis;
        display: -webkit-box;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
        word-break: break-word;
    }
    
    .video-meta {
        font-size: 11px !important;
        margin-top: 4px !important;
    }
    
    /* Optimize badges */
    .duration-badge,
    .quality-badge {
        font-size: 10px !important;
        padding: 2px 4px !important;
        border-radius: 2px !important;
    }
    
    /* Hide unnecessary elements on mobile */
    .preview-sprite,
    .hover-overlay {
        display: none !important;
    }
    
    /* Optimize header */
    .header {
        position: sticky;
        top: 0;
        z-index: 1000;
        background: #000;
        padding: 8px !important;
        box-shadow: 0 2px 4px rgba(0,0,0,0.3);
    }
    
    .logo {
        font-size: 20px !important;
    }
    
    /* Optimize search */
    .search-container {
        width: 100% !important;
        max-width: none !important;
    }
    
    .search-input {
        font-size: 16px !important; /* Prevents zoom on iOS */
        padding: 8px 12px !important;
        border-radius: 20px !important;
    }
    
    /* Optimize navigation */
    .nav-links {
        overflow-x: auto;
        white-space: nowrap;
        -webkit-overflow-scrolling: touch;
        scrollbar-width: none;
        -ms-overflow-style: none;
    }
    
    .nav-links::-webkit-scrollbar {
        display: none;
    }
    
    .nav-links a {
        display: inline-block;
        padding: 8px 16px !important;
        font-size: 14px !important;
    }
    
    /* Optimize filters */
    .filters-container {
        position: sticky;
        top: 60px;
        z-index: 999;
        background: #000;
        padding: 8px !important;
        overflow-x: auto;
        white-space: nowrap;
        -webkit-overflow-scrolling: touch;
    }
    
    .filter-btn {
        display: inline-block;
        padding: 6px 12px !important;
        font-size: 13px !important;
        margin-right: 8px !important;
        border-radius: 16px !important;
    }
    
    /* Optimize pagination */
    .pagination {
        padding: 16px 8px !important;
        gap: 8px !important;
    }
    
    .pagination button {
        min-width: 44px !important;
        min-height: 44px !important;
        font-size: 14px !important;
    }
    
    /* Optimize loading states */
    .loading-spinner {
        width: 40px !important;
        height: 40px !important;
    }
    
    /* Pull-to-refresh indicator */
    .ptr-indicator {
        position: fixed;
        top: 60px;
        left: 50%;
        transform: translateX(-50%);
        z-index: 9999;
        opacity: 0;
        transition: opacity 0.3s;
    }
    
    .ptr-indicator.active {
        opacity: 1;
    }
}

/* ===== LANDSCAPE MODE OPTIMIZATIONS ===== */
@media (max-width: 768px) and (orientation: landscape) {
    .video-grid {
        grid-template-columns: repeat(3, 1fr) !important;
    }
    
    .header {
        padding: 4px 8px !important;
    }
    
    .logo {
        font-size: 16px !important;
    }
    
    .filters-container {
        top: 50px;
    }
}

/* ===== SMALL PHONES ===== */
@media (max-width: 375px) {
    .video-title {
        font-size: 12px !important;
    }
    
    .video-meta {
        font-size: 10px !important;
    }
}

/* ===== LARGE PHONES ===== */
@media (min-width: 414px) and (max-width: 768px) {
    .video-grid {
        grid-template-columns: repeat(2, 1fr) !important;
        gap: 10px !important;
    }
}

/* ===== TABLET PORTRAIT ===== */
@media (min-width: 768px) and (max-width: 1024px) and (orientation: portrait) {
    .video-grid {
        grid-template-columns: repeat(3, 1fr) !important;
        gap: 12px !important;
    }
}

/* ===== TABLET LANDSCAPE ===== */
@media (min-width: 768px) and (max-width: 1024px) and (orientation: landscape) {
    .video-grid {
        grid-template-columns: repeat(4, 1fr) !important;
        gap: 12px !important;
    }
}

/* ===== SMOOTH SCROLL ===== */
@media (max-width: 768px) {
    html {
        scroll-behavior: smooth;
    }
    
    /* Optimize scroll performance */
    .video-grid {
        contain: layout style paint;
    }
    
    .video-card {
        contain: layout style paint;
    }
}

/* ===== REDUCE MOTION FOR ACCESSIBILITY ===== */
@media (prefers-reduced-motion: reduce) {
    * {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }
}

/* ===== DARK MODE OPTIMIZATION ===== */
@media (prefers-color-scheme: dark) {
    body {
        background: #000;
        color: #fff;
    }
}

/* ===== HIGH CONTRAST MODE ===== */
@media (prefers-contrast: high) {
    .video-card {
        border: 1px solid #fff;
    }
}

/* ===== SAFE AREA INSETS (iPhone notch, etc) ===== */
@supports (padding: max(0px)) {
    body {
        padding-left: max(0px, env(safe-area-inset-left));
        padding-right: max(0px, env(safe-area-inset-right));
        padding-bottom: max(0px, env(safe-area-inset-bottom));
    }
    
    .header {
        padding-top: max(8px, env(safe-area-inset-top));
    }
}

/* ===== PREVENT LAYOUT SHIFT ===== */
.video-card {
    content-visibility: auto;
    contain-intrinsic-size: 0 250px;
}

/* ===== OPTIMIZE IMAGES ===== */
img {
    content-visibility: auto;
}

/* ===== CONNECTION WARNING ===== */
@keyframes slideDown {
    from {
        opacity: 0;
        transform: translateX(-50%) translateY(-20px);
    }
    to {
        opacity: 1;
        transform: translateX(-50%) translateY(0);
    }
}

#connection-warning {
    animation: slideDown 0.3s ease-out;
}

/* ===== ULTRA-FAST TAP RESPONSE ===== */
@media (hover: none) and (pointer: coarse) {
    .video-card {
        transition: transform 0.05s ease-out, opacity 0.05s ease-out;
        cursor: pointer;
    }
    
    .video-card:active {
        transform: scale(0.95) !important;
        opacity: 0.9 !important;
        transition: transform 0.05s ease-out, opacity 0.05s ease-out;
    }
    
    /* Show connection indicator on slow networks */
    .slow-connection .video-card::after {
        content: '📶';
        position: absolute;
        top: 8px;
        right: 8px;
        font-size: 12px;
        opacity: 0.6;
    }
    
    button:active,
    a:active {
        opacity: 0.7;
        transform: scale(0.97);
        transition: all 0.05s ease-out;
    }
    
    /* Instant preview container */
    .preview-container {
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        display: none;
        align-items: center;
        justify-content: center;
        background: rgba(0, 0, 0, 0.8);
        z-index: 10;
        border-radius: 8px;
    }
    
    .preview-loading {
        font-size: 24px;
        animation: pulse 0.8s ease-in-out infinite;
    }
    
    @keyframes pulse {
        0%, 100% { opacity: 1; transform: scale(1); }
        50% { opacity: 0.6; transform: scale(1.1); }
    }
    
    /* Optimize video preview on mobile */
    .preview-video {
        width: 100% !important;
        height: 100% !important;
        object-fit: cover;
        border-radius: 8px;
    }
}
