/**
 * Core Web Vitals Optimization CSS
 * Optimized for CLS (Cumulative Layout Shift) and LCP (Largest Contentful Paint)
 * 
 * @author DMF Art Média Kft.
 * @created 2025-01-14
 */

/* ==========================================================================
   1. ASPECT RATIO CONTAINERS - Prevent CLS for images
   ========================================================================== */

/**
 * Aspect ratio utilities for preventing layout shift
 * Use aspect-square, aspect-video, aspect-[16/9] from Tailwind
 */

.aspect-preserve {
  position: relative;
  overflow: hidden;
}

.aspect-preserve img {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

/* Explicit aspect ratios for common use cases */
.aspect-1-1 {
  aspect-ratio: 1 / 1;
}

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

.aspect-16-9 {
  aspect-ratio: 16 / 9;
}

.aspect-21-9 {
  aspect-ratio: 21 / 9;
}

/* ==========================================================================
   2. IMAGE LOADING OPTIMIZATION - Prevent FOIT/FOUT
   ========================================================================== */

/**
 * Image loading states to prevent jarring shifts
 */
img[loading="lazy"] {
  /* Reserve space before image loads */
  min-height: 1px;
}

/* Smooth fade-in when image loads */
img.lazyload {
  opacity: 0;
  transition: opacity 0.3s ease-in-out;
}

img.lazyloaded {
  opacity: 1;
}

/* Placeholder background for loading images */
img:not([src]) {
  background: linear-gradient(
    90deg,
    rgba(226, 232, 240, 0.3) 25%,
    rgba(226, 232, 240, 0.5) 50%,
    rgba(226, 232, 240, 0.3) 75%
  );
  background-size: 200% 100%;
  animation: shimmer 1.5s infinite;
}

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

/* Dark mode placeholder */
.dark img:not([src]) {
  background: linear-gradient(
    90deg,
    rgba(51, 65, 85, 0.3) 25%,
    rgba(51, 65, 85, 0.5) 50%,
    rgba(51, 65, 85, 0.3) 75%
  );
  background-size: 200% 100%;
}

/* ==========================================================================
   3. CONTENT VISIBILITY - Optimize off-screen rendering
   ========================================================================== */

/**
 * Use content-visibility for sections below the fold
 * Reduces initial rendering work and improves LCP
 */
.section-offscreen {
  content-visibility: auto;
  contain-intrinsic-size: auto 500px;
}

.section-large-offscreen {
  content-visibility: auto;
  contain-intrinsic-size: auto 1000px;
}

/* ==========================================================================
   4. SKELETON LOADERS - Prevent CLS during content loading
   ========================================================================== */

/**
 * Skeleton loader for cards and content blocks
 */
.skeleton {
  background: linear-gradient(
    90deg,
    #f0f0f0 25%,
    #e0e0e0 50%,
    #f0f0f0 75%
  );
  background-size: 200% 100%;
  animation: loading 1.5s infinite;
}

.dark .skeleton {
  background: linear-gradient(
    90deg,
    #2d3748 25%,
    #1a202c 50%,
    #2d3748 75%
  );
}

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

.skeleton-text {
  height: 1em;
  margin-bottom: 0.5em;
  border-radius: 4px;
}

.skeleton-title {
  height: 2em;
  margin-bottom: 0.75em;
  border-radius: 6px;
}

.skeleton-image {
  aspect-ratio: 16 / 9;
  border-radius: 8px;
}

/* ==========================================================================
   5. PERFORMANCE OPTIMIZATIONS - Reduce repaints
   ========================================================================== */

/**
 * GPU acceleration for smooth transitions
 */
.will-change-transform {
  will-change: transform;
}

.will-change-opacity {
  will-change: opacity;
}

/* Optimize hover effects */
.optimized-hover {
  transform: translateZ(0);
  backface-visibility: hidden;
  perspective: 1000px;
}

/* ==========================================================================
   6. LAZY LAYOUT - Defer rendering of below-fold sections
   ========================================================================== */

/**
 * Sections that should render lazily
 */
[data-lazy-section] {
  content-visibility: auto;
}

[data-lazy-section].in-viewport {
  content-visibility: visible;
}

/* ==========================================================================
   7. FONT LOADING - Prevent FOIT/FOUT CLS
   ========================================================================== */

/**
 * Already handled in layout.ejs with font-display: swap
 * This ensures text remains visible during font load
 */

/* Fallback font metrics to match web fonts */
body {
  font-synthesis: none;
  text-rendering: optimizeLegibility;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

/* ==========================================================================
   8. CRITICAL RENDERING PATH - Above the fold optimization
   ========================================================================== */

/**
 * Note: Header contain property removed as it creates stacking context
 * that breaks dropdown menu z-index layering. Header stability is maintained
 * through explicit height (h-16 = 64px) in layout.ejs Tailwind classes.
 */

/**
 * Hero section optimization - prevent CLS from dynamic content
 */
.hero-section {
  min-height: 400px;
  contain: layout style paint;
}

@media (min-width: 768px) {
  .hero-section {
    min-height: 500px;
  }
}

/* ==========================================================================
   9. IMAGE PRELOAD HINTS - Prioritize LCP images
   ========================================================================== */

/**
 * Images marked with fetchpriority="high" get loading priority
 * This is handled natively by the browser through the fetchpriority attribute
 * No additional CSS needed - included for documentation
 */

/* ==========================================================================
   10. ANIMATION PERFORMANCE - Reduce layout thrashing
   ========================================================================== */

/**
 * Only animate transform and opacity for 60fps performance
 */
.performant-animation {
  transition-property: transform, opacity;
  transition-timing-function: cubic-bezier(0.4, 0, 0.2, 1);
}

/* Disable animations for reduced motion preference */
@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}

/* ==========================================================================
   11. RESPONSIVE IMAGES - Prevent CLS on viewport changes
   ========================================================================== */

/**
 * Ensure images maintain aspect ratio across breakpoints
 */
.responsive-img {
  width: 100%;
  height: auto;
  display: block;
}

/* ==========================================================================
   12. LAYOUT STABILITY - Prevent shifts from dynamic content
   ========================================================================== */

/**
 * Reserve minimum space for dynamic content areas
 */
.dynamic-content-area {
  min-height: 100px;
}

/* ==========================================================================
   13. INTERSECTION OBSERVER OPTIMIZATION
   ========================================================================== */

/**
 * Elements to be observed for lazy loading
 */
[data-observe] {
  opacity: 0;
  transform: translateY(20px);
  transition: opacity 0.6s ease-out, transform 0.6s ease-out;
}

[data-observe].is-visible {
  opacity: 1;
  transform: translateY(0);
}
