* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  padding: 0;
  overflow: hidden;
  background: #000;
  color: white;
  font-family: system-ui, -apple-system, Segoe UI, Roboto, Helvetica, Arial, sans-serif;
}
canvas {
  width: 100vw !important;
  height: 100vh !important;
  height: 100dvh !important; /* Dynamic viewport height for mobile */
  display: block;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 1;
}
#info {
  position: absolute;
  top: 16px;
  left: 16px;
  z-index: 10;
  background: rgba(0,0,0,0.55);
  padding: 12px 14px;
  border-radius: 10px;
  backdrop-filter: blur(4px);
}

.title-container {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 4px;
}

.logo {
  display: flex;
  align-items: center;
  justify-content: center;
}

.logo-image {
  width: 50px;
  height: 50px;
  object-fit: contain;
  filter: drop-shadow(0 2px 4px rgba(0,0,0,0.3));
  transition: transform 0.3s ease;
}

.logo-image:hover {
  transform: scale(1.1);
}

.title-container h1 {
  margin: 0;
  font-size: 24px;
  font-weight: bold;
  color: white;
  text-shadow: 0 1px 3px rgba(0,0,0,0.5);
}
#legend { display: flex; gap: 10px; margin-top: 8px; margin-bottom: 10px; }
.legend-item { display: flex; align-items: center; gap: 8px; }
.legend-color { 
  width: 14px; 
  height: 14px; 
  border-radius: 999px; 
  display: inline-block; 
}
.legend-color.low { background: rgb(51, 204, 51); }
.legend-color.mid { background: rgb(255, 153, 51); }
.legend-color.hi { background: rgb(183, 28, 28); }
.row { display: flex; gap: 8px; }
button {
  background: #1e293b;
  color: white;
  border: 1px solid #334155;
  border-radius: 10px;
  padding: 8px 10px;
  cursor: pointer;
}
button:hover { background: #0f172a; }

#search-bar-container {
  position: absolute;
  top: 18px;
  right: 24px;
  z-index: 20;
  display: flex;
  gap: 8px;
  align-items: center;
  background: rgba(0,0,0,0.55);
  padding: 8px 12px;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.12);
}
#city-search-input {
  padding: 6px 10px;
  border-radius: 6px;
  border: 1px solid #334155;
  font-size: 15px;
  background: #181e2a;
  color: #fff;
  outline: none;
}
#city-search-btn {
  padding: 6px 14px;
  border-radius: 6px;
  border: none;
  background: #3a8ef6;
  color: white;
  font-size: 15px;
  cursor: pointer;
  transition: background 0.2s;
}
#city-search-btn:hover {
  background: #1e6ddb;
}
#search-result-info {
  margin-left: 12px;
  color: #fff;
  font-size: 15px;
  background: rgba(0,0,0,0.7);
  padding: 6px 10px;
  border-radius: 6px;
  max-width: 260px;
  white-space: pre-line;
}

/* AI Explanation Section */
#ai-explanation-container {
  position: absolute;
  top: 80px;
  right: 24px;
  z-index: 19;
  max-width: 380px;
  background: rgba(15, 23, 42, 0.95);
  border: 1px solid rgba(59, 130, 246, 0.3);
  border-radius: 12px;
  padding: 16px;
  backdrop-filter: blur(8px);
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
  animation: slideIn 0.3s ease-out;
}

.ai-header {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 12px;
  padding-bottom: 8px;
  border-bottom: 1px solid rgba(59, 130, 246, 0.2);
}

.ai-icon {
  font-size: 18px;
  animation: pulse 2s infinite;
}

.ai-title {
  color: #3b82f6;
  font-weight: 600;
  font-size: 14px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

#ai-explanation-content {
  color: #e2e8f0;
  font-size: 13px;
  line-height: 1.5;
  min-height: 60px;
}

.loading-dots {
  display: flex;
  gap: 4px;
  justify-content: center;
  align-items: center;
  height: 60px;
}

.loading-dots span {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #3b82f6;
  animation: bounce 1.4s infinite ease-in-out both;
}

.loading-dots span:nth-child(1) { animation-delay: -0.32s; }
.loading-dots span:nth-child(2) { animation-delay: -0.16s; }

.aqi-explanation {
  margin-bottom: 12px;
}

.city-header {
  display: flex;
  gap: 12px;
  margin-bottom: 12px;
  align-items: flex-start;
}

.city-image-container {
  position: relative;
  width: 80px;
  height: 60px;
  border-radius: 8px;
  overflow: hidden;
  border: 2px solid rgba(59, 130, 246, 0.3);
  flex-shrink: 0;
  transition: transform 0.3s ease, box-shadow 0.3s ease;
}

.city-image-container:hover {
  transform: scale(1.05);
  box-shadow: 0 4px 12px rgba(59, 130, 246, 0.4);
}

.city-image {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  opacity: 0;
  transition: opacity 0.5s ease;
  z-index: 2;
}

.city-image-fallback {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: linear-gradient(135deg, #4a90e2, #357abd);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: white;
  text-align: center;
  z-index: 1;
}

.city-image-fallback::before {
  content: '';
  position: absolute;
  top: -50%;
  left: -50%;
  width: 200%;
  height: 200%;
  background: radial-gradient(circle, rgba(255,255,255,0.1) 0%, transparent 70%);
  animation: shimmer 3s ease-in-out infinite;
}

@keyframes shimmer {
  0%, 100% { transform: rotate(0deg); opacity: 0.3; }
  50% { transform: rotate(180deg); opacity: 0.1; }
}

.city-icon {
  font-size: 20px;
  margin-bottom: 2px;
}

.city-name {
  font-size: 9px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  line-height: 1.1;
  max-width: 70px;
  overflow: hidden;
  text-overflow: ellipsis;
}

.loading-spinner {
  position: absolute;
  top: 8px;
  right: 8px;
  font-size: 12px;
  animation: spin 2s linear infinite;
  z-index: 3;
}

@keyframes spin {
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); }
}

.city-info {
  flex: 1;
  min-width: 0;
}

.aqi-level {
  font-weight: 600;
  padding: 4px 8px;
  border-radius: 4px;
  display: inline-block;
  margin-bottom: 6px;
  font-size: 12px;
}

.aqi-good { background: rgba(51, 204, 51, 0.2); color: #33cc33; }
.aqi-moderate { background: rgba(255, 153, 51, 0.2); color: #ff9933; }
.aqi-unhealthy { background: rgba(183, 28, 28, 0.2); color: #b71c1c; }

.health-effects {
  background: rgba(239, 68, 68, 0.1);
  border-left: 3px solid #ef4444;
  padding: 8px 12px;
  margin: 8px 0;
  border-radius: 0 4px 4px 0;
}

.year-indicator {
  font-size: 11px;
  color: #3b82f6;
  font-weight: 600;
  margin-bottom: 4px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.prediction-reasoning {
  background: rgba(147, 51, 234, 0.1);
  border-left: 3px solid #9333ea;
  padding: 8px 12px;
  margin: 8px 0;
  border-radius: 0 4px 4px 0;
  font-size: 12px;
  line-height: 1.4;
}

.environmental-causes {
  background: rgba(59, 130, 246, 0.1);
  border-left: 3px solid #3b82f6;
  padding: 8px 12px;
  margin: 8px 0;
  border-radius: 0 4px 4px 0;
}

.recommendations {
  background: rgba(34, 197, 94, 0.1);
  border-left: 3px solid #22c55e;
  padding: 8px 12px;
  margin: 8px 0;
  border-radius: 0 4px 4px 0;
}

@keyframes slideIn {
  from {
    opacity: 0;
    transform: translateY(-10px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes pulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.1); }
}

@keyframes bounce {
  0%, 80%, 100% {
    transform: scale(0);
  } 40% {
    transform: scale(1);
  }
}

body {
    margin: 0;
    font-family: Arial, sans-serif;
    background-color: #111;
    color: #eee;
}

header {
    text-align: center;
    padding: 20px 0;
    background: #111;
}

header h1 {
    font-size: 32px;
    font-weight: 700;
    margin-bottom: 5px;
}

header p {
    font-size: 16px;
    opacity: 0.8;
    margin-top: 0;
}

#controls {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 12px;
    padding: 12px;
    background: #1c1c1c;
    border-bottom: 1px solid #333;
}

#controls label, #controls input, #controls button {
    cursor: pointer;
}

#controls button {
    padding: 6px 14px;
    border: none;
    background: #3a8ef6;
    color: white;
    border-radius: 6px;
    font-size: 14px;
    transition: background 0.2s;
}

#controls button:hover {
    background: #1e6ddb;
}

#globe-container {
    width: 100vw;
    height: calc(100vh - 140px);
    overflow: hidden;
    display: flex;
    justify-content: center;
    align-items: center;
}

/* Enhanced tooltip styling */
#tooltip {
    position: absolute;
    pointer-events: none;
    z-index: 1000;
    display: none;
    max-width: 300px;
    font-size: 13px;
    line-height: 1.4;
    box-shadow: 0 4px 12px rgba(0,0,0,0.4);
    border-radius: 8px;
}

.tooltip-content {
    background: rgba(0,0,0,0.95);
    padding: 12px;
    border-radius: 8px;
    color: white;
    font-family: Arial, sans-serif;
    backdrop-filter: blur(5px);
}

.tooltip-title {
    font-size: 16px;
    font-weight: bold;
    margin-bottom: 8px;
    border-bottom: 1px solid rgba(255,255,255,0.2);
    padding-bottom: 4px;
}

.tooltip-data {
    margin-bottom: 4px;
}

.tooltip-footer {
    font-size: 12px;
    color: #ccc;
    margin-top: 8px;
    padding-top: 4px;
    border-top: 1px solid rgba(255,255,255,0.1);
}

/* Speed control enhancements */
input[type="range"] {
    -webkit-appearance: none;
    appearance: none;
    height: 6px;
    background: rgba(255,255,255,0.2);
    border-radius: 3px;
    outline: none;
}

input[type="range"]::-webkit-slider-thumb {
    -webkit-appearance: none;
    appearance: none;
    width: 16px;
    height: 16px;
    background: #4CAF50;
    border-radius: 50%;
    cursor: pointer;
    border: 2px solid white;
    box-shadow: 0 2px 4px rgba(0,0,0,0.3);
}

input[type="range"]::-moz-range-thumb {
    width: 16px;
    height: 16px;
    background: #4CAF50;
    border-radius: 50%;
    cursor: pointer;
    border: 2px solid white;
    box-shadow: 0 2px 4px rgba(0,0,0,0.3);
}

input[type="range"]:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

input[type="range"]:disabled::-webkit-slider-thumb {
    cursor: not-allowed;
}

input[type="range"]:disabled::-moz-range-thumb {
    cursor: not-allowed;
}

/* Mobile Toggle Controls */
#mobile-toggles {
  position: fixed;
  top: 10px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 1001;
  display: none;
  background: rgba(0,0,0,0.85);
  padding: 8px 12px;
  border-radius: 25px;
  gap: 8px;
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255,255,255,0.1);
}

.toggle-btn {
  background: rgba(255,255,255,0.2);
  border: 1px solid rgba(255,255,255,0.3);
  border-radius: 50%;
  width: 40px;
  height: 40px;
  color: white;
  font-size: 16px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.3s ease;
}

.toggle-btn:hover {
  background: rgba(255,255,255,0.3);
  transform: scale(1.1);
}

.toggle-btn.active {
  background: #3a8ef6;
  border-color: #3a8ef6;
}

.toggle-btn.hidden {
  background: rgba(255,0,0,0.6);
  border-color: rgba(255,0,0,0.8);
}

/* Mobile Responsive Styles */
@media (max-width: 768px) {
  /* Show mobile toggles */
  #mobile-toggles {
    display: flex;
  }

  /* Info panel adjustments */
  #info {
    position: fixed;
    top: 70px;
    left: 50%;
    transform: translateX(-50%);
    width: calc(100% - 32px);
    max-width: 400px;
    font-size: 12px;
    padding: 10px 12px;
  }

  .title-container {
    flex-direction: column;
    text-align: center;
    gap: 8px;
  }

  .title-container h1 {
    font-size: 18px;
  }

  .logo-image {
    width: 40px;
    height: 40px;
  }

  #legend {
    flex-direction: column;
    gap: 6px;
    font-size: 11px;
  }

  /* Search bar mobile layout */
  #search-bar-container {
    position: fixed;
    top: auto;
    bottom: 120px;
    right: 50%;
    transform: translateX(50%);
    left: auto;
    width: calc(100% - 32px);
    max-width: 350px;
    flex-direction: column;
    gap: 8px;
    padding: 12px;
  }

  #city-search-input {
    width: 100%;
    font-size: 16px; /* Prevent zoom on iOS */
    padding: 12px 10px;
  }

  #city-search-btn {
    width: 100%;
    padding: 12px 14px;
    font-size: 16px;
  }

  #search-result-info {
    margin-left: 0;
    max-width: none;
    width: 100%;
    text-align: center;
  }

  /* AI Explanation adjustments */
  #ai-explanation-container {
    position: fixed;
    top: auto;
    bottom: 280px;
    right: 50%;
    transform: translateX(50%);
    left: auto;
    width: calc(100% - 32px);
    max-width: 400px;
    max-height: 250px;
    overflow-y: auto;
    font-size: 12px;
  }

  .city-header {
    flex-direction: column;
    align-items: center;
    text-align: center;
  }

  .city-image-container {
    width: 60px;
    height: 45px;
  }

  /* Year slider mobile layout */
  .year-slider-container {
    position: fixed !important;
    bottom: 60px !important;
    left: 50% !important;
    transform: translateX(-50%) !important;
    width: calc(100% - 32px) !important;
    max-width: 350px !important;
    flex-direction: column !important;
    gap: 8px !important;
    padding: 12px !important;
    font-size: 14px !important;
  }

  .year-slider-container input[type="range"] {
    width: 100% !important;
    margin: 8px 0 !important;
  }

  /* Rotation controls mobile layout */
  .rotation-controls {
    position: fixed !important;
    bottom: 10px !important;
    right: 50% !important;
    transform: translateX(50%) !important;
    left: auto !important;
    width: calc(100% - 32px) !important;
    max-width: 350px !important;
    flex-direction: column !important;
    gap: 8px !important;
    padding: 10px 12px !important;
    font-size: 12px !important;
  }

  .rotation-controls .row {
    flex-direction: column;
    gap: 6px;
  }

  .rotation-controls input[type="range"] {
    width: 100% !important;
  }

  .rotation-controls button {
    padding: 8px 12px;
    font-size: 12px;
  }

  /* Tooltip adjustments */
  #tooltip {
    max-width: calc(100vw - 40px) !important;
    font-size: 12px !important;
  }

  .tooltip-content {
    padding: 8px !important;
  }

  .tooltip-title {
    font-size: 14px !important;
  }

  /* Hidden elements on mobile */
  .mobile-hidden {
    display: none !important;
  }

  /* Canvas touch improvements */
  canvas {
    touch-action: none;
  }
}

@media (max-width: 480px) {
  /* Extra small screens */
  #info {
    font-size: 11px;
    padding: 8px 10px;
  }

  .title-container h1 {
    font-size: 16px;
  }

  #legend {
    font-size: 10px;
  }

  #mobile-toggles {
    gap: 6px;
  }

  .toggle-btn {
    width: 35px;
    height: 35px;
    font-size: 14px;
  }

  #ai-explanation-container {
    max-height: 200px;
    font-size: 11px;
  }

  #search-bar-container {
    bottom: 110px;
  }
}

/* Landscape mode adjustments */
@media (max-width: 768px) and (orientation: landscape) {
  #info {
    top: 10px;
    left: 10px;
    transform: none;
    width: 280px;
    max-width: none;
  }

  #search-bar-container {
    top: 10px;
    bottom: auto;
    right: 10px;
    transform: none;
    width: 250px;
    max-width: none;
  }

  #ai-explanation-container {
    top: 80px;
    bottom: auto;
    right: 10px;
    transform: none;
    width: 280px;
    max-width: none;
    max-height: 180px;
  }

  .year-slider-container {
    bottom: 10px !important;
    left: 50% !important;
    transform: translateX(-50%) !important;
    flex-direction: row !important;
    width: auto !important;
    max-width: none !important;
  }

  .rotation-controls {
    bottom: 10px !important;
    right: 10px !important;
    transform: none !important;
    width: 200px !important;
    max-width: none !important;
  }

  #mobile-toggles {
    top: 50%;
    left: 5px;
    transform: translateY(-50%);
    flex-direction: column;
  }
}

/* Animation for smooth transitions */
.mobile-hidden {
  opacity: 0 !important;
  pointer-events: none !important;
  transform: scale(0.8) !important;
  transition: all 0.3s ease !important;
}

#info, #search-bar-container, #ai-explanation-container,
.year-slider-container, .rotation-controls {
  transition: all 0.3s ease;
}

/* Improved mobile scrolling */
@media (max-width: 768px) {
  #ai-explanation-container {
    -webkit-overflow-scrolling: touch;
  }
  
  /* Better button spacing on mobile */
  .rotation-controls button {
    min-height: 44px; /* iOS touch target minimum */
  }
  
  #city-search-btn {
    min-height: 44px;
  }
  
  /* Improved input styling for mobile */
  #city-search-input:focus {
    outline: 2px solid #3a8ef6;
    outline-offset: 2px;
  }
}

/* High DPI/Retina display optimizations */
@media (-webkit-min-device-pixel-ratio: 2), (min-resolution: 192dpi) {
  .logo-image {
    image-rendering: -webkit-optimize-contrast;
    image-rendering: crisp-edges;
  }
}

/* Dark mode improvements for mobile */
@media (prefers-color-scheme: dark) {
  .toggle-btn {
    background: rgba(255,255,255,0.15);
    border-color: rgba(255,255,255,0.25);
  }
  
  .toggle-btn:hover {
    background: rgba(255,255,255,0.25);
  }
}

/* Accessibility improvements */
@media (prefers-reduced-motion: reduce) {
  .mobile-hidden,
  #info, #search-bar-container, #ai-explanation-container,
  .year-slider-container, .rotation-controls,
  .toggle-btn {
    transition: none !important;
    animation: none !important;
  }
}

/* Focus indicators for keyboard navigation */
.toggle-btn:focus {
  outline: 2px solid #3a8ef6;
  outline-offset: 2px;
}

/* Safe area handling for devices with notches */
@supports (padding: max(0px)) {
  @media (max-width: 768px) {
    #mobile-toggles {
      top: max(10px, env(safe-area-inset-top));
      padding-left: max(8px, env(safe-area-inset-left));
      padding-right: max(8px, env(safe-area-inset-right));
    }
    
    #info {
      top: max(70px, calc(env(safe-area-inset-top) + 60px));
      margin-left: max(16px, env(safe-area-inset-left));
      margin-right: max(16px, env(safe-area-inset-right));
    }
    
    .year-slider-container,
    .rotation-controls {
      margin-bottom: max(10px, env(safe-area-inset-bottom)) !important;
      margin-left: max(16px, env(safe-area-inset-left)) !important;
      margin-right: max(16px, env(safe-area-inset-right)) !important;
    }
  }
}
