/* Core Web Vitals Performance Optimizations */

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

/* Critical above-the-fold content */
.modern-header,
.modern-form,
.character-search-form {
  contain: layout style paint;
  will-change: auto; /* Reset will-change after animations */
}

/* Optimize font loading for LCP */
@font-face {
  font-family: 'System';
  src: local('system-ui'), local('-apple-system'), local('BlinkMacSystemFont');
  font-display: swap; /* Prevent invisible text during font load */
}

/* Preload critical fonts */
.critical-text {
  font-display: swap;
  font-family:
    system-ui,
    -apple-system,
    BlinkMacSystemFont,
    'Segoe UI',
    sans-serif;
}

/* ===== CLS (Cumulative Layout Shift) Prevention ===== */

/* Reserve space for images to prevent layout shifts */
.image-placeholder {
  background: linear-gradient(90deg, #f0f0f0 25%, #e0e0e0 50%, #f0f0f0 75%);
  background-size: 200% 100%;
  animation: shimmer 1.5s infinite;
  min-height: 200px; /* Reserve minimum space */
  display: flex;
  align-items: center;
  justify-content: center;
}

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

/* Fixed dimensions for dynamic content */
.character-card {
  min-height: 280px; /* Prevent layout shift during loading */
  contain: layout style paint;
}

/* Gallery grid layout is handled by gallery.css - removed conflicting styles */

/* Image card performance optimizations - avoid conflicting with gallery.css layout */
.image-card {
  contain: layout style paint;
}

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

/* Optimize interactive elements */
.modern-btn,
.character-card,
.image-card {
  transform: translateZ(0); /* Force GPU acceleration */
  backface-visibility: hidden;
}

/* Debounce hover effects */
.modern-btn:hover,
.character-card:hover {
  transition: transform 0.15s ease-out;
  will-change: transform;
}

.modern-btn:not(:hover),
.character-card:not(:hover) {
  will-change: auto; /* Reset will-change when not needed */
}

/* ===== Image Loading Optimizations ===== */

/* Lazy loading states */
.lazy-loading {
  background: linear-gradient(90deg, #f0f0f0 25%, #e0e0e0 50%, #f0f0f0 75%);
  background-size: 200% 100%;
  animation: shimmer 1.5s infinite;
}

.lazy-loading.loaded {
  animation: none;
  background: none;
}

/* WebP support detection */
.webp .image-webp {
  display: block;
}

.no-webp .image-webp {
  display: none;
}

.webp .image-fallback {
  display: none;
}

.no-webp .image-fallback {
  display: block;
}

/* Image loading states */
img.loading {
  opacity: 0.7;
  filter: blur(2px);
  transition:
    opacity 0.3s ease,
    filter 0.3s ease;
}

img.loaded {
  opacity: 1;
  filter: none;
}

img.error {
  background: #f8f9fa;
  border: 1px dashed #dee2e6;
  display: flex;
  align-items: center;
  justify-content: center;
}

img.error::after {
  content: '이미지 로드 실패';
  color: #6c757d;
  font-size: 12px;
}

/* ===== Layout Containment ===== */

/* Contain layout calculations */
.modern-container {
  contain: layout style;
}

.modern-form-section {
  contain: layout;
}

.results-section {
  contain: layout style paint;
}

.gallery-section {
  contain: layout style;
}

/* ===== Scroll Performance ===== */

/* Optimize scrolling */
.scroll-container {
  -webkit-overflow-scrolling: touch;
  scroll-behavior: smooth;
  contain: layout style;
}

/* Passive scroll listeners */
.scroll-area {
  touch-action: pan-y; /* Allow only vertical scrolling */
}

/* ===== Animation Performance ===== */

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

@media (prefers-reduced-motion: no-preference) {
  .smooth-transition {
    transition:
      transform 0.2s ease-out,
      opacity 0.2s ease-out;
  }

  .fade-in {
    animation: fadeIn 0.3s ease-out;
  }

  @keyframes fadeIn {
    from {
      opacity: 0;
      transform: translateY(10px);
    }
    to {
      opacity: 1;
      transform: translateY(0);
    }
  }
}

/* ===== Critical Resource Hints ===== */

/* Optimize font rendering */
.preload-font {
  font-display: swap;
}

/* ===== Memory Management ===== */

/* Limit concurrent animations */
.animation-container {
  contain: layout style paint;
}

/* Optimize transforms */
.gpu-accelerated {
  transform: translateZ(0);
  will-change: transform;
}

.gpu-accelerated:not(:hover):not(:focus):not(.active) {
  will-change: auto;
}

/* ===== Responsive Performance ===== */

/* Mobile optimizations */
@media (max-width: 768px) {
  .character-grid {
    grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
    gap: 12px;
  }

  .image-card {
    min-height: 150px;
  }

  /* Reduce animation complexity on mobile */
  .modern-btn:hover {
    transform: none;
  }
}

/* High DPI display optimizations */
@media (-webkit-min-device-pixel-ratio: 2), (min-resolution: 192dpi) {
  .high-dpi-image {
    image-rendering: -webkit-optimize-contrast;
    image-rendering: crisp-edges;
  }
}
