/* ==========================================
   CSS Variables
   ========================================== */
:root {
    --bg-primary: #0a0a1a;
    --bg-secondary: #12122a;
    --bg-card: #1a1a3a;
    --text-primary: #ffffff;
    --text-secondary: #b0b0d0;
    --text-muted: #6a6a8a;
    --accent: #7b68ee;
    --accent-glow: #9370db;
    --accent-bright: #a78bfa;
    --border-color: #2a2a5a;
    --danger: #ff6b6b;
    --success: #4ade80;
    --gold: #ffd700;
}

/* ==========================================
   Reset & Base
   ========================================== */
*,
*::before,
*::after {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

html,
body {
    height: 100%;
    overflow: hidden;
}

body {
    font-family: 'Press Start 2P', cursive;
    background: var(--bg-primary);
    color: var(--text-primary);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    min-height: 100vh;
    padding: 1rem;
}

/* ==========================================
   Stars Background
   ========================================== */
.stars-container {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    z-index: 0;
    overflow: hidden;
}

.star {
    position: absolute;
    background: #fff;
    border-radius: 50%;
    animation: twinkle var(--duration, 3s) ease-in-out infinite;
    animation-delay: var(--delay, 0s);
}

@keyframes twinkle {

    0%,
    100% {
        opacity: 0.3;
    }

    50% {
        opacity: 1;
    }
}

/* ==========================================
   Game Wrapper
   ========================================== */
.game-wrapper {
    position: relative;
    z-index: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 1rem;
    max-width: 850px;
    width: 100%;
}

/* ==========================================
   Header
   ========================================== */
.game-header {
    width: 100%;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0.5rem 0;
}

.back-home {
    font-size: 0.6rem;
    color: var(--accent);
    text-decoration: none;
    padding: 0.5rem 1rem;
    border: 2px solid var(--accent);
    transition: all 0.2s;
}

.back-home:hover {
    background: var(--accent);
    color: var(--bg-primary);
}

.sound-toggle {
    background: var(--bg-card);
    border: 2px solid var(--border-color);
    padding: 0.5rem 0.75rem;
    cursor: pointer;
    font-size: 1.2rem;
    transition: all 0.2s;
    position: relative;
}

.sound-toggle:hover {
    border-color: var(--accent);
}

.sound-toggle .sound-off {
    display: none;
}

.sound-toggle.muted .sound-on {
    display: none;
}

.sound-toggle.muted .sound-off {
    display: inline;
}

/* ==========================================
   Error Message
   ========================================== */
.error-message {
    text-align: center;
    margin-bottom: 0.5rem;
}

.error-message h1 {
    font-size: 3rem;
    color: var(--accent);
    text-shadow:
        4px 4px 0 var(--accent-glow),
        8px 8px 0 rgba(123, 104, 238, 0.3);
    margin-bottom: 0.5rem;
}

.error-message .zero {
    display: inline-block;
    animation: bounce 1s ease-in-out infinite;
}

@keyframes bounce {

    0%,
    100% {
        transform: translateY(0);
    }

    50% {
        transform: translateY(-10px);
    }
}

.error-message p {
    font-size: 0.6rem;
    color: var(--text-secondary);
}

/* ==========================================
   Game Container
   ========================================== */
.game-container {
    position: relative;
    width: 100%;
    max-width: 800px;
    background: var(--bg-secondary);
    border: 4px solid var(--accent);
    border-bottom: none;
    /* No bottom border so holes look like real gaps */
    box-shadow:
        0 0 20px rgba(123, 104, 238, 0.3),
        inset 0 0 50px rgba(0, 0, 0, 0.5);
    overflow: hidden;
}

#gameCanvas {
    display: block;
    width: 100%;
    height: auto;
    image-rendering: pixelated;
    image-rendering: crisp-edges;
}

/* ==========================================
   Game UI
   ========================================== */
.game-ui {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    padding: 1rem;
    display: flex;
    justify-content: space-between;
    pointer-events: none;
}

.score-display {
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
}

.score-label {
    font-size: 0.5rem;
    color: var(--text-muted);
}

.score-value {
    font-size: 1.2rem;
    color: var(--gold);
    text-shadow: 2px 2px 0 rgba(0, 0, 0, 0.5);
}

/* ==========================================
   Game Overlays
   ========================================== */
.game-overlay {
    position: absolute;
    inset: 0;
    background: rgba(10, 10, 26, 0.95);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 1.5rem;
    padding: 2rem;
    text-align: center;
}

.game-overlay.hidden {
    display: none;
}

.game-overlay h2 {
    font-size: 1.5rem;
    color: var(--accent);
    text-shadow: 3px 3px 0 var(--accent-glow);
}

.game-overlay .instructions {
    font-size: 0.55rem;
    color: var(--text-secondary);
    line-height: 2;
}

.controls-hint {
    font-size: 0.5rem;
    color: var(--text-muted);
}

.key {
    display: inline-block;
    background: var(--bg-card);
    border: 2px solid var(--border-color);
    padding: 0.3rem 0.6rem;
    margin: 0 0.25rem;
}

.final-score {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    font-size: 0.6rem;
    color: var(--text-secondary);
}

.big-score {
    font-size: 2rem;
    color: var(--gold);
    text-shadow: 3px 3px 0 rgba(0, 0, 0, 0.5);
}

/* ==========================================
   Buttons
   ========================================== */
.pixel-btn {
    font-family: 'Press Start 2P', cursive;
    font-size: 0.7rem;
    padding: 1rem 2rem;
    background: var(--accent);
    color: var(--bg-primary);
    border: none;
    cursor: pointer;
    transition: all 0.2s;
    box-shadow:
        4px 4px 0 var(--accent-glow),
        8px 8px 0 rgba(123, 104, 238, 0.3);
}

.pixel-btn:hover {
    transform: translate(-2px, -2px);
    box-shadow:
        6px 6px 0 var(--accent-glow),
        10px 10px 0 rgba(123, 104, 238, 0.3);
}

.pixel-btn:active {
    transform: translate(2px, 2px);
    box-shadow:
        2px 2px 0 var(--accent-glow),
        4px 4px 0 rgba(123, 104, 238, 0.3);
}

.pixel-btn.secondary {
    background: transparent;
    color: var(--accent);
    border: 3px solid var(--accent);
    box-shadow: none;
}

.pixel-btn.secondary:hover {
    background: var(--accent);
    color: var(--bg-primary);
}

/* ==========================================
   High Scores Panel
   ========================================== */
.high-scores-panel {
    width: 100%;
    max-width: 300px;
    background: var(--bg-card);
    border: 3px solid var(--border-color);
    padding: 1rem;
}

.high-scores-panel h3 {
    font-size: 0.65rem;
    color: var(--accent);
    text-align: center;
    margin-bottom: 1rem;
    padding-bottom: 0.5rem;
    border-bottom: 2px solid var(--border-color);
}

.scores-list {
    list-style: none;
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    max-height: 200px;
    overflow-y: auto;
}

.scores-list li {
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 0.5rem;
    padding: 0.5rem;
    background: var(--bg-secondary);
    border: 1px solid var(--border-color);
}

.scores-list li:first-child {
    background: rgba(255, 215, 0, 0.1);
    border-color: var(--gold);
}

.scores-list .rank {
    color: var(--text-muted);
    width: 2rem;
}

.scores-list .name {
    flex: 1;
    color: var(--text-primary);
    text-transform: uppercase;
}

.scores-list .score {
    color: var(--gold);
    min-width: 4rem;
    text-align: right;
}

.scores-list .empty {
    color: var(--text-muted);
    text-align: center;
    font-size: 0.45rem;
}

/* ==========================================
   Name Modal
   ========================================== */
.modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(10, 10, 26, 0.98);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
    padding: 1rem;
}

.modal-overlay.hidden {
    display: none;
}

.modal-content {
    background: var(--bg-card);
    border: 4px solid var(--accent);
    padding: 2rem;
    text-align: center;
    max-width: 400px;
    width: 100%;
    box-shadow:
        0 0 40px rgba(123, 104, 238, 0.4),
        8px 8px 0 var(--accent-glow);
}

.modal-content h2 {
    font-size: 1rem;
    color: var(--accent);
    margin-bottom: 1rem;
}

.modal-content p {
    font-size: 0.5rem;
    color: var(--text-secondary);
    margin-bottom: 1.5rem;
}

.modal-content input {
    font-family: 'Press Start 2P', cursive;
    font-size: 1rem;
    width: 100%;
    padding: 1rem;
    background: var(--bg-secondary);
    border: 3px solid var(--border-color);
    color: var(--text-primary);
    text-align: center;
    text-transform: uppercase;
    margin-bottom: 1.5rem;
    outline: none;
    transition: border-color 0.2s;
}

.modal-content input:focus {
    border-color: var(--accent);
}

.modal-content input::placeholder {
    color: var(--text-muted);
}

/* ==========================================
   Responsive
   ========================================== */
@media (max-width: 850px) {

    html,
    body {
        overflow-y: auto;
        height: auto;
    }

    body {
        min-height: 100vh;
        padding: 0.5rem;
        justify-content: center;
    }

    .error-message h1 {
        font-size: 2rem;
    }

    .game-overlay h2 {
        font-size: 1rem;
    }

    .pixel-btn {
        font-size: 0.6rem;
        padding: 0.75rem 1.5rem;
    }

    .high-scores-panel {
        margin-bottom: 1rem;
    }
}

@media (max-width: 500px) {
    body {
        padding: 0.25rem;
    }

    .game-header {
        padding: 0.25rem 0;
    }

    .back-home {
        font-size: 0.45rem;
        padding: 0.35rem 0.6rem;
    }

    .error-message {
        margin-bottom: 0.25rem;
    }

    .error-message h1 {
        font-size: 1.3rem;
        margin-bottom: 0.25rem;
    }

    .error-message p {
        font-size: 0.4rem;
    }

    .game-overlay .instructions {
        font-size: 0.4rem;
        line-height: 1.8;
    }

    .game-overlay {
        gap: 0.75rem;
        padding: 1rem;
    }

    .game-overlay h2 {
        font-size: 0.8rem;
    }

    .pixel-btn {
        font-size: 0.5rem;
        padding: 0.6rem 1rem;
    }

    .high-scores-panel {
        max-width: 100%;
        padding: 0.5rem;
    }

    .high-scores-panel h3 {
        font-size: 0.5rem;
        margin-bottom: 0.5rem;
    }

    .scores-list {
        max-height: 120px;
    }

    .scores-list li {
        font-size: 0.4rem;
        padding: 0.35rem;
    }
}

/* ==========================================
   Touch Controls
   ========================================== */
#touchControls {
    display: none;
    width: 100%;
    max-width: 800px;
    padding: 0.5rem 0;
    justify-content: space-between;
    gap: 0.5rem;
}

.touch-left,
.touch-right {
    display: flex;
    gap: 0.5rem;
}

.touch-btn {
    font-family: 'Press Start 2P', cursive;
    font-size: 1rem;
    width: 60px;
    height: 60px;
    background: var(--bg-card);
    border: 3px solid var(--accent);
    color: var(--accent);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.1s;
    -webkit-user-select: none;
    user-select: none;
    touch-action: manipulation;
}

.touch-btn:active {
    background: var(--accent);
    color: var(--bg-primary);
    transform: scale(0.95);
}

.touch-btn.jump-btn,
.touch-btn.shoot-btn {
    font-size: 0.5rem;
    width: 70px;
}

.touch-btn.jump-btn {
    background: rgba(74, 222, 128, 0.2);
    border-color: #4ade80;
    color: #4ade80;
}

.touch-btn.shoot-btn {
    background: rgba(135, 206, 235, 0.2);
    border-color: #87ceeb;
    color: #87ceeb;
}

@media (max-width: 850px) {
    #touchControls {
        display: flex;
    }
}

@media (max-width: 500px) {
    .touch-btn {
        width: 50px;
        height: 50px;
        font-size: 0.9rem;
    }

    .touch-btn.jump-btn,
    .touch-btn.shoot-btn {
        width: 55px;
        font-size: 0.35rem;
    }
}

/* ==========================================
   Lives Display
   ========================================== */
.lives-display {
    position: absolute;
    top: 0.5rem;
    right: 0.5rem;
    font-size: 1rem;
    text-shadow: 2px 2px 0 rgba(0, 0, 0, 0.5);
}

/* ==========================================
   Updated Instructions
   ========================================== */
.game-overlay .controls-grid {
    display: grid;
    grid-template-columns: auto auto;
    gap: 0.4rem 0.75rem;
    font-size: 0.4rem;
    color: var(--text-muted);
    margin: 0.25rem 0;
}

.game-overlay .controls-grid span {
    text-align: left;
}

.game-overlay .controls-grid .key {
    font-size: 0.35rem;
    padding: 0.2rem 0.4rem;
}

/* ==========================================
   Landscape Mode (Mobile)
   ========================================== */
.desktop-only {
    display: block;
}

.mobile-home-link {
    display: none;
    font-family: 'Press Start 2P', cursive;
    font-size: 0.4rem;
    color: var(--accent);
    text-decoration: none;
    padding: 0.5rem;
    border: 2px solid var(--accent);
    text-align: center;
    white-space: nowrap;
}

.mobile-home-link:active {
    background: var(--accent);
    color: var(--bg-primary);
}

@media (max-width: 850px) {
    .mobile-home-link {
        display: block;
    }
}

@media (max-height: 600px) and (orientation: landscape) {

    html,
    body {
        height: 100%;
        overflow: hidden;
    }

    body {
        padding: 0;
        justify-content: flex-start;
    }

    .game-wrapper {
        flex-direction: column;
        height: 100vh;
        width: 100%;
        max-width: 100%;
        gap: 0;
    }

    .game-header {
        position: absolute;
        top: 0.25rem;
        right: 0.25rem;
        width: auto;
        z-index: 100;
    }

    .desktop-only {
        display: none;
    }

    .error-message {
        display: none;
    }

    .game-container {
        flex: 0 0 auto;
        width: 100%;
        max-width: 100%;
        height: calc(100vh - 100px);
        /* Leave room for touch controls */
        max-height: calc(100vh - 100px);
        border-width: 2px;
    }

    #gameCanvas {
        width: 100%;
        height: 100%;
        object-fit: contain;
    }

    .high-scores-panel {
        display: none;
    }

    #touchControls {
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        width: 100%;
        padding: 0.35rem 0.5rem;
        background: rgba(10, 10, 26, 0.95);
        border-top: 2px solid var(--accent);
        z-index: 100;
        display: flex;
        align-items: center;
    }

    .mobile-home-link {
        font-size: 0.35rem;
        padding: 0.4rem 0.5rem;
    }

    .touch-btn {
        width: 75px;
        height: 75px;
        font-size: 1.3rem;
    }

    .touch-btn.jump-btn,
    .touch-btn.shoot-btn {
        width: 82px;
        font-size: 0.45rem;
    }

    .game-overlay {
        gap: 0.5rem;
        padding: 0.75rem;
    }

    .game-overlay h2 {
        font-size: 0.8rem;
    }

    .game-overlay .instructions {
        font-size: 0.35rem;
    }

    .pixel-btn {
        font-size: 0.45rem;
        padding: 0.5rem 0.8rem;
    }

    .sound-toggle {
        padding: 0.3rem 0.5rem;
        font-size: 1rem;
    }
}

/* Very small landscape (iPhone SE etc) */
@media (max-height: 400px) and (orientation: landscape) {
    #touchControls {
        padding: 0.25rem 0.5rem;
    }

    .touch-btn {
        width: 63px;
        height: 63px;
        font-size: 1.1rem;
    }

    .touch-btn.jump-btn,
    .touch-btn.shoot-btn {
        width: 72px;
        font-size: 0.4rem;
    }

    .mobile-home-link {
        font-size: 0.35rem;
        padding: 0.4rem 0.5rem;
    }
}