/* =============================================================================
   CB-CIS Design System v2.4 — Components Library
   ============================================================================= */

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

html, body {
  height: 100%;
  background: var(--bg-canvas);
}

button {
  cursor: pointer;
  font-family: var(--font-body);
  border: none;
  outline: none;
  transition: all var(--transition-fast);
  background: none;
}

input, select, textarea {
  font-family: var(--font-body);
  outline: none;
}

/* Focus ring (accessibility) */
:focus-visible {
  outline: 2px solid var(--accent-active);
  outline-offset: 2px;
}

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

/* Scrollbar styling */
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: var(--scrollbar-track); }
::-webkit-scrollbar-thumb { background: var(--scrollbar-thumb); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--scrollbar-thumb-hover); }

/* ---------------------------------------------------------------------------
   Glass Panel
   --------------------------------------------------------------------------- */
.glass-panel {
  background: var(--surface-glass);
  backdrop-filter: var(--backdrop-blur);
  -webkit-backdrop-filter: var(--backdrop-blur);
  border: 1px solid var(--border-hairline);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-card);
}

.glass-panel-elevated {
  background: var(--surface-glass-elevated);
  backdrop-filter: var(--backdrop-blur);
  -webkit-backdrop-filter: var(--backdrop-blur);
  border: 1px solid var(--border-hairline);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-floating);
}

/* ---------------------------------------------------------------------------
   Buttons
   --------------------------------------------------------------------------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  font-size: var(--text-body-sm);
  font-weight: var(--weight-semibold);
  border-radius: var(--radius-md);
  padding: var(--space-2) var(--space-5);
  height: 40px;
  transition: all var(--transition-fast);
  white-space: nowrap;
  text-decoration: none;
}

.btn-primary {
  background: var(--brand-primary);
  color: var(--text-inverse);
  border: none;
}
.btn-primary:hover {
  background: var(--brand-primary-hover);
  box-shadow: var(--shadow-card);
}

.btn-accent {
  background: var(--accent-active);
  color: #FFFFFF;
  border: none;
}
.btn-accent:hover {
  background: var(--accent-active-hover);
  box-shadow: var(--shadow-card);
}

.btn-secondary {
  background: var(--surface-glass);
  color: var(--text-primary);
  border: 1px solid var(--border-hairline);
}
.btn-secondary:hover {
  background: var(--bg-canvas-alt);
  border-color: var(--brand-primary);
  color: var(--brand-primary);
}

.btn-ghost {
  background: transparent;
  color: var(--text-secondary);
  padding: var(--space-2);
}
.btn-ghost:hover {
  background: var(--bg-canvas-alt);
  color: var(--text-primary);
}

.btn-icon {
  width: 40px;
  height: 40px;
  padding: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--radius-md);
  background: var(--surface-glass);
  border: 1px solid var(--border-hairline);
  color: var(--text-secondary);
  transition: all var(--transition-fast);
}
.btn-icon:hover {
  background: var(--bg-canvas-alt);
  color: var(--text-primary);
  border-color: var(--brand-primary);
}

.btn-sm {
  height: 32px;
  font-size: var(--text-small);
  padding: var(--space-1) var(--space-3);
}

.btn-lg {
  height: 48px;
  font-size: var(--text-body);
  padding: var(--space-3) var(--space-8);
  border-radius: var(--radius-lg);
}

/* ---------------------------------------------------------------------------
   KPI Card (Homepage)
   --------------------------------------------------------------------------- */
.kpi-card {
  display: flex;
  align-items: center;
  gap: var(--space-4);
  padding: var(--space-5) var(--space-6);
  background: var(--surface-glass);
  backdrop-filter: var(--backdrop-blur);
  border: 1px solid var(--border-hairline);
  border-radius: var(--radius-xl);
  box-shadow: var(--shadow-card);
  transition: all var(--transition-mid);
}

.kpi-card:hover {
  box-shadow: var(--shadow-floating);
  border-color: var(--brand-primary);
  transform: translateY(-2px);
}

.kpi-card__icon {
  width: 48px;
  height: 48px;
  border-radius: var(--radius-lg);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  background: var(--brand-primary-light);
  color: var(--brand-primary);
}

.kpi-card__content {
  flex: 1;
  min-width: 0;
}

.kpi-card__value {
  font-family: var(--font-display);
  font-size: var(--text-h2);
  font-weight: var(--weight-extrabold);
  line-height: 1;
  color: var(--text-primary);
  font-variant-numeric: tabular-nums;
}

.kpi-card__label {
  font-size: var(--text-small);
  color: var(--text-secondary);
  margin-top: var(--space-1);
  line-height: var(--leading-snug);
}

.kpi-card__link {
  font-size: var(--text-small);
  color: var(--brand-primary);
  font-weight: var(--weight-medium);
  margin-top: var(--space-1);
  display: inline-flex;
  align-items: center;
  gap: var(--space-1);
}

/* ---------------------------------------------------------------------------
   Module Card (Homepage)
   --------------------------------------------------------------------------- */
.module-card {
  padding: var(--space-6);
  background: var(--surface-glass);
  border: 1px solid var(--border-hairline);
  border-radius: var(--radius-lg);
  transition: all var(--transition-mid);
}

.module-card:hover {
  border-color: var(--brand-primary);
  box-shadow: var(--shadow-floating);
  transform: translateY(-2px);
}

.module-card__icon {
  width: 40px;
  height: 40px;
  border-radius: var(--radius-md);
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: var(--space-4);
}

.module-card__title {
  font-size: var(--text-body);
  font-weight: var(--weight-semibold);
  color: var(--text-primary);
  margin-bottom: var(--space-2);
}

.module-card__desc {
  font-size: var(--text-small);
  color: var(--text-secondary);
  line-height: var(--leading-relaxed);
  margin-bottom: var(--space-3);
}

.module-card__link {
  font-size: var(--text-small);
  font-weight: var(--weight-semibold);
  color: var(--brand-primary);
  display: inline-flex;
  align-items: center;
  gap: var(--space-1);
}

/* ---------------------------------------------------------------------------
   Search Bar
   --------------------------------------------------------------------------- */
.search-bar {
  position: relative;
  flex: 1;
  max-width: 420px;
}

.search-bar__input {
  width: 100%;
  height: 40px;
  padding: 0 2.5rem 0 2.75rem;
  background: var(--surface-glass);
  border: 1px solid var(--border-hairline);
  border-radius: var(--radius-full);
  color: var(--text-primary);
  font-size: var(--text-body-sm);
  transition: all var(--transition-mid);
}

.search-bar__input::placeholder {
  color: var(--text-muted);
}

.search-bar__input:focus {
  border-color: var(--brand-primary);
  box-shadow: 0 0 0 3px var(--brand-primary-light);
}

.search-bar__icon {
  position: absolute;
  left: var(--space-4);
  top: 50%;
  transform: translateY(-50%);
  color: var(--text-muted);
  pointer-events: none;
  width: 16px;
  height: 16px;
}

.search-bar__clear {
  position: absolute;
  right: var(--space-2);
  top: 50%;
  transform: translateY(-50%);
  width: 28px;
  height: 28px;
  border-radius: var(--radius-full);
  display: none;
  align-items: center;
  justify-content: center;
  color: var(--text-muted);
  cursor: pointer;
}

.search-bar__clear:hover {
  background: var(--bg-canvas-alt);
  color: var(--text-primary);
}

/* ---------------------------------------------------------------------------
   Search Results Dropdown
   --------------------------------------------------------------------------- */
.search-dropdown {
  position: absolute;
  top: calc(100% + var(--space-2));
  left: 0;
  right: 0;
  background: var(--surface-glass-elevated);
  backdrop-filter: var(--backdrop-blur);
  border: 1px solid var(--border-hairline);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-elevated);
  max-height: 320px;
  overflow-y: auto;
  z-index: var(--z-dropdown);
  display: none;
}

.search-dropdown.visible { display: block; }

.search-dropdown__item {
  padding: var(--space-3) var(--space-4);
  cursor: pointer;
  border-bottom: 1px solid var(--border-hairline);
  transition: background var(--transition-fast);
}

.search-dropdown__item:last-child { border-bottom: none; }

.search-dropdown__item:hover {
  background: var(--brand-primary-light);
}

.search-dropdown__name {
  font-size: var(--text-body-sm);
  font-weight: var(--weight-semibold);
  color: var(--text-primary);
}

.search-dropdown__meta {
  font-size: var(--text-small);
  color: var(--text-muted);
  margin-top: 2px;
}

.search-dropdown__empty {
  padding: var(--space-6);
  text-align: center;
  color: var(--text-muted);
  font-size: var(--text-body-sm);
}

/* ---------------------------------------------------------------------------
   Badge
   --------------------------------------------------------------------------- */
.badge {
  display: inline-flex;
  align-items: center;
  gap: var(--space-1);
  padding: 2px var(--space-2);
  font-size: var(--text-micro);
  font-weight: var(--weight-semibold);
  border-radius: var(--radius-sm);
  line-height: var(--leading-snug);
}

.badge-brand {
  background: var(--brand-primary);
  color: var(--text-inverse);
}

.badge-accent {
  background: var(--accent-light);
  color: var(--accent-active);
}

.badge-success {
  background: var(--status-success-bg);
  color: var(--status-success);
}

.badge-info {
  background: var(--status-info-bg);
  color: var(--status-info);
}

.badge-outline {
  background: transparent;
  border: 1px solid var(--border-hairline);
  color: var(--text-secondary);
}

/* ---------------------------------------------------------------------------
   Data Value Display
   --------------------------------------------------------------------------- */
.data-value {
  display: flex;
  flex-direction: column;
}

.data-value__number {
  font-family: var(--font-display);
  font-size: var(--text-h3);
  font-weight: var(--weight-bold);
  color: var(--text-primary);
  font-variant-numeric: tabular-nums;
  line-height: 1;
}

.data-value__unit {
  font-size: var(--text-small);
  color: var(--text-secondary);
  margin-top: 2px;
}

.data-value__label {
  font-size: var(--text-small);
  color: var(--text-muted);
  margin-top: var(--space-1);
}

/* Compact data row */
.data-row {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  padding: var(--space-2) 0;
  border-bottom: 1px solid var(--border-hairline);
  font-size: var(--text-body-sm);
}

.data-row:last-child { border-bottom: none; }

.data-row__key {
  color: var(--text-secondary);
  font-weight: var(--weight-medium);
  flex-shrink: 0;
}

.data-row__val {
  font-family: var(--font-mono);
  font-size: var(--text-small);
  color: var(--text-primary);
  text-align: right;
  font-variant-numeric: tabular-nums;
}

.data-row__val--highlight {
  font-family: var(--font-display);
  font-size: var(--text-body-sm);
  font-weight: var(--weight-bold);
  color: var(--brand-primary);
}

/* ---------------------------------------------------------------------------
   Layer Toggle (SIG App)
   --------------------------------------------------------------------------- */
.layer-toggle {
  position: relative;
  flex-shrink: 0;
}

.layer-toggle input[type="checkbox"] {
  opacity: 0;
  width: 0;
  height: 0;
  position: absolute;
}

.toggle-track {
  display: block;
  width: 36px;
  height: 20px;
  background: var(--border-hairline);
  border-radius: 10px;
  cursor: pointer;
  transition: background var(--transition-mid);
  position: relative;
}

.toggle-track::after {
  content: '';
  position: absolute;
  left: 2px;
  top: 2px;
  width: 16px;
  height: 16px;
  background: white;
  border-radius: 50%;
  box-shadow: var(--shadow-card);
  transition: left var(--transition-mid);
}

.layer-toggle input:checked + .toggle-track {
  background: var(--brand-primary);
}

.layer-toggle input:checked + .toggle-track::after {
  left: 18px;
}

/* Opacity slider */
.opacity-slider {
  -webkit-appearance: none;
  height: 4px;
  background: var(--border-hairline);
  border-radius: 2px;
  outline: none;
  cursor: pointer;
  flex: 1;
}

.opacity-slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: var(--brand-primary);
  box-shadow: var(--shadow-card);
  cursor: pointer;
  border: 2px solid white;
}

/* ---------------------------------------------------------------------------
   Basemap Thumbnails
   --------------------------------------------------------------------------- */
.basemap-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: var(--space-2);
  padding: var(--space-3);
}

.basemap-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--space-1);
  padding: var(--space-2);
  border: 2px solid var(--border-hairline);
  border-radius: var(--radius-md);
  background: var(--surface-glass);
  font-size: var(--text-micro);
  color: var(--text-secondary);
  font-weight: var(--weight-medium);
  transition: all var(--transition-fast);
  cursor: pointer;
}

.basemap-btn:hover {
  border-color: var(--brand-primary);
  color: var(--brand-primary);
}

.basemap-btn.active {
  border-color: var(--accent-active);
  color: var(--accent-active);
  font-weight: var(--weight-bold);
  box-shadow: 0 0 0 1px var(--accent-active);
}

.basemap-thumb {
  width: 100%;
  aspect-ratio: 16/9;
  border-radius: var(--radius-sm);
  background: var(--bg-canvas-alt);
  overflow: hidden;
}

/* Thumbnails by type */
.basemap-thumb-relief { background: linear-gradient(135deg, #d7ccc8 0%, #a1887f 50%, #5d4037 100%); }
.basemap-thumb-topo { background: linear-gradient(135deg, #f5deb3 0%, #c8a96e 50%, #8b7355 100%); }
.basemap-thumb-satellite { background: linear-gradient(135deg, #1b2838 0%, #2d5a27 40%, #1a3a1a 100%); }
.basemap-thumb-osm { background: linear-gradient(135deg, #e8f5e9 0%, #bbdefb 50%, #c8e6c9 100%); }

/* ---------------------------------------------------------------------------
   Legend
   --------------------------------------------------------------------------- */
.legend-section {
  padding: var(--space-3) var(--space-4);
}

.legend-title {
  font-size: var(--text-micro);
  font-weight: var(--weight-bold);
  color: var(--text-secondary);
  letter-spacing: var(--tracking-wider);
  text-transform: uppercase;
  margin-bottom: var(--space-2);
}

.legend-gradient {
  height: 14px;
  border-radius: var(--radius-sm);
  margin-bottom: var(--space-1);
}

.legend-labels {
  display: flex;
  justify-content: space-between;
  font-size: var(--text-micro);
  color: var(--text-muted);
}

.legend-items {
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  margin-top: var(--space-2);
}

.legend-item {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  font-size: var(--text-small);
  color: var(--text-secondary);
}

.legend-color {
  width: 20px;
  height: 10px;
  border-radius: 2px;
  flex-shrink: 0;
}

/* ---------------------------------------------------------------------------
   Tabs (Catchment Detail Panel)
   --------------------------------------------------------------------------- */
.tabs {
  display: flex;
  border-bottom: 1px solid var(--border-hairline);
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
}

.tabs::-webkit-scrollbar { display: none; }

.tab {
  padding: var(--space-3) var(--space-4);
  font-size: var(--text-small);
  font-weight: var(--weight-medium);
  color: var(--text-muted);
  border-bottom: 2px solid transparent;
  white-space: nowrap;
  cursor: pointer;
  transition: all var(--transition-fast);
  flex-shrink: 0;
}

.tab:hover {
  color: var(--text-primary);
}

.tab.active {
  color: var(--brand-primary);
  border-bottom-color: var(--brand-primary);
  font-weight: var(--weight-semibold);
}

.tab-panel {
  display: none;
  padding: var(--space-4);
}

.tab-panel.active {
  display: block;
}

/* ---------------------------------------------------------------------------
   Chart Container
   --------------------------------------------------------------------------- */
.chart-container {
  background: var(--surface-glass);
  border: 1px solid var(--border-hairline);
  border-radius: var(--radius-md);
  padding: var(--space-4);
  margin: var(--space-3) 0;
}

.chart-container__title {
  font-size: var(--text-small);
  font-weight: var(--weight-semibold);
  color: var(--text-secondary);
  margin-bottom: var(--space-3);
}

/* ---------------------------------------------------------------------------
   Progress / Bar Indicator
   --------------------------------------------------------------------------- */
.bar-indicator {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  flex: 1;
}

.bar-track {
  flex: 1;
  height: 6px;
  background: var(--border-hairline);
  border-radius: 3px;
  overflow: hidden;
}

.bar-fill {
  height: 100%;
  border-radius: 3px;
  transition: width var(--transition-mid);
}

.bar-fill--forest { background: var(--earth-forest); }
.bar-fill--shrub { background: var(--earth-shrub); }
.bar-fill--grass { background: var(--earth-grass); }
.bar-fill--crop { background: var(--earth-crop); }
.bar-fill--water { background: var(--earth-water); }
.bar-fill--built { background: var(--earth-built); }
.bar-fill--brand { background: var(--brand-primary); }

.bar-label {
  font-family: var(--font-mono);
  font-size: var(--text-small);
  color: var(--text-primary);
  min-width: 55px;
  text-align: right;
  font-variant-numeric: tabular-nums;
}

/* ---------------------------------------------------------------------------
   Toast Notification
   --------------------------------------------------------------------------- */
.toast {
  position: fixed;
  bottom: var(--space-6);
  left: 50%;
  transform: translateX(-50%) translateY(120%);
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-3) var(--space-5);
  background: var(--surface-glass-elevated);
  backdrop-filter: var(--backdrop-blur);
  border: 1px solid var(--border-hairline);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-elevated);
  font-size: var(--text-body-sm);
  color: var(--text-primary);
  z-index: var(--z-toast);
  transition: transform var(--transition-spring);
}

.toast.visible {
  transform: translateX(-50%) translateY(0);
}

.toast--success { border-left: 4px solid var(--status-success); }
.toast--warning { border-left: 4px solid var(--status-warning); }
.toast--error { border-left: 4px solid var(--status-danger); }
.toast--info { border-left: 4px solid var(--status-info); }

/* ---------------------------------------------------------------------------
   Loading / Empty States
   --------------------------------------------------------------------------- */
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: var(--space-4);
  padding: var(--space-12) var(--space-6);
  text-align: center;
  color: var(--text-muted);
}

.empty-state__icon {
  width: 64px;
  height: 64px;
  opacity: 0.3;
}

.empty-state__text {
  font-size: var(--text-body-sm);
  line-height: var(--leading-relaxed);
  max-width: 300px;
}

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

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

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

/* ---------------------------------------------------------------------------
   Utilities
   --------------------------------------------------------------------------- */
.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0,0,0,0);
  white-space: nowrap;
  border-width: 0;
}

.text-truncate {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

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

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0,0,0,0);
  border: 0;
}
