/* --- NEW: Improved Base Styling --- */
html {
    height: 100%;
}

body {
    font-family: 'Press Start 2P', cursive;
    /* Use pixel font as primary */
    background-color: #000;
    /* Darker space background */
    color: #ffffff;
    overflow: hidden;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    /* NEW: Prevent pull-to-refresh/overscroll */
    overscroll-behavior-y: contain;
}

/* NEW: Animated star canvas */
#star-canvas {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: -10;
    /* Sit behind everything */
}

/* --- UPDATED: Canvas & Game Container --- */
canvas {
    background-color: #000000;
    display: block;
    /* MODIFIED: Connects to UI bar */
    border-radius: 0 0 8px 8px;
    box-shadow: 0 0 20px rgba(162, 76, 255, 0.6);
    border: 2px solid rgba(162, 76, 255, 0.8);
    /* MODIFIED: No top border */
    border-top: none;
    cursor: crosshair;
    /* NEW: Crispy pixels */
    image-rendering: pixelated;
    image-rendering: -moz-crisp-edges;
    image-rendering: crisp-edges;
    /* NEW: Ensure canvas scales correctly */
    max-width: 100%;
    object-fit: contain;
}

#game-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    /* MODIFIED: Relative for modal */
    position: relative;
    flex-shrink: 0;
    /* Prevent shrinking on mobile */
}

/* --- UPDATED: Main UI Bar --- */
#ui-container {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 10px 20px;
    background-color: rgba(30, 35, 55, 0.7);
    /* MODIFIED: Connects to canvas */
    border-radius: 8px 8px 0 0;
    margin-bottom: 0;
    border: 2px solid rgba(162, 76, 255, 0.8);
    border-bottom: none;
    position: relative;
}

/* MODIFIED: Edit Button position */
#edit-level-btn {
    position: absolute;
    right: 10px;
    top: 50%;
    transform: translateY(-50%);
    padding: 5px 10px;
    font-size: 0.7rem;
    text-transform: none;
    box-shadow: 0 3px 0 rgb(113, 53, 178);
}

#edit-level-btn:hover {
    box-shadow: 0 5px 0 rgb(113, 53, 178);
    /* MODIFIED: Adjust transform for Y-offset */
    transform: translateY(calc(-50% - 2px));
}

#edit-level-btn:active {
    transform: translateY(calc(-50% + 1px));
}

.ui-element {
    margin: 0 10px;
    font-size: 1rem;
    font-family: 'Press Start 2P', cursive;
    text-shadow: 2px 2px #000;
    color: #fff;
}

/* --- UPDATED: Modal Styling --- */
#modal {
    position: absolute;
    /* MODIFIED: Position over game */
    background-color: rgba(11, 16, 32, 0.9);
    border: 2px solid rgb(162, 76, 255);
    border-radius: 10px;
    padding: 25px;
    overflow-y: auto;
    /* Prevent cut-off on small screens */
    text-align: center;
    box-shadow: 0 0 30px rgb(162, 76, 255);
    display: none;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    z-index: 100;
    /* NEW: Glassmorphism effect */
    backdrop-filter: blur(5px);
    -webkit-backdrop-filter: blur(5px);
}

#modal h2 {
    font-family: 'Press Start 2P', cursive;
    font-size: 2rem;
    margin-bottom: 20px;
    color: rgb(162, 76, 255);
    text-transform: uppercase;
    /* NEW: Text shadow */
    text-shadow: 3px 3px 0px #000;
}

#modal p {
    font-size: 0.9rem;
    margin-bottom: 25px;
    line-height: 1.5;
    max-width: 90%;
}

#main-menu-container {
    display: flex;
    flex-direction: column;
    gap: 12px;
    width: 80%;
    margin-top: 15px;
}

/* MODIFIED: Level select containers */
#level-select-container,
#custom-level-select-container {
    display: flex;
    gap: 15px;
    justify-content: center;
    flex-wrap: wrap;
    max-height: 250px;
    overflow-y: auto;
    padding: 10px;
    width: 100%;
    /* NEW: Custom scrollbar */
    scrollbar-width: thin;
    scrollbar-color: rgb(162, 76, 255) #0B1020;
}

/* NEW: Webkit scrollbar */
#level-select-container::-webkit-scrollbar,
#custom-level-select-container::-webkit-scrollbar {
    width: 8px;
}

#level-select-container::-webkit-scrollbar-track,
#custom-level-select-container::-webkit-scrollbar-track {
    background: #0B1020;
    border-radius: 4px;
}

#level-select-container::-webkit-scrollbar-thumb,
#custom-level-select-container::-webkit-scrollbar-thumb {
    background-color: rgb(162, 76, 255);
    border-radius: 4px;
    border: 2px solid #0B1020;
}


#custom-level-select-container {
    max-height: 150px;
    border-top: 2px solid rgb(162, 76, 255);
    margin-top: 15px;
    padding-top: 20px;
}

.custom-level-button-wrapper {
    position: relative;
}

/* MODIFIED: Delete button */
.delete-level-btn {
    position: absolute;
    top: -8px;
    right: -8px;
    width: 24px;
    height: 24px;
    background-color: #FF4136;
    color: white;
    border: 2px solid white;
    border-radius: 50%;
    cursor: pointer;
    font-size: 14px;
    line-height: 20px;
    text-align: center;
    font-family: 'Press Start 2P', cursive;
    /* Use game font */
    z-index: 10;
    box-shadow: 0 2px 0 #b30000;
}

.delete-level-btn:hover {
    background-color: #ff6b6b;
}

.delete-level-btn:active {
    transform: translateY(1px);
    box-shadow: 0 1px 0 #b30000;
}

/* MODIFIED: Level button */
.level-button {
    position: relative;
    width: 75px;
    height: 75px;
    font-size: 1.2rem;
    padding: 8px 4px 20px 4px;
    line-height: 1.2;
    word-break: break-word;
    overflow-wrap: break-word;
}

.custom-level-button {
    font-size: 0.7rem;
    line-height: 1.2;
    padding-top: 10px;
    word-break: break-all;
}

/* --- UPDATED: Button Styling --- */
.button {
    font-family: 'Press Start 2P', cursive;
    background-color: rgb(162, 76, 255);
    color: #ffffff;
    padding: 12px 20px;
    border: none;
    border-radius: 5px;
    cursor: pointer;
    font-size: 1rem;
    text-transform: uppercase;
    transition: all 0.1s ease-in-out;
    /* Faster transition */
    box-shadow: 0 5px 0 rgb(113, 53, 178);
    text-shadow: 2px 2px #000;
    /* Add text shadow */
}

.button:hover {
    background-color: rgb(182, 96, 255);
    transform: translateY(-2px);
    box-shadow: 0 7px 0 rgb(113, 53, 178);
}

.button:active {
    transform: translateY(2px);
    box-shadow: 0 3px 0 rgb(113, 53, 178);
}

/* NEW: Secondary button style (e.g., home) */
.button.secondary {
    background-color: #777;
    box-shadow: 0 5px 0 #444;
}

.button.secondary:hover {
    background-color: #888;
    box-shadow: 0 7px 0 #444;
}

.button.secondary:active {
    box-shadow: 0 3px 0 #444;
}

/* --- UPDATED: Star Containers --- */
.stars-container {
    margin-top: -15px;
    margin-bottom: 15px;
    font-size: 2.5rem;
    color: rgb(255, 200, 0);
    height: 40px;
    text-shadow: 2px 2px #000;
    /* Add shadow */
}

.level-button .stars-container {
    font-size: 0.9rem;
    position: absolute;
    bottom: 4px;
    left: 50%;
    transform: translateX(-50%);
    width: 100%;
    margin: 0;
    height: auto;
    text-shadow: 1px 1px #000;
    /* Lighter shadow */
}

/* --- UPDATED: Editor Panels --- */
#editor-toolbar,
#editor-io {
    display: none;
    flex-direction: column;
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    background-color: rgba(11, 16, 32, 0.9);
    padding: 15px;
    border-radius: 10px;
    border: 2px solid rgb(162, 76, 255);
    gap: 10px;
    z-index: 50;
    /* Below modal */
    backdrop-filter: blur(5px);
    -webkit-backdrop-filter: blur(5px);
    box-shadow: 0 0 20px rgba(162, 76, 255, 0.5);
}

#editor-toolbar {
    left: 20px;
}

#editor-io {
    right: 20px;
    width: 250px;
}

#editor-io h3 {
    font-size: 1rem;
    text-align: center;
    text-transform: uppercase;
    color: rgb(162, 76, 255);
    margin-bottom: 5px;
}

.editor-btn {
    width: 75px;
    height: 65px;
    font-family: 'Press Start 2P', cursive;
    border: 2px solid #fff;
    cursor: pointer;
    padding: 5px;
    text-align: center;
    line-height: 1.1;
    border-radius: 5px;
    /* Add radius */
    transition: all 0.1s ease;
}

.editor-btn.selected {
    border-color: rgb(162, 76, 255);
    box-shadow: 0 0 10px rgb(162, 76, 255);
    transform: scale(1.05);
    /* Add scale */
}

.editor-btn:hover {
    border-color: rgb(182, 96, 255);
}

/* MODIFIED: Style child divs for text */
.editor-btn div:first-child {
    font-size: 1.2rem;
    line-height: 1.1;
    color: #fff;
    text-shadow: 1px 1px #000;
}

.editor-btn div:last-child {
    font-size: 0.5rem;
    line-height: 1;
    text-transform: uppercase;
    margin-top: 4px;
    color: #fff;
    text-shadow: 1px 1px #000;
}


.io-btn {
    padding: 10px;
    font-size: 0.8rem;
}

/* MODIFIED: Editor IO inputs */
#editor-io label {
    font-size: 0.8rem;
    color: #aaa;
    margin-top: 5px;
    margin-bottom: 3px;
}

#theme-select,
#level-name-input,
#level-data-textarea {
    width: 100%;
    background-color: #0B1020;
    color: white;
    border: 1px solid rgb(162, 76, 255);
    border-radius: 5px;
    padding: 8px;
    font-size: 0.8rem;
    font-family: 'Press Start 2P', cursive;
}

#level-data-textarea {
    height: 120px;
    resize: none;
}

#level-name-input {
    margin-bottom: 10px;
}

#theme-select {
    margin-bottom: 10px;
}

/* --- Online Level Browser --- */
#online-level-browser {
    width: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 12px;
    max-height: 320px;
    overflow-y: auto;
    padding: 10px;
    scrollbar-width: thin;
    scrollbar-color: rgb(162, 76, 255) #0B1020;
}

#online-level-browser::-webkit-scrollbar {
    width: 8px;
}

#online-level-browser::-webkit-scrollbar-track {
    background: #0B1020;
    border-radius: 4px;
}

#online-level-browser::-webkit-scrollbar-thumb {
    background-color: rgb(162, 76, 255);
    border-radius: 4px;
    border: 2px solid #0B1020;
}

.online-sort-controls {
    display: flex;
    gap: 8px;
    flex-shrink: 0;
}

.sort-btn {
    font-family: 'Press Start 2P', cursive;
    font-size: 0.55rem;
    padding: 6px 12px;
    background-color: rgba(162, 76, 255, 0.2);
    color: #aaa;
    border: 1px solid rgba(162, 76, 255, 0.4);
    border-radius: 20px;
    cursor: pointer;
    transition: all 0.15s ease;
    text-transform: uppercase;
}

.sort-btn:hover {
    background-color: rgba(162, 76, 255, 0.4);
    color: #fff;
}

.sort-btn.active {
    background-color: rgb(162, 76, 255);
    color: #fff;
    border-color: rgb(162, 76, 255);
    box-shadow: 0 0 10px rgba(162, 76, 255, 0.5);
}

#online-level-cards {
    display: flex;
    flex-direction: column;
    gap: 10px;
    width: 100%;
}

.online-level-card {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    background-color: rgba(30, 35, 55, 0.7);
    border: 1px solid rgba(162, 76, 255, 0.3);
    border-radius: 8px;
    padding: 10px 14px;
    backdrop-filter: blur(5px);
    -webkit-backdrop-filter: blur(5px);
    transition: border-color 0.2s ease, box-shadow 0.2s ease;
}

.online-level-card:hover {
    border-color: rgba(162, 76, 255, 0.7);
    box-shadow: 0 0 12px rgba(162, 76, 255, 0.3);
}

.online-level-info {
    display: flex;
    flex-direction: column;
    gap: 4px;
    flex: 1;
    min-width: 0;
}

.online-level-name {
    font-size: 0.75rem;
    font-family: 'Press Start 2P', cursive;
    color: #fff;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    text-shadow: 1px 1px #000;
}

.online-level-meta {
    display: flex;
    gap: 10px;
    font-size: 0.5rem;
    color: #aaa;
    font-family: 'Press Start 2P', cursive;
}

.online-level-author {
    color: rgb(182, 96, 255);
}

.online-level-actions {
    display: flex;
    gap: 8px;
    align-items: center;
    flex-shrink: 0;
}

.online-play-btn {
    font-family: 'Press Start 2P', cursive;
    font-size: 0.6rem;
    padding: 8px 14px;
    background-color: rgb(1, 255, 112);
    color: #000;
    border: none;
    border-radius: 5px;
    cursor: pointer;
    box-shadow: 0 3px 0 rgb(0, 150, 65);
    transition: all 0.1s ease;
    text-transform: uppercase;
    text-shadow: none;
}

.online-play-btn:hover {
    background-color: rgb(50, 255, 140);
    transform: translateY(-1px);
    box-shadow: 0 4px 0 rgb(0, 150, 65);
}

.online-play-btn:active {
    transform: translateY(1px);
    box-shadow: 0 2px 0 rgb(0, 150, 65);
}

.like-btn {
    font-family: Arial, sans-serif;
    font-size: 1rem;
    padding: 6px 10px;
    background-color: transparent;
    color: #aaa;
    border: 1px solid rgba(255, 255, 255, 0.15);
    border-radius: 5px;
    cursor: pointer;
    transition: all 0.15s ease;
    display: flex;
    align-items: center;
    gap: 4px;
}

.like-btn .like-count {
    font-family: 'Press Start 2P', cursive;
    font-size: 0.5rem;
}

.like-btn:hover {
    border-color: rgba(255, 80, 80, 0.5);
    color: #ff5050;
}

.like-btn.liked {
    color: #ff5050;
    border-color: rgba(255, 80, 80, 0.5);
    background-color: rgba(255, 80, 80, 0.1);
}

/* Upload Button Highlight */
.upload-btn {
    background-color: rgb(1, 200, 112) !important;
    color: #000 !important;
    box-shadow: 0 5px 0 rgb(0, 130, 70) !important;
    text-shadow: none !important;
}

.upload-btn:hover {
    background-color: rgb(50, 220, 140) !important;
    box-shadow: 0 7px 0 rgb(0, 130, 70) !important;
}

.upload-btn:active {
    box-shadow: 0 3px 0 rgb(0, 130, 70) !important;
}

/* Loading Spinner */
.online-loading {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 10px;
    font-size: 0.7rem;
    color: #aaa;
    font-family: 'Press Start 2P', cursive;
    padding: 20px;
}

.loading-spinner {
    width: 28px;
    height: 28px;
    border: 3px solid rgba(162, 76, 255, 0.2);
    border-top-color: rgb(162, 76, 255);
    border-radius: 50%;
    animation: spinner-rotate 0.8s linear infinite;
}

@keyframes spinner-rotate {
    to {
        transform: rotate(360deg);
    }
}

/* Delete button for own uploaded levels */
.online-delete-btn {
    font-family: 'Press Start 2P', cursive;
    font-size: 0.5rem;
    padding: 5px 8px;
    background-color: rgba(255, 65, 54, 0.2);
    color: #FF4136;
    border: 1px solid rgba(255, 65, 54, 0.4);
    border-radius: 5px;
    cursor: pointer;
    transition: all 0.15s ease;
}

.online-delete-btn:hover {
    background-color: rgba(255, 65, 54, 0.4);
    color: #fff;
}

/* --- NEW: Mobile Controls Styling --- */
#mobile-controls {
    display: none;
    /* Hidden by default */
    width: 100%;
    padding: 10px 20px;
    /* MODIFIED: Reduced top/bottom padding */
    box-sizing: border-box;
    background-color: rgba(0, 0, 0, 0.2);
    z-index: 200;
    flex-shrink: 0;
    /* Don't shrink */
}

.mobile-btn-container {
    display: flex;
    justify-content: space-between;
    width: 100%;
    align-items: center;
}

.mobile-btn {
    width: 60px;
    /* MODIFIED: Smaller button */
    height: 60px;
    /* MODIFIED: Smaller button */
    border-radius: 50%;
    background-color: rgba(162, 76, 255, 0.6);
    color: white;
    border: 2px solid rgba(162, 76, 255, 0.9);
    font-size: 2rem;
    /* MODIFIED: Smaller icon */
    line-height: 60px;
    /* MODIFIED: Center icon vertically */
    text-align: center;
    font-family: Arial, sans-serif;
    /* Pixel font is bad for arrows */
    user-select: none;
    -webkit-user-select: none;
    /* Safari */
    -webkit-tap-highlight-color: transparent;
    /* Remove tap flash */
    box-shadow: 0 4px 0 rgb(113, 53, 178);
}

.mobile-btn:active {
    background-color: rgb(182, 96, 255);
    transform: translateY(2px);
    box-shadow: 0 2px 0 rgb(113, 53, 178);
}

/* Group left/right buttons */
.mobile-dpad {
    display: flex;
    gap: 15px;
    /* MODIFIED: Reduced gap */
}

/* --- NEW: Mobile Media Query --- */
@media (max-width: 768px) {
    body {
        flex-direction: column;
        overflow-y: hidden;
        /* Prevent pull-to-refresh */
        height: 100dvh;
        /* MODIFIED: Use dynamic viewport height */
    }

    #game-container {
        flex-grow: 1;
        /* Take remaining space */
        width: 100%;
        max-width: 100vw;
        height: calc(100% - 90px);
        /* MODIFIED: Subtract new control bar height */
        overflow: hidden;
        /* Contain canvas */
        padding-bottom: 0;
        margin-bottom: 0;
        position: relative;
    }

    canvas#gameCanvas {
        width: 100%;
        height: 100%;
        /* Fill the container */
        object-fit: contain;
        /* Maintain aspect ratio */
        border-radius: 0;
        /* Full width */
        border: none;
        box-shadow: none;
    }

    #ui-container {
        width: 100% !important;
        /* Override inline style */
        border-radius: 0;
        border-left: none;
        border-right: none;
    }

    #modal {
        width: 95vw !important;
        height: 80vh !important;
        font-size: 0.8rem;
    }

    #modal h2 {
        font-size: 1.5rem;
    }

    .button {
        padding: 12px 18px;
        font-size: 0.8rem;
    }

    .level-button {
        width: 65px;
        height: 65px;
        font-size: 1rem;
    }

    /* Show mobile controls */
    #mobile-controls {
        display: block;
        /* Show the container */
        height: 90px;
        /* MODIFIED: Fixed height to match content */
        flex-shrink: 0;
    }

    /* Hide desktop-only editor */
    #editor-toolbar,
    #editor-io,
    #edit-level-btn {
        display: none !important;
    }

    /* Hide star canvas on mobile, it's distracting */
    #star-canvas {
        display: none;
    }
}