:root {
  --bg: #0f1420;
  --panel: #1a2130;
  --panel-2: #232c3d;
  --text: #e8ecf3;
  --muted: #93a0b5;
  --accent: #4f7cff;
  --danger: #e5484d;
  --ok: #30a46c;
  --radius: 14px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial,
    sans-serif;
}

.brand {
  font-weight: 700;
  font-size: 1.4rem;
  letter-spacing: -0.02em;
}
.brand span { color: var(--accent); }
.muted { color: var(--muted); }
.tiny { font-size: 0.8rem; }
.error { color: var(--danger); min-height: 1.2em; font-size: 0.9rem; }
.hidden { display: none !important; }

/* ---------- Lobby ---------- */
.lobby {
  display: grid;
  place-items: center;
}
.lobby-card {
  width: min(420px, 92vw);
  background: var(--panel);
  padding: 2rem;
  border-radius: var(--radius);
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.4);
}
.join-form { display: grid; gap: 0.7rem; margin: 1.2rem 0 0.8rem; }
input, select {
  background: var(--panel-2);
  border: 1px solid #324054;
  color: var(--text);
  padding: 0.7rem 0.8rem;
  border-radius: 10px;
  font-size: 1rem;
  width: 100%;
}
button {
  cursor: pointer;
  border: none;
  border-radius: 10px;
  font-size: 1rem;
  padding: 0.7rem 1rem;
  background: var(--accent);
  color: #fff;
  font-weight: 600;
}
button:hover { filter: brightness(1.08); }

/* ---------- Pre-join ---------- */
.prejoin { display: grid; place-items: center; min-height: 100%; padding: 1rem; }
.prejoin-card {
  width: min(460px, 94vw);
  background: var(--panel);
  border-radius: var(--radius);
  padding: 1.6rem;
  display: grid;
  gap: 0.85rem;
}
#previewVideo {
  width: 100%;
  aspect-ratio: 16 / 9;
  background: #000;
  border-radius: 10px;
  object-fit: cover;
  transform: scaleX(-1);
}
.device-row { display: grid; gap: 0.5rem; grid-template-columns: 1fr 1fr; }
.device-row label { font-size: 0.8rem; color: var(--muted); display: grid; gap: 0.25rem; }
.prejoin-toggles { display: flex; gap: 0.5rem; }
.toggle {
  flex: 1;
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid #324054;
}
.toggle.off { background: var(--danger); border-color: var(--danger); }
.primary { background: var(--accent); }

/* ---------- Call ---------- */
.call { position: fixed; inset: 0; display: flex; flex-direction: column; }
.video-grid {
  flex: 1;
  display: grid;
  gap: 10px;
  padding: 12px;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  align-content: center;
  overflow: auto;
}
.tile {
  position: relative;
  background: #000;
  border-radius: 12px;
  overflow: hidden;
  aspect-ratio: 16 / 9;
  min-height: 0;
}
.tile video { width: 100%; height: 100%; object-fit: cover; background: #000; }
.tile.self video { transform: scaleX(-1); }
.tile .label {
  position: absolute;
  left: 10px;
  bottom: 10px;
  background: rgba(0, 0, 0, 0.55);
  padding: 3px 10px;
  border-radius: 20px;
  font-size: 0.85rem;
}
.tile .badges {
  position: absolute;
  right: 10px;
  top: 10px;
  display: flex;
  gap: 6px;
}
.tile .badge {
  background: rgba(0, 0, 0, 0.55);
  border-radius: 6px;
  padding: 2px 6px;
  font-size: 0.8rem;
}
.tile.speaking { outline: 3px solid var(--accent); outline-offset: -3px; }

/* ---------- Controls ---------- */
.control-bar {
  display: flex;
  justify-content: center;
  gap: 12px;
  padding: 14px;
  background: linear-gradient(transparent, rgba(0, 0, 0, 0.35));
}
.ctrl {
  width: 56px;
  height: 56px;
  border-radius: 50%;
  background: var(--panel-2);
  font-size: 1.3rem;
  display: grid;
  place-items: center;
  padding: 0;
}
.ctrl.off { background: var(--danger); }
.ctrl.active { background: var(--accent); }
.ctrl.leave { background: var(--danger); transform: rotate(135deg); }

/* ---------- Chat ---------- */
.chat-panel {
  position: fixed;
  right: 0;
  top: 0;
  bottom: 84px;
  width: min(340px, 90vw);
  background: var(--panel);
  display: flex;
  flex-direction: column;
  border-left: 1px solid #2a3447;
}
.chat-head {
  padding: 12px 14px;
  font-weight: 600;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid #2a3447;
}
.icon { background: transparent; color: var(--muted); padding: 2px 8px; }
.chat-log { flex: 1; overflow-y: auto; padding: 12px; display: grid; gap: 8px; align-content: start; }
.chat-msg { font-size: 0.9rem; }
.chat-msg .who { color: var(--accent); font-weight: 600; margin-right: 6px; }
.chat-form { display: flex; gap: 8px; padding: 10px; border-top: 1px solid #2a3447; }
.chat-form input { flex: 1; }
.chat-form button { padding: 0.6rem 0.9rem; }

/* ---------- Toast ---------- */
.toast {
  position: fixed;
  left: 50%;
  bottom: 92px;
  transform: translateX(-50%);
  background: var(--panel-2);
  padding: 8px 16px;
  border-radius: 20px;
  font-size: 0.9rem;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.4);
  z-index: 30;
  max-width: 86vw;
  text-align: center;
}

/* ============================================================
   Responsive + WhatsApp-style stage mode
   ============================================================ */

/* Use dynamic viewport height so mobile browser chrome doesn't crop the call. */
.call {
  height: 100vh;
  height: 100dvh;
}

/* Floating control pill (all screen sizes), auto-hides in stage mode. */
.control-bar {
  position: fixed;
  left: 50%;
  bottom: calc(14px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  width: auto;
  gap: 14px;
  padding: 10px 14px;
  background: rgba(16, 20, 32, 0.62);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border-radius: 44px;
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.45);
  z-index: 10;
  transition: transform 0.25s ease, opacity 0.25s ease;
}
.call.ui-hidden .control-bar {
  transform: translateX(-50%) translateY(180%);
  opacity: 0;
  pointer-events: none;
}
/* keep grid tiles clear of the floating bar */
.video-grid { padding-bottom: 96px; }
.chat-panel { z-index: 20; }

/* --- Stage mode: one peer fills the screen, the other is a PiP --- */
/* Applied at all widths for a true 1-on-1 WhatsApp feel. */
.video-grid.mode-stage {
  display: block;
  padding: 0;
  overflow: hidden;
}
.mode-stage .tile.stage-main {
  position: fixed;
  inset: 0;
  width: 100vw;
  height: 100vh;
  height: 100dvh;
  border-radius: 0;
  aspect-ratio: auto;
  background: #000;
  z-index: 1;
}
.mode-stage .tile.stage-pip {
  position: fixed;
  top: calc(14px + env(safe-area-inset-top));
  right: 14px;
  left: auto;
  width: 30vw;
  max-width: 150px;
  min-width: 104px;
  height: auto;
  aspect-ratio: 3 / 4;
  border-radius: 14px;
  z-index: 6;
  box-shadow: 0 6px 22px rgba(0, 0, 0, 0.55);
  border: 1px solid rgba(255, 255, 255, 0.16);
  touch-action: none;        /* we handle drag ourselves */
  cursor: grab;
}
.mode-stage .tile.stage-pip:active { cursor: grabbing; }
.mode-stage .tile.stage-pip .label { display: none; }
/* lift the main label above the control pill */
.mode-stage .tile.stage-main .label {
  left: 14px;
  bottom: calc(86px + env(safe-area-inset-bottom));
}
.call.ui-hidden .tile .label { opacity: 0; transition: opacity 0.25s ease; }

/* --- Phone tweaks --- */
@media (max-width: 640px) {
  /* grid mode (3+ people) stacks to a single column */
  .video-grid {
    grid-template-columns: 1fr;
    gap: 8px;
    padding: 8px 8px 96px;
  }
  .ctrl { width: 52px; height: 52px; font-size: 1.2rem; }
  .control-bar { gap: 10px; }

  /* smaller PiP on phones */
  .mode-stage .tile.stage-pip {
    width: 28vw;
    max-width: 120px;
    min-width: 92px;
  }

  /* chat becomes a bottom sheet */
  .chat-panel {
    top: auto;
    bottom: 0;
    right: 0;
    left: 0;
    width: 100%;
    height: 70vh;
    border-left: none;
    border-top: 1px solid #2a3447;
    border-radius: 16px 16px 0 0;
  }

  /* pre-join fills the phone nicely */
  .prejoin-card { width: 100%; padding: 1.1rem; gap: 0.7rem; }
  #previewVideo { aspect-ratio: 3 / 4; max-height: 46vh; margin: 0 auto; }
}

/* very short landscape phones: shrink preview */
@media (max-height: 480px) {
  #previewVideo { aspect-ratio: 16 / 9; max-height: 38vh; }
}
