body {
    margin: 0;
    padding: 0;
    background: #222;
    font-family: monospace;
    color: white;
    min-height: 100vh;
    overflow-x: hidden;
}

main {
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 10px;
    position: relative;
    max-width: 100vw;
    min-height: 100vh;
    box-sizing: border-box;
}

/* --- Wrapper for canvas and overlay --- */
.canvas-container {
    position: relative; /* Essential for positioning the overlay message and level selector */
    width: 85vw;
    max-width: 320px;
    height: auto;
    aspect-ratio: 4 / 6; 
    border: 2px solid #444; /* Added border to the container itself for clarity */
    box-sizing: border-box; /* Ensures border is included in width/height */
}

canvas {
    /* Removed individual border here as it's now on the container */
    background: #333;
    image-rendering: pixelated;
    image-rendering: -moz-crisp-edges;
    image-rendering: crisp-edges;
    width: 100%;
    height: 100%;
}

/* --- Overlay styles for the start message --- */
.overlay-message {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background: rgba(34, 34, 34, 0.85); /* Semi-transparent background */
    padding: 20px;
    border: 2px solid #555;
    border-radius: 6px;
    text-align: center;
    max-width: 80%;
    box-sizing: border-box;
    z-index: 5; /* Ensure it's below the level selector but above canvas */
}

.overlay-message p {
    margin: 0 0 15px 0;
    font-size: 16px;
}

.overlay-message .control-btn {
    font-size: 16px;
    padding: 12px 20px;
}
/* --------------------------------------------- */


.header {
    width: 100%;
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 8px;
    padding: 0 5px;
}

.header h1 {
    font-size: 18px;
    margin: 0;
}

.ui-stats {
    display: flex;
    gap: 15px;
    font-size: 14px;
    align-items: center;
}

.footer {
    width: 100%;
    text-align: center;
    position: relative;
    min-height: 60px;
}

.ui-pixels {
    font-size: 14px;
    margin-top: 8px;
}

.start-message {
    text-align: center;
    margin-top: 10px;
}

.start-message p {
    margin: 0 0 8px 0;
    font-size: 14px;
}

/* --- Mobile controls --- */
.mobile-controls {
    max-width: 400px; 
    width: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    margin-top: 10px;
    gap: 8px;
    padding: 0 5px;
    box-sizing: border-box;
}

@media (max-width: 400px) {
    .mobile-controls {
        justify-content: space-between;
    }
}
/* ------------------------------------------------------------------- */

.control-group {
    display: flex;
    align-items: center;
    gap: 8px;
}

/* --- UPDATED: Reduced size for use in Level/World Selector --- */
.control-btn {
    background: #333;
    border: 2px solid #555;
    color: white;
    font-family: monospace;
    font-size: 12px; /* REDUCED from 14px */
    font-weight: bold;
    padding: 6px 6px; /* REDUCED from 14px 12px */
    border-radius: 6px;
    user-select: none;
    cursor: pointer;
    min-width: 45px;
    text-align: center;
    transition: all 0.1s;
    box-sizing: border-box;
    min-width: unset; /* Allows buttons in grid to shrink */
    height: 100%; /* Ensures proper grid alignment */
}

/* Keep these larger for the main mobile controls */
.control-btn#leftBtn, .control-btn#rightBtn {
    min-width: 65px;
    padding: 14px 18px;
}
/* Overwrite the font-size/padding for mobile action controls */
.mobile-controls .control-btn#leftBtn,
.mobile-controls .control-btn#rightBtn,
.mobile-controls .control-btn.shoot-btn,
.mobile-controls .control-btn.jump-btn {
    font-size: 14px; 
    padding: 16px 12px; 
}


.control-btn:active, .control-btn.pressed {
    background: #555;
    border-color: #777;
    transform: scale(0.95);
}

.jump-btn {
    background: #004400;
    border-color: #006600;
    min-width: 75px;
    padding: 14px 20px;
}

.jump-btn:active, .jump-btn.pressed {
    background: #006600;
    border-color: #008800;
}

.shoot-btn {
    background: #440000;
    border-color: #660000;
    min-width: 75px;
    padding: 14px 20px;
}

.shoot-btn:active, .shoot-btn.pressed {
    background: #660000;
    border-color: #880000;
}

/* --- Level Selector UI (UPDATED FOR CANVAS OVERLAY) --- */
.level-selector {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%; 
    height: 100%;
    background: rgba(34, 34, 34, 0.98);
    padding: 10px; /* REDUCED from 20px */
    border: 4px solid #ffaa00;
    border-radius: 8px;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    box-sizing: border-box;
    text-align: center;
    max-width: none;
    min-width: unset;
    max-height: 100%;
    overflow-y: hidden; /* CHANGED: Prevents scrolling */
    z-index: 10;
    display: none;
}

.level-selector.active {
    display: flex;
}

.level-selector {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%; 
    height: 100%;
    background: rgba(34, 34, 34, 0.98);
    padding: 8px;
    border: 4px solid #ffaa00;
    border-radius: 8px;
    display: flex;
    flex-direction: column;
    justify-content: flex-start;
    align-items: center;
    box-sizing: border-box;
    text-align: center;
    max-width: none;
    min-width: unset;
    max-height: 100%;
    overflow-y: auto;
    z-index: 10;
    display: none;
}

.level-selector.active {
    display: flex;
}

.level-selector h2 {
    margin: 0 0 6px 0;
    font-size: 14px;
    color: #ffaa00;
    border-bottom: 2px solid #555;
    padding-bottom: 4px;
    width: 100%;
}

.level-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 3px;
    width: 100%;
    margin-bottom: 6px;
}

.level-btn {
    background: #444;
    border-color: #666;
    padding: 6px 4px;
    min-width: unset;
    aspect-ratio: 1;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
}

.level-btn:hover {
    background: #555;
    border-color: #777;
}

.level-btn:active {
    transform: scale(0.95);
}

.level-btn.completed {
    background: #004400;
    border-color: #006600;
    color: #00ff00;
}

.level-btn.completed:hover {
    background: #005500;
    border-color: #007700;
}

.level-btn.locked {
    background: #222;
    border-color: #333;
    color: #666;
    cursor: not-allowed;
}

.level-btn.locked:hover {
    background: #222;
    border-color: #333;
    transform: none;
}

.level-number {
    font-size: 11px;
    font-weight: bold;
    line-height: 1;
}

.level-name {
    font-size: 8px;
    margin-top: 2px;
    opacity: 0.7;
    line-height: 1.1;
    max-width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.back-btn {
    background: #440000;
    border-color: #660000;
    margin-top: 6px;
    font-size: 12px;
    padding: 8px 12px;
    width: 100%;
    max-width: none;
}

.back-btn:hover {
    background: #550000;
    border-color: #770000;
}

/* --- Menu Controls (SELECT Button) - Always visible now --- */
.menu-controls-header {
    margin-right: auto;
    margin-left: 10px;
}


/* --- World Selector UI --- */
.world-selector-header {
    margin-bottom: 8px;
    border-bottom: 2px solid #333;
    padding-bottom: 4px;
    background: rgba(34, 34, 34, 0.95);
    width: 100%;
    box-sizing: border-box;
    padding-top: 0;
}

.world-selector-header h2 {
    margin: 0 0 4px 0;
    font-size: 14px;
    color: #ffaa00;
}

.world-selector {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 4px;
    max-width: 100%;
    margin-bottom: 8px;
}

.world-btn {
    flex-grow: 1;
    background: #3a3a3a;
    border-color: #555;
    transition: all 0.2s ease-in-out;
    padding: 8px 6px;
    font-size: 11px;
}

.world-btn.active {
    transform: scale(1.05);
    box-shadow: 0 0 10px var(--world-color, #ffaa00);
    border-color: var(--world-color, #ffaa00);
    color: var(--world-color, #ffaa00);
}

.level-btn.current {
    background: #003366;
    border-color: #0055aa;
    color: #00aaff;
}

.level-btn.current:hover {
    background: #004488;
    border-color: #0066cc;
}