:root {
  --background-color: var(--black2-color);
  --text-color: var(--white2-color);
  --card-bg: rgba(30, 30, 36, 0.7);
  --card-hover: rgba(40, 40, 46, 0.9);
  --primary-accent: var(--primary-color);
  --secondary-accent: var(--secondary-color);
  --transition-speed: 0.3s;
  --gallery-gap: 16px;
  --header-bg: rgba(20, 20, 26, 0.6);
  --input-bg: rgba(40, 40, 46, 0.6);
  --button-hover: rgba(60, 60, 70, 0.8);
}

body {
  background-color: var(--background-color);
  color: var(--text-color);
  font-family: 'Montserrat', sans-serif;
}
/* Main gallery container */
.gallery-container {
  width: 100%;
  max-width: 1800px;
  margin: 0 auto;
  padding: 20px;
}

/* Enhanced Gallery header with title and filters */
.gallery-header {
  margin-bottom: 40px;
  background-color: var(--header-bg);
  border-radius: 15px;
  padding: 25px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.2);
  border: 1px solid rgba(255, 255, 255, 0.05);
  position: relative;
  overflow: hidden;
}

.gallery-header::before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 5px;
  background: linear-gradient(90deg, var(--primary-accent), var(--secondary-accent));
  z-index: 1;
}

.header-content {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 25px;
}

.header-title-area {
  flex: 1;
}

.gallery-header h1 {
  font-size: 3.5rem;
  font-weight: 800;
  margin-bottom: 10px;
  letter-spacing: 2px;
  text-transform: uppercase;
  position: relative;
  display: inline-block;
}

.gallery-header h1::after {
  content: "";
  position: absolute;
  bottom: -5px;
  left: 0;
  width: 80px;
  height: 4px;
  background: var(--primary-accent);
}

.gallery-subtitle {
  font-size: 1.1rem;
  margin: 0;
  margin-top: 15px;
  opacity: 0.8;
  max-width: 600px;
  line-height: 1.5;
}

.header-actions {
  display: flex;
  gap: 15px;
  align-items: center;
}

/* Search container styling */
.search-container {
  position: relative;
  margin-right: 10px;
}

.search-container input {
  background-color: var(--input-bg);
  border: 1px solid rgba(255, 255, 255, 0.1);
  color: var(--text-color);
  padding: 10px 40px 10px 15px;
  border-radius: 30px;
  width: 230px;
  font-size: 0.9rem;
  transition: all 0.3s ease;
}

.search-container input:focus {
  outline: none;
  border-color: var(--primary-accent);
  width: 280px;
  box-shadow: 0 0 0 2px rgba(var(--primary-accent-rgb), 0.2);
}

.search-container button.search-btn {
  position: absolute;
  right: 5px;
  top: 50%;
  transform: translateY(-50%);
  background: transparent;
  border: none;
  color: var(--text-color);
  cursor: pointer;
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  transition: background 0.2s ease;
}

.search-container button.clear-search-btn {
  position: absolute;
  right: 40px;
  top: 50%;
  transform: translateY(-50%);
  background: transparent;
  border: none;
  color: var(--text-color);
  cursor: pointer;
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  transition: all 0.2s ease;
  opacity: 0;
  visibility: hidden;
}

.search-container.has-text button.clear-search-btn {
  opacity: 1;
  visibility: visible;
}

.search-container button:hover {
  background-color: var(--primary-accent);
  color: var(--background-color);
}

.search-container button.clear-search-btn:hover {
  background-color: rgba(255, 87, 87, 0.2);
  color: #ff5757;
}

/* Gallery controls section */
.gallery-controls {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  padding-bottom: 20px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.05);
}

/* Enhanced filter buttons */
.gallery-filters {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
}

.filter-btn {
  padding: 10px 16px;
  background-color: var(--input-bg);
  border: 2px solid transparent;
  color: var(--text-color);
  font-size: 0.95rem;
  font-weight: 600;
  border-radius: 10px;
  cursor: pointer;
  transition: all var(--transition-speed) ease;
  display: flex;
  align-items: center;
  gap: 8px;
}

.filter-btn:hover {
  background-color: var(--button-hover);
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
}

.filter-btn.active {
  background-color: var(--primary-accent);
  border-color: var(--secondary-accent);
  font-weight: 700;
}

.filter-icon {
  display: inline-flex;
  font-size: 1.1rem;
}

/* Sort selection styling */
.gallery-sort {
  display: flex;
  align-items: center;
  gap: 10px;
}

.gallery-sort label {
  font-size: 0.9rem;
  opacity: 0.8;
}

.gallery-sort select {
  background-color: var(--input-bg);
  border: 1px solid rgba(255, 255, 255, 0.1);
  color: var(--text-color);
  padding: 8px 30px 8px 12px;
  border-radius: 8px;
  font-size: 0.9rem;
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='24' height='24' viewBox='0 0 24 24' fill='none' stroke='white' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolyline points='6 9 12 15 18 9'%3E%3C/polyline%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 10px center;
  background-size: 14px;
  cursor: pointer;
}

.gallery-sort select:focus {
  outline: none;
  border-color: var(--primary-accent);
}

/* Gallery stats */
.gallery-stats {
  font-size: 0.95rem;
  opacity: 0.8;
  text-align: right;
  padding-top: 10px;
}

.gallery-stats span {
  font-weight: 700;
  color: var(--primary-accent);
}

/* Loading spinner */
.loading-container {
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 60px 0;
}

.loading-spinner {
  width: 50px;
  height: 50px;
  border: 5px solid rgba(255, 255, 255, 0.1);
  border-top: 5px solid var(--secondary-accent);
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  0% {
    transform: rotate(0deg);
  }

  100% {
    transform: rotate(360deg);
  }
}

.loading-container p {
  margin-top: 20px;
  color: var(--text-color);
  font-size: 1.1rem;
}

/* Masonry grid layout */
.gallery-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  grid-auto-rows: 10px;
  /* Will be adjusted by JS */
  gap: var(--gallery-gap);
}

/* Individual gallery items - enhanced for aspect ratio handling */
.gallery-item {
  grid-row-end: span var(--span);
  break-inside: avoid;
  position: relative;
  background-color: var(--card-bg);
  border-radius: 8px;
  overflow: hidden;
  opacity: 0;
  transform: translateY(20px);
  transition: transform var(--transition-speed) ease,
    box-shadow var(--transition-speed) ease;
  will-change: transform, opacity; /* More specific will-change */
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  contain: content; /* Add CSS containment to prevent layout thrashing */
}

/* Items with the visible class will be shown */
.gallery-item.visible {
  opacity: 1;
  transform: translateY(0);
  transition: opacity 0.6s ease-out, transform 0.6s ease-out;
}

/* Aspect ratio based styling */
.gallery-item[style*="--aspect-ratio"] {
  display: flex;
  flex-direction: column;
}

/* Styling for wide images to maintain proper presentation */
.gallery-item[style*="--aspect-ratio"] img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
  transition: transform var(--transition-speed) ease;
  margin: 0 auto;
  /* Center horizontally */
}

/* Special handling for very wide images (panoramas) */
.gallery-item[style*="--aspect-ratio"]:where([style*="--aspect-ratio: 2"],
  [style*="--aspect-ratio: 3"], [style*="--aspect-ratio: 4"]) img {
  object-fit: cover;
  max-height: 300px;
}

@media screen and (max-width: 768px) {

  .gallery-item[style*="--aspect-ratio"]:where([style*="--aspect-ratio: 2"],
    [style*="--aspect-ratio: 3"], [style*="--aspect-ratio: 4"]) img {
    max-height: 200px;
  }
}

@media screen and (max-width: 480px) {

  .gallery-item[style*="--aspect-ratio"]:where([style*="--aspect-ratio: 2"],
    [style*="--aspect-ratio: 3"], [style*="--aspect-ratio: 4"]) img {
    max-height: 180px;
  }
}

/* Individual gallery items */
.gallery-item img {
  width: 100%;
  height: auto;
  display: block;
  transition: transform var(--transition-speed) ease;
  margin: 0 auto;
}

.gallery-item:hover img {
  transform: scale(1.03);
}

.gallery-item .item-overlay {
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  padding: 15px;
  background: linear-gradient(to top, rgba(0, 0, 0, 0.8), transparent);
  transform: translateY(100%);
  transition: transform var(--transition-speed) ease;
  display: flex;
  flex-direction: column;
}

.gallery-item:hover .item-overlay {
  transform: translateY(0);
}

.item-overlay h3 {
  margin: 0 0 5px 0;
  font-size: 1.1rem;
}

.item-overlay h4 {
  color: var(--text-color);
  margin: 0;
  font-size: 0.9rem;
  font-weight: 400;
}

/* List view styling */
.gallery-list {
  display: flex;
  flex-direction: column;
  gap: 15px;
}

.list-item {
  display: flex;
  align-items: center;
  background-color: var(--card-bg);
  border-radius: 8px;
  overflow: hidden;
  padding-right: 20px;
  transition: transform var(--transition-speed) ease,
    box-shadow var(--transition-speed) ease;
  animation: fadeIn 0.6s forwards;
  opacity: 0;
  transform: translateY(20px);
}

.list-item:hover {
  transform: translateY(-3px);
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
}

.list-item .item-image {
  width: 120px;
  height: 120px;
  object-fit: cover;
  margin-right: 20px;
  transition: transform var(--transition-speed) ease;
}

.list-item:hover .item-image {
  transform: scale(1.05);
}

.list-item .item-info {
  flex: 1;
}

.list-item h3 {
  margin: 0 0 8px 0;
  font-size: 1.2rem;
}

.list-item h4 {
  color: var(--text-color);
  margin: 0;
  font-size: 0.95rem;
  opacity: 0.8;
}

/* Image popup styling */
.image-popup {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  display: none;
  z-index: 100;
}

.popup-overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.9);
  backdrop-filter: blur(5px);
}

.popup-container {
  position: relative;
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: alias;
}

.popup-content {
  position: relative;
  max-width: 90%;
  max-height: 85dvh;
  display: flex;
  flex-direction: column;
  z-index: 101;
  cursor: auto;
}

.popup-content img {
  max-width: 100%;
  max-height: 80dvh;
  object-fit: contain;
  border-radius: 4px;
  box-shadow: 0 5px 25px rgba(0, 0, 0, 0.5);
}

.popup-info {
  margin-top: 20px;
  color: var(--text-color);
  text-align: center;
}

.popup-info h2 {
  font-size: 1.5rem;
  margin: 0 0 8px 0;
}

.popup-info h3 {
  font-size: 1.1rem;
  margin: 0;
  font-weight: 400;
}

.popup-info h3 span {
  color: var(--primary-accent);
  font-weight: 600;
}

/* Navigation buttons */
.nav-btn {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  background-color: rgba(0, 0, 0, 0.5);
  color: white;
  width: 50px;
  height: 50px;
  border: none;
  border-radius: 50%;
  font-size: 24px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: background-color 0.2s ease;
  z-index: 102;
}

.nav-btn:hover {
  background-color: var(--primary-accent);
}

.prev-btn {
  left: 20px;
}

.next-btn {
  right: 20px;
}

.close-btn {
  position: absolute;
  top: 20px;
  right: 30px;
  background-color: rgba(0, 0, 0, 0.5);
  color: white;
  width: 40px;
  height: 40px;
  border: none;
  border-radius: 50%;
  font-size: 24px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.2s ease;
  z-index: 102;
}

.close-btn:hover {
  background-color: #ff4757;
  transform: rotate(90deg);
}

/* Responsive adjustments */
@media screen and (max-width: 1200px) {
  .gallery-grid {
    grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
  }
}

@media screen and (max-width: 992px) {
  .header-content {
    flex-direction: column;
    gap: 20px;
  }

  .header-actions {
    width: 100%;
    justify-content: space-between;
  }

  .search-container {
    width: 100%;
    margin-right: 0;
  }

  .search-container input {
    width: 100%;
  }

  .search-container input:focus {
    width: 100%;
  }

  .gallery-controls {
    flex-direction: column;
    align-items: flex-start;
    gap: 20px;
  }

  .gallery-filters {
    width: 100%;
    justify-content: flex-start;
    flex-wrap: wrap;
  }

  .gallery-sort {
    width: 100%;
    justify-content: flex-end;
  }
}

@media screen and (max-width: 768px) {
  .gallery-container {
    padding: 15px 10px;
  }

  .gallery-header {
    padding: 20px 15px;
    margin-bottom: 25px;
  }

  .gallery-header h1 {
    font-size: 2.5rem;
  }

  .gallery-grid {
    grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  }

  .filter-btn {
    padding: 8px 12px;
    font-size: 0.85rem;
  }

  /* Improve nav buttons for touch */
  .nav-btn {
    width: 40px;
    height: 40px;
    font-size: 18px;
    opacity: 0.9;
    background-color: rgba(0, 0, 0, 0.7);
  }

  .prev-btn {
    left: 5px;
  }

  .next-btn {
    right: 5px;
  }

  /* Improve popup for mobile */
  .popup-content {
    max-width: 95%;
  }

  .popup-info h2 {
    font-size: 1.2rem;
  }

  .popup-info h3 {
    font-size: 0.95rem;
  }

  .close-btn {
    top: 10px;
    right: 15px;
  }
}

@media screen and (max-width: 576px) {

  /* Additional breakpoint for smaller phones */
  .gallery-header h1 {
    font-size: 2.2rem;
  }

  .gallery-subtitle {
    font-size: 0.9rem;
    margin-top: 10px;
  }

  .filter-btn {
    min-width: 75px;
  }

  .header-actions {
    flex-direction: column;
    align-items: stretch;
  }

  /* Fix popup navigation on small screens */
  .nav-btn {
    width: 36px;
    height: 36px;
  }

  .prev-btn {
    left: 2px;
  }

  .next-btn {
    right: 2px;
  }
}

@media screen and (max-width: 480px) {
  .gallery-container {
    padding: 10px 8px;
  }

  .gallery-header h1 {
    font-size: 2rem;
  }

  .gallery-subtitle {
    font-size: 0.9rem;
  }

  .gallery-grid {
    grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
    gap: 10px;
  }

  .gallery-filters {
    flex-wrap: wrap;
    gap: 6px;
  }

  .filter-btn {
    padding: 7px 10px;
    font-size: 0.8rem;
    flex-grow: 1;
    justify-content: center;
    min-width: 70px;
    max-width: calc(50% - 6px);
  }

  .filter-icon {
    font-size: 0.9rem;
  }

  .gallery-sort {
    justify-content: flex-start;
  }

  .gallery-sort select {
    max-width: 140px;
  }

  .item-overlay h3 {
    font-size: 0.9rem;
  }

  .item-overlay h4 {
    font-size: 0.8rem;
  }

  .list-item {
    flex-direction: column;
    padding-right: 0;
  }

  .list-item .item-image {
    width: 100%;
    height: 160px;
    margin-right: 0;
    object-fit: cover;
  }

  .list-item .item-info {
    padding: 12px;
  }
}

/* Additional styles for very small screens */
@media screen and (max-width: 360px) {
  .gallery-header h1 {
    font-size: 1.8rem;
  }

  .gallery-grid {
    grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
  }

  .filter-btn {
    padding: 6px 8px;
    font-size: 0.75rem;
  }

  .gallery-sort label {
    display: none;
  }

  .search-container input {
    padding: 8px 35px 8px 12px;
    font-size: 0.85rem;
  }

  .popup-content img {
    max-height: 70dvh;
  }

  .close-btn {
    width: 36px;
    height: 36px;
    font-size: 20px;
  }
}