:root {
  --bg: #10151c;
  --panel: #182029;
  --line: #2a3542;
  --fg: #e8edf3;
  --muted: #93a1b0;
  --accent: #3d8bfd;
  --danger: #d9534f;
  --ok: #3fb950;
}

* { box-sizing: border-box; }

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

.screen { height: 100%; display: flex; flex-direction: column; }
#join { align-items: center; justify-content: center; padding: 1rem; }

.card {
  width: min(380px, 100%);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 1.5rem;
  display: flex;
  flex-direction: column;
  gap: 0.9rem;
}
.card h1 { margin: 0 0 0.3rem; font-size: 1.15rem; font-weight: 600; }

label { display: flex; flex-direction: column; gap: 0.35rem; font-size: 0.85rem; color: var(--muted); }
label.row { flex-direction: row; align-items: center; gap: 0.5rem; }

input[type=text], input[type=password], input:not([type]) {
  background: #0d1218;
  border: 1px solid var(--line);
  color: var(--fg);
  border-radius: 7px;
  padding: 0.55rem 0.7rem;
  font-size: 0.95rem;
}
input:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

button {
  font: inherit;
  cursor: pointer;
  border-radius: 7px;
  border: 1px solid var(--line);
  background: var(--accent);
  color: #fff;
  padding: 0.6rem 0.9rem;
  border-color: transparent;
}
button:disabled { opacity: 0.55; cursor: progress; }

.error { color: var(--danger); font-size: 0.85rem; margin: 0; }

/* ─── Сцена ─────────────────────────────────────────────────────────────── */
#stage {
  flex: 1;
  display: grid;
  gap: 6px;
  padding: 6px;
  min-height: 0;
  grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
  align-content: center;
}
/* Демонстрация экрана занимает всю сцену — мелкий тайл делает её бесполезной. */
#stage.has-screen { grid-template-columns: 1fr; grid-auto-rows: minmax(0, 1fr); }
#stage.has-screen .tile:not(.screen) { display: none; }

.tile {
  position: relative;
  background: #000;
  border-radius: 9px;
  overflow: hidden;
  min-height: 0;
  aspect-ratio: 16 / 9;
}
#stage.has-screen .tile.screen { aspect-ratio: auto; height: 100%; }

.tile video { width: 100%; height: 100%; object-fit: contain; display: block; }
.tile.speaking { outline: 2px solid var(--ok); outline-offset: -2px; }

.tile .name {
  position: absolute; left: 6px; bottom: 6px;
  background: rgba(0,0,0,0.6);
  padding: 2px 8px; border-radius: 5px;
  font-size: 0.78rem; max-width: 80%;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.tile .muted-badge {
  position: absolute; right: 6px; bottom: 6px;
  background: rgba(0,0,0,0.6); border-radius: 5px;
  padding: 2px 6px; font-size: 0.78rem; color: var(--danger);
}
.tile .placeholder {
  position: absolute; inset: 0;
  display: grid; place-items: center;
  color: var(--muted); font-size: 2rem; background: #141b23;
}

/* ─── Панель управления ─────────────────────────────────────────────────── */
#bar {
  display: flex; gap: 8px; align-items: center;
  padding: 10px; background: var(--panel);
  border-top: 1px solid var(--line);
  flex-wrap: wrap;
}
.ctl { background: #26313d; }
.ctl[aria-pressed="false"] { background: #3a2a2a; color: var(--muted); }
.ctl.danger { background: var(--danger); margin-left: auto; }
.stat { color: var(--muted); font-size: 0.8rem; }

/* ─── Исправление вёрстки и адаптация под телефон ───────────────────────── */
/* Атрибут hidden обязан скрывать экран: стиль .screen { display: flex } иначе перебивает его, форма входа оставалась на
   экране, а панель разговора с кнопками уезжала под неё. Заодно из-за этого тайлы были вне экрана, и adaptiveStream
   LiveKit отключал приём видео (в статистике почти нет кадров). */
[hidden] { display: none !important; }

/* динамическая высота экрана: на телефонах адресная строка не должна «съедать» панель управления */
@supports (height: 100dvh) {
  html, body, .screen { height: 100dvh; }
}

#stage { overflow-y: auto; }
#bar { position: sticky; bottom: 0; padding-bottom: max(10px, env(safe-area-inset-bottom)); }
.ctl { min-height: 44px; }
input[type=text], input[type=password], input:not([type]) { font-size: 16px; }   /* iOS не увеличивает страницу при вводе */

@media (max-width: 640px) {
  #join { padding: 0.75rem; justify-content: flex-start; padding-top: 12vh; }
  .card { padding: 1.1rem; }
  #stage { grid-template-columns: 1fr; align-content: start; gap: 4px; padding: 4px; }
  #bar { gap: 6px; padding: 8px; }
  .ctl { flex: 1 1 calc(33% - 6px); padding: 0.55rem 0.4rem; font-size: 0.9rem; }
  .ctl.danger { flex: 1 1 100%; margin-left: 0; }
  .stat { order: 5; width: 100%; text-align: center; }
  .tile .name { font-size: 0.72rem; }
}
@media (max-width: 640px) and (orientation: landscape) {
  #stage { grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); }
  .ctl.danger { flex: 0 0 auto; }
}

/* Заглушка выключенной камеры и тайл-аватар участника без видео */
.tile .name, .tile .muted-badge { z-index: 2; }
.tile .placeholder { z-index: 1; }
.tile .avatar {
  width: 4.4rem; height: 4.4rem; border-radius: 50%;
  display: grid; place-items: center;
  background: #26313d; color: var(--fg); font-size: 1.9rem; font-weight: 600;
}
.tile.avatar-tile { background: #141b23; }
.tile.avatar-tile .placeholder { background: transparent; }
@media (max-width: 640px) { .tile .avatar { width: 3.4rem; height: 3.4rem; font-size: 1.5rem; } }
