/**
 * ESP Loading States - Centralized loading animation library
 * Provides skeleton loaders, spinners, and shimmer effects
 * Version: 1.0.0
 */

/* ==========================================================================
   CSS CUSTOM PROPERTIES
   ========================================================================== */

:root {
    --esp-skeleton-base: #e2e8f0;
    --esp-skeleton-highlight: #f8fafc;
    --esp-shimmer-duration: 1.5s;
    --esp-fade-duration: 0.3s;
    --esp-stagger-delay: 50ms;
    --esp-spinner-color: currentColor;
    --esp-spinner-track-opacity: 0.25;
    --esp-spinner-size: 20px;
}

@media (prefers-color-scheme: dark) {
    :root {
        --esp-skeleton-base: #374151;
        --esp-skeleton-highlight: #4b5563;
    }
}

/* ==========================================================================
   SHIMMER ANIMATION & BASE SKELETON
   ========================================================================== */

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

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

@keyframes esp-fade-in {
    from { opacity: 0; transform: translateY(8px); }
    to { opacity: 1; transform: translateY(0); }
}

@keyframes esp-loading-bar {
    0% { transform: translateX(-100%); }
    50% { transform: translateX(200%); }
    100% { transform: translateX(400%); }
}

@keyframes esp-shimmer-sweep {
    0% { transform: translateX(0); }
    100% { transform: translateX(300%); }
}

/* Shared shimmer background */
.esp-skeleton,
.esp-skeleton-card__image,
.esp-skeleton-card__title,
.esp-skeleton-card__subtitle,
.esp-skeleton-card__price,
.esp-skeleton-category__image,
.esp-skeleton-category__name,
.esp-drawer__mobile-skeleton-img,
.esp-drawer__mobile-skeleton-text,
.esp-image-skeleton::before {
    background: linear-gradient(90deg, var(--esp-skeleton-base) 0%, var(--esp-skeleton-base) 40%, var(--esp-skeleton-highlight) 50%, var(--esp-skeleton-base) 60%, var(--esp-skeleton-base) 100%);
    background-size: 200% 100%;
    animation: esp-shimmer var(--esp-shimmer-duration) ease-in-out infinite;
}

.esp-skeleton { border-radius: 4px; }

/* ==========================================================================
   SKELETON VARIANTS
   ========================================================================== */

.esp-skeleton--box { display: block; width: 100%; height: 100%; }
.esp-skeleton--text { display: block; height: 1rem; border-radius: 4px; }
.esp-skeleton--text.esp-skeleton--short { width: 60%; }
.esp-skeleton--text.esp-skeleton--medium { width: 80%; }
.esp-skeleton--text.esp-skeleton--long { width: 95%; }
.esp-skeleton--circle { border-radius: 50%; flex-shrink: 0; }
.esp-skeleton--image { width: 100%; aspect-ratio: 1/1; border-radius: 8px; }
.esp-skeleton--image.esp-skeleton--landscape { aspect-ratio: 16/9; }
.esp-skeleton--image.esp-skeleton--portrait { aspect-ratio: 3/4; }

/* ==========================================================================
   SKELETON CARD COMPONENT
   ========================================================================== */

.esp-skeleton-card {
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
    padding: 1rem;
    background: #fff;
    border-radius: 8px;
    border: 1px solid #e5e7eb;
}

.esp-skeleton-card__image { width: 100%; aspect-ratio: 1/1; border-radius: 6px; }
.esp-skeleton-card__content { display: flex; flex-direction: column; gap: 0.5rem; }
.esp-skeleton-card__title { height: 1rem; width: 85%; border-radius: 4px; }
.esp-skeleton-card__subtitle { height: 0.875rem; width: 60%; border-radius: 4px; }
.esp-skeleton-card__price { height: 1.25rem; width: 40%; margin-top: 0.25rem; border-radius: 4px; }

/* ==========================================================================
   CATEGORY CARD SKELETON
   ========================================================================== */

.esp-skeleton-category {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0.5rem;
    padding: 0.75rem;
}

.esp-skeleton-category__image { width: 80px; height: 80px; border-radius: 8px; }
.esp-skeleton-category__name { width: 70%; height: 0.875rem; border-radius: 4px; }

.esp-skeleton-category:nth-child(1) { animation-delay: 0ms; }
.esp-skeleton-category:nth-child(2) { animation-delay: 50ms; }
.esp-skeleton-category:nth-child(3) { animation-delay: 100ms; }
.esp-skeleton-category:nth-child(4) { animation-delay: 150ms; }
.esp-skeleton-category:nth-child(5) { animation-delay: 200ms; }
.esp-skeleton-category:nth-child(6) { animation-delay: 250ms; }

/* ==========================================================================
   CSS SPINNER
   ========================================================================== */

.esp-spinner {
    width: var(--esp-spinner-size);
    height: var(--esp-spinner-size);
    border: 2px solid var(--esp-spinner-color);
    border-top-color: transparent;
    border-radius: 50%;
    animation: esp-spin 0.8s linear infinite;
}

.esp-spinner--sm { --esp-spinner-size: 16px; }
.esp-spinner--lg { --esp-spinner-size: 32px; }
.esp-spinner--xl { --esp-spinner-size: 48px; }

.esp-svg-spinner {
    width: var(--esp-spinner-size);
    height: var(--esp-spinner-size);
    animation: esp-spin 1s linear infinite;
}
.esp-svg-spinner circle { stroke: var(--esp-spinner-color); stroke-linecap: round; }
.esp-svg-spinner .esp-spinner-track { opacity: var(--esp-spinner-track-opacity); }
.esp-svg-spinner .esp-spinner-head { opacity: 1; stroke-dasharray: 40 60; }

/* ==========================================================================
   SHIMMER OVERLAY
   ========================================================================== */

.esp-shimmer-overlay { position: relative; overflow: hidden; }
.esp-shimmer-overlay::after {
    content: '';
    position: absolute;
    top: 0;
    left: -100%;
    width: 100%;
    height: 100%;
    background: linear-gradient(90deg, transparent, rgba(255,255,255,0.3), transparent);
    animation: esp-shimmer-sweep var(--esp-shimmer-duration) ease-in-out infinite;
    pointer-events: none;
}

/* ==========================================================================
   IMAGE LOADING PLACEHOLDER
   ========================================================================== */

.esp-image-skeleton {
    position: relative;
    overflow: hidden;
    background: var(--esp-skeleton-base);
}

.esp-image-skeleton::before {
    content: '';
    position: absolute;
    inset: 0;
    z-index: 1;
}

.esp-image-skeleton img {
    position: relative;
    z-index: 2;
    opacity: 0;
    transition: opacity var(--esp-fade-duration) ease-out;
}

.esp-image-skeleton img.is-loaded,
.esp-image-skeleton.is-loaded img { opacity: 1; }
.esp-image-skeleton.is-loaded::before { display: none; }

/* ==========================================================================
   SEARCH LOADING INDICATOR
   ========================================================================== */

.esp-search-loading {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 1rem;
    gap: 0.5rem;
    color: #64748b;
    font-size: 0.875rem;
}

.esp-search-loading__spinner {
    width: 16px;
    height: 16px;
    border: 2px solid #cbd5e1;
    border-top-color: #3b82f6;
    border-radius: 50%;
    animation: esp-spin 0.8s linear infinite;
}

.esp-search-loading-bar {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    height: 2px;
    background: #e2e8f0;
    overflow: hidden;
    opacity: 0;
    transition: opacity 0.2s ease;
}

.esp-search-loading-bar.is-loading { opacity: 1; }

.esp-search-loading-bar::after {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    width: 30%;
    height: 100%;
    background: var(--esp-primary, #E6AC2C); /* ESP Gold */
    animation: esp-loading-bar 1s ease-in-out infinite;
}

/* ==========================================================================
   FADE IN ANIMATIONS
   ========================================================================== */

.esp-fade-in { animation: esp-fade-in var(--esp-fade-duration) ease-out forwards; }

.esp-fade-in-stagger > * {
    opacity: 0;
    animation: esp-fade-in var(--esp-fade-duration) ease-out forwards;
}

.esp-fade-in-stagger > *:nth-child(1) { animation-delay: 0ms; }
.esp-fade-in-stagger > *:nth-child(2) { animation-delay: 50ms; }
.esp-fade-in-stagger > *:nth-child(3) { animation-delay: 100ms; }
.esp-fade-in-stagger > *:nth-child(4) { animation-delay: 150ms; }
.esp-fade-in-stagger > *:nth-child(5) { animation-delay: 200ms; }
.esp-fade-in-stagger > *:nth-child(6) { animation-delay: 250ms; }
.esp-fade-in-stagger > *:nth-child(7) { animation-delay: 300ms; }
.esp-fade-in-stagger > *:nth-child(8) { animation-delay: 350ms; }
.esp-fade-in-stagger > *:nth-child(n+9) { animation-delay: 400ms; }

/* ==========================================================================
   LOADING CONTAINER UTILITIES
   ========================================================================== */

.esp-loading-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 2rem;
    gap: 1rem;
}

.esp-btn-loading { position: relative; pointer-events: none; cursor: wait; }
.esp-btn-loading .esp-btn-content { opacity: 0; }
.esp-btn-loading .esp-btn-loader { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); }

/* ==========================================================================
   MOBILE CATEGORY DRAWER SKELETON
   ========================================================================== */

.esp-drawer__mobile-skeleton {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 0.75rem;
    padding: 1rem;
}

.esp-drawer__mobile-skeleton-card {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0.5rem;
    padding: 0.75rem;
    background: #f9fafb;
    border-radius: 8px;
}

.esp-drawer__mobile-skeleton-img { width: 60px; height: 60px; border-radius: 6px; }
.esp-drawer__mobile-skeleton-text { width: 70%; height: 0.75rem; border-radius: 4px; }

.esp-drawer__skeleton-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(100px, 1fr));
    gap: 1rem;
    padding: 1rem;
}

/* ==========================================================================
   REDUCED MOTION SUPPORT
   ========================================================================== */

@media (prefers-reduced-motion: reduce) {
    .esp-skeleton,
    .esp-skeleton-card__image,
    .esp-skeleton-card__title,
    .esp-skeleton-card__subtitle,
    .esp-skeleton-card__price,
    .esp-skeleton-category__image,
    .esp-skeleton-category__name,
    .esp-drawer__mobile-skeleton-img,
    .esp-drawer__mobile-skeleton-text,
    .esp-image-skeleton::before {
        animation: none;
        background: var(--esp-skeleton-base);
    }

    .esp-spinner,
    .esp-svg-spinner,
    .esp-search-loading__spinner,
    .esp-search-loading-bar::after {
        animation: none;
    }

    .esp-shimmer-overlay::after { display: none; }
    .esp-search-loading-bar::after { width: 100%; opacity: 0.5; }
    .esp-fade-in, .esp-fade-in-stagger > * { animation: none; opacity: 1; transform: none; }
}
