* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  min-height: 100%;
  background: #050505;
}

body {
  overflow-y: scroll;
}

body.viewer-open {
  overflow: hidden;
}

.folder-toggle,
.ratio-toggle {
  position: fixed;
  top: 12px;
  z-index: 10;
  display: grid;
  width: 40px;
  height: 40px;
  place-items: center;
  padding: 0;
  border: 1px solid rgb(255 255 255 / 0.16);
  border-radius: 999px;
  background: rgb(0 0 0 / 0.36);
  color: #fff;
  cursor: pointer;
  opacity: 0.28;
  backdrop-filter: blur(10px);
  transition: opacity 160ms ease, border-color 160ms ease, background 160ms ease, color 160ms ease;
}

.folder-toggle {
  left: 12px;
}

.ratio-toggle {
  right: 12px;
}

.folder-toggle:hover,
.folder-toggle:focus-visible,
.ratio-toggle:hover,
.ratio-toggle:focus-visible {
  border-color: rgb(255 255 255 / 0.68);
  background: rgb(255 255 255 / 0.92);
  color: #000;
  opacity: 1;
  outline: none;
}

.folder-icon,
.ratio-icon {
  position: relative;
  display: block;
  width: 20px;
  height: 20px;
}

.folder-icon::before,
.folder-icon::after {
  position: absolute;
  top: 3px;
  width: 7px;
  height: 14px;
  border: 2px solid currentColor;
  border-radius: 2px;
  content: "";
}

.folder-icon::before {
  left: 1px;
  opacity: 1;
}

.folder-icon::after {
  right: 1px;
  opacity: 0.38;
}

body[data-folder="nanobanana"] .folder-icon::before {
  opacity: 0.38;
}

body[data-folder="nanobanana"] .folder-icon::after {
  opacity: 1;
}

.ratio-icon::before {
  position: absolute;
  top: 5px;
  left: 1px;
  width: 18px;
  height: 10px;
  border: 2px solid currentColor;
  border-radius: 2px;
  content: "";
}

body[data-tile-ratio="square"] .ratio-icon::before {
  top: 3px;
  left: 3px;
  width: 14px;
  height: 14px;
}

.gallery {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 1px;
  padding: 1px;
  background: #050505;
}

.tile {
  all: unset;
  position: relative;
  display: block;
  width: 100%;
  cursor: zoom-in;
}

.tile-frame {
  position: relative;
  width: 100%;
  aspect-ratio: 16 / 9;
  overflow: hidden;
  background: #0c0d0f;
  isolation: isolate;
}

body[data-tile-ratio="square"] .tile-frame {
  aspect-ratio: 1 / 1;
}

.tile-frame img {
  position: relative;
  z-index: 2;
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
  opacity: 0;
  transition: opacity 220ms ease, transform 220ms ease;
}

.tile-frame::before,
.tile-frame::after {
  position: absolute;
  inset: 0;
  z-index: 0;
  pointer-events: none;
  content: "";
  transition: opacity 260ms ease;
}

.tile-frame::before {
  background:
    radial-gradient(circle at 20% 18%, rgb(255 255 255 / 0.16), transparent 26%),
    radial-gradient(circle at 82% 76%, rgb(143 205 255 / 0.14), transparent 24%),
    linear-gradient(135deg, #0b0c0e 0%, #181c21 48%, #0d0f12 100%);
  transform: scale(1.04);
  animation: tile-pulse 1800ms ease-in-out infinite alternate;
}

.tile-frame::after {
  z-index: 1;
  background:
    linear-gradient(115deg, transparent 0 34%, rgb(255 255 255 / 0.2) 46%, transparent 58%),
    repeating-linear-gradient(90deg, rgb(255 255 255 / 0.045) 0 1px, transparent 1px 13px);
  background-size: 220% 100%, 100% 100%;
  mix-blend-mode: screen;
  animation: tile-sweep 1400ms cubic-bezier(0.45, 0, 0.2, 1) infinite;
}

.tile-loader {
  position: absolute;
  inset: 0;
  z-index: 3;
  display: grid;
  place-items: center;
  pointer-events: none;
  transition: opacity 180ms ease, transform 180ms ease;
}

.tile-loader::before {
  width: 34px;
  height: 34px;
  border: 1px solid rgb(255 255 255 / 0.16);
  border-top-color: rgb(255 255 255 / 0.82);
  border-radius: 50%;
  box-shadow: 0 0 26px rgb(143 205 255 / 0.18);
  content: "";
  animation: tile-spin 900ms linear infinite;
}

.tile.is-loaded .tile-frame img {
  opacity: 0.96;
}

.tile.is-loaded .tile-frame::before,
.tile.is-loaded .tile-frame::after,
.tile.is-loaded .tile-loader {
  opacity: 0;
}

.tile.is-loaded .tile-frame::before,
.tile.is-loaded .tile-frame::after,
.tile.is-loaded .tile-loader::before {
  animation: none;
}

.tile.is-loaded .tile-loader {
  transform: scale(0.86);
}

.tile.is-error .tile-frame::before {
  background:
    linear-gradient(135deg, #111 0%, #1d1313 48%, #0c0c0c 100%);
  animation: none;
}

.tile.is-error .tile-frame::after {
  opacity: 0.28;
  animation-duration: 2600ms;
}

.tile.is-error .tile-loader::before {
  border-color: rgb(255 255 255 / 0.28);
  border-top-color: rgb(255 255 255 / 0.28);
  border-radius: 4px;
  box-shadow: none;
  transform: rotate(45deg);
  animation: none;
}

.tile:focus-visible .tile-frame {
  z-index: 1;
  outline: 2px solid #fff;
  outline-offset: -2px;
}

@media (hover: hover) {
  .tile.is-loaded:hover .tile-frame img {
    opacity: 1;
    transform: scale(1.035);
  }
}

@media (prefers-reduced-motion: reduce) {
  .tile-frame::before,
  .tile-frame::after,
  .tile-loader::before {
    animation: none;
  }
}

@keyframes tile-pulse {
  from {
    filter: brightness(0.86);
    transform: scale(1.04);
  }

  to {
    filter: brightness(1.16);
    transform: scale(1.08);
  }
}

@keyframes tile-sweep {
  from {
    background-position: 180% 0, 0 0;
  }

  to {
    background-position: -80% 0, 0 0;
  }
}

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

.viewer {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: none;
  background: #000;
  color: #fff;
  user-select: none;
  touch-action: none;
}

.viewer.is-open {
  display: block;
}

.stage {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  cursor: grab;
  touch-action: none;
}

.stage.is-dragging {
  cursor: grabbing;
}

.stage img {
  display: block;
  width: auto;
  height: auto;
  max-width: 100vw;
  max-height: 100vh;
  pointer-events: none;
  transform-origin: center;
  will-change: transform;
}

.viewer-actions {
  position: fixed;
  top: 12px;
  right: 12px;
  display: flex;
  gap: 8px;
  opacity: 0.28;
  transition: opacity 160ms ease;
}

.viewer-actions:hover,
.viewer-actions:focus-within {
  opacity: 1;
}

.icon-button {
  display: grid;
  width: 40px;
  height: 40px;
  place-items: center;
  padding: 0;
  border: 1px solid rgb(255 255 255 / 0.16);
  border-radius: 999px;
  background: rgb(0 0 0 / 0.36);
  color: #fff;
  cursor: pointer;
  backdrop-filter: blur(10px);
}

.icon-button:hover,
.icon-button:focus-visible,
.icon-button.is-copied {
  border-color: rgb(255 255 255 / 0.68);
  background: rgb(255 255 255 / 0.92);
  color: #000;
  outline: none;
}

.icon-button svg {
  width: 18px;
  height: 18px;
  fill: none;
  stroke: currentColor;
  stroke-linecap: round;
  stroke-linejoin: round;
  stroke-width: 2;
}

@media (max-width: 520px) {
  .gallery {
    grid-template-columns: repeat(2, 1fr);
  }

  .viewer-actions {
    top: 8px;
    right: 8px;
  }

  .folder-toggle {
    top: 8px;
    left: 8px;
  }

  .ratio-toggle {
    top: 8px;
    right: 8px;
  }
}

@media (min-width: 1280px) {
  .gallery {
    grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  }
}
