@import url('https://fonts.googleapis.com/css2?family=Press+Start+2P&display=swap');

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    user-select: none;
    -webkit-user-select: none;
}

body,
html {
    width: 100%;
    height: 100%;
    overflow: hidden;
    overscroll-behavior: none;
    background-color: #0a0a0a;
    font-family: 'Press Start 2P', 'Courier New', Courier, monospace;
    color: white;
    /* V8.13: Use env() safe areas for notch/island phones */
    padding:
        env(safe-area-inset-top)
        env(safe-area-inset-right)
        env(safe-area-inset-bottom)
        env(safe-area-inset-left);
}

/* ========== START SCREEN ========== */
#start-screen {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: linear-gradient(180deg, #0a0a0a 0%, #1a1a2e 50%, #16213e 100%);
    display: flex;
    flex-direction: column;
    justify-content: flex-start;
    align-items: center;
    z-index: 1000;
    text-align: center;
    padding: 15px;
    padding-top: max(15px, env(safe-area-inset-top));
    overflow-y: auto;
    touch-action: auto;
    -webkit-overflow-scrolling: touch;
}

#start-screen.hidden {
    display: none;
}

.start-content {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: clamp(8px, 2vh, 15px);
    max-width: 500px;
    width: 100%;
    padding-bottom: 30px;
}

#game-logo {
    width: clamp(150px, 50vw, 280px);
    image-rendering: pixelated;
    animation: logoPulse 2s ease-in-out infinite;
}

@keyframes logoPulse {
    0%, 100% { transform: scale(1); filter: drop-shadow(0 0 10px rgba(255, 215, 0, 0.5)); }
    50% { transform: scale(1.03); filter: drop-shadow(0 0 20px rgba(255, 215, 0, 0.8)); }
}

.rotate-hint {
    font-size: clamp(7px, 1.5vw, 10px);
    color: #aaa;
    letter-spacing: 1px;
}

#play-btn {
    font-family: 'Press Start 2P', monospace;
    font-size: clamp(12px, 3vw, 18px);
    padding: clamp(10px, 2vh, 15px) clamp(25px, 6vw, 40px);
    background: linear-gradient(180deg, #ffd700, #ff9500);
    color: #000;
    border: 3px solid #fff;
    border-radius: 8px;
    cursor: pointer;
    text-shadow: none;
    animation: btnGlow 1.5s ease-in-out infinite;
    transition: transform 0.1s;
}

#play-btn:active {
    transform: scale(0.95);
}

@keyframes btnGlow {
    0%, 100% { box-shadow: 0 0 10px rgba(255, 215, 0, 0.4); }
    50% { box-shadow: 0 0 25px rgba(255, 215, 0, 0.8), 0 0 50px rgba(255, 150, 0, 0.3); }
}

/* ========== LEADERBOARD ========== */
.leaderboard-box {
    width: 100%;
    max-width: 400px;
    background: rgba(0, 0, 0, 0.6);
    border: 2px solid #ffd700;
    border-radius: 8px;
    padding: 10px;
}

.leaderboard-box h2 {
    font-size: clamp(9px, 2vw, 12px);
    color: #ffd700;
    margin-bottom: 6px;
    text-align: center;
}

.leaderboard-list {
    font-size: clamp(7px, 1.5vw, 9px);
    line-height: 1.8;
}

.leaderboard-list .lb-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 2px 5px;
    border-bottom: 1px solid rgba(255, 215, 0, 0.15);
}

.leaderboard-list .lb-row:nth-child(-n+3) {
    color: #ffd700;
}

.leaderboard-list .lb-row .lb-rank {
    width: 25px;
    text-align: left;
}

.leaderboard-list .lb-row .lb-name {
    flex: 1;
    text-align: left;
    padding-left: 5px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.leaderboard-list .lb-row .lb-score {
    text-align: right;
    min-width: 50px;
}

.empty-lb {
    color: #666;
    font-size: clamp(7px, 1.5vw, 9px);
    text-align: center;
    padding: 10px;
}

/* ========== GAME CONTAINER ========== */
#game-container {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    height: 100dvh; /* Dynamic viewport height - excludes browser chrome */
    display: none;
}

canvas {
    display: block;
    width: 100%;
    height: 100%;
    background-color: #4facfe;
    touch-action: none;
}

/* ========== UI LAYER ========== */
#ui-layer {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
}

#top-bar {
    display: flex;
    align-items: center;
    gap: clamp(4px, 1vw, 10px);
    padding: clamp(4px, 1vh, 8px) clamp(6px, 1vw, 10px);
    padding-top: max(clamp(4px, 1vh, 8px), env(safe-area-inset-top));
    flex-wrap: nowrap;
}

#health-bar-container {
    width: clamp(80px, 20vw, 150px);
    height: clamp(10px, 2vh, 16px);
    background-color: #333;
    border: 2px solid #fff;
    border-radius: 3px;
    overflow: hidden;
    flex-shrink: 0;
}

#health-bar {
    width: 100%;
    height: 100%;
    background: linear-gradient(90deg, #ff4444, #00ff00);
    transition: width 0.2s;
}

#score {
    font-size: clamp(8px, 1.8vw, 12px);
    font-weight: bold;
    text-shadow: 1px 1px 2px #000;
    white-space: nowrap;
}

#level-display {
    font-size: clamp(7px, 1.5vw, 10px);
    color: #ffd700;
    text-shadow: 1px 1px 2px #000;
    white-space: nowrap;
}

#lives-display {
    font-size: clamp(10px, 2vw, 14px);
    letter-spacing: 1px;
    text-shadow: 1px 1px 2px #000;
    white-space: nowrap;
}

#pause-btn {
    font-size: clamp(14px, 3vw, 20px);
    background: rgba(0, 0, 0, 0.4);
    border: 2px solid rgba(255, 255, 255, 0.3);
    border-radius: 6px;
    color: white;
    padding: 4px 8px;
    cursor: pointer;
    pointer-events: auto;
    margin-left: auto;
    flex-shrink: 0;
}

#pause-btn:active {
    background: rgba(255, 255, 255, 0.3);
}

#char-name-display {
    position: absolute;
    top: clamp(30px, 6vh, 45px);
    left: clamp(6px, 1vw, 10px);
    font-size: clamp(6px, 1.2vw, 8px);
    font-weight: bold;
    color: #fff;
    background: rgba(0, 0, 0, 0.5);
    padding: 3px 6px;
    border-radius: 4px;
    border: 1px solid #ffd700;
}

/* ========== PAUSE OVERLAY ========== */
#pause-overlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.75);
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    gap: 25px;
    pointer-events: auto;
    z-index: 50;
}

#pause-overlay.hidden {
    display: none;
}

#pause-overlay h1 {
    font-size: clamp(18px, 5vw, 28px);
    color: #ffd700;
    text-shadow: 2px 2px 0 #000;
}

#resume-btn {
    font-family: 'Press Start 2P', monospace;
    font-size: clamp(10px, 2.5vw, 14px);
    padding: 12px 30px;
    background: linear-gradient(180deg, #ffd700, #ff9500);
    color: #000;
    border: 3px solid #fff;
    border-radius: 6px;
    cursor: pointer;
}

/* ========== GAME OVER SCREEN ========== */
#game-over-screen {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background: rgba(0, 0, 0, 0.9);
    border: 3px solid #ff4444;
    padding: clamp(12px, 3vw, 20px);
    border-radius: 10px;
    text-align: center;
    pointer-events: auto;
    max-width: 420px;
    width: 90%;
    max-height: 80vh;
    max-height: 80dvh;
    overflow-y: auto;
    z-index: 100;
    touch-action: auto;
    -webkit-overflow-scrolling: touch;
}

#game-over-screen.hidden {
    display: none;
}

#game-over-screen h1 {
    font-size: clamp(16px, 4vw, 22px);
    color: #ff4444;
    text-shadow: 2px 2px 0 #000;
    margin-bottom: 8px;
}

#final-stats {
    margin-bottom: 10px;
}

#final-stats p {
    font-size: clamp(9px, 2vw, 12px);
    margin: 4px 0;
    color: #ffd700;
}

#name-input-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 6px;
    margin-bottom: 10px;
}

#name-input-container label {
    font-size: clamp(7px, 1.5vw, 9px);
    color: #aaa;
}

#player-name {
    font-family: 'Press Start 2P', monospace;
    font-size: clamp(10px, 2.5vw, 14px);
    padding: 6px 10px;
    background: #111;
    border: 2px solid #ffd700;
    border-radius: 4px;
    color: #ffd700;
    text-align: center;
    text-transform: uppercase;
    width: clamp(150px, 50vw, 200px);
    outline: none;
}

#player-name::placeholder {
    color: #555;
}

#save-score-btn {
    font-family: 'Press Start 2P', monospace;
    font-size: clamp(8px, 1.8vw, 10px);
    padding: 6px 16px;
    background: #28a745;
    color: white;
    border: 2px solid #fff;
    border-radius: 4px;
    cursor: pointer;
}

#save-score-btn:disabled {
    background: #555;
    cursor: not-allowed;
    opacity: 0.5;
}

#restart-btn {
    font-family: 'Press Start 2P', monospace;
    font-size: clamp(9px, 2vw, 12px);
    padding: 10px 20px;
    background: linear-gradient(180deg, #ff4444, #cc0000);
    color: white;
    border: 3px solid #fff;
    border-radius: 6px;
    cursor: pointer;
    margin-top: 8px;
}

/* ========== LEVEL OVERLAY ========== */
#level-overlay {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    font-size: clamp(16px, 4vw, 24px);
    font-weight: bold;
    color: #ffd700;
    text-shadow: 2px 2px 0 #000;
    opacity: 0;
    transition: opacity 0.5s;
    pointer-events: none;
    text-align: center;
    white-space: pre-line;
}

#level-overlay.show {
    opacity: 1;
}

/* ========== CONTROLS ========== */
#controls {
    position: absolute;
    bottom: clamp(10px, 3vh, 20px);
    bottom: max(clamp(10px, 3vh, 20px), env(safe-area-inset-bottom));
    width: 100%;
    display: flex;
    justify-content: space-between;
    padding: 0 clamp(15px, 4vw, 40px);
    pointer-events: auto;
    touch-action: none;
}

.control-btn {
    width: clamp(50px, 12vw, 70px);
    height: clamp(50px, 12vw, 70px);
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.2);
    border: 3px solid rgba(255, 255, 255, 0.5);
    color: white;
    font-size: clamp(20px, 5vw, 30px);
    display: flex;
    justify-content: center;
    align-items: center;
    outline: none;
}

.control-btn:active {
    background: rgba(255, 255, 255, 0.5);
}

.d-pad {
    display: flex;
    gap: clamp(15px, 4vw, 30px);
    align-items: flex-end;
}

.action-buttons {
    display: flex;
    gap: clamp(15px, 4vw, 30px);
    align-items: flex-end;
}

#btn-jump {
    width: clamp(55px, 14vw, 80px);
    height: clamp(55px, 14vw, 80px);
    background: rgba(79, 172, 254, 0.3);
}