/* ============================================================
   §1  RESET & CSS VARIABLES
   ============================================================ */
*,
*::before,
*::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    -webkit-tap-highlight-color: transparent;
}

:root {
    /* Backgrounds */
    --c-bg: #f7f1e9;
    --c-overlay: rgba(247, 241, 233, 0.7);
    --c-menuBg: rgba(0, 0, 0, 0.15);
    --c-menuPanel: #f7f1e9;

    /* Text */
    --c-text: #605349;
    --c-textLight: #FFF9F2;
    --c-textMuted: #6D6054;

    /* Borders & surfaces */
    --c-border: #C8BFAF;
    --c-uiFill: #e9e0d4;
    --c-boardBg: #988776;
    --c-menuDivider: #E0D8CF;

    /* Buttons & Interaction Controls */
    --c-btnPrimary: #806C5A;
    --c-btnPrimaryHover: #907B69;
    --c-btnShadow: rgba(139, 115, 85, 0.35);
    --btn-size: 40px;
    --btn-icon-size: 20px;

    /* Unified Border Radius Tokens */
    --radius-sm: 8px;       /* Micro-radius (Tooltip) */
    --radius-md: 15px;      /* Mid-radius (Cấu trúc & Nút phụ) — NOTE: only 1px from --radius-lg; consider consolidating */
    --radius-lg: 16px;      /* Large-radius (Modal/Popup cards) */
    --radius-full: 100px;   /* Full-radius (Nút tương tác chính) */

    /* Dynamic Concentric Radius variables */
    --radius-tile: var(--radius-sm); 
    --padding-board: 10px;
    --radius-board: calc(var(--radius-tile) + var(--padding-board));

    /* Brand accents & highlights */
    --c-gold: #edc22e;       
    --c-accent: #DC9800;     
    --c-danger: #e25c3d;     

    /* Transitions */
    --tr-fast: 0.2s;
    --tr-normal: 0.25s;
    --tr-slow: 0.3s;
}

.dark body {
    --c-bg: #1a1714;
    --c-overlay: rgba(26, 23, 20, 0.7);
    --c-menuBg: rgba(0, 0, 0, 0.4);
    --c-menuPanel: #242018;

    --c-text: #e6e1dc;
    --c-textLight: #f5f5f5;
    --c-textMuted: #B5AFA9;

    --c-border: #3d3630;
    --c-uiFill: #2d2621;
    --c-boardBg: #242018;
    --c-menuDivider: #3d3630;

    --c-btnPrimary: #3D3028;
    --c-btnPrimaryHover: #4D3E34;
    --c-btnShadow: rgba(0, 0, 0, 0.6);
    --c-accent: #edc22e;
}

/* Đảm bảo toàn bộ các thẻ form tự động kế thừa font từ body */
button,
input,
select,
textarea {
    font-family: inherit;
    font-size: inherit;
}

/* ============================================================
   §2  BODY & SAFE-AREA WRAPPER
   ============================================================ */
body {
    font-family: 'Fredoka', sans-serif;
    background-color: var(--c-bg);
    color: var(--c-text);

    touch-action: none;
    overscroll-behavior: none;
    overflow: hidden;
    
    /* Thiết lập không cho select text ở cấp độ body (sẽ mở lại cho textarea) */
    user-select: none;
    -webkit-user-select: none;

    width: 100vw;
    height: 100dvh;
    display: flex;
    align-items: center;
    justify-content: center;
}

/* Tối ưu hóa chuyển đổi Theme dựa trên các lớp chỉ định rõ ràng */
body.theme-transitioning {
    transition: background-color var(--tr-slow) ease;
}
body.theme-transitioning #app-wrapper,
body.theme-transitioning #ui-top,
body.theme-transitioning #ui-bottom,
body.theme-transitioning #game-container,
body.theme-transitioning .score-card,
body.theme-transitioning button,
body.theme-transitioning .btn,
body.theme-transitioning #side-menu,
body.theme-transitioning #menu-panel,
body.theme-transitioning #menu-close-btn,
body.theme-transitioning #menu-theme-switcher,
body.theme-transitioning #tooltip {
    transition: background-color var(--tr-slow) ease, 
                color var(--tr-slow) ease, 
                border-color var(--tr-slow) ease, 
                box-shadow var(--tr-slow) ease;
}

/* ============================================================
   §3  APP LAYOUT (CSS GRID + CONTAINERIZATION)
   ============================================================ */
#app-wrapper {
    width: 100%;
    height: 100%;
    display: grid;
    grid-template-areas:
        "header"
        "board"
        "controls";
    grid-template-columns: 100%;
    grid-template-rows: auto 1fr auto;

    padding-top: max(env(safe-area-inset-top), 0.5rem);
    padding-bottom: max(env(safe-area-inset-bottom), 0.5rem);
    padding-left: max(env(safe-area-inset-left), clamp(0.4rem, 4vw, 1rem));
    padding-right: max(env(safe-area-inset-right), clamp(0.4rem, 4vw, 1rem));
    gap: clamp(0.25rem, 1.5svh, 0.5rem);

    max-width: 1400px;
    margin: 0 auto;
    overflow: hidden;  
}

#ui-panel {
    display: contents;
}

#ui-top {
    grid-area: header;
    display: flex;
    flex-direction: column;
    align-items: center;
    width: 100%;
    max-width: 560px;
    justify-self: center;
    z-index: 10;
    flex-shrink: 0; 
}

#game-center {
    grid-area: board;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    height: 100%;
    min-height: 0;
    min-width: 0;
    z-index: 5;
}

#ui-bottom {
    grid-area: controls;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    width: 100%;
    max-width: 560px;
    justify-self: center;
    gap: clamp(0.4rem, 2svh, 1.2rem);
    z-index: 10;
    flex-shrink: 0; 
    /* 0.5rem bottom margin offsets screen controls and gives breathing room at the screen edge */
    margin-bottom: 0.5rem; 
}



/* ============================================================
   §4  UI COMPONENTS (HEADER, SCORES, BUTTONS, THEME SWITCHER)
   ============================================================ */
#game-header {
    width: 100%;
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: clamp(0.25rem, 2svh, 1.2rem);
}

/* Shared base for header icon buttons (#htp-btn, #menu-btn) */
.header-icon-btn {
    flex-shrink: 0;
    width: var(--btn-size);
    height: var(--btn-size);
    display: flex;
    align-items: center;
    justify-content: center;
    background: transparent;
    border-radius: 50%;
    color: var(--c-textMuted);
    transition: background var(--tr-fast), color var(--tr-fast);
    z-index: 10;
    position: relative;
    box-shadow: none;
}

.header-icon-btn svg {
    display: block;
    color: currentColor;
    width: var(--btn-icon-size);
    height: var(--btn-icon-size);
}

.header-icon-btn::before {
    content: '';
    position: absolute;
    top: -6px;
    left: -6px;
    right: -6px;
    bottom: -6px;
}

@media (hover: hover) {
    .header-icon-btn:hover {
        background: var(--c-uiFill);
        color: var(--c-text);
    }
}

#htp-btn {
    text-decoration: none;
}

#game-title {
    font-weight: 700;
    font-size: clamp(1.4rem, 7.5vmin, 2.6rem);
    text-align: center;
    letter-spacing: 0.02em;
    line-height: 1;
    pointer-events: none;
    white-space: nowrap;
    color: var(--c-text);
    margin: 0;
    flex: 1;
    min-width: 0;
}

#menu-btn {
    border: none;
    cursor: pointer;
}

#menu-theme-switcher {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: clamp(0.5rem, 1.5vh, 1rem) 0;
    border-bottom: 1px solid var(--c-menuDivider);
    margin-bottom: 0.5rem;
}

#menu-theme-label {
    font-size: clamp(1.1rem, 2.5vw, 1.4rem);
    font-weight: 700;
    color: var(--c-text);
}

#dark-toggle-header {
    display: flex;
    align-items: center;
    cursor: pointer;
}

#dark-toggle-icon-header {
    position: relative;
    width: 3.5em;
    height: 1.85em;
    border-radius: 1em;
    background: var(--c-boardBg);
    transition: background 0.3s;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 0.4em;
    overflow: hidden;
}

html:not(.dark) #dark-toggle-icon-header {
    background: #c8bfb0;
}

#dark-toggle-thumb {
    position: absolute;
    top: 0.2em;
    left: 0.2em;
    width: 1.45em;
    height: 1.45em;
    border-radius: 50%;
    background: white;
    transition: transform 0.35s cubic-bezier(0.4, 0, 0.2, 1);
    z-index: 2;
    transform: translateX(0);
}

.dark #dark-toggle-thumb {
    transform: translateX(1.65em);
}

.t-moon {
    left: 0.35em;
}

.t-sun {
    right: 0.35em;
}

.t-moon svg,
.t-sun svg {
    display: block;
    color: var(--c-text);
    width: 0.8em;
    height: 0.8em;
    z-index: 1;
}

#score-row {
    width: 100%;
    display: flex;
    flex-direction: row;
    gap: clamp(0.4rem, 2svh, 1rem);
}

.score-card {
    flex: 1;
    text-align: center;
    padding: clamp(0.25rem, 1.5svh, 0.7rem);
    border: none;
    border-radius: var(--radius-md);
    background: var(--c-uiFill);
    box-shadow: none;
}

.score-label {
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    font-size: clamp(0.72rem, 2.1svh, 1rem);
    line-height: 1;
    margin-bottom: clamp(2px, 0.8svh, 8px);
    color: var(--c-textMuted);
}

.score-value-wrap {
    height: 1.2em;
    display: flex;
    align-items: center;
    justify-content: center;
}

.score-value {
    font-weight: 700;
    font-size: clamp(0.85rem, 4svh, 2.4rem);
    line-height: 1;
    transform-origin: center;
    min-width: 4ch;
    display: inline-block;
    text-align: center;
}

#best-display {
    transition: color 0.4s ease;
}

#new-game-btn {
    background: var(--c-btnPrimary);
    color: var(--c-textLight);
    font-size: clamp(0.95rem, 2.8vmin, 1.5rem);
    padding: 0.75em 1.5em;
    border: none;
    border-radius: var(--radius-full);
    letter-spacing: 0.05em;
    transition: background var(--tr-fast), filter var(--tr-fast);
    min-width: 180px;
    font-weight: 700;
    text-transform: uppercase;
    box-shadow: var(--c-btnShadow);
}

@media (hover: hover) {
    #new-game-btn:hover {
        background: var(--c-btnPrimaryHover);
        filter: brightness(1.05);
    }
}

#new-game-btn:active {
    transform: scale(0.96);
    filter: brightness(0.9);
}

@keyframes score-jump {
    0% { transform: scale(1); }
    20% { transform: scale(1.35); }
    100% { transform: scale(1); }
}

.score-value.jump {
    animation: score-jump 0.5s cubic-bezier(0.175, 0.885, 0.32, 1.275) forwards;
}

/* --- CUSTOM TOOLTIP --- */
#tooltip {
    position: fixed;
    z-index: 10000;
    --tooltip-bg: rgba(28, 25, 23, 0.95);
    --tooltip-border: rgba(255, 255, 255, 0.15);
    --tooltip-shadow: 0 6px 20px rgba(0, 0, 0, 0.35);
    --keycap-bg: #f5f3ef;
    --keycap-color: #1c1917;
    --keycap-border: #d6ccc2;
    --keycap-shadow: #b5a897;

    background: var(--tooltip-bg);
    color: #fff;
    padding: 7px 12px;
    border-radius: var(--radius-sm);
    font-size: 13.5px;
    font-weight: 500;
    pointer-events: none;
    opacity: 0;
    transition: opacity 0.12s cubic-bezier(0.4, 0, 0.2, 1), transform 0.12s cubic-bezier(0.4, 0, 0.2, 1);
    white-space: nowrap;
    border: 1px solid var(--tooltip-border);
    box-shadow: var(--tooltip-shadow);
    display: inline-flex;
    align-items: center;
    will-change: opacity, transform;
}

.dark #tooltip {
    --tooltip-bg: rgba(20, 18, 16, 0.96);
    --tooltip-border: rgba(237, 194, 46, 0.3); 
    --tooltip-shadow: 0 8px 24px rgba(0, 0, 0, 0.6);
    --keycap-bg: #2d2621;
    --keycap-color: #f5f3ef;
    --keycap-border: #4a3e3d;
    --keycap-shadow: #1c1514;
}

.tooltip-keycap {
    font-weight: 700;
    font-size: 0.85em;
    background: var(--keycap-bg);
    color: var(--keycap-color);
    border: 1px solid var(--keycap-border);
    border-radius: 4px;
    padding: 1px 5px;
    margin-left: 6px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 2px 0 var(--keycap-shadow);
    text-transform: uppercase;
    vertical-align: middle;
    position: relative;
    top: -0.5px;
    line-height: 1.2;
}

#tooltip.visible {
    backdrop-filter: blur(4px);
    opacity: 1;
}


/* ============================================================
   §5  GAME CONTAINER & CANVAS
   ============================================================ */
#game-container {
    position: relative;
    box-shadow: 0 16px 32px -8px rgba(0, 0, 0, 0.16);
    
    /* GPU layer cho Canvas — giữ will-change vì OffscreenCanvas cần layer riêng */
    transform: translate3d(0, 0, 0);
    will-change: transform;
    /* backface-visibility removed — không cần cho 2D element */
    
    flex-shrink: 0;
    margin: auto;
    background-color: var(--c-boardBg);
    border-radius: var(--radius-board);
    visibility: hidden;
    
    /* Cô lập Reflow: Ngăn chặn Reflow toàn trang khi bảng thay đổi kích thước */
    contain: layout;
}

#game-container canvas {
    display: block;
    background-color: var(--c-boardBg);
    border-radius: var(--radius-board);
    cursor: grab;
    outline: none;
}

/* ============================================================
   §6  GAME OVERLAYS & INTERACTIVE UTILITIES
   ============================================================ */
button,
a,
[role="button"] {
    /* Đã gỡ bỏ !important để đảm bảo :focus-visible bên dưới hoạt động bình thường */
    outline: none;
    -webkit-tap-highlight-color: transparent;
}

button::-moz-focus-inner {
    border: 0;
}

*:focus-visible {
    outline: 2px solid var(--c-btnPrimary);
    outline-offset: 3px;
}

#game-overlay,
#confirm-overlay {
    position: absolute;
    inset: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    z-index: 50;
    border-radius: inherit;
    background: var(--c-overlay);
    
    /* Layer riêng cho overlay — dùng will-change: opacity vì chỉ transition opacity,
       không dùng translate3d (sai property) hay backface-visibility (không cần cho 2D) */
    will-change: opacity;
    border: 1.5px solid var(--c-border);
    opacity: 1;
    transition: opacity 0.25s ease, background 0.25s ease;
    contain: paint;
}

#game-overlay.overlay-hidden,
#confirm-overlay.overlay-hidden {
    opacity: 0;
}

#confirm-title, #confirm-subtitle, #confirm-buttons,
#overlay-title, #overlay-score, #overlay-buttons {
    transition: opacity 0.28s ease, transform 0.28s cubic-bezier(0.175, 0.885, 0.32, 1.15);
}

.hidden {
    display: none !important;
}

#overlay-title {
    font-weight: 700;
    text-align: center;
    font-size: 2.4em;
    margin-bottom: 0.25em;
}

#overlay-score {
    text-align: center;
    margin-bottom: 1.2em;
    line-height: 1;
}

#overlay-score-label {
    font-weight: 600;
    font-size: 0.75em;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    color: var(--c-textMuted);
    margin-bottom: 0.2em;
}

#overlay-score-value {
    font-weight: 700;
    font-size: 2.8em;
    color: var(--c-text);
    line-height: 1.1;
    white-space: nowrap;
}

#overlay-score.new-best #overlay-score-label,
#overlay-score.new-best #overlay-score-value {
    color: var(--c-accent);
}

.btn {
    font-weight: 700;
    border: none;
    border-radius: var(--radius-full);
    cursor: pointer;
    text-transform: uppercase;
}

@media (hover: hover) {
    .btn:hover {
        filter: brightness(1.1);
        opacity: 1;
    }
}

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

#continue-btn {
    background: var(--c-gold);
    color: var(--c-textLight);
    font-size: 1.05em;
    padding: 0.75em 1.45em;
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}

#try-again-btn {
    background: var(--c-btnPrimary);
    color: var(--c-textLight);
    font-size: 1.05em;
    padding: 0.75em 1.45em;
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}

#confirm-title {
    font-weight: 700;
    font-size: 2.1em;
    color: var(--c-text);
    margin-bottom: 0.35em; 
}

#confirm-subtitle {
    font-size: 0.95em;
    color: var(--c-textMuted);
    margin-bottom: 1.6em; 
}

#confirm-buttons {
    display: flex;
    gap: 1.2em; 
}

#overlay-buttons {
    display: flex;
    gap: 0.75em;
}

#peek-btn {
    position: absolute;
    top: 0.75rem;
    right: 0.75rem;
    width: clamp(2.2rem, 10vmin, 2.6rem);
    height: clamp(2.2rem, 10vmin, 2.6rem);
    background: rgba(0, 0, 0, 0.08);
    border: 1px solid rgba(255, 255, 255, 0.1);
    cursor: pointer;
    color: var(--c-text);
    padding: 0.5rem;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 10;
    transition: background 0.2s ease, color 0.2s ease, opacity 0.2s ease, transform 0.2s ease;
    opacity: 0.45;
}

@media (hover: hover) {
    #peek-btn:hover {
        background: var(--c-btnPrimary);
        color: var(--c-textLight);
        opacity: 1;
        transform: scale(1.1);
    }
}

#peek-btn:active {
    transform: scale(0.9);
}

#peek-btn svg {
    width: 100%;
    height: 100%;
}

#confirm-ok-btn {
    background: var(--c-btnPrimary);
    color: var(--c-textLight);
    font-size: 1.05em;
    padding: 0.75em 1.45em;
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
    border: none;
}

#confirm-cancel-btn {
    background: var(--c-uiFill);
    color: var(--c-text);
    border: 1.5px solid var(--c-border);
    font-size: 1em;
    padding: 0.68em 1.35em;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
}

.interactive {
    transition: opacity 0.15s, transform 0.12s, background 0.2s, color 0.2s, filter 0.15s;
}

@media (hover: hover) {
    .interactive:hover {
        opacity: 0.78;
    }
}

.interactive:active {
    transform: scale(0.92);
    opacity: 0.65;
}

/* ============================================================
   §7  SIDE MENU & CONTROLS
   ============================================================ */
#side-menu {
    position: fixed;
    inset: 0;
    z-index: 1000;
    visibility: hidden;
    pointer-events: none;
    transition: visibility 0.28s;
    display: flex; 
    
    /* Cô lập tránh Repaint & Re-render bên ngoài mà không phá vỡ stacking context của fixed element */
    contain: content;
}

#side-menu.open {
    visibility: visible;
    pointer-events: auto;
}

#menu-backdrop {
    position: absolute;
    inset: 0;
    background: var(--c-menuBg);
    opacity: 0;
    transition: opacity 0.28s ease;
}

#side-menu.open #menu-backdrop {
    opacity: 1;
    backdrop-filter: blur(2px);
}

#menu-panel {
    position: absolute;
    top: 0;
    left: 0;
    bottom: 0;
    width: clamp(260px, 70vw, 320px);
    max-width: 85vw;
    padding-top: max(env(safe-area-inset-top), clamp(1.5rem, 4svh, 3rem));
    padding-right: clamp(1.25rem, 3vw, 2.5rem);
    padding-bottom: max(env(safe-area-inset-bottom), clamp(1.5rem, 4svh, 3rem));
    padding-left: max(env(safe-area-inset-left), clamp(1.25rem, 3vw, 2.5rem));
    display: flex;
    flex-direction: column;
    gap: clamp(0.75rem, 2vh, 1.5rem);
    box-shadow: 4px 0 25px rgba(0, 0, 0, 0.3);
    background: var(--c-menuPanel);
    overflow-y: auto;
    overflow-x: hidden;
    scrollbar-width: none; 

    /* GPU Slide Animation */
    transform: translate3d(-100%, 0, 0);
    transition: transform 0.28s cubic-bezier(0.4, 0, 0.2, 1);
    will-change: transform;
}

#menu-panel::-webkit-scrollbar {
    display: none; 
}

#side-menu.open #menu-panel {
    transform: translate3d(0, 0, 0);
}



#menu-header-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
}

#menu-close-btn {
    width: 2rem;
    height: 2rem;
    background: transparent;
    border: none;
    cursor: pointer;
    color: var(--c-textMuted);
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: var(--radius-md);
    padding: 0;
    flex-shrink: 0;
}

@media (hover: hover) {
    #menu-close-btn:hover {
        color: var(--c-text);
        background: var(--c-menuDivider);
    }
}

#menu-close-btn:active {
    transform: scale(0.9);
}

#menu-label {
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    font-size: clamp(0.75rem, 1.5vw, 0.95rem);
    margin-bottom: 0.25rem;
    color: var(--c-textMuted);
}

.menu-section-label {
    display: block;
    font-size: clamp(1.1rem, 2.5vw, 1.4rem);
    font-weight: 700;
    color: var(--c-text);
}

#menu-game-mode-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 0.75rem;
}

#game-mode-toggle-wrap {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    cursor: pointer;
    color: var(--c-textMuted);
}

#game-mode-current-label {
    font-weight: 700;
    font-size: 1.3rem;
    color: var(--c-text);
}

#game-mode-chevron {
    width: 1em;
    height: 1em;
    transition: transform 0.25s cubic-bezier(0.4, 0, 0.2, 1);
}

.modes-active #game-mode-chevron {
    transform: rotate(180deg);
}

#sidebar-size-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 0.5rem;
    width: 100%;
    margin-top: 1rem;
    max-height: 200px;
    opacity: 1;
    transform: translate3d(0, 0, 0);
    transition: max-height var(--tr-normal) ease, opacity var(--tr-normal) ease, transform var(--tr-normal) ease, margin-top var(--tr-normal) ease;
    overflow: hidden;
}

#sidebar-size-grid.hidden {
    max-height: 0;
    margin-top: 0;
    opacity: 0;
    transform: translate3d(0, -10px, 0);
    pointer-events: none;
}

#sidebar-size-grid button {
    padding: 1rem 0;
    border: none;
    border-radius: var(--radius-md);
    background: var(--c-uiFill);
    color: var(--c-textMuted);
    font-weight: 700;
    font-size: 0.85rem;
    cursor: pointer;
    transition: background 0.2s cubic-bezier(0.4, 0, 0.2, 1), 
                color 0.2s cubic-bezier(0.4, 0, 0.2, 1), 
                transform 0.2s cubic-bezier(0.4, 0, 0.2, 1);
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
    white-space: nowrap;
    overflow: hidden;
    min-width: 0;
}

@media (hover: hover) {
    #sidebar-size-grid button:hover {
        background: var(--c-boardBg);
        color: var(--c-text);
        transform: translate3d(0, -1px, 0);
    }
}

#sidebar-size-grid button.active {
    background: var(--c-btnPrimary);
    color: var(--c-textLight);
}

.dark #sidebar-size-grid button.active {
    background: var(--c-gold);
    color: var(--c-textLight);
}

#sidebar-size-grid button:active {
    transform: scale(0.92);
}

/* ============================================================
   §8  FEEDBACK OVERLAY & EXTRACTED STYLES (COMPONENTS)
   ============================================================ */
#menu-board-size-wrapper {
    padding: clamp(0.75rem, 2vh, 1.25rem) 0;
    border-bottom: 1px solid var(--c-menuDivider);
}

#game-mode-toggle-inner {
    display: flex;
    align-items: center;
    gap: 0.35rem;
}

#game-mode-vibe-label {
    font-size: clamp(0.75rem, 1.8vw, 0.9rem);
    color: var(--c-textMuted);
    font-weight: 500;
}

#menu-footer {
    margin-top: auto;
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
}

/* Shared layout for full-width footer buttons */
.menu-footer-btn {
    width: 100%;
    font-size: 1em;
    padding: 0.8em;
    border-radius: var(--radius-md);
    margin-bottom: 0.25rem;
}

#pwa-install-btn {
    color: var(--c-textLight);
    background: var(--c-btnPrimary);
}

#menu-legal {
    text-align: center;
    margin-top: 0.25rem;
}

#menu-legal-intro {
    font-size: 0.7rem;
    color: var(--c-textMuted);
    opacity: 0.6;
    margin-bottom: 0.25rem;
}

.menu-legal-link {
    font-size: 0.7rem;
    color: var(--c-textMuted);
    text-decoration: underline;
    opacity: 0.8;
}

.menu-legal-sep {
    font-size: 0.7rem;
    color: var(--c-textMuted);
    opacity: 0.3;
}

#peek-icon-hide {
    display: none;
}

#menu-feedback-btn {
    color: var(--c-text);
    background: var(--c-uiFill);
    border: 1.0px solid var(--c-btnPrimary);
    opacity: 0.75;
    transition: background 0.2s, transform 0.1s, opacity 0.2s;
}

@media (hover: hover) {
    #menu-feedback-btn:hover {
        background: var(--c-menuDivider);
        opacity: 1;
    }
}

#menu-feedback-btn:active {
    transform: scale(0.98);
}

/* Feedback Overlay */
.feedback-overlay {
    position: fixed;
    inset: 0;
    z-index: 10000;
    background: var(--c-overlay);
    display: flex;
    align-items: center;
    justify-content: center;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.2s ease;
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    
    /* Cô lập Layer hoàn toàn */
    contain: strict;
}

.feedback-overlay.show {
    opacity: 1;
    pointer-events: auto;
}

.feedback-card {
    background: var(--c-menuPanel);
    border: 1.5px solid var(--c-border);
    border-radius: var(--radius-lg);
    width: clamp(280px, 72vmin, 520px);
    max-width: calc(100dvw - 2rem);
    max-height: min(90dvh, 640px);
    padding: clamp(1rem, 3.5vmin, 1.75rem);
    box-shadow: var(--c-btnShadow);
    transform: scale(0.9);
    transition: transform 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
    text-align: center;
    box-sizing: border-box;
    position: relative;
    perspective: 1000px;
    overflow: auto;
    
    /* Cô lập Content rendering */
    contain: content;
}

.feedback-overlay.show .feedback-card {
    transform: scale(1);
}

.feedback-close-btn {
    position: absolute;
    top: 0.75rem;
    right: 0.75rem;
    width: 2rem;
    height: 2rem;
    border: none;
    border-radius: 50%;
    background: transparent;
    color: var(--c-textMuted);
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: background 0.2s ease, color 0.2s ease, transform 0.15s ease;
    z-index: 2;
}

@media (hover: hover) {
    .feedback-close-btn:hover {
        background: var(--c-menuDivider);
        color: var(--c-text);
    }
}

.feedback-close-btn:active {
    transform: scale(0.92);
}

.feedback-close-btn svg {
    width: 1rem;
    height: 1rem;
}

.feedback-header h2 {
    font-size: clamp(1.1rem, 3vmin, 1.5rem);
    color: var(--c-text);
    margin: 0 0 0.5rem 0;
    font-weight: 700;
}

.feedback-header p {
    font-size: clamp(0.8rem, 2vmin, 0.95rem);
    color: var(--c-textMuted);
    margin: 0 0 clamp(1rem, 2.5vmin, 1.25rem) 0;
    line-height: 1.4;
}

.feedback-stars {
    display: flex;
    justify-content: center;
    gap: clamp(0.35rem, 1.5vmin, 0.6rem);
    margin-bottom: clamp(1rem, 2.5vmin, 1.25rem);
}

.star-btn {
    background: none;
    border: none;
    font-size: clamp(1.8rem, 5.5vmin, 2.6rem);
    color: var(--c-border);
    cursor: pointer;
    padding: 0;
    transition: color 0.15s ease, transform 0.15s cubic-bezier(0.34, 1.56, 0.64, 1);
    outline: none;
}

@media (hover: hover) {
    .star-btn:hover {
        transform: scale(1.2) rotate(8deg);
    }
}

.star-btn:active {
    transform: scale(0.9);
}

.star-btn.active {
    color: var(--c-gold);
    filter: drop-shadow(0 0 4px rgba(237, 194, 46, var(--star-shadow-opacity, 0.4)));
}

.dark {
    --star-shadow-opacity: 0.5;
}

.star-btn.highlighted {
    color: var(--c-gold);
    opacity: 0.8;
}

.feedback-textarea-wrapper {
    margin-bottom: clamp(1rem, 2.5vmin, 1.25rem);
    width: 100%;
}

#feedback-comment {
    width: 100%;
    min-height: clamp(64px, 8vmin, 90px);
    height: auto;
    background: var(--c-uiFill);
    border: 1.5px solid var(--c-border);
    border-radius: var(--radius-md);
    color: var(--c-text);
    padding: clamp(0.5rem, 1.5vmin, 0.75rem);
    font-size: clamp(0.8rem, 2vmin, 0.95rem);
    resize: none;
    outline: none;
    box-sizing: border-box;
    transition: border-color 0.2s ease;
    
    /* Trả lại khả năng lựa chọn văn bản chuẩn cho Textarea */
    user-select: text;
    -webkit-user-select: text;
    touch-action: auto;
    overflow-y: auto;
    scrollbar-width: none;
    -ms-overflow-style: none;
}

#feedback-comment::-webkit-scrollbar {
    display: none;
}

#feedback-comment:focus {
    border-color: var(--c-btnPrimary);
}

.feedback-actions {
    display: flex;
    gap: clamp(0.5rem, 1.5vmin, 0.75rem);
}

.feedback-actions .btn {
    flex: 1;
    padding: 0.7em;
    font-size: clamp(0.82rem, 2vmin, 0.95rem);
    border-radius: var(--radius-md);
    font-weight: 500;
}

.feedback-actions .btn-secondary {
    background: var(--c-uiFill);
    color: var(--c-textMuted);
    border: 1.5px solid var(--c-border);
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.04);
}

@media (hover: hover) {
    .feedback-actions .btn-secondary:hover {
        background: var(--c-menuDivider);
        color: var(--c-text);
        transform: translate3d(0, -1px, 0);
    }
}

.feedback-actions .btn-secondary:active {
    transform: translate3d(0, 0, 0) scale(0.98);
}

.feedback-actions .btn-primary {
    background: var(--c-btnPrimary);
    color: var(--c-textLight);
    border: none;
}

@media (hover: hover) {
    .feedback-actions .btn-primary:hover:not(:disabled) {
        background: var(--c-btnPrimaryHover);
    }
}

.feedback-actions .btn-primary:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

@media (max-width: 480px) {
    .feedback-overlay {
        padding: clamp(1rem, 4vw, 1.5rem);
        box-sizing: border-box;
    }
}

.thankyou-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    animation: thankYouPop 0.3s cubic-bezier(0.34, 1.56, 0.64, 1) forwards;
}

.thankyou-heart {
    font-size: clamp(2.5rem, 8vmin, 3.5rem);
    color: var(--c-danger);
    margin-bottom: 1rem;
    display: inline-block;
    animation: heartBeat 1.2s infinite ease-in-out;
}

.thankyou-title {
    font-size: clamp(1.1rem, 3.5vmin, 1.4rem);
    font-weight: 700;
    color: var(--c-text);
    margin-bottom: 0.5rem;
}

.thankyou-text {
    font-size: clamp(0.8rem, 2vmin, 0.9rem);
    color: var(--c-textMuted);
    line-height: 1.4;
}

@keyframes thankYouPop {
    0% { transform: scale(0.8); opacity: 0; }
    100% { transform: scale(1); opacity: 1; }
}

@keyframes heartBeat {
    0%, 100% { transform: scale(1); }
    25% { transform: scale(1.15); }
    40% { transform: scale(1.05); }
    60% { transform: scale(1.15); }
}

/* ============================================================
   §9  THE RESPONSIVE ENGINE (Luôn đặt ở cuối để đảm bảo tính xếp chồng)
   ============================================================ */

/* ─── §9.1 SIDEBAR CONTAINER INLINE SIZE ─── */
#ui-panel {
    container-type: inline-size;
    container-name: sidebar;
}

/* ─── §9.2 UNIFIED PORTRAIT ENGINE ─── */
@media (orientation: portrait) {
    :root {
        --btn-size: clamp(40px, 8vw + 1svh, 56px);
        --btn-icon-size: clamp(20px, 4vw, 26px);
    }

    #ui-top,
    #ui-bottom {
        max-width: 100%;
    }

    #app-wrapper {
        gap: clamp(4px, 1.2svh, 20px);
    }

    #game-header {
        margin-bottom: clamp(4px, 1.5svh, 16px);
    }

    #game-title {
        font-size: clamp(2.2rem, 9vw, 3.5rem);
    }
    
    #score-row {
        display: flex;
        flex-direction: row;
        gap: clamp(4px, 2.5vw, 16px);
        max-height: 80px;
        opacity: 1;
        margin-bottom: 8px;
        overflow: hidden;
    }

    .score-card {
        display: flex;
        flex-direction: row;
        align-items: center;
        justify-content: space-between;
        padding: clamp(5px, 1.1svh, 11px) clamp(10px, 3vw, 20px);
        min-height: clamp(34px, 4.8svh, 61px);
        border-radius: clamp(10px, 3vmin, 15px);
    }

    .score-label {
        margin-bottom: 0;
        text-align: left;
        font-size: clamp(0.72rem, 1.5vw + 0.5svh, 0.95rem);
    }

    .score-value-wrap { 
        height: auto; 
        justify-content: flex-end; 
    }

    .score-value { 
        font-size: clamp(1.2rem, 4.5vw + 0.5svh, 2.2rem); 
    }

    #new-game-btn {
        min-width: unset;
        width: clamp(200px, 80vw, 280px);
        padding: clamp(0.5em, 1.3svh, 0.95em) clamp(1em, 4vw, 2em);
        font-size: clamp(1rem, 2.5vw + 0.5svh, 1.6rem);
    }
}


/* ─── §9.2.2 PORTRAIT ULTRA-SHORT VIEWPORTS ─── */
@media (orientation: portrait) and (max-height: 285px) {
    #score-row {
        max-height: 0;
        opacity: 0;
        margin-bottom: 0;
    }
}

/* ─── §9.2.3 SIDE MENU TABLET WIDTH ─── */
@media (min-width: 768px) {
    #menu-panel {
        width: clamp(320px, 40vw, 380px);
    }
}

/* ─── §9.3 LANDSCAPE SIDEBAR ENGINE (2-Column Workspace) ─── */
@media (orientation: landscape) {
    #app-wrapper {
        --sidebar-preferred: clamp(120px, 20vw, 270px);
        --sidebar-max: min(var(--sidebar-preferred), 55svh);
        --gap-land: clamp(1.5rem, 4vw, 5rem);
        --board-max: min(115svh, 700px, calc(100% - var(--sidebar-max) - var(--gap-land)));
        grid-template-areas: "sidebar board";
        grid-template-columns: var(--sidebar-max) minmax(0, var(--board-max));
        grid-template-rows: 1fr;
        align-items: center;
        justify-content: center;
        padding-left: max(env(safe-area-inset-left), clamp(0.5rem, 2vw, 1.5rem));
        padding-right: max(env(safe-area-inset-right), clamp(0.5rem, 2vw, 1.5rem));
        gap: var(--gap-land);
    }

    #ui-panel {
        display: flex;
        grid-area: sidebar;
        flex-direction: column;
        justify-content: center;
        height: 100%;
        gap: clamp(0.25rem, 2svh, 2rem);
        padding: 0;
    }

    #ui-top, #ui-bottom { 
        grid-area: auto; 
        max-width: 100%; 
        width: 100%; 
        margin: 0; 
        padding: 0; 
    }

    #game-header { 
        margin-bottom: clamp(0.25rem, 2svh, 2rem); 
    }

    #game-title { 
        font-size: clamp(1.6rem, 20cqi, 4.2rem); 
    }

    :root {
        --btn-size: clamp(42px, 9svh, 56px);
        --btn-icon-size: clamp(20px, 4svh, 26px);
    }

    #score-row {
        flex-direction: column;
        gap: clamp(4px, 1svh, 8px);
    }

    .score-card {
        display: flex;
        flex-direction: row;
        justify-content: space-between;
        align-items: center;
        flex: unset;
        padding: clamp(8px, 1.35svh, 14px) clamp(10px, 2vw, 18px);
    }

    .score-label { 
        margin-bottom: 0; 
        text-align: left; 
    }

    .score-value-wrap { 
        height: auto; 
        justify-content: flex-end; 
    }

    .score-value { 
        font-size: clamp(0.95rem, 3.5svh, 1.8rem); 
        min-width: unset; 
    }

    #new-game-btn {
        min-width: clamp(140px, 80%, 240px);
        width: 100%;
        padding: clamp(9px, 1.6svh, 15px) 0;
        font-size: clamp(0.85rem, 2.4svh, 1.25rem);
        line-height: 1.2;
    }
}

/* ─── §9.3.1 LANDSCAPE BREAKPOINT OVERRIDES ─── */
@media (orientation: landscape) and (min-height: 500px) and (max-width: 900px) {
    #app-wrapper {
        --sidebar-preferred: clamp(180px, 30svh, 240px);
        --gap-land: clamp(1rem, 3vw, 2rem);
    }
}

@media (orientation: landscape) and (min-height: 500px) and (min-width: 900px) and (max-width: 1200px) {
    #app-wrapper {
        --sidebar-preferred: clamp(200px, 22vw, 280px);
        --gap-land: clamp(1.2rem, 3vw, 3rem);
    }
}

@media (orientation: landscape) and (min-width: 1200px) {
    #app-wrapper {
        --sidebar-preferred: clamp(230px, 17vw, 280px);
        --gap-land: clamp(2rem, 4vw, 5rem);
        --board-max: min(90svh, 800px, calc(100% - var(--sidebar-max) - var(--gap-land)));
    }
    #ui-panel {
        height: max-content;
    }
}

/* ─── §9.3.2 LANDSCAPE MOBILE SIDEBAR WIDTH ─── */
@media (orientation: landscape) and (max-height: 430px) and (min-height: 321px) {
    #app-wrapper {
        --sidebar-preferred: clamp(140px, 23vw, 200px);
        --gap-land: clamp(0.5rem, 2vw, 1.2rem);
    }
}

/* ─── §9.4 CONTAINER QUERY OVERRIDES ─── */
@container sidebar (max-width: 230px) {
    #game-header {
        gap: 4px;
        --btn-size: clamp(32px, 8svh, 40px);
        --btn-icon-size: clamp(16px, 4svh, 20px);
    }
}

@container sidebar (max-width: 160px) {
    #game-header {
        gap: 4px;
        --btn-size: 32px;
        --btn-icon-size: 16px;
    }
    #game-title {
        font-size: clamp(1.1rem, 5svh, 1.4rem);
    }
    #score-row {
        flex-direction: column;
        gap: 4px;
    }
    .score-card {
        padding: 4px 8px;
        border-radius: var(--radius-sm);
    }
    .score-label {
        font-size: 0.62rem;
        margin-bottom: 0;
        text-align: center;
    }
    .score-value {
        font-size: 0.85rem;
    }
    #new-game-btn {
        min-width: unset;
        width: 100%;
        padding: 0.5em 1em;
        font-size: clamp(0.7rem, 2.2svh, 1rem);
    }
    #sidebar-size-grid button {
        font-size: 0.7rem;
        padding: 0.6rem 0;
    }
}

/* ─── §9.5 ULTRA-COMPACT LANDSCAPE SUPPORT ─── */
@media (orientation: landscape) and (max-height: 320px) {
    #game-overlay, #confirm-overlay {
        font-size: 0.5rem;
    }
    #overlay-title, #confirm-title {
        margin-bottom: 0.15em;
    }
    #overlay-score {
        margin-bottom: 0.4em;
    }
    #confirm-subtitle {
        margin-bottom: 0.5em;
    }

    #app-wrapper {
        --sidebar-preferred: clamp(100px, 25vw, 120px);
        --sidebar-max: clamp(100px, 25vw, 120px);
        --gap-land: clamp(0.4rem, 1.5vw, 0.8rem);
    }

    #game-title {
        font-size: clamp(1rem, 4svh, 1.4rem);
    }

    .score-label {
        font-size: 0.6rem;
    }

    .score-value {
        font-size: clamp(0.85rem, 3svh, 1.2rem);
    }

    #new-game-btn {
        font-size: clamp(0.7rem, 2.5svh, 1rem);
        padding: 0.4em 0.8em;
        white-space: nowrap;
        min-width: unset;
        width: 100%;
    }
}