/* 🎨 Modernized CSS with CSS variables for better maintainability */
/* This improves themeability while maintaining file:// compatibility */

*,
*::before,
*::after {
    box-sizing: border-box;
}

* {
    font-family: Arial, Helvetica, sans-serif !important;
    font-style: normal !important;
    font-weight: normal !important;
}

:root {
    --snake-color: #4caf50;
    --food-color: #ff5722;
    --background-color: #222;
    --text-color: #eee;
    --border-color: #333;
    --powerup-color: #ffc107;
    --mushroom-color: #9c27b0;
    --wall-color: #ff9800;
    --custom-color-1: #4caf50;
    --custom-color-2: #222;
    --custom-color-3: #333;
    --custom-color-4: #45a049;
    --custom-color-5: #000;
    --custom-color-6: #ff0000;
    --custom-color-7: #eee;
}

html,
body {
    min-height: 100vh;
    height: 100dvh;
    overflow: hidden;
    overscroll-behavior: none;
}

body {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    background-color: var(--custom-color-3);
    color: var(--custom-color-7);
    position: relative; /* Needed for absolute positioning of overlay */
    touch-action: none; /* Prevent touch scrolling on body */
}

/* On wide screens, add side margins for comfortable viewing */
@media (min-width: 1024px) {
    body {
        max-width: 1024px;
        margin-left: auto;
        margin-right: auto;
        padding: 20px 40px;
    }
}

/* On small screens, adjust layout to fit */
@media (max-width: 768px) {
    h1 {
        font-size: 1.1em;
        margin: 0 0 5px 0;
        text-align: center;
    }

    #score,
    #level,
    #password {
        font-size: 0.85em;
        margin: 1px 0 0 0;
        line-height: 1.1;
    }
}

h1 {
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
    margin: 0 0 15px 0;
    font-size: 2em; /* Larger title for better visibility */
}

canvas {
    display: block;
    background-color: var(--custom-color-5);
    border: 2px solid var(--custom-color-7);
    box-sizing: content-box; /* Match original: 600px content + border = 604px total */
    width: 100%;
    max-width: 600px;
    aspect-ratio: 1 / 1;
    height: auto;
    touch-action: none; /* Prevent touch scrolling/zooming on canvas */
}

#minimapCanvas {
    position: absolute;
    top: 20px;
    right: 20px;
    width: 100px;
    height: 100px;
    opacity: 0.7;
    border: 1px solid var(--custom-color-7);
    background-color: var(--custom-color-2);
    z-index: 100;
}

/* Make minimap smaller on mobile */
@media (max-width: 768px) {
    #minimapCanvas {
        width: 60px;
        height: 60px;
        top: 10px;
        right: 10px;
    }
}

#score,
#level,
#password {
    margin-top: 10px;
    font-size: 1.2em;
    min-height: 1.2em; /* Ensure single line height */
    text-align: center;
}

#password {
    color: #aaa; /* Slightly dimmer for password info */
    min-height: 2.4em; /* Ensure two lines height for the new layout */
    line-height: 1.2em;
}

/* Password Input Field */
#passwordInputContainer {
    margin-top: 5px;
    width: 100%;
    display: flex;
    justify-content: center;
}

#passwordInput {
    width: 90%;
    max-width: 400px;
    padding: 8px 12px;
    font-size: 1em;
    font-family: Arial, Helvetica, sans-serif;
    background-color: var(--custom-color-5);
    color: var(--custom-color-7);
    border: 1px solid var(--custom-color-7);
    border-radius: 5px;
    outline: none;
    text-align: center;
    box-sizing: border-box;
}

#passwordInput::placeholder {
    color: #888;
}

#passwordInput:focus {
    border-color: var(--snake-color);
}

/* Game Over Overlay Styles */
#gameOverOverlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.8); /* Semi-transparent black background */
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000; /* Ensure it's on top */
}

#gameOverOverlay.hidden {
    display: none;
}

.gameOverContent {
    background-color: var(--custom-color-2);
    padding: 30px;
    border-radius: 10px;
    text-align: center;
    border: 2px solid var(--custom-color-7);
}

.gameOverContent h2 {
    color: var(--custom-color-6);
    margin-bottom: 15px;
    font-size: 2em;
}

.gameOverContent p {
    font-size: 1.5em;
    margin-bottom: 20px;
}

.gameOverContent button {
    background-color: var(--custom-color-1); /* Green */
    color: white;
    padding: 10px 20px;
    border: none;
    border-radius: 5px;
    cursor: pointer;
    font-size: 1em;
    transition: background-color 0.3s ease;
}

.gameOverContent button:hover {
    background-color: var(--custom-color-4);
}

/* Mobile Controls */
.mobile-pause {
    position: fixed;
    top: 20px;
    left: 20px;
    font-size: 24px;
    padding: 10px;
    background: rgba(0, 0, 0, 0.5);
    border-radius: 5px;
    cursor: pointer;
    z-index: 1000;
    display: none;
}

#mobileControls {
    position: fixed;
    bottom: 20px;
    left: 50%;
    transform: translateX(-50%);
    display: grid;
    grid-template-columns: 1fr auto;
    gap: 20px;
    z-index: 1000;
    background: rgba(0, 0, 0, 0.7);
    padding: 15px;
    border-radius: 10px;
}

.d-pad {
    display: grid;
    grid-template-rows: auto auto auto;
    gap: 5px;
    justify-items: center;
}

.arrow {
    width: 50px;
    height: 50px;
    font-size: 20px;
    background: var(--custom-color-5);
    color: var(--custom-color-7);
    border: 2px solid var(--custom-color-7);
    border-radius: 5px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
}

.arrow:active {
    background: var(--custom-color-4);
    transform: scale(0.95);
}

.middle-row {
    display: grid;
    grid-template-columns: 50px 50px;
    gap: 5px;
}

.mobile-buttons {
    display: flex;
    align-items: center;
}

.control-btn {
    padding: 15px 20px;
    font-size: 16px;
    background: var(--custom-color-1);
    color: white;
    border: none;
    border-radius: 5px;
    cursor: pointer;
    white-space: nowrap;
    touch-action: manipulation;
}

.control-btn:active {
    background: var(--custom-color-4);
    transform: scale(0.95);
}

#mobileKeyboard {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    background: rgba(0, 0, 0, 0.9);
    padding: 10px;
    z-index: 1001;
    max-height: 40vh;
    overflow-y: auto;
    display: none;
}

.keyboard-grid {
    display: grid;
    grid-template-columns: repeat(10, 1fr);
    gap: 4px;
    max-width: 100%;
}

.key {
    padding: 10px 2px;
    font-size: 12px;
    background: var(--custom-color-5);
    color: var(--custom-color-7);
    border: 1px solid var(--custom-color-7);
    border-radius: 3px;
    cursor: pointer;
    touch-action: manipulation;
    min-height: 35px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.key:active {
    background: var(--custom-color-4);
    transform: scale(0.95);
}

.close-keyboard-btn {
    margin-top: 10px;
    width: 100%;
    padding: 12px;
    font-size: 16px;
    background: var(--custom-color-1);
    color: white;
    border: none;
    border-radius: 5px;
    cursor: pointer;
    touch-action: manipulation;
}

.close-keyboard-btn:active {
    background: var(--custom-color-4);
    transform: scale(0.95);
}

/* Show mobile controls on touch devices */
@media (max-width: 768px) {
    .mobile-pause,
    #mobileControls,
    #mobileKeyboard {
        display: flex !important;
    }

    #mobileKeyboard {
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
    }
}
