/**
 * RL STOCK - Layout CSS
 * Estilos do layout principal da aplicação
 * Refatorado em: 18/12/2024 - v3.9.1
 * 
 * ARQUITETURA:
 * - nav-wrapper usa wire:ignore.self para evitar re-render do Alpine
 * - NÃO USAR @persist - causa erro 500 na compilação Blade
 * - Alpine.js gerencia estado via $persist (localStorage)
 * - CSS fornece transições suaves e responsividade
 */

/* ========== RESET & UNIVERSAL COMPATIBILITY ========== */
*, *::before, *::after { box-sizing: border-box; }
html { 
    -webkit-text-size-adjust: 100%; 
    -webkit-tap-highlight-color: transparent;
}
body { 
    overscroll-behavior-y: contain;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    text-rendering: optimizeLegibility;
}

/* ========== SAFE AREAS (Notch/Dynamic Island) ========== */
.safe-area-top { padding-top: env(safe-area-inset-top, 0); }
.safe-area-bottom { padding-bottom: env(safe-area-inset-bottom, 0); }
.safe-area-left { padding-left: env(safe-area-inset-left, 0); }
.safe-area-right { padding-right: env(safe-area-inset-right, 0); }

/* ========== TOUCH OPTIMIZATION ========== */
button, a, [role="button"] {
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
    cursor: pointer;
}
input, select, textarea {
    touch-action: manipulation;
    -webkit-appearance: none;
    -moz-appearance: none;
    appearance: none;
}

/* Prevent iOS zoom on input focus */
@media screen and (max-width: 767px) {
    input, select, textarea { font-size: 16px !important; }
}

/* ========== PREVENT UNWANTED BEHAVIORS ========== */
.no-select {
    -webkit-user-select: none;
    -moz-user-select: none;
    user-select: none;
}

/* Disable pull-to-refresh on PWA */
body.pwa-standalone {
    overscroll-behavior-y: none;
}

/* ========== PWA INSTALL BANNER ========== */
.pwa-install-banner {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    z-index: 9999;
    transform: translateY(100%);
    transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}
.pwa-install-banner.show {
    transform: translateY(0);
}

/* ========== CONNECTION STATUS ========== */
.connection-status {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    z-index: 9998;
    padding: 0.5rem;
    text-align: center;
    font-size: 0.75rem;
    font-weight: 600;
    transform: translateY(-100%);
    transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}
.connection-status.offline {
    background: linear-gradient(90deg, #ef4444, #dc2626);
    color: white;
    transform: translateY(0);
}

/* ========== SIDEBAR OPTIMIZATIONS ========== */
.sidebar-container {
    backface-visibility: hidden;
    -webkit-backface-visibility: hidden;
    will-change: transform, width;
    contain: layout style;
    overflow: hidden;
    /* GPU acceleration via will-change, não transform para não conflitar com Tailwind translate-x */
}

/* Sidebar collapsed state - itens centralizados */
#app-sidebar.w-16 nav a {
    justify-content: center !important;
    padding-left: 0 !important;
    padding-right: 0 !important;
}

#app-sidebar.w-16 nav a svg {
    margin-right: 0 !important;
}

#app-sidebar.w-16 nav a span {
    display: none !important;
}

#app-sidebar.w-16 nav p {
    display: none !important;
}

.sidebar-container svg {
    flex-shrink: 0;
    width: 1.25rem;
    height: 1.25rem;
}

/* Smooth sidebar transitions */
.sidebar-slide {
    transition: none;
}
.sidebar-ready .sidebar-slide {
    transition: transform 0.2s cubic-bezier(0.4, 0, 0.2, 1);
}

@media (min-width: 1024px) {
    .sidebar-width {
        transition: none;
    }
    .sidebar-ready .sidebar-width {
        transition: width 0.15s cubic-bezier(0.4, 0, 0.2, 1);
    }
}

/* Prevent layout shift */
[data-turbo-permanent] {
    contain: layout style;
}

/* Main content transition */
.main-content {
    opacity: 1;
    transition: opacity 0.1s ease-out;
}
.main-content.loading {
    opacity: 0.7;
}

/* Main content wrapper - responsividade fluida */
.main-content-wrapper {
    will-change: margin-left;
    transform: translateZ(0);
    min-height: 100vh;
    min-height: 100dvh; /* Dynamic viewport height para mobile */
    /* Valores padrão - JS sobrescreve inline */
    margin-left: 16rem;
    padding-top: 0;
    transition: margin-left 0.15s cubic-bezier(0.4, 0, 0.2, 1);
}

/* Mobile: no left margin, has top padding */
@media (max-width: 1023px) {
    .main-content-wrapper {
        margin-left: 0 !important;
        padding-top: 3.5rem !important;
        transition: none;
    }
}

/* Aproveitamento máximo da tela */
.main-content {
    width: 100%;
    max-width: 100%;
}

@media (min-width: 1280px) {
    .main-content > div {
        max-width: 100%;
    }
}

/* ========== OVERLAY OPTIMIZATION ========== */
.overlay-backdrop {
    backdrop-filter: blur(4px);
    -webkit-backdrop-filter: blur(4px);
}

@media (prefers-reduced-motion: reduce) {
    .overlay-backdrop {
        backdrop-filter: none;
        -webkit-backdrop-filter: none;
        background: rgba(0, 0, 0, 0.6);
    }
}

/* ========== ANIMATIONS ========== */
@keyframes fadeIn {
    from { opacity: 0; }
    to { opacity: 1; }
}

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

.animate-fade-in { animation: fadeIn 0.2s ease-out; }
.animate-slide-up { animation: slideUp 0.2s ease-out; }

/* Reduce animations on mobile */
@media (max-width: 768px), (prefers-reduced-motion: reduce) {
    *, *::before, *::after {
        animation-duration: 0.1s !important;
        transition-duration: 0.1s !important;
    }
    .animate-pulse { animation: none !important; }
    .backdrop-blur-sm, .backdrop-blur-xl {
        backdrop-filter: none !important;
        -webkit-backdrop-filter: none !important;
    }
}

/* ========== LOADING STATES ========== */
.skeleton {
    background: linear-gradient(90deg, #f1f5f9 25%, #e2e8f0 50%, #f1f5f9 75%);
    background-size: 200% 100%;
    animation: skeleton-loading 1.5s infinite;
}
@keyframes skeleton-loading {
    0% { background-position: 200% 0; }
    100% { background-position: -200% 0; }
}

/* ========== TABLET OPTIMIZATIONS (600px - 900px) ========== */
@media (min-width: 600px) and (max-width: 900px) {
    #app-sidebar {
        width: 280px !important;
    }
    #app-sidebar nav a {
        padding: 0.875rem 1rem !important;
        font-size: 0.9375rem !important;
    }
    #app-sidebar nav svg {
        width: 1.5rem !important;
        height: 1.5rem !important;
    }
    #app-sidebar .border-t {
        padding: 1rem !important;
    }
}

/* Mobile e tablets pequenos (até 1024px) */
@media (max-width: 1024px) {
    .shadow-2xl { 
        box-shadow: 0 10px 25px -5px rgba(0, 0, 0, 0.1), 0 8px 10px -6px rgba(0, 0, 0, 0.1); 
    }
    .backdrop-blur-xl, .backdrop-blur-sm {
        backdrop-filter: blur(8px);
        -webkit-backdrop-filter: blur(8px);
    }
    
    /* Sidebar mobile - transição suave tipo app nativo */
    #app-sidebar {
        transition: transform 0.25s cubic-bezier(0.32, 0.72, 0, 1) !important;
        will-change: transform;
        backface-visibility: hidden;
        -webkit-backface-visibility: hidden;
    }
    
    #app-sidebar.-translate-x-full {
        transform: translateX(-100%) !important;
    }
    
    #app-sidebar.translate-x-0 {
        transform: translateX(0) !important;
    }
    
    .sidebar-overlay {
        transition: opacity 0.2s ease-out;
        backdrop-filter: blur(2px);
        -webkit-backdrop-filter: blur(2px);
    }
    
    /* Bloqueia scroll quando sidebar aberta */
    body.sidebar-open {
        overflow: hidden !important;
        position: fixed;
        width: 100%;
        height: 100%;
    }
    
    /* Menu toggle button - feedback tátil */
    .menu-toggle-btn {
        -webkit-tap-highlight-color: transparent;
        touch-action: manipulation;
    }
    
    .menu-toggle-btn:active {
        transform: scale(0.92);
    }
}

/* Phones pequenos (até 380px) */
@media (max-width: 380px) {
    #app-sidebar {
        width: 85vw !important;
        max-width: 280px !important;
    }
}

/* ========== ALPINE.JS CLOAK ========== */
[x-cloak] { display: none !important; }

/* ========== LIVEWIRE LOADING STATES ========== */
[wire\:loading], [wire\:loading\.delay] {
    display: none;
}
[wire\:loading\.delay\.shortest] { transition-delay: 50ms; }
[wire\:loading\.delay\.shorter] { transition-delay: 100ms; }
[wire\:loading\.delay\.short] { transition-delay: 150ms; }
[wire\:loading\.delay\.long] { transition-delay: 300ms; }
[wire\:loading\.delay\.longer] { transition-delay: 500ms; }
[wire\:loading\.delay\.longest] { transition-delay: 1000ms; }

/* Livewire loading indicator */
.livewire-loading {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    height: 3px;
    background: linear-gradient(90deg, #10b981, #34d399, #10b981);
    background-size: 200% 100%;
    animation: livewire-progress 1s ease-in-out infinite;
    z-index: 9999;
    opacity: 0;
    transition: opacity 0.2s ease-out;
}
.livewire-loading.active {
    opacity: 1;
}
@keyframes livewire-progress {
    0% { background-position: 200% 0; }
    100% { background-position: -200% 0; }
}

.livewire-request-pending {
    pointer-events: none;
    opacity: 0.7;
}

/* ========== SIDEBAR LINK FEEDBACK ========== */
#app-sidebar nav a {
    position: relative;
    overflow: hidden;
}
#app-sidebar nav a::after {
    content: '';
    position: absolute;
    inset: 0;
    background: rgba(255, 255, 255, 0.1);
    opacity: 0;
    transition: opacity 0.1s ease-out;
}
#app-sidebar nav a:active::after {
    opacity: 1;
}

@media (pointer: coarse) {
    #app-sidebar nav a:active {
        transform: scale(0.98);
    }
}

/* ========== PAGE TRANSITIONS ========== */
.page-enter {
    opacity: 0;
    transform: translateY(8px);
}
.page-enter-active {
    opacity: 1;
    transform: translateY(0);
    transition: opacity 0.15s ease-out, transform 0.15s ease-out;
}

/* ========== SCROLLBAR STYLING ========== */
.custom-scrollbar::-webkit-scrollbar {
    width: 6px;
    height: 6px;
}
.custom-scrollbar::-webkit-scrollbar-track {
    background: transparent;
}
.custom-scrollbar::-webkit-scrollbar-thumb {
    background: rgba(148, 163, 184, 0.5);
    border-radius: 3px;
}
.custom-scrollbar::-webkit-scrollbar-thumb:hover {
    background: rgba(148, 163, 184, 0.7);
}

/* ========== SIDEBAR INITIAL STATE (antes do Alpine carregar) ========== */
/* Evita flash de layout enquanto Alpine carrega */
@media (min-width: 1024px) {
    .sidebar-collapsed-initial #app-sidebar {
        width: 4rem !important;
    }
    .sidebar-collapsed-initial #main-content-wrapper {
        margin-left: 4rem !important;
    }
}

/* Body classes para hooks CSS (sidebar controlada pelo Alpine via w-16/w-64) */

/* Estado collapsed - body hook */
body.sidebar-collapsed .main-content-wrapper {
    margin-left: 4rem !important;
}

/* Estado expanded - body hook */
body.sidebar-expanded .main-content-wrapper {
    margin-left: 16rem !important;
}

/* Mobile override - sempre sem margin */
body.is-mobile .main-content-wrapper {
    margin-left: 0 !important;
    padding-top: 3.5rem !important;
}

/* Sidebar state indicators */
#app-sidebar.is-collapsed {
    box-shadow: 2px 0 8px rgba(0, 0, 0, 0.1);
}

#app-sidebar.is-expanded {
    box-shadow: 4px 0 12px rgba(0, 0, 0, 0.15);
}

/* ========== WIDESCREEN OPTIMIZATIONS (1440px+) ========== */
@media (min-width: 1440px) {
    .main-content {
        padding-left: 1.5rem;
        padding-right: 1.5rem;
    }
}

@media (min-width: 1920px) {
    .main-content {
        padding-left: 2rem;
        padding-right: 2rem;
    }
}

/* ========== ULTRA-WIDE (2560px+) ========== */
@media (min-width: 2560px) {
    /* Ultra-wide usa sidebar maior - Alpine controla via classes */
    #app-sidebar.w-64 {
        width: 18rem !important;
    }
    #app-sidebar.w-16 {
        width: 5rem !important;
    }
}

/* ========== UTILITIES ========== */
.hover\:scale-\[1\.02\]:hover { transform: scale(1.02); }

/* Full height utilities */
.h-screen-safe {
    height: 100vh;
    height: 100dvh;
}

/* Container responsivo */
.container-responsive {
    width: 100%;
    padding-left: 1rem;
    padding-right: 1rem;
}

@media (min-width: 640px) {
    .container-responsive {
        padding-left: 1.5rem;
        padding-right: 1.5rem;
    }
}

@media (min-width: 1024px) {
    .container-responsive {
        padding-left: 2rem;
        padding-right: 2rem;
    }
}
