/* ==========================================================================
   NCAR Brand Style Guide Implementation
   Dashboard Grid 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 (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;

  /* Card sizes */
  --card-min-height: 400px;
}

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

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

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

/* --------------------------------------------------------------------------
   App Container
   -------------------------------------------------------------------------- */
.app-container {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

/* --------------------------------------------------------------------------
   Header
   -------------------------------------------------------------------------- */
.header {
  display: flex;
  justify-content: space-between;
  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-left {
  display: flex;
  align-items: center;
  gap: var(--spacing-lg);
}

.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.1rem;
  color: var(--ncar-blue);
}

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

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

.header-title .subtitle {
  font-size: 0.85rem;
  color: var(--light-gray);
  margin-top: 2px;
}

/* --------------------------------------------------------------------------
   Sticky Controls Bar (below header)
   -------------------------------------------------------------------------- */
.controls-bar {
  position: sticky;
  top: 0;
  z-index: 100;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--spacing-lg);
  padding: var(--spacing-md) var(--spacing-xl);
  background: var(--white);
  border-bottom: 1px solid #eee;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
}

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

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

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

.control-group select {
  padding: var(--spacing-xs) var(--spacing-md);
  border: 2px solid var(--light-gray);
  border-radius: 6px;
  font-family: var(--font-family);
  font-size: 0.85rem;
  background: var(--white);
  color: var(--dark-blue);
  cursor: pointer;
  transition: border-color var(--transition-fast);
}

.control-group select:hover,
.control-group select:focus {
  border-color: var(--ncar-blue);
  outline: none;
}

/* --------------------------------------------------------------------------
   Dashboard Grid
   -------------------------------------------------------------------------- */
.dashboard {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: var(--spacing-lg);
  padding: var(--spacing-lg) var(--spacing-xl);
  flex: 1;
}

/* --------------------------------------------------------------------------
   Cards
   -------------------------------------------------------------------------- */
.card {
  background: var(--white);
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 53, 122, 0.08);
  overflow: hidden;
  display: flex;
  flex-direction: column;
  transition: box-shadow var(--transition-normal);
}

.card:hover {
  box-shadow: 0 4px 16px rgba(0, 53, 122, 0.12);
}

.card-full {
  grid-column: 1 / -1;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: var(--spacing-md) var(--spacing-lg);
  background: var(--light-gray);
  border-bottom: 1px solid #e0e0e0;
}

.card-header h2 {
  font-size: 1rem;
  font-weight: 600;
  color: var(--dark-blue);
}

.card-actions {
  display: flex;
  gap: var(--spacing-xs);
}

.btn-expand,
.btn-toggle,
.btn-close {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  background: transparent;
  border: none;
  border-radius: 4px;
  color: var(--dark-blue);
  cursor: pointer;
  transition: all var(--transition-fast);
}

.btn-expand:hover,
.btn-toggle:hover,
.btn-close:hover {
  background: var(--ncar-blue);
  color: var(--white);
}

.btn-expand svg,
.btn-toggle svg,
.btn-close svg {
  width: 18px;
  height: 18px;
}

.card-body {
  flex: 1;
  min-height: var(--card-min-height);
  padding: var(--spacing-sm);
  position: relative;
}

.card-body.collapsed {
  display: none;
}

.card-full .card-body {
  min-height: 350px;
}

.plot-container {
  width: 100%;
  height: 100%;
  min-height: var(--card-min-height);
}

.card-full .plot-container {
  min-height: 350px;
}

/* Toggle rotation for collapsed state */
.card-full .btn-toggle svg {
  transition: transform var(--transition-fast);
}

.card-full.is-collapsed .btn-toggle svg {
  transform: rotate(-90deg);
}

/* --------------------------------------------------------------------------
   Loading State
   -------------------------------------------------------------------------- */
.loading {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
  min-height: var(--card-min-height);
  font-size: 0.9rem;
  color: var(--ncar-blue);
  font-weight: 500;
}

.loading::after {
  content: '';
  width: 20px;
  height: 20px;
  margin-left: var(--spacing-sm);
  border: 2px 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); }
}

/* --------------------------------------------------------------------------
   Modal
   -------------------------------------------------------------------------- */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(1, 24, 55, 0.85);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  padding: var(--spacing-lg);
}

.modal-overlay.active {
  display: flex;
}

.modal {
  background: var(--white);
  border-radius: 12px;
  width: 100%;
  max-width: 1200px;
  max-height: 90vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: var(--spacing-md) var(--spacing-lg);
  background: var(--ncar-blue);
  color: var(--white);
}

.modal-header h2 {
  font-size: 1.25rem;
  font-weight: 600;
}

.modal-header .btn-close {
  color: var(--white);
}

.modal-header .btn-close:hover {
  background: rgba(255, 255, 255, 0.2);
}

.modal-body {
  flex: 1;
  padding: var(--spacing-lg);
  overflow: auto;
  display: flex;
  flex-direction: column;
}

.modal-body .plot-container {
  min-height: 500px;
  flex: 1;
  display: flex;
  flex-direction: column;
}

.modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: var(--spacing-sm);
  padding: var(--spacing-md) var(--spacing-lg);
  background: var(--light-gray);
  border-top: 1px solid #e0e0e0;
}

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

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

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

/* --------------------------------------------------------------------------
   Footer - One-Pager Style (per NCAR Brand Guide p.30)
   Wave separator at top, solid Space color below
   -------------------------------------------------------------------------- */
.footer {
  background: var(--space);
  color: var(--white);
  padding: 0;
  margin-top: 80px;  /* Space for wave to extend above */
  position: relative;
}

/* Wave separator - curved top edge */
.footer::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;
}

.footer-content {
  padding: var(--spacing-lg) var(--spacing-xl);
  text-align: center;
  font-size: 0.8rem;
}

/* --------------------------------------------------------------------------
   Toast Notifications
   -------------------------------------------------------------------------- */
#toast-container {
  position: fixed;
  top: var(--spacing-lg);
  right: var(--spacing-lg);
  z-index: 1100;
  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
   -------------------------------------------------------------------------- */
#map,
.leaflet-container {
  height: 100%;
  width: 100%;
  min-height: var(--card-min-height);
  border-radius: 4px;
}

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

.legend {
  background: var(--white);
  padding: var(--spacing-sm) var(--spacing-md);
  border-radius: 4px;
  font-size: 0.75rem;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.15);
}

/* Map Mode Toggle — pill buttons for Best Model / Comparison */
.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.72rem;
  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 — conic-gradient half-circle for tied models */
.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 — scrollable for many models */
.map-legend {
  background: var(--white);
  padding: var(--spacing-sm) var(--spacing-md);
  border-radius: 4px;
  font-size: 0.72rem;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.15);
  max-height: 180px;
  overflow-y: auto;
  line-height: 1.6;
}

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

.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.68rem;
}

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

  .dashboard {
    grid-template-columns: 1fr;
    padding: var(--spacing-md);
  }

  .card-full {
    grid-column: 1;
  }
}

@media (max-width: 768px) {
  .header-left {
    flex-direction: column;
    align-items: flex-start;
    gap: var(--spacing-sm);
  }

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

  .control-group {
    width: 100%;
  }

  .control-group select {
    flex: 1;
  }

  :root {
    --card-min-height: 320px;
  }
}

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

  .controls-bar,
  .btn-expand,
  .btn-toggle,
  .footer,
  .modal-overlay,
  #toast-container {
    display: none !important;
  }

  .dashboard {
    display: block;
    padding: 0;
  }

  .card {
    page-break-inside: avoid;
    margin-bottom: var(--spacing-lg);
    box-shadow: none;
    border: 1px solid #ddd;
  }

  .card-body.collapsed {
    display: block !important;
  }

  .card-header {
    background: var(--light-gray);
  }
}

/* --------------------------------------------------------------------------
   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 .dashboard {
  opacity: 0;
}

/* Header needs to be positioned for wave animation */
body.page-intro-animation .header {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 1001;
  animation: headerSlideUp 2s cubic-bezier(0.4, 0, 0.2, 1) forwards;
}

/* Footer needs to be positioned for wave animation */
body.page-intro-animation .footer {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  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;
  }
}

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

/* Controls bar starts invisible and fades in with content */
body.page-intro-animation .controls-bar {
  opacity: 0;
  animation: contentFadeIn 0.6s ease-out 0.8s forwards;
}

body.page-intro-complete .controls-bar {
  opacity: 1;
  animation: none;
}

/* Content fade-in - starts earlier now that spacer maintains position */
body.page-intro-animation .dashboard {
  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 .footer {
  position: relative;
  animation: none;
}

body.page-intro-complete .dashboard {
  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 .footer,
  body.page-intro-animation .controls-bar,
  body.page-intro-animation .dashboard {
    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 .footer {
    position: relative;
  }

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