/**
 * iOS/iPad Optimizations - Edirectz Design System
 * Comprehensive mobile optimizations for iPhone, iPad, and iOS Safari
 * Ensures optimal user experience across all Apple devices
 */

/* ============================================
   SAFE AREA INSETS (iPhone X+ Notch/Home Indicator)
   ============================================ */

/* Add safe area padding to elements that touch screen edges */
@supports (padding-top: env(safe-area-inset-top)) {
    /* Header - Top safe area for notch */
    header,
    .site-header,
    .main-header {
        padding-top: env(safe-area-inset-top);
    }
    
    /* Bottom navigation - Home indicator */
    .mobile-bottom-nav,
    .mobile-nav-container,
    .ai-chat-input {
        padding-bottom: env(safe-area-inset-bottom);
    }
    
    /* Modals - Top safe area for notch */
    .modal-container {
        padding-top: env(safe-area-inset-top);
    }
    
    /* Modal footer specifically for bottom safe area (home indicator) */
    .modal-footer {
        padding-bottom: calc(var(--modal-footer-padding-y, 0.75rem) + env(safe-area-inset-bottom));
    }
    
    /* Fixed positioned elements */
    .fixed-bottom,
    .sticky-bottom {
        bottom: calc(20px + env(safe-area-inset-bottom));
    }
    
    /* Main content - Bottom safe area to prevent content behind home indicator */
    main,
    .main-content,
    .content-wrapper {
        padding-bottom: calc(20px + env(safe-area-inset-bottom));
    }
    
    /* Sidebar on iPad */
    @media (min-width: 768px) and (max-width: 1024px) {
        .sidebar,
        .mobile-nav-container {
            padding-top: env(safe-area-inset-top);
            padding-bottom: env(safe-area-inset-bottom);
        }
    }
}

/* ============================================
   TOUCH TARGET OPTIMIZATIONS
   ============================================ */

/* Ensure minimum touch target size of 44x44px (Apple HIG recommendation) */
button,
a,
.btn,
.card,
.portfolio-card,
.ai-suggestion-card,
.suggestion-card,
.nav-item,
.menu-item,
.dropdown-item,
.notification-item {
    min-height: 44px;
    min-width: 44px;
}

/* Make sure inline elements have proper touch targets */
a.btn,
button.btn,
.btn-sm,
.btn-xs {
    min-height: 44px;
    min-width: 44px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}

/* Small buttons can be smaller but still touch-friendly */
.btn-sm {
    min-height: 36px;
    min-width: 36px;
}

/* Icon-only buttons need explicit touch targets */
.icon-btn,
.close-btn,
.modal-close,
.menu-toggle {
    min-width: 44px;
    min-height: 44px;
    padding: 10px;
}

/* ============================================
   IOS SCROLLING OPTIMIZATIONS
   ============================================ */

/* Enable smooth momentum scrolling on iOS */
* {
    -webkit-overflow-scrolling: touch;
}

/* Prevent bounce on specific elements where it causes issues */
.no-bounce,
.modal-body,
.dropdown-menu {
    -webkit-overflow-scrolling: auto;
}

/* Improve scroll performance on iOS */
.modal-body,
.ai-chat-messages,
.portfolio-cards-container,
.gallery-grid,
.dashboard-content {
    transform: translateZ(0);
    -webkit-transform: translateZ(0);
    will-change: transform;
}

/* ============================================
   IOS TAP HIGHLIGHT COLOR
   ============================================ */

/* Remove default blue tap highlight on iOS */
button,
a,
.btn,
.card,
.portfolio-card,
.ai-suggestion-card,
.nav-item,
.menu-item {
    -webkit-tap-highlight-color: transparent;
}

/* Add subtle tap highlight for better feedback (optional) */
.btn:active,
.card:active,
.portfolio-card:active {
    opacity: 0.8;
}

/* ============================================
   IOS FONT RENDERING
   ============================================ */

/* Improve font rendering on iOS */
body,
html {
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    text-rendering: optimizeLegibility;
}

/* Prevent iOS from zooming on input focus */
input,
textarea,
select {
    font-size: 16px !important;
}

/* ============================================
   IOS FORM OPTIMIZATIONS
   ============================================ */

/* Style iOS form elements to match design */
input[type="text"],
input[type="email"],
input[type="password"],
input[type="number"],
input[type="tel"],
input[type="url"],
textarea,
select {
    -webkit-appearance: none;
    appearance: none;
    border-radius: 0;
    background-color: var(--theme-bg-input);
}

/* iOS select dropdown styling */
select {
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath fill='%2364748b' d='M6 9L1 4h10z'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 12px center;
    padding-right: 36px;
    appearance: none;
    -webkit-appearance: none;
}

/* iOS checkbox/radio styling */
input[type="checkbox"],
input[type="radio"] {
    -webkit-appearance: none;
    appearance: none;
    width: 22px;
    height: 22px;
    border: 2px solid var(--theme-border-primary);
    border-radius: 4px;
    background: var(--theme-bg-input);
}

input[type="checkbox"]:checked {
    background: var(--theme-primary);
    border-color: var(--theme-primary);
}

input[type="radio"] {
    border-radius: 50%;
}

input[type="radio"]:checked {
    background: var(--theme-primary);
    border-color: var(--theme-primary);
}

/* ============================================
   IOS BACKDROP FILTER (Glassmorphism)
   ============================================ */

/* Enable backdrop filter with webkit prefix */
.glass,
.glass-effect,
.modal-overlay,
.mobile-nav-container,
.site-header {
    -webkit-backdrop-filter: blur(20px) saturate(180%);
    backdrop-filter: blur(20px) saturate(180%);
}

/* Mobile navigation spacing for iOS */
.mobile-nav-container {
    padding: 0 8px;
    gap: 4px;
}

.mobile-nav-item {
    flex: 0 0 clamp(56px, 18vw, 72px);
    padding: 0 4px;
}

/* ============================================
   IOS SPECIFIC MEDIA QUERIES
   ============================================ */

/* iPhone SE and smaller */
@media (max-width: 375px) {
    .modal-container {
        width: 95vw;
        max-width: 340px;
    }
    
    .btn {
        padding: 12px 16px;
        font-size: 14px;
    }
    
    .card {
        padding: 16px;
    }
}

/* iPhone Pro Max and larger phones */
@media (min-width: 376px) and (max-width: 428px) {
    .modal-container {
        width: 90vw;
        max-width: 380px;
    }
}

/* iPad Portrait */
@media (min-width: 768px) and (max-width: 1024px) and (orientation: portrait) {
    .modal-container {
        width: 85vw;
        max-width: 600px;
    }
    
    .sidebar {
        width: 280px;
    }
    
    .grid-2,
    .grid-3 {
        grid-template-columns: repeat(2, 1fr);
    }
}

/* iPad Landscape */
@media (min-width: 1024px) and (max-width: 1366px) and (orientation: landscape) {
    .modal-container {
        width: 80vw;
        max-width: 800px;
    }
    
    .grid-3 {
        grid-template-columns: repeat(3, 1fr);
    }
}

/* iPad Pro */
@media (min-width: 1024px) and (max-width: 1366px) {
    .btn {
        padding: 14px 20px;
        font-size: 16px;
    }
    
    .card {
        padding: 24px;
    }
}

/* ============================================
   IOS SAFARI SPECIFIC FIXES
   ============================================ */

/* Prevent 100vh issues on iOS Safari */
.ios-fix-100vh {
    height: 100vh;
    height: -webkit-fill-available;
}

.modal-body,
.ai-chat-messages {
    max-height: 80vh;
    max-height: -webkit-fill-available;
}

/* Fix iOS input zoom on landscape */
@media (max-width: 768px) and (orientation: landscape) {
    input,
    textarea,
    select {
        font-size: 16px !important;
    }
}

/* Prevent iOS from selecting text on non-editable elements */
.no-select {
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
}

/* ============================================
   ACCESSIBILITY - REDUCED MOTION
   ============================================ */

@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }
    
    * {
        -webkit-overflow-scrolling: auto;
    }
}

/* ============================================
   DARK MODE IOS OPTIMIZATIONS
   ============================================ */

@media (prefers-color-scheme: dark) {
    /* Adjust safe area backgrounds for dark mode */
    @supports (padding-top: env(safe-area-inset-top)) {
        header,
        .site-header {
            background-color: var(--theme-bg-primary);
        }
        
        .mobile-nav-container {
            background-color: rgba(15, 23, 42, 0.85);
        }
    }
    
    /* iOS form elements in dark mode */
    input[type="text"],
    input[type="email"],
    input[type="password"],
    input[type="number"],
    input[type="tel"],
    input[type="url"],
    textarea,
    select {
        background-color: var(--theme-bg-input);
        color: var(--theme-text-primary);
    }
    
    /* iOS checkbox/radio in dark mode */
    input[type="checkbox"],
    input[type="radio"] {
        border-color: var(--theme-border-secondary);
        background-color: var(--theme-bg-input);
    }
}

/* ============================================
   IOS HOVER STATE FIX
   ============================================ */

/* iOS doesn't have hover, prevent hover styles from showing tap state */
@media (hover: none) and (pointer: coarse) {
    .btn:hover,
    .card:hover,
    .portfolio-card:hover,
    .ai-suggestion-card:hover {
        transform: none;
        box-shadow: none;
    }
    
    .btn:active,
    .card:active,
    .portfolio-card:active,
    .ai-suggestion-card:active {
        transform: scale(0.98);
        opacity: 0.9;
    }
}

/* ============================================
   IOS KEYBOARD HANDLING
   ============================================ */

/* Prevent content from being hidden behind keyboard on iOS */
.modal-container,
.modal-body {
    max-height: calc(100vh - env(keyboard-inset-height, 0) - 100px);
}

/* ============================================
   IOS TEXT SELECTION COLOR
   ============================================ */

::selection {
    background: var(--theme-primary);
    color: var(--theme-text-inverse);
}

::-moz-selection {
    background: var(--theme-primary);
    color: var(--theme-text-inverse);
}

/* ============================================
   IOS IMAGE OPTIMIZATIONS
   ============================================ */

/* Prevent image zoom on double-tap on iOS */
img {
    touch-action: manipulation;
}

/* ============================================
   IOS VIDEO OPTIMIZATIONS
   ============================================ */

/* Note: playsinline is an HTML attribute, not CSS. Add playsinline attribute to video tags in HTML */
video {
    object-fit: contain;
    max-width: 100%;
}
