/* ========================
   RESPONSIVE LAYER - MOBILE FIRST DESIGN v2.0
   ======================== */

/* Mobile-first breakpoint structure với touch-optimized interactions */

/* === Phone Portrait (max-width: 480px) - Ultra Mobile === */
@media (max-width: 480px) {
  html {
    font-size: 13px; /* Base scale cho mobile */
  }

  body {
    touch-action: manipulation; /* Optimize touch gestures */
    user-select: none;
  }

  /* Touch-friendly tap targets (min 44x44px) */
  button, a, [role="button"] {
    min-height: 44px;
    min-width: 44px;
  }

  /* Search Bar: Optimized cho mobile thumb zone */
  .search-bar-container {
    padding: 8px 12px !important;
    touch-action: manipulation;
  }

  .search-bar-input {
    height: 44px !important; /* Touch-friendly target */
    padding: 0 12px;
    font-size: 15px;
    border-radius: 12px;
    -webkit-tap-highlight-color: rgba(16, 185, 129, 0.3);
  }

  .search-bar-input:focus {
    background: rgba(16, 185, 129, 0.15) !important;
  }

  /* Floating Menu: Mobile-first card layout */
  .floating-menu {
    left: 8px !important;
    right: auto !important;
    width: calc(100% - 16px) !important;
    max-height: 85vh !important;
    border-radius: 16px;
    overflow: hidden;
    box-shadow: 
      0 4px 24px rgba(0, 0, 0, 0.4),
      0 0 0 1px rgba(255, 255, 255, 0.1);
    padding: 16px;
  }

  .floating-menu::before {
    content: '';
    display: block;
    height: 4px;
    background: linear-gradient(90deg, #10b97e, #34d399);
    border-radius: 8px 8px 0 0;
  }

  .menu-header {
    margin-bottom: 12px !important;
    padding-bottom: 8px;
    border-bottom: 1px solid rgba(255, 255, 255, 0.15);
  }

  .menu-section-title {
    font-size: 13px !important;
    font-weight: 600;
    color: #fff;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    padding: 4px 8px;
    background: rgba(255, 255, 255, 0.08);
    border-radius: 8px;
    margin-bottom: 12px !important;
  }

  /* Card-based layer items cho mobile */
  .layer-item {
    background: rgba(30, 30, 30, 0.95);
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: 12px;
    padding: 8px;
    margin-bottom: 8px;
    touch-action: manipulation;
  }

  .layer-item.active {
    background: rgba(16, 185, 129, 0.2);
    border-color: #10b97e;
  }

  .layer-toggle-btn {
    font-size: 11px !important;
    padding: 8px 12px !important; /* Touch-friendly */
    min-height: 44px !important;
    background: rgba(255, 255, 255, 0.1);
    border-radius: 8px;
    border: none;
    color: #fff;
    font-weight: 500;
    transition: all 0.2s ease;
  }

  .layer-toggle-btn:active {
    transform: scale(0.96);
    background: rgba(16, 185, 129, 0.3);
  }

  /* Base Layers Grid: Single column với card styling */
  .base-layers-grid {
    grid-template-columns: 1fr !important;
    gap: 8px;
  }

  .layer-item-label {
    font-size: 12px !important;
    max-width: 100%;
    font-weight: 500;
    color: #e4e4e7;
    display: flex;
    align-items: center;
    gap: 6px;
  }

  /* Overlay Layers List: Card-based với touch feedback */
  .overlay-layers-list {
    overflow-x: auto !important;
    flex-wrap: nowrap !important;
    gap: 8px;
    padding: 4px;
  }

  .overlay-layer-item {
    background: rgba(30, 30, 30, 0.9);
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: 10px;
    min-width: 80px;
    padding: 6px 8px;
    text-align: center;
    cursor: pointer;
    transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
    touch-action: manipulation;
  }

  .overlay-layer-item:active {
    transform: scale(0.95);
    background: rgba(16, 185, 129, 0.25);
    border-color: #10b97e;
  }

  /* Kèo Thơm Modal: Mobile-first vertical layout */
  #scan-modal.active .menu-content {
    flex-direction: column !important;
    gap: 16px !important;
  }

  #scan-modal.active .slider-container {
    flex-direction: column !important;
    gap: 12px;
    width: 100% !important;
  }

  #scan-modal.active .slider-row {
    flex-direction: row !important;
    width: 100% !important;
    gap: 12px;
  }

  #scan-modal.active .premium-slider {
    width: 100% !important;
    height: 44px !important;
  }

  /* Account Modal: Full screen card cho mobile */
  .account-modal.full-screen .menu-content {
    padding: 16px;
    max-height: 90vh;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }

  #auth-section-title,
  #user-profile-section-title,
  #weather-section-title,
  #favorites-section-title {
    font-size: 14px !important;
    font-weight: 600;
    color: #fff;
    padding: 8px 12px;
    margin-bottom: 12px;
    background: rgba(255, 255, 255, 0.08);
    border-radius: 8px;
  }

  .account-card {
    font-size: 13px !important;
    padding: 12px;
    margin-bottom: 12px;
    background: rgba(30, 30, 30, 0.95);
    border-radius: 12px;
    border: 1px solid rgba(255, 255, 255, 0.1);
  }

  .auth-provider-grid {
    display: grid !important;
    grid-template-columns: repeat(auto-fit, minmax(80px, 1fr));
    gap: 8px;
    margin-bottom: 16px;
  }

  .auth-provider-grid button {
    padding: 12px !important; /* Touch-friendly */
    font-size: 12px !important;
    min-height: 44px !important;
    background: rgba(30, 30, 30, 0.95);
    border: 1px solid rgba(255, 255, 255, 0.15);
    border-radius: 10px;
    font-weight: 600;
  }

  /* Layer Menu: Card-based mobile layout */
  #layer-menu.active .menu-content {
    flex-direction: column !important;
    gap: 8px !important;
  }

  #layer-menu.active .base-layers-grid {
    grid-template-columns: 1fr !important;
  }

  /* Bottom Panel & Side Panel: Optimized cho mobile thumb */
  #bottom-panel {
    max-width: 96vw !important;
    margin-left: 2%;
    max-height: 75vh !important;
    border-radius: 16px 16px 0 0;
    box-shadow: 0 -4px 24px rgba(0, 0, 0, 0.3);
  }

  #bottom-panel::before {
    content: '';
    display: block;
    height: 4px;
    background: linear-gradient(90deg, #10b97e, #34d399);
    border-radius: 8px 8px 0 0;
  }

  #side-panel {
    max-width: 96vw !important;
    margin-right: 2%;
    min-height: 70vh !important;
    border-radius: 16px;
    box-shadow: 0 4px 24px rgba(0, 0, 0, 0.3);
  }

  .bp-content,
  .sp-content {
    overflow-y: auto !important;
    scrollbar-width: thin;
    scrollbar-color: rgba(255, 255, 255, 0.2) transparent;
    font-size: 13px;
    padding-right: 4px;
  }

  /* Ephemeris Container: Full width cho mobile */
  #ephemeris-container {
    width: 98vw !important;
    max-width: none !important;
    margin-left: auto !important;
    margin-right: auto !important;
    padding: 0 4px;
  }

  /* Timeline: Optimized cho mobile */
  #sp-timeline-scroll {
    font-size: 11px;
    touch-action: pan-x;
  }

  .timeline-graph-v3 {
    width: 180px !important;
    height: 40px !important;
  }

  .timeline-label {
    font-size: 10px !important;
    text-transform: uppercase;
    letter-spacing: 0.5px;
  }

  .time-tick {
    width: 8px !important;
    height: 4px !important;
  }

  /* Scrollable Timeline: Touch-optimized snap */
  #sp-timeline-scroll {
    display: flex;
    align-items: center;
    justify-content: flex-start;
    overflow-x: auto;
    overflow-y: hidden;
    -webkit-overflow-scrolling: touch;
    scroll-snap-type: x mandatory;
    gap: 6px;
  }

  .scroll-snap-container {
    display: flex;
    scroll-snap-stop: always;
    gap: 8px;
  }

  /* Welcome Modal: Mobile-optimized */
  .welcome-modal-content p strong {
    font-size: 13px;
  }

  /* Card-based result layout cho mobile */
  .result-grid {
    display: grid !important;
    grid-template-columns: 1fr;
    gap: 12px;
    margin-bottom: 8px;
  }

  .result-card {
    background: rgba(30, 30, 30, 0.95);
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: 12px;
    padding: 12px;
    display: flex;
    flex-direction: column;
    gap: 8px;
  }

  .result-card-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }

  /* Table cho mobile */
  table {
    display: block !important;
    width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    border-radius: 12px;
    background: rgba(30, 30, 30, 0.95);
    border: 1px solid rgba(255, 255, 255, 0.1);
  }

  /* Chart optimization cho mobile */
  .chart-container {
    max-width: 85vw !important;
    height: 300px !important;
  }

  /* Scrollbar styling cho mobile */
  ::-webkit-scrollbar {
    width: 6px;
    height: 6px;
  }

  ::-webkit-scrollbar-track {
    background: rgba(255, 255, 255, 0.05);
    border-radius: 3px;
  }

  ::-webkit-scrollbar-thumb {
    background: rgba(255, 255, 255, 0.15);
    border-radius: 3px;
  }

  ::-webkit-scrollbar-thumb:hover {
    background: rgba(255, 255, 255, 0.25);
  }

  /* Hide non-essential elements cho mobile */
  .desktop-only {
    display: none !important;
  }
}

/* Small: Phone landscape (481px - 767px) */
@media (min-width: 481px) and (max-width: 767px) {
  html {
    font-size: 14px;
  }

  .search-bar-container {
    padding: 10px 14px !important;
  }

  .search-bar-input {
    height: 48px !important;
    padding: 0 16px;
  }

  .floating-menu {
    left: 12px;
    right: auto;
    width: calc(100% - 24px);
    max-height: 90vh;
  }

  /* Grid cho landscape */
  .base-layers-grid {
    grid-template-columns: repeat(2, 1fr) !important;
    gap: 8px;
  }

  #scan-modal.active .slider-row {
    flex-direction: row;
    gap: 10px;
  }
}

/* Medium: Tablet portrait / Small laptop (768px - 1023px) */
@media (min-width: 768px) and (max-width: 1023px) {
  html {
    font-size: 15px;
  }

  .search-bar-container {
    padding: 11px 16px !important;
  }

  .search-bar-input {
    height: 48px !important;
  }

  .floating-menu {
    left: 16px;
    right: auto;
    width: calc(100% - 32px);
    max-height: 90vh;
    border-radius: 16px;
  }

  /* Tablet card layouts */
  .base-layers-grid {
    grid-template-columns: repeat(2, 1fr) !important;
    gap: 10px;
  }

  .result-grid {
    grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
    gap: 16px;
  }
}

/* Large: Laptop / Desktop (1024px+) */
@media (min-width: 1024px) {
  html {
    font-size: 16px;
  }

  .search-bar-container {
    padding: 12px 20px !important;
  }

  .search-bar-input {
    height: 50px !important;
  }

  .floating-menu {
    left: 20px;
    right: auto;
    width: calc(100% - 40px);
    max-height: 95vh;
  }

  /* Desktop grid layouts */
  .base-layers-grid {
    grid-template-columns: repeat(auto-fill, minmax(120px, 1fr)) !important;
    gap: 12px;
  }

  .result-grid {
    grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
    gap: 20px;
  }

  /* Enhanced hover effects cho desktop */
  .layer-toggle-btn:hover {
    background: rgba(255, 255, 255, 0.15);
    transform: translateY(-2px);
  }

  .overlay-layer-item:hover {
    background: rgba(16, 185, 129, 0.2);
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
  }

  .result-card:hover {
    border-color: rgba(16, 185, 129, 0.5);
    transform: translateY(-4px);
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.3);
  }

  /* Chart optimization cho desktop */
  .chart-container {
    max-width: 100%;
    height: auto;
  }
}

/* Mobile-specific touch feedback */
@media (hover: none) and (pointer: coarse) {
  button, a, [role="button"] {
    transition: none !important;
    -webkit-tap-highlight-color: rgba(16, 185, 129, 0.4);
  }

  .layer-toggle-btn,
  .overlay-layer-item {
    background: rgba(255, 255, 255, 0.15) !important;
    border: none !important;
  }

  .layer-toggle-btn:active,
  .overlay-layer-item:active {
    background: rgba(16, 185, 129, 0.3) !important;
    transform: scale(0.97);
  }

  /* Disable hover effects cho touch devices */
  .hover-only {
    display: none !important;
  }
}

/* Table & chart adjustments */
table {
  font-size: inherit;
}

@media (max-width: 767px) {
  .result-grid {
    grid-template-columns: 1fr !important;
  }

  table {
    display: block;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }

  .chart-container {
    max-width: 85vw !important;
  }
}

/* Small: Phone landscape (380px - 599px) */
@media (min-width: 380px) and (max-width: 599px) {
  html {
    font-size: 13px;
  }

  .search-bar-container {
    padding: 7px 9px;
  }

  /* Floating Menu: Wider cho landscape */
  #layer-menu.active .menu-content {
    flex-direction: row !important;
  }

  .floating-menu {
    left: 10px;
    right: auto;
    width: calc(100% - 20px);
  }
}

/* Medium: Tablet portrait / Small laptop (600px - 959px) */
@media (min-width: 600px) and (max-width: 959px) {
  html {
    font-size: 14px;
  }

  .search-bar-container {
    padding: 8px 12px;
  }

  .search-bar-input {
    height: 36px !important;
  }

  .floating-menu {
    left: 12px;
    right: auto;
    width: calc(100% - 24px);
  }
}

/* Large: Laptop / Small desktop (960px+) */
@media (min-width: 960px) {
  html {
    font-size: 15px;
  }

  .search-bar-container {
    padding: 9px 14px;
  }

  .floating-menu {
    left: 14px;
    right: auto;
    width: calc(100% - 28px);
    max-height: 75vh;
  }
}

/* Table & chart adjustments (retroactively applied) */
table {
  font-size: inherit;
}

@media (max-width: 767px) {
  .result-grid {
    grid-template-columns: 1fr !important;
  }

  table {
    display: block;
    overflow-x: auto;
  }

  .chart-container {
    max-width: 45vw !important;
  }
}

/* === Dark mode adjustments (optional) === */
@media (prefers-color-scheme: light) and (prefers-contrast: more) {
  html.light-theme {
    background: #ffffff;
    color: #1a1a1a;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}