:root {
  color-scheme: light;
  --surface-1: #fcfcfb;
  --page-plane: #f9f9f7;
  --text-primary: #0b0b0b;
  --text-secondary: #52514e;
  --text-muted: #898781;
  --gridline: #e1e0d9;
  --baseline: #c3c2b7;
  --border: rgba(11, 11, 11, 0.10);
  --series-1: #2a78d6;
  --series-2: #eb6834;
  --series-3: #1baf7a;
  --status-good: #0ca30c;
}

@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
    --surface-1: #1a1a19;
    --page-plane: #0d0d0d;
    --text-primary: #ffffff;
    --text-secondary: #c3c2b7;
    --text-muted: #898781;
    --gridline: #2c2c2a;
    --baseline: #383835;
    --border: rgba(255, 255, 255, 0.10);
    --series-1: #3987e5;
    --series-2: #d95926;
    --series-3: #199e70;
    --status-good: #0ca30c;
  }
}

* {
  box-sizing: border-box;
}

html, body {
  height: 100%;
  margin: 0;
}

body {
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 16px;
  background: var(--page-plane);
  color: var(--text-primary);
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  overflow: hidden;
}

header {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

h1 {
  font-size: 20px;
  margin: 0;
}

.title-group {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.site-logo {
  height: 32px;
  width: auto;
  object-fit: contain;
}

.header-controls {
  display: flex;
  align-items: center;
  gap: 12px;
}

.detection-badge {
  font-size: 13px;
  font-weight: 600;
}

.detection-badge.status-running {
  color: var(--status-good);
}

.detection-badge.status-stopped {
  color: var(--text-muted);
}

.settings-link {
  font-size: 13px;
  color: var(--text-secondary);
  text-decoration: none;
  padding: 4px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
}

.settings-link:hover {
  color: var(--text-primary);
}

.event-start-form {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
  font-size: 13px;
  color: var(--text-secondary);
}

.event-start-form input,
.event-start-form select {
  font: inherit;
  padding: 4px 6px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--surface-1);
  color: var(--text-primary);
}

.event-start-form button {
  font: inherit;
  padding: 4px 12px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--series-1);
  color: #ffffff;
  cursor: pointer;
}

.main-row {
  flex: 1 1 auto;
  display: flex;
  gap: 16px;
  min-height: 0;
}

.stats-column {
  flex: 0 0 210px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  min-height: 0;
}

.stat-tile {
  flex: 1 1 0;
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 16px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 4px;
  min-height: 0;
}

.stat-value {
  font-size: 32px;
  font-weight: 600;
  line-height: 1.1;
}

.stat-label {
  color: var(--text-secondary);
  font-size: 12px;
}

.live-view {
  flex: 1 1 auto;
  min-width: 0;
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

.live-view img {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
}

.live-view-placeholder {
  display: none;
  color: var(--text-muted);
  font-size: 14px;
  padding: 16px;
  text-align: center;
}

.chart-section {
  flex: 0 0 220px;
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 12px 16px;
  min-height: 0;
  position: relative;
}

.chart-section canvas {
  width: 100% !important;
  height: 100% !important;
}

/* --- Settings page --- */
/* The dashboard body is a strict no-scroll single-viewport layout; the
   settings page is an ordinary scrollable form, so it opts back out here. */
body.settings-body {
  height: auto;
  min-height: 100%;
  overflow: visible;
  max-width: 640px;
  margin: 0 auto;
}

.back-link {
  font-size: 13px;
  color: var(--text-secondary);
  text-decoration: none;
}

.back-link:hover {
  color: var(--text-primary);
}

.settings-panel {
  min-width: 0;
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 16px 20px;
}

.detection-control {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
}

.detection-status {
  font-size: 24px;
  font-weight: 600;
}

.detection-buttons {
  display: flex;
  gap: 8px;
}

.btn {
  font: inherit;
  font-weight: 600;
  padding: 8px 18px;
  border-radius: 6px;
  border: 1px solid var(--border);
  cursor: pointer;
  background: var(--surface-1);
  color: var(--text-primary);
}

.btn:disabled {
  opacity: 0.5;
  cursor: default;
}

.btn-start {
  background: var(--status-good);
  border-color: var(--status-good);
  color: #ffffff;
}

.btn-stop {
  background: transparent;
}

.btn-save {
  background: var(--series-1);
  border-color: var(--series-1);
  color: #ffffff;
  margin-top: 4px;
}

.settings-form {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.settings-form fieldset {
  /* fieldsets ignore display:flex's shrinkability by default (browser-native
     min-width: min-content) -- without this override a wide child (e.g. the
     stream-URL input) forces the fieldset past the 640px settings column. */
  min-width: 0;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 12px 16px 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.settings-form legend {
  padding: 0 6px;
  color: var(--text-secondary);
  font-size: 13px;
  font-weight: 600;
}

.settings-form label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 13px;
  color: var(--text-secondary);
}

.settings-form label.checkbox-row {
  flex-direction: row;
  align-items: center;
  gap: 8px;
}

.settings-form input,
.settings-form select {
  font: inherit;
  font-size: 14px;
  padding: 6px 8px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--page-plane);
  color: var(--text-primary);
}

.settings-form input[type="checkbox"] {
  width: 16px;
  height: 16px;
}

.settings-form .hint {
  margin: 0;
  font-size: 12px;
  color: var(--text-muted);
}

.saved-urls-row {
  display: flex;
  gap: 8px;
  align-items: center;
}

.saved-urls-row select {
  flex: 1 1 auto;
  min-width: 0;
}

.settings-form kbd {
  font-family: inherit;
  font-size: 11px;
  padding: 1px 5px;
  border-radius: 4px;
  border: 1px solid var(--border);
  background: var(--surface-1);
}

.save-status {
  font-size: 13px;
  color: var(--text-secondary);
}

/* --- Mobile: the desktop dashboard is a strict no-scroll single-viewport
   layout; that goal doesn't translate to a phone screen, so below this
   breakpoint we deliberately switch to a normal scrollable, stacked layout. --- */
@media (max-width: 720px) {
  body {
    height: auto;
    overflow-y: auto;
  }

  .event-start-form {
    font-size: 14px;
  }

  .event-start-form label,
  .event-start-form input {
    flex: 1 1 100%;
  }

  .main-row {
    flex-direction: column;
  }

  .stats-column {
    flex: 0 0 auto;
    flex-direction: row;
    flex-wrap: wrap;
  }

  .stat-tile {
    flex: 1 1 calc(50% - 6px);
  }

  .live-view {
    flex: 0 0 auto;
    aspect-ratio: 4 / 3;
    max-height: 50vh;
  }

  .chart-section {
    flex: 0 0 240px;
  }
}
