:root {
  --bg: #0f1115;
  --panel: #1a1d24;
  --panel-2: #232831;
  --panel-3: #2c323d;
  --text: #f1f3f7;
  --muted: #9ca3af;
  --primary: #3b82f6;
  --primary-hover: #2563eb;
  --danger: #dc2626;
  --danger-hover: #b91c1c;
  --border: #2d333d;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

#app {
  height: 100vh;
  display: flex;
  flex-direction: column;
}

.hidden { display: none !important; }

/* Lobby */
.lobby {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}
.lobby-card {
  width: 100%;
  max-width: 420px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 28px;
}
.lobby h1 { margin: 0 0 6px; font-size: 26px; }
.subtitle { margin: 0 0 22px; color: var(--muted); font-size: 14px; }

#lobby-form { display: flex; flex-direction: column; gap: 16px; }
.field { display: flex; flex-direction: column; gap: 6px; font-size: 14px; }
.field-label { font-weight: 500; }
.field input {
  padding: 10px 12px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--panel-2);
  color: var(--text);
  font-size: 16px;
  width: 100%;
}
.field input:focus {
  outline: 2px solid var(--primary);
  border-color: var(--primary);
}
.hint { color: var(--muted); font-size: 12px; }
.hint em { color: #cbd5e1; font-style: normal; font-weight: 600; }

.room-row { display: flex; gap: 8px; }
.room-row input { flex: 1; }

.primary {
  background: var(--primary);
  color: white;
  border: none;
  border-radius: 8px;
  padding: 12px 16px;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
}
.primary:hover { background: var(--primary-hover); }
.primary.block { width: 100%; }

.secondary {
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 14px;
  font-size: 14px;
  cursor: pointer;
}
.secondary:hover { background: var(--panel-3); }
.secondary.small { padding: 6px 10px; font-size: 13px; }

.privacy-notes {
  margin: 24px 0 0;
  padding: 0 0 0 18px;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.5;
}
.privacy-notes li { margin-bottom: 4px; }

/* Call */
.call {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
}
.call-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 16px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
}
.room-info {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 13px;
  color: var(--muted);
}
.room-info code {
  background: var(--panel-2);
  padding: 4px 8px;
  border-radius: 6px;
  color: var(--text);
  font-size: 13px;
  word-break: break-all;
}

.video-grid {
  flex: 1;
  display: grid;
  gap: 8px;
  padding: 8px;
  grid-template-columns: 1fr;
  grid-auto-rows: 1fr;
  min-height: 0;
}
/* Pick a column count based on tile count so tiles fill the available
   space instead of clustering in a single row with a wall of dead space
   below. Cascade: each rule below overrides the previous when matched. */
.video-grid:has(.tile:nth-child(2)) {
  grid-template-columns: repeat(2, 1fr);
}
.video-grid:has(.tile:nth-child(5)) {
  grid-template-columns: repeat(3, 1fr);
}
.video-grid:has(.tile:nth-child(7)) {
  grid-template-columns: repeat(4, 1fr);
}
/* With exactly 2 tiles in portrait orientation, stack them vertically so
   each tile gets the full width — side-by-side here makes both tiles too
   narrow. Landscape gets the 2×1 row from the rule above. */
.video-grid:has(.tile:nth-child(2):last-child) {
  grid-template-columns: 1fr;
}
/* With exactly 3 tiles: in portrait the third tile spans both 2-grid
   columns so it sits centered across the bottom (otherwise three 1/3-width
   strips are too narrow for faces). In landscape we switch to a uniform
   1×3 row, matching Zoom/Meet/Teams convention. */
.video-grid:has(.tile:nth-child(3):last-child) .tile:nth-child(3) {
  grid-column: span 2;
}
@media (orientation: landscape) {
  .video-grid:has(.tile:nth-child(2):last-child) {
    grid-template-columns: repeat(2, 1fr);
  }
  .video-grid:has(.tile:nth-child(3):last-child) {
    grid-template-columns: repeat(3, 1fr);
  }
  .video-grid:has(.tile:nth-child(3):last-child) .tile:nth-child(3) {
    grid-column: auto;
  }
}

/* Compact / spotlight layout: self-tile floats as a small PIP in the
   bottom-right corner; remote tiles fill the grid. Only applies when at
   least one remote is present (alone falls back to the regular layout). */
.video-grid.layout-spotlight {
  position: relative;
}
.video-grid.layout-spotlight:has(.tile:nth-child(2)) .tile.local {
  position: absolute;
  bottom: 12px;
  right: 12px;
  width: clamp(140px, 25%, 220px);
  aspect-ratio: 4 / 3;
  z-index: 10;
  border: 2px solid var(--border);
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.45);
  cursor: grab;
  touch-action: none;
  transition: top 0.18s ease, left 0.18s ease, right 0.18s ease, bottom 0.18s ease;
}
/* Corner the user dragged the PIP into. Each rule explicitly resets all four
   sides so switching corners is unambiguous; the default state with no class
   uses the bottom-right values from the rule above. */
.video-grid.layout-spotlight .tile.local.pip-tl {
  top: 12px; right: auto; bottom: auto; left: 12px;
}
.video-grid.layout-spotlight .tile.local.pip-tr {
  top: 12px; right: 12px; bottom: auto; left: auto;
}
.video-grid.layout-spotlight .tile.local.pip-bl {
  top: auto; right: auto; bottom: 12px; left: 12px;
}
.video-grid.layout-spotlight .tile.local.pip-br {
  top: auto; right: 12px; bottom: 12px; left: auto;
}
.video-grid.layout-spotlight .tile.local.dragging {
  transition: none;
  cursor: grabbing;
}
/* With self out of flow, the in-flow tiles are remotes only — recompute the
   column layout for each total count. Mapping: spotlight with M total tiles
   reproduces the meeting layout for M-1 tiles. */

/* M=2 (1 remote): single column — remote fills the grid. */
.video-grid.layout-spotlight:has(.tile:nth-child(2):last-child) {
  grid-template-columns: 1fr;
}
/* M=3 (2 remotes): portrait stacks 2 in 1 col; landscape goes 2 cols. */
.video-grid.layout-spotlight:has(.tile:nth-child(3):last-child) {
  grid-template-columns: 1fr;
}
.video-grid.layout-spotlight:has(.tile:nth-child(3):last-child) .tile:nth-child(3) {
  grid-column: auto;
}
/* M=4 (3 remotes): portrait does 2 cols with the 3rd remote spanning both
   at the bottom; landscape goes 1×3. */
.video-grid.layout-spotlight:has(.tile:nth-child(4):last-child) {
  grid-template-columns: repeat(2, 1fr);
}
.video-grid.layout-spotlight:has(.tile:nth-child(4):last-child) .tile:nth-child(4) {
  grid-column: span 2;
}
/* M=5 (4 remotes): 2 cols × 2 rows. */
.video-grid.layout-spotlight:has(.tile:nth-child(5):last-child) {
  grid-template-columns: repeat(2, 1fr);
}
/* M=6,7 (5,6 remotes): 3 cols × 2 rows. */
.video-grid.layout-spotlight:has(.tile:nth-child(6):last-child),
.video-grid.layout-spotlight:has(.tile:nth-child(7):last-child) {
  grid-template-columns: repeat(3, 1fr);
}
/* M=8 (7 remotes): 4 cols × 2 rows. */
.video-grid.layout-spotlight:has(.tile:nth-child(8):last-child) {
  grid-template-columns: repeat(4, 1fr);
}

@media (orientation: landscape) {
  .video-grid.layout-spotlight:has(.tile:nth-child(2):last-child) {
    grid-template-columns: 1fr;
  }
  .video-grid.layout-spotlight:has(.tile:nth-child(3):last-child) {
    grid-template-columns: repeat(2, 1fr);
  }
  .video-grid.layout-spotlight:has(.tile:nth-child(4):last-child) {
    grid-template-columns: repeat(3, 1fr);
  }
  .video-grid.layout-spotlight:has(.tile:nth-child(4):last-child) .tile:nth-child(4) {
    grid-column: auto;
  }
}
.tile {
  position: relative;
  background: var(--panel-2);
  border-radius: 12px;
  overflow: hidden;
  min-height: 0;
}
.tile video {
  width: 100%;
  height: 100%;
  object-fit: cover;
  background: black;
}
.tile.local video { transform: scaleX(-1); }
.tile .label {
  position: absolute;
  bottom: 8px;
  left: 8px;
  background: rgba(0, 0, 0, 0.55);
  padding: 4px 8px;
  border-radius: 6px;
  font-size: 13px;
  max-width: calc(100% - 16px);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* Controls */
.controls {
  display: flex;
  justify-content: center;
  align-items: center;
  flex-wrap: wrap;
  gap: 10px;
  padding: 12px;
  background: var(--panel);
  border-top: 1px solid var(--border);
}
.control {
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 12px 18px;
  font-size: 14px;
  cursor: pointer;
  flex: 0 1 auto;
  white-space: nowrap;
}
.control:hover { background: var(--panel-3); }
.control.off { background: var(--panel-2); color: var(--muted); }
.hangup {
  background: var(--danger);
  color: white;
  border-color: var(--danger);
  font-weight: 600;
}
.hangup:hover { background: var(--danger-hover); border-color: var(--danger-hover); }

/* Modal */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.65);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
  padding: 16px;
}
.modal-card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 24px;
  max-width: 400px;
  width: 100%;
  text-align: center;
}
.modal-card h2 { margin: 0 0 8px; font-size: 20px; }
.modal-card p { margin: 0 0 20px; color: var(--muted); }
.modal-actions {
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.hangup-text {
  background: transparent;
  color: var(--danger);
  border: none;
  padding: 10px;
  font-size: 14px;
  cursor: pointer;
  border-radius: 8px;
}
.hangup-text:hover {
  background: rgba(220, 38, 38, 0.12);
  color: #fca5a5;
}

/* Toast */
.toast {
  position: fixed;
  bottom: 96px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--panel-2);
  border: 1px solid var(--border);
  padding: 10px 16px;
  border-radius: 999px;
  font-size: 14px;
  z-index: 60;
  max-width: calc(100% - 32px);
  text-align: center;
}
