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

:root {
  --bg-page:    #0f1117;
  --bg-card:    #1a1d27;
  --bg-input:   #232636;
  --border:     #2e3348;
  --text:       #e2e8f0;
  --text-muted: #8892a4;
  --accent:     #4f8ef7;
  --accent-dim: #2a4a8a;
  --green:      #4ade80;
  --red:        #f87171;
  --yellow:     #facc15;
  --sidebar-w:  340px;
  --radius:     8px;
  --font: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
}

html, body { height: 100%; font-family: var(--font); background: var(--bg-page); color: var(--text); }

h1, h2, h3 { font-weight: 600; }

/* ===== Layout ===== */
#app {
  display: grid;
  grid-template-columns: var(--sidebar-w) 1fr;
  min-height: 100vh;
}

/* ===== Sidebar ===== */
#sidebar {
  background: var(--bg-card);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 0;
  overflow-y: auto;
  position: sticky;
  top: 0;
  height: 100vh;
}

#sidebar-header {
  padding: 20px 20px 16px;
  border-bottom: 1px solid var(--border);
}

#sidebar-header h1 {
  font-size: 1.1rem;
  line-height: 1.3;
  color: var(--text);
}

#dataset-info {
  margin-top: 6px;
  font-size: 0.75rem;
  color: var(--text-muted);
}

/* Sidebar sections */
#sidebar > section {
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
}

.section-title {
  font-size: 0.7rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--text-muted);
  margin-bottom: 12px;
}

/* ===== Sliders ===== */
.slider-group {
  margin-bottom: 14px;
}

.slider-header {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  margin-bottom: 4px;
}

.slider-name {
  font-size: 0.82rem;
  font-weight: 600;
  display: flex;
  align-items: center;
  gap: 6px;
}

.source-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  display: inline-block;
  flex-shrink: 0;
}

.slider-value {
  font-size: 0.78rem;
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
}

.slider-sub {
  font-size: 0.7rem;
  color: var(--text-muted);
  margin-bottom: 4px;
}

input[type="range"] {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  height: 5px;
  border-radius: 3px;
  background: var(--bg-input);
  outline: none;
  cursor: pointer;
}

input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: var(--accent);
  cursor: pointer;
  transition: background 0.15s;
}

input[type="range"]::-webkit-slider-thumb:hover { background: #7aaafb; }
input[type="range"]::-moz-range-thumb {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: var(--accent);
  border: none;
  cursor: pointer;
}

.slider-pct {
  font-size: 0.68rem;
  color: var(--text-muted);
  text-align: right;
  margin-top: 1px;
}

/* ===== Radio buttons ===== */
.radio-group {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.radio-label {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  font-size: 0.82rem;
}

.radio-label input[type="radio"] {
  accent-color: var(--accent);
  width: 14px;
  height: 14px;
  cursor: pointer;
}

/* ===== Toggle buttons ===== */
#loads-container {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.toggle-label {
  display: flex;
  align-items: center;
  gap: 10px;
  cursor: pointer;
}

.toggle-btn {
  width: 38px;
  height: 22px;
  border-radius: 11px;
  border: none;
  background: var(--bg-input);
  position: relative;
  cursor: pointer;
  flex-shrink: 0;
  transition: background 0.2s;
}

.toggle-btn[data-active="true"] { background: var(--accent); }

.toggle-dot {
  position: absolute;
  top: 3px;
  left: 3px;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: var(--text-muted);
  transition: transform 0.2s, background 0.2s;
}

.toggle-btn[data-active="true"] .toggle-dot {
  transform: translateX(16px);
  background: #fff;
}

.toggle-text {
  font-size: 0.82rem;
  line-height: 1.4;
}

.toggle-text small { color: var(--text-muted); font-size: 0.72rem; }

/* ===== Summary stats ===== */
#summary-stats {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.stat-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 0.8rem;
}

.stat-label { color: var(--text-muted); }

.stat-value {
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

/* ===== Export / Reset buttons ===== */
#export-section { padding: 16px 20px; margin-top: auto; display: flex; flex-direction: column; gap: 8px; }

#export-btn, #reset-btn, #optimize-btn {
  width: 100%;
  padding: 9px 14px;
  border-radius: var(--radius);
  font-size: 0.82rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s, color 0.15s, opacity 0.15s;
}

#export-btn {
  background: var(--accent-dim);
  border: 1px solid var(--accent);
  color: #90b8ff;
}

#export-btn:hover { background: var(--accent); color: #fff; }

#reset-btn {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text-muted);
}

#reset-btn:hover { background: var(--bg-input); color: var(--text); }

#optimize-btn {
  background: rgba(74, 222, 128, 0.12);
  border: 1px solid var(--green);
  color: #86efac;
}

#optimize-btn:hover { background: var(--green); color: #06280f; }

#optimize-btn:disabled {
  opacity: 0.6;
  cursor: default;
  background: rgba(74, 222, 128, 0.12);
  color: #86efac;
}

/* ===== Time zoom control ===== */
#zoom-card { padding: 14px 20px 16px; }

.zoom-header {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  margin-bottom: 8px;
}

.zoom-header .chart-title { margin-bottom: 0; }

#zoom-reset-btn {
  background: none;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text-muted);
  font-size: 0.72rem;
  padding: 4px 10px;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}

#zoom-reset-btn:hover { background: var(--bg-input); color: var(--text); }

.zoom-track-wrap {
  position: relative;
  height: 22px;
  display: flex;
  align-items: center;
}

.zoom-track-base {
  position: absolute;
  left: 0;
  right: 0;
  height: 5px;
  border-radius: 3px;
  background: var(--bg-input);
}

.zoom-track-selected {
  position: absolute;
  height: 5px;
  border-radius: 3px;
  background: var(--accent);
}

.zoom-track-wrap input[type="range"] {
  position: absolute;
  left: 0;
  right: 0;
  width: 100%;
  margin: 0;
  background: none;
  pointer-events: none;
}

.zoom-track-wrap input[type="range"]::-webkit-slider-thumb {
  pointer-events: auto;
}

.zoom-track-wrap input[type="range"]::-moz-range-thumb {
  pointer-events: auto;
}

.zoom-ticks {
  display: flex;
  justify-content: space-between;
  font-size: 0.68rem;
  color: var(--text-muted);
  margin-top: 2px;
}

/* ===== Main chart area ===== */
#charts {
  padding: 20px 24px;
  display: flex;
  flex-direction: column;
  gap: 20px;
  overflow-y: auto;
}

.chart-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px 20px 12px;
}

.chart-title {
  font-size: 0.85rem;
  font-weight: 600;
  margin-bottom: 10px;
  color: var(--text);
}

.chart-subtitle {
  font-size: 0.72rem;
  color: var(--text-muted);
  font-weight: 400;
}

.chart-wrap { position: relative; height: 240px; }
.chart-wrap-short { height: 110px; }

.chart-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 20px;
}

.chart-half { min-width: 0; }
.chart-half .chart-wrap { height: 200px; }

/* ===== Error banner ===== */
#error-banner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: #3b1a1a;
  border: 1px solid var(--red);
  border-radius: var(--radius);
  padding: 10px 14px;
  font-size: 0.82rem;
  color: var(--red);
}

#error-banner.hidden { display: none; }

#error-close {
  background: none;
  border: none;
  color: var(--red);
  cursor: pointer;
  font-size: 1rem;
  padding: 0 4px;
}

/* ===== Responsive ===== */
@media (max-width: 900px) {
  #app { grid-template-columns: 1fr; }
  #sidebar { position: static; height: auto; border-right: none; border-bottom: 1px solid var(--border); }
  .chart-row { grid-template-columns: 1fr; }
}

@media (max-width: 768px) {
  :root { --sidebar-w: 100%; }
  #charts { padding: 12px; }
}

/* ===== Loading overlay ===== */
#loading {
  position: fixed;
  inset: 0;
  background: rgba(15,17,23,0.75);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  font-size: 0.9rem;
  color: var(--text-muted);
}

#loading.hidden { display: none; }

.spinner {
  width: 28px;
  height: 28px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
  margin-right: 12px;
}

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