/* Reset & Base */
*, *::before, *::after {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

html {
  -webkit-tap-highlight-color: transparent;
  -webkit-text-size-adjust: 100%;
}

body {
  background: #0d0d1a;
  color: #e0e0e0;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  min-height: 100vh;
  overflow-x: hidden;
  padding-top: calc(env(safe-area-inset-top, 0px) + 110px);
  padding-bottom: env(safe-area-inset-bottom, 0px);
}

/* ─── Header ─── */
#header {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 100;
  background: rgba(13, 13, 26, 0.95);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  padding: calc(env(safe-area-inset-top, 0px) + 8px) 12px 0;
  border-bottom: 1px solid rgba(255,255,255,0.08);
}

.header-top {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  margin-bottom: 6px;
}

.logo {
  font-size: 1.1rem;
  font-weight: 700;
  color: #a78bfa;
  white-space: nowrap;
  flex-shrink: 0;
}

.controls {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  flex: 1;
  min-width: 0;
}

#search {
  flex: 1;
  min-width: 120px;
  padding: 8px 12px;
  border-radius: 8px;
  border: 1px solid rgba(255,255,255,0.15);
  background: rgba(255,255,255,0.07);
  color: #fff;
  font-size: 14px;
  outline: none;
  min-height: 44px;
}

#search:focus {
  border-color: #a78bfa;
}

#source-filter, #sort-order {
  padding: 8px 10px;
  border-radius: 8px;
  border: 1px solid rgba(255,255,255,0.15);
  background: rgba(255,255,255,0.07);
  color: #fff;
  font-size: 13px;
  min-height: 44px;
  appearance: none;
  -webkit-appearance: none;
  cursor: pointer;
}

/* Shuffle button */
.shuffle-btn {
  min-width: 44px;
  min-height: 44px;
  padding: 8px 12px;
  border-radius: 8px;
  border: 1px solid rgba(255,255,255,0.15);
  background: rgba(255,255,255,0.07);
  color: #fff;
  font-size: 18px;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  flex-shrink: 0;
  transition: background 0.15s;
}

.shuffle-btn:active {
  background: rgba(167, 139, 250, 0.4);
}

.shuffle-btn.active {
  background: rgba(167, 139, 250, 0.3);
  border-color: #a78bfa;
}

.stats-bar {
  font-size: 12px;
  color: rgba(255,255,255,0.5);
  padding: 4px 0 6px;
  text-align: center;
}

/* ─── Gallery Grid (CSS columns — masonry-like) ─── */
.gallery {
  column-count: 2;
  column-gap: 6px;
  padding: 6px;
}

.gallery-item {
  break-inside: avoid;
  margin-bottom: 6px;
  border-radius: 8px;
  overflow: hidden;
  position: relative;
  cursor: pointer;
  opacity: 0;
  transform: translateY(10px);
  transition: opacity 0.3s ease, transform 0.3s ease;
}

.gallery-item.visible {
  opacity: 1;
  transform: translateY(0);
}

.gallery-item img {
  display: block;
  width: 100%;
  height: auto;
  background: #1a1a2e;
  border-radius: 8px;
}

/* ─── Loader ─── */
.loader {
  text-align: center;
  padding: 20px;
  color: rgba(255,255,255,0.4);
  font-size: 14px;
}

.loader.hidden {
  display: none;
}

/* ─── Lightbox ─── */
.lightbox {
  display: none; /* DEFAULT: hidden. Shown via .open class */
  position: fixed;
  inset: 0;
  z-index: 1000;
  background: rgba(0, 0, 0, 0.95);
  align-items: center;
  justify-content: center;
  flex-direction: column;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: env(safe-area-inset-top, 0px) env(safe-area-inset-right, 0px) env(safe-area-inset-bottom, 0px) env(safe-area-inset-left, 0px);
}

.lightbox.open {
  display: flex;
}

/* Close button — MUST work on iOS */
.lightbox-close {
  position: fixed;
  top: calc(env(safe-area-inset-top, 0px) + 10px);
  right: calc(env(safe-area-inset-right, 0px) + 10px);
  z-index: 9999;
  pointer-events: auto;
  width: 50px;
  height: 50px;
  border: none;
  background: rgba(0, 0, 0, 0.6);
  color: #fff;
  font-size: 32px;
  line-height: 50px;
  text-align: center;
  border-radius: 50%;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  padding: 0;
}

.lightbox-close:active {
  background: rgba(255, 255, 255, 0.3);
}

/* Nav arrows — hidden on mobile, shown on desktop */
.lightbox-nav {
  display: none;
  position: fixed;
  top: 50%;
  transform: translateY(-50%);
  z-index: 9998;
  pointer-events: auto;
  width: 50px;
  height: 80px;
  border: none;
  background: rgba(0, 0, 0, 0.4);
  color: #fff;
  font-size: 36px;
  cursor: pointer;
  border-radius: 8px;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  padding: 0;
}

.lightbox-prev-btn { left: 10px; }
.lightbox-next-btn { right: 10px; }

/* Lightbox content */
.lightbox-content {
  display: flex;
  flex-direction: column;
  align-items: center;
  max-width: 100%;
  max-height: 100%;
  padding: 60px 10px 20px;
  pointer-events: none;
}

.lightbox-img {
  max-width: 100%;
  max-height: 65vh;
  object-fit: contain;
  border-radius: 8px;
  pointer-events: auto;
  user-select: none;
  -webkit-user-select: none;
}

.lightbox-info {
  width: 100%;
  max-width: 600px;
  padding: 16px 4px;
  pointer-events: auto;
}

.lightbox-field {
  margin-bottom: 12px;
}

.lightbox-label {
  display: block;
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: #a78bfa;
  margin-bottom: 4px;
}

.lightbox-field-text {
  font-size: 15px;
  line-height: 1.5;
  color: #d0d0d0;
  word-break: break-word;
  max-height: 200px;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding-right: 4px;
}

.lightbox-meta {
  font-size: 12px;
  color: rgba(255,255,255,0.4);
  margin-bottom: 12px;
}

.lightbox-actions {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.lightbox-actions .action-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 12px 16px;
  border: 1px solid rgba(255,255,255,0.15);
  border-radius: 8px;
  background: rgba(255,255,255,0.07);
  color: #e0e0e0;
  font-size: 14px;
  cursor: pointer;
  min-height: 44px;
  min-width: 44px;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  text-decoration: none;
  text-align: center;
}

.lightbox-actions .action-btn:active {
  background: rgba(167, 139, 250, 0.3);
}

/* ─── Toast ─── */
.toast {
  position: fixed;
  bottom: calc(env(safe-area-inset-bottom, 0px) + 30px);
  left: 50%;
  transform: translateX(-50%) translateY(100px);
  background: rgba(167, 139, 250, 0.95);
  color: #fff;
  padding: 10px 24px;
  border-radius: 24px;
  font-size: 14px;
  font-weight: 600;
  z-index: 99999;
  pointer-events: none;
  opacity: 0;
  transition: transform 0.3s ease, opacity 0.3s ease;
  white-space: nowrap;
}

.toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

/* ─── Desktop (3+ columns, show nav arrows) ─── */
@media (min-width: 600px) {
  .gallery { column-count: 3; column-gap: 8px; padding: 8px; }
  .lightbox-nav { display: block; }
  .lightbox-actions { flex-direction: row; flex-wrap: wrap; }
  .lightbox-actions .action-btn { flex: 1; min-width: 120px; }
}

@media (min-width: 900px) {
  .gallery { column-count: 4; }
}

@media (min-width: 1200px) {
  .gallery { column-count: 5; column-gap: 10px; padding: 10px; }
}
