/**
 * =============================================================================
 * HOSPITAL FE — MOBILE TOUCH UX
 * =============================================================================
 * Optimized for touch devices: iPad, iPhone, Android tablets & phones.
 * Uses @media (hover: none) and (pointer: coarse) to target touch-only.
 * Safe to load alongside desktop CSS — no conflicts.
 * =============================================================================
 */

/* ============================================================
   1. GLOBAL TOUCH TARGETS — WCAG 2.5.5 AA minimum 44x44px
   ============================================================ */
@media (hover: none) and (pointer: coarse) {
  /* All interactive elements must have minimum 44x44 touch target */
  button,
  [type="button"],
  [type="submit"],
  [type="reset"],
  a,
  .btn,
  .dsf-btn,
  .nav-link,
  .dropdown-toggle,
  .dsf-tab {
    min-height: 44px !important;
    min-width: 44px !important;
  }

  /* Remove hover-only effects that don't work on touch */
  a:hover,
  .btn:hover,
  .dsf-card:hover {
    transform: none !important;
  }

  /* Larger checkbox & radio inputs for touch */
  input[type="checkbox"],
  input[type="radio"] {
    width: 20px !important;
    height: 20px !important;
    accent-color: var(--color-accent-primary);
    cursor: pointer;
  }

  /* Larger selects for touch */
  select,
  .dsf-select {
    min-height: 44px !important;
    font-size: 16px !important; /* prevent iOS zoom on focus */
    cursor: pointer;
  }

  /* Larger text inputs for touch */
  input[type="text"],
  input[type="number"],
  input[type="email"],
  input[type="tel"],
  input[type="date"],
  input[type="time"],
  input[type="datetime-local"],
  input[type="search"],
  input[type="password"],
  textarea,
  .form-control,
  .dsf-input {
    min-height: 44px !important;
    font-size: 16px !important; /* iOS: prevent auto zoom */
    padding: 10px 12px !important;
  }

  /* Remove problematic hover pseudo-states on touch */
  .page-sidebar .page-sidebar-menu > li > a:hover {
    background: transparent !important;
  }
}

/* ============================================================
   2. SWIPE-FRIENDLY SIDEBAR — Touch gesture helpers
   CSS only: apply visual swipe indicator on mobile
   ============================================================ */
@media (max-width: 991px) {
  /* Swipe handle on the sidebar edge */
  body .page-sidebar-wrapper::after {
    content: '';
    position: absolute;
    top: 50%;
    right: -12px;
    transform: translateY(-50%);
    width: 4px;
    height: 48px;
    background: var(--color-accent-primary);
    border-radius: 0 4px 4px 0;
    opacity: 0;
    transition: opacity 0.3s ease;
    pointer-events: none;
  }

  body:not(.page-sidebar-closed) .page-sidebar-wrapper::after {
    opacity: 0.6;
  }

  /* Hamburger / Responsive toggler — touch optimized */
  body .page-header.navbar .responsive-toggler,
  body .page-header.navbar .sidebar-toggler,
  body .page-header.navbar .menu-toggler {
    display: flex !important;
    align-items: center !important;
    justify-content: center !important;
    width: 44px !important;
    height: 44px !important;
    border-radius: 10px !important;
    cursor: pointer !important;
    -webkit-tap-highlight-color: transparent !important;
    touch-action: manipulation !important;
    background: transparent !important;
    border: none !important;
    transition: background 0.2s ease !important;
  }

  body .page-header.navbar .responsive-toggler:active,
  body .page-header.navbar .sidebar-toggler:active {
    background: var(--color-accent-subtle) !important;
  }
}

/* ============================================================
   3. SCROLL OPTIMIZATION — Smooth native scrolling
   ============================================================ */
@media (max-width: 991px) {
  /* Smooth momentum scrolling on iOS */
  body .page-sidebar,
  body .page-sidebar-wrapper,
  body .page-content-wrapper .page-content,
  body .table-responsive,
  body .patient-info-header {
    -webkit-overflow-scrolling: touch !important;
  }

  /* Full-height layout using dvh (dynamic viewport height) for iOS Safari */
  body .page-container,
  body .page-wrapper {
    min-height: 100dvh !important;
  }

  body .page-sidebar {
    height: calc(100dvh - var(--header-height, 60px)) !important;
  }

  /* Safe area insets for iOS notch / home bar */
  body .page-header.navbar {
    padding-top: env(safe-area-inset-top, 0) !important;
    height: calc(var(--header-height, 60px) + env(safe-area-inset-top, 0px)) !important;
  }

  body .page-content-wrapper .page-content {
    padding-bottom: calc(16px + env(safe-area-inset-bottom, 0px)) !important;
  }
}

/* ============================================================
   4. FORM UX ON TOUCH — Auto focus & tap improvements
   ============================================================ */
@media (hover: none) and (pointer: coarse) {
  /* Remove 300ms tap delay universally */
  * {
    touch-action: manipulation;
    -webkit-tap-highlight-color: rgba(59, 130, 246, 0.15);
  }

  /* Allow scroll on scroll containers, not click */
  .page-sidebar,
  .table-responsive,
  .dsf-modal-body {
    touch-action: pan-y !important;
  }

  /* Horizontal scroll containers */
  .page-bar,
  .patient-info-header,
  .dsf-tabbar {
    touch-action: pan-x !important;
  }

  /* date inputs — use native pickers on mobile */
  input[type="date"],
  input[type="time"],
  input[type="datetime-local"] {
    appearance: none !important;
    -webkit-appearance: none !important;
    background: var(--color-bg-base) !important;
    color: var(--color-text-primary) !important;
  }

  /* Inputmode numeric for number fields */
  input[type="number"] {
    -moz-appearance: textfield !important;
  }

  /* Avoid zoom on focus on iOS (font-size >= 16px) */
  .form-control,
  .dsf-input,
  select,
  input {
    font-size: max(16px, 1em) !important;
  }
}

/* ============================================================
   5. DROPDOWN MENUS — Touch friendly positioning
   ============================================================ */
@media (max-width: 991px) {
  /* Dropdowns open below on mobile — avoid overflow */
  .dropdown-menu {
    position: fixed !important;
    top: auto !important;
    left: 8px !important;
    right: 8px !important;
    max-width: calc(100vw - 16px) !important;
    max-height: 60vh !important;
    overflow-y: auto !important;
    -webkit-overflow-scrolling: touch !important;
    border-radius: 16px !important;
    box-shadow: var(--shadow-2xl) !important;
    z-index: var(--z-dropdown, 100) !important;
  }

  /* Dropdown items — touch target */
  .dropdown-menu > li > a {
    padding: 14px 18px !important;
    font-size: 14px !important;
    min-height: 48px !important;
    display: flex !important;
    align-items: center !important;
    gap: 10px !important;
  }
}

/* ============================================================
   6. AG-GRID TOUCH — Pinch zoom disable, smooth scroll
   ============================================================ */
@media (hover: none) and (pointer: coarse) {
  /* Disable ag-grid's own touch interference */
  .ag-body-viewport,
  .ag-center-cols-viewport,
  .ag-horizontal-left-spacer,
  .ag-horizontal-right-spacer {
    -webkit-overflow-scrolling: touch !important;
  }

  /* Larger row height for finger tapping */
  body .ag-theme-fresh .ag-row {
    height: 44px !important;
    min-height: 44px !important;
    cursor: pointer !important;
  }

  /* Touch ripple on row click */
  body .ag-theme-fresh .ag-row:active {
    background-color: var(--color-accent-subtle) !important;
  }

  /* Compact pagination buttons for touch */
  body .ag-theme-fresh .ag-paging-button {
    min-width: 36px !important;
    min-height: 36px !important;
  }
}

/* ============================================================
   7. POPUP / MODELBOX TOUCH — Full screen on phone
   ============================================================ */
@media (max-width: 767px) {
  /* Legacy popup-static full screen on phone */
  body .popup-static.portlet {
    position: fixed !important;
    inset: 0 !important;
    width: 100vw !important;
    height: 100dvh !important;
    margin: 0 !important;
    border-radius: 0 !important;
    overflow-y: auto !important;
    -webkit-overflow-scrolling: touch !important;
  }

  body .popup-static .modelbox-div {
    width: 100% !important;
    max-width: 100% !important;
    flex: none !important;
    margin: 0 !important;
    border-radius: 0 !important;
    min-height: 100dvh !important;
    box-shadow: none !important;
  }

  /* Popup close button — easier to tap */
  .close-modelbox button,
  .close-modelbox .btn {
    min-width: 44px !important;
    min-height: 44px !important;
    border-radius: 0 0 0 12px !important;
  }
}

/* ============================================================
   8. PRINT OVERRIDE — Remove touch styles when printing
   ============================================================ */
@media print {
  * {
    touch-action: auto !important;
    -webkit-tap-highlight-color: transparent !important;
  }
}
