/* Тёмная серая тема: почти чёрный фон, плоские поверхности, тонкие границы.
   Акцент намеренно неброский — интерфейс не должен спорить с картинкой. */
:root {
  --bg: #08090a;
  --bg-2: #101113;
  --bg-3: #17181a;
  --bg-4: #1f2124;
  --border: #232529;
  --border-soft: #1a1c1f;
  --text: #e4e5e6;
  --muted: #83858a;
  --faint: #5d6065;
  --accent: #8e9aa6;
  --accent-dim: #262b31;
  --accent-ink: #08090a;
  --ok: #6f9c6a;
  --warn: #b08f52;
  --err: #b4635b;
  --radius: 10px;
  --radius-sm: 8px;
  --tap: 36px;
  --ease: cubic-bezier(0.22, 0.61, 0.36, 1);
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

html,
body {
  margin: 0;
  padding: 0;
  height: 100%;
}

/* Комната никогда не прокручивается целиком: высоту задаёт visualViewport
   (в CSS-переменной --app-height, её обновляет room.js), иначе при открытой
   клавиатуре браузер подкручивал бы страницу к полю ввода и весь экран
   «дёргался». Лобби оставляем обычным документом — там есть прокрутка. */
body.room {
  height: var(--app-height, 100dvh);
  overflow: hidden;
  position: fixed;
  inset: 0;
  width: 100%;
}

body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 "Inter", "Segoe UI", system-ui, -apple-system, sans-serif;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior: none;
}

a {
  color: var(--accent);
  text-decoration: none;
}

a:hover {
  text-decoration: underline;
}

button,
input,
select,
textarea {
  font: inherit;
  color: inherit;
}

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

::selection {
  background: var(--accent-dim);
}

*::-webkit-scrollbar {
  width: 9px;
  height: 9px;
}

*::-webkit-scrollbar-track {
  background: transparent;
}

*::-webkit-scrollbar-thumb {
  background: var(--bg-4);
  border-radius: 999px;
}

*::-webkit-scrollbar-thumb:hover {
  background: #2f353b;
}

/* Иконки */
.ic {
  width: 18px;
  height: 18px;
  flex: none;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.7;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.ic.fill {
  fill: currentColor;
  stroke: none;
}

.ic.lg {
  width: 22px;
  height: 22px;
}

/* Кнопки */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  min-height: var(--tap);
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg-3);
  color: var(--text);
  cursor: pointer;
  transition: background 0.16s var(--ease), border-color 0.16s var(--ease),
    color 0.16s var(--ease), transform 0.12s var(--ease);
  white-space: nowrap;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

.btn:hover:not(:disabled) {
  background: var(--bg-4);
  border-color: #2e3135;
}

.btn:active:not(:disabled) {
  background: #141517;
  transform: scale(0.97);
}

.btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.btn.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-ink);
  font-weight: 600;
}

.btn.primary:hover:not(:disabled) {
  background: #a3aeb9;
  border-color: #a3aeb9;
}

/* Полупрозрачные кнопки поверх видео: без своей подложки не перекрывают кадр. */
.btn.ghost {
  background: rgba(12, 13, 14, 0.55);
  border-color: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(6px);
}

.btn.ghost:hover:not(:disabled) {
  background: rgba(28, 30, 33, 0.75);
  border-color: rgba(255, 255, 255, 0.2);
}

.btn.ghost[aria-pressed="true"] {
  background: rgba(142, 154, 166, 0.28);
  border-color: rgba(142, 154, 166, 0.6);
  color: #fff;
}

.btn.sm {
  min-height: 30px;
  padding: 5px 10px;
  font-size: 13px;
}

.btn.icon {
  width: var(--tap);
  min-height: var(--tap);
  padding: 0;
}

.btn.icon.sm {
  width: 30px;
  min-height: 30px;
}

.btn[aria-pressed="true"] {
  background: var(--accent-dim);
  border-color: var(--accent);
  color: var(--accent);
}

.btn.danger {
  color: var(--err);
  border-color: #3a2725;
  background: #1e1514;
}

.btn.danger:hover:not(:disabled) {
  background: #2a1c1b;
  border-color: #4d302d;
}

/* Поля */
input[type="text"],
input[type="url"],
input[type="number"],
select,
textarea {
  width: 100%;
  min-height: var(--tap);
  padding: 9px 11px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg);
  outline: none;
  transition: border-color 0.12s ease;
}

input::placeholder,
textarea::placeholder {
  color: var(--faint);
}

input:hover,
select:hover {
  border-color: #2e343a;
}

input:focus,
select:focus,
textarea:focus {
  border-color: var(--accent);
}

/* Карточки и мелочь */
.card {
  background: var(--bg-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

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

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

.row {
  display: flex;
  gap: 10px;
  align-items: center;
}

.grow {
  flex: 1;
  min-width: 0;
}

.tag {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 3px 9px;
  border-radius: 999px;
  background: var(--bg-3);
  border: 1px solid var(--border);
  font-size: 12px;
  color: var(--muted);
  white-space: nowrap;
}

.tag.live {
  color: var(--ok);
  border-color: #2c4230;
}

.tag.host {
  color: var(--accent);
  border-color: #2c3a44;
}

.dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--faint);
  display: inline-block;
  flex: none;
}

.dot.on {
  background: var(--ok);
}

.dot.off {
  background: var(--err);
}

/* Уведомления */
.toast-host {
  position: fixed;
  left: 50%;
  bottom: 20px;
  transform: translateX(-50%);
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 100;
  width: min(420px, calc(100vw - 24px));
}

.toast {
  padding: 10px 14px;
  border-radius: var(--radius-sm);
  background: var(--bg-3);
  border: 1px solid var(--border);
  animation: rise 0.18s ease;
}

.toast.err {
  border-color: #3a2725;
  background: #1e1514;
}

.toast.ok {
  border-color: #2c4230;
  background: #141d16;
}

@keyframes rise {
  from {
    opacity: 0;
    transform: translateY(6px);
  }
}

/* --- Анимации ------------------------------------------------- */

@keyframes fade-up {
  from {
    opacity: 0;
    transform: translateY(8px);
  }
}

@keyframes pop-in {
  from {
    opacity: 0;
    transform: scale(0.94);
  }
}

@keyframes fade-in {
  from {
    opacity: 0;
  }
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

@keyframes pulse-dot {
  0%,
  100% {
    opacity: 1;
  }
  50% {
    opacity: 0.35;
  }
}

/* Появление карточек комнат по очереди — не рандом, а по индексу, иначе
   при каждом обновлении всё мигает заново. */
.room-card {
  animation: fade-up 0.26s var(--ease) both;
}

.room-card:nth-child(1) { animation-delay: 0ms; }
.room-card:nth-child(2) { animation-delay: 25ms; }
.room-card:nth-child(3) { animation-delay: 50ms; }
.room-card:nth-child(4) { animation-delay: 75ms; }
.room-card:nth-child(n + 5) { animation-delay: 100ms; }

.lobby-form,
.lobby-head {
  animation: fade-up 0.3s var(--ease) both;
}

.msg:not(.system) {
  animation: pop-in 0.2s var(--ease) both;
}

.modal {
  animation: pop-in 0.2s var(--ease) both;
}

.tag.live::before {
  content: "";
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: currentColor;
  animation: pulse-dot 1.8s ease-in-out infinite;
}

/* Значок непрочитанных */
.badge {
  min-width: 18px;
  height: 18px;
  padding: 0 5px;
  border-radius: 999px;
  background: var(--accent);
  color: var(--accent-ink);
  font-size: 11px;
  font-weight: 700;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-variant-numeric: tabular-nums;
  animation: pop-in 0.2s var(--ease);
}

/* Сообщения: непрочитанные подсвечены, свои помечены галочкой прочтения */
.msg.unseen .bubble {
  border-color: #3a3f45;
  background: #1d1f22;
}

.msg.pending .seen,
.msg.mine .seen {
  font-size: 11px;
  color: var(--faint);
  text-align: right;
  min-height: 13px;
}

.msg.mine .seen:not(:empty)::before {
  content: "✓ ";
}

/* Плавное вращение иконки при ручном обновлении */
.btn.refresh.spin .ic {
  animation: spin 0.7s var(--ease) infinite;
}

@media (prefers-reduced-motion: reduce) {
  * {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}

/* Лобби */
.lobby {
  max-width: 940px;
  margin: 0 auto;
  padding: 36px 18px 56px;
}

.lobby-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 24px;
  flex-wrap: wrap;
}

.lobby-head h1 {
  margin: 0;
  font-size: 24px;
  font-weight: 650;
  letter-spacing: -0.01em;
}

.lobby-form {
  display: grid;
  grid-template-columns: 1fr 1fr auto;
  gap: 12px;
  align-items: end;
  padding: 18px;
}

.lobby-form label {
  display: block;
}

.lobby-form label > span {
  display: block;
  font-size: 12.5px;
  color: var(--muted);
  margin-bottom: 6px;
}

/* Карточка комнаты — ссылка целиком: нажать можно в любое место строки. */
.room-card {
  display: flex;
  gap: 12px;
  align-items: center;
  padding: 13px 15px;
  margin-bottom: 9px;
  background: var(--bg-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: inherit;
  text-decoration: none;
  transition: background 0.16s var(--ease), border-color 0.16s var(--ease),
    transform 0.16s var(--ease);
}

.room-card:hover {
  text-decoration: none;
  background: var(--bg-3);
  border-color: #2e3135;
  transform: translateY(-1px);
}

.room-card:active {
  transform: translateY(0) scale(0.995);
}

.room-play {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  flex: none;
  border-radius: 50%;
  background: var(--bg-3);
  border: 1px solid var(--border);
  color: var(--muted);
}

.room-play .ic {
  width: 16px;
  height: 16px;
}

.room-card:hover .room-play {
  color: var(--text);
}

.room-code {
  flex: none;
  font-size: 12px;
  font-variant-numeric: tabular-nums;
  color: var(--faint);
  border: 1px solid var(--border-soft);
  border-radius: var(--radius-sm);
  padding: 3px 7px;
}

.rooms-empty {
  padding: 22px;
  text-align: center;
  font-size: 14px;
  color: var(--muted);
  background: var(--bg-2);
  border: 1px dashed var(--border);
  border-radius: var(--radius);
}

.rooms-head {
  margin: 22px 0 12px;
  flex-wrap: wrap;
}

.rooms-head h2 {
  margin: 0;
  font-size: 16px;
}

@media (max-width: 720px) {
  .lobby-form {
    grid-template-columns: 1fr;
  }
}

/* Комната: оболочка */
.room-shell {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 350px;
  height: 100%;
  overflow: hidden;
}

.stage {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  background: #0a0c0e;
}

/* Верхняя панель комнаты.
   flex-wrap здесь был причиной моргания: текст бейджа синхронизации
   меняется каждые 500 мс («синхронно», «+0.42с», «перемотка по комнате»),
   при переносах это меняло высоту шапки, а вместе с ней — размер кадра. */
.topbar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  background: var(--bg-2);
  border-bottom: 1px solid var(--border-soft);
  flex-wrap: nowrap;
}

.room-title {
  font-size: 15px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  min-width: 0;
  flex: 1 1 auto;
}

.sync-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  font-size: 12px;
  padding: 4px 9px;
  border-radius: 999px;
  background: var(--bg-3);
  border: 1px solid var(--border);
  color: var(--muted);
  white-space: nowrap;
  flex: none;
  /* Фиксированная ширина: смена текста не должна двигать шапку. */
  min-width: 104px;
  max-width: 40vw;
  overflow: hidden;
  text-overflow: ellipsis;
}

.sync-badge.typing {
  min-width: 0;
  max-width: 40vw;
  color: var(--text);
  background: var(--bg-4);
  animation: fade-in 0.18s var(--ease);
}

/* Видео */
.video-wrap {
  flex: 1;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 0;
  background: #000;
  /* Нижняя граница в пикселях: даже если окно совсем низкое, кадр не
     схлопнется в ноль. */
  min-block-size: 150px;
  overflow: hidden;
}

video {
  width: 100%;
  height: 100%;
  background: #000;
  outline: none;
}

/* Панель управления поверх кадра */
.player {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  pointer-events: none;
  opacity: 1;
  transition: opacity 0.22s var(--ease);
}

.player[hidden] {
  display: none;
}

/* Скрытая панель не должна перехватывать клики по кадру. */
.player.idle,
.player.idle .player-center,
.player.idle .player-bar,
.player.idle .player-scrim {
  pointer-events: none;
}

.player.idle {
  opacity: 0;
}

.player-scrim {
  position: absolute;
  inset: 0;
  background: linear-gradient(
    to bottom,
    rgba(0, 0, 0, 0.32) 0%,
    rgba(0, 0, 0, 0) 22%,
    rgba(0, 0, 0, 0) 52%,
    rgba(0, 0, 0, 0.62) 100%
  );
  opacity: 0.85;
  transition: opacity 0.22s var(--ease);
}

.player-center {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 66px;
  height: 66px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  border: 1px solid rgba(255, 255, 255, 0.16);
  background: rgba(10, 11, 12, 0.62);
  color: #fff;
  cursor: pointer;
  pointer-events: auto;
  backdrop-filter: blur(8px);
  transition: transform 0.18s var(--ease), background 0.18s var(--ease);
}

.player-center:hover {
  background: rgba(30, 32, 35, 0.78);
  transform: translate(-50%, -50%) scale(1.06);
}

.player-center:active {
  transform: translate(-50%, -50%) scale(0.96);
}

.player-center .ic {
  width: 26px;
  height: 26px;
}

.player-bar {
  position: relative;
  padding: 8px 10px 10px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  pointer-events: auto;
  background: linear-gradient(to top, rgba(0, 0, 0, 0.66), rgba(0, 0, 0, 0));
}

.empty-state {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  text-align: center;
  padding: 22px;
  background: #0a0c0e;
}

.empty-state h2 {
  margin: 0;
  font-size: 18px;
}

.empty-state p {
  margin: 0 0 6px;
  max-width: 460px;
  font-size: 14px;
}

.scrub {
  display: flex;
  align-items: center;
  gap: 10px;
}

.time {
  font-size: 12.5px;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
  width: 46px;
  flex: none;
}

.time.right {
  text-align: right;
}

input[type="range"] {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  height: 20px;
  background: transparent;
  outline: none;
  cursor: pointer;
}

input[type="range"]::-webkit-slider-runnable-track {
  height: 5px;
  border-radius: 999px;
  background: var(--bg-4);
}

input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 14px;
  height: 14px;
  margin-top: -4.5px;
  border-radius: 50%;
  background: var(--accent);
  border: none;
}

input[type="range"]::-moz-range-track {
  height: 5px;
  border-radius: 999px;
  background: var(--bg-4);
}

input[type="range"]::-moz-range-thumb {
  width: 14px;
  height: 14px;
  border: none;
  border-radius: 50%;
  background: var(--accent);
}

.controls-row {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
}

/* Боковая панель: чат и зрители */
.side {
  display: flex;
  flex-direction: column;
  min-height: 0;
  border-left: 1px solid var(--border-soft);
  background: var(--bg-2);
}

.side-tabs {
  display: flex;
  gap: 2px;
  padding: 6px;
  border-bottom: 1px solid var(--border-soft);
}

.side-tabs button {
  flex: 1;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 32px;
  padding: 6px 8px;
  background: none;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  color: var(--muted);
  cursor: pointer;
  font-size: 13.5px;
  transition: background 0.12s ease, color 0.12s ease;
}

.side-tabs button:hover {
  background: var(--bg-3);
  color: var(--text);
}

.side-tabs button.active {
  background: var(--bg-3);
  border-color: var(--border);
  color: var(--text);
}

.side-tabs .count {
  font-size: 11.5px;
  color: var(--faint);
  font-variant-numeric: tabular-nums;
}

.chat-log {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 9px;
  -webkit-overflow-scrolling: touch;
}

.msg {
  max-width: 92%;
}

.msg .meta {
  font-size: 11.5px;
  color: var(--faint);
  margin-bottom: 3px;
}

.msg .bubble {
  padding: 7px 11px;
  border-radius: var(--radius);
  background: var(--bg-3);
  border: 1px solid var(--border-soft);
  word-break: break-word;
  white-space: pre-wrap;
  font-size: 14px;
}

.msg.mine {
  align-self: flex-end;
}

.msg.mine .meta {
  text-align: right;
}

.msg.mine .bubble {
  background: var(--accent-dim);
  border-color: #35485a;
}

.msg.system {
  align-self: center;
  max-width: 100%;
}

.msg.system .bubble {
  background: transparent;
  border: none;
  color: var(--faint);
  font-size: 12.5px;
  text-align: center;
  padding: 2px;
}

.people {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 10px;
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.person {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  border-radius: var(--radius-sm);
  background: var(--bg-3);
  font-size: 14px;
}

.person .name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.person .ic.star {
  width: 13px;
  height: 13px;
  color: var(--warn);
}

.person .drift {
  margin-left: auto;
  font-size: 11.5px;
  font-variant-numeric: tabular-nums;
  color: var(--muted);
}

.chat-form {
  display: flex;
  gap: 7px;
  padding: 8px;
  border-top: 1px solid var(--border-soft);
  background: var(--bg-2);
}

.chat-form input {
  flex: 1;
  min-width: 0;
}

/* Модальные окна */
.modal-back {
  position: fixed;
  inset: 0;
  background: #05070a;
  opacity: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
  padding: 18px;
}

.modal {
  width: min(540px, 100%);
  padding: 18px;
  max-height: 90dvh;
  overflow-y: auto;
}

.modal-head {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 4px;
}

.modal-head h3 {
  margin: 0;
  font-size: 17px;
  flex: 1;
}

.modal h3 {
  margin: 0 0 4px;
}

label.field {
  display: block;
  margin-top: 14px;
}

label.field > span {
  display: block;
  font-size: 12.5px;
  color: var(--muted);
  margin-bottom: 6px;
}

.check {
  display: flex;
  align-items: flex-start;
  gap: 9px;
  margin-top: 14px;
  padding: 10px 11px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg-3);
  cursor: pointer;
  font-size: 13.5px;
  color: var(--muted);
}

.check input {
  width: 16px;
  height: 16px;
  margin: 1px 0 0;
  accent-color: var(--accent);
  flex: none;
}

.hint {
  font-size: 12.5px;
  color: var(--faint);
  margin-top: 8px;
}

.modal-actions {
  display: flex;
  gap: 8px;
  margin-top: 18px;
  justify-content: flex-end;
}

/* Телефон: видео сверху, чат под ним, оба видны без прокрутки */
@media (max-width: 900px) {
  .room-shell {
    grid-template-columns: 1fr;
    grid-template-rows: auto auto minmax(0, 1fr);
  }

  /* Высота кадра задаётся шириной, а не текущей высотой окна. Это ключевой
     момент: клавиатура меняет высоту окна, и видео с привязкой к ней меняло
     бы размер — вот тот самый «дёргающийся» экран. svh — высота без
     клавиатуры и без панелей браузера, она стабильна, поэтому в упор на
     неё можно безопасно ограничивать кадр на низких экранах.
     Освободившееся место спокойно занимает чат. */
  .video-wrap {
    flex: none;
    /*aspect-ratio: 16 / 9;
    max-height: min(40vw, 52svh);
    */
    min-height: 0;
  }

  .side {
    border-left: none;
    border-top: 1px solid var(--border-soft);
  }

  .topbar {
    padding: 6px 8px;
  }

  :root {
    --tap: 40px;
  }

  .btn.icon {
    width: 40px;
  }

  /* Компактнее панель на телефоне: экономим высоту, её занимает клавиатура. */
  .player-bar {
    padding: 6px 8px 8px;
  }

  .player-center {
    width: 56px;
    height: 56px;
  }

  .player-center .ic {
    width: 22px;
    height: 22px;
  }

  .time {
    width: 40px;
    font-size: 11.5px;
  }
}

/* Телефон в альбомной ориентации: места по вертикали мало, шапку ужимаем.
   Размер кадра не трогаем — он и так ограничен svh выше. */
@media (max-width: 900px) and (orientation: landscape) {
  .topbar {
    padding: 4px 8px;
  }

  .room-title {
    font-size: 13.5px;
  }
}

@media (max-width: 420px) {
  .room-title {
    font-size: 14px;
  }
}
