:root {
    --bg-graphite: #121316;
    --bg-panel: #1a1c22;
    --bg-cell: #23262f;
    --gold: #F5B301;
    --gold-bright: #ffd147;
    --gold-dim: rgba(245, 179, 1, 0.18);
    --ivory: #F7F7F7;
    --muted-text: #a5acb8;
    --border-subtle: #2e323d;
    --gap: clamp(4px, 2vmin, 10px);
    --pad: clamp(5px, 2vmin, 10px);
    --board-size: min(calc(100vw - 24px), calc(100vh - 110px), 420px);
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    user-select: none;
    -webkit-user-select: none;
}

html, body {
    width: 100%;
    height: 100%;
    background-color: var(--bg-graphite);
    color: var(--ivory);
    font-family: system-ui, -apple-system, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
    overflow: hidden;
    touch-action: manipulation;
}

body {
    display: flex;
    justify-content: center;
    align-items: center;
}

.game-shell {
    width: 100%;
    max-width: 520px;
    height: 100%;
    max-height: 100vh;
    padding: 10px 12px;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    align-items: center;
    gap: 6px;
}

/* HUD Header */
.hud {
    width: min(100%, 420px);
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    align-items: center;
    gap: 6px;
    flex-shrink: 0;
}

.brand {
    display: flex;
    flex-direction: column;
}

.brand-tag {
    font-size: 9px;
    letter-spacing: 0.14em;
    color: var(--gold);
    font-weight: 700;
}

.game-title {
    font-size: clamp(18px, 4.5vw, 24px);
    font-weight: 800;
    letter-spacing: 0.03em;
    color: var(--ivory);
    line-height: 1.05;
}

.game-title span {
    color: var(--gold);
    text-shadow: 0 0 12px rgba(245, 179, 1, 0.45);
}

.hud-right {
    display: flex;
    align-items: center;
    gap: 6px;
    flex-wrap: wrap;
    justify-content: flex-end;
}

.scores {
    display: flex;
    gap: 6px;
}

.score-box {
    background: var(--bg-panel);
    border: 1px solid var(--border-subtle);
    border-radius: 6px;
    padding: 3px 9px;
    min-width: 64px;
    display: flex;
    flex-direction: column;
    align-items: center;
}

.score-label {
    font-size: 9px;
    font-weight: 700;
    letter-spacing: 0.08em;
    color: var(--muted-text);
}

.score-val {
    font-size: 14px;
    font-weight: 800;
    color: var(--gold);
    font-variant-numeric: tabular-nums;
}

.hud-actions {
    display: flex;
    gap: 6px;
}

/* Buttons & Focus States */
button {
    font-family: inherit;
    cursor: pointer;
    border: none;
    background: none;
    color: var(--ivory);
}

button:focus,
button:focus-visible {
    outline: 3px solid var(--gold);
    outline-offset: 2px;
}

.ctrl-btn {
    background: var(--bg-panel);
    color: var(--ivory);
    border: 1px solid var(--border-subtle);
    border-radius: 6px;
    padding: 6px 9px;
    font-size: 12px;
    font-weight: 600;
    display: inline-flex;
    align-items: center;
    gap: 4px;
    transition: background-color 0.15s, border-color 0.15s, transform 0.1s;
}

.ctrl-btn:hover {
    border-color: var(--gold);
    background: #23262f;
}

.ctrl-btn[aria-pressed="true"] {
    border-color: var(--gold);
    color: var(--gold);
}

.ctrl-btn-accent {
    background: var(--gold-dim);
    border-color: var(--gold);
    color: var(--gold);
}

.ctrl-btn-accent:hover {
    background: var(--gold);
    color: var(--bg-graphite);
}

/* Board Stage */
.board-stage {
    display: flex;
    justify-content: center;
    align-items: center;
    flex: 1 1 0;
    width: 100%;
    min-height: 0;
    min-width: 0;
}

.board-container {
    position: relative;
    width: var(--board-size);
    height: var(--board-size);
    background: var(--bg-panel);
    border: 2px solid var(--border-subtle);
    border-radius: 12px;
    padding: var(--pad);
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.55), inset 0 0 0 1px rgba(245, 179, 1, 0.08);
    touch-action: none;
    overflow: hidden;
}

.grid-bg {
    width: 100%;
    height: 100%;
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    grid-template-rows: repeat(4, 1fr);
    gap: var(--gap);
}

.grid-cell {
    background: var(--bg-cell);
    border-radius: 8px;
    border: 1px solid rgba(255, 255, 255, 0.03);
}

.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

.tile-layer {
    position: absolute;
    inset: var(--pad);
    pointer-events: none;
}

.tile-layer:focus {
    outline: none;
}

.tile {
    --r: 0;
    --c: 0;
    position: absolute;
    top: 0;
    left: 0;
    width: calc((100% - 3 * var(--gap)) / 4);
    height: calc((100% - 3 * var(--gap)) / 4);
    transform: translate(
        calc(var(--c) * (100% + var(--gap))),
        calc(var(--r) * (100% + var(--gap)))
    );
    border-radius: 8px;
    display: flex;
    justify-content: center;
    align-items: center;
    font-weight: 800;
    font-size: clamp(12px, calc(var(--board-size) * 0.072), 30px);
    font-variant-numeric: tabular-nums;
    transition: transform 130ms ease-out;
    will-change: transform;
}

.tile-inner {
    width: 100%;
    height: 100%;
    border-radius: 8px;
    display: flex;
    justify-content: center;
    align-items: center;
    border: 1px solid transparent;
}

.tile-new .tile-inner {
    animation: tile-appear 150ms ease-out;
}

.tile-merged .tile-inner {
    animation: tile-pop 170ms ease-out;
}

@keyframes tile-appear {
    0% { transform: scale(0.35); opacity: 0; }
    100% { transform: scale(1); opacity: 1; }
}

@keyframes tile-pop {
    0% { transform: scale(1); }
    50% { transform: scale(1.16); }
    100% { transform: scale(1); }
}

/* Tile Color Palette - Graphite, Gold, Ivory */
.tile-2 .tile-inner {
    background: #272b35;
    color: var(--ivory);
    border-color: #383e4c;
}

.tile-4 .tile-inner {
    background: #303543;
    color: var(--ivory);
    border-color: #4a5164;
}

.tile-8 .tile-inner {
    background: #3b3322;
    color: var(--gold-bright);
    border-color: #6b5620;
}

.tile-16 .tile-inner {
    background: #4e3f16;
    color: var(--gold-bright);
    border-color: #8f6e13;
}

.tile-32 .tile-inner {
    background: #68500f;
    color: var(--ivory);
    border-color: #b5890c;
}

.tile-64 .tile-inner {
    background: #876508;
    color: var(--ivory);
    border-color: var(--gold);
    box-shadow: 0 0 10px rgba(245, 179, 1, 0.22);
}

.tile-128 .tile-inner {
    background: #a87c05;
    color: var(--bg-graphite);
    border-color: var(--gold-bright);
    font-size: clamp(10px, calc(var(--board-size) * 0.057), 24px);
    box-shadow: 0 0 12px rgba(245, 179, 1, 0.3);
}

.tile-256 .tile-inner {
    background: #c49003;
    color: var(--bg-graphite);
    border-color: var(--ivory);
    font-size: clamp(10px, calc(var(--board-size) * 0.057), 24px);
    box-shadow: 0 0 14px rgba(245, 179, 1, 0.4);
}

.tile-512 .tile-inner {
    background: #df9f02;
    color: var(--bg-graphite);
    border-color: var(--ivory);
    font-size: clamp(10px, calc(var(--board-size) * 0.057), 24px);
    box-shadow: 0 0 18px rgba(245, 179, 1, 0.5);
}

.tile-1024 .tile-inner {
    background: var(--gold);
    color: var(--bg-graphite);
    border-color: var(--ivory);
    font-size: clamp(9px, calc(var(--board-size) * 0.048), 20px);
    box-shadow: 0 0 22px rgba(245, 179, 1, 0.65);
}

.tile-2048 .tile-inner,
.tile-super .tile-inner {
    background: var(--ivory);
    color: var(--bg-graphite);
    border: 2px solid var(--gold);
    font-size: clamp(9px, calc(var(--board-size) * 0.048), 20px);
    box-shadow: 0 0 26px rgba(245, 179, 1, 0.85);
}

/* Overlay Panel */
.overlay {
    position: absolute;
    inset: 0;
    background: rgba(18, 19, 22, 0.92);
    backdrop-filter: blur(4px);
    display: flex;
    justify-content: center;
    align-items: center;
    padding: clamp(4px, calc(var(--board-size) * 0.034), 14px);
    overflow-y: auto;
    overscroll-behavior: contain;
    z-index: 20;
}

.overlay.hidden,
.hidden {
    display: none !important;
}

.overlay-panel {
    width: 100%;
    max-width: 340px;
    margin: auto;
    background: var(--bg-panel);
    border: 1.5px solid var(--gold);
    border-radius: 10px;
    padding: clamp(6px, calc(var(--board-size) * 0.034), 14px) clamp(6px, calc(var(--board-size) * 0.038), 16px);
    text-align: center;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: clamp(3px, calc(var(--board-size) * 0.019), 8px);
    box-shadow: 0 12px 32px rgba(0, 0, 0, 0.7);
}

.overlay-kicker {
    font-size: clamp(8px, calc(var(--board-size) * 0.024), 10px);
    font-weight: 700;
    letter-spacing: 0.14em;
    color: var(--gold);
}

.overlay-panel h2 {
    font-size: clamp(12px, calc(var(--board-size) * 0.057), 24px);
    line-height: 1.1;
    color: var(--ivory);
    font-weight: 800;
}

.overlay-panel p {
    font-size: clamp(9px, calc(var(--board-size) * 0.031), 13px);
    line-height: 1.28;
    color: var(--muted-text);
}

.overlay-panel p strong {
    color: var(--gold);
}

.overlay-stats {
    display: flex;
    gap: clamp(4px, calc(var(--board-size) * 0.024), 10px);
    width: 100%;
    justify-content: center;
    margin: 2px 0;
}

.stat-pill {
    flex: 1;
    background: var(--bg-cell);
    border: 1px solid var(--border-subtle);
    border-radius: 6px;
    padding: clamp(2px, calc(var(--board-size) * 0.015), 6px) clamp(4px, calc(var(--board-size) * 0.019), 8px);
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.stat-pill span {
    font-size: clamp(8px, calc(var(--board-size) * 0.024), 10px);
    color: var(--muted-text);
}

.stat-pill strong {
    font-size: clamp(11px, calc(var(--board-size) * 0.038), 16px);
    color: var(--gold);
}

.overlay-buttons {
    display: flex;
    gap: clamp(4px, calc(var(--board-size) * 0.019), 8px);
    flex-wrap: wrap;
    justify-content: center;
    margin-top: 2px;
}

.primary-btn {
    background: var(--gold);
    color: var(--bg-graphite);
    font-weight: 800;
    font-size: clamp(10px, calc(var(--board-size) * 0.031), 13px);
    padding: clamp(4px, calc(var(--board-size) * 0.019), 8px) clamp(8px, calc(var(--board-size) * 0.043), 18px);
    border-radius: 6px;
    transition: background-color 0.15s, transform 0.1s;
}

.primary-btn:hover {
    background: var(--gold-bright);
}

.secondary-btn {
    background: var(--bg-cell);
    color: var(--ivory);
    border: 1px solid var(--gold);
    font-weight: 700;
    font-size: clamp(10px, calc(var(--board-size) * 0.031), 13px);
    padding: clamp(4px, calc(var(--board-size) * 0.019), 8px) clamp(8px, calc(var(--board-size) * 0.034), 14px);
    border-radius: 6px;
}

.secondary-btn:hover {
    background: var(--gold-dim);
}

/* Footer Controls */
.footer-controls {
    width: min(100%, 420px);
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 8px;
    flex-shrink: 0;
}

.hint-text {
    font-size: 11px;
    color: var(--muted-text);
}

.dpad {
    display: flex;
    gap: 4px;
}

.dpad-btn {
    width: 32px;
    height: 28px;
    background: var(--bg-panel);
    border: 1px solid var(--border-subtle);
    border-radius: 5px;
    color: var(--ivory);
    font-size: 14px;
    font-weight: 700;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}

.dpad-btn:hover {
    border-color: var(--gold);
    color: var(--gold);
}

@media (max-width: 400px) {
    :root {
        --gap: clamp(4px, 2vmin, 8px);
        --pad: clamp(5px, 2vmin, 8px);
        --board-size: min(calc(100vw - 20px), calc(100vh - 104px), 340px);
    }
    .ctrl-btn {
        padding: 5px 7px;
        font-size: 11px;
    }
    .hint-text {
        font-size: 10px;
    }
}

@media (max-height: 440px) {
    .game-shell {
        padding: 6px 10px;
        gap: 4px;
    }
    .brand-tag {
        display: none;
    }
    .score-box {
        padding: 2px 7px;
        min-width: 52px;
    }
    .score-val {
        font-size: 12px;
    }
    .ctrl-btn {
        padding: 4px 8px;
        font-size: 11px;
    }
    .dpad-btn {
        width: 28px;
        height: 24px;
        font-size: 13px;
    }
}

@media (max-height: 280px) {
    :root {
        --board-size: min(calc(100vw - 16px), calc(100vh - 54px), 340px);
    }
    .game-shell {
        padding: 4px 8px;
        gap: 2px;
    }
    .hud {
        gap: 4px;
    }
    .game-title {
        font-size: 13px;
    }
    #mute-label {
        display: none;
    }
    .score-box {
        padding: 1px 5px;
        min-width: 40px;
    }
    .score-label {
        font-size: 7.5px;
    }
    .score-val {
        font-size: 11px;
    }
    .ctrl-btn {
        padding: 3px 6px;
        font-size: 10px;
    }
    .overlay-kicker {
        display: none;
    }
    .footer-controls {
        gap: 4px;
    }
    .hint-text {
        font-size: 8.5px;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }
    .dpad-btn {
        width: 24px;
        height: 18px;
        font-size: 11px;
    }
}

@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after,
    .tile,
    .tile-new .tile-inner,
    .tile-merged .tile-inner {
        transition: none !important;
        animation: none !important;
    }
}
