/* ============================================================
   KTMY — Responsive Breakpoints  (v3.2 — Full Mobile Fix)
   ============================================================ */

/* ============================================================
   GLOBAL OVERFLOW GUARD
   Prevents any element from ever creating horizontal scroll
   ============================================================ */
*, *::before, *::after {
  box-sizing: border-box;
}

html, body {
  overflow-x: hidden;
  max-width: 100vw;
}

/* Ensure the app layout never widens beyond the viewport */
.app-layout,
.main-content,
.content-area {
  max-width: 100%;
  overflow-x: hidden;
}

/* ============================================================
   ALL TABLES GET AN OVERFLOW WRAPPER
   ============================================================ */
.data-table,
table {
  display: table; /* keep default table display */
  min-width: 0;
}

/* Any table or wide grid in a glass-card gets overflow protection */
.glass-card .data-table,
.glass-card table {
  width: 100%;
}

/* ============================================================
   TABLET (max-width: 1024px) — sidebar collapses
   ============================================================ */
@media (max-width: 1024px) {
  /* Ensure .tabs strips never overflow their container */
  .tabs {
    max-width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    width: 100% !important;
    border-radius: var(--radius-full);
  }
  .tabs::-webkit-scrollbar { display: none; }

  /* Topbar search shouldn't overflow */
  .topbar-search {
    width: 200px;
  }
}

/* ---------- Tablet (768px) ---------- */
@media (max-width: 768px) {
  .grid-2 { grid-template-columns: 1fr; }
  .grid-3 { grid-template-columns: repeat(2, 1fr); }
  .grid-4 { grid-template-columns: repeat(2, 1fr); }

  .nav-links { display: none; }

  .mobile-menu-btn { display: flex; }

  .nav-links.mobile-open {
    display: flex;
    flex-direction: column;
    position: fixed;
    top: 60px;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(10, 14, 39, 0.97);
    backdrop-filter: blur(20px);
    padding: var(--space-xl);
    gap: var(--space-sm);
    overflow-y: auto;
    max-width: 100%;
    animation: fadeIn var(--duration-fast) var(--ease-out);
    z-index: var(--z-overlay);
  }

  .nav-links.mobile-open .nav-link {
    width: 100%;
    padding: var(--space-md) var(--space-lg);
    font-size: var(--fs-body);
    border-radius: var(--radius-md);
  }

  .hero-title { letter-spacing: -1px; }

  .hero-stats { gap: var(--space-xl); }

  .hero-stat .stat-number { font-size: var(--fs-h3); }

  .section-header { margin-bottom: var(--space-xl); }

  .footer-grid {
    grid-template-columns: 1fr 1fr;
    gap: var(--space-xl);
  }

  .footer-brand { grid-column: 1 / -1; }

  .footer-bottom {
    flex-direction: column;
    gap: var(--space-md);
    text-align: center;
  }

  .glass-card {
    padding: var(--space-lg);
    /* Allow inner content to scroll if needed instead of being clipped */
    overflow: visible;
  }

  .chart-container {
    padding: var(--space-lg);
    min-height: 250px;
  }

  .tabs {
    width: 100% !important;
    max-width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    border-radius: var(--radius-full);
  }
  .tabs::-webkit-scrollbar { display: none; }

  .tab-btn {
    flex-shrink: 0;
    white-space: nowrap;
  }

  .filter-bar {
    overflow-x: auto;
    flex-wrap: nowrap;
    padding-bottom: var(--space-sm);
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
  }
  .filter-bar::-webkit-scrollbar { display: none; }

  .filter-chip { flex-shrink: 0; }

  /* Prevent flex-between from pushing tags off-screen in incentives */
  .flex-between {
    flex-wrap: wrap !important;
    gap: 8px !important;
  }

  /* Override any fixed-width assignments on glass-card children */
  .glass-card > * {
    max-width: 100%;
  }

  /* Ensure all table wrappers scroll horizontally */
  .table-wrapper,
  [style*="overflow-x: auto"],
  .glass-card > div[style*="overflow"] {
    max-width: 100%;
  }

  /* Government / Tariff tab headers */
  #tab-government .flex-between,
  #tab-tariffs .flex-between {
    flex-direction: column !important;
    align-items: flex-start !important;
    gap: 8px !important;
  }
  #tab-government .flex-between .tag,
  #tab-tariffs .flex-between .tag {
    align-self: flex-start !important;
  }

  /* ---- FEEDBACK FAB ---- */
  /* Move it above the mobile bottom nav (60px) + safe margin */
  .feedback-fab {
    bottom: 76px !important;
    right: 14px !important;
    width: 46px !important;
    height: 46px !important;
    font-size: 1.2rem !important;
    z-index: 210 !important;
  }
  .feedback-card {
    bottom: 132px !important;
    right: 12px !important;
    width: calc(100vw - 24px) !important;
    max-width: 300px !important;
    z-index: 210 !important;
  }

  /* ---- WEATHER PAGE ---- */
  .weather-current-hero {
    padding: 1rem !important;
  }

  .weather-main-info {
    flex-direction: column !important;
    align-items: flex-start !important;
    gap: var(--space-md) !important;
  }

  .weather-emoji-large {
    font-size: 3.5rem !important;
  }

  .weather-temp-main {
    font-size: 3rem !important;
  }

  /* Force 2-column gauge layout */
  .weather-gauges-row {
    grid-template-columns: repeat(2, 1fr) !important;
    gap: 8px !important;
  }

  /* Weather map */
  .weather-map-iframe-container {
    height: 300px !important;
    width: 100% !important;
    max-width: 100% !important;
    overflow: hidden !important;
  }

  /* Make map layer button bar scrollable */
  .map-layer-selector {
    overflow-x: auto !important;
    -webkit-overflow-scrolling: touch !important;
    scrollbar-width: none !important;
    flex-wrap: nowrap !important;
  }
  .map-layer-selector::-webkit-scrollbar { display: none !important; }
  .map-layer-btn {
    flex-shrink: 0 !important;
  }

  /* 7-day forecast: horizontal scroll carousel */
  .forecast-days-container {
    display: flex !important;
    overflow-x: auto !important;
    scroll-snap-type: x mandatory !important;
    scrollbar-width: none !important;
    gap: 8px !important;
    padding-bottom: 8px !important;
    grid-template-columns: unset !important;
  }
  .forecast-days-container::-webkit-scrollbar { display: none !important; }
  .forecast-day-card {
    flex: 0 0 110px !important;
    scroll-snap-align: start !important;
    padding: 10px 8px !important;
  }

  /* City weather grid: 2-column minimum */
  .city-weather-grid {
    grid-template-columns: repeat(2, 1fr) !important;
  }

  /* City select dropdown: full width on mobile */
  .city-select {
    min-width: unset !important;
    width: 100% !important;
    max-width: 100% !important;
  }

  /* Weather controls glass card: stack vertically */
  #section-weather-content .glass-card[style*="display: flex"] {
    flex-direction: column !important;
    align-items: flex-start !important;
  }
  #section-weather-content .glass-card[style*="display: flex"] > * {
    width: 100% !important;
  }
}

/* ---------- Small Tablet / Large Phone (640px) ---------- */
@media (max-width: 640px) {
  .grid-3 { grid-template-columns: 1fr; }
  .grid-auto {
    grid-template-columns: 1fr;
  }

  .hero {
    padding: var(--space-3xl) var(--container-padding);
  }

  .hero-stats {
    flex-direction: column;
    gap: var(--space-lg);
  }

  .price-tag { padding: var(--space-lg); }

  .currency-card { flex-wrap: wrap; }

  .currency-card .currency-sparkline {
    width: 100%;
    order: 4;
  }

  /* Hide sidebar by default, use overlay */
  .sidebar {
    transform: translateX(-100%);
    transition: transform 0.3s ease;
    position: fixed;
    z-index: 210; /* Above mobile header (200) and overlay (99) */
  }

  .sidebar.mobile-open {
    transform: translateX(0);
  }

  /* Main content takes full width and keeps clearance for header/bottom-nav */
  .main-content {
    margin-left: 0 !important;
    padding-top: 56px !important;
    padding-bottom: 64px !important;
  }
}

/* ---------- Phone (480px) ---------- */
@media (max-width: 480px) {
  :root {
    --container-padding: 1rem;
    --space-lg: 1.25rem;
    --space-md: 0.875rem;
    --space-sm: 0.5rem;
    --fs-h1: 1.55rem;
    --fs-h2: 1.3rem;
    --fs-h3: 1.1rem;
    --fs-h4: 0.95rem;
    --fs-body: 0.88rem;
    --fs-small: 0.78rem;
    --fs-xs: 0.72rem;
  }

  .grid-4 { grid-template-columns: 1fr; }

  .section { padding: var(--space-2xl) 0; }

  .nav-inner {
    height: 52px;
    padding: 0 var(--space-md);
  }

  .nav-logo { font-size: 1rem; }

  .glass-card {
    padding: 1.25rem 1rem;
    border-radius: 14px;
    margin-bottom: 1rem;
    /* Allow content to be visible instead of clipping */
    overflow: visible;
  }

  .weather-card { padding: var(--space-md); }

  .stat-number { font-size: var(--fs-h3); }

  .info-card {
    flex-direction: column;
    text-align: center;
  }

  .info-card .info-icon {
    align-self: center;
    font-size: 1.5rem;
  }

  .data-table { font-size: var(--fs-xs); }

  .data-table thead th,
  .data-table tbody td {
    padding: 0.25rem 0.35rem;
  }

  .footer-grid { grid-template-columns: 1fr; }

  .back-to-top {
    width: 36px;
    height: 36px;
    bottom: 80px;
    right: var(--space-sm);
    font-size: 0.85rem;
  }

  /* Make tabs scrollable horizontally */
  .tabs {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    flex-wrap: nowrap;
    gap: 0.25rem;
    padding-bottom: 0.5rem;
    width: 100% !important;
    max-width: 100% !important;
  }

  .tab-btn {
    flex-shrink: 0;
    padding: 0.4rem 0.7rem;
    font-size: 0.78rem;
    white-space: nowrap;
  }

  /* Make tables scroll horizontally */
  .table-wrapper {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }

  /* Mobile bottom nav compact */
  .mobile-bottom-nav {
    height: 58px;
    padding: 0 0.25rem;
  }

  .mobile-bottom-item .bottom-label { font-size: 9px; }
  .mobile-bottom-item .bottom-icon { font-size: 1.1rem; }

  /* Sidebar on mobile */
  .sidebar { width: 260px !important; }

  .sidebar.collapsed { transform: translateX(-100%); }

  /* Spacing utilities */
  .mb-xl { margin-bottom: 1.5rem; }
  .mb-lg { margin-bottom: 1.25rem; }
  .mb-md { margin-bottom: 0.875rem; }
  .mt-2xl { margin-top: 2rem; }
  .mb-2xl { margin-bottom: 2rem; }
  .gap-md { gap: 0.875rem; }
  .gap-lg { gap: 1.25rem; }

  /* Weather temp: scale down for small screens */
  .weather-temp-main { font-size: 2.5rem !important; }
  .weather-emoji-large { font-size: 3rem !important; }
  .weather-unit { font-size: 1.5rem !important; }
  .weather-gauges-row { grid-template-columns: repeat(2, 1fr) !important; }

  /* Incentives: ensure items don't push off-screen */
  .glass-card .flex-between {
    flex-wrap: wrap !important;
    gap: 8px !important;
  }

  /* Hero quick links: 2 per row on phone */
  .hero-quick-links {
    grid-template-columns: repeat(2, 1fr) !important;
  }

  /* City weather grid: 2 columns */
  .city-weather-grid {
    grid-template-columns: repeat(2, 1fr) !important;
  }

  /* Map height reduced for tiny screens */
  .weather-map-iframe-container {
    height: 260px !important;
  }

  /* FAB adjusted for small phones */
  .feedback-fab {
    bottom: 70px !important;
    right: 12px !important;
    width: 42px !important;
    height: 42px !important;
    font-size: 1.1rem !important;
  }
  .feedback-card {
    bottom: 122px !important;
    right: 10px !important;
    width: calc(100vw - 20px) !important;
  }
}

/* ---------- Large Desktop (1440px+) ---------- */
@media (min-width: 1440px) {
  :root {
    --container-max: 1600px;
  }
  .grid-4 {
    grid-template-columns: repeat(4, 1fr);
  }
}

/* ---------- Print Styles ---------- */
@media print {
  body {
    background: white;
    color: black;
  }

  .nav,
  .back-to-top,
  .particle-canvas,
  .ticker {
    display: none !important;
  }

  .glass-card {
    background: white;
    border: 1px solid #ccc;
    backdrop-filter: none;
  }

  .section {
    page-break-inside: avoid;
  }
}

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

  html { scroll-behavior: auto; }

  .reveal,
  .reveal-left,
  .reveal-right,
  .reveal-scale {
    opacity: 1;
    transform: none;
  }
}

/* ---------- Mobile Landscape & Low-Height Screens ---------- */
@media (max-height: 500px) and (orientation: landscape) {
  .mobile-header { height: 44px; }
  .mobile-bottom-nav { height: 48px; }
  .main-content {
    padding-top: 44px !important;
    padding-bottom: 48px !important;
  }
  .mobile-bottom-item { padding: 2px 4px; }
  .mobile-bottom-item .bottom-icon { font-size: 0.95rem; }
  .mobile-bottom-item .bottom-label { font-size: 8px; }
  .content-area { padding: 10px 12px; }
  .section-header { margin-bottom: var(--space-md); }
  .weather-map-iframe-container { height: 220px !important; }
  .feedback-fab { bottom: 60px !important; }
}

/* ============================================================
   TOUCH-DEVICE WIDE-ELEMENT SAFEGUARDS
   Catch any remaining overflow sources on touch screens
   ============================================================ */
@media (max-width: 768px) {
  /* Ensure no child of content-area can be wider than the viewport */
  .content-area > * {
    max-width: 100%;
    min-width: 0;
  }

  /* Any inline-flex or inline-block that might overflow */
  .section-badge,
  .tag,
  .filter-chip,
  .tab-btn,
  .btn {
    max-width: 100%;
  }

  /* Stat numbers should shrink gracefully */
  .stat-number {
    word-break: break-word;
    overflow-wrap: break-word;
  }

  /* Chart canvases should never overflow their container */
  .chart-container canvas {
    max-width: 100% !important;
    height: auto !important;
  }

  /* Ensure overhaul 3D card tilt animations don't shift layout */
  .glass-card {
    transform-origin: center center;
  }

  /* Transport data tables: force horizontal scroll */
  .glass-card[style*="overflow-x"] {
    max-width: 100% !important;
    overflow-x: auto !important;
  }

  /* Back-to-top button: keep clear of mobile nav */
  .back-to-top {
    bottom: 78px;
    right: 12px;
  }

  /* Section spacing: reduce cramping */
  .section {
    padding-left: 0;
    padding-right: 0;
  }

  /* Grid gaps: tighter on mobile */
  .grid {
    gap: var(--space-md);
  }

  /* Grid: force single column on very wide grid-auto */
  .grid-auto {
    grid-template-columns: repeat(auto-fill, minmax(min(260px, 100%), 1fr));
  }

  /* Currency sparklines: hide on very small screens to reduce overflow */
  .currency-card .currency-sparkline {
    display: none;
  }
}

/* ============================================================
   INLINE STYLE OVERRIDE FOR WEATHER CONTROLS ON MOBILE
   The city-select has min-width: 220px set inline — override it
   ============================================================ */
@media (max-width: 768px) {
  select.city-select,
  #weather-city-select {
    min-width: 0 !important;
    width: 100% !important;
    max-width: 100% !important;
    box-sizing: border-box !important;
  }

  /* Weather controls row: stack on mobile */
  .weather-controls .glass-card {
    flex-direction: column !important;
    align-items: stretch !important;
  }

  #weather-last-updated {
    margin-left: 0 !important;
  }
}
