/* AI Try-On Modal Responsive Styles */
#tryonFacePreview,
#tryonResultImage {
  image-orientation: from-image;
}

/* DEFAULT: Result image displays full width on both desktop and mobile */
#tryonResultImage {
  width: 100% !important;
  max-width: 100% !important;
  height: auto !important;
  max-height: none !important;
  object-fit: contain !important;
  object-position: center !important;
  display: block !important;
  margin: 0 auto !important;
  min-width: 0 !important;
  min-height: 0 !important;
}

/* Result box: allow dynamic height to show full image */
#tryonResultBox {
  min-height: 200px !important;
  max-height: none !important;
  overflow: visible !important;
  display: flex !important;
  flex-direction: column !important;
  align-items: center !important;
  justify-content: center !important;
  width: 100% !important;
  box-sizing: border-box !important;
}

/* Avoid squashing face preview to a square box (neutralize Bootstrap's .ratio in our modal) */
#tryOnAIModal .ratio,
#tryOnAIModal .ratio-1x1 {
  /* disable fixed aspect-ratio wrappers inside the face area */
  aspect-ratio: auto !important;
  padding-top: 0 !important;
  height: auto !important;
}

/* Ensure the face image sizes naturally */
#tryOnAIModal img#tryonFacePreview {
  position: static !important;
  /* override .ratio child absolute fill */
  width: 100% !important;
  height: auto !important;
  max-width: 100% !important;
  object-fit: contain !important;
  display: block !important;
}

/* Ensure modal is above everything (header, nav, etc.) */
#tryOnAIModal {
  z-index: 9999 !important;
}

#tryOnAIModal .modal-dialog {
  z-index: 10000 !important;
}

#tryOnAIModal .ratio::before {
  /* Disable Bootstrap ratio padding box */
  display: none !important;
  padding-top: 0 !important;
}

#tryOnAIModal .ratio>* {
  /* Cancel absolute fill applied by .ratio utility */
  position: static !important;
  width: 100% !important;
  height: auto !important;
}


#tryOnAIModal .modal-backdrop {
  z-index: 9998 !important;
}

/* Prevent body scroll when modal is open (avoid layout shifts) */
body.modal-open {
  overflow: hidden !important;
  position: fixed !important;
  width: 100% !important;
}

/* Desktop: nice layout */
@media (min-width: 768px) {
  #tryOnAIModal .modal-dialog {
    max-width: 900px;
  }
}

/* Mobile responsive improvements */
@media (max-width: 767.98px) {

  /* Modal takes full viewport on mobile */
  #tryOnAIModal .modal-dialog {
    margin: 0;
    max-width: 100%;
    height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
  }

  #tryOnAIModal .modal-content {
    border-radius: 0;
    max-height: 100vh;
    overflow-y: auto;
  }

  #tryOnAIModal .modal-body {
    padding: 1rem;
  }

  /* Stack vertically on mobile instead of side-by-side */
  #tryOnAIModal .row {
    flex-direction: column !important;
  }

  #tryOnAIModal .col-md-5,
  #tryOnAIModal .col-md-7 {
    max-width: 100% !important;
    width: 100% !important;
  }

  /* Face preview area */
  #tryOnAIModal .ratio {
    /* neutralize enforced square on mobile too */
    aspect-ratio: auto !important;
    padding-top: 0 !important;
    height: auto !important;
    max-width: 100%;
    margin: 0 auto 1rem;
  }

  /* On mobile, ensure face preview never gets stretched */
  #tryonFaceArea img#tryonFacePreview {
    width: 100% !important;
    height: auto !important;
    object-fit: contain !important;
  }

  /* Result box on mobile: allow dynamic height to accommodate full image */
  #tryonResultBox {
    min-height: 200px !important;
    max-height: none !important; /* Remove height limit to show full image */
    overflow: visible !important; /* Allow full image display */
    display: flex !important;
    flex-direction: column !important;
    align-items: center !important;
    justify-content: center !important;
    /* Prevent horizontal overflow */
    max-width: 100% !important;
    width: 100% !important;
    box-sizing: border-box !important;
  }

  /* Result image: FULL width to show entire image without horizontal crop */
  #tryonResultImage {
    /* PRIORITY: Full width to prevent horizontal cropping */
    width: 100% !important;
    max-width: 100% !important;
    /* Let height adjust automatically to maintain aspect ratio */
    height: auto !important;
    max-height: none !important;
    /* Contain without cropping - show full image */
    object-fit: contain !important;
    object-position: center !important;
    /* Display and alignment */
    display: block !important;
    margin: 0 auto !important;
    /* Remove any inherited sizing */
    min-width: 0 !important;
    min-height: 0 !important;
    aspect-ratio: auto !important;
    /* Force orientation */
    image-orientation: from-image !important;
  }

  /* Wrapper constraint to prevent any overflow */
  #tryOnAIModal .col-md-7 {
    overflow-x: hidden !important;
    overflow-y: visible !important;
    max-width: 100% !important;
  }

  /* Additional constraint for modal body on mobile */
  #tryOnAIModal .modal-body {
    overflow-x: hidden !important;
    max-width: 100vw !important;
    box-sizing: border-box !important;
  }

  /* Placeholder text */
  #tryonResultPlaceholder {
    text-align: center;
    padding: 1rem;
  }

  /* Loading spinner */
  #tryonLoading {
    padding: 2rem 1rem;
  }

  /* Buttons stack on mobile */
  #tryOnAIModal .d-flex.gap-2 {
    flex-direction: column !important;
    gap: 0.5rem !important;
  }

  #tryOnAIModal .d-flex.gap-2 button {
    width: 100% !important;
  }

  /* Status text smaller on mobile */
  #tryonStatus {
    font-size: 0.875rem;
    line-height: 1.4;
  }

  /* Glasses preview thumbnail */
  #tryonSelectedGlassesWrap {
    margin: 0.5rem 0 !important;
  }

  #tryonSelectedGlassesWrap img {
    width: 64px !important;
    height: auto;
  }

  /* Reference log text wrap */
  #tryonRefLog {
    font-size: 0.75rem;
    word-break: break-all;
  }

  #tryonRefLog a {
    display: inline-block;
    max-width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
}

/* Extra small devices (portrait phones, < 576px) */
@media (max-width: 575.98px) {
  #tryOnAIModal .modal-header {
    padding: 0.75rem 1rem;
  }

  #tryOnAIModal .modal-title {
    font-size: 1.1rem;
  }

  #tryonResultBox {
    min-height: 200px !important;
    max-height: none !important; /* Allow full height for image */
    overflow: visible !important;
    padding: 0.5rem !important;
    display: flex !important;
    flex-direction: column !important;
    align-items: center !important;
    justify-content: center !important;
    width: 100% !important;
    box-sizing: border-box !important;
  }

  #tryonResultImage {
    width: 100% !important; /* Full width - no horizontal crop */
    max-width: 100% !important;
    height: auto !important; /* Auto height to maintain aspect */
    max-height: none !important; /* Remove height restriction */
    object-fit: contain !important;
    display: block !important;
    margin: 0 auto !important;
    /* Critical: prevent any horizontal expansion */
    min-width: 0 !important;
  }

  /* Form controls full width */
  #tryonFaceInput {
    font-size: 0.875rem;
  }

  /* Smaller buttons */
  #tryonGenerateBtn,
  #tryonDownloadBtn,
  #tryonRetryBtn {
    padding: 0.5rem 1rem;
    font-size: 0.875rem;
  }
}

/* Landscape mobile phones */
@media (max-height: 600px) and (orientation: landscape) {
  #tryOnAIModal .modal-content {
    max-height: 100vh;
    overflow-y: auto;
  }

  #tryonResultBox {
    min-height: 180px !important;
    max-height: none !important; /* Allow full display */
    overflow: visible !important;
  }

  #tryonResultImage {
    width: 100% !important; /* Full width */
    max-width: 100% !important;
    height: auto !important; /* Auto height */
    max-height: none !important;
  }

  #tryOnAIModal .ratio {
    aspect-ratio: auto !important;
  }
}

/* Ensure smooth animations */
#tryOnAIModal .modal.fade .modal-dialog {
  transition: transform 0.3s ease-out;
}

#tryOnAIModal.show .modal-dialog {
  transform: none;
}

/* Fix for iOS Safari bottom safe area (no @supports to satisfy older linters) */
@media (max-width: 767.98px) {
  #tryOnAIModal .modal-content {
    /* Old iOS fallback */
    padding-bottom: constant(safe-area-inset-bottom, 0px);
    /* Modern iOS */
    padding-bottom: env(safe-area-inset-bottom, 0px);
  }
}