:root {
  --bg: #0e0f13;
  --panel: #16181f;
  --panel-2: #1d2029;
  --border: #2a2e3a;
  --text: #e8e9ee;
  --muted: #8b90a0;
  --accent: #7c5cff;
  --accent-glow: rgba(124, 92, 255, 0.45);
  --live: #ff4d5e;
  --radius: 10px;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
button { font: inherit; color: inherit; cursor: pointer; }
[hidden] { display: none !important; }
.spacer { flex: 1; }

/* ---------- top bar ---------- */
.topbar {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--panel);
  flex-wrap: wrap;
}
.brand { font-weight: 700; font-size: 16px; display: flex; align-items: center; gap: 8px; }
.dot { width: 10px; height: 10px; border-radius: 50%; background: var(--live); box-shadow: 0 0 8px var(--live); }
.segmented { display: inline-flex; background: var(--panel-2); border: 1px solid var(--border); border-radius: 8px; padding: 2px; }
.segmented button { background: none; border: 0; padding: 5px 14px; border-radius: 6px; color: var(--muted); }
.segmented button[aria-pressed="true"] { background: var(--accent); color: #fff; }
.toggle { display: inline-flex; align-items: center; gap: 6px; color: var(--muted); cursor: pointer; }
.status { color: var(--muted); }
.status.error { color: var(--live); }
.ghost { background: none; border: 1px solid var(--border); border-radius: 6px; padding: 4px 10px; color: var(--muted); }
.ghost:hover { color: var(--text); }
a.ghost { text-decoration: none; }

/* ---------- main layout ---------- */
.layout { flex: 1; display: grid; grid-template-columns: 1fr 300px; min-height: 0; }
.stage {
  position: relative;
  padding: 12px;
  display: grid;
  gap: 10px;
  min-height: 0;
  grid-template-columns: 1fr;
  grid-auto-rows: 1fr;
}
body[data-layout="grid"] .stage[data-count="2"] { grid-template-columns: 1fr 1fr; grid-template-rows: 1fr; }
body[data-layout="grid"] .stage[data-count="3"],
body[data-layout="grid"] .stage[data-count="4"] { grid-template-columns: 1fr 1fr; grid-template-rows: 1fr 1fr; }

.empty { position: absolute; inset: 0; display: grid; place-content: center; text-align: center; color: var(--muted); }
.empty h2 { color: var(--text); margin: 0 0 6px; }

/* ---------- stage tiles ---------- */
.tile {
  position: relative;
  min-height: 0;
  min-width: 0;
  background: #000;
  border-radius: var(--radius);
  border: 2px solid transparent;
  overflow: hidden;
  cursor: pointer;
  transition: border-color .15s, box-shadow .15s;
}
.tile.active { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-glow); }
.tile-video, .tile-video .player { position: absolute; inset: 0; }
.player video { width: 100%; height: 100%; object-fit: contain; background: #000; display: block; }
.player-overlay {
  position: absolute; inset: 0; display: grid; place-content: center;
  color: var(--muted); font-size: 15px; pointer-events: none;
}
.player[data-state="playing"] .player-overlay { display: none; }
.player:not([data-state="playing"]) video { opacity: .35; }

.tile-bar {
  position: absolute; left: 0; right: 0; bottom: 0;
  display: flex; align-items: center; gap: 8px;
  padding: 18px 10px 8px;
  background: linear-gradient(transparent, rgba(0,0,0,.75));
  opacity: 0; transition: opacity .15s;
}
.tile:hover .tile-bar, .tile.active .tile-bar { opacity: 1; }
.tile-name { font-weight: 600; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tile-bar > * { flex-shrink: 0; }
.tile-bar > .tile-name, .tile-bar > .tile-volume { flex-shrink: 1; }
.badge { font-size: 11px; padding: 1px 6px; border-radius: 4px; background: rgba(255,255,255,.12); color: var(--muted); }
.tile-bar { user-select: none; -webkit-user-select: none; }
.tile-audio { font-size: 14px; line-height: 1; }
.tile-quality {
  background: rgba(255,255,255,.1); color: var(--text); border: 0; border-radius: 6px;
  font: inherit; font-size: 12px; padding: 4px 6px; height: 28px; cursor: pointer;
}
.tile-quality:hover { background: rgba(255,255,255,.2); }
.tile-quality option { background: var(--panel-2); color: var(--text); }
.tile-quality:focus { outline: none; }
.tile-quality:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.tile.active .tile-audio[aria-pressed="true"] { background: var(--accent); }
.tile.muted { border-color: var(--muted); box-shadow: none; }
.tile.muted .tile-volume { opacity: .4; }
.icon-btn:focus { outline: none; }
.icon-btn:focus-visible, .tile-volume:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.tile-volume { width: 90px; min-width: 36px; accent-color: var(--accent); display: none; }
.tile.active .tile-volume { display: inline-block; }
.icon-btn { background: rgba(255,255,255,.1); border: 0; border-radius: 6px; width: 28px; height: 28px; }
.icon-btn:hover { background: rgba(255,255,255,.2); }
.tile:fullscreen { border: 0; border-radius: 0; }

/* ---------- sidebar ---------- */
.sidebar {
  border-left: 1px solid var(--border);
  background: var(--panel);
  padding: 12px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.sidebar h3 { margin: 0; font-size: 12px; letter-spacing: .08em; text-transform: uppercase; color: var(--muted); }
.stream-list { display: flex; flex-direction: column; gap: 10px; }
.card {
  display: flex; flex-direction: column; gap: 6px; text-align: left;
  background: var(--panel-2); border: 2px solid var(--border); border-radius: var(--radius);
  padding: 6px; transition: border-color .15s;
}
.card:hover { border-color: #3d4252; }
.card.staged { border-color: var(--accent); }
.card.offline { opacity: .5; }
.card-thumb { position: relative; aspect-ratio: 16 / 9; background: #000; border-radius: 6px; overflow: hidden; }
.card-thumb .player { position: absolute; inset: 0; }
.card-thumb .player-overlay { font-size: 12px; }
.card.staged .card-thumb { display: none; }
.live-pill {
  position: absolute; top: 6px; left: 6px; z-index: 1;
  background: var(--live); color: #fff; font-size: 10px; font-weight: 700;
  padding: 1px 6px; border-radius: 4px;
}
.card.offline .live-pill { background: var(--muted); }
.card-name { font-weight: 600; }
.card-meta, .card-hint { font-size: 12px; color: var(--muted); }
.card-hint { color: var(--accent); }
.help { margin-top: auto; font-size: 12px; color: var(--muted); }
kbd { background: var(--panel-2); border: 1px solid var(--border); border-radius: 4px; padding: 0 4px; font-size: 11px; }

/* ---------- overlays ---------- */
.unmute, .toast {
  position: fixed; left: 50%; transform: translateX(-50%); z-index: 20;
  border-radius: 999px; padding: 10px 18px; border: 0;
}
.unmute { bottom: 24px; background: var(--accent); color: #fff; font-weight: 600; box-shadow: 0 6px 24px var(--accent-glow); }
.toast { top: 64px; background: var(--panel-2); border: 1px solid var(--border); }

.modal { position: fixed; inset: 0; z-index: 30; background: rgba(0,0,0,.7); display: grid; place-items: center; padding: 16px; }
.modal-card {
  width: min(360px, 100%); background: var(--panel); border: 1px solid var(--border);
  border-radius: 14px; padding: 24px; display: flex; flex-direction: column; gap: 10px;
}
.modal-card h2 { margin: 0; }
.modal-card p { margin: 0 0 6px; color: var(--muted); }
.modal-card input {
  background: var(--panel-2); border: 1px solid var(--border); border-radius: 8px;
  padding: 10px 12px; color: var(--text); font: inherit;
}
.modal-card input:focus { outline: 2px solid var(--accent); border-color: transparent; }
.primary { background: var(--accent); border: 0; border-radius: 8px; padding: 10px; color: #fff; font-weight: 600; }
.error-text { color: var(--live); min-height: 1em; font-size: 13px; }

/* ---------- small screens ---------- */
@media (max-width: 800px) {
  body { overflow: auto; }
  .layout { grid-template-columns: 1fr; }
  .stage { padding: 8px; aspect-ratio: 16 / 9; }
  body[data-layout="grid"] .stage { aspect-ratio: auto; height: 70vh; }
  .sidebar { border-left: 0; border-top: 1px solid var(--border); }
  .stream-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); }
  .help { display: none; }
}
