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

html, body {
  font-family: 'Helvetica', sans-serif;
  overflow: hidden;
  background: #000;
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
}


#main-container {
  display: flex;
  align-items: flex-start;
}


#container {
  position: relative;
  height: 480px;
  overflow: visible;
  line-height: 0;
  box-shadow: 0 0 10px #000;
  display: none; /* Hidden initially */
  /* Width will be set dynamically by JavaScript */
}

#container > * {
  position: absolute;
  display: block;
  user-select: none;
  top: 0;
  left: 0;
  height: 480px;
  animation: fadeIn 1.2s;
  /* Width will be set dynamically by JavaScript */
}

#measurement-panel {
  width: 320px;
  height: 480px;
  border: 2px solid #333;
  box-shadow: 0 0 10px #000;
  background: #fff;
  position: relative;
  display: none; /* Hidden initially */
  flex-direction: column;
  padding: 20px;
}

#visualizer-panel {
  width: 400px;
  height: 480px;
  border: 2px solid #333;
  box-shadow: 0 0 10px #000;
  background: #fff;
  position: relative;
  display: none; /* Hidden initially */
}

#visualizer-panel::after {
  content: "3D Scene Visualizer";
  position: absolute;
  top: 10px;
  color: #333;
  font-size: 14px;
  font-weight: bold;
  z-index: 10;
}

#measurement-panel::after {
  content: "AR Ruler - Distance Tool (RGBD Video)";
  position: absolute;
  top: 10px;
  left: 10px;
  color: #333;
  font-size: 14px;
  font-weight: bold;
  z-index: 10;
}

#distance-display {
  font-size: 2rem;
  font-weight: bold;
  text-align: center;
  margin: 20px 0 10px 0;
  padding: 15px;
  background: #f0f0f0;
  border-radius: 10px;
  border: 2px solid #333;
}

#nearest-distance-display {
  font-size: 1.2rem;
  font-weight: bold;
  text-align: center;
  margin: 0 0 10px 0;
  padding: 10px;
  background: #e8f5e8;
  border-radius: 8px;
  border: 2px solid #28a745;
  color: #155724;
  display: block; /* Ensure it's visible */
  min-height: 40px; /* Ensure it has some height */
}

#status-display {
  font-size: 1rem;
  text-align: center;
  margin: 0 0 20px 0;
  padding: 8px;
  background: #e8f4fd;
  border-radius: 5px;
  border: 1px solid #007bff;
  color: #0056b3;
}


#marker-distance-control {
  margin: 0 0 15px 0;
  padding: 8px;
  background: #f8f9fa;
  border-radius: 6px;
  border: 1px solid #6c757d;
}

#marker-distance-control label {
  display: block;
  font-size: 0.8rem;
  font-weight: bold;
  color: #495057;
  margin-bottom: 5px;
}

#marker-distance-slider {
  width: 100%;
  height: 4px;
  border-radius: 2px;
  background: #ddd;
  outline: none;
  margin-bottom: 5px;
  -webkit-appearance: none;
}

#marker-distance-slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: #007bff;
  cursor: pointer;
  border: 2px solid #fff;
  box-shadow: 0 2px 4px rgba(0,0,0,0.2);
}

#marker-distance-slider::-moz-range-thumb {
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: #007bff;
  cursor: pointer;
  border: 2px solid #fff;
  box-shadow: 0 2px 4px rgba(0,0,0,0.2);
}

#marker-distance-value {
  font-size: 0.7rem;
  font-weight: bold;
  text-align: center;
  color: #007bff;
  background: #e3f2fd;
  padding: 5px;
  border-radius: 4px;
  border: 1px solid #bbdefb;
}

#video-selection-top {
  position: absolute;
  top: 10px;
  right: 10px;
  z-index: 1002; /* Higher than left-frame-click-overlay (1001) to take precedence */
  padding: 8px 12px;
  background: rgba(0, 0, 0, 0.8);
  border-radius: 6px;
  border: 1px solid #555;
  backdrop-filter: blur(5px);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
}


#video-selection-top label {
  display: block;
  font-size: 0.7rem;
  font-weight: bold;
  color: #fff;
  margin-bottom: 4px;
}

#video-dropdown {
  width: 200px;
  padding: 4px 6px;
  background: #333;
  color: #fff;
  border: 1px solid #666;
  border-radius: 4px;
  font-size: 0.7rem;
  cursor: pointer;
}

#video-dropdown:focus {
  outline: none;
  border-color: #007bff;
  box-shadow: 0 0 0 2px rgba(0, 123, 255, 0.25);
}

#video-dropdown option {
  background: #333;
  color: #fff;
}

#toggle-panels {
  background: #007bff;
  color: white;
  border: 1px solid #0056b3;
  border-radius: 4px;
  padding: 6px 10px;
  font-size: 14px;
  cursor: pointer;
  transition: all 0.3s ease;
  min-width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
}

#toggle-panels:hover {
  background: #0056b3;
  transform: scale(1.05);
}

#toggle-panels.panels-hidden {
  background: #6c757d;
  border-color: #5a6268;
}

#toggle-panels.panels-hidden:hover {
  background: #5a6268;
}

#crosshair {
  position: absolute;
  top: 50%;
  left: 320px; /* Center of left camera frame (640px / 2) */
  transform: translate(-50%, -50%);
  width: 20px;
  height: 20px;
  pointer-events: none;
  z-index: 1000;
}

#crosshair::before,
#crosshair::after {
  content: '';
  position: absolute;
  background: #ffffff;
  box-shadow: 0 0 2px rgba(0, 0, 0, 0.8);
}

#crosshair::before {
  width: 2px;
  height: 20px;
  left: 50%;
  top: 0;
  transform: translateX(-50%);
}

#crosshair::after {
  width: 20px;
  height: 2px;
  left: 0;
  top: 50%;
  transform: translateY(-50%);
}

#left-frame-click-overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 640px; /* Left frame width */
  height: 480px; /* Left frame height */
  z-index: 1001; /* Above crosshair but below other UI elements */
  cursor: pointer;
  background: transparent;
  /* Invisible but clickable */
}

#control-buttons {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-top: auto;
}

#control-buttons .button-row {
  display: flex;
  flex-direction: row;
  gap: 3px;
}

#control-buttons button {
  padding: 12px 16px;
  font-size: 14px;
  font-weight: bold;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.3s ease;
}

#place-marker {
  background: #28a745;
  color: white;
  flex: 1;
  width: 25%;
}

#place-marker:hover {
  background: #218838;
}

#end-measurement {
  background: #dc3545;
  color: white;
  flex: 1;
  width: 25%;
}

#end-measurement:hover {
  background: #c82333;
}

#reset-measurement {
  background: #6c757d;
  color: white;
  flex: 1;
  width: 25%;
}

#reset-measurement:hover {
  background: #5a6268;
}

#toggle-right-camera {
  background: #17a2b8;
  color: white;
  width: 100%;
}

#toggle-right-camera:hover {
  background: #138496;
}

#draw-plane {
  background: #007bff;
  color: white;
  flex: 1;
  width: 25%;
}

#draw-plane:hover {
  background: #0056b3;
}

#draw-plane.plane-active {
  background: #28a745;
}

#draw-plane.plane-active:hover {
  background: #218838;
}

canvas {
  display: block;
  height: 480px;
  /* Width will be set dynamically by JavaScript */
}

#splash {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: #000;
  opacity: 1;
}

#overlay {
  position: absolute;
  font-size: 16px;
  z-index: 2;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  background: rgba(0, 0, 0, 0.5);
}

#overlay::before {
  position: absolute;
  width: 100%;
  white-space: pre-wrap;
  content: "RGBD Video AR Ruler - Distance Measurement \A Using pre-recorded RGBD stereo video \A (v2_ruler_left_web.mp4 / v2_ruler_right_web.mp4) \A \A 📏 INTERACTIONS: \A • Click LEFT FRAME first time: Place GREEN start marker \A • Click LEFT FRAME second time: Place RED end marker \A • Click LEFT FRAME third time: Clear all \A \A 🔄 Auto-loop • 📹 Dropdown for video • 👁️ Eye button for extended UI";
  top: calc(50% + 50px);
  text-align: center;
  color: #949494;
  font-size: 14px;
  line-height: 1.4;
}

#overlay button {
  background: transparent;
  border: 1px solid white;
  border-radius: 4px;
  color: white;
  padding: 12px 18px;
  text-transform: uppercase;
  cursor: pointer;
}

@keyframes fadeIn {
  0% { opacity: 0; }
  50% { opacity: 0.6; }
  100% { opacity: 1; }
}

@media screen and (max-device-width: 480px) and (orientation: landscape) {
  #main-container { display: none; }
  body::before {
    content: "Rotate device to portrait mode.";
    color: white;
  }
}

@media screen and (max-device-width: 480px) and (orientation: portrait) {
  #main-container {
    display: block !important;
    width: 100% !important;
    padding: 10px !important;
    /* min-height: 100vh !important; */
  }
  
  #container {
    width: 100% !important;
    max-width: 100vw !important;
  }
  
  /* Create a horizontal container for the panels */
  #measurement-panel, #visualizer-panel {
    display: inline-block !important;
    width: 49% !important;
    max-width: 49vw !important;
    height: 500px !important;
    vertical-align: top !important;
  }
  
  /* #measurement-panel {
    margin-right: 2% !important;
  }
  
  #visualizer-panel {
    margin-left: 2% !important;
  }
   */
  /* Ensure panels are hidden by default on mobile */
  #measurement-panel:not([style*="display: flex"]) {
    display: none !important;
  }
  
  #visualizer-panel:not([style*="display: block"]) {
    display: none !important;
  }
  
  /* Override inline styles when panels are shown */
  #measurement-panel[style*="display: flex"] {
    display: inline-block !important;
  }
  
  #visualizer-panel[style*="display: block"] {
    display: inline-block !important;
  }
  
  body::before { content: none; }
}

/* Mobile layout: video frame on top, panels side by side below */
@media screen and (max-width: 768px), screen and (max-height: 768px), (pointer: coarse) {
  html, body {
    overflow: auto !important;
    height: auto !important;
    min-height: 100vh;
    display: block !important;
  }
  
  #main-container {
    display: block !important;
    width: 100% !important;
    /* min-height: 100vh !important; */
  }
  
  /* Hide toggle button on mobile */
  #toggle-right-camera {
    display: none !important;
  }
  
  /* Create a horizontal container for the panels */
  #measurement-panel, #visualizer-panel {
    display: inline-block !important;
    width: 49% !important;
    max-width: 49vw !important;
    height: 500px !important;
    vertical-align: top !important;
  }
  
  /* #measurement-panel {
    margin-right: 2% !important;
  } */
  
  /* Ensure panels are hidden by default on mobile */
  #measurement-panel:not([style*="display: flex"]) {
    display: none !important;
  }
  
  #visualizer-panel:not([style*="display: block"]) {
    display: none !important;
  }
  
  /* Override inline styles when panels are shown */
  #measurement-panel[style*="display: flex"] {
    display: inline-block !important;
  }
  
  #visualizer-panel[style*="display: block"] {
    display: inline-block !important;
  }
}