/* Tiles wrap onto new lines and every line, an incomplete last one included, is centered. */
#grid {
  flex: 1;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  align-content: flex-start;
  align-items: flex-start;
  gap: 16px;
  padding: 14px 20px 120px;
}

.tile {
  flex: none;
  width: min(100%, 560px);
  position: relative;
  margin: 0;
  aspect-ratio: 4 / 3;
  overflow: hidden;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: #000;
  box-shadow: var(--shadow);
  animation: pop .35s ease both;
}

.tile video,
.tile canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.tile.local {
  border-color: color-mix(in srgb, var(--accent) 65%, transparent);
}

#local {
  transform: scaleX(-1);
}

/* Joining or leaving a call with others changes the size of the tile, and it eases between the two.
   It is a size container all the time (see the badge below), so nothing jumps when the state changes. */
.tile.local {
  container-type: inline-size;
  transition: width .35s ease, margin .35s ease;
}

/* Alone on its line: the side margins fill the rest of the line. */
body.has-peers .tile.local {
  --local-w: 240px;
  width: var(--local-w);
  margin: 0 calc((100% - var(--local-w)) / 2);
  position: sticky;
  top: 8px;
  z-index: 5;
}

/* The no-camera badge keeps its proportion to the tile: it is 92 px in a full-size (560 px) tile, so
   in the small tile of a call with others it shrinks with the tile's width (container query units;
   the fixed sizes of `.avatar` stay as the fallback for browsers without them). */
body.has-peers .tile.local .avatar {
  width: 16.4cqw;
  height: 16.4cqw;
  font-size: 7.1cqw;
  box-shadow: 0 2.1cqw 6cqw rgba(109, 106, 247, .45);
}

body.has-peers .tile.local .avatar.named {
  font-size: 6cqw;
  box-shadow: inset 0 0 0 .4cqw color-mix(in srgb, var(--who) 38%, transparent), 0 2.1cqw 6cqw color-mix(in srgb, var(--who) 30%, transparent);
}

body.has-peers .tile.local .avatar::after {
  height: .9cqw;
  border-radius: .5cqw;
  box-shadow: 0 0 0 .4cqw var(--bg), 0 0 1.8cqw rgba(239, 68, 68, .55);
}

/* The badge eases with the tile, both ways. Without this it would jump to its full size the moment
   the last guest leaves. */
.tile.local .avatar {
  transition: width .35s ease, height .35s ease, font-size .35s ease, box-shadow .35s ease;
}

.tile.local .avatar::after {
  transition: height .35s ease, border-radius .35s ease, box-shadow .35s ease;
}

.tag {
  position: absolute;
  max-width: calc(100% - 20px);
  padding: 4px 11px;
  font-size: 12px;
  line-height: 1.5;
  color: #fff;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  border-radius: 999px;
  background: var(--chip);
  backdrop-filter: blur(10px);
}

.name {
  left: 10px;
  bottom: 10px;
  font-weight: 500;
}

.stat {
  right: 10px;
  top: 10px;
  font-variant-numeric: tabular-nums;
  opacity: .85;
}

.stat.warn {
  opacity: 1;
  color: #fecaca;
  background: rgba(127, 29, 29, .72);
}

.stat:empty {
  display: none;
}

.nocam {
  position: absolute;
  inset: 0;
  display: none;
  place-items: center;
  /* --level (0 to 1) is set by ui/levels.js from the loudness of the sound; at 0 this is the quiet
     glow, at 1 the glow is about three times as strong and reaches half as far again. */
  background:
    radial-gradient(circle at 50% 40%, rgba(109, 106, 247, calc(.22 + var(--level, 0) * .5)), transparent calc(60% + var(--level, 0) * 30%)),
    #0e1018;
}

.nocam.show {
  display: grid;
}

.avatar {
  position: relative;
  display: grid;
  place-items: center;
  width: 92px;
  height: 92px;
  font-size: 40px;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  box-shadow: 0 12px 34px rgba(109, 106, 247, .45);
}

/* With a name: the initials in the person's color (--who) on a dark circle with a hint of it, and no
   strike-through. */
.avatar.named {
  font-size: 34px;
  font-weight: 600;
  letter-spacing: .02em;
  color: var(--who);
  background: color-mix(in srgb, var(--who) 16%, #10121a);
  box-shadow: inset 0 0 0 2px color-mix(in srgb, var(--who) 38%, transparent), 0 12px 34px color-mix(in srgb, var(--who) 30%, transparent);
}

.avatar.named::after {
  display: none;
}

@keyframes pop {
  from {
    opacity: 0;
    transform: scale(.96);
  }
}

@media (max-width: 520px) {
  body.has-peers .tile.local {
    --local-w: 168px;
  }

  #grid {
    padding: 14px 12px 110px;
    gap: 12px;
  }

  body.dock-up #grid {
    padding-bottom: calc(110px + var(--bar-h, 30px));
  }
}
