/* =====================================================================
   IMPROVED IMAGE PREVIEW — FIXES & UX ENHANCEMENTS
   ===================================================================== */

/* ---- IMAGE THUMBNAIL IN CHAT ---- */

.ai-response-image-wrap {
  position: relative;
  display: inline-block;
  max-width: 100%;
  margin: 10px 0 6px;
  border-radius: 12px;
  overflow: hidden;
  cursor: zoom-in;
  transition: all .3s ease;
  background: var(--chat-surface);
  border: 1px solid var(--chat-border);
  /* Prevents the image from causing layout shift while loading */
  aspect-ratio: auto;
}

.ai-response-image-wrap:hover {
  transform: scale(1.02);
  box-shadow: 0 8px 24px rgba(0, 0, 0, .3);
  border-color: rgba(138, 180, 248, .35);
}

/* LOADING STATE: shown while img is fetching */
.ai-response-image-wrap.loading::before {
  content: '';
  position: absolute;
  inset: 0;
  background: var(--chat-surface);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 2;
  border-radius: 12px;
}

.ai-response-image-wrap.loading::after {
  content: '';
  position: absolute;
  inset: 50%;
  transform: translate(-50%, -50%);
  width: 20px;
  height: 20px;
  border: 2px solid rgba(138, 180, 248, .3);
  border-top-color: var(--chat-accent);
  border-radius: 50%;
  animation: spin .8s linear infinite;
  z-index: 3;
}

/* ERROR STATE: shown if image fails to load */
.ai-response-image-wrap.error {
  min-width: 240px;
  min-height: 160px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(255, 107, 107, .08);
  border-color: rgba(255, 107, 107, .25);
  cursor: default;
}

.ai-response-image-wrap.error::before {
  content: '⚠ Image unavailable';
  color: var(--chat-danger);
  font-size: .85rem;
  font-family: var(--chat-font);
  font-weight: 500;
  text-align: center;
  padding: 20px;
  line-height: 1.5;
}

.ai-response-image-wrap.error:hover {
  transform: none;
  box-shadow: none;
}

/* The actual image element */
.ai-response-image {
  display: block;
  width: 100%;
  max-width: 320px;
  border-radius: 12px;
  background: var(--chat-surface);
  transition: all .2s ease;
  /* Lazy loading */
  loading: lazy;
  decoding: async;
}

/* IMAGE LABEL (top-right corner) */
.ai-response-image-label {
  position: absolute;
  top: 8px;
  right: 8px;
  background: rgba(0, 0, 0, .75);
  color: #fff;
  padding: 5px 12px;
  border-radius: 6px;
  font-size: 11px;
  font-family: var(--chat-font);
  font-weight: 500;
  letter-spacing: .3px;
  opacity: 0;
  transition: opacity .2s ease;
  backdrop-filter: blur(4px);
  pointer-events: none;
  z-index: 1;
  white-space: nowrap;
}

/* Show label on hover (desktop) or always on mobile */
.ai-response-image-wrap:hover .ai-response-image-label {
  opacity: 1;
}

@media (pointer: coarse) {
  .ai-response-image-label { opacity: 0.85; }
}

/* ---- IMAGE DOWNLOAD BUTTON (bottom-right corner) ---- */

.ai-response-image-download {
  position: absolute;
  bottom: 8px;
  right: 8px;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: rgba(0, 0, 0, .8);
  border: 1px solid rgba(255, 255, 255, .2);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  color: #fff;
  transition: all .2s ease;
  backdrop-filter: blur(4px);
  opacity: 0;
  transform: scale(0.8);
  pointer-events: none;
  z-index: 1;
  touch-action: manipulation;
}

.ai-response-image-wrap:hover .ai-response-image-download,
.ai-response-image-wrap:focus-within .ai-response-image-download {
  opacity: 1;
  transform: scale(1);
  pointer-events: auto;
}

@media (pointer: coarse) {
  /* On touch devices, always show download button */
  .ai-response-image-download {
    opacity: 0.75;
    transform: scale(1);
    pointer-events: auto;
  }
}

.ai-response-image-download:hover {
  background: rgba(0, 0, 0, .95);
  transform: scale(1.15);
  box-shadow: 0 6px 16px rgba(0, 0, 0, .5);
}

.ai-response-image-download:active {
  transform: scale(1.05);
}

.ai-response-image-download svg {
  width: 16px;
  height: 16px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

/* ---- FULLSCREEN IMAGE VIEWER MODAL ---- */

.image-viewer-modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, .98);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 1001;
  backdrop-filter: blur(8px);
  animation: fadeIn .25s ease;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
  /* Prevent scroll behind modal */
  overflow: hidden;
}

.image-viewer-modal.active {
  display: flex;
}

/* FIX: Close button must be clickable and visible */
.image-viewer-modal.active {
  animation: fadeIn .25s ease;
}

.image-viewer-container {
  position: relative;
  max-width: 90vw;
  max-height: 90vh;
  display: flex;
  align-items: center;
  justify-content: center;
  animation: zoomIn .3s cubic-bezier(0.34, 1.56, 0.64, 1);
}

.image-viewer-image {
  max-width: 100%;
  max-height: 100%;
  border-radius: 12px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, .8);
  /* Prevent dragging the image (better UX on mobile) */
  user-select: none;
  -webkit-user-select: none;
  cursor: grab;
  transition: filter .2s ease;
}

.image-viewer-image:active {
  cursor: grabbing;
}

/* ---- CLOSE BUTTON (TOP-RIGHT, INSIDE MODAL) ---- */

.image-viewer-close {
  position: absolute;
  top: 16px;
  right: 16px;
  width: 40px;
  height: 40px;
  background: rgba(255, 255, 255, .12);
  border: 1px solid rgba(255, 255, 255, .2);
  border-radius: 50%;
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all .2s ease;
  font-size: 24px;
  line-height: 1;
  padding: 0;
  z-index: 10;
  touch-action: manipulation;
}

.image-viewer-close:hover {
  background: rgba(255, 255, 255, .2);
  border-color: rgba(255, 255, 255, .4);
  transform: scale(1.1) rotate(90deg);
}

.image-viewer-close:active {
  transform: scale(0.95);
}

.image-viewer-close:focus-visible {
  outline: 2px solid rgba(138, 180, 248, .6);
  outline-offset: 2px;
}

.image-viewer-close svg {
  width: 20px;
  height: 20px;
  stroke: currentColor;
  fill: none;
  stroke-width: 2.5;
  stroke-linecap: round;
  stroke-linejoin: round;
}

/* ---- IMAGE INFO & DOWNLOAD (BOTTOM OF MODAL) ---- */

.image-viewer-controls {
  position: absolute;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  gap: 12px;
  background: rgba(0, 0, 0, .7);
  padding: 10px 16px;
  border-radius: 12px;
  backdrop-filter: blur(8px);
  border: 1px solid rgba(255, 255, 255, .15);
  z-index: 5;
  animation: slideUp .3s ease .15s both;
}

.image-viewer-info {
  font-size: 12px;
  color: rgba(255, 255, 255, .7);
  font-family: var(--chat-font);
  letter-spacing: .2px;
  white-space: nowrap;
}

.image-viewer-download-btn {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 12px;
  border-radius: 6px;
  border: 1px solid rgba(255, 255, 255, .3);
  background: rgba(138, 180, 248, .2);
  color: rgba(138, 180, 248, .9);
  font-family: var(--chat-font);
  font-size: 12px;
  font-weight: 500;
  cursor: pointer;
  transition: all .2s ease;
  touch-action: manipulation;
  white-space: nowrap;
}

.image-viewer-download-btn:hover {
  background: rgba(138, 180, 248, .3);
  border-color: rgba(138, 180, 248, .6);
  color: #fff;
  transform: translateY(-1px);
}

.image-viewer-download-btn:active {
  transform: scale(0.95);
}

.image-viewer-download-btn svg {
  width: 14px;
  height: 14px;
  stroke: currentColor;
  fill: none;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

/* ---- RESPONSIVE ADJUSTMENTS ---- */

@media (max-width: 759px) {
  .image-viewer-modal {
    padding-top: max(16px, env(safe-area-inset-top));
    padding-bottom: max(16px, env(safe-area-inset-bottom));
  }

  .image-viewer-container {
    max-width: 95vw;
    max-height: 85vh;
  }

  .image-viewer-close {
    top: 12px;
    right: 12px;
    width: 36px;
    height: 36px;
    font-size: 20px;
  }

  .image-viewer-close svg {
    width: 18px;
    height: 18px;
  }

  .image-viewer-controls {
    bottom: 12px;
    left: 12px;
    right: 12px;
    transform: none;
    width: auto;
  }

  .ai-response-image-wrap {
    max-width: 90vw;
  }

  .ai-response-image {
    max-width: 100%;
  }
}

@media (max-width: 420px) {
  .image-viewer-controls {
    flex-direction: column;
    gap: 8px;
    width: calc(100% - 24px);
  }

  .image-viewer-info {
    width: 100%;
    text-align: center;
    font-size: 11px;
  }

  .image-viewer-download-btn {
    width: 100%;
    justify-content: center;
    font-size: 11px;
    padding: 8px;
  }

  .ai-response-image-label {
    font-size: 10px;
    padding: 4px 8px;
    top: 6px;
    right: 6px;
  }

  .ai-response-image-download {
    width: 32px;
    height: 32px;
    bottom: 6px;
    right: 6px;
  }

  .ai-response-image-download svg {
    width: 14px;
    height: 14px;
  }
}

/* ---- KEYBOARD & ACCESSIBILITY ---- */

/* Trap focus inside modal when active */
.image-viewer-modal.active {
  outline: none;
}

.image-viewer-close:focus {
  outline: 2px solid rgba(138, 180, 248, .8);
  outline-offset: 3px;
}

/* ---- PEXELS CREDIT ATTRIBUTION ---- */

.pexels-credit {
  font-family: var(--chat-font);
  font-size: 10.5px;
  color: var(--chat-text-faint);
  margin: -2px 0 6px 2px;
  letter-spacing: .1px;
}

.pexels-credit a {
  color: var(--chat-text-dim);
  text-decoration: underline;
  transition: color .2s ease;
}

.pexels-credit a:hover {
  color: var(--chat-text);
}

/* ---- ANIMATIONS (if not already defined) ---- */

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

@keyframes zoomIn {
  from { opacity: 0; transform: scale(.85); }
  to { opacity: 1; transform: scale(1); }
}

@keyframes slideUp {
  from { opacity: 0; transform: translateY(12px); }
  to { opacity: 1; transform: translateY(0); }
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* ---- BROWSER PRINT STYLES ---- */

@media print {
  .image-viewer-modal,
  .ai-response-image-label,
  .ai-response-image-download,
  .image-viewer-close {
    display: none !important;
  }
}
