:root {
  --bg: #0d0e12;
  --panel: #16181f;
  --panel-2: #1d2029;
  --line: #2a2e3a;
  --text: #e8eaf0;
  --muted: #8b90a0;
  --accent: #ff4d6d;
  --accent-2: #5b8cff;
  --ok: #4ade80;
}

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

html, body {
  margin: 0;
  height: 100%;
  background: #07080b;
  color: var(--text);
  font-family: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  overscroll-behavior: none;
}

/* ---- App shell: phone-frame on desktop, full-screen on mobile ---- */
.app {
  display: flex;
  flex-direction: column;
  width: 100%;
  max-width: 480px;
  height: 100dvh;
  margin: 0 auto;
  background: var(--bg);
  position: relative;
  overflow: hidden;
}
@media (min-width: 520px) {
  body { display: flex; align-items: center; justify-content: center; padding: 20px; }
  .app {
    height: min(900px, calc(100dvh - 40px));
    border-radius: 28px;
    box-shadow: 0 30px 80px rgba(0, 0, 0, 0.6), 0 0 0 1px var(--line);
    overflow: hidden;
  }
}

/* ---- App bar ---- */
.appbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: max(12px, env(safe-area-inset-top)) 16px 12px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  flex-shrink: 0;
}
.brand { font-weight: 700; font-size: 18px; color: var(--accent); }
.brand span { color: var(--text); }
.appbar-right { display: flex; align-items: center; gap: 8px; }
.users {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  background: var(--panel-2);
  border: 1px solid var(--line);
  color: var(--ok);
  font-size: 13px;
  font-variant-numeric: tabular-nums;
  border-radius: 999px;
  padding: 6px 11px;
  cursor: pointer;
}
.users:active { transform: scale(0.96); }
.users-dot { font-size: 9px; line-height: 1; }
.icon-btn {
  width: 34px;
  height: 34px;
  display: grid;
  place-items: center;
  background: var(--panel-2);
  border: 1px solid var(--line);
  color: var(--text);
  border-radius: 50%;
  font-size: 20px;
  line-height: 1;
  cursor: pointer;
}
.icon-btn:active { transform: scale(0.94); }

.pill-btn {
  background: var(--accent);
  color: #fff;
  border: 0;
  border-radius: 999px;
  padding: 8px 16px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
}
.pill-btn:active { transform: scale(0.96); }

/* ---- Room strip ---- */
.room-strip {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 16px;
  background: var(--panel-2);
  border-bottom: 1px solid var(--line);
  flex-shrink: 0;
}
.room-label { font-size: 10px; letter-spacing: 1.5px; color: var(--muted); }
.room-code {
  font-size: 14px;
  letter-spacing: 2px;
  color: var(--text);
  font-weight: 600;
}
.resync {
  margin-left: auto;
  background: transparent;
  border: 1px solid var(--line);
  color: var(--muted);
  border-radius: 999px;
  padding: 5px 12px;
  font-size: 12px;
  cursor: pointer;
}
.resync:active { background: var(--panel); }

/* ---- Stage (player + status + reactions column) ---- */
/* On mobile it's a plain vertical stack (natural height); on desktop it becomes
   the left grid column beside the chat. */
.stage { display: flex; flex-direction: column; flex-shrink: 0; min-width: 0; }

/* ---- Player ---- */
.player {
  position: relative;
  width: 100%;
  aspect-ratio: 16 / 9;
  background: #000;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}
.player-surface {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: none;
  border: 0;
}
/* Platform surfaces fill the player box. The YouTube iframe is nested inside a
   .player-surface div (YT.Player replaces a child node), so target descendants
   too — otherwise the iframe renders at its default 640x390 and overflows. */
.player-surface iframe, .player-surface video { width: 100%; height: 100%; border: 0; }
video.player-surface { background: #000; object-fit: contain; }
/* Transparent click-blocker over the YouTube iframe for members who can't
   control — swallows taps so YouTube's own control bar never engages. */
.surface-block { position: absolute; inset: 0; z-index: 4; background: transparent; cursor: not-allowed; }
.surface-block[hidden] { display: none; }

.placeholder { text-align: center; color: var(--muted); padding: 0 28px; }
.ph-emoji { font-size: 40px; margin-bottom: 8px; }
.placeholder p { margin: 0; font-size: 14px; line-height: 1.5; }

.now-badge {
  position: absolute;
  top: 10px;
  left: 10px;
  z-index: 6;
  display: flex;
  align-items: center;
  gap: 6px;
  background: rgba(0, 0, 0, 0.6);
  backdrop-filter: blur(6px);
  border: 1px solid rgba(255, 255, 255, 0.12);
  color: #fff;
  border-radius: 999px;
  padding: 6px 12px;
  font-size: 12.5px;
  font-weight: 600;
  max-width: calc(100% - 20px);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  pointer-events: none;
  /* Hidden by default; .show flashes it in, then JS fades it back out. */
  opacity: 0;
  visibility: hidden;
  transform: translateY(-4px);
  transition: opacity 0.35s ease, visibility 0.35s ease, transform 0.35s ease;
}
.now-badge.show {
  opacity: 1;
  visibility: visible;
  transform: translateY(0);
}
.now-badge.playing { color: var(--ok); }
.now-badge.paused { color: #ffd166; }

/* Picture-in-Picture toggle — top-right of the player. */
.pip-btn {
  position: absolute;
  top: 10px;
  right: 10px;
  z-index: 6;
  width: 36px;
  height: 36px;
  display: grid;
  place-items: center;
  background: rgba(0, 0, 0, 0.55);
  backdrop-filter: blur(6px);
  border: 1px solid rgba(255, 255, 255, 0.14);
  color: #fff;
  border-radius: 10px;
  font-size: 15px;
  cursor: pointer;
  transition: background 0.15s ease, transform 0.08s ease;
}
.pip-btn[hidden] { display: none; }
.pip-btn:hover { background: rgba(0, 0, 0, 0.75); }
.pip-btn:active { transform: scale(0.92); }
.pip-btn.active { background: var(--accent); border-color: var(--accent); }

.unmute-btn {
  position: absolute;
  bottom: 14px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 5;
  background: var(--accent);
  color: #fff;
  border: 0;
  border-radius: 999px;
  padding: 10px 20px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.5);
}

/* ---- Loader ---- */
.loader {
  display: flex;
  gap: 8px;
  padding: 12px 16px 6px;
  flex-shrink: 0;
}
.loader input {
  flex: 1;
  min-width: 0;
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 13px 14px;
  color: var(--text);
  font-size: 16px; /* 16px avoids iOS zoom-on-focus */
  outline: none;
}
.loader input:focus { border-color: var(--accent-2); }

.btn {
  border: 0;
  border-radius: 12px;
  padding: 13px 18px;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
}
.btn.primary { background: var(--accent); color: #fff; }
.btn.primary:active { transform: scale(0.97); }
.btn.big { font-size: 17px; padding: 15px 32px; margin-top: 18px; border-radius: 14px; }
.btn.ghost { background: var(--panel-2); color: var(--text); border: 1px solid var(--line); }
.btn.ghost:active { transform: scale(0.97); }
.btn.ghost i { margin-right: 6px; }

.status {
  min-height: 16px;
  margin: 2px 18px 0;
  font-size: 12px;
  color: var(--muted);
  flex-shrink: 0;
}

/* ---- Chat ---- */
.chat {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
  border-top: 1px solid var(--line);
  margin-top: 6px;
}
.chat-log {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 12px 16px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  font-size: 14px;
}
.chat-msg { line-height: 1.4; word-break: break-word; }
.chat-msg .who { color: var(--accent-2); font-weight: 600; margin-right: 6px; }
.chat-msg.system { color: var(--muted); font-style: italic; font-size: 12.5px; }

.chat-form {
  display: flex;
  gap: 8px;
  padding: 10px 12px calc(10px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--line);
  background: var(--panel);
  flex-shrink: 0;
}
.chat-form input {
  flex: 1;
  min-width: 0;
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 12px 16px;
  color: var(--text);
  font-size: 16px;
  outline: none;
}
.chat-form input:focus { border-color: var(--accent-2); }
.send-btn {
  width: 46px;
  height: 46px;
  border-radius: 50%;
  border: 0;
  background: var(--accent);
  color: #fff;
  font-size: 18px;
  cursor: pointer;
  flex-shrink: 0;
}
.send-btn:active { transform: scale(0.94); }

/* ---- Join gate ---- */
.gate {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: center;
  background: radial-gradient(circle at 50% 32%, #1a1c25, #07080b 72%);
  padding: env(safe-area-inset-top) 24px env(safe-area-inset-bottom);
}
.gate.hidden { display: none; }
.gate-card { text-align: center; max-width: 360px; }
.gate-icon { font-size: 56px; color: var(--accent); }
.gate-card h1 { margin: 6px 0 10px; font-size: 30px; letter-spacing: 0.5px; }
.gate-card p { color: var(--muted); line-height: 1.6; font-size: 15px; margin: 0; }
.gate-input {
  width: 100%;
  margin-top: 20px;
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 15px 16px;
  color: var(--text);
  font-size: 16px; /* 16px avoids iOS zoom-on-focus */
  text-align: center;
  outline: none;
}
.gate-input:focus { border-color: var(--accent-2); }
.gate-card .btn.big { width: 100%; }
.gate-card #gateCreate { margin-top: 10px; }

/* ---- Floating emoji reactions ---- */
.reaction-layer { position: absolute; inset: 0; pointer-events: none; z-index: 7; overflow: hidden; }
.reaction {
  position: absolute;
  bottom: 8%;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
  will-change: transform, opacity;
  animation: floatUp 2.6s ease-out forwards;
}
.reaction-emoji { font-size: 34px; line-height: 1; filter: drop-shadow(0 2px 4px rgba(0, 0, 0, 0.5)); }
.reaction-name {
  max-width: 90px;
  padding: 1px 7px;
  border-radius: 999px;
  background: rgba(0, 0, 0, 0.6);
  color: #fff;
  font-size: 11px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
@keyframes floatUp {
  0%   { transform: translateY(0) scale(0.4); opacity: 0; }
  12%  { transform: translateY(-10%) scale(1.1); opacity: 1; }
  100% { transform: translateY(-280%) scale(1); opacity: 0; }
}

/* ---- Reactions bar ---- */
.reactions {
  display: flex;
  gap: 6px;
  padding: 8px 12px 4px;
  overflow-x: auto;
  overflow-y: hidden;
  scrollbar-width: none;
  -webkit-overflow-scrolling: touch; /* momentum scroll on iOS */
  overscroll-behavior-x: contain;
  scroll-snap-type: x proximity;
  flex-shrink: 0;
}
.reactions::-webkit-scrollbar { display: none; }
.reactions button {
  flex: 0 0 auto;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: 1px solid var(--line);
  background: var(--panel-2);
  font-size: 20px;
  line-height: 1;
  cursor: pointer;
  scroll-snap-align: center;
  transition: transform 0.08s ease, background 0.12s ease;
}
.reactions button:active { transform: scale(0.86); background: var(--panel); }

/* ---- Typing indicator ---- */
.typing {
  padding: 0 16px 6px;
  font-size: 12px;
  color: var(--muted);
  font-style: italic;
  min-height: 14px;
}
.typing[hidden] { display: none; }

/* ---- Scrim (shared by drawer + sheet) ---- */
.scrim {
  position: fixed;
  inset: 0;
  z-index: 40;
  background: rgba(0, 0, 0, 0.5);
  backdrop-filter: blur(2px);
}
.scrim[hidden] { display: none; }

/* ---- Members drawer ---- */
.drawer {
  position: fixed;
  top: 0;
  right: 0;
  z-index: 45;
  width: min(320px, 84vw);
  height: 100dvh;
  background: var(--panel);
  border-left: 1px solid var(--line);
  transform: translateX(100%);
  transition: transform 0.25s ease;
  display: flex;
  flex-direction: column;
  padding: max(14px, env(safe-area-inset-top)) 16px calc(14px + env(safe-area-inset-bottom));
}
.drawer.open { transform: translateX(0); }
.drawer-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 14px; }
.drawer-head h2 { font-size: 17px; margin: 0; }

.me-card {
  display: flex;
  gap: 12px;
  align-items: center;
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 12px;
  margin-bottom: 16px;
}
.me-fields { flex: 1; min-width: 0; }
.me-fields label { display: block; font-size: 11px; color: var(--muted); margin-bottom: 5px; letter-spacing: 0.3px; }
.me-row { display: flex; gap: 6px; }
.me-row input {
  flex: 1;
  min-width: 0;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 9px;
  padding: 9px 10px;
  color: var(--text);
  font-size: 16px;
  outline: none;
}
.me-row input:focus { border-color: var(--accent-2); }

.avatar {
  width: 44px;
  height: 44px;
  flex-shrink: 0;
  border-radius: 50%;
  display: grid;
  place-items: center;
  font-weight: 700;
  font-size: 18px;
  color: #fff;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  text-transform: uppercase;
}
.avatar.sm { width: 34px; height: 34px; font-size: 14px; }

.btn-sm {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  border: 0;
  border-radius: 10px;
  padding: 9px 16px;
  background: var(--accent);
  color: #fff;
  font-size: 13.5px;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
}
.btn-sm i { font-size: 13px; }
.btn-sm:active { transform: scale(0.96); }

.list-head { font-size: 11px; letter-spacing: 0.6px; color: var(--muted); text-transform: uppercase; margin: 0 0 6px; }
.member-list { flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 4px; }
.member-list .alone-hint { color: var(--muted); font-size: 13px; line-height: 1.5; padding: 8px 6px; }

.drawer-foot { border-top: 1px solid var(--line); padding-top: 12px; margin-top: 10px; }
.foot-label { font-size: 11px; color: var(--muted); margin: 0 0 7px; }
.invite-row { display: flex; gap: 6px; align-items: center; }
.invite-link {
  flex: 1;
  min-width: 0;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 9px;
  padding: 9px 10px;
  font-size: 12px;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.member {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px 8px;
  padding: 8px 6px;
  border-radius: 10px;
}
.member .avatar.sm { flex: none; }
.member .m-name { min-width: 0; margin-right: auto; font-size: 15px; font-weight: 500; }
.member .m-you { color: var(--muted); font-size: 12px; font-weight: 400; margin-left: 4px; }
.member .m-leader,
.member .m-ctrl {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  flex: none;
  font-size: 9.5px;
  font-weight: 700;
  letter-spacing: 0.3px;
  text-transform: uppercase;
  line-height: 1;
  padding: 4px 9px;
  border-radius: 999px;
  white-space: nowrap;
}
.member .m-leader {
  color: #f7d070;
  background: rgba(245, 196, 81, 0.13);
  border: 1px solid rgba(245, 196, 81, 0.32);
}
.member .m-ctrl {
  color: var(--ok);
  background: rgba(74, 222, 128, 0.12);
  border: 1px solid rgba(74, 222, 128, 0.3);
}

/* Leader-only lock toggle in the members drawer */
.perm-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 6px;
  margin-top: 6px;
  border-top: 1px solid var(--line);
}
.perm-row .perm-text { display: flex; flex-direction: column; gap: 2px; }
.perm-row .perm-title { font-size: 14px; font-weight: 500; }
.perm-row .perm-sub { font-size: 12px; color: var(--muted); }
.perm-row .perm-switch { margin-left: auto; width: 20px; height: 20px; accent-color: var(--accent, #f5c451); flex: none; }

/* Disabled add-video button (member, room locked) */
.icon-btn:disabled { opacity: 0.35; cursor: not-allowed; }
.icon-btn:disabled:active { transform: none; }

/* ---- Add-video bottom sheet ---- */
.sheet {
  position: fixed;
  left: 50%;
  bottom: 0;
  z-index: 45;
  width: 100%;
  max-width: 480px;
  transform: translate(-50%, 100%);
  transition: transform 0.28s ease;
  background: var(--panel);
  border-top: 1px solid var(--line);
  border-radius: 22px 22px 0 0;
  padding: 8px 18px calc(20px + env(safe-area-inset-bottom));
}
.sheet.open { transform: translate(-50%, 0); }
.sheet-grip { width: 40px; height: 4px; border-radius: 2px; background: var(--line); margin: 4px auto 12px; }
.sheet h2 { font-size: 18px; margin: 0 0 12px; }
.sheet .loader { padding: 0; }
.sheet-hint { font-size: 12px; color: var(--muted); line-height: 1.5; margin: 12px 0 0; }
.btn.block { width: 100%; margin-top: 12px; }

/* ---- Font Awesome icon polish ---- */
/* Glyphs sit a hair high by default — nudge them onto the text baseline. */
.fa-solid, .fa-regular, .fa-brands { line-height: 1; vertical-align: -1px; }
/* Round icon buttons: center the glyph and tame its size. */
.icon-btn i { font-size: 17px; vertical-align: 0; }
.send-btn { display: grid; place-items: center; }
.send-btn i { font-size: 16px; vertical-align: 0; }
/* Tinted accents inherit their container colour, just need sizing. */
.users i { font-size: 12px; }
.resync i { font-size: 11px; }
.ph-emoji i { color: var(--muted); }
/* Small inline status dots/crowns in the roster. */
.member .m-leader i { font-size: 10px; vertical-align: 0; }
.member .m-ctrl i { font-size: 7px; vertical-align: 0; animation: ctrl-pulse 1.6s ease-in-out infinite; }
@keyframes ctrl-pulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.35; } }
/* Pill/text buttons: keep icon and label snug. */
.pill-btn i, .btn-sm i { margin-right: 1px; }

/* ---- Desktop layout: theater video + chat sidebar ---------------------- */
/* Below this width the app stays a centered phone frame (see the 520px rule).
   At ≥900px it expands into a two-column grid: the stage (player/status/
   reactions) fills the space on the left, chat docks as a sidebar on the right. */
@media (min-width: 900px) {
  body { padding: 0; align-items: stretch; justify-content: center; }
  .app {
    max-width: 1700px;
    width: 100%;
    height: 100dvh;
    margin: 0 auto;
    border-radius: 0;
    box-shadow: none;
    overflow: hidden;
    display: grid;
    grid-template-columns: minmax(0, 1fr) 380px;
    grid-template-rows: auto auto minmax(0, 1fr);
    grid-template-areas:
      "bar   bar"
      "strip strip"
      "stage chat";
  }
  .appbar     { grid-area: bar; }
  .room-strip { grid-area: strip; }

  /* Left column: let the player grow to fill the height; the video letterboxes
     itself via object-fit: contain, so it stays 16:9 inside the black stage. */
  .stage { grid-area: stage; flex: 1; min-height: 0; background: #000; }
  .player { flex: 1 1 auto; aspect-ratio: auto; min-height: 0; }
  .status { background: var(--bg); }
  .reactions { background: var(--bg); padding-bottom: 8px; }

  /* Right column: chat fills the full height beside the stage. */
  .chat {
    grid-area: chat;
    margin-top: 0;
    border-top: 0;
    border-left: 1px solid var(--line);
    min-height: 0;
  }

  /* Hover affordances make sense once there's a pointer. */
  .icon-btn:hover, .send-btn:hover, .reactions button:hover { filter: brightness(1.15); }
  .pill-btn:hover, .btn.primary:hover, .btn-sm:hover { filter: brightness(1.08); }
  .resync:hover { background: var(--panel); color: var(--text); }

  /* The add-video "sheet" becomes a centered modal dialog on desktop rather
     than a bottom sheet (which looks out of place with a mouse). */
  .sheet {
    top: 50%;
    bottom: auto;
    left: 50%;
    width: min(460px, calc(100% - 40px));
    max-width: 460px;
    border: 1px solid var(--line);
    border-radius: 18px;
    padding: 20px 22px 24px;
    transform: translate(-50%, -46%);
    opacity: 0;
    visibility: hidden;
    transition: transform 0.22s ease, opacity 0.22s ease, visibility 0.22s ease;
  }
  .sheet.open { transform: translate(-50%, -50%); opacity: 1; visibility: visible; }
  .sheet-grip { display: none; } /* drag handle is a touch affordance */
}
