* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

/* Prevent scrolling and overscroll bounce on mobile */
html, body {
  overflow: hidden;
  overscroll-behavior: none;
}

:root {
  --primary: #6366f1;
  --primary-dark: #4f46e5;
  --danger: #ef4444;
  --danger-dark: #dc2626;
  --success: #10b981;
  --bg: #0f172a;
  --bg-light: #1e293b;
  --text: #f1f5f9;
  --text-muted: #94a3b8;
  --border: #334155;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.6;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.container {
  width: 100%;
  max-width: 1200px;
  margin: 0 auto;
  padding: 1rem;
  flex: 1;
  display: flex;
  flex-direction: column;
}

/* Header */
header {
  text-align: center;
  padding: 2rem 0;
}

header h1 {
  font-size: 2.5rem;
  font-weight: 700;
  margin-bottom: 0.5rem;
}

.tagline {
  font-size: 1.125rem;
  color: var(--text-muted);
}

/* Main */
main {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1.5rem;
}

/* Card */
.card {
  background: var(--bg-light);
  border: 1px solid var(--border);
  border-radius: 1rem;
  padding: 2rem;
  width: 100%;
  max-width: 400px;
  text-align: center;
}

.card h2 {
  font-size: 1.5rem;
  margin-bottom: 1.5rem;
}

/* PIN Form */
#pin-form {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

#pin-input {
  width: 100%;
  padding: 1rem;
  font-size: 2rem;
  text-align: center;
  background: var(--bg);
  color: var(--text);
  border: 2px solid var(--border);
  border-radius: 0.5rem;
  letter-spacing: 0.5rem;
  font-family: 'Courier New', monospace;
}

#pin-input:focus {
  outline: none;
  border-color: var(--primary);
}

button {
  padding: 0.75rem 1.5rem;
  font-size: 1rem;
  font-weight: 600;
  border: none;
  border-radius: 0.5rem;
  cursor: pointer;
  transition: all 0.2s;
}

#connect-btn {
  background: var(--primary);
  color: white;
}

#connect-btn:hover:not(:disabled) {
  background: var(--primary-dark);
}

#connect-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.hint {
  margin-top: 1rem;
  font-size: 0.875rem;
  color: var(--text-muted);
}

/* Call Interface */
#call-interface {
  width: 100%;
  max-width: 1200px;
}

.status-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 1rem;
  background: var(--bg-light);
  border: 1px solid var(--border);
  border-radius: 0.5rem;
  margin-bottom: 1rem;
}

#status-text {
  font-weight: 600;
}

#pin-display {
  font-family: 'Courier New', monospace;
  font-weight: 600;
  color: var(--primary);
}

/* Video Container */
.video-container {
  position: relative;
  width: 100%;
  aspect-ratio: 16 / 9;
  background: #000;
  border-radius: 1rem;
  overflow: hidden;
  margin-bottom: 1rem;
}

video {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

#remote-video {
  width: 100%;
  height: 100%;
}

#local-video {
  position: absolute;
  /* Using top/left for drag positioning - initial position set by JS */
  top: 0;
  left: 0;
  /* Base PiP size - non-fullscreen fallback, overridden by .mobile-call/.desktop-call */
  width: 100px;
  aspect-ratio: 3 / 4;
  border: 2px solid var(--border);
  border-radius: 0.5rem;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.3);
  /* Draggable PiP styles */
  cursor: grab;
  touch-action: none;
  user-select: none;
  will-change: left, top;
  z-index: 5;
}

#local-video.pip-dragging {
  cursor: grabbing;
}

/* Controls */
.controls {
  display: flex;
  justify-content: center;
  gap: 1rem;
}

.control-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.5rem;
  padding: 1rem 1.5rem;
  background: var(--bg-light);
  color: var(--text);
  border: 1px solid var(--border);
  min-width: 100px;
}

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

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

.control-btn.danger {
  background: var(--danger);
  border-color: var(--danger);
}

.control-btn.danger:hover {
  background: var(--danger-dark);
}

.control-btn .icon {
  font-size: 1.5rem;
}

.control-btn .label {
  font-size: 0.875rem;
}

/* Error Display */
.error {
  background: var(--danger);
  color: white;
  padding: 1rem;
  border-radius: 0.5rem;
  max-width: 600px;
  text-align: center;
  animation: slideIn 0.3s ease-out;
}

@keyframes slideIn {
  from {
    opacity: 0;
    transform: translateY(-1rem);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* Footer */
footer {
  text-align: center;
  padding: 2rem 0;
  color: var(--text-muted);
  font-size: 0.875rem;
}

footer a {
  color: var(--primary);
  text-decoration: none;
}

footer a:hover {
  text-decoration: underline;
}

/* Utility */
.hidden {
  display: none !important;
}

/* Flip button - hidden by default, shown via JS when multiple cameras */
#flip-btn {
  display: none;
}

#flip-btn.visible {
  display: flex;
}

#flip-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

/* Desktop Full-Screen Call Mode */
#call-interface.desktop-call {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  max-width: none;
  z-index: 100;
  background: #000;
  padding: 0;
  margin: 0;
}

.desktop-call .video-container {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  aspect-ratio: unset;
  border-radius: 0;
  margin: 0;
}

.desktop-call .status-bar {
  position: absolute;
  top: 1rem;
  right: 1rem;
  left: auto;
  padding: 0.75rem 1rem;
  background: rgba(30, 41, 59, 0.85);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 0.5rem;
  margin: 0;
  z-index: 10;
}

.desktop-call .controls {
  position: absolute;
  bottom: 2rem;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(30, 41, 59, 0.85);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 1rem;
  padding: 1rem;
  z-index: 10;
}

/* Desktop PiP - unified full-screen styling */
.desktop-call #local-video {
  width: 160px;
  max-height: 120px;
  aspect-ratio: 4 / 3;
  border-radius: 0.75rem;
  border: 2px solid rgba(255, 255, 255, 0.3);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.4);
  object-fit: cover;
}

/* PiP Toggle Button - hidden by default, shown via JS */
#pip-toggle-btn {
  display: none;
}

#pip-toggle-btn.visible {
  display: flex;
}

#local-video.pip-hidden {
  display: none !important;
}

/* Waiting Overlay */
.waiting-overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: none;
  z-index: 4;
}

.waiting-overlay-text {
  font-size: 1.5rem;
  font-weight: 700;
  color: rgba(255, 255, 255, 0.9);
  text-transform: uppercase;
  letter-spacing: 0.1em;
  text-shadow: 0 2px 8px rgba(0, 0, 0, 0.5);
  background: rgba(0, 0, 0, 0.4);
  padding: 1rem 2rem;
  border-radius: 0.5rem;
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
}

/* Mobile Responsive - Non-call screens */
@media (max-width: 767px) {
  header h1 {
    font-size: 2rem;
  }

  .card {
    padding: 1.5rem;
  }
}

/* Mobile Full-Screen Call Mode */
@media (max-width: 767px) {
  /* Full-screen call interface */
  #call-interface.mobile-call {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    /* Use dynamic viewport height to account for mobile browser chrome */
    height: 100vh; /* Fallback for older browsers */
    height: 100svh; /* Small viewport height - excludes browser UI */
    height: 100dvh; /* Dynamic viewport height - adjusts with browser UI */
    max-width: none;
    z-index: 100;
    background: #000;
    padding: 0;
    margin: 0;
  }

  /* Video container fills entire viewport */
  .mobile-call .video-container {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    aspect-ratio: unset;
    border-radius: 0;
    margin: 0;
  }

  /* Remote video fills container */
  .mobile-call #remote-video {
    width: 100%;
    height: 100%;
    object-fit: cover;
  }

  /* Mobile PiP - portrait orientation, unified full-screen styling */
  .mobile-call #local-video {
    width: 100px;
    height: 133px; /* 3:4 portrait ratio - explicit for iOS Safari */
    max-height: 133px;
    aspect-ratio: 3 / 4;
    border-radius: 0.75rem;
    border: 2px solid rgba(255, 255, 255, 0.3);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.4);
    object-fit: cover;
  }

  /* Status bar overlay at top-right */
  .mobile-call .status-bar {
    position: absolute;
    top: 0;
    left: auto;
    right: 0;
    padding: 1rem;
    padding-top: env(safe-area-inset-top, 1rem);
    padding-right: env(safe-area-inset-right, 1rem);
    background: linear-gradient(180deg, rgba(0, 0, 0, 0.7) 0%, transparent 100%);
    border: none;
    border-radius: 0;
    margin: 0;
    z-index: 10;
    transition: opacity 0.3s ease-out;
    text-align: right;
  }

  .mobile-call .status-bar.hidden-overlay {
    opacity: 0;
    pointer-events: none;
  }

  .mobile-call .status-bar #pin-display {
    font-size: 1.25rem;
  }

  .mobile-call .status-bar #status-text {
    display: none; /* Show only PIN on mobile during call */
  }

  /* Circular overlay controls at bottom */
  .mobile-call .controls {
    position: absolute;
    bottom: 2rem;
    bottom: calc(env(safe-area-inset-bottom, 0px) + 2rem);
    left: 0;
    right: 0;
    display: flex;
    justify-content: center;
    gap: 1.25rem;
    padding: 0 1rem;
    z-index: 10;
    flex-wrap: nowrap;
  }

  .mobile-call .control-btn {
    width: 56px;
    height: 56px;
    min-width: unset;
    border-radius: 50%;
    padding: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    background: rgba(30, 41, 59, 0.8);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    border: 1px solid rgba(255, 255, 255, 0.2);
  }

  .mobile-call .control-btn .icon {
    font-size: 1.25rem;
  }

  .mobile-call .control-btn .label {
    display: none; /* Hide labels on mobile */
  }

  .mobile-call .control-btn.danger {
    background: rgba(239, 68, 68, 0.9);
  }

  .mobile-call .control-btn.active {
    background: rgba(239, 68, 68, 0.8);
  }

  .mobile-call .control-btn:hover {
    background: rgba(51, 65, 85, 0.9);
  }

  .mobile-call .control-btn.danger:hover {
    background: rgba(220, 38, 38, 0.95);
  }

  /* Flip button visibility in mobile-call mode */
  .mobile-call #flip-btn.visible {
    display: flex;
  }
}
