:root {
  --bg: #f6f3ee;
  --fg: #1b1b1b;
  --muted: #6a6a6a;
  --card: #ffffff;
  --accent: #2e86ab;
  --danger: #c83c3c;
  --ring: rgba(46, 134, 171, 0.35);
  --radius: 14px;
  --shadow: 0 2px 10px rgba(0, 0, 0, 0.08);
  --tap: 56px;
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
  font: 17px/1.45 system-ui, -apple-system, "Segoe UI", sans-serif;
  color: var(--fg);
  background: var(--bg);
}

body.home {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 24px 16px 40px;
}

.home header {
  max-width: 820px;
  width: 100%;
  text-align: center;
  margin: 24px 0 32px;
}
.home h1 {
  margin: 0 0 6px;
  font-size: clamp(2rem, 5vw, 2.6rem);
  letter-spacing: -0.02em;
}
.home .tagline {
  margin: 0;
  color: var(--muted);
  font-size: 1.05rem;
}

.home main {
  width: 100%;
  max-width: 820px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 28px;
}

.rooms {
  width: 100%;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 16px;
  min-height: 160px;
}
.rooms .empty {
  grid-column: 1 / -1;
  text-align: center;
  color: var(--muted);
  padding: 40px 0;
  margin: 0;
}

.room-card {
  display: block;
  background: var(--card);
  border: 1px solid rgba(0,0,0,0.06);
  border-radius: var(--radius);
  padding: 10px;
  text-decoration: none;
  color: inherit;
  box-shadow: var(--shadow);
  transition: transform 0.08s ease, box-shadow 0.08s ease;
}
.room-card:focus-visible,
.room-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 18px rgba(0,0,0,0.12);
  outline: none;
}
.room-card[aria-disabled="true"] {
  opacity: 0.5;
  pointer-events: none;
}
.room-card img {
  width: 100%;
  aspect-ratio: 4 / 3;
  object-fit: cover;
  border-radius: calc(var(--radius) - 4px);
  display: block;
  background: #ddd;
}
.room-card .meta {
  display: flex;
  justify-content: space-between;
  margin-top: 10px;
  font-size: 0.92rem;
  color: var(--muted);
}

button.primary {
  background: var(--accent);
  color: white;
  border: none;
  border-radius: 999px;
  padding: 16px 28px;
  min-height: var(--tap);
  font-size: 1.1rem;
  font-weight: 600;
  cursor: pointer;
  box-shadow: var(--shadow);
  transition: filter 0.1s ease;
}
button.primary:hover { filter: brightness(1.05); }
button.primary:active { filter: brightness(0.95); }
button.primary:disabled { opacity: 0.5; cursor: not-allowed; }

.capacity {
  color: var(--muted);
  font-size: 0.95rem;
  margin: 0;
}

.build {
  margin-top: auto;
  padding-top: 32px;
  color: var(--muted);
  font-size: 0.8rem;
  opacity: 0.6;
  font-family: ui-monospace, Menlo, Consolas, monospace;
}

/* Room page */

body.room {
  display: grid;
  /* Desktop: tiles fill most of the screen, chat is a sidebar, controls run
     the full width along the bottom. On mobile (below) we restack all three
     rows vertically and let the chat collapse to zero when closed. */
  grid-template-columns: 1fr 320px;
  grid-template-rows: 1fr auto auto auto;
  grid-template-areas:
    "tiles    chat"
    "compat   compat"
    "controls controls"
    "footer   footer";
  /* 100svh is the smallest viewport height — it guarantees the controls row
     sits above the iOS Safari URL bar (which 100dvh does not reliably do). */
  height: 100svh;
  background: #111;
  color: #eee;
}

.compat-banner {
  grid-area: compat;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 6px 14px;
  font-size: 0.85rem;
  color: #f5d28a;
  background: #2a2111;
  border-top: 1px solid #40311a;
  border-bottom: 1px solid #40311a;
}

.compat-banner[hidden] { display: none; }

.compat-icon {
  font-size: 0.95rem;
  line-height: 1;
}

.room-footer {
  grid-area: footer;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: 4px 10px 6px;
  font-size: 0.75rem;
  color: #8a8a8a;
  background: #0a0a0a;
  flex-wrap: wrap;
}

.room-footer-note {
  opacity: 0.75;
}

.room-footer-link {
  background: none;
  border: 1px solid #333;
  color: #bbb;
  font-size: 0.75rem;
  padding: 2px 8px;
  border-radius: 6px;
  cursor: pointer;
}

.room-footer-link:hover,
.room-footer-link:focus {
  border-color: #666;
  color: #fff;
}

.room-footer-status {
  min-width: 6em;
  opacity: 0.9;
}

.tiles {
  grid-area: tiles;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  padding: 8px;
  min-height: 0;
  margin: 0;
  /* Containing block for the tucked-away self tile on mobile. */
  position: relative;
}
.tile {
  position: relative;
  margin: 0;
  background: #000;
  border-radius: 12px;
  overflow: hidden;
  min-height: 0;
}
.tile video,
.tile canvas {
  width: 100%;
  height: 100%;
  object-fit: cover;
  background: #000;
  display: block;
}
.tile figcaption {
  position: absolute;
  bottom: 8px;
  left: 10px;
  background: rgba(0,0,0,0.55);
  padding: 4px 10px;
  border-radius: 999px;
  font-size: 0.85rem;
}

.tile-menu-btn {
  position: absolute;
  top: 8px;
  right: 8px;
  width: 36px;
  height: 36px;
  border-radius: 999px;
  border: none;
  background: rgba(0, 0, 0, 0.55);
  color: #eee;
  font-size: 1rem;
  cursor: pointer;
  z-index: 3;
}
.tile-menu-btn:hover { background: rgba(0, 0, 0, 0.75); }

.tile-menu {
  position: absolute;
  top: 52px;
  right: 8px;
  left: 8px;
  max-width: 280px;
  margin-left: auto;
  background: #1d1d1d;
  border: 1px solid #333;
  border-radius: 10px;
  padding: 10px 12px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  z-index: 4;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.4);
}
.tile-menu[hidden] { display: none; }
.tile-menu label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 0.85rem;
  color: #bbb;
}
.tile-menu select {
  background: #0f0f0f;
  color: #eee;
  border: 1px solid #333;
  border-radius: 6px;
  padding: 8px;
  min-height: 40px;
  font: inherit;
  max-width: 100%;
}

.chat {
  grid-area: chat;
  display: flex;
  flex-direction: column;
  min-height: 0;
  background: #1d1d1d;
  border-left: 1px solid #2a2a2a;
}
.chat-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 14px;
  font-weight: 600;
  border-bottom: 1px solid #2a2a2a;
}
.chat-log {
  list-style: none;
  margin: 0;
  padding: 10px 12px;
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.chat-log li {
  padding: 8px 10px;
  border-radius: 10px;
  max-width: 85%;
  word-wrap: break-word;
}
.chat-log li.you  { background: #2e86ab; align-self: flex-end; }
.chat-log li.them { background: #3a3a3a; align-self: flex-start; }
.chat-log li time { display: block; font-size: 0.72rem; opacity: 0.7; margin-top: 2px; }
.chat-form {
  display: flex;
  gap: 8px;
  padding: 10px;
  border-top: 1px solid #2a2a2a;
}
.chat-form input {
  flex: 1;
  min-height: 44px;
  padding: 10px 14px;
  border-radius: 999px;
  border: 1px solid #333;
  background: #0f0f0f;
  color: #eee;
  font: inherit;
}
.chat-form button {
  min-height: 44px;
  padding: 0 18px;
  font-size: 0.95rem;
}

.controls {
  grid-area: controls;
  display: flex;
  justify-content: center;
  gap: 12px;
  padding: 10px 8px env(safe-area-inset-bottom) 8px;
  background: #181818;
  border-top: 1px solid #242424;
  flex-wrap: wrap;
}
.ctrl {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-width: 86px;
  min-height: var(--tap);
  padding: 8px 14px;
  border-radius: 14px;
  background: #2a2a2a;
  color: #eee;
  border: none;
  cursor: pointer;
  font-size: 0.82rem;
  position: relative;
}
.ctrl:hover { background: #333; }
.ctrl[aria-pressed="false"] { background: #4d2323; }
.ctrl.danger { background: var(--danger); }
.ctrl-icon { font-size: 1.4rem; line-height: 1; }
.ctrl-label { margin-top: 4px; }
.badge {
  position: absolute;
  top: 6px;
  right: 6px;
  background: #f18f01;
  color: #111;
  border-radius: 999px;
  font-size: 0.7rem;
  padding: 1px 6px;
  font-weight: 700;
}

/* Narrow layout: stack tiles → controls → chat top-to-bottom, with the chat
   row collapsing to zero when closed. Chat is a real bottom section now,
   not a slide-up overlay, so the controls always stay visible between the
   tiles and the chat. */
@media (max-width: 900px) {
  body.room {
    grid-template-columns: 1fr;
    grid-template-rows: 1fr auto auto 0 auto;
    grid-template-areas:
      "tiles"
      "compat"
      "controls"
      "chat"
      "footer";
  }
  body.room:has(.chat.open) {
    grid-template-rows: 1fr auto auto 45svh auto;
  }
  .chat {
    overflow: hidden;
    border-left: none;
    border-top: 1px solid #2a2a2a;
  }
  .tiles {
    grid-template-columns: 1fr;
    grid-template-rows: 1fr;
  }
  /* On phone screen real estate is tight: before anyone joins, show only
     self; once peered, show only them. The self-preview is a luxury we
     don't have room for on a 5-inch display. */
  body.room:not(.peered) .tile.peer { display: none; }
  body.room.peered .tile.self {
    /* Don't `display: none` — Safari can pause a hidden <video> element
       and that would freeze the outgoing JPEG stream. Keep it rendered at
       a sensible size but tucked off-screen. */
    position: absolute;
    top: -9999px;
    left: -9999px;
    width: 160px;
    height: 120px;
    pointer-events: none;
  }
}

/* Phone-sized: force all four controls onto one row by letting them share
   the available width. Without this the leave button wraps and the chat
   form hides behind it on iPhone portrait. */
@media (max-width: 600px) {
  .controls {
    flex-wrap: nowrap;
    gap: 6px;
    padding: 6px 8px;
    padding-bottom: calc(8px + env(safe-area-inset-bottom));
  }
  .ctrl {
    flex: 1 1 0;
    min-width: 0;
    padding: 6px 4px;
    border-radius: 12px;
  }
  .ctrl-icon { font-size: 1.25rem; }
  .ctrl-label { font-size: 0.7rem; margin-top: 2px; }
}
