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

:root {
  --bg: #0f1117;
  --surface: #1a1d27;
  --surface-hover: #232733;
  --border: #2a2e3a;
  --text: #e4e4e7;
  --text-muted: #8b8d98;
  --accent: #6366f1;
  --accent-hover: #818cf8;
  --danger: #ef4444;
  --warning: #f59e0b;
  --success: #22c55e;
  /* AQI config colors — one per tunable parameter */
  --cfg-w-pl: #f87171;
  --cfg-m-pl: #fb923c;
  --cfg-w-j:  #38bdf8;
  --cfg-m-j:  #22d3ee;
  --cfg-w-l:  #c084fc;
  --cfg-d-l:  #4ade80;
  --cfg-t-l:  #fbbf24;
  /* AQI v2 config colors */
  --cfg2-k:     #38bdf8;
  --cfg2-knee:  #22d3ee;
  --cfg2-bpl:   #4ade80;
  --cfg2-a:     #c084fc;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
}

.container {
  max-width: 900px;
  margin: 0 auto;
  padding: 48px 24px;
}

header {
  margin-bottom: 40px;
}

h1 {
  font-size: 24px;
  font-weight: 600;
  letter-spacing: -0.02em;
}

h2 {
  font-size: 14px;
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
  margin-bottom: 16px;
}

.subtitle {
  color: var(--text-muted);
  font-size: 14px;
  margin-top: 6px;
}

/* Upload / Source selection */
.upload-section {
  margin-bottom: 32px;
}

.source-columns {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
}

.source-upload {
  display: flex;
  flex-direction: column;
}

.source-samples {
  display: flex;
  flex-direction: column;
  border: 1px solid var(--border);
  border-radius: 12px;
  overflow: hidden;
  max-height: 280px;
}

.samples-header {
  padding: 12px 16px;
  font-size: 13px;
  font-weight: 500;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  flex-shrink: 0;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.samples-count {
  font-size: 12px;
  font-weight: 400;
  color: var(--text-muted);
  opacity: 0.7;
  text-transform: none;
  letter-spacing: normal;
}

.samples-list {
  overflow-y: auto;
  flex: 1;
}

.samples-empty {
  padding: 24px 16px;
  text-align: center;
  color: var(--text-muted);
  font-size: 13px;
}

.sample-card {
  position: relative;
  padding: 12px 16px;
  cursor: pointer;
  border-bottom: 1px solid var(--border);
  overflow: hidden;
  transition: background 0.15s;
  min-height: 56px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

.sample-card:last-child {
  border-bottom: none;
}

.sample-card:hover {
  background: var(--surface-hover);
}

.sample-card.selected {
  background: rgba(99, 102, 241, 0.1);
  border-left: 3px solid var(--accent);
}

.sample-card canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  opacity: 0.15;
  pointer-events: none;
}

.sample-name {
  font-size: 13px;
  font-weight: 500;
  position: relative;
  z-index: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.sample-duration {
  font-size: 11px;
  font-family: 'SF Mono', 'Fira Code', monospace;
  color: var(--text-muted);
  position: relative;
  z-index: 1;
  align-self: flex-end;
}

.drop-zone {
  border: 2px dashed var(--border);
  border-radius: 12px;
  padding: 48px 24px;
  text-align: center;
  cursor: pointer;
  transition: border-color 0.2s, background 0.2s;
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
}

.drop-zone:hover,
.drop-zone.dragover {
  border-color: var(--accent);
  background: rgba(99, 102, 241, 0.05);
}

.drop-zone-content svg {
  color: var(--text-muted);
  margin-bottom: 12px;
}

.drop-zone-content p {
  color: var(--text-muted);
  font-size: 14px;
}

.browse-link {
  color: var(--accent);
  cursor: pointer;
  text-decoration: underline;
}

.file-types {
  font-size: 12px !important;
  margin-top: 8px;
  color: var(--text-muted);
  opacity: 0.6;
}

.file-info {
  display: none;
  align-items: center;
  justify-content: space-between;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 12px 16px;
  margin-top: 12px;
  font-size: 14px;
}

.file-info.visible {
  display: flex;
}

/* Controls */
.controls-section {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 24px;
  margin-bottom: 32px;
}

.controls-grid {
  display: flex;
  flex-direction: column;
  gap: 24px;
  margin-bottom: 24px;
}

.control-group label {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 14px;
  font-weight: 500;
  margin-bottom: 8px;
}

.control-group .unit {
  color: var(--text-muted);
  font-weight: 400;
}

.control-value {
  margin-left: auto;
  font-family: 'SF Mono', 'Fira Code', monospace;
  font-size: 13px;
  color: var(--accent);
  font-weight: 600;
}

select {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  font-family: inherit;
  cursor: pointer;
  outline: none;
  transition: border-color 0.2s;
}

select:hover:not(:disabled) {
  border-color: var(--text-muted);
}

select:focus {
  border-color: var(--accent);
}

select:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

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

input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: var(--accent);
  cursor: pointer;
  border: 2px solid var(--bg);
  box-shadow: 0 0 0 2px var(--accent);
}

.range-labels {
  display: flex;
  justify-content: space-between;
  font-size: 11px;
  color: var(--text-muted);
  margin-top: 4px;
  opacity: 0.6;
}

.loss-mode-toggle {
  display: flex;
  gap: 12px;
  margin-top: 6px;
}
.loss-mode-option {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 12px;
  color: var(--text-muted);
  cursor: pointer;
}
.loss-mode-option input[type="radio"] {
  accent-color: var(--primary);
  margin: 0;
}
.loss-mode-option:has(input:checked) span {
  color: var(--text);
}

/* Advanced section */
.advanced-section {
  margin-bottom: 24px;
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
}

.advanced-section summary {
  padding: 12px 16px;
  font-size: 13px;
  font-weight: 500;
  color: var(--text-muted);
  cursor: pointer;
  user-select: none;
  transition: color 0.15s;
  list-style: none;
}

.advanced-section summary::-webkit-details-marker {
  display: none;
}

.advanced-section summary::before {
  content: '\25B6';
  display: inline-block;
  margin-right: 8px;
  font-size: 10px;
  transition: transform 0.2s;
}

.advanced-section[open] summary::before {
  transform: rotate(90deg);
}

.advanced-section summary:hover {
  color: var(--text);
}

.advanced-content {
  padding: 4px 16px 16px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

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

.checkbox-label input[type="checkbox"] {
  width: 16px;
  height: 16px;
  margin-top: 2px;
  accent-color: var(--accent);
  cursor: pointer;
  flex-shrink: 0;
}

.checkbox-text {
  display: flex;
  flex-direction: column;
  gap: 2px;
  font-size: 14px;
}

.checkbox-text strong {
  font-weight: 500;
}

.checkbox-desc {
  font-size: 12px;
  color: var(--text-muted);
  line-height: 1.4;
}

/* Simulate row */
.simulate-row {
  display: flex;
  align-items: center;
  gap: 16px;
}

.simulate-row .btn-primary {
  flex: 1;
}

.toggle-label {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  color: var(--text-muted);
  cursor: pointer;
  white-space: nowrap;
  user-select: none;
}

.toggle-switch {
  position: relative;
  display: inline-block;
  width: 36px;
  height: 20px;
}

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

.toggle-track {
  position: absolute;
  inset: 0;
  background: var(--border);
  border-radius: 10px;
  transition: background 0.2s;
}

.toggle-track::after {
  content: '';
  position: absolute;
  top: 2px;
  left: 2px;
  width: 16px;
  height: 16px;
  background: var(--text);
  border-radius: 50%;
  transition: transform 0.2s;
}

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

.toggle-switch input:checked + .toggle-track::after {
  transform: translateX(16px);
}

/* Buttons */
.btn-primary {
  width: 100%;
  padding: 12px;
  border: none;
  border-radius: 8px;
  background: var(--accent);
  color: white;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s, opacity 0.2s;
}

.btn-primary:hover:not(:disabled) {
  background: var(--accent-hover);
}

.btn-primary:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.btn-icon {
  background: none;
  border: 1px solid var(--border);
  color: var(--text);
  width: 40px;
  height: 40px;
  border-radius: 8px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s, border-color 0.15s;
}

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

.btn-play {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  background: var(--accent);
  border-color: var(--accent);
}

.btn-play:hover {
  background: var(--accent-hover) !important;
  border-color: var(--accent-hover) !important;
}

/* Player */
.player-section {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 24px;
}

.stats-bar {
  display: flex;
  gap: 24px;
  flex-wrap: wrap;
  margin-bottom: 20px;
  padding-bottom: 16px;
  border-bottom: 1px solid var(--border);
  font-size: 13px;
}

.stat {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.stat-label {
  color: var(--text-muted);
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.stat-value {
  font-family: 'SF Mono', 'Fira Code', monospace;
  font-weight: 600;
}

.stat-value.good { color: var(--success); }
.stat-value.warn { color: var(--warning); }
.stat-value.bad { color: var(--danger); }

/* Tabs */
.player-tabs {
  display: flex;
  gap: 4px;
  margin-bottom: 16px;
  background: var(--bg);
  border-radius: 8px;
  padding: 3px;
}

.tab {
  flex: 1;
  padding: 8px 12px;
  border: none;
  border-radius: 6px;
  background: transparent;
  color: var(--text-muted);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}

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

.tab.active {
  background: var(--surface);
  color: var(--text);
}

/* Waveform */
.waveform-container {
  position: relative;
  height: 80px;
  background: var(--bg);
  border-radius: 8px;
  overflow: hidden;
  cursor: pointer;
  margin-bottom: 8px;
}

.waveform-container canvas {
  width: 100%;
  height: 100%;
}

.playhead {
  position: absolute;
  top: 0;
  left: 0;
  width: 2px;
  height: 100%;
  background: var(--accent);
  pointer-events: none;
  transition: left 0.05s linear;
}

.time-display {
  display: flex;
  justify-content: space-between;
  font-size: 12px;
  font-family: 'SF Mono', 'Fira Code', monospace;
  color: var(--text-muted);
  margin-bottom: 12px;
}

.player-controls {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
}

/* AQI Section */
.aqi-section {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 24px;
  margin-top: 32px;
}

.aqi-header {
  display: flex;
  gap: 32px;
  align-items: center;
}

.aqi-score-display {
  text-align: center;
  flex-shrink: 0;
  min-width: 140px;
}

.aqi-score-label {
  font-size: 11px;
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
  margin-bottom: 4px;
}

.aqi-score {
  font-size: 56px;
  font-weight: 700;
  font-family: 'SF Mono', 'Fira Code', monospace;
  line-height: 1;
  margin-bottom: 6px;
  transition: color 0.3s;
}

.aqi-score.good { color: var(--success); }
.aqi-score.degraded { color: var(--warning); }
.aqi-score.unusable { color: var(--danger); }

.aqi-band {
  font-size: 13px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.aqi-band.good { color: var(--success); }
.aqi-band.degraded { color: var(--warning); }
.aqi-band.unusable { color: var(--danger); }

.aqi-breakdown {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.aqi-component {
  display: flex;
  align-items: center;
  gap: 12px;
}

.aqi-component-label {
  font-size: 13px;
  color: var(--text-muted);
  width: 90px;
  flex-shrink: 0;
}

.aqi-component-bar {
  flex: 1;
  height: 8px;
  background: var(--bg);
  border-radius: 4px;
  overflow: hidden;
}

.aqi-component-fill {
  display: block;
  height: 100%;
  border-radius: 4px;
  transition: width 0.3s, background 0.3s;
  width: 0%;
}

.aqi-component-fill.good { background: var(--success); }
.aqi-component-fill.warn { background: var(--warning); }
.aqi-component-fill.bad { background: var(--danger); }

.aqi-component-value {
  font-size: 12px;
  font-family: 'SF Mono', 'Fira Code', monospace;
  color: var(--text-muted);
  width: 48px;
  text-align: right;
  flex-shrink: 0;
}

/* AQI Formula */
.aqi-formula-details {
  margin-top: 20px;
  border: 1px solid var(--border);
  border-radius: 8px;
}

.aqi-formula-details summary {
  padding: 12px 16px;
  font-size: 13px;
  font-weight: 500;
  color: var(--text-muted);
  cursor: pointer;
  user-select: none;
  transition: color 0.15s;
  list-style: none;
}

.aqi-formula-details summary::-webkit-details-marker {
  display: none;
}

.aqi-formula-details summary::before {
  content: '\25B6';
  display: inline-block;
  margin-right: 8px;
  font-size: 10px;
  transition: transform 0.2s;
}

.aqi-formula-details[open] summary::before {
  transform: rotate(90deg);
}

.aqi-formula-details summary:hover {
  color: var(--text);
}

.aqi-formula-content {
  padding: 16px;
}

/* AQI Config Grid */
.aqi-config {
  margin-bottom: 20px;
  padding-bottom: 20px;
  border-bottom: 1px solid var(--border);
}

.config-row {
  display: grid;
  grid-template-columns: 90px 1fr 1fr 1fr;
  gap: 10px;
  align-items: end;
  margin-bottom: 10px;
}

.config-row:last-of-type {
  margin-bottom: 12px;
}

.config-metric {
  font-size: 13px;
  font-weight: 500;
  color: var(--text-muted);
  padding-bottom: 8px;
}

.config-spacer {
  display: block;
}

.config-input {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.config-input span:first-child {
  font-size: 11px;
  color: var(--cfg-color, var(--text-muted));
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.config-input input[type="number"] {
  width: 100%;
  padding: 6px 8px;
  border: 1px solid var(--border);
  border-left: 3px solid var(--cfg-color, var(--border));
  border-radius: 4px;
  background: var(--bg);
  color: var(--text);
  font-size: 13px;
  font-family: 'SF Mono', 'Fira Code', monospace;
  outline: none;
  transition: border-color 0.15s;
}

.config-input input[type="number"]:focus {
  border-color: var(--cfg-color, var(--accent));
  border-left-color: var(--cfg-color, var(--accent));
}

.config-unit {
  font-size: 11px !important;
  color: var(--text-muted) !important;
  text-transform: none !important;
  letter-spacing: normal !important;
  font-weight: 400 !important;
  position: absolute;
  right: 8px;
  bottom: 8px;
}

.config-input:has(.config-unit) {
  position: relative;
}

.config-input:has(.config-unit) input {
  padding-right: 32px;
}

/* Config tooltips */
.has-tooltip {
  position: relative;
}

.has-tooltip::after {
  content: attr(data-tooltip);
  position: absolute;
  bottom: calc(100% + 10px);
  left: 50%;
  transform: translateX(-50%);
  width: 300px;
  padding: 12px 14px;
  background: #1e2130;
  border: 1px solid var(--cfg-color, var(--border));
  border-radius: 8px;
  color: var(--text);
  font-size: 12px;
  line-height: 1.55;
  white-space: pre-line;
  pointer-events: none;
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.15s, visibility 0.15s;
  z-index: 20;
  box-shadow: 0 8px 24px rgba(0,0,0,0.4);
}

.has-tooltip::before {
  content: '';
  position: absolute;
  bottom: calc(100% + 4px);
  left: 50%;
  transform: translateX(-50%);
  border: 6px solid transparent;
  border-top-color: var(--cfg-color, var(--border));
  pointer-events: none;
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.15s, visibility 0.15s;
  z-index: 21;
}

.has-tooltip:hover::after,
.has-tooltip:hover::before {
  opacity: 1;
  visibility: visible;
}

/* Keep tooltips on-screen for edge items */
.config-row .has-tooltip:first-of-type::after {
  left: 0;
  transform: none;
}
.config-row .has-tooltip:first-of-type::before {
  left: 24px;
  transform: none;
}
.config-row .has-tooltip:last-of-type::after {
  left: auto;
  right: 0;
  transform: none;
}
.config-row .has-tooltip:last-of-type::before {
  left: auto;
  right: 24px;
  transform: none;
}

.aqi2-band-tooltip::after {
  width: 380px;
  border-color: var(--border);
}
.aqi2-band-tooltip::before {
  border-top-color: var(--border);
}

.btn-config-reset {
  background: none;
  border: 1px solid var(--border);
  color: var(--text-muted);
  font-size: 12px;
  padding: 5px 12px;
  border-radius: 4px;
  cursor: pointer;
  transition: color 0.15s, border-color 0.15s;
}

.btn-config-reset:hover {
  color: var(--text);
  border-color: var(--text-muted);
}

.aqi-formula-block {
  text-align: center;
  margin: 12px 0;
  overflow-x: auto;
}

.aqi-formula-where {
  text-align: center;
  font-size: 14px;
  font-style: italic;
  color: var(--text-muted);
  margin: 16px 0 8px;
}

.aqi-variables {
  margin-top: 20px;
  border-top: 1px solid var(--border);
  padding-top: 16px;
}

.aqi-variables h3 {
  font-size: 13px;
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
  margin-bottom: 12px;
}

.aqi-variables dl {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 6px 16px;
  align-items: baseline;
}

.aqi-variables dt {
  font-family: 'SF Mono', 'Fira Code', monospace;
  font-size: 13px;
  color: var(--text);
}

.aqi-variables dd {
  font-size: 13px;
  color: var(--text-muted);
  line-height: 1.5;
}

/* AQI v2 extras */
.aqi2-subtitle {
  font-size: 10px;
  font-weight: 400;
  color: var(--text-muted);
  opacity: 0.7;
  text-transform: none;
  letter-spacing: normal;
}

.aqi2-mos {
  font-size: 12px;
  font-family: 'SF Mono', 'Fira Code', monospace;
  color: var(--text-muted);
  margin-top: 4px;
}

.cfg2-bpl-source {
  font-size: 10px;
  font-weight: 400;
  opacity: 0.7;
  text-transform: none;
  letter-spacing: normal;
}

/* Loading */
.loading-overlay {
  position: fixed;
  inset: 0;
  background: rgba(15, 17, 23, 0.85);
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  z-index: 100;
  display: none;
}

.loading-overlay.visible {
  display: flex;
}

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

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

@media (max-width: 640px) {
  .container { padding: 24px 16px; }
  .source-columns { grid-template-columns: 1fr; }
  .source-samples { max-height: 220px; }
  .stats-bar { gap: 16px; }
  .aqi-header { flex-direction: column; gap: 20px; }
  .config-row { grid-template-columns: 1fr 1fr; gap: 8px; }
  .config-metric { grid-column: 1 / -1; padding-bottom: 0; }
  .config-spacer { display: none; }
}
