/* Performance Optimizations CSS */

/* Critical Above-the-fold Styles */
:root {
    --primary-color: #6C5CE7;
    --primary-hover: #4b3bbd;
    --bg-dark: #0F0F23;
    --bg-card: #181828;
    --text-light: #fff;
    --text-muted: #aaa;
    --border-radius: 16px;
    --shadow-light: 0 2px 8px rgba(0,0,0,0.12);
    --shadow-medium: 0 8px 24px rgba(108,92,231,0.2);
    --transition: all 0.3s ease;
}

/* Performance optimizations */
* {
    box-sizing: border-box;
}

html {
    scroll-behavior: smooth;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

body {
    margin: 0;
    font-family: 'Poppins', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background: var(--bg-dark);
    color: var(--text-light);
    line-height: 1.6;
    font-display: swap; /* Better font loading */
}

/* Container with optimal sizing */
.container {
    max-width: 1200px;
    margin: 0 auto;
    padding: 0 min(20px, 4vw);
}

/* Optimized grid system */
.games-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(min(280px, 100vw - 40px), 1fr));
    gap: clamp(16px, 3vw, 24px);
    padding: clamp(16px, 3vw, 20px) 0;
}

/* Game card optimizations */
.game-card {
    background: var(--bg-card);
    border-radius: var(--border-radius);
    overflow: hidden;
    transition: var(--transition);
    will-change: transform;
    contain: layout style paint;
}

/* Image optimization */
.game-image {
    aspect-ratio: 3/2;
    object-fit: cover;
    width: 100%;
    height: auto;
    transition: transform 0.3s ease;
    will-change: transform;
}

/* LCP image optimization for single game page */
.game-loader img {
    aspect-ratio: 16/11;
    object-fit: cover;
    width: 100%;
    max-width: 320px;
    height: auto;
    contain: layout style paint;
}

/* Prevent layout shift for game containers */
.game-container {
    contain: layout style paint;
    min-height: 400px;
}

.game-player {
    contain: layout style;
}

/* Critical loading states */
.game-loader {
    min-height: 500px;
    contain: layout;
}

/* Optimize iframe loading */
iframe[loading="lazy"] {
    content-visibility: auto;
    contain-intrinsic-size: 800px 600px;
}

/* Header optimization */
.site-header {
    background: var(--bg-card);
    padding: clamp(15px, 3vw, 20px) 0;
    position: sticky;
    top: 0;
    z-index: 100;
    backdrop-filter: blur(10px);
    contain: layout style paint;
}

/* Button optimizations */
.play-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    background: var(--primary-color);
    color: var(--text-light);
    padding: clamp(8px, 2vw, 10px) clamp(18px, 4vw, 24px);
    border-radius: 20px;
    font-weight: 600;
    text-decoration: none;
    transition: var(--transition);
    font-size: clamp(0.75rem, 2vw, 0.85rem);
    min-width: clamp(80px, 20vw, 100px);
    border: none;
    cursor: pointer;
    will-change: transform, background-color;
}

/* Responsive optimizations */
@media (max-width: 768px) {
    .games-grid {
        grid-template-columns: repeat(auto-fill, minmax(min(260px, 100vw - 32px), 1fr));
        gap: 16px;
        padding: 16px 0;
    }
    
    .game-card {
        max-height: 380px;
    }
    
    .game-image {
        height: 160px;
    }
}

@media (max-width: 480px) {
    .games-grid {
        grid-template-columns: 1fr;
        gap: 12px;
    }
    
    .game-card {
        max-height: 360px;
    }
    
    .game-image {
        height: 140px;
    }
}

/* Performance optimizations for animations */
@media (prefers-reduced-motion: reduce) {
    * {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }
}

/* High contrast mode support */
@media (prefers-contrast: high) {
    :root {
        --bg-dark: #000000;
        --bg-card: #1a1a1a;
        --text-light: #ffffff;
        --primary-color: #7c6df7;
    }
}

/* Dark mode optimization (already dark, but just in case) */
@media (prefers-color-scheme: dark) {
    :root {
        color-scheme: dark;
    }
}

/* Print styles optimization */
@media print {
    .site-header,
    .play-btn,
    .game-controls {
        display: none !important;
    }
    
    .game-card {
        break-inside: avoid;
        box-shadow: none;
        border: 1px solid #ccc;
    }
}

/* Focus management for accessibility */
:focus-visible {
    outline: 2px solid var(--primary-color);
    outline-offset: 2px;
}

/* Loading states */
.loading {
    opacity: 0.6;
    pointer-events: none;
}

.loading::after {
    content: '';
    position: absolute;
    top: 50%;
    left: 50%;
    width: 20px;
    height: 20px;
    margin: -10px 0 0 -10px;
    border: 2px solid var(--primary-color);
    border-radius: 50%;
    border-top-color: transparent;
    animation: spin 1s linear infinite;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

/* Intersection Observer optimizations */
.lazy-load {
    opacity: 0;
    transform: translateY(20px);
    transition: opacity 0.6s ease, transform 0.6s ease;
}

.lazy-load.loaded {
    opacity: 1;
    transform: translateY(0);
}

/* GPU acceleration for smoother animations */
.game-card,
.play-btn,
.game-image {
    transform: translateZ(0);
}

/* Content containment for better performance */
.game-info {
    contain: layout style;
}

/* Optimize text rendering */
h1, h2, h3, h4, h5, h6 {
    text-rendering: optimizeLegibility;
}

/* Optimize button interactions */
.play-btn:hover {
    background: var(--primary-hover);
    transform: translateY(-2px);
}

.play-btn:active {
    transform: translateY(0);
}

/* Touch device optimizations */
@media (hover: none) and (pointer: coarse) {
    .play-btn:hover {
        background: var(--primary-color);
        transform: none;
    }
    
    .play-btn:active {
        background: var(--primary-hover);
        transform: scale(0.95);
    }
    
    .game-card:active {
        transform: scale(0.98);
    }
}

/* Optimize for high DPI displays */
@media (-webkit-min-device-pixel-ratio: 2), (min-resolution: 192dpi) {
    .game-image {
        image-rendering: -webkit-optimize-contrast;
        image-rendering: crisp-edges;
    }
}
