:root {
  --bg: #0f1115;
  --panel: #171a21;
  --panel-2: #1f232c;
  --border: #2a2f3a;
  --text: #e8eaf0;
  --muted: #9aa3b2;
  --accent: #7c5cff;
  --accent-2: #9b82ff;
  --ok: #3ecf8e;
  --warn: #f5a524;
  --err: #ff5d6c;
  --radius: 12px;
  --app-h: 100dvh;            /* JS уточняет по visualViewport (клавиатура на телефоне) */
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
  color-scheme: dark;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg); color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}
a { color: var(--accent-2); }
button, input, select, textarea { font: inherit; color: inherit; }
input {
  width: 100%; padding: 11px 13px; border-radius: 10px;
  background: var(--panel-2); border: 1px solid var(--border); outline: none;
}
input:focus { border-color: var(--accent); }
button {
  padding: 11px 16px; border-radius: 10px; border: 1px solid transparent;
  background: var(--accent); color: #fff; cursor: pointer; font-weight: 600;
  min-height: 44px; touch-action: manipulation;
}
button:hover { background: var(--accent-2); }
button:disabled { opacity: .55; cursor: default; }
button.ghost { background: transparent; border-color: var(--border); color: var(--text); font-weight: 500; }
button.ghost:hover { background: var(--panel-2); }
button.danger { color: var(--err); border-color: #ff5d6c55; }
button.wide { width: 100%; }
:focus-visible { outline: 2px solid var(--accent-2); outline-offset: 2px; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.error { color: var(--err); min-height: 1.4em; margin: 0; }
@media (max-width: 520px) { .hide-sm { display: none; } }
/* iOS увеличивает страницу при фокусе на поле со шрифтом < 16px */
@media (pointer: coarse) { input, textarea, select { font-size: 16px !important; } }

/* ---------- Главная ---------- */
.page { max-width: 560px; margin: 0 auto; padding: calc(32px + var(--safe-t)) calc(16px + var(--safe-r)) calc(48px + var(--safe-b)) calc(16px + var(--safe-l)); }
.brand { font-size: 28px; font-weight: 800; margin: 0 0 4px; letter-spacing: -.02em; }
.brand span { color: var(--accent-2); }
.card {
  background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 20px; margin-top: 20px;
}
.card h2 { margin: 0 0 14px; font-size: 17px; }
.stack { display: flex; flex-direction: column; gap: 12px; }
.row { display: flex; gap: 10px; align-items: center; }
.row > input { flex: 1; min-width: 0; }
label { font-size: 13px; color: var(--muted); display: block; margin-bottom: 5px; }
.topbar { display: flex; justify-content: space-between; align-items: center; gap: 12px; }
.rooms { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.rooms a {
  display: block; padding: 12px 14px; border-radius: 10px; background: var(--panel-2);
  color: var(--text); text-decoration: none; border: 1px solid transparent;
}
.rooms a:hover { border-color: var(--accent); }
.rooms li { display: flex; gap: 8px; align-items: stretch; }
.rooms li > a { flex: 1; min-width: 0; }
.room-del { flex: none; width: 48px; padding: 0; font-size: 16px; color: var(--muted); }
.room-del:hover { color: var(--err); border-color: #ff5d6c55; }
.rooms .name { font-weight: 600; }
.rooms .meta { font-size: 13px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; background: var(--ok); margin-right: 6px; vertical-align: middle; }
@media (max-width: 420px) { .page { padding-top: calc(20px + var(--safe-t)); } .card { padding: 16px; } }

/* =====================================================================
   Комната. Три режима:
   1) ПК / планшет горизонтально — видео слева, панель с вкладками справа;
   2) телефон вертикально («stacked») — видео сверху (прилипает), вкладки ниже;
   3) телефон горизонтально — как ПК, но компактно.
   ===================================================================== */
.room-head {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: calc(8px + var(--safe-t)) calc(16px + var(--safe-r)) 8px calc(16px + var(--safe-l));
  border-bottom: 1px solid var(--border); background: var(--panel); min-height: 52px;
}
.head-left { min-width: 0; gap: 6px; }
.head-right { gap: 8px; flex: none; }
.room-head .back { color: var(--muted); text-decoration: none; font-size: 20px; padding: 4px 8px; margin-left: -8px; }
.room-head h1 { font-size: 17px; margin: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.status { font-size: 13px; padding: 4px 10px; border-radius: 99px; background: var(--panel-2); white-space: nowrap; }
.status[data-state="online"] { color: var(--ok); }
.status[data-state="connecting"] { color: var(--warn); }
.status[data-state="offline"] { color: var(--err); }
.small-btn { min-height: 34px; padding: 6px 12px; font-size: 13px; }

.room-layout {
  display: grid; grid-template-columns: minmax(0, 1fr) 360px; gap: 16px;
  padding: 16px calc(16px + var(--safe-r)) calc(16px + var(--safe-b)) calc(16px + var(--safe-l));
  max-width: 1600px; margin: 0 auto;
}
@media (max-width: 1100px) { .room-layout { grid-template-columns: minmax(0, 1fr) 320px; } }
.main-col { min-width: 0; }

/* ---------- Видео ---------- */
.theater { display: flex; flex-direction: column; background: #000; border-radius: var(--radius); overflow: hidden; }
.stage {
  position: relative; width: 100%; background: #000; aspect-ratio: 16 / 9;
  /* на широком низком экране видео не выталкивает панель управления за край */
  max-height: calc(var(--app-h) - 230px); min-height: 180px;
}
.player-host, .player-host > * { position: absolute; inset: 0; width: 100%; height: 100%; border: 0; display: block; }
.player-host video { object-fit: contain; background: #000; }
.stage-empty { position: absolute; inset: 0; display: grid; place-items: center; text-align: center; color: var(--muted); padding: 16px; }
.stage-empty .big { font-size: 40px; margin-bottom: 8px; }
.stage-msg { position: absolute; left: 12px; right: 12px; top: 12px; padding: 10px 14px; border-radius: 10px; background: #000c; border: 1px solid var(--border); font-size: 14px; z-index: 3; }
.stage-msg[data-kind="error"] { border-color: var(--err); }
.stage-msg[data-kind="warn"] { border-color: var(--warn); }
.blocked { position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%); z-index: 4; font-size: 17px; padding: 16px 22px; box-shadow: 0 8px 32px #000a; white-space: nowrap; }

/* ---------- Панель управления ---------- */
.controls { display: flex; align-items: center; gap: 6px; padding: 6px 8px; background: var(--panel); }
.controls .icon { width: 44px; padding: 0; font-size: 18px; flex: none; }
.seek { flex: 1; min-width: 0; display: flex; align-items: center; gap: 8px; }
.seek .time { font-variant-numeric: tabular-nums; font-size: 13px; color: var(--muted); min-width: 36px; text-align: center; }
.seek input[type="range"] { flex: 1; min-width: 60px; padding: 0; height: 36px; background: transparent; border: 0; accent-color: var(--accent); cursor: pointer; }
.controls select {
  background: var(--panel-2); border: 1px solid var(--border); border-radius: 10px; color: var(--text);
  height: 44px; padding: 0 6px; flex: none; min-width: 60px;
}
/* узко: время под ползунком, ползунку — вся ширина */
@media (max-width: 560px) {
  .seek { display: grid; grid-template-columns: auto 1fr auto; align-items: center; row-gap: 0; column-gap: 4px; }
  .seek input[type="range"] { grid-column: 1 / -1; grid-row: 1; height: 26px; }
  .seek .time { grid-row: 2; font-size: 11px; line-height: 1.1; min-width: 0; text-align: left; }
  .seek #c-dur { grid-column: 3; text-align: right; }
  .controls select { min-width: 54px; padding: 0 4px; }
}
.theater:fullscreen, .theater:-webkit-full-screen { border-radius: 0; width: 100vw; height: 100vh; }
.theater:fullscreen .stage, .theater:-webkit-full-screen .stage { aspect-ratio: auto; flex: 1; max-height: none; }
/* iPhone: у iframe нет настоящего полноэкранного режима — растягиваем «театр» на весь экран сами */
body.pseudo-fs { overflow: hidden; }
.theater.pseudo-fs {
  position: fixed; inset: 0; z-index: 100; border-radius: 0;
  padding: var(--safe-t) var(--safe-r) var(--safe-b) var(--safe-l);
}
.theater.pseudo-fs .stage { aspect-ratio: auto; flex: 1; max-height: none; min-height: 0; }

/* ---------- Под видео: название и добавление ---------- */
.now { display: flex; align-items: center; gap: 8px; margin-top: 10px; min-height: 36px; }
.now-title { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; flex: 1; }
.now .mini { width: 34px; min-height: 34px; padding: 0; font-size: 14px; flex: none; }
.now .mini-text { min-height: 34px; padding: 4px 10px; font-size: 13px; flex: none; white-space: nowrap; }
.now-link { font-size: 13px; white-space: nowrap; flex: none; text-decoration: none; padding: 6px 4px; }
.add-form { display: flex; gap: 8px; margin-top: 8px; }
.add-form > input { flex: 1; min-width: 0; }
.add-form > button { flex: none; white-space: nowrap; }
@media (max-width: 520px) { .add-form > button { width: 48px; padding: 0; font-size: 18px; } }

/* ---------- Боковая панель: участники + вкладки ---------- */
.side-col { min-width: 0; }
.side-panel {
  display: flex; flex-direction: column; background: var(--panel); border: 1px solid var(--border);
  border-radius: var(--radius); overflow: hidden; position: sticky; top: 16px;
  height: calc(var(--app-h) - 52px - 32px - var(--safe-t)); min-height: 360px;
}
.members { list-style: none; padding: 10px 12px 4px; margin: 0; display: flex; flex-wrap: wrap; gap: 6px; }
.members li { padding: 4px 10px; background: var(--panel-2); border-radius: 99px; font-size: 13px; transition: box-shadow .15s; }
.members li::before { content: ""; display: inline-block; width: 7px; height: 7px; border-radius: 50%; background: var(--ok); margin-right: 6px; vertical-align: 1px; }
.members li.me::after { content: " (вы)"; color: var(--muted); }
.members li[data-voice]::before { content: "🎙"; width: auto; height: auto; background: none; margin-right: 4px; font-size: 12px; }
.members li[data-voice="muted"]::before { content: "🔇"; }
.members li.speaking { box-shadow: 0 0 0 2px var(--ok); }
.tabs { display: flex; gap: 4px; padding: 6px 12px 0; border-bottom: 1px solid var(--border); }
.tabs button {
  flex: 1; background: transparent; color: var(--muted); border-radius: 10px 10px 0 0; font-weight: 600;
  border-bottom: 2px solid transparent; min-height: 44px; padding: 8px 6px; white-space: nowrap;
}
.tabs button:hover { background: var(--panel-2); }
.tabs button[aria-selected="true"] { color: var(--text); border-bottom-color: var(--accent); }
.badge { display: inline-block; min-width: 18px; padding: 0 5px; border-radius: 99px; background: var(--accent); color: #fff; font-size: 11px; line-height: 18px; text-align: center; margin-left: 4px; }
.tab-pane { flex: 1; min-height: 0; display: flex; flex-direction: column; }
#tab-voice, #tab-queue { padding: 12px; overflow-y: auto; overscroll-behavior: contain; }

/* Чат */
.chat-list { list-style: none; margin: 0; padding: 10px 12px; flex: 1; min-height: 0; overflow-y: auto; display: flex; flex-direction: column; gap: 2px; overscroll-behavior: contain; }
.chat-list li { max-width: 88%; overflow-wrap: anywhere; }
.chat-list .msg { align-self: flex-start; background: var(--panel-2); padding: 6px 10px; border-radius: 4px 12px 12px 12px; margin-top: 6px; }
.chat-list .msg.own { align-self: flex-end; background: #3a2f7a; border-radius: 12px 4px 12px 12px; }
.chat-list .msg.cont { margin-top: 0; border-radius: 12px; }
.msg-head { display: flex; gap: 8px; align-items: baseline; font-size: 13px; margin-bottom: 2px; }
.msg-head time { color: var(--muted); font-size: 11px; }
.msg.own .msg-head b { color: var(--accent-2); }
.msg-text { white-space: pre-wrap; }
.chat-list .sys { align-self: center; color: var(--muted); font-size: 12px; margin: 6px 0 2px; text-align: center; max-width: 100%; }
.chat-form { display: flex; gap: 8px; padding: 8px 12px; border-top: 1px solid var(--border); align-items: flex-end; }
.chat-form textarea {
  flex: 1; min-width: 0; resize: none; padding: 10px 12px; border-radius: 10px; background: var(--panel-2);
  border: 1px solid var(--border); color: var(--text); outline: none; max-height: 120px; line-height: 1.35;
}
.chat-form textarea:focus { border-color: var(--accent); }
.chat-form .icon { width: 44px; padding: 0; flex: none; }

/* Очередь: карточка — обложка слева, справа название и кнопки */
.queue-now, .queue-list li {
  display: grid; grid-template-columns: 112px minmax(0, 1fr); grid-template-rows: auto auto; column-gap: 10px; row-gap: 4px;
  background: var(--panel-2); border-radius: 10px; padding: 8px;
}
.queue-now { margin-bottom: 12px; box-shadow: inset 3px 0 0 var(--accent); }
.queue-now .thumb, .queue-list .thumb { grid-row: 1 / span 2; }
.queue-now .q-btns, .queue-list .q-btns { grid-column: 2; }
.queue-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.thumb {
  position: relative; width: 112px; aspect-ratio: 16 / 9; border-radius: 8px; overflow: hidden;
  background: #2a2f3a; display: grid; place-items: center; align-self: start;
}
.thumb[data-kind="youtube"] { background: linear-gradient(135deg, #5a1a1a, #2a1414); }
.thumb[data-kind="rutube"] { background: linear-gradient(135deg, #1d2a5a, #14182a); }
.thumb[data-kind="vk"] { background: linear-gradient(135deg, #0b4a8f, #0a2547); }
.thumb-ph { font-weight: 800; font-size: 18px; color: #ffffffb0; }
.thumb img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }
.thumb-num { position: absolute; left: 4px; top: 4px; background: #000b; color: #fff; font-size: 11px; font-weight: 700; padding: 1px 6px; border-radius: 6px; }
.q-info { min-width: 0; }
.q-title { font-weight: 600; overflow: hidden; overflow-wrap: anywhere; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; line-height: 1.3; }
.q-btns { display: flex; gap: 2px; flex-wrap: wrap; align-self: end; }
.q-btns .icon { width: 34px; min-height: 32px; padding: 0; font-size: 14px; }
@media (pointer: coarse) { .q-btns .icon { width: 40px; min-height: 40px; } .q-btns { gap: 4px; } }
@media (max-width: 360px) { .queue-now, .queue-list li { grid-template-columns: 88px minmax(0, 1fr); } .thumb { width: 88px; } }
.queue-empty { margin: 4px 0 0; }
.queue-actions { margin-top: 12px; }
.queue-actions button { width: 100%; }

/* Голос — внутри вкладки чата: панель над полем ввода + раскрывающийся список с громкостью */
.voice-bar { display: flex; align-items: center; gap: 6px; padding: 6px 12px; border-top: 1px solid var(--border); }
.voice-bar .icon { width: 40px; min-height: 40px; padding: 0; font-size: 16px; flex: none; }
.voice-bar .vb-join { min-height: 40px; padding: 6px 12px; flex: none; }
.voice-bar .active { background: var(--panel-2); border-color: var(--accent); }
#v-mute { --lvl: 0; box-shadow: 0 0 0 calc(var(--lvl) * 4px) #3ecf8e88; transition: box-shadow .1s linear; }
#v-mute.danger { box-shadow: none; }
.vb-people { flex: 1; min-width: 0; display: flex; gap: 4px; overflow: hidden; }
.vb-person {
  flex: none; width: 30px; height: 30px; border-radius: 50%; display: grid; place-items: center;
  background: var(--panel-2); font-size: 11px; font-weight: 700; text-transform: uppercase; color: var(--muted);
  border: 2px solid transparent; transition: border-color .15s, color .15s;
}
.vb-person.speaking { border-color: var(--ok); color: var(--text); }
.vb-person.muted { opacity: .5; }
.voice-panel {
  border-top: 1px solid var(--border); padding: 10px 12px; max-height: 45%; overflow-y: auto;
  overscroll-behavior: contain; background: var(--panel); flex: none;
}
.voice-panel-head { display: flex; align-items: baseline; justify-content: space-between; margin-bottom: 8px; }
.voice-hint { margin: 8px 0 0; }
kbd { background: var(--panel-2); border: 1px solid var(--border); border-radius: 4px; padding: 0 5px; font-size: 12px; }
#v-error:empty { display: none; }
#v-error { margin: 0; padding: 6px 12px 0; }
.voice-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.voice-list li { background: var(--panel-2); border-radius: 10px; padding: 10px 12px; transition: box-shadow .15s; }
.voice-list li.speaking { box-shadow: 0 0 0 2px var(--ok); }
.v-row { display: flex; align-items: center; gap: 8px; }
.v-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.v-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--warn); flex: none; }
.voice-list li[data-state="connected"] .v-dot { background: var(--ok); }
.voice-list li[data-state="failed"] .v-dot, .voice-list li[data-state="disconnected"] .v-dot { background: var(--err); }
.v-state:empty { display: none; }
.v-vol { width: 100%; margin-top: 6px; accent-color: var(--accent); padding: 0; height: 32px; background: transparent; border: 0; }
.voice-clock { margin: 4px 0 0; }

/* Подсказка перемотки с клавиатуры поверх видео */
.seek-osd {
  position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%); z-index: 5;
  background: #000b; color: #fff; padding: 10px 18px; border-radius: 12px; font-size: 18px; font-weight: 600;
  pointer-events: none; font-variant-numeric: tabular-nums; white-space: nowrap;
}
.icon-only { width: 36px; padding: 0; }
#delete-room:hover { color: var(--err); border-color: #ff5d6c55; }

/* ---------- Тосты ---------- */
.toasts {
  position: fixed; left: 50%; bottom: calc(20px + var(--safe-b)); transform: translateX(-50%);
  display: flex; flex-direction: column; gap: 8px; z-index: 200; width: min(92vw, 380px); pointer-events: none;
}
.toast { background: var(--panel-2); border: 1px solid var(--border); padding: 10px 14px; border-radius: 10px; box-shadow: 0 6px 24px #0008; animation: toast-in .2s ease; }
@keyframes toast-in { from { opacity: 0; transform: translateY(8px); } }

/* =====================================================================
   Телефон вертикально / узкий экран: видео сверху и прилипает при прокрутке,
   панель вкладок занимает остаток экрана (высоту считает JS: --panel-h).
   ===================================================================== */
@media (max-width: 640px), (max-width: 1100px) and (orientation: portrait) {
  .room-head { min-height: 48px; padding-top: calc(6px + var(--safe-t)); padding-bottom: 6px; }
  .room-layout { display: block; padding: 0 0 var(--safe-b); }
  /* sticky прилипает только в пределах родителя — «растворяем» колонку, чтобы видео держалось над панелью */
  .main-col { display: contents; }
  .theater { border-radius: 0; position: sticky; top: 0; z-index: 20; }
  .stage { max-height: 50vh; min-height: 0; }
  .now, .add-form { margin-left: calc(12px + var(--safe-l)); margin-right: calc(12px + var(--safe-r)); }
  .now { margin-top: 6px; }
  .side-col { margin-top: 10px; }
  .side-panel {
    position: static; border-radius: 0; border-left: 0; border-right: 0; border-bottom: 0;
    height: var(--panel-h, 60vh); min-height: 240px;
  }
  .chat-form { padding-bottom: calc(8px + var(--safe-b)); }
  #tab-voice, #tab-queue { padding-bottom: calc(12px + var(--safe-b)); }
  .toasts { bottom: auto; top: calc(56px + var(--safe-t)); gap: 6px; }
  .toast { padding: 7px 12px; font-size: 14px; background: #1f232ce6; }
  /* Пишем в чат: прячем всё лишнее, чтобы над клавиатурой поместилась переписка */
  body.typing .controls, body.typing .now, body.typing .add-form, body.typing .members, body.typing .voice-panel { display: none; }
}

/* Телефон горизонтально (низкий экран): как ПК, но компактно */
@media (max-height: 520px) and (orientation: landscape) and (min-width: 641px) {
  .room-head { min-height: 40px; padding-top: 4px; padding-bottom: 4px; }
  .room-head h1 { font-size: 15px; }
  .room-layout { grid-template-columns: minmax(0, 1fr) 300px; gap: 10px; padding-top: 8px; padding-bottom: calc(8px + var(--safe-b)); }
  .stage { max-height: calc(var(--app-h) - 40px - 16px - 56px); min-height: 0; }
  .now, .add-form { display: none; }
  .side-panel { top: 8px; height: calc(var(--app-h) - 40px - 16px - var(--safe-b)); min-height: 0; }
  .members { display: none; }
  .tabs button { min-height: 40px; padding: 4px; }
}
