:root {
  --bg: #0f1218;
  --panel: #181d27;
  --panel-2: #212836;
  --text: #e8ecf4;
  --muted: #8d97ab;
  --accent: #3b82f6;
  --danger: #ef4444;
  --ok: #22c55e;
  --radius: 14px;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
.offscreen { position: fixed; left: -9999px; top: 0; width: 1px; height: 1px; opacity: 0; }
.hidden { display: none; }

html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 16px;
  line-height: 1.4;
}

h1 { font-size: 1.6rem; margin: 0 0 6px; }
h2 { font-size: 1.1rem; margin: 18px 0 10px; }
h3 { font-size: 0.95rem; margin: 18px 0 8px; }
.muted { color: var(--muted); }
.small { font-size: 0.85rem; }
.error { color: #fca5a5; background: rgba(239, 68, 68, 0.12); padding: 10px 12px; border-radius: 10px; margin: 12px 0; }
.badge { display: inline-block; background: rgba(34, 197, 94, 0.15); color: #86efac; padding: 4px 10px; border-radius: 999px; font-size: 0.85rem; }
.link { display: block; text-align: center; margin-top: 14px; color: var(--muted); text-decoration: none; }
.link:hover { color: var(--text); }
.code-pill { font-family: ui-monospace, Menlo, Consolas, monospace; background: var(--panel-2); padding: 2px 10px; border-radius: 999px; font-size: 0.9em; letter-spacing: 0.05em; }

.card {
  width: min(480px, calc(100% - 32px));
  margin: 4vh auto;
  background: var(--panel);
  border-radius: 20px;
  padding: 24px;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.35);
}

label { display: block; margin: 14px 0 6px; font-size: 0.9rem; color: var(--muted); }
label.check { display: flex; align-items: center; gap: 8px; color: var(--text); }
input[type="text"], input[type="password"] {
  width: 100%;
  padding: 11px 12px;
  border-radius: 10px;
  border: 1px solid #2d3648;
  background: var(--bg);
  color: var(--text);
  font-size: 1rem;
}
input:focus-visible, button:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

button {
  font: inherit;
  cursor: pointer;
  border: 0;
  border-radius: 10px;
  padding: 11px 16px;
  background: var(--panel-2);
  color: var(--text);
}
button:disabled { opacity: 0.5; cursor: not-allowed; }
button.primary { width: 100%; background: var(--accent); color: #fff; margin-top: 14px; font-weight: 600; }
button.danger { background: rgba(239, 68, 68, 0.18); color: #fecaca; }
button.small-btn { padding: 8px 12px; font-size: 0.9rem; white-space: nowrap; }
.row { display: flex; gap: 8px; align-items: center; }
.row.center { justify-content: center; margin-top: 10px; }
.divider { text-align: center; color: var(--muted); margin: 18px 0 0; }

.home section { margin-top: 18px; }

/* pre-join */
.preview { position: relative; aspect-ratio: 4 / 3; background: #000; border-radius: var(--radius); overflow: hidden; margin-top: 14px; }
.preview video { width: 100%; height: 100%; object-fit: cover; transform: scaleX(-1); }
.preview-off { position: absolute; inset: 0; display: grid; place-items: center; background: var(--panel-2); color: var(--muted); }

/* call layout */
body.in-call { overflow: hidden; }
#call { display: flex; flex-direction: column; height: 100dvh; }
.topbar { display: flex; justify-content: space-between; align-items: center; padding: 10px 14px; background: var(--panel); }
.topbar .title { display: flex; gap: 10px; align-items: center; }
.status { color: #fcd34d; font-size: 0.9rem; }
.stage { flex: 1; display: flex; min-height: 0; }

.grid { flex: 1; display: grid; gap: 8px; padding: 8px; min-width: 0; min-height: 0; overflow: hidden; grid-auto-rows: minmax(0, 1fr); grid-template-columns: 1fr; }
.grid[data-count="2"] { grid-template-columns: repeat(2, 1fr); }
.grid[data-count="3"], .grid[data-count="4"] { grid-template-columns: repeat(2, 1fr); }
.grid[data-count="5"], .grid[data-count="6"], .grid[data-count="7"], .grid[data-count="8"], .grid[data-count="9"] { grid-template-columns: repeat(3, 1fr); }

.tile { position: relative; background: var(--panel-2); border-radius: var(--radius); overflow: hidden; min-height: 0; border: 3px solid transparent; transition: border-color 0.15s; }
.tile.speaking { border-color: var(--ok); }
.tile video { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; display: none; }
.tile.has-video video { display: block; }
.tile.mirror video { transform: scaleX(-1); }
.tile.screen video { object-fit: contain; background: #000; }
.tile .avatar { position: absolute; inset: 0; display: grid; place-items: center; font-size: clamp(1.6rem, 6vw, 3.4rem); font-weight: 600; color: var(--muted); }
.tile.has-video .avatar { display: none; }
.tile .label { position: absolute; left: 8px; bottom: 8px; display: flex; gap: 6px; align-items: center; background: rgba(0, 0, 0, 0.55); padding: 3px 10px; border-radius: 999px; font-size: 0.85rem; max-width: calc(100% - 16px); }
.tile .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tile .mic { width: 8px; height: 8px; border-radius: 50%; background: var(--ok); flex: none; }
.tile .mic.off { background: var(--danger); }
.tile.host .name::after { content: " (host)"; color: var(--muted); }

.drawer { width: 340px; max-width: 100%; background: var(--panel); display: flex; flex-direction: column; min-height: 0; }
.tabs { display: flex; gap: 4px; padding: 8px; border-bottom: 1px solid #2d3648; }
.tab { padding: 8px 10px; background: transparent; color: var(--muted); font-size: 0.9rem; }
.tab.active { background: var(--panel-2); color: var(--text); }
.tab.close { margin-left: auto; }
.panel { flex: 1; overflow-y: auto; padding: 12px; min-height: 0; }
#panel-chat { display: flex; flex-direction: column; padding: 0; }
.messages { flex: 1; list-style: none; margin: 0; padding: 12px; overflow-y: auto; }
.msg { display: flex; flex-direction: column; margin-bottom: 10px; max-width: 90%; }
.msg strong { font-size: 0.8rem; color: var(--muted); }
.msg span { background: var(--panel-2); padding: 8px 10px; border-radius: 10px; overflow-wrap: anywhere; }
.msg.mine { margin-left: auto; align-items: flex-end; }
.msg.mine span { background: var(--accent); }
.chat-form { display: flex; gap: 8px; padding: 10px; border-top: 1px solid #2d3648; }
.people { list-style: none; margin: 0; padding: 0; }
.person { display: flex; gap: 8px; align-items: center; padding: 8px 0; border-bottom: 1px solid #232b3a; }
.person .who { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.person .tags { color: var(--muted); font-size: 0.8rem; }
.device-result { background: var(--bg); border-radius: 10px; padding: 4px 10px 12px; margin: 10px 0; }

.controls { display: flex; flex-wrap: wrap; justify-content: center; gap: 8px; padding: 10px 10px calc(10px + env(safe-area-inset-bottom)); background: var(--panel); }
.round { border-radius: 999px; }
.round[aria-pressed="false"] { background: rgba(239, 68, 68, 0.2); color: #fecaca; }
#screenBtn[aria-pressed="false"] { background: var(--panel-2); color: var(--text); }
#screenBtn[aria-pressed="true"] { background: var(--accent); color: #fff; }
.round.leave { background: var(--danger); color: #fff; }
.unread { background: var(--danger); border-radius: 999px; padding: 0 7px; font-size: 0.75rem; margin-left: 4px; }
.floating { position: fixed; top: 64px; left: 50%; transform: translateX(-50%); background: var(--accent); color: #fff; z-index: 20; }
.overlay { position: fixed; inset: 0; background: rgba(8, 10, 14, 0.85); display: grid; place-items: center; z-index: 30; }

#toast { position: fixed; bottom: 90px; left: 50%; transform: translateX(-50%) translateY(20px); background: #000; color: #fff; padding: 10px 16px; border-radius: 10px; opacity: 0; pointer-events: none; transition: all 0.2s; z-index: 40; max-width: 90vw; }
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* device page */
.device-preview { aspect-ratio: 16 / 9; background: #000; border-radius: var(--radius); overflow: hidden; margin-top: 14px; }
.device-preview video { width: 100%; height: 100%; object-fit: cover; }
.state { display: flex; align-items: center; gap: 8px; margin-top: 14px; font-weight: 600; }
.dot { width: 10px; height: 10px; border-radius: 50%; background: var(--muted); }
.dot.live { background: var(--ok); }
.dot.wait { background: #fbbf24; }
.dot.bad { background: var(--danger); }
select { width: 100%; padding: 11px 12px; border-radius: 10px; border: 1px solid #2d3648; background: var(--bg); color: var(--text); font-size: 1rem; }

@media (max-width: 760px) {
  .stage { flex-direction: column; }
  .drawer { width: 100%; max-height: 50%; }
  .grid[data-count="2"], .grid[data-count="3"], .grid[data-count="4"] { grid-template-columns: 1fr 1fr; }
  .grid[data-count="5"], .grid[data-count="6"], .grid[data-count="7"], .grid[data-count="8"], .grid[data-count="9"] { grid-template-columns: 1fr 1fr; }
  .topbar .small-btn { padding: 6px 10px; }
  .controls button { padding: 10px 12px; font-size: 0.85rem; }
}
