:root {
    --bg-dark: #050510;
    --bg-panel: #0f0a1e;
    --gold-primary: #ffd700;
    --gold-secondary: #d4af37;
    --gold-dim: rgba(255, 215, 0, 0.2);
    --text-primary: #ffffff;
    --text-secondary: rgba(255, 255, 255, 0.7);
    --glass: rgba(255, 255, 255, 0.05);
    --glass-border: rgba(255, 255, 255, 0.1);
    --grid-gap: 15px;
    --tile-size: 100px;
    /* Base size, responsive */
    --tile-radius: 8px;
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    -webkit-tap-highlight-color: transparent;
}

body {
    background-color: var(--bg-dark);
    color: var(--text-primary);
    font-family: 'Montserrat', sans-serif;
    min-height: 100vh;
    overflow-x: hidden;
    background: radial-gradient(circle at 50% 0%, #1a0b2e 0%, var(--bg-dark) 80%);
    padding-top: calc(68px + env(safe-area-inset-top));
    display: flex;
    flex-direction: column;
    align-items: center;
}

/* Luxury Header */
.header {
    position: fixed;
    top: calc(env(safe-area-inset-top) + 3px);
    left: 0;
    width: 100%;
    height: 65px;
    background: rgba(5, 5, 16, 0.9);
    backdrop-filter: blur(20px);
    -webkit-backdrop-filter: blur(20px);
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0 20px;
    z-index: 1000;
    border-bottom: 1px solid var(--glass-border);
    opacity: 0;
    /* Hidden initially */
    pointer-events: none;
    transition: opacity 0.5s ease;
}

.header-title {
    font-family: 'Playfair Display', serif;
    font-size: 1.5rem;
    font-weight: 700;
    background: linear-gradient(to right, #fff, var(--gold-primary));
    -webkit-background-clip: text;
    background-clip: text;
    -webkit-text-fill-color: transparent;
    letter-spacing: 2px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, calc(-50% + 2px));
    /* Perfectly centered + 2px down */
}

.wallet-badge {
    position: absolute;
    right: 20px;
    background: var(--glass);
    border: 1px solid var(--gold-dim);
    padding: 5px 12px;
    border-radius: 20px;
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 0.9rem;
    color: var(--gold-primary);
    font-family: 'Montserrat', sans-serif;
    font-weight: 600;
}

/* Game Container */
.game-container {
    padding: 20px;
    width: 100%;
    max-width: 500px;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 20px;
}

/* Score Board */
.scores-wrapper {
    display: flex;
    gap: 15px;
    width: 100%;
    justify-content: space-between;
}

.score-box {
    background: var(--glass);
    border: 1px solid var(--glass-border);
    padding: 10px 20px;
    border-radius: 12px;
    text-align: center;
    flex: 1;
    position: relative;
    overflow: hidden;
}

.score-box::after {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 2px;
    background: linear-gradient(90deg, transparent, var(--gold-secondary), transparent);
    opacity: 0.5;
}

.score-label {
    font-size: 0.7rem;
    text-transform: uppercase;
    color: var(--text-secondary);
    letter-spacing: 1px;
}

.score-value {
    font-family: 'Playfair Display', serif;
    font-size: 1.5rem;
    font-weight: 700;
    color: white;
    margin-top: 5px;
}

.score-add {
    position: absolute;
    right: 18px;
    bottom: 25px;
    font-size: 1rem;
    color: var(--gold-primary);
    font-weight: bold;
    opacity: 0;
    z-index: 100;
}

@keyframes floatUp {
    0% {
        transform: translateY(0);
        opacity: 1;
    }

    100% {
        transform: translateY(-30px);
        opacity: 0;
    }
}

.float-up {
    animation: floatUp 0.8s ease-out forwards;
}

/* Menu Bar */
.menu-bar {
    display: flex;
    justify-content: space-between;
    width: 100%;
    margin-bottom: 10px;
}

.menu-btn {
    background: var(--glass);
    border: 1px solid var(--glass-border);
    color: white;
    padding: 10px 20px;
    border-radius: 8px;
    font-family: 'Montserrat', sans-serif;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.3s ease;
    text-transform: uppercase;
    font-size: 0.8rem;
    letter-spacing: 1px;
}

.menu-btn.primary {
    background: linear-gradient(135deg, var(--gold-primary), var(--gold-secondary));
    color: #000;
    border: none;
    box-shadow: 0 4px 15px rgba(212, 175, 55, 0.3);
}

/* 2048 Grid */
.game-board {
    background: #0a0a10;
    border: 2px solid var(--gold-dim);
    padding: var(--grid-gap);
    border-radius: 12px;
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    grid-template-rows: repeat(4, 1fr);
    gap: var(--grid-gap);
    width: 100%;
    aspect-ratio: 1;
    position: relative;
    touch-action: none;
    /* Prevent scrolling while swiping */
}

.grid-cell {
    background: rgba(255, 255, 255, 0.05);
    border-radius: var(--tile-radius);
    width: 100%;
    height: 100%;
}

.tile {
    position: relative;
    /* Changed from absolute to fix sizing */
    width: 100%;
    height: 100%;
    border-radius: var(--tile-radius);
    display: flex;
    justify-content: center;
    align-items: center;
    font-family: 'Montserrat', sans-serif;
    /* Cleaner font for numbers */
    font-weight: 800;
    font-size: 2.2rem;
    /* Reduced base size */
    transition: transform 0.15s ease-in-out, background-color 0.15s;
    z-index: 10;
    color: #fff;
    box-shadow: 0 0 15px rgba(0, 0, 0, 0.3);
    text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.5);
}

.tile-inner {
    width: 100%;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
}

/* Tile Colors - Cosmic Neon Palette covering full box */
/* Large font for single/double digits, smaller for more */
.tile-2 {
    background: linear-gradient(135deg, #00f260, #0575e6);
    font-size: 3rem;
}

.tile-4 {
    background: linear-gradient(135deg, #b21f1f, #fdbb2d);
    font-size: 3rem;
}

.tile-8 {
    background: linear-gradient(135deg, #c31432, #240b36);
    font-size: 3rem;
}

.tile-16 {
    background: linear-gradient(135deg, #12c2e9, #c471ed, #f64f59);
    font-size: 2.5rem;
}

.tile-32 {
    background: linear-gradient(135deg, #833ab4, #fd1d1d, #fcb045);
    font-size: 2.5rem;
}

.tile-64 {
    background: linear-gradient(135deg, #fe8c00, #f83600);
    font-size: 2.5rem;
}

.tile-128 {
    background: linear-gradient(135deg, #02aab0, #00cdac);
    font-size: 2.2rem;
    box-shadow: 0 0 20px rgba(2, 170, 176, 0.6);
}

.tile-256 {
    background: linear-gradient(135deg, #DA22FF, #9733EE);
    font-size: 2rem;
    box-shadow: 0 0 25px rgba(218, 34, 255, 0.6);
}

.tile-512 {
    background: linear-gradient(135deg, #FF416C, #FF4B2B);
    font-size: 2rem;
    box-shadow: 0 0 25px rgba(255, 65, 108, 0.6);
}

.tile-1024 {
    background: linear-gradient(135deg, #FDC830, #F37335);
    font-size: 1.6rem;
    box-shadow: 0 0 30px rgba(253, 200, 48, 0.6);
}

.tile-2048 {
    background: linear-gradient(135deg, #FF0099, #493240);
    color: #fff;
    box-shadow: 0 0 40px rgba(255, 0, 153, 0.8);
    font-size: 1.6rem;
    animation: pulse 2s infinite;
    border: 2px solid rgba(255, 255, 255, 0.5);
}

.tile-super {
    background: #000;
    border: 2px solid var(--gold-primary);
    color: var(--gold-primary);
    font-size: 1.4rem;
}

@keyframes pulse {
    0% {
        box-shadow: 0 0 20px rgba(255, 0, 153, 0.6);
    }

    50% {
        box-shadow: 0 0 40px rgba(255, 0, 153, 0.9);
        transform: scale(1.02);
    }

    100% {
        box-shadow: 0 0 20px rgba(255, 0, 153, 0.6);
    }
}

@keyframes popIn {
    0% {
        transform: scale(0);
        opacity: 0;
    }

    50% {
        transform: scale(1.1);
    }

    100% {
        transform: scale(1);
        opacity: 1;
    }
}

.tile-new {
    animation: popIn 0.2s ease-out backwards;
}

@keyframes merge {
    0% {
        transform: scale(1);
    }

    50% {
        transform: scale(1.15);
    }

    100% {
        transform: scale(1);
    }
}

.tile-merged {
    animation: merge 0.2s ease-in-out;
    z-index: 20;
}

/* Modal specific styles override/addition */
/* Modal specific styles */
.modal-content {
    background: var(--bg-panel);
    width: 90%;
    max-width: 400px;
    padding: 20px;
    border-radius: 16px;
    border: 1px solid var(--gold-dim);
    box-shadow: 0 0 30px rgba(0, 0, 0, 0.8);
    display: flex;
    flex-direction: column;
    max-height: 80vh;
}

.modal-content h2 {
    text-align: center;
    font-family: 'Playfair Display', serif;
    color: var(--gold-primary);
    margin-bottom: 15px;
}

/* Reusing Leaderboard Section styles within modal */
.leaderboard-section {
    /* Deprecated in favor of modal structure but reusing inner classes */
    display: none;
}

.tabs {
    display: flex;
    padding: 0 10px 10px 10px;
    border-bottom: 1px solid var(--glass-border);
}

.tab {
    flex: 1;
    background: none;
    border: none;
    padding: 12px;
    color: var(--text-secondary);
    font-family: 'Montserrat', sans-serif;
    font-weight: 600;
    font-size: 0.9rem;
    cursor: pointer;
    position: relative;
    transition: color 0.3s;
}

.tab.active {
    color: var(--gold-primary);
}

.tab.active::after {
    content: '';
    position: absolute;
    bottom: -2px;
    /* Aligned with border bottom of tabs container if any */
    left: 20%;
    width: 60%;
    height: 2px;
    background: var(--gold-primary);
    box-shadow: 0 0 10px var(--gold-primary);
}

.tab-content {
    flex: 1;
    overflow-y: auto;
    padding: 15px;
}

.lb-header {
    display: flex;
    justify-content: space-between;
    font-size: 0.75rem;
    color: var(--text-secondary);
    padding-bottom: 10px;
    border-bottom: 1px solid var(--glass-border);
    margin-bottom: 10px;
}

.lb-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 10px 0;
    border-bottom: 1px solid rgba(255, 255, 255, 0.03);
}

.lb-user {
    display: flex;
    align-items: center;
    gap: 10px;
}

.rank {
    width: 24px;
    font-weight: 700;
    color: var(--gold-dim);
}

.rank-1 {
    color: #FFD700;
    text-shadow: 0 0 5px rgba(255, 215, 0, 0.5);
}

.rank-2 {
    color: #C0C0C0;
}

.rank-3 {
    color: #CD7F32;
}

.user-name {
    font-size: 0.95rem;
    font-weight: 500;
}

.user-score {
    font-family: 'Orbitron', sans-serif;
    /* Tech feel for numbers */
    color: var(--gold-primary);
    font-size: 0.9rem;
}

.timer-banner {
    background: rgba(212, 175, 55, 0.1);
    color: var(--gold-secondary);
    font-size: 0.8rem;
    text-align: center;
    padding: 8px;
    border-radius: 0 0 14px 14px;
    /* Bottom rounded inside container */
    margin: 0 10px 10px 10px;
    border: 1px solid rgba(212, 175, 55, 0.2);
}

/* Base Overlay System (Leaderboard, Rules, etc) */
.overlay {
    position: fixed;
    inset: 0;
    background: rgba(5, 5, 16, 0.95);
    display: none;
    /* Changed from flex to none */
    flex-direction: column;
    justify-content: center;
    align-items: center;
    z-index: 5000;
    backdrop-filter: blur(10px);
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.3s ease;
}

.overlay.active {
    display: flex;
    opacity: 1;
    pointer-events: all;
}

/* Specific overlay for Game Board (Game Over) */
.game-board .overlay {
    position: absolute;
    z-index: 100;
    border-radius: 12px;
    display: flex;
    /* Overrides display:none for game layout */
}

/* Start Screen Gate */
.start-screen-gate {
    position: fixed;
    inset: 0;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    z-index: 3000;
    transition: opacity 0.5s ease, visibility 0.5s;
    background: #050510;
    overflow-y: auto;
    padding: 60px 20px;
}

.overlay h2 {
    font-family: 'Playfair Display', serif;
    font-size: 2.5rem;
    color: white;
    margin-bottom: 10px;
}

.overlay p {
    color: var(--text-secondary);
    margin-bottom: 25px;
}

/* Mobile Adjustments */
@media (max-width: 400px) {
    :root {
        --tile-size: 70px;
        --grid-gap: 10px;
    }

    .header-title {
        font-size: 1.2rem;
    }

    .score-value {
        font-size: 1.2rem;
    }
}

/* Loading Screen Styles */
.game-title-large {
    font-family: 'Playfair Display', serif;
    background: linear-gradient(to right, #fff, var(--gold-primary));
    -webkit-background-clip: text;
    background-clip: text;
    -webkit-text-fill-color: transparent;
    font-size: 2.5rem;
    margin-bottom: 20px;
    letter-spacing: 4px;
}

/* New UI Elements (Matched to Slash) */
.game-logo {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    text-align: center;
}

.start-btn {
    background: linear-gradient(135deg, var(--gold-primary), #d4af37);
    border: none;
    color: #000;
    padding: 15px 40px;
    border-radius: 30px;
    font-size: 1.2rem;
    font-weight: 900;
    font-family: 'Orbitron', sans-serif;
    cursor: pointer;
    box-shadow: 0 0 20px rgba(255, 215, 0, 0.4);
    position: relative;
    overflow: hidden;
    transition: transform 0.2s;
    margin-bottom: 20px;
}

.start-btn:active {
    transform: scale(0.95);
}

.btn-shine {
    position: absolute;
    top: 0;
    left: -100%;
    width: 100%;
    height: 100%;
    background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.6), transparent);
    animation: shine 3s infinite;
}

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

    20% {
        left: 100%;
    }

    100% {
        left: 100%;
    }
}

.icon-btn {
    background: var(--glass);
    border: 1px solid var(--glass-border);
    color: white;
    padding: 10px 20px;
    border-radius: 15px;
    cursor: pointer;
    font-size: 0.9rem;
    font-family: 'Montserrat', sans-serif;
    font-weight: 600;
    transition: all 0.2s;
}

.icon-btn:hover {
    background: rgba(255, 255, 255, 0.1);
    border-color: var(--gold-dim);
}

.back-btn {
    background: transparent;
    border: 1px solid var(--glass-border);
    color: var(--text-secondary);
    padding: 10px 25px;
    border-radius: 20px;
    font-size: 0.8rem;
    cursor: pointer;
    font-family: 'Montserrat', sans-serif;
    transition: all 0.2s;
}

.back-btn:hover {
    color: white;
    border-color: rgba(255, 255, 255, 0.3);
}

/* Loading Screen Styles - Cosmic Edition */
.summon-circle-container {
    position: relative;
    width: 200px;
    height: 200px;
    display: flex;
    justify-content: center;
    align-items: center;
    margin: 0 auto 30px auto;
}

.rune-ring {
    position: absolute;
    border-radius: 50%;
    border: 1px solid transparent;
    /* Center rings absolute */
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
}

/* Outer Ring - Gold */
.ring-outer {
    width: 200px;
    height: 200px;
    border: 1px solid rgba(255, 215, 0, 0.2);
    border-top: 2px solid var(--gold-primary);
    border-bottom: 2px solid var(--gold-primary);
    animation: spin-outer 10s linear infinite;
    box-shadow: 0 0 15px rgba(255, 215, 0, 0.1);
}

/* Middle Ring - Tech Blue/White */
.ring-middle {
    width: 140px;
    height: 140px;
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-left: 2px solid #fff;
    border-right: 2px solid #fff;
    animation: spin-middle 6s linear infinite reverse;
    box-shadow: 0 0 20px rgba(255, 255, 255, 0.1);
}

/* Inner Ring - Dashed */
.ring-inner {
    width: 80px;
    height: 80px;
    border: 2px dashed rgba(5, 117, 230, 0.6);
    animation: spin-inner 15s linear infinite;
}

@keyframes spin-outer {
    from {
        transform: translate(-50%, -50%) rotate(0deg);
    }

    to {
        transform: translate(-50%, -50%) rotate(360deg);
    }
}

@keyframes spin-middle {
    from {
        transform: translate(-50%, -50%) rotate(0deg);
    }

    to {
        transform: translate(-50%, -50%) rotate(-360deg);
    }
}

@keyframes spin-inner {
    from {
        transform: translate(-50%, -50%) rotate(0deg);
    }

    to {
        transform: translate(-50%, -50%) rotate(360deg);
    }
}

/* Core - Singularity (No Ball) */
.core-gem {
    position: absolute;
    width: 4px;
    height: 4px;
    background: #fff;
    border-radius: 50%;
    box-shadow:
        0 0 10px #fff,
        0 0 20px var(--gold-primary),
        0 0 40px var(--gold-primary),
        0 0 80px var(--gold-primary);
    animation: pulseCore 2.5s ease-in-out infinite;
    z-index: 10;
}

@keyframes spin {
    to {
        transform: rotate(360deg);
    }
}

@keyframes pulseCore {

    0%,
    100% {
        transform: scale(1);
        opacity: 0.8;
    }

    50% {
        transform: scale(1.5);
        opacity: 1;
        box-shadow: 0 0 30px #fff, 0 0 60px var(--gold-primary);
    }
}

@keyframes planet-pulse {
    0% {
        transform: scale(1);
        box-shadow: 0 0 30px var(--gold-dim);
    }

    50% {
        transform: scale(1.1);
        box-shadow: 0 0 60px var(--gold-primary);
    }

    100% {
        transform: scale(1);
        box-shadow: 0 0 30px var(--gold-dim);
    }
}

#loading-text {
    font-family: 'Orbitron', sans-serif;
    color: var(--text-secondary);
    font-size: 0.8rem;
    letter-spacing: 3px;
    margin-top: 20px;
    animation: blink 1.5s infinite;
}

@keyframes blink {

    0%,
    100% {
        opacity: 1;
    }

    50% {
        opacity: 0.3;
    }
}

.menu-btn.large {
    padding: 15px 0;
    font-size: 0.9rem;
    width: 100%;
    letter-spacing: 2px;
    margin-bottom: 10px;
}