/* The overlay itself is positioned like the block game's: fixed over the page, dark in both themes through
   game-dark-surface, with the answer screen (game-answer-screen.css) covering it when a gate is open. */
.minesweeper-overlay {
    position: fixed;
    inset: 0;
    z-index: 1050;
    display: flex;
    flex-direction: column;
    background: var(--bg-primary, #0f172a);
    color: var(--text-primary);
    padding-bottom: env(safe-area-inset-bottom);
}

html:has(.minesweeper-overlay) {
    overscroll-behavior: none;
}

.minesweeper-header {
    display: flex;
    align-items: center;
    gap: 1rem;
    padding: 0.75rem 1rem;
    padding-top: calc(0.75rem + env(safe-area-inset-top));
    font-variant-numeric: tabular-nums;
}

.minesweeper-score {
    font-size: 1.35rem;
    font-weight: 800;
    color: #fde047;
    transform-origin: left center;
    display: inline-block;
    animation: minesweeper-score-bump 420ms cubic-bezier(0.34, 1.56, 0.64, 1);
}

@keyframes minesweeper-score-bump {
    0%   { transform: scale(1); }
    35%  { transform: scale(1.2); text-shadow: 0 0 14px rgba(253, 224, 71, 0.7); }
    100% { transform: scale(1); }
}

.minesweeper-clicks {
    margin-left: auto;
    opacity: 0.9;
}

.minesweeper-close-button {
    background: none;
    border: none;
    color: inherit;
    min-width: 2.75rem;
    min-height: 2.75rem;
}

.minesweeper-close-button:disabled {
    opacity: 0.5;
    cursor: default;
}

.minesweeper-toolbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.75rem;
    padding: 0 1rem 0.5rem;
}

.minesweeper-toolbar .btn {
    min-height: 2.75rem;
}

.minesweeper-mines {
    font-variant-numeric: tabular-nums;
    opacity: 0.9;
}

.minesweeper-board-frame {
    flex: 1;
    min-height: 0;
    display: flex;
    align-items: flex-start;
    justify-content: center;
    padding: 0 1rem 1rem;
    overflow: auto;
}

/* Each cell is at most the 2.75rem a finger needs and shrinks to fit the width the phone has. */
.minesweeper-board {
    width: min(100%, calc(var(--minesweeper-columns) * 2.75rem));
    display: flex;
    flex-direction: column;
    gap: 2px;
    user-select: none;
    -webkit-user-select: none;
    -webkit-touch-callout: none;
    touch-action: manipulation;
}

.minesweeper-board:focus-visible,
.minesweeper-board[data-focus-visible] {
    outline: 2px solid var(--focus-ring, #93c5fd);
    outline-offset: 3px;
}

.minesweeper-row {
    display: grid;
    grid-template-columns: repeat(var(--minesweeper-columns), 1fr);
    gap: 2px;
}

.minesweeper-cell {
    aspect-ratio: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 0.25rem;
    background: #64748b;
    color: #ffffff;
    font-size: clamp(0.85rem, 4.5vw, 1.35rem);
    font-weight: 800;
    line-height: 1;
    cursor: pointer;
}

.minesweeper-cell.is-opened {
    background: #1e293b;
    cursor: default;
}

.minesweeper-cell.is-mine {
    background: #7f1d1d;
    animation: minesweeper-mine-reveal 300ms ease-out both;
    animation-delay: var(--reveal-delay, 500ms);
}

/* The icon is held back with the cell, so a mine is not shown before it goes off. */
.minesweeper-cell.is-mine > i {
    animation: minesweeper-mine-icon 300ms ease-out both;
    animation-delay: var(--reveal-delay, 500ms);
}

.minesweeper-cell.is-exploded {
    position: relative;
    z-index: 2;
    background: #dc2626;
    animation: minesweeper-blast 700ms ease-out both;
}

/* A burst that grows past the cell and fades, over the whole board. */
.minesweeper-cell.is-exploded::after {
    content: "";
    position: absolute;
    inset: -90%;
    border-radius: 50%;
    background: radial-gradient(circle, rgba(253, 224, 71, 0.95) 0%, rgba(249, 115, 22, 0.8) 35%, rgba(220, 38, 38, 0) 70%);
    pointer-events: none;
    animation: minesweeper-blast-burst 700ms ease-out both;
}

.minesweeper-board.is-exploding {
    animation: minesweeper-shake 450ms ease-in-out;
}

/* The burst reaches past the board frame's edge, which clips overflow the rest of the time. */
.minesweeper-board-frame:has(.is-exploding) {
    overflow: visible;
}

@keyframes minesweeper-blast {
    0%   { transform: scale(1); background: #fde047; }
    30%  { transform: scale(1.5); background: #f97316; }
    100% { transform: scale(1); background: #dc2626; }
}

@keyframes minesweeper-blast-burst {
    0%   { transform: scale(0.2); opacity: 1; }
    100% { transform: scale(1.6); opacity: 0; }
}

@keyframes minesweeper-shake {
    0%, 100% { transform: translate(0, 0); }
    15%      { transform: translate(-6px, 3px); }
    30%      { transform: translate(5px, -4px); }
    45%      { transform: translate(-4px, -3px); }
    60%      { transform: translate(4px, 3px); }
    80%      { transform: translate(-2px, 1px); }
}

@keyframes minesweeper-mine-reveal {
    0%   { background: #64748b; transform: scale(1); }
    50%  { background: #dc2626; transform: scale(1.25); }
    100% { background: #7f1d1d; transform: scale(1); }
}

@keyframes minesweeper-mine-icon {
    from { opacity: 0; }
    to   { opacity: 1; }
}

@media (prefers-reduced-motion: reduce) {
    .minesweeper-cell.is-mine,
    .minesweeper-cell.is-mine > i,
    .minesweeper-cell.is-exploded,
    .minesweeper-cell.is-exploded::after,
    .minesweeper-board.is-exploding {
        animation: none;
    }

    .minesweeper-cell.is-exploded::after {
        display: none;
    }
}

/* Drawn only while the board holds focus, since the cursor is the keyboard's and a tap-only player never sees it. */
.minesweeper-board:focus .minesweeper-cell.is-cursor {
    outline: 3px solid #fde047;
    outline-offset: -3px;
}

.minesweeper-cell.number-1 { color: #60a5fa; }
.minesweeper-cell.number-2 { color: #4ade80; }
.minesweeper-cell.number-3 { color: #f87171; }
.minesweeper-cell.number-4 { color: #c084fc; }
.minesweeper-cell.number-5 { color: #fbbf24; }
.minesweeper-cell.number-6 { color: #22d3ee; }
.minesweeper-cell.number-7 { color: #f1f5f9; }
.minesweeper-cell.number-8 { color: #cbd5e1; }

/* Over the dimmed preview board, as three buttons in a column. */
.minesweeper-levels {
    position: absolute;
    inset: 0;
    z-index: 10;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 0.75rem;
    padding: 1rem;
    /* The panel covers the header too, and the close button must stay reachable through it. */
    pointer-events: none;
}

.minesweeper-level-button {
    pointer-events: auto;
    width: min(100%, 18rem);
    padding: 0.9rem 1rem;
    border-radius: 0.75rem;
    background: #1e293b;
    border: 1px solid #94a3b8;
    color: var(--text-primary);
    font-size: 1.15rem;
    font-weight: 800;
}

.minesweeper-level-button:hover,
.minesweeper-level-button:focus-visible {
    background: #334155;
    color: var(--text-primary);
}

.minesweeper-toolbar[inert],
.minesweeper-board-frame[inert] {
    opacity: 0.45;
    filter: blur(1.5px);
}

.minesweeper-panel {
    padding: 1rem;
    text-align: center;
}

.minesweeper-error {
    color: var(--color-danger-text, #fca5a5);
}

.minesweeper-result {
    position: absolute;
    inset: 0;
    z-index: 10;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 1rem;
    background: rgba(15, 23, 42, 0.82);
    backdrop-filter: blur(2px);
}

.minesweeper-result-card {
    text-align: center;
    max-width: 22rem;
    width: 100%;
}

.minesweeper-result-title {
    margin: 0 0 0.25rem;
    font-size: 1.25rem;
    font-weight: 800;
}

.minesweeper-result-bonus {
    margin: 0 0 1.5rem;
    font-size: 2rem;
    font-weight: 900;
    color: #fde047;
}

.minesweeper-result-record {
    display: inline-block;
    margin: 0 0 0.5rem;
    padding: 0.2rem 0.8rem;
    border-radius: 999px;
    background: rgba(253, 224, 71, 0.18);
    color: #fde047;
    font-weight: 800;
}

.minesweeper-result-score {
    margin: 0;
    font-size: 5rem;
    line-height: 1;
    font-weight: 900;
    color: #fde047;
    font-variant-numeric: tabular-nums;
}

.minesweeper-result-label {
    margin: 0.25rem 0 1.75rem;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    opacity: 0.8;
}

.minesweeper-result-actions {
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
}

.minesweeper-result-actions .btn {
    padding: 0.9rem 1rem;
    font-size: 1.05rem;
    font-weight: 700;
    border-radius: 0.75rem;
}
