:root {
    --bg: #0a0a0f;
    --card-bg: #111118;
    --card-border: #1e1e2a;
    --text: #e0e0e0;
    --text-secondary: #a0a0b8;
    --cyan: #00e5ff;
    --pink: #ff4081;
    --green: #4cff4c;
    --green-dark: #1a8a1a;
    --gold: #ffd740;
    --orange: #ff6d00;
    --red: #ff1744;
    --grid-line: rgba(255, 255, 255, 0.025);
    --shadow-glow: 0 0 40px rgba(0, 229, 255, 0.25), 0 0 80px rgba(0, 229, 255, 0.08);
    --shadow-card: 0 8px 40px rgba(0, 0, 0, 0.5);
    --radius-lg: 24px;
    --radius-md: 16px;
    --radius-sm: 10px;
    --font-mono: 'SF Mono', 'Cascadia Code', 'JetBrains Mono', 'Courier New', monospace;
    --font-sans: 'Segoe UI', system-ui, -apple-system, sans-serif;
    --transition-fast: 0.15s cubic-bezier(0.4, 0, 0.2, 1);
    --transition-smooth: 0.35s cubic-bezier(0.4, 0, 0.2, 1);
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    -webkit-tap-highlight-color: transparent;
    -webkit-user-select: none;
    user-select: none;
    -webkit-touch-callout: none;
}

body {
    background: var(--bg);
    background-image:
        radial-gradient(ellipse at 50% 20%, rgba(0, 229, 255, 0.06) 0%, transparent 60%),
        radial-gradient(ellipse at 50% 80%, rgba(255, 64, 129, 0.04) 0%, transparent 60%),
        radial-gradient(ellipse at 20% 50%, rgba(76, 255, 76, 0.03) 0%, transparent 50%);
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 100vh;
    min-height: 100dvh;
    padding: 16px;
    font-family: var(--font-sans);
    overflow: hidden;
    touch-action: manipulation;
}

.bg-particles {
    position: fixed;
    inset: 0;
    pointer-events: none;
    z-index: 0;
    opacity: 0.3;
}
.bg-particle {
    position: absolute;
    border-radius: 50%;
    animation: floatUp linear infinite;
    opacity: 0;
}
@keyframes floatUp {
    0% { transform: translateY(0) translateX(0) scale(0); opacity: 0; }
    10% { opacity: 1; }
    80% { opacity: 0.5; }
    100% { transform: translateY(-110vh) translateX(40px) scale(1.2); opacity: 0; }
}

.game-wrapper {
    position: relative;
    z-index: 1;
    width: 100%;
    max-width: 520px;
    height: 100dvh; /* قفل کردن ارتفاع به صورت داینامیک */
    max-height: 100dvh;
    display: flex;
    flex-direction: column;
    justify-content: space-evenly; /* پخش خودکار فضا بین المان‌ها */
    gap: 8px;
}

.game-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    flex-wrap: wrap;
}
.game-title {
    font-size: 2rem;
    font-weight: 900;
    letter-spacing: -0.02em;
    background: linear-gradient(135deg, var(--cyan) 0%, var(--green) 50%, var(--pink) 100%);
    -webkit-background-clip: text;
    background-clip: text;
    -webkit-text-fill-color: transparent;
    filter: drop-shadow(0 0 18px rgba(0, 229, 255, 0.4));
    animation: titleShimmer 4s ease-in-out infinite;
}
@keyframes titleShimmer {
    0%, 100% { filter: drop-shadow(0 0 18px rgba(0, 229, 255, 0.4)); }
    50% { filter: drop-shadow(0 0 28px rgba(255, 64, 129, 0.5)); }
}
.title-emoji {
    font-size: 2.2rem;
    animation: bounce 1.5s ease-in-out infinite;
    display: inline-block;
    -webkit-text-fill-color: initial;
}
@keyframes bounce {
    0%, 100% { transform: translateY(0); }
    30% { transform: translateY(-12px); }
    50% { transform: translateY(0); }
    70% { transform: translateY(-6px); }
}

.stats-bar {
    display: flex;
    gap: 10px;
    flex-wrap: wrap;
}
.stat-card {
    flex: 1;
    min-width: 80px;
    background: var(--card-bg);
    border: 1px solid var(--card-border);
    border-radius: var(--radius-md);
    padding: 12px 14px;
    text-align: center;
    transition: var(--transition-smooth);
    position: relative;
    overflow: hidden;
}
.stat-card::before {
    content: '';
    position: absolute;
    inset: 0;
    border-radius: var(--radius-md);
    opacity: 0;
    transition: opacity 0.3s;
    pointer-events: none;
}
.stat-card.score-card::before { background: radial-gradient(circle at center, rgba(0, 229, 255, 0.08) 0%, transparent 70%); }
.stat-card.level-card::before { background: radial-gradient(circle at center, rgba(255, 64, 129, 0.08) 0%, transparent 70%); }
.stat-card.best-card::before { background: radial-gradient(circle at center, rgba(255, 215, 64, 0.08) 0%, transparent 70%); }
.stat-card.pulse-anim::before {
    opacity: 1;
    animation: statPulse 0.5s ease-out;
}
@keyframes statPulse {
    0% { opacity: 1; transform: scale(1.3); }
    100% { opacity: 0; transform: scale(1); }
}
.stat-label {
    font-size: 0.7rem;
    text-transform: uppercase;
    letter-spacing: 0.12em;
    color: var(--text-secondary);
    margin-bottom: 4px;
    font-weight: 600;
}
.stat-value {
    font-size: 1.5rem;
    font-weight: 800;
    color: var(--text);
    font-family: var(--font-mono);
    transition: var(--transition-fast);
    letter-spacing: -0.02em;
}
.stat-value.highlight {
    color: var(--gold);
    animation: valuePop 0.4s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}
@keyframes valuePop {
    0% { transform: scale(1); }
    40% { transform: scale(1.35); color: var(--cyan); }
    100% { transform: scale(1); }
}

.speed-control-panel {
    display: flex;
    align-items: center;
    justify-content: space-between;
    background: var(--card-bg);
    border: 1px solid var(--card-border);
    border-radius: var(--radius-md);
    padding: 10px 16px;
}
.speed-label {
    font-size: 0.8rem;
    color: var(--text-secondary);
    font-weight: 600;
}
.speed-btns {
    display: flex;
    align-items: center;
    gap: 12px;
}
.speed-btn {
    background: #1a1a28;
    border: 1px solid #2a2a3a;
    color: var(--cyan);
    padding: 6px 14px;
    border-radius: var(--radius-sm);
    cursor: pointer;
    font-size: 0.75rem;
    font-weight: 600;
    font-family: var(--font-sans);
    transition: var(--transition-fast);
}
.speed-btn:hover {
    background: #222235;
    border-color: var(--cyan);
}
.speed-btn:active {
    transform: scale(0.92);
}
.speed-value {
    font-size: 0.85rem;
    font-weight: 700;
    color: var(--gold);
    min-width: 65px;
    text-align: center;
}

.game-container {
    position: relative;
    background: var(--card-bg);
    border: 1px solid var(--card-border);
    border-radius: var(--radius-lg);
    padding: 12px;
    box-shadow: var(--shadow-card), var(--shadow-glow);
    transition: var(--transition-smooth);
    display: flex;
    justify-content: center;
    align-items: center;
}
.game-container.game-over-shake {
    animation: shake 0.5s cubic-bezier(0.36, 0.07, 0.19, 0.97);
}
@keyframes shake {
    0%, 100% { transform: translateX(0); }
    10%, 50%, 90% { transform: translateX(-6px); }
    30%, 70% { transform: translateX(6px); }
}
.game-inner {
    position: relative;
    width: 100%;
    aspect-ratio: 1 / 1;
    max-width: 440px;
    border-radius: var(--radius-sm);
    overflow: hidden;
    box-shadow: inset 0 0 30px rgba(0, 0, 0, 0.4);
    background: #0d0d15;
}
canvas {
    display: block;
    width: 100%;
    height: 100%;
    border-radius: var(--radius-sm);
}

.game-overlay {
    position: absolute;
    inset: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    pointer-events: none;
    z-index: 10;
    transition: var(--transition-smooth);
    border-radius: var(--radius-lg);
}
.game-overlay.active { pointer-events: all; }
.overlay-content {
    background: rgba(10, 10, 20, 0.92);
    backdrop-filter: blur(16px);
    -webkit-backdrop-filter: blur(16px);
    border: 1px solid rgba(255, 255, 255, 0.12);
    border-radius: var(--radius-lg);
    padding: 28px 32px;
    text-align: center;
    transform: scale(0.85);
    opacity: 0;
    transition: var(--transition-smooth);
    max-width: 85%;
}
.game-overlay.active .overlay-content {
    transform: scale(1);
    opacity: 1;
}
.overlay-title {
    font-size: 1.8rem;
    font-weight: 900;
    margin-bottom: 8px;
    letter-spacing: -0.02em;
}
.overlay-title.game-over-title { color: var(--red); text-shadow: 0 0 30px rgba(255, 23, 68, 0.5); }
.overlay-title.pause-title { color: var(--cyan); text-shadow: 0 0 30px rgba(0, 229, 255, 0.5); }
.overlay-title.win-title { color: var(--gold); text-shadow: 0 0 30px rgba(255, 215, 64, 0.5); }
.overlay-subtitle {
    color: var(--text-secondary);
    font-size: 0.95rem;
    margin-bottom: 6px;
    line-height: 1.5;
}
.overlay-score {
    font-size: 2.6rem;
    font-weight: 900;
    color: var(--text);
    font-family: var(--font-mono);
    margin: 8px 0;
}
.overlay-btn {
    display: inline-block;
    margin-top: 14px;
    padding: 12px 28px;
    border-radius: 50px;
    border: none;
    cursor: pointer;
    font-weight: 700;
    font-size: 0.95rem;
    letter-spacing: 0.04em;
    font-family: var(--font-sans);
    transition: var(--transition-fast);
    position: relative;
    overflow: hidden;
    color: #fff;
}
.btn-restart {
    background: linear-gradient(135deg, var(--pink), #e91e63);
    box-shadow: 0 6px 24px rgba(255, 64, 129, 0.35);
}
.btn-restart:hover { box-shadow: 0 8px 32px rgba(255, 64, 129, 0.55); transform: translateY(-2px); }
.btn-restart:active { transform: scale(0.95); transition: 0.1s; }
.btn-resume {
    background: linear-gradient(135deg, var(--cyan), #0097a7);
    box-shadow: 0 6px 24px rgba(0, 229, 255, 0.35);
}
.btn-resume:hover { box-shadow: 0 8px 32px rgba(0, 229, 255, 0.55); transform: translateY(-2px); }

.key-hints {
    display: flex;
    gap: 16px;
    justify-content: center;
    flex-wrap: wrap;
    font-size: 0.75rem;
    color: var(--text-secondary);
    letter-spacing: 0.04em;
}
.key-hint {
    display: flex;
    align-items: center;
    gap: 6px;
    background: var(--card-bg);
    border: 1px solid var(--card-border);
    border-radius: 20px;
    padding: 6px 12px;
    font-weight: 500;
}
.key-icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 26px;
    height: 26px;
    background: #1a1a28;
    border-radius: 6px;
    font-weight: 700;
    font-size: 0.7rem;
    color: var(--cyan);
    border: 1px solid #2a2a3a;
    font-family: var(--font-mono);
}

.dpad-container {
    display: none;
    justify-content: center;
    padding: 4px 0;
    touch-action: none; /* جلوگیری کامل از رفتار پیش‌فرض اسکرول یا زوم مرورگر */
}
/* تنظیمات چیدمان اصلی دکمه‌ها */
.dpad {
    display: grid;
    /* ۳ ستون با عرض مساوی */
    grid-template-columns: 55px 55px 55px; 
    /* ۲ ردیف با ارتفاع مساوی */
    grid-template-rows: 55px 55px; 
    gap: 8px;
    justify-content: center;
    touch-action: none;
}

/* این کلاس بسیار مهم است تا فضای خالی کنار دکمه بالا حفظ شود */
.dpad-empty {
    width: 100%;
    height: 100%;
    visibility: hidden; /* مخفی می‌ماند اما جای خودش را در جدول اشغال می‌کند */
    pointer-events: none;
}
.dpad-btn {
    width: 70px;
    height: 70px;
    border-radius: 18px;
    border: 1px solid rgba(255, 255, 255, 0.15);
    background: rgba(20, 20, 30, 0.85);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.5rem;
    transition: var(--transition-fast);
    color: #ccc;
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    touch-action: none; /* پاسخ‌دهی آنی و ۱۰۰٪ دکمه در تاچ موبایل بدون لگ */
}
.dpad-btn:active {
    background: rgba(0, 229, 255, 0.25);
    border-color: var(--cyan);
    transform: scale(0.9);
    color: #fff;
    box-shadow: 0 0 20px rgba(0, 229, 255, 0.3);
}
.dpad-empty { pointer-events: none; background: transparent; border: none; }

@media (max-width: 768px) {
    body { padding: 5px; }
    .game-wrapper { padding: 5px 0; justify-content: space-between; height: 98dvh; }
    
    /* فشرده‌سازی المان‌های بالایی برای باز کردن فضا */
    .game-header { margin-bottom: 0; }
    .game-title { font-size: 1.3rem; }
    
    .stats-bar { gap: 5px; }
    .stat-card { padding: 6px 8px; }
    .stat-label { font-size: 0.6rem; margin-bottom: 2px; }
    .stat-value { font-size: 1.1rem; }
    
    .speed-control-panel { padding: 6px 10px; }
    .speed-label, .speed-value { font-size: 0.75rem; }
    .speed-btn { padding: 4px 10px; font-size: 0.7rem; }

    .game-container { padding: 6px; }
    
    /* کلید طلایی: بوم بازی نباید بیشتر از 40 درصد ارتفاع صفحه را بگیرد */
    .game-inner { max-width: min(100%, 40dvh); margin: 0 auto; }
    
    /* تنظیمات دی‌پد برای جا شدن در پایین صفحه */
    .dpad-container { display: flex; margin-bottom: 8px; touch-action: none; }
    .dpad { 
        grid-template-columns: 55px 55px 55px; 
        grid-template-rows: 55px 55px; 
        gap: 6px; 
        touch-action: none; 
    }
    .dpad-btn { width: 55px; height: 55px; font-size: 1.2rem; border-radius: 12px; }
    
    /* تنظیمات مدال روی صفحه (Game Over) */
    .key-hints { display: none; }
    .overlay-content { padding: 15px 20px; }
    .overlay-title { font-size: 1.2rem; margin-bottom: 4px; }
    .overlay-score { font-size: 1.5rem; margin: 4px 0; }
    .overlay-btn { padding: 8px 20px; font-size: 0.85rem; margin-top: 8px; }
}

@media (max-width: 380px) {
    /* فشرده‌سازی نهایی برای گوشی‌های بسیار کوچک */
    .dpad { grid-template-columns: 48px 48px 48px; grid-template-rows: 48px 48px; gap: 4px; }
    .dpad-btn { width: 48px; height: 48px; font-size: 1.1rem; border-radius: 10px; }
    .stat-card { padding: 4px 6px; }
    .stat-value { font-size: 1rem; }
    .game-title { font-size: 1.2rem; }
}