/* ===== Reset & Base ===== */
*, *::before, *::after {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    -webkit-tap-highlight-color: transparent;
}

:root {
    --bg-primary: #030310;
    --bg-secondary: #080820;
    --neon-green: #39ff14;
    --neon-blue: #00f0ff;
    --neon-pink: #ff00ff;
    --neon-yellow: #ffff00;
    --neon-orange: #ff6600;
    --text-primary: #ffffff;
    --text-secondary: #8888bb;
    --glass-bg: rgba(8, 8, 32, 0.85);
    --glass-border: rgba(100, 100, 200, 0.15);
    --safe-top: env(safe-area-inset-top, 0px);
    --safe-bottom: env(safe-area-inset-bottom, 0px);
    --safe-left: env(safe-area-inset-left, 0px);
    --safe-right: env(safe-area-inset-right, 0px);
}

html, body {
    width: 100%;
    height: 100%;
    overflow: hidden;
    background: var(--bg-primary);
    font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
    color: var(--text-primary);
    touch-action: none;
    user-select: none;
    -webkit-user-select: none;
    position: fixed;
    top: 0;
    left: 0;
}

/* ===== Game Canvas ===== */
#gameCanvas {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 1;
    display: none;
}

/* ===== UI Overlay ===== */
#uiOverlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 10;
    padding-top: var(--safe-top);
    padding-bottom: var(--safe-bottom);
    padding-left: var(--safe-left);
    padding-right: var(--safe-right);
}

/* ===== Screens ===== */
.screen {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    display: none;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    animation: fadeIn 0.3s ease;
}

.screen.active {
    display: flex;
}

.screen.hidden {
    display: none !important;
}

@keyframes fadeIn {
    from { opacity: 0; transform: translateY(10px); }
    to { opacity: 1; transform: translateY(0); }
}

/* ===== Main Menu ===== */
#mainMenu {
    background: radial-gradient(ellipse at center, #0d0d2b 0%, #030310 70%);
}

.menu-content {
    text-align: center;
    padding: 20px;
}

.game-title {
    font-size: clamp(3rem, 12vw, 6rem);
    font-weight: 900;
    line-height: 1;
    margin-bottom: 10px;
    text-shadow: 0 0 20px rgba(57, 255, 20, 0.5);
}

.title-snake {
    display: block;
    color: var(--neon-green);
    text-shadow: 0 0 10px var(--neon-green), 0 0 20px var(--neon-green), 0 0 40px var(--neon-green);
    animation: glow 2s ease-in-out infinite alternate;
}

.title-game {
    display: block;
    font-size: 0.5em;
    color: var(--neon-blue);
    text-shadow: 0 0 10px var(--neon-blue), 0 0 20px var(--neon-blue);
    letter-spacing: 0.3em;
}

@keyframes glow {
    from { text-shadow: 0 0 10px var(--neon-green), 0 0 20px var(--neon-green); }
    to { text-shadow: 0 0 15px var(--neon-green), 0 0 30px var(--neon-green), 0 0 50px var(--neon-green); }
}

.subtitle {
    color: var(--text-secondary);
    font-size: clamp(0.9rem, 3vw, 1.2rem);
    margin-bottom: 40px;
    letter-spacing: 0.1em;
}

/* ===== Buttons ===== */
.btn {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 10px;
    width: 100%;
    max-width: 280px;
    margin: 8px auto;
    padding: 14px 24px;
    border: 2px solid var(--glass-border);
    border-radius: 12px;
    background: var(--glass-bg);
    color: var(--text-primary);
    font-size: 1rem;
    font-weight: 700;
    letter-spacing: 0.1em;
    cursor: pointer;
    transition: all 0.2s ease;
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
}

.btn:active {
    transform: scale(0.95);
}

.btn-play {
    border-color: var(--neon-green);
    box-shadow: 0 0 15px rgba(57, 255, 20, 0.3), inset 0 0 15px rgba(57, 255, 20, 0.1);
}

.btn-play:active {
    box-shadow: 0 0 25px rgba(57, 255, 20, 0.5), inset 0 0 25px rgba(57, 255, 20, 0.2);
}

.btn-leaderboard {
    border-color: var(--neon-yellow);
    box-shadow: 0 0 15px rgba(255, 255, 0, 0.2), inset 0 0 15px rgba(255, 255, 0, 0.05);
}

.btn-howto {
    border-color: var(--neon-blue);
    box-shadow: 0 0 15px rgba(0, 240, 255, 0.2), inset 0 0 15px rgba(0, 240, 255, 0.05);
}

.btn-icon {
    font-size: 1.2em;
}

.menu-buttons {
    display: flex;
    flex-direction: column;
    align-items: center;
}

/* ===== Screen Header ===== */
.screen-header {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 16px 20px;
    padding-top: calc(16px + var(--safe-top));
    background: var(--glass-bg);
    border-bottom: 1px solid var(--glass-border);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
}

.screen-header h2 {
    font-size: 1.2rem;
    letter-spacing: 0.1em;
}

.btn-back, .btn-share-lb {
    position: absolute;
    top: calc(16px + var(--safe-top));
    background: none;
    border: none;
    color: var(--text-primary);
    font-size: 1.5rem;
    cursor: pointer;
    padding: 8px;
}

.btn-back { left: 12px; }
.btn-share-lb { right: 12px; }

/* ===== Mode Select ===== */
#modeSelect {
    background: var(--bg-primary);
    padding-top: calc(70px + var(--safe-top));
}

.mode-cards {
    display: flex;
    flex-direction: column;
    gap: 16px;
    padding: 20px;
    width: 100%;
    max-width: 500px;
    overflow-y: auto;
    max-height: calc(100vh - 100px);
}

.mode-card {
    background: var(--glass-bg);
    border: 1px solid var(--glass-border);
    border-radius: 16px;
    padding: 20px;
    text-align: center;
    cursor: pointer;
    transition: all 0.2s ease;
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
}

.mode-card:active {
    transform: scale(0.97);
}

.mode-card[data-mode="classic"] {
    border-color: rgba(57, 255, 20, 0.4);
}

.mode-card[data-mode="maze"] {
    border-color: rgba(0, 240, 255, 0.4);
}

.mode-card[data-mode="speed"] {
    border-color: rgba(255, 255, 0, 0.4);
}

.mode-icon {
    font-size: 2.5rem;
    margin-bottom: 8px;
}

.mode-card h3 {
    font-size: 1.1rem;
    letter-spacing: 0.15em;
    margin-bottom: 8px;
}

.mode-card p {
    color: var(--text-secondary);
    font-size: 0.85rem;
    line-height: 1.4;
    margin-bottom: 10px;
}

.mode-badge {
    display: inline-block;
    padding: 4px 12px;
    border-radius: 20px;
    font-size: 0.7rem;
    font-weight: 600;
    letter-spacing: 0.05em;
    background: rgba(255, 255, 255, 0.1);
    color: var(--text-secondary);
}

/* ===== How To Play ===== */
#howToPlay {
    background: var(--bg-primary);
    padding-top: calc(70px + var(--safe-top));
}

.howto-content {
    padding: 20px;
    width: 100%;
    max-width: 500px;
}

.howto-item {
    display: flex;
    align-items: flex-start;
    gap: 16px;
    padding: 16px;
    margin-bottom: 12px;
    background: var(--glass-bg);
    border: 1px solid var(--glass-border);
    border-radius: 12px;
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
}

.howto-icon {
    font-size: 2rem;
    flex-shrink: 0;
}

.howto-item h4 {
    font-size: 0.95rem;
    margin-bottom: 4px;
}

.howto-item p {
    color: var(--text-secondary);
    font-size: 0.8rem;
    line-height: 1.4;
}

/* ===== Game HUD ===== */
#gameHUD {
    pointer-events: none;
    justify-content: flex-start;
}

.hud-top {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 12px 16px;
    padding-top: calc(12px + var(--safe-top));
    background: linear-gradient(to bottom, rgba(0,0,0,0.7) 0%, transparent 100%);
    pointer-events: auto;
}

.hud-score, .hud-best {
    display: flex;
    flex-direction: column;
    align-items: center;
}

.hud-label {
    font-size: 0.6rem;
    color: var(--text-secondary);
    letter-spacing: 0.15em;
}

.hud-value {
    font-size: 1.5rem;
    font-weight: 900;
    color: var(--neon-green);
    text-shadow: 0 0 10px var(--neon-green);
}

.hud-best .hud-value {
    color: var(--neon-yellow);
    text-shadow: 0 0 10px var(--neon-yellow);
}

.hud-mode {
    font-size: 0.75rem;
    font-weight: 700;
    letter-spacing: 0.2em;
    color: var(--neon-blue);
    text-shadow: 0 0 8px var(--neon-blue);
    padding: 4px 12px;
    border: 1px solid rgba(0, 240, 255, 0.3);
    border-radius: 20px;
}

.btn-pause {
    position: absolute;
    top: calc(12px + var(--safe-top));
    right: 16px;
    background: none;
    border: none;
    color: var(--text-primary);
    font-size: 1.5rem;
    cursor: pointer;
    pointer-events: auto;
    padding: 8px;
    display: none;
}

.hud-timer {
    position: absolute;
    top: calc(60px + var(--safe-top));
    left: 50%;
    transform: translateX(-50%);
    font-size: 2rem;
    font-weight: 900;
    color: var(--neon-orange);
    text-shadow: 0 0 15px var(--neon-orange);
    pointer-events: none;
}

.hud-timer.hidden { display: none; }

/* ===== Modals ===== */
.modal {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(0, 0, 0, 0.8);
    z-index: 100;
    animation: fadeIn 0.2s ease;
    padding: 20px;
}

.modal.hidden { display: none; }

.modal-content {
    background: var(--bg-secondary);
    border: 1px solid var(--glass-border);
    border-radius: 20px;
    padding: 30px;
    text-align: center;
    width: 100%;
    max-width: 360px;
    max-height: 90vh;
    overflow-y: auto;
}

.modal-content h2 {
    font-size: 1.8rem;
    letter-spacing: 0.15em;
    margin-bottom: 20px;
    color: var(--neon-pink);
    text-shadow: 0 0 15px var(--neon-pink);
}

.new-record {
    font-size: 1.2rem;
    color: var(--neon-yellow);
    text-shadow: 0 0 15px var(--neon-yellow);
    margin-bottom: 15px;
    animation: pulse 0.5s ease infinite alternate;
}

.new-record.hidden { display: none; }

@keyframes pulse {
    from { transform: scale(1); }
    to { transform: scale(1.05); }
}

.final-score {
    margin: 20px 0;
}

.final-label {
    display: block;
    font-size: 0.8rem;
    color: var(--text-secondary);
    letter-spacing: 0.15em;
    margin-bottom: 5px;
}

.final-value {
    font-size: 3rem;
    font-weight: 900;
    color: var(--neon-green);
    text-shadow: 0 0 20px var(--neon-green);
}

.final-best {
    color: var(--text-secondary);
    font-size: 0.9rem;
    margin-bottom: 20px;
}

.modal-buttons {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.btn-retry {
    border-color: var(--neon-green);
    box-shadow: 0 0 15px rgba(57, 255, 20, 0.3);
}

.btn-share-score {
    border-color: var(--neon-blue);
    box-shadow: 0 0 15px rgba(0, 240, 255, 0.3);
}

.btn-resume {
    border-color: var(--neon-green);
    box-shadow: 0 0 15px rgba(57, 255, 20, 0.3);
}

.btn-restart {
    border-color: var(--neon-blue);
    box-shadow: 0 0 15px rgba(0, 240, 255, 0.2);
}

.btn-quit {
    border-color: var(--neon-pink);
    box-shadow: 0 0 15px rgba(255, 0, 255, 0.2);
}

/* ===== Leaderboard ===== */
#leaderboardScreen {
    background: var(--bg-primary);
    padding-top: calc(70px + var(--safe-top));
}

.lb-tabs {
    display: flex;
    width: 100%;
    max-width: 500px;
    padding: 16px 20px 0;
}

.lb-tab {
    flex: 1;
    padding: 10px;
    background: none;
    border: none;
    border-bottom: 2px solid transparent;
    color: var(--text-secondary);
    font-size: 0.8rem;
    font-weight: 700;
    letter-spacing: 0.1em;
    cursor: pointer;
    transition: all 0.2s;
}

.lb-tab.active {
    color: var(--neon-green);
    border-bottom-color: var(--neon-green);
}

.lb-list {
    width: 100%;
    max-width: 500px;
    padding: 16px 20px;
    overflow-y: auto;
    max-height: calc(100vh - 160px);
}

.lb-entry {
    display: flex;
    align-items: center;
    padding: 12px 16px;
    margin-bottom: 8px;
    background: var(--glass-bg);
    border: 1px solid var(--glass-border);
    border-radius: 10px;
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
}

.lb-rank {
    width: 36px;
    height: 36px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
    font-weight: 900;
    font-size: 0.85rem;
    margin-right: 12px;
    flex-shrink: 0;
}

.lb-entry:nth-child(1) .lb-rank {
    background: linear-gradient(135deg, #ffd700, #ff8c00);
    color: #000;
}

.lb-entry:nth-child(2) .lb-rank {
    background: linear-gradient(135deg, #c0c0c0, #808080);
    color: #000;
}

.lb-entry:nth-child(3) .lb-rank {
    background: linear-gradient(135deg, #cd7f32, #8b4513);
    color: #fff;
}

.lb-entry:nth-child(n+4) .lb-rank {
    background: rgba(255, 255, 255, 0.1);
    color: var(--text-secondary);
}

.lb-info {
    flex: 1;
    min-width: 0;
}

.lb-name {
    font-weight: 700;
    font-size: 0.9rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.lb-date {
    font-size: 0.7rem;
    color: var(--text-secondary);
}

.lb-score {
    font-size: 1.2rem;
    font-weight: 900;
    color: var(--neon-green);
    text-shadow: 0 0 8px var(--neon-green);
    margin-left: 8px;
}

.lb-share-btn {
    background: none;
    border: none;
    color: var(--neon-blue);
    font-size: 1.1rem;
    cursor: pointer;
    padding: 8px;
    margin-left: 4px;
}

.lb-empty {
    text-align: center;
    color: var(--text-secondary);
    padding: 40px 20px;
    font-size: 0.9rem;
}

/* ===== Share Modal ===== */
.share-content {
    max-width: 400px;
}

.share-content canvas {
    width: 100%;
    height: auto;
    border-radius: 10px;
    margin-bottom: 16px;
    border: 1px solid var(--glass-border);
}

.share-buttons {
    display: flex;
    justify-content: center;
    gap: 12px;
    margin-bottom: 16px;
    flex-wrap: wrap;
}

.btn-share {
    width: 48px;
    height: 48px;
    border-radius: 50%;
    border: 1px solid var(--glass-border);
    background: var(--glass-bg);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
}

.btn-share:active { transform: scale(0.9); }

.btn-share svg {
    width: 22px;
    height: 22px;
    fill: var(--text-primary);
}

.btn-share.twitter:hover { border-color: #1da1f2; }
.btn-share.facebook:hover { border-color: #4267B2; }
.btn-share.whatsapp:hover { border-color: #25D366; }
.btn-share.telegram:hover { border-color: #0088cc; }
.btn-share.reddit:hover { border-color: #ff4500; }
.btn-share.copy:hover { border-color: var(--neon-green); }

.btn-download-card {
    border-color: var(--neon-green);
    box-shadow: 0 0 10px rgba(57, 255, 20, 0.2);
    margin-bottom: 8px;
}

.btn-close-share {
    border-color: var(--text-secondary);
}

/* ===== Nickname Modal ===== */
.nickname-input {
    width: 100%;
    padding: 14px 16px;
    border: 2px solid var(--glass-border);
    border-radius: 10px;
    background: rgba(255, 255, 255, 0.05);
    color: var(--text-primary);
    font-size: 1rem;
    text-align: center;
    margin-bottom: 16px;
    outline: none;
    transition: border-color 0.2s;
}

.nickname-input:focus {
    border-color: var(--neon-blue);
}

.btn-confirm-nick {
    border-color: var(--neon-green);
    box-shadow: 0 0 15px rgba(57, 255, 20, 0.3);
}

/* ===== Challenge Screen ===== */
.challenge-content {
    text-align: center;
    padding: 30px;
    background: radial-gradient(ellipse at center, #0d0d2b 0%, #030310 70%);
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
}

.challenge-text {
    font-size: clamp(1.1rem, 4vw, 1.5rem);
    margin-bottom: 10px;
    color: var(--text-primary);
}

.challenge-text span {
    color: var(--neon-green);
    font-weight: 900;
}

.challenge-question {
    font-size: clamp(1.5rem, 6vw, 2.5rem);
    font-weight: 900;
    color: var(--neon-yellow);
    text-shadow: 0 0 15px var(--neon-yellow);
    margin-bottom: 40px;
}

/* ===== Toast ===== */
.toast {
    position: fixed;
    bottom: calc(30px + var(--safe-bottom));
    left: 50%;
    transform: translateX(-50%);
    padding: 12px 24px;
    background: var(--glass-bg);
    border: 1px solid var(--neon-green);
    border-radius: 30px;
    color: var(--text-primary);
    font-size: 0.85rem;
    z-index: 200;
    animation: toastIn 0.3s ease;
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    box-shadow: 0 0 15px rgba(57, 255, 20, 0.3);
}

.toast.hidden { display: none; }

@keyframes toastIn {
    from { opacity: 0; transform: translateX(-50%) translateY(20px); }
    to { opacity: 1; transform: translateX(-50%) translateY(0); }
}

/* ===== Responsive ===== */
@media (min-width: 768px) {
    .mode-cards {
        flex-direction: row;
        max-width: 700px;
    }
    
    .mode-card {
        flex: 1;
    }
    
    .modal-content {
        max-width: 420px;
    }
}

@media (max-height: 600px) {
    .game-title {
        font-size: 2.5rem;
    }
    
    .subtitle {
        margin-bottom: 20px;
    }
    
    .btn {
        padding: 10px 20px;
        margin: 5px auto;
    }
}
