/**
 * SkyFuns Mobile Enhanced Experience
 * 
 * Comprehensive mobile optimizations for touch-first interactions,
 * responsive layouts, and native app-like experience
 * 
 * Author: SkyFuns Development Team
 * Last Updated: October 31, 2025
 */

/* ==================== MOBILE-FIRST BASE ==================== */
:root {
  /* Mobile-optimized spacing */
  --mobile-spacing-xs: 0.5rem;
  --mobile-spacing-sm: 0.75rem;
  --mobile-spacing-md: 1rem;
  --mobile-spacing-lg: 1.5rem;
  --mobile-spacing-xl: 2rem;
  
  /* Touch target sizes (minimum 44x44px per WCAG) */
  --touch-target-min: 44px;
  --touch-target-comfortable: 48px;
  
  /* Mobile typography */
  --mobile-font-xs: 0.813rem;
  --mobile-font-sm: 0.875rem;
  --mobile-font-base: 1rem;
  --mobile-font-lg: 1.125rem;
  --mobile-font-xl: 1.25rem;
  
  /* Safe areas for notched devices */
  --safe-area-inset-top: env(safe-area-inset-top, 0);
  --safe-area-inset-right: env(safe-area-inset-right, 0);
  --safe-area-inset-bottom: env(safe-area-inset-bottom, 0);
  --safe-area-inset-left: env(safe-area-inset-left, 0);
}

/* ==================== VIEWPORT & SCROLL ==================== */
@media (max-width: 768px) {
  html {
    /* Prevent text size adjustment after orientation change */
    -webkit-text-size-adjust: 100%;
    -moz-text-size-adjust: 100%;
    -ms-text-size-adjust: 100%;
    text-size-adjust: 100%;
    
    /* Smooth scrolling */
    scroll-behavior: smooth;
    
    /* Optimize for touch */
    -webkit-tap-highlight-color: rgba(63, 183, 180, 0.2);
    -webkit-touch-callout: none;
  }

  body {
    /* Prevent horizontal scroll */
    overflow-x: hidden;
    
    /* Minimum font size to prevent zoom on inputs */
    font-size: 16px;
    
    /* Better text rendering */
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    text-rendering: optimizeLegibility;
  }

  /* Prevent iOS zoom on focus */
  input[type="text"],
  input[type="number"],
  input[type="email"],
  input[type="tel"],
  input[type="password"],
  input[type="search"],
  input[type="date"],
  input[type="time"],
  select,
  textarea {
    font-size: 16px !important;
  }
}

/* ==================== MOBILE NAVIGATION ==================== */
@media (max-width: 768px) {
  /* Fixed header with safe area support */
  .sf-nav,
  #header {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    z-index: 1000;
    padding-top: var(--safe-area-inset-top);
    background: color-mix(in srgb, var(--bg-primary, #ffffff) 96%, transparent);
    border-bottom: 1px solid color-mix(in srgb, var(--border-light, rgba(148, 163, 184, 0.25)) 85%, transparent);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    box-shadow: 0 16px 32px color-mix(in srgb, var(--shadow-primary, rgba(15, 23, 42, 0.35)) 45%, transparent);
  }

  /* Content padding to account for fixed header */
  main,
  .page-content,
  #content:not(.content-auth) {
    padding-top: calc(64px + var(--safe-area-inset-top));
  }

  /* Mobile menu drawer */
  .sf-nav__drawer {
    position: fixed;
    top: 64px;
    left: 0;
    right: 0;
    bottom: 0;
    background: var(--bg-primary);
    transform: translateX(-100%);
    transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
    padding: var(--mobile-spacing-lg);
    padding-bottom: calc(var(--mobile-spacing-xl) + var(--safe-area-inset-bottom));
  }

  .sf-nav__drawer:not([hidden]) {
    transform: translateX(0);
  }

  .sf-nav__drawer-links {
    display: flex;
    flex-direction: column;
    gap: var(--mobile-spacing-xs);
  }

  .sf-nav__drawer-links a {
    display: flex;
    align-items: center;
    padding: var(--mobile-spacing-md);
    min-height: var(--touch-target-comfortable);
    border-radius: 12px;
    font-size: var(--mobile-font-base);
    font-weight: 500;
    color: var(--text-primary);
    text-decoration: none;
    transition: all 0.2s ease;
    -webkit-tap-highlight-color: transparent;
  }

  .sf-nav__drawer-links a:active {
    transform: scale(0.98);
    background: color-mix(in srgb, var(--accent-primary) 10%, transparent);
  }

  /* Hamburger menu button */
  .sf-nav__menu-toggle {
    width: var(--touch-target-comfortable);
    height: var(--touch-target-comfortable);
    padding: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    border: none;
    background: transparent;
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
  }

  /* Prevent body scroll when menu is open */
  html.mobile-nav-open,
  html.mobile-nav-open body {
    overflow: hidden;
    height: 100%;
  }
}

/* ==================== TOUCH-OPTIMIZED BUTTONS ==================== */
@media (max-width: 768px) {
  button,
  .sf-btn,
  .booking-form__submit,
  .rent-btn,
  input[type="button"],
  input[type="submit"],
  .btn,
  [role="button"] {
    min-height: var(--touch-target-comfortable);
    min-width: var(--touch-target-comfortable);
    padding: 0.875rem 1.25rem;
    font-size: var(--mobile-font-base);
    border-radius: 12px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
    user-select: none;
    -webkit-user-select: none;
  }

  /* Compact button variant */
  .sf-btn--compact,
  .btn--compact {
    min-height: var(--touch-target-min);
    padding: 0.625rem 1rem;
    font-size: var(--mobile-font-sm);
  }

  /* Full-width buttons on mobile */
  .sf-btn--block,
  .btn--block,
  .booking-form__submit,
  .rent-btn--primary {
    width: 100%;
  }

  /* Button groups stack vertically */
  .sf-inline-actions,
  .sf-actions,
  .booking-header__actions,
  .flight-actions {
    flex-direction: column;
    gap: var(--mobile-spacing-md);
  }

  .sf-inline-actions .sf-btn,
  .sf-actions .sf-btn {
    width: 100%;
  }

  /* Icon buttons maintain square aspect */
  .sf-btn-icon,
  button[aria-label]:not(:has(span)),
  .booking-header__action {
    width: var(--touch-target-comfortable);
    height: var(--touch-target-comfortable);
    min-width: unset;
    padding: 0;
  }
}

/* ==================== FORM INPUTS ==================== */
@media (max-width: 768px) {
  input[type="text"],
  input[type="number"],
  input[type="email"],
  input[type="tel"],
  input[type="password"],
  input[type="search"],
  input[type="date"],
  input[type="time"],
  input[type="datetime-local"],
  input[type="url"],
  select,
  textarea {
    width: 100%;
    min-height: var(--touch-target-comfortable);
    padding: 0.875rem 1rem;
    font-size: 16px; /* Prevent zoom on iOS */
    border-radius: 12px;
    border: 2px solid var(--border-light);
    background: var(--bg-primary);
    color: var(--text-primary);
    transition: all 0.2s ease;
    -webkit-appearance: none;
    appearance: none;
  }

  input:focus,
  select:focus,
  textarea:focus {
    outline: none;
    border-color: var(--accent-primary);
    box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent-primary) 15%, transparent);
  }

  textarea {
    min-height: 120px;
    resize: vertical;
  }

  /* Labels with proper spacing */
  label,
  .booking-form__label,
  .flight-label {
    display: block;
    margin-bottom: var(--mobile-spacing-xs);
    font-size: var(--mobile-font-sm);
    font-weight: 600;
    color: var(--text-primary);
  }

  /* Form rows stack */
  .booking-form__row,
  .flight-form__row,
  .form-row {
    display: grid;
    gap: var(--mobile-spacing-sm);
    margin-bottom: var(--mobile-spacing-md);
  }

  /* Checkbox and radio larger tap targets */
  input[type="checkbox"],
  input[type="radio"] {
    width: 24px;
    height: 24px;
    margin-right: var(--mobile-spacing-sm);
  }

  /* Select dropdown arrow */
  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='%23666' d='M6 9L1 4h10z'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 1rem center;
    padding-right: 2.5rem;
  }
}

/* ==================== CARDS & PANELS ==================== */
@media (max-width: 768px) {
  .sf-card,
  .sf-panel,
  .booking-price-card,
  .flight-card {
    padding: var(--mobile-spacing-lg);
    border-radius: 16px;
    margin-bottom: var(--mobile-spacing-md);
  }

  /* Card headers */
  .sf-card__title,
  .booking-section__title {
    font-size: var(--mobile-font-lg);
    margin-bottom: var(--mobile-spacing-sm);
  }

  /* List items with proper touch targets */
  .sf-list__item {
    padding: var(--mobile-spacing-lg);
    border-radius: 16px;
    margin-bottom: var(--mobile-spacing-sm);
    min-height: var(--touch-target-comfortable);
  }

  .sf-list__row {
    flex-direction: column;
    align-items: flex-start;
    gap: var(--mobile-spacing-sm);
  }

  .sf-tag {
    align-self: flex-start;
  }
}

/* ==================== GRIDS & LAYOUTS ==================== */
@media (max-width: 768px) {
  /* All grids become single column */
  .sf-grid,
  .sf-grid--two,
  .sf-grid--three,
  .sf-grid--four,
  .booking-layout,
  .flight-submit__layout {
    grid-template-columns: 1fr;
    gap: var(--mobile-spacing-md);
  }

  /* Sidebar becomes full width and reorders */
  .booking-sidebar,
  .flight-summary {
    position: static;
    width: 100%;
    max-width: none;
    order: -1; /* Show summary first on mobile */
  }

  /* Gallery adjustments */
  .booking-gallery__thumbs {
    grid-template-columns: repeat(2, 1fr);
    gap: var(--mobile-spacing-sm);
  }

  /* Stats grid responsive */
  .sf-stat-list {
    grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
    gap: var(--mobile-spacing-sm);
  }

  /* Highlight cards */
  .booking-highlights {
    grid-template-columns: 1fr;
    gap: var(--mobile-spacing-sm);
  }
}

/* ==================== DASHBOARD MOBILE ==================== */
@media (max-width: 768px) {
  .sf-dashboard {
    padding: var(--mobile-spacing-lg) var(--mobile-spacing-md);
    padding-bottom: calc(var(--mobile-spacing-xl) + var(--safe-area-inset-bottom));
  }

  .sf-dashboard__container {
    gap: var(--mobile-spacing-lg);
  }

  /* Dashboard hero section */
  .sf-dashboard__hero {
    padding: var(--mobile-spacing-lg);
    border-radius: 20px;
  }

  .sf-dashboard__greeting {
    font-size: var(--mobile-font-xl);
  }

  /* Module sections */
  .sf-module {
    padding: var(--mobile-spacing-lg);
    border-radius: 18px;
  }

  .sf-module__title {
    font-size: var(--mobile-font-lg);
  }

  /* Metrics/stats */
  .sf-stat {
    padding: var(--mobile-spacing-md);
    border-radius: 14px;
  }

  .sf-stat__value {
    font-size: 1.75rem;
  }

  .sf-stat__label {
    font-size: var(--mobile-font-xs);
  }
}

/* ==================== TABLES ==================== */
@media (max-width: 768px) {
  /* Responsive table wrapper */
  .table-responsive,
  .rent-table__wrap {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    margin: 0 calc(var(--mobile-spacing-md) * -1);
    padding: 0 var(--mobile-spacing-md);
  }

  table {
    min-width: 600px; /* Prevent squishing */
  }

  table td,
  table th {
    padding: var(--mobile-spacing-md) var(--mobile-spacing-sm);
    font-size: var(--mobile-font-sm);
  }

  /* Convert complex tables to cards on mobile */
  .table-as-cards {
    display: block;
  }

  .table-as-cards thead {
    display: none;
  }

  .table-as-cards tbody,
  .table-as-cards tr {
    display: block;
  }

  .table-as-cards tr {
    margin-bottom: var(--mobile-spacing-md);
    padding: var(--mobile-spacing-md);
    border-radius: 12px;
    background: var(--surface-card);
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
  }

  .table-as-cards td {
    display: flex;
    justify-content: space-between;
    padding: var(--mobile-spacing-sm) 0;
    border: none;
  }

  .table-as-cards td::before {
    content: attr(data-label);
    font-weight: 600;
    margin-right: var(--mobile-spacing-md);
  }
}

/* ==================== MODALS & OVERLAYS ==================== */
@media (max-width: 768px) {
  .modal,
  .sf-modal,
  .overlay-modal {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    max-width: 100%;
    max-height: 100%;
    margin: 0;
    border-radius: 0;
    padding: var(--mobile-spacing-lg);
    padding-top: calc(var(--mobile-spacing-lg) + var(--safe-area-inset-top));
    padding-bottom: calc(var(--mobile-spacing-lg) + var(--safe-area-inset-bottom));
  }

  /* Modal content scrollable */
  .modal__content,
  .sf-modal__body {
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    max-height: calc(100vh - 120px);
  }

  /* Modal header sticky */
  .modal__header,
  .sf-modal__header {
    position: sticky;
    top: 0;
    background: var(--bg-primary);
    z-index: 10;
    padding-bottom: var(--mobile-spacing-md);
    border-bottom: 1px solid var(--border-light);
    margin-bottom: var(--mobile-spacing-md);
  }

  /* Close button accessible */
  .modal__close,
  .sf-modal__close {
    width: var(--touch-target-comfortable);
    height: var(--touch-target-comfortable);
  }
}

/* ==================== IMAGES & MEDIA ==================== */
@media (max-width: 768px) {
  img,
  picture,
  video,
  .responsive-image {
    max-width: 100%;
    height: auto;
    display: block;
  }

  /* Gallery images */
  .booking-gallery__main,
  .flight-gallery__main {
    aspect-ratio: 16 / 9;
    border-radius: 16px;
    overflow: hidden;
  }

  /* Avatar sizes */
  .sf-avatar,
  .booking-pilot__avatar {
    width: 48px;
    height: 48px;
    border-radius: 50%;
  }

  .sf-avatar--large {
    width: 64px;
    height: 64px;
  }
}

/* ==================== CALENDAR & DATE PICKERS ==================== */
@media (max-width: 768px) {
  .booking-calendar-section {
    padding: var(--mobile-spacing-md);
  }

  .booking-calendar__slot {
    padding: var(--mobile-spacing-md);
    margin-bottom: var(--mobile-spacing-sm);
  }

  /* Native date picker styling */
  input[type="date"]::-webkit-calendar-picker-indicator,
  input[type="time"]::-webkit-calendar-picker-indicator {
    width: 24px;
    height: 24px;
    opacity: 0.6;
  }
}

/* ==================== TOASTS & NOTIFICATIONS ==================== */
@media (max-width: 768px) {
  .sf-toast,
  .notification,
  .alert {
    left: var(--mobile-spacing-md);
    right: var(--mobile-spacing-md);
    bottom: calc(var(--mobile-spacing-md) + var(--safe-area-inset-bottom));
    max-width: calc(100% - var(--mobile-spacing-md) * 2);
    border-radius: 12px;
    padding: var(--mobile-spacing-md);
    font-size: var(--mobile-font-sm);
  }
}

/* ==================== BOTTOM NAVIGATION (Optional Enhancement) ==================== */
@media (max-width: 768px) {
}

/* ==================== PULL-TO-REFRESH STYLING ==================== */
@media (max-width: 768px) {
  .ptr-wrapper {
    position: relative;
  }

  .ptr-element {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: 60px;
    display: flex;
    align-items: center;
    justify-content: center;
    transform: translateY(-60px);
    transition: transform 0.3s ease;
    background: var(--bg-primary);
  }

  .ptr-element.ptr-refresh {
    transform: translateY(0);
  }

  .ptr-spinner {
    width: 24px;
    height: 24px;
    border: 3px solid var(--border-light);
    border-top-color: var(--accent-primary);
    border-radius: 50%;
    animation: ptr-spin 1s linear infinite;
  }

  @keyframes ptr-spin {
    to { transform: rotate(360deg); }
  }
}

/* ==================== ACCESSIBILITY ENHANCEMENTS ==================== */
@media (max-width: 768px) {
  /* Focus visible states */
  *:focus-visible {
    outline: 3px solid var(--accent-primary);
    outline-offset: 2px;
  }

  /* Skip to content link */
  .skip-to-content {
    position: absolute;
    top: -100px;
    left: 0;
    background: var(--accent-primary);
    color: white;
    padding: var(--mobile-spacing-md);
    text-decoration: none;
    z-index: 9999;
  }

  .skip-to-content:focus {
    top: 0;
  }

  /* Reduced motion support */
  @media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
      animation-duration: 0.01ms !important;
      animation-iteration-count: 1 !important;
      transition-duration: 0.01ms !important;
    }
  }
}

/* ==================== LANDSCAPE ORIENTATION ==================== */
@media (max-width: 768px) and (orientation: landscape) {
  /* Reduce vertical padding in landscape */
  .sf-dashboard,
  .booking,
  main {
    padding-top: calc(48px + var(--safe-area-inset-top));
    padding-bottom: var(--mobile-spacing-md);
  }

  /* Compact header in landscape */
  .sf-nav,
  #header {
    height: 48px;
  }

  /* Two-column layout for stats in landscape */
  .sf-stat-list {
    grid-template-columns: repeat(2, 1fr);
  }
}

/* ==================== DARK MODE SUPPORT ==================== */
/* ==================== PROGRESSIVE WEB APP SUPPORT ==================== */
@media (max-width: 768px) {
  /* Standalone mode (when installed as PWA) */
  @media (display-mode: standalone) {
    body {
      padding-top: var(--safe-area-inset-top);
    }

    .sf-nav,
    #header {
      padding-top: calc(12px + var(--safe-area-inset-top));
    }
  }
}

/* ==================== ANDROID & iOS SPECIFIC FIXES ==================== */
@media (max-width: 768px) {
  /* Fix for iOS bottom toolbar overlap */
  @supports (-webkit-touch-callout: none) {
    body {
      min-height: -webkit-fill-available;
    }

  }

  /* Android specific optimizations */
  @supports (not (-webkit-touch-callout: none)) {
    /* Smoother scrolling on Android */
    * {
      -webkit-overflow-scrolling: touch;
      scroll-behavior: smooth;
    }
  }
}
