* {
    box-sizing: border-box;
}

:root {
    --page-bg:
        radial-gradient(circle at 20% 20%, rgba(255, 214, 102, 0.34), transparent 28%),
        radial-gradient(circle at 78% 18%, rgba(81, 181, 255, 0.28), transparent 26%),
        linear-gradient(145deg, #f8fafc 0%, #dbeafe 100%);
    --text-main: #18212f;
    --text-strong: #0f172a;
    --text-muted: #64748b;
    --shell-bg: rgba(255, 255, 255, 0.86);
    --shell-border: rgba(24, 33, 47, 0.12);
    --shell-shadow: rgba(24, 33, 47, 0.16);
    --control-bg: #ffffff;
    --stat-bg: #f8fafc;
    --board-wrap-bg: #cbd5e1;
    --board-bg: #64748b;
    --cell-bg: #94a3b8;
    --cell-hover-bg: #a8b5c8;
    --cell-highlight: rgba(255, 255, 255, 0.45);
    --cell-shadow: rgba(15, 23, 42, 0.28);
    --revealed-bg: #f1f5f9;
    --revealed-border: #cbd5e1;
    --flag-bg: #fde68a;
    --flag-color: #92400e;
    --question-bg: #bfdbfe;
    --question-color: #1e3a8a;
    --mine-bg: #fecaca;
    --mine-color: #7f1d1d;
    --primary: #2563eb;
    --primary-hover: #1d4ed8;
}

body[data-theme="fresh"] {
    --page-bg:
        radial-gradient(circle at 18% 18%, rgba(125, 211, 252, 0.32), transparent 27%),
        radial-gradient(circle at 82% 22%, rgba(167, 243, 208, 0.35), transparent 26%),
        linear-gradient(145deg, #ecfeff 0%, #f0fdf4 100%);
    --text-main: #12313a;
    --text-strong: #082f49;
    --text-muted: #4b7280;
    --shell-bg: rgba(255, 255, 255, 0.9);
    --stat-bg: #f0fdfa;
    --board-wrap-bg: #b6e3dd;
    --board-bg: #4f8f96;
    --cell-bg: #79b8c1;
    --cell-hover-bg: #8fd0d8;
    --revealed-bg: #f7fffb;
    --revealed-border: #b7d9d2;
    --primary: #0891b2;
    --primary-hover: #0e7490;
}

body[data-theme="dark"] {
    --page-bg:
        radial-gradient(circle at 18% 20%, rgba(59, 130, 246, 0.22), transparent 28%),
        radial-gradient(circle at 78% 18%, rgba(245, 158, 11, 0.16), transparent 26%),
        linear-gradient(145deg, #0f172a 0%, #1e293b 100%);
    --text-main: #e5e7eb;
    --text-strong: #f8fafc;
    --text-muted: #94a3b8;
    --shell-bg: rgba(15, 23, 42, 0.9);
    --shell-border: rgba(226, 232, 240, 0.14);
    --shell-shadow: rgba(0, 0, 0, 0.34);
    --control-bg: #1e293b;
    --stat-bg: #111827;
    --board-wrap-bg: #0f172a;
    --board-bg: #334155;
    --cell-bg: #64748b;
    --cell-hover-bg: #74859c;
    --cell-highlight: rgba(255, 255, 255, 0.26);
    --cell-shadow: rgba(0, 0, 0, 0.38);
    --revealed-bg: #1f2937;
    --revealed-border: #334155;
    --flag-bg: #78350f;
    --flag-color: #fde68a;
    --question-bg: #1e3a8a;
    --question-color: #bfdbfe;
    --mine-bg: #7f1d1d;
    --mine-color: #fecaca;
    --primary: #3b82f6;
    --primary-hover: #2563eb;
}

body[data-theme="forest"] {
    --page-bg:
        radial-gradient(circle at 18% 18%, rgba(190, 242, 100, 0.24), transparent 27%),
        radial-gradient(circle at 78% 22%, rgba(45, 212, 191, 0.22), transparent 26%),
        linear-gradient(145deg, #ecfccb 0%, #bbf7d0 100%);
    --text-main: #183323;
    --text-strong: #052e16;
    --text-muted: #52735b;
    --shell-bg: rgba(255, 255, 255, 0.88);
    --stat-bg: #f0fdf4;
    --board-wrap-bg: #b7d7af;
    --board-bg: #4d7c0f;
    --cell-bg: #84a85a;
    --cell-hover-bg: #95ba68;
    --revealed-bg: #f7fee7;
    --revealed-border: #bbd99b;
    --primary: #16a34a;
    --primary-hover: #15803d;
}

body {
    min-height: 100vh;
    margin: 0;
    padding: 32px 16px;
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--text-main);
    font-family: Arial, "Microsoft YaHei", sans-serif;
    background: var(--page-bg);
}

.game-shell {
    width: min(100%, 1380px);
    padding: 24px;
    border: 1px solid var(--shell-border);
    border-radius: 8px;
    background: var(--shell-bg);
    box-shadow: 0 22px 55px var(--shell-shadow);
}

.game-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
    margin-bottom: 18px;
}

.header-actions {
    display: flex;
    align-items: flex-end;
    gap: 12px;
}

.difficulty-control,
.theme-control {
    display: grid;
    gap: 6px;
    color: var(--text-muted);
    font-size: 13px;
    font-weight: 700;
}

#difficulty-select,
#theme-select {
    min-width: 104px;
    height: 38px;
    padding: 0 34px 0 12px;
    border: 1px solid var(--shell-border);
    border-radius: 6px;
    color: var(--text-strong);
    font-size: 14px;
    font-weight: 700;
    background: var(--control-bg);
}

.eyebrow {
    margin: 0 0 4px;
    color: var(--text-muted);
    font-size: 12px;
    font-weight: 700;
    letter-spacing: 0;
}

h1 {
    margin: 0;
    color: var(--text-strong);
    font-size: 32px;
    line-height: 1.1;
}

#game-info {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 12px;
    margin-bottom: 18px;
}

.stat {
    min-height: 76px;
    padding: 14px 16px;
    border: 1px solid var(--shell-border);
    border-radius: 8px;
    background: var(--stat-bg);
}

.stat-label {
    display: block;
    margin-bottom: 8px;
    color: var(--text-muted);
    font-size: 13px;
}

.stat strong {
    color: var(--text-strong);
    font-size: 26px;
    line-height: 1;
}

.rules-panel {
    margin-bottom: 18px;
    border: 1px solid var(--shell-border);
    border-radius: 8px;
    background: var(--stat-bg);
}

.rules-panel:not([open]) {
    display: none;
}

.rules-panel summary {
    display: none;
}

.rules-content {
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 14px;
    padding: 16px;
    color: var(--text-main);
}

.rules-content section {
    min-width: 0;
}

.rules-content h2 {
    margin: 0 0 8px;
    color: var(--text-strong);
    font-size: 15px;
    line-height: 1.3;
}

.rules-content p,
.rules-content ul {
    margin: 0;
    color: var(--text-muted);
    font-size: 14px;
    line-height: 1.7;
}

.rules-content ul {
    padding-left: 18px;
}

.rules-content li + li {
    margin-top: 4px;
}

.board-wrap {
    display: flex;
    justify-content: center;
    padding: 14px;
    border: 1px solid var(--shell-border);
    border-radius: 8px;
    background: var(--board-wrap-bg);
}

#minesweeper-board {
    display: inline-grid;
    gap: 3px;
    padding: 4px;
    border-radius: 4px;
    background: var(--board-bg);
}

.cell {
    width: 40px;
    height: 40px;
    display: flex;
    align-items: center;
    justify-content: center;
    border: 0;
    border-radius: 2px;
    color: var(--text-strong);
    font-size: 20px;
    font-weight: 800;
    line-height: 1;
    cursor: pointer;
    user-select: none;
    background: var(--cell-bg);
    box-shadow:
        inset 2px 2px 0 var(--cell-highlight),
        inset -2px -2px 0 var(--cell-shadow);
    transition: transform 0.08s ease, background-color 0.15s ease, box-shadow 0.15s ease;
}

.cell:hover {
    transform: translateY(-1px);
    background: var(--cell-hover-bg);
}

.revealed {
    cursor: default;
    background: var(--revealed-bg);
    border: 1px solid var(--revealed-border);
    color: var(--text-main);
    box-shadow: inset 0 1px 3px rgba(15, 23, 42, 0.08);
}

.revealed:hover {
    transform: none;
}

.mine {
    color: var(--mine-color);
    background: var(--mine-bg) !important;
}

.flagged {
    color: var(--flag-color);
    background: var(--flag-bg) !important;
}

.questioned {
    color: var(--question-color);
    background: var(--question-bg) !important;
    font-size: 24px;
    font-weight: 900;
}

.cell[data-value="1"] {
    color: #2563eb;
}

.cell[data-value="2"] {
    color: #16a34a;
}

.cell[data-value="3"] {
    color: #dc2626;
}

.cell[data-value="4"] {
    color: #7c3aed;
}

.cell[data-value="5"] {
    color: #be123c;
}

.cell[data-value="6"] {
    color: #0891b2;
}

.cell[data-value="7"] {
    color: #111827;
}

.cell[data-value="8"] {
    color: #475569;
}

#reset-button,
#rules-toggle-button {
    min-width: 96px;
    padding: 10px 16px;
    border: none;
    border-radius: 6px;
    color: #fff;
    font-size: 14px;
    font-weight: 700;
    cursor: pointer;
    background: var(--primary);
    box-shadow: 0 8px 18px rgba(37, 99, 235, 0.22);
}

#reset-button:hover,
#rules-toggle-button:hover {
    background: var(--primary-hover);
}

.modal-overlay {
    position: fixed;
    inset: 0;
    z-index: 1000;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 18px;
    background-color: rgba(15, 23, 42, 0.55);
    animation: fadeIn 0.3s ease;
}

.modal-overlay.hidden {
    display: none;
}

.modal-box {
    width: min(100%, 360px);
    padding: 34px 30px;
    border-radius: 8px;
    text-align: center;
    background-color: #fff;
    box-shadow: 0 18px 50px rgba(15, 23, 42, 0.24);
    animation: popIn 0.28s ease;
}

.modal-icon {
    margin-bottom: 16px;
    font-size: 64px;
    line-height: 1;
}

.modal-title {
    margin: 0 0 10px;
    font-size: 30px;
}

.modal-message {
    margin: 0 0 26px;
    color: #64748b;
    font-size: 16px;
}

.modal-restart-button {
    min-width: 120px;
    padding: 12px 24px;
    border: none;
    border-radius: 6px;
    color: #fff;
    font-size: 16px;
    font-weight: 700;
    cursor: pointer;
}

.modal-box.win {
    border-top: 6px solid #16a34a;
}

.modal-box.win .modal-title {
    color: #16a34a;
}

.modal-box.win .modal-restart-button {
    background-color: #16a34a;
}

.modal-box.lose {
    border-top: 6px solid #dc2626;
}

.modal-box.lose .modal-title {
    color: #dc2626;
}

.modal-box.lose .modal-restart-button {
    background-color: #dc2626;
}

@keyframes fadeIn {
    from {
        opacity: 0;
    }
    to {
        opacity: 1;
    }
}

@keyframes popIn {
    from {
        opacity: 0;
        transform: translateY(10px) scale(0.98);
    }
    to {
        opacity: 1;
        transform: translateY(0) scale(1);
    }
}

@media (max-width: 520px) {
    body {
        align-items: flex-start;
        padding: 18px 10px;
    }

    .game-shell {
        padding: 16px;
    }

    .game-header {
        align-items: flex-start;
        flex-direction: column;
    }

    .header-actions {
        width: 100%;
        align-items: stretch;
        flex-direction: column;
    }

    h1 {
        font-size: 28px;
    }

    #reset-button,
    #rules-toggle-button {
        width: 100%;
    }

    #game-info {
        grid-template-columns: 1fr;
    }

    .rules-content {
        grid-template-columns: 1fr;
    }

    .board-wrap {
        overflow-x: auto;
        justify-content: flex-start;
    }
}
