body {
    margin: 0;
    padding: 0;
    touch-action: manipulation;
    overflow: hidden;
    font-family: 'Roboto', sans-serif;
}

/* --- Loading screen --- */
#loadingScreen {
    position: fixed;
    inset: 0;
    background: #0a0a0a;
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 9999;
    transition: opacity 0.4s;
}

#loadingScreen.hidden {
    opacity: 0;
    pointer-events: none;
}

.loading-content {
    text-align: center;
    background: #f4e4b0;
    background-image:
        radial-gradient(ellipse at top left, #e8d080 0%, transparent 60%),
        radial-gradient(ellipse at bottom right, #c8a840 0%, transparent 60%);
    border: 3px solid #8b6914;
    border-radius: 12px;
    padding: 2.5rem 3rem;
    box-shadow: 0 0 40px rgba(0,0,0,0.7), inset 0 0 20px rgba(180,130,20,0.3);
    color: #3a2000;
    font-family: 'Georgia', serif;
}

.loading-content h1 {
    font-size: 2rem;
    margin: 0 0 1.5rem;
    color: #5a3500;
    letter-spacing: 1px;
}

.loading-bar-container {
    width: 220px;
    height: 10px;
    background: rgba(90,53,0,0.2);
    border: 1px solid #8b6914;
    border-radius: 5px;
    margin: 0 auto 1rem;
    overflow: hidden;
}

.loading-bar {
    height: 100%;
    width: 0%;
    background: #8b6914;
    border-radius: 5px;
    transition: width 0.2s;
}

#loadingText {
    font-size: 0.9rem;
    color: #5a3500;
    font-style: italic;
}

/* --- Mission alert popup --- */
.mission-alert {
    position: fixed;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 800;
    background: rgba(0,0,0,0.6);
    user-select: none;
    -webkit-user-select: none;
    -webkit-tap-highlight-color: transparent;
    transition: opacity 0.3s;
}

.mission-alert.hidden {
    opacity: 0;
    pointer-events: none;
}

.mission-alert-scroll {
    background: #f4e4b0;
    background-image:
        radial-gradient(ellipse at top left, #e8d080 0%, transparent 60%),
        radial-gradient(ellipse at bottom right, #c8a840 0%, transparent 60%);
    border: 3px solid #8b6914;
    border-radius: 12px;
    padding: 1.5rem 2rem;
    text-align: center;
    color: #2a1a00;
    font-size: 1.3rem;
    max-width: 320px;
    box-shadow: 0 0 30px rgba(0,0,0,0.5);
}

#myCanvas {
    display: block;
    width: 100%;
    height: 100%;
}

.fullmapbox {
    display: none;
    position: fixed;
    bottom: 20px;
    right: 20px;
    background-color: rgba(0, 0, 0, 1);
    color: green;
    z-index: 99;
    border: 2px solid #8b6914;
    text-align: center;
    user-select: none;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
}

.viewport {
    position: fixed;
    background-color: rgba(0, 0, 0, 0.7);
    top: 20px;
    right: 20px;
    overflow: hidden;
    z-index: 99;
    border: 2px solid #8b6914;
    box-shadow: 0 4px 16px rgba(0,0,0,0.5);
    user-select: none;
    -webkit-user-select: none;
    -moz-user-select: none;
    cursor: pointer;
}

.infobox {
    position: fixed;
    background: #f4e4b0;
    background-image:
        radial-gradient(ellipse at top left,  #e8d080 0%, transparent 60%),
        radial-gradient(ellipse at bottom right, #c8a840 0%, transparent 60%);
    color: #3a2000;
    box-shadow: 0 4px 16px rgba(0,0,0,0.5), inset 0 0 10px rgba(180,130,20,0.2);
    border-radius: 8px;
    top: 20px;
    left: 250px;
    width: 500px;
    height: auto;
    padding: 8px 12px;
    max-height: 200px;
    overflow-y: auto;
    z-index: 199;
    text-align: center;
    border: 2px solid #8b6914;
    font-family: 'Georgia', serif;
    font-size: 0.9em;
    user-select: none;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
}

#map { position: absolute; }

#playerContainer {
    position: absolute;
    width: 10px;
    height: 10px;
    transform-origin: center center;
    z-index: 5;
}

#player {
    position: absolute;
    width: 10px;
    height: 10px;
    background-color: blueviolet;
    z-index: 5;
    border-radius: 50%;
    pointer-events: none;
}

#playerArrow {
    position: absolute;
    width: 0;
    height: 0;
    border-left: 6px solid transparent;
    border-right: 6px solid transparent;
    border-bottom: 12px solid blue;
    top: -8px;
    left: -1px;
    z-index: 6;
    pointer-events: none;
}

#enemy {
    position: absolute;
    width: 10px;
    height: 10px;
    background-color: red;
    z-index: 5;
    border-radius: 50%;
    pointer-events: none;
}

#pathLayer {
    position: absolute;
    top: 0;
    left: 0;
    pointer-events: none;
    z-index: 4;
}

.path-dot {
    position: absolute;
    width: 6px;
    height: 6px;
    background: rgba(255, 0, 0, 0.6);
    border-radius: 50%;
}

.exit-dot {
    position: absolute;
    width: 5px;
    height: 5px;
    background: rgba(0, 255, 180, 0.7);
    border-radius: 50%;
}

.control-panel {
    position: fixed;
    top: 20px;
    left: 20px;
    width: 180px;
    background: #f4e4b0;
    background-image:
        radial-gradient(ellipse at top left,  #e8d080 0%, transparent 60%),
        radial-gradient(ellipse at bottom right, #c8a840 0%, transparent 60%);
    color: #3a2000;
    padding: 10px;
    user-select: none;
    -webkit-user-select: none;
    -webkit-tap-highlight-color: transparent;
    border-radius: 8px;
    border: 2px solid #8b6914;
    box-shadow: 0 4px 16px rgba(0,0,0,0.5), inset 0 0 10px rgba(180,130,20,0.2);
    z-index: 200;
    user-select: none;
    font-family: 'Georgia', serif;
}

.control-btn {
    display: block;
    width: 100%;
    margin: 5px 0;
    padding: 8px;
    background-color: rgba(139,105,20,0.15);
    color: #3a2000;
    border: 1px solid rgba(139,105,20,0.4);
    border-radius: 4px;
    cursor: pointer;
    text-align: center;
    font-size: 16px;
    box-sizing: border-box;
}

.control-btn:hover { background-color: rgba(139,105,20,0.3); }
.control-btn:active { background-color: rgba(139,105,20,0.45); }

#goldenDisplay:empty { display: none; }
#goldenDisplay { color: #ffd700; }
/* Mobile D-pad */
#dpad {
    display: none;
    position: fixed;
    bottom: 30px;
    right: 2vw;
    z-index: 300;
    user-select: none;
    -webkit-user-select: none;
}

.dpad-row {
    display: flex;
    justify-content: flex-end;
}

.dpad-btn {
    user-select: none;
    -webkit-user-select: none;
    -webkit-touch-callout: none;
    width: 115px;
    height: 115px;
    margin: 3px;
    background-color: rgba(255,255,255,0.35);
    border: 2px solid rgba(255,255,255,0.5);
    border-radius: 10px;
    color: white;
    font-size: 47px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    touch-action: none;
}

.dpad-btn.pressed { background-color: rgba(255,255,255,0.45); }

.dpad-spacer {
    width: 80px;
    height: 115px;
    margin: 3px;
}

#turboBtn {
    position: fixed;
    bottom: 175px;
    left: 20px;
    width: 130px;
    height: 130px;
    background-color: rgba(255, 180, 0, 0.3);
    border: 2px solid rgba(255, 180, 0, 0.7);
    border-radius: 50%;
    color: white;
    font-size: 22px;
    font-weight: bold;
    display: none;
    align-items: center;
    justify-content: center;
    z-index: 300;
    cursor: pointer;
    touch-action: none;
    user-select: none;
    -webkit-user-select: none;
    -webkit-touch-callout: none;
    text-align: center;
}

#turboBtn.pressed { background-color: rgba(255, 180, 0, 0.7); }

#btnUp {
    display: none;
    position: fixed;
    bottom: 30px;
    left: 160px;
    z-index: 300;
}

/* Play prompt overlay */
#playPrompt {
    position: fixed;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
    background: #0a0a0a;
    user-select: none;
    -webkit-user-select: none;
    -webkit-tap-highlight-color: transparent;
    transition: opacity 0.3s;
}

#playPrompt.hidden {
    opacity: 0;
    pointer-events: none;
}

#playPrompt .play-scroll {
    background: #f4e4b0;
    background-image:
        radial-gradient(ellipse at top left,  #e8d080 0%, transparent 60%),
        radial-gradient(ellipse at bottom right, #c8a840 0%, transparent 60%);
    border: 3px solid #8b6914;
    border-radius: 8px;
    box-shadow: 0 0 40px rgba(0,0,0,0.7), inset 0 0 20px rgba(180,130,20,0.3);
    padding: 52px 72px;
    text-align: center;
    color: #3a2000;
    font-family: 'Georgia', serif;
    cursor: pointer;
    user-select: none;
}

#playPrompt .play-scroll h2 {
    font-size: 2em;
    letter-spacing: 2px;
    text-transform: uppercase;
    margin: 0 0 16px;
    color: #5a3500;
}

#playPrompt .play-scroll .play-glyph {
    font-size: 6.5em;
    line-height: 1;
    display: block;
    margin-bottom: 12px;
}

#playPrompt .play-scroll p {
    font-size: 1.3em;
    font-style: italic;
    color: #7a5010;

    margin: 0;
}

/* Lobby controls */
.lobby-controls {
    margin: 18px 0;
}

.lobby-difficulty {
    display: flex;
    gap: 8px;
    justify-content: center;
    margin-bottom: 12px;
}

.diff-btn {
    padding: 8px 18px;
    border: 2px solid #8b6914;
    border-radius: 6px;
    background: rgba(90,53,0,0.1);
    color: #5a3500;
    font-family: 'Georgia', serif;
    font-size: 0.95em;
    cursor: pointer;
    transition: background 0.15s, border-color 0.15s;
}

.diff-btn:hover {
    background: rgba(180,130,20,0.2);
}

.diff-btn.selected {
    background: #5a3500;
    color: #f4e4b0;
    border-color: #5a3500;
}

.lobby-training {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    font-size: 0.95em;
    color: #5a3500;
    cursor: pointer;
}

.lobby-training input[type="checkbox"] {
    width: 16px;
    height: 16px;
    accent-color: #5a3500;
}

/* Game over overlay */
#gameOverOverlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.55);
    z-index: 1000;
    align-items: center;
    justify-content: center;
}

#gameOverOverlay.open { display: flex; }

#gameOverOverlay .gameover-scroll {
    background: #f4e4b0;
    background-image:
        radial-gradient(ellipse at top left,  #e8d080 0%, transparent 60%),
        radial-gradient(ellipse at bottom right, #c8a840 0%, transparent 60%);
    border: 3px solid #8b6914;
    border-radius: 8px;
    box-shadow: 0 0 40px rgba(0,0,0,0.7), inset 0 0 20px rgba(180,130,20,0.3);
    padding: 52px 72px;
    max-width: 50vw;
    max-height: 85vh;
    overflow-y: auto;
    text-align: center;
    color: #3a2000;
    font-family: 'Georgia', serif;
    user-select: none;
    -webkit-user-select: none;
    -webkit-tap-highlight-color: transparent;
    -webkit-touch-callout: none;
}

#gameOverOverlay .gameover-scroll .play-glyph {
    font-size: 6.5em;
    line-height: 1;
    display: block;
    margin-bottom: 12px;
}

#gameOverOverlay .gameover-scroll h2 {
    font-size: 2em;
    letter-spacing: 2px;
    text-transform: uppercase;
    margin: 0 0 16px;
    color: #5a3500;
}

#gameOverOverlay .gameover-scroll p {
    font-size: 1.3em;
    font-style: italic;
    color: #7a5010;
    margin: 8px 0;
}

#gameOverOverlay .gameover-scroll .close-hint {
    font-size: 0.78em;
    margin-top: 20px;
}

/* Leaderboard */
#leaderboardToggle {
    display: block;
    margin: 12px auto;
    padding: 14px 24px;
    font-family: 'Georgia', serif;
    font-size: 1em;
    color: #3a2000;
    background: #e8d080;
    border: 2px solid #8b6914;
    border-radius: 6px;
    cursor: pointer;
}

#leaderboardToggle.qualified {
    background: #ffd700;
    border-color: #b8860b;
    font-weight: bold;
}

#leaderboardSection {
    margin: 16px 0 8px;
    text-align: center;
    user-select: none;
    -webkit-user-select: none;
    -webkit-tap-highlight-color: transparent;
}

#leaderboardSection input {
    user-select: text;
    -webkit-user-select: text;
}

#leaderboardInput {
    display: flex;
    gap: 8px;
    justify-content: center;
    align-items: center;
    margin-bottom: 12px;
}

#playerNameInput {
    font-family: 'Georgia', serif;
    font-size: 1em;
    padding: 6px 12px;
    border: 2px solid #8b6914;
    border-radius: 4px;
    background: #fff8e0;
    color: #3a2000;
    width: 160px;
    user-select: text;
    -webkit-user-select: text;
}

#playerNameInput::placeholder { color: #b89040; font-style: italic; }

#submitScoreBtn {
    font-family: 'Georgia', serif;
    font-size: 0.95em;
    padding: 6px 16px;
    border: 2px solid #8b6914;
    border-radius: 4px;
    background: #e8c840;
    color: #3a2000;
    cursor: pointer;
    font-weight: bold;
}

#submitScoreBtn:hover { background: #d4b030; }
#submitScoreBtn:disabled { opacity: 0.5; cursor: default; }

#leaderboardResults {
    text-align: center;
    display: flex;
    gap: 24px;
    justify-content: center;
    flex-wrap: wrap;
    user-select: none;
}

#leaderboardResults h3 {
    font-size: 1em;
    color: #5a3500;
    margin: 0 0 6px;
    text-align: center;
}

#leaderboardResults ol {
    margin: 0 auto;
    padding-left: 20px;
    font-size: 0.9em;
    color: #3a2000;
    display: inline-block;
    text-align: left;
}

#leaderboardResults li {
    margin: 3px 0;
}

/* Restart options */
.restart-options {
    margin-top: 20px;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 10px;
    user-select: none;
    -webkit-user-select: none;
    -webkit-tap-highlight-color: transparent;
}

.restart-btn {
    font-family: 'Georgia', serif;
    font-size: 1em;
    padding: 8px 20px;
    border: 2px solid #8b6914;
    border-radius: 4px;
    background: #e8c840;
    color: #3a2000;
    cursor: pointer;
    font-weight: bold;
    min-width: 160px;
}

.restart-btn:hover { background: #d4b030; }

.seed-input-row {
    display: flex;
    gap: 6px;
    align-items: center;
}

.seed-input-row input {
    font-family: 'Georgia', serif;
    font-size: 1em;
    padding: 6px 10px;
    border: 2px solid #8b6914;
    border-radius: 4px;
    background: #fff8e0;
    color: #3a2000;
    width: 100px;
    text-align: center;
    user-select: text;
    -webkit-user-select: text;
}

.seed-input-row .restart-btn { min-width: auto; padding: 6px 14px; }

.share-btn {
    background: #d4c090;
    font-size: 0.85em;
    margin-top: 4px;
    transition: background 0.2s;
}

.share-btn.copied {
    background: #6ab04c;
    color: #fff;
}

/* Help button */
#fadeOverlay {
    position: fixed;
    inset: 0;
    background: #000;
    opacity: 1;
    pointer-events: none;
    z-index: 999;
    transition: opacity 1s ease;
}
#fadeOverlay.done {
    opacity: 0;
    visibility: hidden;
    transition: opacity 1s ease, visibility 0s linear 1s;
}
#helpBtn {
    position: fixed;
    bottom: 20px;
    left: 20px;
    width: 44px;
    height: 44px;
    background: rgba(180, 140, 60, 0.85);
    border: 2px solid rgba(220, 180, 80, 0.9);
    border-radius: 50%;
    color: #fff8e7;
    font-size: 22px;
    font-weight: bold;
    cursor: pointer;
    z-index: 400;
    display: flex;
    align-items: center;
    justify-content: center;
    user-select: none;
    box-shadow: 0 2px 8px rgba(0,0,0,0.5);
}

#fullscreenToggle {
    position: fixed;
    bottom: 20px;
    left: 74px;
    width: 44px;
    height: 44px;
    background: rgba(180, 140, 60, 0.85);
    border: 2px solid rgba(220, 180, 80, 0.9);
    border-radius: 50%;
    color: #fff8e7;
    font-size: 20px;
    cursor: pointer;
    z-index: 400;
    display: flex;
    align-items: center;
    justify-content: center;
    user-select: none;
    box-shadow: 0 2px 8px rgba(0,0,0,0.5);
}

/* Parchment overlay */
#helpOverlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.6);
    z-index: 500;
    align-items: center;
    justify-content: center;
}

#helpOverlay.open { display: flex; }

#helpScroll {
    background: #f4e4b0;
    background-image:
        radial-gradient(ellipse at top left,  #e8d080 0%, transparent 60%),
        radial-gradient(ellipse at bottom right, #c8a840 0%, transparent 60%);
    border: 3px solid #8b6914;
    border-radius: 8px;
    box-shadow: 0 0 40px rgba(0,0,0,0.7), inset 0 0 20px rgba(180,130,20,0.3);
    padding: 32px 36px;
    max-width: 640px;
    width: 90vw;
    max-height: 92vh;
    overflow-y: auto;
    color: #3a2000;
    font-family: 'Georgia', serif;
    position: relative;
}

#helpScroll h1 {
    font-size: 1.4em;
    text-align: center;
    margin: 0 0 4px;
    letter-spacing: 2px;
    text-transform: uppercase;
    border-bottom: 2px solid #8b6914;
    padding-bottom: 8px;
}

#helpScroll h2 {
    font-size: 1em;
    margin: 16px 0 6px;
    text-transform: uppercase;
    letter-spacing: 1px;
    color: #5a3500;
}

#helpScroll table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.9em;
    margin-bottom: 8px;
}

#helpScroll td, #helpScroll th {
    padding: 4px 8px;
    border-bottom: 1px solid rgba(139,105,20,0.3);
    text-align: left;
}

#helpScroll th { font-style: italic; color: #5a3500; }

#helpScroll p { font-size: 0.88em; margin: 6px 0; line-height: 1.5; }

#helpScroll .close-hint {
    text-align: center;
    font-size: 0.78em;
    color: #7a5010;
    margin-top: 20px;
    font-style: italic;
}

@media (max-width: 1024px) {
    .viewport {
        width: 400px !important;
        height: 400px !important;
        top: 20px !important;
    }
    .infobox {
        top: 20px !important;
        left: 20px !important;
        right: auto !important;
        width: 37vw !important;
        font-size: 1.2em !important;
    }
    .control-panel {
        top: 150px !important;
        min-width: 210px !important;
        width: auto !important;
    }
    .control-btn {
        width: 210px !important;
        font-size: 1.2em !important;
    }

    /* Game over overlay — mobile */
    #gameOverOverlay .gameover-scroll {
        padding: 32px 24px;
        max-height: 90vh;
        max-width: none;
        overflow-y: auto;
        width: 80vw;
        box-sizing: border-box;
        font-size: 1.3em;
    }
    #gameOverOverlay .gameover-scroll .play-glyph { font-size: 4em; }
    #gameOverOverlay .gameover-scroll h2 { font-size: 1.5em; }
    #gameOverOverlay .gameover-scroll p { font-size: 1.1em; }
    #leaderboardResults { flex-direction: column; gap: 16px; align-items: center; }
    #leaderboardResults ol { font-size: 1em; padding-left: 0; list-style-position: inside; text-align: center; }
    #leaderboardResults li { margin: 6px 0; }
    #seedScores, #globalScores { text-align: center; }
    .restart-btn { font-size: 1.2em; padding: 14px 28px; min-width: 220px; }
    .seed-input-row input { font-size: 1.2em; padding: 12px; width: 130px; }
    .seed-input-row .restart-btn { padding: 14px 18px; min-width: auto; }
    #playerNameInput { font-size: 1.2em; padding: 12px 14px; }
    #submitScoreBtn { font-size: 1.2em; padding: 14px 24px; }
    .share-btn { font-size: 1.2em; padding: 14px 28px; min-width: 220px; background: #e8c840; }
    #leaderboardInput { gap: 10px; }

    /* Lobby — scale up for mobile */
    #playPrompt .play-scroll {
        padding: 32px 24px;
        width: 85vw;
        box-sizing: border-box;
    }
    #playPrompt .play-scroll h2 { font-size: 1.6em; }
    #playPrompt .play-scroll .play-glyph { font-size: 4.5em; }
    #playPrompt .play-scroll p { font-size: 1.2em; }
    .diff-btn { padding: 12px 20px; font-size: 1.1em; }
    .lobby-training { font-size: 1.1em; }
}
