/* ── Design tokens ─────────────────────────────────────────── */
:root {
  --bg:          #1e1f22;
  --bg2:         #2b2d31;
  --bg3:         #313338;
  --accent:      #5865f2;
  --accent-dim:  #4752c4;
  --danger:      #ed4245;
  --danger-dim:  #c03537;
  --success:     #23a55a;
  --text:        #dbdee1;
  --text-muted:  #949ba4;
  --border:      #3f4147;
  --shadow:      rgba(0,0,0,.35);

  --sidebar-w:   220px;
  --chat-w:      280px;
  --bar-h:       72px;
  --radius:      8px;
  --radius-sm:   4px;

  --font: "Inter", system-ui, -apple-system, sans-serif;
  --transition: 0.18s ease;
}

/* ── Reset ─────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; }
body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}
button { cursor: pointer; border: none; background: none; font: inherit; color: inherit; }
input  { font: inherit; color: inherit; }
a { color: var(--accent); }

/* ═══════════════════════════════════════════════════════════ */
/*  JOIN PAGE                                                  */
/* ═══════════════════════════════════════════════════════════ */
.join-page {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  background: var(--bg);
}

.join-card {
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 40px 36px;
  width: 100%;
  max-width: 380px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
  box-shadow: 0 8px 32px var(--shadow);
}

.join-logo { margin-bottom: 4px; }
.join-title { font-size: 1.5rem; font-weight: 700; }
.join-subtitle { color: var(--text-muted); font-size: .9rem; margin-top: -8px; }

.join-form {
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.field-group {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.field-group label {
  font-size: .8rem;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: .05em;
}

.field-group input {
  background: var(--bg3);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  font-size: .95rem;
  outline: none;
  transition: border-color var(--transition);
}

.field-group input:focus {
  border-color: var(--accent);
}

.join-error {
  background: rgba(237,66,69,.15);
  border: 1px solid rgba(237,66,69,.4);
  border-radius: var(--radius-sm);
  color: #ff7b7d;
  font-size: .875rem;
  padding: 10px 12px;
}

/* ── Buttons ────────────────────────────────────────────────── */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 10px 18px;
  border-radius: var(--radius-sm);
  font-size: .95rem;
  font-weight: 600;
  transition: background var(--transition), opacity var(--transition);
}

.btn-primary  { background: var(--accent); color: #fff; }
.btn-primary:hover  { background: var(--accent-dim); }
.btn-primary:disabled { opacity: .5; cursor: not-allowed; }

.btn-secondary { background: var(--bg3); color: var(--text); border: 1px solid var(--border); }
.btn-secondary:hover { background: var(--border); }

.btn-full { width: 100%; }

/* ── Waiting status ─────────────────────────────────────────── */
.waiting-status {
  display: flex;
  align-items: center;
  gap: 12px;
  color: var(--text-muted);
  font-size: .9rem;
}

.waiting-spinner {
  width: 18px; height: 18px;
  border: 2px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.9s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }



/* ═══════════════════════════════════════════════════════════ */
/*  ROOM PAGE LAYOUT                                           */
/* ═══════════════════════════════════════════════════════════ */
.room-page {
  display: grid;
  grid-template-columns: 1fr;
  grid-template-rows: 1fr var(--bar-h);
  grid-template-areas:
    "video"
    "bar";
  height: 100vh;
  overflow: hidden;
}

.room-page.chat-open {
  grid-template-columns: 1fr var(--chat-w);
  grid-template-areas:
    "video  chat"
    "bar    chat";
}

/* ── Video tiles ─────────────────────────────────────────── */
.tiles-container {
  display: flex;
  align-items: stretch;
  gap: 24px;
  padding: 0;
  width: 100%;
}

.video-tile {
  flex: 1 1 0;
  min-width: 0;
  min-height: 0;
  position: relative;
  border-radius: 16px;
  overflow: hidden;
  background: var(--bg2);
  border: 2px solid var(--border);
  transition: border-color 0.35s ease, opacity 0.35s ease, transform 0.35s ease;
}

/* Local tile: mirror the camera */
.tile-local video { transform: scaleX(-1); }

/* Both tiles: video fills the tile */
.video-tile video {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

/* Screen share tiles: show full content with letterbox instead of cropping */
.video-tile.sharing-screen video,
.video-tile.tile-main video {
  object-fit: contain;
  background: #000;
}

/* Ensure hidden video elements leave no black artefact */
#localVideo, #remoteVideo {
  display: none;
  background: transparent;
}

/* Avatar overlay — shown when camera off or not connected */
.tile-avatar-overlay {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg2);
  z-index: 1;
}

/* Name label — always visible at tile bottom */
.tile-label {
  position: absolute;
  bottom: 0; left: 0; right: 0;
  padding: 20px 12px 10px;
  background: linear-gradient(transparent, rgba(0,0,0,.45));
  color: rgba(255,255,255,.65);
  font-size: .78rem;
  font-weight: 500;
  text-align: center;
  pointer-events: none;
  z-index: 2;
}

/* Waiting state: dimmed + dashed border */
.tile-waiting {
  opacity: 0.4;
  border: 2px dashed var(--border);
}

/* "Nobody here" text shown inside remote tile when friend is not connected */
.nobody-text {
  font-size: 16px;
  color: #949ba4;
  text-align: center;
  padding: 0 20px;
  pointer-events: none;
}

/* Darker bg for the waiting (nobody) state */
.tile-nobody,
.tile-nobody .tile-avatar-overlay {
  background: #1a1b1e;
}

/* Reveal animation: fade + scale up */
@keyframes tileReveal {
  from { opacity: 0.4; transform: scale(0.95); }
  to   { opacity: 1;   transform: scale(1);    }
}

.tile-connected {
  animation: tileReveal 0.4s cubic-bezier(0.34, 1.4, 0.64, 1) forwards;
}

/* Speaking indicator */
.video-tile.speaking {
  border: 2px solid #23a55a;
  box-shadow: 0 0 0 3px rgba(35, 165, 90, 0.35);
  transition: box-shadow 0.15s ease, border-color 0.15s ease;
}

/* Muted indicator — not shown while tile-waiting/tile-nobody (no peer connected yet).
   speaking wins if both classes are somehow present. */
.video-tile.muted-indicator:not(.tile-waiting):not(.tile-nobody) {
  border: 2px solid var(--danger);
  box-shadow: 0 0 0 3px rgba(237, 66, 69, 0.35);
  transition: box-shadow 0.15s ease, border-color 0.15s ease;
}
.video-tile.muted-indicator.speaking:not(.tile-waiting):not(.tile-nobody) {
  border-color: #23a55a;
  box-shadow: 0 0 0 3px rgba(35, 165, 90, 0.35);
}

/* ── Screen share layout ──────────────────────────────────── */
.tiles-container.screen-share-mode {
  gap: 0;
  padding: 16px;
  position: relative;
  justify-content: flex-start;
  align-items: stretch;
}

.video-tile.tile-main {
  flex: 1 1 auto;
  width: auto;
  height: 100%;
  border-radius: 12px;
}

.video-tile.tile-pip {
  position: absolute;
  bottom: 28px;
  right: 28px;
  width: 200px;
  height: 200px;
  border-radius: 12px;
  z-index: 5;
  box-shadow: 0 6px 24px rgba(0,0,0,.55);
  flex: none;
}

/* ── Video area ──────────────────────────────────────────── */
.video-area {
  grid-area: video;
  background: #111214;
  position: relative;
  overflow: hidden;
  display: flex;
  align-items: stretch;
}

.avatar-large {
  width: 88px; height: 88px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 2.2rem;
  font-weight: 700;
  color: #fff;
  flex-shrink: 0;
}

/* Screen share label */
.screen-share-label {
  position: absolute;
  top: 14px; left: 50%;
  transform: translateX(-50%);
  background: rgba(35,165,90,.85);
  color: #fff;
  font-size: .8rem;
  font-weight: 600;
  padding: 4px 12px;
  border-radius: 20px;
  display: flex;
  align-items: center;
  gap: 6px;
  z-index: 10;
}

.screen-share-label::before {
  content: "";
  width: 8px; height: 8px;
  background: #fff;
  border-radius: 50%;
  animation: pulse 1.5s ease infinite;
}

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: .3; }
}

/* Screen-share border: applied when local tile is in main/expanded mode */
.video-tile.tile-main { outline: 3px solid var(--success); outline-offset: -3px; }

/* ── Chat panel ──────────────────────────────────────────── */
.chat-panel {
  grid-area: chat;
  background: var(--bg2);
  border-left: 1px solid var(--border);
  display: none;
  flex-direction: column;
  height: 100%;
}

.room-page.chat-open .chat-panel { display: flex; }

.chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  font-weight: 700;
  font-size: .95rem;
  border-bottom: 1px solid var(--border);
  min-height: 52px;
}

.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  scrollbar-width: thin;
  scrollbar-color: var(--border) transparent;
}

.chat-message {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.chat-message .msg-header {
  display: flex;
  align-items: baseline;
  gap: 8px;
}

.chat-message .msg-author {
  font-size: .8rem;
  font-weight: 700;
  color: var(--accent);
}

.chat-message.self .msg-author { color: var(--success); }

.chat-message .msg-time {
  font-size: .7rem;
  color: var(--text-muted);
}

.chat-message .msg-text {
  font-size: .875rem;
  line-height: 1.45;
  color: var(--text);
  word-break: break-word;
}

.chat-message.system .msg-text {
  color: var(--text-muted);
  font-style: italic;
  font-size: .8rem;
}

.chat-input-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px;
  border-top: 1px solid var(--border);
}

.chat-input-row input {
  flex: 1;
  background: var(--bg3);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 8px 12px;
  font-size: .875rem;
  outline: none;
  transition: border-color var(--transition);
}

.chat-input-row input:focus { border-color: var(--accent); }

/* ── Control bar ─────────────────────────────────────────── */
.control-bar {
  grid-area: bar;
  background: var(--bg2);
  border-top: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 24px;
  gap: 12px;
}

.controls-left, .controls-right {
  display: flex;
  align-items: center;
  gap: 4px;
  flex: 1;
}

.controls-right { justify-content: flex-end; }

.controls-center {
  display: flex;
  align-items: center;
  gap: 8px;
}

.ctrl-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  width: 56px;
  height: 56px;
  border-radius: var(--radius);
  background: var(--bg3);
  transition: background var(--transition), color var(--transition);
  color: var(--text);
}

.ctrl-btn:hover { background: var(--border); }

.ctrl-btn.active {
  background: var(--accent);
  color: #fff;
}

.ctrl-btn.muted, .ctrl-btn.cam-off {
  background: rgba(237,66,69,.2);
  color: var(--danger);
}

.ctrl-btn.screen-sharing {
  background: rgba(35,165,90,.2);
  color: var(--success);
  outline: 1px solid var(--success);
}

.ctrl-btn-danger { background: var(--danger); color: #fff; width: 64px; height: 56px; }
.ctrl-btn-danger:hover { background: var(--danger-dim); }

.ctrl-icon { display: flex; align-items: center; justify-content: center; position: relative; }
.ctrl-label { font-size: .65rem; font-weight: 600; color: var(--text-muted); text-transform: uppercase; letter-spacing: .04em; }
.ctrl-btn.muted .ctrl-label,
.ctrl-btn.cam-off .ctrl-label { color: var(--danger); }
.ctrl-btn.screen-sharing .ctrl-label { color: var(--success); }
.ctrl-btn-danger .ctrl-label { color: rgba(255,255,255,.8); }

/* ── Icon button (generic small) ─────────────────────────── */
.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 32px; height: 32px;
  border-radius: var(--radius-sm);
  color: var(--text-muted);
  transition: background var(--transition), color var(--transition);
}

.icon-btn:hover { background: var(--bg3); color: var(--text); }

.send-btn { color: var(--accent); }
.send-btn:hover { background: rgba(88,101,242,.15); color: var(--accent); }

/* ── Unread badge ────────────────────────────────────────── */
.unread-badge {
  position: absolute;
  top: -6px; right: -8px;
  background: var(--danger);
  color: #fff;
  font-size: .6rem;
  font-weight: 700;
  min-width: 16px;
  height: 16px;
  border-radius: 8px;
  padding: 0 4px;
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: none;
}

/* ── Avatar color swatches (join page) ──────────────────── */
.avatar-swatch-row {
  display: flex;
  gap: 10px;
  justify-content: center;
  margin-top: 4px;
}

.avatar-swatch {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  cursor: pointer;
  border: 2px solid transparent;
  transition: border-color 0.15s ease, transform 0.15s ease;
  padding: 0;
}

.avatar-swatch.selected {
  border-color: #fff;
  transform: scale(1.15);
}

/* ── Avatar colours (hash-based, assigned in JS) ─────────── */
.av-0 { background: #5865f2; }
.av-1 { background: #23a55a; }
.av-2 { background: #f0b232; }
.av-3 { background: #ed4245; }
.av-4 { background: #e91e8c; }
.av-5 { background: #00b0f4; }

/* ── Remote volume popover ───────────────────────────────── */
.vol-ctrl {
  position: relative;
}

.vol-panel {
  position: absolute;
  bottom: calc(100% + 10px);
  left: 50%;
  transform: translateX(-50%);
  background: var(--bg3);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 14px;
  min-width: 180px;
  z-index: 200;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.5);
}

.vol-header {
  font-size: 11px;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  text-align: center;
  margin-bottom: 10px;
}

#volSlider {
  width: 100%;
  cursor: pointer;
  accent-color: var(--accent);
}

.vol-pct {
  font-size: 13px;
  font-weight: 600;
  color: var(--text);
  text-align: center;
  margin-top: 6px;
}

/* ═══════════════════════════════════════════════════════════ */
/*  RESPONSIVE — mobile stacked layout                        */
/* ═══════════════════════════════════════════════════════════ */
@media (max-width: 680px) {
  .room-page.chat-open {
    grid-template-columns: 1fr;
    grid-template-rows: 1fr 50vh var(--bar-h);
    grid-template-areas:
      "video"
      "chat"
      "bar";
  }

  .chat-panel { border-left: none; border-top: 1px solid var(--border); }

  /* Stack tiles vertically on mobile */
  .tiles-container { flex-direction: column; gap: 12px; }

  .ctrl-btn { width: 46px; height: 50px; }
  .ctrl-label { display: none; }
}
