/* ==========================================
   DESIGN SYSTEM - GLOBAL STABILITY ENGINE
   v1.0 - Visual Stability & Performance
   ==========================================
   
   PRINCÍPIOS:
   1. Layout-first → State → Render → Animate
   2. Nunca animar width, height, margin, padding
   3. Somente animar: transform, opacity, filter
   4. Todo elemento deve ter dimensões definidas
   5. Espaço reservado antes do carregamento
   ========================================== */

/* ==========================================
   1. GRID SYSTEM - 8px Base
   ========================================== */
:root {
    /* Grid Units (8px base) */
    --space-1: 8px;
    --space-2: 16px;
    --space-3: 24px;
    --space-4: 32px;
    --space-5: 40px;
    --space-6: 48px;
    --space-7: 56px;
    --space-8: 64px;
    --space-10: 80px;
    --space-12: 96px;
    --space-16: 128px;
    
    /* Container Max Widths */
    --container-xs: 320px;
    --container-sm: 480px;
    --container-md: 640px;
    --container-lg: 800px;
    --container-xl: 1000px;
    --container-2xl: 1200px;
    
    /* Fixed Component Sizes */
    --card-width-sm: 100px;
    --card-width-md: 140px;
    --card-width-lg: 180px;
    --card-width-xl: 240px;
    
    --card-aspect-ratio: 3 / 4;
    
    --modal-width-sm: 400px;
    --modal-width-md: 500px;
    --modal-width-lg: 700px;
    --modal-width-xl: 900px;
    
    --button-height-sm: 32px;
    --button-height-md: 40px;
    --button-height-lg: 48px;
    --button-height-xl: 56px;
    
    /* Loader Sizes */
    --loader-size-sm: 24px;
    --loader-size-md: 40px;
    --loader-size-lg: 56px;
    
    /* Animation Timings - Padronizados */
    --duration-instant: 100ms;
    --duration-fast: 200ms;
    --duration-normal: 300ms;
    --duration-slow: 500ms;
    --duration-slower: 800ms;
    
    /* Easing Functions */
    --ease-out: cubic-bezier(0.22, 1, 0.36, 1);
    --ease-in-out: cubic-bezier(0.4, 0, 0.2, 1);
    --ease-bounce: cubic-bezier(0.34, 1.56, 0.64, 1);
    --ease-linear: linear;
    
    /* Z-Index Scale */
    --z-base: 1;
    --z-dropdown: 100;
    --z-sticky: 200;
    --z-fixed: 500;
    --z-modal-backdrop: 9000;
    --z-modal: 10000;
    --z-tooltip: 11000;
    --z-effects: 12000;
    --z-debug: 99999;
}

/* ==========================================
   2. STABLE CONTAINER CLASSES
   ========================================== */

/* Container base - previne overflow e define contexto */
.stable-container {
    position: relative;
    box-sizing: border-box;
    contain: layout style;
}

/* Container com dimensões bloqueadas */
.locked-container {
    min-width: 0;
    max-width: 100%;
    overflow: hidden;
}

/* Flex container estável */
.stable-flex {
    display: flex;
    min-width: 0;
    min-height: 0;
}

/* Grid container estável */
.stable-grid {
    display: grid;
    min-width: 0;
    min-height: 0;
}

.stat-container {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    box-sizing: border-box;
    min-width: 0;
    max-width: 100%;
    overflow: hidden;
    position: relative;
}

.stat-container .stat-icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    line-height: 1;
    flex-shrink: 0;
}

.stat-container .stat-value {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    line-height: 1;
    white-space: nowrap;
    flex-shrink: 0;
    font-variant-numeric: tabular-nums;
}

/* ==========================================
   3. LOADER SYSTEM - CORRETO
   Ícone gira, texto NUNCA gira
   ========================================== */

/* Container do loader - NUNCA anima */
.loader-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: var(--space-2);
    padding: var(--space-4);
    min-height: 120px;
}

/* Spinner - ÚNICO elemento que gira */
.loader-spinner {
    width: var(--loader-size-md);
    height: var(--loader-size-md);
    min-width: var(--loader-size-md);
    min-height: var(--loader-size-md);
    border: 4px solid rgba(255, 255, 255, 0.15);
    border-top-color: var(--primary, #4fc3f7);
    border-radius: 50%;
    animation: loader-spin 0.8s var(--ease-linear) infinite;
    flex-shrink: 0;
}

.loader-spinner.small {
    width: var(--loader-size-sm);
    height: var(--loader-size-sm);
    min-width: var(--loader-size-sm);
    min-height: var(--loader-size-sm);
    border-width: 3px;
}

.loader-spinner.large {
    width: var(--loader-size-lg);
    height: var(--loader-size-lg);
    min-width: var(--loader-size-lg);
    min-height: var(--loader-size-lg);
    border-width: 5px;
}

/* Texto do loader - NUNCA anima rotação */
.loader-text {
    color: rgba(255, 255, 255, 0.8);
    font-size: 0.9rem;
    font-weight: 600;
    text-align: center;
    animation: none !important;
    transform: none !important;
}

/* Animação ÚNICA de spin */
@keyframes loader-spin {
    to { 
        transform: rotate(360deg); 
    }
}

/* Loader inline (ao lado do texto) */
.loader-inline {
    display: inline-flex;
    align-items: center;
    gap: var(--space-1);
}

.loader-inline .loader-spinner {
    width: 16px;
    height: 16px;
    min-width: 16px;
    min-height: 16px;
    border-width: 2px;
}

/* ==========================================
   4. SKELETON LOADERS - Espaço Reservado
   ========================================== */

/* Base skeleton */
.skeleton {
    background: linear-gradient(
        90deg,
        rgba(255, 255, 255, 0.06) 0%,
        rgba(255, 255, 255, 0.12) 50%,
        rgba(255, 255, 255, 0.06) 100%
    );
    background-size: 200% 100%;
    animation: skeleton-shimmer 1.5s var(--ease-in-out) infinite;
    border-radius: var(--space-1);
}

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

/* Skeleton para cards */
.skeleton-card {
    width: var(--card-width-md);
    aspect-ratio: var(--card-aspect-ratio);
    border-radius: 12px;
}

/* Skeleton para texto */
.skeleton-text {
    height: 1em;
    width: 100%;
    border-radius: 4px;
}

.skeleton-text.short { width: 60%; }
.skeleton-text.medium { width: 80%; }

/* Skeleton para avatar */
.skeleton-avatar {
    width: 48px;
    height: 48px;
    border-radius: 50%;
}

/* Skeleton para botão */
.skeleton-button {
    height: var(--button-height-md);
    width: 120px;
    border-radius: 20px;
}

/* ==========================================
   5. MODAL STABILITY SYSTEM
   ========================================== */

/* Modal backdrop - fixo */
.modal-stable {
    position: fixed;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: var(--space-3);
    background: rgba(0, 0, 0, 0.85);
    backdrop-filter: blur(8px);
    z-index: var(--z-modal);
    opacity: 0;
    visibility: hidden;
    transition: opacity var(--duration-normal) var(--ease-out),
                visibility var(--duration-normal);
}

.modal-stable.active {
    opacity: 1;
    visibility: visible;
}

/* Modal content - dimensões bloqueadas */
.modal-content-stable {
    position: relative;
    width: 100%;
    max-width: var(--modal-width-md);
    max-height: 90vh;
    background: linear-gradient(180deg, #1565c0 0%, #0d47a1 100%);
    border-radius: var(--space-3);
    border: 4px solid rgba(255, 255, 255, 0.3);
    box-shadow: 0 10px 0 rgba(0, 0, 0, 0.3), 0 15px 40px rgba(0, 0, 0, 0.5);
    overflow: hidden;
    display: flex;
    flex-direction: column;
    transform: translateY(20px) scale(0.95);
    opacity: 0;
    transition: transform var(--duration-normal) var(--ease-bounce),
                opacity var(--duration-normal) var(--ease-out);
}

.modal-stable.active .modal-content-stable {
    transform: translateY(0) scale(1);
    opacity: 1;
}

/* Modal header - altura fixa */
.modal-header-stable {
    flex-shrink: 0;
    padding: var(--space-2) var(--space-3);
    border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}

/* Modal body - scroll interno */
.modal-body-stable {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    padding: var(--space-3);
    min-height: 0;
}

/* Modal footer - altura fixa */
.modal-footer-stable {
    flex-shrink: 0;
    padding: var(--space-2) var(--space-3);
    border-top: 1px solid rgba(255, 255, 255, 0.1);
}

/* ==========================================
   6. ANIMATION RULES - SAFE PROPERTIES ONLY
   ========================================== */

/* Classes utilitárias para animações seguras */

/* Fade */
.animate-fade-in {
    animation: safe-fade-in var(--duration-normal) var(--ease-out) forwards;
}

@keyframes safe-fade-in {
    from { opacity: 0; }
    to { opacity: 1; }
}

/* Scale (usando transform) */
.animate-scale-in {
    animation: safe-scale-in var(--duration-normal) var(--ease-bounce) forwards;
}

@keyframes safe-scale-in {
    from { 
        opacity: 0;
        transform: scale(0.9);
    }
    to { 
        opacity: 1;
        transform: scale(1);
    }
}

/* Slide up (usando transform) */
.animate-slide-up {
    animation: safe-slide-up var(--duration-normal) var(--ease-out) forwards;
}

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

/* Pulse glow (usando filter/opacity) */
.animate-glow {
    animation: safe-glow 2s var(--ease-in-out) infinite;
}

@keyframes safe-glow {
    0%, 100% { 
        filter: drop-shadow(0 0 8px currentColor);
        opacity: 1;
    }
    50% { 
        filter: drop-shadow(0 0 20px currentColor);
        opacity: 0.9;
    }
}

/* Hover transitions - SAFE */
.hover-lift {
    transition: transform var(--duration-fast) var(--ease-out),
                opacity var(--duration-fast) var(--ease-out);
}

.hover-lift:hover {
    transform: translateY(-4px);
}

.hover-scale {
    transition: transform var(--duration-fast) var(--ease-bounce);
}

.hover-scale:hover {
    transform: scale(1.05);
}

/* ==========================================
   7. CARD STABILITY RULES
   ========================================== */

/* Card com aspect ratio bloqueado */
.card-stable {
    position: relative;
    width: 100%;
    max-width: var(--card-width-md);
    aspect-ratio: var(--card-aspect-ratio);
    min-height: 0;
    overflow: hidden;
    contain: layout style paint;
}

/* Card sizes */
.card-stable.size-sm { max-width: var(--card-width-sm); }
.card-stable.size-lg { max-width: var(--card-width-lg); }
.card-stable.size-xl { max-width: var(--card-width-xl); }

/* ==========================================
   8. BUTTON STABILITY
   ========================================== */

.btn-stable {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: var(--space-1);
    min-height: var(--button-height-md);
    padding: 0 var(--space-3);
    border: none;
    border-radius: 25px;
    font-weight: 600;
    white-space: nowrap;
    cursor: pointer;
    transition: transform var(--duration-fast) var(--ease-out),
                filter var(--duration-fast) var(--ease-out),
                opacity var(--duration-fast) var(--ease-out);
}

.btn-stable:hover:not(:disabled) {
    transform: translateY(-2px);
}

.btn-stable:active:not(:disabled) {
    transform: translateY(0) scale(0.98);
}

.btn-stable:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

/* Button loading state */
.btn-stable.loading {
    pointer-events: none;
    position: relative;
}

.btn-stable.loading .btn-text {
    opacity: 0;
}

.btn-stable.loading::after {
    content: '';
    position: absolute;
    width: 20px;
    height: 20px;
    border: 2px solid rgba(255, 255, 255, 0.3);
    border-top-color: #fff;
    border-radius: 50%;
    animation: loader-spin 0.8s linear infinite;
}

/* ==========================================
   9. GRID LAYOUTS - ESTÁVEIS
   ========================================== */

/* Grid responsivo para cards */
.cards-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(var(--card-width-md), 1fr));
    gap: var(--space-2);
    width: 100%;
}

/* Grid com colunas fixas */
.grid-cols-2 { grid-template-columns: repeat(2, 1fr); }
.grid-cols-3 { grid-template-columns: repeat(3, 1fr); }
.grid-cols-4 { grid-template-columns: repeat(4, 1fr); }

/* ==========================================
   10. EFFECTS LAYER (GLOBAL)
   Partículas e efeitos NUNCA são filhos de containers que mudam
   ========================================== */

#effects-layer {
    position: fixed;
    inset: 0;
    pointer-events: none;
    z-index: var(--z-effects);
    overflow: hidden;
}

#effects-layer * {
    pointer-events: none;
}

/* ==========================================
   11. UI DEBUG MODE
   ========================================== */

/* Ativar com: body.debug-ui */
body.debug-ui * {
    outline: 1px solid rgba(255, 0, 0, 0.3) !important;
}

body.debug-ui .modal,
body.debug-ui .modal-content {
    outline: 2px solid lime !important;
}

body.debug-ui [class*="card"] {
    outline: 2px solid cyan !important;
}

body.debug-ui [class*="btn"] {
    outline: 2px solid yellow !important;
}

/* Debug overlay */
#debug-overlay {
    position: fixed;
    top: var(--space-1);
    right: var(--space-1);
    padding: var(--space-1) var(--space-2);
    background: rgba(0, 0, 0, 0.9);
    color: #0f0;
    font-family: monospace;
    font-size: 12px;
    border-radius: var(--space-1);
    z-index: var(--z-debug);
    pointer-events: none;
    display: none;
}

body.debug-ui #debug-overlay {
    display: block;
}

/* ==========================================
   12. PREVENT LAYOUT SHIFT
   ========================================== */

/* Prevent images from causing layout shift */
img {
    max-width: 100%;
    height: auto;
}

/* Font rendering optimization */
html {
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    text-rendering: optimizeLegibility;
}

/* ==========================================
   13. ANTI-SHAKE / ANTI-JITTER
   ========================================== */

/* Previne sub-pixel rendering issues */
.no-jitter {
    transform: translateZ(0);
    backface-visibility: hidden;
    -webkit-font-smoothing: antialiased;
}

/* Hardware acceleration para elementos animados */
.gpu-accelerated {
    will-change: transform, opacity;
    transform: translateZ(0);
}

/* Desabilita will-change quando não hover (performance) */
.gpu-on-hover {
    will-change: auto;
}

.gpu-on-hover:hover {
    will-change: transform, opacity;
}

/* ==========================================
   14. SCROLLBAR STABILITY
   ========================================== */

/* Reserva espaço para scrollbar */
.scroll-stable {
    overflow-y: scroll;
    scrollbar-gutter: stable;
}

/* Scroll suave */
.scroll-smooth {
    scroll-behavior: smooth;
}

/* ==========================================
   15. RESPONSIVE BREAKPOINTS
   ========================================== */

@media (max-width: 480px) {
    :root {
        --card-width-md: 110px;
        --modal-width-md: 95vw;
    }
    
    .cards-grid {
        grid-template-columns: repeat(auto-fill, minmax(100px, 1fr));
        gap: var(--space-1);
    }
}

@media (min-width: 481px) and (max-width: 768px) {
    :root {
        --card-width-md: 130px;
    }
}

@media (min-width: 769px) {
    :root {
        --card-width-md: 150px;
    }
}

/* ==========================================
   16. UTILITY CLASSES
   ========================================== */

/* Display */
.hidden { display: none !important; }
.visible { visibility: visible !important; }
.invisible { visibility: hidden !important; }

/* Flex utilities */
.flex { display: flex; }
.flex-center { 
    display: flex; 
    align-items: center; 
    justify-content: center; 
}
.flex-col { flex-direction: column; }
.flex-wrap { flex-wrap: wrap; }
.flex-1 { flex: 1; }
.flex-shrink-0 { flex-shrink: 0; }

/* Gap utilities */
.gap-1 { gap: var(--space-1); }
.gap-2 { gap: var(--space-2); }
.gap-3 { gap: var(--space-3); }
.gap-4 { gap: var(--space-4); }

/* Padding utilities */
.p-1 { padding: var(--space-1); }
.p-2 { padding: var(--space-2); }
.p-3 { padding: var(--space-3); }
.p-4 { padding: var(--space-4); }

/* Text */
.text-center { text-align: center; }
.text-left { text-align: left; }
.text-right { text-align: right; }

/* Overflow */
.overflow-hidden { overflow: hidden; }
.overflow-auto { overflow: auto; }
.overflow-scroll { overflow: scroll; }

/* Position */
.relative { position: relative; }
.absolute { position: absolute; }
.fixed { position: fixed; }

/* Width */
.w-full { width: 100%; }
.w-auto { width: auto; }

/* Height */
.h-full { height: 100%; }
.h-auto { height: auto; }
