/* ===== RESET & BASE ===== */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    background-color: #0a0a0f;
    color: #e0e0e0;
    font-family: 'Segoe UI', 'Courier New', monospace;
    min-height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    overflow: hidden;
}

/* ===== GAME WRAPPER ===== */
.game-wrapper {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 20px;
    padding: 20px;
    max-width: 100vw;
}

/* ===== HUD ===== */
.hud {
    display: flex;
    justify-content: space-between;
    align-items: center;
    width: 100%;
    max-width: 700px;
    background: linear-gradient(135deg, #1a1a2e 0%, #16213e 100%);
    border: 2px solid #0f3460;
    border-radius: 12px;
    padding: 15px 25px;
    box-shadow: 0 0 20px rgba(15, 52, 96, 0.4), inset 0 0 10px rgba(0,0,0,0.5);
}

.hud-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 4px;
}

.hud-label {
    font-size: 11px;
    text-transform: uppercase;
    letter-spacing: 2px;
    color: #8892b0;
}

.hud-value {
    font-size: 22px;
    font-weight: bold;
    color: #64ffda;
    text-shadow: 0 0 10px rgba(100, 255, 218, 0.3);
}

.battery-bar-container {
    position: relative;
    width: 200px;
    height: 24px;
    background: #0a0a0f;
    border: 2px solid #0f3460;
    border-radius: 12px;
    overflow: hidden;
}

.battery-bar {
    height: 100%;
    width: 100%;
    background: linear-gradient(90deg, #e63946 0%, #f4a261 50%, #2a9d8f 100%);
    border-radius: 10px;
    transition: width 0.4s ease, background 0.4s ease;
    box-shadow: 0 0 10px rgba(42, 157, 143, 0.5);
}

.battery-bar.low {
    background: linear-gradient(90deg, #e63946 0%, #e63946 100%);
    box-shadow: 0 0 15px rgba(230, 57, 70, 0.6);
    animation: pulse-red 1s infinite;
}

.battery-bar.medium {
    background: linear-gradient(90deg, #f4a261 0%, #2a9d8f 100%);
}

@keyframes pulse-red {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.7; }
}

.battery-text {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    font-size: 13px;
    font-weight: bold;
    color: #fff;
    text-shadow: 0 0 5px rgba(0,0,0,0.8);
    pointer-events: none;
}

.restart-btn {
    background: linear-gradient(135deg, #e63946 0%, #d62828 100%);
    color: #fff;
    border: none;
    padding: 10px 20px;
    border-radius: 8px;
    font-size: 14px;
    font-weight: bold;
    cursor: pointer;
    text-transform: uppercase;
    letter-spacing: 1px;
    transition: all 0.3s ease;
    box-shadow: 0 0 15px rgba(230, 57, 70, 0.3);
}

.restart-btn:hover {
    transform: translateY(-2px);
    box-shadow: 0 0 25px rgba(230, 57, 70, 0.6);
}

/* ===== GAME AREA ===== */
.game-area {
    display: flex;
    justify-content: center;
    align-items: center;
    width: 100%;
}

#game-board {
    display: grid;
    width: min(90vw, calc(100vh - 280px), 480px);
    height: min(90vw, calc(100vh - 280px), 480px);
    aspect-ratio: 1 / 1;
    gap: 2px;
    background: #0a0a0f;
    border: 3px solid #0f3460;
    border-radius: 8px;
    padding: 4px;
    box-shadow: 0 0 30px rgba(15, 52, 96, 0.3);
    container-type: inline-size;
}

/* ===== CELL STYLES ===== */
.cell {
    width: 100%;
    height: 100%;
    border-radius: 4px;
    display: flex;
    justify-content: center;
    align-items: center;
    transition: all 0.2s ease;
    position: relative;
}

/* Empty / Floor */
.cell.empty {
    background: #1a1a2e;
    border: 1px solid #16213e;
}

/* Wall */
.cell.wall {
    background: linear-gradient(135deg, #0d1b2a 0%, #1b263b 100%);
    border: 1px solid #415a77;
    box-shadow: inset 0 0 8px rgba(0,0,0,0.5);
}

.cell.wall::after {
    content: '';
    width: 60%;
    height: 60%;
    background: repeating-linear-gradient(
        45deg,
        #1b263b,
        #1b263b 3px,
        #0d1b2a 3px,
        #0d1b2a 6px
    );
    border-radius: 2px;
    opacity: 0.6;
}

/* Player */
.cell.player {
    background: #1a1a2e;
    border: 1px solid #16213e;
}

.cell.player::after {
    content: '🤖';
    font-size: calc(65cqw / var(--cols, 8));
    filter: drop-shadow(0 0 8px rgba(100, 200, 255, 0.8));
    animation: player-bob 2s ease-in-out infinite;
}

@keyframes player-bob {
    0%, 100% { transform: translateY(0); }
    50% { transform: translateY(-2px); }
}

/* Light Source */
.cell.light {
    background: #1a1a2e;
    border: 1px solid #16213e;
    position: relative;
}

.cell.light::after {
    content: '💡';
    font-size: calc(60cqw / var(--cols, 8));
    filter: drop-shadow(0 0 12px rgba(255, 223, 0, 0.9));
    animation: light-glow 1.5s ease-in-out infinite;
}

@keyframes light-glow {
    0%, 100% { filter: drop-shadow(0 0 8px rgba(255, 223, 0, 0.7)); transform: scale(1); }
    50% { filter: drop-shadow(0 0 18px rgba(255, 223, 0, 1)); transform: scale(1.1); }
}

/* Drained Light (dim) */
.cell.light-drained {
    background: #1a1a2e;
    border: 1px solid #16213e;
}

.cell.light-drained::after {
    content: '💡';
    font-size: calc(60cqw / var(--cols, 8));
    opacity: 0.25;
    filter: grayscale(100%);
}

/* Exit */
.cell.exit {
    background: #1a1a2e;
    border: 1px solid #16213e;
    position: relative;
}

.cell.exit::after {
    content: '🚪';
    font-size: calc(65cqw / var(--cols, 8));
    filter: drop-shadow(0 0 12px rgba(0, 255, 136, 0.8));
    animation: exit-pulse 2s ease-in-out infinite;
}

@keyframes exit-pulse {
    0%, 100% { filter: drop-shadow(0 0 8px rgba(0, 255, 136, 0.6)); }
    50% { filter: drop-shadow(0 0 20px rgba(0, 255, 136, 1)); }
}

/* Hazard / Broken Wire */
.cell.hazard {
    background: #1a1a2e;
    border: 1px solid #16213e;
    position: relative;
}

.cell.hazard::after {
    content: '⚡';
    font-size: calc(60cqw / var(--cols, 8));
    filter: drop-shadow(0 0 10px rgba(255, 80, 0, 0.8));
    animation: hazard-flicker 0.8s ease-in-out infinite;
}

@keyframes hazard-flicker {
    0%, 100% { opacity: 1; filter: drop-shadow(0 0 8px rgba(255, 80, 0, 0.7)); }
    25% { opacity: 0.6; filter: drop-shadow(0 0 4px rgba(255, 80, 0, 0.4)); }
    50% { opacity: 1; filter: drop-shadow(0 0 14px rgba(255, 80, 0, 1)); }
    75% { opacity: 0.5; filter: drop-shadow(0 0 3px rgba(255, 80, 0, 0.3)); }
}

/* ===== INSTRUCTIONS ===== */
.instructions {
    text-align: center;
    color: #8892b0;
    font-size: 13px;
    line-height: 1.8;
    background: rgba(15, 52, 96, 0.15);
    padding: 12px 24px;
    border-radius: 8px;
    border: 1px solid rgba(15, 52, 96, 0.3);
}

.instructions strong {
    color: #64ffda;
}

.exit-hint {
    color: #00ff88;
    font-weight: bold;
}

/* ===== MODALS ===== */
.modal {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.85);
    backdrop-filter: blur(5px);
    justify-content: center;
    align-items: center;
    z-index: 1000;
    animation: fade-in 0.3s ease;
}

.modal.active {
    display: flex;
}

@keyframes fade-in {
    from { opacity: 0; }
    to { opacity: 1; }
}

.modal-content {
    background: linear-gradient(135deg, #1a1a2e 0%, #16213e 100%);
    border: 2px solid #0f3460;
    border-radius: 16px;
    padding: 40px 50px;
    text-align: center;
    box-shadow: 0 0 40px rgba(15, 52, 96, 0.5), 0 0 80px rgba(15, 52, 96, 0.2);
    animation: slide-up 0.4s ease;
    max-width: 450px;
    width: 90%;
}

@keyframes slide-up {
    from { transform: translateY(30px); opacity: 0; }
    to { transform: translateY(0); opacity: 1; }
}

.modal-title {
    font-size: 28px;
    font-weight: bold;
    margin-bottom: 15px;
    letter-spacing: 3px;
}

.game-over-title {
    color: #e63946;
    text-shadow: 0 0 20px rgba(230, 57, 70, 0.6);
}

.level-complete-title {
    color: #2a9d8f;
    text-shadow: 0 0 20px rgba(42, 157, 143, 0.6);
}

.game-complete-title {
    color: #ffd700;
    text-shadow: 0 0 20px rgba(255, 215, 0, 0.6);
}

.modal-desc {
    color: #8892b0;
    font-size: 16px;
    margin-bottom: 25px;
    line-height: 1.5;
}

.modal-btn {
    background: linear-gradient(135deg, #0f3460 0%, #1a5f7a 100%);
    color: #fff;
    border: 2px solid #64ffda;
    padding: 14px 35px;
    border-radius: 10px;
    font-size: 16px;
    font-weight: bold;
    cursor: pointer;
    text-transform: uppercase;
    letter-spacing: 2px;
    transition: all 0.3s ease;
    box-shadow: 0 0 15px rgba(100, 255, 218, 0.2);
}

.modal-btn:hover {
    transform: translateY(-3px);
    box-shadow: 0 0 30px rgba(100, 255, 218, 0.5);
    background: linear-gradient(135deg, #1a5f7a 0%, #0f3460 100%);
}

/* ===== RESPONSIVE ===== */
@media (max-width: 600px) {
    .hud {
        flex-direction: column;
        gap: 12px;
        padding: 12px;
    }
    
    .battery-bar-container {
        width: 150px;
    }
    
    .modal-content {
        padding: 30px 25px;
    }
    
    .modal-title {
        font-size: 22px;
    }
}

/* ===== MOBILE CONTROLLER & RESPONSIVE INSTRUCTIONS ===== */
.mobile-controls {
    display: none; /* Hidden by default, shown on touch devices */
    position: fixed;
    bottom: 20px;
    left: 20px;
    right: 20px;
    width: calc(100% - 40px);
    max-width: none;
    justify-content: space-between;
    align-items: flex-end;
    pointer-events: none;
    z-index: 100;
}

body.touch-device .mobile-controls {
    display: flex;
}

.dpad, .ctrl-btn.action {
    pointer-events: auto;
}

body.touch-device .instructions {
    max-width: 60%;
    margin: 0 auto;
    font-size: 11px;
    padding: 8px 12px;
    line-height: 1.5;
}

body.touch-device .desktop-only {
    display: none;
}

body.touch-device .mobile-only {
    display: block;
}

.desktop-only {
    display: block;
}

.mobile-only {
    display: none;
}

/* D-PAD */
.dpad {
    display: grid;
    grid-template-areas:
        ".    up    ."
        "left center right"
        ".    down  .";
    grid-template-columns: 45px 45px 45px;
    grid-template-rows: 45px 45px 45px;
    gap: 4px;
}

.ctrl-btn {
    background: linear-gradient(135deg, #16213e 0%, #0f3460 100%);
    border: 2px solid #0f3460;
    color: #64ffda;
    border-radius: 8px;
    font-size: 18px;
    font-weight: bold;
    display: flex;
    justify-content: center;
    align-items: center;
    cursor: pointer;
    user-select: none;
    -webkit-user-select: none;
    touch-action: none;
    transition: all 0.1s ease;
    box-shadow: 0 4px 10px rgba(0,0,0,0.3);
}

.ctrl-btn:active {
    transform: scale(0.92);
    background: linear-gradient(135deg, #0f3460 0%, #1a5f7a 100%);
    border-color: #64ffda;
    box-shadow: 0 0 15px rgba(100, 255, 218, 0.4);
}

.ctrl-btn.up { grid-area: up; }
.ctrl-btn.left { grid-area: left; }
.ctrl-btn.right { grid-area: right; }
.ctrl-btn.down { grid-area: down; }
.dpad-center {
    grid-area: center;
    background: #0a0a0f;
    border: 1px solid rgba(15, 52, 96, 0.5);
    border-radius: 4px;
}

/* ACTION BUTTON */
.ctrl-btn.action {
    flex-direction: column;
    gap: 4px;
    width: 90px;
    height: 90px;
    border-radius: 50%;
    background: linear-gradient(135deg, #ffd700 0%, #ffa500 100%);
    border: 2px solid #ffd700;
    color: #0a0a0f;
    font-size: 11px;
    letter-spacing: 1px;
    box-shadow: 0 4px 15px rgba(255, 215, 0, 0.3);
    text-shadow: none;
}

.ctrl-btn.action:active {
    background: linear-gradient(135deg, #ffa500 0%, #ff8c00 100%);
    border-color: #ff8c00;
    box-shadow: 0 0 25px rgba(255, 215, 0, 0.6);
}

.action-icon {
    font-size: 24px;
}

.action-label {
    font-weight: 800;
}

/* ===== LANDSCAPE MOBILE ADJUSTMENTS ===== */
@media (max-height: 500px) and (orientation: landscape) {
    body {
        overflow: hidden;
    }
    
    .game-wrapper {
        padding: 5px;
        gap: 8px;
        justify-content: center;
    }
    
    .hud {
        max-width: 500px;
        padding: 8px 16px;
        margin-bottom: 2px;
    }
    
    #game-board {
        width: min(75vw, calc(100vh - 110px));
        height: min(75vw, calc(100vh - 110px));
    }
    
    .instructions {
        display: none; /* Hide instructions to save space */
    }
    
    /* Make controls float at corners in landscape */
    .mobile-controls {
        display: flex;
        position: fixed;
        bottom: 15px;
        left: 20px;
        right: 20px;
        width: calc(100% - 40px);
        max-width: none;
        padding: 0;
        pointer-events: none;
        z-index: 100;
        margin-top: 0;
    }
    
    .dpad, .ctrl-btn.action {
        pointer-events: auto;
    }
    
    .dpad {
        grid-template-columns: 40px 40px 40px;
        grid-template-rows: 40px 40px 40px;
    }
    
    .ctrl-btn.action {
        width: 75px;
        height: 75px;
    }
}