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

:root {
    --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);
    --edge-gap: 12px;
}

html,
body {
    width: 100%;
    height: 100%;
    background: #111;
    overflow: hidden;
    touch-action: none;
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    user-select: none;
}

body {
    display: flex;
    justify-content: center;
    align-items: center;
}

#game-container {
    position: relative;
    width: 100vw;
    height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    overflow: hidden;
    padding: var(--safe-top) var(--safe-right) var(--safe-bottom) var(--safe-left);
    background: radial-gradient(circle at center, #1a1a2e 0%, #090914 100%);
}

@media (max-width: 960px), (max-height: 720px) {
    #game-container {
        align-items: flex-end;
    }
}

#game {
    display: block;
    width: min(calc(100vw - var(--safe-left) - var(--safe-right)), calc((100vh - var(--safe-top) - var(--safe-bottom)) * 4 / 3), 960px);
    height: min(calc(100vh - var(--safe-top) - var(--safe-bottom)), calc((100vw - var(--safe-left) - var(--safe-right)) * 3 / 4), 720px);
    image-rendering: pixelated;
    image-rendering: crisp-edges;
    touch-action: none;
}

.mobile-panel {
    display: none;
}

.touch-device #game-container {
    flex-direction: column;
    justify-content: flex-end;
    align-items: center;
    gap: 12px;
    padding-top: max(var(--safe-top), 16px);
    padding-bottom: max(var(--safe-bottom), 16px);
}

.touch-device #game {
    width: min(calc(100vw - var(--safe-left) - var(--safe-right) - 16px), 520px);
    height: auto;
    aspect-ratio: 4 / 3;
    max-height: min(50vh, 720px);
}

.touch-device .mobile-panel {
    display: flex;
    width: min(calc(100vw - var(--safe-left) - var(--safe-right) - 16px), 520px);
    flex-direction: column;
    gap: 10px;
    padding: 0 4px max(var(--safe-bottom), 8px);
}

.mobile-hint {
    color: rgba(255, 255, 255, 0.72);
    font: 11px/1.3 monospace;
    text-align: center;
}

.mobile-controls {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 14px;
}

.mobile-joystick-zone {
    position: relative;
    width: 132px;
    height: 132px;
    flex: 0 0 132px;
    border-radius: 999px;
    border: 2px solid rgba(255, 255, 255, 0.12);
    background: rgba(255, 255, 255, 0.04);
    touch-action: none;
}

.mobile-joystick-base {
    position: absolute;
    inset: 16px;
    border-radius: 999px;
    border: 2px solid rgba(255, 255, 255, 0.06);
}

.mobile-joystick-knob {
    position: absolute;
    left: 50%;
    top: 50%;
    width: 40px;
    height: 40px;
    transform: translate(-50%, -50%);
    border-radius: 999px;
    background: rgba(241, 196, 15, 0.22);
    border: 2px solid rgba(241, 196, 15, 0.35);
}

.mobile-action-cluster {
    display: grid;
    grid-template-columns: repeat(2, minmax(68px, 1fr));
    gap: 10px;
    flex: 1 1 auto;
}

.mobile-action-btn {
    min-height: 68px;
    border-radius: 16px;
    background: rgba(19, 24, 45, 0.78);
    border: 2px solid rgba(241, 196, 15, 0.28);
    color: #fff;
    font: 11px monospace;
    display: flex;
    align-items: center;
    justify-content: center;
    touch-action: none;
}

.mobile-action-btn:active {
    transform: scale(0.98);
    background: rgba(42, 52, 92, 0.92);
}

@media (pointer: fine) {
    .mobile-panel {
        display: none !important;
    }
}

@media (max-width: 960px) {
    canvas {
        width: 100vw;
        height: auto;
    }
}

@media (max-height: 720px) and (min-width: 961px) {
    canvas {
        height: 100vh;
        width: auto;
    }
}
