/* ===== Design System & Modern Reset ===== */

:root {
    --bg-color-1: #120726;
    --bg-color-2: #38103c;
    --bg-color-3: #1a082b;
    --bg-color-4: #091326;
    --card-bg: rgba(255, 255, 255, 0.08);
    --card-border: rgba(255, 255, 255, 0.16);
    --card-border-hover: rgba(255, 255, 255, 0.45);
    --text-color: #ffffff;
    --text-muted: rgba(255, 255, 255, 0.75);
    --accent: #ff477e;
    --accent-dark: #d62d62;
    --accent-glow: rgba(255, 71, 126, 0.45);
    --success: #06d6a0;
    --success-dark: #05b08a;
    --success-glow: rgba(6, 214, 160, 0.45);
    --warning: #ffd166;
    --warning-glow: rgba(255, 209, 102, 0.5);
    --magic-purple: #9d4edd;
    --magic-glow: rgba(157, 78, 221, 0.65);
    --glass-bg: rgba(24, 12, 44, 0.82);
    --glass-shadow: 0 16px 40px rgba(0, 0, 0, 0.45);
    --card-shadow: 0 4px 14px rgba(0, 0, 0, 0.35);
    --card-shadow-hover: 0 10px 25px rgba(255, 71, 126, 0.35);
    
    /* ===== 120 FPS Natural Spring Physics ===== */
    --ease-smooth: cubic-bezier(0.16, 1, 0.3, 1);
    --ease-spring: cubic-bezier(0.34, 1.25, 0.64, 1);
    --ease-vortex: cubic-bezier(0.2, 0.8, 0.2, 1);
    --ease-out: cubic-bezier(0, 0, 0.2, 1);
    
    --transition-lift: transform 0.24s var(--ease-spring), box-shadow 0.24s ease;
    --transition-flip: transform 0.44s var(--ease-spring);
    --transition-ui: all 0.28s var(--ease-smooth);
}

*, *::before, *::after {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    -webkit-tap-highlight-color: transparent;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    text-rendering: optimizeLegibility;
}

html {
    font-size: 15px;
    height: 100%;
}

body {
    font-family: 'Cairo', sans-serif;
    color: var(--text-color);
    min-height: 100vh;
    min-height: 100dvh;
    width: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    padding: clamp(0.4rem, 1.5vh, 1rem) clamp(0.5rem, 2vw, 1rem);
    padding-top: max(clamp(0.4rem, 1.5vh, 1rem), env(safe-area-inset-top));
    padding-bottom: max(clamp(0.4rem, 1.5vh, 1rem), env(safe-area-inset-bottom));
    padding-left: max(clamp(0.5rem, 2vw, 1rem), env(safe-area-inset-left));
    padding-right: max(clamp(0.5rem, 2vw, 1rem), env(safe-area-inset-right));
    overflow-x: hidden;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    position: relative;
    background-color: var(--bg-color-1);
    transform: translate3d(0, 0, 0);
}

/* ===== Ambient Dynamic Background ===== */
.background-animation {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    height: 100dvh;
    background: radial-gradient(circle at 15% 20%, #4a1942 0%, transparent 45%),
                radial-gradient(circle at 85% 80%, #0b1a30 0%, transparent 45%),
                radial-gradient(circle at 50% 50%, #280a3a 0%, transparent 65%),
                linear-gradient(135deg, var(--bg-color-1), var(--bg-color-3));
    background-size: 140% 140%;
    animation: gradientBG 20s ease-in-out infinite alternate;
    z-index: -2;
    pointer-events: none;
    will-change: transform;
    transform: translate3d(0, 0, 0);
}

@keyframes gradientBG {
    0% {
        transform: scale3d(1, 1, 1) translate3d(0, 0, 0);
    }
    50% {
        transform: scale3d(1.04, 1.04, 1) translate3d(-1%, -1%, 0);
    }
    100% {
        transform: scale3d(1, 1, 1) translate3d(0, 0, 0);
    }
}

/* ===== Floating Glowing Particles ===== */
.particles-container {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    height: 100dvh;
    pointer-events: none;
    z-index: -1;
    overflow: hidden;
    transform: translate3d(0, 0, 0);
}

.particle {
    position: absolute;
    background: rgba(255, 255, 255, 0.45);
    border-radius: 50%;
    animation: float120 14s infinite linear;
    pointer-events: none;
    will-change: transform, opacity;
    transform: translate3d(0, 0, 0);
}

.particle.glow {
    box-shadow: 0 0 8px rgba(255, 71, 126, 0.6), 0 0 16px rgba(6, 214, 160, 0.4);
}

@keyframes float120 {
    0% {
        transform: translate3d(0, 105vh, 0);
        opacity: 0;
    }
    10% {
        opacity: 0.6;
    }
    90% {
        opacity: 0.35;
    }
    100% {
        transform: translate3d(0, -5vh, 0);
        opacity: 0;
    }
}

/* ===== Main Game Container ===== */
.game-container {
    background: var(--glass-bg);
    backdrop-filter: blur(20px);
    -webkit-backdrop-filter: blur(20px);
    border: 1px solid var(--card-border);
    border-radius: clamp(16px, 3vw, 22px);
    padding: clamp(0.75rem, 1.8vw, 1.25rem) clamp(0.8rem, 2vw, 1.35rem);
    box-shadow: var(--glass-shadow), 0 0 35px rgba(255, 71, 126, 0.1);
    width: 100%;
    max-width: 440px;
    margin: auto;
    display: flex;
    flex-direction: column;
    gap: clamp(0.45rem, 1.1vh, 0.8rem);
    position: relative;
    box-sizing: border-box;
    transform: translate3d(0, 0, 0);
    transition: max-width 0.4s var(--ease-smooth);
}

.game-container.mode-16 {
    max-width: 680px;
}

.game-container::before {
    content: '';
    position: absolute;
    top: 0;
    left: 18px;
    right: 18px;
    height: 3px;
    background: linear-gradient(90deg, transparent, var(--accent), var(--success), var(--warning), transparent);
    border-radius: 3px;
    z-index: 1;
}

/* ===== Header ===== */
header {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: clamp(0.32rem, 0.8vh, 0.55rem);
    width: 100%;
}

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

h1 {
    font-weight: 900;
    font-size: clamp(1.15rem, 2.6vw, 1.45rem);
    text-align: right;
    line-height: 1.15;
    background: linear-gradient(45deg, var(--accent), #ff9f1c, var(--success));
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
    filter: drop-shadow(0 2px 5px rgba(255, 71, 126, 0.3));
    animation: titleGlow 4s ease-in-out infinite alternate;
}

@keyframes titleGlow {
    0% {
        filter: drop-shadow(0 2px 5px rgba(255, 71, 126, 0.3));
    }
    100% {
        filter: drop-shadow(0 3px 10px rgba(6, 214, 160, 0.45));
    }
}

.header-actions {
    display: flex;
    align-items: center;
    gap: 6px;
}

.icon-toggle-btn {
    background: rgba(255, 255, 255, 0.08);
    border: 1px solid var(--card-border);
    color: var(--text-muted);
    font-family: inherit;
    font-size: clamp(0.68rem, 1.4vw, 0.74rem);
    font-weight: 700;
    padding: 3px 9px;
    border-radius: 14px;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    gap: 4px;
    transition: all 0.22s var(--ease-spring);
    user-select: none;
    -webkit-user-select: none;
    touch-action: manipulation;
}

.icon-toggle-btn.active {
    background: rgba(255, 71, 126, 0.2);
    border-color: var(--accent);
    color: #ffffff;
    box-shadow: 0 0 10px var(--accent-glow);
}

.icon-toggle-btn.active #action-cards-status {
    color: var(--success);
}

.icon-btn {
    background: rgba(255, 255, 255, 0.08);
    border: 1px solid var(--card-border);
    color: #ffd166;
    font-size: 0.95rem;
    padding: 2px 8px;
    border-radius: 12px;
    cursor: pointer;
    transition: all 0.22s var(--ease-spring);
    user-select: none;
    -webkit-user-select: none;
    touch-action: manipulation;
}

.icon-btn:hover {
    background: rgba(255, 255, 255, 0.16);
    transform: scale(1.08);
    box-shadow: 0 0 12px rgba(255, 209, 102, 0.4);
}

/* ===== Toolbar & Mode Selectors ===== */
.toolbar-container {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 6px;
    width: 100%;
}

.mode-selector {
    background: rgba(0, 0, 0, 0.35);
    border: 1px solid var(--card-border);
    border-radius: 30px;
    padding: 3px;
    display: inline-flex;
    gap: 3px;
    box-shadow: inset 0 2px 6px rgba(0, 0, 0, 0.4);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    position: relative;
    z-index: 2;
}

.mode-btn {
    background: transparent;
    border: none;
    color: var(--text-muted);
    font-family: inherit;
    font-size: clamp(0.7rem, 1.5vw, 0.78rem);
    font-weight: 700;
    padding: 3px 10px;
    border-radius: 20px;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    gap: 4px;
    transition: all 0.24s var(--ease-spring);
    user-select: none;
    -webkit-user-select: none;
    touch-action: manipulation;
}

.mode-btn:hover:not(.active) {
    color: #ffffff;
    background: rgba(255, 255, 255, 0.08);
}

.mode-btn.active {
    background: linear-gradient(135deg, var(--accent), var(--accent-dark));
    color: #ffffff;
    box-shadow: 0 0 12px var(--accent-glow), 0 2px 8px rgba(0, 0, 0, 0.3);
    transform: scale3d(1.02, 1.02, 1);
}

.mode-btn#mode-16-btn.active {
    background: linear-gradient(135deg, var(--magic-purple), #7b2cbf);
    box-shadow: 0 0 14px var(--magic-glow), 0 2px 8px rgba(0, 0, 0, 0.3);
}

.mode-icon {
    font-size: clamp(0.8rem, 1.6vw, 0.9rem);
}

/* ===== AI Difficulty Bar ===== */
.ai-difficulty-bar {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 5px;
    background: rgba(0, 0, 0, 0.3);
    padding: 2px 10px;
    border-radius: 20px;
    border: 1px solid rgba(255, 255, 255, 0.08);
    animation: fadeIn 0.3s ease;
    margin-top: 1px;
}

.ai-label {
    font-size: 0.72rem;
    font-weight: 700;
    color: var(--text-muted);
}

.diff-btn {
    background: transparent;
    border: 1px solid transparent;
    color: var(--text-muted);
    font-family: inherit;
    font-size: 0.68rem;
    font-weight: 700;
    padding: 2px 7px;
    border-radius: 12px;
    cursor: pointer;
    transition: all 0.2s ease;
    user-select: none;
    -webkit-user-select: none;
}

.diff-btn.active {
    background: rgba(255, 255, 255, 0.16);
    border-color: var(--card-border-hover);
    color: #ffffff;
    box-shadow: 0 0 8px rgba(255, 255, 255, 0.2);
}

/* ===== Timer Bar for Time Attack Mode ===== */
.timer-container {
    width: 100%;
    max-width: 320px;
    display: flex;
    flex-direction: column;
    gap: 3px;
    animation: fadeIn 0.3s ease;
}

.timer-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 0.78rem;
    font-weight: 800;
}

.timer-label {
    color: var(--text-muted);
}

.timer-val {
    color: var(--warning);
    font-variant-numeric: tabular-nums;
    font-weight: 900;
}

.timer-val.danger {
    color: #ff3366;
    animation: heartbeat 0.8s infinite ease-in-out;
}

.timer-bar {
    height: 7px;
    background: rgba(0, 0, 0, 0.4);
    border-radius: 6px;
    overflow: hidden;
    border: 1px solid var(--card-border);
    position: relative;
}

.timer-fill {
    height: 100%;
    width: 100%;
    background: linear-gradient(90deg, var(--accent), var(--warning), var(--success));
    transition: width 0.2s linear;
}

.timer-fill.danger {
    background: linear-gradient(90deg, #ff0055, #ff3366);
    box-shadow: 0 0 10px rgba(255, 0, 85, 0.8);
}

/* ===== Player Stats & Lifelines ===== */
.players-stats {
    display: flex;
    gap: clamp(0.45rem, 1.6vw, 0.75rem);
    width: 100%;
    justify-content: center;
}

.player-box {
    flex: 1;
    max-width: 185px;
    background: rgba(0, 0, 0, 0.28);
    padding: clamp(0.25rem, 0.6vh, 0.4rem) clamp(0.35rem, 1vw, 0.6rem);
    border-radius: clamp(10px, 1.8vw, 13px);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 2px;
    border: 2px solid rgba(255, 255, 255, 0.08);
    transition: transform 0.3s var(--ease-spring), border-color 0.25s ease, background 0.25s ease, box-shadow 0.25s ease, opacity 0.25s ease;
    opacity: 0.65;
    position: relative;
    overflow: hidden;
    transform: translate3d(0, 0, 0);
}

.player-box::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: linear-gradient(135deg, rgba(255, 255, 255, 0.12), transparent);
    opacity: 0;
    transition: opacity 0.3s ease;
    border-radius: inherit;
    pointer-events: none;
}

.player-box.active {
    opacity: 1;
    border-color: var(--success);
    background: rgba(6, 214, 160, 0.12);
    box-shadow: 0 0 16px var(--success-glow), inset 0 0 10px rgba(6, 214, 160, 0.12);
    transform: translate3d(0, -2px, 0) scale3d(1.02, 1.02, 1);
}

.player-box.active::before {
    opacity: 1;
}

.player-box .label {
    font-size: clamp(0.72rem, 1.7vw, 0.82rem);
    font-weight: 700;
    cursor: pointer;
    border-bottom: 1px dashed rgba(255, 255, 255, 0.35);
    padding-bottom: 1px;
    position: relative;
    z-index: 1;
    transition: color 0.2s ease, border-color 0.2s ease;
    color: var(--text-muted);
    max-width: 100%;
    text-overflow: ellipsis;
    overflow: hidden;
    white-space: nowrap;
    user-select: text;
}

.player-box.active .label {
    color: #ffffff;
    border-bottom-color: var(--success);
}

.player-box .label:focus {
    outline: none;
    border-bottom: 1px solid var(--accent);
    color: #ffffff;
}

.player-box .value {
    font-size: clamp(1.1rem, 2.5vw, 1.35rem);
    font-weight: 900;
    color: var(--accent);
    position: relative;
    z-index: 1;
    line-height: 1.05;
    text-shadow: 0 0 8px rgba(255, 71, 126, 0.4);
    transition: transform 0.25s var(--ease-spring), color 0.2s ease;
    transform: translate3d(0, 0, 0);
}

.player-box.active .value {
    color: var(--success);
    text-shadow: 0 0 12px var(--success-glow);
}

/* ===== Shuffle Lifeline Button ===== */
.lifeline-btn {
    background: rgba(255, 255, 255, 0.08);
    border: 1px solid rgba(255, 255, 255, 0.15);
    color: var(--text-color);
    font-family: inherit;
    font-size: clamp(0.68rem, 1.5vw, 0.76rem);
    font-weight: 700;
    padding: 2px 8px;
    border-radius: 12px;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    gap: 4px;
    position: relative;
    z-index: 2;
    transition: all 0.2s ease;
    touch-action: manipulation;
    user-select: none;
    margin-top: 1px;
}

.player-box.active .lifeline-btn:not(.used) {
    background: rgba(157, 78, 221, 0.25);
    border-color: var(--magic-purple);
    color: #ffffff;
    box-shadow: 0 0 10px rgba(157, 78, 221, 0.4);
}

.player-box.active .lifeline-btn:not(.used):hover {
    background: rgba(157, 78, 221, 0.45);
    transform: scale(1.05);
    box-shadow: 0 0 14px var(--magic-glow);
}

.player-box.active .lifeline-btn:not(.used):active {
    transform: scale(0.96);
}

.lifeline-btn.used, .player-box:not(.active) .lifeline-btn {
    opacity: 0.45;
    cursor: not-allowed;
    border-color: transparent;
    background: rgba(0, 0, 0, 0.2);
}

.lifeline-badge {
    background: var(--magic-purple);
    color: #ffffff;
    font-size: 0.65rem;
    font-weight: 900;
    padding: 0 4px;
    border-radius: 6px;
    line-height: 1.2;
}

.lifeline-btn.used .lifeline-badge {
    background: rgba(255, 255, 255, 0.2);
    color: var(--text-muted);
}

/* ===== Progress Bar ===== */
.progress-container {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    width: 100%;
    max-width: 310px;
    direction: ltr;
}

.progress-bar {
    flex: 1;
    height: 7px;
    background: rgba(0, 0, 0, 0.35);
    border-radius: 10px;
    overflow: hidden;
    border: 1px solid var(--card-border);
    position: relative;
}

.progress-fill {
    height: 100%;
    width: 0%;
    background: linear-gradient(90deg, var(--accent), var(--success));
    border-radius: 10px;
    transition: width 0.45s var(--ease-smooth);
    box-shadow: 0 0 8px var(--success-glow);
    position: relative;
    overflow: hidden;
}

.progress-fill::after {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.4), transparent);
    animation: progressShine 1.8s infinite;
}

@keyframes progressShine {
    0% {
        transform: translate3d(-100%, 0, 0);
    }
    100% {
        transform: translate3d(100%, 0, 0);
    }
}

.progress-text {
    font-size: clamp(0.72rem, 1.6vw, 0.8rem);
    font-weight: 800;
    color: var(--text-muted);
    min-width: 44px;
    text-align: center;
    direction: ltr;
    unicode-bidi: isolate;
    font-variant-numeric: tabular-nums;
}

/* ===== Buttons ===== */
.btn {
    background: rgba(255, 255, 255, 0.08);
    border: 1px solid var(--card-border);
    color: white;
    padding: clamp(0.3rem, 0.6vh, 0.42rem) clamp(0.85rem, 1.8vw, 1.2rem);
    font-family: inherit;
    font-size: clamp(0.78rem, 1.7vw, 0.86rem);
    font-weight: 700;
    border-radius: 50px;
    cursor: pointer;
    transition: transform 0.22s var(--ease-spring), background 0.2s ease, box-shadow 0.2s ease, border-color 0.2s ease;
    backdrop-filter: blur(6px);
    -webkit-backdrop-filter: blur(6px);
    position: relative;
    overflow: hidden;
    z-index: 1;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 0.35rem;
    touch-action: manipulation;
    user-select: none;
    transform: translate3d(0, 0, 0);
}

.btn::before {
    content: '';
    position: absolute;
    top: 50%;
    left: 50%;
    width: 0;
    height: 0;
    background: rgba(255, 255, 255, 0.2);
    border-radius: 50%;
    transform: translate3d(-50%, -50%, 0);
    transition: width 0.35s var(--ease-out), height 0.35s var(--ease-out);
    z-index: -1;
}

.btn:hover::before {
    width: 220px;
    height: 220px;
}

.btn:hover {
    background: rgba(255, 255, 255, 0.16);
    transform: translate3d(0, -2px, 0);
    box-shadow: 0 4px 14px rgba(0, 0, 0, 0.3);
    border-color: var(--card-border-hover);
}

.btn:active {
    transform: translate3d(0, 0, 0) scale3d(0.97, 0.97, 1);
}

.btn.primary {
    background: linear-gradient(135deg, var(--accent), var(--accent-dark));
    border: none;
    color: white;
    box-shadow: 0 4px 14px var(--accent-glow);
}

.btn.primary:hover {
    background: linear-gradient(135deg, #ff5e8f, var(--accent));
    box-shadow: 0 6px 18px var(--accent-glow);
    transform: translate3d(0, -2px, 0);
}

/* ===== Card Grid ===== */
.grid-container {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: clamp(5px, 1.2vw, 8px);
    width: 100%;
    box-sizing: border-box;
}

/* 16 Pairs (32 Cards) Responsive Layout */
.game-container.mode-16 .grid-container {
    grid-template-columns: repeat(8, 1fr);
    gap: clamp(4px, 0.8vw, 6px);
}

.game-container.mode-16 .emoji-symbol {
    font-size: clamp(1.1rem, 2.8vw, 1.6rem);
}

.game-container.mode-16 .card-icon {
    font-size: clamp(0.9rem, 2.2vw, 1.25rem);
}

@media (max-width: 680px) {
    .game-container.mode-16 {
        max-width: 460px;
    }
    .game-container.mode-16 .grid-container {
        grid-template-columns: repeat(4, 1fr);
        gap: clamp(4px, 1vw, 6px);
    }
    .game-container.mode-16 .emoji-symbol {
        font-size: clamp(1.2rem, 3.8vw, 1.65rem);
    }
}

@media (min-width: 461px) and (max-width: 680px) {
    .game-container.mode-16 {
        max-width: 540px;
    }
    .game-container.mode-16 .grid-container {
        grid-template-columns: repeat(6, 1fr);
        gap: 5px;
    }
}

/* ===== Three-Layer Card Architecture with 3D Tilt ===== */
.card {
    aspect-ratio: 1 / 1;
    width: 100%;
    position: relative;
    cursor: pointer;
    border-radius: clamp(9px, 1.8vw, 13px);
    touch-action: manipulation;
    user-select: none;
    -webkit-user-select: none;
    transform-style: preserve-3d;
    -webkit-transform-style: preserve-3d;
    transform: perspective(800px) rotateX(0deg) rotateY(0deg) translate3d(0, 0, 0);
    will-change: transform;
}

.grid-container .card:active {
    transform: perspective(800px) scale3d(0.96, 0.96, 1);
}

/* ===== Luminous 3D Vortex Swirling Animation ===== */
.card.swirling {
    animation: vortexSwirl3D 0.82s cubic-bezier(0.34, 1.35, 0.64, 1) forwards;
    z-index: 100;
    pointer-events: none;
}

@keyframes vortexSwirl3D {
    0% {
        transform: perspective(900px) translate3d(0, 0, 0) scale3d(1, 1, 1) rotateZ(0deg);
        filter: brightness(1);
    }
    30% {
        transform: perspective(900px) translate3d(var(--swirl-dx, 0px), var(--swirl-dy, 0px), 70px) scale3d(1.16, 1.16, 1) rotateZ(var(--swirl-rot, 15deg)) rotateY(20deg);
        box-shadow: 0 0 35px var(--magic-glow), 0 0 60px rgba(6, 214, 160, 0.45);
        filter: brightness(1.25);
    }
    60% {
        transform: perspective(900px) translate3d(calc(var(--swirl-dx, 0px) * -0.4), calc(var(--swirl-dy, 0px) * -0.4), 45px) scale3d(1.1, 1.1, 1) rotateZ(calc(var(--swirl-rot, 15deg) * -0.5));
        box-shadow: 0 0 25px var(--magic-glow);
        filter: brightness(1.15);
    }
    85% {
        transform: perspective(900px) translate3d(0, 0, 8px) scale3d(0.98, 0.98, 1) rotateZ(0deg);
        box-shadow: 0 0 10px var(--magic-glow);
        filter: brightness(1.05);
    }
    100% {
        transform: perspective(900px) translate3d(0, 0, 0) scale3d(1, 1, 1) rotateZ(0deg);
        box-shadow: var(--card-shadow);
        filter: brightness(1);
    }
}

/* Inner 3D Engine: Rotates on Y Axis */
.card-inner {
    position: relative;
    width: 100%;
    height: 100%;
    border-radius: inherit;
    transform-style: preserve-3d;
    -webkit-transform-style: preserve-3d;
    transition: var(--transition-flip);
    will-change: transform;
    transform: translate3d(0, 0, 0);
}

.card.flipped .card-inner {
    transform: rotate3d(0, 1, 0, 180deg);
}

.card.matched .card-inner {
    transform: rotate3d(0, 1, 0, 180deg);
}

.card.matched {
    pointer-events: none;
    z-index: 5;
}

/* Front and Back Faces */
.card-face {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    backface-visibility: hidden;
    -webkit-backface-visibility: hidden;
    display: flex;
    justify-content: center;
    align-items: center;
    border-radius: inherit;
    box-shadow: var(--card-shadow);
    overflow: hidden;
    transform: translate3d(0, 0, 0);
}

.card-icon {
    font-size: clamp(1.1rem, 3.2vw, 1.55rem);
    opacity: 0.8;
    filter: drop-shadow(0 2px 4px rgba(0, 0, 0, 0.35));
    transition: transform 0.25s var(--ease-spring), opacity 0.2s ease;
    transform: translate3d(0, 0, 0);
}

.card:not(.flipped):not(.matched):hover .card-icon {
    transform: scale3d(1.18, 1.18, 1) rotate(6deg);
    opacity: 1;
}

.emoji-symbol {
    font-size: clamp(1.4rem, 4.5vw, 2.1rem);
    transform: translate3d(0, 0, 0);
}

/* Dynamic Glare & Shading Light Reflection */
.card-glare {
    position: absolute;
    top: -50%;
    left: -50%;
    width: 200%;
    height: 200%;
    background: radial-gradient(circle at 50% 50%, rgba(255, 255, 255, 0.25) 0%, transparent 60%);
    pointer-events: none;
    opacity: 0.5;
    transition: opacity 0.25s ease;
}

.card-front {
    background: linear-gradient(135deg, rgba(255, 255, 255, 0.13), rgba(255, 255, 255, 0.04));
    border: 1px solid var(--card-border);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    transition: border-color 0.24s ease, background 0.24s ease, box-shadow 0.24s ease;
}

.card:not(.flipped):not(.matched):hover .card-front {
    border-color: var(--card-border-hover);
    box-shadow: var(--card-shadow-hover);
}

.card-back {
    background: linear-gradient(135deg, #ffffff, #f4f4f9);
    transform: rotate3d(0, 1, 0, 180deg);
    border: 2px solid var(--accent);
    box-shadow: 0 0 12px rgba(255, 71, 126, 0.3);
}

.card.matched .card-back {
    border-color: var(--warning);
    background: linear-gradient(135deg, #fff9db, #ffeaa7);
    box-shadow: 0 0 20px rgba(255, 209, 102, 0.85);
    animation: silkyMatch 0.55s var(--ease-spring);
}

@keyframes silkyMatch {
    0% {
        transform: rotate3d(0, 1, 0, 180deg) scale3d(1, 1, 1);
    }
    35% {
        transform: rotate3d(0, 1, 0, 180deg) scale3d(1.15, 1.15, 1);
    }
    70% {
        transform: rotate3d(0, 1, 0, 180deg) scale3d(0.98, 0.98, 1);
    }
    100% {
        transform: rotate3d(0, 1, 0, 180deg) scale3d(1, 1, 1);
    }
}

/* ===== Smooth Modal Overlay & Windows ===== */
.modal-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    width: 100vw;
    height: 100vh;
    height: 100dvh;
    background: rgba(8, 3, 18, 0.82);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    display: flex;
    justify-content: center;
    align-items: center;
    padding: clamp(0.8rem, 2.5vw, 1.2rem);
    opacity: 0;
    visibility: hidden;
    pointer-events: none;
    transition: opacity 0.35s var(--ease-smooth), visibility 0.35s var(--ease-smooth);
    z-index: 100000;
}

.modal-overlay.active {
    opacity: 1;
    visibility: visible;
    pointer-events: all;
}

.modal {
    background: rgba(28, 12, 50, 0.96);
    border: 1px solid rgba(255, 255, 255, 0.22);
    padding: clamp(1.3rem, 3.5vw, 1.8rem) clamp(1rem, 3vw, 1.6rem);
    border-radius: clamp(18px, 3.5vw, 24px);
    text-align: center;
    transform: scale3d(0.88, 0.88, 1) translate3d(0, 20px, 0);
    transition: transform 0.45s var(--ease-spring);
    box-shadow: 0 25px 60px rgba(0, 0, 0, 0.75), 0 0 40px rgba(255, 71, 126, 0.35);
    position: relative;
    overflow: hidden;
    width: 100%;
    max-width: 380px;
    z-index: 100001;
    will-change: transform;
}

.modal-overlay.active .modal {
    transform: scale3d(1, 1, 1) translate3d(0, 0, 0);
}

.modal-glow {
    position: absolute;
    top: -50%;
    left: -50%;
    width: 200%;
    height: 200%;
    background: radial-gradient(circle, rgba(255, 71, 126, 0.22), transparent 65%);
    animation: modalGlow 5s ease-in-out infinite alternate;
    pointer-events: none;
    z-index: 0;
}

@keyframes modalGlow {
    0% {
        transform: scale3d(1, 1, 1) rotate(0deg);
        opacity: 0.5;
    }
    100% {
        transform: scale3d(1.12, 1.12, 1) rotate(10deg);
        opacity: 0.8;
    }
}

.modal h2 {
    font-size: clamp(1.35rem, 3.8vw, 1.65rem);
    margin-bottom: 0.5rem;
    color: var(--success);
    position: relative;
    z-index: 1;
    text-shadow: 0 0 12px var(--success-glow);
}

.modal p {
    font-size: clamp(0.88rem, 2.2vw, 0.96rem);
    margin-bottom: 0.5rem;
    color: var(--text-muted);
    position: relative;
    z-index: 1;
}

.modal #winner-text {
    font-weight: 900;
    color: var(--success);
    font-size: clamp(1.15rem, 3.2vw, 1.35rem);
    margin: 0.7rem 0;
}

.final-scores {
    display: flex;
    flex-direction: column;
    gap: 0.4rem;
    margin: 0.85rem 0;
    position: relative;
    z-index: 1;
}

.final-scores p {
    font-size: clamp(0.85rem, 2vw, 0.92rem);
    padding: 0.4rem 0.7rem;
    background: rgba(0, 0, 0, 0.3);
    border-radius: 10px;
    border: 1px solid rgba(255, 255, 255, 0.05);
    color: var(--text-color);
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.modal span {
    font-weight: 900;
    color: var(--accent);
}

.modal .btn {
    margin-top: 0.5rem;
    position: relative;
    z-index: 1;
}

.modal-action-buttons {
    display: flex;
    flex-direction: column;
    gap: 8px;
    margin-top: 10px;
    position: relative;
    z-index: 1;
}

.btn.gold-btn {
    background: linear-gradient(135deg, #ffd166, #ff9f1c);
    color: #1a082b;
    border: none;
    font-weight: 800;
    box-shadow: 0 4px 14px rgba(255, 209, 102, 0.4);
}

.btn.gold-btn:hover {
    background: linear-gradient(135deg, #ffe082, #ffa726);
    transform: translate3d(0, -2px, 0);
    box-shadow: 0 6px 18px rgba(255, 209, 102, 0.6);
}

.modal-buttons {
    display: flex;
    gap: 0.5rem;
    justify-content: center;
    margin-top: 0.85rem;
}

/* ===== Stats Modal Content & Grid ===== */
.stats-modal-content {
    max-width: 420px;
}

.stats-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 8px;
    margin: 14px 0;
    position: relative;
    z-index: 1;
}

.stat-card {
    background: rgba(0, 0, 0, 0.35);
    border: 1px solid rgba(255, 255, 255, 0.08);
    border-radius: 12px;
    padding: 10px 8px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 3px;
    transition: transform 0.2s ease, border-color 0.2s ease;
}

.stat-card:hover {
    border-color: rgba(255, 255, 255, 0.2);
    transform: translateY(-2px);
}

.stat-icon {
    font-size: 1.25rem;
}

.stat-title {
    font-size: 0.68rem;
    color: var(--text-muted);
    font-weight: 700;
    text-align: center;
}

.stat-num {
    font-size: 1.15rem;
    color: var(--warning);
    font-weight: 900;
}

/* ===== Special Action Cards (Freeze & Gold Perks) ===== */
.card.card-freeze .card-back {
    border-color: #00f0ff !important;
    background: linear-gradient(135deg, #e6fcff, #c2f8ff) !important;
    box-shadow: 0 0 20px rgba(0, 240, 255, 0.9) !important;
}

.card.card-gold .card-back {
    border-color: #ffd700 !important;
    background: linear-gradient(135deg, #fffdf0, #ffeaa7) !important;
    box-shadow: 0 0 24px rgba(255, 215, 0, 0.95) !important;
}

/* Player States: Frozen & AI Thinking */
.player-box.frozen {
    border-color: #00f0ff !important;
    background: rgba(0, 240, 255, 0.15) !important;
    box-shadow: 0 0 18px rgba(0, 240, 255, 0.5) !important;
    position: relative;
}

.player-box.frozen::after {
    content: '❄️ مجمّد';
    position: absolute;
    top: 2px;
    right: 4px;
    font-size: 0.62rem;
    font-weight: 900;
    color: #00f0ff;
    text-shadow: 0 0 6px rgba(0, 240, 255, 0.8);
}

.player-box.thinking {
    animation: aiThinking 1s infinite alternate ease-in-out;
}

@keyframes aiThinking {
    from {
        border-color: rgba(255, 255, 255, 0.2);
        box-shadow: 0 0 8px rgba(157, 78, 221, 0.3);
    }
    to {
        border-color: var(--magic-purple);
        box-shadow: 0 0 20px var(--magic-glow);
    }
}

/* Screen Shake Effect */
.screen-shake {
    animation: screenShake 0.4s cubic-bezier(.36,.07,.19,.97) both;
}

@keyframes screenShake {
    10%, 90% { transform: translate3d(-2px, 0, 0); }
    20%, 80% { transform: translate3d(3px, 0, 0); }
    30%, 50%, 70% { transform: translate3d(-4px, 0, 0); }
    40%, 60% { transform: translate3d(4px, 0, 0); }
}

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

/* ===== Winner Animation ===== */
.winner-name-anim {
    display: inline-block;
    color: var(--warning);
    text-shadow: 0 0 10px rgba(255, 209, 102, 0.8);
    animation: winnerLoop 1.8s infinite ease-in-out;
    font-size: clamp(1.25rem, 3.5vw, 1.5rem);
    padding: 0 4px;
    font-weight: 900;
    transform: translate3d(0, 0, 0);
    will-change: transform;
}

@keyframes winnerLoop {
    0%, 100% {
        transform: scale3d(1, 1, 1);
        color: var(--warning);
    }
    50% {
        transform: scale3d(1.08, 1.08, 1);
        color: #ff9f1c;
    }
}

/* ===== Short Screen Adjustments ===== */
@media (max-height: 650px) {
    body {
        padding: 0.3rem;
    }

    .game-container {
        padding: 0.6rem 0.8rem;
        gap: 0.4rem;
    }

    header {
        gap: 0.25rem;
    }

    h1 {
        font-size: 1.15rem;
    }

    .player-box {
        padding: 0.2rem 0.4rem;
    }

    .player-box .value {
        font-size: 1.1rem;
    }

    .grid-container {
        gap: 5px;
    }

    .btn {
        padding: 0.25rem 0.8rem;
        font-size: 0.78rem;
    }
}

/* ===== Score Pop Effect ===== */
.score-pop {
    transform: scale3d(1.35, 1.35, 1);
    color: var(--success);
}

/* ===== Game Footer & Brand Link ===== */
.game-footer {
    display: flex;
    justify-content: center;
    align-items: center;
    padding-top: clamp(0.25rem, 0.6vh, 0.45rem);
    margin-top: clamp(0.1rem, 0.3vh, 0.25rem);
    border-top: 1px solid rgba(255, 255, 255, 0.08);
}

.game-footer-link {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 7px;
    font-size: clamp(0.72rem, 1.8vw, 0.8rem);
    font-weight: 600;
    color: rgba(255, 255, 255, 0.65);
    text-decoration: none;
    padding: 3px 8px;
    border-radius: 12px;
    transition: all 0.25s var(--ease-smooth);
    letter-spacing: 0.2px;
}

.game-footer-logo {
    width: 15px;
    height: 15px;
    object-fit: contain;
    filter: drop-shadow(0 0 4px rgba(6, 214, 160, 0.35));
    transition: transform 0.25s var(--ease-smooth);
}

.game-footer-link:hover {
    color: #ffffff;
    background: rgba(255, 255, 255, 0.06);
}

.game-footer-link:hover .game-footer-logo {
    transform: scale(1.15);
}

/* ===== Accessibility: Prefers Reduced Motion ===== */
@media (prefers-reduced-motion: reduce) {
    .background-animation,
    .particle,
    .card.swirling,
    .modal-glow,
    .winner-name-anim {
        animation: none !important;
    }
    .card-inner,
    .player-box,
    .modal,
    .btn {
        transition-duration: 0.05s !important;
    }
}