:root {
  --bg: var(--tg-theme-bg-color, #17181c);
  --bg2: var(--tg-theme-secondary-bg-color, #22242a);
  --section: var(--tg-theme-section-bg-color, #1d1f25);
  --text: var(--tg-theme-text-color, #f1f1f3);
  --hint: var(--tg-theme-hint-color, #8d909a);
  --accent: var(--tg-theme-button-color, #e5484d);
  --accent-text: var(--tg-theme-button-text-color, #ffffff);
  --link: var(--tg-theme-link-color, #6ea8fe);
  --ok: #3ecf8e;
  --warn: #f5a524;
  --radius: 12px;
  --gap: 12px;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  overscroll-behavior: none;
  overflow-x: hidden;
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
  touch-action: manipulation; /* без зума двойным тапом */
}

body {
  display: flex;
  flex-direction: column;
  height: var(--app-height, 100vh);
  overflow: hidden;
}

.hidden { display: none !important; }

button { font: inherit; color: inherit; cursor: pointer; }

.btn {
  border: 0;
  border-radius: 10px;
  padding: 10px 14px;
  background: var(--bg2);
  color: var(--text);
  white-space: nowrap;
}
.btn:active { opacity: .7; }
.btn-accent { background: var(--accent); color: var(--accent-text); }
.btn-big { padding: 14px 22px; font-size: 17px; }
.btn-wide { width: 100%; margin-top: 8px; }

.icon-btn {
  flex: none;
  display: grid;
  place-items: center;
  width: 36px; height: 36px;
  border: 0; border-radius: 50%;
  background: var(--bg2);
}
.icon-btn:active { opacity: .7; }
.icon-btn-accent { background: var(--accent); color: var(--accent-text); }

input, select {
  min-width: 0;
  border: 1px solid transparent;
  border-radius: 10px;
  padding: 10px 12px;
  background: var(--bg2);
  color: var(--text);
  font: inherit;
  /* Меньше 16px — iOS приближает страницу при вводе и потом не возвращает масштаб. */
  font-size: 16px;
}
input:focus, select:focus { outline: none; border-color: var(--accent); }
input:disabled, select:disabled, .segmented.disabled { opacity: .5; }

/* --- шапка --- */
.topbar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px var(--gap);
  padding-top: calc(8px + var(--tg-safe-top, 0px));
}
.avatars {
  flex: none;
  display: flex;
  align-items: center;
  border: 0; background: none; padding: 0;
  min-width: 28px; height: 32px;
}
.titles { flex: 1; min-width: 0; }
.title {
  font-size: 15px; font-weight: 600;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.subtitle {
  font-size: 12px; color: var(--hint);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.avatar {
  width: 28px; height: 28px;
  margin-right: -8px;
  border-radius: 50%;
  border: 2px solid var(--bg);
  background: var(--bg2) center / cover;
  display: grid; place-items: center;
  font-size: 12px; font-weight: 600;
  flex: none;
  position: relative;
}
.avatar.more { font-size: 11px; color: var(--hint); }
.avatar.buffering { border-color: var(--warn); }

/* --- плеер --- */
.stage { position: relative; background: #000; flex: none; }
.player { position: relative; width: 100%; aspect-ratio: 16 / 9; max-height: 56vh; }
.player > video, .player > iframe, .player > div:not(.placeholder) {
  position: absolute; inset: 0; width: 100%; height: 100%; border: 0; background: #000;
}
.placeholder {
  position: absolute; inset: 0;
  display: grid; place-content: center; justify-items: center; gap: 10px;
  text-align: center; color: #9a9ca5;
}
.placeholder-icon { font-size: 40px; }
.placeholder-text { font-size: 14px; }
.placeholder-actions { display: flex; gap: 8px; flex-wrap: wrap; justify-content: center; }

.overlay {
  position: absolute; inset: 0;
  display: grid; place-items: center;
  background: rgba(0, 0, 0, .55);
  z-index: 5;
}
.countdown { font-size: 96px; font-weight: 700; color: #fff; pointer-events: none; }

.toast {
  position: absolute; left: 50%; top: 10px; transform: translateX(-50%);
  max-width: calc(100% - 24px);
  padding: 6px 12px; border-radius: 999px;
  background: rgba(0, 0, 0, .78); color: #fff; font-size: 13px;
  z-index: 6; pointer-events: none;
}

/* --- строка состояния --- */
.statusbar {
  display: flex; align-items: center; flex-wrap: wrap; gap: 6px 8px;
  padding: 8px var(--gap);
}
.status-dot {
  width: 8px; height: 8px; border-radius: 50%;
  background: var(--hint); flex: none;
}
.status-dot.ok { background: var(--ok); }
.status-dot.warn { background: var(--warn); }
.status-text {
  flex: 1 1 120px; min-width: 0;
  font-size: 13px; color: var(--hint);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
/* На узком экране кнопки уходят на свою строку, чтобы текст состояния не обрезался. */
@media (max-width: 380px) {
  .status-text { flex-basis: calc(100% - 20px); }
  .status-actions { width: 100%; }
  .status-actions .chip { flex: 1; }
}
.status-actions { display: flex; gap: 6px; flex: none; }
.chip {
  border: 0; border-radius: 999px;
  padding: 6px 12px;
  background: var(--bg2);
  font-size: 13px;
}
.chip:active { opacity: .7; }
.chip-accent { background: var(--accent); color: var(--accent-text); }
.chip-icon { display: grid; place-items: center; padding: 6px 10px; }
@media (max-width: 380px) {
  .status-actions .chip-icon { flex: none; }
}

.embed-note {
  margin: 0 var(--gap) 8px;
  padding: 8px 10px;
  border-radius: 10px;
  background: var(--bg2);
  color: var(--hint);
  font-size: 13px;
}

/* --- чат --- */
.chat {
  flex: 1; min-height: 0;
  display: flex; flex-direction: column;
  border-top: 1px solid var(--bg2);
}
.messages {
  flex: 1; overflow-y: auto;
  margin: 0; padding: 8px var(--gap);
  list-style: none;
  display: flex; flex-direction: column; gap: 6px;
}
.msg {
  max-width: 85%; align-self: flex-start;
  background: var(--bg2);
  padding: 6px 10px; border-radius: 12px;
  overflow-wrap: anywhere;
}
.msg.mine { align-self: flex-end; background: var(--accent); color: var(--accent-text); }
.msg-name { font-size: 12px; font-weight: 600; color: var(--link); }
.msg.mine .msg-name { display: none; }
.notice { align-self: center; font-size: 12px; color: var(--hint); text-align: center; }
.chat-form {
  display: flex; gap: 8px; align-items: center;
  padding: 8px var(--gap) calc(8px + var(--tg-safe-bottom, env(safe-area-inset-bottom)));
}
.chat-form input { flex: 1; }
#reactBtn { font-size: 18px; }
#reactBtn.active { background: var(--accent); }

/* --- реакции --- */
.reaction-bar {
  display: flex; justify-content: space-between; gap: 2px;
  padding: 6px var(--gap) 0;
}
.react-emoji {
  flex: 1; min-width: 0;
  border: 0; border-radius: 10px;
  padding: 6px 0; background: var(--bg2);
  font-size: 22px; line-height: 1;
}
.react-emoji:active { transform: scale(1.2); }

.reaction-layer {
  position: absolute; inset: 0;
  overflow: hidden; pointer-events: none;
  z-index: 4;
}
.flying {
  position: absolute; bottom: 8%;
  display: flex; flex-direction: column; align-items: center;
  animation: fly-up 2.6s ease-out forwards;
}
.flying-emoji { font-size: 30px; line-height: 1; filter: drop-shadow(0 1px 2px rgba(0, 0, 0, .5)); }
.flying-name {
  margin-top: 2px; padding: 1px 6px; border-radius: 999px;
  background: rgba(0, 0, 0, .55); color: #fff; font-size: 10px;
  max-width: 90px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
@keyframes fly-up {
  0% { transform: translate(0, 0) scale(.6); opacity: 0; }
  12% { transform: translate(0, -10%) scale(1.1); opacity: 1; }
  100% { transform: translate(var(--drift, 0), -260%) scale(1); opacity: 0; }
}

/* --- кинозал: видео на весь экран, реакции и чат поверх --- */
.theater-ctl, .theater-reactions, .ticker { display: none; }
body.theater .stage {
  position: fixed; inset: 0; z-index: 30;
  display: flex; align-items: center; justify-content: center;
}
body.theater .player { aspect-ratio: auto; width: 100%; height: 100%; max-height: none; }
body.theater .theater-ctl {
  display: grid; place-items: center;
  position: absolute; z-index: 7;
  width: 38px; height: 38px;
  border: 0; border-radius: 50%;
  background: rgba(0, 0, 0, .55); color: #fff;
  font-size: 18px; opacity: .75;
}
/* Кнопки — у середины боковых краёв: сверху и снизу свои панели у самих плееров. */
body.theater .theater-exit { left: 8px; top: 50%; transform: translateY(-50%); }
body.theater .theater-react { right: 8px; top: 50%; transform: translateY(-50%); }
body.theater .theater-reactions:not(.hidden) {
  display: grid; grid-template-columns: repeat(2, 40px); gap: 2px;
  position: absolute; z-index: 7;
  right: 52px; top: 50%; transform: translateY(-50%);
  padding: 4px; border-radius: 12px;
  background: rgba(0, 0, 0, .6);
}
body.theater .theater-reactions .react-emoji { width: 40px; height: 40px; padding: 0; background: none; }
body.theater .ticker {
  display: flex; flex-direction: column; gap: 4px;
  position: absolute; z-index: 5; pointer-events: none;
  left: 10px; top: calc(56px + var(--tg-safe-top, 0px));
  max-width: min(60%, 360px);
}
.ticker-line {
  padding: 4px 8px; border-radius: 8px;
  background: rgba(0, 0, 0, .6); color: #fff; font-size: 13px;
  overflow-wrap: anywhere; transition: opacity 1s;
}
.ticker-line.fade { opacity: 0; }
body.theater .toast { top: calc(12px + var(--tg-safe-top, 0px)); }

/* --- шторки --- */
.scrim {
  position: fixed; inset: 0;
  background: rgba(0, 0, 0, .5);
  z-index: 20;
}
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0;
  z-index: 21;
  /* Считаем от высоты окна Telegram, иначе всплывшая клавиатура прячет кнопку отправки. */
  max-height: calc(var(--app-height, 100vh) * 0.85); overflow-y: auto;
  padding: 6px var(--gap) calc(20px + var(--tg-safe-bottom, env(safe-area-inset-bottom)));
  background: var(--section);
  border-radius: 16px 16px 0 0;
  animation: rise .22s ease-out;
}
@keyframes rise { from { transform: translateY(100%); } to { transform: translateY(0); } }
.sheet-grip {
  width: 36px; height: 4px; margin: 6px auto 10px;
  border-radius: 2px; background: var(--hint); opacity: .4;
}
.sheet-title { margin: 0 0 12px; font-size: 18px; }
.sheet-label {
  margin: 16px 0 6px;
  font-size: 12px; text-transform: uppercase; letter-spacing: .04em;
  color: var(--hint);
}
.sheet input:not([type=checkbox]), .sheet select { width: 100%; }
.field-row { display: flex; gap: 8px; }
.field-row input { flex: 1; }
.hint { margin: 10px 0 0; font-size: 13px; color: var(--hint); }
.now-playing {
  margin-bottom: 10px;
  padding: 8px 10px; border-radius: 10px;
  background: var(--bg2); color: var(--hint); font-size: 13px;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.advanced { margin-top: 14px; }
.advanced summary { font-size: 13px; color: var(--link); cursor: pointer; }
.advanced select { margin-top: 8px; }

.host-note {
  padding: 8px 10px; border-radius: 10px;
  background: var(--bg2); color: var(--hint); font-size: 13px;
}

.segmented { display: flex; gap: 4px; padding: 3px; border-radius: 10px; background: var(--bg2); }
.segmented button {
  flex: 1; border: 0; border-radius: 8px;
  padding: 8px 6px; background: none; font-size: 14px;
}
.segmented button.active { background: var(--accent); color: var(--accent-text); }

.switch-row {
  display: flex; align-items: center; gap: 12px;
  padding: 12px 0;
  border-bottom: 1px solid var(--bg2);
}
.switch-row:last-of-type { border-bottom: 0; }
.switch-row > div { flex: 1; min-width: 0; } /* переключатели ровно у правого края */
.switch-title { font-size: 15px; }
.switch-sub { font-size: 12px; color: var(--hint); margin-top: 2px; }
.switch-row input[type=checkbox] {
  appearance: none; -webkit-appearance: none;
  flex: none; position: relative;
  width: 46px; height: 28px; margin: 0; padding: 0;
  border: 0; border-radius: 999px;
  background: rgba(128, 132, 145, .4); /* выключенный виден на любом фоне и любой теме Telegram */
  transition: background .15s;
}
.switch-row input[type=checkbox]::after {
  content: ''; position: absolute; top: 3px; left: 3px;
  width: 22px; height: 22px; border-radius: 50%;
  background: #fff; transition: transform .15s;
}
.switch-row input[type=checkbox]:checked { background: var(--ok); }
.switch-row input[type=checkbox]:checked::after { transform: translateX(18px); }

.member-list, .recent { list-style: none; margin: 0; padding: 0; }
.member {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 0;
  border-bottom: 1px solid var(--bg2);
}
.member:last-child { border-bottom: 0; }
.member-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.badge {
  flex: none;
  padding: 3px 8px; border-radius: 999px;
  background: var(--bg2); color: var(--hint); font-size: 12px;
}
.badge.host { background: var(--accent); color: var(--accent-text); }
.badge.buffering { background: var(--warn); color: #23200f; }

/* --- шторка «Видео»: вкладки, списки, карточка фильма --- */
.tabs {
  display: flex; gap: 2px; margin-bottom: 12px;
  padding: 3px; border-radius: 10px; background: var(--bg2);
}
.tabs button {
  flex: 1; min-width: 0;
  border: 0; border-radius: 8px;
  padding: 8px 2px; background: none;
  font-size: 13px; white-space: nowrap;
}
.tabs button.active { background: var(--section); font-weight: 600; }
.count:not(:empty) {
  display: inline-block; min-width: 18px; margin-left: 2px;
  padding: 0 5px; border-radius: 999px;
  background: var(--accent); color: var(--accent-text);
  font-size: 11px; line-height: 18px; text-align: center;
}
.btn-row { display: flex; gap: 8px; margin-top: 8px; }
.btn-row .btn { flex: 1; min-width: 0; }
.btn:disabled { opacity: .5; }
.search-form { display: flex; gap: 8px; margin: 10px 0 4px; }
.search-form input { flex: 1; }

.media-list { list-style: none; margin: 0; padding: 0; }
.media-row {
  display: flex; align-items: center; gap: 10px;
  padding: 8px 0;
  border-bottom: 1px solid var(--bg2);
}
.media-row:last-child { border-bottom: 0; }
.media-row.clickable .media-thumb, .media-row.clickable .media-text { cursor: pointer; }
.media-thumb {
  position: relative; flex: none;
  width: 88px; height: 50px; border-radius: 8px; overflow: hidden;
  background: var(--bg2);
}
.media-thumb.portrait { width: 48px; height: 72px; border-radius: 6px; }
.media-thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.media-badge {
  position: absolute; left: 4px; top: 4px;
  padding: 0 5px; border-radius: 6px;
  background: rgba(0, 0, 0, .7); color: #fff; font-size: 11px; line-height: 16px;
}
.media-text { flex: 1; min-width: 0; }
.media-title {
  font-size: 14px; line-height: 1.3;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
  overflow-wrap: anywhere;
}
.media-sub {
  margin-top: 2px; font-size: 12px; color: var(--hint);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.media-actions { display: flex; gap: 4px; flex: none; }
.mini-btn {
  width: 32px; height: 32px;
  border: 0; border-radius: 8px;
  background: var(--bg2); font-size: 14px;
}
.mini-btn.accent { background: var(--accent); color: var(--accent-text); }
.mini-btn:active { opacity: .7; }
.empty-row { padding: 14px 0; color: var(--hint); font-size: 13px; text-align: center; }

.link-btn {
  border: 0; background: none; padding: 8px 0;
  color: var(--link); font-size: 14px;
}
.film-card { display: flex; gap: 12px; margin: 4px 0 8px; }
.film-poster {
  flex: none; width: 96px; height: 144px;
  border-radius: 10px; object-fit: cover; background: var(--bg2);
}
.film-info { min-width: 0; }
.film-title { font-size: 17px; font-weight: 600; line-height: 1.25; }
.film-meta { margin-top: 4px; font-size: 13px; color: var(--hint); }
.film-desc { margin-top: 6px; font-size: 13px; }
.film-extract { margin: 0 0 10px; font-size: 13px; line-height: 1.45; color: var(--hint); }
.film-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.film-actions .btn { white-space: normal; }

.recent li { padding: 4px 0; }
.recent button {
  width: 100%; text-align: left;
  border: 0; border-radius: 10px; padding: 10px;
  background: var(--bg2); font-size: 13px;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
