/* Mobile Performance Optimizations - Global */
/* Focus on PageSpeed for mobile devices */

/* ============================================
   Reduce Layout Shifts (CLS)
   ============================================ */

/* Reserve space for images to prevent layout shift */
img {
  max-width: 100%;
  height: auto;
  /* Reserve aspect ratio space */
  aspect-ratio: attr(width) / attr(height);
}

/* Reserve space for common aspect ratios */
.aspect-16-9 {
  aspect-ratio: 16 / 9;
}

.aspect-4-3 {
  aspect-ratio: 4 / 3;
}

.aspect-1-1 {
  aspect-ratio: 1 / 1;
}

/* Font loading optimization */
.fonts-loading body {
  /* Prevent layout shift during font load */
  font-family: Arial, sans-serif;
}

/* ============================================
   Optimize Largest Contentful Paint (LCP)
   ============================================ */

/* Preload critical images */
.hero-image,
.featured-image {
  /* Ensure critical images load quickly */
  content-visibility: auto;
  contain-intrinsic-size: 320px 180px;
}

/* ============================================
   Improve First Input Delay (FID)
   ============================================ */

/* Optimize touch interactions */
@media (hover: none) and (pointer: coarse) {
  /* All clickable elements should respond quickly */
  a, button, summary, .btn, [role="button"] {
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
  }

  /* Remove hover states on touch devices */
  a:hover,
  button:hover,
  .btn:hover {
    background-color: inherit;
    transform: none;
  }
}

/* ============================================
   Reduce Paint Complexity
   ============================================ */

/* Use CSS containment for better performance */
.card,
.accordion,
details,
.module {
  contain: layout style;
}

/* Sections that are off-screen */
.section:not(:first-child) {
  content-visibility: auto;
  contain-intrinsic-size: 0 500px;
}

/* ============================================
   Optimize Scrolling Performance
   ============================================ */

/* Smooth but performant scrolling */
@media (prefers-reduced-motion: no-preference) {
  html {
    scroll-behavior: smooth;
    scroll-padding-top: 60px; /* Account for fixed header */
  }
}

/* Optimize scroll performance on mobile */
@media (max-width: 768px) {
  * {
    /* Disable expensive properties during scroll */
    will-change: auto !important;
  }

  /* Only enable for elements that actually need it */
  .is-scrolling * {
    pointer-events: none;
  }

  /* Re-enable after scroll */
  body:not(.is-scrolling) * {
    pointer-events: auto;
  }
}

/* ============================================
   Reduce Animation Overhead
   ============================================ */

/* Simplify animations on mobile */
@media (max-width: 768px) {
  /* Use opacity-only transitions for better performance */
  .fade-in {
    animation: fadeIn 0.3s ease-in;
  }

  @keyframes fadeIn {
    from { opacity: 0; }
    to { opacity: 1; }
  }

  /* Disable complex animations */
  .complex-animation {
    animation: none !important;
  }

  /* Reduce transition complexity */
  * {
    transition-property: opacity, transform !important;
    transition-duration: 0.2s !important;
  }
}

/* ============================================
   Optimize Web Fonts
   ============================================ */

/* Use font-display: swap for better performance */
@font-face {
  font-display: swap;
}

/* Fallback fonts for faster initial render */
.font-loading h1,
.font-loading h2,
.font-loading h3 {
  font-family: -apple-system, BlinkMacSystemFont, sans-serif;
}

/* ============================================
   Lazy Loading Helpers
   ============================================ */

/* Images below the fold */
img[loading="lazy"] {
  /* Prevent layout shift while loading */
  background: #f0f0f0;
}

/* Lazy loaded content placeholder */
.lazy-section {
  min-height: 200px;
  background: linear-gradient(90deg, #f0f0f0 25%, #e0e0e0 50%, #f0f0f0 75%);
  background-size: 200% 100%;
  animation: loading 1.5s infinite;
}

@keyframes loading {
  0% { background-position: 200% 0; }
  100% { background-position: -200% 0; }
}

/* ============================================
   Mobile-Specific Memory Optimizations
   ============================================ */

@media (max-width: 768px) and (max-device-memory: 4) {
  /* Reduce quality for low-memory devices */
  img {
    filter: none !important;
  }

  /* Simplify shadows */
  * {
    box-shadow: none !important;
  }

  /* Reduce border-radius processing */
  * {
    border-radius: 0 !important;
  }
}

/* ============================================
   Critical Render Path Optimization
   ============================================ */

/* Hide non-critical content until loaded */
.below-fold {
  content-visibility: auto;
}

/* Prioritize above-the-fold content */
.above-fold {
  content-visibility: visible;
  contain: none;
}

/* ============================================
   Mobile Network Optimization
   ============================================ */

/* Reduce quality on slow connections */
@media (prefers-reduced-data: reduce) {
  img {
    filter: contrast(0.9);
  }

  video {
    display: none;
  }

  /* Use system fonts only */
  * {
    font-family: system-ui, -apple-system, sans-serif !important;
  }
}

/* ============================================
   Accessibility + Performance
   ============================================ */

/* Skip animations for users who prefer reduced motion */
@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;
  }
}

/* ============================================
   Mobile Debug Helpers (Remove in Production)
   ============================================ */

/* Uncomment to debug layout shifts */
/*
* {
  outline: 1px solid rgba(255, 0, 0, 0.1);
}
*/

/* Performance timing indicator */
.perf-indicator {
  position: fixed;
  bottom: 10px;
  left: 10px;
  background: rgba(0, 0, 0, 0.8);
  color: #0f0;
  padding: 5px 10px;
  font-family: monospace;
  font-size: 12px;
  z-index: 9999;
  border-radius: 3px;
}