/* ── Call Overlay ────────────────────────────────────────────────────── */
#call-overlay {
  position: fixed; inset: 0; z-index: 500;
  background: #0d0e0f;
  display: flex; flex-direction: column;
  animation: fadeIn .2s ease;
}

@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }

#call-overlay.hidden { display: none !important; }

/* ── Video grid ──────────────────────────────────────────────────────── */
#call-video-grid {
  flex: 1; display: grid; gap: 8px; padding: 16px;
  overflow: hidden; align-items: center; justify-items: center;
}

/* Dynamic grid columns based on participant count */
#call-video-grid.p1  { grid-template-columns: 1fr; }
#call-video-grid.p2  { grid-template-columns: 1fr 1fr; }
#call-video-grid.p3,
#call-video-grid.p4  { grid-template-columns: 1fr 1fr; }
#call-video-grid.p5,
#call-video-grid.p6  { grid-template-columns: 1fr 1fr 1fr; }

/* ── Video tile ──────────────────────────────────────────────────────── */
.video-tile {
  position: relative; width: 100%; height: 100%;
  max-height: calc(100vh - 140px);
  background: #1a1d21; border-radius: 12px; overflow: hidden;
  display: flex; align-items: center; justify-content: center;
}

.video-tile video {
  width: 100%; height: 100%; object-fit: cover;
  transform: scaleX(-1); /* mirror local video */
}

.video-tile.remote video { transform: none; }

.video-tile canvas {
  width: 100%; height: 100%; object-fit: cover;
  /* No transform — previewCanvas already mirrors only the person internally */
}

/* Avatar fallback when camera is off */
.tile-avatar-fallback {
  display: flex; flex-direction: column; align-items: center; gap: 12px;
}
.tile-avatar {
  width: 80px; height: 80px; border-radius: 20px;
  display: flex; align-items: center; justify-content: center;
  font-size: 28px; font-weight: 700; color: #fff;
}
.tile-avatar img { width: 100%; height: 100%; object-fit: cover; border-radius: 20px; }

/* Name label */
.tile-name {
  position: absolute; bottom: 10px; left: 12px;
  background: rgba(0,0,0,.6); color: #fff;
  font-size: 13px; font-weight: 600; padding: 3px 8px;
  border-radius: 6px; backdrop-filter: blur(4px);
  display: flex; align-items: center; gap: 6px;
}

.tile-muted-icon {
  font-size: 12px; opacity: .9;
}

/* Speaking indicator */
.video-tile.speaking {
  box-shadow: 0 0 0 3px #cc0066;
}

/* ── Controls bar ────────────────────────────────────────────────────── */
#call-controls {
  height: 80px; background: rgba(0,0,0,.8);
  display: flex; align-items: center; justify-content: center;
  gap: 12px; flex-shrink: 0; backdrop-filter: blur(8px);
}

.call-btn {
  width: 48px; height: 48px; border-radius: 50%;
  border: none; cursor: pointer; font-size: 20px;
  display: flex; align-items: center; justify-content: center;
  transition: background .15s, transform .1s;
  background: rgba(255,255,255,.12); color: #fff;
}
.call-btn:hover { background: rgba(255,255,255,.2); }
.call-btn:active { transform: scale(.92); }

.call-btn.active {
  background: rgba(255,255,255,.9);
  filter: grayscale(1); /* emoji looks muted */
}
.call-btn.off {
  background: rgba(224,30,90,.3);
  position: relative;
}
.call-btn.off::after {
  content: '';
  position: absolute; inset: 0; border-radius: 50%;
  background: linear-gradient(135deg, transparent 45%, rgba(224,30,90,.8) 45%, rgba(224,30,90,.8) 55%, transparent 55%);
}

/* Screen-sharing active — green glow */
.call-btn.sharing {
  background: rgba(46, 182, 125, .35);
  box-shadow: 0 0 0 2px #2eb67d;
}
.call-btn.sharing:hover { background: rgba(46, 182, 125, .5); }

/* Any screen-sharing tile (local OR remote): show the whole frame, never crop.
   Without this the receiver gets object-fit: cover which crops the screen edges. */
.video-tile.screen-sharing video {
  transform: none;
  object-fit: contain;
  background: #000;  /* letterbox bars when aspect doesn't match the tile */
}

/* ── Spotlight layout ──────────────────────────────────────────────────
   Activated by JS adding .has-spotlight when any tile has .screen-sharing.
   The screen tile takes the main area; everything else stacks in a sidebar
   on the right, sized by the --spotlight-sidebar CSS var (drag-resizable). */
#call-video-grid.has-spotlight {
  position: relative;
  grid-template-columns: 1fr var(--spotlight-sidebar, 240px);
  /* Explicit rows so `grid-row: 1 / -1` on the screen tile spans the full
     column 1 height. Falls back to a single 1fr row before JS sets the var. */
  grid-template-rows: var(--spotlight-rows, 1fr);
  grid-auto-rows: minmax(120px, 1fr);
  grid-auto-flow: row;
}
#call-video-grid.has-spotlight .video-tile.screen-sharing {
  grid-column: 1;
  grid-row: 1 / -1;          /* span every row in column 1 */
  max-height: none;          /* override the default cap so it fills the area */
}
#call-video-grid.has-spotlight .video-tile:not(.screen-sharing) {
  grid-column: 2;
  max-height: none;
}

/* Draggable divider between main and sidebar. Invisible by default, highlights
   on hover/drag. Positioned with right offset matching the sidebar width. */
.spotlight-divider {
  position: absolute;
  top: 16px; bottom: 16px;
  right: calc(var(--spotlight-sidebar, 240px) + 4px);
  width: 8px;
  cursor: col-resize;
  background: transparent;
  border-radius: 4px;
  z-index: 10;
  transition: background .15s;
}
.spotlight-divider:hover,
.spotlight-divider:active {
  background: rgba(255,255,255,.15);
}
.spotlight-divider::before {
  content: '';
  position: absolute; left: 50%; top: 50%;
  transform: translate(-50%, -50%);
  width: 2px; height: 32px;
  background: rgba(255,255,255,.3);
  border-radius: 1px;
}

/* On narrow viewports (mobile portrait), flip to a stacked layout: screen
   on top, other tiles in a row below. Drag is disabled because the divider
   isn't shown. */
@media (max-width: 700px) {
  #call-video-grid.has-spotlight {
    grid-template-columns: 1fr;
    grid-template-rows: 1fr var(--spotlight-sidebar, 120px);
  }
  #call-video-grid.has-spotlight .video-tile.screen-sharing {
    grid-row: 1;
    grid-column: 1 / -1;
  }
  #call-video-grid.has-spotlight .video-tile:not(.screen-sharing) {
    grid-row: 2;
    grid-column: auto;
  }
  .spotlight-divider { display: none; }
}

.call-btn.end-call {
  background: #e01e5a; width: 56px; height: 56px; font-size: 22px;
  transform: rotate(135deg);
}
.call-btn.end-call:hover { background: #c0163f; }
.call-btn.end-call:active { transform: rotate(135deg) scale(.92); }

.call-btn-label {
  font-size: 11px; color: rgba(255,255,255,.6);
  text-align: center; margin-top: 4px;
}

.call-btn-wrap {
  display: flex; flex-direction: column; align-items: center;
}
.call-btn-wrap.hidden { display: none !important; }

/* ── Incoming call notification ──────────────────────────────────────── */
#incoming-call {
  position: fixed; bottom: 24px; right: 24px; z-index: 600;
  background: #2c2d31; border: 1px solid #373a3f;
  border-radius: 16px; padding: 20px 24px; width: 300px;
  box-shadow: 0 8px 32px rgba(0,0,0,.6);
  animation: slideIn .3s ease;
}

@keyframes slideIn {
  from { transform: translateX(120%); opacity: 0; }
  to   { transform: translateX(0);    opacity: 1; }
}

.incoming-call-header {
  display: flex; align-items: center; gap: 12px; margin-bottom: 16px;
}
.incoming-avatar {
  width: 44px; height: 44px; border-radius: 12px;
  display: flex; align-items: center; justify-content: center;
  font-size: 16px; font-weight: 700; color: #fff; overflow: hidden; flex-shrink: 0;
}
.incoming-avatar img { width: 100%; height: 100%; object-fit: cover; border-radius: 12px; }
.incoming-call-info { flex: 1; }
.incoming-call-name { font-size: 15px; font-weight: 700; color: #f2f3f5; }
.incoming-call-sub  { font-size: 12px; color: #9ea2a8; margin-top: 2px; }

.incoming-call-pulse {
  width: 10px; height: 10px; border-radius: 50%;
  background: #cc0066; animation: pulse 1.2s infinite;
}
@keyframes pulse {
  0%,100% { transform: scale(1); opacity: 1; }
  50%      { transform: scale(1.4); opacity: .6; }
}

.incoming-call-actions { display: flex; gap: 10px; }
.btn-accept {
  flex: 1; padding: 10px; border-radius: 10px;
  background: #cc0066; color: #fff; border: none;
  font-size: 14px; font-weight: 600; cursor: pointer;
  transition: background .15s;
}
.btn-accept:hover { background: #aa0055; }
.btn-reject {
  flex: 1; padding: 10px; border-radius: 10px;
  background: #e01e5a; color: #fff; border: none;
  font-size: 14px; font-weight: 600; cursor: pointer;
  transition: background .15s;
}
.btn-reject:hover { background: #c0163f; }

/* ── Channel call join banner stack ──────────────────────────────────── */
/* Container holds zero or more .call-banner items, one per active call in  */
/* a channel the user belongs to. Hidden when empty.                        */
#channel-call-banner {
  display: flex; flex-direction: column;
  flex-shrink: 0;   /* don't let the flex column compress it */
}
#channel-call-banner:empty { display: none; }

.call-banner {
  /* Sits inside #main (which is position:relative) so it only spans
     the chat column, not the full app width. */
  background: linear-gradient(90deg, #cc0066, #aa0044);
  padding: 10px 20px;
  display: flex; align-items: center; justify-content: space-between;
  gap: 12px;
  animation: slideDown .25s ease;
  flex-shrink: 0;
}
.call-banner + .call-banner {
  border-top: 1px solid rgba(255,255,255,.2);
}
@keyframes slideDown {
  from { transform: translateY(-100%); }
  to   { transform: translateY(0); }
}
.banner-text { font-size: 14px; font-weight: 600; color: #fff; }
.banner-sub  { font-size: 12px; color: rgba(255,255,255,.75); margin-top: 1px; }
.banner-actions {
  display: flex; align-items: center; gap: 8px; flex-shrink: 0;
}
.btn-join-call {
  padding: 8px 18px; border-radius: 8px;
  background: rgba(255,255,255,.2); color: #fff;
  border: 1.5px solid rgba(255,255,255,.4);
  font-size: 13px; font-weight: 600; cursor: pointer;
  transition: background .15s;
}
.btn-join-call:hover { background: rgba(255,255,255,.3); }
.btn-dismiss-banner {
  background: transparent; border: none;
  color: rgba(255,255,255,.7); font-size: 16px; line-height: 1;
  cursor: pointer; padding: 4px 8px; border-radius: 6px;
  transition: color .15s, background .15s;
}
.btn-dismiss-banner:hover { color: #fff; background: rgba(255,255,255,.15); }

/* ── Background picker ───────────────────────────────────────────────── */
#bg-picker {
  position: fixed; bottom: 100px; left: 50%; transform: translateX(-50%);
  z-index: 550; background: #2c2d31; border: 1px solid #373a3f;
  border-radius: 16px; padding: 16px; width: 340px;
  box-shadow: 0 8px 32px rgba(0,0,0,.6);
}
.bg-picker-title {
  font-size: 13px; font-weight: 700; color: #9ea2a8;
  text-transform: uppercase; letter-spacing: .6px; margin-bottom: 12px;
}
.bg-picker-grid {
  display: grid; grid-template-columns: repeat(5, 1fr); gap: 8px;
  margin-bottom: 12px;
}
.bg-option {
  aspect-ratio: 16/9; border-radius: 8px; cursor: pointer;
  border: 2px solid transparent; transition: border-color .15s, transform .1s;
  overflow: hidden; position: relative;
}
.bg-option:hover { transform: scale(1.05); }
.bg-option.selected { border-color: #cc0066; }
.bg-option.selected::after {
  content: '✓'; position: absolute; bottom: 2px; right: 4px;
  color: #fff; font-size: 10px; font-weight: 700;
  text-shadow: 0 1px 3px rgba(0,0,0,.8);
}
.bg-option img { width: 100%; height: 100%; object-fit: cover; }
.bg-option-none {
  background: #1a1d21; display: flex; align-items: center; justify-content: center;
  font-size: 18px;
}
.bg-option-blur {
  background: linear-gradient(135deg, #667eea, #764ba2);
  display: flex; align-items: center; justify-content: center;
  font-size: 14px; color: rgba(255,255,255,.8); font-weight: 600;
  filter: blur(1px);
}
.bg-upload-btn {
  width: 100%; padding: 9px; border-radius: 8px;
  background: transparent; border: 1.5px dashed #565859;
  color: #9ea2a8; font-size: 13px; cursor: pointer;
  transition: border-color .15s, color .15s;
}
.bg-upload-btn:hover { border-color: #cc0066; color: #f2f3f5; }

/* ── Minimised call overlay ──────────────────────────────────────────── */
/* Collapses to a slim bar at the bottom so users can keep chatting. */
#call-overlay.minimised {
  /* Dock over the CENTRE column only (the chat / #main track), not the whole
     viewport — so the left sidebar (admin menu / nav) and the right panel stay
     clickable during a call. The rails are the same vars the #app grid uses,
     so the bar always tracks the columns. Mobile resets these to 0 below. */
  top: auto; bottom: 0;
  left: var(--rail-l); right: var(--rail-r);
  height: auto;               /* shrink to fit the controls bar               */
  background: rgba(13,14,15,.97);
  backdrop-filter: blur(16px);
  border-top: 1px solid rgba(255,255,255,.1);
  border-radius: 12px 12px 0 0; /* docked-huddle look, top corners only        */
  box-shadow: 0 -4px 24px rgba(0,0,0,.6);
  animation: none;            /* skip the fadeIn when restoring               */
}

/* Narrow desktop windows: the centre column gets tight for six controls plus
   the label — drop the (non-essential) label first so the buttons never clip.
   Above the mobile breakpoint only; on mobile the bar is full-width with room. */
@media (min-width: 769px) and (max-width: 1040px) {
  #call-overlay.minimised .call-minimised-label { display: none; }
}

#call-overlay.minimised #call-video-grid { display: none; }

/* Tighten the control bar height when minimised */
#call-overlay.minimised #call-controls {
  height: 64px;
  min-height: unset;
}

/* "Call in progress" text — hidden by default, shown when minimised */
.call-minimised-label {
  display: none;
  color: rgba(255,255,255,.75);
  font-size: 13px;
  font-weight: 600;
  white-space: nowrap;
  margin-right: 8px;
}
#call-overlay.minimised .call-minimised-label { display: block; }

/* Mobile: layout collapses to a single column (#app is display:block), so
   there's no side rail to dodge — span full width again and respect the home
   indicator. */
@media (max-width: 768px) {
  #call-overlay.minimised {
    left: 0; right: 0;
    border-radius: 0;
  }
  #call-overlay.minimised #call-controls {
    padding-bottom: max(10px, env(safe-area-inset-bottom));
    height: auto;
  }
}

/* ── Lift #main when the call bar is minimised ───────────────────────── */
/* The minimised bar is position:fixed at the bottom — without this     */
/* padding it covers the chat input bar. Shrinking the flex content     */
/* area by the bar height pushes #input-bar above the fixed overlay.    */
body.call-minimised #main {
  padding-bottom: 64px;    /* matches #call-overlay.minimised height    */
}
@media (max-width: 768px) {
  body.call-minimised #main {
    /* Mobile bar grows for home-indicator safe area */
    padding-bottom: max(74px, calc(64px + env(safe-area-inset-bottom)));
  }
}

/* ── Call button in header ───────────────────────────────────────────── */
.call-start-btn {
  display: flex; align-items: center; gap: 6px;
  padding: 5px 12px; border-radius: 6px;
  background: transparent; border: 1.5px solid #373a3f;
  color: #9ea2a8; font-size: 13px; font-weight: 600;
  cursor: pointer; transition: background .15s, color .15s, border-color .15s;
}
.call-start-btn:hover { background: rgba(204,0,102,.15); border-color: #cc0066; color: #cc0066; }
.call-start-btn.call-active {
  background: rgba(43,172,118,.15); border-color: #2bac76; color: #2bac76;
}
.call-start-btn.call-active:hover {
  background: rgba(43,172,118,.25); border-color: #2bac76; color: #2bac76;
}
.call-start-btn .call-icon { width: 16px; height: 16px; flex-shrink: 0; }
.call-start-btn .call-btn-label-text { font-size: 13px; }

/* ══════════════════════════════════════════════════════════════════════
   Call overlay uses dynamic viewport height so it always fills the screen
   on mobile even when browser chrome appears/disappears.
   ══════════════════════════════════════════════════════════════════════ */
#call-overlay {
  height: 100vh;
  height: 100dvh;
  height: -webkit-fill-available;
}

/* ══════════════════════════════════════════════════════════════════════
   MOBILE CALL LAYOUT (≤ 768px)
   ══════════════════════════════════════════════════════════════════════ */
@media (max-width: 768px) {

  /* Account for notch / status bar at top, home indicator at bottom */
  #call-overlay {
    padding-top: env(safe-area-inset-top);
  }

  #call-video-grid {
    padding: 8px;
    gap: 6px;
  }

  /* Portrait: stack 1 col for 1-2 people, 2 cols for 3-6.
     Landscape (overridden below) gets a roomier layout. */
  #call-video-grid.p1,
  #call-video-grid.p2 { grid-template-columns: 1fr; }
  #call-video-grid.p3,
  #call-video-grid.p4,
  #call-video-grid.p5,
  #call-video-grid.p6 { grid-template-columns: 1fr 1fr; }

  .video-tile {
    max-height: none;           /* let grid manage sizing */
    border-radius: 10px;
  }
  .tile-name {
    font-size: 12px;
    padding: 3px 8px;
  }
  .tile-avatar { width: 64px; height: 64px; font-size: 22px; }

  /* Controls bar: bigger touch targets, room for the home indicator */
  #call-controls {
    height: auto;
    min-height: 86px;
    gap: 10px;
    padding: 10px 8px;
    padding-bottom: max(10px, env(safe-area-inset-bottom));
  }
  .call-btn {
    width: 56px; height: 56px; font-size: 22px;
    -webkit-tap-highlight-color: transparent;
  }
  .call-btn:active { transform: scale(.92); }
  .call-btn.end-call {
    width: 64px; height: 64px; font-size: 24px;
  }
  .call-btn-label {
    font-size: 10px; margin-top: 3px;
  }

  /* Incoming call card: full-width bottom sheet */
  #incoming-call {
    right: 12px; left: 12px;
    bottom: max(12px, env(safe-area-inset-bottom));
    width: auto; padding: 16px;
  }
  .incoming-call-actions { gap: 10px; }
  .btn-accept, .btn-reject {
    padding: 14px; font-size: 16px;
    min-height: 48px;
  }

  /* Channel call banner — slim, doesn't crowd the screen */
  .call-banner {
    padding: 10px 12px; gap: 10px;
    flex-wrap: wrap;
  }
  .banner-text { font-size: 13px; line-height: 1.3; }
  .banner-sub  { font-size: 11px; }
  .btn-join-call {
    padding: 9px 16px; font-size: 13px;
    min-height: 40px;
  }
  .btn-dismiss-banner {
    padding: 6px 10px; font-size: 18px;
    min-height: 40px;
  }

  /* Background picker as a centred bottom sheet */
  #bg-picker {
    bottom: max(108px, calc(86px + env(safe-area-inset-bottom)));
    left: 8px; right: 8px;
    transform: none;
    width: auto;
    max-width: none;
  }
  .bg-picker-grid { grid-template-columns: repeat(4, 1fr); gap: 8px; }
}

/* ══════════════════════════════════════════════════════════════════════
   Landscape phones in a call — single row of tiles when possible,
   shorter control bar so video gets more space
   ══════════════════════════════════════════════════════════════════════ */
@media (max-width: 900px) and (max-height: 500px) and (orientation: landscape) {
  #call-video-grid.p1 { grid-template-columns: 1fr; }
  #call-video-grid.p2 { grid-template-columns: 1fr 1fr; }
  #call-video-grid.p3 { grid-template-columns: repeat(3, 1fr); }
  #call-video-grid.p4,
  #call-video-grid.p5,
  #call-video-grid.p6 { grid-template-columns: repeat(3, 1fr); }
  #call-controls { min-height: 64px; }
  .call-btn { width: 48px; height: 48px; font-size: 20px; }
  .call-btn.end-call { width: 56px; height: 56px; }
}

/* Very small phones */
@media (max-width: 380px) {
  .call-btn { width: 50px; height: 50px; font-size: 20px; }
  .call-btn.end-call { width: 58px; height: 58px; }
  #call-controls { gap: 6px; }
  .bg-picker-grid { grid-template-columns: repeat(3, 1fr); }
}

/* ── Start call button — icon-only on mobile ─────────────────────────── */
@media (max-width: 768px) {
  .call-start-btn {
    padding: 0;
    width: 44px; height: 44px;
    border: none;
    background: transparent;
    color: var(--text-primary);
    border-radius: 8px;
    font-size: 0;          /* hides "Start call" text */
    justify-content: center;
    -webkit-tap-highlight-color: transparent;
  }
  .call-start-btn .call-icon { width: 22px; height: 22px; }
  .call-start-btn .call-btn-label-text { display: none; } /* icon-only on mobile */
  .call-start-btn:hover { background: transparent; border: none; color: var(--text-primary); }
  .call-start-btn:active { background: var(--bg-hover); }
  /* Active call: solid green tint + green icon so the indicator is obvious */
  .call-start-btn.call-active {
    background: rgba(43,172,118,.22);
    color: #2bac76;
    border-radius: 8px;
  }
  .call-start-btn.call-active:hover { background: rgba(43,172,118,.32); color: #2bac76; }
  .call-start-btn.call-active:active { background: rgba(43,172,118,.40); }
}
