/* ===========================================
   MASTER CASTER - Game Styling (Optimized)
   iOS safe areas, reduced backdrop-filter,
   orientation handling
   =========================================== */

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: 'Inter', sans-serif;
    background: #000;
    overflow: hidden;
    height: 100vh;
    width: 100vw;
    touch-action: none;
    -webkit-user-select: none;
    user-select: none;
    /* Prevent iOS overscroll bounce */
    overscroll-behavior: none;
    -webkit-overflow-scrolling: auto;
}

#gameContainer {
    width: 100vw;
    height: 100vh;
    position: relative;
    overflow: hidden;
}

#gameCanvas {
    display: block;
    image-rendering: pixelated;
    image-rendering: crisp-edges;
    /* Size set by JS for DPR scaling */
}

/* ========= HUD ========= */
#hud {
    position: absolute;
    top: 10px;
    left: 10px;
    right: 10px;
    /* iOS safe area insets */
    top: max(10px, env(safe-area-inset-top, 10px));
    left: max(10px, env(safe-area-inset-left, 10px));
    right: max(10px, env(safe-area-inset-right, 10px));
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    pointer-events: none;
    z-index: 10;
}

#hud.hidden {
    display: none;
}

.hud-left,
.hud-right {
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.hud-center {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 4px;
}

.hud-item {
    background: rgba(0, 0, 0, 0.6);
    /* Removed backdrop-filter: blur() for mobile perf */
    padding: 6px 12px;
    border-radius: 8px;
    border: 1px solid rgba(255, 255, 255, 0.1);
    color: white;
    font-size: 0.75rem;
    font-weight: 600;
    display: flex;
    align-items: center;
    gap: 6px;
    white-space: nowrap;
}

.hud-label {
    color: rgba(255, 255, 255, 0.5);
    text-transform: uppercase;
    font-size: 0.6rem;
    letter-spacing: 1px;
}

.hud-value {
    color: #fff;
    font-weight: 800;
    font-size: 0.85rem;
}

/* Health Bar */
.health-bar-container {
    width: 120px;
    height: 10px;
    background: rgba(255, 0, 0, 0.2);
    border-radius: 5px;
    overflow: hidden;
    border: 1px solid rgba(255, 255, 255, 0.1);
}

#healthBar {
    height: 100%;
    background: linear-gradient(90deg, #ff0044, #ff4488);
    border-radius: 5px;
    transition: width 0.2s ease;
    width: 100%;
}

/* Weapon display */
.weapon-display {
    border-color: rgba(0, 255, 255, 0.3);
}

.weapon-name {
    color: #0ff;
    font-weight: 800;
}

/* Score */
.score-display {
    border-color: rgba(168, 85, 247, 0.3);
}

.score-value {
    color: #a855f7;
    font-weight: 900;
    font-size: 1rem;
}

/* Level display */
.level-display {
    font-size: 0.7rem;
    color: rgba(255, 255, 255, 0.6);
}

/* Boss HUD */
#bossHud {
    display: none;
    flex-direction: column;
    align-items: center;
    gap: 4px;
}

.boss-name {
    color: #ff4444;
    font-size: 0.8rem;
    font-weight: 900;
    text-transform: uppercase;
    letter-spacing: 2px;
    text-shadow: 0 0 10px rgba(255, 0, 0, 0.5);
}

.boss-bar-container {
    width: 250px;
    height: 8px;
    background: rgba(255, 0, 0, 0.15);
    border-radius: 4px;
    overflow: hidden;
    border: 1px solid rgba(255, 0, 0, 0.3);
}

#bossHealthBar {
    height: 100%;
    background: linear-gradient(90deg, #ff0000, #ff4444);
    border-radius: 4px;
    transition: width 0.3s ease;
    width: 100%;
}

/* ========= SCREENS / OVERLAYS ========= */
.screen-overlay {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    background: rgba(0, 0, 0, 0.85);
    z-index: 30;
    /* Keep backdrop-filter only for overlay screens (not HUD) */
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    /* iOS safe area padding */
    padding: env(safe-area-inset-top, 0) env(safe-area-inset-right, 0) env(safe-area-inset-bottom, 0) env(safe-area-inset-left, 0);
}

.screen-overlay.hidden {
    display: none;
}

.game-title {
    font-size: 4rem;
    font-weight: 900;
    background: linear-gradient(135deg, #f59e0b, #ef4444, #ec4899);
    -webkit-background-clip: text;
    background-clip: text;
    -webkit-text-fill-color: transparent;
    margin-bottom: 0.5rem;
    letter-spacing: -2px;
    text-align: center;
}

.game-subtitle {
    color: rgba(255, 255, 255, 0.6);
    font-size: 1.1rem;
    margin-bottom: 2rem;
    text-align: center;
}

.game-instructions {
    color: rgba(255, 255, 255, 0.5);
    font-size: 0.85rem;
    margin-bottom: 2rem;
    text-align: center;
    line-height: 1.8;
}

.game-btn {
    background: linear-gradient(135deg, #f59e0b, #ef4444);
    color: white;
    border: none;
    padding: 1rem 3rem;
    font-size: 1.2rem;
    font-weight: 800;
    border-radius: 1rem;
    cursor: pointer;
    text-transform: uppercase;
    letter-spacing: 2px;
    transition: all 0.3s ease;
    box-shadow: 0 10px 30px rgba(245, 158, 11, 0.4);
    margin-top: 1rem;
    pointer-events: all;
    /* Prevent iOS tap highlight */
    -webkit-tap-highlight-color: transparent;
}

.game-btn:hover {
    transform: translateY(-3px) scale(1.05);
    box-shadow: 0 15px 40px rgba(245, 158, 11, 0.6);
}

.game-btn:active {
    transform: translateY(0) scale(1);
}

.game-over-title {
    font-size: 3.5rem;
    font-weight: 900;
    color: #ff4444;
    text-shadow: 0 0 30px rgba(255, 0, 0, 0.5);
    margin-bottom: 1rem;
}

.score-display-large {
    font-size: 2rem;
    font-weight: 800;
    color: #f59e0b;
    margin: 0.5rem 0;
}

.level-complete-title {
    font-size: 3rem;
    font-weight: 900;
    background: linear-gradient(135deg, #0f0, #0ff);
    -webkit-background-clip: text;
    background-clip: text;
    -webkit-text-fill-color: transparent;
    margin-bottom: 1rem;
}

/* ========= MOBILE CONTROLS ========= */
.mobile-controls {
    display: none;
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    height: 180px;
    pointer-events: none;
    z-index: 20;
    /* iOS safe area — push controls inward */
    padding-bottom: env(safe-area-inset-bottom, 0);
    padding-left: env(safe-area-inset-left, 0);
    padding-right: env(safe-area-inset-right, 0);
}

.joystick-container {
    position: absolute;
    bottom: calc(30px + env(safe-area-inset-bottom, 0px));
    left: calc(30px + env(safe-area-inset-left, 0px));
    width: 120px;
    height: 120px;
    background: rgba(255, 255, 255, 0.08);
    border: 2px solid rgba(255, 255, 255, 0.15);
    border-radius: 50%;
    pointer-events: all;
    touch-action: none;
    /* Prevent iOS tap highlight */
    -webkit-tap-highlight-color: transparent;
}

#joystickKnob {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 50px;
    height: 50px;
    background: rgba(255, 255, 255, 0.2);
    border: 2px solid rgba(255, 255, 255, 0.3);
    border-radius: 50%;
}

.action-buttons {
    position: absolute;
    bottom: calc(30px + env(safe-area-inset-bottom, 0px));
    right: calc(20px + env(safe-area-inset-right, 0px));
    display: flex;
    gap: 10px;
    pointer-events: all;
}

.action-btn {
    width: 60px;
    height: 60px;
    border-radius: 50%;
    border: 2px solid rgba(255, 255, 255, 0.2);
    background: rgba(255, 255, 255, 0.08);
    color: white;
    font-size: 0.7rem;
    font-weight: 700;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    touch-action: none;
    -webkit-tap-highlight-color: transparent;
}

.action-btn.shoot-btn {
    background: rgba(255, 68, 68, 0.2);
    border-color: rgba(255, 68, 68, 0.4);
    font-size: 1.5rem;
}

.action-btn.jump-btn {
    background: rgba(0, 255, 255, 0.2);
    border-color: rgba(0, 255, 255, 0.4);
}

.action-btn.grenade-btn {
    background: rgba(255, 136, 0, 0.2);
    border-color: rgba(255, 136, 0, 0.4);
}

#pauseBtn {
    position: absolute;
    top: max(10px, env(safe-area-inset-top, 10px));
    right: max(10px, env(safe-area-inset-right, 10px));
    width: 40px;
    height: 40px;
    background: rgba(0, 0, 0, 0.5);
    border: 1px solid rgba(255, 255, 255, 0.2);
    border-radius: 8px;
    color: white;
    font-size: 1.2rem;
    display: none;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    pointer-events: all;
    z-index: 15;
    -webkit-tap-highlight-color: transparent;
}

/* Show mobile controls on touch devices */
@media (hover: none) and (pointer: coarse) {
    .mobile-controls {
        display: block;
    }

    #pauseBtn {
        display: flex;
    }

    #hud {
        top: max(55px, calc(env(safe-area-inset-top, 10px) + 45px));
    }
}

/* ========= RESPONSIVE ========= */
@media (max-width: 768px) {
    .game-title {
        font-size: 2.5rem;
    }

    .game-subtitle {
        font-size: 0.9rem;
    }

    .game-instructions {
        font-size: 0.75rem;
        padding: 0 1rem;
    }

    .game-btn {
        padding: 0.8rem 2rem;
        font-size: 1rem;
    }

    .game-over-title {
        font-size: 2.5rem;
    }

    .score-display-large {
        font-size: 1.5rem;
    }

    .health-bar-container {
        width: 80px;
    }

    .hud-item {
        padding: 4px 8px;
        font-size: 0.65rem;
    }

    .boss-bar-container {
        width: 180px;
    }
}

@media (max-width: 480px) {
    .game-title {
        font-size: 2rem;
    }

    .joystick-container {
        width: 100px;
        height: 100px;
    }

    #joystickKnob {
        width: 40px;
        height: 40px;
    }

    .action-btn {
        width: 50px;
        height: 50px;
    }
}

/* Landscape preference — suggest rotation in portrait */
@media (orientation: portrait) and (max-height: 600px) {
    .game-title {
        font-size: 1.8rem;
    }

    .game-subtitle,
    .game-instructions {
        display: none;
    }
}

/* Portrait mode — overlay for landscape suggestion */
@media (orientation: portrait) and (hover: none) {
    #orientationHint {
        display: flex;
    }
}

@media (orientation: landscape) {
    #orientationHint {
        display: none !important;
    }
}

#orientationHint {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.9);
    z-index: 100;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    color: white;
    font-family: 'Inter', sans-serif;
    pointer-events: all;
}

#orientationHint .rotate-icon {
    font-size: 4rem;
    margin-bottom: 1rem;
    animation: rotateHint 2s ease-in-out infinite;
}

#orientationHint .rotate-text {
    font-size: 1.2rem;
    font-weight: 600;
    color: rgba(255, 255, 255, 0.7);
}

@keyframes rotateHint {

    0%,
    100% {
        transform: rotate(0deg);
    }

    50% {
        transform: rotate(90deg);
    }
}

/* ========= BACK BUTTON ========= */
.back-link {
    position: absolute;
    top: max(12px, env(safe-area-inset-top, 12px));
    left: max(12px, env(safe-area-inset-left, 12px));
    color: rgba(255, 255, 255, 0.5);
    text-decoration: none;
    font-size: 0.8rem;
    font-weight: 600;
    z-index: 40;
    padding: 4px 10px;
    background: rgba(0, 0, 0, 0.5);
    border-radius: 6px;
    border: 1px solid rgba(255, 255, 255, 0.1);
    transition: all 0.3s;
    -webkit-tap-highlight-color: transparent;
}

.back-link:hover {
    color: white;
    background: rgba(0, 0, 0, 0.7);
}