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

body {
    font-family: 'Arial', sans-serif;
    background: #1a1a2e;
    color: white;
    display: flex;
    justify-content: center;
    align-items: flex-start;
    min-height: 100vh;
    padding: 5px;
    margin: 0;
    box-sizing: border-box;
    overflow-x: auto;
}

#gameContainer {
    background: #16213e;
    border-radius: 20px;
    padding: 15px;
    box-shadow: 0 10px 30px rgba(0,0,0,0.5);
    width: 100%;
    max-width: 1500px;
    min-height: 100vh;
    box-sizing: border-box;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    margin: 0 auto;
}

#header {
    text-align: center;
    margin-bottom: 8px;
}

h1 {
    color: #00d4ff;
    margin-bottom: 8px;
    font-size: 2.2em;
    text-shadow: 0 0 20px rgba(0,212,255,0.5);
}

#levelInfo {
    display: flex;
    justify-content: space-between;
    margin-bottom: 8px;
    padding: 6px 12px;
    background: #0f3460;
    border-radius: 10px;
    font-size: 0.9em;
}

#gameArea {
    display: flex;
    gap: 15px;
    justify-content: center;
    align-items: flex-start;
    margin-bottom: 5px;
    flex: 0 0 auto;
    min-height: 280px;
    max-height: 50vh;
}

#gameArea.build-level {
    justify-content: center;
    margin-bottom: 5px;
    max-height: 55vh;
    min-height: 300px;
    flex: 1;
}

#canvasContainer {
    position: relative;
    flex: 0 0 auto;
    min-height: 0;
    display: flex;
    justify-content: center;
    align-items: center;
}

#canvasContainer.build-level {
    flex: 1;
}

#gameCanvas {
    border: 3px solid #00d4ff;
    border-radius: 10px;
    display: block;
    background: #0a1929;
    cursor: pointer;
    max-width: 100%;
    max-height: 45vh;
    object-fit: contain;
}

#sidePanel {
    background: #0f3460;
    padding: 15px;
    border-radius: 10px;
    width: 400px;
    flex-shrink: 0;
    max-height: 50vh;
    overflow-y: auto;
    font-size: 0.9em;
}

#sidePanel.build-level {
    max-height: 55vh;
    padding: 12px;
    width: 420px;
    font-size: 0.85em;
}

#sidePanel h3 {
    margin-top: 0;
    margin-bottom: 10px;
    font-size: 1.1em;
}

#sidePanel.build-level h3 {
    margin-bottom: 8px;
    font-size: 1.0em;
}

#controls {
    margin: 5px 0;
    text-align: center;
    z-index: 10;
    position: relative;
}

#controls.build-level {
    margin: 5px 0;
}

button {
    background: #00d4ff;
    color: #0a1929;
    border: none;
    padding: 10px 20px;
    border-radius: 5px;
    font-size: 16px;
    font-weight: bold;
    cursor: pointer;
    margin: 0 5px;
    transition: all 0.3s;
}

button:hover {
    background: #00a8cc;
    transform: translateY(-2px);
    box-shadow: 0 5px 15px rgba(0,212,255,0.3);
}

button:disabled {
    background: #555;
    cursor: not-allowed;
    transform: none;
}

.flow-indicator {
    display: inline-block;
    width: 20px;
    height: 20px;
    border-radius: 50%;
    margin: 0 5px;
    vertical-align: middle;
}

.flow-on {
    background: #00ff00;
    box-shadow: 0 0 10px #00ff00;
}

.flow-off {
    background: #333;
}

#message {
    text-align: center;
    margin: 5px 0;
    font-size: 1.2em;
    min-height: 25px;
    z-index: 10;
    position: relative;
}

#message.build-level {
    margin: 5px 0;
    font-size: 1.1em;
}

.success {
    color: #00ff00;
    text-shadow: 0 0 10px rgba(0,255,0,0.5);
}

.error {
    color: #ff4444;
}

#tutorialOverlay {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0,0,0,0.8);
    display: none;
    justify-content: center;
    align-items: center;
    z-index: 1000;
    border-radius: 10px;
}

#tutorialBox {
    background: #16213e;
    padding: 30px;
    border-radius: 10px;
    border: 2px solid #00d4ff;
    max-width: 500px;
    text-align: center;
}

#tutorialBox h2 {
    color: #00d4ff;
    margin-bottom: 20px;
}

#tutorialBox p {
    margin-bottom: 20px;
    line-height: 1.6;
}

table {
    margin: 10px auto;
    background: #1a1a2e;
    padding: 10px;
    border-radius: 5px;
    border-collapse: collapse;
}

th, td {
    padding: 8px;
    text-align: center;
    border: 1px solid #0f3460;
}

th {
    background: #0f3460;
    color: #00d4ff;
}

#gateDescription {
    margin-top: 20px;
    padding: 15px;
    background: #1a1a2e;
    border-radius: 5px;
    line-height: 1.6;
}

#gateSelector {
    margin-top: 20px;
    padding: 15px;
    background: #0f3460;
    border-radius: 10px;
    display: none;
}

#gateSelector h3 {
    color: #00d4ff;
    margin-bottom: 10px;
    font-size: 1.1em;
}

.gate-option {
    display: inline-block;
    margin: 5px;
    padding: 10px 15px;
    background: #ff8c42;
    border: none;
    border-radius: 5px;
    color: white;
    font-weight: bold;
    cursor: pointer;
    transition: all 0.3s;
}

.gate-option:hover {
    background: #ff6b35;
    transform: translateY(-2px);
}

.gate-option.selected {
    background: #00d4ff;
    color: #0a1929;
}

#buildInfo {
    margin-top: 10px;
    padding: 10px;
    background: #1a1a2e;
    border-radius: 5px;
    font-size: 0.9em;
}

#levelSelect {
    margin: 8px 0;
    text-align: center;
    clear: both;
    z-index: 5;
    position: relative;
}

#levelSelect.build-level {
    margin: 5px 0;
}

#levelSelect h3 {
    color: #00d4ff;
    margin-bottom: 15px;
    font-size: 1.2em;
}

#levelGrid {
    display: grid;
    grid-template-columns: repeat(6, 1fr);
    gap: 8px;
    max-width: 400px;
    margin: 0 auto;
}

.level-button {
    background: linear-gradient(135deg, #ff8c42, #ff6b35);
    color: white;
    border: none;
    border-radius: 8px;
    padding: 12px;
    font-size: 16px;
    font-weight: bold;
    cursor: pointer;
    transition: all 0.3s;
    position: relative;
    min-height: 48px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.level-button:hover {
    transform: translateY(-2px);
    box-shadow: 0 5px 15px rgba(255, 140, 66, 0.4);
}

.level-button.completed {
    background: linear-gradient(135deg, #4caf50, #45a049);
}

.level-button.current {
    background: linear-gradient(135deg, #00d4ff, #0099cc);
    animation: pulse 2s infinite;
}

.level-button.locked {
    background: #555;
    cursor: not-allowed;
    opacity: 0.5;
}

.level-button.locked:hover {
    transform: none;
    box-shadow: none;
}

.level-button.challenge {
    background: linear-gradient(135deg, #ff6b35, #f7931e);
    position: relative;
}

.level-button.challenge:hover {
    transform: translateY(-2px);
    box-shadow: 0 5px 15px rgba(255, 107, 53, 0.4);
}

.level-button.challenge.completed {
    background: linear-gradient(135deg, #ff8c42, #4caf50);
}

.level-button.challenge.current {
    background: linear-gradient(135deg, #ff4444, #ff6b35);
    animation: challengePulse 2s infinite;
}

@keyframes challengePulse {
    0% { box-shadow: 0 0 0 0 rgba(255, 68, 68, 0.7); }
    70% { box-shadow: 0 0 0 10px rgba(255, 68, 68, 0); }
    100% { box-shadow: 0 0 0 0 rgba(255, 68, 68, 0); }
}

.challenge-badge {
    position: absolute;
    top: -6px;
    right: -6px;
    background: #ff4444;
    color: white;
    border-radius: 50%;
    width: 20px;
    height: 20px;
    font-size: 10px;
    font-weight: bold;
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 10;
    border: 2px solid #fff;
    box-shadow: 0 2px 4px rgba(0,0,0,0.3);
}

.beta-badge {
    background: #ff8c42;
    font-size: 8px;
}

.challenge-tooltip {
    position: absolute;
    top: -35px;
    left: 50%;
    transform: translateX(-50%);
    background: #333;
    color: white;
    padding: 4px 8px;
    border-radius: 4px;
    font-size: 10px;
    white-space: nowrap;
    z-index: 20;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.3s;
}

.level-button.challenge:hover .challenge-tooltip {
    opacity: 1;
}

@keyframes pulse {
    0% { box-shadow: 0 0 0 0 rgba(0, 212, 255, 0.7); }
    70% { box-shadow: 0 0 0 10px rgba(0, 212, 255, 0); }
    100% { box-shadow: 0 0 0 0 rgba(0, 212, 255, 0); }
}

.stars {
    position: absolute;
    top: -8px;
    left: 50%;
    transform: translateX(-50%);
    display: flex;
    gap: 2px;
}

.star {
    width: 12px;
    height: 12px;
    background: #ffd700;
    clip-path: polygon(50% 0%, 61% 35%, 98% 35%, 68% 57%, 79% 91%, 50% 70%, 21% 91%, 32% 57%, 2% 35%, 39% 35%);
}

.debug-info {
    background: #444;
    color: #ff0;
    padding: 5px;
    font-size: 12px;
    margin-top: 10px;
    border-radius: 3px;
}

/* Responsive Design - Mobile First Approach */

/* Base styles are mobile-first, then enhanced for larger screens */

/* Extra small devices (phones, 480px and down) */
@media (max-width: 480px) {
    body {
        padding: 2px;
    }
    
    #gameContainer {
        padding: 5px;
        border-radius: 10px;
        min-height: 100vh;
    }
    
    h1 {
        font-size: 1.5em;
        margin-bottom: 3px;
    }
    
    #levelInfo {
        flex-direction: column;
        gap: 5px;
        text-align: center;
        font-size: 0.8em;
        padding: 8px;
    }
    
    #gameArea {
        flex-direction: column;
        gap: 8px;
        min-height: auto;
        max-height: none;
    }
    
    #gameCanvas {
        width: 100%;
        max-width: 100%;
        height: auto;
        max-height: 50vh;
    }
    
    #sidePanel {
        width: 100%;
        max-width: none;
        max-height: 40vh;
        overflow-y: auto;
        font-size: 0.7em;
        padding: 8px;
    }
    
    #levelGrid {
        grid-template-columns: repeat(3, 1fr);
        gap: 5px;
        max-width: 300px;
    }
    
    .level-button {
        padding: 8px 4px;
        font-size: 12px;
        min-height: 40px;
    }
    
    button {
        padding: 8px 12px;
        font-size: 14px;
        margin: 2px;
    }
    
    .gate-option {
        padding: 6px 8px;
        font-size: 0.8em;
        margin: 2px;
    }
}

/* Small devices (tablets, 576px and up) */
@media (min-width: 481px) and (max-width: 768px) {
    #gameContainer {
        padding: 8px;
    }
    
    h1 {
        font-size: 1.8em;
    }
    
    #levelInfo {
        font-size: 0.85em;
    }
    
    #gameArea {
        flex-direction: column;
        gap: 10px;
    }
    
    #gameCanvas {
        max-height: 45vh;
    }
    
    #sidePanel {
        width: 100%;
        max-height: 35vh;
        font-size: 0.8em;
    }
    
    #levelGrid {
        grid-template-columns: repeat(4, 1fr);
        gap: 6px;
    }
    
    .level-button {
        padding: 10px 8px;
        font-size: 13px;
        min-height: 45px;
    }
}

/* Medium devices (landscape tablets, 768px and up) */
@media (min-width: 769px) and (max-width: 1024px) {
    #gameArea {
        flex-direction: column;
        gap: 12px;
    }
    
    #gameCanvas {
        max-height: 50vh;
    }
    
    #sidePanel {
        width: 100%;
        max-width: 700px;
        max-height: 40vh;
        font-size: 0.85em;
    }
    
    #levelGrid {
        grid-template-columns: repeat(5, 1fr);
    }
}

/* Large devices (laptops/desktops, 1025px and up) */
@media (min-width: 1025px) and (max-width: 1200px) {
    #gameArea {
        flex-direction: row;
        gap: 15px;
    }
    
    #canvasContainer {
        flex: 1;
    }
    
    #sidePanel {
        width: 350px;
        max-height: 60vh;
    }
    
    #levelGrid {
        grid-template-columns: repeat(6, 1fr);
    }
}

/* Extra large devices (large laptops and desktops, 1201px and up) */
@media (min-width: 1201px) {
    #gameArea {
        flex-direction: row;
        gap: 20px;
    }
    
    #sidePanel {
        width: 400px;
        max-height: 65vh;
    }
}

/* Ultra-wide screens (1400px and up) */
@media (min-width: 1401px) {
    #gameContainer {
        max-width: 1600px;
    }
    
    #gameArea {
        gap: 25px;
    }
    
    #sidePanel {
        width: 450px;
    }
    
    #gameCanvas {
        max-width: 800px;
    }
}

/* Height-based adjustments for short screens */
@media (max-height: 600px) {
    #gameContainer {
        min-height: 100vh;
        padding: 5px;
    }
    
    h1 {
        font-size: 1.6em;
        margin-bottom: 3px;
    }
    
    #levelInfo {
        margin-bottom: 5px;
        padding: 4px 8px;
    }
    
    #gameArea {
        max-height: 60vh;
        min-height: 200px;
    }
    
    #gameCanvas {
        max-height: 30vh;
    }
    
    #sidePanel {
        max-height: 25vh;
        font-size: 0.75em;
        padding: 8px;
    }
    
    #controls, #levelSelect, #message {
        margin: 2px 0;
    }
    
    button {
        padding: 6px 12px;
        font-size: 14px;
    }
}

@media (min-height: 601px) and (max-height: 800px) {
    #gameArea {
        max-height: 50vh;
        min-height: 300px;
    }
    
    #gameCanvas {
        max-height: 40vh;
    }
    
    #sidePanel {
        max-height: 35vh;
    }
}

/* Build level specific responsive adjustments */
@media (max-width: 768px) {
    #gameArea.build-level {
        max-height: none;
        min-height: auto;
    }
    
    #sidePanel.build-level {
        max-height: 45vh;
        font-size: 0.75em;
        padding: 8px;
    }
    
    #levelGrid.build-level {
        gap: 3px;
    }
    
    .level-button.build-level {
        padding: 4px 6px;
        font-size: 10px;
        min-height: 28px;
        min-width: 30px;
    }
    
    #controls.build-level, #levelSelect.build-level, #message.build-level {
        margin: 2px 0;
    }
}

/* Touch-friendly improvements for mobile */
@media (hover: none) and (pointer: coarse) {
    button {
        min-height: 44px;
        padding: 12px 16px;
    }
    
    .gate-option {
        min-height: 44px;
        padding: 10px 12px;
    }
    
    .level-button {
        min-height: 48px;
    }
    
    #gameCanvas {
        touch-action: manipulation;
    }
}

/* Ensure canvas responsiveness */
#gameCanvas {
    max-width: 100%;
    height: auto;
    display: block;
}

/* Improve scroll behavior on mobile */
@media (max-width: 768px) {
    #sidePanel {
        -webkit-overflow-scrolling: touch;
    }
    
    body {
        -webkit-text-size-adjust: none;
    }
}