/* ========================================
   PRELOADER E OTIMIZAÇÕES DE CARREGAMENTO
   ======================================== */

/* Preloader - Aparece enquanto carrega */
#preloader {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    z-index: 999999;
    transition: opacity 0.3s ease, visibility 0.3s ease;
}

#preloader.hidden {
    opacity: 0;
    visibility: hidden;
    pointer-events: none;
}

.preloader-logo {
    font-size: 4rem;
    color: white;
    margin-bottom: 1.5rem;
    animation: pulse-logo 2s ease-in-out infinite;
}

.preloader-text {
    color: white;
    font-size: 1.5rem;
    font-weight: 600;
    margin-bottom: 2rem;
    text-align: center;
}

.preloader-spinner {
    width: 60px;
    height: 60px;
    border: 4px solid rgba(255, 255, 255, 0.3);
    border-top-color: white;
    border-radius: 50%;
    animation: spin 1s linear infinite;
}

.preloader-progress {
    margin-top: 2rem;
    width: 300px;
    height: 6px;
    background: rgba(255, 255, 255, 0.3);
    border-radius: 10px;
    overflow: hidden;
}

.preloader-progress-bar {
    height: 100%;
    background: white;
    border-radius: 10px;
    width: 0;
    animation: progress 2s ease-out forwards;
}

.preloader-version {
    margin-top: 1rem;
    color: rgba(255, 255, 255, 0.8);
    font-size: 0.875rem;
}

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

@keyframes pulse-logo {
    0%, 100% { transform: scale(1); }
    50% { transform: scale(1.1); }
}

@keyframes progress {
    0% { width: 0; }
    50% { width: 60%; }
    100% { width: 100%; }
}

/* Evita FOUC (Flash of Unstyled Content) */
body:not(.loaded) {
    overflow: hidden;
}

body:not(.loaded) .header,
body:not(.loaded) .main,
body:not(.loaded) footer {
    opacity: 0;
    visibility: hidden;
}

body.loaded .header,
body.loaded .main,
body.loaded footer {
    opacity: 1;
    visibility: visible;
    animation: fadeIn 0.5s ease;
}

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

/* Skeleton Loading para conteúdo */
.skeleton {
    background: linear-gradient(90deg, #f0f0f0 25%, #e0e0e0 50%, #f0f0f0 75%);
    background-size: 200% 100%;
    animation: skeleton-loading 1.5s ease-in-out infinite;
    border-radius: 8px;
}

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

.skeleton-text {
    height: 1rem;
    margin-bottom: 0.5rem;
}

.skeleton-card {
    height: 200px;
    margin-bottom: 1rem;
}

/* Lazy Loading de Imagens */
img[loading="lazy"] {
    opacity: 0;
    transition: opacity 0.3s ease;
}

img[loading="lazy"].loaded {
    opacity: 1;
}

/* Otimização de Performance */
* {
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

/* Reduz animações para dispositivos com preferência */
@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }
}

/* Loading Inline para elementos específicos */
.loading-inline {
    display: inline-block;
    width: 1rem;
    height: 1rem;
    border: 2px solid rgba(0, 0, 0, 0.1);
    border-top-color: currentColor;
    border-radius: 50%;
    animation: spin 0.6s linear infinite;
}

/* Botão com estado de loading */
.btn.loading {
    position: relative;
    pointer-events: none;
    opacity: 0.7;
}

.btn.loading::after {
    content: '';
    position: absolute;
    top: 50%;
    right: 1rem;
    transform: translateY(-50%);
    width: 1rem;
    height: 1rem;
    border: 2px solid rgba(255, 255, 255, 0.3);
    border-top-color: white;
    border-radius: 50%;
    animation: spin 0.6s linear infinite;
}

/* Overlay de Loading para modais */
.modal-loading {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(255, 255, 255, 0.9);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 10;
    border-radius: 12px;
}

.modal-loading-spinner {
    width: 40px;
    height: 40px;
    border: 4px solid rgba(102, 126, 234, 0.3);
    border-top-color: #667eea;
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

/* Toast de Loading */
.toast.loading {
    background: #3b82f6;
    color: white;
}

.toast.loading::before {
    content: '';
    display: inline-block;
    width: 1rem;
    height: 1rem;
    margin-right: 0.5rem;
    border: 2px solid rgba(255, 255, 255, 0.3);
    border-top-color: white;
    border-radius: 50%;
    animation: spin 0.6s linear infinite;
    vertical-align: middle;
}

/* Placeholder para conteúdo não carregado */
.content-placeholder {
    min-height: 400px;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #94a3b8;
    font-size: 1.1rem;
}

.content-placeholder i {
    font-size: 3rem;
    margin-bottom: 1rem;
    opacity: 0.5;
}

/* Otimização de Scroll */
html {
    scroll-behavior: smooth;
}

@media (prefers-reduced-motion: reduce) {
    html {
        scroll-behavior: auto;
    }
}

/* Cache de fontes */
@font-face {
    font-family: 'Poppins';
    font-display: swap; /* Mostra texto enquanto carrega fonte */
}

/* Priorização de CSS crítico */
.critical-styles {
    /* Estilos críticos inline no <head> */
}

/* Lazy load de seções */
.section:not(.active) {
    display: none;
}

.section.active {
    display: block;
    animation: fadeIn 0.3s ease;
}

/* Melhoria de performance em mobile */
@media (max-width: 768px) {
    * {
        -webkit-tap-highlight-color: transparent;
    }
    
    /* Desabilita animações pesadas em mobile */
    .heavy-animation {
        animation: none !important;
    }
}

/* Indicador de progresso de carregamento */
.loading-bar {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 3px;
    background: transparent;
    z-index: 999999;
    pointer-events: none;
}

.loading-bar::before {
    content: '';
    display: block;
    height: 100%;
    background: linear-gradient(90deg, #667eea, #764ba2);
    width: 0;
    transition: width 0.3s ease;
}

.loading-bar.active::before {
    width: 70%;
}

.loading-bar.complete::before {
    width: 100%;
    transition: width 0.2s ease, opacity 0.3s ease 0.2s;
    opacity: 0;
}

/* Shimmer effect para cards */
.shimmer {
    position: relative;
    overflow: hidden;
    background: #f6f7f8;
}

.shimmer::after {
    content: '';
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    transform: translateX(-100%);
    background: linear-gradient(
        90deg,
        rgba(255, 255, 255, 0) 0,
        rgba(255, 255, 255, 0.2) 20%,
        rgba(255, 255, 255, 0.5) 60%,
        rgba(255, 255, 255, 0)
    );
    animation: shimmer 2s infinite;
}

@keyframes shimmer {
    100% {
        transform: translateX(100%);
    }
}

/* Estado de erro de carregamento */
.loading-error {
    padding: 2rem;
    text-align: center;
    color: #ef4444;
}

.loading-error i {
    font-size: 3rem;
    margin-bottom: 1rem;
}

.loading-error p {
    margin: 0.5rem 0;
}

.loading-error button {
    margin-top: 1rem;
    padding: 0.75rem 1.5rem;
    background: #667eea;
    color: white;
    border: none;
    border-radius: 8px;
    cursor: pointer;
    font-weight: 600;
}

.loading-error button:hover {
    background: #5568d3;
}

/* Responsive preloader */
@media (max-width: 768px) {
    .preloader-logo {
        font-size: 3rem;
    }
    
    .preloader-text {
        font-size: 1.2rem;
    }
    
    .preloader-spinner {
        width: 50px;
        height: 50px;
    }
    
    .preloader-progress {
        width: 80%;
    }
}
