/* ==========================================
   DESIGN SYSTEM - CSS Custom Properties
   (Adapted from Tensora Mapping app)
   ========================================== */
:root {
  --color-primary: #1E3A5F;
  --color-primary-dark: #0F2744;
  --color-primary-light: #2D5A8A;
  --color-primary-hover: #2A4A73;

  --color-status-active: #059669;
  --color-status-pending: #D97706;
  --color-status-error: #DC2626;

  --color-gray-50: #F9FAFB;
  --color-gray-100: #F3F4F6;
  --color-gray-200: #E5E7EB;
  --color-gray-300: #D1D5DB;
  --color-gray-400: #9CA3AF;
  --color-gray-500: #6B7280;
  --color-gray-600: #4B5563;
  --color-gray-700: #374151;
  --color-gray-800: #1F2937;

  --color-background: #FFFFFF;
  --color-surface: var(--color-gray-50);
  --color-border: var(--color-gray-200);
  --color-text-primary: var(--color-gray-800);
  --color-text-secondary: var(--color-gray-600);
  --color-text-muted: var(--color-gray-400);

  --font-sans: 'Montserrat', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  --font-mono: 'JetBrains Mono', 'Fira Code', Consolas, monospace;

  --text-xs: 0.6875rem;
  --text-sm: 0.8125rem;
  --text-base: 0.9375rem;
  --text-lg: 1.125rem;
  --text-xl: 1.375rem;

  --space-1: 0.25rem;
  --space-2: 0.5rem;
  --space-3: 0.75rem;
  --space-4: 1rem;
  --space-6: 1.5rem;
  --space-8: 2rem;

  --radius-sm: 4px;
  --radius-md: 6px;
  --radius-lg: 8px;

  --shadow-sm: 0 1px 2px rgba(0,0,0,0.05);
  --shadow-md: 0 4px 6px -1px rgba(0,0,0,0.1), 0 2px 4px -1px rgba(0,0,0,0.06);
  --shadow-lg: 0 10px 15px -3px rgba(0,0,0,0.1), 0 4px 6px -2px rgba(0,0,0,0.05);

  --header-height: 52px;
  --transition-fast: 150ms ease;
  --transition-base: 200ms ease;
}

/* ==========================================
   BASE
   ========================================== */
*, *::before, *::after { margin: 0; padding: 0; box-sizing: border-box; }
body {
  font-family: var(--font-sans);
  font-size: var(--text-base);
  color: var(--color-text-primary);
  background: var(--color-surface);
  height: 100vh;
  overflow: hidden;
}
/* Ensure form controls inherit Montserrat — browsers don't apply body font to inputs/selects by default */
input, select, textarea, button { font-family: inherit; }

.screen { width: 100%; height: 100vh; }
[hidden] { display: none !important; }

/* ==========================================
   LOGIN
   ========================================== */
#login-screen {
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, var(--color-primary-dark) 0%, var(--color-primary) 50%, var(--color-primary-light) 100%);
}

.login-card {
  background: var(--color-background);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-xl);
  padding: var(--space-8);
  width: 100%;
  max-width: 380px;
}

.login-header {
  text-align: center;
  margin-bottom: var(--space-6);
}

.login-header h1 {
  font-size: var(--text-xl);
  font-weight: 700;
  color: var(--color-primary);
}

.subtitle {
  font-size: var(--text-sm);
  color: var(--color-text-secondary);
  margin-top: var(--space-1);
}

.form-group {
  margin-bottom: var(--space-4);
}

.form-group label {
  display: block;
  font-size: var(--text-sm);
  font-weight: 500;
  color: var(--color-text-secondary);
  margin-bottom: var(--space-1);
}

.form-group input {
  width: 100%;
  padding: var(--space-2) var(--space-3);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  font-family: var(--font-sans);
  font-size: var(--text-base);
  transition: border-color var(--transition-fast);
}

.form-group input:focus {
  outline: none;
  border-color: var(--color-primary-light);
  box-shadow: 0 0 0 3px rgba(45, 90, 138, 0.15);
}

.error-msg {
  color: var(--color-status-error);
  font-size: var(--text-sm);
  text-align: center;
  margin-top: var(--space-3);
}

/* ==========================================
   BUTTONS
   ========================================== */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  padding: var(--space-2) var(--space-4);
  font-family: var(--font-sans);
  font-size: var(--text-sm);
  font-weight: 500;
  border: none;
  border-radius: var(--radius-md);
  cursor: pointer;
  transition: all var(--transition-fast);
}

.btn-primary {
  background: var(--color-primary);
  color: white;
}
.btn-primary:hover { background: var(--color-primary-hover); }

.btn-ghost {
  background: transparent;
  color: var(--color-text-secondary);
}
.btn-ghost:hover { background: var(--color-gray-100); color: var(--color-text-primary); }

.btn-sm { padding: var(--space-1) var(--space-3); font-size: var(--text-xs); }

.btn-block { width: 100%; }

/* ==========================================
   HEADER
   ========================================== */
.app-header {
  height: var(--header-height);
  background: var(--color-primary);
  color: white;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 var(--space-4);
  box-shadow: var(--shadow-md);
  z-index: 100;
}

.header-left { display: flex; align-items: center; gap: var(--space-3); }
.header-right { display: flex; align-items: center; gap: var(--space-2); }

.header-title { font-size: var(--text-lg); font-weight: 600; }

.header-badge {
  font-size: var(--text-xs);
  padding: 2px 8px;
  border-radius: 10px;
  background: rgba(255,255,255,0.2);
}

.header-right .btn { color: rgba(255,255,255,0.8); }
.header-right .btn:hover { background: rgba(255,255,255,0.1); color: white; }

/* ==========================================
   LAYOUT
   ========================================== */
#app-screen { display: flex; flex-direction: column; }

.app-content {
  display: flex;
  flex: 1;
  height: calc(100vh - var(--header-height));
  overflow: hidden;
}

.panel {
  display: flex;
  flex-direction: column;
  background: var(--color-background);
  overflow: hidden;
}

.panel-map {
  width: 380px;
  min-width: 320px;
  border-right: 1px solid var(--color-border);
}

.panel-plots {
  flex: 1;
  min-width: 0;
}

.panel-header {
  padding: var(--space-3) var(--space-4);
  border-bottom: 1px solid var(--color-border);
  background: var(--color-surface);
}

.panel-header h2 {
  font-size: var(--text-sm);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--color-text-secondary);
}

/* ==========================================
   MAP
   ========================================== */
#map {
  flex: 1;
  min-height: 300px;
}

/* ==========================================
   STATION INFO
   ========================================== */
.station-info {
  padding: var(--space-4);
  border-top: 1px solid var(--color-border);
  background: var(--color-surface);
}

.info-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: var(--space-3);
}

.info-header h3 {
  font-size: var(--text-lg);
  font-weight: 600;
}

.info-badge {
  font-size: var(--text-xs);
  font-weight: 500;
  padding: 2px 8px;
  border-radius: 10px;
}

.info-badge.complete { background: #D1FAE5; color: #065F46; }
.info-badge.pending { background: #FEF3C7; color: #92400E; }
.info-badge.error { background: #FEE2E2; color: #991B1B; }
.info-badge.processing { background: #DBEAFE; color: #1E40AF; }

.info-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: var(--space-2);
  margin-bottom: var(--space-3);
}

.info-item { }
.info-label {
  display: block;
  font-size: var(--text-xs);
  color: var(--color-text-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}
.info-value {
  font-size: var(--text-sm);
  font-weight: 500;
  color: var(--color-text-primary);
}

/* Station notes */
.info-notes {
  margin-bottom: var(--space-3);
}
.info-notes-text {
  font-size: var(--text-sm);
  color: var(--color-text-secondary);
  margin-top: 2px;
  line-height: 1.5;
}

/* Date range picker */
.date-range {
  margin-bottom: var(--space-3);
}

.date-range > .info-label {
  margin-bottom: var(--space-1);
}

.date-range-inputs {
  display: flex;
  align-items: center;
  gap: var(--space-1);
}

.date-range-inputs input[type="date"] {
  flex: 1;
  min-width: 0;
  padding: var(--space-1) var(--space-2);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  font-family: var(--font-sans);
  font-size: var(--text-xs);
  color: var(--color-text-primary);
  background: var(--color-background);
}

.date-range-inputs input[type="date"]:focus {
  outline: none;
  border-color: var(--color-primary-light);
  box-shadow: 0 0 0 2px rgba(45, 90, 138, 0.15);
}

.date-sep {
  color: var(--color-text-muted);
  font-size: var(--text-xs);
}

.info-actions { display: flex; gap: var(--space-2); }

/* ==========================================
   PLOT TABS
   ========================================== */
.plot-tabs {
  display: flex;
  gap: var(--space-1);
  margin-top: var(--space-2);
  overflow-x: auto;
  overflow-y: hidden;
  scrollbar-width: none;
  -ms-overflow-style: none;
  scroll-snap-type: x proximity;
  padding-bottom: 2px;
}
.plot-tabs::-webkit-scrollbar { display: none; }
.tab { flex-shrink: 0; }

.tab {
  padding: var(--space-1) var(--space-3);
  font-family: var(--font-sans);
  font-size: var(--text-xs);
  font-weight: 500;
  color: var(--color-text-secondary);
  background: transparent;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: all var(--transition-fast);
}

.tab:hover { background: var(--color-gray-100); }

.tab.active {
  background: var(--color-primary);
  color: white;
  border-color: var(--color-primary);
}

/* ==========================================
   PLOT CONTROLS (spectrogram / gauge / geo)
   ========================================== */
.plot-controls {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  flex-wrap: wrap;
  padding: var(--space-2) var(--space-4);
  background: var(--color-surface);
  border-top: 1px solid var(--color-border);
  font-size: var(--text-sm);
  color: var(--color-text-secondary);
}

.plot-controls-label {
  font-size: var(--text-xs);
  font-weight: 600;
  color: var(--color-text-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  flex-shrink: 0;
}

.plot-controls select,
.plot-controls input[type="datetime-local"],
.plot-controls input[type="time"],
.plot-controls input[type="number"] {
  font-size: var(--text-sm);
  padding: var(--space-1) var(--space-2);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  color: var(--color-text-primary);
  background: var(--color-background);
  font-family: var(--font-sans);
}

.plot-controls select:focus,
.plot-controls input:focus {
  outline: none;
  border-color: var(--color-primary-light);
  box-shadow: 0 0 0 2px rgba(45, 90, 138, 0.12);
}

.plot-controls .btn {
  flex-shrink: 0;
}

/* ==========================================
   PLOT AREA
   ========================================== */
/* Use ID to avoid colliding with Plotly's internal .plot-container div */
#plot-container {
  flex: 1;
  position: relative;
  overflow-y: auto;
  overflow-x: hidden;
  min-height: 0;   /* required for flex children to scroll */
}

#plot-area {
  width: 100%;
  /* min-height gives Plotly's autosize a non-zero container to measure,
     while overflow-y:auto on the parent lets the uPlot legend scroll into view */
  min-height: 520px;
  padding: var(--space-4);
  box-sizing: border-box;
}

/* uPlot legend: always visible, readable font */
.u-legend {
  font-size: 0.8rem;
  padding: 0.25rem 0.5rem;
}

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  color: var(--color-text-muted);
  gap: var(--space-3);
}

.empty-state p { font-size: var(--text-sm); }

.loading {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: rgba(255,255,255,0.9);
  gap: var(--space-3);
}

.loading p { font-size: var(--text-sm); color: var(--color-text-secondary); }

.spinner {
  width: 32px;
  height: 32px;
  border: 3px solid var(--color-gray-200);
  border-top-color: var(--color-primary);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

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

/* ==========================================
   LOADING SCREEN (cold-start splash)
   ========================================== */
.loading-screen {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, var(--color-primary-dark) 0%, var(--color-primary) 50%, var(--color-primary-light) 100%);
  z-index: 999;
}

.loading-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--space-4);
  padding: var(--space-8);
}

.tensora-logo {
  width: 64px;
  height: 64px;
  background: rgba(255,255,255,0.15);
  border: 2px solid rgba(255,255,255,0.4);
  border-radius: var(--radius-lg);
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: var(--font-sans);
  font-size: 2rem;
  font-weight: 700;
  color: white;
  letter-spacing: -0.02em;
}

.spinner-lg {
  width: 48px;
  height: 48px;
  border: 4px solid rgba(255,255,255,0.25);
  border-top-color: white;
  border-radius: 50%;
  animation: spin 0.9s linear infinite;
}

.loading-title {
  font-family: var(--font-sans);
  font-size: var(--text-lg);
  font-weight: 600;
  color: white;
  margin: 0;
  letter-spacing: -0.01em;
}

.loading-sub {
  font-family: var(--font-sans);
  font-size: var(--text-sm);
  color: rgba(255,255,255,0.65);
  margin: 0;
}

/* ==========================================
   PROGRESS BAR
   ========================================== */
.progress-track {
  width: 100%;
  height: 6px;
  background: var(--color-gray-200);
  border-radius: 3px;
  overflow: hidden;
  margin-top: var(--space-2);
  margin-bottom: 4px;
}

.progress-fill {
  height: 100%;
  width: 0%;
  background: var(--color-primary);
  border-radius: 3px;
  transition: width 0.4s ease;
}

.progress-fill.indeterminate {
  width: 35% !important;
  animation: progress-slide 1.6s ease-in-out infinite;
}

@keyframes progress-slide {
  0%   { transform: translateX(-200%); }
  100% { transform: translateX(400%); }
}

.progress-label {
  font-size: var(--text-xs);
  color: var(--color-text-muted);
}

/* Processing section within station info card */
.processing-section {
  margin-top: var(--space-3);
  padding-top: var(--space-3);
  border-top: 1px solid var(--color-border);
}

.processing-section-label {
  font-size: var(--text-xs);
  font-weight: 600;
  color: var(--color-text-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin-bottom: var(--space-2);
  display: block;
}

/* ==========================================
   RESPONSIVE
   ========================================== */
@media (max-width: 900px) {
  .app-content { flex-direction: column; }
  .panel-map {
    width: 100%;
    height: 40vh;
    border-right: none;
    border-bottom: 1px solid var(--color-border);
  }
  .panel-plots { height: 60vh; }
}

/* ---- OPERA VLM legend (Leaflet control) ---- */
.opera-legend {
  background: rgba(255, 255, 255, 0.92);
  padding: 8px 10px;
  border-radius: 6px;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.2);
  font-family: 'Montserrat', sans-serif;
  font-size: 11px;
  min-width: 140px;
  pointer-events: none;
}
.opera-legend-title {
  font-weight: 600;
  margin-bottom: 4px;
  color: #1f2937;
}
.opera-legend-bar {
  height: 12px;
  /* RdYlBu diverging colormap — blue=subsidence, red=uplift */
  background: linear-gradient(to right,
    #4575b4, #74add1, #abd9e9, #e0f3f8,
    #ffffbf,
    #fee090, #fdae61, #f46d43, #d73027);
  border-radius: 2px;
  margin: 4px 0;
}
.opera-legend-labels {
  display: flex;
  justify-content: space-between;
  color: #6b7280;
  font-size: 10px;
}
.opera-legend-src {
  margin-top: 5px;
  color: #9ca3af;
  font-size: 10px;
  text-align: right;
}

/* OPERA time-window date picker (appears when InSAR layer is toggled on) */
.opera-date-control {
  background: rgba(255,255,255,0.95);
  padding: 8px 10px;
  border-radius: 6px;
  box-shadow: 0 2px 6px rgba(0,0,0,0.2);
  font-family: 'Montserrat', sans-serif;
  font-size: 11px;
  min-width: 195px;
  margin-top: 4px;
}
.opera-date-title {
  font-weight: 600;
  margin-bottom: 6px;
  color: #374151;
}
.opera-date-row {
  display: flex;
  flex-direction: column;
  gap: 5px;
}
.opera-date-row label {
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: #6b7280;
}
.opera-date-row input[type="month"] {
  font-family: inherit;
  font-size: 11px;
  border: 1px solid #d1d5db;
  border-radius: 4px;
  padding: 2px 4px;
  color: #111827;
}
.opera-date-hint {
  margin-top: 4px;
  color: #9ca3af;
  font-size: 10px;
  min-height: 12px;
}
.opera-window-select {
  width: 100%;
  font-family: 'Montserrat', sans-serif;
  font-size: 11px;
  border: 1px solid #d1d5db;
  border-radius: 4px;
  padding: 3px 5px;
  color: #111827;
  background: white;
  cursor: pointer;
}
