/**
 * ===============================================================================================
 * LOFI SWAP MODAL - COMPREHENSIVE RESPONSIVE FIXES
 * ===============================================================================================
 * 
 * This file ensures the swap modal is perfectly sized and functional on ALL devices.
 * Addresses:
 * - Modal overflow issues
 * - Proper viewport constraints
 * - Safe area handling for notched devices
 * - Virtual keyboard handling
 * - Landscape orientation support
 * - Touch-friendly interactions
 * - Smooth scrolling behavior
 * 
 * ===============================================================================================
 */

/* ===============================================================================================
 * CSS CUSTOM PROPERTIES FOR RESPONSIVE BEHAVIOR
 * ===============================================================================================
 */

:root {
  /* Safe area insets for modern devices */
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  
  /* Dynamic viewport height (set by JS) */
  --vh: 1vh;
  
  /* Modal spacing */
  --modal-padding: 20px;
  --modal-max-width: 480px;
  --modal-border-radius: 24px;
  
  /* Touch targets */
  --min-touch-target: 44px;
  
  /* Virtual keyboard offset */
  --keyboard-offset: 0px;
}

/* ===============================================================================================
 * VIEWPORT HEIGHT FIX
 * ===============================================================================================
 */

/* Use custom viewport height property for accurate sizing */
.lofi-swap-modal-container {
  height: calc(var(--vh, 1vh) * 100);
}

/* ===============================================================================================
 * MODAL OVERLAY IMPROVEMENTS
 * ===============================================================================================
 */

#lofi-swap-modal {
  /* Ensure full viewport coverage */
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  width: 100vw;
  height: 100vh;
  height: calc(var(--vh, 1vh) * 100);
  
  /* Safe area padding */
  padding: var(--safe-top) var(--safe-right) var(--safe-bottom) var(--safe-left);
  
  /* Flexbox centering with constraints */
  display: flex;
  align-items: center;
  justify-content: center;
  
  /* Prevent body scroll when modal is open */
  overscroll-behavior: contain;
}

/* Body scroll lock when modal is open */
body.modal-open {
  overflow: hidden;
  position: fixed;
  width: 100%;
  height: 100%;
  touch-action: none;
}

/* ===============================================================================================
 * MODAL CONTAINER CONSTRAINTS
 * ===============================================================================================
 */

.lofi-swap-container {
  /* Size constraints */
  max-width: var(--modal-max-width);
  width: calc(100% - 2 * var(--modal-padding));
  max-height: calc(100vh - 2 * var(--modal-padding) - var(--safe-top) - var(--safe-bottom));
  max-height: calc(var(--vh, 1vh) * 100 - 2 * var(--modal-padding) - var(--safe-top) - var(--safe-bottom));
  
  /* Flexible height */
  height: auto;
  
  /* Internal layout */
  display: flex;
  flex-direction: column;
  
  /* Prevent overflow */
  overflow: hidden;
  
  /* Maintain aspect ratio on very small screens */
  min-height: 400px;
}

/* Modal header - fixed at top */
.lofi-modal-header {
  flex-shrink: 0;
  position: sticky;
  top: 0;
  z-index: 10;
  background: inherit;
  border-radius: var(--modal-border-radius) var(--modal-border-radius) 0 0;
}

/* Modal content - scrollable area */
.lofi-swap-content {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  overscroll-behavior: contain;
  
  /* Smooth scrolling */
  scroll-behavior: smooth;
  -webkit-overflow-scrolling: touch;
  
  /* Scroll padding for sticky elements */
  scroll-padding-bottom: 80px;
  
  /* Prevent horizontal scroll */
  max-width: 100%;
}

/* Custom scrollbar styling */
.lofi-swap-content::-webkit-scrollbar {
  width: 6px;
}

.lofi-swap-content::-webkit-scrollbar-track {
  background: rgba(42, 45, 94, 0.3);
  border-radius: 3px;
}

.lofi-swap-content::-webkit-scrollbar-thumb {
  background: rgba(138, 138, 237, 0.4);
  border-radius: 3px;
}

.lofi-swap-content::-webkit-scrollbar-thumb:hover {
  background: rgba(138, 138, 237, 0.6);
}

/* ===============================================================================================
 * MOBILE SPECIFIC (PHONES)
 * ===============================================================================================
 */

@media (max-width: 767px) {
  :root {
    --modal-padding: 0px;
  }
  
  #lofi-swap-modal {
    padding: 0;
    align-items: flex-end;
  }
  
  .lofi-swap-container {
    /* Full width bottom sheet */
    max-width: 100%;
    width: 100%;
    max-height: calc(100vh - var(--safe-top));
    max-height: calc(var(--vh, 1vh) * 100 - var(--safe-top));
    
    /* Bottom sheet styling */
    border-radius: var(--modal-border-radius) var(--modal-border-radius) 0 0;
    
    /* Handle virtual keyboard */
    margin-bottom: var(--keyboard-offset);
    
    /* Ensure minimum height for usability */
    min-height: 50vh;
    min-height: calc(var(--vh, 1vh) * 50);
  }
  
  /* Modal header mobile adjustments */
  .lofi-modal-header {
    padding-top: max(16px, var(--safe-top));
    
    /* Visual drag indicator */
    &::before {
      content: '';
      position: absolute;
      top: 8px;
      left: 50%;
      transform: translateX(-50%);
      width: 40px;
      height: 4px;
      background: rgba(138, 138, 237, 0.3);
      border-radius: 2px;
    }
  }
  
  /* Content padding with safe areas */
  .lofi-swap-content {
    padding-left: max(16px, var(--safe-left));
    padding-right: max(16px, var(--safe-right));
    padding-bottom: max(16px, var(--safe-bottom));
  }
  
  /* Larger touch targets */
  .lofi-swap-button,
  .lofi-max-button,
  .lofi-slippage-preset,
  .lofi-token-selector {
    min-height: var(--min-touch-target);
  }
  
  /* Prevent input zoom on iOS */
  input[type="number"],
  input[type="text"] {
    font-size: 16px !important;
  }
  
  /* Sticky swap button */
  .lofi-swap-button {
    position: sticky;
    bottom: var(--safe-bottom);
    margin-top: auto;
    z-index: 5;
  }
}

/* ===============================================================================================
 * SMALL PHONES (iPhone SE, Galaxy S5, etc.)
 * ===============================================================================================
 */

@media (max-width: 375px) {
  .lofi-swap-container {
    min-height: 45vh;
    min-height: calc(var(--vh, 1vh) * 45);
  }
  
  /* More compact spacing */
  .lofi-swap-token-section {
    padding: 14px;
  }
  
  .lofi-swap-input {
    font-size: 22px;
  }
  
  .lofi-modal-header h2 {
    font-size: 16px;
  }
  
  /* Smaller details section */
  .lofi-swap-details {
    font-size: 12px;
    padding: 10px;
  }
}

/* ===============================================================================================
 * TABLETS (iPad, Surface, etc.)
 * ===============================================================================================
 */

@media (min-width: 768px) and (max-width: 1024px) {
  :root {
    --modal-max-width: 540px;
  }
  
  .lofi-swap-container {
    max-height: 80vh;
    max-height: calc(var(--vh, 1vh) * 80);
  }
  
  /* Slightly larger elements for tablets */
  .lofi-swap-button {
    padding: 20px;
    font-size: 19px;
  }
  
  .lofi-swap-input {
    font-size: 30px;
  }
}

/* ===============================================================================================
 * LANDSCAPE ORIENTATION
 * ===============================================================================================
 */

@media (orientation: landscape) and (max-height: 600px) {
  .lofi-swap-container {
    max-height: calc(100vh - 40px);
    max-height: calc(var(--vh, 1vh) * 100 - 40px);
    min-height: auto;
  }
  
  /* More compact header */
  .lofi-modal-header {
    padding: 12px 16px;
  }
  
  .lofi-modal-header h2 {
    font-size: 16px;
  }
  
  /* Reduce spacing */
  .lofi-swap-content {
    padding: 12px;
  }
  
  .lofi-swap-token-section {
    padding: 14px;
    margin-bottom: 8px;
  }
  
  /* Hide recent transactions in landscape */
  .lofi-recent-section {
    display: none;
  }
  
  /* Smaller inputs */
  .lofi-swap-input {
    font-size: 24px;
  }
}

/* Two-column layout for landscape tablets/phones */
@media (orientation: landscape) and (min-width: 768px) and (max-height: 600px) {
  .lofi-swap-content {
    display: grid;
    grid-template-columns: 1fr auto 1fr;
    gap: 16px;
    align-items: start;
  }
  
  .lofi-swap-direction-indicator {
    grid-column: 2;
    align-self: center;
  }
  
  .lofi-swap-details,
  .lofi-swap-button {
    grid-column: 1 / -1;
  }
}

/* ===============================================================================================
 * DESKTOP IMPROVEMENTS
 * ===============================================================================================
 */

@media (min-width: 1025px) {
  .lofi-swap-container {
    /* Better desktop proportions */
    max-height: 85vh;
    max-height: calc(var(--vh, 1vh) * 85);
    
    /* Add some breathing room */
    margin: 20px;
  }
  
  /* Hover effects only on desktop */
  @media (hover: hover) {
    .lofi-swap-token-section {
      transition: transform 0.2s ease, box-shadow 0.2s ease;
    }
    
    .lofi-swap-token-section:hover {
      transform: translateY(-1px);
    }
  }
}

/* ===============================================================================================
 * VIRTUAL KEYBOARD HANDLING
 * ===============================================================================================
 */

/* When input is focused (keyboard likely visible) */
.lofi-swap-input:focus,
.lofi-slippage-input:focus {
  /* Scroll input into view */
  scroll-margin-bottom: 100px;
}

/* iOS specific keyboard handling */
@supports (-webkit-touch-callout: none) {
  .lofi-swap-container {
    /* Account for iOS keyboard */
    padding-bottom: env(keyboard-inset-height, 0);
  }
}

/* ===============================================================================================
 * TRANSACTION SLIDER RESPONSIVE FIXES
 * ===============================================================================================
 */

.lofi-transaction-slider-wrapper {
  max-width: 100%;
  margin: 0 -4px; /* Compensate for slider padding */
}

.lofi-transaction-slider {
  /* Ensure it doesn't cause horizontal scroll */
  max-width: calc(100% + 8px);
}

@media (max-width: 767px) {
  .lofi-transaction-slider .lofi-transaction-item {
    width: min(240px, calc(100vw - 80px));
    min-width: min(240px, calc(100vw - 80px));
  }
}

/* ===============================================================================================
 * ACCESSIBILITY IMPROVEMENTS
 * ===============================================================================================
 */

/* Focus visible for keyboard navigation */
.lofi-swap-button:focus-visible,
.lofi-modal-close:focus-visible,
.lofi-max-button:focus-visible,
.lofi-slippage-preset:focus-visible {
  outline: 3px solid rgba(79, 118, 255, 0.8);
  outline-offset: 2px;
}

/* High contrast mode */
@media (prefers-contrast: high) {
  .lofi-swap-container {
    border: 3px solid white;
  }
  
  .lofi-swap-token-section {
    border: 2px solid white;
  }
}

/* Reduced motion */
@media (prefers-reduced-motion: reduce) {
  .lofi-swap-container,
  .lofi-swap-button,
  .lofi-transaction-slider {
    animation: none;
    transition: none;
  }
}

/* ===============================================================================================
 * PRINT STYLES
 * ===============================================================================================
 */

@media print {
  #lofi-swap-modal {
    display: none !important;
  }
}

/* ===============================================================================================
 * PERFORMANCE OPTIMIZATIONS
 * ===============================================================================================
 */

/* Use GPU acceleration for smooth animations */
.lofi-swap-container {
  will-change: transform;
  transform: translateZ(0);
  backface-visibility: hidden;
}

/* Optimize repaints during scrolling */
.lofi-swap-content {
  will-change: scroll-position;
}

/* Reduce paint areas */
.lofi-swap-input,
.lofi-slippage-input {
  will-change: auto;
}

/* ===============================================================================================
 * FALLBACKS FOR OLDER BROWSERS
 * ===============================================================================================
 */

/* Fallback for browsers without CSS env() support */
@supports not (padding: env(safe-area-inset-top)) {
  :root {
    --safe-top: 0px;
    --safe-bottom: 0px;
    --safe-left: 0px;
    --safe-right: 0px;
  }
}

/* Fallback for browsers without gap support in flexbox */
@supports not (gap: 1px) {
  .lofi-swap-input-group > * + * {
    margin-left: 16px;
  }
  
  .lofi-balance-display > * + * {
    margin-left: 8px;
  }
}

/* ===============================================================================================
 * EDGE CASE FIXES
 * ===============================================================================================
 */

/* Fix for iOS Safari bounce scroll */
@supports (-webkit-touch-callout: none) {
  #lofi-swap-modal {
    position: fixed;
    overflow: hidden;
  }
  
  .lofi-swap-content {
    -webkit-overflow-scrolling: touch;
    overflow-y: scroll;
  }
}

/* Fix for Android Chrome address bar */
@media (max-width: 767px) {
  .lofi-swap-container {
    max-height: calc(100vh - 56px); /* Account for address bar */
    max-height: calc(var(--vh, 1vh) * 100 - 56px);
  }
}

/* Fix for notched devices in landscape */
@media (orientation: landscape) and (max-width: 896px) {
  .lofi-swap-container {
    padding-left: max(16px, var(--safe-left));
    padding-right: max(16px, var(--safe-right));
  }
}