/* ==========================================================================
   NCAR Brand Style Guide Implementation
   Tab-Based Navigation Version
   ========================================================================== */

/* --------------------------------------------------------------------------
   CSS Custom Properties (NCAR Brand Colors)
   -------------------------------------------------------------------------- */
:root {
  /* Primary Colors */
  --ncar-blue: #0057C2;
  --dark-blue: #00357A;

  /* Secondary Colors */
  --space: #011837;
  --light-blue: #42C0FF;
  --light-gray: #F1F0EE;
  --white: #FFFFFF;

  /* Accent Colors */
  --accent-orange: #FAA119;
  --accent-yellow: #FFDD31;

  /* Model Colors (for charts — grouped-hue, colorblind-safe) */
  --model-credit: #0072B2;
  --model-multimodel: #56B4E9;
  --model-fourcastnet: #009E73;
  --model-fourcastnet_multimodel: #66CCAA;
  --model-graphcast: #E69F00;
  --model-graphcast_multimodel: #F0C75E;
  --model-pangu: #CC79A7;
  --model-pangu_multimodel: #DEAAC8;
  --model-4dwx: #999999;

  /* Typography */
  --font-family: 'Poppins', Helvetica, Arial, sans-serif;

  /* Spacing */
  --spacing-xs: 0.25rem;
  --spacing-sm: 0.5rem;
  --spacing-md: 1rem;
  --spacing-lg: 1.5rem;
  --spacing-xl: 2rem;

  /* Transitions */
  --transition-fast: 150ms ease;
  --transition-normal: 250ms ease;
}

/* --------------------------------------------------------------------------
   Reset & Base Styles
   -------------------------------------------------------------------------- */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html {
  font-size: 16px;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

body {
  font-family: var(--font-family);
  background-color: var(--light-gray);
  color: var(--dark-blue);
  line-height: 1.5;
  min-height: 100vh;
}

/* --------------------------------------------------------------------------
   App Container Layout
   -------------------------------------------------------------------------- */
.app-container {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  max-width: 1600px;
  margin: 0 auto;
  background: var(--white);
  box-shadow: 0 0 40px rgba(0, 53, 122, 0.1);
}

/* --------------------------------------------------------------------------
   Header - NCAR Brand
   -------------------------------------------------------------------------- */
.header {
  display: flex;
  align-items: flex-end;  /* Bottom-align items when wrapping */
  flex-wrap: wrap;
  gap: var(--spacing-md) var(--spacing-lg);  /* row-gap column-gap */
  padding: 30px var(--spacing-xl);  /* Clear space: 30px min around 60px logo */
  background: var(--ncar-blue);
  position: relative;
  margin-bottom: 50px;  /* Space for wave to extend below */
}

/* Wave separator - curved bottom edge (matches footer curve direction) */
.header::after {
  content: '';
  position: absolute;
  bottom: -50px;
  left: 0;
  right: 0;
  height: 50px;
  background: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 1440 100' preserveAspectRatio='none'%3E%3Cpath d='M0,0 L0,70 Q360,100 720,30 Q1080,-20 1440,70 L1440,0 Z' fill='%230057C2'/%3E%3C/svg%3E");
  background-size: 100% 100%;
  pointer-events: none;
}

.header-brand {
  flex-shrink: 0;
}

.ncar-logo {
  height: 60px;  /* NCAR Brand: minimum 5/8" (~60px at 96dpi) */
  width: auto;
  /* Clear space: half logo height on all sides (30px) is provided by parent padding */
}

.logo-fallback {
  font-weight: 700;
  font-size: 1.25rem;
  color: var(--white);
}

.title-link {
  text-decoration: none;
  color: inherit;
  cursor: pointer;
}
.title-link:hover {
  opacity: 0.85;
}

.header-title h1 {
  font-size: 1.75rem;
  font-weight: 700;
  color: var(--accent-orange);
  line-height: 1.2;
}

.header-title .subtitle {
  font-size: 0.95rem;
  color: var(--light-gray);
  font-weight: 400;
  margin-top: var(--spacing-xs);
}

/* --------------------------------------------------------------------------
   Tab Navigation
   -------------------------------------------------------------------------- */
.tab-nav {
  display: flex;
  gap: var(--spacing-xs);
  padding: 0 var(--spacing-xl);
  background: var(--light-gray);
  border-bottom: 1px solid #ddd;
  overflow-x: auto;
}

.tab {
  display: flex;
  align-items: center;
  gap: var(--spacing-sm);
  padding: var(--spacing-md) var(--spacing-lg);
  background: transparent;
  border: none;
  border-bottom: 3px solid transparent;
  color: var(--dark-blue);
  font-family: var(--font-family);
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  transition: all var(--transition-fast);
  white-space: nowrap;
}

.tab:hover {
  background: rgba(0, 87, 194, 0.05);
  color: var(--ncar-blue);
}

.tab.active {
  background: var(--white);
  border-bottom-color: var(--ncar-blue);
  color: var(--ncar-blue);
}

.tab-icon {
  width: 18px;
  height: 18px;
  flex-shrink: 0;
}

/* --------------------------------------------------------------------------
   Control Pills
   -------------------------------------------------------------------------- */
.controls-bar {
  display: flex;
  flex-wrap: wrap;
  gap: var(--spacing-lg);
  padding: var(--spacing-md) var(--spacing-xl);
  background: var(--white);
  border-bottom: 1px solid #eee;
}

.control-group {
  display: flex;
  align-items: center;
  gap: var(--spacing-sm);
}

.control-label {
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--dark-blue);
}

.pill-group {
  display: flex;
  gap: var(--spacing-xs);
}

.pill {
  display: inline-flex;
  align-items: center;
  cursor: pointer;
}

.pill input[type="radio"] {
  position: absolute;
  opacity: 0;
  width: 0;
  height: 0;
}

.pill span {
  padding: var(--spacing-xs) var(--spacing-md);
  background: var(--light-gray);
  border: 2px solid transparent;
  border-radius: 20px;
  font-size: 0.85rem;
  font-weight: 500;
  color: var(--dark-blue);
  transition: all var(--transition-fast);
}

.pill:hover span {
  border-color: var(--light-blue);
}

.pill input[type="radio"]:checked + span {
  background: var(--ncar-blue);
  color: var(--white);
  border-color: var(--ncar-blue);
}

.pill input[type="radio"]:focus + span {
  box-shadow: 0 0 0 3px rgba(0, 87, 194, 0.2);
}

/* --------------------------------------------------------------------------
   Main Content / Plot Area
   -------------------------------------------------------------------------- */
.main-content {
  flex: 1;
  padding: var(--spacing-lg) var(--spacing-xl);
  min-height: 500px;
}

.plot-area {
  width: 100%;
  min-height: 500px;
  background: var(--white);
  border-radius: 8px;
  position: relative;
}

.loading {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 500px;
  font-size: 1rem;
  color: var(--ncar-blue);
  font-weight: 500;
}

.loading::after {
  content: '';
  width: 24px;
  height: 24px;
  margin-left: var(--spacing-sm);
  border: 3px solid var(--light-gray);
  border-top-color: var(--ncar-blue);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

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

/* --------------------------------------------------------------------------
   Bottom Stats Bar - One-Pager Style (per NCAR Brand Guide p.30)
   Wave separator at top, solid Space color below
   -------------------------------------------------------------------------- */
.stats-bar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--spacing-md);
  padding: var(--spacing-md) var(--spacing-xl);
  background: var(--space);
  color: var(--white);
  margin-top: 80px;  /* Space for wave to extend above */
  position: relative;
}

/* Wave separator - curved top edge */
.stats-bar::before {
  content: '';
  position: absolute;
  top: -80px;
  left: 0;
  right: 0;
  height: 80px;
  background: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 1440 100' preserveAspectRatio='none'%3E%3Cpath d='M0,100 L0,70 Q360,100 720,30 Q1080,-20 1440,70 L1440,100 Z' fill='%23011837'/%3E%3C/svg%3E");
  background-size: 100% 100%;
  pointer-events: none;
}

.stats-content {
  display: flex;
  flex-wrap: wrap;
  gap: var(--spacing-lg);
}

.stat-item {
  display: flex;
  align-items: center;
  gap: var(--spacing-xs);
}

.stat-label {
  font-size: 0.8rem;
  opacity: 0.8;
}

.stat-value {
  font-size: 0.9rem;
  font-weight: 600;
}

.stat-item.model-credit .stat-value {
  color: var(--light-blue);
}

.stat-item.model-multimodel .stat-value {
  color: #8DD0F5;
}

.stat-item.model-fourcastnet .stat-value {
  color: #56D4A8;
}

.stat-item.model-fourcastnet_multimodel .stat-value {
  color: #99DDCC;
}

.stat-item.model-graphcast .stat-value {
  color: #F0C75E;
}

.stat-item.model-graphcast_multimodel .stat-value {
  color: #F5DA8A;
}

.stat-item.model-pangu .stat-value {
  color: #E0A0C4;
}

.stat-item.model-pangu_multimodel .stat-value {
  color: #EECADB;
}

.stats-actions {
  display: flex;
  gap: var(--spacing-sm);
}

.btn-export {
  display: flex;
  align-items: center;
  gap: var(--spacing-xs);
  padding: var(--spacing-xs) var(--spacing-md);
  background: var(--ncar-blue);
  border: none;
  border-radius: 4px;
  color: var(--white);
  font-family: var(--font-family);
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
  transition: background var(--transition-fast);
}

.btn-export:hover {
  background: var(--dark-blue);
}

.btn-export svg {
  width: 16px;
  height: 16px;
}

.stats-attribution {
  width: 100%;
  text-align: center;
  font-size: 0.75rem;
  opacity: 0.6;
  margin-top: var(--spacing-xs);
}

/* --------------------------------------------------------------------------
   Toast Notifications
   -------------------------------------------------------------------------- */
#toast-container {
  position: fixed;
  top: var(--spacing-lg);
  right: var(--spacing-lg);
  z-index: 1000;
  display: flex;
  flex-direction: column;
  gap: var(--spacing-sm);
}

.toast {
  padding: var(--spacing-md) var(--spacing-lg);
  background: var(--space);
  color: var(--white);
  border-radius: 6px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
  animation: slideIn 0.3s ease-out;
}

.toast.error {
  background: #c62828;
}

.toast.success {
  background: #2e7d32;
}

@keyframes slideIn {
  from {
    transform: translateX(100%);
    opacity: 0;
  }
  to {
    transform: translateX(0);
    opacity: 1;
  }
}

/* --------------------------------------------------------------------------
   Map Styles (Leaflet overrides)
   -------------------------------------------------------------------------- */
#map {
  height: 100%;
  width: 100%;
  min-height: 500px;
  border-radius: 8px;
}

.leaflet-popup-content {
  font-family: var(--font-family);
  font-size: 0.85rem;
  color: var(--dark-blue);
}

.legend {
  background: var(--white);
  padding: var(--spacing-md);
  border-radius: 6px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
  font-size: 0.8rem;
  line-height: 1.6;
}

.legend strong {
  display: block;
  margin-bottom: var(--spacing-xs);
  color: var(--dark-blue);
}

.legend i {
  width: 16px;
  height: 16px;
  display: inline-block;
  margin-right: var(--spacing-sm);
  vertical-align: middle;
  border-radius: 2px;
}

/* Map Mode Toggle */
.map-mode-toggle {
  display: inline-flex;
  border-radius: 14px;
  overflow: hidden;
  border: 2px solid var(--ncar-blue);
  flex-shrink: 0;
}

.map-mode-btn {
  padding: 3px 12px;
  border: none;
  background: transparent;
  color: var(--ncar-blue);
  font-family: var(--font-family);
  font-size: 0.75rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 150ms ease;
  white-space: nowrap;
}

.map-mode-btn.active {
  background: var(--ncar-blue);
  color: #fff;
}

.map-mode-btn:hover:not(.active) {
  background: rgba(0, 87, 194, 0.08);
}

/* Split Marker */
.split-marker {
  background: transparent !important;
  border: none !important;
}

.split-marker-inner {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  border: 1px solid #333;
  transition: transform 120ms ease;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.25);
}

.split-marker-inner:hover {
  transform: scale(1.25);
}

/* Enhanced Map Popup */
.map-popup { min-width: 160px; }

.popup-station-name {
  font-weight: 700;
  font-size: 0.85rem;
  margin-bottom: 6px;
  padding-bottom: 4px;
  border-bottom: 1px solid #e0e0e0;
}

.popup-model-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.8rem;
}

.popup-model-table td { padding: 2px 4px; white-space: nowrap; }
.popup-model-table tr.best-row td { font-weight: 700; }

.popup-color-dot {
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  margin-right: 4px;
  vertical-align: middle;
}

/* Map Legend */
.map-legend {
  background: var(--white);
  padding: var(--spacing-sm) var(--spacing-md);
  border-radius: 6px;
  font-size: 0.75rem;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
  max-height: 200px;
  overflow-y: auto;
  line-height: 1.6;
}

.map-legend strong {
  display: block;
  margin-bottom: 2px;
  font-size: 0.8rem;
}

.map-legend-swatch {
  display: inline-block;
  width: 12px;
  height: 12px;
  border-radius: 50%;
  margin-right: 5px;
  vertical-align: middle;
  border: 1px solid rgba(0, 0, 0, 0.15);
}

.map-legend-note {
  font-style: italic;
  color: #666;
  margin-top: 4px;
  font-size: 0.7rem;
}

/* --------------------------------------------------------------------------
   Responsive Design
   -------------------------------------------------------------------------- */
@media (max-width: 1024px) {
  .header {
    flex-direction: column;
    align-items: flex-start;
    gap: var(--spacing-md);
  }

  .tab-nav {
    padding: 0 var(--spacing-md);
  }

  .tab {
    padding: var(--spacing-sm) var(--spacing-md);
    font-size: 0.85rem;
  }

  .tab span {
    display: none;
  }

  .tab-icon {
    width: 20px;
    height: 20px;
  }

  .controls-bar {
    flex-direction: column;
    align-items: flex-start;
    padding: var(--spacing-md);
  }

  .main-content {
    padding: var(--spacing-md);
  }
}

@media (max-width: 768px) {
  .header-title h1 {
    font-size: 1.25rem;
  }

  .pill span {
    padding: var(--spacing-xs) var(--spacing-sm);
    font-size: 0.8rem;
  }

  .stats-bar {
    flex-direction: column;
    align-items: flex-start;
  }

  .stats-content {
    gap: var(--spacing-md);
  }
}

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

  .app-container {
    box-shadow: none;
    max-width: none;
  }

  .tab-nav,
  .controls-bar,
  .stats-actions,
  #toast-container {
    display: none !important;
  }

  .header {
    border-bottom: 2px solid var(--dark-blue);
    padding: var(--spacing-md);
  }

  .main-content {
    padding: 0;
  }

  .plot-area {
    min-height: auto;
    page-break-inside: avoid;
  }

  .stats-bar {
    background: var(--light-gray);
    color: var(--dark-blue);
    padding: var(--spacing-sm) var(--spacing-md);
    page-break-inside: avoid;
  }

  .stat-item[class*="model-"] .stat-value {
    color: var(--dark-blue);
  }

  .stats-attribution {
    color: var(--dark-blue);
    opacity: 1;
  }
}

/* --------------------------------------------------------------------------
   Page Intro Animation
   Header and footer waves start together at center, then separate to reveal content
   -------------------------------------------------------------------------- */

/* Animation active state */
body.page-intro-animation {
  overflow: hidden;
}

/* Spacer takes up header space while header is fixed */
body.page-intro-animation .header-spacer {
  height: 170px;  /* Header height (~120px) + margin-bottom (50px) */
}

/* Hide spacer after animation */
body.page-intro-complete .header-spacer {
  display: none;
}

/* Content starts invisible */
body.page-intro-animation .tab-nav,
body.page-intro-animation .controls-bar,
body.page-intro-animation .main-content {
  opacity: 0;
}

/* Header needs to be positioned for wave animation */
/* max-width matches .app-container to prevent jarring width change */
body.page-intro-animation .header {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  max-width: 1600px;
  margin-left: auto;
  margin-right: auto;
  z-index: 1001;
  animation: headerSlideUp 2s cubic-bezier(0.4, 0, 0.2, 1) forwards;
}

/* Stats bar (footer) needs to be positioned for wave animation */
/* max-width matches .app-container to prevent jarring width change */
body.page-intro-animation .stats-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  max-width: 1600px;
  margin-left: auto;
  margin-right: auto;
  z-index: 1000;
  animation: footerSlideDown 2s cubic-bezier(0.4, 0, 0.2, 1) forwards;
}

/* Header starts at center, slides up to top */
@keyframes headerSlideUp {
  0% {
    top: calc(50vh - 130px);
  }
  100% {
    top: 0;
  }
}

/* Stats-bar starts at center, slides down to bottom */
@keyframes footerSlideDown {
  0% {
    bottom: calc(50vh - 120px);
  }
  100% {
    bottom: 0;
  }
}

/* Content fade-in - starts earlier now that spacer maintains position */
body.page-intro-animation .tab-nav,
body.page-intro-animation .controls-bar,
body.page-intro-animation .main-content {
  animation: contentFadeIn 0.6s ease-out 0.8s forwards;
}

@keyframes contentFadeIn {
  0% {
    opacity: 0;
    transform: translateY(20px);
  }
  100% {
    opacity: 1;
    transform: translateY(0);
  }
}

/* Skip hint shown during animation */
body.page-intro-animation::before {
  content: 'Click or press any key to skip';
  position: fixed;
  bottom: 15%;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(1, 24, 55, 0.85);
  color: var(--white);
  padding: var(--spacing-sm) var(--spacing-lg);
  border-radius: 20px;
  font-size: 0.75rem;
  font-weight: 500;
  z-index: 1002;
  opacity: 0;
  animation: skipHintFade 2s ease-out forwards;
  pointer-events: none;
}

@keyframes skipHintFade {
  0%, 15% { opacity: 0; }
  25% { opacity: 1; }
  75% { opacity: 1; }
  90%, 100% { opacity: 0; }
}

/* After animation completes - restore normal state */
body.page-intro-complete {
  overflow: visible;
}

body.page-intro-complete .header,
body.page-intro-complete .stats-bar {
  position: relative;
  animation: none;
}

body.page-intro-complete .tab-nav,
body.page-intro-complete .controls-bar,
body.page-intro-complete .main-content {
  opacity: 1;
  transform: none;
  animation: none;
}

body.page-intro-complete::before {
  display: none;
}

/* Accessibility: Respect reduced motion preference */
@media (prefers-reduced-motion: reduce) {
  body.page-intro-animation .header,
  body.page-intro-animation .stats-bar,
  body.page-intro-animation .tab-nav,
  body.page-intro-animation .controls-bar,
  body.page-intro-animation .main-content {
    animation: none !important;
    opacity: 1 !important;
    transform: none !important;
  }

  body.page-intro-animation::before {
    display: none !important;
  }

  body.page-intro-animation {
    overflow: visible;
  }

  body.page-intro-animation .header,
  body.page-intro-animation .stats-bar {
    position: relative;
  }

  body.page-intro-animation .header-spacer {
    display: none;
  }
}
