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

html, body {
    height: 100%;
    overflow: hidden;
}

body {
    background-color: #1a1a2e;
    color: #eee;
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    min-height: 100vh;
    min-height: 100dvh; /* Dynamic viewport height for mobile */
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 10px;
}

#game-container {
    display: flex;
    gap: 20px;
    padding: 20px;
    background-color: #16213e;
    border-radius: 10px;
    box-shadow: 0 0 20px rgba(0, 0, 0, 0.5);
    max-height: 100%;
    max-width: 100%;
}

#game-board {
    border: 3px solid #0f3460;
    border-radius: 5px;
    /* Canvas will be sized by JS, but constrain display size */
    max-height: 100%;
    width: auto;
}

#side-panel {
    display: flex;
    flex-direction: column;
    gap: 15px;
    min-width: 120px;
}

#score-container,
#level-container,
#next-piece-container,
#controls {
    background-color: #0f3460;
    padding: 12px;
    border-radius: 8px;
}

h2 {
    font-size: 0.9rem;
    color: #e94560;
    margin-bottom: 6px;
    text-transform: uppercase;
    letter-spacing: 1px;
}

#score,
#level {
    font-size: 1.3rem;
    font-weight: bold;
    color: #fff;
}

#next-piece {
    background-color: #111;
    border: 2px solid #0f3460;
    border-radius: 5px;
    max-width: 100%;
    height: auto;
}

#controls ul {
    list-style: none;
    font-size: 0.8rem;
}

#controls li {
    margin-bottom: 4px;
}

kbd {
    background-color: #1a1a2e;
    padding: 2px 5px;
    border-radius: 3px;
    border: 1px solid #333;
    font-family: monospace;
    font-size: 0.75rem;
}

/* Touch controls - hidden by default */
#touch-controls {
    display: none;
}

/* Show touch controls only on touch devices (set via JS) */
body.touch-device #touch-controls {
    display: flex;
    justify-content: space-between;
    position: fixed;
    bottom: 20px;
    left: 20px;
    right: 20px;
    z-index: 100;
    pointer-events: auto;
}


body.touch-device #controls {
    display: none;
}

body.touch-device #game-container {
    margin-bottom: 100px; /* Space for fixed controls on larger screens */
}

#touch-controls-left,
#touch-controls-center,
#touch-controls-right {
    display: flex;
    gap: 10px;
}

.touch-btn {
    width: 70px;
    height: 70px;
    font-size: 28px;
    border: none;
    border-radius: 15px;
    background: linear-gradient(145deg, #1a3a5c, #0f2844);
    color: #fff;
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.4), inset 0 1px 0 rgba(255, 255, 255, 0.1);
    cursor: pointer;
    user-select: none;
    -webkit-user-select: none;
    touch-action: manipulation;
    transition: transform 0.1s, box-shadow 0.1s;
}

.touch-btn:active {
    transform: scale(0.95);
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.4), inset 0 1px 0 rgba(255, 255, 255, 0.1);
    background: linear-gradient(145deg, #0f2844, #1a3a5c);
}


/* Tablet and small desktop */
@media (max-height: 700px) {
    #game-container {
        padding: 10px;
        gap: 15px;
    }

    #side-panel {
        gap: 10px;
        min-width: 100px;
    }

    #score-container,
    #level-container,
    #next-piece-container,
    #controls {
        padding: 8px;
    }

    h2 {
        font-size: 0.75rem;
        margin-bottom: 4px;
    }

    #score,
    #level {
        font-size: 1.1rem;
    }
}

/* Mobile - portrait */
@media (max-width: 500px) {
    body {
        padding: 5px;
        padding-top: 10px;
    }

    body.touch-device {
        padding-bottom: 70px;
        padding-top: 35px;
    }

    body.touch-device #touch-controls {
        bottom: 10px;
        left: 10px;
        right: 10px;
    }

    body.touch-device #game-container {
        margin-bottom: 70px;
    }

    #game-container {
        flex-direction: column;
        padding: 5px;
        gap: 5px;
        align-items: center;
    }

    #side-panel {
        flex-direction: row;
        flex-wrap: nowrap;
        justify-content: center;
        gap: 8px;
        min-width: auto;
        width: auto;
        order: -1;
        flex-shrink: 0;
    }

    #game-board {
        flex-shrink: 0;
    }

    #score-container,
    #level-container,
    #next-piece-container {
        padding: 3px 6px;
        flex: 0 1 auto;
        min-width: 50px;
        text-align: center;
    }

    #score-container h2,
    #level-container h2,
    #next-piece-container h2 {
        font-size: 0.5rem;
        margin-bottom: 1px;
    }

    #score,
    #level {
        font-size: 0.8rem;
    }

    #next-piece {
        width: 35px;
        height: 35px;
    }

    .touch-btn {
        width: 50px;
        height: 50px;
        font-size: 18px;
        border-radius: 10px;
    }

    .touch-btn-small {
        width: 34px;
        height: 34px;
        font-size: 12px;
    }

    #touch-controls-left,
    #touch-controls-right {
        gap: 5px;
    }
}

/* Very small phones */
@media (max-width: 360px) {
    .touch-btn {
        width: 45px;
        height: 45px;
        font-size: 16px;
    }

    .touch-btn-small {
        width: 30px;
        height: 30px;
        font-size: 10px;
    }

    #touch-controls-left,
    #touch-controls-right {
        gap: 4px;
    }

    #score-container,
    #level-container,
    #next-piece-container {
        padding: 2px 5px;
        min-width: 45px;
    }

    #next-piece {
        width: 30px;
        height: 30px;
    }
}

/* Landscape mode on mobile */
@media (max-height: 500px) and (orientation: landscape) {
    body {
        padding: 5px;
    }

    body.touch-device {
        padding-bottom: 80px;
    }

    #game-container {
        flex-direction: row;
        padding: 8px;
        gap: 10px;
    }

    #side-panel {
        flex-direction: column;
        gap: 5px;
        min-width: 80px;
    }

    #score-container,
    #level-container,
    #next-piece-container {
        padding: 5px 8px;
    }

    h2 {
        font-size: 0.6rem;
        margin-bottom: 2px;
    }

    #score,
    #level {
        font-size: 0.9rem;
    }

    .touch-btn {
        width: 50px;
        height: 50px;
        font-size: 20px;
    }

    .touch-btn-small {
        width: 40px;
        height: 40px;
        font-size: 14px;
    }

    #touch-controls {
        bottom: 10px;
    }
}
