/* ============================================
   Mobile-First Performance Optimizations
   ============================================ */

/* Critical CSS - Load First */
* {
  -webkit-tap-highlight-color: transparent;
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  user-select: none;
}

input, textarea, [contenteditable] {
  -webkit-user-select: text;
  user-select: text;
}

/* Mobile-First Breakpoints */
/* Base: Mobile (320px+) */
/* Tablet: 768px+ */
/* Desktop: 1024px+ */

/* Performance: Reduce Motion for 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;
  }
}

/* Performance: Disable Animations on Low-End Devices */
@media (prefers-reduced-motion: reduce) {
  .fade-in,
  .slide-in,
  .scale-in {
    animation: none !important;
  }
}

/* Touch Optimizations */
.touch-optimized {
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

button,
a,
[role="button"] {
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  min-height: 44px; /* iOS touch target minimum */
  min-width: 44px;
}

/* Smooth Scrolling - Mobile Optimized */
@media (prefers-reduced-motion: no-preference) {
  html {
    scroll-behavior: smooth;
  }
  
  @media (max-width: 767px) {
    html {
      scroll-behavior: auto; /* Disable on mobile for performance */
    }
  }
}

/* Image Optimization */
img {
  image-rendering: -webkit-optimize-contrast;
  image-rendering: crisp-edges;
  image-rendering: optimize-contrast;
  -ms-interpolation-mode: nearest-neighbor;
}

/* Lazy Loading Images */
img[loading="lazy"] {
  opacity: 0;
  transition: opacity 0.3s;
}

img[loading="lazy"].loaded {
  opacity: 1;
}

/* Will-Change for Performance */
.performance-optimized {
  will-change: transform;
  transform: translateZ(0);
  backface-visibility: hidden;
  perspective: 1000px;
}

/* GPU Acceleration for Animations */
.animate-gpu {
  transform: translate3d(0, 0, 0);
  will-change: transform;
}

/* Mobile-Specific Optimizations */
@media (max-width: 767px) {
  /* Reduce shadows on mobile */
  .product-card,
  .category-card {
    box-shadow: var(--shadow-sm);
  }
  
  /* Optimize fonts */
  body {
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    text-rendering: optimizeLegibility;
  }
  
  /* Reduce backdrop blur on mobile */
  .navbar,
  .search-overlay {
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
  }
  
  /* Optimize glassmorphism */
  .hero-slide-content {
    backdrop-filter: blur(15px);
    -webkit-backdrop-filter: blur(15px);
  }
}

/* High-Performance Scroll Containers */
.scroll-container {
  -webkit-overflow-scrolling: touch;
  overflow-scrolling: touch;
  overscroll-behavior: contain;
}

/* Prevent Layout Shift */
.aspect-ratio {
  position: relative;
  width: 100%;
}

.aspect-ratio::before {
  content: '';
  display: block;
  padding-bottom: 100%; /* 1:1 ratio */
}

.aspect-ratio > * {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

/* Mobile Navigation Optimizations */
@media (max-width: 767px) {
  .navbar {
    position: sticky;
    top: 0;
    z-index: var(--z-sticky);
  }
  
  .mobile-menu {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: var(--color-surface);
    z-index: var(--z-modal);
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }
}

/* Performance: Contain Layout */
.contain-layout {
  contain: layout style paint;
}

.contain-strict {
  contain: strict;
}

/* Optimize Product Grid for Mobile */
@media (max-width: 767px) {
  .products-grid {
    grid-template-columns: repeat(2, 1fr);
    gap: var(--spacing-md);
  }
  
  .product-card {
    min-height: auto;
  }
  
  .product-image {
    aspect-ratio: 1;
    object-fit: cover;
  }
}

/* Tablet Optimizations */
@media (min-width: 768px) and (max-width: 1023px) {
  .products-grid {
    grid-template-columns: repeat(3, 1fr);
  }
}

/* Desktop - Only load when needed */
@media (min-width: 1024px) {
  .products-grid {
    grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  }
}

/* Critical Above-the-Fold Content */
.above-fold {
  content-visibility: auto;
  contain-intrinsic-size: 400px;
}

/* Optimize Chat Widget for Mobile */
@media (max-width: 767px) {
  .chat-widget {
    bottom: var(--spacing-md);
    left: var(--spacing-md);
  }
  
  .chat-container {
    width: calc(100vw - 2rem);
    height: calc(100vh - 120px);
    max-height: 600px;
  }
}

/* Loading States - Mobile Optimized */
.skeleton {
  background: linear-gradient(
    90deg,
    var(--color-bg-secondary) 0%,
    var(--color-bg-tertiary) 50%,
    var(--color-bg-secondary) 100%
  );
  background-size: 200% 100%;
  animation: skeleton-loading 1.5s ease-in-out infinite;
}

@keyframes skeleton-loading {
  0% {
    background-position: 200% 0;
  }
  100% {
    background-position: -200% 0;
  }
}

/* Reduce Motion for Performance */
@media (prefers-reduced-motion: reduce) {
  .skeleton {
    animation: none;
    background: var(--color-bg-secondary);
  }
}

/* Mobile-Specific Font Loading */
@font-face {
  font-family: 'Cairo';
  font-display: swap; /* Show fallback immediately */
  src: url('https://fonts.googleapis.com/css2?family=Cairo:wght@300;400;600;700&display=swap');
}

/* Optimize Viewport */
@supports (height: 100dvh) {
  .full-height {
    height: 100dvh; /* Dynamic viewport height */
  }
}

/* Touch Feedback */
.touch-feedback {
  position: relative;
  overflow: hidden;
}

.touch-feedback::after {
  content: '';
  position: absolute;
  top: 50%;
  left: 50%;
  width: 0;
  height: 0;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.3);
  transform: translate(-50%, -50%);
  transition: width 0.3s, height 0.3s;
}

.touch-feedback:active::after {
  width: 300px;
  height: 300px;
}

/* Prevent Text Selection on Mobile */
@media (max-width: 767px) {
  .no-select {
    -webkit-user-select: none;
    user-select: none;
    -webkit-touch-callout: none;
  }
}

/* Optimize Transitions for Mobile */
@media (max-width: 767px) {
  * {
    transition-duration: 0.2s !important;
    transition-timing-function: ease-out !important;
  }
  
  /* Disable complex animations on mobile */
  .hero-slide {
    transition: opacity 0.3s ease !important;
  }
}

/* Memory Optimization - Contain Paint */
.paint-contained {
  contain: paint;
}

/* Optimize Large Lists */
.virtual-scroll {
  contain: strict;
  content-visibility: auto;
}

/* Mobile Input Optimizations */
@media (max-width: 767px) {
  input,
  textarea,
  select {
    font-size: 16px; /* Prevent zoom on iOS */
    -webkit-appearance: none;
    appearance: none;
  }
  
  input[type="search"] {
    -webkit-appearance: none;
  }
  
  input[type="search"]::-webkit-search-cancel-button {
    -webkit-appearance: none;
  }
}

/* Performance: Reduce Repaints */
.stable-layout {
  contain: layout;
}

/* Optimize Cards for Mobile */
@media (max-width: 767px) {
  .product-card {
    contain: layout style paint;
  }
}

/* Fast Click - Remove 300ms Delay */
.fast-click {
  touch-action: manipulation;
}

/* Optimize Hero Section for Mobile */
@media (max-width: 767px) {
  .hero {
    height: 50vh;
    min-height: 300px;
    max-height: 400px;
  }
  
  .hero-slide-content {
    padding: var(--spacing-md);
    font-size: var(--font-size-base);
  }
  
  .hero-slide-content h1 {
    font-size: var(--font-size-xl);
  }
}

/* Optimize Footer for Mobile */
@media (max-width: 767px) {
  .footer-content {
    grid-template-columns: 1fr;
    gap: var(--spacing-lg);
  }
}

/* Performance Monitoring */
.perf-mark {
  content-visibility: auto;
  contain-intrinsic-size: 200px;
}
