/* Reset and base styles */
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

html,
body {
  height: 100%;
  overflow: hidden;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
}

/* Main container with performance optimizations */
.binocular-container {
  position: relative;
  width: 100vw;
  height: 100vh;
  background-color: #000;
  overflow: hidden;
  /* Enable hardware acceleration and optimize rendering */
  will-change: transform;
  transform: translateZ(0);
  -webkit-backface-visibility: hidden;
  backface-visibility: hidden;
  /* Optimize compositing */
  isolation: isolate;
}

/* Background image container with GPU acceleration */
.background-image {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-image: url("strategy.png");
  background-size: cover;
  background-position: center center;
  background-repeat: no-repeat;
  /* Enable hardware acceleration for better performance */
  will-change: transform;
  transform: translateZ(0);
  -webkit-backface-visibility: hidden;
  backface-visibility: hidden;
  /* Ensure proper scaling on mobile */
  background-attachment: scroll;
}

/* Viewport mask for binocular effect with optimized animations */
.viewport-mask {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  /* CSS custom properties for dynamic positioning */
  --viewport-x: 50%;
  --viewport-y: 50%;
  --viewport-radius: 150px;
  /* Create the circular viewport hole using a radial gradient */
  background: radial-gradient(
    circle var(--viewport-radius) at var(--viewport-x) var(--viewport-y),
    transparent 0,
    transparent calc(var(--viewport-radius) - 2px),
    rgba(40, 40, 40, 0.3) calc(var(--viewport-radius) - 1px),
    rgba(0, 0, 0, 0.95) calc(var(--viewport-radius) + 1px),
    rgba(0, 0, 0, 0.95) 100%
  );
  /* Optimized smooth transitions for viewport movement */
  transition: background 0.05s cubic-bezier(0.25, 0.46, 0.45, 0.94);
  /* Enable hardware acceleration for better performance */
  will-change: background;
  transform: translateZ(0);
}

/* Add subtle vignette effect inside the viewport for realism with optimized animations */
.viewport-mask::after {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: radial-gradient(
    circle calc(var(--viewport-radius) * 0.8) at var(--viewport-x) var(--viewport-y),
    transparent 0,
    transparent 60%,
    rgba(0, 0, 0, 0.1) 100%
  );
  /* Create a circular clip to only show vignette inside viewport */
  clip-path: circle(var(--viewport-radius) at var(--viewport-x) var(--viewport-y));
  /* Optimized smooth transitions with hardware acceleration */
  transition: clip-path 0.05s cubic-bezier(0.25, 0.46, 0.45, 0.94);
  will-change: clip-path;
  transform: translateZ(0);
}

/* Loading indicator with enhanced error handling styles */
.loading-indicator {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  color: white;
  text-align: center;
  z-index: 1000;
  font-size: 1.2rem;
  max-width: 90%;
  padding: 20px;
  background: rgba(0, 0, 0, 0.8);
  border-radius: 10px;
  transition: opacity 0.3s ease;
}

/* Error state styling */
.loading-indicator.error {
  background: rgba(139, 0, 0, 0.9);
  border: 2px solid #ff6b6b;
  color: #fff;
  max-width: 500px;
}

.loading-indicator.critical {
  background: rgba(139, 0, 0, 0.95);
  border: 3px solid #ff4757;
  animation: pulse-error 2s infinite;
}

/* Error state content */
.error-state h3,
.initialization-error h2 {
  margin-bottom: 15px;
  color: #ff6b6b;
}

.error-state p,
.initialization-error p {
  margin-bottom: 10px;
  line-height: 1.4;
}

.retry-button {
  background: #4caf50;
  color: white;
  border: none;
  padding: 10px 20px;
  border-radius: 5px;
  cursor: pointer;
  font-size: 1rem;
  margin: 10px 5px;
  transition: background 0.3s ease;
}

.retry-button:hover {
  background: #45a049;
}

.retry-button:active {
  transform: translateY(1px);
}

.error-details {
  margin-top: 20px;
  padding: 15px;
  background: rgba(0, 0, 0, 0.3);
  border-radius: 5px;
  font-size: 0.9rem;
  text-align: left;
}

.error-details ul {
  margin: 10px 0;
  padding-left: 20px;
}

.error-details li {
  margin: 5px 0;
}

.error-actions {
  margin: 20px 0;
}

/* User error toast notifications */
.user-error-toast {
  position: fixed;
  top: 20px;
  right: 20px;
  z-index: 2000;
  animation: slideInRight 0.3s ease;
}

.error-toast-content {
  background: rgba(139, 0, 0, 0.95);
  color: white;
  padding: 15px 20px;
  border-radius: 8px;
  border-left: 4px solid #ff6b6b;
  display: flex;
  align-items: center;
  gap: 10px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
  max-width: 400px;
}

.error-icon {
  font-size: 1.2rem;
  flex-shrink: 0;
}

.error-message {
  flex: 1;
  font-size: 0.9rem;
  line-height: 1.3;
}

.error-close {
  background: none;
  border: none;
  color: white;
  font-size: 1.5rem;
  cursor: pointer;
  padding: 0;
  width: 24px;
  height: 24px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  transition: background 0.2s ease;
}

.error-close:hover {
  background: rgba(255, 255, 255, 0.2);
}

/* Animations */
@keyframes pulse-error {
  0%,
  100% {
    border-color: #ff4757;
    box-shadow: 0 0 0 0 rgba(255, 71, 87, 0.7);
  }
  50% {
    border-color: #ff6b6b;
    box-shadow: 0 0 0 10px rgba(255, 71, 87, 0);
  }
}

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

/* Hide loading indicator when page is loaded (only if no error) */
.loaded .loading-indicator:not(.error) {
  display: none;
}

/* Browser compatibility fallback styles */
.no-clip-path .viewport-mask {
  /* Fallback styling when clip-path is not supported */
  background: rgba(0, 0, 0, 0.95) !important;
}

.no-custom-properties .viewport-mask {
  /* Fallback for browsers without CSS custom properties */
  background: radial-gradient(
    circle 150px at 50% 50%,
    transparent 0,
    transparent 148px,
    rgba(40, 40, 40, 0.3) 149px,
    rgba(0, 0, 0, 0.95) 151px,
    rgba(0, 0, 0, 0.95) 100%
  ) !important;
}

.legacy-browser .viewport-mask {
  /* Very basic fallback for legacy browsers */
  background: #000 !important;
  opacity: 0.95 !important;
}

.legacy-browser .background-image {
  /* Simplified background for legacy browsers */
  background-attachment: scroll !important;
  background-size: contain !important;
}

/* ===== RESPONSIVE DESIGN ===== */

/* Mobile first approach - Base styles for mobile */
@media (max-width: 480px) {
  .viewport-mask {
    --viewport-radius: 80px;
  }

  .binocular-container {
    touch-action: none;
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
  }

  .background-image {
    /* Force proper background sizing on small mobile devices */
    background-size: cover !important;
    background-position: center center !important;
    /* Fix for iOS Safari background-attachment issues */
    background-attachment: scroll !important;
    /* Ensure minimum size to prevent tiny backgrounds */
    min-width: 100vw;
    min-height: 100vh;
    /* Alternative fallback using background-size auto if cover fails */
    background-size: auto 100%, cover;
  }

  .loading-indicator {
    font-size: 1rem;
    padding: 0 20px;
  }
}

/* Small tablets and large phones */
@media (min-width: 481px) and (max-width: 768px) {
  .viewport-mask {
    --viewport-radius: 100px;
  }

  .binocular-container {
    touch-action: none;
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
  }

  .background-image {
    /* Ensure proper background sizing on larger mobile devices */
    background-size: cover !important;
    background-position: center center !important;
    background-attachment: scroll !important;
    min-width: 100vw;
    min-height: 100vh;
  }
}

/* Tablets */
@media (min-width: 769px) and (max-width: 1024px) {
  .viewport-mask {
    --viewport-radius: 120px;
  }
}

/* Desktop and larger screens */
@media (min-width: 1025px) {
  .viewport-mask {
    --viewport-radius: 150px;
  }
}

/* Very large screens */
@media (min-width: 1440px) {
  .viewport-mask {
    --viewport-radius: 180px;
  }
}

/* ===== ORIENTATION HANDLING ===== */

/* Portrait orientation adjustments */
@media (orientation: portrait) {
  .viewport-mask {
    /* Adjust radius based on screen width in portrait */
    --viewport-radius: min(15vw, 120px);
  }

  .background-image {
    /* Ensure image covers properly in portrait */
    background-position: center center !important;
    background-size: cover !important;
    /* Force full coverage in portrait mode */
    width: 100vw !important;
    height: 100vh !important;
    min-width: 100vw;
    min-height: 100vh;
    /* Alternative sizing for problematic devices */
    background-size: auto 100%, cover;
  }
}

/* Landscape orientation adjustments */
@media (orientation: landscape) {
  .viewport-mask {
    /* Adjust radius based on screen height in landscape */
    --viewport-radius: min(20vh, 150px);
  }

  .background-image {
    /* Optimize for landscape viewing */
    background-position: center center !important;
    background-size: cover !important;
    /* Force full coverage in landscape mode */
    width: 100vw !important;
    height: 100vh !important;
    min-width: 100vw;
    min-height: 100vh;
  }
}

/* ===== DEVICE-SPECIFIC OPTIMIZATIONS ===== */

/* High DPI displays */
@media (-webkit-min-device-pixel-ratio: 2), (min-resolution: 192dpi) {
  .background-image {
    background-size: cover;
    /* Ensure crisp rendering on retina displays */
    image-rendering: -webkit-optimize-contrast;
    image-rendering: crisp-edges;
  }
}

/* Touch devices with optimized performance */
@media (hover: none) and (pointer: coarse) {
  .binocular-container {
    /* Optimize for touch interaction */
    touch-action: none;
    -webkit-touch-callout: none;
    -webkit-tap-highlight-color: transparent;
    /* Fix viewport issues on mobile */
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
  }

  .background-image {
    /* Mobile-specific background image fixes */
    background-size: cover !important;
    background-position: center center !important;
    background-attachment: scroll !important;
    /* Force proper dimensions */
    position: fixed !important;
    top: 0 !important;
    left: 0 !important;
    width: 100vw !important;
    height: 100vh !important;
    min-width: 100vw !important;
    min-height: 100vh !important;
    /* iOS Safari specific fixes */
    -webkit-background-size: cover !important;
    /* Prevent background from being too small */
    background-size: max(100vw, 100vh) auto, cover !important;
  }

  .viewport-mask {
    /* Ultra-fast transitions for touch with hardware acceleration */
    transition: background 0.02s cubic-bezier(0.25, 0.46, 0.45, 0.94);
    /* Force hardware acceleration on mobile */
    -webkit-transform: translateZ(0);
    transform: translateZ(0);
    -webkit-backface-visibility: hidden;
    backface-visibility: hidden;
    -webkit-perspective: 1000px;
    perspective: 1000px;
  }

  .viewport-mask::after {
    transition: clip-path 0.02s cubic-bezier(0.25, 0.46, 0.45, 0.94);
    -webkit-transform: translateZ(0);
    transform: translateZ(0);
    -webkit-backface-visibility: hidden;
    backface-visibility: hidden;
  }
}

/* Devices with hover capability (desktop) with optimized animations */
@media (hover: hover) and (pointer: fine) {
  .viewport-mask {
    /* Optimized smooth transitions for mouse interaction */
    transition: background 0.05s cubic-bezier(0.25, 0.46, 0.45, 0.94);
  }

  .viewport-mask::after {
    transition: clip-path 0.05s cubic-bezier(0.25, 0.46, 0.45, 0.94);
  }
}

/* ===== ACCESSIBILITY AND PERFORMANCE ===== */

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

  .viewport-mask,
  .viewport-mask::after {
    transition: none !important;
    will-change: auto !important;
  }
}

/* Performance optimization for low-end devices */
@media (max-width: 480px) and (max-height: 800px) {
  .viewport-mask {
    /* Reduce transition complexity on low-end mobile devices */
    transition: background 0.03s linear;
  }

  .viewport-mask::after {
    transition: clip-path 0.03s linear;
  }
}

/* High refresh rate displays optimization */
@media (min-resolution: 120dpi) and (hover: hover) {
  .viewport-mask {
    /* Smoother transitions for high refresh rate displays */
    transition: background 0.04s cubic-bezier(0.25, 0.46, 0.45, 0.94);
  }

  .viewport-mask::after {
    transition: clip-path 0.04s cubic-bezier(0.25, 0.46, 0.45, 0.94);
  }
}

/* High contrast mode support */
@media (prefers-contrast: high) {
  .viewport-mask {
    background: radial-gradient(
      circle var(--viewport-radius) at var(--viewport-x) var(--viewport-y),
      transparent 0,
      transparent calc(var(--viewport-radius) - 2px),
      rgba(255, 255, 255, 0.1) calc(var(--viewport-radius) - 1px),
      rgba(0, 0, 0, 1) calc(var(--viewport-radius) + 1px),
      rgba(0, 0, 0, 1) 100%
    );
  }
}

/* ===== RESPONSIVE TYPOGRAPHY ===== */

@media (max-width: 480px) {
  .loading-indicator {
    font-size: 0.9rem;
    line-height: 1.4;
  }
}

@media (min-width: 1200px) {
  .loading-indicator {
    font-size: 1.4rem;
  }
}

/* ===== MOBILE BACKGROUND IMAGE FIXES ===== */

/* Specific fixes for very small mobile screens */
@media (max-width: 360px), (max-height: 640px) {
  .background-image {
    /* Force background to fill entire screen on very small devices */
    background-size: cover !important;
    background-position: center center !important;
    background-attachment: scroll !important;
    position: fixed !important;
    top: 0 !important;
    left: 0 !important;
    width: 100vw !important;
    height: 100vh !important;
    min-width: 100vw !important;
    min-height: 100vh !important;
    max-width: none !important;
    max-height: none !important;
    /* Fallback for browsers that don't handle cover properly */
    background-size: 100% 100%, cover !important;
  }
}

/* iOS Safari specific fixes */
@supports (-webkit-touch-callout: none) {
  .background-image {
    /* iOS Safari background-attachment fix */
    background-attachment: scroll !important;
    /* Force proper sizing on iOS */
    -webkit-background-size: cover !important;
    background-size: cover !important;
    /* Prevent iOS from shrinking the background */
    min-width: 100vw !important;
    min-height: 100vh !important;
  }
}

/* Android Chrome specific fixes */
@media screen and (-webkit-min-device-pixel-ratio: 0) and (min-resolution: 0.001dpcm) {
  .background-image {
    /* Android Chrome background fixes */
    background-size: cover !important;
    background-position: center center !important;
    transform: translateZ(0) !important;
  }
}

/* ===== SAFE AREA HANDLING (for devices with notches) ===== */

@supports (padding: max(0px)) {
  .binocular-container {
    padding-top: env(safe-area-inset-top);
    padding-bottom: env(safe-area-inset-bottom);
    padding-left: env(safe-area-inset-left);
    padding-right: env(safe-area-inset-right);
  }
}

/* ===== TOUCH INSTRUCTIONS (for mobile devices) ===== */

.touch-instructions {
  position: fixed;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(0, 0, 0, 0.8);
  color: white;
  padding: 8px 16px;
  border-radius: 20px;
  font-size: 0.8rem;
  text-align: center;
  z-index: 1000;
  opacity: 0;
  transition: opacity 0.3s ease;
  pointer-events: none;
}

.touch-instructions.show {
  opacity: 1;
}

/* Hide instructions on desktop */
@media (hover: hover) and (pointer: fine) {
  .touch-instructions {
    display: none;
  }
}

/* Show instructions on touch devices initially */
@media (hover: none) and (pointer: coarse) {
  .touch-instructions.initial {
    opacity: 1;
  }
}
