/* Performance and Error Prevention CSS */

/* Critical CSS for above-the-fold content */
.critical-content {
    opacity: 1;
    visibility: visible;
}

/* Loading states */
.loading {
    opacity: 0;
    transition: opacity 0.3s ease-in-out;
}

.loaded {
    opacity: 1;
}

/* Image optimization */
img {
    max-width: 100%;
    height: auto;
    display: block;
    /* Prevent layout shift */
    min-height: 1px;
}

/* Lazy loading placeholder */
img[data-src] {
    background: linear-gradient(90deg, #f0f0f0 25%, #e0e0e0 50%, #f0f0f0 75%);
    background-size: 200% 100%;
    animation: loading-shimmer 1.5s infinite;
    min-height: 100px;
}

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

/* Smooth scrolling */
html {
    scroll-behavior: smooth;
    scroll-padding-top: 80px; /* Account for fixed header */
}

/* Font optimization */
body {
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    text-rendering: optimizeLegibility;
    font-display: swap; /* Improve font loading performance */
}

/* Reduce layout thrashing */
* {
    box-sizing: border-box;
}

/* GPU acceleration for animations */
.animate,
.carousel-item,
.modal,
.dropdown-menu {
    transform: translateZ(0);
    backface-visibility: hidden;
    perspective: 1000px;
}

/* Optimize carousel performance */
.owl-carousel .owl-item {
    backface-visibility: hidden;
    transform: translateZ(0);
}

/* Smooth transitions */
.transition-smooth {
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

/* Prevent FOUC (Flash of Unstyled Content) */
.no-js .js-only {
    display: none !important;
}

.js .no-js-only {
    display: none !important;
}

/* Scroll animations */
[data-scroll-animation] {
    opacity: 0;
    transform: translateY(30px);
    transition: all 0.6s cubic-bezier(0.25, 0.46, 0.45, 0.94);
}

[data-scroll-animation].scroll-animated {
    opacity: 1;
    transform: translateY(0);
}

/* Stagger animation delays for multiple elements */
[data-scroll-animation]:nth-child(1) { transition-delay: 0.1s; }
[data-scroll-animation]:nth-child(2) { transition-delay: 0.2s; }
[data-scroll-animation]:nth-child(3) { transition-delay: 0.3s; }
[data-scroll-animation]:nth-child(4) { transition-delay: 0.4s; }
[data-scroll-animation]:nth-child(5) { transition-delay: 0.5s; }

/* Optimize button interactions */
.btn {
    transition: all 0.2s ease-in-out;
    transform: translateZ(0); /* Force GPU acceleration */
}

.btn:hover {
    transform: translateZ(0) translateY(-1px);
}

.btn:active {
    transform: translateZ(0) translateY(0);
}

/* Optimize modal performance */
.modal {
    will-change: transform, opacity;
}

.modal.fade {
    transition: opacity 0.15s linear, transform 0.15s ease-out;
}

/* Prevent layout shift for carousels */
.carousel,
.owl-carousel {
    min-height: 300px; /* Adjust based on your content */
}

/* Optimize form animations */
.form-group {
    position: relative;
}

.form-control:focus {
    transform: translateZ(0);
    transition: all 0.2s ease-in-out;
}

/* Loading spinner optimization */
.spinner {
    animation: spin 1s linear infinite;
    will-change: transform;
}

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

/* Intersection Observer fallback */
@media (prefers-reduced-motion: reduce) {
    * {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }
    
    [data-scroll-animation] {
        opacity: 1;
        transform: none;
    }
}

/* Critical path CSS for above-the-fold */
.hero-section,
.header,
.navigation {
    contain: layout style paint;
}

/* Optimize third-party embeds */
iframe {
    /* Remove loading: lazy as it's not a CSS property */
    width: 100%;
    height: auto;
}

/* Performance hints for browsers */
.carousel-inner {
    contain: layout;
}

.modal-body {
    contain: layout style;
}

/* Prevent reflow on hover states */
.nav-link:hover,
.dropdown-toggle:hover {
    text-decoration: none;
    contain: layout;
}

/* Optimize background images */
.bg-image {
    background-attachment: scroll; /* Better performance than fixed */
    background-size: cover;
    background-position: center;
    background-repeat: no-repeat;
}

/* Error state styling */
.error-fallback {
    display: none;
    padding: 20px;
    background: #f8f9fa;
    border: 1px solid #dee2e6;
    border-radius: 4px;
    text-align: center;
    color: #6c757d;
}

.has-error .error-fallback {
    display: block;
}

/* Hide broken images gracefully */
img {
    position: relative;
}

img:after {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: #f8f9fa url('data:image/svg+xml;charset=utf-8,<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="3" y="3" width="18" height="18" rx="2" ry="2"/><circle cx="8.5" cy="8.5" r="1.5"/><polyline points="21,15 16,10 5,21"/></svg>') center/24px no-repeat;
    border: 1px solid #dee2e6;
    display: none;
}

img[src=""],
img:not([src]),
img[src*="undefined"] {
    display: none;
}

/* Performance monitoring */
.perf-monitor {
    position: fixed;
    top: 10px;
    right: 10px;
    background: rgba(0,0,0,0.8);
    color: white;
    padding: 5px 10px;
    font-size: 12px;
    border-radius: 4px;
    z-index: 9999;
    display: none;
}

.debug-mode .perf-monitor {
    display: block;
}
