/* ============================================
   APPLEGRADE MOBILE OPTIMIZATIONS FOR TBM
   Complete Mobile UX Overhaul

   Performance Targets:
   - Touch targets: ≥48x48px
   - Text: ≥16px (prevents iOS zoom)
   - Contrast: ≥4.5:1 WCAG AA
   - Motion: 200-250ms with iOS easing
   - 60fps scrolling and animations
   ============================================ */

/* ============================================
   1. CRITICAL VIEWPORT & SAFE AREA FIXES
   ============================================ */
@supports(padding: max(0px)) {
  .navbar,
  #tbm-popup-container,
  .mobile-menu-backdrop {
    padding-top: max(env(safe-area-inset-top, 0), 20px);
    padding-left: max(env(safe-area-inset-left, 0), 20px);
    padding-right: max(env(safe-area-inset-right, 0), 20px);
  }

  /* Bottom safe area for floating buttons */
  .floating-quote-btn {
    bottom: max(env(safe-area-inset-bottom, 0), 20px);
    right: max(env(safe-area-inset-right, 0), 20px);
  }
}

/* ============================================
   2. TOUCH TARGET OPTIMIZATION (48x48px minimum)
   ============================================ */
@media (max-width: 768px) {
  /* Universal button touch targets */
  .btn,
  button,
  input[type="submit"],
  input[type="button"],
  a[class*="btn"],
  .tbm-popup-button,
  #tbm-popup-close {
    min-height: 48px !important;
    min-width: 48px !important;
    padding: 14px 24px !important;
    font-size: 16px !important;
    position: relative;
    -webkit-tap-highlight-color: rgba(0, 166, 81, 0.1);
    touch-action: manipulation;
  }

  /* Close button specific optimization */
  #tbm-popup-close {
    width: 48px !important;
    height: 48px !important;
    padding: 0 !important;
    display: flex !important;
    align-items: center !important;
    justify-content: center !important;
    font-size: 28px !important;
    line-height: 1 !important;
    border-radius: 50% !important;
    background: rgba(0, 0, 0, 0.05) !important;
    transition: all 200ms cubic-bezier(0.25, 0.46, 0.45, 0.94) !important;
  }

  #tbm-popup-close:active {
    background: rgba(0, 0, 0, 0.1) !important;
    transform: scale(0.95) !important;
  }

  /* Phone number links - premium touch target */
  a[href^="tel:"] {
    min-height: 52px !important;
    padding: 16px 20px !important;
    display: inline-flex !important;
    align-items: center !important;
    justify-content: center !important;
    font-size: 18px !important;
    font-weight: 600 !important;
    background: linear-gradient(135deg, #00a651, #00c263) !important;
    color: white !important;
    border-radius: 12px !important;
    box-shadow: 0 4px 12px rgba(0, 166, 81, 0.25) !important;
    text-decoration: none !important;
    margin: 8px 0 !important;
    width: 100% !important;
    max-width: 320px !important;
  }

  a[href^="tel:"]:active {
    transform: scale(0.98) !important;
    box-shadow: 0 2px 8px rgba(0, 166, 81, 0.2) !important;
  }

  /* Navigation items - proper touch targets */
  .navbar-menu a {
    min-height: 48px !important;
    padding: 14px 20px !important;
    margin: 2px 0 !important;
  }

  /* Hamburger menu - larger touch target */
  .navbar-toggle {
    width: 48px !important;
    height: 48px !important;
    padding: 10px !important;
    margin: -10px -10px -10px 0 !important;
  }
}

/* ============================================
   3. FORM INPUT OPTIMIZATION
   ============================================ */
@media (max-width: 768px) {
  /* All form inputs - prevent iOS zoom */
  input,
  textarea,
  select,
  .tbm-popup-input,
  .form-input,
  .form-control {
    font-size: 16px !important; /* Critical: prevents iOS zoom */
    min-height: 48px !important;
    padding: 14px 16px !important;
    border: 2px solid #e0e0e0 !important;
    border-radius: 8px !important;
    background: white !important;
    -webkit-appearance: none !important;
    appearance: none !important;
    width: 100% !important;
    box-sizing: border-box !important;
    transition: all 200ms cubic-bezier(0.25, 0.46, 0.45, 0.94) !important;
  }

  /* Focus states with proper contrast */
  input:focus,
  textarea:focus,
  select:focus,
  .tbm-popup-input:focus {
    outline: none !important;
    border-color: #00a651 !important;
    box-shadow: 0 0 0 3px rgba(0, 166, 81, 0.1) !important;
  }

  /* Textarea specific */
  textarea {
    min-height: 120px !important;
    resize: vertical !important;
    line-height: 1.5 !important;
  }

  /* Labels - ensure visibility */
  label,
  .tbm-popup-label {
    font-size: 14px !important;
    font-weight: 600 !important;
    color: #333 !important;
    margin-bottom: 8px !important;
    display: block !important;
  }

  /* Required field indicators */
  label:has(+ :required)::after,
  .tbm-popup-label:has(+ :required)::after {
    content: " *";
    color: #ef4444;
  }
}

/* ============================================
   4. LEAD MAGNET POPUP MOBILE OPTIMIZATION
   ============================================ */
@media (max-width: 768px) {
  /* Popup overlay - full screen on mobile */
  #tbm-popup-overlay {
    padding: 0 !important;
    display: flex !important;
    align-items: center !important;
    justify-content: center !important;
    backdrop-filter: blur(8px) !important;
    -webkit-backdrop-filter: blur(8px) !important;
  }

  /* Popup container - optimized for mobile */
  #tbm-popup-container {
    width: calc(100% - 32px) !important;
    max-width: 400px !important;
    max-height: calc(100vh - 40px) !important;
    max-height: calc(100dvh - 40px) !important;
    margin: 20px !important;
    padding: 32px 24px !important;
    border-radius: 20px !important;
    overflow-y: auto !important;
    -webkit-overflow-scrolling: touch !important;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3) !important;
  }

  /* Popup close button positioning */
  #tbm-popup-close {
    position: absolute !important;
    top: 12px !important;
    right: 12px !important;
    z-index: 10 !important;
  }

  /* Popup title - better hierarchy */
  .tbm-popup-title {
    font-size: 24px !important;
    line-height: 1.3 !important;
    margin-bottom: 12px !important;
  }

  /* Popup subtitle */
  .tbm-popup-subtitle {
    font-size: 16px !important;
    line-height: 1.5 !important;
    margin-bottom: 24px !important;
    color: #666 !important;
  }

  /* Popup button - premium CTA */
  .tbm-popup-button {
    min-height: 52px !important;
    font-size: 17px !important;
    font-weight: 600 !important;
    border-radius: 12px !important;
    background: linear-gradient(135deg, #00a651, #00c263) !important;
    box-shadow: 0 4px 16px rgba(0, 166, 81, 0.3) !important;
    margin-top: 16px !important;
  }

  .tbm-popup-button:active {
    transform: scale(0.98) !important;
    box-shadow: 0 2px 8px rgba(0, 166, 81, 0.25) !important;
  }

  /* Privacy text - ensure readability */
  .tbm-popup-privacy {
    font-size: 13px !important;
    line-height: 1.5 !important;
    color: #767676 !important; /* WCAG AA compliant */
    margin-top: 16px !important;
  }
}

/* ============================================
   5. TYPOGRAPHY & READABILITY
   ============================================ */
@media (max-width: 768px) {
  /* Base font size - prevent zoom */
  html {
    font-size: 16px !important;
    -webkit-text-size-adjust: 100% !important;
    text-size-adjust: 100% !important;
  }

  /* Body text optimization */
  body {
    font-size: 16px !important;
    line-height: 1.6 !important;
    letter-spacing: -0.011em !important; /* iOS optimization */
  }

  /* Heading hierarchy */
  h1 {
    font-size: 28px !important;
    line-height: 1.2 !important;
    margin-bottom: 16px !important;
    letter-spacing: -0.02em !important;
  }

  h2 {
    font-size: 24px !important;
    line-height: 1.3 !important;
    margin-bottom: 14px !important;
    letter-spacing: -0.019em !important;
  }

  h3 {
    font-size: 20px !important;
    line-height: 1.35 !important;
    margin-bottom: 12px !important;
  }

  p, li {
    font-size: 16px !important;
    line-height: 1.6 !important;
    margin-bottom: 16px !important;
  }

  /* Link optimization */
  a {
    color: #00a651 !important;
    text-decoration: underline !important;
    text-underline-offset: 2px !important;
    -webkit-tap-highlight-color: transparent !important;
  }

  /* Footer text - ensure visibility */
  .footer-section p,
  .footer-section li,
  .footer-bottom p {
    font-size: 14px !important;
    line-height: 1.6 !important;
    color: rgba(255, 255, 255, 0.9) !important;
  }
}

/* ============================================
   6. NAVIGATION MOBILE FIXES
   ============================================ */
@media (max-width: 768px) {
  /* Fixed navigation height */
  .navbar {
    min-height: 56px !important;
    max-height: 56px !important;
  }

  /* Navigation container */
  .navbar-container {
    padding: 12px 16px !important;
    min-height: 56px !important;
    height: 56px !important;
  }

  /* Logo sizing */
  .navbar-logo {
    font-size: 20px !important;
    max-width: 200px !important;
  }

  /* Mobile menu optimization */
  .navbar-menu {
    top: 0 !important;
    padding-top: 72px !important;
    width: min(320px, 85vw) !important;
    -webkit-overflow-scrolling: touch !important;
  }

  /* Menu items spacing */
  .navbar-menu li {
    margin: 0 !important;
    border-bottom: 1px solid rgba(0, 0, 0, 0.06) !important;
  }

  .navbar-menu a {
    padding: 16px 20px !important;
    font-size: 17px !important;
    font-weight: 500 !important;
  }
}

/* ============================================
   7. PERFORMANCE OPTIMIZATIONS
   ============================================ */
/* Hardware acceleration for smooth animations */
.btn,
.card,
.navbar-menu,
.floating-quote-btn,
#tbm-popup-container,
.tbm-popup-button {
  -webkit-transform: translateZ(0);
  transform: translateZ(0);
  -webkit-backface-visibility: hidden;
  backface-visibility: hidden;
  will-change: transform;
}

/* Optimized transitions */
* {
  -webkit-transition-timing-function: cubic-bezier(0.25, 0.46, 0.45, 0.94);
  transition-timing-function: cubic-bezier(0.25, 0.46, 0.45, 0.94);
}

/* Reduce motion for accessibility */
@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;
  }
}

/* ============================================
   8. CONTENT LAYOUT FIXES
   ============================================ */
@media (max-width: 768px) {
  /* Section padding optimization */
  section,
  .section {
    padding: 48px 16px !important;
  }

  /* Container width */
  .container,
  .content-container {
    max-width: 100% !important;
    padding: 0 16px !important;
  }

  /* Card layouts */
  .card,
  .feature-card,
  .service-card {
    padding: 20px !important;
    margin-bottom: 16px !important;
    border-radius: 12px !important;
  }

  /* Grid to stack on mobile */
  .grid,
  [class*="grid-"],
  .row {
    grid-template-columns: 1fr !important;
    gap: 16px !important;
  }

  /* Flexbox to column */
  .flex-row {
    flex-direction: column !important;
    gap: 16px !important;
  }
}

/* ============================================
   9. FLOATING ACTION BUTTON
   ============================================ */
@media (max-width: 768px) {
  .floating-quote-btn {
    width: 60px !important;
    height: 60px !important;
    bottom: 20px !important;
    right: 20px !important;
    border-radius: 30px !important;
    box-shadow: 0 4px 20px rgba(0, 166, 81, 0.4) !important;
    display: flex !important;
    align-items: center !important;
    justify-content: center !important;
    font-size: 24px !important;
    z-index: 1000 !important;
  }

  .floating-quote-btn:active {
    transform: scale(0.95) !important;
  }
}

/* ============================================
   10. SCROLL BEHAVIOR & OVERSCROLL
   ============================================ */
html {
  scroll-behavior: smooth;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: none;
}

body {
  overscroll-behavior-y: none;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

/* Prevent horizontal scroll */
@media (max-width: 768px) {
  body,
  html {
    overflow-x: hidden !important;
    max-width: 100vw !important;
  }
}

/* ============================================
   11. IMAGE OPTIMIZATION
   ============================================ */
@media (max-width: 768px) {
  img,
  picture,
  video {
    max-width: 100% !important;
    height: auto !important;
    display: block !important;
    border-radius: 8px !important;
  }

  /* Lazy loading optimization */
  img[loading="lazy"] {
    background: #f0f0f0;
    min-height: 200px;
  }
}

/* ============================================
   12. ACCESSIBILITY ENHANCEMENTS
   ============================================ */
/* Focus indicators */
*:focus-visible {
  outline: 3px solid #00a651 !important;
  outline-offset: 2px !important;
  border-radius: 4px !important;
}

/* Skip to content link */
.skip-to-content {
  position: absolute;
  top: -40px;
  left: 0;
  background: #00a651;
  color: white;
  padding: 8px;
  text-decoration: none;
  z-index: 10000;
}

.skip-to-content:focus {
  top: 0;
}

/* Screen reader only */
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}