:root {
    --primary: #06b6d4;
    --secondary: #10b981;
    --bg: #020617;
    --dark: #083344;
    
    --bg-base: var(--bg);
    --bg-panel: var(--dark);
    --text-primary: #f8fafc;
    --text-secondary: var(--primary);
    --accent-glow: var(--primary);
    --accent-secondary: var(--secondary);
    --accent-success: var(--secondary);
    --font-ui: 'Inter', sans-serif;
    --font-game: 'Press Start 2P', cursive;
}

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

body {
    background-color: var(--bg-base);
    color: var(--text-primary);
    font-family: var(--font-ui);
    min-height: 100vh;
    overflow: hidden;
    display: flex;
    justify-content: center;
    align-items: center;
    background: radial-gradient(circle at center, var(--dark) 0%, var(--bg) 100%);
}

#app {
    width: 100vw;
    height: 100vh;
    position: relative;
    display: flex;
    flex-direction: column;
}

.screen {
    display: none;
    width: 100%;
    height: 100%;
    position: absolute;
    top: 0;
    left: 0;
    flex-direction: column;
    align-items: center;
}

.screen.active {
    display: flex;
    animation: fadeIn 0.4s ease-out forwards;
}

@keyframes fadeIn {
    from { opacity: 0; transform: scale(0.98); }
    to { opacity: 1; transform: scale(1); }
}

header {
    text-align: center;
    margin: 40px 0;
}

header h1 {
    font-size: 3rem;
    font-weight: 800;
    background: linear-gradient(135deg, var(--primary), var(--secondary));
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    margin-bottom: 10px;
    letter-spacing: -1px;
}

header p {
    color: var(--text-secondary);
    font-size: 1.1rem;
    letter-spacing: 2px;
    text-transform: uppercase;
}

.grid-container {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
    gap: 25px;
    width: 90%;
    max-width: 1200px;
    padding: 20px;
    overflow-y: auto;
    padding-bottom: 60px;
    flex: 1;
    min-height: 0;
    align-content: start;
}

/* --- Mobile: make the main-menu scrollable and cards fully visible --- */
#main-menu {
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
}

#shop-screen {
    -webkit-overflow-scrolling: touch;
}

/* Scrollbar styling for grid */
.grid-container::-webkit-scrollbar {
    width: 8px;
}
.grid-container::-webkit-scrollbar-thumb {
    background: var(--bg-panel);
    border-radius: 4px;
}

.game-card {
    background: var(--bg-panel);
    backdrop-filter: blur(10px);
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: 16px;
    padding: 20px;
    cursor: pointer;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    display: flex;
    flex-direction: column;
    align-items: center;
    text-align: center;
    position: relative;
    overflow: hidden;
    min-height: 160px;
}

.game-card::before {
    content: '';
    position: absolute;
    top: 0; left: 0; width: 100%; height: 100%;
    background: linear-gradient(45deg, transparent, rgba(6, 182, 212, 0.1), transparent);
    transform: translateX(-100%);
    transition: transform 0.6s;
}

.game-card:hover {
    transform: translateY(-5px);
    border-color: var(--border-glow);
    box-shadow: 0 10px 25px rgba(6, 182, 212, 0.15);
}

.game-card:hover::before {
    transform: translateX(100%);
}

.game-icon-canvas {
    width: 64px;
    height: 64px;
    margin-bottom: 15px;
    filter: drop-shadow(0 0 10px rgba(6, 182, 212, 0.5));
}

.game-card h3 {
    font-size: 1.2rem;
    font-weight: 600;
    margin-bottom: 5px;
}

.game-card p {
    font-size: 0.85rem;
    color: var(--text-secondary);
}

/* Game Overlay */
#game-ui-bar {
    width: 100%;
    padding: 15px 30px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    background: rgba(15, 23, 42, 0.8);
    backdrop-filter: blur(10px);
    border-bottom: 1px solid rgba(255, 255, 255, 0.1);
    z-index: 10;
}

#game-title {
    font-weight: 600;
    font-size: 1.2rem;
    color: var(--accent-glow);
    text-transform: uppercase;
    letter-spacing: 2px;
}

#game-score {
    font-family: var(--font-game);
    font-size: 1rem;
}

#game-container {
    flex: 1;
    min-height: 0;
    display: flex;
    justify-content: center;
    align-items: center;
    width: 100%;
    position: relative;
    padding: 20px;
}

canvas {
    background: #000;
    box-shadow: 0 0 40px rgba(0,0,0,0.5), 0 0 20px var(--border-glow);
    border-radius: 8px;
    max-width: 100%;
    max-height: 100%;
    object-fit: contain;
}

/* Buttons */
.btn {
    background: rgba(255,255,255,0.1);
    border: 1px solid rgba(255,255,255,0.2);
    color: var(--text-primary);
    padding: 10px 20px;
    border-radius: 8px;
    font-family: var(--font-ui);
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s;
    outline: none;
}

.btn:hover {
    background: rgba(255,255,255,0.2);
}

.btn-primary {
    background: var(--accent-glow);
    color: #000;
    border: none;
    box-shadow: 0 0 15px var(--border-glow);
}

.btn-primary:hover {
    background: #22d3ee;
    box-shadow: 0 0 25px var(--border-glow);
    transform: scale(1.05);
}

/* Game Over Overlay */
#game-over-overlay {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background: rgba(15, 23, 42, 0.95);
    padding: 40px;
    border-radius: 20px;
    border: 1px solid var(--accent-secondary);
    box-shadow: 0 0 50px rgba(244, 63, 94, 0.3);
    text-align: center;
    backdrop-filter: blur(15px);
    z-index: 100;
    flex-direction: column;
    gap: 20px;
}

#game-over-overlay.hidden {
    display: none !important;
}

#game-over-overlay h2 {
    font-size: 2.5rem;
    color: var(--accent-secondary);
    margin-bottom: 10px;
}

#game-over-overlay p {
    font-family: var(--font-game);
    font-size: 1.2rem;
    margin-bottom: 30px;
}

/* Utility Animations */
.shake {
    animation: shake 0.4s cubic-bezier(.36,.07,.19,.97) both;
}

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

/* ======================================
   TOUCH CONTROLS (mobile game buttons)
   ====================================== */
#touch-controls {
    display: none;
}

#touch-controls.active {
    display: flex;
    width: 100%;
    padding: 8px 10px;
    justify-content: space-between;
    align-items: center;
    flex-shrink: 0;
    gap: 10px;
    background: rgba(15, 23, 42, 0.9);
    border-top: 1px solid rgba(255, 255, 255, 0.1);
    z-index: 20;
}

/* D-Pad container */
.touch-dpad {
    display: grid;
    grid-template-columns: 50px 50px 50px;
    grid-template-rows: 50px 50px 50px;
    gap: 4px;
    flex-shrink: 0;
}

.touch-dpad .dpad-up    { grid-column: 2; grid-row: 1; }
.touch-dpad .dpad-left  { grid-column: 1; grid-row: 2; }
.touch-dpad .dpad-center { grid-column: 2; grid-row: 2; pointer-events: none; }
.touch-dpad .dpad-right { grid-column: 3; grid-row: 2; }
.touch-dpad .dpad-down  { grid-column: 2; grid-row: 3; }

/* Left/Right only bar */
.touch-lr {
    display: flex;
    gap: 8px;
    flex-shrink: 0;
}

/* Action buttons container */
.touch-actions {
    display: flex;
    gap: 8px;
    flex-shrink: 0;
    flex-wrap: wrap;
    justify-content: flex-end;
}

/* Menu buttons container */
.touch-menu-btns {
    display: flex;
    gap: 10px;
    justify-content: center;
    width: 100%;
    flex-wrap: wrap;
}

/* Individual touch button */
.touch-btn {
    width: 50px;
    height: 50px;
    border-radius: 12px;
    background: rgba(255, 255, 255, 0.12);
    border: 1px solid rgba(255, 255, 255, 0.2);
    color: var(--text-primary);
    font-size: 1.4rem;
    font-weight: 700;
    display: flex;
    justify-content: center;
    align-items: center;
    cursor: pointer;
    user-select: none;
    -webkit-user-select: none;
    touch-action: manipulation;
    transition: background 0.1s, transform 0.1s;
}

.touch-btn:active, .touch-btn.pressed {
    background: var(--accent-glow);
    color: #000;
    transform: scale(0.92);
}

.touch-btn-wide {
    width: auto;
    min-width: 70px;
    padding: 0 14px;
    font-size: 0.85rem;
    height: 44px;
}

.touch-btn-action {
    width: 56px;
    height: 56px;
    border-radius: 50%;
    font-size: 0.75rem;
    font-weight: 700;
    background: rgba(6, 182, 212, 0.2);
    border: 2px solid var(--accent-glow);
}

.touch-btn-action:active, .touch-btn-action.pressed {
    background: var(--accent-glow);
    color: #000;
}

/* Menu mode buttons (Pong 1/2, Connect4 1/2/3) */
.touch-btn-menu {
    width: auto;
    min-width: 100px;
    height: 50px;
    padding: 0 20px;
    font-size: 0.8rem;
    border-radius: 10px;
    text-align: center;
    line-height: 1.2;
}

/* ======================================
   MOBILE RESPONSIVE (≤ 600px)
   ====================================== */
@media (max-width: 600px) {
    /* Let the body scroll so nothing gets cut off */
    body {
        overflow: auto;
    }

    /* Header: smaller text, less margin */
    header {
        margin: 20px 0 12px;
    }

    header h1 {
        font-size: 1.8rem;
    }

    header p {
        font-size: 0.8rem;
        letter-spacing: 1px;
    }

    /* Grid: 2 columns that always fit on mobile */
    .grid-container {
        grid-template-columns: repeat(2, 1fr);
        gap: 12px;
        width: 95%;
        padding: 10px;
        padding-bottom: 40px;
        overflow-y: visible;
        flex-shrink: 0;
    }

    /* Cards: tighter padding, fully visible icon + name */
    .game-card {
        padding: 14px 10px;
        border-radius: 12px;
        min-height: 120px;
    }

    .game-card h3 {
        font-size: 0.95rem;
        word-break: break-word;
        overflow-wrap: break-word;
    }

    .game-card p {
        font-size: 0.75rem;
    }

    .game-icon-canvas {
        width: 48px;
        height: 48px;
        margin-bottom: 10px;
    }

    /* Game UI bar: stack vertically so nothing overlaps */
    #game-ui-bar {
        flex-wrap: wrap;
        gap: 8px;
        padding: 10px 12px;
        justify-content: center;
    }

    #game-title {
        font-size: 0.9rem;
        letter-spacing: 1px;
    }

    #game-score {
        font-size: 0.75rem;
    }

    /* Game-over overlay: narrower on mobile */
    #game-over-overlay {
        padding: 24px 16px;
        width: 85%;
        max-width: 340px;
    }

    #game-over-overlay h2 {
        font-size: 1.6rem;
    }

    #game-over-overlay p {
        font-size: 0.9rem;
    }

    .btn {
        padding: 8px 14px;
        font-size: 0.85rem;
    }
}

/* ======================================
   VERY SMALL SCREENS (≤ 380px)
   ====================================== */
@media (max-width: 380px) {
    header h1 {
        font-size: 1.5rem;
    }

    .grid-container {
        grid-template-columns: repeat(2, 1fr);
        gap: 8px;
    }

    .game-card {
        padding: 10px 8px;
    }

    .game-card h3 {
        font-size: 0.85rem;
    }

    .game-icon-canvas {
        width: 40px;
        height: 40px;
        margin-bottom: 8px;
    }
}
