/* ================================
   Loading States & Performance
   ================================ */

/* Image Loading Placeholder */
.img-loading {
  position: relative;
  overflow: hidden;
  background: var(--color-bg-secondary);
}

.img-loading::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(
    90deg,
    var(--color-bg-secondary) 0%,
    var(--color-bg-tertiary) 50%,
    var(--color-bg-secondary) 100%
  );
  background-size: 200% 100%;
  animation: shimmer 1.5s infinite;
  z-index: 1;
  pointer-events: none;
}

.img-loading img {
  opacity: 0;
  transition: opacity 0.3s ease-in-out;
}

/* When loaded, remove shimmer and show image */
.img-loading.loaded::before {
  display: none;
}

.img-loading.loaded img {
  opacity: 1;
}

/* Fallback: if img-loading has no 'loaded' class after 3s, show anyway */
.img-loading:not(.loaded) img {
  animation: forceShow 0.3s ease-in-out 3s forwards;
}

@keyframes forceShow {
  to {
    opacity: 1;
  }
}

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

/* Skeleton Loading for Cards */
.skeleton {
  background: var(--color-bg-secondary);
  border-radius: var(--radius-md);
  position: relative;
  overflow: hidden;
}

.skeleton::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(
    90deg,
    transparent 0%,
    rgba(255, 255, 255, 0.05) 50%,
    transparent 100%
  );
  background-size: 200% 100%;
  animation: shimmer 1.5s infinite;
}

/* Skeleton Text */
.skeleton-text {
  height: 1em;
  margin-bottom: 0.5em;
  background: var(--color-bg-tertiary);
  border-radius: var(--radius-sm);
}

.skeleton-text:last-child {
  margin-bottom: 0;
  width: 60%;
}

/* Skeleton Card Layout */
.skeleton-card {
  padding: var(--space-4);
}

.skeleton-image {
  width: 100%;
  height: 200px;
  background: var(--color-bg-tertiary);
  border-radius: var(--radius-md);
  margin-bottom: var(--space-4);
}

.skeleton-title {
  height: 24px;
  width: 70%;
  background: var(--color-bg-tertiary);
  border-radius: var(--radius-sm);
  margin-bottom: var(--space-3);
}

.skeleton-line {
  height: 16px;
  background: var(--color-bg-tertiary);
  border-radius: var(--radius-sm);
  margin-bottom: var(--space-2);
}

.skeleton-line:last-child {
  width: 80%;
}

/* Progressive Image Loading */
.progressive-image {
  position: relative;
  display: block;
  overflow: hidden;
  background: var(--color-bg-secondary);
}

.progressive-image-placeholder {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  filter: blur(10px);
  transform: scale(1.1);
  transition: opacity 0.3s ease-in-out;
  z-index: 1;
}

.progressive-image img {
  position: relative;
  z-index: 2;
  opacity: 0;
  transition: opacity 0.3s ease-in-out;
}

.progressive-image.loaded img {
  opacity: 1;
}

.progressive-image.loaded .progressive-image-placeholder {
  opacity: 0;
}

/* Fade-in Animation for Content */
.fade-in {
  animation: fadeIn 0.5s ease-in-out;
}

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

/* Loading Spinner */
.spinner {
  display: inline-block;
  width: 40px;
  height: 40px;
  border: 4px solid var(--color-bg-tertiary);
  border-top-color: var(--color-accent-primary);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

.spinner-sm {
  width: 20px;
  height: 20px;
  border-width: 2px;
}

.spinner-lg {
  width: 60px;
  height: 60px;
  border-width: 6px;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

/* Loading State Container */
.loading-container {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 200px;
  flex-direction: column;
  gap: var(--space-4);
}

.loading-text {
  color: var(--color-text-tertiary);
  font-size: var(--font-size-sm);
  margin-top: var(--space-2);
}

/* Reduce Motion for Accessibility */
@media (prefers-reduced-motion: reduce) {
  .img-loading::before,
  .skeleton::before,
  .progressive-image-placeholder,
  .progressive-image img,
  .fade-in,
  .spinner {
    animation: none;
    transition: none;
  }

  .img-loading img,
  .progressive-image img {
    opacity: 1;
  }

  .img-loading::before {
    display: none;
  }
}
