:root {
  --bg: #f6f7f9;
  --panel: #ffffff;
  --text: #15171a;
  --muted: #6b7280;
  --border: #e3e6ea;
  --accent: #f38020;
  --accent-text: #ffffff;
  --accent-soft: #fdebdc;
  --danger: #e5484d;
  --tile: #1d2025;
  --mine: #f38020;
  --mine-text: #ffffff;
  --theirs: #eef0f3;
  --radius: 12px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1113;
    --panel: #171a1d;
    --text: #e8eaed;
    --muted: #9aa1aa;
    --border: #2a2e33;
    --accent-soft: #3a2614;
    --tile: #000000;
    --theirs: #23272c;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
}
[hidden] { display: none !important; }
code { font-family: ui-monospace, Consolas, monospace; background: var(--theirs); padding: 1px 6px; border-radius: 6px; }

button, input, textarea { font: inherit; color: inherit; }
button {
  border: 1px solid var(--border);
  background: var(--panel);
  border-radius: 10px;
  padding: 9px 14px;
  cursor: pointer;
  white-space: nowrap;
}
button:hover { filter: brightness(0.97); }
button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); font-weight: 600; }
button.ghost { background: transparent; border-color: transparent; }
button:focus-visible, input:focus-visible, textarea:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
input, textarea {
  width: 100%;
  border: 1px solid var(--border);
  background: var(--panel);
  border-radius: 10px;
  padding: 10px 12px;
}
.row { display: flex; gap: 8px; }
.row input { flex: 1; }
.muted { color: var(--muted); }

/* Landing / name */
.screen:not(.room) {
  min-height: 100%;
  display: grid;
  place-items: center;
  padding: 24px 16px;
}
.card {
  width: 100%;
  max-width: 400px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 28px 24px;
  display: grid;
  gap: 12px;
}
.card h1 { margin: 0; font-size: 26px; }
.card p { margin: 0 0 8px; }
.card label { font-size: 13px; font-weight: 600; color: var(--muted); }
.card .primary { padding: 12px; }
.divider { display: flex; align-items: center; gap: 10px; color: var(--muted); font-size: 13px; margin: 6px 0; }
.divider::before, .divider::after { content: ""; flex: 1; height: 1px; background: var(--border); }

/* Room layout */
.room { height: 100dvh; display: flex; flex-direction: column; }
.topbar {
  display: flex; align-items: center; gap: 10px;
  padding: 8px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--panel);
  min-height: 52px;
}
.brand { font-weight: 700; color: var(--accent); text-decoration: none; }
#copy-btn { display: inline-flex; align-items: center; gap: 6px; font-family: ui-monospace, Consolas, monospace; color: var(--muted); padding: 6px 8px; }
.people { display: flex; gap: 4px; margin-left: auto; overflow: hidden; }
.avatar {
  width: 28px; height: 28px; border-radius: 50%;
  display: grid; place-items: center;
  font-size: 12px; font-weight: 700; color: #fff;
  flex: none; position: relative;
}
.avatar.in-call::after {
  content: ""; position: absolute; right: -1px; bottom: -1px;
  width: 9px; height: 9px; border-radius: 50%;
  background: #30a46c; border: 2px solid var(--panel);
}
.status { font-size: 12px; color: var(--muted); }
.status.bad { color: var(--danger); }

.room-body { flex: 1; display: flex; min-height: 0; }
.stage { flex: 1; display: flex; flex-direction: column; min-width: 0; background: #0b0c0e; }
.chat {
  width: 100%;
  display: flex; flex-direction: column;
  background: var(--panel);
  min-height: 0;
}
.stage:not([hidden]) + .chat { width: 360px; border-left: 1px solid var(--border); flex: none; }

/* Video grid */
.grid {
  flex: 1; min-height: 0;
  display: grid; gap: 8px; padding: 8px;
  grid-template-columns: repeat(var(--cols, 1), minmax(0, 1fr));
  grid-auto-rows: minmax(0, 1fr);
}
.tile {
  position: relative; background: var(--tile);
  border-radius: var(--radius); overflow: hidden;
  display: grid; place-items: center;
}
.tile video { width: 100%; height: 100%; object-fit: cover; background: #000; }
.tile.mirror video { transform: scaleX(-1); }
.tile.screen video { object-fit: contain; }
.tile .big-avatar {
  position: absolute; width: 72px; height: 72px; border-radius: 50%;
  display: none; place-items: center; font-size: 28px; font-weight: 700; color: #fff;
}
.tile.cam-off video { visibility: hidden; }
.tile.cam-off .big-avatar { display: grid; }
.tile .label {
  position: absolute; left: 8px; bottom: 8px;
  background: rgba(0,0,0,.55); color: #fff;
  font-size: 12px; padding: 3px 8px; border-radius: 6px;
  display: flex; gap: 6px; align-items: center;
}
.tile .label .mic-off { display: none; color: #ff8a8d; }
.tile.mic-off .label .mic-off { display: inline; }
.tile .conn { position: absolute; top: 8px; right: 8px; font-size: 11px; color: #fff; background: rgba(0,0,0,.55); padding: 2px 8px; border-radius: 6px; }
.tile .conn:empty { display: none; }

.controls { display: flex; justify-content: center; gap: 12px; padding: 12px; }
.round {
  width: 48px; height: 48px; border-radius: 50%; padding: 0;
  display: grid; place-items: center;
  background: #2b2f35; border-color: #2b2f35; color: #fff;
}
.round svg { width: 22px; height: 22px; }
.round .off { display: none; }
.round[aria-pressed="false"] .on { display: none; }
.round[aria-pressed="false"] .off { display: block; }
#mic-btn[aria-pressed="false"], #cam-btn[aria-pressed="false"] { background: #fff; color: #111; }
#screen-btn[aria-pressed="true"] { background: var(--accent); border-color: var(--accent); }
.round.danger { background: var(--danger); border-color: var(--danger); width: 60px; border-radius: 24px; }
.round:disabled { opacity: .4; cursor: not-allowed; }

/* Chat */
.call-banner {
  display: flex; align-items: center; justify-content: space-between; gap: 10px; flex-wrap: wrap;
  padding: 10px 16px; border-bottom: 1px solid var(--border);
  font-size: 14px;
}
.call-banner.live { background: var(--accent-soft); }
.call-banner button { padding: 7px 12px; font-size: 14px; }
.messages {
  flex: 1; overflow-y: auto; list-style: none; margin: 0;
  padding: 16px; display: flex; flex-direction: column; gap: 4px;
  width: 100%; max-width: 820px; align-self: center;
}
.msg { display: flex; flex-direction: column; align-items: flex-start; max-width: 80%; }
.msg.mine { align-self: flex-end; align-items: flex-end; }
.msg .who { font-size: 12px; color: var(--muted); margin: 8px 4px 2px; }
.msg .bubble {
  background: var(--theirs); padding: 8px 12px; border-radius: 16px;
  white-space: pre-wrap; overflow-wrap: anywhere;
}
.msg.mine .bubble { background: var(--mine); color: var(--mine-text); }
.msg .bubble a { color: inherit; }
.msg .time { font-size: 11px; color: var(--muted); margin: 2px 6px 0; }
.sys { align-self: center; font-size: 12px; color: var(--muted); margin: 6px 0; text-align: center; }
.typing { height: 18px; font-size: 12px; color: var(--muted); padding: 0 16px; max-width: 820px; width: 100%; align-self: center; }
.composer {
  display: flex; gap: 8px; padding: 8px 16px 4px;
  max-width: 820px; width: 100%; align-self: center;
}
.composer textarea { resize: none; max-height: 140px; }
.composer button { padding: 0 14px; }
.ephemeral { margin: 0; padding: 4px 16px 10px; font-size: 11px; color: var(--muted); text-align: center; }

.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%);
  background: #222; color: #fff; padding: 10px 16px; border-radius: 10px;
  font-size: 14px; z-index: 10; max-width: calc(100% - 32px);
}

/* Mobile: stack video above chat */
@media (max-width: 760px) {
  .room-body { flex-direction: column; }
  .stage { flex: none; height: 55dvh; }
  .stage:not([hidden]) + .chat { width: 100%; border-left: 0; border-top: 1px solid var(--border); flex: 1; }
  .people { display: none; }
  .topbar .status { margin-left: auto; }
  .controls { padding: 8px; gap: 10px; }
  .round { width: 44px; height: 44px; }
  .msg { max-width: 88%; }
}
