body, html {
    margin: 0;
    padding: 0;
    width: 100%;
    height: 100%;
    background-color: #050508;
    display: flex;
    justify-content: center;
    align-items: center;
    font-family: 'Courier New', Courier, monospace;
    overflow: hidden;
    color: #fff;
}

#game-container {
    position: relative;
    width: 100%;
    max-width: 100vw;
    max-height: 100vh;
    aspect-ratio: 16 / 9;
    box-shadow: 0 0 50px rgba(0, 255, 204, 0.1);
    touch-action: none;
    user-select: none;
    -webkit-user-select: none;
}

canvas {
    width: 100%;
    height: 100%;
    display: block;
    background-color: #1a1a2e;
}

#ui-layer {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    display: none;
    flex-direction: column;
    justify-content: space-between;
    padding: 2% 4%;
    box-sizing: border-box;
    z-index: 10;
}

body.in-game #ui-layer {
    display: flex;
}

.hud-top {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    font-size: 2.5vw;
    font-weight: bold;
    text-shadow: 2px 2px 0 #000, 0 0 10px rgba(0, 255, 204, 0.8);
    color: #00ffcc;
}

.hud-info {
    display: flex;
    flex-direction: column;
    gap: 10px;
}

#room-display {
    font-size: 1.8vw;
    color: #00ffcc;
    text-shadow: 0 0 8px rgba(0, 255, 204, 0.6);
}

#minimap-container {
    width: 180px;
    height: 150px;
    border: 2px solid #00ffcc;
    background: rgba(0, 0, 0, 0.8);
    box-shadow: 0 0 10px rgba(0, 255, 204, 0.4);
    border-radius: 4px;
    overflow: hidden;
}

#minimap-container canvas {
    width: 100%;
    height: 100%;
    background: transparent;
}

.hud-bottom {
    display: flex;
    justify-content: space-between;
    align-items: flex-end;
    gap: 20px;
}

#sanity-container {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

#sanity-bar {
    width: 300px;
    height: 20px;
    background: rgba(0, 0, 0, 0.7);
    border: 2px solid #ff3366;
    position: relative;
    overflow: hidden;
    border-radius: 3px;
}

#sanity-fill {
    height: 100%;
    background: linear-gradient(90deg, #ff3366, #ff6699);
    width: 100%;
    transition: width 0.1s linear;
    box-shadow: 0 0 10px rgba(255, 51, 102, 0.8);
}

#sanity-text {
    font-size: 1.2vw;
    color: #ff3366;
    text-shadow: 0 0 5px rgba(255, 51, 102, 0.6);
}


#interaction-prompt {
    position: absolute;
    background: rgba(0, 0, 0, 0.9);
    border: 1px solid #00ffcc;
    padding: 8px 12px;
    border-radius: 4px;
    font-size: 1.2vw;
    color: #00ffcc;
    text-shadow: 0 0 8px rgba(0, 255, 204, 0.6);
    opacity: 0;
    pointer-events: none;
    z-index: 15;
    transition: opacity 0.2s;
}

#screens {
    position: absolute;
    top: 0; left: 0; width: 100%; height: 100%;
    background: rgba(0, 0, 0, 0.78);
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    text-align: center;
    z-index: 20;
    pointer-events: auto;
    overflow: hidden;
}

.screen {
    display: none;
    flex-direction: column;
    align-items: center;
}

.screen.active {
    display: flex;
}

h1 {
    font-size: 5vw;
    color: #00ffcc;
    text-shadow: 0 0 20px rgba(0, 255, 204, 0.5);
    margin: 20px 0;
    animation: glitch-title 4s infinite;
}

@keyframes glitch-title {
    0%, 90%, 100% {
        text-shadow: 0 0 20px rgba(0, 255, 204, 0.5);
        color: #00ffcc;
        transform: none;
    }
    92% {
        text-shadow: -3px 0 #ff3366, 3px 0 #00ffcc;
        color: #ff3366;
        transform: skewX(-2deg);
    }
    94% {
        text-shadow: 3px 0 #ff3366, -3px 0 #00ffcc;
        color: #00ffcc;
        transform: skewX(2deg);
    }
    96% {
        text-shadow: -3px 0 #ff3366, 3px 0 #00ffcc;
        transform: none;
    }
}

p {
    font-size: 1.5vw;
    max-width: 90%;
    line-height: 1.8;
}

.pulse {
    animation: pulse 2s infinite;
}

@keyframes pulse {
    0%   { opacity: 0.7; }
    50%  { opacity: 1; text-shadow: 0 0 30px #00ffcc; }
    100% { opacity: 0.7; }
}

button {
    display: block;
    margin: 20px auto 0;
    padding: 15px 40px;
    font-size: 2vw;
    font-family: inherit;
    background: transparent;
    color: #00ffcc;
    border: 2px solid #00ffcc;
    border-radius: 6px;
    cursor: pointer;
    transition: all 0.3s;
    letter-spacing: 0.2em;
    text-transform: uppercase;
    position: relative;
    overflow: hidden;
    pointer-events: auto;
}

button::before {
    content: '';
    position: absolute;
    top: 0; left: -100%; width: 100%; height: 100%;
    background: linear-gradient(90deg, transparent, rgba(0,255,204,0.15), transparent);
    transition: left 0.5s;
}

button:hover::before { left: 100%; }

button:hover {
    background: #00ffcc;
    color: #000;
    box-shadow: 0 0 20px #00ffcc;
}

/* ============================================================
   MENU BUTTON LAYOUT
============================================================ */
.menu-buttons {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0;
}

.btn-secondary {
    font-size: 1.4vw !important;
    padding: 10px 32px !important;
    color: #aaffea !important;
    border-color: #aaffea !important;
    opacity: 0.85;
}

.btn-secondary:hover {
    background: #aaffea !important;
    color: #000 !important;
    box-shadow: 0 0 16px #aaffea !important;
    opacity: 1;
}

.btn-exit {
    font-size: 1.2vw !important;
    padding: 8px 28px !important;
    color: #ff3366 !important;
    border-color: #ff3366 !important;
    opacity: 0.8;
}

.btn-exit::before {
    background: linear-gradient(90deg, transparent, rgba(255,51,102,0.15), transparent) !important;
}

.btn-exit:hover {
    background: #ff3366 !important;
    color: #000 !important;
    box-shadow: 0 0 16px #ff3366 !important;
    opacity: 1;
}

/* ============================================================
   HOW TO PLAY SCREEN
============================================================ */
#howtoplay-screen {
    /* Fill #screens completely so scroll works against a fixed boundary */
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    max-height: none;
    overflow-y: auto;
    overflow-x: hidden;
    scrollbar-width: thin;
    scrollbar-color: #00ffcc44 transparent;
    padding: 2% 5% 3%;
    box-sizing: border-box;
    /* Override the .screen centering — start from top so button is reachable */
    justify-content: flex-start;
    align-items: center;
}

#howtoplay-screen::-webkit-scrollbar {
    width: 4px;
}
#howtoplay-screen::-webkit-scrollbar-thumb {
    background: rgba(0,255,204,0.3);
    border-radius: 2px;
}

#howtoplay-screen h1 {
    font-size: 3.5vw;
    margin: 0 0 14px;
    flex-shrink: 0;
}

.htp-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 2vw;
    width: 100%;
    text-align: left;
}

.htp-col {
    display: flex;
    flex-direction: column;
    gap: 1vw;
}

.htp-section {
    background: rgba(0, 255, 204, 0.04);
    border: 1px solid rgba(0, 255, 204, 0.18);
    border-radius: 4px;
    padding: 0.9vw 1.2vw;
}

.htp-section h3 {
    font-size: 1.1vw;
    color: #00ffcc;
    text-shadow: 0 0 8px rgba(0,255,204,0.5);
    margin: 0 0 0.6vw;
    letter-spacing: 0.18em;
    font-family: inherit;
}

.htp-section p {
    font-size: 1vw;
    color: #aaffea;
    line-height: 1.7;
    margin: 0;
    max-width: 100%;
}

.htp-list {
    color: rgba(170,255,234,0.65) !important;
    font-size: 0.85vw !important;
    letter-spacing: 0.05em;
    margin-top: 0.5vw !important;
}

.key {
    display: inline-block;
    background: rgba(0,255,204,0.1);
    border: 1px solid rgba(0,255,204,0.5);
    border-radius: 3px;
    padding: 1px 6px;
    font-size: 0.9em;
    color: #00ffcc;
    letter-spacing: 0.05em;
}

.key-touch {
    display: inline-block;
    background: rgba(255,215,0,0.1);
    border: 1px solid rgba(255,215,0,0.5);
    border-radius: 3px;
    padding: 1px 6px;
    font-size: 0.9em;
    color: #ffd700;
    letter-spacing: 0.05em;
}

#btn-htp-back {
    font-size: 1.4vw;
    padding: 10px 36px;
    margin: 1.5vw auto 0;
    flex-shrink: 0;
}

@media (pointer: coarse) {
    .htp-grid {
        grid-template-columns: 1fr;
    }
    .htp-section h3 {
        font-size: max(12px, 2.5vw);
    }
    .htp-section p {
        font-size: max(11px, 2.2vw);
    }
    .htp-list {
        font-size: max(10px, 1.8vw) !important;
    }
    .btn-secondary {
        font-size: max(14px, 2.8vw) !important;
    }
    .btn-exit {
        font-size: max(12px, 2.4vw) !important;
    }
    #btn-htp-back {
        font-size: max(14px, 3vw);
    }
}

#puzzle-overlay {
    position: absolute;
    top: 0; left: 0; width: 100%; height: 100%;
    background: rgba(0, 0, 0, 0.85);
    display: none;
    justify-content: center;
    align-items: center;
    z-index: 30;
    pointer-events: auto;
}

#puzzle-overlay.active {
    display: flex;
}

#puzzle-canvas {
    width: 800px;
    height: 600px;
    border: 2px solid #00ffcc;
    box-shadow: 0 0 30px rgba(0, 255, 204, 0.3);
    background: #050508;
}

#puzzle-close {
    position: absolute;
    top: 20px;
    right: 30px;
    font-size: 2vw;
    color: #ff3366;
    border-color: #ff3366;
    padding: 8px 20px;
    margin: 0;
}

#puzzle-close:hover {
    background: #ff3366;
    color: #000;
    box-shadow: 0 0 20px #ff3366;
}

#puzzle-timer-bar {
    position: absolute;
    top: 0; left: 0;
    height: 4px;
    background: #ff3366;
    transition: width 0.1s linear;
    box-shadow: 0 0 8px #ff3366;
    z-index: 35;
}

#note-overlay {
    position: absolute;
    top: 50%; left: 50%;
    transform: translate(-50%, -50%);
    background: rgba(5, 5, 8, 0.97);
    border: 1px solid #00ffcc;
    padding: 40px 50px;
    max-width: 500px;
    text-align: center;
    z-index: 40;
    display: none;
    pointer-events: auto;
    box-shadow: 0 0 40px rgba(0, 255, 204, 0.2);
}

#note-overlay.active {
    display: block;
}

#note-text {
    font-size: 1.4vw;
    color: #aaffea;
    line-height: 1.9;
    margin-bottom: 20px;
    white-space: pre-line;
}

#note-close {
    font-size: 1.2vw;
    padding: 8px 24px;
    margin: 0 auto;
}

/* ============================================================
   TOUCH CONTROLS — virtual joystick + interact button
   Shown only on coarse-pointer (touch) devices via media query.
   JS further controls display based on game state.
============================================================ */
#touch-controls {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    height: 38%;
    pointer-events: none;
    z-index: 45;
    display: none;
}

@media (pointer: coarse) {
    body.in-game #touch-controls {
        display: block;
    }
    #touch-hint {
        display: inline !important;
    }
}

#joystick-zone {
    position: absolute;
    bottom: 14px;
    left: 14px;
    width: 130px;
    height: 130px;
    pointer-events: auto;
    touch-action: none;
}

#joystick-base {
    position: absolute;
    inset: 0;
    background: rgba(0, 255, 204, 0.07);
    border: 2px solid rgba(0, 255, 204, 0.35);
    border-radius: 50%;
}

#joystick-knob {
    width: 52px;
    height: 52px;
    background: rgba(0, 255, 204, 0.32);
    border: 2px solid #00ffcc;
    border-radius: 50%;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    box-shadow: 0 0 14px rgba(0, 255, 204, 0.5);
    pointer-events: none;
    transition: box-shadow 0.1s;
}

#btn-interact {
    position: absolute;
    bottom: 20px;
    right: 20px;
    width: 90px;
    height: 90px;
    border-radius: 50%;
    background: rgba(0, 255, 204, 0.1);
    border: 2px solid #00ffcc;
    color: #00ffcc;
    font-size: 13px;
    font-family: inherit;
    letter-spacing: 0.12em;
    text-transform: uppercase;
    pointer-events: auto;
    touch-action: manipulation;
    box-shadow: 0 0 12px rgba(0, 255, 204, 0.25);
    padding: 0;
    margin: 0;
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
}

#btn-interact:active {
    background: rgba(0, 255, 204, 0.28);
    box-shadow: 0 0 24px rgba(0, 255, 204, 0.55);
}

/* Scale puzzle canvas to fit mobile screens */
@media (pointer: coarse) {
    #puzzle-canvas {
        width: min(800px, 90vw) !important;
        height: min(600px, 67.5vw) !important; /* maintains 4:3 ratio */
    }

    #puzzle-close {
        font-size: max(14px, 3.5vw);
        padding: 10px 22px;
        top: 10px;
        right: 14px;
    }

    #note-overlay {
        max-width: 88vw;
        padding: 24px 26px;
    }

    #note-text {
        font-size: max(12px, 3vw);
    }

    #note-close {
        font-size: max(12px, 3.5vw);
        padding: 10px 28px;
    }
}

canvas, #joystick-zone, #btn-interact {
    -webkit-tap-highlight-color: transparent;
}

#crt-overlay {
    position: absolute;
    top: 0; left: 0;
    width: 100%; height: 100%;
    pointer-events: none;
    z-index: 999;
    background:
        repeating-linear-gradient(
            0deg,
            transparent,
            transparent 2px,
            rgba(0, 0, 0, 0.03) 2px,
            rgba(0, 0, 0, 0.03) 4px
        ),
        repeating-linear-gradient(
            90deg,
            rgba(255, 0, 0, 0.015) 0px,
            rgba(0, 255, 0, 0.015) 2px,
            rgba(0, 0, 255, 0.015) 4px
        );
}
