:root {
  --bg: #0f1115;
  --panel: #171a21;
  --panel-2: #1f242d;
  --border: #2a2f3a;
  --text: #e6e9ef;
  --muted: #9aa3b2;
  --accent: #4f8cff;
  --danger: #ef4444;
  --ok: #22c55e;
  --radius: 12px;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
}
.hidden { display: none !important; }

/* ---------- Join page ---------- */
.join-body { display: grid; place-items: center; }
.join-card {
  width: min(420px, 92vw);
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 32px 28px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, .45);
}
.brand { display: flex; align-items: center; gap: 12px; }
.brand-logo {
  width: 40px; height: 40px; border-radius: 10px;
  background: linear-gradient(135deg, var(--accent), #7b5cff);
  display: grid; place-items: center; font-weight: 700; color: #fff;
}
.brand h1 { font-size: 20px; margin: 0; }
.join-sub { color: var(--muted); margin: 8px 0 24px; }
.join-form { display: flex; flex-direction: column; gap: 16px; }
.field { display: flex; flex-direction: column; gap: 6px; font-size: 13px; color: var(--muted); }
.field input {
  background: var(--panel-2); border: 1px solid var(--border); color: var(--text);
  border-radius: 8px; padding: 12px 14px; font-size: 15px; outline: none;
}
.field input:focus { border-color: var(--accent); }
.field input.locked { opacity: .7; }
.join-actions { margin-top: 8px; }
.join-hint { color: var(--muted); font-size: 12px; min-height: 16px; }

.btn {
  border: none; border-radius: 8px; padding: 12px 18px; font-size: 15px;
  font-weight: 600; cursor: pointer; width: 100%;
}
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover { filter: brightness(1.08); }
.btn-ghost { background: transparent; border: 1px solid var(--border); color: var(--text); width: auto; }

/* ---------- Room ---------- */
.room-body { display: flex; flex-direction: column; height: 100vh; }
.room-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 16px; background: var(--panel); border-bottom: 1px solid var(--border);
}
.room-title { display: flex; align-items: center; gap: 8px; font-weight: 600; }
.room-dot { width: 10px; height: 10px; border-radius: 50%; background: var(--ok); }
.room-header-right { display: flex; align-items: center; gap: 16px; }
.participant-count {
  background: var(--panel-2); border: 1px solid var(--border); border-radius: 20px;
  padding: 4px 12px; font-size: 13px; color: var(--muted);
}
.participant-count::before { content: "👥 "; }
.view-toggle { display: flex; background: var(--panel-2); border-radius: 8px; padding: 3px; }
.view-btn {
  background: transparent; border: none; color: var(--muted); padding: 6px 14px;
  border-radius: 6px; cursor: pointer; font-size: 13px; font-weight: 600;
}
.view-btn.active { background: var(--accent); color: #fff; }

.stage { flex: 1; overflow: hidden; padding: 12px; }
.video-grid {
  display: grid; gap: 10px; height: 100%;
  grid-template-columns: repeat(var(--cols, 1), 1fr);
  grid-auto-rows: 1fr;
}

/* Speaker view: featured tile big, strip tiles small along the bottom */
.stage[data-view="speaker"] .video-grid {
  display: flex; flex-direction: column;
}
.stage[data-view="speaker"] .tile.featured { flex: 1; }
.stage[data-view="speaker"] .tile.strip {
  display: inline-flex; width: 160px; height: 90px; flex: 0 0 auto;
}
.stage[data-view="speaker"] .video-grid {
  position: relative;
}
.stage[data-view="speaker"] .tile.strip {
  position: absolute; bottom: 12px; z-index: 3;
}

.tile {
  position: relative; background: var(--panel-2); border: 1px solid var(--border);
  border-radius: 10px; overflow: hidden; display: grid; place-items: center; min-height: 0;
}
.tile.speaking { outline: 3px solid var(--accent); }
.tile video {
  /* contain (not cover): show the camera/screenshare frame in its natural shape,
     letterboxed over the dark tile background, instead of cropping it. */
  width: 100%; height: 100%; object-fit: contain; display: none;
}
.tile.has-video video { display: block; }
.tile.has-video .tile-avatar { display: none; }
.tile-avatar {
  width: 72px; height: 72px; border-radius: 50%;
  background: linear-gradient(135deg, var(--accent), #7b5cff);
  display: grid; place-items: center; font-size: 24px; font-weight: 700; color: #fff;
}
.tile-badge {
  position: absolute; left: 8px; bottom: 8px; background: rgba(0, 0, 0, .6);
  padding: 3px 10px; border-radius: 6px; font-size: 12px; max-width: 80%;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}

/* Muted-mic indicator: hidden until the tile has .mic-muted */
.tile-mic {
  position: absolute; top: 8px; right: 8px; display: none;
  background: rgba(239, 68, 68, .85); color: #fff; border-radius: 50%;
  width: 26px; height: 26px; place-items: center; font-size: 13px; line-height: 26px;
  text-align: center;
}
.tile.mic-muted .tile-mic { display: grid; }

/* Camera off (muted, not unpublished): hide the black video, show the avatar */
.tile.cam-off video { display: none; }
.tile.cam-off .tile-avatar { display: grid; }

/* ---------- Control bar ---------- */
.control-bar {
  display: flex; align-items: center; justify-content: center; gap: 12px;
  padding: 12px; background: var(--panel); border-top: 1px solid var(--border);
}
.control-group { display: flex; align-items: stretch; }
.ctrl {
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  background: var(--panel-2); border: 1px solid var(--border); color: var(--text);
  border-radius: 10px; padding: 8px 16px; cursor: pointer; min-width: 76px;
}
.ctrl:hover { border-color: var(--accent); }
.ctrl.off { background: #3a1f22; border-color: var(--danger); }
.ctrl.active { background: #1f2e46; border-color: var(--accent); }
.ctrl-icon { font-size: 20px; }
.ctrl-label { font-size: 11px; color: var(--muted); }
.ctrl-danger { background: var(--danger); border-color: var(--danger); }
.ctrl-danger .ctrl-label { color: #fff; }
.ctrl-caret {
  background: var(--panel-2); border: 1px solid var(--border); border-left: none;
  color: var(--muted); border-radius: 0 10px 10px 0; cursor: pointer; padding: 0 8px;
}
.control-group .ctrl { border-radius: 10px 0 0 10px; }
.ctrl-caret:hover { color: var(--text); border-color: var(--accent); }

/* ---------- Overlays ---------- */
.overlay {
  position: fixed; inset: 0; background: rgba(10, 12, 16, .92);
  display: grid; place-items: center; z-index: 50; gap: 16px;
  grid-auto-flow: row; text-align: center; color: var(--muted);
}
.spinner {
  width: 44px; height: 44px; border: 4px solid var(--border);
  border-top-color: var(--accent); border-radius: 50%; animation: spin 1s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.error-box { background: var(--panel); border: 1px solid var(--border); padding: 28px; border-radius: 12px; }
.error-box h2 { margin-top: 0; }

/* ---------- Device modal ---------- */
.modal { position: fixed; inset: 0; background: rgba(0, 0, 0, .5); display: grid; place-items: center; z-index: 60; }
.modal-card { width: min(400px, 92vw); background: var(--panel); border: 1px solid var(--border); border-radius: 12px; overflow: hidden; }
.modal-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 16px; border-bottom: 1px solid var(--border); }
.modal-head h3 { margin: 0; font-size: 16px; }
.modal-close { background: none; border: none; color: var(--muted); font-size: 18px; cursor: pointer; }
.device-search { width: 100%; background: var(--panel-2); border: none; border-bottom: 1px solid var(--border); color: var(--text); padding: 12px 16px; outline: none; font-size: 14px; }
.device-list { list-style: none; margin: 0; padding: 6px; max-height: 300px; overflow-y: auto; }
.device-item { padding: 10px 12px; border-radius: 8px; cursor: pointer; font-size: 14px; }
.device-item:hover { background: var(--panel-2); }
.device-item.active { color: var(--accent); font-weight: 600; }
.device-item.active::after { content: " ✓"; }
.device-item.empty { color: var(--muted); cursor: default; }

@media (max-width: 640px) {
  .ctrl-label { display: none; }
  .ctrl { min-width: 52px; padding: 10px; }
  .stage[data-view="speaker"] .tile.strip { width: 110px; height: 62px; }
}
