/**
 * Mobile-First Optimization Styles
 * Following 2025 best practices for mobile app design
 * Based on Apple HIG, Material Design, and industry standards
 */

/* ============================================
   MOBILE-FIRST DESIGN PRINCIPLES
   ============================================ */

/* 1. Touch Target Sizes (Minimum 44x44px per Apple HIG & Material Design) */
@media (max-width: 768px) {
  /* All interactive elements must be at least 44x44px */
  button,
  a[role="button"],
  input[type="button"],
  input[type="submit"],
  .nav-tab,
  .suggestion-btn,
  .voice-btn,
  .send-btn,
  .map-control-btn {
    min-width: 44px;
    min-height: 44px;
    padding: 12px 16px;
  }
  
  /* Larger touch targets for primary actions */
  button.primary,
  .chat-send-btn,
  .voice-btn.primary {
    min-width: 48px;
    min-height: 48px;
    padding: 14px 18px;
  }
  
  /* Ensure adequate spacing between touch targets */
  button + button,
  .nav-tab + .nav-tab {
    margin-left: 8px;
  }
}

/* 2. Responsive Charts - Mobile Optimized */
@media (max-width: 768px) {
  /* Chart containers */
  #tide-chart,
  #weather-history-chart,
  canvas {
    max-width: 100%;
    height: auto !important;
    min-height: 240px;
    max-height: 280px;
  }
  
  /* Chart wrapper responsive sizing */
  .chart-wrap,
  .chart-container {
    width: 100%;
    height: 280px;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }
  
  /* Chart.js responsive configuration */
  .chart-container canvas {
    touch-action: pan-x pan-y pinch-zoom;
  }
  
  /* Smaller chart padding on mobile */
  .glass-card canvas {
    padding: 8px;
  }
}

/* Landscape mode - shorter charts */
@media (max-width: 896px) and (orientation: landscape) {
  .chart-wrap,
  .chart-container {
    height: 200px;
    max-height: 200px;
  }
  
  #tide-chart,
  #weather-history-chart {
    max-height: 200px;
  }
}

/* 3. Chat Box Mobile Enhancements */
@media (max-width: 768px) {
  /* Full-screen chat on mobile */
  .chat-panel {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    width: 100vw;
    height: 100vh;
    max-width: 100vw;
    max-height: 100vh;
    border-radius: 0;
    margin: 0;
    z-index: 9999;
  }
  
  /* Safe area support for notched devices */
  .chat-header {
    padding-top: calc(env(safe-area-inset-top, 0px) + 16px);
    padding-left: env(safe-area-inset-left, 0px);
    padding-right: env(safe-area-inset-right, 0px);
  }
  
  .chat-input {
    padding-bottom: calc(env(safe-area-inset-bottom, 0px) + 16px);
    padding-left: env(safe-area-inset-left, 0px);
    padding-right: env(safe-area-inset-right, 0px);
  }
  
  /* Larger message bubbles on mobile */
  .message {
    max-width: 85%;
    padding: 14px 18px;
    font-size: 16px; /* Prevent iOS zoom on focus */
    line-height: 1.6;
  }
  
  /* Better touch targets for chat buttons */
  .chat-input button,
  .voice-btn,
  .send-btn {
    min-width: 48px;
    min-height: 48px;
    width: 48px;
    height: 48px;
  }
  
  /* Larger input field */
  .chat-text-input {
    font-size: 16px; /* Prevents iOS zoom */
    padding: 14px 18px;
    min-height: 48px;
  }
  
  /* Swipe to close gesture hint */
  .chat-header::after {
    content: '← Swipe down to close';
    position: absolute;
    bottom: -24px;
    left: 50%;
    transform: translateX(-50%);
    font-size: 12px;
    color: rgba(255, 255, 255, 0.7);
    pointer-events: none;
  }
}

/* 4. Modal & Window Mobile Optimization */
@media (max-width: 768px) {
  /* Full-screen modals on mobile */
  #qr-modal,
  .modal,
  .dialog {
    padding: 0;
    align-items: flex-start;
  }
  
  .modal > div,
  .dialog > div {
    width: 100vw;
    max-width: 100vw;
    height: 100vh;
    max-height: 100vh;
    border-radius: 0;
    margin: 0;
    padding: calc(env(safe-area-inset-top, 0px) + 16px) 
             env(safe-area-inset-left, 0px) 
             calc(env(safe-area-inset-bottom, 0px) + 16px) 
             env(safe-area-inset-right, 0px);
  }
  
  /* Modal close button - larger and easier to tap */
  .modal button[aria-label="Close"],
  .modal .close-btn {
    min-width: 44px;
    min-height: 44px;
    padding: 12px;
    position: fixed;
    top: calc(env(safe-area-inset-top, 0px) + 8px);
    right: calc(env(safe-area-inset-right, 0px) + 8px);
    z-index: 10001;
  }
}

/* 5. Map Windows & Panels Mobile Optimization */
@media (max-width: 768px) {
  /* Map layers panel - full width on mobile */
  #layers-panel,
  #legend-panel {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    width: 100vw;
    max-width: 100vw;
    height: auto;
    max-height: 80vh;
    border-radius: 0 0 20px 20px;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }
  
  /* Map controls - larger touch targets */
  .map-control-btn {
    min-width: 44px;
    min-height: 44px;
    padding: 12px 16px;
    font-size: 14px;
  }
  
  /* Map info panel - bottom sheet style */
  #map-info,
  #map-summary-panel {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    width: 100vw;
    max-width: 100vw;
    border-radius: 20px 20px 0 0;
    padding-bottom: calc(env(safe-area-inset-bottom, 0px) + 16px);
  }
  
  /* Map container - full height on mobile */
  #map-container,
  #map {
    height: 60vh;
    min-height: 400px;
  }
}

/* 6. Navigation & Header Mobile Improvements */
@media (max-width: 768px) {
  /* Header - compact on mobile */
  #main-header {
    padding: 12px 16px;
    padding-top: calc(env(safe-area-inset-top, 0px) + 12px);
  }
  
  /* Header buttons - stack on very small screens */
  #main-header .flex.items-center.gap-4 {
    flex-wrap: wrap;
    gap: 8px;
  }
  
  /* Navigation tabs - horizontal scroll */
  nav {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    -ms-overflow-style: none;
  }
  
  nav::-webkit-scrollbar {
    display: none;
  }
  
  /* Navigation tabs - larger on mobile */
  .nav-tab {
    min-width: 70px;
    min-height: 64px;
    padding: 12px 16px;
    flex-shrink: 0;
  }
  
  /* Hide text labels on very small screens, show only icons */
  @media (max-width: 360px) {
    .nav-tab span:last-child {
      display: none;
    }
    
    .nav-tab {
      min-width: 56px;
    }
  }
}

/* 7. Cards & Content Mobile Optimization */
@media (max-width: 768px) {
  /* Single column layout */
  .grid-enhanced,
  .grid {
    grid-template-columns: 1fr !important;
    gap: 16px;
  }
  
  /* Cards - full width, better padding */
  .glass-card {
    width: 100%;
    margin: 0 0 16px 0;
    padding: 16px;
    border-radius: 16px;
  }
  
  /* Card content - better spacing */
  .glass-card > div {
    padding: 12px;
  }
  
  /* Grid items - stack on mobile */
  .grid.grid-cols-2,
  .grid.grid-cols-4 {
    grid-template-columns: 1fr;
  }
  
  /* Stats grid - 2 columns max on mobile */
  .grid.grid-cols-4 {
    grid-template-columns: repeat(2, 1fr);
    gap: 12px;
  }
}

/* 8. Touch Feedback & Interactions */
@media (hover: none) and (pointer: coarse) {
  /* Visual feedback on touch */
  button:active,
  .nav-tab:active,
  .suggestion-btn:active,
  a:active {
    transform: scale(0.96);
    opacity: 0.8;
    transition: transform 0.1s, opacity 0.1s;
  }
  
  /* Remove hover effects on touch devices */
  button:hover,
  .nav-tab:hover,
  .glass-card:hover {
    transform: none;
  }
  
  /* Prevent text selection on touch */
  button,
  .nav-tab,
  .suggestion-btn {
    -webkit-user-select: none;
    user-select: none;
    -webkit-tap-highlight-color: rgba(255, 255, 255, 0.1);
  }
  
  /* Larger tap highlight area */
  button,
  a,
  .clickable {
    -webkit-tap-highlight-color: rgba(255, 255, 255, 0.2);
  }
}

/* 9. Typography & Readability */
@media (max-width: 768px) {
  /* Base font size - readable on mobile */
  body {
    font-size: 16px;
    line-height: 1.6;
  }
  
  /* Headings - responsive sizes */
  h1 {
    font-size: 1.75rem;
    line-height: 1.3;
  }
  
  h2 {
    font-size: 1.5rem;
    line-height: 1.3;
  }
  
  h3 {
    font-size: 1.25rem;
    line-height: 1.4;
  }
  
  /* Prevent iOS text size adjustment */
  input,
  textarea,
  select {
    font-size: 16px;
  }
  
  /* Better line spacing for readability */
  p {
    line-height: 1.7;
    margin-bottom: 1em;
  }
}

/* 10. Performance Optimizations */
@media (max-width: 768px) {
  /* Reduce animations on mobile for better performance */
  * {
    animation-duration: 0.2s !important;
    transition-duration: 0.2s !important;
  }
  
  /* Disable expensive effects on mobile */
  .glass-card {
    backdrop-filter: blur(8px); /* Reduced from 10px */
  }
  
  /* Simplify shadows on mobile */
  .glass-card {
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.1);
  }
  
  .glass-card:hover {
    box-shadow: 0 6px 20px rgba(0, 0, 0, 0.12);
  }
}

/* 11. Landscape Mode Optimizations */
@media (max-width: 896px) and (orientation: landscape) {
  /* Compact header in landscape */
  #main-header {
    padding: 8px 16px;
  }
  
  /* Shorter navigation */
  nav {
    padding: 4px 0;
  }
  
  /* Compact cards */
  .glass-card {
    padding: 12px;
    margin-bottom: 12px;
  }
  
  /* Shorter charts */
  .chart-wrap {
    height: 180px;
  }
  
  /* Map - use more vertical space */
  #map-container {
    height: calc(100vh - 120px);
  }
}

/* 12. Very Small Screens (iPhone SE, etc.) */
@media (max-width: 375px) {
  /* Even more compact */
  .glass-card {
    padding: 12px;
  }
  
  /* Smaller text but still readable */
  body {
    font-size: 15px;
  }
  
  /* Compact buttons */
  button {
    padding: 10px 14px;
    font-size: 14px;
  }
  
  /* Hide less important elements */
  #live-date {
    display: none;
  }
}

/* 13. Tablet Optimizations (iPad, etc.) */
@media (min-width: 768px) and (max-width: 1024px) {
  /* 2-column layout on tablet */
  .grid-enhanced {
    grid-template-columns: repeat(2, 1fr);
    gap: 20px;
  }
  
  /* Medium-sized charts */
  .chart-wrap {
    height: 320px;
  }
  
  /* Chat panel - side panel style */
  .chat-panel {
    width: 400px;
    height: 600px;
  }
}

/* 14. Accessibility - High Contrast & Reduced Motion */
@media (prefers-contrast: high) {
  .glass-card {
    border: 2px solid rgba(255, 255, 255, 0.4);
  }
  
  button {
    border: 2px solid currentColor;
  }
}

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

/* 15. PWA Enhancements */
@media (display-mode: standalone) {
  /* Full-screen PWA mode */
  body {
    padding-top: env(safe-area-inset-top, 0px);
    padding-bottom: env(safe-area-inset-bottom, 0px);
  }
  
  #main-header {
    padding-top: calc(env(safe-area-inset-top, 0px) + 16px);
  }
}

/* 16. iOS Safari Specific Fixes */
@supports (-webkit-touch-callout: none) {
  /* Fix viewport height bug */
  body {
    min-height: -webkit-fill-available;
  }
  
  /* Fix input zoom */
  input[type="text"],
  input[type="email"],
  input[type="number"],
  textarea,
  select {
    font-size: 16px;
  }
  
  /* Better scrolling */
  * {
    -webkit-overflow-scrolling: touch;
  }
}

/* 17. Android Chrome Specific */
@media screen and (-webkit-min-device-pixel-ratio: 0) {
  /* Better touch handling */
  button,
  a {
    -webkit-tap-highlight-color: rgba(255, 255, 255, 0.2);
  }
}

/* 18. Utility Classes for Mobile */
.mobile-only {
  display: none;
}

.desktop-only {
  display: block;
}

@media (max-width: 768px) {
  .mobile-only {
    display: block;
  }
  
  .desktop-only {
    display: none;
  }
}

/* 19. Swipe Gestures Support */
.swipeable {
  touch-action: pan-y;
  -webkit-overflow-scrolling: touch;
}

/* 20. Loading States - Mobile Optimized */
@media (max-width: 768px) {
  .loading-spinner {
    width: 32px;
    height: 32px;
  }
  
  .skeleton-loader {
    min-height: 200px;
  }
}

