:root {
  --bg: #0f1115;
  --panel: #171a21;
  --panel-2: #1e222b;
  --text: #e8eaed;
  --muted: #9aa0a6;
  --accent: #3b82f6;
  --accent-2: #2563eb;
  --danger: #ef4444;
  --ok: #22c55e;
  --radius: 14px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
}

.hidden { display: none !important; }

/* ---------- lobby screens ---------- */
.screen {
  min-height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}

.card {
  width: 100%;
  max-width: 420px;
  background: var(--panel);
  border: 1px solid #262a33;
  border-radius: var(--radius);
  padding: 28px;
  text-align: center;
}

.card h1 { margin: 0 0 8px; font-size: 24px; }
.muted { color: var(--muted); }
.field {
  display: block;
  text-align: left;
  margin: 18px 0;
  font-size: 14px;
  color: var(--muted);
}
.field input {
  display: block;
  width: 100%;
  margin-top: 6px;
  padding: 12px 14px;
  font-size: 16px;
  color: var(--text);
  background: var(--panel-2);
  border: 1px solid #2c313b;
  border-radius: 10px;
}
.field input:focus { outline: none; border-color: var(--accent); }

button {
  font: inherit;
  cursor: pointer;
  border: none;
  border-radius: 10px;
}
.primary {
  width: 100%;
  padding: 14px 18px;
  font-size: 16px;
  font-weight: 600;
  color: #fff;
  background: var(--accent-2);
}
.primary:hover { background: var(--accent); }
.error { color: var(--danger); font-size: 14px; margin: 10px 0 0; }
code {
  background: var(--panel-2);
  padding: 2px 8px;
  border-radius: 6px;
  font-size: 14px;
}

/* ---------- call screen ---------- */
#screen-call {
  height: 100dvh;
  display: flex;
  flex-direction: column;
}

#stage {
  position: relative;
  flex: 1;
  min-height: 0;
  padding: 12px;
  padding-bottom: 96px;
  overflow: hidden;
}

#sharedWrap {
  width: 100%;
  height: 100%;
  position: relative;
  background: #000;
  border-radius: var(--radius);
  overflow: hidden;
}
#sharedVideo { width: 100%; height: 100%; object-fit: contain; background: #000; }
.shared-label {
  position: absolute;
  left: 12px;
  bottom: 12px;
  padding: 6px 12px;
  font-size: 13px;
  background: rgba(0, 0, 0, .6);
  border-radius: 8px;
}

#grid {
  display: grid;
  gap: 10px;
  width: 100%;
  height: 100%;
  grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
  align-content: center;
  justify-content: center;
}
#grid.grid-1 { grid-template-columns: minmax(0, 1fr); max-width: 900px; margin: 0 auto; }
#grid.grid-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }

/* when a screen is shared, tiles become a corner strip */
#stage.sharing #sharedWrap { height: 100%; }
#stage.sharing #grid {
  position: absolute;
  right: 20px;
  top: 20px;
  width: 220px;
  height: auto;
  max-height: calc(100% - 120px);
  grid-template-columns: 1fr;
  overflow-y: auto;
  align-content: start;
  z-index: 5;
}
#stage.sharing #grid .tile { aspect-ratio: 16 / 10; }

.tile {
  position: relative;
  aspect-ratio: 16 / 9;
  background: #000;
  border-radius: 12px;
  overflow: hidden;
  min-height: 0;
}
.tile video {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
  /* Safari: форсируем композитный слой, иначе <video> с MediaStream
     рендерится чёрным в странице (в PiP тот же элемент показывает кадры). */
  transform: translateZ(0);
  -webkit-backface-visibility: hidden;
  will-change: transform;
}
.tile.self video { transform: scaleX(-1) translateZ(0); }
.tile .name {
  position: absolute;
  left: 10px;
  bottom: 8px;
  padding: 3px 9px;
  font-size: 12px;
  background: rgba(0, 0, 0, .55);
  border-radius: 7px;
  max-width: 80%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.tile .badges {
  position: absolute;
  right: 10px;
  top: 8px;
  display: flex;
  gap: 6px;
}
.badge {
  font-size: 11px;
  padding: 3px 8px;
  border-radius: 7px;
  background: rgba(239, 68, 68, .85);
}
.tile.cam-off video { opacity: 0.15; }
.tile .kick {
  position: absolute;
  left: 10px;
  top: 8px;
  width: 26px;
  height: 26px;
  border: none;
  border-radius: 50%;
  background: rgba(239, 68, 68, .9);
  color: #fff;
  font-size: 15px;
  line-height: 1;
  cursor: pointer;
  z-index: 3;
}
.tile .kick:hover { background: var(--danger, #ef4444); }

/* fullscreen toggle (peer camera tile + shared view) */
.fs {
  position: absolute;
  right: 10px;
  bottom: 8px;
  width: 28px;
  height: 28px;
  border: none;
  border-radius: 7px;
  background: rgba(0, 0, 0, .5);
  color: #fff;
  font-size: 16px;
  line-height: 1;
  cursor: pointer;
  z-index: 3;
}
.fs:hover { background: rgba(0, 0, 0, .8); }
#sharedWrap .fs { opacity: .85; }

/* ---------- controls ---------- */
#controls {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  gap: 8px;
  justify-content: center;
  flex-wrap: wrap;
  padding: 14px 12px calc(14px + env(safe-area-inset-bottom));
  background: linear-gradient(to top, rgba(15, 17, 21, .96), rgba(15, 17, 21, 0));
}
.ctrl {
  padding: 11px 16px;
  font-size: 14px;
  color: var(--text);
  background: var(--panel-2);
  border: 1px solid #2c313b;
}
.ctrl:hover { background: #262b35; }
.ctrl.active { background: var(--accent-2); border-color: var(--accent); color: #fff; }
.ctrl.off { background: #3a1d1d; border-color: #5b2626; color: #ffb4b4; }
.ctrl.danger { background: var(--danger); color: #fff; border-color: var(--danger); }
.ctrl:disabled { opacity: .4; cursor: default; }

/* ---------- toast ---------- */
#toast {
  position: fixed;
  left: 50%;
  bottom: 110px;
  transform: translateX(-50%);
  padding: 10px 18px;
  background: rgba(0, 0, 0, .85);
  border-radius: 10px;
  font-size: 14px;
  z-index: 20;
}

/* ---------- mobile ---------- */
@media (max-width: 640px) {
  #stage { padding: 8px; padding-bottom: 92px; }
  #grid { grid-template-columns: 1fr; gap: 8px; }
  #grid.grid-2 { grid-template-columns: 1fr; }
  #stage.sharing #grid {
    right: 12px;
    top: 12px;
    width: 130px;
  }
  .ctrl { padding: 10px 12px; font-size: 13px; }
}
