:root {
  --bg: #0b0e14;
  --surface: #151a24;
  --surface-hover: #1d2431;
  --border: #26303f;
  --text: #e6ebf4;
  --muted: #8b97ab;
  --accent: #4da3ff;
  --danger: #ff6b6b;
  color-scheme: dark;
}

* { box-sizing: border-box; }

/*
 * The UA stylesheet hides [hidden] with `display: none`, but that is a normal
 * author-beatable rule -- any class here setting `display: grid` or `flex` wins,
 * and `el.hidden = true` then silently does nothing. Several elements in this UI
 * are toggled that way, so make `hidden` actually mean hidden.
 */
[hidden] { display: none !important; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 system-ui, -apple-system, "Segoe UI", sans-serif;
  -webkit-font-smoothing: antialiased;
}

button, input, select {
  font: inherit;
  color: inherit;
}

button {
  cursor: pointer;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.5rem 0.9rem;
}

button:hover { background: var(--surface-hover); }

.app-bar {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.75rem 1rem;
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  background: color-mix(in srgb, var(--bg) 92%, transparent);
  backdrop-filter: blur(8px);
  z-index: 10;
}

.app-bar h1 { font-size: 1.1rem; margin: 0; flex: 1; }
.app-bar .back { padding: 0.35rem 0.7rem; }

.tuner-pill {
  font-size: 0.8rem;
  color: var(--muted);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 0.2rem 0.65rem;
}

.tuner-pill.full { color: var(--danger); border-color: var(--danger); }

main { padding: 1rem; max-width: 1100px; margin: 0 auto; }

/* Login */
.login form {
  max-width: 320px;
  margin: 15vh auto;
  display: grid;
  gap: 0.75rem;
}

.login input {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.6rem 0.75rem;
}

.error { color: var(--danger); }

/* Guide */
.guide-controls {
  display: flex;
  gap: 1rem;
  align-items: center;
  margin-bottom: 1rem;
}

.guide-controls input[type=search] {
  flex: 1;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.55rem 0.75rem;
}

.hd-only { color: var(--muted); font-size: 0.9rem; display: flex; gap: 0.4rem; align-items: center; }

.channel-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 0.75rem;
}

.channel {
  display: flex;
  gap: 0.85rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 0.85rem;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s;
}

.channel:hover, .channel:focus-visible {
  background: var(--surface-hover);
  border-color: var(--accent);
  outline: none;
}

.channel-number {
  font-variant-numeric: tabular-nums;
  font-weight: 600;
  color: var(--muted);
  min-width: 2.5ch;
  text-align: right;
}

.channel-body { min-width: 0; flex: 1; }

.channel h3 {
  margin: 0 0 0.15rem;
  font-size: 0.95rem;
  display: flex;
  gap: 0.4rem;
  align-items: center;
}

.badge {
  font-size: 0.65rem;
  background: var(--accent);
  color: #04101f;
  border-radius: 4px;
  padding: 0 0.3rem;
  font-weight: 700;
}

.channel p {
  margin: 0;
  font-size: 0.85rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.channel .now { color: var(--text); }
.channel .next { color: var(--muted); font-size: 0.78rem; }

.progress {
  height: 3px;
  background: var(--border);
  border-radius: 2px;
  margin-top: 0.5rem;
  overflow: hidden;
}

.progress span { display: block; height: 100%; background: var(--accent); }

/* Player */
.player {
  position: relative;
  background: #000;
  border-radius: 12px;
  overflow: hidden;
  aspect-ratio: 16 / 9;
}

.player video { width: 100%; height: 100%; display: block; background: #000; }

.player:fullscreen { border-radius: 0; aspect-ratio: auto; height: 100%; }

.player-overlay {
  position: absolute;
  inset: 0;
  display: grid;
  place-content: center;
  gap: 1rem;
  justify-items: center;
  background: rgba(0, 0, 0, 0.72);
  text-align: center;
  padding: 1rem;
}

.cast-overlay {
  position: absolute;
  inset: 0;
  display: grid;
  place-content: center;
  justify-items: center;
  gap: 1.25rem;
  background: var(--bg);
  text-align: center;
  padding: 1rem;
}

.cast-target {
  margin: 0;
  color: var(--accent);
  font-weight: 600;
}

.cast-controls { display: flex; gap: 0.6rem; }

.cast-controls button[disabled] {
  opacity: 0.45;
  cursor: not-allowed;
}

.player-bar {
  display: flex;
  align-items: center;
  gap: 1rem;
  padding: 0.85rem 0.25rem;
}

.player-title { flex: 1; min-width: 0; }
.player-title strong { display: block; }
.player-now { color: var(--muted); font-size: 0.85rem; }
.player-actions { display: flex; gap: 0.5rem; align-items: center; }

.profile {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.45rem 0.6rem;
}

/* Tuners panel */
.tuners-panel {
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
  padding: 0.9rem 1rem;
  margin-bottom: 1rem;
}

.tuners-head { display: flex; align-items: center; gap: 1rem; }
.tuners-head h3 { margin: 0; font-size: 1rem; flex: 1; }
.tuners-close { padding: 0.15rem 0.55rem; line-height: 1; }

.tuner {
  display: grid;
  grid-template-columns: 4.5rem 1fr auto auto;
  gap: 0.75rem;
  align-items: center;
  padding: 0.5rem 0;
  border-top: 1px solid var(--border);
  font-size: 0.88rem;
}

.tuner-name { color: var(--muted); font-variant-numeric: tabular-nums; }
.tuner-ch { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tuner.idle .tuner-ch { font-weight: 400; }
.tuner.stale { color: var(--danger); }
.tuner button { padding: 0.25rem 0.6rem; font-size: 0.8rem; }

.tuners-foot {
  display: flex;
  gap: 0.75rem;
  align-items: center;
  flex-wrap: wrap;
  margin-top: 0.85rem;
  padding-top: 0.75rem;
  border-top: 1px solid var(--border);
}

.tuner-pill { cursor: pointer; }

@media (max-width: 600px) {
  .tuner { grid-template-columns: 3.5rem 1fr auto; }
  .tuner > .muted:nth-child(3) { grid-column: 2 / -1; font-size: 0.78rem; }
}

/* Technical information */
.stats {
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
  margin-top: 0.5rem;
}

.stats > summary {
  cursor: pointer;
  padding: 0.75rem 1rem;
  font-size: 0.9rem;
  color: var(--muted);
  user-select: none;
}

.stats[open] > summary { border-bottom: 1px solid var(--border); color: var(--text); }

.stats-body { padding: 0.5rem 1rem 1rem; }

.stats-body h4 {
  margin: 1rem 0 0.5rem;
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
  font-weight: 600;
}

.stat-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  gap: 0.75rem 1.25rem;
}

.stat { display: grid; gap: 0.15rem; align-content: start; }

.stat-label { font-size: 0.72rem; color: var(--muted); }

.stat-value {
  font-variant-numeric: tabular-nums;
  font-size: 0.95rem;
}

.stat-value.good { color: #58d68d; }
.stat-value.warn { color: #f5b041; }
.stat-value.bad { color: var(--danger); }

.meter {
  height: 4px;
  background: var(--border);
  border-radius: 2px;
  overflow: hidden;
  margin-top: 0.2rem;
}

.meter span { display: block; height: 100%; background: #58d68d; }
.meter.warn span { background: #f5b041; }
.meter.bad span { background: var(--danger); }

.source-format {
  margin: 0.75rem 0 0;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.7rem;
  color: var(--muted);
  word-break: break-word;
}

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

@media (max-width: 600px) {
  main { padding: 0.65rem; }
  .channel-list { grid-template-columns: 1fr; }
}
