/* ============================================
   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 - contextual styling */
  /* Inline phone links (in text/footer) - subtle but tappable */
  a[href^="tel:"] {
    min-height: 44px !important;
    padding: 10px 14px !important;
    display: inline-flex !important;
    align-items: center !important;
    gap: 6px !important;
    font-size: 17px !important;
    font-weight: 600 !important;
    color: #00a651 !important;
    background-color: rgba(0, 166, 81, 0.1) !important;
    border-radius: 8px !important;
    text-decoration: none !important;
    margin: 4px 0 !important;
    transition: all 200ms cubic-bezier(0.25, 0.46, 0.45, 0.94) !important;
  }

  a[href^="tel:"]:active {
    transform: scale(0.98) !important;
    background-color: rgba(0, 166, 81, 0.2) !important;
  }

  /* Standalone phone CTA - prominent button style */
  .phone-cta a[href^="tel:"],
  .hero a[href^="tel:"],
  .cta a[href^="tel:"],
  a[href^="tel:"].btn,
  a[href^="tel:"].phone-button {
    min-height: 52px !important;
    padding: 14px 24px !important;
    font-size: 18px !important;
    font-weight: 700 !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;
    width: 100% !important;
    max-width: 320px !important;
    justify-content: center !important;
  }

  .phone-cta a[href^="tel:"]:active,
  .hero a[href^="tel:"]:active,
  a[href^="tel:"].btn:active {
    transform: scale(0.98) !important;
    box-shadow: 0 2px 8px rgba(0, 166, 81, 0.3) !important;
  }

  /* Footer phone links - lighter styling */
  .footer a[href^="tel:"],
  footer a[href^="tel:"],
  .footer-section a[href^="tel:"] {
    background-color: rgba(255, 255, 255, 0.1) !important;
    color: white !important;
  }

  .footer a[href^="tel:"]:active,
  footer a[href^="tel:"]:active {
    background-color: rgba(255, 255, 255, 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 - but NOT for buttons */
  a:not([class*="btn"]):not(.navbar-cta):not(.tbm-mobile-cta-button):not(.floating-quote-btn):not(.tbm-popup-button) {
    color: #00a651 !important;
    text-decoration: underline !important;
    text-underline-offset: 2px !important;
    -webkit-tap-highlight-color: transparent !important;
  }

  /* FIXED: Ensure button text is visible (white on green) */
  a[class*="btn"],
  .navbar-cta,
  .tbm-mobile-cta-button,
  .floating-quote-btn,
  .tbm-popup-button,
  .btn-primary,
  .btn-large {
    color: white !important;
    text-decoration: none !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;
  }

  /* Footer links - proper touch targets with spacing */
  .footer-section a,
  footer a,
  .footer-links a {
    display: inline-flex !important;
    align-items: center !important;
    min-height: 44px !important;
    padding: 10px 0 !important;
    font-size: 15px !important;
    color: rgba(255, 255, 255, 0.9) !important;
    text-decoration: none !important;
    transition: color 200ms ease !important;
  }

  .footer-section a:active,
  footer a:active {
    color: #00a651 !important;
  }

  /* Footer list items - vertical spacing */
  .footer-section li,
  .footer-links li {
    margin-bottom: 4px !important;
  }

  /* Footer social links - icon buttons */
  .footer-social a,
  .social-links a {
    width: 44px !important;
    height: 44px !important;
    min-width: 44px !important;
    min-height: 44px !important;
    padding: 10px !important;
    margin-right: 8px !important;
    display: inline-flex !important;
    align-items: center !important;
    justify-content: center !important;
    background: rgba(255, 255, 255, 0.1) !important;
    border-radius: 50% !important;
  }

  .footer-social a:active,
  .social-links a:active {
    background: rgba(255, 255, 255, 0.2) !important;
    transform: scale(0.95) !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;
}

/* ============================================
   13. DESIGN TOKENS - APPLEGRADE SYSTEM
   Use CSS custom properties for consistency
   ============================================ */
:root {
  /* Touch Targets */
  --touch-min: 44px;
  --touch-comfortable: 48px;
  --touch-large: 52px;

  /* Spacing Rhythm (8pt grid) */
  --space-2xs: 4px;
  --space-xs: 8px;
  --space-sm: 12px;
  --space-md: 16px;
  --space-lg: 24px;
  --space-xl: 32px;
  --space-2xl: 48px;

  /* Typography Scale */
  --text-xs: 12px;
  --text-sm: 14px;
  --text-base: 16px;
  --text-lg: 17px;
  --text-xl: 20px;
  --text-2xl: 24px;
  --text-3xl: 28px;
  --text-4xl: 32px;

  /* Border Radius */
  --radius-sm: 4px;
  --radius-md: 8px;
  --radius-lg: 12px;
  --radius-xl: 16px;
  --radius-full: 9999px;

  /* Shadows (iOS-style) */
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.05);
  --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.1);
  --shadow-lg: 0 8px 24px rgba(0, 0, 0, 0.15);
  --shadow-xl: 0 20px 40px rgba(0, 0, 0, 0.2);

  /* Motion */
  --duration-fast: 150ms;
  --duration-normal: 200ms;
  --duration-slow: 300ms;
  --easing-default: cubic-bezier(0.25, 0.46, 0.45, 0.94);
  --easing-spring: cubic-bezier(0.34, 1.56, 0.64, 1);
  --easing-out: cubic-bezier(0, 0, 0.2, 1);

  /* Colors - TBM Brand */
  --color-primary: #00a651;
  --color-primary-dark: #008a43;
  --color-primary-light: #00c263;
  --color-primary-alpha-10: rgba(0, 166, 81, 0.1);
  --color-primary-alpha-15: rgba(0, 166, 81, 0.15);
  --color-primary-alpha-20: rgba(0, 166, 81, 0.2);

  /* Text Colors */
  --color-text-primary: #1f2937;
  --color-text-secondary: #6b7280;
  --color-text-muted: #9ca3af;

  /* Background Colors */
  --color-bg-primary: #ffffff;
  --color-bg-secondary: #f9fafb;
  --color-bg-tertiary: #f3f4f6;

  /* Border Colors */
  --color-border: #e5e7eb;
  --color-border-light: #f3f4f6;
}

/* ============================================
   14. MOTION SPECIFICATIONS
   ============================================ */
/* Standard transitions */
.btn,
button,
a,
input,
select,
textarea {
  transition-property: transform, background-color, box-shadow, border-color, color;
  transition-duration: var(--duration-normal);
  transition-timing-function: var(--easing-default);
}

/* Active press states */
.btn:active,
button:active,
a:active {
  transition-duration: var(--duration-fast);
}

/* Menu slide transitions - FIXED: Use right instead of transform */
.navbar-menu {
  transition: right var(--duration-slow) var(--easing-default) !important;
}

/* Overlay fade */
.mobile-menu-backdrop,
.modal-overlay {
  transition: opacity var(--duration-normal) ease,
              visibility var(--duration-normal) ease;
}

/* ============================================
   15. ADDITIONAL ACCESSIBILITY FIXES
   ============================================ */
/* Ensure minimum contrast for placeholder text */
::placeholder {
  color: var(--color-text-muted);
  opacity: 1;
}

/* High contrast mode support */
@media (prefers-contrast: high) {
  .btn,
  button,
  input,
  select,
  textarea {
    border-width: 2px !important;
  }

  a:not([class*="btn"]) {
    text-decoration: underline !important;
  }
}

/* Forced colors mode (Windows High Contrast) */
@media (forced-colors: active) {
  .btn,
  button {
    border: 2px solid currentColor;
  }

  :focus-visible {
    outline: 3px solid currentColor !important;
  }
}

/* Dynamic Type / Text Scaling Support */
@media (max-width: 768px) {
  /* Use relative units for better scaling */
  html {
    font-size: clamp(14px, 4vw, 18px);
  }

  /* Ensure minimum readable size */
  body,
  p,
  li,
  span {
    font-size: max(1rem, 16px);
    line-height: 1.6;
  }
}