:root {
  --bg: #120b1f;
  --bg-2: #1b1230;
  --bg-3: #261a42;
  --line: #34264f;
  --text: #f4eefc;
  --muted: #a99bc4;
  --accent: #ff6b8b;
  --accent-2: #b46bff;
  --ok: #4ade80;
  --warn: #fbbf24;
  --bad: #f87171;
  --radius: 16px;
  --app-h: 100dvh;
  --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; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); }
body {
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  overflow: hidden;
  overscroll-behavior: none;
}
[hidden] { display: none !important; }
button { font: inherit; color: inherit; cursor: pointer; }
input { font: inherit; color: var(--text); }
svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
svg .fill { fill: currentColor; stroke: none; }
.muted { color: var(--muted); }
.small { font-size: 13px; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  border: 1px solid var(--line); background: var(--bg-3); border-radius: 12px;
  padding: 10px 16px; min-height: 44px; font-weight: 600;
  transition: transform .1s, background .2s;
}
.btn:active { transform: scale(.97); }
.btn.primary { background: linear-gradient(135deg, var(--accent), var(--accent-2)); border: 0; color: #fff; }
.btn.big { width: 100%; min-height: 54px; font-size: 17px; border-radius: 14px; }
.btn.ghost { background: transparent; border-color: transparent; color: var(--muted); }
.btn.small { min-height: 36px; padding: 6px 12px; font-size: 14px; }
.icon-btn {
  display: inline-grid; place-items: center; width: 40px; height: 40px;
  border: 0; background: transparent; border-radius: 50%; padding: 0; flex: none;
}
.icon-btn:active { background: rgba(255,255,255,.08); }
.icon-btn.dim { color: var(--muted); }
.icon-btn.big svg { width: 30px; height: 30px; }

input[type=url], input:not([type]), #join-code, #profile-name, #url-input {
  width: 100%; min-height: 48px; padding: 10px 14px;
  border: 1px solid var(--line); background: var(--bg-2); border-radius: 12px; outline: none;
  font-size: 16px; /* меньше 16px — iOS зумит при фокусе */
}
input:focus { border-color: var(--accent-2); }

.screen {
  position: fixed; inset: 0; height: var(--app-h);
  padding: var(--safe-t) var(--safe-r) 0 var(--safe-l);
}

/* ---------- Главный экран ---------- */
#home { overflow-y: auto; background: radial-gradient(120% 60% at 50% 0%, #3a1d5e 0%, var(--bg) 60%); }
.home-inner { max-width: 420px; margin: 0 auto; padding: 48px 20px calc(24px + var(--safe-b)); display: flex; flex-direction: column; gap: 14px; text-align: center; }
.logo { width: 88px; height: 88px; border-radius: 22px; margin: 0 auto; box-shadow: 0 10px 40px rgba(255,107,139,.35); }
h1 { margin: 4px 0 0; font-size: 34px; letter-spacing: -.5px; }
h2 { font-size: 18px; margin: 0 0 8px; }
.home-inner > p { margin: 0 0 12px; }
.join-form { display: flex; gap: 8px; }
.join-form input { flex: 1; text-transform: lowercase; }
.recent { text-align: left; margin-top: 12px; }
.recent h2 { font-size: 14px; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: .5px; }
.recent ul { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.recent li button {
  width: 100%; display: flex; justify-content: space-between; align-items: center;
  padding: 14px 16px; border-radius: 12px; border: 1px solid var(--line); background: var(--bg-2); text-align: left;
}
.recent li .code { font-family: ui-monospace, Menlo, monospace; font-weight: 700; letter-spacing: 1px; }
.install-hint { font-size: 14px; color: var(--muted); background: var(--bg-2); border: 1px dashed var(--line); padding: 12px; border-radius: 12px; text-align: left; }

/* ---------- Комната ---------- */
#room { display: flex; flex-direction: column; }
.room-head { display: flex; align-items: center; gap: 6px; padding: 6px 8px; flex: none; min-width: 0; }
.room-title { flex: 1; min-width: 0; }
.now-title { font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.room-sub { font-size: 12px; color: var(--muted); display: flex; align-items: center; gap: 6px; font-family: ui-monospace, Menlo, monospace; }
.conn-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--warn); }
.conn-dot.on { background: var(--ok); }
.conn-dot.off { background: var(--bad); }
.members { display: flex; }
.avatar {
  width: 30px; height: 30px; border-radius: 50%; display: grid; place-items: center;
  font-weight: 700; font-size: 13px; color: #fff; border: 2px solid var(--bg); margin-left: -8px; position: relative;
}
.avatar .cam-dot { position: absolute; right: -3px; bottom: -3px; font-size: 10px; }

.stage {
  position: relative; flex: none; width: 100%; aspect-ratio: 16 / 9; max-height: 45vh;
  background: #000; overflow: hidden; user-select: none; -webkit-user-select: none;
}
.stage video, .yt-host, .yt-host iframe { position: absolute; inset: 0; width: 100%; height: 100%; border: 0; }
.stage video { object-fit: contain; background: #000; }
.cover { position: absolute; inset: 0; z-index: 2; }
.cover.passthrough { pointer-events: none; }
.empty {
  position: absolute; inset: 0; z-index: 3; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 10px;
  background: radial-gradient(80% 80% at 50% 40%, #2a1748, #0b0714); color: var(--muted);
}
.empty-icon { font-size: 36px; }
.spinner {
  position: absolute; z-index: 3; left: 50%; top: 50%; width: 44px; height: 44px; margin: -22px 0 0 -22px;
  border: 4px solid rgba(255,255,255,.2); border-top-color: #fff; border-radius: 50%; animation: spin 0.9s linear infinite; pointer-events: none;
}
@keyframes spin { to { transform: rotate(360deg); } }
.gesture {
  position: absolute; inset: 0; z-index: 6; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 10px;
  background: rgba(10,6,20,.72); padding: 20px; text-align: center;
}
.gesture .btn { width: auto; padding: 0 28px; }
.gesture-hint { font-size: 13px; color: var(--muted); }
/* Для YouTube: подсказка внизу, а касание уходит в сам плеер */
.gesture.yt { background: transparent; pointer-events: none; justify-content: flex-end; padding-bottom: 14px; }
.gesture.yt .btn { display: none; }
.gesture.yt .gesture-hint { background: rgba(0,0,0,.75); color: #fff; padding: 8px 14px; border-radius: 20px; font-size: 14px; }

.controls {
  position: absolute; left: 0; right: 0; bottom: 0; z-index: 4;
  padding: 24px 8px 4px; background: linear-gradient(transparent, rgba(0,0,0,.75));
  transition: opacity .25s; opacity: 1;
}
.stage.idle .controls { opacity: 0; pointer-events: none; }
.ctrl-row { display: flex; align-items: center; gap: 4px; }
.time { font-size: 12px; font-variant-numeric: tabular-nums; min-width: 38px; text-align: center; color: #ddd; }
.seek { flex: 1; min-width: 40px; accent-color: var(--accent); height: 28px; background: transparent; }
.sync-badge {
  position: absolute; top: 8px; left: 8px; z-index: 5; font-size: 12px; padding: 3px 8px; border-radius: 10px;
  background: rgba(0,0,0,.6); color: #fff; pointer-events: none;
}

.reactions-layer { position: absolute; inset: 0; z-index: 5; pointer-events: none; overflow: hidden; }
.floater { position: absolute; bottom: 10px; font-size: 34px; animation: floatUp 2.6s ease-out forwards; }
.floater small { display: block; font-size: 10px; text-align: center; color: #fff; text-shadow: 0 1px 2px #000; }
@keyframes floatUp {
  0% { transform: translateY(0) scale(.6); opacity: 0; }
  12% { transform: translateY(-20px) scale(1.15); opacity: 1; }
  100% { transform: translateY(-220px) scale(1); opacity: 0; }
}

.toolbar { display: flex; align-items: center; gap: 6px; padding: 8px; border-bottom: 1px solid var(--line); flex: none; overflow-x: auto; scrollbar-width: none; }
.toolbar::-webkit-scrollbar { display: none; }
.tool {
  display: flex; flex-direction: column; align-items: center; gap: 2px; flex: none;
  font-size: 11px; color: var(--muted); border: 0; background: var(--bg-2); border-radius: 12px; padding: 6px 10px; min-width: 58px;
}
.tool span { font-size: 18px; }
.tool.on { background: var(--bg-3); color: var(--text); box-shadow: inset 0 0 0 1px var(--accent); }
.tool.off-mic { box-shadow: inset 0 0 0 1px var(--bad); }
.react-bar { display: flex; margin-left: auto; gap: 2px; flex: none; }
.react-bar button { border: 0; background: transparent; font-size: 22px; width: 38px; height: 44px; padding: 0; border-radius: 10px; }
.react-bar button:active { background: var(--bg-3); transform: scale(1.2); }

.chat { flex: 1; overflow-y: auto; padding: 8px 10px; min-height: 0; -webkit-overflow-scrolling: touch; }
.messages { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.msg { max-width: 82%; align-self: flex-start; }
.msg .who { font-size: 12px; font-weight: 700; margin: 0 0 2px 10px; }
.msg .bubble { background: var(--bg-2); padding: 8px 12px; border-radius: 16px 16px 16px 4px; white-space: pre-wrap; overflow-wrap: anywhere; }
.msg .ts { font-size: 10px; color: var(--muted); margin-left: 6px; }
.msg.mine { align-self: flex-end; }
.msg.mine .who { display: none; }
.msg.mine .bubble { background: linear-gradient(135deg, #7d3cdb, #c2477a); border-radius: 16px 16px 4px 16px; }
.msg.system { align-self: center; font-size: 12px; color: var(--muted); }
.msg.cont .who { display: none; }

.composer { display: flex; gap: 8px; padding: 8px 10px calc(8px + var(--safe-b)); border-top: 1px solid var(--line); flex: none; background: var(--bg); }
.composer input { flex: 1; min-height: 44px; border-radius: 22px; }
.send { width: 44px; height: 44px; border-radius: 50%; border: 0; background: linear-gradient(135deg, var(--accent), var(--accent-2)); color: #fff; display: grid; place-items: center; flex: none; }
.chat-toggle { display: none; }

/* ---------- Чат: ответы, реакции, смайлы ---------- */
.chat { position: relative; }
.msg { position: relative; transition: transform .15s; touch-action: pan-y; }
.msg.swiping { transition: none; }
.msg .reply-arrow { position: absolute; left: -30px; top: 50%; margin-top: -12px; opacity: 0; font-size: 18px; transition: transform .12s; }
.msg.swipe-left .reply-arrow { left: auto; right: -30px; }
.chat { overflow-x: hidden; }
.msg.flash .bubble { animation: flash 1.2s; }
@keyframes flash { 0%, 40% { box-shadow: 0 0 0 3px var(--accent); } 100% { box-shadow: 0 0 0 0 transparent; } }
.quote {
  display: block; border-left: 3px solid var(--accent); padding: 2px 8px; margin: 0 0 6px; border-radius: 4px;
  background: rgba(255,255,255,.07); font-size: 13px; cursor: pointer;
}
.quote b { display: block; font-size: 12px; }
.quote span { display: block; color: #ddd; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 60vw; }
.bubble.jumbo { font-size: 40px; line-height: 1.1; background: transparent !important; padding: 0 4px; }
.reacts { display: flex; flex-wrap: wrap; gap: 4px; margin-top: -6px; padding: 0 6px; position: relative; z-index: 1; }
.msg.mine .reacts { justify-content: flex-end; }
.react-chip {
  display: inline-flex; align-items: center; gap: 3px; border: 1px solid var(--line); background: var(--bg-3);
  border-radius: 12px; padding: 1px 7px; font-size: 13px; min-height: 24px;
}
.react-chip.me { border-color: var(--accent); background: #4a2050; }
.react-chip i { font-style: normal; font-size: 11px; color: var(--muted); }
.react-chip.me i { color: #fff; }
.heart-burst { position: absolute; left: 50%; top: 50%; font-size: 44px; transform: translate(-50%, -50%); pointer-events: none; animation: burst .8s ease-out forwards; z-index: 3; }
@keyframes burst { 0% { transform: translate(-50%,-50%) scale(.3); opacity: 0; } 30% { transform: translate(-50%,-50%) scale(1.25); opacity: 1; } 100% { transform: translate(-50%,-90%) scale(1); opacity: 0; } }
.new-msgs { position: sticky; bottom: 4px; left: 50%; display: block; margin: 4px auto 0; border: 0; border-radius: 16px; padding: 6px 14px; background: var(--accent-2); color: #fff; font-size: 13px; }

.emoji-toggle { width: 40px; height: 44px; border: 0; background: transparent; font-size: 24px; padding: 0; flex: none; }
.emoji-panel {
  flex: none; display: grid; grid-template-columns: repeat(auto-fill, minmax(42px, 1fr)); gap: 2px;
  max-height: 200px; overflow-y: auto; padding: 8px; border-top: 1px solid var(--line); background: var(--bg-2);
}
.emoji-panel button { border: 0; background: transparent; font-size: 26px; height: 44px; border-radius: 10px; padding: 0; }
.emoji-panel button:active { background: var(--bg-3); }
.reply-bar { flex: none; display: flex; align-items: center; gap: 8px; padding: 6px 6px 6px 12px; border-top: 1px solid var(--line); background: var(--bg-2); }
.reply-line { width: 3px; align-self: stretch; background: var(--accent); border-radius: 2px; }
.reply-content { flex: 1; min-width: 0; }
.reply-name { font-size: 13px; font-weight: 700; color: var(--accent); }
.reply-text { font-size: 13px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.msg-menu { position: fixed; inset: 0; z-index: 55; }
.msg-menu-backdrop { position: absolute; inset: 0; background: rgba(0,0,0,.45); }
.msg-menu-body {
  position: absolute; left: 50%; transform: translateX(-50%); width: min(340px, calc(100vw - 24px));
  background: var(--bg-2); border: 1px solid var(--line); border-radius: 18px; padding: 8px; box-shadow: 0 10px 40px rgba(0,0,0,.5);
  animation: pop .15s ease-out;
}
@keyframes pop { from { transform: translateX(-50%) scale(.92); opacity: 0; } }
.msg-menu-reacts { display: flex; justify-content: space-between; padding: 4px 2px 8px; border-bottom: 1px solid var(--line); margin-bottom: 4px; }
.msg-menu-reacts button { border: 0; background: transparent; font-size: 28px; width: 44px; height: 48px; border-radius: 12px; padding: 0; }
.msg-menu-reacts button.me { background: var(--bg-3); }
.msg-menu-reacts button:active { transform: scale(1.25); }
.mm-item { display: block; width: 100%; text-align: left; border: 0; background: transparent; padding: 12px; border-radius: 10px; font-size: 16px; }
.mm-item:active { background: var(--bg-3); }
.url-actions { display: flex; gap: 8px; }
.url-actions .btn { flex: 1; }
.url-form p { margin: 0; }

/* ---------- Нижний лист ---------- */
.sheet, .modal { position: fixed; inset: 0; z-index: 50; }
.sheet-backdrop { position: absolute; inset: 0; background: rgba(0,0,0,.55); }
.sheet-body {
  position: absolute; left: 0; right: 0; bottom: 0; max-height: 85%; overflow-y: auto;
  background: var(--bg-2); border-radius: 20px 20px 0 0; padding: 10px 16px calc(20px + var(--safe-b));
  display: flex; flex-direction: column; gap: 12px; animation: up .22s ease-out;
  max-width: 560px; margin: 0 auto;
}
@keyframes up { from { transform: translateY(40px); opacity: 0; } }
.sheet-grip { width: 40px; height: 5px; border-radius: 3px; background: var(--line); margin: 0 auto 4px; }
.url-form { display: flex; flex-direction: column; gap: 8px; }
.or { display: flex; align-items: center; gap: 10px; color: var(--muted); font-size: 13px; }
.or::before, .or::after { content: ""; flex: 1; height: 1px; background: var(--line); }
.upload-btn { cursor: pointer; }
.uploads { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.uploads li { background: var(--bg-3); border-radius: 12px; padding: 10px 12px; font-size: 14px; }
.uploads .u-top { display: flex; justify-content: space-between; gap: 8px; }
.uploads .u-title { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 600; }
.uploads .u-status { color: var(--muted); flex: none; }
.uploads .u-status.err { color: var(--bad); }
.bar { height: 6px; border-radius: 3px; background: var(--bg); margin-top: 8px; overflow: hidden; }
.bar > i { display: block; height: 100%; width: 0; background: linear-gradient(90deg, var(--accent-2), var(--accent)); transition: width .3s; }
.uploads button { margin-top: 8px; }

.modal { display: grid; place-items: center; background: rgba(0,0,0,.7); padding: 20px; }
.modal-body { width: 100%; max-width: 360px; background: var(--bg-2); border-radius: 20px; padding: 20px; display: flex; flex-direction: column; gap: 14px; }
.colors { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; }
.colors button { width: 38px; height: 38px; border-radius: 50%; border: 3px solid transparent; }
.colors button.sel { border-color: #fff; }

.toasts { position: fixed; z-index: 60; left: 0; right: 0; top: calc(10px + var(--safe-t)); display: flex; flex-direction: column; align-items: center; gap: 6px; pointer-events: none; }
.toast { background: rgba(30,20,50,.95); border: 1px solid var(--line); padding: 8px 14px; border-radius: 20px; font-size: 14px; animation: up .2s; max-width: 90vw; }

/* ---------- Полноэкранный / ландшафтный режим ---------- */
body.immersive #room { padding: 0; background: #000; }
body.immersive .room-head, body.immersive .toolbar { display: none; }
body.immersive .stage { position: fixed; inset: 0; max-height: none; aspect-ratio: auto; height: var(--app-h); z-index: 1; }
body.immersive .controls { padding-left: calc(8px + var(--safe-l)); padding-right: calc(8px + var(--safe-r)); padding-bottom: calc(4px + var(--safe-b)); }
body.immersive .chat {
  position: fixed; z-index: 10; right: calc(6px + var(--safe-r)); bottom: calc(64px + var(--safe-b)); width: min(320px, 40vw); max-height: 55%;
  background: transparent; pointer-events: none; padding: 0;
  -webkit-mask-image: linear-gradient(transparent, #000 30%); mask-image: linear-gradient(transparent, #000 30%);
}
body.immersive .msg .bubble { background: rgba(20,12,36,.72); }
body.immersive .msg.mine .bubble { background: rgba(125,60,219,.75); }
body.immersive .msg.old { display: none; }
body.immersive .composer { display: none; position: fixed; z-index: 11; right: calc(6px + var(--safe-r)); bottom: calc(8px + var(--safe-b)); width: min(360px, 60vw); border: 0; background: transparent; padding: 0; }
body.immersive.chat-open .composer { display: flex; }
body.immersive.chat-open .chat { pointer-events: auto; }
body.immersive.chat-open .msg.old { display: block; }
body.immersive .chat-toggle {
  display: grid; place-items: center; position: fixed; z-index: 12; top: calc(10px + var(--safe-t)); right: calc(10px + var(--safe-r));
  width: 42px; height: 42px; border-radius: 50%; border: 0; background: rgba(0,0,0,.5); font-size: 20px;
}
body.immersive .reacts, body.immersive .new-msgs, body.immersive .reply-bar { display: none; }
body.immersive .emoji-panel { position: fixed; z-index: 11; right: calc(6px + var(--safe-r)); bottom: calc(60px + var(--safe-b)); width: min(360px, 60vw); max-height: 40vh; border-radius: 14px; border: 1px solid var(--line); }
body.immersive:not(.chat-open) .emoji-panel { display: none; }

@media (min-width: 900px) {
  #room { display: grid; grid-template-columns: 1fr 360px; grid-template-rows: auto auto 1fr auto; }
  .room-head { grid-column: 1 / -1; }
  .stage { grid-column: 1; grid-row: 2 / 5; max-height: none; height: 100%; aspect-ratio: auto; }
  .toolbar { grid-column: 2; grid-row: 2; }
  .chat { grid-column: 2; grid-row: 3; }
  .composer { grid-column: 2; grid-row: 4; }
}
