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

body {
    background: #1a1a2e;
    color: #e0e0e0;
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    min-height: 100vh;
    display: flex;
    flex-direction: column;
    align-items: center;
    overflow-x: hidden;
    touch-action: manipulation;
    -webkit-touch-callout: none;
}

.back-link {
    position: absolute;
    top: 16px;
    left: 20px;
    color: #888;
    text-decoration: none;
    font-size: 14px;
    transition: color 0.2s;
    z-index: 10;
}

.back-link:hover {
    color: #fff;
}

.title {
    margin-top: 18px;
    margin-bottom: 10px;
    font-size: 32px;
    font-weight: 700;
    letter-spacing: 4px;
    text-transform: uppercase;
    color: #fff;
    text-shadow: 0 0 20px rgba(0, 240, 240, 0.4);
    text-align: center;
}

/* ---------- difficulty bar ---------- */

.difficulty-bar {
    display: flex;
    gap: 8px;
    justify-content: center;
    margin-bottom: 14px;
}

.diff-btn {
    padding: 7px 20px;
    font-size: 14px;
    font-weight: 600;
    border: 2px solid #2a2a4a;
    border-radius: 6px;
    background: rgba(255, 255, 255, 0.04);
    color: #aaa;
    cursor: pointer;
    transition: all 0.2s;
    user-select: none;
    -webkit-user-select: none;
}

.diff-btn:hover {
    border-color: #00f0f0;
    color: #fff;
}

.diff-btn.active {
    border-color: #00f0f0;
    background: rgba(0, 240, 240, 0.15);
    color: #00f0f0;
    box-shadow: 0 0 10px rgba(0, 240, 240, 0.2);
}

/* ---------- layout ---------- */

.game-wrapper {
    display: flex;
    flex-direction: column;
    align-items: center;
    max-width: 900px;
    width: 100%;
    padding: 0 16px;
}

.game-container {
    display: flex;
    gap: 28px;
    align-items: flex-start;
    justify-content: center;
}

.game-area {
    position: relative;
    flex-shrink: 0;
    touch-action: none;
}

#board {
    display: block;
    border: 2px solid #333;
    border-radius: 4px;
    background: #111;
    /* Scales with the viewport on all screen sizes;
       42 vh cap keeps it within the visible area even on tall monitors */
    width: min(70vmin, 42vh, 500px);
    height: min(140vmin, 84vh, 1000px);
    touch-action: none;
}

/* ---------- overlays ---------- */

.overlay {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(0, 0, 0, 0.78);
    border-radius: 4px;
    z-index: 5;
}

.overlay.hidden {
    display: none;
}

.overlay-content {
    text-align: center;
}

.overlay-content h2 {
    font-size: 30px;
    margin-bottom: 14px;
    color: #fff;
    text-shadow: 0 0 12px rgba(0, 240, 240, 0.5);
}

.overlay-content p {
    font-size: 16px;
    margin-bottom: 8px;
    color: #ccc;
}

.overlay-content strong {
    color: #00f0f0;
}

#play-again-btn {
    margin-top: 14px;
    padding: 10px 28px;
    font-size: 16px;
    border: none;
    border-radius: 6px;
    background: #00f0f0;
    color: #111;
    font-weight: 700;
    cursor: pointer;
    transition: background 0.2s, transform 0.1s;
}

#play-again-btn:hover {
    background: #00d4d4;
}

#play-again-btn:active {
    transform: scale(0.96);
}

/* ---------- info panel ---------- */

.info-panel {
    display: flex;
    flex-direction: column;
    gap: 16px;
    min-width: 140px;
}

.info-box {
    background: rgba(255, 255, 255, 0.04);
    border: 1px solid #2a2a4a;
    border-radius: 8px;
    padding: 14px 18px;
    text-align: center;
}

.info-box h3 {
    font-size: 12px;
    text-transform: uppercase;
    letter-spacing: 2px;
    color: #888;
    margin-bottom: 8px;
}

.info-box p {
    font-size: 26px;
    font-weight: 700;
    color: #fff;
    font-variant-numeric: tabular-nums;
}

#next-piece {
    display: block;
    margin: 0 auto;
    background: transparent;
}

/* ---------- touch controls ---------- */

.touch-controls {
    display: none;
    flex-direction: column;
    gap: 10px;
    align-items: center;
    margin-top: 16px;
    padding: 0 10px 20px;
    width: 100%;
    max-width: 400px;
}

.touch-row {
    display: flex;
    gap: 10px;
    justify-content: center;
}

.touch-btn {
    width: 72px;
    height: 64px;
    font-size: 26px;
    border: none;
    border-radius: 12px;
    background: rgba(255, 255, 255, 0.12);
    color: #fff;
    cursor: pointer;
    user-select: none;
    -webkit-user-select: none;
    touch-action: manipulation;
    transition: background 0.1s;
    display: flex;
    align-items: center;
    justify-content: center;
}

.touch-btn.wide {
    width: 120px;
    font-size: 20px;
}

.touch-btn:active,
.touch-btn.pressed {
    background: rgba(255, 255, 255, 0.3);
}

/* Show touch controls on touch devices (JS adds this class) */
body.has-touch .touch-controls {
    display: flex;
}

/* ---------- how to play ---------- */

.how-to-play {
    max-width: 500px;
    width: 90%;
    margin: 24px auto 40px;
    background: rgba(255, 255, 255, 0.04);
    border: 1px solid #2a2a4a;
    border-radius: 8px;
    overflow: hidden;
}

.how-to-play summary {
    padding: 14px 18px;
    font-size: 15px;
    font-weight: 600;
    color: #aaa;
    cursor: pointer;
    user-select: none;
    -webkit-user-select: none;
    transition: color 0.2s, background 0.2s;
    list-style: none;
}

.how-to-play summary::-webkit-details-marker {
    display: none;
}

.how-to-play summary::before {
    content: '\25B6  ';
    font-size: 11px;
    display: inline-block;
    transition: transform 0.2s;
}

.how-to-play[open] summary::before {
    transform: rotate(90deg);
}

.how-to-play summary:hover {
    color: #fff;
    background: rgba(255, 255, 255, 0.04);
}

.how-to-play-body {
    padding: 4px 18px 18px;
}

.how-to-play-body ul {
    list-style: disc;
    padding-left: 20px;
}

.how-to-play-body li {
    font-size: 14px;
    line-height: 1.7;
    color: #bbb;
}

.how-to-play-body strong {
    color: #00f0f0;
}

/* ---------- responsive ---------- */

/* ── Portrait mobile — everything fits on screen, no scrolling ── */
@media (max-width: 600px) {

    body.has-touch {
        overflow-x: hidden;
        padding-bottom: 135px; /* space to scroll above fixed touch controls */
    }

    /* Tight header */
    .title {
        font-size: 20px;
        margin-top: 6px;
        margin-bottom: 4px;
    }

    .difficulty-bar {
        margin-bottom: 6px;
    }

    .diff-btn {
        padding: 4px 12px;
        font-size: 12px;
    }

    .game-container {
        flex-direction: column;
        align-items: center;
        gap: 8px;
    }

    /* Stats above board */
    .game-area  { order: 2; }
    .info-panel { order: 1; }

    /* How to play above the game (order 0 = before game-wrapper which gets order 1) */
    .game-wrapper { order: 1; }
    .how-to-play  { order: 0; margin-top: 0; margin-bottom: 8px; }

    /* Hide "Next piece" box — saves ~100 px of height */
    .info-box:has(#next-piece) {
        display: none;
    }

    /* Score / Level / Lines in one slim row */
    .info-panel {
        flex-direction: row;
        flex-wrap: nowrap;
        justify-content: space-around;
        min-width: auto;
        width: 100%;
        gap: 6px;
    }

    .info-box {
        flex: 1;
        min-width: 0;
        padding: 4px 8px;
    }

    .info-box h3 {
        font-size: 9px;
        letter-spacing: 1px;
        margin-bottom: 2px;
    }

    .info-box p {
        font-size: 18px;
    }

    /* Board: viewport minus header(~114px) + how-to-play(~36px) + controls(~126px) */
    #board {
        height: calc(100vh  - 280px);
        height: calc(100dvh - 280px);
        width: auto;
        max-width: calc(100vw - 32px);
        aspect-ratio: 1 / 2;
    }

    .how-to-play {
        width: calc(100vw - 32px);
    }

    /* Compact how-to-play summary on mobile so it takes minimal height */
    .how-to-play summary {
        padding: 6px 12px;
        font-size: 13px;
    }

    /* Touch controls: pinned at bottom, slightly compact */
    body.has-touch .touch-controls {
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        background: rgba(26, 26, 46, 0.97);
        border-top: 1px solid #2a2a4a;
        padding: 6px 10px 10px;
        z-index: 100;
        max-width: 100%;
        margin: 0;
        gap: 6px;
    }

    .touch-btn {
        width: 66px;
        height: 52px;
        font-size: 22px;
    }

    .touch-btn.wide {
        width: 110px;
        font-size: 17px;
    }
}

/* ── Landscape mobile ── */
@media (max-height: 520px) and (orientation: landscape) {
    .title {
        font-size: 18px;
        margin-top: 8px;
        margin-bottom: 4px;
    }

    .difficulty-bar {
        margin-bottom: 6px;
    }

    #board {
        /* In landscape, height is the scarce resource */
        width: auto;
        height: min(72vh, 360px);
        max-width: none;
        aspect-ratio: 1 / 2;
    }

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

    body.has-touch .touch-controls {
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        background: rgba(26, 26, 46, 0.97);
        border-top: 1px solid #2a2a4a;
        padding: 4px 10px 8px;
        z-index: 100;
        max-width: 100%;
        margin: 0;
    }

    body.has-touch {
        padding-bottom: 140px;
    }

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

    .touch-btn.wide {
        width: 96px;
        font-size: 16px;
    }
}
