:root {
    color-scheme: dark;
    --safe-top: env(safe-area-inset-top, 0px);
    --safe-right: env(safe-area-inset-right, 0px);
    --safe-bottom: env(safe-area-inset-bottom, 0px);
    --safe-left: env(safe-area-inset-left, 0px);
}

* { box-sizing: border-box; }

html, body {
    width: 100%;
    height: 100%;
    margin: 0;
    overflow: hidden;
    overscroll-behavior: none;
    background: #03050a;
    font-family: "Courier New", monospace;
    touch-action: none;
}

body {
    display: grid;
    place-items: center;
}

#game-container {
    position: relative;
    width: min(100vw, calc(100dvh * 16 / 9));
    height: min(100dvh, calc(100vw * 9 / 16));
    max-width: 100vw;
    max-height: 100dvh;
    overflow: hidden;
    background: #05070d;
    box-shadow: 0 0 40px rgba(0, 220, 255, .18);
}

canvas {
    display: block;
    width: 100%;
    height: 100%;
    background: #05070d;
    image-rendering: pixelated;
}

#mobile-controls {
    display: none;
    position: absolute;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 18;
    pointer-events: none;
}

.mobile-controls-panel {
    pointer-events: none;
    display: flex;
    align-items: flex-end;
    justify-content: space-between;
    gap: 18px;
    min-height: calc(118px + var(--safe-bottom));
    padding: 12px calc(14px + var(--safe-right)) calc(12px + var(--safe-bottom)) calc(14px + var(--safe-left));
    background: linear-gradient(180deg, rgba(6, 12, 20, 0), rgba(5, 8, 14, .58) 16%, rgba(5, 8, 14, .88) 100%);
    border-top: 1px solid rgba(80, 228, 255, .18);
    backdrop-filter: blur(4px);
}

.move-pad-wrap,
.control-cluster {
    pointer-events: auto;
}

.move-pad-wrap {
    display: flex;
    align-items: flex-end;
    min-width: clamp(120px, 21vw, 180px);
}

.move-pad {
    position: relative;
    width: clamp(112px, 20vw, 172px);
    height: clamp(78px, 13vw, 112px);
    touch-action: none;
    user-select: none;
    -webkit-user-select: none;
}

.move-pad-base {
    position: absolute;
    inset: 0;
    border: 2px solid rgba(61, 235, 255, .82);
    border-top: 0;
    border-radius: 0 0 999px 999px;
    background: radial-gradient(circle at 50% 35%, rgba(41, 209, 231, .34), rgba(4, 22, 31, .66));
    box-shadow: inset 0 0 18px rgba(0, 225, 255, .15), 0 0 14px rgba(0, 220, 255, .16);
}

.move-pad::before,
.move-pad::after {
    content: '';
    position: absolute;
    left: 50%;
    bottom: 10px;
    width: 2px;
    height: 52%;
    background: rgba(255,255,255,.08);
    transform-origin: bottom center;
}

.move-pad::before { transform: translateX(-50%) rotate(-34deg); }
.move-pad::after { transform: translateX(-50%) rotate(34deg); }

.move-pad[data-dir-x="left"] .move-pad-base,
.move-pad[data-dir-x="right"] .move-pad-base,
.move-pad[data-dir-y="down"] .move-pad-base,
.move-pad.is-active .move-pad-base {
    box-shadow: inset 0 0 20px rgba(255,255,255,.2), 0 0 22px rgba(0, 230, 255, .32);
}

.move-stick {
    position: absolute;
    left: 50%;
    top: 12px;
    width: clamp(34px, 6.4vw, 52px);
    height: clamp(34px, 6.4vw, 52px);
    margin-left: calc(clamp(34px, 6.4vw, 52px) / -2);
    border: 2px solid rgba(196, 250, 255, .9);
    border-radius: 50%;
    background: radial-gradient(circle at 35% 30%, rgba(150, 250, 255, .82), rgba(42, 134, 150, .55));
    box-shadow: 0 0 14px rgba(109, 240, 255, .34);
    transform: translate(0px, 0px);
    transition: transform .04s linear;
}

.control-cluster {
    display: flex;
    align-items: flex-end;
    gap: clamp(8px, 1.7vw, 16px);
}

.control-btn {
    width: clamp(50px, 9.4vw, 72px);
    height: clamp(50px, 9.4vw, 72px);
    border: 2px solid rgba(61, 235, 255, .85);
    border-radius: 50%;
    color: #eaffff;
    background: radial-gradient(circle at 35% 30%, rgba(40, 200, 225, .38), rgba(4, 22, 31, .62));
    box-shadow: inset 0 0 14px rgba(0, 225, 255, .18), 0 0 12px rgba(0, 220, 255, .18);
    font: 700 clamp(16px, 2.8vw, 24px)/1 "Courier New", monospace;
    user-select: none;
    -webkit-user-select: none;
    touch-action: none;
    backdrop-filter: blur(3px);
    transition: transform .06s, background .06s, box-shadow .06s;
}

.control-btn.action { border-color: rgba(255, 82, 203, .9); }
.control-btn.dash { font-size: clamp(10px, 1.9vw, 15px); }
.control-btn.shoot { color: #fff3a8; }
.control-btn.jump { margin-bottom: 0; }
.control-btn.is-pressed {
    transform: scale(.88);
    background: rgba(75, 240, 255, .55);
    box-shadow: inset 0 0 18px rgba(255,255,255,.35), 0 0 22px rgba(0, 230, 255, .55);
}
#rotate-overlay {
    display: none;
    position: fixed;
    inset: 0;
    z-index: 100;
    place-items: center;
    padding: 24px;
    background:
        radial-gradient(circle at 50% 35%, rgba(0, 205, 255, .13), transparent 35%),
        #03050a;
    color: #eefcff;
    text-align: center;
}

.rotate-card {
    display: grid;
    gap: 12px;
    max-width: 340px;
    padding: 28px;
    border: 1px solid rgba(0, 225, 255, .55);
    background: rgba(5, 13, 22, .92);
    box-shadow: 0 0 35px rgba(0, 220, 255, .18);
}
.rotate-card strong { font-size: 20px; }
.rotate-card span { color: #9bb8c2; font-size: 14px; line-height: 1.45; }
.phone-icon { font-size: 52px; color: #3de8ff; text-shadow: 0 0 18px #00d9ff; }

#fullscreen-hint {
    display: none;
    position: absolute;
    z-index: 20;
    top: calc(10px + var(--safe-top));
    right: calc(10px + var(--safe-right));
    border: 1px solid rgba(255,255,255,.4);
    border-radius: 5px;
    padding: 8px 11px;
    color: #dffcff;
    background: rgba(0,0,0,.58);
    font: 700 11px "Courier New", monospace;
}

[hidden] { display: none !important; }

.is-mobile #mobile-controls { display: flex; }
.is-mobile.is-portrait #rotate-overlay { display: grid; }
.is-mobile.is-landscape:not(.is-fullscreen) #fullscreen-hint:not([hidden]) { display: block; }

@media (orientation: landscape) and (max-height: 480px) {
    .is-mobile .control-btn,
    .is-mobile .move-pad { opacity: .84; }
}

#game-status {
    position: absolute; inset: 0; z-index: 50; display: grid; place-items: center;
    color: #9ef6ff; background: #05070d; font: 700 18px "Courier New", monospace;
    letter-spacing: .08em; text-align: center; padding: 24px;
}
#game-status.error { color: #ff7188; white-space: pre-line; }

/* v5: alkuvalikko, loppuvalikko ja uudelleenkäynnistys */
.game-menu {
    position: absolute;
    inset: 0;
    z-index: 40;
    display: grid;
    place-items: center;
    padding: clamp(16px, 3vw, 32px);
    background:
        linear-gradient(rgba(2, 5, 10, .78), rgba(2, 5, 10, .9)),
        repeating-linear-gradient(90deg, rgba(40, 220, 245, .035) 0 1px, transparent 1px 48px);
    overflow: auto;
    touch-action: pan-y;
}

.menu-panel {
    width: min(760px, 94%);
    padding: clamp(18px, 3vw, 34px);
    border: 1px solid rgba(61, 235, 255, .58);
    background: rgba(4, 12, 20, .94);
    box-shadow: 0 0 34px rgba(0, 220, 255, .2), inset 0 0 24px rgba(0, 220, 255, .05);
    text-align: center;
}

.menu-kicker {
    margin: 0 0 4px;
    color: #51e8ff;
    font-size: 12px;
    font-weight: 700;
    letter-spacing: .34em;
}

.menu-panel h1,
.menu-panel h2 {
    margin: 0;
    color: #f2fdff;
    font-size: clamp(28px, 5vw, 54px);
    line-height: 1;
    text-shadow: 0 0 18px rgba(61, 235, 255, .45);
}

.menu-copy {
    max-width: 620px;
    margin: 14px auto 20px;
    color: #abc8d1;
    font-size: clamp(12px, 1.8vw, 16px);
    line-height: 1.5;
}

.difficulty-grid {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 10px;
}

.difficulty-grid button,
.end-actions button {
    border: 1px solid rgba(61, 235, 255, .5);
    border-radius: 4px;
    padding: 14px 16px;
    color: #ecfdff;
    background: linear-gradient(180deg, rgba(21, 68, 82, .72), rgba(8, 24, 32, .9));
    font-family: "Courier New", monospace;
    cursor: pointer;
    touch-action: manipulation;
}

.difficulty-grid button {
    display: grid;
    gap: 5px;
    min-height: 78px;
    text-align: left;
}

.difficulty-grid button strong {
    color: #5cecff;
    font-size: 16px;
}

.difficulty-grid button span {
    color: #a8c5ce;
    font-size: 12px;
    line-height: 1.35;
}

.difficulty-grid button:hover,
.difficulty-grid button:focus-visible,
.end-actions button:hover,
.end-actions button:focus-visible {
    outline: none;
    border-color: #6af0ff;
    background: linear-gradient(180deg, rgba(31, 105, 120, .9), rgba(9, 36, 45, .95));
    box-shadow: 0 0 18px rgba(61, 235, 255, .23);
}

.difficulty-grid .recommended {
    border-color: rgba(255, 92, 214, .72);
    box-shadow: inset 0 0 16px rgba(255, 92, 214, .08);
}

.difficulty-grid .recommended strong {
    color: #ff76df;
}

.controls-help {
    margin: 18px 0 0;
    color: #789aa4;
    font-size: 11px;
    line-height: 1.4;
}

.end-panel {
    width: min(620px, 94%);
}

.end-actions {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px;
}

.end-actions button {
    min-height: 52px;
    font-weight: 700;
}

.end-actions .primary-action {
    border-color: rgba(89, 255, 155, .72);
    color: #caffdc;
}

.end-actions .secondary-action {
    border-color: rgba(255, 92, 214, .58);
    color: #ffd0f5;
}

.ui-blocking #mobile-controls {
    display: none !important;
}

@media (max-width: 700px), (max-height: 520px) {
    .game-menu {
        align-items: start;
        padding: 10px;
    }

    .menu-panel {
        width: 100%;
        padding: 14px;
    }

    .menu-panel h1,
    .menu-panel h2 {
        font-size: clamp(24px, 7vw, 38px);
    }

    .menu-copy {
        margin: 8px auto 12px;
    }

    .difficulty-grid {
        gap: 7px;
    }

    .difficulty-grid button {
        min-height: 58px;
        padding: 9px 11px;
    }

    .difficulty-grid button strong {
        font-size: 13px;
    }

    .difficulty-grid button span,
    .controls-help {
        font-size: 9px;
    }

    .controls-help {
        margin-top: 10px;
    }
}

@media (max-width: 520px) {
    .difficulty-grid,
    .end-actions {
        grid-template-columns: 1fr;
    }
}


/* level-selector-tool */
.level-selector {
    display: grid;
    grid-template-columns: auto minmax(0, 1fr);
    align-items: center;
    gap: 8px 14px;
    margin: 0 0 14px;
    padding: 12px;
    border: 1px solid rgba(61, 235, 255, .32);
    background: rgba(6, 23, 31, .72);
    text-align: left;
}
.level-selector label {
    color: #5cecff;
    font-size: 13px;
    font-weight: 700;
    letter-spacing: .08em;
}
.level-selector select {
    min-width: 0;
    width: 100%;
    min-height: 42px;
    border: 1px solid rgba(61, 235, 255, .58);
    border-radius: 4px;
    padding: 8px 36px 8px 11px;
    color: #ecfdff;
    background: #071923;
    font: 700 13px "Courier New", monospace;
    cursor: pointer;
    touch-action: manipulation;
}
.level-selector select:focus-visible {
    outline: 2px solid rgba(92, 236, 255, .7);
    outline-offset: 2px;
}
.level-selector span {
    grid-column: 1 / -1;
    color: #789aa4;
    font-size: 11px;
    line-height: 1.35;
}
@media (max-width: 700px), (max-height: 520px) {
    .level-selector {
        grid-template-columns: 1fr;
        gap: 6px;
        margin-bottom: 9px;
        padding: 8px;
    }
    .level-selector span { display: none; }
    .level-selector select {
        min-height: 36px;
        padding-top: 6px;
        padding-bottom: 6px;
    }
}


/* Gyroxen: language, character select and storage notice */
.selector-row {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 10px;
    margin: 0 0 10px;
}
.selector-card {
    display: grid;
    grid-template-columns: 1fr;
    gap: 7px;
    min-width: 0;
    padding: 10px 12px;
    text-align: left;
    border: 1px solid rgba(61, 235, 255, .26);
    background: linear-gradient(180deg, rgba(8, 19, 29, .88), rgba(9, 10, 23, .84));
    box-shadow: inset 0 0 16px rgba(0, 223, 255, .04);
}
.language-selector {
    border-color: rgba(255, 69, 111, .32);
    background: linear-gradient(180deg, rgba(26, 9, 22, .9), rgba(19, 7, 18, .76));
}
.character-selector {
    border-color: rgba(109, 255, 174, .28);
    background: linear-gradient(180deg, rgba(8, 24, 20, .92), rgba(9, 12, 24, .78));
}
.selector-card label {
    font: 700 13px "Courier New", monospace;
    letter-spacing: .08em;
}
.language-selector label { color: #ff6f9b; }
.character-selector label { color: #80ffc4; }
.selector-card select {
    width: 100%;
    min-width: 0;
    min-height: 38px;
    border-radius: 4px;
    padding: 7px 10px;
    font: 700 13px "Courier New", monospace;
}
.language-selector select {
    border: 1px solid rgba(255, 91, 139, .58);
    color: #fff4f8;
    background: #1a0b14;
}
.character-selector select {
    border: 1px solid rgba(112, 255, 184, .52);
    color: #f0fff8;
    background: #0d1c18;
}
.selector-card select:focus-visible {
    outline: 2px solid rgba(145, 244, 255, .72);
    outline-offset: 2px;
}
.storage-notice {
    position: fixed;
    inset: 0;
    z-index: 10000;
    display: grid;
    place-items: center;
    padding: 18px;
    background: rgba(0, 3, 8, .84);
    backdrop-filter: blur(6px);
}
.storage-notice[hidden] { display: none; }
.storage-card {
    width: min(560px, 100%);
    border: 1px solid rgba(71, 229, 255, .62);
    box-shadow: 0 0 36px rgba(40, 208, 255, .18);
    padding: 24px;
    color: #eafcff;
    background: linear-gradient(145deg, rgba(7, 22, 31, .98), rgba(19, 7, 20, .98));
    font-family: "Courier New", monospace;
}
.storage-card h2 { margin: 0 0 12px; color: #62ecff; }
.storage-card p { margin: 0 0 18px; line-height: 1.55; color: #b8d5db; }
.storage-card button {
    min-width: 140px;
    min-height: 42px;
    border: 1px solid #5deaff;
    color: #031016;
    background: #5deaff;
    font: 800 14px "Courier New", monospace;
    cursor: pointer;
}
@media (max-width: 700px), (max-height: 520px) {
    .selector-row {
        grid-template-columns: 1fr;
        gap: 7px;
        margin-bottom: 7px;
    }
    .selector-card {
        padding: 7px 8px;
    }
    .selector-card select { min-height: 34px; }
    .storage-card { padding: 16px; }
}

/* =========================================================
   Gyroxen UI settings v1: full-screen stage and resizable game frame
   ========================================================= */

#game-container {
    position: fixed;
    inset: 0;
    width: 100vw;
    height: 100dvh;
    max-width: none;
    max-height: none;
    overflow: hidden;
    background:
        radial-gradient(circle at 50% 48%, rgba(20, 185, 220, .06), transparent 44%),
        repeating-linear-gradient(0deg, rgba(55, 205, 225, .025) 0 1px, transparent 1px 32px),
        repeating-linear-gradient(90deg, rgba(55, 205, 225, .025) 0 1px, transparent 1px 32px),
        #03050a;
    box-shadow: none;
}

#game-viewport {
    position: absolute;
    z-index: 1;
    left: 50%;
    top: 50%;
    width: min(100vw, calc(100dvh * 16 / 9));
    height: min(100dvh, calc(100vw * 9 / 16));
    transform: translate(-50%, -50%);
    overflow: hidden;
    background: #05070d;
    box-shadow: 0 0 40px rgba(0, 220, 255, .18);
}

#game-viewport canvas {
    width: 100%;
    height: 100%;
}

.hud-menu-button {
    position: absolute;
    z-index: 35;
    left: 1.458333%;
    top: 2.592593%;
    width: 32.291667%;
    height: 15.185185%;
    margin: 0;
    padding: 0;
    border: 0;
    border-radius: 0;
    outline: 0;
    color: transparent;
    background: transparent;
    cursor: pointer;
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
}

.hud-menu-button:focus-visible {
    outline: 2px solid rgba(102, 241, 255, .95);
    outline-offset: -3px;
    background: rgba(55, 225, 245, .08);
}

.hud-menu-button:active {
    background: rgba(55, 225, 245, .12);
}

#game-container.ui-blocking .hud-menu-button,
body.gx-ui-open .hud-menu-button {
    display: none;
}

.visually-hidden {
    position: absolute !important;
    width: 1px !important;
    height: 1px !important;
    padding: 0 !important;
    margin: -1px !important;
    overflow: hidden !important;
    clip: rect(0, 0, 0, 0) !important;
    white-space: nowrap !important;
    border: 0 !important;
}

/* =========================================================
   Gyroxen UI editor v1.2: explicit fullscreen and menu entry
   ========================================================= */

.menu-editor-button {
    width: 100%;
    min-height: 44px;
    margin-top: 10px;
    border: 1px solid rgba(255, 109, 215, .62);
    border-radius: 4px;
    padding: 10px 14px;
    color: #ffd9f5;
    background: linear-gradient(180deg, rgba(91, 25, 76, .72), rgba(35, 9, 29, .92));
    font: 700 12px/1.25 "Courier New", monospace;
    cursor: pointer;
    touch-action: manipulation;
}

.menu-editor-button:hover,
.menu-editor-button:focus-visible {
    outline: none;
    border-color: #ff7add;
    box-shadow: 0 0 16px rgba(255, 80, 204, .22);
}

.fullscreen-toggle {
    position: absolute;
    z-index: 39;

    /*
     * Oikea reuna HUD-alueen alapuolella:
     * - ei peitä kentän nimeä tai edistymispalkkia
     * - jää toimintopainikkeiden yläpuolelle
     * - pysyy helposti peukalon ulottuvilla
     */
    top: clamp(
        calc(64px + var(--safe-top)),
        calc(17% + var(--safe-top)),
        calc(94px + var(--safe-top))
    );
    right: calc(8px + var(--safe-right));

    width: clamp(36px, 6.8dvh, 46px);
    height: clamp(36px, 6.8dvh, 46px);
    display: grid;
    place-items: center;
    margin: 0;
    padding: 7px;
    border: 1px solid rgba(89, 235, 255, .66);
    border-radius: 5px;
    color: #e6fcff;
    background: rgba(3, 13, 21, .76);
    box-shadow: 0 0 13px rgba(43, 223, 245, .16);
    cursor: pointer;
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
}

.fullscreen-toggle:hover,
.fullscreen-toggle:focus-visible {
    outline: none;
    border-color: #7af3ff;
    box-shadow: 0 0 18px rgba(43, 223, 245, .28);
}

/* Rauhallinen vihje vain silloin, kun koko näyttö ei ole käytössä. */
html:not(.is-fullscreen) .fullscreen-toggle {
    animation: gx-fullscreen-pulse 2.8s ease-in-out infinite;
}

html.is-fullscreen .fullscreen-toggle {
    animation: none;
}

@keyframes gx-fullscreen-pulse {
    0%,
    100% {
        border-color: rgba(89, 235, 255, .52);
        box-shadow:
            0 0 8px rgba(43, 223, 245, .10),
            inset 0 0 0 rgba(43, 223, 245, 0);
        opacity: .82;
    }

    50% {
        border-color: rgba(116, 244, 255, .92);
        box-shadow:
            0 0 20px rgba(43, 223, 245, .32),
            inset 0 0 8px rgba(43, 223, 245, .10);
        opacity: 1;
    }
}

@media (prefers-reduced-motion: reduce) {
    html:not(.is-fullscreen) .fullscreen-toggle {
        animation: none;
        opacity: 1;
    }
}

.fullscreen-icon {
    width: 100%;
    height: 100%;
    fill: none;
    stroke: currentColor;
    stroke-width: 2;
    stroke-linecap: square;
    stroke-linejoin: miter;
}

.fullscreen-toggle .icon-exit { display: none; }
.is-fullscreen .fullscreen-toggle .icon-enter { display: none; }
.is-fullscreen .fullscreen-toggle .icon-exit { display: block; }

/* Vanha tekstillinen fullscreen-painike ei kuulu v1.2-rakenteeseen. */
#fullscreen-hint { display: none !important; }

.hud-menu-button[hidden] { display: none !important; }

/* Valikot ovat pelikehyksen sisällä; ohjaimet voivat jäädä kehyksen ulkopuolelle. */
body.gx-ui-open .fullscreen-toggle {
    pointer-events: none;
    opacity: .3;
}

@media (max-height: 500px) and (orientation: landscape) {
    .menu-editor-button {
        min-height: 34px;
        margin-top: 7px;
        padding: 6px 9px;
        font-size: 10px;
    }

    .fullscreen-toggle {
        top: calc(58px + var(--safe-top));
        right: calc(6px + var(--safe-right));
        width: 34px;
        height: 34px;
        padding: 6px;
    }
}

/* Fullscreen-kuvake on käytettävissä myös alku- ja loppuvalikon päällä. */
.fullscreen-toggle { z-index: 45; }
