/* ── Reset & Base ──────────────────────────────── */
*,
*::before,
*::after {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

:root {
  --bg: #08080d;
  --surface: #111118;
  --surface-glass: rgba(17, 17, 24, 0.75);
  --border: rgba(255, 255, 255, 0.07);
  --border-light: rgba(255, 255, 255, 0.12);
  --text: #e4e4e7;
  --text-muted: #71717a;
  --accent: #22d3ee;
  --accent-dim: rgba(34, 211, 238, 0.12);
  --accent-glow: rgba(34, 211, 238, 0.25);
  --purple: #a78bfa;
  --danger: #ef4444;
  --danger-dim: rgba(239, 68, 68, 0.15);
  --success: #22c55e;
  --radius: 14px;
  --radius-sm: 10px;
  --font: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
}

html,
body {
  height: 100%;
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}

.hidden {
  display: none !important;
}

/* ── Lobby ─────────────────────────────────────── */
#lobby {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  padding: 24px;
  background:
    radial-gradient(ellipse at 30% 20%, rgba(34, 211, 238, 0.06) 0%, transparent 50%),
    radial-gradient(ellipse at 70% 80%, rgba(167, 139, 250, 0.05) 0%, transparent 50%),
    var(--bg);
}

.lobby-card {
  width: 100%;
  max-width: 380px;
  padding: 40px 32px;
  background: var(--surface-glass);
  backdrop-filter: blur(24px);
  -webkit-backdrop-filter: blur(24px);
  border: 1px solid var(--border);
  border-radius: 20px;
  text-align: center;
  animation: fadeUp 0.5s ease-out;
}

@keyframes fadeUp {
  from {
    opacity: 0;
    transform: translateY(20px);
  }

  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.lobby-logo {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 14px;
  margin-bottom: 8px;
}

.lobby-logo h1 {
  font-size: 1.75rem;
  font-weight: 700;
  background: linear-gradient(135deg, var(--accent), var(--purple));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.lobby-subtitle {
  color: var(--text-muted);
  font-size: 0.875rem;
  margin-bottom: 32px;
}

.lobby-form {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.lobby-form input {
  width: 100%;
  padding: 14px 18px;
  background: rgba(255, 255, 255, 0.04);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  font-family: var(--font);
  font-size: 0.95rem;
  outline: none;
  transition: border-color 0.2s, box-shadow 0.2s;
}

.lobby-form input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-dim);
}

.lobby-form input::placeholder {
  color: var(--text-muted);
}

#join-btn {
  position: relative;
  padding: 14px;
  background: linear-gradient(135deg, var(--accent), #06b6d4);
  border: none;
  border-radius: var(--radius-sm);
  color: #0a0a0f;
  font-family: var(--font);
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  transition: transform 0.15s, box-shadow 0.2s;
}

#join-btn:hover {
  transform: translateY(-1px);
  box-shadow: 0 6px 24px var(--accent-glow);
}

#join-btn:active {
  transform: translateY(0);
}

#join-btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
  transform: none;
}

.btn-loader {
  display: inline-block;
  width: 18px;
  height: 18px;
  border: 2.5px solid rgba(10, 10, 15, 0.3);
  border-top-color: #0a0a0f;
  border-radius: 50%;
  animation: spin 0.6s linear infinite;
}

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

.error-msg {
  margin-top: 12px;
  color: var(--danger);
  font-size: 0.85rem;
}

/* ── Room ──────────────────────────────────────── */
#room {
  display: flex;
  flex-direction: column;
  height: 100vh;
  background: var(--bg);
  position: relative;
}

/* ── Video Grid (base) ─────────────────────────── */
#video-grid {
  flex: 1;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
  gap: 10px;
  padding: 12px;
  overflow-y: auto;
  align-content: center;
  position: relative;
}

.video-wrapper {
  position: relative;
  border-radius: var(--radius);
  overflow: hidden;
  background: var(--surface);
  border: 1px solid var(--border);
  aspect-ratio: 9 / 16;
  animation: fadeIn 0.35s ease-out;
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: scale(0.95);
  }

  to {
    opacity: 1;
    transform: scale(1);
  }
}

.video-wrapper video {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

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

.video-label {
  position: absolute;
  bottom: 10px;
  left: 10px;
  padding: 4px 10px;
  background: rgba(0, 0, 0, 0.55);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  border-radius: 6px;
  font-size: 0.8rem;
  font-weight: 500;
  color: #fff;
  pointer-events: none;
}

/* ── Solo: single participant centered ──────────── */
#video-grid.layout-solo {
  grid-template-columns: 1fr;
  place-items: center;
}

#video-grid.layout-solo .video-wrapper {
  max-width: 400px;
  width: 100%;
}

/* ── PiP: FaceTime-style 1:1 ───────────────────── */
#video-grid.layout-pip {
  display: block;
  padding: 0;
  overflow: hidden;
}

/* Remote video fills entire area */
#video-grid.layout-pip .video-wrapper:not(#local-wrapper) {
  width: 100%;
  height: 100%;
  border-radius: 0;
  border: none;
  aspect-ratio: auto;
  animation: none;
}

/* Local video becomes PiP overlay */
#video-grid.layout-pip #local-wrapper {
  position: absolute;
  bottom: 20px;
  right: 20px;
  width: 120px;
  height: 160px;
  aspect-ratio: auto;
  border-radius: 16px;
  border: 2px solid rgba(255, 255, 255, 0.2);
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.5);
  z-index: 10;
  animation: pipIn 0.4s ease-out;
  cursor: grab;
  transition: box-shadow 0.2s;
}

#video-grid.layout-pip #local-wrapper:hover {
  box-shadow: 0 8px 40px rgba(0, 0, 0, 0.7);
}

#video-grid.layout-pip #local-wrapper:active {
  cursor: grabbing;
}

#video-grid.layout-pip #local-wrapper .video-label {
  display: none;
}

@keyframes pipIn {
  from {
    opacity: 0;
    transform: scale(0.5);
  }

  to {
    opacity: 1;
    transform: scale(1);
  }
}

/* ── Grid: Zoom/Teams-style 3+ ─────────────────── */
#video-grid.layout-grid {
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: 8px;
  align-content: center;
}

#video-grid.layout-grid .video-wrapper {
  aspect-ratio: 16 / 9;
  max-height: 45vh;
}

#video-grid.layout-grid #local-wrapper {
  position: relative;
  bottom: auto;
  right: auto;
  width: auto;
  height: auto;
  border: 1px solid var(--border);
  box-shadow: none;
  border-radius: var(--radius);
  cursor: default;
}

#video-grid.layout-grid #local-wrapper .video-label {
  display: block;
}

/* ── Controls ──────────────────────────────────── */
#controls {
  display: flex;
  justify-content: center;
  gap: 12px;
  padding: 16px;
  background: var(--surface-glass);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  border-top: 1px solid var(--border);
}

.ctrl-btn {
  position: relative;
  width: 52px;
  height: 52px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 1px solid var(--border-light);
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.06);
  color: var(--text);
  cursor: pointer;
  transition: background 0.2s, border-color 0.2s, transform 0.15s;
}

.ctrl-btn:hover {
  background: rgba(255, 255, 255, 0.1);
  border-color: rgba(255, 255, 255, 0.2);
  transform: scale(1.06);
}

.ctrl-btn.active {
  background: var(--danger-dim);
  border-color: var(--danger);
  color: var(--danger);
}

.ctrl-btn .icon {
  width: 22px;
  height: 22px;
}

.ctrl-btn-danger {
  background: var(--danger);
  border-color: var(--danger);
  color: #fff;
}

.ctrl-btn-danger:hover {
  background: #dc2626;
  border-color: #dc2626;
}

/* Chat badge */
.chat-badge {
  position: absolute;
  top: -4px;
  right: -4px;
  min-width: 20px;
  height: 20px;
  padding: 0 5px;
  background: var(--accent);
  color: #0a0a0f;
  font-size: 0.7rem;
  font-weight: 700;
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* ── Chat Panel ────────────────────────────────── */
#chat-panel {
  position: absolute;
  top: 0;
  right: 0;
  width: 340px;
  height: 100%;
  display: flex;
  flex-direction: column;
  background: var(--surface-glass);
  backdrop-filter: blur(24px);
  -webkit-backdrop-filter: blur(24px);
  border-left: 1px solid var(--border);
  animation: slideIn 0.25s ease-out;
  z-index: 20;
}

@keyframes slideIn {
  from {
    transform: translateX(100%);
  }

  to {
    transform: translateX(0);
  }
}

.chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 18px;
  border-bottom: 1px solid var(--border);
  font-weight: 600;
  font-size: 0.95rem;
}

.chat-close {
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 1.5rem;
  cursor: pointer;
  line-height: 1;
  padding: 4px;
}

.chat-close:hover {
  color: var(--text);
}

#chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 14px 18px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.chat-msg {
  max-width: 85%;
  padding: 10px 14px;
  border-radius: 12px 12px 12px 4px;
  background: rgba(255, 255, 255, 0.06);
  font-size: 0.875rem;
  line-height: 1.45;
  animation: fadeIn 0.2s ease-out;
}

.chat-msg.mine {
  align-self: flex-end;
  background: var(--accent-dim);
  border-radius: 12px 12px 4px 12px;
}

.chat-msg .chat-sender {
  display: block;
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--accent);
  margin-bottom: 3px;
}

.chat-msg.mine .chat-sender {
  color: var(--purple);
}

.chat-msg.system {
  align-self: center;
  max-width: 100%;
  background: none;
  color: var(--text-muted);
  font-size: 0.8rem;
  text-align: center;
  padding: 4px;
}

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

.chat-input-row input {
  flex: 1;
  padding: 10px 14px;
  background: rgba(255, 255, 255, 0.04);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  font-family: var(--font);
  font-size: 0.875rem;
  outline: none;
  transition: border-color 0.2s;
}

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

.chat-send {
  width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--accent);
  border: none;
  border-radius: var(--radius-sm);
  color: #0a0a0f;
  cursor: pointer;
  transition: background 0.2s;
}

.chat-send:hover {
  background: #06b6d4;
}

/* ── Toast ─────────────────────────────────────── */
#toast-container {
  position: fixed;
  top: 20px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 100;
  display: flex;
  flex-direction: column;
  gap: 8px;
  pointer-events: none;
}

.toast {
  padding: 10px 20px;
  background: var(--surface-glass);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  border: 1px solid var(--border);
  border-radius: 10px;
  font-size: 0.85rem;
  color: var(--text);
  animation: toastIn 0.3s ease-out;
  white-space: nowrap;
}

.toast.toast-out {
  animation: toastOut 0.25s ease-in forwards;
}

@keyframes toastIn {
  from {
    opacity: 0;
    transform: translateY(-12px);
  }

  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes toastOut {
  from {
    opacity: 1;
    transform: translateY(0);
  }

  to {
    opacity: 0;
    transform: translateY(-12px);
  }
}

/* ── Responsive ────────────────────────────────── */
@media (max-width: 768px) {
  #video-grid {
    grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
    gap: 6px;
    padding: 6px;
  }

  .video-wrapper {
    border-radius: var(--radius-sm);
  }

  #video-grid.layout-pip #local-wrapper {
    width: 100px;
    height: 140px;
    bottom: 14px;
    right: 14px;
    border-radius: 12px;
  }

  #video-grid.layout-grid .video-wrapper {
    aspect-ratio: 16 / 9;
    max-height: none;
  }

  #controls {
    gap: 10px;
    padding: 12px;
  }

  .ctrl-btn {
    width: 46px;
    height: 46px;
  }

  .ctrl-btn .icon {
    width: 20px;
    height: 20px;
  }

  /* Chat as full overlay on mobile */
  #chat-panel {
    width: 100%;
    border-left: none;
    border-radius: 0;
  }

  .lobby-card {
    padding: 32px 24px;
  }
}

@media (max-width: 480px) {
  #video-grid.layout-grid {
    grid-template-columns: 1fr 1fr;
  }

  #video-grid.layout-pip #local-wrapper {
    width: 90px;
    height: 120px;
    bottom: 10px;
    right: 10px;
  }

  .ctrl-btn {
    width: 42px;
    height: 42px;
  }

  .ctrl-btn .icon {
    width: 18px;
    height: 18px;
  }
}

/* ── Scrollbar ─────────────────────────────────── */
::-webkit-scrollbar {
  width: 6px;
}

::-webkit-scrollbar-track {
  background: transparent;
}

::-webkit-scrollbar-thumb {
  background: rgba(255, 255, 255, 0.1);
  border-radius: 3px;
}

::-webkit-scrollbar-thumb:hover {
  background: rgba(255, 255, 255, 0.2);
}