*, *::before, *::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

:root {
    --bg: #0f0f1a;
    --bg-light: #1a1a2e;
    --bg-mid: #16213e;
    --accent: #ff6b35;
    --accent-hover: #e85d2c;
    --p1: #e74c3c;
    --p2: #3498db;
    --text: #ecf0f1;
    --text-dim: #7f8c8d;
    --success: #2ecc71;
    --danger: #e74c3c;
}

html, body {
    touch-action: none;
    overflow: hidden;
    width: 100%;
    height: 100%;
}

body {
    font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
    background: var(--bg);
    color: var(--text);
    min-height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
}

/* Screens */
.screen { display: none; width: 100%; }
.screen.active { display: flex; justify-content: center; align-items: center; min-height: 100vh; }

/* Lobby */
.lobby-container {
    text-align: center;
    padding: 2rem;
    max-width: 500px;
}

.lobby-container h1 {
    font-size: 3rem;
    margin-bottom: 0.25rem;
    letter-spacing: 2px;
}

.subtitle {
    color: var(--text-dim);
    margin-bottom: 2.5rem;
    font-size: 1.1rem;
}

.lobby-buttons {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 1rem;
}

.divider {
    color: var(--text-dim);
    font-size: 0.9rem;
    text-transform: uppercase;
    letter-spacing: 2px;
}

.join-form {
    display: flex;
    gap: 0.5rem;
    align-items: center;
}

.join-form input {
    padding: 0.75rem 1rem;
    font-size: 1.1rem;
    text-transform: uppercase;
    letter-spacing: 3px;
    text-align: center;
    background: var(--bg-light);
    border: 2px solid var(--bg-mid);
    color: var(--text);
    border-radius: 8px;
    width: 180px;
    outline: none;
    transition: border-color 0.2s;
}

.join-form input:focus {
    border-color: var(--accent);
}

.how-to-play {
    margin-top: 2.5rem;
    padding: 1.25rem;
    background: var(--bg-light);
    border-radius: 12px;
    text-align: left;
}

.how-to-play h3 {
    font-size: 1rem;
    margin-bottom: 0.5rem;
    color: var(--accent);
}

.how-to-play p {
    font-size: 0.85rem;
    color: var(--text-dim);
    line-height: 1.5;
}

/* Buttons */
.btn {
    padding: 0.75rem 2rem;
    font-size: 1rem;
    font-weight: 600;
    border: none;
    border-radius: 8px;
    cursor: pointer;
    transition: all 0.2s;
    letter-spacing: 0.5px;
}

.btn-primary {
    background: var(--accent);
    color: var(--bg);
}

.btn-primary:hover {
    background: var(--accent-hover);
    transform: translateY(-1px);
}

.btn-secondary {
    background: var(--bg-mid);
    color: var(--text);
}

.btn-secondary:hover {
    background: #1e3a5f;
}

.btn-small {
    padding: 0.5rem 1rem;
    font-size: 0.85rem;
}

.hidden { display: none !important; }

/* Error */
.error-message {
    color: var(--danger);
    margin-top: 1rem;
    font-size: 0.9rem;
}

/* Waiting */
.waiting-container {
    text-align: center;
    padding: 2rem;
}

.game-code-display {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.75rem;
    margin: 1.5rem 0;
}

.game-code-display span {
    font-size: 2.5rem;
    font-weight: 700;
    letter-spacing: 8px;
    color: var(--accent);
    background: var(--bg-light);
    padding: 0.75rem 1.5rem;
    border-radius: 12px;
    border: 2px solid var(--bg-mid);
    font-family: 'Consolas', 'Courier New', monospace;
}

.spinner {
    width: 40px;
    height: 40px;
    border: 4px solid var(--bg-mid);
    border-top-color: var(--accent);
    border-radius: 50%;
    animation: spin 1s linear infinite;
    margin: 2rem auto 0;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

/* Game Screen */
#screen-game {
    position: relative;
    width: 100vw;
    height: 100vh;
    overflow: hidden;
}

#screen-game.active {
    min-height: unset;
}

#game-canvas {
    width: 100%;
    height: 100%;
    display: block;
}

/* Touch D-pad */
.touch-controls {
    position: fixed;
    bottom: max(24px, env(safe-area-inset-bottom, 0px) + 12px);
    right: 24px;
    display: grid;
    grid-template-areas:
        ". up ."
        "left . right"
        ". down .";
    grid-template-columns: 56px 56px 56px;
    grid-template-rows: 56px 56px 56px;
    gap: 4px;
    z-index: 10;
    pointer-events: none;
}

.dpad-btn {
    pointer-events: auto;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(255, 255, 255, 0.12);
    border: 2px solid rgba(255, 255, 255, 0.2);
    color: rgba(255, 255, 255, 0.7);
    font-size: 1.3rem;
    border-radius: 14px;
    cursor: pointer;
    user-select: none;
    -webkit-user-select: none;
    touch-action: manipulation;
    transition: background 0.1s, border-color 0.1s;
}

.dpad-up { grid-area: up; }
.dpad-left { grid-area: left; }
.dpad-right { grid-area: right; }
.dpad-down { grid-area: down; }

.dpad-btn.active,
.dpad-btn:active {
    background: rgba(255, 255, 255, 0.3);
    border-color: rgba(255, 255, 255, 0.5);
    color: #fff;
}

/* Leave button (floating) */
.btn-leave-float {
    position: fixed;
    top: 12px;
    left: 12px;
    z-index: 10;
    padding: 6px 14px;
    font-size: 0.8rem;
    font-weight: 600;
    background: rgba(255, 255, 255, 0.1);
    border: 1px solid rgba(255, 255, 255, 0.2);
    color: rgba(255, 255, 255, 0.6);
    border-radius: 8px;
    cursor: pointer;
    transition: background 0.15s;
}

.btn-leave-float:hover {
    background: rgba(255, 255, 255, 0.2);
    color: #fff;
}

.btn-leave {
    margin-top: 1.5rem;
}

/* Game Over */
.gameover-container {
    text-align: center;
    padding: 2rem;
}

.gameover-container h2 {
    font-size: 2.5rem;
    margin-bottom: 1rem;
}

.gameover-container .win { color: var(--success); }
.gameover-container .lose { color: var(--danger); }
.gameover-container .draw { color: var(--accent); }

.gameover-container p {
    color: var(--text-dim);
    margin-bottom: 2rem;
    font-size: 1.05rem;
}

.gameover-buttons {
    display: flex;
    gap: 1rem;
    justify-content: center;
    flex-wrap: wrap;
}

.rematch-status {
    color: var(--accent);
    margin-top: 1.25rem;
    font-size: 0.95rem;
    animation: pulse-text 1.5s ease-in-out infinite;
}

@keyframes pulse-text {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.5; }
}

/* Responsive */
@media (max-width: 768px) {
    .lobby-container h1 { font-size: 2rem; }
    .game-code-display span { font-size: 1.8rem; letter-spacing: 5px; }

    .touch-controls {
        grid-template-columns: 50px 50px 50px;
        grid-template-rows: 50px 50px 50px;
    }
}
