/**
 * Loading Optimization & FOUC Prevention
 * 로딩 최적화 및 깜빡임 현상 방지
 */

/* ==========================================================================
   FOUC (Flash of Unstyled Content) Prevention
   ========================================================================== */

/* HTML이 로드되기 전까지 body 숨김 */
html {
    opacity: 0;
    transition: opacity 0.2s ease-in;
}

/* CSS가 로드되면 표시 */
html.loaded {
    opacity: 1;
}

/* 로딩 중 스켈레톤 효과 */
.loading-skeleton {
    background: linear-gradient(
        90deg,
        rgba(240, 240, 240, 0.4) 25%,
        rgba(240, 240, 240, 0.6) 50%,
        rgba(240, 240, 240, 0.4) 75%
    );
    background-size: 200% 100%;
    animation: skeleton-loading 1.5s infinite;
}

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

/* ==========================================================================
   Enhanced Loading Screen
   ========================================================================== */

.loading-screen {
    background: linear-gradient(
        135deg,
        rgba(255, 255, 255, 0.95) 0%,
        rgba(255, 248, 240, 0.98) 100%
    );
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
}

/* 로딩 애니메이션 최적화 */
.loading-screen__logo {
    animation: gentle-pulse 2s ease-in-out infinite;
    will-change: transform;
}

@keyframes gentle-pulse {
    0%, 100% { 
        transform: scale(1);
        opacity: 0.8;
    }
    50% { 
        transform: scale(1.05);
        opacity: 1;
    }
}

/* 텍스트 페이드인 효과 */
.loading-screen__text {
    animation: fade-in-up 0.6s ease-out 0.3s both;
}

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

/* ==========================================================================
   Performance Optimizations
   ========================================================================== */

/* 중요 요소들의 GPU 가속 */
.loading-screen,
.loading-screen__logo,
.hero,
.nav {
    transform: translateZ(0);
    backface-visibility: hidden;
    will-change: auto;
}

/* 로딩 완료 후 will-change 제거 */
.loaded .loading-screen,
.loaded .loading-screen__logo {
    will-change: auto;
}

/* ==========================================================================
   Progressive Enhancement
   ========================================================================== */

/* JavaScript가 비활성화된 경우 */
.no-js .loading-screen {
    display: none;
}

.no-js html {
    opacity: 1;
}

/* 느린 연결을 위한 최적화 */
@media (prefers-reduced-data: reduce) {
    .loading-screen {
        background: white;
        backdrop-filter: none;
        -webkit-backdrop-filter: none;
    }
    
    .loading-screen__logo {
        animation: none;
    }
}

/* ==========================================================================
   Critical Resource Loading States
   ========================================================================== */

/* 폰트 로딩 중 폴백 */
.font-loading {
    font-family: system-ui, -apple-system, sans-serif;
}

/* 이미지 로딩 상태 - 애니메이션과 호환되도록 개선 */
img {
    opacity: 0;
    transition: opacity 0.3s ease;
    will-change: opacity;
}

img.loaded {
    opacity: 1;
}

/* AOS 애니메이션과 충돌 방지 */
img[data-aos] {
    opacity: 1 !important;
}

img[data-aos].loaded {
    opacity: 1 !important;
}

/* 히어로 로고 특별 처리 - 애니메이션 완료 후 표시 */
.hero__logo {
    opacity: 0;
    transition: opacity 0.5s ease 0.3s; /* 0.3초 지연 후 페이드인 */
}

.hero__logo.loaded {
    opacity: 1;
}

/* 지연 로딩 이미지 */
img[loading="lazy"] {
    background: #f0f0f0;
}

/* ==========================================================================
   Mobile Optimization
   ========================================================================== */

@media (max-width: 768px) {
    .loading-screen {
        gap: var(--space-4);
    }
    
    .loading-screen__logo {
        width: 80px;
        height: 80px;
        animation-duration: 2.5s;
    }
    
    .loading-screen__text {
        font-size: var(--text-lg);
    }
    
    /* 모바일에서 더 빠른 로딩 */
    html {
        transition-duration: 0.15s;
    }
}

/* ==========================================================================
   Dark Mode Support
   ========================================================================== */

@media (prefers-color-scheme: dark) {
    .loading-screen {
        background: linear-gradient(
            135deg,
            rgba(18, 18, 18, 0.95) 0%,
            rgba(25, 25, 25, 0.98) 100%
        );
    }
    
    .loading-screen__text {
        color: rgba(255, 255, 255, 0.8);
    }
    
    .loading-skeleton {
        background: linear-gradient(
            90deg,
            rgba(60, 60, 60, 0.4) 25%,
            rgba(80, 80, 80, 0.6) 50%,
            rgba(60, 60, 60, 0.4) 75%
        );
    }
}

/* ==========================================================================
   Accessibility Improvements
   ========================================================================== */

/* 동작 민감성 사용자 배려 */
@media (prefers-reduced-motion: reduce) {
    html {
        transition: none;
    }
    
    .loading-screen {
        transition: none;
    }
    
    .loading-screen__logo {
        animation: none;
    }
    
    .loading-screen__text {
        animation: none;
    }
    
    .loading-skeleton {
        animation: none;
        background: rgba(240, 240, 240, 0.5);
    }
}

/* 고대비 모드 */
@media (prefers-contrast: high) {
    .loading-screen {
        background: white;
        border: 2px solid black;
    }
    
    .loading-screen__text {
        color: black;
        font-weight: bold;
    }
}

/* ==========================================================================
   Error States
   ========================================================================== */

.loading-error {
    color: #dc2626;
    text-align: center;
    padding: var(--space-4);
}

.loading-retry {
    margin-top: var(--space-2);
    padding: var(--space-2) var(--space-4);
    background: #dc2626;
    color: white;
    border: none;
    border-radius: var(--radius-sm);
    cursor: pointer;
    transition: background 0.2s ease;
}

.loading-retry:hover {
    background: #b91c1c;
}