/* Modern P2P Video Calling App with Grid Motion Detection */

:root {
  /* Color System */
  --primary-50: #eff6ff;
  --primary-100: #dbeafe;
  --primary-500: #3b82f6;
  --primary-600: #2563eb;
  --primary-700: #1d4ed8;
  
  --secondary-50: #f0fdfa;
  --secondary-100: #ccfbf1;
  --secondary-500: #14b8a6;
  --secondary-600: #0d9488;
  
  --success-50: #ecfdf5;
  --success-100: #d1fae5;
  --success-500: #10b981;
  --success-600: #059669;
  
  --warning-50: #fffbeb;
  --warning-100: #fef3c7;
  --warning-500: #f59e0b;
  --warning-600: #d97706;
  
  --error-50: #fef2f2;
  --error-100: #fecaca;
  --error-500: #ef4444;
  --error-600: #dc2626;
  
  --gray-50: #f9fafb;
  --gray-100: #f3f4f6;
  --gray-200: #e5e7eb;
  --gray-300: #d1d5db;
  --gray-400: #9ca3af;
  --gray-500: #6b7280;
  --gray-600: #4b5563;
  --gray-700: #374151;
  --gray-800: #1f2937;
  --gray-900: #111827;
  
  /* Spacing System (8px base) */
  --space-1: 0.5rem;   /* 8px */
  --space-2: 1rem;     /* 16px */
  --space-3: 1.5rem;   /* 24px */
  --space-4: 2rem;     /* 32px */
  --space-6: 3rem;     /* 48px */
  --space-8: 4rem;     /* 64px */
  
  /* Typography */
  --font-size-sm: 0.875rem;
  --font-size-base: 1rem;
  --font-size-lg: 1.125rem;
  --font-size-xl: 1.25rem;
  --font-size-2xl: 1.5rem;
  --font-size-3xl: 1.875rem;
  
  --line-height-tight: 1.2;
  --line-height-normal: 1.5;
  --line-height-relaxed: 1.6;
  
  --font-weight-normal: 400;
  --font-weight-medium: 500;
  --font-weight-semibold: 600;
  --font-weight-bold: 700;
  
  /* Border Radius */
  --border-radius-sm: 0.375rem;
  --border-radius-md: 0.5rem;
  --border-radius-lg: 0.75rem;
  --border-radius-xl: 1rem;
  
  /* Shadows */
  --shadow-sm: 0 1px 2px 0 rgb(0 0 0 / 0.05);
  --shadow-md: 0 4px 6px -1px rgb(0 0 0 / 0.1), 0 2px 4px -2px rgb(0 0 0 / 0.1);
  --shadow-lg: 0 10px 15px -3px rgb(0 0 0 / 0.1), 0 4px 6px -4px rgb(0 0 0 / 0.1);
  --shadow-xl: 0 20px 25px -5px rgb(0 0 0 / 0.1), 0 8px 10px -6px rgb(0 0 0 / 0.1);
}

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

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell', sans-serif;
  font-size: var(--font-size-base);
  line-height: var(--line-height-normal);
  color: var(--gray-100);
  background: linear-gradient(135deg, var(--gray-900) 0%, var(--gray-800) 100%);
  min-height: 100vh;
  overflow-x: hidden;
}

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

/* Header Styles - Not fixed */
.header {
  background: rgba(31, 41, 55, 0.95);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--gray-700);
  padding: var(--space-4) var(--space-3);
  z-index: 100;
}

.header h1 {
  font-size: var(--font-size-2xl);
  font-weight: var(--font-weight-bold);
  color: var(--primary-400);
  margin-bottom: var(--space-3);
  text-align: center;
}

.connection-info {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: var(--space-3);
}

.peer-id-section {
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
}

.peer-id-section label {
  font-size: var(--font-size-sm);
  font-weight: var(--font-weight-medium);
  color: var(--gray-400);
}

.peer-id-container {
  display: flex;
  gap: var(--space-2);
  align-items: center;
}

.peer-id-container input {
  background: var(--gray-800);
  border: 1px solid var(--gray-600);
  border-radius: var(--border-radius-md);
  padding: var(--space-2) var(--space-3);
  color: var(--gray-100);
  font-family: 'Monaco', 'Menlo', monospace;
  font-size: var(--font-size-sm);
  min-width: 200px;
  max-width: 280px;
}

.connection-status {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  font-size: var(--font-size-sm);
  font-weight: var(--font-weight-medium);
}

.status-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--gray-500);
  animation: pulse 2s infinite;
}

.status-dot.connected {
  background: var(--success-500);
}

.status-dot.calling {
  background: var(--warning-500);
}

.status-dot.in-call {
  background: var(--primary-500);
}

.status-dot.error {
  background: var(--error-500);
}

/* Main Content */
.main-content {
  flex: 1;
  padding: var(--space-4) var(--space-3);
  display: flex;
  flex-direction: column;
  gap: var(--space-4);
  min-height: 0; /* Allow flex shrinking */
}

/* Connection Controls */
.connection-controls {
  background: rgba(55, 65, 81, 0.8);
  backdrop-filter: blur(10px);
  border: 1px solid var(--gray-700);
  border-radius: var(--border-radius-xl);
  padding: var(--space-4);
  display: flex;
  flex-direction: column;
  gap: var(--space-4);
  flex-shrink: 0; /* Prevent shrinking */
}

.connect-section {
  display: flex;
  gap: var(--space-3);
  align-items: center;
  flex-wrap: wrap;
}

.connect-section input {
  flex: 1;
  min-width: 200px;
  background: var(--gray-800);
  border: 1px solid var(--gray-600);
  border-radius: var(--border-radius-md);
  padding: var(--space-3);
  color: var(--gray-100);
  font-size: var(--font-size-base);
  transition: border-color 0.2s ease;
}

.connect-section input:focus {
  outline: none;
  border-color: var(--primary-500);
  box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
}

.connect-section input::placeholder {
  color: var(--gray-500);
}

/* Camera Controls */
.camera-controls {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: var(--space-3);
  padding-top: var(--space-3);
  border-top: 1px solid var(--gray-700);
}

.camera-selection {
  display: flex;
  align-items: center;
  gap: var(--space-2);
}

.camera-selection label {
  font-size: var(--font-size-sm);
  font-weight: var(--font-weight-medium);
  color: var(--gray-400);
  white-space: nowrap;
}

.camera-dropdown {
  background: var(--gray-800);
  border: 1px solid var(--gray-600);
  border-radius: var(--border-radius-md);
  padding: var(--space-2) var(--space-3);
  color: var(--gray-100);
  font-size: var(--font-size-sm);
  min-width: 180px;
  cursor: pointer;
}

.camera-dropdown:focus {
  outline: none;
  border-color: var(--primary-500);
  box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
}

.video-transform-controls {
  display: flex;
  gap: var(--space-2);
  align-items: center;
}

/* Motion Controls */
.motion-controls {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: var(--space-3);
  padding-top: var(--space-3);
  border-top: 1px solid var(--gray-700);
}

.toggle-container {
  display: flex;
  align-items: center;
  gap: var(--space-2);
}

.toggle-switch {
  position: relative;
  display: inline-block;
  width: 140px;
  height: 34px;
  cursor: pointer;
}

.toggle-switch input {
  opacity: 0;
  width: 0;
  height: 0;
}

.toggle-slider {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: var(--gray-600);
  border-radius: 17px;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

.toggle-text {
  font-size: var(--font-size-sm);
  font-weight: var(--font-weight-medium);
  color: var(--gray-300);
  transition: color 0.3s ease;
}

.toggle-switch input:checked + .toggle-slider {
  background: var(--primary-600);
}

.toggle-switch input:checked + .toggle-slider .toggle-text {
  color: white;
}

.motion-status {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  font-size: var(--font-size-sm);
  font-weight: var(--font-weight-medium);
  color: var(--gray-400);
}

.motion-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--gray-600);
  transition: all 0.3s ease;
}

.motion-dot.active {
  background: var(--success-500);
  animation: pulse 1.5s infinite;
}

.motion-dot.paused {
  background: var(--warning-500);
  animation: pulse 1s infinite;
}

/* Button Styles */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-1);
  padding: var(--space-3) var(--space-4);
  border: none;
  border-radius: var(--border-radius-md);
  font-size: var(--font-size-base);
  font-weight: var(--font-weight-medium);
  cursor: pointer;
  transition: all 0.2s ease;
  text-decoration: none;
  white-space: nowrap;
}

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

.btn-primary {
  background: var(--primary-600);
  color: white;
}

.btn-primary:hover:not(:disabled) {
  background: var(--primary-700);
  transform: translateY(-1px);
  box-shadow: var(--shadow-lg);
}

.btn-secondary {
  background: var(--gray-600);
  color: white;
}

.btn-secondary:hover:not(:disabled) {
  background: var(--gray-700);
}

.btn-secondary.success {
  background: var(--success-600);
}

.btn-secondary.active {
  background: var(--primary-600);
  color: white;
}

.btn-danger {
  background: var(--error-600);
  color: white;
}

.btn-danger:hover:not(:disabled) {
  background: var(--error-700);
  transform: translateY(-1px);
  box-shadow: var(--shadow-lg);
}

.btn-icon {
  padding: var(--space-2);
  min-width: auto;
}

.btn-icon svg {
  width: 16px;
  height: 16px;
}

/* Video Section - Improved for better screen fitting */
.video-section {
  flex: 1;
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 0; /* Allow flex shrinking */
  margin-bottom: var(--space-4); /* Add space before legend */
}

.video-container {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: var(--space-4);
  width: 100%;
  height: 100%;
  max-width: 100vw;
  max-height: calc(100vh - 500px); /* Account for header, controls, and legend */
}

.video-wrapper {
  position: relative;
  background: var(--gray-800);
  border-radius: var(--border-radius-xl);
  overflow: hidden;
  box-shadow: var(--shadow-xl);
  border: 2px solid var(--gray-700);
  width: 100%;
  height: 100%;
  min-height: 200px;
}

.video-wrapper video {
  width: 100%;
  height: 100%;
  object-fit: cover;
  background: var(--gray-900);
}

.local-video-wrapper {
  border-color: var(--primary-600);
}

/* Grid Overlay - Fixed positioning and sizing */
.grid-overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 5;
  object-fit: cover;
}

.video-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  pointer-events: none;
  z-index: 10;
}

.video-label {
  position: absolute;
  top: var(--space-2);
  left: var(--space-2);
  background: rgba(0, 0, 0, 0.7);
  color: white;
  padding: var(--space-1) var(--space-2);
  border-radius: var(--border-radius-sm);
  font-size: var(--font-size-sm);
  font-weight: var(--font-weight-medium);
}

.no-video-placeholder {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: var(--gray-800);
  color: var(--gray-400);
  text-align: center;
}

.placeholder-icon {
  font-size: 4rem;
  margin-bottom: var(--space-2);
  opacity: 0.5;
}

.placeholder-content p {
  font-size: var(--font-size-lg);
  font-weight: var(--font-weight-medium);
}

/* Motion Detection Legend - Fixed positioning to prevent overlap */
.motion-legend {
  background: rgba(55, 65, 81, 0.8);
  backdrop-filter: blur(10px);
  border: 1px solid var(--gray-700);
  border-radius: var(--border-radius-xl);
  padding: var(--space-4);
  flex-shrink: 0; /* Prevent shrinking */
  margin-top: var(--space-4); /* Ensure separation from video */
}

.motion-legend h3 {
  font-size: var(--font-size-lg);
  font-weight: var(--font-weight-semibold);
  color: var(--gray-100);
  margin-bottom: var(--space-3);
}

.legend-items {
  display: flex;
  gap: var(--space-4);
  margin-bottom: var(--space-3);
  flex-wrap: wrap;
}

.legend-item {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  font-size: var(--font-size-sm);
  color: var(--gray-300);
}

.legend-color {
  width: 20px;
  height: 20px;
  border-radius: var(--border-radius-sm);
  border: 1px solid var(--gray-600);
}

.legend-color.low {
  background: rgba(255, 255, 0, 0.6);
}

.legend-color.medium {
  background: rgba(255, 165, 0, 0.7);
}

.legend-color.high {
  background: rgba(255, 0, 0, 0.8);
}

.grid-info {
  display: flex;
  gap: var(--space-4);
  flex-wrap: wrap;
}

.grid-info p {
  font-size: var(--font-size-sm);
  color: var(--gray-400);
  margin: 0;
}

/* Motion Log Section */
.motion-log-section {
  background: rgba(55, 65, 81, 0.8);
  backdrop-filter: blur(10px);
  border: 1px solid var(--gray-700);
  border-radius: var(--border-radius-xl);
  padding: var(--space-4);
  flex-shrink: 0; /* Prevent shrinking */
}

.log-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: var(--space-3);
  flex-wrap: wrap;
  gap: var(--space-2);
}

.log-header h3 {
  font-size: var(--font-size-lg);
  font-weight: var(--font-weight-semibold);
  color: var(--gray-100);
}

.log-controls {
  display: flex;
  gap: var(--space-2);
}

.motion-log {
  max-height: 200px;
  overflow-y: auto;
  border: 1px solid var(--gray-600);
  border-radius: var(--border-radius-md);
  background: var(--gray-800);
}

.motion-log-entry {
  display: grid;
  grid-template-columns: 60px 80px 80px 1fr;
  gap: var(--space-2);
  padding: var(--space-2) var(--space-3);
  border-bottom: 1px solid var(--gray-700);
  font-size: var(--font-size-sm);
  align-items: center;
}

.motion-log-entry:last-child {
  border-bottom: none;
}

.motion-log-entry.low {
  background: rgba(255, 255, 0, 0.1);
}

.motion-log-entry.medium {
  background: rgba(255, 165, 0, 0.1);
}

.motion-log-entry.high {
  background: rgba(255, 0, 0, 0.1);
}

.log-coordinate {
  font-weight: var(--font-weight-semibold);
  color: var(--primary-400);
}

.log-intensity {
  text-transform: uppercase;
  font-weight: var(--font-weight-medium);
}

.log-level {
  color: var(--gray-300);
}

.log-time {
  color: var(--gray-400);
  text-align: right;
}

/* Error Messages */
.error-container {
  position: fixed;
  top: var(--space-4);
  right: var(--space-4);
  z-index: 1000;
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  max-width: 400px;
}

.error-message {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  background: var(--error-50);
  color: var(--error-800);
  padding: var(--space-3);
  border-radius: var(--border-radius-lg);
  border: 1px solid var(--error-200);
  box-shadow: var(--shadow-lg);
  animation: slideIn 0.3s ease;
}

.error-icon {
  font-size: var(--font-size-lg);
}

.error-text {
  flex: 1;
  font-weight: var(--font-weight-medium);
}

.error-close {
  background: none;
  border: none;
  color: var(--error-600);
  font-size: var(--font-size-xl);
  cursor: pointer;
  padding: 0;
  width: 24px;
  height: 24px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--border-radius-sm);
}

.error-close:hover {
  background: var(--error-100);
}

/* Animations */
@keyframes pulse {
  0%, 100% {
    opacity: 1;
  }
  50% {
    opacity: 0.5;
  }
}

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

/* Responsive Design - Improved for better screen fitting */
@media (max-width: 1024px) {
  .main-content {
    gap: var(--space-3);
  }
  
  .video-container {
    max-height: calc(100vh - 450px);
  }
  
  .connection-info {
    flex-direction: column;
    align-items: stretch;
    text-align: center;
  }
  
  .camera-controls {
    flex-direction: column;
    align-items: center;
    text-align: center;
  }
  
  .motion-controls {
    flex-direction: column;
    align-items: center;
    text-align: center;
  }
  
  .legend-items {
    justify-content: center;
  }
  
  .grid-info {
    justify-content: center;
    text-align: center;
  }
}

@media (max-width: 768px) {
  .header {
    padding: var(--space-3) var(--space-2);
  }
  
  .main-content {
    padding: var(--space-3) var(--space-2);
    gap: var(--space-3);
  }
  
  .video-container {
    grid-template-columns: 1fr;
    max-height: calc(100vh - 400px);
    aspect-ratio: 16/9;
  }
  
  .connect-section {
    flex-direction: column;
    align-items: stretch;
  }
  
  .connect-section input {
    min-width: auto;
  }
  
  .peer-id-container {
    flex-direction: column;
    align-items: stretch;
  }
  
  .peer-id-container input {
    min-width: auto;
    max-width: none;
  }
  
  .camera-selection {
    flex-direction: column;
    align-items: stretch;
  }
  
  .camera-dropdown {
    min-width: auto;
  }
  
  .video-transform-controls {
    justify-content: center;
  }
  
  .error-container {
    left: var(--space-2);
    right: var(--space-2);
    max-width: none;
  }
  
  .motion-log-entry {
    grid-template-columns: 50px 70px 70px 1fr;
    gap: var(--space-1);
    padding: var(--space-1) var(--space-2);
  }
  
  .log-header {
    flex-direction: column;
    align-items: stretch;
  }
  
  .log-controls {
    justify-content: center;
  }
}

@media (max-width: 480px) {
  .header h1 {
    font-size: var(--font-size-xl);
  }
  
  .connection-controls {
    padding: var(--space-3);
  }
  
  .btn {
    padding: var(--space-2) var(--space-3);
    font-size: var(--font-size-sm);
  }
  
  .toggle-switch {
    width: 120px;
    height: 30px;
  }
  
  .toggle-slider {
    border-radius: 15px;
  }
  
  .video-container {
    max-height: calc(100vh - 350px);
  }
  
  .legend-items {
    flex-direction: column;
    align-items: center;
  }
  
  .motion-log-entry {
    grid-template-columns: 40px 60px 60px 1fr;
    font-size: 0.75rem;
  }
}

/* Focus styles for accessibility */
.btn:focus,
input:focus,
select:focus,
.toggle-switch:focus-within {
  outline: 2px solid var(--primary-500);
  outline-offset: 2px;
}

/* High contrast mode support */
@media (prefers-contrast: high) {
  :root {
    --gray-800: #000000;
    --gray-700: #333333;
    --gray-600: #666666;
  }
}

/* Reduced motion support */
@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}

/* Custom scrollbar for motion log */
.motion-log::-webkit-scrollbar {
  width: 6px;
}

.motion-log::-webkit-scrollbar-track {
  background: var(--gray-700);
  border-radius: 3px;
}

.motion-log::-webkit-scrollbar-thumb {
  background: var(--gray-500);
  border-radius: 3px;
}

.motion-log::-webkit-scrollbar-thumb:hover {
  background: var(--gray-400);
}