/* ========================================
   iOS SPECIFIC ENHANCEMENTS
   Safari Mobile Optimization
   CRITICAL: overflow-x: hidden ONLY on html element
   body must remain overflow: visible
   ======================================== */

/* iOS Safari fixes - ONLY on mobile Safari (max-width: 768px) */
@supports (-webkit-touch-callout: none) {
    /* html is the ONLY scroll container */
    html {
        height: -webkit-fill-available !important;
        overflow-x: hidden !important;
        overflow-y: auto !important;
    }

    /* body must NOT have overflow-x: hidden - breaks desktop Chrome scrolling */
    body {
        height: auto !important;
        min-height: -webkit-fill-available !important;
        -webkit-overflow-scrolling: touch !important;
        position: relative !important;
        overflow: visible !important;
    }

    /* Dashboard container - NO overflow rules */
    .dashboard-container,
    .app-container {
        min-height: 100vh !important;
        min-height: -webkit-fill-available !important;
        position: relative !important;
    }

    /* Main content - NO overflow rules, scrolling handled by html */
    .main-content,
    .dashboard-content,
    .page-content {
        position: relative !important;
    }

    /* Fix header positioning - STICKY POSITION */
    .dashboard-header,
    .header {
        position: sticky !important;
        top: 0 !important;
        left: 0 !important;
        right: 0 !important;
        width: 100% !important;
        z-index: 999999 !important;
        background: #2A3441 !important;
        color: white !important;
        -webkit-backdrop-filter: blur(10px) !important;
        backdrop-filter: blur(10px) !important;
        min-height: 70px !important;
        padding: env(safe-area-inset-top, 0) 1rem 1rem 1rem !important;
    }

    /* Header Buttons Touch-Target Fix */
    .header .btn,
    .header button {
        min-height: 44px !important;
        min-width: 44px !important;
        touch-action: manipulation !important;
        -webkit-tap-highlight-color: transparent !important;
    }

    /* Ensure proper rendering */
    .dashboard-header,
    .header {
        -webkit-backface-visibility: hidden !important;
        backface-visibility: hidden !important;
    }

    /* Fix form scrolling - only on actual scrollable areas */
    .form-container,
    .question-container,
    .questionnaire-container {
        -webkit-overflow-scrolling: touch !important;
    }

    /* Prevent zoom on input focus */
    input,
    textarea,
    select {
        font-size: 16px !important;
        -webkit-appearance: none !important;
        border-radius: 0 !important;
    }

    /* Fix touch scrolling momentum */
    .scrollable,
    .content-area,
    .card-body {
        -webkit-overflow-scrolling: touch !important;
        transform: translateZ(0) !important;
        -webkit-transform: translateZ(0) !important;
    }
}

/* Mobile-only iOS overscroll/bounce prevention */
@supports (-webkit-touch-callout: none) {
    @media screen and (max-width: 768px) {
        body {
            overscroll-behavior: none !important;
            -webkit-overscroll-behavior: none !important;
        }

        .dashboard-container,
        .app-container,
        .main-content {
            overscroll-behavior-y: contain !important;
            -webkit-overscroll-behavior-y: contain !important;
        }
    }
}

/* iPhone Safari specific fixes */
@media only screen
and (device-width: 375px)
and (device-height: 812px)
and (-webkit-device-pixel-ratio: 3) {
    body {
        padding-top: env(safe-area-inset-top) !important;
        padding-bottom: env(safe-area-inset-bottom) !important;
    }

    .dashboard-header {
        padding-top: calc(env(safe-area-inset-top) + 1rem) !important;
    }
}

@media only screen
and (device-width: 414px)
and (device-height: 896px)
and (-webkit-device-pixel-ratio: 2) {
    body {
        padding-top: env(safe-area-inset-top) !important;
        padding-bottom: env(safe-area-inset-bottom) !important;
    }

    .dashboard-header {
        padding-top: calc(env(safe-area-inset-top) + 1rem) !important;
    }
}

/* General iOS mobile fixes */
@media screen and (max-device-width: 480px) and (-webkit-min-device-pixel-ratio: 2) {
    .dashboard-container,
    .main-content,
    .card,
    .form-container {
        -webkit-transform: translateZ(0) !important;
        transform: translateZ(0) !important;
        -webkit-backface-visibility: hidden !important;
        backface-visibility: hidden !important;
    }

    .dashboard-header {
        will-change: transform !important;
        contain: layout style paint !important;
    }
}

/* iOS orientation change fixes */
@media screen and (orientation: portrait) and (max-device-width: 480px) {
    .dashboard-container {
        min-height: 100vh !important;
        min-height: -webkit-fill-available !important;
    }
}

@media screen and (orientation: landscape) and (max-device-width: 850px) {
    .dashboard-container {
        min-height: 100vh !important;
        min-height: -webkit-fill-available !important;
    }

    .dashboard-header {
        padding: 0.5rem 1rem !important;
    }
}

/* Additional iOS Safari bug fixes */
@supports (-webkit-touch-callout: none) {
    .ios-viewport-fix {
        height: 100vh !important;
        height: -webkit-fill-available !important;
    }

    .prevent-bounce {
        overscroll-behavior: none !important;
        -webkit-overscroll-behavior: none !important;
    }

    .dashboard-header {
        position: -webkit-sticky !important;
        position: sticky !important;
        top: 0 !important;
        z-index: 999999 !important;
    }
}

/* === ANDROID SPECIFIC OPTIMIZATIONS === */
@media screen and (-webkit-min-device-pixel-ratio: 0) and (min-resolution: .001dpcm) {
  input[type="text"],
  input[type="email"],
  input[type="password"],
  input[type="tel"],
  textarea {
    font-size: 16px !important;
  }

  .header {
    position: sticky;
    top: 0;
    z-index: 1000;
  }
}

/* === ENHANCED TOUCH TARGETS === */
.btn-touch-optimized,
.btn.btn-primary,
.btn.btn-secondary,
.radio-item,
.checkbox-item {
  min-height: 56px !important;
  min-width: 56px !important;
  padding: 1rem !important;
}

.radio-group .radio-item:not(:last-child),
.checkbox-group .checkbox-item:not(:last-child) {
  margin-bottom: 16px !important;
}

/* === IMPROVED TYPOGRAPHY FOR MOBILE === */
@media (max-width: 768px) {
  body {
    font-size: 16px !important;
    line-height: 1.6 !important;
  }

  h1 {
    font-size: 1.75rem !important;
    line-height: 1.3 !important;
  }

  h2 {
    font-size: 1.5rem !important;
    line-height: 1.3 !important;
  }

  h3 {
    font-size: 1.25rem !important;
    line-height: 1.4 !important;
  }

  .question-title {
    font-size: 1.25rem !important;
    line-height: 1.4 !important;
  }

  .form-label {
    font-size: 1rem !important;
    font-weight: 600 !important;
  }

  .text-muted,
  .text-secondary {
    color: #666 !important;
  }
}

/* === OPTIMIZED FORM LAYOUTS FOR MOBILE === */
@media (max-width: 768px) {
  .form-group-grid {
    grid-template-columns: 1fr !important;
    gap: 1rem !important;
  }

  .radio-group.grid-layout,
  .checkbox-group {
    grid-template-columns: 1fr !important;
    gap: 1rem !important;
  }

  .contracts-grid {
    grid-template-columns: 1fr !important;
    gap: 1.5rem !important;
  }

  .dashboard-stats {
    grid-template-columns: 1fr !important;
    gap: 1rem !important;
  }
}

/* === LANDSCAPE MODE OPTIMIZATIONS === */
@media (orientation: landscape) and (max-height: 600px) {
  .header,
  .dashboard-header {
    min-height: 56px !important;
    padding: 0.75rem !important;
  }

  .logo-image,
  .header-logo {
    height: 36px !important;
  }

  .main-content {
    padding: 1rem !important;
  }

  .question-card,
  .card {
    padding: 1.5rem !important;
    margin-bottom: 1rem !important;
  }

  .radio-group.grid-layout,
  .checkbox-group {
    grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)) !important;
  }

  .nav-buttons {
    flex-direction: row !important;
    justify-content: space-between !important;
    gap: 1rem !important;
  }
}

/* === TABLET SPECIFIC OPTIMIZATIONS === */
@media (min-width: 768px) and (max-width: 1024px) {
  .container {
    max-width: 90% !important;
    padding: 0 2rem !important;
  }

  .radio-group.grid-layout,
  .checkbox-group {
    grid-template-columns: repeat(2, 1fr) !important;
    gap: 1.5rem !important;
  }

  .contracts-grid {
    grid-template-columns: repeat(2, 1fr) !important;
    gap: 2rem !important;
  }

  .dashboard-stats {
    grid-template-columns: repeat(2, 1fr) !important;
    gap: 1.5rem !important;
  }

  .btn,
  .radio-item,
  .checkbox-item {
    min-height: 52px !important;
    padding: 1rem 1.5rem !important;
  }
}

/* === ENHANCED VISUAL FEEDBACK === */
.btn:active,
.radio-item:active,
.checkbox-item:active,
.contract-card:active {
  transform: scale(0.98) !important;
  transition: transform 0.1s ease !important;
}

/* === ACCESSIBILITY IMPROVEMENTS === */
@media (prefers-reduced-motion: reduce) {
  .btn:active,
  .radio-item:active,
  .checkbox-item:active {
    transform: none !important;
  }

  .animate-fade-in,
  .animate-slide-up,
  .animate-scale-in {
    animation: none !important;
  }
}

/* === HIGH CONTRAST MODE === */
@media (prefers-contrast: high) {
  .btn {
    border: 3px solid currentColor !important;
    font-weight: bold !important;
  }

  .radio-item,
  .checkbox-item {
    border: 2px solid currentColor !important;
  }

  .card {
    border: 2px solid var(--text-primary) !important;
  }
}

/* === DARK MODE SUPPORT === */
@media (prefers-color-scheme: dark) {
  :root {
    --bg-primary: #1a1a1a;
    --bg-secondary: #2d2d2d;
    --text-primary: #ffffff;
    --text-secondary: #b0b0b0;
    --border-primary: #404040;
  }
}

/* === PRINT OPTIMIZATIONS === */
@media print {
  .header,
  .nav-buttons,
  .btn,
  .user-menu-container {
    display: none !important;
  }

  .main-content {
    padding: 0 !important;
    margin: 0 !important;
  }

  .card {
    break-inside: avoid !important;
    box-shadow: none !important;
    border: 1px solid #000 !important;
    margin-bottom: 1rem !important;
  }

  body {
    background: white !important;
    color: black !important;
    font-size: 12pt !important;
  }

  h1, h2, h3 {
    color: black !important;
  }
}

/* === PERFORMANCE OPTIMIZATIONS === */
.btn,
.radio-item,
.checkbox-item,
.card {
  will-change: transform;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
}

/* === KEYBOARD NAVIGATION SUPPORT === */
.btn:focus-visible,
.radio-item:focus-visible,
.checkbox-item:focus-visible,
input:focus-visible,
textarea:focus-visible {
  outline: 3px solid var(--primary) !important;
  outline-offset: 2px !important;
  z-index: 10 !important;
}