/* ============================================================
   EcoTwin — Marxan-style dark spatial planning UI
   ============================================================ */

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

:root {
  --rail-w:   48px;
  --panel-w:  320px;
  --right-w:  260px;
  --bg:       #0d0e14;
  --panel:    #13141c;
  --panel-2:  #1a1b26;
  --panel-3:  #20212e;
  --border:   #252632;
  --border-2: #2e2f42;
  --text:     #c8cdd8;
  --text-dim: #8892a4;
  --text-muted: #505870;
  --accent:   #00b0ff;
  --accent-2: #0090d4;
  --accent-glow: rgba(0,176,255,0.18);
  --green:    #4cffb0;
  --green-dim: rgba(76,255,176,0.15);
  --blue:     #2979ff;
  --blue-dim: rgba(41,121,255,0.15);
  --danger:   #ff5252;
  --font:     -apple-system, 'SF Pro Text', 'Inter', system-ui, sans-serif;
  --font-mono: 'SF Mono', 'Menlo', 'Fira Code', 'Consolas', monospace;
  --radius:   4px;
  --radius-lg: 6px;
}

html, body {
  width: 100vw; height: 100vh;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 12px;
  -webkit-font-smoothing: antialiased;
  line-height: 1.4;
}

/* ── App shell ───────────────────────────────────────────── */

#app {
  display: flex;
  width: 100vw;
  height: 100vh;
  position: relative;
}

/* ── Map ─────────────────────────────────────────────────── */

#map {
  flex: 1;
  height: 100%;
  position: relative;
  z-index: 1;
}

#agent-canvas {
  position: absolute;
  top: 0; left: 0;
  width: 100%; height: 100%;
  pointer-events: none;
  z-index: 500;
}

/* Leaflet zone label */
.zone-label {
  background: transparent !important;
  border: none !important;
  box-shadow: none !important;
  font-family: var(--font-mono) !important;
  font-size: 9px !important;
  color: rgba(76,255,176,0.55) !important;
  text-transform: uppercase;
  letter-spacing: 1px;
  white-space: nowrap;
}

/* ── Left rail ───────────────────────────────────────────── */

#sidebar {
  display: flex;
  height: 100%;
  position: absolute;
  left: 0; top: 0;
  z-index: 1000;
}

#rail {
  width: var(--rail-w);
  height: 100%;
  background: var(--panel);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 8px 0;
  gap: 2px;
  flex-shrink: 0;
}

.rail-logo {
  width: 28px; height: 28px;
  background: var(--accent);
  border-radius: 6px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 13px;
  font-weight: 700;
  color: var(--bg);
  margin-bottom: 10px;
  letter-spacing: -0.5px;
  flex-shrink: 0;
  cursor: default;
  user-select: none;
}

.rail-btn {
  width: 34px; height: 34px;
  border-radius: var(--radius);
  border: none;
  background: transparent;
  color: var(--text-muted);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: background 0.12s, color 0.12s;
  flex-shrink: 0;
  position: relative;
}

.rail-btn:hover { background: var(--panel-2); color: var(--text); }

.rail-btn.active {
  background: var(--accent-glow);
  color: var(--accent);
}

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

.rail-divider {
  width: 24px;
  height: 1px;
  background: var(--border);
  margin: 6px 0;
}

.rail-spacer { flex: 1; }

/* ── Left panel ──────────────────────────────────────────── */

#left-panel {
  width: var(--panel-w);
  height: 100%;
  background: var(--panel);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  transform: translateX(calc(-1 * var(--panel-w)));
  transition: transform 0.22s cubic-bezier(0.4, 0, 0.2, 1);
  overflow: hidden;
  flex-shrink: 0;
}

#left-panel.open { transform: translateX(0); }

.panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 11px 14px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.panel-title {
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.04em;
  color: var(--text);
  display: flex;
  align-items: center;
  gap: 6px;
}

.panel-title-dot {
  width: 6px; height: 6px;
  border-radius: 50%;
  background: var(--accent);
}

#panel-close {
  width: 22px; height: 22px;
  border: none;
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
  border-radius: var(--radius);
  display: flex; align-items: center; justify-content: center;
  font-size: 14px;
  transition: background 0.1s, color 0.1s;
}

#panel-close:hover { background: var(--panel-2); color: var(--text); }

.panel-body {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
}

.panel-body::-webkit-scrollbar { width: 3px; }
.panel-body::-webkit-scrollbar-thumb { background: var(--border-2); border-radius: 2px; }

/* Panel sections */
.panel-section { display: none; }
.panel-section.active { display: block; }
.panel-section.hidden { display: none; }

.ps-group {
  padding: 12px 14px;
  border-bottom: 1px solid var(--border);
}

.ps-label {
  font-size: 9px;
  font-weight: 700;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--text-muted);
  margin-bottom: 10px;
  display: flex;
  align-items: center;
  gap: 6px;
}

.ps-label::after {
  content: '';
  flex: 1;
  height: 1px;
  background: var(--border);
}

/* Simulation controls */
.sim-controls-row {
  display: flex;
  gap: 6px;
  margin-bottom: 8px;
}

.btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 5px;
  height: 30px;
  padding: 0 12px;
  border-radius: var(--radius);
  border: 1px solid var(--border-2);
  background: var(--panel-2);
  color: var(--text);
  font-family: var(--font);
  font-size: 11px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.12s;
  white-space: nowrap;
  flex-shrink: 0;
}

.btn:hover { background: var(--panel-3); border-color: var(--text-muted); }

.btn.primary {
  background: transparent;
  border-color: var(--accent);
  color: var(--accent);
  flex: 1;
  height: 34px;
  font-size: 12px;
}
.btn.primary:hover { background: var(--accent-glow); }
.btn.primary.active {
  background: var(--accent-glow);
}

.btn.ghost {
  background: transparent;
  border-color: var(--border);
  color: var(--text-dim);
}
.btn.ghost:hover { border-color: var(--text-muted); color: var(--text); }

/* Field rows */
.field-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 7px;
  gap: 8px;
}

.field-label {
  font-size: 11px;
  color: var(--text-dim);
  white-space: nowrap;
}

.field-input {
  width: 70px;
  height: 26px;
  background: var(--panel-3);
  border: 1px solid var(--border-2);
  border-radius: var(--radius);
  color: var(--text);
  font-family: var(--font-mono);
  font-size: 11px;
  padding: 0 7px;
  text-align: right;
}

select.field-select {
  appearance: none;
  height: 26px;
  background: var(--panel-3);
  border: 1px solid var(--border-2);
  border-radius: var(--radius);
  color: var(--text);
  font-family: var(--font);
  font-size: 11px;
  padding: 0 22px 0 7px;
  cursor: pointer;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='6' fill='none'%3E%3Cpath d='M1 1l4 4 4-4' stroke='%23505870' stroke-width='1.5' stroke-linecap='round'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 7px center;
}

select.field-select:hover { border-color: var(--text-muted); }

/* Stats grid */
.stats-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 5px;
  margin-bottom: 10px;
}

.stat-tile {
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 8px 9px;
}

.stat-v {
  font-family: var(--font-mono);
  font-size: 17px;
  font-weight: 600;
  color: var(--text);
  letter-spacing: -0.5px;
  line-height: 1;
}

.stat-k {
  font-size: 9.5px;
  color: var(--text-muted);
  margin-top: 3px;
  letter-spacing: 0.02em;
}

/* Stage bar */
.stage-bar {
  display: flex;
  height: 5px;
  border-radius: 3px;
  overflow: hidden;
  background: var(--panel-3);
  margin: 6px 0 4px;
  gap: 1px;
}

.stage-bar .seg {
  height: 100%;
  min-width: 1px;
  transition: width 0.5s ease;
}

.stage-legend {
  display: flex;
  flex-wrap: wrap;
  gap: 4px 10px;
  margin-top: 4px;
}

.stage-dot {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 9.5px;
  color: var(--text-muted);
}

.dot { width: 6px; height: 6px; border-radius: 50%; flex-shrink: 0; }

/* Scenario thumbnails */
.thumb-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 6px;
}

.thumb {
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 7px 8px;
  cursor: default;
  position: relative;
  overflow: hidden;
  min-height: 62px;
  transition: border-color 0.15s;
}

.thumb:hover { border-color: var(--border-2); }

.thumb.current {
  border-color: var(--accent);
  background: rgba(0,176,255,0.05);
}

.thumb-year {
  font-size: 8.5px;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--accent);
  margin-bottom: 3px;
}

.thumb.current .thumb-year { color: var(--accent); }

.thumb-pop {
  font-family: var(--font-mono);
  font-size: 9.5px;
  color: var(--text-dim);
}

.thumb-bar {
  position: absolute;
  bottom: 0; left: 0; right: 0;
  height: 3px;
  background: var(--accent);
  opacity: 0.3;
  transform-origin: left;
  transform: scaleX(0);
  transition: transform 0.4s ease;
}

.thumb.current .thumb-bar {
  opacity: 0.7;
  transform: scaleX(1);
}

/* Species rows */
.species-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 7px 0;
  border-bottom: 1px solid var(--border);
}

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

.sp-dot { width: 8px; height: 8px; border-radius: 50%; flex-shrink: 0; }

.sp-name {
  flex: 1;
  font-size: 11px;
  color: var(--text);
}

.sp-sci {
  font-size: 9.5px;
  color: var(--text-muted);
  font-style: italic;
}

/* Toggle switch */
.toggle {
  position: relative;
  width: 28px; height: 15px;
  flex-shrink: 0;
}

.toggle input { opacity: 0; width: 0; height: 0; position: absolute; }

.toggle-track {
  position: absolute;
  inset: 0;
  background: var(--panel-3);
  border-radius: 8px;
  border: 1px solid var(--border-2);
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s;
}

.toggle input:checked + .toggle-track { background: var(--accent); border-color: var(--accent); }

.toggle-track::after {
  content: '';
  position: absolute;
  top: 1.5px; left: 1.5px;
  width: 10px; height: 10px;
  background: var(--text-muted);
  border-radius: 50%;
  transition: transform 0.15s, background 0.15s;
}

.toggle input:checked + .toggle-track::after {
  transform: translateX(13px);
  background: #fff;
}

/* Live data panel */
.live-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 0;
  border-bottom: 1px solid var(--border);
}
.live-row:last-child { border-bottom: none; }

.live-k {
  font-size: 10.5px;
  color: var(--text-dim);
  display: flex;
  align-items: center;
  gap: 6px;
}

.live-v {
  font-family: var(--font-mono);
  font-size: 11px;
  font-weight: 600;
  color: var(--text);
}

.source-badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  background: rgba(0,176,255,0.08);
  border: 1px solid rgba(0,176,255,0.2);
  border-radius: 3px;
  padding: 2px 6px;
  font-size: 9px;
  color: var(--accent);
  font-family: var(--font-mono);
}

/* Charts (results section) */
.chart-canvas {
  width: 100%;
  height: 80px;
  display: block;
  background: var(--panel-2);
  border-radius: var(--radius);
  border: 1px solid var(--border);
  margin-top: 6px;
}

.chart-label {
  font-size: 9.5px;
  color: var(--text-muted);
  margin-top: 8px;
  margin-bottom: 2px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

/* ── Right layer panel ───────────────────────────────────── */

#layer-panel {
  position: absolute;
  right: 0; top: 0;
  width: var(--right-w);
  height: 100%;
  background: var(--panel);
  border-left: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  z-index: 900;
  overflow: hidden;
}

.lp-header {
  padding: 11px 12px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.lp-title {
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--text-dim);
}

.lp-body {
  flex: 1;
  overflow-y: auto;
}

.lp-body::-webkit-scrollbar { width: 3px; }
.lp-body::-webkit-scrollbar-thumb { background: var(--border-2); border-radius: 2px; }

.lp-section {
  border-bottom: 1px solid var(--border);
}

.lp-section-header {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 8px 12px;
  cursor: pointer;
  user-select: none;
  background: var(--panel);
  transition: background 0.1s;
}

.lp-section-header:hover { background: var(--panel-2); }

.lp-chevron {
  width: 10px; height: 10px;
  color: var(--text-muted);
  transition: transform 0.15s;
  flex-shrink: 0;
}

.lp-section.collapsed .lp-chevron { transform: rotate(-90deg); }

.lp-section-title {
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--text);
  flex: 1;
}

.lp-section-body { padding: 4px 0; }
.lp-section.collapsed .lp-section-body { display: none; }

.layer-row {
  display: flex;
  align-items: center;
  gap: 7px;
  padding: 5px 12px;
  transition: background 0.1s;
}

.layer-row:hover { background: var(--panel-2); }

.layer-dot {
  width: 8px; height: 8px;
  border-radius: 50%;
  flex-shrink: 0;
}

.layer-dot.outline {
  background: transparent;
  border: 1.5px solid currentColor;
}

.layer-name {
  flex: 1;
  font-size: 11px;
  color: var(--text);
}

.layer-actions {
  display: flex;
  gap: 2px;
}

.layer-icon-btn {
  width: 20px; height: 20px;
  border: none;
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
  border-radius: 3px;
  display: flex; align-items: center; justify-content: center;
  transition: background 0.1s, color 0.1s;
}

.layer-icon-btn:hover { background: var(--panel-3); color: var(--text); }
.layer-eye.off { color: var(--border-2); }

/* Slider */
.slider-group {
  padding: 6px 12px 10px;
}

.slider-labels {
  display: flex;
  justify-content: space-between;
  font-size: 9px;
  color: var(--text-muted);
  margin-bottom: 4px;
}

input[type=range].lp-slider {
  width: 100%;
  -webkit-appearance: none;
  height: 4px;
  background: var(--panel-3);
  border-radius: 2px;
  outline: none;
}

input[type=range].lp-slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 12px; height: 12px;
  border-radius: 50%;
  background: var(--green);
  cursor: pointer;
  box-shadow: 0 0 4px rgba(76,255,176,0.4);
}

.slider-val {
  text-align: right;
  font-family: var(--font-mono);
  font-size: 10px;
  color: var(--accent);
  margin-top: 3px;
}

/* ── Breadcrumb / top-left overlay ───────────────────────── */

#breadcrumb {
  position: absolute;
  top: 0;
  left: calc(var(--rail-w) + 1px);
  z-index: 800;
  padding: 10px 14px;
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 10.5px;
  color: var(--text-muted);
  pointer-events: none;
  background: linear-gradient(to bottom, rgba(13,14,20,0.85) 0%, transparent 100%);
}

#breadcrumb span { color: var(--text-dim); }
#breadcrumb span.active { color: var(--text); font-weight: 500; }
#breadcrumb .sep { color: var(--border-2); }

/* ── Bottom status bar ───────────────────────────────────── */

#statusbar {
  position: absolute;
  bottom: 0;
  left: calc(var(--rail-w) + 1px);
  right: var(--right-w);
  z-index: 800;
  height: 28px;
  background: rgba(13,14,20,0.88);
  border-top: 1px solid var(--border);
  display: flex;
  align-items: center;
  padding: 0 12px;
  gap: 20px;
  backdrop-filter: blur(8px);
  pointer-events: none;
}

.sb-item {
  display: flex;
  align-items: center;
  gap: 5px;
  font-size: 10px;
}

.sb-key { color: var(--text-muted); }
.sb-val {
  font-family: var(--font-mono);
  font-size: 10px;
  color: var(--text);
  font-weight: 600;
}

.sb-divider {
  width: 1px;
  height: 12px;
  background: var(--border);
}

.sb-spacer { flex: 1; }

.sb-date {
  font-family: var(--font-mono);
  font-size: 10px;
  color: var(--accent);
}

/* ── Leaflet overrides ───────────────────────────────────── */

.leaflet-control-zoom {
  margin-bottom: 32px !important;
  margin-right: calc(var(--right-w) + 12px) !important;
  border: 1px solid var(--border) !important;
  border-radius: var(--radius) !important;
  overflow: hidden;
}

.leaflet-control-zoom a {
  background: var(--panel) !important;
  color: var(--text-dim) !important;
  border-bottom: 1px solid var(--border) !important;
  width: 26px !important;
  height: 26px !important;
  line-height: 26px !important;
  font-size: 14px !important;
}

.leaflet-control-zoom a:hover {
  background: var(--panel-2) !important;
  color: var(--text) !important;
}

.leaflet-control-attribution {
  background: rgba(13,14,20,0.7) !important;
  color: var(--text-muted) !important;
  font-size: 9px !important;
  margin-right: calc(var(--right-w) + 6px) !important;
  margin-bottom: 30px !important;
  border-radius: var(--radius) !important;
  padding: 2px 6px !important;
}

.leaflet-control-attribution a { color: var(--text-muted) !important; }

.leaflet-popup-content-wrapper {
  background: var(--panel) !important;
  border: 1px solid var(--border) !important;
  border-radius: var(--radius) !important;
  box-shadow: 0 4px 16px rgba(0,0,0,0.5) !important;
  padding: 0 !important;
}

.leaflet-popup-tip { background: var(--panel) !important; }
.leaflet-popup-content { margin: 0 !important; }

/* ── Utility ─────────────────────────────────────────────── */

.hidden { display: none !important; }

.live-dot {
  width: 6px; height: 6px;
  border-radius: 50%;
  background: var(--green);
  box-shadow: 0 0 4px var(--green);
  animation: pulse 2s infinite;
}

@keyframes pulse {
  0%,100% { box-shadow: 0 0 3px var(--green); }
  50%      { box-shadow: 0 0 8px var(--green); }
}
