/* ============ CSS Variables ============ */
:root {
  --bg-primary: #0f0f13;
  --bg-secondary: #1a1a23;
  --bg-tertiary: #252532;
  --accent: #6366f1;
  --accent-hover: #818cf8;
  --text-primary: #f4f4f5;
  --text-secondary: #a1a1aa;
  --text-muted: #71717a;
  --border: #2e2e3a;
  --success: #22c55e;
  --warning: #f59e0b;
  --error: #ef4444;
  --radius: 12px;
  --shadow: 0 4px 24px rgba(0, 0, 0, 0.4);
}

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

body {
  font-family: 'Inter', system-ui, -apple-system, sans-serif;
  background: var(--bg-primary);
  color: var(--text-primary);
  min-height: 100vh;
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
}

/* ============ Layout ============ */
.container {
  max-width: 1000px;
  margin: 0 auto;
  padding: 2rem 1.5rem;
  display: flex;
  flex-direction: column;
  gap: 2rem;
  min-height: 100vh;
}

.main-content {
  display: grid;
  grid-template-columns: 1fr 320px;
  gap: 1.5rem;
  align-items: start;
}

/* ============ Header ============ */
.header {
  text-align: center;
}

.title {
  font-size: 2rem;
  font-weight: 700;
  letter-spacing: -0.02em;
  margin-bottom: 0.5rem;
}

.accent {
  background: linear-gradient(135deg, var(--accent), #a855f7);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.subtitle {
  color: var(--text-secondary);
  font-size: 0.95rem;
}

/* ============ Video Section ============ */
.video-section {
  display: flex;
  justify-content: center;
}

.video-container {
  position: relative;
  width: 100%;
  aspect-ratio: 1;
  background: var(--bg-secondary);
  border-radius: var(--radius);
  overflow: hidden;
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
}

#video, #canvas {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

#canvas {
  z-index: 10;
  pointer-events: none;
}

/* ============ Loader Overlay ============ */
.loader-overlay {
  position: absolute;
  inset: 0;
  z-index: 20;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: rgba(15, 15, 19, 0.95);
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.3s, visibility 0.3s;
}

.loader-overlay.visible {
  opacity: 1;
  visibility: visible;
}

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

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

.loader-overlay p {
  color: var(--text-secondary);
  font-size: 0.9rem;
}

/* ============ Status & FPS Badges ============ */
.status-badge, .fps-badge {
  position: absolute;
  top: 1rem;
  z-index: 15;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.4rem 0.75rem;
  background: rgba(0, 0, 0, 0.6);
  backdrop-filter: blur(8px);
  border-radius: 20px;
  font-size: 0.75rem;
  font-weight: 500;
  border: 1px solid rgba(255, 255, 255, 0.1);
}

.status-badge {
  left: 1rem;
}

.fps-badge {
  right: 1rem;
  font-family: 'SF Mono', 'Fira Code', monospace;
  color: var(--text-secondary);
}

.fps-badge #fps {
  color: var(--accent);
  font-weight: 600;
}

.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--text-muted);
}

.status-dot.loading {
  background: var(--warning);
  animation: pulse 1.5s infinite;
}

.status-dot.ready {
  background: var(--success);
}

.status-dot.running {
  background: var(--success);
  animation: pulse 1.5s infinite;
}

.status-dot.error {
  background: var(--error);
}

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.5; }
}

/* ============ Controls ============ */
.controls {
  display: flex;
  flex-direction: column;
  gap: 1.25rem;
  background: var(--bg-secondary);
  padding: 1.5rem;
  border-radius: var(--radius);
  border: 1px solid var(--border);
}

.control-group {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.control-group label {
  font-size: 0.85rem;
  font-weight: 500;
  color: var(--text-secondary);
}

/* Select */
select {
  appearance: none;
  background: var(--bg-tertiary);
  border: 1px solid var(--border);
  color: var(--text-primary);
  padding: 0.75rem 1rem;
  border-radius: 8px;
  font-size: 0.9rem;
  cursor: pointer;
  transition: border-color 0.2s;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' fill='%23a1a1aa' viewBox='0 0 16 16'%3E%3Cpath d='M8 11L3 6h10l-5 5z'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 0.75rem center;
}

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

select:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.2);
}

/* Toggle Group */
.toggle-group {
  display: flex;
  gap: 1.5rem;
}

.toggle {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  cursor: pointer;
  user-select: none;
}

.toggle input {
  display: none;
}

.toggle-slider {
  width: 44px;
  height: 24px;
  background: var(--bg-tertiary);
  border-radius: 12px;
  position: relative;
  transition: background 0.2s;
  border: 1px solid var(--border);
}

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

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

.toggle input:checked + .toggle-slider::after {
  transform: translateX(20px);
  background: white;
}

.toggle-label {
  font-size: 0.9rem;
  color: var(--text-primary);
}

/* Threshold Range */
.threshold-control label {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.threshold-control #threshold-value {
  background: var(--bg-tertiary);
  padding: 0.25rem 0.5rem;
  border-radius: 4px;
  font-family: 'SF Mono', 'Fira Code', monospace;
  font-size: 0.8rem;
  color: var(--accent);
}

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

input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 18px;
  height: 18px;
  background: var(--accent);
  border-radius: 50%;
  cursor: pointer;
  box-shadow: 0 2px 8px rgba(99, 102, 241, 0.4);
  transition: transform 0.2s;
}

input[type="range"]::-webkit-slider-thumb:hover {
  transform: scale(1.1);
}

input[type="range"]::-moz-range-thumb {
  width: 18px;
  height: 18px;
  background: var(--accent);
  border: none;
  border-radius: 50%;
  cursor: pointer;
}

/* Primary Button */
.btn-primary {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.75rem;
  padding: 0.875rem 1.5rem;
  background: var(--accent);
  color: white;
  border: none;
  border-radius: 8px;
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s, transform 0.1s, box-shadow 0.2s;
  box-shadow: 0 4px 12px rgba(99, 102, 241, 0.3);
}

.btn-primary:hover:not(:disabled) {
  background: var(--accent-hover);
  transform: translateY(-1px);
  box-shadow: 0 6px 16px rgba(99, 102, 241, 0.4);
}

.btn-primary:active:not(:disabled) {
  transform: translateY(0);
}

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

.btn-primary.running {
  background: var(--error);
  box-shadow: 0 4px 12px rgba(239, 68, 68, 0.3);
}

.btn-primary.running:hover:not(:disabled) {
  background: #f87171;
  box-shadow: 0 6px 16px rgba(239, 68, 68, 0.4);
}

#btn-icon {
  font-size: 0.8rem;
}

/* ============ Footer ============ */
.footer {
  text-align: center;
  margin-top: auto;
  padding-top: 1rem;
}

.footer p {
  font-size: 0.85rem;
  color: var(--text-muted);
}

.footer a {
  color: var(--accent);
  text-decoration: none;
  transition: color 0.2s;
}

.footer a:hover {
  color: var(--accent-hover);
}

/* ============ Responsive ============ */
@media (max-width: 768px) {
  .main-content {
    grid-template-columns: 1fr;
  }

  .controls {
    order: 1;
  }

  .video-section {
    order: 0;
  }
}

@media (max-width: 480px) {
  .container {
    padding: 1.5rem 1rem;
    gap: 1.5rem;
  }

  .title {
    font-size: 1.5rem;
  }

  .toggle-group {
    flex-direction: column;
    gap: 1rem;
  }

  .controls {
    padding: 1.25rem;
  }
}
