/* Image Lazy Loading Styles */

/* Базовые стили для изображений с lazy loading */
img[data-src] {
  background: linear-gradient(
    90deg,
    rgba(255, 255, 255, 0.04) 0%,
    rgba(255, 255, 255, 0.08) 50%,
    rgba(255, 255, 255, 0.04) 100%
  );
  background-size: 200% 100%;
  animation: img-shimmer 1.5s ease-in-out infinite;
}

[data-theme='light'] img[data-src] {
  background: linear-gradient(
    90deg,
    rgba(0, 0, 0, 0.04) 0%,
    rgba(0, 0, 0, 0.08) 50%,
    rgba(0, 0, 0, 0.04) 100%
  );
}

/* Состояние загрузки */
img.img-loading {
  opacity: 0.6;
  filter: blur(5px);
}

/* Анимация появления загруженного изображения */
img.img-loaded {
  opacity: 0;
  animation: img-fade-in 0.4s ease-out forwards;
}

/* Состояние ошибки */
img.img-error {
  opacity: 0.5;
  filter: grayscale(1);
  position: relative;
}

img.img-error::after {
  content: '✕';
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  font-size: 24px;
  color: var(--muted);
}

/* Анимации */
@keyframes img-shimmer {
  0% {
    background-position: -200% 0;
  }
  100% {
    background-position: 200% 0;
  }
}

@keyframes img-fade-in {
  from {
    opacity: 0;
    transform: scale(0.98);
  }
  to {
    opacity: 1;
    transform: scale(1);
  }
}

/* Blur-up эффект для изображений с placeholder */
.img-wrapper {
  position: relative;
  overflow: hidden;
}

.img-wrapper img.img-placeholder {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  filter: blur(10px);
  transform: scale(1.1);
  opacity: 1;
  transition: opacity 0.4s ease-out;
}

.img-wrapper img.img-loaded ~ img.img-placeholder {
  opacity: 0;
}

/* Lazy background images */
[data-bg-src] {
  background-color: var(--surface);
  background-size: cover;
  background-position: center;
  position: relative;
}

[data-bg-src]::before {
  content: '';
  position: absolute;
  inset: 0;
  background: linear-gradient(
    90deg,
    transparent 0%,
    rgba(255, 255, 255, 0.05) 50%,
    transparent 100%
  );
  background-size: 200% 100%;
  animation: img-shimmer 1.5s ease-in-out infinite;
}

[data-bg-src]:not([data-lazy-bg])::before {
  display: none;
}

/* Responsive images */
img[data-src],
img[data-srcset] {
  max-width: 100%;
  height: auto;
  display: block;
}

/* Уменьшение motion для пользователей с prefers-reduced-motion */
@media (prefers-reduced-motion: reduce) {
  img[data-src] {
    animation: none;
  }

  img.img-loading {
    filter: none;
  }

  img.img-loaded {
    animation: none;
    opacity: 1;
  }

  [data-bg-src]::before {
    animation: none;
  }
}

/* Поддержка темной темы */
[data-theme='dark'] img.img-error {
  background-color: rgba(255, 255, 255, 0.05);
}

[data-theme='light'] img.img-error {
  background-color: rgba(0, 0, 0, 0.05);
}

/* Aspect ratio boxes для предотвращения layout shift */
.img-box {
  position: relative;
  width: 100%;
  overflow: hidden;
}

.img-box::before {
  content: '';
  display: block;
  padding-top: var(--aspect-ratio, 75%); /* 4:3 по умолчанию */
}

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

/* Популярные соотношения сторон */
.img-box--16-9::before { padding-top: 56.25%; }
.img-box--4-3::before { padding-top: 75%; }
.img-box--1-1::before { padding-top: 100%; }
.img-box--21-9::before { padding-top: 42.857%; }
.img-box--3-2::before { padding-top: 66.667%; }

/* Поддержка современного aspect-ratio */
@supports (aspect-ratio: 16 / 9) {
  .img-box::before {
    display: none;
  }

  .img-box {
    aspect-ratio: var(--aspect-ratio, 4 / 3);
  }

  .img-box--16-9 { aspect-ratio: 16 / 9; }
  .img-box--4-3 { aspect-ratio: 4 / 3; }
  .img-box--1-1 { aspect-ratio: 1 / 1; }
  .img-box--21-9 { aspect-ratio: 21 / 9; }
  .img-box--3-2 { aspect-ratio: 3 / 2; }

  .img-box img {
    position: static;
  }
}
