:root {
  --bg: #0f1115;
  --panel: #181b22;
  --line: #2a2f3a;
  --text: #e8eaf0;
  --muted: #8b93a5;
  --accent: #3b82f6;
  --danger: #ef4444;
  --ok: #22c55e;
}
* { 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; }
body.center { display: grid; place-items: center; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: 16px;
  padding: 24px; width: min(420px, calc(100vw - 32px)); display: grid; gap: 14px; }
.center-card { margin: 12vh auto 0; }
h1 { margin: 0; font-size: 24px; }
.muted { color: var(--muted); margin: 0; }
.small { font-size: 13px; }
.warn { color: #fbbf24; margin: 0; font-size: 13px; }
label { display: grid; gap: 6px; font-size: 13px; color: var(--muted); }
label.check { display: flex; align-items: center; gap: 8px; color: var(--text); font-size: 15px; }
input, select, textarea { background: #0f1115; color: var(--text); border: 1px solid var(--line);
  border-radius: 10px; padding: 10px 12px; font: inherit; }
input[type=checkbox] { width: 18px; height: 18px; padding: 0; }
input[type=range] { padding: 0; width: 100%; }
button { background: #232834; color: var(--text); border: 1px solid var(--line);
  border-radius: 10px; padding: 10px 14px; font: inherit; cursor: pointer; }
button:hover { background: #2c3340; }
button.primary { background: var(--accent); border-color: var(--accent); }
button.danger { background: var(--danger); border-color: var(--danger); }
button.ghost { background: transparent; }
button.on { background: var(--ok); border-color: var(--ok); color: #04210f; }

#app { display: grid; grid-template-rows: auto 1fr auto; min-height: 100vh; gap: 10px; padding: 10px 16px 0; }
.top { display: flex; align-items: center; gap: 12px; }
.top .ghost { margin-left: auto; }
.layout { display: grid; grid-template-columns: minmax(0, 1fr) 320px; gap: 12px; min-height: 0; }
.main { display: grid; gap: 10px; align-content: start; min-width: 0; }
.main[hidden], [hidden] { display: none !important; }

.stage { position: relative; background: #000; border-radius: 14px; overflow: hidden;
  aspect-ratio: 16 / 9; max-height: 62vh; justify-self: center; width: 100%; }
.stage video { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: contain; background: #000; }
.status { position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%); background: rgba(0,0,0,.7);
  padding: 8px 14px; border-radius: 10px; font-size: 14px; text-align: center; max-width: 80%; }
.stage-wrap { display: grid; gap: 8px; }
#stats-toggle { display: none; } /* кнопка нужна только на телефоне, на ПК статистика видна сразу */
/* панель под картинкой: задержка, звук, остановка показа (не перекрывает экран) */
.stage-bar { display: flex; gap: 10px; align-items: center; justify-content: space-between; flex-wrap: wrap;
  background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 8px 12px; }
.stage-bar .delay { flex: 1 1 240px; max-width: 460px; }
.stage-actions { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.stage-actions label { display: flex; gap: 8px; align-items: center; color: var(--text); font-size: 13px; }
.stage-actions input[type=range] { width: 90px; }

.chat { display: grid; grid-template-rows: auto 1fr auto auto; background: var(--panel); border: 1px solid var(--line);
  border-radius: 12px; min-height: 320px; max-height: calc(100vh - 140px); overflow: hidden; }
.chat-title { padding: 10px 12px; font-weight: 600; border-bottom: 1px solid var(--line);
  display: flex; justify-content: space-between; align-items: center; }
#chat-close { display: none; } /* закрывать чат кнопкой нужно только на узком экране, где он ложится поверх */
.chat-list { overflow-y: auto; padding: 8px 12px; display: grid; gap: 8px; align-content: start; }
.chat-msg { font-size: 14px; }
.chat-head { display: flex; gap: 8px; align-items: baseline; }
.chat-head .muted { font-size: 12px; }
.chat-head .small-btn { margin-left: auto; }
.chat-text { white-space: pre-wrap; overflow-wrap: anywhere; }
.chat-text a { color: #7cb0ff; }
.chat-quote { border-left: 3px solid var(--accent); padding: 2px 8px; margin: 2px 0; color: var(--muted); font-size: 13px; cursor: pointer; }
.chat-system { color: var(--muted); font-size: 12px; text-align: center; }
.chat-reply { display: flex; justify-content: space-between; align-items: center; gap: 8px; padding: 4px 12px;
  font-size: 13px; color: var(--muted); border-top: 1px solid var(--line); }
.chat-form { display: grid; gap: 6px; padding: 8px; border-top: 1px solid var(--line); }
.chat-form textarea { resize: none; }
.small-btn { padding: 2px 8px; font-size: 12px; }
.badge { position: absolute; top: 10px; left: 10px; background: rgba(0,0,0,.6);
  padding: 4px 10px; border-radius: 8px; font-size: 13px; }
.stats { position: absolute; right: 10px; top: 10px; margin: 0; background: rgba(0,0,0,.65);
  padding: 8px 10px; border-radius: 8px; font: 12px/1.45 ui-monospace, Consolas, monospace; max-width: 60%; white-space: pre-wrap; }
.delay { font-size: 13px; display: grid; gap: 4px; }

.tiles { display: grid; grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); gap: 10px; align-content: start; }
.tile { position: relative; background: var(--panel); border: 2px solid transparent;
  border-radius: 12px; overflow: hidden; aspect-ratio: 16 / 10; display: grid; place-items: center; }
.tile.speaking { border-color: var(--ok); }
.tile video { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }
.tile .avatar { width: 56px; height: 56px; border-radius: 50%; background: #2c3340;
  display: grid; place-items: center; font-size: 22px; }
.tile .name { position: absolute; left: 8px; bottom: 6px; background: rgba(0,0,0,.6);
  padding: 2px 8px; border-radius: 6px; font-size: 12px; }

.panel { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 12px; }
.row { display: flex; gap: 12px; flex-wrap: wrap; }
.row label { min-width: 140px; }

.controls { display: flex; gap: 8px; flex-wrap: wrap; justify-content: center;
  padding: 10px 0 14px; position: sticky; bottom: 0; background: linear-gradient(transparent, var(--bg) 30%); }

.toast { position: fixed; left: 50%; bottom: 80px; transform: translateX(-50%);
  background: #000c; padding: 10px 16px; border-radius: 10px; max-width: calc(100vw - 32px); }

@media (max-width: 900px) {
  /* на узких экранах чат открывается кнопкой и ложится поверх */
  .layout { grid-template-columns: minmax(0, 1fr); }
  .chat { display: none; position: fixed; inset: 56px 10px 70px 10px; z-index: 5; max-height: none; }
  #app.chat-open .chat { display: grid; }
  #chat-close { display: inline-block; }
}
@media (max-width: 640px) {
  #app { padding: 8px 10px 0; }
  .top { flex-wrap: wrap; }
  .badge { font-size: 12px; top: 6px; left: 6px; padding: 2px 8px; max-width: calc(100% - 12px); }
  /* на телефоне статистика закрывала бы картинку: показываем по кнопке "Статистика" */
  .stats { display: none; max-width: calc(100% - 12px); font-size: 10px; right: 6px; top: 34px; }
  .stage-wrap.show-stats .stats { display: block; }
  #stats-toggle { display: inline-block; }
  .tiles { grid-template-columns: repeat(2, 1fr); }
  .stage-bar .delay { flex-basis: 100%; max-width: none; }
  .status { font-size: 12px; padding: 6px 10px; }
}

/* окно диагностики */
.modal { position: fixed; inset: 0; background: rgba(0, 0, 0, .65); display: grid; place-items: center; z-index: 30; }
.modal-box { background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 14px;
  width: min(960px, calc(100vw - 20px)); max-height: calc(100vh - 20px); display: grid; gap: 10px; grid-template-rows: auto auto auto auto minmax(0, 1fr); }
.modal-head { display: flex; justify-content: space-between; align-items: center; }
.modal-box .row { gap: 8px; }
#diag-text { width: 100%; height: 100%; min-height: 220px; font: 11px/1.4 ui-monospace, Consolas, monospace; resize: none; white-space: pre; overflow: auto; }

/* предупреждение показывающему: интернет не тянет */
.warn-bar { position: absolute; left: 8px; right: 8px; bottom: 8px; background: rgba(120, 53, 15, .92); color: #fff7ed;
  border: 1px solid #f59e0b; border-radius: 10px; padding: 8px 12px; font-size: 13px; line-height: 1.35; }
