:root {
  color-scheme: dark;
  --bg: #0e1116;
  --surface: #161b22;
  --surface-2: #1f2630;
  --surface-3: #2a323e;
  --line: #2a313b;
  --text: #e7eaee;
  --muted: #8d97a5;
  --accent: #3b82f6;
  --accent-hover: #2f6fdc;
  --danger: #e5484d;
  --danger-hover: #cc3d42;
  --ok: #30a46c;
  --warn: #f5a524;
  --radius: 12px;
  --gutter: 16px;
  --safe-b: env(safe-area-inset-bottom, 0px);
}

/* Elements with [hidden] must never be shown, whatever their display rule. */
[hidden] { display: none !important; }

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}
h1, h2 { margin: 0; line-height: 1.2; }
h2 { font-size: 18px; margin-bottom: 8px; }
p { margin: 0 0 12px; }
a { color: var(--accent); }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.center { text-align: center; }
.spacer { flex: 1; }

/* ---------- Icons ---------- */
.icon { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex: none; }
.icon.sm { width: 16px; height: 16px; }
.logo { width: 28px; height: 28px; flex: none; }
.icon.slash { display: none; position: absolute; top: 50%; transform: translateY(-50%); }
.round .icon.slash { left: 50%; transform: translate(-50%, -50%); }
.off > .icon.slash { display: block; }

/* ---------- Buttons ---------- */
button, input, textarea { font: inherit; color: inherit; }
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 10px 16px; border: 1px solid var(--line); border-radius: 10px;
  background: var(--surface-2); color: var(--text); font-weight: 500;
  text-decoration: none; cursor: pointer; white-space: nowrap;
  transition: background .15s, border-color .15s;
}
.btn:hover { background: var(--surface-3); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { background: var(--accent-hover); }
.btn.light { background: #fff; border-color: #fff; color: #111; }
.btn.sm { padding: 6px 10px; font-size: 13px; border-radius: 8px; }
.btn.block { width: 100%; }
.btn:disabled { opacity: .45; cursor: default; }

.icon-btn {
  display: inline-grid; place-items: center; width: 38px; height: 38px; padding: 0;
  border: 0; border-radius: 10px; background: transparent; color: var(--muted); cursor: pointer;
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); }
.icon-btn.primary { background: var(--accent); color: #fff; }

input, textarea {
  width: 100%; padding: 11px 12px; border: 1px solid var(--line); border-radius: 10px;
  background: var(--bg); outline: none; transition: border-color .15s;
}
input:focus, textarea:focus { border-color: var(--accent); }
textarea { resize: none; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------- Lobby ---------- */
.lobby { height: 100%; overflow-y: auto; display: grid; place-items: center; padding: 24px var(--gutter); }
.lobby-card {
  width: min(920px, 100%); display: grid; grid-template-columns: 1.3fr 1fr; gap: 32px; align-items: center;
}
.preview {
  position: relative; aspect-ratio: 16 / 10; border-radius: 16px; overflow: hidden;
  background: var(--surface); border: 1px solid var(--line);
}
.preview video { width: 100%; height: 100%; object-fit: cover; transform: scaleX(-1); display: block; }
.preview-off { position: absolute; inset: 0; display: grid; place-items: center; color: var(--muted); }
.preview-controls { position: absolute; left: 0; right: 0; bottom: 14px; display: flex; justify-content: center; gap: 12px; }
.round {
  position: relative; width: 48px; height: 48px; display: grid; place-items: center;
  border-radius: 50%; border: 1px solid rgba(255,255,255,.25); background: rgba(0,0,0,.35);
  color: #fff; cursor: pointer; backdrop-filter: blur(6px);
}
.round.off { background: var(--danger); border-color: var(--danger); }
/* The noise filter being off is a choice, not a warning: no red. */
.round.media-noise.off { background: rgba(0,0,0,.35); border-color: rgba(255,255,255,.25); color: rgba(255,255,255,.6); }
.lobby-form { display: grid; gap: 14px; }
.brand { display: flex; align-items: center; gap: 10px; font-weight: 600; font-size: 17px; }
.lobby-form h1 { font-size: 28px; margin-top: 6px; }
.lobby-form p { margin: 0; }
.field { display: grid; gap: 6px; font-size: 14px; font-weight: 500; }

/* ---------- Session ended ---------- */
.ended { height: 100%; display: grid; place-items: center; padding: 24px var(--gutter); }
.ended-card { max-width: 420px; display: grid; justify-items: center; gap: 12px; text-align: center; }
.ended-card .logo { width: 44px; height: 44px; }
.ended-card h1 { font-size: 26px; }
.ended-card p { margin: 0; }
.ended-actions { display: flex; flex-wrap: wrap; justify-content: center; gap: 10px; margin-top: 8px; }

/* ---------- Call layout ---------- */
.call { height: 100%; height: 100dvh; display: grid; grid-template-rows: auto 1fr auto; }
.topbar { display: flex; align-items: center; gap: 12px; padding: 10px var(--gutter); min-width: 0; }
.chip {
  display: inline-flex; align-items: center; gap: 8px; min-width: 0; max-width: 50vw;
  padding: 6px 12px; border: 1px solid var(--line); border-radius: 999px;
  background: var(--surface); color: var(--text); cursor: pointer; font-size: 14px;
}
.chip span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chip:hover { background: var(--surface-2); }
.pill { display: inline-flex; align-items: center; gap: 8px; font-size: 13px; color: var(--muted); white-space: nowrap; min-width: 0; }
.pill #status { overflow: hidden; text-overflow: ellipsis; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); flex: none; }
.pill[data-state="connecting"] .dot { background: var(--warn); }
.pill[data-state="connected"] .dot { background: var(--ok); }
.pill[data-state="failed"] .dot { background: var(--danger); }

.main { display: flex; min-height: 0; gap: 12px; padding: 0 12px; }

/* ---------- Stage ---------- */
.stage {
  position: relative; flex: 1; min-width: 0; border-radius: 16px; overflow: hidden;
  background: #05070a; border: 1px solid var(--line);
}
.empty {
  position: absolute; inset: 0; display: grid; place-content: center; justify-items: center;
  gap: 4px; padding: 24px; text-align: center;
}
.empty-title { font-size: 20px; font-weight: 600; margin-bottom: 4px; }
.invite {
  display: flex; align-items: center; gap: 8px; max-width: min(560px, 100%); margin-top: 8px;
  padding: 6px 6px 6px 14px; border: 1px solid var(--line); border-radius: 12px; background: var(--surface);
}
.invite code { font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }

#remoteScreen { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: contain; outline: none; background: #000; }
#remoteScreen.controlling { cursor: crosshair; touch-action: none; }

/* Other people's mouse pointers over the shared screen, each in their color. */
.pointers { position: absolute; inset: 0; pointer-events: none; overflow: hidden; }
.pointer {
  position: absolute; left: 0; top: 0; display: flex; align-items: flex-start;
  transition: transform 70ms linear, opacity .4s; will-change: transform;
}
.pointer svg { width: 22px; height: 22px; fill: var(--c); stroke: #fff; stroke-width: 1.5; stroke-linejoin: round; filter: drop-shadow(0 1px 2px rgba(0,0,0,.5)); }
.pointer span {
  margin: 16px 0 0 -4px; padding: 2px 8px; border-radius: 999px; background: var(--c); color: #fff;
  font-size: 12px; font-weight: 600; white-space: nowrap; box-shadow: 0 1px 3px rgba(0,0,0,.4);
}
.pointer.idle { opacity: .35; }

.tile { position: absolute; overflow: hidden; background: var(--surface-2); }
.tile video { width: 100%; height: 100%; object-fit: cover; display: block; }
.name-tag {
  position: absolute; left: 8px; bottom: 8px; max-width: calc(100% - 16px);
  padding: 2px 8px; border-radius: 6px; background: rgba(0,0,0,.55); font-size: 12px;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.avatar {
  position: absolute; inset: 0; display: grid; place-items: center;
  font-size: 28px; font-weight: 600; color: var(--muted); background: var(--surface-2);
}
.tile.self video { transform: scaleX(-1); }

/* Everyone else's cameras: a grid filling the stage (one person fills it all),
   and a floating strip while someone shares their screen. */
.grid {
  position: absolute; inset: 0; display: grid; gap: 8px; padding: 8px; align-content: center; justify-content: center;
  grid-template-columns: repeat(var(--cols, 1), var(--tile-w, 1fr)); grid-auto-rows: var(--tile-h, 1fr);
}
.grid .tile { position: relative; border-radius: 10px; min-height: 0; }
.grid[data-count="1"] { padding: 0; gap: 0; grid-template-columns: 100%; grid-auto-rows: 100%; }
.grid[data-count="1"] .tile { border-radius: 0; }
.grid[data-count="1"] video { object-fit: contain; background: #05070a; }
.grid .tile.focused { outline: 2px solid var(--accent); outline-offset: -2px; }
.share-tag {
  position: absolute; left: 8px; top: 8px; padding: 2px 8px; border-radius: 6px;
  background: var(--accent); color: #fff; font-size: 11px; font-weight: 600;
}
.stage[data-mode="screen"] .grid {
  inset: auto; top: 12px; right: 12px; width: 200px; max-height: calc(100% - 24px);
  display: flex; flex-direction: column; gap: 8px; padding: 0; overflow-y: auto; z-index: 2;
}
.stage[data-mode="screen"] .grid .tile {
  flex: none; aspect-ratio: 16 / 10; border-radius: 10px; cursor: pointer;
  box-shadow: 0 6px 24px rgba(0,0,0,.5); border: 1px solid rgba(255,255,255,.08);
}
.stage[data-mode="screen"] .grid video { object-fit: cover; }
.screen-chip {
  position: absolute; left: 12px; bottom: 12px; z-index: 2; padding: 4px 10px; border-radius: 8px;
  background: rgba(0,0,0,.6); font-size: 13px; pointer-events: none;
}
.tile.self {
  right: 12px; bottom: 12px; width: 200px; aspect-ratio: 16 / 10; border-radius: 10px;
  box-shadow: 0 6px 24px rgba(0,0,0,.5); border: 1px solid rgba(255,255,255,.08); z-index: 2;
}
.stage[data-mode="empty"] .tile.self { width: 240px; }
/* Floating tiles can be dragged anywhere on the stage. */
.tile.self, .stage[data-mode="screen"] .grid { cursor: grab; touch-action: none; user-select: none; }
.stage[data-mode="screen"] .grid.dragging { cursor: grabbing; }
.tile.dragging { cursor: grabbing; opacity: .9; }
.tile-btn {
  position: absolute; top: 6px; right: 6px; width: 30px; height: 30px; display: grid; place-items: center;
  border: 0; border-radius: 8px; background: rgba(0,0,0,.55); color: #fff; cursor: pointer;
  opacity: 0; transition: opacity .15s;
}
.tile:hover .tile-btn, .tile-btn:focus-visible { opacity: 1; }
@media (hover: none) { .tile-btn { opacity: 1; } }
.grid[data-count="1"] .tile-btn { top: 12px; right: 12px; }
/* While the cameras are popped out, don't show them twice. */
.stage.popped .tile.self, .stage.popped[data-mode="screen"] .grid { display: none; }

/* ---------- Pop-out camera window (Document Picture-in-Picture) ---------- */
.pip { position: fixed; inset: 0; background: #05070a; overflow: hidden; }
.pip-peers { position: absolute; inset: 0; display: grid; grid-auto-rows: 1fr; gap: 2px; }
.pip-peers video { width: 100%; height: 100%; min-height: 0; object-fit: cover; display: block; }
.pip-wait { position: absolute; inset: 0; display: grid; place-items: center; color: var(--muted); font-size: 14px; }
.pip-self {
  position: absolute; right: 8px; bottom: 8px; width: 30%; aspect-ratio: 16 / 10; border-radius: 8px; overflow: hidden;
  background: var(--surface-2); border: 1px solid rgba(255,255,255,.12); box-shadow: 0 4px 16px rgba(0,0,0,.5);
}
.pip-self video { width: 100%; height: 100%; object-fit: cover; transform: scaleX(-1); display: block; }
.pip .name-tag { top: 8px; bottom: auto; }
.pip .ctrl { position: absolute; left: 8px; bottom: 8px; width: 40px; height: 40px; padding: 0; justify-content: center; }
.pip .ctrl .icon.slash { left: 8px; }

/* ---------- Banners and toasts ---------- */
.banner {
  position: absolute; top: 12px; left: 50%; transform: translateX(-50%); z-index: 3;
  display: flex; align-items: center; gap: 10px; max-width: calc(100% - 24px);
  padding: 6px 6px 6px 14px; border-radius: 999px; background: var(--accent); color: #fff;
  font-size: 14px; box-shadow: 0 6px 24px rgba(0,0,0,.4);
}
.banner > span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.banner .btn { background: rgba(255,255,255,.18); border-color: transparent; color: #fff; border-radius: 999px; }
.banner .btn.light { background: #fff; color: #111; }
.banner.danger { background: var(--danger); }
.stage[data-mode="screen"] .banner { top: 12px; }
.pulse { width: 8px; height: 8px; border-radius: 50%; background: #fff; flex: none; animation: pulse 1.2s infinite; }
@keyframes pulse { 50% { opacity: .25; } }

.toasts {
  position: absolute; left: 50%; bottom: 16px; transform: translateX(-50%); z-index: 4;
  display: grid; gap: 8px; justify-items: center; width: max-content; max-width: calc(100% - 24px); pointer-events: none;
}
.toast {
  padding: 8px 14px; border-radius: 10px; background: rgba(22,27,34,.95); border: 1px solid var(--line);
  font-size: 14px; box-shadow: 0 6px 24px rgba(0,0,0,.4); animation: toast-in .2s ease-out;
}
@keyframes toast-in { from { opacity: 0; transform: translateY(6px); } }

/* ---------- Chat ---------- */
.chat {
  width: 340px; flex: none; display: grid; grid-template-rows: auto 1fr auto; min-height: 0;
  border-radius: 16px; background: var(--surface); border: 1px solid var(--line); overflow: hidden;
}
.chat header { display: flex; align-items: center; gap: 10px; padding: 8px 8px 8px 16px; border-bottom: 1px solid var(--line); }
.chat header .hint { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.messages { overflow-y: auto; padding: 14px; display: grid; gap: 10px; align-content: start; overscroll-behavior: contain; }
.msg { display: grid; gap: 2px; max-width: 100%; }
.msg .who { font-size: 12px; color: var(--muted); }
.msg .body {
  white-space: pre-wrap; overflow-wrap: anywhere; font: 13px/1.45 ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  background: var(--surface-2); padding: 8px 10px; border-radius: 10px;
}
.msg.mine { justify-items: end; }
.msg.mine .body { background: #1d3a66; }
.msg.system .body { font-family: inherit; background: transparent; color: var(--muted); padding: 0; font-size: 13px; text-align: center; }
.msg.system { justify-items: center; }
.composer { display: flex; align-items: flex-end; gap: 8px; padding: 10px; border-top: 1px solid var(--line); }
.composer textarea { max-height: 160px; font: 14px/1.4 ui-monospace, "SF Mono", Menlo, Consolas, monospace; }
.composer .icon-btn { width: 42px; height: 42px; flex: none; }

/* ---------- Control bar ---------- */
.controls {
  display: flex; justify-content: center; gap: 10px;
  padding: 12px var(--gutter) calc(12px + var(--safe-b));
}
.ctrl {
  position: relative; display: inline-flex; align-items: center; gap: 8px; height: 48px; padding: 0 18px;
  border: 0; border-radius: 999px; background: var(--surface-2); color: var(--text);
  font-weight: 500; cursor: pointer; transition: background .15s;
}
.ctrl:not(.active, .off, .danger):hover:not(:disabled) { background: var(--surface-3); }
.ctrl:disabled { opacity: .4; cursor: default; }
.ctrl.off { background: var(--danger); color: #fff; }
.ctrl.active { background: var(--accent); color: #fff; }
.ctrl.media-noise.off { background: var(--surface-2); color: var(--muted); }
.ctrl.danger { background: var(--danger); color: #fff; }
.ctrl.danger:hover { background: var(--danger-hover); }
.ctrl .icon.slash { left: 18px; }
.ctrl .swatch { width: 10px; height: 10px; border-radius: 50%; background: var(--c, transparent); box-shadow: 0 0 0 2px rgba(255,255,255,.85); }
.badge {
  position: absolute; top: 4px; right: 6px; min-width: 18px; height: 18px; padding: 0 5px;
  border-radius: 9px; background: var(--accent); color: #fff; font-size: 11px; font-weight: 600;
  display: grid; place-items: center;
}

/* ---------- Dialogs ---------- */
dialog {
  width: min(440px, calc(100% - 2 * var(--gutter))); padding: 22px;
  background: var(--surface); color: var(--text); border: 1px solid var(--line); border-radius: 16px;
  box-shadow: 0 20px 60px rgba(0,0,0,.6);
}
dialog::backdrop { background: rgba(0,0,0,.55); backdrop-filter: blur(2px); }
dialog menu { display: flex; align-items: center; gap: 8px; justify-content: flex-end; padding: 0; margin: 16px 0 0; }
dialog .btn.block { margin: 4px 0 14px; }
dialog textarea { font: 14px/1.4 ui-monospace, "SF Mono", Menlo, Consolas, monospace; }
.waiting { display: flex; align-items: center; gap: 10px; margin-bottom: 6px; }
.spinner { width: 16px; height: 16px; border: 2px solid var(--surface-3); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; flex: none; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- Tablet: chat floats over the stage ---------- */
@media (max-width: 1024px) {
  .chat {
    position: fixed; top: 60px; right: 12px; bottom: calc(84px + var(--safe-b)); z-index: 10;
    width: min(380px, calc(100% - 24px)); box-shadow: 0 20px 60px rgba(0,0,0,.6);
  }
}

/* ---------- Phone ---------- */
@media (max-width: 640px) {
  :root { --gutter: 12px; }
  .lobby { place-items: start center; padding-top: 16px; }
  .lobby-card { grid-template-columns: 1fr; gap: 20px; }
  .preview { aspect-ratio: 4 / 3; }
  .lobby-form h1 { font-size: 24px; }
  .brand { order: -1; }

  .topbar { padding: 8px var(--gutter); gap: 8px; }
  .topbar .logo { width: 24px; height: 24px; }
  .chip { font-size: 13px; padding: 5px 10px; max-width: 45vw; }
  .main { padding: 0 8px; }
  .stage { border-radius: 12px; }

  .tile.self, .stage[data-mode="empty"] .tile.self { width: 96px; aspect-ratio: 3 / 4; right: 8px; bottom: 8px; border-radius: 8px; }
  .stage[data-mode="screen"] .grid { width: 96px; top: 8px; right: 8px; gap: 6px; }
  .stage[data-mode="screen"] .grid .tile { aspect-ratio: 3 / 4; border-radius: 8px; }
  .stage[data-mode="screen"] .grid .tile-btn { display: none; }
  .share-tag { left: 4px; top: 4px; font-size: 10px; padding: 1px 6px; }
  .screen-chip { left: 8px; bottom: 8px; font-size: 12px; }
  .stage[data-mode="screen"] .banner, .banner { top: 8px; font-size: 13px; }
  .stage[data-mode="screen"] .banner { left: 8px; right: 112px; transform: none; max-width: none; }
  .banner .btn .label { display: none; }
  .name-tag { font-size: 11px; left: 4px; bottom: 4px; }
  .avatar { font-size: 22px; }
  .empty-title { font-size: 18px; }
  .invite { flex-direction: column; align-items: stretch; padding: 10px; }
  .invite code { white-space: normal; overflow-wrap: anywhere; }
  .toasts { bottom: 12px; max-width: calc(100% - 120px); left: 12px; transform: none; justify-items: start; }
  .toast { font-size: 13px; padding: 7px 12px; }
  /* Fill the phone screen with the other person's camera. */
  .grid[data-count="1"] video { object-fit: cover; }

  /* Chat becomes a full-screen sheet. */
  .chat { inset: 0; top: 0; right: 0; bottom: 0; width: 100%; border-radius: 0; border: 0; padding-bottom: var(--safe-b); }
  .chat header .hint { display: none; }

  .controls { gap: 4px; justify-content: space-between; }
  .ctrl { width: 44px; height: 44px; padding: 0; justify-content: center; flex: 0 1 44px; min-width: 34px; }
  .ctrl .label { display: none; }
  .ctrl .icon.slash { left: calc(50% - 11px); }
  .ctrl .swatch { position: absolute; top: 6px; right: 6px; width: 8px; height: 8px; }
  .pointer svg { width: 18px; height: 18px; }
  .pointer span { font-size: 11px; padding: 1px 6px; }
}

@media (max-width: 360px) {
  .ctrl { width: 40px; height: 40px; flex-basis: 40px; }
}
