* { margin: 0; padding: 0; box-sizing: border-box; }

html, body {
    width: 100%;
    height: 100%;
    overflow: hidden;
    background: radial-gradient(ellipse at 50% 30%, #1a1033 0%, #0b0618 60%, #050310 100%);
    font-family: 'Courier New', monospace;
    touch-action: none;
    user-select: none;
    -webkit-user-select: none;
}

.back-link {
    position: fixed;
    top: 14px;
    left: 16px;
    z-index: 200;
    color: #9d8cff;
    text-decoration: none;
    font-weight: bold;
    text-shadow: 0 0 8px rgba(157, 140, 255, 0.6);
}
.back-link:hover { color: #fff; }

#hint {
    position: fixed;
    top: 14px;
    left: 50%;
    transform: translateX(-50%);
    z-index: 200;
    color: rgba(200, 190, 255, 0.55);
    font-size: 13px;
    text-align: center;
    pointer-events: none;
    white-space: nowrap;
}

/* ============ SCENE / WORLD ============ */

#scene {
    position: absolute;
    top: 50%;
    left: 50%;
    width: 1900px;
    height: 1260px;
    transform-origin: center center;
}

#world {
    position: absolute;
    left: 960px;
    top: 290px;
}

/* Floor: a 600x600 logical square projected with matrix(1,.5,-1,.5)
   so that logical (x,y) -> screen (x - y, (x+y)/2). Entities placed in
   JS use the same projection, so the grid lines up exactly. */
#floor {
    position: absolute;
    left: 0;
    top: 0;
    width: 900px;
    height: 900px;
    transform-origin: 0 0;
    transform: matrix(1, 0.5, -1, 0.5, 0, 0);
    background:
        repeating-linear-gradient(0deg,  rgba(140, 110, 255, 0.22) 0 2px, transparent 2px 50px),
        repeating-linear-gradient(90deg, rgba(140, 110, 255, 0.22) 0 2px, transparent 2px 50px),
        linear-gradient(135deg, #221643 0%, #170e30 55%, #110a24 100%);
    box-shadow:
        0 0 60px rgba(120, 80, 255, 0.25),
        inset 0 0 90px rgba(0, 0, 0, 0.55);
    border: 2px solid rgba(170, 140, 255, 0.45);
}

/* ============ SPIRIT ============ */

#spirit {
    position: absolute;
    width: 0;
    height: 0;
    pointer-events: none;
}

.spirit-body {
    position: absolute;
    left: -26px;
    bottom: 6px;
    width: 52px;
    height: 52px;
    object-fit: contain;
    filter: drop-shadow(0 0 8px rgba(150, 220, 255, 0.7));
    animation: spirit-bob 2.2s ease-in-out infinite;
}

.spirit-shadow {
    position: absolute;
    left: -20px;
    top: -7px;
    width: 40px;
    height: 14px;
    background: rgba(0, 0, 0, 0.45);
    border-radius: 50%;
    filter: blur(3px);
}

@keyframes spirit-bob {
    0%, 100% { transform: translateY(0); }
    50%      { transform: translateY(-9px); }
}

/* ============ REMOTE SPIRITS ============ */

.remote-spirit {
    position: absolute;
    width: 0;
    height: 0;
    pointer-events: none;
}

.spirit-label {
    position: absolute;
    bottom: 72px;
    left: 50%;
    transform: translateX(-50%);
    white-space: nowrap;
    font-size: 11px;
    color: #d6cdff;
    text-shadow: 0 0 6px rgba(0, 0, 0, 0.9), 0 1px 3px rgba(0, 0, 0, 0.8);
    pointer-events: none;
    letter-spacing: 0.5px;
}

/* ============ ARCADE CABINETS ============ */

.machine {
    position: absolute;
    width: 0;
    height: 0;
    cursor: pointer;
}

.machine-tag {
    position: absolute;
    top: 10px;
    left: 50%;
    transform: translateX(-50%);
    white-space: nowrap;
    font-size: 9px;
    letter-spacing: 0.5px;
    color: rgba(200, 180, 255, 0.38);
    pointer-events: none;
    text-align: center;
}

.cab {
    position: absolute;
    left: -55px;
    bottom: 0;
    width: 110px;
    height: 185px;
}

/* dark side face for a pseudo-iso look */
.cab::before {
    content: '';
    position: absolute;
    right: -16px;
    top: 9px;
    width: 16px;
    height: 176px;
    background: linear-gradient(180deg, #14102a, #0a0718);
    transform: skewY(28deg);
    transform-origin: left top;
    border-radius: 0 4px 4px 0;
}

.cab-marquee {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 30px;
    line-height: 30px;
    text-align: center;
    font-weight: bold;
    font-size: 13px;
    letter-spacing: 1px;
    color: #0a0718;
    background: var(--cab-color, #39d353);
    border-radius: 6px 6px 0 0;
    box-shadow: 0 0 10px var(--cab-color, #39d353);
}

.cab-screen {
    position: absolute;
    top: 34px;
    left: 8px;
    width: 94px;
    height: 70px;
    background: #050308;
    border: 3px solid #2a2150;
    border-radius: 4px;
    transition: box-shadow 0.3s, background 0.3s;
}

.machine.near .cab-screen {
    background: radial-gradient(circle at 50% 45%, var(--cab-color, #39d353) 0%, #08060f 80%);
    box-shadow: 0 0 22px var(--cab-color, #39d353);
}

.machine.powering .cab-screen {
    animation: crt-flicker 0.55s steps(2, jump-none) infinite;
}

@keyframes crt-flicker {
    0%   { background: #fff; }
    50%  { background: var(--cab-color, #39d353); }
    100% { background: #08060f; }
}

.cab-body {
    position: absolute;
    top: 30px;
    left: 0;
    width: 100%;
    height: 125px;
    background: linear-gradient(180deg, #2b2152 0%, #1c1538 100%);
    border-radius: 0 0 2px 2px;
    z-index: -1;
}

.cab-panel {
    position: absolute;
    top: 108px;
    left: 4px;
    width: 102px;
    height: 22px;
    background: linear-gradient(180deg, #3a2d6e, #251c4a);
    border-radius: 3px;
    transform: skewX(-6deg);
}

.cab-panel::before, .cab-panel::after {
    content: '';
    position: absolute;
    top: 6px;
    width: 10px;
    height: 10px;
    border-radius: 50%;
}
.cab-panel::before { left: 22px; background: #ff5a5a; box-shadow: 0 0 5px #ff5a5a; }
.cab-panel::after  { left: 40px; background: #ffd34d; box-shadow: 0 0 5px #ffd34d; }

.cab-base {
    position: absolute;
    bottom: 0;
    left: 2px;
    width: 106px;
    height: 52px;
    background: linear-gradient(180deg, #18122e, #0d0920);
    border-radius: 0 0 4px 4px;
}

/* "press to turn on" prompt */
.cab-prompt {
    position: absolute;
    bottom: 196px;
    left: 50%;
    transform: translateX(-50%) translateY(6px);
    padding: 5px 10px;
    background: rgba(10, 6, 24, 0.92);
    border: 1px solid var(--cab-color, #39d353);
    border-radius: 6px;
    color: var(--cab-color, #39d353);
    font-size: 12px;
    font-weight: bold;
    white-space: nowrap;
    opacity: 0;
    transition: opacity 0.25s, transform 0.25s;
    pointer-events: none;
}

.machine.near .cab-prompt {
    opacity: 1;
    transform: translateX(-50%) translateY(0);
}

/* ============ HIGH SCORES POPUP ============ */

.scores-pop {
    position: absolute;
    bottom: 230px;
    left: 50%;
    transform: translateX(-50%) translateY(12px) scale(0.92);
    width: 210px;
    background: rgba(8, 5, 20, 0.95);
    border: 2px solid var(--cab-color, #39d353);
    border-radius: 10px;
    box-shadow: 0 0 24px rgba(57, 211, 83, 0.35);
    padding: 10px 12px;
    opacity: 0;
    transition: opacity 0.3s, transform 0.3s;
    pointer-events: none;
}

.scores-pop::after {
    content: '';
    position: absolute;
    bottom: -8px;
    left: 50%;
    transform: translateX(-50%) rotate(45deg);
    width: 14px;
    height: 14px;
    background: rgba(8, 5, 20, 0.95);
    border-right: 2px solid var(--cab-color, #39d353);
    border-bottom: 2px solid var(--cab-color, #39d353);
}

.machine.near .scores-pop {
    opacity: 1;
    transform: translateX(-50%) translateY(0) scale(1);
}

.scores-pop h3 {
    color: var(--cab-color, #39d353);
    font-size: 13px;
    text-align: center;
    letter-spacing: 1px;
    margin-bottom: 7px;
}

.scores-pop .score-row {
    display: flex;
    justify-content: space-between;
    gap: 8px;
    color: #d6cdff;
    font-size: 12px;
    padding: 2px 0;
}

.scores-pop .score-row .nm {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.scores-pop .score-row .sc { color: #fff; font-weight: bold; }

.scores-pop .scores-msg {
    color: rgba(214, 205, 255, 0.6);
    font-size: 12px;
    text-align: center;
}

/* ============ JOYSTICK ============ */

#joystick {
    position: fixed;
    left: 28px;
    bottom: 28px;
    width: 120px;
    height: 120px;
    border-radius: 50%;
    background: rgba(120, 90, 255, 0.08);
    border: 2px solid rgba(157, 140, 255, 0.4);
    z-index: 150;
    touch-action: none;
}

#joy-knob {
    position: absolute;
    left: 50%;
    top: 50%;
    width: 52px;
    height: 52px;
    margin: -26px 0 0 -26px;
    border-radius: 50%;
    background: radial-gradient(circle at 35% 30%, #b9a8ff, #5a3fd6);
    box-shadow: 0 0 14px rgba(130, 100, 255, 0.6);
}

/* ============ ACTION BUTTON ============ */

#action-btn {
    position: fixed;
    right: 28px;
    bottom: 48px;
    z-index: 150;
    padding: 16px 22px;
    font-family: inherit;
    font-size: 16px;
    font-weight: bold;
    color: #0a0718;
    background: #39d353;
    border: none;
    border-radius: 12px;
    box-shadow: 0 0 18px rgba(57, 211, 83, 0.6);
    cursor: pointer;
}
#action-btn:active { transform: scale(0.95); }

/* ============ PLAY OVERLAY (big cabinet) ============ */

#overlay[hidden] { display: none; }

#overlay {
    position: fixed;
    inset: 0;
    z-index: 300;
    background: rgba(3, 2, 8, 0.92);
    display: flex;
    align-items: center;
    justify-content: center;
    animation: overlay-in 0.35s ease-out;
}

@keyframes overlay-in {
    from { opacity: 0; transform: scale(1.06); }
    to   { opacity: 1; transform: scale(1); }
}

.big-cab {
    display: flex;
    flex-direction: column;
    width: min(96vw, 1100px);
    height: min(96vh, 860px);
    background: linear-gradient(180deg, #2b2152 0%, #181030 100%);
    border: 3px solid #4a3a8c;
    border-radius: 14px;
    overflow: hidden;
    box-shadow: 0 0 50px rgba(110, 80, 255, 0.4);
}

.big-marquee {
    flex: 0 0 auto;
    padding: 10px;
    text-align: center;
    font-size: clamp(18px, 3vw, 28px);
    font-weight: bold;
    letter-spacing: 4px;
    color: #0a0718;
    background: var(--cab-color, #39d353);
    text-shadow: 0 1px 0 rgba(255, 255, 255, 0.35);
}

.big-bezel {
    flex: 1 1 auto;
    margin: 12px 16px 0;
    border: 4px solid #0c081c;
    border-radius: 8px;
    background: #000;
    overflow: hidden;
}

#game-frame {
    width: 100%;
    height: 100%;
    border: none;
    background: #000;
}

.big-panel {
    flex: 0 0 auto;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 22px;
}

.big-buttons { display: flex; gap: 12px; }

.cab-btn {
    width: 26px;
    height: 26px;
    border-radius: 50%;
    display: inline-block;
}
.cab-btn.red    { background: #ff5a5a; box-shadow: 0 0 9px #ff5a5a; }
.cab-btn.yellow { background: #ffd34d; box-shadow: 0 0 9px #ffd34d; }
.cab-btn.blue   { background: #58a6ff; box-shadow: 0 0 9px #58a6ff; }

#leave-btn {
    padding: 10px 18px;
    font-family: inherit;
    font-size: 15px;
    font-weight: bold;
    color: #fff;
    background: #3a2d6e;
    border: 2px solid #6c57c9;
    border-radius: 8px;
    cursor: pointer;
}
#leave-btn:hover { background: #4a3a8c; }

/* ============ CODE GATE ============ */

#code-gate[hidden] { display: none; }

#code-gate {
    position: fixed;
    inset: 0;
    z-index: 300;
    background: rgba(3, 2, 8, 0.92);
    display: flex;
    align-items: center;
    justify-content: center;
    animation: overlay-in 0.2s ease-out;
}

.code-gate-panel {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 14px;
    padding: 32px 40px;
    background: linear-gradient(180deg, #2b2152 0%, #181030 100%);
    border: 3px solid var(--cab-color, #ff8c42);
    border-radius: 16px;
    box-shadow: 0 0 40px color-mix(in srgb, var(--cab-color, #ff8c42) 40%, transparent);
}

.code-gate-title {
    font-size: 22px;
    font-weight: bold;
    letter-spacing: 4px;
    color: var(--cab-color, #ff8c42);
    text-shadow: 0 0 12px var(--cab-color, #ff8c42);
}

.code-gate-label {
    font-size: 12px;
    letter-spacing: 3px;
    color: rgba(200, 180, 255, 0.55);
}

.code-gate-display {
    display: flex;
    gap: 12px;
}

.code-char {
    width: 52px;
    height: 64px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 30px;
    font-weight: bold;
    letter-spacing: 0;
    color: rgba(200, 180, 255, 0.3);
    background: rgba(0, 0, 0, 0.5);
    border: 2px solid rgba(200, 180, 255, 0.2);
    border-radius: 8px;
    transition: color 0.1s, border-color 0.1s;
}

.code-char.filled {
    color: var(--cab-color, #ff8c42);
    border-color: var(--cab-color, #ff8c42);
    box-shadow: 0 0 10px color-mix(in srgb, var(--cab-color, #ff8c42) 40%, transparent);
}

@keyframes shake {
    0%, 100% { transform: translateX(0); }
    20%       { transform: translateX(-8px); }
    40%       { transform: translateX(8px); }
    60%       { transform: translateX(-6px); }
    80%       { transform: translateX(6px); }
}

.code-gate-display.shake {
    animation: shake 0.45s ease;
}

.code-gate-display.shake .code-char {
    color: #ff5a5a;
    border-color: #ff5a5a;
    box-shadow: 0 0 10px rgba(255, 90, 90, 0.4);
}

.code-gate-hint {
    font-size: 11px;
    letter-spacing: 1px;
    color: rgba(200, 180, 255, 0.35);
}

#code-cancel-btn {
    padding: 9px 20px;
    font-family: inherit;
    font-size: 13px;
    font-weight: bold;
    color: #fff;
    background: #3a2d6e;
    border: 2px solid #6c57c9;
    border-radius: 8px;
    cursor: pointer;
    margin-top: 4px;
}
#code-cancel-btn:hover { background: #4a3a8c; }

@media (max-width: 700px) {
    #hint {
        top: 44px;
        font-size: 11px;
        white-space: normal;
        width: 85vw;
    }
}
