*, *::before, *::after {
    box-sizing: border-box;
}

:root {
    --void: #08080d;
    --panel: #11121a;
    --line: #34364a;
    --text: #f1f4df;
    --muted: #9ba18b;
    --green: #7dff74;
    --amber: #ffd36a;
    --pink: #ff5ea8;
    --cyan: #64e8ff;
    --danger: #ff4d54;
}

html,
body {
    width: 100%;
    height: 100%;
    margin: 0;
    overflow: hidden;
    touch-action: none;
}

body {
    color: var(--text);
    background:
        radial-gradient(circle at 30% 20%, rgba(255, 94, 168, 0.12), transparent 28%),
        radial-gradient(circle at 78% 72%, rgba(100, 232, 255, 0.1), transparent 24%),
        linear-gradient(180deg, #08080d 0%, #101018 58%, #07070c 100%);
    font-family: "Segoe UI", system-ui, -apple-system, BlinkMacSystemFont, sans-serif;
}

body::before {
    content: "";
    position: fixed;
    inset: 0;
    pointer-events: none;
    background-image:
        linear-gradient(rgba(255, 255, 255, 0.025) 1px, transparent 1px),
        linear-gradient(90deg, rgba(255, 255, 255, 0.025) 1px, transparent 1px);
    background-size: 34px 34px;
    mask-image: linear-gradient(to bottom, transparent, black 12%, black 86%, transparent);
}

.screen {
    display: none;
    width: 100vw;
    height: 100vh;
}

.screen.active {
    display: flex;
    align-items: center;
    justify-content: center;
}

.menu-shell,
.waiting-shell,
.gameover-shell {
    width: min(92vw, 560px);
    padding: clamp(24px, 5vh, 52px);
    border: 1px solid rgba(255, 255, 255, 0.13);
    border-radius: 8px;
    background:
        linear-gradient(180deg, rgba(17, 18, 26, 0.94), rgba(10, 10, 16, 0.94)),
        repeating-linear-gradient(0deg, rgba(255, 255, 255, 0.02) 0 2px, transparent 2px 5px);
    box-shadow: 0 24px 90px rgba(0, 0, 0, 0.45);
    text-align: center;
}

.cabinet-mark,
.panel-label {
    margin: 0 0 14px;
    color: var(--amber);
    font-size: 0.78rem;
    font-weight: 800;
    letter-spacing: 0.22rem;
    text-transform: uppercase;
}

h1 {
    margin: 0 0 34px;
    color: var(--green);
    font-size: clamp(2.6rem, 12vw, 5.4rem);
    line-height: 0.92;
    letter-spacing: 0;
    text-transform: uppercase;
    text-shadow: 0 0 24px rgba(125, 255, 116, 0.4);
}

.menu-actions {
    display: grid;
    gap: 12px;
}

.join-row {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    gap: 10px;
}

input {
    min-width: 0;
    height: 50px;
    padding: 0 16px;
    border: 1px solid var(--line);
    border-radius: 6px;
    color: var(--text);
    background: #0b0c12;
    font: 800 1.08rem "Cascadia Mono", Consolas, monospace;
    letter-spacing: 0.2rem;
    text-align: center;
    text-transform: uppercase;
    outline: none;
}

input:focus {
    border-color: var(--cyan);
    box-shadow: 0 0 0 3px rgba(100, 232, 255, 0.16);
}

.btn,
.icon-btn,
.touch-btn {
    border: 0;
    border-radius: 6px;
    cursor: pointer;
    user-select: none;
    -webkit-user-select: none;
}

.btn {
    min-height: 50px;
    padding: 0 22px;
    color: var(--void);
    font-size: 1rem;
    font-weight: 850;
    letter-spacing: 0;
    text-transform: uppercase;
    transition: transform 0.12s ease, filter 0.12s ease, background 0.12s ease;
}

.btn:hover {
    transform: translateY(-1px);
    filter: brightness(1.08);
}

.btn:disabled {
    cursor: default;
    opacity: 0.55;
    transform: none;
}

.btn-primary {
    background: var(--green);
}

.btn-secondary {
    background: var(--cyan);
}

.btn-quiet {
    color: var(--text);
    background: #27202e;
}

.menu-readout {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 8px;
    margin-top: 24px;
    color: var(--muted);
    font: 700 0.78rem "Cascadia Mono", Consolas, monospace;
    text-transform: uppercase;
}

.menu-readout span {
    min-width: 0;
    padding: 8px 6px;
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: 6px;
    background: rgba(255, 255, 255, 0.03);
}

.hidden {
    display: none !important;
}

.error-message {
    min-height: 1.3rem;
    margin-top: 16px;
    color: var(--danger);
    font-weight: 700;
}

.game-code-display {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 12px;
    margin: 22px 0;
}

.game-code-display span {
    min-width: 0;
    padding: 12px 18px;
    border: 1px solid var(--line);
    border-radius: 8px;
    color: var(--green);
    background: #0b0c12;
    font: 900 clamp(1.7rem, 9vw, 3.2rem) "Cascadia Mono", Consolas, monospace;
    letter-spacing: clamp(0.18rem, 1.6vw, 0.55rem);
}

.icon-btn {
    width: 48px;
    height: 48px;
    color: var(--void);
    background: var(--amber);
    font-size: 1.4rem;
    font-weight: 900;
}

.signal-bars {
    display: flex;
    align-items: end;
    justify-content: center;
    gap: 6px;
    height: 46px;
    margin: 10px 0 24px;
}

.signal-bars span {
    width: 12px;
    background: var(--pink);
    animation: signal-pulse 0.9s infinite ease-in-out;
}

.signal-bars span:nth-child(1) { height: 15px; animation-delay: 0s; }
.signal-bars span:nth-child(2) { height: 24px; animation-delay: 0.08s; }
.signal-bars span:nth-child(3) { height: 34px; animation-delay: 0.16s; }
.signal-bars span:nth-child(4) { height: 43px; animation-delay: 0.24s; }

@keyframes signal-pulse {
    0%, 100% { opacity: 0.35; transform: scaleY(0.72); }
    50% { opacity: 1; transform: scaleY(1); }
}

#screen-game {
    position: relative;
    overflow: hidden;
    background: #050509;
}

#screen-game.active {
    display: block;
}

#game-canvas {
    display: block;
    width: 100vw;
    height: 100vh;
}

.btn-leave-float {
    position: fixed;
    top: max(12px, env(safe-area-inset-top, 0px) + 8px);
    left: 12px;
    z-index: 4;
    min-height: 36px;
    padding: 0 14px;
    color: rgba(255, 255, 255, 0.72);
    background: rgba(255, 255, 255, 0.08);
    border: 1px solid rgba(255, 255, 255, 0.16);
    font-size: 0.78rem;
}

.touch-controls {
    position: fixed;
    right: max(18px, env(safe-area-inset-right, 0px) + 12px);
    bottom: max(18px, env(safe-area-inset-bottom, 0px) + 12px);
    z-index: 5;
    display: grid;
    grid-template-columns: repeat(3, 58px);
    gap: 8px;
    pointer-events: none;
}

.touch-btn {
    pointer-events: auto;
    width: 58px;
    height: 58px;
    border: 1px solid rgba(255, 255, 255, 0.22);
    color: rgba(255, 255, 255, 0.8);
    background: rgba(255, 255, 255, 0.1);
    font-size: 2rem;
    font-weight: 900;
    line-height: 1;
}

.touch-fire {
    color: var(--void);
    background: rgba(255, 211, 106, 0.92);
}

.touch-btn.active,
.touch-btn:active {
    color: #fff;
    background: rgba(125, 255, 116, 0.32);
}

.gameover-shell h2 {
    margin: 0 0 14px;
    color: var(--pink);
    font-size: clamp(2rem, 9vw, 4rem);
    line-height: 1;
    letter-spacing: 0;
    text-transform: uppercase;
}

#gameover-message {
    min-height: 3rem;
    margin: 0 0 22px;
    color: var(--muted);
    font: 700 1rem "Cascadia Mono", Consolas, monospace;
    line-height: 1.55;
}

.gameover-actions {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 12px;
}

.rematch-status {
    margin: 18px 0 0;
    color: var(--amber);
    font-weight: 800;
}

@media (max-width: 640px) {
    .menu-shell,
    .waiting-shell,
    .gameover-shell {
        padding: 22px;
    }

    .menu-readout {
        grid-template-columns: 1fr;
    }

    .join-row {
        grid-template-columns: 1fr;
    }

    .touch-controls {
        grid-template-columns: repeat(3, 52px);
    }

    .touch-btn {
        width: 52px;
        height: 52px;
    }
}
