/**
 * iOS & Apple Device Enhancements v2.0
 * 
 * Comprehensive fixes for:
 * - Safari on iOS (iPhone/iPad)
 * - Standalone PWA mode
 * - Safe area insets (notch, home indicator)
 * - Virtual keyboard handling
 * - Scroll behavior and momentum
 * - Touch interactions
 * - Input handling
 * - Fixed positioning quirks
 * - Orientation changes
 */

/* ==================== CSS Variables for iOS ==================== */
:root {
  /* iOS Safe Area Insets */
  --ios-safe-top: env(safe-area-inset-top, 0px);
  --ios-safe-right: env(safe-area-inset-right, 0px);
  --ios-safe-bottom: env(safe-area-inset-bottom, 0px);
  --ios-safe-left: env(safe-area-inset-left, 0px);
  
  /* Keyboard-aware bottom safe area */
  --ios-keyboard-height: 0px;
  --ios-actual-bottom: max(var(--ios-safe-bottom), var(--ios-keyboard-height));
  
  /* Dynamic viewport units for iOS */
  --ios-vh: 1vh;
  --ios-dvh: 1dvh;
  --ios-svh: 1svh;
  --ios-lvh: 1lvh;
  
  /* Touch target sizes (Apple HIG recommends 44px minimum) */
  --ios-touch-target: 44px;
  --ios-touch-target-lg: 48px;
}

/* ==================== Base iOS Fixes ==================== */

/* Prevent iOS rubber-band bounce on body */
html {
  position: fixed;
  width: 100%;
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
}

body {
  position: fixed;
  width: 100%;
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
  -webkit-overflow-scrolling: touch;
  
  /* Prevent text size adjust */
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
  
  /* Smooth font rendering */
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

/* Use 100dvh for full viewport */
#root {
  height: 100dvh;
  height: 100vh; /* Fallback */
  min-height: -webkit-fill-available;
  overflow: hidden;
}

/* ==================== Safe Area Handling ==================== */

/* Apply safe area padding to fixed elements */
.ios-safe-top {
  padding-top: var(--ios-safe-top);
}

.ios-safe-bottom {
  padding-bottom: var(--ios-safe-bottom);
}

.ios-safe-left {
  padding-left: var(--ios-safe-left);
}

.ios-safe-right {
  padding-right: var(--ios-safe-right);
}

.ios-safe-all {
  padding-top: var(--ios-safe-top);
  padding-right: var(--ios-safe-right);
  padding-bottom: var(--ios-safe-bottom);
  padding-left: var(--ios-safe-left);
}

/* Safe area for header/navigation */
.ios-header-safe {
  padding-top: max(1rem, var(--ios-safe-top));
}

/* Safe area for bottom navigation/input */
.ios-bottom-safe {
  padding-bottom: max(0.75rem, var(--ios-safe-bottom));
}

/* ==================== Scrolling Fixes ==================== */

/* Enable momentum scrolling */
.ios-scroll {
  -webkit-overflow-scrolling: touch;
  overflow-y: auto;
  overflow-x: hidden;
  overscroll-behavior-y: contain;
  scroll-behavior: smooth;
}

/* Horizontal scroll with momentum */
.ios-scroll-x {
  -webkit-overflow-scrolling: touch;
  overflow-x: auto;
  overflow-y: hidden;
  overscroll-behavior-x: contain;
  scroll-behavior: smooth;
}

/* Prevent scroll propagation */
.ios-scroll-contain {
  overscroll-behavior: contain;
}

/* Fix for fixed elements during scroll */
.ios-fixed {
  position: fixed;
  -webkit-transform: translateZ(0);
  transform: translateZ(0);
  will-change: transform;
}

/* ==================== Input Handling ==================== */

/* Prevent iOS zoom on input focus (16px minimum) */
input,
select,
textarea {
  font-size: 16px !important;
  -webkit-appearance: none;
  appearance: none;
  border-radius: 0;
}

/* Style iOS-friendly inputs */
.ios-input {
  font-size: 16px !important;
  -webkit-appearance: none;
  appearance: none;
  border-radius: 8px;
  padding: 12px 16px;
  min-height: var(--ios-touch-target);
  touch-action: manipulation;
}

/* Prevent iOS input shadow */
input:not([type="checkbox"]):not([type="radio"]),
textarea,
select {
  -webkit-appearance: none;
  appearance: none;
  background-clip: padding-box;
}

/* Fix iOS input placeholder color */
input::placeholder,
textarea::placeholder {
  color: #94a3b8;
  opacity: 1;
}

/* ==================== Touch Interactions ==================== */

/* Remove tap highlight */
* {
  -webkit-tap-highlight-color: transparent;
  -webkit-touch-callout: none;
}

/* Enable text selection for content */
.ios-selectable {
  -webkit-user-select: text;
  user-select: text;
  -webkit-touch-callout: default;
}

/* Minimum touch targets */
.ios-touch-target {
  min-width: var(--ios-touch-target);
  min-height: var(--ios-touch-target);
  touch-action: manipulation;
}

/* Larger touch targets */
.ios-touch-target-lg {
  min-width: var(--ios-touch-target-lg);
  min-height: var(--ios-touch-target-lg);
  touch-action: manipulation;
}

/* Button touch behavior */
button,
[role="button"],
.ios-button {
  -webkit-touch-callout: none;
  touch-action: manipulation;
  cursor: pointer;
  min-height: var(--ios-touch-target);
}

/* Active state for touch feedback */
button:active,
[role="button"]:active,
.ios-button:active {
  opacity: 0.7;
  transform: scale(0.98);
}

/* ==================== Fixed Positioning Fixes ==================== */

/* Fix iOS fixed position + transform issue */
.ios-modal-backdrop {
  position: fixed;
  inset: 0;
  -webkit-transform: translateZ(0);
  transform: translateZ(0);
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
}

/* Modal content that respects keyboard */
.ios-modal-content {
  position: fixed;
  top: var(--ios-safe-top);
  left: var(--ios-safe-left);
  right: var(--ios-safe-right);
  bottom: var(--ios-safe-bottom);
  max-height: calc(100dvh - var(--ios-safe-top) - var(--ios-safe-bottom));
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

/* Bottom sheet handling */
.ios-bottom-sheet {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  padding-bottom: var(--ios-safe-bottom);
  border-radius: 16px 16px 0 0;
  -webkit-transform: translateZ(0);
  transform: translateZ(0);
  max-height: 85dvh;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

/* ==================== Keyboard Handling ==================== */

/* Class added when keyboard is visible */
.ios-keyboard-visible {
  --ios-keyboard-height: 300px; /* Approximate, updated via JS */
}

.ios-keyboard-visible .ios-bottom-fixed {
  transform: translateY(calc(-1 * var(--ios-keyboard-height)));
}

/* Input area that stays above keyboard */
.ios-chat-input {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  padding-bottom: var(--ios-safe-bottom);
  background: inherit;
  -webkit-transform: translateZ(0);
  transform: translateZ(0);
  will-change: transform;
  transition: transform 0.25s ease-out;
}

/* ==================== PWA Standalone Mode ==================== */

@media all and (display-mode: standalone) {
  /* Add extra top padding for status bar */
  .ios-standalone-header {
    padding-top: calc(var(--ios-safe-top) + 0.5rem);
  }
  
  /* Ensure content doesn't go behind status bar */
  body {
    padding-top: var(--ios-safe-top);
  }
  
  /* Home indicator area */
  .ios-standalone-footer {
    padding-bottom: calc(var(--ios-safe-bottom) + 0.5rem);
  }
}

/* ==================== iPhone-Specific Fixes ==================== */

/* iPhone X and later (notch devices) */
@supports (padding: max(0px)) {
  .ios-notch-safe {
    padding-left: max(16px, var(--ios-safe-left));
    padding-right: max(16px, var(--ios-safe-right));
  }
}

/* iPhone SE and older (smaller screens) */
@media screen and (max-width: 375px) {
  .ios-small-screen {
    --ios-touch-target: 40px;
    font-size: 14px;
  }
  
  .ios-small-screen button,
  .ios-small-screen .ios-button {
    padding: 8px 12px;
    font-size: 14px;
  }
}

/* iPhone Pro Max and Plus models */
@media screen and (min-width: 414px) {
  .ios-large-screen {
    --ios-touch-target: 48px;
  }
}

/* ==================== iPad-Specific Fixes ==================== */

@media screen and (min-width: 768px) and (max-width: 1024px) {
  /* iPad portrait */
  .ios-ipad-portrait {
    --ios-touch-target: 50px;
  }
}

@media screen and (min-width: 1024px) and (max-width: 1366px) {
  /* iPad landscape */
  .ios-ipad-landscape {
    --ios-touch-target: 44px;
  }
}

/* ==================== Orientation Changes ==================== */

@media screen and (orientation: landscape) {
  .ios-landscape {
    /* Reduce vertical spacing in landscape */
    --ios-touch-target: 40px;
  }
  
  .ios-landscape-hide {
    display: none !important;
  }
}

@media screen and (orientation: portrait) {
  .ios-portrait-hide {
    display: none !important;
  }
}

/* ==================== iOS Safari Specific ==================== */

/* Safari-only scrollbar styling */
@supports (-webkit-touch-callout: none) {
  /* Hide scrollbars on iOS Safari while maintaining scroll */
  .ios-hide-scrollbar {
    -ms-overflow-style: none;
    scrollbar-width: none;
  }
  
  .ios-hide-scrollbar::-webkit-scrollbar {
    display: none;
    width: 0;
    height: 0;
  }
  
  /* Fix Safari flex gap bug */
  .ios-flex-gap {
    gap: 0;
  }
  
  .ios-flex-gap > * + * {
    margin-left: 8px;
  }
}

/* ==================== Animations for iOS ==================== */

/* Prefer reduced motion */
@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;
  }
}

/* iOS-optimized transitions */
.ios-transition {
  transition-property: transform, opacity;
  transition-duration: 0.25s;
  transition-timing-function: cubic-bezier(0.4, 0, 0.2, 1);
  -webkit-transform: translateZ(0);
  transform: translateZ(0);
}

/* Slide up animation */
.ios-slide-up {
  animation: ios-slide-up 0.3s ease-out;
}

@keyframes ios-slide-up {
  from {
    opacity: 0;
    transform: translateY(100%);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* Slide down animation */
.ios-slide-down {
  animation: ios-slide-down 0.3s ease-out;
}

@keyframes ios-slide-down {
  from {
    opacity: 0;
    transform: translateY(-100%);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* Fade in animation */
.ios-fade-in {
  animation: ios-fade-in 0.2s ease-out;
}

@keyframes ios-fade-in {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

/* ==================== Utility Classes ==================== */

/* Full viewport height accounting for iOS */
.ios-h-screen {
  height: 100dvh;
  height: -webkit-fill-available;
}

/* Full viewport with safe areas */
.ios-h-safe {
  height: calc(100dvh - var(--ios-safe-top) - var(--ios-safe-bottom));
}

/* Prevent iOS bounce */
.ios-no-bounce {
  overscroll-behavior: none;
  -webkit-overflow-scrolling: auto;
}

/* Enable iOS bounce (for pull-to-refresh) */
.ios-bounce {
  overscroll-behavior-y: auto;
  -webkit-overflow-scrolling: touch;
}

/* GPU acceleration */
.ios-gpu {
  -webkit-transform: translateZ(0);
  transform: translateZ(0);
  will-change: transform;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
}

/* Fix blurry text on iOS */
.ios-crisp-text {
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-rendering: optimizeLegibility;
}

/* Disable iOS text selection */
.ios-no-select {
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}

/* Enable iOS text selection */
.ios-select {
  -webkit-user-select: text;
  user-select: text;
  -webkit-touch-callout: default;
}

