:root {
  color-scheme: dark;
  --bg: #050711;
  --panel: rgba(255,255,255,.08);
  --line: rgba(255,255,255,.14);
  --text: #f6f7fb;
  --muted: #a9b1c7;
  --accent: #22c55e;
  --danger: #ff4d6d;
}
* { box-sizing: border-box; }
body { margin: 0; min-height: 100vh; font-family: Inter, ui-sans-serif, system-ui, -apple-system, Segoe UI, sans-serif; background: radial-gradient(circle at top left, #14532d, transparent 34rem), var(--bg); color: var(--text); }
.app { width: min(1120px, calc(100% - 32px)); margin: 32px auto; display: grid; gap: 18px; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: 22px; padding: 22px; backdrop-filter: blur(16px); box-shadow: 0 18px 60px rgba(0,0,0,.28); }
.eyebrow { color: #bbf7d0; text-transform: uppercase; letter-spacing: .14em; font-size: .75rem; font-weight: 800; }
h1 { margin: .1rem 0 .5rem; font-size: clamp(2rem, 5vw, 4rem); }
p { color: var(--muted); line-height: 1.5; }
.join { display: grid; gap: 10px; margin-top: 18px; }
label { font-weight: 750; }
.row { display: flex; gap: 10px; }
input, button { border: 0; border-radius: 14px; padding: 13px 15px; font: inherit; }
input { min-width: 0; flex: 1; background: rgba(255,255,255,.1); color: var(--text); outline: 1px solid var(--line); }
button { cursor: pointer; color: var(--text); background: rgba(255,255,255,.14); border: 1px solid var(--line); font-weight: 800; }
button:hover:not(:disabled) { transform: translateY(-1px); filter: brightness(1.08); }
button:disabled { cursor: not-allowed; opacity: .45; }
.primary { background: linear-gradient(135deg, var(--accent), #06b6d4); }
.danger { background: linear-gradient(135deg, var(--danger), #ff8a4d); }
.status { min-height: 1.5em; margin-bottom: 0; }
.callArea { display: none; min-height: 55vh; }
.videoGrid { display: grid; gap: 12px; height: 100%; grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); grid-auto-rows: minmax(210px, 1fr); }
.tile { position: relative; overflow: hidden; border-radius: 24px; background: #020617; border: 1px solid var(--line); min-height: 210px; }
.tile video { width: 100%; height: 100%; object-fit: cover; display: block; background: #020617; }
.tile span { position: absolute; left: 12px; bottom: 12px; padding: 7px 10px; border-radius: 999px; background: rgba(0,0,0,.55); color: white; font-size: .86rem; }
.controls { display: none; gap: 12px; }
.call-status { margin: 0; color: #dbeafe; font-size: .95rem; }
.control-buttons { display: flex; flex-wrap: wrap; gap: 10px; }
.control-buttons button { flex: 1 1 150px; }
body.in-call { background: #020617; overflow: hidden; }
body.in-call .intro { display: none; }
body.in-call .callArea { display: block; }
body.in-call .controls { display: grid; position: sticky; bottom: 10px; z-index: 5; }
body.in-call .app { width: min(1180px, calc(100% - 24px)); margin: 12px auto; min-height: calc(100vh - 24px); grid-template-rows: minmax(0, 1fr) auto; }
body.in-call .videoGrid.count-1 { grid-template-columns: 1fr; }
body.in-call .videoGrid.count-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
body.in-call .videoGrid.count-3, body.in-call .videoGrid.count-4 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
@supports (height: 100dvh) { body.in-call .app { min-height: 100dvh; } }
@media (max-width: 760px) {
  body { padding-top: env(safe-area-inset-top); padding-bottom: env(safe-area-inset-bottom); }
  .app { width: min(100% - 20px, 760px); margin: 14px auto; gap: 12px; }
  .card { padding: 16px; border-radius: 18px; }
  .row { flex-direction: column; }
  body.in-call .app { width: 100%; margin: 0; min-height: 100dvh; max-height: 100dvh; padding: calc(env(safe-area-inset-top) + 10px) 8px calc(env(safe-area-inset-bottom) + 104px); overflow: hidden; }
  body.in-call .callArea { min-height: 0; height: 100%; overflow: hidden; }
  body.in-call .videoGrid { height: 100%; gap: 8px; grid-template-columns: 1fr; grid-auto-rows: minmax(0, 1fr); }
  body.in-call .videoGrid.count-2, body.in-call .videoGrid.count-3, body.in-call .videoGrid.count-4 { grid-template-columns: 1fr; }
  body.in-call .tile { border-radius: 18px; min-height: 0; }
  body.in-call .controls { position: fixed; left: 8px; right: 8px; bottom: calc(env(safe-area-inset-bottom) + 14px); padding: 10px; max-height: 96px; overflow: hidden; }
  body.in-call .control-buttons { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; }
  body.in-call .control-buttons button { min-height: 42px; padding: 11px 8px; font-size: 13px; }
  body.in-call .call-status { font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
}

/* Group-call fit-to-screen refinements: avoid face cropping + desktop scroll */
body.in-call .app {
  height: calc(100vh - 24px);
  max-height: calc(100vh - 24px);
  overflow: hidden;
}
body.in-call .callArea {
  min-height: 0;
  height: 100%;
  overflow: hidden;
}
body.in-call .videoGrid {
  min-height: 0;
  height: 100%;
  grid-auto-rows: minmax(0, 1fr);
}
body.in-call .tile {
  min-height: 0;
  height: 100%;
}
body.in-call .tile video {
  object-fit: contain;
  background: #020617;
}
body.in-call .videoGrid.count-1 { grid-template-columns: 1fr; grid-template-rows: 1fr; }
body.in-call .videoGrid.count-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); grid-template-rows: 1fr; }
body.in-call .videoGrid.count-3,
body.in-call .videoGrid.count-4 { grid-template-columns: repeat(2, minmax(0, 1fr)); grid-template-rows: repeat(2, minmax(0, 1fr)); }

@supports (height: 100dvh) {
  body.in-call .app {
    height: calc(100dvh - 24px);
    max-height: calc(100dvh - 24px);
  }
}

@media (min-width: 761px) {
  body.in-call .controls {
    align-self: end;
    padding: 12px 14px;
  }
  body.in-call .call-status {
    font-size: 13px;
  }
  body.in-call .control-buttons button {
    padding: 10px 12px;
  }
}

@media (max-width: 760px) {
  body.in-call .app {
    padding: calc(env(safe-area-inset-top) + 18px) 8px calc(env(safe-area-inset-bottom) + 112px);
  }
  body.in-call .videoGrid {
    align-content: stretch;
  }
  body.in-call .videoGrid.count-1 { grid-template-rows: 1fr; }
  body.in-call .videoGrid.count-2 { grid-template-rows: repeat(2, minmax(0, 1fr)); }
  body.in-call .videoGrid.count-3 { grid-template-rows: repeat(3, minmax(0, 1fr)); }
  body.in-call .videoGrid.count-4 { grid-template-rows: repeat(4, minmax(0, 1fr)); }
  body.in-call .tile video {
    object-fit: contain;
  }
}

/* Desktop/iPad fit-all mode: controls overlay, grid owns the viewport */
body.in-call .app {
  width: 100vw;
  height: 100vh;
  max-height: 100vh;
  margin: 0;
  padding: 10px 10px 92px;
  display: grid;
  grid-template-rows: minmax(0, 1fr);
}
body.in-call .callArea {
  width: 100%;
  height: 100%;
  min-height: 0;
}
body.in-call .videoGrid {
  width: 100%;
  height: 100%;
  min-height: 0;
  grid-auto-rows: minmax(0, 1fr);
}
body.in-call .controls {
  position: fixed;
  left: 10px;
  right: 10px;
  bottom: 10px;
  z-index: 10;
  padding: 10px 12px;
  background: rgba(8, 15, 29, .72);
  backdrop-filter: blur(18px);
}
body.in-call .control-buttons button {
  min-height: 38px;
  padding: 9px 12px;
}
body.in-call .call-status {
  font-size: 12px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
body.in-call .videoGrid.count-1 {
  grid-template-columns: 1fr;
  grid-template-rows: 1fr;
}
body.in-call .videoGrid.count-2 {
  grid-template-columns: repeat(2, minmax(0, 1fr));
  grid-template-rows: 1fr;
}
body.in-call .videoGrid.count-3,
body.in-call .videoGrid.count-4 {
  grid-template-columns: repeat(2, minmax(0, 1fr));
  grid-template-rows: repeat(2, minmax(0, 1fr));
}

@supports (height: 100dvh) {
  body.in-call .app {
    height: 100dvh;
    max-height: 100dvh;
  }
}

/* Narrow tablets / split-screen: keep 2x2 longer, only stack when truly phone-width */
@media (max-width: 560px) {
  body.in-call .app {
    padding: calc(env(safe-area-inset-top) + 18px) 8px calc(env(safe-area-inset-bottom) + 112px);
  }
  body.in-call .videoGrid.count-2,
  body.in-call .videoGrid.count-3,
  body.in-call .videoGrid.count-4 {
    grid-template-columns: 1fr;
  }
  body.in-call .videoGrid.count-2 { grid-template-rows: repeat(2, minmax(0, 1fr)); }
  body.in-call .videoGrid.count-3 { grid-template-rows: repeat(3, minmax(0, 1fr)); }
  body.in-call .videoGrid.count-4 { grid-template-rows: repeat(4, minmax(0, 1fr)); }
  body.in-call .controls {
    left: 8px;
    right: 8px;
    bottom: calc(env(safe-area-inset-bottom) + 14px);
  }
}

/* Polished call controls */
.control-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  border-radius: 999px;
  background: linear-gradient(180deg, rgba(255,255,255,.18), rgba(255,255,255,.08));
  border: 1px solid rgba(255,255,255,.18);
  box-shadow: inset 0 1px 0 rgba(255,255,255,.12), 0 8px 24px rgba(0,0,0,.22);
}
.control-btn.active {
  background: linear-gradient(135deg, rgba(34,197,94,.95), rgba(6,182,212,.9));
}
.control-btn.danger {
  background: linear-gradient(135deg, #ef4444, #f97316);
}
.control-btn .icon { font-size: 1.08rem; line-height: 1; }
.control-btn .label { font-weight: 850; }
@media (max-width: 560px) {
  .control-btn { flex-direction: column; gap: 3px; }
  .control-btn .icon { font-size: 1.15rem; }
  .control-btn .label { font-size: 11px; }
}

/* Chrome/iPad/desktop strict single-screen tiling */
html, body.in-call {
  width: 100%;
  height: 100%;
  overflow: hidden;
}
body.in-call .app {
  box-sizing: border-box;
  width: 100vw;
  height: 100vh;
  max-height: 100vh;
  margin: 0;
  padding: 8px 8px 78px;
  gap: 0;
}
body.in-call .callArea,
body.in-call .videoGrid,
body.in-call .tile {
  min-width: 0;
  min-height: 0;
}
body.in-call .videoGrid {
  display: grid;
  width: 100%;
  height: 100%;
  gap: 8px;
  overflow: hidden;
}
body.in-call .videoGrid.count-1 {
  grid-template: minmax(0, 1fr) / minmax(0, 1fr);
}
body.in-call .videoGrid.count-2 {
  grid-template: minmax(0, 1fr) / repeat(2, minmax(0, 1fr));
}
body.in-call .videoGrid.count-3,
body.in-call .videoGrid.count-4 {
  grid-template: repeat(2, minmax(0, 1fr)) / repeat(2, minmax(0, 1fr));
}
body.in-call .tile {
  width: 100%;
  height: 100%;
  max-width: 100%;
  max-height: 100%;
}
body.in-call .tile video {
  width: 100%;
  height: 100%;
  object-fit: contain;
}
body.in-call .controls {
  max-height: 68px;
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: center;
  gap: 10px;
}
body.in-call .control-buttons {
  flex-wrap: nowrap;
}
body.in-call .control-buttons button {
  min-width: 96px;
  min-height: 38px;
}

@supports (height: 100dvh) {
  body.in-call .app {
    height: 100dvh;
    max-height: 100dvh;
  }
}

/* Only phones that are genuinely narrow stack vertically. iPad/Chrome split-screen keeps grid. */
@media (max-width: 480px) and (orientation: portrait) {
  body.in-call .app {
    padding: calc(env(safe-area-inset-top) + 12px) 6px calc(env(safe-area-inset-bottom) + 96px);
  }
  body.in-call .videoGrid.count-2 {
    grid-template: repeat(2, minmax(0, 1fr)) / minmax(0, 1fr);
  }
  body.in-call .videoGrid.count-3 {
    grid-template: repeat(3, minmax(0, 1fr)) / minmax(0, 1fr);
  }
  body.in-call .videoGrid.count-4 {
    grid-template: repeat(4, minmax(0, 1fr)) / minmax(0, 1fr);
  }
  body.in-call .controls {
    grid-template-columns: 1fr;
    max-height: 92px;
  }
  body.in-call .control-buttons button {
    min-width: 0;
  }
}


.hidden { display: none !important; }
.host-only { background: linear-gradient(135deg, #dc2626, #7f1d1d) !important; }
