:root {
    --bg-dark: #050510;
    --neon-green: #00ff88;
    --neon-blue: #00d4ff;
    --neon-orange: #ff9d00;
    --neon-red: #ff3e3e;
    --pixel-border: 4px solid #fff;
    --pixel-shadow: 6px 6px 0px rgba(0, 0, 0, 0.5);
    --font-main: 'Outfit', sans-serif;
    --font-code: 'JetBrains Mono', monospace;
    --glass-bg: rgba(0, 0, 0, 0.9);
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    -webkit-tap-highlight-color: transparent;
    image-rendering: pixelated;
    user-select: none;
    -webkit-user-select: none;
}

html,
body {
    margin: 0;
    padding: 0;
    height: 100%;
    width: 100%;
    overflow: hidden;
    position: fixed;
    /* Prevents bounce scroll on mobile */
}

body {
    background: var(--bg-dark);
    color: white;
    font-family: var(--font-main);
    touch-action: none;
}

#game-container {
    position: relative;
    width: 100%;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    background: #000;
}

/* Retro Scanlines */
#game-container::after {
    content: "";
    position: absolute;
    inset: 0;
    background: linear-gradient(rgba(18, 16, 16, 0) 50%, rgba(0, 0, 0, 0.15) 50%),
        linear-gradient(90deg, rgba(255, 0, 0, 0.03), rgba(0, 255, 0, 0.01), rgba(0, 0, 255, 0.03));
    background-size: 100% 4px, 3px 100%;
    pointer-events: none;
    z-index: 100;
    opacity: 0.2;
}

#game-canvas {
    display: block;
    width: 100%;
    height: 100%;
    object-fit: contain;
}

#ui-layer {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    z-index: 10;
}

#ui-layer>* {
    pointer-events: auto;
}

.hidden {
    display: none !important;
}

/* Screens */
.screen {
    position: absolute;
    inset: 0;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    background: var(--glass-bg);
    backdrop-filter: blur(12px);
    z-index: 200;
    padding: 2rem;
    animation: fadeIn 0.4s ease;
    overflow-y: auto;
    overflow-x: hidden;
}

@keyframes fadeIn {
    from {
        opacity: 0;
    }

    to {
        opacity: 1;
    }
}

/* Retro Typography */
.game-title {
    font-size: 3rem;
    font-weight: 800;
    letter-spacing: -0.1rem;
    color: #fff;
    text-shadow: 3px 3px 0px var(--neon-blue), -3px -3px 0px var(--neon-red);
    margin-bottom: 0.1rem;
    text-transform: uppercase;
    text-align: center;
}

#best-score {
    font-family: var(--font-code);
    color: var(--neon-blue);
    margin-bottom: 1rem;
    font-size: 0.85rem;
    opacity: 0.9;
}

.game-subtitle {
    font-size: 0.9rem;
    color: var(--neon-green);
    letter-spacing: 0.3rem;
    text-transform: uppercase;
    background: #000;
    padding: 0.4rem 1.5rem;
    border: 2px solid var(--neon-green);
    margin-bottom: 1rem;
    box-shadow: 4px 4px 0px rgba(0, 255, 136, 0.2);
    text-align: center;
}

/* Mission Briefing */
#mission-briefing {
    background: rgba(0, 212, 255, 0.08);
    border: 1px solid var(--neon-blue);
    border-left: 4px solid var(--neon-blue);
    padding: 0.8rem 1.2rem;
    margin: 0 auto 1.5rem auto;
    /* Centered */
    max-width: 450px;
    font-family: var(--font-code);
    font-size: 0.75rem;
    color: #fff;
    text-align: center;
    position: relative;
}

#mission-briefing::before {
    content: "MISSION_LOG";
    display: block;
    font-size: 0.6rem;
    color: var(--neon-blue);
    margin-bottom: 5px;
    font-weight: 800;
}

.typewriter {
    border-right: 2px solid var(--neon-blue);
    white-space: nowrap;
    overflow: hidden;
    animation: typing 3s steps(40, end), blink-caret .75s step-end infinite;
}

@keyframes typing {
    from {
        width: 0
    }

    to {
        width: 100%
    }
}

@keyframes blink-caret {

    from,
    to {
        border-color: transparent
    }

    50% {
        border-color: var(--neon-blue);
    }
}

/* Quick Controls Ribbon */
.controls-ribbon {
    display: flex;
    gap: 20px;
    background: rgba(0, 0, 0, 0.8);
    border: 2px solid #333;
    padding: 8px 15px;
    margin-top: 1.5rem;
    border-radius: 4px;
    justify-content: center;
    backdrop-filter: blur(5px);
}

.ribbon-item {
    display: flex;
    align-items: center;
    gap: 10px;
    font-size: 0.7rem;
    font-family: var(--font-code);
}

.ribbon-key {
    background: #fff;
    color: #000;
    padding: 2px 6px;
    font-weight: 800;
    font-size: 0.6rem;
    border-radius: 2px;
}

/* Blocky Retro Buttons */
.btn-primary,
.btn-secondary {
    padding: 1rem 2rem;
    font-family: var(--font-code);
    font-weight: 800;
    font-size: 1.1rem;
    border: 4px solid #fff;
    background: #000;
    color: #fff;
    cursor: pointer;
    position: relative;
    box-shadow: 6px 6px 0px #fff;
    text-transform: uppercase;
    transition: all 0.1s;
    margin: 10px;
}

.btn-primary {
    background: linear-gradient(135deg, var(--neon-blue), #0088ff);
    border: none;
    box-shadow: 0 8px 25px rgba(0, 212, 255, 0.4), inset 0 2px 2px rgba(255, 255, 255, 0.3);
}

.btn-primary:hover {
    background: #fff;
    color: #000;
}

.menu-buttons {
    display: flex;
    flex-direction: column;
    align-items: center;
    width: 100%;
    gap: 8px;
}

.btn-primary:active {
    box-shadow: 0 0 0;
    transform: translate(4px, 4px);
}

.btn-secondary {
    background: #222;
    border-color: #777;
    box-shadow: 6px 6px 0px #777;
}

.btn-secondary:hover {
    background: #444;
}

.sub-buttons {
    display: flex;
    gap: 10px;
    justify-content: center;
    margin-top: 0.5rem;
    flex-wrap: wrap;
    width: 100%;
}

.sub-buttons .btn-secondary {
    padding: 0.6rem 1rem;
    font-size: 0.75rem;
    box-shadow: 4px 4px 0px #777;
    margin: 5px;
    min-width: 100px;
}

.btn-mini {
    padding: 0.5rem 1rem;
    font-family: var(--font-code);
    font-size: 0.8rem;
    border: 2px solid #fff;
    background: #000;
    color: #fff;
    cursor: pointer;
    box-shadow: 3px 3px 0px #fff;
}

.btn-mini:hover {
    background: #fff;
    color: #000;
}

/* Mobile Controls Overlay */
#mobile-controls {
    position: absolute;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 100%;
    display: flex;
    justify-content: space-between;
    align-items: flex-end;
    padding: 2rem;
    pointer-events: none;
    z-index: 150;
}

.control-btn {
    pointer-events: auto;
    width: 120px;
    height: 120px;
    background: rgba(0, 0, 0, 0.5);
    border: 4px solid #fff;
    color: #fff;
    border-radius: 50%;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    font-family: var(--font-code);
    backdrop-filter: blur(5px);
    transition: transform 0.1s, background 0.1s;
    user-select: none;
}

.control-btn:active {
    transform: scale(0.9);
    background: rgba(255, 255, 255, 0.3);
}

.btn-icon {
    font-size: 2rem;
    margin-bottom: 5px;
}

.btn-text {
    font-size: 0.7rem;
    font-weight: 800;
}

#mobile-jump {
    border-color: var(--neon-blue);
    color: var(--neon-blue);
}

#mobile-slide {
    border-color: var(--neon-orange);
    color: var(--neon-orange);
}

@media (max-width: 800px) {
    #mobile-controls:not(.hidden) {
        display: flex;
    }
}

/* HUD */
#hud {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    padding: 1.5rem;
    pointer-events: none;
    z-index: 100;
}

#hud button,
#hud .key-status {
    pointer-events: auto;
}

.hud-top {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    width: 100%;
}

.stats-group {
    display: flex;
    flex-direction: column;
    gap: 15px;
    background: #000;
    border: 3px solid #fff;
    padding: 1rem;
    box-shadow: 6px 6px 0px rgba(0, 0, 0, 0.5);
}

.stat-item {
    margin-bottom: 10px;
}

.stat-item:last-child {
    margin-bottom: 0;
}

.stat-label {
    font-family: var(--font-code);
    font-size: 0.7rem;
    text-transform: uppercase;
    margin-bottom: 4px;
    display: block;
}

.progress-bar {
    width: 100%;
    max-width: 200px;
    height: 14px;
    background: #000;
    border: 2px solid #fff;
    padding: 1px;
    overflow: hidden;
    position: relative;
}

.progress-fill {
    height: 100%;
    background: var(--neon-green);
    transition: width 0.3s ease;
    box-shadow: 0 0 10px currentColor;
}

/* Key Indicator */
.key-status {
    background: #000;
    border: 3px solid #333;
    padding: 10px;
    display: flex;
    flex-direction: column;
    align-items: center;
    color: #333;
}

.key-status svg {
    opacity: 0.3;
}

.key-status.filled {
    border-color: var(--neon-blue);
    color: var(--neon-blue);
    box-shadow: 0 0 15px rgba(0, 212, 255, 0.3);
}

.key-status.filled svg {
    opacity: 1;
    filter: drop-shadow(0 0 5px var(--neon-blue));
}

#key-text {
    font-size: 0.6rem;
    font-family: var(--font-code);
    letter-spacing: 0.2em;
    margin-top: 5px;
}

.world-indicator {
    text-align: right;
    display: flex;
    flex-direction: column;
    gap: 10px;
}

#world-name {
    font-size: 0.85rem;
    font-weight: 800;
    color: var(--neon-blue);
    letter-spacing: 0.1em;
}

/* World Grid */
.world-grid {
    display: flex;
    justify-content: center;
    gap: 12px;
    margin-bottom: 1.5rem;
    padding: 5px;
    width: 100%;
    flex-wrap: wrap;
}

.world-card {
    border: 4px solid #333;
    padding: 1.5rem;
    background: #111;
    width: 160px;
    text-align: center;
    cursor: pointer;
    transition: all 0.3s;
}

.world-card:hover {
    border-color: var(--neon-blue);
    transform: translateY(-5px);
}

.world-card.active {
    border-color: var(--neon-blue);
    background: #1a1a1a;
    box-shadow: 6px 6px 0px var(--neon-blue);
}

.world-card.locked {
    opacity: 0.4;
    filter: grayscale(1);
    cursor: not-allowed;
}

.world-icon {
    font-size: 2rem;
    margin-bottom: 0.5rem;
}

.world-card-title {
    font-size: 0.75rem;
    font-family: var(--font-code);
    font-weight: 700;
}

/* Modal / Terminal */
.modal {
    position: absolute;
    inset: 0;
    display: flex;
    justify-content: center;
    align-items: center;
    background: rgba(0, 0, 0, 0.8);
    backdrop-filter: blur(15px);
    z-index: 1000;
}

.modal-content.terminal {
    background: #000;
    border: 6px solid #fff;
    padding: 2.5rem;
    width: 90%;
    max-width: 650px;
    box-shadow: 10px 10px 0px rgba(0, 0, 0, 0.8);
    animation: dialog-pop 0.4s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

@keyframes dialog-pop {
    from {
        transform: scale(0.9);
        opacity: 0;
    }

    to {
        transform: scale(1);
        opacity: 1;
    }
}

#question-text {
    font-size: 1.4rem;
    margin-bottom: 2rem;
    text-align: center;
    line-height: 1.4;
    font-family: var(--font-code);
}

.options-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 1rem;
}

.option-btn {
    padding: 1rem;
    background: #111;
    border: 3px solid #fff;
    color: #fff;
    font-family: var(--font-code);
    font-size: 1rem;
    cursor: pointer;
    transition: all 0.2s;
}

.option-btn:hover {
    background: #fff;
    color: #000;
}

/* Tutorial & About */
.stats-card {
    background: #000;
    border: 4px solid #fff;
    padding: 2.5rem;
    width: 100%;
    max-width: 600px;
    text-align: center;
}

.tutorial-scroll {
    max-height: 60vh;
    overflow-y: scroll;
    padding-right: 15px;
    text-align: left;
}

.tutorial-scroll::-webkit-scrollbar {
    width: 8px;
}

.tutorial-scroll::-webkit-scrollbar-thumb {
    background: #fff;
}

.tutorial-item {
    display: flex;
    align-items: center;
    gap: 15px;
    margin-bottom: 1.5rem;
}

.key-cap {
    background: #fff;
    color: #000;
    padding: 5px 10px;
    font-family: var(--font-code);
    font-weight: 800;
    font-size: 0.7rem;
    min-width: 100px;
    text-align: center;
    border: 2px solid #555;
}

.key-cap.object {
    background: rgba(0, 212, 255, 0.2);
    color: var(--neon-blue);
    border: 1px solid var(--neon-blue);
}

/* Animations & Effects */
.death-msg {
    color: var(--neon-red);
    font-size: 3.5rem;
    font-weight: 900;
    margin-bottom: 1rem;
    animation: blink 0.5s infinite;
}

@keyframes glitch {
    0% {
        transform: translate(0);
    }

    20% {
        transform: translate(-2px, 2px);
    }

    40% {
        transform: translate(-2px, -2px);
    }

    60% {
        transform: translate(2px, 2px);
    }

    80% {
        transform: translate(2px, -2px);
    }

    100% {
        transform: translate(0);
    }
}

@keyframes blink {

    0%,
    100% {
        opacity: 1;
    }

    50% {
        opacity: 0;
    }
}

/* Notifications */
#notification-container {
    position: absolute;
    top: 20%;
    left: 50%;
    transform: translateX(-50%);
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 10px;
    pointer-events: none;
    z-index: 1000;
}

.notif {
    padding: 0.7rem 1.5rem;
    background: #000;
    border: 3px solid #fff;
    font-family: var(--font-code);
    font-weight: 800;
    box-shadow: 4px 4px 0px #000;
    backdrop-filter: blur(5px);
}

.mobile-only {
    display: none;
}

.desktop-only {
    display: flex;
}

@media (max-width: 600px) {
    .screen {
        padding: 1rem;
        padding-top: env(safe-area-inset-top);
        padding-bottom: env(safe-area-inset-bottom);
        justify-content: flex-start;
        /* Start from top on mobile to allow scrolling if needed */
        overflow-y: auto;
    }

    .game-title {
        font-size: 2rem;
        margin-top: 1rem;
        margin-bottom: 0.1rem;
    }

    .game-subtitle {
        font-size: 0.55rem;
        letter-spacing: 0.1rem;
        padding: 0.2rem 0.5rem;
        margin-bottom: 0.6rem;
    }

    #best-score {
        font-size: 0.65rem;
        margin-bottom: 0.4rem;
    }

    #mission-briefing {
        padding: 0.4rem 0.6rem;
        margin-bottom: 0.8rem;
        font-size: 0.6rem;
        max-width: 90%;
        border-left-width: 3px;
    }

    .controls-ribbon {
        display: none;
    }

    .mobile-only {
        display: flex;
    }

    .desktop-only {
        display: none;
    }

    /* HUD Adjustments */
    #hud {
        padding: 0.5rem;
    }

    .hud-top {
        display: flex;
        justify-content: space-between;
        align-items: flex-start;
        width: 100%;
        padding: 0 0.5rem;
    }

    .stats-group {
        border-width: 1.5px;
        padding: 0.4rem;
        gap: 4px;
        max-width: 130px;
        background: rgba(0, 0, 0, 0.8);
        box-shadow: 3px 3px 0px rgba(0, 0, 0, 0.5);
    }

    .stat-item {
        margin-bottom: 2px;
    }

    .progress-bar {
        width: 65px;
        height: 8px;
        border-width: 1px;
    }

    .stat-label {
        font-size: 0.45rem;
        margin-bottom: 1px;
    }

    #distance-score {
        font-size: 0.7rem !important;
    }

    .world-indicator {
        text-align: right;
        align-items: flex-end;
        gap: 2px;
    }

    #world-name {
        font-size: 0.55rem;
        letter-spacing: 0.05rem;
    }

    .btn-mini {
        padding: 0.2rem 0.4rem;
        font-size: 0.55rem;
        box-shadow: 2px 2px 0px #fff;
    }

    /* World Grid and Buttons */
    .world-grid {
        display: grid;
        grid-template-columns: repeat(3, 1fr);
        /* 3 columns on mobile to save vertical space */
        gap: 5px;
        margin-bottom: 0.8rem;
        width: 100%;
    }

    .world-card {
        width: 100%;
        padding: 0.5rem 0.2rem;
        border-width: 2px;
    }

    .world-icon {
        font-size: 1.2rem;
        margin-bottom: 0.2rem;
    }

    .world-card-title {
        font-size: 0.45rem;
        line-height: 1.1;
    }

    .menu-buttons {
        width: 100%;
        margin-top: auto;
        padding-bottom: 1rem;
    }

    .btn-primary {
        width: 100%;
        padding: 0.7rem;
        font-size: 0.85rem;
        margin: 4px 0;
        box-shadow: 4px 4px 0px #00d4ff;
    }

    .sub-buttons {
        gap: 5px;
        margin-top: 5px;
    }

    .sub-buttons .btn-secondary {
        flex: 1;
        min-width: 70px;
        font-size: 0.6rem;
        padding: 0.5rem 0.2rem;
        margin: 2px;
        box-shadow: 3px 3px 0px #777;
    }

    /* Modals */
    .modal-content.terminal {
        padding: 0.8rem;
        width: 95%;
        border-width: 3px;
    }

    #question-text {
        font-size: 0.9rem;
        margin-bottom: 0.8rem;
    }

    .options-grid {
        grid-template-columns: 1fr;
        gap: 6px;
    }

    .option-btn {
        padding: 0.6rem;
        font-size: 0.75rem;
        border-width: 1.5px;
    }

    .control-btn {
        width: 70px;
        height: 70px;
        border-width: 2px;
    }

    #mobile-controls {
        padding: 0.8rem;
    }

    .stats-card {
        padding: 1rem;
        width: 100%;
    }
}

@media (max-height: 500px) {
    .game-title {
        font-size: 1.8rem;
    }

    .game-subtitle {
        margin-bottom: 0.5rem;
    }

    #mission-briefing {
        display: none;
    }

    /* Hide on very small heights to save space */
}