/**
 * Carousel Component Styles
 * 
 * Contains styles for:
 * - 3D carousel container and structure
 * - Carousel cards and animations
 * - Navigation controls
 * - Warning container
 * - Responsive adjustments
 */

/* Warning Container */
.warning-container {
    background-color: #fef2f2;
    border: 1px solid #f87171;
    color: #dc2626;
    padding: 12px 16px;
    border-radius: 8px;
    margin-bottom: 24px;
    font-size: 16px;
    text-align: center;
    animation: slideIn 0.3s ease-out;
    position: relative;
    z-index: 10;
}

@keyframes slideIn {
    from {
        opacity: 0;
        transform: translateY(-10px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

/* Carousel Container and Base Structure */
.carousel-container {
    perspective: 2000px;
    margin: 45px auto 20px;
    width: 360px;
    height: 400px;
    position: relative;
    transform-style: preserve-3d;
    background: var(--color-bg-secondary);
    border-radius: 16px;
    padding: 20px;
    border: none;
    box-shadow: none;
}

/* Add extra top margin when warning is visible */
.carousel-container.warning-active {
    margin-top: 80px !important;
    transition: margin-top 0.3s ease-out;
}

@media (max-width: 768px) {
    .carousel-container {
        width: 260px;
        height: 300px; /* add headroom so selected seal and card are not clipped */
        perspective: 1500px;
        margin: 32px auto 4px;
        padding: 12px 12px 8px;
        overflow: hidden; /* clip side cards that extend beyond container */
    }
    
    .carousel-container.warning-active {
        margin-top: 60px !important;
    }
    /* keep action button visually close after increasing carousel headroom */
    #storyCategoryScreen .mt-4,
    #lessonTopicScreen .mt-4 { margin-top: -8px !important; }
}

.carousel {
    width: 100%;
    height: 380px;
    position: absolute;
    transform-style: preserve-3d;
    transition: transform 0.8s cubic-bezier(0.4, 0, 0.2, 1);
    top: 50%;
    left: 50%;
    transform-origin: center;
    transform: translate(-50%, -50%);
    touch-action: pan-y;
}

@media (max-width: 768px) {
    .carousel {
        height: 240px; /* retain stable card centering with added container headroom */
        /* inherit default centering (top: 50%) */
    }
}

/* Carousel Cards */
.carousel-card {
    position: absolute;
    width: 260px;
    height: 350px;
    left: 50%;
    margin-left: -130px;
    background: transparent;
    border-radius: 12px;
    border: none;
    cursor: pointer;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: flex-start;
    padding: 0;
    transform-origin: center;
    transition: transform 0.5s cubic-bezier(0.4, 0, 0.2, 1), opacity 0.3s ease;
    box-shadow: none;
    opacity: 0.8 !important;
    backdrop-filter: none;
    -webkit-backface-visibility: hidden;
    backface-visibility: hidden;
    will-change: transform, opacity;
    transform-style: preserve-3d;
    isolation: isolate; /* allow pseudo-elements to layer underneath without escaping */
}

@media (max-width: 768px) {
    .carousel-card {
        width: 150px;
        height: 200px;
        margin-left: -75px;
        transition: transform 0.5s cubic-bezier(0.4, 0, 0.2, 1), opacity 0.3s ease;
        will-change: transform, opacity;
    }
}

.carousel-card .card-face {
    position: absolute;
    width: 100%;
    height: 100%;
    backface-visibility: hidden;
    transition: transform 0.6s cubic-bezier(0.4, 0, 0.2, 1);
    border-radius: 12px;
    overflow: hidden;
    will-change: transform; /* GPU optimization for smooth flip animation */
}

.carousel-card .card-front {
    transform: rotateY(0deg);
    z-index: 2;
}

.carousel-card .card-back {
    transform: rotateY(180deg);
    padding: 24px 20px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    background: var(--color-bg-secondary);
    border-radius: 12px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15), 0 2px 4px rgba(0, 0, 0, 0.1);
    border: 1px solid rgba(0, 0, 0, 0.08);
    z-index: 1;
}

.carousel-card.selected .card-front {
    transform: rotateY(-180deg);
    z-index: 1;
}

.carousel-card.selected .card-back {
    transform: rotateY(0deg);
    z-index: 2;
}

/* =============================================
   Wax Seal Selection Indicator
   ============================================= */
.selection-seal {
    position: absolute;
    top: -8px;
    right: -8px;
    width: 44px;
    height: 44px;
    border-radius: 50%;
    background: radial-gradient(
        circle at 35% 35%,
        #14b8a6 0%,
        #0d9488 50%,
        #0a7470 100%
    );
    box-shadow:
        inset 2px 2px 4px rgba(255, 255, 255, 0.3),
        inset -2px -2px 4px rgba(0, 0, 0, 0.2),
        0 4px 12px rgba(13, 148, 136, 0.4),
        0 2px 4px rgba(0, 0, 0, 0.15);
    transform: translateZ(60px) rotate(-15deg);
    opacity: 0;
    pointer-events: none;
    transition: all 0.4s cubic-bezier(0.34, 1.56, 0.64, 1);
    z-index: 100;
    display: flex;
    align-items: center;
    justify-content: center;
}

.carousel-card.selected .selection-seal {
    opacity: 1;
    animation: sealStamp 0.5s cubic-bezier(0.34, 1.56, 0.64, 1);
}

/* Elegant flourish checkmark */
.selection-seal::after {
    content: '';
    width: 12px;
    height: 20px;
    border: solid #fdfcf7;
    border-width: 0 3.5px 3.5px 0;
    transform: rotate(45deg) translate(-2px, -2px);
    filter: drop-shadow(0 1px 1px rgba(0, 0, 0, 0.2));
}

/* Wax shine highlight */
.selection-seal::before {
    content: '';
    position: absolute;
    top: 6px;
    left: 8px;
    width: 12px;
    height: 8px;
    background: linear-gradient(
        135deg,
        rgba(255, 255, 255, 0.5) 0%,
        rgba(255, 255, 255, 0) 100%
    );
    border-radius: 50%;
    opacity: 0.7;
}

@keyframes sealStamp {
    0% {
        transform: translateZ(60px) rotate(-15deg) scale(0.3);
        opacity: 0;
    }
    50% {
        transform: translateZ(60px) rotate(-15deg) scale(1.15);
    }
    70% {
        transform: translateZ(60px) rotate(-15deg) scale(0.95);
    }
    100% {
        transform: translateZ(60px) rotate(-15deg) scale(1);
        opacity: 1;
    }
}

/* Mobile adjustments for wax seal */
@media (max-width: 768px) {
    .selection-seal {
        width: 36px;
        height: 36px;
        top: 4px;
        right: 0;
        transform: translateZ(30px) rotate(-15deg);
    }
    .selection-seal::after {
        width: 10px;
        height: 16px;
        border-width: 0 3px 3px 0;
    }
}

@media (max-width: 768px) {
    .carousel-card.selected .selection-seal {
        animation: sealStampMobile 0.5s cubic-bezier(0.34, 1.56, 0.64, 1);
    }
}

@keyframes sealStampMobile {
    0% {
        transform: translateZ(30px) rotate(-15deg) scale(0.3);
        opacity: 0;
    }
    50% {
        transform: translateZ(30px) rotate(-15deg) scale(1.08);
    }
    70% {
        transform: translateZ(30px) rotate(-15deg) scale(0.96);
    }
    100% {
        transform: translateZ(30px) rotate(-15deg) scale(1);
        opacity: 1;
    }
}

.card-image {
    width: 100%;
    height: 100%;
    object-fit: cover;
    border-radius: 10px;
}

.carousel-card.active {
    opacity: 1;
    transform: scale(1.06) translateZ(0);
    border: none;
    background: transparent;
    z-index: 1;
    /* Use a softer organic glow via pseudo-element instead of box-shadow */
    animation: gentleWiggle 4s ease-in-out infinite;
}

/* Organic radial glow under the active card */
.carousel-card.active::after {
    content: '';
    position: absolute;
    inset: -40px; /* expand well beyond card bounds to avoid squared edges */
    z-index: -1; /* sit beneath the card */
    border-radius: 50%;
    pointer-events: none;
    /* layered gradients for a strong core + soft falloff (bottom-biased) */
    background:
        radial-gradient(circle at 50% 60%,
            rgba(255, 193, 7, 0.95) 0%,
            rgba(255, 181, 21, 0.75) 24%,
            rgba(255, 170, 30, 0.45) 48%,
            rgba(255, 160, 40, 0.16) 70%,
            rgba(255, 160, 40, 0.00) 85%),
        radial-gradient(ellipse at center,
            rgba(255, 193, 7, 0.35) 0%,
            rgba(255, 193, 7, 0.00) 70%);
    mix-blend-mode: normal; /* render bright on light backgrounds */
    filter: blur(26px) saturate(135%);
    transform: scale(0.98);
    animation: organicPulse 1.6s ease-in-out infinite;
}

/* Top glow: exact mirror of bottom glow, clipped to top band */
.carousel-card.active::before {
    content: '';
    position: absolute;
    inset: -40px;
    z-index: -1;
    border-radius: 50%;
    pointer-events: none;
    /* same layered gradients as bottom */
    background:
        radial-gradient(circle at 50% 60%,
            rgba(255, 193, 7, 0.95) 0%,
            rgba(255, 181, 21, 0.75) 24%,
            rgba(255, 170, 30, 0.45) 48%,
            rgba(255, 160, 40, 0.16) 70%,
            rgba(255, 160, 40, 0.00) 85%),
        radial-gradient(ellipse at center,
            rgba(255, 193, 7, 0.35) 0%,
            rgba(255, 193, 7, 0.00) 70%);
    mix-blend-mode: normal;
    filter: blur(26px) saturate(135%);
    transform: scale(0.98) scaleY(-1); /* flip vertically to mirror bottom */
    /* keep only a top band to prevent a dome */
    -webkit-mask-image: linear-gradient(to bottom, rgba(0,0,0,1) 0%, rgba(0,0,0,1) 28%, rgba(0,0,0,0) 48%);
            mask-image: linear-gradient(to bottom, rgba(0,0,0,1) 0%, rgba(0,0,0,1) 28%, rgba(0,0,0,0) 48%);
}

@keyframes organicPulse {
    0%, 100% {
        transform: scale(0.88);
        opacity: 0.6;
        filter: blur(18px) saturate(110%) brightness(1.0);
    }
    50% {
        transform: scale(1.12);
        opacity: 1;
        filter: blur(34px) saturate(145%) brightness(1.2);
    }
}

@keyframes emeraldBreath {
    0%, 100% {
        box-shadow: 
            0 0 10px rgba(139, 69, 219, 0.2),
            0 0 20px rgba(139, 69, 219, 0.3),
            0 0 30px rgba(139, 69, 219, 0.2),
            0 0 40px rgba(167, 139, 250, 0.1);
        filter: drop-shadow(0 0 8px rgba(139, 69, 219, 0.2)) brightness(1);
    }
    50% {
        box-shadow: 
            0 0 20px rgba(139, 69, 219, 0.6),
            0 0 40px rgba(139, 69, 219, 0.5),
            0 0 60px rgba(139, 69, 219, 0.4),
            0 0 80px rgba(167, 139, 250, 0.3),
            0 0 100px rgba(167, 139, 250, 0.2);
        filter: drop-shadow(0 0 20px rgba(139, 69, 219, 0.5)) brightness(1.05);
    }
}

@keyframes gentleWiggle {
    0%, 100% {
        filter: hue-rotate(0deg) drop-shadow(0 0 12px rgba(255, 193, 7, 0.35));
    }
    25% {
        filter: hue-rotate(2deg) drop-shadow(0 0 16px rgba(255, 193, 7, 0.45));
    }
    50% {
        filter: hue-rotate(0deg) drop-shadow(0 0 20px rgba(255, 193, 7, 0.6));
    }
    75% {
        filter: hue-rotate(-2deg) drop-shadow(0 0 16px rgba(255, 193, 7, 0.45));
    }
}

@media (max-width: 768px) {
    .carousel-card.active {
        transform: scale(1.12) translateZ(0);
    }
}

/* Prevent parent wrappers from clipping the glow and nav arrows on selection screens */
#storyCategoryScreen,
#lessonTopicScreen {
    overflow: visible !important;
}

/* On mobile, clip carousel overflow so side cards stay within screen bounds */
@media (max-width: 768px) {
    #storyCategoryScreen,
    #lessonTopicScreen {
        overflow: hidden !important;
    }
}

/* Card Content Styles */
.carousel-title {
    font-family: 'Crimson Text', Georgia, serif;
    font-size: 1.3rem;
    font-weight: 700;
    color: #1a1a1a;
    margin-bottom: 10px;
    text-align: center;
    line-height: 1.3;
    width: 100%;
    padding: 0 12px;
    letter-spacing: 0.02em;
    text-shadow: 0 0 0.3px rgba(0,0,0,0.1);
}

.carousel-description {
    font-family: 'Crimson Text', Georgia, serif;
    font-size: 0.85rem;
    font-weight: 400;
    color: #4B5563;
    text-align: center;
    overflow: hidden;
    display: -webkit-box;
    -webkit-line-clamp: 12;
    -webkit-box-orient: vertical;
    line-height: 1.55;
    max-height: 280px;
    width: 100%;
    padding: 0 12px;
    letter-spacing: 0.02em;
    text-shadow: 0 0 0.3px rgba(0,0,0,0.1);
}

@media (max-width: 768px) {
    .carousel-title {
        font-size: 1.15rem;
        margin-bottom: 6px;
        padding: 0 6px;
    }

    .carousel-description {
        font-size: 0.75rem;
        line-height: 1.35;
        -webkit-line-clamp: 6;
        max-height: 130px;
        padding: 0 6px;
    }

    .carousel-card.selected .card-back {
        padding: 16px 12px;
        background: var(--color-bg-secondary);
        border-radius: 12px;
        box-shadow: 0 6px 20px rgba(0, 0, 0, 0.2), 0 4px 8px rgba(0, 0, 0, 0.15);
        border: 1px solid rgba(0, 0, 0, 0.1);
        transform: rotateY(0deg) scale(0.95);
    }

    /* Specific styles for active card when selected */
    .carousel-card.active.selected .carousel-title {
        font-size: 1.1rem;
        margin-bottom: 4px;
        padding: 0 4px;
    }

    .carousel-card.active.selected .carousel-description {
        font-size: 0.7rem;
        line-height: 1.3;
        -webkit-line-clamp: 8;
        max-height: 150px;
        padding: 0 4px;
    }

    /* Regular selected card (non-active) */
    .carousel-card.selected .carousel-title {
        font-size: 1.3rem;
        margin-bottom: 8px;
    }

    .carousel-card.selected .carousel-description {
        font-size: 0.8rem;
        line-height: 1.4;
        max-height: 150px;
    }
}

/* Navigation Controls */
.carousel-wrapper {
    position: relative;
}

@media (max-width: 768px) {
    .carousel-wrapper {
        overflow: clip; /* clip 3D-transformed children that escape overflow:hidden */
    }
}

.carousel-nav {
    position: absolute;
    top: 50%;
    left: -250px;
    right: -250px;
    transform: translateY(-50%);
    display: flex;
    justify-content: space-between;
    z-index: 1000;
    pointer-events: none;
}

@media (max-width: 768px) {
    .carousel-nav {
        left: -30px;
        right: -30px;
        top: 50%;
        bottom: auto;
        padding: 0;
        transform: translateY(-50%);
        display: none; /* Hide navigation arrows on mobile */
    }
    
    .carousel-nav button {
        width: 28px;
        height: 28px;
        background: rgba(13, 148, 136, 0.3);
        box-shadow: none;
        opacity: 0.9;
        backdrop-filter: none;
    }
    
    .carousel-nav button svg {
        width: 16px;
        height: 16px;
    }
}

.carousel-nav button {
    background: rgba(13, 148, 136, 0.45);
    color: white;
    border: none;
    width: 44px;
    height: 44px;
    border-radius: 50%;
    cursor: pointer;
    transition: all 0.3s;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    pointer-events: auto;
    backdrop-filter: none;
}

.carousel-nav button:hover {
    background: rgba(15, 118, 110, 0.65);
    transform: scale(1.1);
    box-shadow: 0 2px 12px rgba(0, 0, 0, 0.15);
}

.carousel-nav button:active {
    transform: scale(0.95);
}

.carousel-nav button svg {
    width: 22px;
    height: 22px;
    fill: currentColor;
}

/* Swipe Tip */
.swipe-tip {
    position: absolute;
    top: 30%;
    left: 50%;
    transform: translateX(-50%);
    background: rgba(79, 70, 229, 0.9);
    color: white;
    padding: 8px 16px;
    border-radius: 8px;
    font-size: 0.9rem;
    backdrop-filter: blur(4px);
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
    z-index: 2000;
    animation: fadeOutTip 3s forwards, tipGlow 1.5s ease-in-out infinite;
    display: flex;
    align-items: center;
    gap: 8px;
    overflow: hidden;
}

/* Hide swipe tip on desktop */
@media (min-width: 769px) {
    .swipe-tip {
        display: none;
    }
}

.swipe-tip::before {
    content: '';
    position: absolute;
    top: 0;
    left: -100%;
    width: 50%;
    height: 100%;
    background: linear-gradient(
        90deg,
        transparent,
        rgba(255, 255, 255, 0.4),
        transparent
    );
    animation: shine 2s ease-in-out infinite;
}

.swipe-tip .hand {
    display: inline-block;
    animation: swipeHint 1.5s ease-in-out infinite;
}

@keyframes fadeOutTip {
    0% { opacity: 1; }
    70% { opacity: 1; }
    100% { opacity: 0; visibility: hidden; }
}

@keyframes swipeHint {
    0% { transform: translateX(-5px); }
    50% { transform: translateX(5px); }
    100% { transform: translateX(-5px); }
}

@keyframes tipGlow {
    0% {
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
        border: 1px solid rgba(79, 70, 229, 0.4);
    }
    50% {
        box-shadow: 0 0 15px rgba(79, 70, 229, 0.6), 0 0 30px rgba(79, 70, 229, 0.4);
        border: 1px solid rgba(79, 70, 229, 0.8);
    }
    100% {
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
        border: 1px solid rgba(79, 70, 229, 0.4);
    }
}

@keyframes shine {
    0% {
        left: -100%;
    }
    50% {
        left: 100%;
    }
    100% {
        left: 100%;
    }
}

/* Animations */

@keyframes selectCard {
    0% { 
        transform: scale(1) translateZ(0) rotateY(0deg);
    }
    50% { 
        transform: scale(1.1) translateZ(50px) rotateY(0deg);
    }
    100% { 
        transform: scale(1) translateZ(0) rotateY(0deg);
    }
}

.carousel-card.selecting {
    animation: selectCard 0.3s ease-in-out;
}

/* =============================================
   Ripple Effect on Card Tap
   ============================================= */
.carousel-card .card-front::before {
    content: '';
    position: absolute;
    top: 50%;
    left: 50%;
    width: 0;
    height: 0;
    background: radial-gradient(circle, rgba(255,255,255,0.5) 0%, transparent 70%);
    border-radius: 50%;
    transform: translate(-50%, -50%);
    transition: width 0.3s ease-out, height 0.3s ease-out, opacity 0.3s ease-out;
    opacity: 0;
    pointer-events: none;
    z-index: 10;
}

.carousel-card:active .card-front::before {
    width: 200%;
    height: 200%;
    opacity: 1;
    transition: width 0s, height 0s, opacity 0s;
}

/* =============================================
   Accessibility Focus Rings
   ============================================= */
.carousel-card:focus {
    outline: none;
}

.carousel-card:focus-visible {
    outline: none;
    box-shadow:
        0 0 0 3px rgba(253, 252, 247, 0.9),
        0 0 0 6px var(--color-primary);
}
