*,
*::after,
*::before {
  box-sizing: border-box;
}

:root {
  font-size: 14px;
  --color-text: #fff;
  --color-bg: #0f0e0e;
  --color-link: #ffffff;
  --color-link-hover: rgba(255, 255, 255, 0.6);
  --page-padding: 0.5rem;
  --aspect: 4/5;
  --grid-item-height: 20vh;
  --c-gap: 3rem;
  --r-gap: 3rem;
  --column: 80px;
  --column-count: 3;
  --border-radius: 4px;
}

html {
  height: 100%;
  overflow-x: hidden;
}

body {
  height: 100%;
  width: 100%;
  font-family: 'forma-djr-mono', sans-serif;
  margin: 0;
  color: var(--color-text);
  background-color: var(--color-bg);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

@media (scripting: enabled) {
  .loading {
    &::before,
    &::after {
      content: '';
      position: fixed;
      z-index: 10000;
    }

    &::before {
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      background: var(--color-bg);
    }

    &::after {
      top: 50%;
      left: 50%;
      width: 100px;
      height: 1px;
      margin: 0 0 0 -50px;
      background: var(--color-link);
      animation: loaderAnim 1.5s ease-in-out infinite alternate forwards;
    }
  }
}

@keyframes loaderAnim {
  0% {
    transform: scaleX(0);
    transform-origin: 0% 50%;
  }

  50% {
    transform: scaleX(1);
    transform-origin: 0% 50%;
  }

  50.1% {
    transform: scaleX(1);
    transform-origin: 100% 50%;
  }

  100% {
    transform: scaleX(0);
    transform-origin: 100% 50%;
  }
}

a {
  text-decoration: none;
  color: var(--color-link);
  outline: none;
  cursor: pointer;
  transition: all 0.3s ease;

  &:hover {
    text-decoration: none;
    color: var(--color-link-hover);
  }

  &:focus {
    outline: none;
    background: lightgrey;

    &:not(:focus-visible) {
      background: transparent;
    }

    &:focus-visible {
      outline: 2px solid red;
      background: transparent;
    }
  }
}

.line {
  display: inline-block;
  overflow: hidden;
  position: relative;
  vertical-align: top;

  &::before {
    background: var(--color-link-hover);
    bottom: 0;
    content: '';
    height: 1px;
    left: 0;
    position: absolute;
    transition: transform 0.4s ease;
    width: 100%;
    transform: scaleX(0);
    transform-origin: right center;
  }

  &:hover::before {
    transform: scaleX(1);
    transform-origin: left center;
  }
}

.frame {
  display: grid;
  z-index: 1000;
  width: 100%;
  height: 100vh;
  position: fixed;
  grid-column-gap: var(--c-gap);
  grid-row-gap: 0.5rem;
  pointer-events: none;
  justify-items: start;
  padding: var(--page-padding);
  align-content: space-between;
  justify-content: space-between;
  align-items: end;
  grid-template-areas: 'title links' 'tags sponsor';

  #cdawrap {
    justify-self: start;
    grid-area: sponsor;
    max-width: 230px;
    text-align: right;
  }

  a,
  button {
    pointer-events: auto;
    color: var(--color-text);
  }

  .frame__title {
    font-size: inherit;
    margin: 0;
    font-weight: inherit;
    grid-area: title;
    text-align: right;
  }

  .frame__tags,
  .frame__links {
    grid-area: tags;
    display: flex;
    gap: 1rem;
    align-items: start;
  }

  .frame__links {
    grid-area: links;
    justify-self: end;
  }

  &.frame--footer {
    display: flex;
    min-height: 300px;
    align-items: end;
    justify-content: space-between;
  }
}

.scene {
  perspective: 900px;
  position: relative;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
}

.scene__title {
  position: relative;
  display: inline-block;
  padding: 1rem 2rem;
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  border: 1px solid rgba(255, 255, 255, 0.2);
  box-shadow: 
    0 8px 32px rgba(0, 0, 0, 0.1),
    inset 0 1px 0 rgba(255, 255, 255, 0.2),
    inset 0 -1px 0 rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
  overflow: hidden;
  z-index: 100; /* Ensure title appears above carousel */
}

.scene__title::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(
    135deg,
    rgba(255, 255, 255, 0.15) 0%,
    rgba(255, 255, 255, 0.05) 50%,
    rgba(255, 255, 255, 0.1) 100%
  );
  pointer-events: none;
  z-index: -1;
}

.scene__title:hover {
  background: rgba(255, 255, 255, 0.15);
  border-color: rgba(255, 255, 255, 0.3);
  box-shadow: 
    0 12px 40px rgba(0, 0, 0, 0.15),
    inset 0 1px 0 rgba(255, 255, 255, 0.3),
    inset 0 -1px 0 rgba(0, 0, 0, 0.1);
  transform: translateY(-2px);
}

.scene__title a {
  color: #1e3a8a; /* Dark blue text */
  text-decoration: none;
  font-weight: 600;
  text-shadow: 0 1px 2px rgba(255, 255, 255, 0.8); /* Light shadow for contrast */
  letter-spacing: 0.5px;
  position: relative;
  z-index: 1;
}

.scene__title a:hover {
  color: #1e40af; /* Slightly lighter blue on hover */
  text-shadow: 0 1px 3px rgba(255, 255, 255, 0.9);
}

.scene__title span {
  display: inline-block;
}

.scene__title .char {
  display: inline-block;
  transform-style: preserve-3d;
  transform-origin: 50% 0%;
}

.carousel {
  width: 400px;
  height: 500px;
  top: 50%;
  left: 50%;
  margin: -250px 0 0 -200px;
  position: absolute;
  transform-style: preserve-3d;
  will-change: transform;
  transform: translateZ(-550px) rotateY(0deg);
}

.scene:nth-child(even) .carousel {
  transform: translateZ(-550px) rotateY(45deg);
}

.carousel__cell {
  position: absolute;
  width: 350px;
  height: 420px;
  left: 0;
  top: 0;
  transform-style: preserve-3d;
}

.card {
  width: 100%;
  height: 100%;
  position: relative;
  transform-style: preserve-3d;
  cursor: pointer;
  transition: transform 0.3s ease;
  outline: none;
}

.card:hover {
  transform: scale(1.02);
}

.card:focus {
  outline: 2px solid rgba(255, 255, 255, 0.8);
  outline-offset: 4px;
}

.card__face {
  width: 100%;
  height: 100%;
  position: absolute;
  backface-visibility: hidden;
  background-image: var(--img);
  background-size: cover;
}

.card__face img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: var(--border-radius);
}

.card__face--back {
  transform: rotateY(180deg);
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: 1.5rem;
  font-weight: bold;
  text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.5);
}

.card__face--back::before {
  content: '★';
  font-size: 3rem;
  opacity: 0.8;
}

/* Card rotation controls */
.card__controls {
  position: absolute;
  top: 10px;
  right: 10px;
  display: flex;
  gap: 5px;
  opacity: 0;
  transition: opacity 0.3s ease;
  z-index: 10;
  pointer-events: none;
}

.card:hover .card__controls {
  opacity: 1;
  pointer-events: auto;
}

.card__btn {
  background: rgba(255, 255, 255, 0.9);
  border: none;
  border-radius: 50%;
  width: 30px;
  height: 30px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  transition: all 0.2s ease;
  -webkit-backdrop-filter: blur(10px);
  backdrop-filter: blur(10px);
  color: #333;
  font-weight: bold;
}

.card__btn:hover {
  background: rgba(255, 255, 255, 1);
  transform: scale(1.1);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
}

.card__btn:focus {
  outline: 2px solid rgba(255, 255, 255, 0.8);
  outline-offset: 2px;
}

/* Animation states */
.card--flipped {
  transform: rotateY(180deg);
}

.card--spinning {
  animation: cardSpin 1s ease-in-out;
}

@keyframes cardSpin {
  0% { transform: rotateY(0deg); }
  100% { transform: rotateY(360deg); }
}

/* Enhanced card interactions */
.card {
  will-change: transform;
}

.card::before {
  content: '';
  position: absolute;
  top: -2px;
  left: -2px;
  right: -2px;
  bottom: -2px;
  background: linear-gradient(45deg, transparent, rgba(255, 255, 255, 0.1), transparent);
  border-radius: inherit;
  opacity: 0;
  transition: opacity 0.3s ease;
  pointer-events: none;
  z-index: -1;
}

.card:hover::before {
  opacity: 1;
}

/* Accessibility improvements */
@media (prefers-reduced-motion: reduce) {
  .card {
    transition: none;
  }
  
  .card:hover {
    transform: none;
  }
  
  .card__btn {
    transition: none;
  }
  
  .card--spinning {
    animation: none;
  }
  
  @keyframes cardSpin {
    0%, 100% { transform: rotateY(0deg); }
  }
}

.preview {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  padding: 120px 15vw 0 15vw; /* Added top padding to account for header */
  display: grid;
  align-content: center;
  justify-items: center;
  grid-row-gap: 1rem;
  opacity: 0;
  pointer-events: none;
  z-index: 100; /* Higher than header z-index: 50 */
  background: rgba(0, 0, 0, 0.95); /* Semi-transparent background */
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
}

.preview__header {
  display: flex;
  width: 100%;
  align-items: flex-end;
  justify-content: space-between;
  line-height: 1;
}

.preview__title {
  margin: 0;
  color: #ffffff;
  text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.8);
  font-size: clamp(1.5rem, 4vw, 3rem);
  font-weight: bold;
}

.preview__close {
  background: none;
  text-transform: inherit;
  border: 0;
  padding: 8px;
  margin: 0;
  font: inherit;
  cursor: pointer;
  color: #ffffff;
  font-size: 1.2rem;
  font-weight: bold;
  border-radius: 4px;
  transition: all 0.3s ease;
}

.preview__close:hover,
.preview__close:focus {
  outline: none;
  background: rgba(255, 255, 255, 0.1);
  color: #ffffff;
  transform: scale(1.1);
}

.grid {
  padding: 1rem 0;
  display: grid;
  grid-template-columns: repeat(var(--column-count), minmax(var(--column), 1fr));
  grid-column-gap: var(--c-gap);
  grid-row-gap: var(--r-gap);
  width: 100%;
  perspective: 900px;
}

.grid__item {
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  will-change: transform, clip-path;
  overflow: hidden;
  border-radius: var(--border-radius);
  position: relative;
  transform-style: preserve-3d;
}

.grid__item-image {
  width: 100%;
  aspect-ratio: var(--aspect);
  background-size: 100%;
  background-position: 50% 50%;
  transition: opacity 0.15s cubic-bezier(0.2, 0, 0.2, 1);
}

.grid__item:hover .grid__item-image {
  opacity: 0.7;
}

.grid__item-caption h3 {
  font-size: 1rem;
  font-weight: 400;
  margin: 0;
}

@media screen and (min-width: 65em) {
  :root {
    --column-count: 6;
  }
}

/* Mobile adjustments for preview */
@media screen and (max-width: 768px) {
  .preview {
    padding: 100px 5vw 0 5vw; /* Reduced padding for mobile */
  }
  
  .preview__header {
    flex-direction: column;
    gap: 1rem;
    align-items: center;
  }
}

@media screen and (max-width: 480px) {
  .preview {
    padding: 80px 2vw 0 2vw; /* Even smaller padding for very small screens */
  }
}

/* Mobile optimizations for frosted titles */
@media screen and (max-width: 768px) {
  .scene__title {
    padding: 0.75rem 1.5rem;
    border-radius: 8px;
    backdrop-filter: blur(15px);
    -webkit-backdrop-filter: blur(15px);
  }
  
  .scene__title a {
    color: #1e3a8a; /* Maintain dark blue on mobile */
    font-size: 0.9rem;
    letter-spacing: 0.3px;
  }
}

@media screen and (max-width: 480px) {
  .scene__title {
    padding: 0.5rem 1rem;
    border-radius: 6px;
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
  }
  
  .scene__title a {
    color: #1e3a8a; /* Maintain dark blue on small screens */
    font-size: 0.8rem;
    letter-spacing: 0.2px;
  }
}

/* Enhanced contrast for better readability */
@media (prefers-contrast: high) {
  .scene__title {
    background: rgba(255, 255, 255, 0.9); /* Light background for high contrast */
    border-color: rgba(0, 0, 0, 0.3);
  }
  
  .scene__title a {
    color: #1e3a8a; /* Keep dark blue even in high contrast */
    text-shadow: 0 0 2px rgba(255, 255, 255, 1);
  }
}

/* Reduced motion support */
@media (prefers-reduced-motion: reduce) {
  .scene__title {
    transition: none;
  }
  
  .scene__title:hover {
    transform: none;
  }
}

/* Image Preview Modal */
.image-preview-modal {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.9);
  z-index: 1000;
  align-items: center;
  justify-content: center;
  padding: 1rem;
  box-sizing: border-box;
}

.modal-content {
  position: relative;
  max-width: 100%;
  max-height: 100%;
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
}

.modal-close {
  position: absolute;
  top: 1rem;
  right: 1rem;
  background: rgba(255, 255, 255, 0.2);
  border: none;
  color: white;
  font-size: 1.5rem;
  cursor: pointer;
  padding: 0.5rem;
  width: 44px;
  height: 44px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  transition: all 0.2s ease;
  z-index: 1001;
}

.modal-close:hover {
  background: rgba(255, 255, 255, 0.3);
  transform: scale(1.1);
}

#modal-image {
  max-width: 100%;
  max-height: 70vh;
  object-fit: contain;
  border-radius: 8px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.3);
}

#modal-title {
  color: white;
  margin-top: 1rem;
  font-size: 1.2rem;
  text-align: center;
  max-width: 90%;
  line-height: 1.4;
}

/* Grid item click cursor */
.grid__item {
  cursor: pointer;
  transition: transform 0.2s ease;
  -webkit-tap-highlight-color: transparent;
}

.grid__item:hover {
  transform: scale(1.02);
}

/* Mobile optimizations */
@media screen and (max-width: 768px) {
  .image-preview-modal {
    padding: 0.5rem;
  }
  
  .modal-close {
    top: 0.5rem;
    right: 0.5rem;
    width: 48px;
    height: 48px;
    font-size: 1.8rem;
    background: rgba(0, 0, 0, 0.5);
  }
  
  #modal-image {
    max-height: 60vh;
    border-radius: 4px;
  }
  
  #modal-title {
    font-size: 1rem;
    margin-top: 0.75rem;
  }
  
  .grid__item:hover {
    transform: none; /* Disable hover effects on mobile */
  }
}

@media screen and (max-width: 480px) {
  .modal-close {
    width: 52px;
    height: 52px;
    font-size: 2rem;
  }
  
  #modal-image {
    max-height: 50vh;
  }
  
  #modal-title {
    font-size: 0.9rem;
    margin-top: 0.5rem;
  }
}

/* Touch device optimizations */
@media (hover: none) and (pointer: coarse) {
  .grid__item:hover {
    transform: none;
  }
  
  .modal-close:hover {
    background: rgba(255, 255, 255, 0.2);
    transform: none;
  }
}
