:root {
  --bg: #0f1115;
  --panel: #1a1d23;
  --panel-2: #23272f;
  --tile: #1d2027;
  --line: #2e333c;
  --text: #e9ebef;
  --muted: #9aa1ad;
  --accent: #ff8a3d;
  --accent-ink: #1a0d03;
  --danger: #e5484d;
  --ok: #3ecf8e;
  --radius: 14px;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}
[hidden] { display: none !important; }
a { color: var(--accent); }
code { background: var(--panel-2); padding: .1em .4em; border-radius: 6px; font-size: .92em; }
h1, h2, h3 { line-height: 1.2; margin: 0 0 .5em; }
.muted { color: var(--muted); }
.small { font-size: .9em; }

.sprite { position: absolute; width: 0; height: 0; overflow: hidden; }
.i { width: 22px; height: 22px; fill: currentColor; flex: none; }

/* ---------- buttons & fields ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 44px; padding: 0 18px; border-radius: 999px;
  border: 1px solid var(--line); background: var(--panel-2); color: var(--text);
  font: inherit; font-weight: 600; cursor: pointer; text-decoration: none;
  transition: background .15s, transform .05s;
}
.btn:hover { background: #2c313a; }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .5; cursor: default; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { background: #ff9b57; }
.btn.ghost { background: transparent; }
.btn.wide { width: 100%; }

.field { display: grid; gap: 6px; font-weight: 600; font-size: .9em; }
input[type=text], input[type=password] {
  width: 100%; min-height: 44px; padding: 0 14px;
  border-radius: 10px; border: 1px solid var(--line);
  background: var(--bg); color: var(--text); font: inherit; font-weight: 400;
}
input:focus { outline: 2px solid var(--accent); outline-offset: 1px; border-color: transparent; }
button:focus-visible, a:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.msg { margin: 0; color: var(--muted); font-size: .92em; min-height: 1.2em; }
.msg.error { color: #ff8b8e; }

.icon-btn {
  display: inline-grid; place-items: center; width: 38px; height: 38px;
  border: 0; border-radius: 50%; background: transparent; color: var(--text); cursor: pointer;
}
.icon-btn:hover { background: rgba(255,255,255,.08); }

/* ---------- brand ---------- */
.brand { display: inline-flex; align-items: center; gap: 10px; font-size: 18px; letter-spacing: .2px; color: var(--text); }
.brand b { color: var(--accent); font-weight: 700; }
.brand-link { text-decoration: none; }

/* ---------- gate / card pages ---------- */
body.gate { display: grid; place-items: center; min-height: 100vh; padding: 16px; }
.card {
  width: 100%; max-width: 400px; background: var(--panel);
  border: 1px solid var(--line); border-radius: 18px; padding: 28px;
}
.card .brand { margin-bottom: 22px; }
.card h1 { font-size: 22px; }
.card.center { text-align: center; }
.stack { display: grid; gap: 14px; margin-top: 18px; }
.row { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; margin-top: 18px; }

/* ---------- app screens ---------- */
body.app { height: 100vh; height: 100dvh; overflow: hidden; }
.screen { height: 100%; }
#left { display: grid; place-items: center; padding: 16px; }

.lobby-top { padding: 18px 24px; }
.lobby-body {
  max-width: 640px; margin: 0 auto; padding: 8vh 16px 32px;
}
.lobby-body h1 { font-size: clamp(28px, 5vw, 42px); letter-spacing: -.5px; }
.lobby-body > p { font-size: 17px; margin: 0 0 28px; }
.lobby-actions { display: flex; flex-wrap: wrap; gap: 12px; align-items: center; }
.join-form { display: flex; gap: 8px; flex: 1 1 260px; }
.join-form input { flex: 1; min-width: 0; }

#lobby, #prejoin { overflow-y: auto; }

/* ---------- pre-join ---------- */
.pj-wrap {
  display: grid; grid-template-columns: minmax(0, 1.6fr) minmax(260px, 1fr);
  gap: 32px; align-items: center;
  max-width: 1080px; margin: 0 auto; padding: 4vh 24px 32px;
}
.pj-preview .tile { width: 100%; aspect-ratio: 16 / 9; height: auto; }
.pj-side { display: grid; gap: 14px; }
.pj-side h2 { font-size: 26px; margin: 0; }
.pj-side p { margin: 0; }
.tile-controls {
  position: absolute; left: 0; right: 0; bottom: 14px;
  display: flex; justify-content: center; gap: 14px; z-index: 3;
}
@media (max-width: 760px) {
  .pj-wrap { grid-template-columns: 1fr; padding: 8px 16px 24px; gap: 20px; }
  .lobby-top { padding: 14px 16px; }
}

/* ---------- round control buttons ---------- */
.round {
  position: relative; display: inline-grid; place-items: center;
  width: 50px; height: 50px; border-radius: 50%;
  border: 0; background: #3a3f48; color: #fff; cursor: pointer;
  transition: background .15s;
}
.round:hover { background: #474d58; }
.round.off { background: var(--danger); }
.round.off:hover { background: #f05f63; }
.round.active { background: var(--accent); color: var(--accent-ink); }
.round.danger { background: var(--danger); }
.round.danger:hover { background: #f05f63; }
.round.wide-round { width: 68px; border-radius: 999px; }
.tile-controls .round { background: rgba(30,32,38,.75); backdrop-filter: blur(6px); }
.tile-controls .round.off { background: var(--danger); }

.badge {
  position: absolute; top: -2px; right: -2px; min-width: 20px; height: 20px; padding: 0 5px;
  border-radius: 10px; background: var(--accent); color: var(--accent-ink);
  font-size: 12px; font-weight: 700; line-height: 20px;
}

/* ---------- call ---------- */
#call { display: flex; flex-direction: column; }
.stage { flex: 1; min-height: 0; display: flex; position: relative; }
.grid {
  flex: 1; min-width: 0;
  display: flex; flex-wrap: wrap; align-content: center; justify-content: center;
  gap: 10px; padding: 10px;
  --tw: 320px; --th: 180px;
}
.grid .tile { width: var(--tw); height: var(--th); }

.controls {
  flex: none; display: grid; grid-template-columns: 1fr auto 1fr; align-items: center;
  gap: 12px; padding: 10px 16px calc(12px + env(safe-area-inset-bottom));
}
.ctl-main { display: flex; gap: 12px; }
.ctl-side { display: flex; gap: 12px; justify-self: end; }
.ctl-info { display: flex; gap: 12px; align-items: baseline; min-width: 0; white-space: nowrap; overflow: hidden; }
.room-code { font-weight: 600; overflow: hidden; text-overflow: ellipsis; }
@media (max-width: 640px) {
  .controls { display: flex; justify-content: center; flex-wrap: wrap; gap: 8px; padding-top: 6px; }
  .ctl-info { display: none; }
  .controls .round { width: 42px; height: 42px; }
  .controls .round.wide-round { width: 54px; }
  .controls, .ctl-main, .ctl-side { gap: 6px; }
}

/* ---------- tiles ---------- */
.tile {
  position: relative; overflow: hidden; border-radius: var(--radius);
  background: var(--tile); isolation: isolate;
}
.tile video {
  position: absolute; inset: 0; width: 100%; height: 100%;
  object-fit: cover; background: #000;
}
.tile.mirror video { transform: scaleX(-1); }
.tile.screen video { object-fit: contain; }
.tile .avatar {
  position: absolute; inset: 0; display: none; place-items: center;
  background: var(--tile); z-index: 1;
}
.tile.no-video .avatar { display: grid; }
.tile .avatar span {
  --h: 24;
  display: grid; place-items: center;
  width: clamp(56px, 28%, 120px); aspect-ratio: 1; border-radius: 50%;
  background: var(--c, hsl(var(--h) 45% 38%)); color: #fff;
  font-size: clamp(22px, 5vw, 48px); font-weight: 600;
}
.tile::after {
  content: ""; position: absolute; inset: 0; border-radius: inherit; z-index: 4;
  box-shadow: inset 0 0 0 3px transparent; transition: box-shadow .15s; pointer-events: none;
}
.tile.speaking:not(.mic-muted)::after { box-shadow: inset 0 0 0 3px var(--c, var(--accent)); }

.tile .label {
  position: absolute; left: 10px; bottom: 10px; z-index: 3; max-width: calc(100% - 20px);
  display: inline-flex; align-items: center; gap: 6px;
  padding: 3px 10px 3px 8px; border-radius: 8px;
  background: rgba(0,0,0,.55); font-size: 13px; font-weight: 600;
}
.tile .label::before {
  content: ""; width: 9px; height: 9px; border-radius: 50%; flex: none;
  background: var(--c); display: none;
}
.tile.has-color .label::before { display: block; }
.tile .label .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tile .mic-ind { width: 16px; height: 16px; color: #ff8b8e; display: none; }
.tile.mic-muted .mic-ind { display: block; }

.tile .status {
  position: absolute; inset: 0; z-index: 2; display: none; place-items: center;
  background: rgba(15,17,21,.55); color: var(--muted); font-weight: 600;
}
.tile.connecting .status { display: grid; }

.tile .fs {
  position: absolute; top: 8px; right: 8px; z-index: 3;
  background: rgba(0,0,0,.5); opacity: 0; transition: opacity .15s;
}
.tile:hover .fs, .tile .fs:focus-visible { opacity: 1; }
@media (hover: none) { .tile .fs { opacity: .8; } }
#pj-tile .fs { display: none; }

.tile:fullscreen { border-radius: 0; width: 100%; height: 100%; }
.tile:fullscreen video { object-fit: contain; }

/* ---------- chat ---------- */
.chat {
  flex: none; width: 340px; margin: 10px 10px 10px 0;
  display: flex; flex-direction: column;
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
  overflow: hidden;
}
.chat header { display: flex; align-items: center; justify-content: space-between; padding: 10px 8px 0 16px; }
.chat h3 { margin: 0; font-size: 16px; }
.chat-note {
  margin: 8px 12px; padding: 8px 10px; border-radius: 8px;
  background: var(--panel-2); color: var(--muted); font-size: 12.5px;
}
.chat-log { flex: 1; overflow-y: auto; padding: 4px 16px 12px; display: flex; flex-direction: column; gap: 12px; }
.chat-head { font-size: 13px; }
.chat-head time { color: var(--muted); font-size: 12px; }
.chat-text { white-space: pre-wrap; overflow-wrap: anywhere; }
.chat-msg { border-left: 3px solid var(--c, var(--line)); padding-left: 10px; }
.chat-msg .chat-head b { color: var(--c, var(--text)); }
.chat-sys { color: var(--muted); font-size: 12.5px; font-style: italic; }
.chat-form { display: flex; gap: 6px; padding: 10px; border-top: 1px solid var(--line); }
.chat-form input { border-radius: 999px; }
.chat-form .icon-btn { width: 44px; height: 44px; color: var(--accent); }
@media (max-width: 760px) {
  .chat { position: absolute; inset: 0; width: auto; margin: 0; border-radius: 0; border: 0; z-index: 10; }
}

/* ---------- settings dialog ---------- */
.dialog {
  width: min(440px, calc(100% - 32px)); max-height: calc(100% - 32px); overflow-y: auto;
  padding: 0; border: 1px solid var(--line); border-radius: 18px;
  background: var(--panel); color: var(--text);
}
.dialog::backdrop { background: rgba(0,0,0,.6); }
.dialog form { display: grid; gap: 18px; padding: 18px 22px 22px; }
.dialog header { display: flex; align-items: center; justify-content: space-between; margin-right: -8px; }
.dialog h3 { margin: 0; font-size: 18px; }
.dialog .field label { font-weight: 600; }
.dialog p { margin: -8px 0 0; }
select {
  width: 100%; min-width: 0; min-height: 44px; padding: 0 12px;
  border-radius: 10px; border: 1px solid var(--line);
  background: var(--bg); color: var(--text); font: inherit; font-weight: 400;
}
select:focus { outline: 2px solid var(--accent); outline-offset: 1px; }
.inline { display: flex; gap: 8px; }
.inline select { flex: 1; }
.level { height: 6px; border-radius: 3px; background: var(--panel-2); overflow: hidden; }
.level > div { height: 100%; width: 0; background: var(--ok); transition: width .1s linear; }

/* ---------- banner & toasts ---------- */
.banner {
  position: fixed; top: 12px; left: 50%; transform: translateX(-50%); z-index: 50;
  max-width: calc(100% - 32px); padding: 10px 16px; border-radius: 10px;
  background: #3b2a12; color: #ffd8b0; border: 1px solid #6b4a1f; font-size: 14px;
}
.toasts {
  position: fixed; left: 50%; bottom: 96px; transform: translateX(-50%); z-index: 60;
  display: grid; gap: 8px; justify-items: center; width: max-content; max-width: calc(100% - 32px);
  pointer-events: none;
}
.toast {
  padding: 10px 16px; border-radius: 10px; background: #2b3038; color: var(--text);
  box-shadow: 0 6px 24px rgba(0,0,0,.4); font-size: 14px; max-width: 100%;
  overflow-wrap: anywhere; animation: toast-in .2s ease-out;
}
.toast.out { opacity: 0; transition: opacity .3s; }
@keyframes toast-in { from { opacity: 0; transform: translateY(6px); } }
@media (prefers-reduced-motion: reduce) {
  .toast { animation: none; }
  * { transition: none !important; }
}
