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

/* ── THEME ────────────────────────────────────────────────────
   Light là mặc định. Dark chỉ cần ghi đè biến.
   Script trong <head> luôn gán data-theme (kể cả chế độ System),
   nên không cần lặp lại bộ biến trong @media (prefers-color-scheme). */
:root {
    --bg-primary: #f8fafc;
    --text-primary: #0f172a;
    --text-secondary: #64748b;
    --header-bg: #ffffff;
    --header-border: rgba(0, 0, 0, 0.08);
    --header-shadow: 0 4px 16px rgba(0, 0, 0, 0.06);
    --btn-bg: rgba(0, 0, 0, 0.05);
    --btn-border: rgba(0, 0, 0, 0.12);
    --btn-color: #0f172a;
    --btn-hover-bg: rgba(0, 0, 0, 0.1);
    --badge-bg: rgba(0, 0, 0, 0.04);
    --badge-border: rgba(0, 0, 0, 0.1);
    --board-bg: #cbd5e1;
    --board-border: #94a3b8;
    --board-shadow: 0 12px 32px rgba(0, 0, 0, 0.12), inset 0 2px 4px rgba(255, 255, 255, 0.6);
    --cell-bg: #f8fafc;
    --cell-bg-hover: #ffffff;
    --cell-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.06), 0 1px 2px rgba(255, 255, 255, 0.8);
    --cell-active-border: #0284c7;
    --modal-bg: rgba(255, 255, 255, 0.97);
    --modal-border: rgba(0, 0, 0, 0.12);
    --modal-shadow: 0 20px 40px rgba(0, 0, 0, 0.2);

    --score-color: #0284c7;
    --score-glow: 0 0 2px rgba(2, 132, 199, 0.3);
    --led-color: #0284c7;
    --led-glow: 0 0 1px rgba(2, 132, 199, 0.35);
    --best-color: #d97706;
    --best-glow: none;

    --board-size: min(calc(100vw - 24px), calc(100dvh - 88px), 580px);
}

[data-theme="dark"] {
    --bg-primary: #0b0f19;
    --text-primary: #f8fafc;
    --text-secondary: #94a3b8;
    --header-bg: #0f172a;
    --header-border: rgba(255, 255, 255, 0.12);
    --header-shadow: 0 8px 24px rgba(0, 0, 0, 0.5);
    --btn-bg: rgba(255, 255, 255, 0.08);
    --btn-border: rgba(255, 255, 255, 0.18);
    --btn-color: #f8fafc;
    --btn-hover-bg: rgba(255, 255, 255, 0.16);
    --badge-bg: rgba(255, 255, 255, 0.08);
    --badge-border: rgba(255, 255, 255, 0.16);
    --board-bg: #151d2f;
    --board-border: #334155;
    --board-shadow: 0 20px 40px rgba(0, 0, 0, 0.7), inset 0 1px 2px rgba(255, 255, 255, 0.1);
    --cell-bg: #1c263c;
    --cell-bg-hover: #263553;
    --cell-shadow: inset 0 2px 5px rgba(0, 0, 0, 0.55);
    --cell-active-border: #00f2fe;
    --modal-bg: rgba(15, 23, 42, 0.98);
    --modal-border: rgba(255, 255, 255, 0.15);
    --modal-shadow: 0 25px 50px rgba(0, 0, 0, 0.8);

    --score-color: #00f2fe;
    --score-glow: 0 0 10px rgba(0, 242, 254, 0.75);
    --led-color: #00f2fe;
    --led-glow: 0 0 8px rgba(0, 242, 254, 0.65);
    --best-color: #f59e0b;
    --best-glow: 0 0 5px rgba(245, 158, 11, 0.4);
}

/* ── LAYOUT ───────────────────────────────────────────────── */
html, body {
    width: 100%;
    height: 100%;
    overflow: hidden;
    overscroll-behavior: none;
    background-color: var(--bg-primary);
    color: var(--text-primary);
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    user-select: none;
    -webkit-user-select: none;
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
}

#game-wrapper {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: space-between;
    width: 100%;
    height: 100%;
    max-height: 100dvh;
    padding: 8px 12px 12px;
}

/* ── HEADER ───────────────────────────────────────────────── */
#game-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    width: var(--board-size);
    max-width: 100%;
    padding: 8px 14px;
    background: var(--header-bg);
    border: 1px solid var(--header-border);
    border-radius: 14px;
    box-shadow: var(--header-shadow);
    flex-shrink: 0;
}

.brand-link {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    text-decoration: none;
    flex-shrink: 0;
}

.brand-logo {
    height: 30px;
    width: 30px;
    display: block;
    flex-shrink: 0;
}

.brand-title {
    font-size: 18px;
    font-weight: 800;
    letter-spacing: 0.5px;
    background: linear-gradient(135deg, #00f2fe 0%, #4facfe 100%);
    -webkit-background-clip: text;
    background-clip: text;
    -webkit-text-fill-color: transparent;
    white-space: nowrap;
}

.header-right {
    display: flex;
    align-items: center;
    gap: 12px;
}

.header-stats {
    display: flex;
    align-items: center;
    gap: 8px;
}

.stat-badge {
    display: inline-flex;
    align-items: center;
    gap: 5px;
    background: var(--badge-bg);
    border: 1px solid var(--badge-border);
    border-radius: 8px;
    padding: 4px 8px;
    box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.2);
}

.stat-badge.score-badge {
    padding: 3px 10px;
    background: rgba(0, 242, 254, 0.08);
    border-color: rgba(0, 242, 254, 0.25);
}

.trophy-icon {
    width: 14px;
    height: 14px;
    color: #f59e0b;
    flex-shrink: 0;
}

/* Số LED 7 gạch (DSEG7). Font dự phòng hiện ngay trong lúc font tải. */
.stat-val {
    font-family: 'DSEG7-Classic', ui-monospace, monospace;
    font-size: 16px;
    font-weight: 700;
    font-style: italic;
    color: var(--led-color);
    text-shadow: var(--led-glow);
    letter-spacing: 1.5px;
    line-height: 1;
    min-width: 20px;
    text-align: right;
}

.stat-val.best-num {
    font-size: 13px;
    color: var(--best-color);
    text-shadow: var(--best-glow);
    min-width: 18px;
}

.stat-val.score-num {
    font-size: 22px;
    color: var(--score-color);
    text-shadow: var(--score-glow);
    letter-spacing: 2px;
    min-width: 22px;
}

.header-actions {
    display: flex;
    align-items: center;
    gap: 7px;
}

.icon-btn {
    width: 34px;
    height: 34px;
    border-radius: 50%;
    border: 1px solid var(--btn-border);
    background-color: var(--btn-bg);
    color: var(--btn-color);
    display: inline-flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    flex-shrink: 0;
    transition: transform 0.2s ease, background-color 0.2s ease;
}

.icon-btn:focus-visible {
    outline: 2px solid var(--cell-active-border);
    outline-offset: 2px;
}

.icon-btn:active {
    transform: scale(0.95);
}

@media (hover: hover) {
    .icon-btn:hover {
        background-color: var(--btn-hover-bg);
        transform: scale(1.08);
    }
}

/* Icon theme / sound */
.theme-icon-sun, .theme-icon-moon, .theme-icon-system { display: none; }
[data-theme-mode="light"] .theme-icon-sun,
[data-theme-mode="dark"] .theme-icon-moon,
[data-theme-mode="system"] .theme-icon-system { display: block; }

#btn-sound[data-on="0"] .sound-icon-on,
#btn-sound:not([data-on="0"]) .sound-icon-off { display: none; }

/* ── BÀN CỜ ───────────────────────────────────────────────── */
.board-wrapper {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    padding: 6px 0;
}

.board-grid {
    display: grid;
    grid-template-columns: repeat(9, 1fr);
    grid-template-rows: repeat(9, 1fr);
    gap: clamp(3px, 0.8vw, 6px);
    width: var(--board-size);
    height: var(--board-size);
    padding: clamp(6px, 1.4vw, 10px);
    background-color: var(--board-bg);
    border: 3px solid var(--board-border);
    border-radius: 16px;
    box-shadow: var(--board-shadow);
    contain: layout;
}

.cell {
    aspect-ratio: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    background-color: var(--cell-bg);
    border-radius: clamp(4px, 1.2vw, 8px);
    box-shadow: var(--cell-shadow);
    position: relative;
    cursor: pointer;
}

@media (hover: hover) {
    .cell:hover { background-color: var(--cell-bg-hover); }
}

.cell.selected-cell {
    background-color: rgba(2, 132, 199, 0.15);
    box-shadow: inset 0 0 0 2px var(--cell-active-border);
}

/* ── BÓNG ─────────────────────────────────────────────────────
   Mọi trạng thái (thường / xem trước / chọn) dùng chung kích thước,
   chỉ khác transform → không gây layout, chạy trên compositor. */
.ball {
    width: 82%;
    height: 82%;
    border-radius: 50%;
    position: relative;
    flex-shrink: 0;
    pointer-events: none;
    box-shadow:
        0 4px 10px rgba(0, 0, 0, 0.35),
        inset -4px -4px 8px rgba(0, 0, 0, 0.45),
        inset 3px 3px 6px rgba(255, 255, 255, 0.7);
}

.ball::after {
    content: '';
    position: absolute;
    top: 12%;
    left: 20%;
    width: 28%;
    height: 18%;
    border-radius: 50%;
    background: radial-gradient(ellipse at center, rgba(255, 255, 255, 0.88) 0%, rgba(255, 255, 255, 0) 80%);
    transform: rotate(-25deg);
}

.ball-hidden { display: none; }

/* Đang di chuyển: nổi lên trên các ô phía sau */
.ball.moving { z-index: 5; }

.ball.selected {
    animation: ballBounce 0.45s infinite ease-in-out;
    box-shadow:
        0 6px 14px var(--ball-glow, rgba(255, 255, 255, 0.8)),
        inset -4px -4px 8px rgba(0, 0, 0, 0.4),
        inset 3px 3px 6px rgba(255, 255, 255, 0.8);
}

@keyframes ballBounce {
    0%, 100% { transform: translateY(0) scale(1); }
    50% { transform: translateY(-22%) scale(1.08); }
}

/* Bóng xem trước: cùng kích thước, thu nhỏ bằng transform */
.ball.ball-sm {
    transform: scale(0.42);
    opacity: 0.9;
    animation: pulseSmall 1.8s infinite ease-in-out;
}

@keyframes pulseSmall {
    0%, 100% { transform: scale(0.42); }
    50% { transform: scale(0.48); }
}

/* 6 màu bóng */
.ball-1 { background: radial-gradient(circle at 35% 35%, #ff7b7b 0%, #dc2626 55%, #7f1d1d 100%); --ball-glow: rgba(239, 68, 68, 0.7); }
.ball-2 { background: radial-gradient(circle at 35% 35%, #70b4ff 0%, #2563eb 55%, #1e3a8a 100%); --ball-glow: rgba(37, 99, 235, 0.7); }
.ball-3 { background: radial-gradient(circle at 35% 35%, #6ee7b7 0%, #16a34a 55%, #14532d 100%); --ball-glow: rgba(22, 163, 74, 0.7); }
.ball-4 { background: radial-gradient(circle at 35% 35%, #fef08a 0%, #eab308 55%, #854d0e 100%); --ball-glow: rgba(234, 179, 8, 0.7); }
.ball-5 { background: radial-gradient(circle at 35% 35%, #f0abfc 0%, #c026d3 55%, #701a75 100%); --ball-glow: rgba(192, 38, 211, 0.7); }
.ball-6 { background: radial-gradient(circle at 35% 35%, #fdba74 0%, #ea580c 55%, #7c2d12 100%); --ball-glow: rgba(234, 88, 12, 0.7); }

/* ── MODAL GAME OVER ──────────────────────────────────────── */
.modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.7);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 9999;
    padding: 16px;
    animation: fadeIn 0.25s ease-out;
}

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

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

.modal-card {
    background: var(--modal-bg);
    border: 1px solid var(--modal-border);
    border-radius: 20px;
    box-shadow: var(--modal-shadow);
    padding: 28px 24px;
    width: 100%;
    max-width: 340px;
    text-align: center;
    animation: scaleUp 0.3s cubic-bezier(0.16, 1, 0.3, 1);
}

@keyframes scaleUp {
    from { transform: scale(0.9); opacity: 0; }
    to { transform: scale(1); opacity: 1; }
}

.modal-title {
    font-size: 26px;
    font-weight: 900;
    background: linear-gradient(135deg, #ef4444 0%, #f59e0b 100%);
    -webkit-background-clip: text;
    background-clip: text;
    -webkit-text-fill-color: transparent;
    margin-bottom: 6px;
}

.modal-subtitle {
    font-size: 14px;
    color: var(--text-secondary);
    margin-bottom: 20px;
}

.modal-score-box {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 8px;
    background: var(--badge-bg);
    border: 1px solid var(--badge-border);
    border-radius: 12px;
    padding: 12px 6px;
    margin-bottom: 22px;
}

.modal-score-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 4px;
}

.modal-label {
    font-size: 11px;
    font-weight: 700;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.5px;
    white-space: nowrap;
}

.modal-val {
    font-size: 20px;
    font-weight: 900;
    color: var(--text-primary);
    font-family: 'Arial Black', Arial, sans-serif;
}

.modal-btn {
    width: 100%;
    padding: 12px;
    border: none;
    border-radius: 12px;
    background: linear-gradient(135deg, #0284c7 0%, #00f2fe 100%);
    color: #ffffff;
    font-size: 16px;
    font-weight: 800;
    cursor: pointer;
    box-shadow: 0 4px 16px rgba(2, 132, 199, 0.35);
    transition: transform 0.2s ease;
}

.modal-btn:focus-visible {
    outline: 2px solid var(--text-primary);
    outline-offset: 2px;
}

.modal-btn:active { transform: scale(0.98); }

@media (hover: hover) {
    .modal-btn:hover { transform: translateY(-2px); }
}

/* ── RESPONSIVE ───────────────────────────────────────────── */
@media (max-width: 480px) {
    :root { --board-size: min(calc(100vw - 16px), calc(100dvh - 78px), 580px); }
    #game-wrapper { padding: 6px 8px 10px; }
    #game-header { padding: 6px 10px; }
    .brand-logo { height: 24px; width: 24px; }
    .brand-title { font-size: 14px; }
    .header-right { gap: 6px; }
    .header-stats { gap: 4px; }
    .stat-badge { padding: 3px 6px; gap: 4px; }
    .trophy-icon { width: 12px; height: 12px; }
    .stat-val.best-num { font-size: 11.5px; min-width: 15px; }
    .stat-badge.score-badge { padding: 2px 7px; }
    .stat-val.score-num { font-size: 18px; min-width: 18px; }
    .header-actions { gap: 4px; }
    .icon-btn { width: 28px; height: 28px; }
}

@media (max-height: 540px) {
    :root { --board-size: min(calc(100vw - 16px), calc(100dvh - 56px), 440px); }
    #game-wrapper { padding: 4px 8px; }
    #game-header { padding: 4px 10px; }
    .brand-logo { height: 20px; width: 20px; }
    .brand-title { font-size: 13px; }
    .icon-btn { width: 26px; height: 26px; }
    .stat-badge { padding: 2px 5px; }
    .trophy-icon { width: 11px; height: 11px; }
    .stat-val.best-num { font-size: 11px; }
    .stat-val.score-num { font-size: 16px; }
}

/* Tôn trọng cài đặt giảm chuyển động của hệ thống */
@media (prefers-reduced-motion: reduce) {
    .ball.selected { animation: none; transform: scale(1.08); }
    .ball.ball-sm { animation: none; }
    .modal-overlay, .modal-card { animation: none; }
}
