/**
 * Market Maps Page - Mobile Optimization
 * Bloomberg-grade finance UX for mobile devices
 * 
 * Extends maps.css with comprehensive mobile-first optimizations
 * Breakpoints: <768px mobile, 768-1023px tablet, ≥1024px desktop
 */

/* ============================================
   MOBILE BREAKPOINT (<768px)
   ============================================ */
@media (max-width: 767px) {
  
  /* --------------------------------------------
     MAIN CONTAINER - Mobile padding
     -------------------------------------------- */
  main.container:has(.treemap-wrapper) {
    padding-left: 12px !important;
    padding-right: 12px !important;
    padding-top: 12px !important;
    padding-bottom: 80px !important; /* Safe area for bottom nav */
  }
  
  /* --------------------------------------------
     PAGE HEADER - Compact mobile
     -------------------------------------------- */
  .dashboard-header {
    margin-bottom: 16px !important;
  }
  
  .dashboard-header .dashboard-title {
    font-size: 1.5rem !important;
    line-height: 1.2 !important;
  }
  
  .dashboard-header .dashboard-subtitle {
    font-size: 0.8125rem !important;
    margin-top: 4px !important;
  }
  
  /* Stale badge compact */
  .dashboard-header .stale-badge {
    font-size: 0.6875rem !important;
    padding: 4px 8px !important;
  }
  
  /* --------------------------------------------
     EXPLAIN PANEL - Already good, minor tweaks
     -------------------------------------------- */
  .explain-panel {
    margin-bottom: 12px !important;
  }
  
  /* --------------------------------------------
     ELITE FILTER BAR - Stack vertically
     -------------------------------------------- */
  .elite-filter-bar {
    margin-bottom: 12px !important;
  }
  
  .elite-filter-grid {
    display: flex !important;
    flex-direction: column !important;
    gap: 12px !important;
  }
  
  /* Each filter group full width */
  .elite-filter-group {
    width: 100% !important;
  }
  
  /* Search - prominent */
  .elite-filter-group:has(.elite-search-wrapper) {
    order: -1 !important;
  }
  
  .elite-search {
    height: 44px !important;
    font-size: 1rem !important;
  }
  
  /* Labels smaller */
  .elite-filter-label {
    font-size: 0.6875rem !important;
    margin-bottom: 4px !important;
  }
  
  /* Custom select compact */
  .custom-select-trigger {
    height: 44px !important;
    padding: 0 12px !important;
  }
  
  .custom-select-label {
    font-size: 0.875rem !important;
  }
  
  .custom-select-icon {
    width: 16px !important;
    height: 16px !important;
  }
  
  /* Dropdown menu full width */
  .custom-select-menu {
    min-width: 100% !important;
    left: 0 !important;
    right: 0 !important;
  }
  
  .custom-select-option {
    padding: 12px 16px !important;
    min-height: 44px !important;
  }
  
  /* Results display compact */
  .filter-results-display {
    padding: 10px 14px !important;
    flex-direction: column !important;
    align-items: center !important;
    gap: 4px !important;
  }
  
  .filter-results-count {
    font-size: 1.25rem !important;
  }
  
  .filter-results-label {
    font-size: 0.625rem !important;
  }
  
  /* --------------------------------------------
     MAPS CONTROL BAR - Horizontal scroll
     -------------------------------------------- */
  .maps-control-bar {
    padding: 12px !important;
    gap: 12px !important;
    border-radius: 12px !important;
    margin-bottom: 16px !important;
  }
  
  /* Time Range Selector - Horizontal scroll */
  .maps-range-selector {
    width: 100% !important;
    flex-direction: column !important;
    align-items: stretch !important;
    gap: 8px !important;
  }
  
  .maps-range-label {
    font-size: 0.625rem !important;
  }
  
  .segmented-control {
    overflow-x: auto !important;
    overflow-y: hidden !important;
    -webkit-overflow-scrolling: touch !important;
    scrollbar-width: none !important;
    padding: 3px !important;
    gap: 3px !important;
    scroll-snap-type: x mandatory !important;
  }
  
  .segmented-control::-webkit-scrollbar {
    display: none !important;
  }
  
  .segmented-btn {
    flex: 0 0 auto !important;
    min-width: 42px !important;
    padding: 8px 10px !important;
    font-size: 0.6875rem !important;
    scroll-snap-align: start !important;
  }
  
  .segmented-btn-compact {
    padding: 8px 10px !important;
    min-width: 40px !important;
  }
  
  /* Subset Selector - Full width */
  .maps-subset-selector {
    width: 100% !important;
    flex-direction: column !important;
    align-items: stretch !important;
    gap: 8px !important;
    margin-left: 0 !important;
  }
  
  .maps-subset-label {
    font-size: 0.625rem !important;
  }
  
  .subset-buttons {
    display: flex !important;
    gap: 6px !important;
    width: 100% !important;
  }
  
  .subset-btn {
    flex: 1 !important;
    justify-content: center !important;
    padding: 10px 8px !important;
    font-size: 0.6875rem !important;
    border-radius: 8px !important;
    min-height: 44px !important;
  }
  
  .subset-btn svg {
    width: 14px !important;
    height: 14px !important;
  }
  
  /* --------------------------------------------
     TREEMAP CONTAINER - Full width bleed
     -------------------------------------------- */
  .treemap-wrapper {
    margin: 0 -12px !important;
    width: calc(100% + 24px) !important;
    margin-bottom: 16px !important;
  }
  
  .treemap-sort-label {
    margin: 0 12px 8px !important;
    padding: 8px 12px !important;
    font-size: 0.6875rem !important;
    border-radius: 6px !important;
  }
  
  .treemap-sort-label svg {
    width: 14px !important;
    height: 14px !important;
  }
  
  .treemap-d3-container {
    min-height: 400px !important;
    border-radius: 0 !important;
    padding: 6px !important;
  }
  
  /* Treemap touch interactions */
  .treemap-svg {
    touch-action: pan-x pan-y pinch-zoom !important;
  }
  
  /* Skeleton loading - 2 columns mobile */
  .treemap-skeleton-grid {
    grid-template-columns: repeat(2, 1fr) !important;
    gap: 4px !important;
  }
  
  .skeleton-tile-lg {
    min-height: 140px !important;
  }
  
  .skeleton-tile-md,
  .skeleton-tile-sm {
    min-height: 80px !important;
  }
  
  /* Transition overlay */
  .treemap-transition-overlay {
    padding: 8px !important;
    border-radius: 0 !important;
  }
  
  /* --------------------------------------------
     D3 TOOLTIP - Mobile optimized
     -------------------------------------------- */
  .treemap-tooltip-d3 {
    min-width: 200px !important;
    max-width: calc(100vw - 32px) !important;
    padding: 14px !important;
    border-radius: 12px !important;
    /* Position at bottom of screen on mobile */
    position: fixed !important;
    bottom: 80px !important;
    left: 50% !important;
    top: auto !important;
    transform: translateX(-50%) !important;
    animation: tooltipSlideUpMobile 200ms ease !important;
  }
  
  @keyframes tooltipSlideUpMobile {
    from {
      opacity: 0;
      transform: translateX(-50%) translateY(20px);
    }
    to {
      opacity: 1;
      transform: translateX(-50%) translateY(0);
    }
  }
  
  .tooltip-header {
    gap: 10px !important;
    margin-bottom: 10px !important;
  }
  
  .tooltip-rank {
    width: 28px !important;
    height: 28px !important;
    font-size: 12px !important;
  }
  
  .tooltip-symbol {
    font-size: 18px !important;
  }
  
  .tooltip-name {
    font-size: 11px !important;
    margin-bottom: 12px !important;
  }
  
  .tooltip-perf {
    font-size: 22px !important;
    margin-bottom: 10px !important;
  }
  
  .tooltip-sign {
    font-size: 16px !important;
  }
  
  .tooltip-vs-avg {
    padding: 8px 10px !important;
    font-size: 11px !important;
    margin-bottom: 10px !important;
  }
  
  .tooltip-vs-avg .tooltip-label {
    font-size: 9px !important;
  }
  
  .tooltip-hint {
    font-size: 9px !important;
    padding-top: 8px !important;
  }
  
  /* --------------------------------------------
     STATS CARDS - 2 columns
     -------------------------------------------- */
  .maps-stats {
    margin: 0 -12px !important;
    width: calc(100% + 24px) !important;
    padding: 0 12px !important;
    grid-template-columns: repeat(2, 1fr) !important;
    gap: 8px !important;
    margin-top: 16px !important;
  }
  
  .stat-card {
    padding: 12px !important;
    border-radius: 10px !important;
    gap: 4px !important;
  }
  
  .stat-label {
    font-size: 0.5625rem !important;
  }
  
  .stat-value {
    font-size: 1.25rem !important;
  }
  
  /* --------------------------------------------
     LOADING STATE - Compact
     -------------------------------------------- */
  .maps-loading {
    min-height: 300px !important;
    gap: 16px !important;
  }
  
  .loading-spinner svg {
    width: 32px !important;
    height: 32px !important;
  }
  
  .loading-spinner p {
    font-size: 0.875rem !important;
  }
  
  /* --------------------------------------------
     EMPTY STATE - Compact
     -------------------------------------------- */
  .empty-state {
    min-height: 300px !important;
    padding: 24px !important;
  }
  
  .empty-state-icon {
    width: 48px !important;
    height: 48px !important;
  }
  
  .empty-state h3 {
    font-size: 1.125rem !important;
  }
  
  .empty-state p {
    font-size: 0.875rem !important;
  }
  
  .empty-state .btn-primary {
    padding: 10px 20px !important;
    font-size: 0.875rem !important;
    min-height: 44px !important;
  }
  
  /* --------------------------------------------
     INFO TOOLTIPS - Mobile friendly
     -------------------------------------------- */
  .info-tooltip {
    max-width: calc(100vw - 32px) !important;
    font-size: 0.75rem !important;
  }
  
  .info-tooltip-title {
    font-size: 0.8125rem !important;
  }
  
  .info-tooltip-description {
    font-size: 0.75rem !important;
  }
  
  /* Touch feedback */
  .segmented-btn:active,
  .subset-btn:active,
  .stat-card:active {
    transform: scale(0.97) !important;
    transition: transform 0.1s ease !important;
  }
  
}

/* ============================================
   TABLET BREAKPOINT (768px - 1023px)
   ============================================ */
@media (min-width: 768px) and (max-width: 1023px) {
  
  /* Elite Filter Bar - 3 columns */
  .elite-filter-grid {
    display: grid !important;
    grid-template-columns: repeat(3, 1fr) !important;
    gap: 12px !important;
  }
  
  /* Search spans 2 columns */
  .elite-filter-group:has(.elite-search-wrapper) {
    grid-column: span 2 !important;
  }
  
  /* Results display last */
  .elite-filter-group:has(.filter-results-display) {
    grid-column: span 1 !important;
  }
  
  /* Treemap - larger than mobile */
  .treemap-d3-container {
    min-height: 500px !important;
  }
  
  /* Stats - 4 columns */
  .maps-stats {
    grid-template-columns: repeat(4, 1fr) !important;
    gap: 12px !important;
  }
  
}

/* ============================================
   LIGHT MODE FIXES
   ============================================ */
:root:not(.dark) {
  
  @media (max-width: 767px) {
    
    /* Control bar background */
    .maps-control-bar {
      background: rgba(255, 255, 255, 0.9) !important;
      border-color: rgba(0, 0, 0, 0.1) !important;
    }
    
    /* Segmented control */
    .segmented-control {
      background: rgba(0, 0, 0, 0.03) !important;
      border-color: rgba(0, 0, 0, 0.1) !important;
    }
    
    .segmented-btn {
      color: var(--text-secondary) !important;
    }
    
    .segmented-btn.active {
      background: white !important;
      color: var(--accent-primary) !important;
      box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1) !important;
    }
    
    /* Subset buttons */
    .subset-btn {
      background: rgba(0, 0, 0, 0.03) !important;
      border-color: rgba(0, 0, 0, 0.1) !important;
      color: var(--text-secondary) !important;
    }
    
    /* Stat cards */
    .stat-card {
      background: white !important;
      border-color: rgba(0, 0, 0, 0.1) !important;
    }
    
    /* Treemap container */
    .treemap-d3-container {
      background: rgba(249, 250, 251, 0.8) !important;
    }
    
    /* Sort label */
    .treemap-sort-label {
      background: rgba(59, 130, 246, 0.08) !important;
      color: var(--text-primary) !important;
    }
    
    /* Filter results */
    .filter-results-display {
      background: rgba(16, 185, 129, 0.08) !important;
      border-color: rgba(16, 185, 129, 0.2) !important;
    }
    
  }
  
}

/* ============================================
   TREEMAP TILE TAP FEEDBACK
   ============================================ */
@media (max-width: 767px) {
  
  /* Visual feedback on tile tap */
  .treemap-tile:active rect {
    opacity: 0.8 !important;
    transition: opacity 0.1s ease !important;
  }
  
  /* Disable hover effects on touch devices */
  @media (hover: none) {
    .treemap-tile:hover {
      transform: none !important;
    }
    
    .treemap-tile rect {
      transition: opacity 0.15s ease !important;
    }
  }
  
}

/* ============================================
   SAFE AREA INSETS (iPhone notch/home indicator)
   ============================================ */
@supports (padding-bottom: env(safe-area-inset-bottom)) {
  @media (max-width: 767px) {
    main.container:has(.treemap-wrapper) {
      padding-bottom: calc(80px + env(safe-area-inset-bottom)) !important;
    }
    
    .treemap-tooltip-d3 {
      bottom: calc(80px + env(safe-area-inset-bottom)) !important;
    }
  }
}

/* ============================================
   ANIMATIONS - Reduced motion
   ============================================ */
@media (max-width: 767px) {
  @media (prefers-reduced-motion: reduce) {
    * {
      animation-duration: 0.01ms !important;
      transition-duration: 0.01ms !important;
    }
  }
}
