/* UNIFIED MOBILE & PWA FIX - 2024 */
/* Mobile-only optimizations - preserves desktop layouts */

/* ===== CRITICAL VIEWPORT FIXES ===== */
* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html {
  -webkit-text-size-adjust: 100%;
  -ms-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

body {
  margin: 0;
  padding: 0;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

/* ===== PREVENT HORIZONTAL SCROLL (Mobile Only) ===== */
@media (max-width: 767px) {

  html,
  body,
  #root {
    overflow-x: hidden !important;
    max-width: 100vw !important;
  }
}

/* ===== TOUCH TARGETS (Mobile Only) ===== */
@media (max-width: 767px) {

  button,
  [role="button"],
  a,
  input[type="submit"],
  input[type="button"],
  .btn {
    min-height: 44px;
    min-width: 44px;
    touch-action: manipulation;
  }
}

/* ===== PREVENT iOS ZOOM ON INPUT FOCUS ===== */
@media (max-width: 767px) {
  /* Prevent iOS zoom */

  input,
  textarea,
  select {
    font-size: 16px !important;
    color: #1e293b !important;
    /* Force dark text for visibility on light backgrounds */
  }

  /* Exception for dark mode if we can detect it or if it's already dark */
  .dark input,
  .dark textarea,
  .dark select {
    color: #f8fafc !important;
  }
}

/* ===== SAFE AREA INSETS (iPhone notch/island) ===== */
:root {
  --safe-area-inset-top: env(safe-area-inset-top, 0px);
  --safe-area-inset-bottom: env(safe-area-inset-bottom, 0px);
  --safe-area-inset-left: env(safe-area-inset-left, 0px);
  --safe-area-inset-right: env(safe-area-inset-right, 0px);
}

/* ===== PWA MODE ADJUSTMENTS ===== */
@media (display-mode: standalone) {
  body {
    padding-bottom: var(--safe-area-inset-bottom);
  }
}

/* ===== IPHONE SPECIFIC OPTIMIZATIONS ===== */

/* iPhone 13/14/15 (390x844) */
@media screen and (device-width: 390px) and (device-height: 844px) and (-webkit-device-pixel-ratio: 3) {
  html {
    font-size: 16px;
  }
}

/* iPhone 13/14/15 Pro Max (430x932) */
@media screen and (device-width: 430px) and (device-height: 932px) and (-webkit-device-pixel-ratio: 3) {
  html {
    font-size: 17px;
  }
}

/* iPhone 13 Mini / iPhone 12 Mini (375x812) */
@media screen and (device-width: 375px) and (device-height: 812px) and (-webkit-device-pixel-ratio: 3) {
  html {
    font-size: 15px;
  }
}

/* iPhone 15 (393x852) */
@media screen and (device-width: 393px) and (device-height: 852px) and (-webkit-device-pixel-ratio: 3) {
  html {
    font-size: 16px;
  }
}

/* iPhone 15 Plus (430x932) */
@media screen and (device-width: 430px) and (device-height: 932px) and (-webkit-device-pixel-ratio: 3) {
  html {
    font-size: 17px;
  }
}

/* iPhone 15 Pro (393x852) */
@media screen and (device-width: 393px) and (device-height: 852px) and (-webkit-device-pixel-ratio: 3) {
  html {
    font-size: 16px;
  }
}

/* iPhone 15 Pro Max (430x932) */
@media screen and (device-width: 430px) and (device-height: 932px) and (-webkit-device-pixel-ratio: 3) {
  html {
    font-size: 18px;
  }
}

/* iPhone 16 (402x874) */
@media screen and (device-width: 402px) and (device-height: 874px) and (-webkit-device-pixel-ratio: 3) {
  html {
    font-size: 17px;
  }
}

/* iPhone 16 Plus / 16 Pro Max (440x956) */
@media screen and (device-width: 440px) and (device-height: 956px) and (-webkit-device-pixel-ratio: 3) {
  html {
    font-size: 19px;
  }
}

/* iPhone 15/16 Pro Dynamic Island Alignment */
@media screen and (max-device-width: 440px) and (-webkit-min-device-pixel-ratio: 3) {
  :root {
    --safe-area-inset-top: env(safe-area-inset-top, 59px);
  }
}

/* iPhone SE (375x667) */
@media screen and (device-width: 375px) and (device-height: 667px) and (-webkit-device-pixel-ratio: 2) {
  html {
    font-size: 15px;
  }
}

/* iPhone 8 Plus (414x736) */
@media screen and (device-width: 414px) and (device-height: 736px) and (-webkit-device-pixel-ratio: 3) {
  html {
    font-size: 16px;
  }
}

/* ===== MOBILE BREAKPOINTS (Responsive font sizing only) ===== */

/* Small phones (320px - 374px) */
@media (max-width: 374px) {
  html {
    font-size: 14px;
  }
}

/* Standard phones (375px - 413px) */
@media (min-width: 375px) and (max-width: 413px) {
  html {
    font-size: 15px;
  }
}

/* Large phones (414px - 767px) */
@media (min-width: 414px) and (max-width: 767px) {
  html {
    font-size: 16px;
  }
}

/* ===== SMOOTH SCROLLING ===== */
* {
  -webkit-overflow-scrolling: touch;
}

html {
  scroll-behavior: smooth;
}

/* ===== DISABLE PULL-TO-REFRESH ON PWA ===== */
@media (display-mode: standalone) {
  body {
    overscroll-behavior-y: contain;
  }
}

/* ===== FIX IMAGES ===== */
img {
  max-width: 100%;
  height: auto;
}

/* ===== FIX TABLES ON MOBILE ===== */
@media (max-width: 767px) {
  table {
    display: block;
    overflow-x: auto;
    white-space: nowrap;
  }
}

/* ===== PERFORMANCE OPTIMIZATIONS ===== */
.gpu-accelerated {
  transform: translateZ(0);
  will-change: transform;
  -webkit-transform: translateZ(0);
}

/* ===== ACCESSIBILITY ===== */
@media (prefers-reduced-motion: reduce) {
  * {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}

/* ===== LANDSCAPE MODE ===== */
@media (max-width: 767px) and (orientation: landscape) {
  body {
    padding-top: calc(var(--safe-area-inset-top) + 8px);
  }
}

/* ===== UTILITY CLASSES ===== */

/* Hide desktop-only elements on mobile */
@media (max-width: 767px) {
  .desktop-only {
    display: none !important;
  }
}

/* Restored native video controls */
video {
  z-index: 1;
  pointer-events: auto;
}

/* Base Mobile Fixes */
.mobile-only {
  display: none;
}

@media (max-width: 767px) {
  .mobile-only {
    display: block !important;
  }
}

/* ===== PREVENT TEXT OVERFLOW ===== */
* {
  word-wrap: break-word;
  overflow-wrap: break-word;
}

/* ===== ENSURE CLICKABLE AREAS ===== */
a,
button,
[role="button"],
[onclick] {
  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
}

/* ===== PREVENT DOUBLE-TAP ZOOM ===== */
a,
button,
[role="button"] {
  touch-action: manipulation;
}

/* ===== FIX VIDEO PLAYERS ===== */
video {
  max-width: 100%;
  height: auto;
}

/* ===== FIX IFRAMES ===== */
iframe {
  max-width: 100%;
}

/* ===== IPHONE SAFE AREA HANDLING ===== */
@supports (padding: max(0px)) {
  @media (max-width: 767px) {
    body {
      padding-left: max(env(safe-area-inset-left), 0px);
      padding-right: max(env(safe-area-inset-right), 0px);
    }
  }
}

/* ===== IPHONE NOTCH/DYNAMIC ISLAND AWARENESS ===== */
@media (display-mode: standalone) {

  header,
  [class*="HeaderContainer"] {
    padding-top: max(env(safe-area-inset-top), 8px) !important;
  }

  main,
  .app-content {
    padding-top: 0 !important;
  }

  footer,
  .bottom-nav {
    padding-bottom: var(--safe-area-inset-bottom);
  }
}

/* ===== FINAL MOBILE SAFETY ===== */
@media (max-width: 767px) {
  body * {
    max-width: 100vw;
  }
}