:root {
  --bg: #0f172a;
  --panel: #111c33;
  --panel-2: #1a2742;
  --line: #24324f;
  --text: #e2e8f0;
  --muted: #94a3b8;
  --accent: #38bdf8;
  --accent-ink: #04202e;
  --danger: #ef4444;
  --ok: #22c55e;
  --radius: 12px;
  color-scheme: dark;
}
@media (prefers-color-scheme: light) {
  :root {
    --bg: #f1f5f9; --panel: #ffffff; --panel-2: #eef2f7; --line: #dbe2ec;
    --text: #0f172a; --muted: #5b6b82; --accent: #0284c7; --accent-ink: #ffffff;
    color-scheme: light;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg); color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}
[hidden] { display: none !important; }
svg { width: 18px; height: 18px; fill: currentColor; flex: none; }

/* Buttons */
.btn {
  display: inline-flex; align-items: center; gap: 6px;
  border: 1px solid var(--line); background: var(--panel-2); color: var(--text);
  padding: 8px 12px; border-radius: 10px; font: inherit; cursor: pointer;
  transition: background .15s, border-color .15s;
}
.btn:hover { border-color: var(--accent); }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: transparent; font-weight: 600; }
.btn.danger { background: var(--danger); color: #fff; border-color: transparent; }
.btn.ghost { background: transparent; }
.btn.small { padding: 4px 10px; font-size: 13px; }
.btn.off { background: #7f1d1d; color: #fecaca; border-color: transparent; }
.btn.on { background: var(--accent); color: var(--accent-ink); border-color: transparent; }

/* Join */
.join { min-height: 100%; display: flex; align-items: center; justify-content: center; padding: 24px 16px; }
.card {
  width: 100%; max-width: 380px; background: var(--panel); border: 1px solid var(--line);
  border-radius: 16px; padding: 28px; display: flex; flex-direction: column; gap: 14px;
}
.card h1 { margin: 0; font-size: 30px; letter-spacing: -.02em; }
.card h1 span { color: var(--accent); margin-left: 4px; font-weight: 500; }
.sub { margin: 0 0 6px; color: var(--muted); }
label { display: flex; flex-direction: column; gap: 6px; font-size: 13px; color: var(--muted); }
input, textarea {
  font: inherit; color: var(--text); background: var(--bg); border: 1px solid var(--line);
  border-radius: 10px; padding: 10px 12px; outline: none; width: 100%;
}
input:focus, textarea:focus { border-color: var(--accent); }
.roomfield { display: flex; align-items: center; background: var(--bg); border: 1px solid var(--line); border-radius: 10px; }
.roomfield span { padding-left: 12px; color: var(--muted); }
.roomfield input { border: 0; background: transparent; }
.error { color: var(--danger); margin: 0; font-size: 14px; }

/* App shell */
.app { height: 100%; display: flex; flex-direction: column; }
.top {
  display: flex; align-items: center; justify-content: space-between; gap: 10px; flex-wrap: wrap;
  padding: 10px 16px; border-bottom: 1px solid var(--line); background: var(--panel);
}
.title { display: flex; align-items: center; gap: 10px; min-width: 0; }
.title strong { font-size: 17px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conn { color: #fbbf24; font-size: 13px; }
.actions, .group { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }

.banner {
  display: flex; align-items: center; gap: 10px; flex-wrap: wrap;
  padding: 8px 16px; background: #14352a;
  background: color-mix(in srgb, var(--ok) 18%, var(--panel)); border-bottom: 1px solid var(--line);
}
.banner span { flex: 1; min-width: 150px; }

.body { flex: 1; min-height: 0; display: flex; }
.chat { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.people { width: 220px; border-left: 1px solid var(--line); background: var(--panel); padding: 12px 16px; overflow-y: auto; }
.people h2 { font-size: 12px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 4px 0 10px; }
.people ul { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.people li { display: flex; align-items: center; gap: 8px; }
.people li .tag { margin-left: auto; color: var(--ok); }
.dot { width: 26px; height: 26px; border-radius: 50%; display: grid; place-items: center; font-size: 12px; font-weight: 700; color: #fff; flex: none; }

/* Messages */
.messages { flex: 1; overflow-y: auto; margin: 0; padding: 16px; list-style: none; display: flex; flex-direction: column; gap: 2px; }
.msg { display: flex; gap: 10px; padding: 3px 0; }
.msg.first { margin-top: 10px; }
.msg .dot { margin-top: 2px; }
.msg:not(.first) .dot { visibility: hidden; height: 0; }
.msg .meta { font-size: 13px; }
.msg .meta b { margin-right: 6px; }
.msg .meta time { color: var(--muted); font-size: 12px; }
.msg .text { white-space: pre-wrap; overflow-wrap: anywhere; }
.msg .text a { color: var(--accent); }
.sys { color: var(--muted); font-size: 13px; text-align: center; margin: 8px 0; }
.typing { height: 20px; padding: 0 16px; color: var(--muted); font-size: 13px; }
.composer { display: flex; gap: 8px; padding: 10px 16px 14px; }
.composer textarea { resize: none; max-height: 140px; }

/* Call stage */
.stage {
  flex: 1.3; min-width: 0; padding: 12px; gap: 10px; overflow-y: auto;
  display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); align-content: center;
  background: #020617; border-right: 1px solid var(--line);
}
.tile {
  position: relative; aspect-ratio: 16 / 9; background: #0b1324; border-radius: var(--radius);
  overflow: hidden; border: 2px solid transparent; transition: border-color .12s;
}
.tile.speaking { border-color: var(--ok); }
.tile video { width: 100%; height: 100%; object-fit: cover; background: #000; }
.tile.screen video { object-fit: contain; }
.tile.mine video { transform: scaleX(-1); }
.tile.mine.screen video { transform: none; }
.tile .avatar { position: absolute; inset: 0; display: grid; place-items: center; }
.tile .avatar .dot { width: 72px; height: 72px; font-size: 28px; }
.tile.has-video .avatar { display: none; }
.tile .label {
  position: absolute; left: 8px; bottom: 8px; font-size: 13px; color: #fff;
  background: rgba(0,0,0,.55); padding: 2px 8px; border-radius: 6px; display: flex; gap: 6px; align-items: center;
}
.tile .label .muted { color: #fca5a5; }
.tile .state { position: absolute; right: 8px; top: 8px; font-size: 12px; color: #fbbf24; background: rgba(0,0,0,.55); padding: 2px 8px; border-radius: 6px; }
.tile { cursor: pointer; }
.tile .tools {
  position: absolute; left: 8px; top: 8px; display: flex; gap: 4px;
  opacity: 0; transition: opacity .15s;
}
.tile:hover .tools, .tile:focus-within .tools { opacity: 1; }
@media (hover: none) { .tile .tools { opacity: 1; } }
.tile .tools button {
  width: 32px; height: 32px; display: grid; place-items: center; padding: 0;
  border: 0; border-radius: 8px; background: rgba(0,0,0,.6); color: #fff; cursor: pointer;
}
.tile .tools button:hover { background: rgba(0,0,0,.85); }
.tile:not(.has-video) .tools .pip { display: none; }

/* One tile enlarged, the rest in a strip below */
.body.focused .stage { flex: 3; }
.stage.has-focus { grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); align-content: start; }
.stage.has-focus .tile.focus { grid-column: 1 / -1; order: -1; }
.stage.has-focus .tile.focus video { object-fit: contain; }

/* Fullscreen tile */
.tile:fullscreen { border-radius: 0; border: 0; aspect-ratio: auto; width: 100%; height: 100%; background: #000; }
.tile:fullscreen video { object-fit: contain; }
.tile:fullscreen .tools { opacity: 1; }

.toast {
  position: fixed; left: 50%; bottom: 90px; transform: translateX(-50%); max-width: calc(100% - 32px);
  background: var(--panel-2); border: 1px solid var(--line); padding: 10px 16px; border-radius: 10px; z-index: 10;
  box-shadow: 0 10px 30px rgba(0,0,0,.35);
}

/* Narrow screens: stage on top, people as overlay */
@media (max-width: 860px) {
  .body { flex-direction: column; position: relative; }
  .stage { flex: none; max-height: 48vh; border-right: 0; border-bottom: 1px solid var(--line);
           grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); }
  .stage.has-focus { max-height: 70vh; grid-template-columns: repeat(auto-fill, minmax(100px, 1fr)); }
  .people { position: absolute; right: 0; top: 0; bottom: 0; width: min(260px, 80%); z-index: 5; box-shadow: -10px 0 30px rgba(0,0,0,.35); }
  .people:not(.open) { display: none; }
  .btn span { display: none; }
  .btn.small span, .banner .btn, #peopleCount { display: inline; }
  .top { padding: 8px 12px; }
}
@media (min-width: 861px) {
  .people.closed { display: none; }
}

/* Audio settings panel */
.app { position: relative; }
.panel {
  position: absolute; right: 16px; top: 58px; z-index: 20; width: min(340px, calc(100% - 32px));
  background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 16px;
  display: flex; flex-direction: column; gap: 12px; box-shadow: 0 16px 40px rgba(0,0,0,.4);
}
.panel h3 { margin: 0; font-size: 15px; }
.panel select { font: inherit; color: var(--text); background: var(--bg); border: 1px solid var(--line); border-radius: 10px; padding: 8px 10px; }
.panel .check { flex-direction: row; align-items: center; gap: 8px; color: var(--text); font-size: 14px; }
.panel .check input { width: auto; }
.panel small { color: var(--muted); font-size: 12px; }
.panel .note { margin: 0; color: #fbbf24; font-size: 13px; }
.gatebox { display: flex; flex-direction: column; gap: 8px; }
.gatebox.disabled { opacity: .45; pointer-events: none; }
.gatebox input[type=range] { padding: 0; border: 0; background: transparent; accent-color: var(--accent); }
.meter { position: relative; height: 10px; border-radius: 5px; background: var(--bg); border: 1px solid var(--line); overflow: hidden; }
.meter .level { height: 100%; width: 0; background: var(--muted); transition: width .06s linear; }
.meter .level.open { background: var(--ok); }
.meter .mark { position: absolute; top: -2px; bottom: -2px; width: 2px; background: #fff; }
