:root {
  color-scheme: dark;
  --bg: #0b0d12;
  --bg-2: #11141b;
  --panel: rgba(24, 28, 38, 0.72);
  --card: #1b1f29;
  --stroke: rgba(255, 255, 255, 0.08);
  --accent: #5aa2ff;
  --accent-2: #7c5cff;
  --danger: #ff5a6a;
  --text: #eef1f7;
  --muted: #98a2b6;
  --ctrl-bg: rgba(38, 43, 56, 0.85);
  --shadow: 0 18px 50px rgba(0, 0, 0, 0.45);
  --radius: 16px;
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
}

body {
  margin: 0;
  font-family: "Inter", system-ui, -apple-system, sans-serif;
  -webkit-font-smoothing: antialiased;
  background:
    radial-gradient(1200px 700px at 15% -10%, #1a2030 0%, transparent 55%),
    radial-gradient(1000px 700px at 110% 10%, #1d1830 0%, transparent 50%),
    var(--bg);
  color: var(--text);
  overflow: hidden;
}

/* ---------- App shell ---------- */

#app {
  height: 100dvh;
  display: grid;
  grid-template-columns: 1fr 340px;
  grid-template-rows: auto 1fr;
  grid-template-areas:
    "topbar topbar"
    "stage  panel";
  gap: 0.9rem;
  padding: 0.9rem;
}

#topbar {
  grid-area: topbar;
  display: flex;
  align-items: baseline;
  gap: 0.9rem;
  padding: 0.1rem 0.4rem;
}

.brand {
  font-size: 1.05rem;
  font-weight: 700;
  letter-spacing: 0.01em;
}

#status {
  margin: 0;
  color: var(--muted);
  font-size: 0.85rem;
}

/* ---------- Stage ---------- */

#stage {
  grid-area: stage;
  position: relative;
  border-radius: var(--radius);
  background: linear-gradient(180deg, var(--bg-2), #0c0e14);
  border: 1px solid var(--stroke);
  overflow: hidden;
  min-height: 0;
}

#grid {
  position: absolute;
  inset: 0;
  display: grid;
  gap: 0.5rem;
  padding: 0.5rem;
  grid-template-columns: 1fr;
  place-items: stretch;
}

#grid[data-count="0"] {
  grid-template-columns: 1fr;
}
#grid[data-count="1"] {
  grid-template-columns: 1fr;
}
#grid[data-count="2"] {
  grid-template-columns: 1fr 1fr;
}
#grid[data-count="3"],
#grid[data-count="4"] {
  grid-template-columns: 1fr 1fr;
  grid-auto-rows: 1fr;
}
#grid[data-count="5"],
#grid[data-count="6"] {
  grid-template-columns: 1fr 1fr 1fr;
  grid-auto-rows: 1fr;
}
#grid[data-count="empty"] {
  place-items: center;
}

#grid:empty::after {
  content: "Waiting for others to join…";
  color: var(--muted);
  font-size: 0.95rem;
  place-self: center;
  grid-column: 1 / -1;
}

.tile {
  position: relative;
  border-radius: 12px;
  overflow: hidden;
  background: #05070b;
  min-height: 0;
}

.tile video {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.tile .badge {
  position: absolute;
  bottom: 0.5rem;
  left: 0.55rem;
  font-size: 0.78rem;
  font-weight: 600;
  background: rgba(0, 0, 0, 0.5);
  backdrop-filter: blur(6px);
  padding: 0.18rem 0.5rem;
  border-radius: 8px;
}

/* ---------- Self picture-in-picture ---------- */

#self-pip {
  position: absolute;
  top: 0.9rem;
  right: 0.9rem;
  width: 22%;
  max-width: 220px;
  min-width: 120px;
  aspect-ratio: 3 / 4;
  border-radius: 14px;
  border: 1px solid rgba(255, 255, 255, 0.14);
  box-shadow: var(--shadow);
  z-index: 5;
  transition: inset 0.25s ease, width 0.25s ease, max-width 0.25s ease;
}

#self-pip video {
  transform: scaleX(-1);
}

/* When nobody else is here, you fill the stage */
#stage.solo #self-pip {
  top: 0.5rem;
  right: 0.5rem;
  left: 0.5rem;
  bottom: 0.5rem;
  width: auto;
  max-width: none;
  aspect-ratio: auto;
  box-shadow: none;
  border-color: var(--stroke);
}

body.cam-off #self-pip .cam-off {
  display: grid;
}

#self-pip .cam-off {
  display: none;
  position: absolute;
  inset: 0;
  place-items: center;
  background: #11141b;
  color: var(--muted);
}

#self-pip .cam-off svg {
  width: 30%;
  max-width: 48px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.7;
  stroke-linecap: round;
  stroke-linejoin: round;
}

/* ---------- Control bar ---------- */

#controls {
  position: absolute;
  left: 50%;
  bottom: 1rem;
  transform: translateX(-50%);
  display: flex;
  gap: 0.65rem;
  padding: 0.5rem;
  border-radius: 999px;
  background: var(--ctrl-bg);
  backdrop-filter: blur(14px);
  border: 1px solid var(--stroke);
  box-shadow: var(--shadow);
  z-index: 6;
}

.ctrl {
  width: 52px;
  height: 52px;
  border-radius: 50%;
  border: 0;
  background: rgba(255, 255, 255, 0.1);
  color: var(--text);
  display: grid;
  place-items: center;
  cursor: pointer;
  transition: background 0.15s ease, transform 0.1s ease;
  position: relative;
}

.ctrl:hover {
  background: rgba(255, 255, 255, 0.18);
}

.ctrl:active {
  transform: scale(0.94);
}

.ctrl svg {
  width: 22px;
  height: 22px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.ctrl .icon-off {
  display: none;
}

.ctrl.off {
  background: var(--danger);
  color: #fff;
}

.ctrl.off .icon-on {
  display: none;
}

.ctrl.off .icon-off {
  display: block;
}

.chat-toggle {
  display: none;
}

.chat-toggle .dot {
  position: absolute;
  top: 8px;
  right: 8px;
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: var(--accent);
  border: 2px solid var(--ctrl-bg);
}

/* ---------- Side panel ---------- */

#panel {
  grid-area: panel;
  display: flex;
  flex-direction: column;
  gap: 0.9rem;
  min-height: 0;
}

#panel-close {
  display: none;
}

.card {
  background: var(--panel);
  backdrop-filter: blur(14px);
  border: 1px solid var(--stroke);
  border-radius: var(--radius);
  padding: 1rem;
}

.card h2 {
  margin: 0 0 0.7rem;
  font-size: 0.78rem;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.08em;
}

#peers {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

#peers li {
  display: flex;
  align-items: center;
  gap: 0.55rem;
  font-size: 0.92rem;
}

#peers li::before {
  content: "";
  width: 0.55rem;
  height: 0.55rem;
  border-radius: 50%;
  background: #46d27a;
  box-shadow: 0 0 8px rgba(70, 210, 122, 0.7);
}

#chat {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

#chat-log {
  list-style: none;
  margin: 0 0 0.75rem;
  padding: 0;
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}

#chat-log li {
  font-size: 0.9rem;
  line-height: 1.35;
}

#chat-log li .who {
  color: var(--accent);
  font-weight: 600;
  margin-right: 0.35rem;
}

#chat-log li.system {
  color: var(--muted);
  font-style: italic;
}

#chat-form {
  display: flex;
  gap: 0.5rem;
}

input {
  background: #0e1118;
  border: 1px solid var(--stroke);
  border-radius: 10px;
  color: var(--text);
  padding: 0.6rem 0.75rem;
  font-size: 0.95rem;
  outline: none;
  transition: border-color 0.15s ease;
}

input:focus {
  border-color: var(--accent);
}

#chat-input {
  flex: 1;
}

button {
  background: linear-gradient(180deg, var(--accent), var(--accent-2));
  color: #fff;
  border: 0;
  border-radius: 10px;
  padding: 0.6rem 1.1rem;
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
}

button:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

#scrim {
  display: none;
}

/* ---------- Login ---------- */

#login {
  display: none;
}

body.signed-out #login {
  display: grid;
  place-items: center;
  height: 100dvh;
  padding: 1.5rem;
}

body.signed-out #app {
  display: none;
}

#login-form {
  background: var(--panel);
  backdrop-filter: blur(16px);
  border: 1px solid var(--stroke);
  padding: 1.75rem;
  border-radius: 18px;
  width: 340px;
  max-width: 100%;
  display: flex;
  flex-direction: column;
  gap: 0.85rem;
  box-shadow: var(--shadow);
}

.login-brand {
  font-weight: 700;
  font-size: 1.15rem;
  background: linear-gradient(90deg, var(--accent), var(--accent-2));
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
}

#login-form h2 {
  margin: 0 0 0.3rem;
  font-size: 1.25rem;
}

#login-form label {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  font-size: 0.82rem;
  color: var(--muted);
}

#login-form button {
  margin-top: 0.3rem;
  padding: 0.7rem;
  font-size: 1rem;
}

#login-error {
  color: var(--danger);
  font-size: 0.88rem;
  min-height: 1.2em;
  margin: 0;
}

/* ---------- Mobile ---------- */

@media (max-width: 820px) {
  #app {
    grid-template-columns: 1fr;
    grid-template-rows: auto 1fr;
    grid-template-areas:
      "topbar"
      "stage";
    gap: 0.6rem;
    padding: 0.6rem;
    padding-top: max(0.6rem, env(safe-area-inset-top));
  }

  .chat-toggle {
    display: grid;
  }

  #grid[data-count="2"] {
    grid-template-columns: 1fr;
    grid-auto-rows: 1fr;
  }
  #grid[data-count="3"],
  #grid[data-count="4"],
  #grid[data-count="5"],
  #grid[data-count="6"] {
    grid-template-columns: 1fr 1fr;
  }

  #self-pip {
    width: 32%;
    min-width: 96px;
    top: 0.6rem;
    right: 0.6rem;
  }

  #panel {
    position: fixed;
    inset: 0 0 0 auto;
    width: min(86vw, 360px);
    padding: 0.9rem;
    padding-top: max(0.9rem, env(safe-area-inset-top));
    background: var(--bg-2);
    border-left: 1px solid var(--stroke);
    transform: translateX(100%);
    transition: transform 0.25s ease;
    z-index: 20;
  }

  body.panel-open #panel {
    transform: translateX(0);
  }

  #panel-close {
    display: block;
    align-self: flex-end;
    background: none;
    color: var(--muted);
    font-size: 1.6rem;
    line-height: 1;
    padding: 0.2rem 0.5rem;
  }

  body.panel-open #scrim {
    display: block;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: 15;
  }

  #controls {
    bottom: max(1rem, env(safe-area-inset-bottom));
  }
}
