:root {
    --bg-color: #0f172a;
    --text-color: #f8fafc;
    --primary: #f43f5e;
    --primary-hover: #e11d48;
    --secondary: #334155;
    --secondary-hover: #475569;
    --success: #10b981;
    --danger: #ef4444;
    --glass-bg: rgba(30, 41, 59, 0.7);
    --glass-border: rgba(255, 255, 255, 0.1);
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

body {
    font-family: 'Outfit', sans-serif;
    background: linear-gradient(-45deg, #0f172a, #1a0033, #0f172a, #260026);
    background-size: 400% 400%;
    animation: gradientBG 15s ease infinite;
    color: var(--text-color);
    min-height: 100vh;
    display: flex;
    flex-direction: column;
    overflow-x: hidden;
}

@keyframes gradientBG {
    0% {
        background-position: 0% 50%;
    }

    50% {
        background-position: 100% 50%;
    }

    100% {
        background-position: 0% 50%;
    }
}

.hidden {
    display: none !important;
}

/* Glassmorphism Classes */
.glass-header {
    background: rgba(15, 23, 42, 0.6);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    border-bottom: 1px solid var(--glass-border);
    padding: clamp(0.5rem, 2vw, 1rem) clamp(1rem, 4vw, 2rem);
    display: flex;
    justify-content: space-between;
    align-items: center;
    position: sticky;
    top: 0;
    z-index: 10;
}

.glass-header h1 {
    font-size: clamp(1.2rem, 4vw, 2rem);
    margin: 0;
}

.glass-panel,
.lobby-card {
    background: var(--glass-bg);
    backdrop-filter: blur(16px);
    -webkit-backdrop-filter: blur(16px);
    border: 1px solid var(--glass-border);
    border-radius: 16px;
    padding: 2rem;
    box-shadow: 0 8px 32px 0 rgba(0, 0, 0, 0.3);
}

.highlight {
    color: var(--primary);
    font-weight: 700;
}

/* Layout */
#app {
    display: flex;
    flex-direction: column;
    flex: 1;
}

.panel {
    display: flex;
    justify-content: center;
    align-items: center;
    flex: 1;
    padding: 2rem;
}

.lobby-card {
    max-width: 500px;
    width: 100%;
    text-align: center;
    animation: fadeIn 0.4s ease-out;
}

.lobby-card h2 {
    margin-bottom: 1.5rem;
    font-size: 2rem;
}

/* Inputs and Buttons */
.input-group {
    display: flex;
    flex-direction: column;
    gap: 1rem;
    margin-bottom: 1.5rem;
}

.mode-selector {
    text-align: left;
    margin-bottom: 1.5rem;
    padding: 1rem;
    background: rgba(0, 0, 0, 0.2);
    border-radius: 8px;
    border: 1px solid var(--glass-border);
}

.mode-selector p {
    margin-bottom: 0.5rem;
    font-size: 0.9rem;
    font-weight: bold;
    color: var(--primary);
}

.radio-group {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.radio-label {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    cursor: pointer;
    font-size: 0.95rem;
}

.radio-label input {
    width: auto;
    padding: 0;
}

input {
    width: 100%;
    padding: 1rem;
    border-radius: 8px;
    border: 1px solid var(--glass-border);
    background: rgba(0, 0, 0, 0.2);
    color: white;
    font-family: inherit;
    font-size: 1rem;
    transition: all 0.2s;
}

input:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 2px rgba(244, 63, 94, 0.3);
}

.button-group {
    display: flex;
    gap: 1rem;
}

.btn {
    flex: 1;
    padding: 1rem;
    border: none;
    border-radius: 8px;
    font-size: 1rem;
    font-weight: bold;
    cursor: pointer;
    transition: transform 0.1s, background-color 0.2s, opacity 0.2s;
    font-family: inherit;
    color: white;
}

.btn:active {
    transform: scale(0.96);
}

.btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
    transform: none;
}

.primary {
    background-color: var(--primary);
}

.primary:hover:not(:disabled) {
    background-color: var(--primary-hover);
}

.secondary {
    background-color: var(--secondary);
}

.secondary:hover:not(:disabled) {
    background-color: var(--secondary-hover);
}

.success {
    background-color: var(--success);
}

.success:hover:not(:disabled) {
    background-color: #059669;
}

.small-btn {
    padding: 0.5rem 1rem;
    font-size: 0.8rem;
    margin-left: 1rem;
}

.icon-btn {
    padding: 0.5rem;
    font-size: 1.2rem;
    line-height: 1;
    background: transparent;
    border: 1px solid var(--glass-border);
}

.audio-controls {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    margin-right: auto;
    margin-left: 2rem;
}

.volume-slider {
    width: 80px;
    height: 4px;
    background: var(--glass-border);
    border: none;
    appearance: none;
    -webkit-appearance: none;
    border-radius: 4px;
    outline: none;
    padding: 0;
}

.volume-slider::-webkit-slider-thumb {
    -webkit-appearance: none;
    width: 12px;
    height: 12px;
    border-radius: 50%;
    background: var(--primary);
    cursor: pointer;
}

.banner-container {
    display: flex;
    align-items: center;
}

.giant-btn {
    width: 100%;
    background-color: var(--success);
    font-size: 1.25rem;
    padding: 1.25rem;
    margin-top: 1rem;
}

/* Upload Section */
.upload-section {
    background: rgba(0, 0, 0, 0.2);
    padding: 1.5rem;
    border-radius: 12px;
    margin: 1.5rem 0;
    border: 1px dashed var(--glass-border);
}

.upload-label {
    display: inline-block;
    padding: 0.75rem 1.5rem;
    background: var(--secondary);
    border-radius: 8px;
    cursor: pointer;
    margin: 1rem 0;
    transition: background 0.2s;
}

.upload-label:hover {
    background: var(--secondary-hover);
}

#preview-container {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
    justify-content: center;
    margin-bottom: 1rem;
    max-height: 200px;
    overflow-y: auto;
}

.preview-img {
    width: 50px;
    height: 70px;
    object-fit: cover;
    border-radius: 4px;
    border: 1px solid var(--glass-border);
}

.error-message {
    color: var(--danger);
    font-weight: bold;
    margin: 0.5rem 0;
    font-size: 0.9rem;
}

/* Game Board */
#game-board {
    display: flex;
    flex-direction: column;
    flex: 1;
    position: relative;
    padding: 1rem;
    animation: fadeIn 0.5s ease;
}

/* Players Area (Scores) */
.players-area {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    pointer-events: none;
    z-index: 5;
}

.player-stat {
    position: absolute;
    background: var(--glass-bg);
    padding: clamp(0.2rem, 1vw, 0.75rem) clamp(0.5rem, 2vw, 1.5rem);
    border-radius: 12px;
    font-size: clamp(0.7rem, 2.5vw, 1rem);
    font-weight: bold;
    border: 1px solid var(--glass-border);
    backdrop-filter: blur(8px);
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.3);
    text-align: center;
}

.player-stat span {
    color: var(--primary);
    font-size: clamp(0.9rem, 3vw, 1.25rem);
}

/* Adjust opponent positions on Mobile to prevent overlap */
.opp-top {
    top: 10px;
    left: 50%;
    transform: translateX(-50%);
}

.opp-left {
    top: 30px;
    left: 10px;
}

.opp-right {
    top: 30px;
    right: 10px;
}

.my-stat {
    bottom: 10px;
    left: 50%;
    transform: translateX(-50%);
    border-color: var(--primary);
    background: rgba(244, 63, 94, 0.1);
}

/* Center */
.center-area {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    margin: 0;
    padding: 1rem 0 60px 0;
    /* Space for my-stat at bottom */
    width: 100%;
}

.turn-indicator {
    font-size: clamp(1rem, 4vw, 1.5rem);
    font-weight: bold;
    margin-bottom: clamp(0.5rem, 2vw, 1.5rem);
    background: var(--glass-bg);
    padding: clamp(0.5rem, 1vw, 0.75rem) clamp(1rem, 3vw, 2rem);
    border-radius: 30px;
    border: 1px solid var(--glass-border);
    box-shadow: 0 0 20px rgba(0, 0, 0, 0.2);
    transition: all 0.3s ease;
    z-index: 6;
    /* stay above grid */
}

.turn-indicator.active {
    background: var(--primary);
    color: white;
    border-color: var(--primary-hover);
    box-shadow: 0 0 30px rgba(244, 63, 94, 0.5);
    animation: pulse 2s infinite;
}

.tile-grid {
    display: grid;
    /* Determine column counts based on smallest safe relative unit vh/vw so it auto-shrinks cards */
    grid-template-columns: repeat(auto-fit, minmax(clamp(45px, min(12vw, 10vh), 100px), 1fr));
    gap: clamp(4px, 2vw, 12px);
    justify-content: center;
    align-content: center;
    max-width: 95%;
    width: 100%;
    /* Take full available width relative to max-width */
    max-width: 600px;
    /* Height is now height: auto to prevent the grid background from popping out and clipping tiles */
    height: auto;
    min-height: 40vh;
    padding: clamp(0.5rem, 2vw, 2rem) clamp(0.5rem, 2vw, 2rem);
    /* Dynamic moving internal background using gradient */
    background: linear-gradient(135deg, rgba(30, 41, 59, 0.4), rgba(15, 23, 42, 0.8));
    background-size: 200% 200%;
    animation: gradientBG 6s ease infinite;
    border-radius: 16px;
    border: 1px solid var(--glass-border);
    box-shadow: inset 0 0 20px rgba(0, 0, 0, 0.5), 0 10px 30px rgba(0, 0, 0, 0.3);
    perspective: 1000px;
    /* Crucial for 3D flip */
}

/* 3D Flipping Card */
.card {
    width: 100%;
    aspect-ratio: 3/4;
    /* Let the card figure out its size constraints inside the grid cell */
    max-height: 100%;
    margin: 0 auto;
    background-color: transparent;
    cursor: pointer;
    border-radius: 8px;
    user-select: none;
}

.card-inner {
    position: relative;
    width: 100%;
    height: 100%;
    text-align: center;
    transition: transform 0.6s cubic-bezier(0.175, 0.885, 0.32, 1.275);
    transform-style: preserve-3d;
    box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2);
    border-radius: 8px;
}

/* Flipped state */
.card.flipped .card-inner,
.card.matched .card-inner {
    transform: rotateY(180deg);
}

.card.matched {
    cursor: default;
    opacity: 0.8;
}

.card-front,
.card-back {
    position: absolute;
    width: 100%;
    height: 100%;
    -webkit-backface-visibility: hidden;
    /* Safari */
    backface-visibility: hidden;
    border-radius: 8px;
    overflow: hidden;
    display: flex;
    justify-content: center;
    align-items: center;
}

/* Face Down (Back of card) */
.card-back {
    background: linear-gradient(135deg, var(--secondary) 0%, #1e293b 100%);
    border: 2px solid var(--secondary-hover);
    color: var(--primary);
    font-size: 2rem;
    font-weight: bold;
}

.card-back::after {
    content: '?';
    opacity: 0.5;
}

.card:hover:not(.flipped):not(.matched) .card-inner {
    transform: scale(1.05);
    /* Slight bounce on hover */
    box-shadow: 0 8px 16px 0 rgba(0, 0, 0, 0.4);
}

/* Face Up (Front with image) */
.card-front {
    background: white;
    transform: rotateY(180deg);
    border: 2px solid #e2e8f0;
}

.card-front img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

/* Modals */
.modal {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.8);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 100;
}

.modal-content {
    text-align: center;
    animation: scaleUp 0.3s ease-out;
}

.modal-content h2 {
    font-size: 3rem;
    margin-bottom: 1rem;
    background: -webkit-linear-gradient(45deg, var(--primary), var(--success));
    -webkit-background-clip: text;
    background-clip: text;
    -webkit-text-fill-color: transparent;
}

ul {
    list-style: none;
    margin: 1rem 0;
    padding: 1rem;
    background: rgba(0, 0, 0, 0.2);
    border-radius: 8px;
    text-align: left;
}

li {
    padding: 0.5rem 0;
    border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}

li:last-child {
    border: none;
}

/* Animations */
@keyframes fadeIn {
    from {
        opacity: 0;
    }

    to {
        opacity: 1;
    }
}

@keyframes scaleUp {
    from {
        transform: scale(0.9);
        opacity: 0;
    }

    to {
        transform: scale(1);
        opacity: 1;
    }
}

@keyframes pulse {
    0% {
        box-shadow: 0 0 10px rgba(244, 63, 94, 0.4);
    }

    50% {
        box-shadow: 0 0 30px rgba(244, 63, 94, 0.8);
    }

    100% {
        box-shadow: 0 0 10px rgba(244, 63, 94, 0.4);
    }
}

/* Scrollbar customization */
::-webkit-scrollbar {
    width: 8px;
    height: 8px;
}

::-webkit-scrollbar-track {
    background: rgba(0, 0, 0, 0.1);
}

::-webkit-scrollbar-thumb {
    background: var(--secondary);
    border-radius: 4px;
}

::-webkit-scrollbar-thumb:hover {
    background: var(--primary);
}

.glass-footer {
    text-align: center;
    padding: 1.5rem;
    font-size: 0.9rem;
    opacity: 0.7;
    margin-top: auto;
    border-top: 1px solid var(--glass-border);
    backdrop-filter: blur(8px);
    background: rgba(15, 23, 42, 0.4);
}

.glass-footer .highlight {
    font-weight: 500;
}

.glass-footer a {
    color: inherit;
    text-decoration: none;
    cursor: pointer;
}

.glass-footer a:hover {
    color: var(--primary);
}