/* ============================================================================
   LiveKit POC — lobby + full-screen call UI
   Layout goal: Zoom/FaceTime-like. The remote participant fills the screen,
   your own camera floats in a draggable corner tile, controls sit in a
   floating bar. iOS safe areas are respected throughout.
   ========================================================================== */
:root {
  --bg: #0b0e14;
  --panel: #131822;
  --panel-2: #1a212e;
  --line: #232c3d;
  --text: #e7ebf3;
  --muted: #8e9bb3;
  --accent: #34d399;
  --accent-dim: #1d7d5c;
  --danger: #f87171;
  --warn: #fbbf24;
  --glass: rgba(12, 16, 23, 0.62);
  --radius: 14px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
}

* { box-sizing: border-box; }

/* An author `display` rule beats the UA rule for [hidden], which is exactly how
   the lobby and the call view once ended up on screen at the same time. */
[hidden] { display: none !important; }

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

body {
  background: #000;
  color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior: none;
}
body.in-call { overflow: hidden; }

.muted { color: var(--muted); }
code { background: var(--panel-2); padding: 1px 6px; border-radius: 6px; font-size: 0.92em; }

button {
  font: inherit;
  color: inherit;
  border: 1px solid var(--line);
  background: var(--panel-2);
  border-radius: 10px;
  padding: 9px 14px;
  cursor: pointer;
  touch-action: manipulation;
  transition: background 0.15s, border-color 0.15s, transform 0.05s;
}
button:hover { background: #222c3c; }
button:active { transform: translateY(1px); }
button:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
button.primary { background: var(--accent); border-color: var(--accent); color: #06231a; font-weight: 600; }
button.primary:hover { background: #45dfa9; }
button.primary:disabled { opacity: 0.6; cursor: progress; }

input {
  font: inherit;
  color: inherit;
  background: #0e131c;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px 12px;
  width: 100%;
}
input:focus { outline: none; border-color: var(--accent-dim); }
.error { color: var(--danger); font-size: 14px; }
.hint { color: var(--muted); font-size: 13px; margin-top: 4px; }

/* =============================================================== lobby ==== */
.lobby {
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: 24px;
  background: radial-gradient(1200px 600px at 50% -10%, #16202f 0%, var(--bg) 60%);
}

.card {
  width: min(100%, 420px);
  background: color-mix(in srgb, var(--panel) 92%, transparent);
  border: 1px solid var(--line);
  border-radius: 18px;
  padding: 26px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  box-shadow: 0 24px 60px -30px #000;
}
.card h1 { margin: 0; font-size: 26px; letter-spacing: -0.02em; }
.card h1 em { font-style: normal; color: var(--accent); }
.card > p { margin: 0; }
.card label { font-size: 13px; color: var(--muted); margin-bottom: -6px; }
#code-field { display: flex; flex-direction: column; gap: 12px; }

/* ================================================================ call ==== */
.call {
  position: fixed;
  inset: 0;
  background: #000;
  overflow: hidden;
}

/* legibility scrims behind the top bar and controls */
.call::before,
.call::after {
  content: "";
  position: absolute;
  left: 0;
  right: 0;
  height: 132px;
  pointer-events: none;
  z-index: 1;
}
.call::before { top: 0; background: linear-gradient(rgba(0, 0, 0, 0.66), transparent); }
.call::after { bottom: 0; background: linear-gradient(transparent, rgba(0, 0, 0, 0.72)); }

/* stage: the "main" participant fills the whole viewport */
.stage { position: absolute; inset: 0; }

.stage:not(.grid) > .tile {
  position: absolute;
  inset: 0;
  aspect-ratio: auto;
  border: 0;
  border-radius: 0;
}
.stage:not(.grid) > .tile .tile-video { object-fit: cover; }

/* grid layout for 3+ participants, or when the user picks it */
.stage.grid {
  display: grid;
  gap: 10px;
  padding: calc(var(--safe-top) + 66px) calc(var(--safe-right) + 10px)
    calc(var(--safe-bottom) + 108px) calc(var(--safe-left) + 10px);
  grid-template-columns: repeat(auto-fit, minmax(min(100%, 320px), 1fr));
  align-content: center;
  overflow: auto;
}
.stage.grid .tile { border-radius: 14px; }

/* filmstrip of the other participants while someone owns the stage */
.strip {
  position: absolute;
  z-index: 2;
  top: calc(var(--safe-top) + 58px);
  left: var(--safe-left);
  right: var(--safe-right);
  display: flex;
  gap: 8px;
  padding: 6px 10px;
  overflow-x: auto;
  justify-content: center;
  scrollbar-width: none;
}
.strip::-webkit-scrollbar { display: none; }
.strip .tile { flex: 0 0 auto; width: 118px; border-radius: 12px; }
.strip .tile .tile-bar { font-size: 11px; padding: 12px 6px 4px; }

/* self view: floating, draggable tile */
.pip {
  position: absolute;
  z-index: 3;
  width: min(36vw, 210px);
  aspect-ratio: 3 / 4;
  border-radius: 16px;
  overflow: hidden;
  background: #11161f;
  border: 1px solid rgba(255, 255, 255, 0.14);
  box-shadow: 0 14px 40px rgba(0, 0, 0, 0.55);
  touch-action: none;
  cursor: grab;
}
.pip.dragging { cursor: grabbing; box-shadow: 0 18px 48px rgba(0, 0, 0, 0.7); }
.pip-slot { position: absolute; inset: 0; }
.pip .tile,
.pip-slot .tile { position: absolute; inset: 0; aspect-ratio: auto; border: 0; border-radius: 0; }
.pip .tile-bar { font-size: 11px; padding: 12px 8px 5px; }

.pip[data-corner="br"] { right: calc(var(--safe-right) + 12px); bottom: calc(var(--safe-bottom) + 100px); }
.pip[data-corner="bl"] { left: calc(var(--safe-left) + 12px); bottom: calc(var(--safe-bottom) + 100px); }
.pip[data-corner="tr"] { right: calc(var(--safe-right) + 12px); top: calc(var(--safe-top) + 64px); }
.pip[data-corner="tl"] { left: calc(var(--safe-left) + 12px); top: calc(var(--safe-top) + 64px); }

@media (min-width: 760px) {
  .pip { width: 232px; aspect-ratio: 4 / 3; }
}

/* ------------------------------------------------------------------ tiles */
.tile {
  position: relative;
  overflow: hidden;
  aspect-ratio: 16 / 9;
  background: #0d1117;
  border: 1px solid rgba(255, 255, 255, 0.06);
}
.tile.screen { background: #000; }
.tile.screen .tile-video { object-fit: contain; }
.tile.local .tile-video { transform: scaleX(-1); } /* mirror your own camera only */

.tile-video {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: none;
  background: #0a0d13;
  -webkit-touch-callout: none;
}
.tile.has-video .tile-video { display: block; }
.tile.has-video .tile-placeholder { display: none; }
.tile.speaking { box-shadow: inset 0 0 0 3px var(--accent); }

.tile-placeholder { position: absolute; inset: 0; display: grid; place-items: center; }
.avatar {
  width: 72px;
  height: 72px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  background: var(--panel-2);
  border: 1px solid var(--line);
  font-size: 26px;
  font-weight: 600;
  color: var(--muted);
}

.tile-bar {
  position: absolute;
  inset: auto 0 0 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 20px 10px 8px;
  background: linear-gradient(transparent, rgba(0, 0, 0, 0.65));
  font-size: 13px;
}
.tile-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tile-badges { display: flex; gap: 6px; flex-shrink: 0; }

.badge {
  font-size: 11px;
  padding: 2px 7px;
  border-radius: 999px;
  background: rgba(0, 0, 0, 0.6);
  border: 1px solid var(--line);
  color: var(--muted);
  white-space: nowrap;
}
.badge.mute { color: var(--danger); border-color: #55303a; }
.badge.quality[data-q="poor"],
.badge.quality[data-q="lost"] { color: var(--warn); }

/* -------------------------------------------------------------- top bar --- */
.call-top {
  position: absolute;
  z-index: 4;
  top: calc(var(--safe-top) + 10px);
  left: calc(var(--safe-left) + 12px);
  right: calc(var(--safe-right) + 12px);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.pill {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
  padding: 6px 13px;
  border-radius: 999px;
  background: var(--glass);
  border: 1px solid rgba(255, 255, 255, 0.08);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  font-size: 14px;
}
.pill strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pill .muted { font-size: 12px; white-space: nowrap; }

.dot { width: 9px; height: 9px; border-radius: 50%; background: var(--warn); flex-shrink: 0; }
.dot[data-state="connected"] { background: var(--accent); }
.dot[data-state="reconnecting"] { background: var(--warn); animation: pulse 1.2s infinite; }
.dot[data-state="disconnected"] { background: var(--danger); }
@keyframes pulse { 50% { opacity: 0.35; } }

.top-actions { display: flex; gap: 8px; flex-shrink: 0; }

.icon-btn {
  width: 40px;
  height: 40px;
  padding: 0;
  display: grid;
  place-items: center;
  border-radius: 50%;
  background: var(--glass);
  border: 1px solid rgba(255, 255, 255, 0.08);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
}
.icon-btn svg {
  width: 19px;
  height: 19px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}

/* ------------------------------------------------------------- controls --- */
.controls {
  position: absolute;
  z-index: 4;
  left: var(--safe-left);
  right: var(--safe-right);
  bottom: calc(var(--safe-bottom) + 16px);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
}

.ctl {
  width: 56px;
  height: 56px;
  padding: 0;
  border-radius: 50%;
  display: grid;
  place-items: center;
  position: relative;
  background: rgba(255, 255, 255, 0.14);
  border: 1px solid rgba(255, 255, 255, 0.14);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
}
.ctl svg {
  width: 24px;
  height: 24px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}
/* off state: red icon, red tint, and a slash so it reads at a glance */
.ctl[data-on="false"] { color: var(--danger); background: rgba(60, 20, 26, 0.72); border-color: #55303a; }
.ctl[data-on="false"]::after {
  content: "";
  position: absolute;
  width: 62%;
  height: 2px;
  background: currentColor;
  transform: rotate(-45deg);
  border-radius: 2px;
}
.ctl[data-on="true"]::after { content: none; }

.ctl.end { width: 64px; background: #ff3b30; border-color: #ff3b30; color: #fff; }
.ctl.end:hover { background: #ff5747; }

.audio-prompt {
  position: absolute;
  z-index: 5;
  left: 50%;
  transform: translateX(-50%);
  bottom: calc(var(--safe-bottom) + 88px);
  padding: 10px 18px;
  border-radius: 999px;
  background: var(--warn);
  border: 0;
  color: #241a02;
  font-weight: 600;
}

.notice {
  position: absolute;
  z-index: 5;
  left: 50%;
  transform: translateX(-50%);
  top: calc(var(--safe-top) + 62px);
  max-width: 90vw;
  padding: 8px 15px;
  border-radius: 999px;
  background: rgba(42, 33, 19, 0.95);
  color: var(--warn);
  font-size: 13px;
  text-align: center;
}
.notice.error { background: rgba(42, 22, 24, 0.95); color: var(--danger); }

/* ------------------------------------------------------------------ chat --- */
.chat {
  position: absolute;
  z-index: 6;
  top: 0;
  right: 0;
  bottom: 0;
  width: min(370px, 100%);
  display: flex;
  flex-direction: column;
  background: rgba(15, 20, 28, 0.94);
  border-left: 1px solid rgba(255, 255, 255, 0.08);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
}
.chat-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: calc(var(--safe-top) + 12px) 12px 12px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.08);
}
#chat-log { flex: 1; overflow: auto; padding: 12px; display: flex; flex-direction: column; gap: 8px; }
#chat-log .msg { font-size: 14px; overflow-wrap: anywhere; }
#chat-log .msg .who { color: var(--accent); font-weight: 600; margin-right: 6px; }
#chat-log .msg.self .who { color: var(--muted); }
#chat-log .empty { color: var(--muted); font-size: 13px; }
#chat-form {
  display: flex;
  gap: 8px;
  padding: 10px 10px calc(var(--safe-bottom) + 10px);
  border-top: 1px solid rgba(255, 255, 255, 0.08);
}

#audio-sink { display: none; }

/* ---------------------------------------------------------------- mobile --- */
@media (max-width: 760px) {
  .pip { width: min(38vw, 150px); aspect-ratio: 3 / 4; }
  .pip[data-corner="br"],
  .pip[data-corner="bl"] { bottom: calc(var(--safe-bottom) + 96px); }
  .ctl { width: 52px; height: 52px; }
  .ctl.end { width: 58px; }
  .controls { gap: 9px; }
  .strip .tile { width: 96px; }
  .chat {
    top: auto;
    left: 0;
    width: 100%;
    height: min(74dvh, 560px);
    border-left: 0;
    border-top: 1px solid rgba(255, 255, 255, 0.08);
    border-radius: 18px 18px 0 0;
  }
  .chat-head { padding-top: 12px; }
}

@media (max-height: 520px) {
  /* landscape phones: keep the controls from eating the stage */
  .ctl { width: 46px; height: 46px; }
  .controls { bottom: calc(var(--safe-bottom) + 8px); }
  .pip { width: min(26vw, 150px); aspect-ratio: 4 / 3; }
  .pip[data-corner="tr"],
  .pip[data-corner="tl"] { top: calc(var(--safe-top) + 56px); }
}
