:root {
    --bg: #050505;
    --panel: rgba(0, 0, 0, 0.62);
    --panel-strong: rgba(0, 0, 0, 0.78);
    --line: rgba(255, 255, 255, 0.13);
    --line-strong: rgba(255, 255, 255, 0.22);
    --text: #ffffff;
    --muted: rgba(255, 255, 255, 0.72);
    --accent: #ff6b6b;
    --accent-hover: #ff5252;
}

* {
    box-sizing: border-box;
}

html,
body {
    min-height: 100%;
}

body {
    margin: 0;
    font-family: "Helvetica Neue", Arial, sans-serif;
    line-height: 1.6;
    color: var(--text);
    background: var(--bg);
    overflow-x: hidden;
}

#ambientCanvas {
    position: fixed;
    inset: 0;
    width: 100%;
    height: 100%;
    z-index: -1;
    background: linear-gradient(rgba(0, 0, 0, 0.9), rgba(0, 0, 0, 0.96));
}

.content {
    position: relative;
    z-index: 1;
    min-height: 100vh;
}

.container {
    width: min(1200px, calc(100% - 40px));
    margin: 0 auto;
}

.site-header {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    z-index: 1000;
    background-color: rgba(0, 0, 0, 0.72);
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.3);
    backdrop-filter: blur(5px);
}

.top-nav {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 20px;
    padding: 18px 0;
}

.logo {
    color: #fff;
    font-size: 24px;
    font-weight: 700;
    text-decoration: none;
    white-space: nowrap;
}

.nav-links {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 24px;
    color: var(--muted);
    font-weight: 500;
}

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-height: 44px;
    padding: 12px 24px;
    border: 0;
    border-radius: 4px;
    color: #fff;
    background-color: var(--accent);
    font: inherit;
    font-weight: 500;
    text-decoration: none;
    cursor: pointer;
    transition: background-color 0.3s, box-shadow 0.3s, transform 0.3s;
}

.btn:hover,
.btn:focus-visible {
    background-color: var(--accent-hover);
    transform: translateY(-2px);
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.28);
    outline: none;
}

.btn:disabled {
    color: rgba(255, 255, 255, 0.58);
    background-color: rgba(255, 255, 255, 0.12);
    box-shadow: none;
    cursor: not-allowed;
    transform: none;
}

.btn.is-active {
    color: #111;
    background-color: #fff;
}

.btn.compact {
    min-height: 38px;
    padding: 9px 18px;
}

.app-shell {
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 96px 20px 32px;
}

.game-section {
    width: 100%;
}

.game-layout {
    display: grid;
    grid-template-columns: minmax(220px, 286px) minmax(300px, 520px);
    align-items: start;
    justify-content: center;
    gap: 28px;
}

.hud-panel,
.board-frame {
    border: 1px solid var(--line);
    border-radius: 8px;
    background-color: var(--panel);
    box-shadow: 0 16px 32px rgba(0, 0, 0, 0.28);
    backdrop-filter: blur(5px);
}

.hud-panel {
    display: flex;
    flex-direction: column;
    gap: 22px;
    padding: 24px;
}

.hud-block {
    display: grid;
    gap: 2px;
}

.primary-score {
    padding-bottom: 18px;
    border-bottom: 1px solid var(--line);
}

.hud-row {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 18px;
    padding-bottom: 18px;
    border-bottom: 1px solid var(--line);
}

.hud-label {
    color: var(--muted);
    font-size: 13px;
    font-weight: 500;
    text-transform: uppercase;
}

.hud-block strong {
    min-height: 32px;
    color: #fff;
    font-size: 28px;
    line-height: 1.15;
}

.primary-score strong {
    min-height: 56px;
    font-size: 52px;
    line-height: 1.05;
}

.points-block {
    padding-bottom: 18px;
    border-bottom: 1px solid var(--line);
}

.next-panel {
    display: grid;
    grid-template-columns: 1fr 96px;
    align-items: center;
    gap: 18px;
    padding-bottom: 20px;
    border-bottom: 1px solid var(--line);
}

#nextCanvas {
    width: 96px;
    height: 96px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background-color: rgba(255, 255, 255, 0.06);
}

.board-frame {
    position: relative;
    width: min(520px, calc((100vh - 240px) * 0.6667), 100%);
    min-width: 300px;
    padding: 12px;
    background-color: var(--panel-strong);
    transition: transform 0.1s ease-out;
}

.board-frame.is-jiggling {
    animation: board-jiggle 280ms ease-in-out;
}

#gameCanvas {
    display: block;
    width: 100%;
    height: auto;
    aspect-ratio: 2 / 3;
    border-radius: 6px;
    background-color: #070707;
    touch-action: none;
}

#gameCanvas.is-removing {
    cursor: crosshair;
}

.board-actions {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    align-items: center;
    gap: 14px;
    margin-top: 12px;
}

.board-actions .btn {
    min-height: 42px;
}

.board-actions span {
    grid-column: 1 / -1;
    color: var(--muted);
    font-size: 13px;
    line-height: 1.35;
    text-align: center;
}

.canvas-status {
    position: absolute;
    inset: 12px 12px 92px;
    display: grid;
    place-items: center;
    margin: 0;
    padding: 28px;
    border-radius: 6px;
    color: #fff;
    text-align: center;
    background-color: rgba(0, 0, 0, 0.82);
}

.canvas-status[hidden] {
    display: none;
}

@keyframes board-jiggle {
    0% {
        transform: translateX(0);
    }

    18% {
        transform: translate(-9px, -2px) rotate(-0.65deg);
    }

    36% {
        transform: translate(9px, 1px) rotate(0.65deg);
    }

    54% {
        transform: translate(-6px, -1px) rotate(-0.4deg);
    }

    72% {
        transform: translate(6px, 1px) rotate(0.4deg);
    }

    100% {
        transform: translateX(0);
    }
}

/* Compact layout: the whole game fits on one screen, no scrolling. */
@media (max-width: 860px) {
    body {
        overflow: hidden;
    }

    .content {
        display: flex;
        flex-direction: column;
        height: 100vh;
        height: 100dvh;
        min-height: 0;
        overflow: hidden;
    }

    .site-header {
        position: static;
        flex: 0 0 auto;
    }

    .top-nav {
        padding: 10px 0;
    }

    .app-shell {
        flex: 1 1 auto;
        min-height: 0;
        align-items: stretch;
        padding: 10px 12px 12px;
    }

    .game-section {
        display: flex;
        min-height: 0;
    }

    .game-layout {
        display: flex;
        flex-direction: column;
        flex: 1 1 auto;
        align-items: stretch;
        min-height: 0;
        gap: 10px;
    }

    /* HUD collapses from a tall sidebar into one compact strip. */
    .hud-panel {
        order: 1;
        flex: 0 0 auto;
        display: grid;
        grid-template-columns: 1.15fr 1fr 1fr 1fr auto;
        align-items: center;
        width: 100%;
        gap: 10px;
        padding: 10px 12px;
    }

    .hud-row {
        display: contents;
    }

    .primary-score,
    .hud-row,
    .points-block,
    .next-panel {
        padding-bottom: 0;
        border-bottom: 0;
    }

    .hud-label {
        font-size: 10px;
    }

    .hud-block strong,
    .primary-score strong {
        min-height: 0;
        font-size: 20px;
        line-height: 1.2;
    }

    .primary-score strong {
        font-size: 26px;
    }

    .next-panel {
        grid-template-columns: auto;
        justify-items: center;
        gap: 3px;
    }

    #nextCanvas {
        width: 42px;
        height: 42px;
    }

    /* The header already carries a Restart button in this layout. */
    #restartButton {
        display: none;
    }

    .board-frame {
        order: 2;
        display: flex;
        flex-direction: column;
        flex: 1 1 auto;
        width: 100%;
        min-width: 0;
        min-height: 0;
        gap: 8px;
        padding: 8px;
    }

    /* The stage absorbs the leftover height; the board fits inside it at 2:3. */
    .board-stage {
        display: flex;
        flex: 1 1 auto;
        align-items: center;
        justify-content: center;
        min-height: 0;
    }

    #gameCanvas {
        width: auto;
        height: auto;
        max-width: 100%;
        max-height: 100%;
    }

    .board-actions {
        flex: 0 0 auto;
        margin-top: 0;
        gap: 8px;
    }

    .board-actions .btn {
        min-height: 38px;
    }

    .board-actions span {
        font-size: 12px;
        line-height: 1.25;
    }

    .canvas-status {
        inset: 8px;
        padding: 20px;
    }
}

@media (max-width: 560px) {
    .container {
        width: min(100% - 20px, 1200px);
    }

    .logo {
        font-size: 19px;
    }

    .nav-links {
        gap: 10px;
    }

    .nav-links span {
        display: none;
    }

    .app-shell {
        padding: 8px 10px 10px;
    }

    .hud-panel {
        gap: 8px;
        padding: 8px 10px;
    }

    .hud-label {
        font-size: 9px;
    }

    .hud-block strong,
    .primary-score strong {
        font-size: 17px;
    }

    .primary-score strong {
        font-size: 22px;
    }

    #nextCanvas {
        width: 36px;
        height: 36px;
    }

    .board-actions {
        gap: 8px;
    }

    .board-actions span {
        font-size: 11px;
    }
}

/* Landscape phones: put the strip beside the board so the board keeps its height. */
@media (max-width: 860px) and (max-height: 520px) {
    .game-layout {
        flex-direction: row;
        gap: 8px;
    }

    .hud-panel {
        grid-template-columns: minmax(0, 1fr);
        align-content: start;
        width: auto;
        min-width: 104px;
        gap: 6px;
    }

    .next-panel {
        justify-items: start;
    }

    .board-frame {
        flex: 1 1 auto;
        flex-direction: row;
        align-items: stretch;
        min-width: 0;
    }

    .board-stage {
        min-width: 0;
    }

    /* Actions move beside the board so the board keeps the full height. */
    .board-actions {
        flex: 0 0 auto;
        grid-template-columns: minmax(0, 1fr);
        align-content: center;
        width: 92px;
        gap: 6px;
    }

    .board-actions span {
        grid-column: auto;
    }
}
