:root {
    --bg-color: #050510;
    --grid-bg: #0a0a1a;
    --grid-line: #1a1a2e;
    --neon-blue: #00f3ff;
    --neon-pink: #ff00ff;
    --neon-green: #00ff9d;
    --neon-yellow: #ffea00;
    --text-color: #e0e6ed;
    --panel-bg: rgba(20, 25, 40, 0.9);
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

body {
    background-color: var(--bg-color);
    color: var(--text-color);
    font-family: 'Roboto', sans-serif;
    height: 100vh;
    overflow: hidden;
    display: flex;
    justify-content: center;
    align-items: center;
    /* Use fixed positioning on body to absolutely prevent scrolling on mobile */
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
}

.game-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    width: 100%;
    max-width: 1200px;
    height: 100%;
    /* Fill body completely */
}

.game-header {
    width: 100%;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 20px;
    margin-bottom: 20px;
}

.back-btn {
    color: var(--neon-blue);
    text-decoration: none;
    font-family: 'Orbitron', sans-serif;
    font-size: 1.2rem;
    transition: all 0.3s ease;
}

.back-btn:hover {
    color: #fff;
    text-shadow: 0 0 10px var(--neon-blue);
}

.neon-text {
    font-family: 'Orbitron', sans-serif;
    color: var(--neon-blue);
    text-shadow: 0 0 10px rgba(0, 243, 255, 0.7), 0 0 20px rgba(0, 243, 255, 0.5);
    letter-spacing: 3px;
    font-size: 2.5rem;
}

.main-content {
    display: flex;
    justify-content: center;
    align-items: flex-start;
    gap: 20px;
    width: 100%;
    height: 100%;
}

/* PANELS */
.side-panel {
    display: flex;
    flex-direction: column;
    gap: 20px;
    width: 200px;
}

.panel-box {
    background: var(--panel-bg);
    border: 2px solid var(--neon-blue);
    border-radius: 10px;
    padding: 15px;
    text-align: center;
    box-shadow: 0 0 15px rgba(0, 243, 255, 0.2);
    display: flex;
    flex-direction: column;
    align-items: center;
}

.panel-box h2 {
    font-family: 'Orbitron', sans-serif;
    color: var(--neon-pink);
    margin-bottom: 10px;
    font-size: 1.2rem;
    text-shadow: 0 0 5px var(--neon-pink);
}

/* .panel-box canvas { background: rgba(0,0,0,0.3); } */

.stats-box .stat-item {
    margin: 15px 0;
    display: flex;
    flex-direction: column;
}

.stat-item .label {
    font-size: 0.9rem;
    color: #aaa;
    margin-bottom: 5px;
}

.stat-item .value {
    font-family: 'Orbitron', sans-serif;
    font-size: 1.8rem;
    color: var(--neon-yellow);
    text-shadow: 0 0 5px var(--neon-yellow);
}

.controls-info {
    background: rgba(0, 0, 0, 0.5);
    padding: 15px;
    border-radius: 8px;
    border: 1px solid #333;
    font-size: 0.85rem;
}

.controls-info h3 {
    color: var(--neon-green);
    margin-bottom: 10px;
    text-align: center;
    font-family: 'Orbitron', sans-serif;
}

.controls-info ul {
    list-style: none;
}

.controls-info li {
    margin-bottom: 8px;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.key {
    display: inline-block;
    background: #333;
    padding: 2px 6px;
    border-radius: 4px;
    border: 1px solid #555;
    font-family: monospace;
    color: #fff;
    font-weight: bold;
}

/* GAME BOARD */
.game-board-container {
    position: relative;
    border: 4px solid var(--neon-blue);
    border-radius: 4px;
    box-shadow: 0 0 30px rgba(0, 243, 255, 0.3);
    background: var(--grid-bg);
}

canvas#game-canvas {
    display: block;
    background-image:
        linear-gradient(var(--grid-line) 1px, transparent 1px),
        linear-gradient(90deg, var(--grid-line) 1px, transparent 1px);
    background-size: 30px 30px;
    /* Assuming 30px block size */
    background-position: -1px -1px;
}


/* OVERLAYS */
.overlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.85);
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    z-index: 10;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.3s;
    text-align: center;
}

.overlay.active {
    opacity: 1;
    pointer-events: all;
}

.overlay h2 {
    font-size: 3rem;
    margin-bottom: 20px;
}

.overlay p {
    font-size: 1.2rem;
    margin-bottom: 10px;
    color: #ccc;
}

.red-glow {
    color: #ff3333 !important;
    text-shadow: 0 0 20px #ff0000 !important;
}

.cyber-btn {
    margin-top: 30px;
    padding: 15px 40px;
    background: transparent;
    border: 2px solid var(--neon-green);
    color: var(--neon-green);
    font-family: 'Orbitron', sans-serif;
    font-size: 1.5rem;
    cursor: pointer;
    text-transform: uppercase;
    transition: all 0.2s;
    box-shadow: 0 0 10px rgba(0, 255, 157, 0.2);
}

.cyber-btn:hover {
    background: var(--neon-green);
    color: #000;
    box-shadow: 0 0 30px rgba(0, 255, 157, 0.6);
}

/* Responsive */
@media (max-width: 800px) {
    body {
        /* Prevent annoying pull-to-refresh on mobile */
        overscroll-behavior-y: none;
        position: fixed;
        width: 100vw;
        height: 100dvh;
        /* Force exact device visible height without address bar */
        margin: 0;
        padding: 0;
    }

    .game-container {
        padding: 0;
        margin: 0;
        justify-content: flex-start;
        height: 100%;
        /* Inherit fixed body height */
        width: 100%;
        overflow: hidden;
    }

    .game-header {
        margin: 0;
        /* Remove top margin */
        padding: 5px 10px;
        flex: 0 0 auto;
        height: 40px;
        /* Fixed height for header */
        display: flex;
        align-items: center;
    }

    .back-btn {
        font-size: 1rem;
    }

    .neon-text {
        font-size: 1.2rem;
        /* Shrink title slightly to save space */
    }

    .main-content {
        display: grid;
        grid-template-columns: 70px 1fr 70px;
        /* Slightly narrower side panels */
        grid-template-rows: 60px 1fr;
        /* Fixed height for top panels row! */
        grid-template-areas:
            "hold stats next"
            "board board board";
        gap: 0;
        /* Absolutely NO gap between top row and bottom */
        width: 100%;
        height: calc(100% - 40px);
        /* Fill remaining space exactly minus header */
        padding: 0 2px 5px 2px;
        /* Tiny side padding, 5px bottom breathing room */
    }

    .side-panel {
        display: contents;
    }

    /* Map boxes to grid areas */
    .hold-box {
        grid-area: hold;
    }

    .stats-box {
        grid-area: stats;
    }

    .next-box {
        grid-area: next;
    }

    .game-board-container {
        grid-area: board;
    }

    .panel-box {
        width: 100%;
        padding: 2px;
        margin: 0;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        height: 100%;
        border-width: 1px;
        /* Thinner borders to save space */
        border-bottom: none;
        /* Blend into the game board visually if needed */
    }

    .panel-box h2 {
        font-size: 0.55rem;
        margin-bottom: 0px;
    }

    .stats-box {
        flex-direction: row;
        justify-content: space-evenly;
        gap: 2px;
    }

    .stats-box .stat-item {
        margin: 0;
        align-items: center;
    }

    .stat-item .label {
        font-size: 0.5rem;
        margin-bottom: 0px;
    }

    .stat-item .value {
        font-size: 0.9rem;
    }

    canvas#hold-canvas,
    canvas#next-canvas {
        width: 100%;
        max-width: 40px;
        /* Slightly smaller */
        height: auto;
        aspect-ratio: 1 / 1;
        object-fit: contain;
    }

    .controls-info {
        display: none;
    }

    .game-board-container {
        display: flex;
        justify-content: center;
        align-items: flex-start;
        /* Align top inside the container instead of center */
        width: 100%;
        margin: 0;
        padding: 0;
        border: 2px solid var(--neon-blue);
        height: 100%;
        /* Strictly fill its grid area */
        overflow: hidden;
        min-height: 0;
        /* Critical for grid item overflow prevention */
        min-width: 0;
    }

    canvas#game-canvas {
        max-height: 100%;
        max-width: 100%;
        height: 100%;
        width: auto;
        object-fit: contain;
        display: block;
    }

    .cyber-btn {
        margin-top: 5px;
        padding: 5px 15px;
        font-size: 1rem;
    }
}

@media (pointer: coarse) {
    .controls-info {
        display: none !important;
    }
}