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

html, body {
    overflow-x: hidden;
}

body {
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    min-height: 100dvh;
    min-height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 20px;
    -webkit-tap-highlight-color: transparent;
}

.container {
    max-width: 600px;
    width: 100%;
}

header {
    text-align: center;
    color: white;
    margin-bottom: 30px;
}

header h1 {
    font-size: 3rem;
    margin-bottom: 10px;
    text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.3);
}

.subtitle {
    font-size: 1.2rem;
    opacity: 0.9;
}

.game-wrapper {
    background: white;
    border-radius: 20px;
    padding: 30px;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
}

.score-board {
    display: flex;
    justify-content: space-around;
    margin-bottom: 20px;
    padding: 15px;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    border-radius: 10px;
    color: white;
}

.score-item {
    text-align: center;
}

.score-item .label {
    display: block;
    font-size: 0.9rem;
    opacity: 0.9;
    margin-bottom: 5px;
}

.score-item .value {
    display: block;
    font-size: 2rem;
    font-weight: bold;
}

#gameCanvas {
    display: block;
    margin: 0 auto;
    border: 3px solid #667eea;
    border-radius: 10px;
    background: #f8f9fa;
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
    max-width: 100%;
    height: auto;
    touch-action: none;
    aspect-ratio: 1 / 1;
}

.controls {
    display: flex;
    gap: 10px;
    justify-content: center;
    margin: 20px 0;
}

.btn {
    padding: 12px 24px;
    font-size: 1rem;
    font-weight: 600;
    border: none;
    border-radius: 8px;
    cursor: pointer;
    transition: all 0.3s ease;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.btn-primary {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
}

.btn-primary:hover:not(:disabled) {
    transform: translateY(-2px);
    box-shadow: 0 6px 12px rgba(102, 126, 234, 0.4);
}

.btn-secondary {
    background: #6c757d;
    color: white;
}

.btn-secondary:hover:not(:disabled) {
    background: #5a6268;
    transform: translateY(-2px);
}

.btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.instructions {
    margin-top: 20px;
    padding: 20px;
    background: #f8f9fa;
    border-radius: 10px;
    border-left: 4px solid #667eea;
}

.instructions h3 {
    color: #667eea;
    margin-bottom: 15px;
    font-size: 1.3rem;
}

.instructions ul {
    list-style: none;
    padding: 0;
}

.instructions li {
    padding: 8px 0;
    color: #495057;
    font-size: 0.95rem;
}

.instructions strong {
    color: #667eea;
}

.modal {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.7);
    justify-content: center;
    align-items: center;
    z-index: 1000;
}

.modal.show {
    display: flex;
}

.modal-content {
    background: white;
    padding: 40px;
    border-radius: 20px;
    text-align: center;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
    animation: slideIn 0.3s ease;
}

@keyframes slideIn {
    from {
        transform: translateY(-50px);
        opacity: 0;
    }
    to {
        transform: translateY(0);
        opacity: 1;
    }
}

.modal-content h2 {
    color: #667eea;
    font-size: 2.5rem;
    margin-bottom: 20px;
}

.modal-content p {
    font-size: 1.3rem;
    margin: 15px 0;
    color: #495057;
}

#finalScore {
    color: #667eea;
    font-weight: bold;
    font-size: 2rem;
}

.new-high-score {
    color: #28a745;
    font-weight: bold;
    animation: pulse 1s infinite;
}

@keyframes pulse {
    0%, 100% {
        transform: scale(1);
    }
    50% {
        transform: scale(1.05);
    }
}

@media (max-width: 500px) {
    header h1 {
        font-size: 2rem;
    }
    
    #gameCanvas {
        width: 100%;
        height: auto;
    }
    
    .controls {
        flex-direction: column;
    }
    
    .btn {
        width: 100%;
    }
}

/* ── D-pad touch controls ─────────────────────── */
.dpad {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    grid-template-areas:
        ". up ."
        "left . right"
        ". down .";
    gap: 8px;
    width: 180px;
    max-width: 70%;
    margin: 16px auto 0;
    justify-content: center;
}

.dpad-btn {
    width: 56px;
    height: 56px;
    min-width: 48px;
    min-height: 48px;
    border: none;
    border-radius: 12px;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
    font-size: 1.4rem;
    cursor: pointer;
    user-select: none;
    -webkit-user-select: none;
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 4px 10px rgba(0, 0, 0, 0.25);
}

.dpad-btn:active {
    transform: scale(0.92);
}

.dpad-btn[data-dir="up"]    { grid-area: up; }
.dpad-btn[data-dir="left"]  { grid-area: left; }
.dpad-btn[data-dir="right"] { grid-area: right; }
.dpad-btn[data-dir="down"]  { grid-area: down; }

/* Only show the D-pad on touch-capable devices */
@media (hover: hover) and (pointer: fine) {
    .dpad {
        display: none;
    }
}

/* ── Safe-area + landscape guards ─────────────── */
@media (max-height: 520px) and (orientation: landscape) {
    body {
        padding: 10px;
    }
    header {
        margin-bottom: 10px;
    }
    .game-wrapper {
        padding: 12px;
    }
    .instructions,
    .dpad {
        display: none;
    }
    .score-board {
        margin-bottom: 8px;
    }
}

@media (max-height: 520px) {
    .modal-content {
        max-height: 90dvh;
        max-height: 90vh;
        overflow-y: auto;
        padding: 24px;
    }
}
