/* Characters Section */
.characters-section {
    padding: 4rem 2rem;
    background-color: var(--color-bg-light);
    position: relative;
    overflow: hidden;
}

.characters-section::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-image: 
        repeating-linear-gradient(45deg, 
            var(--color-accent-danger) 0px, 
            var(--color-accent-danger) 1px, 
            transparent 1px, transparent 15px),
        repeating-linear-gradient(-45deg, 
            var(--color-accent-surreal) 0px, 
            var(--color-accent-surreal) 1px, 
            transparent 1px, transparent 15px);
    opacity: 0.15; /* Faint diagonal stripes (10-15%) */
    z-index: -1;
}

.character-grid {
    display: flex; /* Changed from grid to flex */
    flex-wrap: wrap;
    justify-content: center; /* Center cards horizontally */
    gap: 2.5rem;
    max-width: 1200px;
    margin: 0 auto;
}

.character-card {
    position: relative;
    height: 400px; /* Fixed height for consistent card size */
    background-color: var(--color-bg-light);
    border-radius: 20px; /* 2xl rounded corners */
    box-shadow: 0 10px 20px rgba(0, 0, 0, 0.15), 0 0 0 2px var(--card-border-color); /* Soft shadow and accent border */
    overflow: hidden;
    cursor: url('/white_glove_cursor.png'), auto;
    transition: transform 0.3s ease, box-shadow 0.3s ease;
    perspective: 1000px; /* Perspective for the 3D flip effect */
    -webkit-tap-highlight-color: transparent; /* Remove tap highlight on mobile */
    will-change: transform;
    flex-basis: 280px; /* Minimum width for the card */
    flex-grow: 1; /* Allow cards to grow and fill available space */
    max-width: calc(25% - (3 * 2.5rem / 4)); /* Default for 4 cards per row on large desktop with gap */
}

.character-card:focus-visible {
    outline: 3px dashed var(--color-accent-surreal);
    outline-offset: 4px;
}

.character-card:hover {
    transform: scale(1.03);
    box-shadow: 0 15px 30px rgba(0, 0, 0, 0.2), 0 0 25px var(--card-border-color); /* Glow on hover */
}

.card-inner {
    position: relative;
    width: 100%;
    height: 100%;
    text-align: center;
    transition: transform 0.4s cubic-bezier(0.4, 0.0, 0.2, 1); /* Smooth transition 300-450ms ease */
    transform-style: preserve-3d; /* Enable 3D transformations */
    will-change: transform;
}

.character-card .card-inner.is-flipped {
    transform: rotateY(180deg);
}

.card-face {
    position: absolute;
    width: 100%;
    height: 100%;
    -webkit-backface-visibility: hidden; /* Hide the back of the face when rotated */
    backface-visibility: hidden;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    padding: 1rem;
    text-align: center;
    background-color: var(--color-bg-light); /* Ensure faces have a background */
    border-radius: 20px; /* Match card border-radius */
}

.card-front {
    /* Styles for the front face */
}

.card-front img {
    max-width: 80%;
    max-height: 70%;
    object-fit: contain;
    margin-bottom: 1rem;
    filter: drop-shadow(5px 5px 5px rgba(0,0,0,0.3)); /* Add subtle shadow to image */
}

.card-front h3 {
    font-family: var(--font-heading);
    font-weight: 700;
    font-size: clamp(1.5rem, 3vw, 2rem);
    color: var(--color-base-deep);
}

.card-back {
    transform: rotateY(180deg); /* Start rotated for the back face */
    padding: 2rem;
    display: flex; /* Ensure content is centered */
    align-items: center;
    justify-content: center;
}

.card-back p {
    font-family: var(--font-body);
    font-size: clamp(0.9rem, 2vw, 1.1rem);
    color: var(--color-base-deep);
    text-align: left; /* Lore description can be left-aligned for legibility */
    line-height: 1.5;
    max-height: 100%;
    overflow-y: auto; /* Allow scrolling for longer lore */
    -webkit-overflow-scrolling: touch; /* Smooth scrolling on iOS */
}

/* Confetti for Character Cards */
.card-sparkle-container {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    overflow: hidden;
    z-index: 10;
}

.card-sparkle {
    position: absolute;
    width: 8px;
    height: 8px;
    background-color: var(--color-highlight-fun);
    opacity: 0;
    border-radius: 0; /* Override for star shape */
    clip-path: polygon(50% 0%, 61% 35%, 98% 35%, 68% 57%, 79% 91%, 50% 70%, 21% 91%, 32% 57%, 2% 35%, 39% 35%);
    animation: sparkle-burst 0.7s ease-out forwards;
}

@keyframes sparkle-burst {
    0% {
        transform: translate(0, 0) scale(0);
        opacity: 1;
    }
    50% {
        transform: translate(var(--sparkle-dx), var(--sparkle-dy)) scale(1);
        opacity: 1;
    }
    100% {
        transform: translate(var(--sparkle-dx), var(--sparkle-dy)) scale(0);
        opacity: 0;
    }
}

/* Reduced Motion for Character Cards */
@media (prefers-reduced-motion: reduce) {
    .card-inner {
        transition: none; /* Disable flip animation */
        transform-style: flat;
    }
    .card-inner.is-flipped .card-front {
        opacity: 0; /* Fade out front */
        transition: opacity 0.2s ease-in-out;
    }
    .card-inner.is-flipped .card-back {
        opacity: 1; /* Fade in back */
        transition: opacity 0.2s ease-in-out;
        transform: rotateY(0deg); /* No rotation for back face */
    }
    .card-front, .card-back {
        transition: opacity 0.2s ease-in-out;
        transform: none !important; /* Ensure no 3D transform */
    }
    .card-back {
        opacity: 0; /* Hidden by default */
        pointer-events: none; /* Disable interaction when hidden */
    }
    .card-inner.is-flipped .card-front {
        pointer-events: none; /* Disable interaction on front when flipped */
    }
    .card-inner.is-flipped .card-back {
        pointer-events: auto; /* Enable interaction on back when flipped */
    }
    .character-card:hover {
        transform: none; /* Disable scale on hover */
    }
    .card-sparkle {
        display: none; /* Hide sparkles */
    }
}

/* Responsive adjustments for character grid */
@media (min-width: 1200px) {
    .character-card {
        max-width: calc(25% - (3 * 2.5rem / 4)); /* 4 cards per row */
    }
}

@media (min-width: 769px) and (max-width: 1199px) {
    .character-card {
        max-width: calc(33.333% - (2 * 2.5rem / 3)); /* 3 cards per row */
    }
}

@media (min-width: 481px) and (max-width: 768px) {
    .character-card {
        max-width: calc(50% - (1 * 2.5rem / 2)); /* 2 cards per row */
    }
}

@media (max-width: 480px) {
    .character-card {
        max-width: calc(100% - 2rem); /* 1 card per row (adjusting for parent padding) */
    }
}