:root {
    color-scheme: dark;
    --orange: #ff7518;
    --font-pixel: "Press Start 2P", monospace;
    --font-body: "VT323", monospace;
}

* {
    box-sizing: border-box;
}

html,
body {
    width: 100%;
    height: 100%;
    height: 100dvh;
    margin: 0;
    overflow: hidden;
    background: #020202;
    overscroll-behavior: none;
    font-family: var(--font-body);
    font-size: 20px;
    line-height: 1.35;
    -webkit-font-smoothing: none;
    -moz-osx-font-smoothing: grayscale;
}

button,
a {
    font: inherit;
}

h1,
h2,
.step-label,
.menu-card button,
.dialog-card button,
.menu-link,
.hud strong,
.icon-button {
    font-family: var(--font-pixel);
}

.game-shell {
    position: relative;
    width: 100%;
    height: 100%;
    height: 100dvh;
    overflow: hidden;
    touch-action: none;
    background:
        radial-gradient(circle at 75% 45%, rgba(129, 31, 5, .42), transparent 34%),
        linear-gradient(#020202, #080604);
}

#gameCanvas {
    display: block;
    width: 100%;
    height: 100%;
    touch-action: none;
    image-rendering: pixelated;
    image-rendering: crisp-edges;
}

/* Overlays — scroll from top on mobile so buttons stay reachable */
.overlay-screen {
    position: absolute;
    inset: 0;
    z-index: 10;
    display: none;
    overflow-x: hidden;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
    padding:
        max(12px, env(safe-area-inset-top))
        max(12px, env(safe-area-inset-right))
        max(12px, env(safe-area-inset-bottom))
        max(12px, env(safe-area-inset-left));
    background: radial-gradient(circle at 70% 50%, rgba(124, 34, 7, .2), rgba(0, 0, 0, .82) 55%);
}

.overlay-screen.visible {
    display: flex;
    align-items: flex-start;
    justify-content: center;
}

.menu-card,
.dialog-card {
    width: min(92vw, 560px);
    margin: auto 0;
    padding: 24px;
    border: 3px solid rgba(255, 117, 24, .5);
    border-radius: 4px;
    background: linear-gradient(145deg, rgba(18, 18, 20, .98), rgba(4, 4, 5, .96));
    box-shadow:
        0 0 0 1px #000,
        0 0 40px rgba(255, 77, 0, .18),
        inset 0 0 0 1px rgba(255, 255, 255, .04);
}

.menu-card {
    width: min(92vw, 460px);
    max-height: none;
}

.tutorial-card,
.journey-card,
.result-card {
    width: min(96vw, 720px);
    max-height: calc(100dvh - max(24px, env(safe-area-inset-top)) - max(24px, env(safe-area-inset-bottom)));
    display: flex;
    flex-direction: column;
    padding: 0;
    overflow: hidden;
}

.tutorial-scroll,
.journey-scroll,
.result-scroll {
    flex: 1 1 auto;
    min-height: 0;
    overflow-x: hidden;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    padding: 20px 20px 12px;
}

.tutorial-actions,
.dialog-actions {
    flex: 0 0 auto;
    padding: 12px 20px max(16px, env(safe-area-inset-bottom));
    border-top: 2px solid rgba(255, 117, 24, .25);
    background: rgba(0, 0, 0, .35);
}

.tutorial-actions button,
.dialog-actions button {
    margin: 6px 0;
}

.brand-line {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
}

h1 {
    margin: 0;
    color: #fff;
    font-size: clamp(18px, 5vw, 28px);
    line-height: 1.5;
    letter-spacing: 0;
    text-shadow: 2px 2px 0 #000, 0 0 12px var(--orange);
}

h1 span {
    color: #ffb16e;
    font-size: .72em;
    letter-spacing: 0;
}

h2 {
    margin: 8px 0 16px;
    font-size: clamp(11px, 3.2vw, 16px);
    line-height: 1.6;
    color: #fff;
}

p {
    color: #c8c8cb;
    line-height: 1.4;
    margin: 0;
}

.subtitle {
    margin: 18px 0 22px;
    color: #f0b083;
    font-size: 22px;
}

.brand-block {
    width: 56px;
    height: 72px;
    flex-shrink: 0;
    transform: rotate(-14deg) skewY(5deg);
    background:
        linear-gradient(135deg, rgba(255, 255, 255, .17), transparent 35%),
        repeating-linear-gradient(45deg, #7f3c21 0 8px, #4c2a25 8px 16px);
    border: 2px solid #9d5734;
    box-shadow: 0 0 16px var(--orange);
    image-rendering: pixelated;
}

.menu-card button,
.dialog-card button,
.menu-link {
    display: block;
    width: 100%;
    min-height: 44px;
    margin: 8px 0;
    padding: 12px 14px;
    color: #eee;
    text-align: left;
    text-decoration: none;
    font-size: 10px;
    line-height: 1.5;
    border: 2px solid transparent;
    border-radius: 2px;
    background: rgba(255, 255, 255, .06);
    cursor: pointer;
    transition: .15s ease;
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
}

.menu-card button:hover,
.dialog-card button:hover,
.menu-link:hover {
    color: #fff;
    border-color: var(--orange);
    background: rgba(255, 117, 24, .14);
}

.dialog-card button,
.tutorial-actions button,
.dialog-actions button {
    text-align: center;
    background: var(--orange);
    color: #190a00;
    border-color: #c44e00;
    box-shadow: 0 3px 0 #8a3000;
}

.dialog-card button:active,
.tutorial-actions button:active,
.dialog-actions button:active {
    transform: translateY(2px);
    box-shadow: 0 1px 0 #8a3000;
}

.dialog-card button.secondary,
.tutorial-actions button.secondary {
    background: rgba(255, 255, 255, .08);
    color: #eee;
    border-color: rgba(255, 255, 255, .2);
    box-shadow: none;
}

.step-label {
    display: block;
    color: var(--orange);
    font-size: 8px;
    line-height: 1.6;
    letter-spacing: 1px;
    margin-bottom: 8px;
}

.tutorial-grid {
    display: grid;
    grid-template-columns: 1fr;
    gap: 10px;
    margin: 12px 0;
}

.tutorial-grid article {
    display: flex;
    gap: 12px;
    padding: 12px;
    border: 2px solid rgba(255, 255, 255, .12);
    border-radius: 2px;
    background: rgba(255, 255, 255, .04);
}

.tutorial-grid strong {
    display: block;
    margin-bottom: 4px;
    color: #fff;
    font-family: var(--font-pixel);
    font-size: 8px;
    line-height: 1.6;
}

.tutorial-grid p {
    font-size: 20px;
    line-height: 1.25;
}

.tutorial-number {
    display: grid;
    place-items: center;
    flex: 0 0 28px;
    width: 28px;
    height: 28px;
    border-radius: 2px;
    color: #1b0900;
    background: var(--orange);
    font-family: var(--font-pixel);
    font-size: 8px;
    box-shadow: 0 2px 0 #8a3000;
}

.keyboard-help,
.touch-help {
    margin: 12px 0 4px;
    padding: 12px;
    border: 2px solid rgba(255, 255, 255, .1);
    border-radius: 2px;
    background: #080808;
}

.keyboard-help {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 6px;
}

.keyboard-help span {
    display: grid;
    place-items: center;
    flex: 0 0 32px;
    height: 32px;
    border: 2px solid #555;
    border-radius: 2px;
    background: #202020;
    font-family: var(--font-pixel);
    font-size: 10px;
}

.keyboard-help p,
.touch-help p {
    font-size: 20px;
    line-height: 1.25;
}

.keyboard-help p {
    flex: 1 1 100%;
    margin: 4px 0 0;
}

.touch-help {
    text-align: center;
}

.touch-help-icon {
    display: block;
    margin-bottom: 6px;
    font-family: var(--font-pixel);
    font-size: 10px;
    letter-spacing: 4px;
    color: var(--orange);
}

.mobile-only {
    display: none;
}

.desktop-only {
    display: block;
}

.hud {
    position: absolute;
    top: max(10px, env(safe-area-inset-top));
    left: 50%;
    z-index: 5;
    display: flex;
    align-items: center;
    gap: 10px;
    width: min(calc(100% - 20px), 440px);
    padding: 8px 12px;
    transform: translateX(-50%);
    border: 2px solid rgba(255, 255, 255, .14);
    border-radius: 2px;
    background: rgba(0, 0, 0, .82);
}

.hud.hidden {
    display: none;
}

.hud div {
    flex: 1;
    min-width: 0;
    text-align: center;
}

.hud strong {
    display: block;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-size: 8px;
    line-height: 1.6;
}

.hud span {
    display: block;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    color: #aaa;
    font-size: 18px;
    margin-top: 2px;
}

.icon-button {
    flex: 0 0 40px;
    width: 40px;
    height: 40px;
    padding: 0;
    color: #fff;
    font-size: 12px;
    border: 2px solid rgba(255, 255, 255, .18);
    border-radius: 2px;
    background: rgba(255, 255, 255, .08);
    cursor: pointer;
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
    box-shadow: 0 2px 0 rgba(0, 0, 0, .5);
}

.touch-controls {
    position: absolute;
    left: 50%;
    bottom: max(14px, env(safe-area-inset-bottom));
    z-index: 6;
    display: none;
    grid-template-columns: repeat(3, 60px);
    grid-template-rows: repeat(2, 54px);
    gap: 6px;
    transform: translateX(-50%);
    touch-action: none;
}

.touch-controls button {
    display: grid;
    place-items: center;
    width: 60px;
    height: 54px;
    padding: 0;
    color: #fff;
    font-family: var(--font-pixel);
    font-size: 12px;
    border: 2px solid rgba(255, 255, 255, .28);
    border-radius: 2px;
    background: rgba(10, 10, 12, .88);
    box-shadow: 0 4px 0 rgba(0, 0, 0, .55);
    touch-action: manipulation;
    user-select: none;
    -webkit-user-select: none;
    -webkit-tap-highlight-color: transparent;
}

.touch-controls button:active {
    transform: translateY(3px);
    border-color: var(--orange);
    background: rgba(255, 117, 24, .32);
    box-shadow: 0 1px 0 rgba(0, 0, 0, .55);
}

.touch-controls [data-direction="up"] { grid-column: 2; }
.touch-controls [data-direction="left"] { grid-column: 1; grid-row: 2; }
.touch-controls [data-direction="down"] { grid-column: 2; grid-row: 2; }
.touch-controls [data-direction="right"] { grid-column: 3; grid-row: 2; }

.overlay-screen.visible ~ .touch-controls {
    display: none !important;
}

.result-card {
    text-align: center;
}

.result-scroll {
    padding: 24px 20px 12px;
}

.result-actions button.secondary {
    background: rgba(255, 255, 255, .08);
    color: #eee;
    border-color: rgba(255, 255, 255, .2);
    box-shadow: none;
}

.result-icon {
    display: block;
    color: var(--orange);
    font-size: 48px;
    font-family: var(--font-pixel);
}

.journey-card .journey-scroll {
    padding-bottom: 8px;
}

.journey-help {
    margin: -4px 0 14px;
    font-size: 20px;
}

.stage-journey {
    position: relative;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 16px;
    padding: 16px 8px 24px;
}

.stage-journey::before {
    content: "";
    position: absolute;
    top: 18px;
    bottom: 32px;
    left: 50%;
    width: 6px;
    transform: translateX(-50%);
    background: repeating-linear-gradient(to bottom, #442518 0 12px, #1c1715 12px 22px);
}

.stage-node {
    position: relative;
    z-index: 1;
    display: grid !important;
    place-items: center;
    width: 76px !important;
    height: 76px !important;
    min-height: 76px !important;
    margin: 0 !important;
    padding: 6px !important;
    border: 4px solid #8a411e !important;
    border-radius: 4px !important;
    color: #fff !important;
    text-align: center !important;
    background: linear-gradient(145deg, #ff852d, #ba430c) !important;
    box-shadow: 0 5px 0 #6e2609, 0 8px 20px rgba(0, 0, 0, .42) !important;
    transform: none !important;
    font-family: var(--font-pixel) !important;
}

.stage-node:nth-child(4n+1) { margin-left: -100px !important; }
.stage-node:nth-child(4n+2) { margin-left: -36px !important; }
.stage-node:nth-child(4n+3) { margin-left: 52px !important; }
.stage-node:nth-child(4n) { margin-left: 96px !important; }

.stage-node span {
    display: block;
    font-size: 14px;
}

.stage-node small {
    display: block;
    max-width: 64px;
    overflow: hidden;
    font-family: var(--font-body);
    font-size: 14px;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.stage-node.completed {
    border-color: #4b8c2e !important;
    background: linear-gradient(145deg, #8ed34f, #3c8c28) !important;
    box-shadow: 0 5px 0 #225a18, 0 8px 20px rgba(0, 0, 0, .42) !important;
}

.stage-node.current {
    animation: stagePulse 1.6s infinite;
}

.stage-node.locked {
    color: #999 !important;
    border-color: #3c3c3c !important;
    background: linear-gradient(145deg, #383838, #202020) !important;
    box-shadow: 0 5px 0 #101010 !important;
    cursor: not-allowed !important;
}

.stage-node.locked::after {
    content: "X";
    position: absolute;
    right: -6px;
    bottom: -4px;
    display: grid;
    place-items: center;
    width: 24px;
    height: 24px;
    border: 2px solid #333;
    border-radius: 2px;
    background: #171717;
    font-size: 10px;
}

.stage-node:disabled {
    opacity: 1;
}

@keyframes stagePulse {
    50% {
        filter: brightness(1.25);
        box-shadow: 0 5px 0 #6e2609, 0 0 24px rgba(255, 117, 24, .75) !important;
    }
}

/* Tablet / mobile */
@media (max-width: 760px), (pointer: coarse) {
    .touch-controls {
        display: grid;
    }

    .mobile-only {
        display: block;
    }

    .desktop-only {
        display: none;
    }

    .menu-card,
    .dialog-card:not(.tutorial-card):not(.journey-card):not(.result-card) {
        width: min(100%, 520px);
        padding: 18px;
    }

    .menu-card {
        max-height: calc(100dvh - 24px);
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
    }

    .brand-line {
        gap: 10px;
    }

    .brand-block {
        width: 44px;
        height: 58px;
    }

    .subtitle {
        font-size: 20px;
    }

    .touch-controls {
        grid-template-columns: repeat(3, 64px);
        grid-template-rows: repeat(2, 58px);
    }

    .touch-controls button {
        width: 64px;
        height: 58px;
    }

    .stage-node {
        width: 68px !important;
        height: 68px !important;
        min-height: 68px !important;
    }

    .stage-node:nth-child(4n+1) { margin-left: -88px !important; }
    .stage-node:nth-child(4n+2) { margin-left: -30px !important; }
    .stage-node:nth-child(4n+3) { margin-left: 44px !important; }
    .stage-node:nth-child(4n) { margin-left: 84px !important; }
}

@media (max-width: 380px) {
    html,
    body {
        font-size: 18px;
    }

    .menu-card,
    .dialog-card:not(.tutorial-card):not(.journey-card):not(.result-card) {
        padding: 14px;
    }

    h1 {
        font-size: 16px;
    }

    .brand-block {
        width: 38px;
        height: 50px;
    }

    .tutorial-scroll,
    .journey-scroll {
        padding: 14px 14px 8px;
    }

    .tutorial-actions,
    .dialog-actions {
        padding: 10px 14px max(14px, env(safe-area-inset-bottom));
    }

    .tutorial-grid p,
    .touch-help p {
        font-size: 18px;
    }

    .touch-controls {
        grid-template-columns: repeat(3, 56px);
        grid-template-rows: repeat(2, 50px);
    }

    .touch-controls button {
        width: 56px;
        height: 50px;
        font-size: 10px;
    }

    .stage-node:nth-child(4n+1) { margin-left: -72px !important; }
    .stage-node:nth-child(4n+2) { margin-left: -24px !important; }
    .stage-node:nth-child(4n+3) { margin-left: 36px !important; }
    .stage-node:nth-child(4n) { margin-left: 68px !important; }
}

/* Landscape mobile — keep tutorial readable, single column */
@media (orientation: landscape) and (max-height: 560px) {
    .overlay-screen {
        padding:
            max(8px, env(safe-area-inset-top))
            max(10px, env(safe-area-inset-right))
            max(8px, env(safe-area-inset-bottom))
            max(10px, env(safe-area-inset-left));
    }

    .tutorial-card,
    .journey-card,
    .result-card {
        max-height: calc(100dvh - 16px);
    }

    .tutorial-scroll,
    .journey-scroll,
    .result-scroll {
        padding: 12px 14px 8px;
    }

    .tutorial-grid {
        grid-template-columns: 1fr 1fr;
        gap: 8px;
        margin: 8px 0;
    }

    .tutorial-grid article {
        padding: 8px;
    }

    .tutorial-grid p {
        font-size: 16px;
    }

    .keyboard-help,
    .touch-help {
        margin: 8px 0 2px;
        padding: 8px;
    }

    .touch-help p,
    .keyboard-help p {
        font-size: 16px;
    }

    .tutorial-actions button,
    .dialog-actions button {
        min-height: 36px;
        margin: 4px 0;
        padding: 8px 10px;
        font-size: 8px;
    }

    .hud {
        top: max(6px, env(safe-area-inset-top));
        left: max(10px, env(safe-area-inset-left));
        width: 220px;
        transform: none;
    }

    .touch-controls {
        left: auto;
        right: max(10px, env(safe-area-inset-right));
        bottom: 50%;
        transform: translateY(50%);
        grid-template-columns: repeat(3, 52px);
        grid-template-rows: repeat(2, 46px);
    }

    .touch-controls button {
        width: 52px;
        height: 46px;
        font-size: 9px;
    }

    .menu-card {
        display: grid;
        grid-template-columns: 1fr 1fr;
        gap: 6px 10px;
        width: min(94vw, 680px);
        max-height: calc(100dvh - 16px);
        overflow: auto;
    }

    .brand-line,
    .subtitle {
        grid-column: 1 / -1;
    }

    .menu-card button,
    .menu-link {
        width: 100%;
        min-height: 38px;
        margin: 0;
        padding: 8px 10px;
        font-size: 8px;
    }

    .stage-node:nth-child(4n+1) { margin-left: -80px !important; }
    .stage-node:nth-child(4n+2) { margin-left: -28px !important; }
    .stage-node:nth-child(4n+3) { margin-left: 40px !important; }
    .stage-node:nth-child(4n) { margin-left: 76px !important; }
}

@media (min-width: 761px) {
    .hud {
        max-width: 480px;
    }

    .tutorial-grid {
        grid-template-columns: 1fr 1fr;
    }
}

@media (min-width: 900px) {
    .tutorial-grid {
        grid-template-columns: repeat(2, 1fr);
    }
}
