:root {
    --font-main: 'Luckiest Guy', cursive;
    --color-bg: #450a0a;
    --color-text: #ffffff;
    --color-accent: #e94560;
    --color-card-back: #16213e;
    --color-card-back-pattern: #0f3460;

    /* Event Group Colors */
    --color-group-1: #FF5733;
    /* 2026 Christmas */
    --color-group-2: #FFC300;
    /* 2027 CNY */
    --color-group-3: #28B463;
    /* 2027 Christmas */
    --color-group-4: #3498DB;
    /* 2028 CNY */

    --card-width: 140px;
    --card-height: 200px;
}

* {
    box-sizing: border-box;
    user-select: none;
    /* Prevent text selection during rapid tapping */
    -webkit-tap-highlight-color: transparent;
}

body {
    margin: 0;
    padding: 0;
    background-color: var(--color-bg);
    color: var(--color-text);
    font-family: var(--font-main);
    overflow: hidden;
    /* Prevent scrolling during play */
    height: 100vh;
    height: 100dvh;
    /* Fallback and modern mobile fix */
    display: flex;
    justify-content: center;
    align-items: center;
}

.app-container {
    width: 100%;
    max-width: 800px;
    height: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 20px;
}

header {
    text-align: center;
    margin-bottom: 20px;
    z-index: 10;
    width: 100%;
}

.header-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    width: 100%;
}

h1 {
    margin: 0;
    font-size: 3rem;
    color: #FFC107;
    text-shadow: 3px 3px 0px rgba(0, 0, 0, 0.5);
    letter-spacing: 2px;
    text-align: left;
    line-height: 1;
}

.status-text {
    margin-top: 10px;
    font-size: 1.5rem;
    min-height: 1.5rem;
    color: #ffd700;
    text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.8);
    animation: bounce 1.5s infinite ease-in-out;
}

/* Card Grid */
.card-grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    /* Make rows flexible and equal height, shrinking to fit container */
    grid-auto-rows: minmax(0, 1fr);
    gap: 10px;
    width: 100%;
    max-width: 800px;
    height: 100%;
    min-height: 0;
    /* Important for flex container child scrolling/shrinking */
    perspective: 1000px;
    flex-grow: 1;
    align-content: center;
    justify-items: center;
    z-index: 5;
    padding: 10px 0;
}

/* Responsive adjustments */
@media (max-width: 600px) {
    .card-grid {
        grid-template-columns: repeat(2, 1fr);
        /* 2 cols -> 4 rows. They will share vertical space equally thanks to grid-auto-rows */
    }

    h1 {
        font-size: 1.8rem;
    }

    .status-text {
        font-size: 1rem;
        margin-top: 5px;
    }

    .app-container {
        padding: 10px;
    }

    /* Ensure footer buttons don't get squashed too much */
    .controls {
        margin-top: 10px;
        flex-shrink: 0;
    }

    header {
        margin-bottom: 10px;
        flex-shrink: 0;
    }
}

/* Card Structure */
.card-container {
    width: 100%;
    height: 100%;
    max-width: none;
    aspect-ratio: unset;
    margin: 0;
    position: relative;
    cursor: pointer;
    transform-style: preserve-3d;
    transition: transform 0.6s cubic-bezier(0.4, 0.2, 0.2, 1);
}

/* On very short screens, we prioritize visibility over aspect ratio if needed, or just let them get small */
@media (max-height: 700px) {
    .card-container {
        max-width: 14vh;
        /* Scale with height */
    }
}

/* Desktop Adjustments: Fix "narrow and long" issue */
@media (min-width: 601px) {
    .card-grid {
        /* grid-auto-rows: auto;  <-- Removed to ensure it fills height */
        gap: 25px;
        /* More breathing room */
    }

    .card-container {
        width: 100%;
        height: 100%;
        max-width: none;
    }
}

.card-container.flipped {
    transform: rotateY(180deg);
}

/* Sorting animations */
.card-container {
    transition: transform 0.6s, left 0.5s, top 0.5s;
    /* Add position transition for sorting if needed */
}

.card-face {
    position: absolute;
    width: 100%;
    height: 100%;
    backface-visibility: hidden;
    border-radius: 12px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.3);
    border: 4px solid #fff;
}

/* Front Face (Face Down) */
.card-front {
    background: repeating-linear-gradient(45deg,
            var(--color-card-back),
            var(--color-card-back) 10px,
            var(--color-card-back-pattern) 10px,
            var(--color-card-back-pattern) 20px);
    display: flex;
    align-items: center;
    justify-content: center;
}

.card-logo {
    font-size: 6rem;
    opacity: 0.5;
    color: #fff;
}

/* Back Face (Face Up/Revealed) */
.card-back {
    background-color: #fff;
    color: #333;
    transform: rotateY(180deg);
    padding: 10px;
    text-align: center;
    overflow: hidden;
}

.event-name {
    font-size: 1.2rem;
    color: #fff;
    /* Will be set dynamically based on group color */
    text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.5);
    z-index: 2;
}

.organizer-name {
    margin-top: 10px;
    font-size: 1.4rem;
    color: #000;
    z-index: 2;
}

/* Highlight Effect */
.card-container.highlighted .card-front {
    border-color: #ffff00;
    box-shadow: 0 0 15px #ffff00;
    transform: scale(1.05);
}

/* Controls */
.controls {
    display: flex;
    gap: 15px;
    z-index: 20;
}

button {
    font-family: var(--font-main);
    cursor: pointer;
    border: none;
    outline: none;
    transition: transform 0.1s;
}

button:active {
    transform: scale(0.95);
}

.icon-btn {
    background: none;
    color: #fff;
    font-size: 1.8rem;
    opacity: 0.7;
    padding: 5px;
}

.icon-btn:hover {
    opacity: 1;
}

.control-btn {
    padding: 10px 20px;
    border-radius: 30px;
    font-size: 1.2rem;
    color: var(--color-bg);
    background-color: #fff;
    box-shadow: 0 4px 0 #ccc;
}

.control-btn:active {
    box-shadow: 0 2px 0 #ccc;
    transform: translateY(2px);
}

.hidden {
    display: none !important;
}

.tap-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    z-index: 1;
    /* Below cards but above background */
    cursor: pointer;
}

/* Modal */
.modal {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.8);
    z-index: 100;
    display: flex;
    align-items: center;
    justify-content: center;
    backdrop-filter: blur(5px);
}

.modal-content {
    background: var(--color-bg);
    border: 4px solid var(--color-accent);
    padding: 20px;
    border-radius: 15px;
    width: 90%;
    max-width: 500px;
    max-height: 90vh;
    overflow-y: auto;
}

.input-list {
    display: grid;
    gap: 8px;
    margin-bottom: 20px;
}

input {
    font-family: var(--font-main);
    padding: 8px;
    font-size: 1rem;
    border-radius: 8px;
    border: 2px solid #444;
    background: #2a2a40;
    color: #fff;
}

.modal-actions {
    display: flex;
    justify-content: space-between;
    gap: 10px;
}

.action-btn {
    flex: 1;
    padding: 10px;
    border-radius: 8px;
    font-size: 1.1rem;
}

.save {
    background-color: var(--color-accent);
    color: #fff;
}

.cancel {
    background-color: #444;
    color: #ccc;
}

.danger-btn {
    margin-top: 20px;
    background: #ff3333;
    color: white;
    width: 100%;
    padding: 10px;
    border-radius: 8px;
}

@keyframes bounce {

    0%,
    100% {
        transform: translateY(0);
    }

    50% {
        transform: translateY(-5px);
    }
}

/* Mobile Layout & Text Visibility Fixes */
@media (max-width: 600px) {
    .card-container {
        width: 100%;
        height: 100%;
    }

    .event-name {
        font-size: 1rem;
    }

    .organizer-name {
        font-size: 1.1rem;
    }

    .card-logo {
        font-size: 4rem;
    }
}


/* Festive Background Images */
.festive-bg {
    position: absolute;
    bottom: 0;
    right: 0;
    width: 60%;
    max-height: 50%;
    opacity: 0.2;
    pointer-events: none;
    z-index: 1;
    object-fit: contain;
}


/* Landscape Scale Fix */
@media (max-height: 500px) {
    h1 {
        font-size: 1.5rem;
    }

    header {
        margin-bottom: 5px;
    }

    .card-grid {
        grid-auto-rows: minmax(0, 1fr) !important;
    }

    .card-container {
        width: 100%;
        height: 100%;
        aspect-ratio: unset;
        max-width: unset;
    }

    .card-logo {
        font-size: 3rem;
    }

    .event-name {
        font-size: 0.9rem;
    }

    .organizer-name {
        font-size: 1rem;
    }
}

/** New Organizer Selection Styles */
.organizer-list-container {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 10px;
    padding: 10px;
    margin-bottom: 20px;
    width: 100%;
    max-width: 800px;
    margin-left: auto;
    margin-right: auto;
    position: relative;
    /* Needed for z-index */
    z-index: 20;
    /* Ensure on top */
}

.organizer-chip {
    background-color: rgba(255, 255, 255, 0.2);
    border: 2px solid rgba(255, 255, 255, 0.5);
    border-radius: 20px;
    padding: 10px 20px;
    color: white;
    font-family: 'Luckiest Guy', cursive;
    font-size: 1.2rem;
    cursor: pointer;
    transition: all 0.2s ease;
    user-select: none;
    text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.5);
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.2);
    /* z-index autmatically handled by container, but can be explicit if needed */
}

/* ... existing chip styles ... */

.tap-overlay {
    display: none !important;
    /* Disable validation overlay as it's no longer needed and blocks clicks */
}

.organizer-chip:hover {
    background-color: rgba(255, 255, 255, 0.4);
    transform: translateY(-2px);
}

.organizer-chip.selected {
    background-color: #ffd700;
    color: #d32f2f;
    border-color: #fff;
    transform: scale(1.1);
    box-shadow: 0 0 15px #ffd700;
    z-index: 10;
}

.organizer-chip.used {
    background-color: rgba(0, 0, 0, 0.3);
    border-color: rgba(255, 255, 255, 0.1);
    color: rgba(255, 255, 255, 0.5);
    cursor: default;
    transform: none;
    box-shadow: none;
    text-decoration: none;
    opacity: 0.8;
    padding-right: 35px;
    /* Make room for icon */
    position: relative;
}

.organizer-chip.used::after {
    content: '✔';
    position: absolute;
    right: 10px;
    top: 50%;
    transform: translateY(-50%);
    color: #4caf50;
    /* Green tick */
    font-size: 1.2rem;
    text-shadow: 0 0 5px rgba(0, 0, 0, 0.5);
}

/* Ensure no pointer events on used chips */
.organizer-chip.used {
    pointer-events: none;
}

/* Adjust card grid margin since we added the list */
.card-grid {
    margin-top: 10px;
}