/* ========================================
   BS CUSTOM - MOBILE PERFORMANCE 2026
   Optimisations Core Web Vitals
   LCP, CLS, INP optimizations
   ======================================== */

/* ==================== CRITICAL: PREVENT LAYOUT SHIFTS (CLS) ==================== */

/* Reserve space for images to prevent CLS */
@media (max-width: 980px) {
  /* Hero images - fixed aspect ratio */
  .hero {
    aspect-ratio: auto;
    contain: layout style;
  }

  .hero-slide,
  .hero-bg {
    position: absolute;
    inset: 0;
  }

  /* Vehicle card images */
  .vehicle-media {
    aspect-ratio: 16/10;
    height: auto !important;
    min-height: 180px;
    contain: layout;
  }

  .vehicle-media img {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    object-fit: cover;
  }

  /* Shop card images */
  .shop-card-media {
    aspect-ratio: 4/3;
    position: relative;
    contain: layout;
  }

  .shop-card-media img {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    object-fit: cover;
  }

  /* Gallery images */
  .gallery-item {
    aspect-ratio: 1/1;
    position: relative;
    contain: layout;
  }

  .gallery-item img {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    object-fit: cover;
  }

  /* Service cards */
  .svc-card {
    aspect-ratio: 16/9;
    min-height: auto;
    contain: layout;
  }

  /* Instagram feed items */
  .instagram-item {
    aspect-ratio: 1/1;
    contain: layout;
  }

  /* Brand logo */
  .brand-mark {
    width: 38px;
    height: 38px;
    flex-shrink: 0;
  }

  .brand-mark img {
    width: 100%;
    height: 100%;
    object-fit: cover;
  }
}

/* ==================== OPTIMIZE LCP (Largest Contentful Paint) ==================== */

@media (max-width: 980px) {
  /* Hero background - optimize rendering */
  .hero-slide {
    background-size: cover;
    background-position: center;
    background-repeat: no-repeat;
    /* Hint browser to prioritize */
    content-visibility: visible;
  }

  /* First hero slide is LCP - prioritize */
  .hero-slide.is-active {
    z-index: 1;
  }

  /* Defer non-critical hero slides */
  .hero-slide:not(.is-active) {
    content-visibility: auto;
  }

  /* Optimize text rendering for LCP text */
  .hero-title,
  .hero-word,
  .hero-tagline {
    text-rendering: optimizeSpeed;
    font-display: swap;
  }
}

/* ==================== OPTIMIZE INP (Interaction to Next Paint) ==================== */

@media (max-width: 980px) {
  /* Ensure fast touch response */
  .btn,
  .filter-btn,
  .mobile-nav-item,
  .vehicle-card,
  .shop-card,
  a,
  button {
    /* Fast tap response */
    touch-action: manipulation;

    /* Prevent 300ms tap delay */
    -webkit-tap-highlight-color: transparent;

    /* Optimize for compositing */
    will-change: transform;
    transform: translateZ(0);
  }

  /* Optimize transitions for 60fps */
  .btn,
  .filter-btn,
  .vehicle-card,
  .shop-card,
  .svc-card {
    transition-property: transform, opacity, background-color, border-color, box-shadow;
    transition-duration: 0.15s;
    transition-timing-function: cubic-bezier(0.4, 0, 0.2, 1);
  }

  /* Use GPU-accelerated properties only */
  .mobile-nav-item:active,
  .btn:active,
  .filter-btn:active,
  .vehicle-card:active,
  .shop-card:active {
    transform: scale(0.97) translateZ(0);
  }
}

/* ==================== CONTAIN FOR PERFORMANCE ==================== */

@media (max-width: 980px) {
  /* Contain layout for major sections */
  .section {
    contain: layout style;
  }

  .footer {
    contain: layout style;
  }

  /* Strict containment for cards */
  .vehicle-card,
  .shop-card,
  .svc-card {
    contain: layout style paint;
  }

  /* Mobile nav containment */
  .mobile-bottom-nav {
    contain: layout style;
    isolation: isolate;
  }
}

/* ==================== CONTENT VISIBILITY (Auto-load) ==================== */

@media (max-width: 980px) {
  /* Defer rendering of below-fold content */
  .section:not(:first-of-type) {
    content-visibility: auto;
    contain-intrinsic-size: auto 500px;
  }

  .footer {
    content-visibility: auto;
    contain-intrinsic-size: auto 400px;
  }

  /* Gallery and Instagram - defer */
  .gallery-grid,
  .instagram-feed {
    content-visibility: auto;
    contain-intrinsic-size: auto 300px;
  }
}

/* ==================== FONT OPTIMIZATION ==================== */

@media (max-width: 980px) {
  /* Optimize font rendering */
  body {
    font-synthesis: none;
    text-rendering: optimizeLegibility;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
  }

  /* Prevent font FOUT/FOIT */
  .hero-title,
  .hero-word,
  h1, h2, h3 {
    font-display: swap;
  }
}

/* ==================== IMAGE OPTIMIZATION ==================== */

@media (max-width: 980px) {
  /* Lazy load images by default */
  img[loading="lazy"] {
    /* Fade in when loaded */
    opacity: 1;
    transition: opacity 0.3s ease;
  }

  img[loading="lazy"]:not([src]) {
    opacity: 0;
  }

  /* Optimize image rendering */
  img {
    image-rendering: -webkit-optimize-contrast;
    image-rendering: crisp-edges;

    /* Prevent layout shift */
    max-width: 100%;
    height: auto;
  }

  /* Background images optimization */
  [style*="background-image"] {
    background-size: cover;
    background-position: center;
    background-repeat: no-repeat;
  }
}

/* ==================== SCROLL OPTIMIZATION ==================== */

@media (max-width: 980px) {
  /* Smooth scroll with performance in mind */
  html {
    scroll-behavior: smooth;
  }

  /* Optimize scrolling containers */
  .filters,
  .modal-content,
  #modalContent,
  .booking-modal-content > div:nth-of-type(2) {
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
    scroll-behavior: smooth;
  }

  /* Hide scrollbars but keep functionality */
  .filters::-webkit-scrollbar {
    display: none;
  }

  .filters {
    scrollbar-width: none;
    -ms-overflow-style: none;
  }
}

/* ==================== ANIMATION PERFORMANCE ==================== */

@media (max-width: 980px) {
  /* Use transform and opacity only for animations */
  @keyframes fadeIn {
    from {
      opacity: 0;
      transform: translateY(10px);
    }
    to {
      opacity: 1;
      transform: translateY(0);
    }
  }

  @keyframes slideIn {
    from {
      transform: translateX(-100%);
    }
    to {
      transform: translateX(0);
    }
  }

  /* Ensure animations use GPU */
  .hero-slide {
    transform: translateZ(0);
    backface-visibility: hidden;
    perspective: 1000px;
  }

  /* Reduce animation complexity on low-end devices */
  @media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
      animation-duration: 0.01ms !important;
      animation-iteration-count: 1 !important;
      transition-duration: 0.01ms !important;
      scroll-behavior: auto !important;
    }
  }
}

/* ==================== PAINT OPTIMIZATION ==================== */

@media (max-width: 980px) {
  /* Isolate elements that change frequently */
  .mobile-bottom-nav,
  .nav,
  .hero-dots,
  .hero-slide {
    isolation: isolate;
  }

  /* Optimize layers */
  .hero {
    transform: translateZ(0);
  }

  /* Prevent unnecessary repaints */
  .btn:hover,
  .filter-btn:hover,
  .vehicle-card:hover,
  .shop-card:hover {
    will-change: transform;
  }

  .btn:not(:hover),
  .filter-btn:not(:hover),
  .vehicle-card:not(:hover),
  .shop-card:not(:hover) {
    will-change: auto;
  }
}

/* ==================== FORM OPTIMIZATION ==================== */

@media (max-width: 980px) {
  /* Optimize form inputs */
  input,
  textarea,
  select {
    /* Prevent zoom on iOS */
    font-size: 16px !important;

    /* Fast focus response */
    outline: none;
    -webkit-appearance: none;
    appearance: none;
  }

  /* Focus states optimized */
  input:focus,
  textarea:focus,
  select:focus {
    /* Use border instead of box-shadow for performance */
    border-color: rgba(198, 169, 109, 0.5);
    outline: 2px solid rgba(198, 169, 109, 0.2);
    outline-offset: 2px;
  }
}

/* ==================== MODAL PERFORMANCE ==================== */

@media (max-width: 767px) {
  /* Optimize modal rendering */
  #bookingModal,
  #modalOverlay {
    /* Hide when not visible */
    visibility: hidden;
    pointer-events: none;
  }

  #bookingModal[style*="display: flex"],
  #bookingModal[style*="display:flex"],
  #modalOverlay[style*="display: flex"],
  #modalOverlay[style*="display:flex"] {
    visibility: visible;
    pointer-events: auto;
  }

  /* GPU acceleration for modal animation */
  .booking-modal-content {
    transform: translateZ(0);
    backface-visibility: hidden;
  }

  /* Optimize modal scroll */
  .booking-modal-content > div:nth-of-type(2) {
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
  }
}

/* ==================== BATTERY OPTIMIZATION ==================== */

@media (max-width: 980px) {
  /* Reduce power consumption */
  @media (prefers-reduced-motion: reduce) {
    .hero-slide {
      animation: none;
    }

    .mobile-bottom-nav {
      animation: none;
    }
  }

  /* Dark mode efficiency */
  @media (prefers-color-scheme: dark) {
    /* Already dark, minimal changes needed */
  }
}

/* ==================== NETWORK OPTIMIZATION ==================== */

@media (max-width: 980px) {
  /* Save-data mode support */
  @media (prefers-reduced-data: reduce) {
    /* Hide decorative images */
    .hero-slide:not(.is-active) {
      display: none;
    }

    /* Simplify backgrounds */
    .svc-card__media {
      opacity: 0.5;
    }

    /* Reduce animation */
    * {
      animation: none !important;
      transition: none !important;
    }
  }
}

/* ==================== MEMORY OPTIMIZATION ==================== */

@media (max-width: 980px) {
  /* Limit simultaneous animations */
  .hero-slide {
    animation-play-state: paused;
  }

  .hero-slide.is-active {
    animation-play-state: running;
  }

  /* Clean up off-screen content */
  .section:not(:first-of-type) {
    content-visibility: auto;
  }
}

/* ==================== CRITICAL CSS INLINE HELPERS ==================== */

/* These styles should be inlined in <head> for fastest LCP */
.critical-hero {
  display: block;
  min-height: 50vh;
  background: #000;
}

.critical-nav {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: 56px;
  background: rgba(15, 15, 15, 0.85);
  z-index: 100;
}

.critical-text {
  font-family: system-ui, -apple-system, sans-serif;
  color: #F8F8F8;
}

/* ==================== DEBUG HELPERS (Remove in production) ==================== */

/*
.debug-cls * {
  outline: 1px solid red;
}

.debug-lcp .hero {
  outline: 3px solid green;
}

.debug-perf * {
  transition: none !important;
  animation: none !important;
}
*/
