:root {
  --bg: #0f1115;
  --panel: #181b22;
  --panel-2: #222631;
  --border: #2c3140;
  --text: #e7e9ee;
  --muted: #9aa1b2;
  --accent: #5b8cff;
  --danger: #e5484d;
  --ok: #3fb950;
  --warn: #d29922;
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

[hidden] { display: none !important; }

.muted { color: var(--muted); }

button, select, input {
  font: inherit;
  color: inherit;
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 12px;
}

button { cursor: pointer; white-space: nowrap; }
button:hover { border-color: var(--accent); }
button:disabled { opacity: .5; cursor: progress; }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
button.danger { border-color: var(--danger); color: #ffb3b5; }
button.active { background: var(--accent); border-color: var(--accent); color: #fff; }

/* ---------- Lobby ---------- */

.lobby {
  min-height: 100%;
  display: grid;
  place-items: center;
  padding: 16px;
}

.card {
  width: 100%;
  max-width: 360px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 24px;
  display: grid;
  gap: 14px;
}

.card h1 { margin: 0; font-size: 1.5rem; }
.card p { margin: 0; }

.card label { display: grid; gap: 6px; font-size: .9rem; color: var(--muted); }
.card input { width: 100%; color: var(--text); }
.card input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }

.actions { display: flex; gap: 10px; flex-direction: row-reverse; }
.actions button { flex: 1; }

.error { color: #ffb3b5; font-size: .9rem; }

/* ---------- Room ---------- */

.room { height: 100%; display: flex; flex-direction: column; }

.topbar {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  justify-content: space-between;
  align-items: center;
  padding: 10px 16px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
}

.room-info { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; min-width: 0; }

#room-title[data-mode]::after {
  content: attr(data-mode);
  margin-left: 8px;
  padding: 1px 6px;
  border: 1px solid var(--border);
  border-radius: 4px;
  font-size: .7rem;
  font-weight: 600;
  color: var(--muted);
  vertical-align: middle;
}

.participants { list-style: none; margin: 0; padding: 0; display: flex; flex-wrap: wrap; gap: 6px; }
.participants li {
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 2px 10px;
  font-size: .85rem;
}
.participants li.me { border-color: var(--accent); }

.controls { display: flex; flex-wrap: wrap; gap: 8px; }

.grid {
  flex: 1;
  overflow: auto;
  padding: 16px;
  display: grid;
  gap: 12px;
  grid-template-columns: repeat(auto-fit, minmax(min(100%, 420px), 1fr));
  align-content: start;
}

.empty { grid-column: 1 / -1; text-align: center; margin-top: 15vh; }

.tile {
  position: relative;
  background: #000;
  border: 1px solid var(--border);
  border-radius: 10px;
  overflow: hidden;
  aspect-ratio: 16 / 9;
}

.tile video { width: 100%; height: 100%; object-fit: contain; display: block; background: #000; }

.tile .label {
  position: absolute;
  left: 8px;
  bottom: 8px;
  background: rgba(0, 0, 0, .65);
  padding: 2px 8px;
  border-radius: 6px;
  font-size: .85rem;
}

.tile .tile-buttons { position: absolute; right: 8px; bottom: 8px; display: flex; gap: 6px; }
.tile .tile-buttons button { background: rgba(0, 0, 0, .65); padding: 4px 8px; font-size: .85rem; }

.tile:fullscreen { border: 0; border-radius: 0; }

/* ---------- Stats ---------- */

.stats {
  max-height: 40vh;
  overflow: auto;
  background: var(--panel);
  border-top: 1px solid var(--border);
  padding: 8px 16px;
}

.stats table { width: 100%; border-collapse: collapse; font-size: .85rem; font-variant-numeric: tabular-nums; }
.stats th, .stats td { text-align: left; padding: 4px 8px; border-bottom: 1px solid var(--border); white-space: nowrap; }
.stats th { color: var(--muted); font-weight: 600; }
.stats tfoot td { font-weight: 600; border-bottom: 0; }
.stats .relay { color: var(--warn); }
.stats .direct { color: var(--ok); }

/* ---------- Toasts ---------- */

.toasts {
  position: fixed;
  left: 50%;
  bottom: 16px;
  transform: translateX(-50%);
  display: grid;
  gap: 8px;
  z-index: 10;
  width: max-content;
  max-width: calc(100% - 32px);
}

.toast {
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 14px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, .4);
}

@media (max-width: 600px) {
  .topbar { padding: 10px; }
  .controls button, .controls select { padding: 6px 10px; font-size: .9rem; }
  .grid { padding: 10px; }
}
