/*
 * style.css — COEXIST dashboard styles
 *
 * Design spec from DESIGN_PLAN.md Phase 6.
 * Colours, typography, and spacing defined as CSS custom properties so
 * changes flow through everywhere from one place.
 */

/* ---------------------------------------------------------------------------
   Custom properties (design tokens)
   --------------------------------------------------------------------------- */

:root {
  /* Colour palette */
  --primary:        #1B4965;
  --primary-hover:  #153B52;
  --accent:         #5FA8D3;
  --surface:        #F7F9FB;
  --card:           #FFFFFF;
  --text:           #1E293B;
  --text-muted:     #64748B;
  --border:         #E2E8F0;
  --success:        #16A34A;
  --warning:        #D97706;
  --error:          #DC2626;
  --selected:       #FF6B00;

  /* Typography */
  --font: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  --font-size-base: 14px;
  --font-size-h2:   18px;
  --font-size-h3:   15px;
  --font-size-sm:   12px;

  /* Spacing (8px grid) */
  --sp-1: 8px;
  --sp-2: 16px;
  --sp-3: 24px;
  --sp-4: 32px;

  /* Borders */
  --radius:       6px;
  --radius-lg:    8px;
  --shadow-card:  0 1px 3px rgba(0,0,0,0.06);
  --shadow-modal: 0 4px 24px rgba(0,0,0,0.14);
}

/* ---------------------------------------------------------------------------
   Reset & base
   --------------------------------------------------------------------------- */

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  height: 100%;
  font-family: var(--font);
  font-size: var(--font-size-base);
  color: var(--text);
  background: var(--surface);
  line-height: 1.5;
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

h2 { font-size: var(--font-size-h2); font-weight: 600; }
h3 { font-size: var(--font-size-h3); font-weight: 600; }

/* ---------------------------------------------------------------------------
   Login page
   --------------------------------------------------------------------------- */

.login-page {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  background: var(--surface);
}

.login-card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-modal);
  padding: var(--sp-4);
  width: 360px;
  max-width: 95vw;
}

.login-logo {
  text-align: center;
  margin-bottom: var(--sp-3);
}
.logo-text {
  display: block;
  font-size: 22px;
  font-weight: 700;
  letter-spacing: 0.08em;
  color: var(--primary);
}
.logo-sub {
  display: block;
  font-size: var(--font-size-sm);
  color: var(--text-muted);
  margin-top: 2px;
}

.login-note {
  margin-top: var(--sp-2);
  font-size: var(--font-size-sm);
  color: var(--text-muted);
  text-align: center;
}

/* ---------------------------------------------------------------------------
   Buttons
   --------------------------------------------------------------------------- */

.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: var(--sp-1) var(--sp-2);
  border: none;
  border-radius: var(--radius);
  font-size: var(--font-size-base);
  font-family: var(--font);
  cursor: pointer;
  transition: background 0.15s, opacity 0.15s;
  white-space: nowrap;
  text-decoration: none;
}
.btn:hover { text-decoration: none; }

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

.btn-secondary {
  background: #fff;
  color: var(--primary);
  border: 1px solid var(--primary);
}
.btn-secondary:hover { background: var(--surface); }

.btn-danger {
  background: var(--error);
  color: #fff;
}
.btn-danger:hover { opacity: 0.87; }

.btn-sm { padding: 6px 12px; font-size: var(--font-size-sm); }
.btn-full { width: 100%; justify-content: center; }

/* ---------------------------------------------------------------------------
   Forms
   --------------------------------------------------------------------------- */

.form-group {
  margin-bottom: var(--sp-2);
}
.form-group label {
  display: block;
  font-size: var(--font-size-sm);
  font-weight: 600;
  color: var(--text-muted);
  margin-bottom: 4px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.form-input,
.form-group input[type="text"],
.form-group input[type="email"],
.form-group input[type="password"],
.form-group select,
.form-group textarea {
  width: 100%;
  padding: var(--sp-1) 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: var(--font-size-base);
  font-family: var(--font);
  color: var(--text);
  background: #fff;
  transition: border-color 0.15s, box-shadow 0.15s;
}
.form-input:focus,
.form-group input:focus,
.form-group select:focus,
.form-group textarea:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 2px rgba(95,168,211,0.25);
}
.field-error {
  display: block;
  color: var(--error);
  font-size: var(--font-size-sm);
  margin-top: 4px;
}

/* ---------------------------------------------------------------------------
   Banners
   --------------------------------------------------------------------------- */

.banner {
  padding: var(--sp-1) var(--sp-2);
  border-radius: var(--radius);
  margin-bottom: var(--sp-2);
  font-size: var(--font-size-sm);
}
.banner-error   { background: #FEF2F2; color: var(--error);   border: 1px solid #FECACA; }
.banner-success { background: #F0FDF4; color: var(--success); border: 1px solid #BBF7D0; }
.banner-warning { background: #FFFBEB; color: var(--warning); border: 1px solid #FDE68A; }

/* ---------------------------------------------------------------------------
   Status badges
   --------------------------------------------------------------------------- */

.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 99px;
  font-size: var(--font-size-sm);
  font-weight: 600;
}
.badge-complete  { background: var(--success); color: #fff; }
.badge-valid     { background: transparent; color: var(--success); border: 1px solid var(--success); }
.badge-invalid   { background: var(--error);   color: #fff; }
.badge-pending   { background: var(--warning); color: #fff; }
.badge-processing{ background: var(--accent);  color: #fff; }
.badge-rejected  { background: var(--error);   color: #fff; }
.badge-removed   { background: var(--text-muted); color: #fff; }
.badge-uploaded   { background: var(--accent); color: #fff; }
.badge-processed  { background: var(--success); color: #fff; }

/* ---------------------------------------------------------------------------
   Cards
   --------------------------------------------------------------------------- */

.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-card);
  padding: var(--sp-2);
}

/* ---------------------------------------------------------------------------
   Tables
   --------------------------------------------------------------------------- */

.table-wrap { overflow-x: auto; }

table { width: 100%; border-collapse: collapse; }
thead tr { background: var(--surface); }
th {
  text-align: left;
  padding: var(--sp-1) var(--sp-2);
  font-size: var(--font-size-sm);
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  border-bottom: 1px solid var(--border);
}
td {
  padding: var(--sp-1) var(--sp-2);
  border-bottom: 1px solid var(--border);
  font-size: var(--font-size-base);
}
tr:hover td { background: var(--surface); }

/* ---------------------------------------------------------------------------
   Header / nav
   --------------------------------------------------------------------------- */

.app-header {
  height: 48px;
  background: var(--primary);
  color: #fff;
  display: flex;
  align-items: center;
  padding: 0 var(--sp-2);
  gap: var(--sp-2);
  flex-shrink: 0;
  z-index: 900;
}

.header-logo {
  font-weight: 700;
  font-size: 15px;
  letter-spacing: 0.08em;
  color: #fff;
  white-space: nowrap;
}

.header-controls {
  display: flex;
  align-items: center;
  gap: var(--sp-1);
  flex: 1;
}

.header-select {
  background: rgba(255,255,255,0.15);
  border: 1px solid rgba(255,255,255,0.3);
  color: #fff;
  border-radius: var(--radius);
  padding: 4px 8px;
  font-size: var(--font-size-sm);
  font-family: var(--font);
  cursor: pointer;
}
.header-select option { background: var(--primary); }

.header-btn {
  background: rgba(255,255,255,0.15);
  border: 1px solid rgba(255,255,255,0.25);
  color: #fff;
  border-radius: var(--radius);
  padding: 4px 10px;
  font-size: var(--font-size-sm);
  cursor: pointer;
  transition: background 0.15s;
}
.header-btn:hover { background: rgba(255,255,255,0.25); }
.header-btn.active {
  background: rgba(255,255,255,0.35);
  border-color: rgba(255,255,255,0.6);
}

.header-right {
  display: flex;
  align-items: center;
  gap: var(--sp-1);
  margin-left: auto;
}

/* ---------------------------------------------------------------------------
   Map page layout
   --------------------------------------------------------------------------- */

.app-body {
  display: flex;
  height: calc(100vh - 48px);
  overflow: hidden;
}

/* Map fills the left portion */
#map {
  flex: 3;
  min-width: 0;
  position: relative;
}

/* Floating selection banner */
#selection-banner {
  position: absolute;
  top: var(--sp-1);
  left: 50%;
  transform: translateX(-50%);
  z-index: 500;
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 99px;
  padding: 4px 14px;
  font-size: var(--font-size-sm);
  box-shadow: 0 2px 8px rgba(0,0,0,0.12);
  white-space: nowrap;
  pointer-events: none;
}

/* Shift-drag hint */
.shift-hint {
  position: absolute;
  bottom: var(--sp-1);
  left: 50%;
  transform: translateX(-50%);
  z-index: 500;
  background: rgba(0,0,0,0.55);
  color: #fff;
  border-radius: var(--radius);
  padding: 3px 10px;
  font-size: var(--font-size-sm);
  pointer-events: none;
}

/* Legend */
#map-legend {
  position: absolute;
  bottom: 32px;
  right: var(--sp-1);
  z-index: 500;
  background: rgba(255,255,255,0.92);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: var(--sp-1);
  font-size: var(--font-size-sm);
  min-width: 120px;
}
.legend-title { font-weight: 600; margin-bottom: 4px; font-size: var(--font-size-sm); }
.legend-row { display: flex; align-items: center; gap: 6px; margin-top: 3px; }
.legend-swatch { width: 14px; height: 14px; border-radius: 2px; flex-shrink: 0; }

/* Resize handle between map and stats panel */
.panel-resizer {
  width: 6px;
  cursor: col-resize;
  background: var(--border);
  flex-shrink: 0;
  position: relative;
  z-index: 10;
  transition: background 0.15s;
}
.panel-resizer:hover,
.panel-resizer.active {
  background: var(--accent);
}

/* Stats panel */
#stats-panel {
  flex: 2;
  min-width: 250px;
  max-width: 50vw;
  background: var(--card);
  border-left: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.stats-header {
  padding: var(--sp-2);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.stats-title {
  font-size: var(--font-size-h2);
  font-weight: 600;
}
.stats-subtitle {
  font-size: var(--font-size-sm);
  color: var(--text-muted);
  margin-top: 2px;
}

#charts-container {
  flex: 1;
  overflow-y: auto;
  padding: var(--sp-2);
}

/* Stats-panel tabs */
.stats-tabs {
  display: flex;
  border-bottom: 2px solid var(--border);
  padding: 0 var(--sp-2);
  gap: 0;
  flex-shrink: 0;
  position: sticky;
  top: 0;
  background: var(--card);
  z-index: 10;
  margin: calc(-1 * var(--sp-2)) calc(-1 * var(--sp-2)) 0;
  padding-top: 4px;
}
.stats-tab-btn {
  padding: 6px var(--sp-1);
  background: none;
  border: none;
  border-bottom: 3px solid transparent;
  font-size: var(--font-size-sm);
  font-family: var(--font);
  cursor: pointer;
  color: var(--text-muted);
  margin-bottom: -2px;
  transition: color 0.15s, border-color 0.15s;
  white-space: nowrap;
}
.stats-tab-btn:hover { color: var(--text); }
.stats-tab-btn.active {
  color: var(--primary);
  border-bottom-color: var(--primary);
  font-weight: 600;
}
.stats-tab-pane { display: none; }
.stats-tab-pane.active { display: block; padding-top: var(--sp-2); }

/* Individual chart sections */
.chart-section {
  margin-bottom: var(--sp-2);
  padding: var(--sp-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--card);
  position: relative;
  z-index: 1;
}
.chart-section h3 {
  margin-bottom: var(--sp-1);
  display: flex;
  align-items: baseline;
  gap: 6px;
}
.n-label {
  font-size: var(--font-size-sm);
  color: var(--text-muted);
  font-weight: 400;
}
.no-data {
  color: var(--text-muted);
  font-size: var(--font-size-sm);
  font-style: italic;
  padding: var(--sp-1) 0;
}

/* Stacked percentage bar sections */
.stacked-bar-section {
  margin-bottom: var(--sp-1);
  padding: var(--sp-1) var(--sp-2);
}
.stacked-bar-section h4 {
  font-size: var(--font-size-sm);
  font-weight: 600;
  margin-bottom: 4px;
  display: flex;
  align-items: baseline;
  gap: 6px;
}
.stacked-bar-track {
  display: flex;
  height: 28px;
  border-radius: 4px;
  overflow: hidden;
  background: var(--border);
}
.stacked-bar-seg {
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 11px;
  font-weight: 600;
  color: #fff;
  white-space: nowrap;
  overflow: hidden;
  transition: flex-basis 0.3s ease;
  position: relative;
  cursor: default;
}
.stacked-bar-seg span {
  padding: 0 4px;
  text-shadow: 0 1px 2px rgba(0,0,0,0.3);
}
.stacked-bar-legend {
  display: flex;
  flex-wrap: wrap;
  gap: 6px 12px;
  margin-top: 4px;
}
.stacked-bar-legend-item {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 11px;
  color: var(--text-muted);
}
.stacked-bar-legend-swatch {
  width: 10px;
  height: 10px;
  border-radius: 2px;
  flex-shrink: 0;
}

/* Spatial profile section */
.spatial-profile-row {
  display: flex;
  gap: var(--sp-2);
  margin-top: var(--sp-1);
}
.sp-box { flex: 1; text-align: center; }
.sp-value { font-size: 22px; font-weight: 700; }
.sp-label { font-size: var(--font-size-sm); color: var(--text-muted); margin-top: 2px; }
.sp-positive { color: var(--error); }
.sp-negative { color: var(--primary); }
.sp-neutral  { color: var(--text); }

/* Loading overlay */
#loading-overlay {
  display: none;
  position: absolute;
  inset: 0;
  background: rgba(255,255,255,0.7);
  z-index: 600;
  align-items: center;
  justify-content: center;
  font-size: 15px;
  color: var(--text-muted);
}
#loading-overlay.active { display: flex; }

/* Location search */
#location-search {
  position: relative;
}
#location-input {
  background: rgba(255,255,255,0.15);
  border: 1px solid rgba(255,255,255,0.3);
  color: #fff;
  border-radius: var(--radius);
  padding: 4px 8px;
  font-size: var(--font-size-sm);
  font-family: var(--font);
  width: 160px;
}
#location-input::placeholder { color: rgba(255,255,255,0.6); }
#location-results {
  display: none;
  position: absolute;
  top: 100%;
  left: 0;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  min-width: 260px;
  box-shadow: var(--shadow-modal);
  z-index: 1000;
  max-height: 220px;
  overflow-y: auto;
}
#location-results.open { display: block; }
.loc-result {
  padding: 8px var(--sp-2);
  cursor: pointer;
  font-size: var(--font-size-sm);
  border-bottom: 1px solid var(--border);
  color: var(--text);
}
.loc-result:hover { background: var(--surface); }
.loc-none { padding: 8px var(--sp-2); font-size: var(--font-size-sm); color: var(--text-muted); }

/* ---------------------------------------------------------------------------
   Upload page layout
   --------------------------------------------------------------------------- */

.upload-page {
  max-width: 720px;
  margin: 0 auto;
  padding: var(--sp-3) var(--sp-2);
}

.upload-page h2 { margin-bottom: var(--sp-2); }

/* Drop zone */
.drop-zone {
  border: 2px dashed var(--border);
  border-radius: var(--radius-lg);
  background: var(--surface);
  padding: var(--sp-4);
  text-align: center;
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s;
}
.drop-zone.dragover {
  border-color: var(--accent);
  background: rgba(95,168,211,0.08);
}
.drop-zone-text { color: var(--text-muted); margin-bottom: var(--sp-2); }
.drop-zone-text strong { display: block; font-size: 15px; color: var(--text); margin-bottom: 4px; }

/* Validation results */
.validation-card {
  margin-top: var(--sp-2);
}
.validation-header {
  display: flex;
  align-items: center;
  gap: var(--sp-1);
  padding: var(--sp-1) var(--sp-2);
  border-radius: var(--radius) var(--radius) 0 0;
  font-weight: 600;
}
.validation-header.valid   { background: #F0FDF4; color: var(--success); border: 1px solid #BBF7D0; }
.validation-header.invalid { background: #FEF2F2; color: var(--error);   border: 1px solid #FECACA; }
.validation-body {
  padding: var(--sp-2);
  border: 1px solid var(--border);
  border-top: none;
  border-radius: 0 0 var(--radius) var(--radius);
}

.error-group { margin-bottom: var(--sp-2); }
.error-group-title {
  font-weight: 600;
  font-size: var(--font-size-sm);
  color: var(--text-muted);
  margin-bottom: var(--sp-1);
  text-transform: uppercase;
}
.error-item {
  padding: 6px var(--sp-1);
  background: var(--surface);
  border-left: 3px solid var(--error);
  border-radius: 0 var(--radius) var(--radius) 0;
  margin-bottom: 4px;
  font-size: var(--font-size-sm);
}
.error-row-label { color: var(--text-muted); }

/* ---------------------------------------------------------------------------
   Admin panel page
   --------------------------------------------------------------------------- */

.admin-page {
  max-width: 960px;
  margin: 0 auto;
  padding: var(--sp-3) var(--sp-2);
}
.admin-page h2 { margin-bottom: var(--sp-2); }

/* Tabs */
.tabs {
  display: flex;
  border-bottom: 2px solid var(--border);
  margin-bottom: var(--sp-2);
  gap: 0;
}
.tab-btn {
  padding: var(--sp-1) var(--sp-2);
  background: none;
  border: none;
  border-bottom: 3px solid transparent;
  font-size: var(--font-size-base);
  font-family: var(--font);
  cursor: pointer;
  color: var(--text-muted);
  margin-bottom: -2px;
  transition: color 0.15s, border-color 0.15s;
}
.tab-btn:hover { color: var(--text); }
.tab-btn.active {
  color: var(--primary);
  border-bottom-color: var(--primary);
  font-weight: 600;
}

.tab-pane { display: none; }
.tab-pane.active { display: block; }

/* Actions bar */
.actions-bar {
  display: flex;
  gap: var(--sp-1);
  margin-top: var(--sp-2);
  flex-wrap: wrap;
}

/* Expandable upload detail panel */
.upload-detail {
  padding: var(--sp-2);
  background: rgba(0,0,0,0.02);
  border-top: 1px solid var(--border);
  font-size: var(--font-size-sm);
  line-height: 1.6;
}
.upload-detail dt { font-weight: 600; color: var(--text-muted); margin-top: var(--sp-1); }
.upload-detail dt:first-child { margin-top: 0; }
.upload-detail dd { margin: 0 0 0 0; }
.upload-detail .log-block {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 8px 12px;
  font-family: monospace;
  font-size: 11px;
  white-space: pre-wrap;
  max-height: 160px;
  overflow-y: auto;
  margin-top: 4px;
}
.upload-detail .stat-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
  gap: 8px;
  margin-top: 6px;
}
.upload-detail .stat-item {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 8px 12px;
  text-align: center;
}
.upload-detail .stat-item .stat-value {
  font-size: 20px;
  font-weight: 700;
  color: var(--primary);
}
.upload-detail .stat-item .stat-label {
  font-size: 11px;
  color: var(--text-muted);
}

/* Toggle arrow on expandable rows */
.expand-toggle {
  cursor: pointer;
  user-select: none;
  transition: transform 0.15s;
  display: inline-block;
}
.expand-toggle.open { transform: rotate(90deg); }

/* Processing button spinner state */
.btn-processing {
  pointer-events: none;
  opacity: 0.7;
  position: relative;
}
.btn-processing::after {
  content: '';
  display: inline-block;
  width: 12px;
  height: 12px;
  border: 2px solid rgba(255,255,255,0.3);
  border-top-color: #fff;
  border-radius: 50%;
  animation: btn-spin 0.6s linear infinite;
  margin-left: 6px;
  vertical-align: middle;
}
@keyframes btn-spin {
  to { transform: rotate(360deg); }
}

/* Inline confirmation form (reject / remove) */
.inline-confirm {
  padding: var(--sp-1) var(--sp-2);
  background: rgba(0,0,0,0.02);
  border-top: 1px solid var(--border);
}
.inline-confirm textarea {
  width: 100%;
  font-family: var(--font);
  font-size: var(--font-size-sm);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 8px;
  resize: vertical;
  min-height: 60px;
}
.inline-confirm .confirm-actions {
  display: flex;
  gap: 8px;
  margin-top: 8px;
}

/* Summary text next to status badge */
.status-summary {
  font-size: 11px;
  color: var(--text-muted);
  margin-left: 6px;
}

/* ---------------------------------------------------------------------------
   Loading skeletons
   --------------------------------------------------------------------------- */

.skeleton {
  background: linear-gradient(90deg, var(--border) 25%, #f0f0f0 50%, var(--border) 75%);
  background-size: 200% 100%;
  animation: shimmer 1.5s infinite;
  border-radius: var(--radius);
}
@keyframes shimmer {
  0%   { background-position: 200% 0; }
  100% { background-position: -200% 0; }
}

/* ---------------------------------------------------------------------------
   Responsive
   --------------------------------------------------------------------------- */

/* Prevent text selection while dragging the resizer */
body.resizing {
  user-select: none;
  cursor: col-resize;
}

@media (max-width: 1024px) {
  /* Stats panel becomes a slide-up drawer on tablet */
  .app-body { flex-direction: column; }
  .panel-resizer { display: none; }
  #stats-panel {
    flex: none;
    max-width: 100%;
    width: 100% !important;  /* override JS-set width */
    height: 300px;
    border-left: none;
    border-top: 1px solid var(--border);
  }
}

@media (max-width: 768px) {
  #stats-panel { height: 260px; }
  .header-controls { gap: 4px; }
  #location-input { width: 100px; }
}
