:root {
  --bg: #17202e;
  --surface: #212c3d;
  --surface-2: #2c394d;
  --text: #e8ecf2;
  --muted: #9aa6b8;
  --primary: #3e8ef7;
  --marigold: #f5b83d;
  --danger: #e5484d;
  --radius: 14px;
  color-scheme: dark;
  box-sizing: border-box;
  padding-top: env(safe-area-inset-top, 0px);
  padding-bottom: env(safe-area-inset-bottom, 0px);
}
*, *::before, *::after { box-sizing: inherit; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 "Figtree", system-ui, -apple-system, "Segoe UI", sans-serif;
}
[hidden] { display: none !important; }
button, input { font: inherit; color: inherit; }
button { cursor: pointer; }
:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; }

/* Buttons */
.btn { border: 0; border-radius: 10px; padding: 10px 16px; font-weight: 600; }
.btn.primary { background: var(--primary); color: #fff; }
.btn.primary:disabled { opacity: .6; cursor: wait; }
.btn.ghost { background: var(--surface-2); }
.btn.small { padding: 6px 12px; font-size: 13px; }
.btn.wide { width: 100%; }
.icon-btn { background: none; border: 0; padding: 6px 10px; color: var(--muted); margin-left: auto; }

/* Join screen */
.screen.join { min-height: 100%; display: grid; place-items: center; padding: 24px; }
.join-card { width: 100%; max-width: 420px; background: var(--surface); border-radius: 20px; padding: 32px 28px; }
.join-card h1 { margin: 0 0 6px; font-size: 28px; letter-spacing: -0.01em; }
.muted { color: var(--muted); margin: 0 0 24px; }
form label { display: block; font-size: 13px; color: var(--muted); margin-bottom: 16px; }
form input {
  display: block; width: 100%; margin-top: 6px; padding: 11px 12px;
  background: var(--bg); border: 1px solid var(--surface-2); border-radius: 10px; font-size: 15px;
}
.row { display: flex; gap: 8px; }
.row input { flex: 1; }
.row .btn { margin-top: 6px; white-space: nowrap; }
details { margin-bottom: 16px; }
summary { cursor: pointer; color: var(--muted); font-size: 13px; margin-bottom: 12px; }
.error { color: #ff8a8d; min-height: 1.2em; font-size: 14px; margin: 0 0 12px; }

/* Meeting layout */
.screen.meeting {
  height: 100%; display: grid;
  grid-template-rows: auto 1fr auto;
  grid-template-columns: 1fr auto;
  grid-template-areas: "top top" "stage panel" "ctrl ctrl";
}
.topbar { grid-area: top; display: flex; justify-content: space-between; align-items: center; padding: 10px 16px; gap: 12px; }
.room-info { display: flex; align-items: center; gap: 10px; min-width: 0; }
.room-meta { color: var(--muted); font-size: 14px; white-space: nowrap; }

.stage-wrap { grid-area: stage; display: flex; flex-direction: column; gap: 10px; padding: 0 12px; min-height: 0; overflow: hidden; }
.stage { flex: 1 1 auto; min-height: 0; background: #000; border-radius: var(--radius); position: relative; }
.stage video { width: 100%; height: 100%; object-fit: contain; }
.stage .label { position: absolute; left: 10px; bottom: 10px; }

.grid {
  flex: 1 1 auto; min-height: 0; overflow-y: auto;
  display: grid; gap: 10px; align-content: center;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
}
.stage:not([hidden]) + .grid {
  flex: 0 0 auto; display: flex; overflow-x: auto; overflow-y: hidden; align-content: normal;
}
.stage:not([hidden]) + .grid .tile { flex: 0 0 180px; }

/* Participant tile */
.tile {
  position: relative; aspect-ratio: 16 / 9; background: var(--surface);
  border-radius: var(--radius); overflow: hidden;
  box-shadow: inset 0 0 0 0 var(--marigold); transition: box-shadow .15s;
}
.tile.speaking { box-shadow: inset 0 0 0 3px var(--marigold); }
.tile video { width: 100%; height: 100%; object-fit: cover; display: block; }
.tile.local video { transform: scaleX(-1); }
.avatar {
  position: absolute; inset: 0; margin: auto; width: 64px; height: 64px; border-radius: 50%;
  display: grid; place-items: center; font-size: 26px; font-weight: 700; color: #fff;
}
.label {
  position: absolute; left: 8px; bottom: 8px; right: 8px;
  display: flex; align-items: center; gap: 6px;
  font-size: 13px; font-weight: 500;
}
.label .name { background: rgba(0,0,0,.55); padding: 2px 8px; border-radius: 6px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.label .mic-off { background: var(--danger); border-radius: 6px; padding: 1px 5px; font-size: 11px; }
.hand-flag { position: absolute; top: 8px; right: 8px; background: var(--marigold); color: #1a1a1a; border-radius: 8px; padding: 2px 8px; font-size: 13px; font-weight: 600; }
.host-tag { color: var(--marigold); font-size: 11px; font-weight: 600; }

/* Side panel */
.panel { grid-area: panel; width: 340px; background: var(--surface); border-radius: var(--radius); margin-right: 12px; display: flex; flex-direction: column; min-height: 0; }
.panel-tabs { display: flex; gap: 4px; padding: 10px; border-bottom: 1px solid var(--surface-2); }
.tab { background: none; border: 0; padding: 6px 12px; border-radius: 8px; color: var(--muted); font-weight: 600; }
.tab.active { background: var(--surface-2); color: var(--text); }
.tab-body { flex: 1; display: flex; flex-direction: column; min-height: 0; padding: 10px; gap: 10px; }
.messages, .people { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; }
.messages li { margin-bottom: 12px; overflow-wrap: anywhere; }
.messages .who { font-size: 12px; color: var(--muted); display: block; }
.messages .empty, .people .empty { color: var(--muted); font-size: 14px; }
.chat-form { display: flex; gap: 8px; }
.chat-form input { flex: 1; padding: 9px 12px; background: var(--bg); border: 1px solid var(--surface-2); border-radius: 10px; min-width: 0; }
.people li { display: flex; align-items: center; gap: 8px; padding: 8px 4px; border-bottom: 1px solid var(--surface-2); }
.people .pname { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.people .btn { padding: 4px 8px; font-size: 12px; }

/* Controls */
.controls { grid-area: ctrl; display: flex; justify-content: center; gap: 8px; padding: 12px; flex-wrap: wrap; }
.ctrl {
  position: relative; display: flex; flex-direction: column; align-items: center; gap: 2px;
  min-width: 64px; padding: 8px 10px; border: 0; border-radius: 12px; background: var(--surface-2); font-size: 20px;
}
.ctrl span { font-size: 11px; color: var(--muted); }
.ctrl.off { background: #4a2a31; }
.ctrl.active { background: var(--primary); }
.ctrl.active span { color: #fff; }
.ctrl.hand-on { background: var(--marigold); }
.ctrl.hand-on span { color: #1a1a1a; }
.ctrl.leave { background: var(--danger); }
.ctrl.leave span { color: #fff; }
.badge { position: absolute; top: 6px; right: 10px; width: 9px; height: 9px; border-radius: 50%; background: var(--marigold); }

.audio-unlock { position: fixed; top: 60px; left: 50%; transform: translateX(-50%); background: var(--marigold); color: #1a1a1a; border: 0; border-radius: 10px; padding: 10px 16px; font-weight: 600; z-index: 20; }
.toast { position: fixed; bottom: calc(100px + env(safe-area-inset-bottom, 0px)); left: 50%; transform: translateX(-50%); background: var(--surface-2); padding: 10px 16px; border-radius: 10px; z-index: 30; max-width: 90vw; }

/* Mobile */
@media (max-width: 760px) {
  .screen.meeting { grid-template-columns: 1fr; grid-template-areas: "top" "stage" "ctrl"; }
  .panel { position: fixed; inset: 0; width: auto; margin: 0; border-radius: 0; z-index: 10; padding-top: env(safe-area-inset-top, 0px); }
  .grid { grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); }
  .ctrl { min-width: 52px; padding: 6px 8px; font-size: 18px; }
  .ctrl span { display: none; }
  #copy-link { display: none; }
}
@media (prefers-reduced-motion: reduce) { .tile { transition: none; } }
