/* ============================================
   Video2 Mobile - Responsive Stylesheet
   ============================================ */

@media (max-width: 800px) {
  /* Prevent scrolling on body */
  html, body {
    height: 100%;
    height: 100dvh; /* Use dynamic viewport height for mobile browsers */
    overflow: hidden;
    touch-action: none;
    position: relative;
  }

  /* Hide device labels on mobile views (phones only, not tablets) */
  @media (max-width: 767px) {
    .device-label,
    #local-webcam-label,
    #local-microphone-label {
      display: none !important;
    }
  }
}

/* Tablet/iPad specific styles - show labels on touch */
@media (min-width: 768px) and (max-width: 1024px) and (hover: none) and (pointer: coarse) {
  /* Show device labels on tablet when local settings overlay is active */
  #self:active .device-label,
  #self:active #local-webcam-label,
  #self:active #local-microphone-label,
  #self .local-settings-overlay:active .device-label,
  #self .local-settings-overlay:active #local-webcam-label,
  #self .local-settings-overlay:active #local-microphone-label,
  #self .local-settings-overlay.show-overlay .device-label,
  #self .local-settings-overlay.show-overlay #local-webcam-label,
  #self .local-settings-overlay.show-overlay #local-microphone-label {
    display: block !important;
    opacity: 1 !important;
    visibility: visible !important;
  }
  
  /* Show local settings overlay on touch for tablets */
  #self:active .local-settings-overlay,
  #self .local-settings-overlay.show-overlay {
    opacity: 1 !important;
    pointer-events: auto;
    background-color: var(--bg-overlay) !important;
  }
  
  /* Show webcam label on touch for peer video on tablets */
  #peer:active .webcam-label-container,
  #peer .webcam-label-container.show-overlay {
    opacity: 1 !important;
    pointer-events: auto;
  }
}

@media (max-width: 800px) {

  .local-settings-overlay {
    /* Show labels and gear on touch down */
    flex-direction: column;
    justify-content: center;
    align-items: center;
    gap: var(--spacing-sm);
    /* On mobile, keep overlay transparent so it doesn't darken the video */
    background-color: transparent !important;
    opacity: 0;
    pointer-events: auto;
    transition: opacity 0.2s ease;
  }
  
  /* Show overlay on touch down or when show-overlay class is present for mobile */
  #self:active .local-settings-overlay,
  .local-settings-overlay:active,
  .local-settings-overlay.show-overlay {
    opacity: 1 !important;
    pointer-events: auto;
    visibility: visible !important;
  }

  /* On mobile: gear and labels are hidden by default, shown when overlay is active */
  .control-btn-settings,
  #device-settings-btn {
    margin-top: var(--spacing-sm);
    margin-bottom: 0;
    opacity: 0 !important;
    visibility: hidden !important;
    transition: opacity 0.2s ease, visibility 0.2s ease;
    /* Ensure proper touch target size for mobile */
    min-width: 44px;
    min-height: 44px;
    width: 44px;
    height: 44px;
    display: flex !important;
    align-items: center;
    justify-content: center;
    z-index: calc(var(--z-base) + 10);
    pointer-events: auto !important;
    touch-action: manipulation;
    /* Override desktop positioning - use static on mobile */
    position: static !important;
    bottom: auto !important;
    right: auto !important;
  }

  /* Show device labels when overlay is shown on mobile */
  .local-settings-overlay.show-overlay .device-label,
  .local-settings-overlay.show-overlay #local-webcam-label,
  .local-settings-overlay.show-overlay #local-microphone-label,
  #self:active .device-label,
  #self:active #local-webcam-label,
  #self:active #local-microphone-label,
  .local-settings-overlay:active .device-label,
  .local-settings-overlay:active #local-webcam-label,
  .local-settings-overlay:active #local-microphone-label {
    display: block !important;
    opacity: 1 !important;
    visibility: visible !important;
  }

  /* Show gear icon when overlay is shown or active on mobile */
  .local-settings-overlay.show-overlay .control-btn-settings,
  #self:active .control-btn-settings,
  .local-settings-overlay:active .control-btn-settings {
    opacity: 1 !important;
    visibility: visible !important;
    pointer-events: auto !important;
  }
  
  /* Ensure gear icon is clickable and has proper styling on mobile */
  .local-settings-overlay.show-overlay .control-btn-settings svg,
  #self:active .control-btn-settings svg {
    width: 24px;
    height: 24px;
    color: white;
    stroke: white;
    pointer-events: none; /* Let parent handle clicks */
  }

  /* Main Container - Stack vertically on mobile (video layout) */
  #main.app-container {
    grid-template-rows: 60px auto minmax(0, 1fr);
    grid-template-columns: 100%;
    grid-template-areas:
      'header'
      'videos'
      'messages';
    height: 100vh;
    height: 100dvh; /* Use dynamic viewport height for mobile browsers */
    max-height: 100vh;
    max-height: 100dvh;
    overflow: hidden;
    /* Account for fixed input area at bottom */
    padding-bottom: 0;
  }

  /* Chat-only layout: no videos row, let messages fill all space below header */
  #main.app-container.chat-layout {
    grid-template-rows: 60px minmax(0, 1fr);
    grid-template-columns: 100%;
    grid-template-areas:
      'header'
      'messages';
  }


  /* Header - Smaller on mobile */
  #top-bar.app-header {
    height: 60px;
    min-height: 60px;
    padding: var(--spacing-sm) var(--spacing-md);
  }

  .logo-image {
    max-height: 40px;
  }

  .header-controls {
    gap: var(--spacing-sm);
  }

  .online-text {
    font-size: var(--font-size-sm);
    padding: var(--spacing-xs) var(--spacing-sm);
  }

  .button-feedback {
    display: none;
  }

  /* Video Section - Peer as main feed, self as overlay box */
  #videos.video-section {
    grid-area: videos;
    display: flex;
    flex-direction: column;
    position: relative;
    margin: var(--spacing-sm);
    height: 50vh; /* Increased by another 20% from 42vh (42 * 1.2 = 50.4, rounded down to 50) */
    min-height: 200px;
    overflow: hidden;
  }

  /* Peer video - Main large feed */
  .video-container-peer {
    position: relative;
    width: 100%;
    height: 100%;
    background-color: var(--bg-dark);
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: var(--border-radius);
  }
  
  /* Show webcam label on touch down for peer video on mobile */
  #peer:active .webcam-label-container,
  #peer .webcam-label-container.show-overlay {
    opacity: 1 !important;
    pointer-events: auto;
  }

  .video-container-peer .video-player-peer {
    width: 100%;
    height: 100%;
    object-fit: cover;
  }

  /* Self video - Small overlay box in bottom right */
  .video-container-self {
    position: absolute;
    width: 20%;
    height: 20%;
    bottom: var(--spacing-sm);
    right: var(--spacing-sm);
    z-index: 10;
    border-radius: var(--border-radius-sm);
    overflow: hidden;
    background-color: var(--bg-dark);
    /* Disable dragging on regular mobile - only enable on smallest screens */
    touch-action: auto;
    -webkit-user-select: auto;
    -moz-user-select: auto;
    user-select: auto;
    -webkit-touch-callout: default;
    box-shadow: var(--shadow-lg);
  }

  .video-container-self .video-player-self {
    width: 100%;
    height: 100%;
    object-fit: cover;
  }

  /* Video controls on peer video */
  .video-container-peer .video-controls {
    top: var(--spacing-sm);
    right: var(--spacing-sm);
  }

  .control-btn {
    width: 44px;
    height: 44px;
    /* Larger touch target for mobile */
  }

  .control-btn svg {
    width: 20px;
    height: 20px;
  }

  .watermark {
    width: 25%;
    left: var(--spacing-sm);
    bottom: var(--spacing-sm);
  }

  /* Self video - ensure video doesn't interfere with dragging on smallest screens only */
  .video-container-self .video-player-self {
    touch-action: auto;
    pointer-events: auto;
  }

  /* Self video overlay - allow interaction */
  .video-container-self .local-settings-overlay {
    pointer-events: auto;
    /* Allow our JS drag/resize logic to fully control touch gestures */
    touch-action: none;
  }

  /* Keep settings button clickable on mobile */
  .video-container-self .control-btn-settings,
  .local-settings-overlay .control-btn-settings {
    pointer-events: auto !important;
    touch-action: manipulation;
    cursor: pointer;
    -webkit-tap-highlight-color: rgba(255, 255, 255, 0.3);
  }
  
  /* Prevent button from being blocked by overlay */
  .local-settings-overlay.show-overlay .control-btn-settings {
    position: relative;
    z-index: calc(var(--z-base) + 10);
  }

  /* Message Section - Full width on mobile */
  #message-area.message-section {
    grid-area: messages;
    border-top: 1px solid var(--border-color);
    min-height: 0;
    max-height: 100%;
    /* Add padding bottom to account for fixed input area */
    padding-bottom: 80px;
    box-sizing: border-box;
  }

  .messages-container {
    padding: var(--spacing-sm);
  }

  .message {
    max-width: 85%;
    font-size: var(--font-size-sm);
  }

  /* Input Section - Fixed at bottom */
  #input-area.input-section {
    position: fixed !important;
    bottom: 0 !important;
    left: 0 !important;
    right: 0 !important;
    grid-area: unset;
    padding: var(--spacing-sm) var(--spacing-md);
    padding-bottom: max(var(--spacing-sm), env(safe-area-inset-bottom));
    gap: var(--spacing-sm);
    z-index: var(--z-sticky);
    border-top: 1px solid var(--border-color);
    background-color: var(--bg-primary);
    box-shadow: 0 -2px 8px rgba(0, 0, 0, 0.1);
    width: 100% !important;
    transform: none !important;
    will-change: auto !important;
  }

  .input-wrapper {
    padding: var(--spacing-xs);
  }

  /* Make input and Skip button the same height on small screens */
  .message-input {
    font-size: var(--font-size-sm);
    padding: var(--spacing-xs) var(--spacing-sm);
    line-height: 1.2;
    height: 44px;
    box-sizing: border-box;
    display: flex;
    align-items: center;
  }

  .button-skip,
  .button-send {
    min-width: 70px;
    padding: 0 var(--spacing-md);
    font-size: var(--font-size-sm);
    height: 44px;
    display: flex;
    align-items: center;
    justify-content: center;
  }

  .button-send {
    display: none; /* Hide send button on mobile, use Enter key */
  }

  .keyboard-hint {
    display: none; /* Hide keyboard hints on mobile */
  }

  /* Modals - Full screen on mobile */
  .modal-content {
    max-width: 100%;
    max-height: 100vh;
    border-radius: 0;
    padding: var(--spacing-lg);
  }

  .modal-device .device-modal {
    max-width: 100%;
  }

  /* Audio Popup - Bottom on mobile */
  .audio-popup {
    bottom: 80px; /* Above input area */
    right: var(--spacing-md);
    left: var(--spacing-md);
    width: auto;
  }

  .audio-popup-content {
    min-width: auto;
    width: 100%;
  }

  /* Upload Toast - Adjusted for mobile */
  .upload-toast {
    left: var(--spacing-md);
    right: var(--spacing-md);
    transform: none;
    width: auto;
  }

  /* Theme Toggle - Smaller on mobile */
  .theme-toggle-small {
    width: 45px;
    height: 22px;
  }

  .toggle-slider-small:before {
    width: 18px;
    height: 18px;
  }

  .theme-toggle-input:checked + .toggle-slider-small:before {
    transform: translateX(23px);
  }

  .toggle-icon {
    font-size: var(--font-size-xs);
  }
}

/* Smaller mobile devices - iPhone smallest view */
@media (max-width: 480px) {
  #top-bar.app-header {
    padding: var(--spacing-xs) var(--spacing-sm);
  }

  .logo-image {
    max-height: 35px;
  }

  .online-text {
    font-size: var(--font-size-xs);
    padding: var(--spacing-xs);
  }

  #videos.video-section {
    height: 43vh; /* Increased by another 20% from 36vh (36 * 1.2 = 43.2, rounded down to 43) */
    min-height: 180px;
  }

  .control-btn {
    width: 40px;
    height: 40px;
  }

  .control-btn svg {
    width: 18px;
    height: 18px;
  }

  /* Self video slightly smaller on very small screens */
  .video-container-self {
    width: 25%;
    height: 25%;
    /* Enable dragging ONLY on smallest iPhone view */
    touch-action: none;
    -webkit-user-select: none;
    -moz-user-select: none;
    user-select: none;
    -webkit-touch-callout: none;
  }

  /* Enable dragging for self video on smallest screens only */
  .video-container-self .video-player-self {
    touch-action: none;
    pointer-events: none;
  }

  .video-container-self .local-settings-overlay {
    pointer-events: auto;
    touch-action: none;
  }

  /* Hide device labels on smallest iPhone view (draggable view) - ONLY show gear icon */
  .device-label,
  #local-webcam-label,
  #local-microphone-label {
    display: none !important;
    opacity: 0 !important;
    visibility: hidden !important;
  }
  
  /* Keep labels hidden even when overlay is shown on smallest screens */
  .local-settings-overlay.show-overlay .device-label,
  .local-settings-overlay.show-overlay #local-webcam-label,
  .local-settings-overlay.show-overlay #local-microphone-label,
  #self:active .device-label,
  #self:active #local-webcam-label,
  #self:active #local-microphone-label,
  .local-settings-overlay:active .device-label,
  .local-settings-overlay:active #local-webcam-label,
  .local-settings-overlay:active #local-microphone-label {
    display: none !important;
    opacity: 0 !important;
    visibility: hidden !important;
  }

  .local-settings-overlay {
    /* Only show settings button, hide labels */
    flex-direction: row;
    justify-content: center;
    align-items: center;
    gap: 0;
  }

  /* Hide gear by default on smallest screens, show on touch */
  .control-btn-settings,
  #device-settings-btn {
    margin-top: 0;
    margin-bottom: 0;
    opacity: 0 !important;
    visibility: hidden !important;
    /* Ensure no duplicate positioning from desktop styles */
    position: static !important;
    bottom: auto !important;
    right: auto !important;
    top: auto !important;
    left: auto !important;
  }
  
  /* Hide desktop-positioned gear icon (absolute positioned) on smallest screens */
  .video-container-self .control-btn-settings[style*="position: absolute"],
  #self .control-btn-settings[style*="position: absolute"],
  .video-container-self #device-settings-btn[style*="position: absolute"],
  #self #device-settings-btn[style*="position: absolute"] {
    display: none !important;
  }
  
  /* More specific: Hide any gear icon that's absolutely positioned (desktop style) */
  .video-container-self > .control-btn-settings,
  #self > .control-btn-settings,
  .video-container-self > #device-settings-btn,
  #self > #device-settings-btn {
    display: none !important;
  }
  
  /* Show gear button when overlay is shown on smallest screens */
  /* Only show the one inside the overlay */
  .local-settings-overlay.show-overlay .control-btn-settings,
  .local-settings-overlay.show-overlay #device-settings-btn,
  #self:active .local-settings-overlay .control-btn-settings,
  #self:active .local-settings-overlay #device-settings-btn,
  .local-settings-overlay:active .control-btn-settings,
  .local-settings-overlay:active #device-settings-btn {
    opacity: 1 !important;
    visibility: visible !important;
    pointer-events: auto !important;
    position: static !important;
    display: flex !important;
  }

  /* Make message area more compact on smallest screens */
  #message-area.message-section {
    padding-bottom: 70px; /* Slightly less padding */
  }

  .messages-container {
    padding: 6px 8px !important; /* Reduced from var(--spacing-sm) */
    gap: 1px !important; /* Even smaller spacing between messages */
  }

  .message {
    font-size: 17px !important; /* A few px bigger */
    line-height: 1.4 !important; /* Better line height */
    letter-spacing: -0.1px !important; /* Slightly tighter letter spacing */
    max-width: 90% !important; /* Allow slightly more width */
    margin-bottom: 1px !important; /* Even smaller margin */
  }

  .message .you,
  .message .strange {
    font-size: 16px !important; /* A few px bigger for labels */
    font-weight: 600 !important; /* Slightly bolder but compact */
    line-height: 1.3 !important;
  }

  .message-status {
    font-size: 15px !important; /* A few px bigger status messages */
    line-height: 1.4 !important;
    padding: 1px 0 !important; /* Minimal padding */
    margin-bottom: 1px !important; /* Even smaller margin - minimal gaps */
    margin-top: 0 !important; /* No top margin */
  }

  .message-status.slide-in {
    margin-bottom: 2px !important; /* Very small gap after slide-in message */
  }
  
  /* Remove any large gaps between status messages */
  .message-status + .message-status {
    margin-top: 0 !important;
    margin-bottom: 1px !important;
  }

  .button-skip {
    min-width: 60px;
    padding: 0 var(--spacing-sm);
    font-size: var(--font-size-xs);
    height: calc(var(--spacing-xs) * 2 + var(--spacing-xs) * 2 + 1.2em);
    display: flex;
    align-items: center;
    justify-content: center;
  }

  .message-input {
    font-size: var(--font-size-xs);
    line-height: 1.2;
  }
}

/* Landscape orientation on mobile */
@media (max-width: 800px) and (orientation: landscape) {
  #videos.video-section {
    height: 40vh;
  }

  #input-area.input-section {
    padding: var(--spacing-xs) var(--spacing-sm);
  }
}

/* Touch device optimizations */
@media (hover: none) and (pointer: coarse) {
  .control-btn,
  .button {
    min-height: 44px; /* Minimum touch target size */
  }

  .close {
    min-width: 44px;
    min-height: 44px;
    display: flex;
    align-items: center;
    justify-content: center;
  }

  /* On phones (small screens), keep overlay transparent and labels hidden */
  @media (max-width: 767px) {
    .local-settings-overlay {
      opacity: 1; /* Always show on touch devices */
      pointer-events: auto;
    }

    #self:hover .local-settings-overlay {
      opacity: 1; /* No hover state on touch */
    }
  }
  
  /* On tablets/iPad (768px-1024px), show overlay and labels on touch */
  @media (min-width: 768px) and (max-width: 1024px) {
    .local-settings-overlay {
      opacity: 0; /* Hidden by default */
      pointer-events: auto;
      background-color: var(--bg-overlay) !important;
    }
    
    .local-settings-overlay.show-overlay {
      opacity: 1 !important;
      background-color: var(--bg-overlay) !important;
    }
    
    /* Show device labels when overlay is shown on tablets - override mobile hide rule */
    .local-settings-overlay.show-overlay .device-label,
    .local-settings-overlay.show-overlay #local-webcam-label,
    .local-settings-overlay.show-overlay #local-microphone-label,
    #self:active .local-settings-overlay .device-label,
    #self:active .local-settings-overlay #local-webcam-label,
    #self:active .local-settings-overlay #local-microphone-label {
      display: block !important;
      opacity: 1 !important;
      visibility: visible !important;
    }
  }
}

