/* Show All Merits - Vertical Button */
.show-merits-btn {
    position: absolute;
    /* Attached flush to the card border */
    right: -42px;
    /* Adjusted for exact flush fit to aligned with border */
    top: 50%;
    transform: translateY(-50%) rotate(90deg);
    transform-origin: center center;
    background: linear-gradient(135deg, var(--color-primary) 0%, var(--color-primary-dark) 100%);
    color: white;
    /* Border matching the card's border */
    border: 1px solid rgba(255, 255, 255, 0.1);
    /* Remove the bottom border (which connects to card) to simulate "pasted" look */
    border-bottom: none;
    padding: 10px 20px;
    font-family: var(--font-body);
    font-weight: 700;
    font-size: 0.85rem;
    letter-spacing: 1.5px;
    cursor: pointer;
    z-index: 900;
    border-radius: 8px 8px 0 0;
    box-shadow: 4px -4px 12px rgba(0, 0, 0, 0.2);
    transition: all 0.3s ease;
    display: flex;
    flex-direction: row-reverse;
    /* Arrow on right (bottom when vertical) */
    align-items: center;
    gap: 8px;
    white-space: nowrap;
}

.show-merits-btn:hover {
    background: linear-gradient(135deg, var(--color-secondary) 0%, var(--color-primary) 100%);
    /* Move slightly 'up' relative to rotation (away from card) to show pull effect */
    right: -46px;
    /* Subtle movement */
}

.show-merits-btn .btn-arrow {
    width: 14px;
    height: 14px;
}

/* Merits Modal */
.merits-modal {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 9999;
    opacity: 0;
    transition: opacity 0.3s ease;
    overflow-x: hidden;
    /* Ensure card throws don't trigger scrollbar */
    overscroll-behavior: none;
    /* Prevent scroll chaining/rubber-banding on mobile */
}

.merits-modal.active {
    display: flex;
    align-items: center;
    justify-content: center;
    opacity: 1;
}

.merits-modal-backdrop {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.7);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
}

/* Hold Effect - Pop & Glassy */
.merit-card.is-held {
    transform: scale(1.05) translateZ(50px) !important;
    background: rgba(40, 60, 40, 0.4);
    backdrop-filter: blur(12px) saturate(180%);
    -webkit-backdrop-filter: blur(12px) saturate(180%);
    border-color: rgba(255, 255, 255, 0.6);
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.7), 0 0 30px rgba(255, 255, 255, 0.15);
    z-index: 100 !important;
    cursor: grabbing;
    will-change: transform, backdrop-filter;
}

.merits-modal-content {
    position: relative;
    background: linear-gradient(135deg, #1a2a1a 0%, #2F3E2F 100%);
    border-radius: 16px;
    max-width: 900px;
    width: 90%;
    max-height: 85vh;
    padding: 3rem 2rem;
    overflow-y: auto;
    overflow-x: hidden;
    /* Prevent horizontal scrollbar when card flies out */
    z-index: 10000;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
    border: 1px solid rgba(255, 255, 255, 0.1);
    animation: slideUp 0.4s ease-out;
}

@keyframes slideUp {
    from {
        opacity: 0;
        transform: translateY(50px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.merits-modal-close {
    position: absolute;
    top: 20px;
    right: 20px;
    background: rgba(255, 255, 255, 0.1);
    border: 1px solid rgba(255, 255, 255, 0.2);
    border-radius: 50%;
    width: 40px;
    height: 40px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all 0.3s ease;
    color: white;
    z-index: 10001;
}

.merits-modal-close:hover {
    background: var(--color-primary);
    border-color: var(--color-primary);
    transform: rotate(90deg);
}

.merits-modal-close i {
    width: 24px;
    height: 24px;
}

.merits-modal-title {
    color: white;
    font-family: var(--font-heading);
    font-size: 2rem;
    font-weight: 700;
    text-align: center;
    margin-bottom: 2.5rem;
    letter-spacing: 1px;
    text-transform: uppercase;
    background: linear-gradient(to right, var(--color-secondary), #fff);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
}

.merits-card-stack {
    position: relative;
    width: 320px;
    height: 220px;
    margin: 2rem auto;
    perspective: 1000px;
    /* Fixed dimensions to prevent shaking */
    min-height: 220px;
}

/* Merit Card Styling */
.merit-card {
    position: absolute;
    width: 100%;
    height: 100%;
    background: linear-gradient(145deg, #2F3E2F 0%, #1a2a1a 100%);
    border: 2px solid rgba(255, 255, 255, 0.15);
    border-radius: 16px;
    padding: 1.5rem;
    text-align: center;
    box-shadow: 0 10px 40px rgba(0, 0, 0, 0.4);
    cursor: grab;
    /* Essential for mobile swipe to work without scrolling the page */
    touch-action: none;
    transition: transform 0.2s cubic-bezier(0.23, 1, 0.32, 1), opacity 0.2s ease;
    backface-visibility: hidden;
    transform-origin: center center;
    /* Contain text */
    overflow: hidden;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
}

.merit-card:hover {
    box-shadow: 0 15px 50px rgba(0, 0, 0, 0.5);
}

.merit-card .card-rank {
    position: absolute;
    top: 10px;
    right: 15px;
    background: linear-gradient(135deg, var(--color-secondary), #c9a84c);
    color: var(--color-primary-dark);
    padding: 4px 12px;
    border-radius: 20px;
    font-weight: 700;
    font-size: 0.7rem;
    letter-spacing: 1px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
}

.merit-card .card-name {
    font-family: var(--font-heading);
    font-size: 1.3rem;
    color: var(--color-secondary);
    margin: 0.5rem 0 0.3rem;
    font-weight: 700;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 100%;
}

.merit-card .card-marks {
    font-size: 1.8rem;
    font-weight: 700;
    color: white;
    margin: 0.3rem 0;
    letter-spacing: 2px;
}

.merit-card .card-position {
    color: rgba(255, 255, 255, 0.6);
    font-size: 0.8rem;
    letter-spacing: 1px;
    text-transform: uppercase;
}

/* Card Stack Effect - Cards behind have slight offset */
.merit-card[data-position="0"] {
    z-index: 20;
}

.merit-card[data-position="1"] {
    z-index: 19;
}

.merit-card[data-position="2"] {
    z-index: 18;
}

/* Swipe to back animation - realistic card folding physics */
.merit-card.swiping {
    animation: cardFoldToBack 0.6s cubic-bezier(0.4, 0, 0.2, 1) forwards;
}

@keyframes cardFoldToBack {
    0% {
        transform: rotate(var(--random-rotate, 0deg));
        opacity: 1;
        z-index: 20;
    }

    15% {
        /* Lift up slightly */
        transform: translateY(-15px) translateZ(30px) rotate(var(--random-rotate, 0deg));
        opacity: 1;
    }

    35% {
        /* Start folding - bend the card */
        transform: translateY(-10px) translateX(-30%) translateZ(20px) rotateY(-35deg) rotateX(8deg) scale(0.95);
        opacity: 0.9;
    }

    55% {
        /* Continue fold - card bending more */
        transform: translateY(5px) translateX(-50%) translateZ(-10px) rotateY(-50deg) rotateX(5deg) scale(0.85);
        opacity: 0.6;
    }

    75% {
        /* Slide behind - card flattening */
        transform: translateY(15px) translateX(-20%) translateZ(-30px) rotateY(-25deg) rotateX(2deg) scale(0.8);
        opacity: 0.3;
    }

    100% {
        /* Rest at back of stack */
        transform: translateY(30px) translateX(0) translateZ(-50px) rotateY(0deg) rotateX(0deg) scale(0.88);
        opacity: 0;
        z-index: 1;
    }
}

/* Counter display */
.stack-counter {
    text-align: center;
    margin-top: 1.5rem;
    color: rgba(255, 255, 255, 0.5);
    font-size: 0.9rem;
}

/* Scrollbar styling for modal */
.merits-modal-content::-webkit-scrollbar {
    width: 8px;
}

.merits-modal-content::-webkit-scrollbar-track {
    background: rgba(255, 255, 255, 0.05);
    border-radius: 10px;
}

.merits-modal-content::-webkit-scrollbar-thumb {
    background: var(--color-primary);
    border-radius: 10px;
}

.merits-modal-content::-webkit-scrollbar-thumb:hover {
    background: var(--color-primary-dark);
}

/* Mobile Responsive - Button & Modal */
@media (max-width: 768px) {
    .show-merits-btn {
        font-size: 0.65rem;
        padding: 8px 14px;
        letter-spacing: 0.5px;
        right: -32px;
        gap: 5px;
    }

    .show-merits-btn:hover {
        right: -36px;
    }

    .show-merits-btn .btn-arrow {
        width: 10px;
        height: 10px;
    }

    /* 
       USER REQUEST: Copy Desktop view to Mobile from scratch.
       Removed .merits-modal-content, .merits-card-stack, .merit-card overrides 
       to enforce desktop sizing and layout rigidity.
    */

    .merits-modal-close {
        top: 10px;
        right: 10px;
        width: 32px;
        height: 32px;
    }

    .merits-modal-close i {
        width: 18px;
        height: 18px;
    }

    /* Force centering for Merit Stack on Mobile */
    .merits-modal-content {
        padding: 1.5rem 1rem !important;
        /* Reduce padding to fit stack */
        display: flex;
        flex-direction: column;
        align-items: center;
        overflow: hidden !important;
        /* Prevent background from moving */
    }

    .merits-card-stack {
        margin-left: auto !important;
        margin-right: auto !important;
        left: auto !important;
        right: auto !important;
        transform: none !important;
        /* Remove any desktop transform offsets */
    }
}

/* Helper for manual dragging to remove transition latency */
.merit-card.dragging {
    transition: none !important;
    cursor: grabbing;
    will-change: transform;
}

/* Calendar Scroll / Rolodex Animation for Auto-Advance */
.merit-card.auto-cycle {
    animation: slideUpFadeOut 0.8s cubic-bezier(0.16, 1, 0.3, 1) forwards;
    will-change: transform, opacity;
}

@keyframes slideUpFadeOut {
    0% {
        transform: rotate(var(--random-rotate)) translateY(0);
        opacity: 1;
        z-index: 20;
    }

    100% {
        /* Smooth slide up and scale down */
        transform: rotate(var(--random-rotate)) translateY(-60px) scale(0.9);
        opacity: 0;
        z-index: 20;
    }
}