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

body {
    background: #0d0806;
    font-family: 'Courier New', monospace;
    overflow: hidden;
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 100vh;
}

#frame-wrapper {
    display: flex;
    justify-content: center;
    align-items: center;
    width: 100%;
    height: 100vh;
    padding: 0;
    overflow: hidden;
}

#frame-scene {
    position: relative;
    /* Responsive frame: use most of the viewport, capped at 1200px.
       On mobile (portrait), the height is the constraint, so we also
       cap at 100vh via the aspect-ratio + max-height. */
    width: min(96vw, 96vh * 1.5, 1200px);
    aspect-ratio: 3 / 2;
    overflow: hidden;
    flex-shrink: 0;
    /* Smooth zoom uses transform so everything scales uniformly */
    transition: transform 8s cubic-bezier(0.25, 0.1, 0.25, 1);
    transform: scale(1);
    /* Computed so screen center lands at viewport center at scale(2.69) */
    transform-origin: 45.1% 28.6%;
}

/* Zoomed-in state: scale up so the monitor screen nearly fills the viewport.
   Only ~3px of monitor frame visible at top/bottom. */
#frame-scene.zoomed {
    transform: scale(2.69);
}

#frame-art {
    width: 100%;
    height: 100%;
    object-fit: contain;
    display: block;
    image-rendering: pixelated;
    position: relative;
    z-index: 2;
    pointer-events: none;
    transform: scale(1.08);
    transform-origin: center;
    transition: opacity 1.5s ease;
}

/* Frame fading out before fullscreen switch */
#frame-art.fading {
    opacity: 0;
}

#frame-screen {
    position: absolute;
    left: var(--screen-x, 30%);
    left: var(--screen-x, calc(30% - 17px));
    top: var(--screen-y, calc(25% - 29px));
    width: var(--screen-w, 42%);
    height: var(--screen-h, 31.2%);
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
    border-radius: 12px;
    z-index: 1;
    transition: border-radius 1s ease;
}

#screen-overlay {
    position: absolute;
    inset: 0;
    z-index: 5;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
    pointer-events: auto;
}

#game-container {
    position: relative;
    display: inline-block;
    width: 100%;
    height: 100%;
    overflow: hidden;
}

#game-canvas {
    display: block;
    border: none;
    width: 100%;
    height: 100%;
    /* Pixel-perfect rendering */
    image-rendering: -moz-crisp-edges;
    image-rendering: -webkit-crisp-edges;
    image-rendering: pixelated;
    image-rendering: crisp-edges;
}

#debug-info {
    position: absolute;
    top: 10px;
    left: 10px;
    background: rgba(0, 0, 0, 0.7);
    color: #0f0;
    padding: 10px;
    font-size: 12px;
    border: 1px solid #0f0;
    font-family: 'Courier New', monospace;
    z-index: 1000;
}

#debug-info.hidden {
    display: none;
}

#debug-info div {
    margin-bottom: 5px;
}

/* Character Builder Modal */
.character-builder {
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background: #2a2a2a;
    border: 4px solid #555;
    padding: 30px;
    z-index: 2000;
    color: #fff;
    min-width: 400px;
}

.character-builder h2 {
    margin-bottom: 20px;
    text-align: center;
    color: #f0f0f0;
}

.character-builder label {
    display: block;
    margin-top: 15px;
    margin-bottom: 5px;
    font-weight: bold;
}

.character-builder input[type="text"],
.character-builder select {
    width: 100%;
    padding: 8px;
    font-size: 14px;
    border: 2px solid #555;
    background: #1a1a1a;
    color: #fff;
}

.character-builder input[type="color"] {
    width: 60px;
    height: 30px;
    border: 2px solid #555;
    cursor: pointer;
}

.character-builder .checkbox-group {
    display: flex;
    gap: 15px;
    margin-top: 10px;
}

.character-builder .checkbox-group label {
    display: flex;
    align-items: center;
    gap: 5px;
    margin: 0;
}

.character-builder button {
    width: 100%;
    margin-top: 20px;
    padding: 12px;
    font-size: 16px;
    font-weight: bold;
    background: #4CAF50;
    color: white;
    border: none;
    cursor: pointer;
    transition: background 0.3s;
}

.character-builder button:hover {
    background: #45a049;
}

.character-preview {
    width: 100%;
    height: 120px;
    background: #1a1a1a;
    border: 2px solid #555;
    margin-top: 15px;
    display: flex;
    justify-content: center;
    align-items: center;
}

.character-preview canvas {
    image-rendering: pixelated;
    image-rendering: crisp-edges;
}

/* Dialog Box */
.dialog-box {
    position: absolute;
    bottom: 20px;
    left: 50%;
    transform: translateX(-50%);
    width: 90%;
    max-width: 600px;
    background: #fff;
    border: 4px solid #000;
    padding: 20px;
    font-family: 'Courier New', monospace;
    font-size: 16px;
    z-index: 100;
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.5);
}

.dialog-box .speaker-name {
    font-weight: bold;
    margin-bottom: 10px;
    color: #c03030;
}

.dialog-box .dialog-text {
    line-height: 1.5;
    color: #000;
}

.dialog-box .continue-arrow {
    position: absolute;
    bottom: 5px;
    right: 10px;
    font-size: 20px;
    animation: blink 1s infinite;
}

@keyframes blink {
    0%, 49% { opacity: 1; }
    50%, 100% { opacity: 0; }
}

/* HUD */
.hud {
    position: absolute;
    top: 10px;
    right: 10px;
    background: rgba(0, 0, 0, 0.7);
    color: #fff;
    padding: 10px 15px;
    border: 2px solid #555;
    font-size: 14px;
    z-index: 50;
}

.hud .player-name {
    font-weight: bold;
    margin-bottom: 5px;
}

/* ─── Top-right HUD panel (music + online status) ─── */
#hud-panel {
    position: fixed;
    top: 10px;
    right: 10px;
    display: flex;
    flex-direction: column;
    gap: 0;
    z-index: 9999;
    pointer-events: auto;
    min-width: 120px;
}

#hud-panel > * {
    background: rgba(13, 8, 6, 0.85);
    font-family: 'Courier New', monospace;
    font-size: 11px;
    color: #e8d5cc;
    padding: 6px 10px;
    border: 1px solid rgba(196, 58, 36, 0.4);
    letter-spacing: 0.5px;
}

/* First child gets rounded top corners */
#hud-panel > *:first-child {
    border-radius: 4px 4px 0 0;
}
/* Last child gets rounded bottom corners */
#hud-panel > *:last-child {
    border-radius: 0 0 4px 4px;
}
/* Only child gets all corners */
#hud-panel > *:only-child {
    border-radius: 4px;
}
/* Collapse shared borders */
#hud-panel > * + * {
    border-top: none;
}

/* Music toggle button */
.music-hint {
    cursor: pointer;
    text-align: center;
    user-select: none;
    -webkit-user-select: none;
    -webkit-tap-highlight-color: transparent;
    transition: background 0.15s;
    color: #c43a24;
}

.music-hint:active {
    background: rgba(196, 58, 36, 0.15);
}

.music-hint:hover {
    background: rgba(196, 58, 36, 0.1);
}

.music-hint kbd {
    background: rgba(196, 58, 36, 0.15);
    border: 1px solid rgba(196, 58, 36, 0.4);
    border-radius: 2px;
    padding: 1px 5px;
    margin-right: 4px;
    font-weight: bold;
    font-size: 10px;
    color: #c43a24;
}

/* Controls hint — inside HUD panel, hidden until gameplay */
#controls-hint {
    text-align: center;
    cursor: pointer;
    color: #8a7068;
    display: none; /* shown via JS when game is active */
}

#controls-hint kbd {
    background: rgba(138, 64, 48, 0.15);
    border: 1px solid rgba(138, 64, 48, 0.3);
    border-radius: 2px;
    padding: 0px 4px;
    margin-right: 3px;
    font-weight: bold;
    font-size: 9px;
    color: #8a7068;
}

/* Player count row(s) — hidden until populated */
#player-count {
    text-align: center;
    pointer-events: none;
    font-size: 10px;
    line-height: 1.5;
}

#player-count:empty {
    display: none;
}

/* Fullscreen mode */
body.fullscreen-mode {
    background: #0d0806;
}

body.fullscreen-mode #frame-wrapper {
    padding: 0;
}

body.fullscreen-mode #frame-scene {
    width: 100vw;
    height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    aspect-ratio: unset;
}

body.fullscreen-mode #frame-art {
    display: none;
}

body.fullscreen-mode #frame-screen {
    position: relative;
    left: unset !important;
    top: unset !important;
    /* Maintain 5:4 aspect ratio (640x512), fit within viewport */
    width: min(100vw, calc(100vh * 1.25)) !important;
    height: min(100vh, calc(100vw * 0.8)) !important;
    aspect-ratio: 5 / 4;
    border-radius: 0;
}

body.fullscreen-mode #hud-panel {
    top: 10px;
    right: 10px;
}

/* ─── Mobile overrides (portrait phones) ─── */
@media (max-width: 768px) and (orientation: portrait) {
    /* Fill width, use the natural height — game centered in viewport */
    body.fullscreen-mode {
        background: #0d0806;
    }

    body.fullscreen-mode #frame-scene {
        /* Center the game vertically in the viewport */
        align-items: flex-start;
        padding-top: env(safe-area-inset-top, 0px);
    }

    body.fullscreen-mode #frame-screen {
        width: 100vw !important;
        /* Keep 5:4 aspect — game fills width, natural height */
        height: calc(100vw * 0.8) !important;
        aspect-ratio: 5 / 4;
    }

    /* Shrink HUD panel for mobile */
    #hud-panel {
        top: 6px;
        right: 6px;
        min-width: 90px;
    }

    #hud-panel > * {
        font-size: 9px;
        padding: 4px 8px;
    }

    .music-hint kbd {
        padding: 1px 3px;
        margin-right: 2px;
        font-size: 8px;
    }

    /* Hide keyboard controls hint on mobile */
    #controls-hint {
        display: none !important;
    }

    /* Character builder — fit on small screens */
    .character-builder {
        min-width: unset !important;
        width: 90vw;
        padding: 16px;
        max-height: 90vh;
        overflow-y: auto;
    }
}

/* Landscape phones — game fills the viewport */
@media (max-width: 920px) and (orientation: landscape) {
    body.fullscreen-mode #frame-screen {
        width: 100vw !important;
        height: 100vh !important;
        aspect-ratio: unset;
    }
}
