/* --- Desktop Version (781px and above) --- */
@media (min-width: 781px) {
  /* The Overlay */
  .img-modal {
    display: none; /* Controlled by JS flex/none */
    position: fixed;
    z-index: 9999;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.9);
    
    /* Flexbox ensures perfect horizontal and vertical centering */
    display: none; 
    align-items: center;
    justify-content: center;
    overflow: hidden; 
  }

  /* The Image */
  .img-modal-content {
    margin: auto;
    display: block;
    max-width: 85%;
    max-height: 85vh; /* Prevents image from touching bottom */
    border-radius: 4px;
    box-shadow: 0 0 25px rgba(0, 0, 0, 0.5);
    transition: opacity 0.2s ease;
    z-index: 10001;
  }

  /* Close Button */
  .img-modal-close {
    position: absolute;
    top: 20px;
    right: 35px;
    color: #fff;
    font-size: 40px;
    font-weight: bold;
    cursor: pointer;
    z-index: 10002;
    transition: color 0.2s;
  }

  .img-modal-close:hover {
    color: #bbb;
  }

  /* Desktop Navigation Arrows */
  .modal-nav {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    background: rgba(255, 255, 255, 0.1);
    color: white;
    padding: 20px 15px;
    border: none;
    font-size: 35px;
    cursor: pointer;
    border-radius: 5px;
    transition: background 0.3s;
    user-select: none;
    z-index: 10002;
  }

  .modal-nav:hover {
    background: rgba(255, 255, 255, 0.3);
  }

  .modal-prev {
    left: 30px;
  }

  .modal-next {
    right: 30px;
  }
}

/* --- Mobile Version (780px and below) --- */
@media (max-width: 780px) {
  .img-modal {
    display: none; /* Controlled by JS */
    position: fixed;
    z-index: 10000;
    left: 0;
    top: 0;
    width: 100vw;
    height: 100vh;
    background: #000; /* Solid black for immersive mobile gallery */
    
    /* Flexbox is crucial for centering during swipe gestures */
    display: none; 
    align-items: center;
    justify-content: center;
    overflow: hidden; /* Prevent page scroll when modal is open */
  }

  /* When JS sets display to flex */
  .img-modal[style*="display: flex"] {
    display: flex !important;
  }

  .img-modal-content {
    width: 100%;
    height: auto;
    max-height: 80vh; /* Leave room for thumb gestures top/bottom */
    object-fit: contain;
    /* Hardware acceleration for smoother swiping */
    will-change: transform;
    user-select: none;
    -webkit-user-drag: none;
  }

  .img-modal-close {
    position: absolute;
    top: 15px;
    right: 15px;
    color: #fff;
    font-size: 32px;
    width: 44px; /* Standard mobile touch target */
    height: 44px;
    background: rgba(255, 255, 255, 0.2);
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 10001;
    cursor: pointer;
    /* Remove default gray box on tap */
    -webkit-tap-highlight-color: transparent;
  }
}