@import url('https://fonts.googleapis.com/css2?family=Bebas+Neue&family=DM+Sans:opsz,wght@9..40,400;9..40,500;9..40,700&family=DM+Mono:wght@400;500&display=swap');

:root {
    --bg:          #0b0907;
    --surface:     #161210;
    --surface-2:   #1e1a15;
    --surface-3:   #28231c;
    --surface-4:   #322b22;
    --gold:        #d49020;
    --gold-bright: #f0b030;
    --gold-glow:   rgba(240,176,48,0.22);
    --border:      rgba(210,150,50,0.22);
    --border-dim:  rgba(210,150,50,0.09);
    --text:        #ede0cc;
    --text-dim:    #7a6b58;
    --text-muted:  #40352a;
    --board-bg:    #28211a;
    --cell-bg:     #1d1710;
    --radius:      10px;
    --radius-sm:   6px;
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

body {
    min-height: 100vh;
    background:
        radial-gradient(ellipse 80% 50% at 5% -5%,  rgba(190,100,15,0.16) 0%, transparent 55%),
        radial-gradient(ellipse 60% 60% at 95% 105%, rgba(110,55,5,0.12)  0%, transparent 55%),
        var(--bg);
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 20px 12px;
    font-family: 'DM Sans', system-ui, sans-serif;
    -webkit-tap-highlight-color: transparent;
    color: var(--text);
}

/* Grain texture overlay */
body::after {
    content: '';
    position: fixed;
    inset: 0;
    background-image: url("data:image/svg+xml,%3Csvg viewBox='0 0 256 256' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.75' numOctaves='4' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)' opacity='0.035'/%3E%3C/svg%3E");
    background-size: 256px 256px;
    pointer-events: none;
    z-index: 200;
}

/* ── Game Container ─────────────── */
#game-container {
    width: min(450px, 100%);
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 24px 18px 28px;
    display: flex;
    flex-direction: column;
    align-items: center;
    position: relative;
    animation: container-in 350ms cubic-bezier(0.22,1,0.36,1) both;
    box-shadow:
        0 0 0 1px var(--border-dim),
        0 2px 0  rgba(255,165,50,0.06) inset,
        0 40px 100px rgba(0,0,0,0.75),
        0  8px  24px rgba(0,0,0,0.50);
}

@keyframes container-in {
    from { opacity: 0; transform: translateY(16px) scale(0.98); }
    to   { opacity: 1; transform: translateY(0)   scale(1); }
}

/* ── Header ─────────────────────── */
#game-title {
    font-family: 'Bebas Neue', sans-serif;
    font-size: clamp(52px, 14vw, 72px);
    letter-spacing: 0.04em;
    line-height: 1;
    margin-bottom: 4px;
    color: var(--gold-bright);
    text-shadow: 0 0 40px rgba(240,176,48,0.30);
}

#game-title span {
    color: var(--text-dim);
    font-size: 0.62em;
}

.sub-title {
    font-family: 'DM Mono', monospace;
    font-size: 10px;
    letter-spacing: 0.24em;
    text-transform: uppercase;
    color: var(--gold);
    margin-bottom: 22px;
    opacity: 0.85;
}

/* ── Scores ─────────────────────── */
#score-container {
    display: flex;
    width: 100%;
    gap: 8px;
    margin-bottom: 12px;
}

.score-box {
    flex: 1;
    background: var(--surface-2);
    border: 1px solid var(--border-dim);
    border-radius: var(--radius-sm);
    padding: 10px 14px 12px;
    text-align: center;
    position: relative;
    overflow: hidden;
}

.score-box::before {
    content: '';
    position: absolute;
    top: 0; left: 0; right: 0;
    height: 1px;
    background: linear-gradient(90deg, transparent, var(--gold-glow), transparent);
}

.score-box span {
    display: block;
    font-family: 'DM Mono', monospace;
    font-size: 9px;
    letter-spacing: 0.24em;
    text-transform: uppercase;
    color: var(--text-dim);
    margin-bottom: 5px;
}

.score-box div {
    font-family: 'DM Mono', monospace;
    font-size: 24px;
    font-weight: 500;
    color: var(--gold-bright);
    line-height: 1;
}

/* ── Buttons ────────────────────── */
#button-row {
    display: flex;
    width: 100%;
    gap: 8px;
    margin-bottom: 12px;
}

.btn {
    flex: 1;
    height: 44px;
    padding: 0 14px;
    background: var(--surface-3);
    color: var(--text);
    border: 1px solid rgba(255,255,255,0.06);
    border-radius: var(--radius-sm);
    font-family: 'DM Mono', monospace;
    font-size: 11px;
    font-weight: 500;
    letter-spacing: 0.13em;
    text-transform: uppercase;
    cursor: pointer;
    touch-action: manipulation;
    user-select: none;
    transition: background 100ms, border-color 100ms, color 100ms;
}

.btn:hover {
    background: var(--surface-4);
    border-color: rgba(255,255,255,0.10);
}

.btn:active {
    background: rgba(210,150,30,0.20);
    border-color: rgba(210,150,30,0.50);
}

.btn-undo {
    color: var(--gold);
    border-color: rgba(210,150,30,0.20);
}

.btn-undo:hover {
    border-color: rgba(210,150,30,0.40);
}

#player-row {
    width: 100%;
    margin-bottom: 12px;
}

#player-name {
    width: 100%;
    height: 36px;
    padding: 0 12px;
    background: var(--surface-2);
    color: var(--text);
    border: 1px solid var(--border-dim);
    border-radius: var(--radius-sm);
    font-family: 'DM Mono', monospace;
    font-size: 12px;
    outline: none;
    transition: border-color 150ms;
}

#player-name::placeholder {
    color: var(--text-muted);
}

#player-name:focus {
    border-color: var(--border);
}

/* ── Autopilot ──────────────────── */
#autopilot-controls {
    width: 100%;
    margin-bottom: 14px;
    background: var(--surface-2);
    border: 1px solid var(--border-dim);
    border-radius: var(--radius-sm);
    padding: 12px 14px;
}

.autopilot-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 10px;
}

.toggle-label {
    display: flex;
    align-items: center;
    gap: 10px;
    cursor: pointer;
    font-family: 'DM Mono', monospace;
    font-size: 10px;
    letter-spacing: 0.16em;
    text-transform: uppercase;
    color: var(--text-dim);
    user-select: none;
}

.toggle-switch {
    position: relative;
    display: inline-block;
    width: 44px;
    height: 24px;
}

.toggle-switch input {
    opacity: 0;
    width: 0;
    height: 0;
    position: absolute;
}

.slider {
    position: absolute;
    inset: 0;
    background: var(--surface-4);
    border: 1px solid rgba(255,255,255,0.08);
    border-radius: 24px;
    cursor: pointer;
    transition: background 0.2s, border-color 0.2s;
}

.knob {
    position: absolute;
    top: 4px;
    left: 4px;
    width: 14px;
    height: 14px;
    background: var(--text-dim);
    border-radius: 50%;
    transition: transform 0.2s ease, background 0.2s;
    box-shadow: 0 1px 5px rgba(0,0,0,0.6);
    pointer-events: none;
}

.autopilot-status {
    font-family: 'DM Mono', monospace;
    font-size: 10px;
    letter-spacing: 0.16em;
    text-transform: uppercase;
    color: var(--text-muted);
}

.speed-row {
    display: flex;
    align-items: center;
    gap: 10px;
}

.speed-label-text {
    font-family: 'DM Mono', monospace;
    font-size: 10px;
    letter-spacing: 0.16em;
    text-transform: uppercase;
    color: var(--text-dim);
    white-space: nowrap;
}

#speed-slider {
    flex: 1;
    accent-color: var(--gold);
    cursor: pointer;
}

.speed-value {
    font-family: 'DM Mono', monospace;
    font-size: 10px;
    color: var(--text-dim);
    min-width: 42px;
    text-align: right;
}

/* ── Game Board ─────────────────── */
#game-board {
    width: 100%;
    max-width: 415px;
    aspect-ratio: 1 / 1;
    /* Empty cell wells rendered as SVG background, scales with board */
    background-color: var(--board-bg);
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 415 415'%3E%3Crect x='15'  y='15'  width='85' height='85' rx='6' fill='%231d1710'/%3E%3Crect x='115' y='15'  width='85' height='85' rx='6' fill='%231d1710'/%3E%3Crect x='215' y='15'  width='85' height='85' rx='6' fill='%231d1710'/%3E%3Crect x='315' y='15'  width='85' height='85' rx='6' fill='%231d1710'/%3E%3Crect x='15'  y='115' width='85' height='85' rx='6' fill='%231d1710'/%3E%3Crect x='115' y='115' width='85' height='85' rx='6' fill='%231d1710'/%3E%3Crect x='215' y='115' width='85' height='85' rx='6' fill='%231d1710'/%3E%3Crect x='315' y='115' width='85' height='85' rx='6' fill='%231d1710'/%3E%3Crect x='15'  y='215' width='85' height='85' rx='6' fill='%231d1710'/%3E%3Crect x='115' y='215' width='85' height='85' rx='6' fill='%231d1710'/%3E%3Crect x='215' y='215' width='85' height='85' rx='6' fill='%231d1710'/%3E%3Crect x='315' y='215' width='85' height='85' rx='6' fill='%231d1710'/%3E%3Crect x='15'  y='315' width='85' height='85' rx='6' fill='%231d1710'/%3E%3Crect x='115' y='315' width='85' height='85' rx='6' fill='%231d1710'/%3E%3Crect x='215' y='315' width='85' height='85' rx='6' fill='%231d1710'/%3E%3Crect x='315' y='315' width='85' height='85' rx='6' fill='%231d1710'/%3E%3C/svg%3E");
    background-size: 100% 100%;
    border-radius: var(--radius-sm);
    border: 1px solid rgba(0,0,0,0.55);
    position: relative;
    overflow: hidden;
    touch-action: none;
    user-select: none;
    -webkit-user-select: none;
    box-shadow:
        inset 0  3px 12px rgba(0,0,0,0.65),
        inset 0 -1px  4px rgba(255,140,20,0.04),
              0  6px 20px rgba(0,0,0,0.45);
}

/* Subtle top-edge highlight on the board */
#game-board::before {
    content: '';
    position: absolute;
    top: 0; left: 0; right: 0;
    height: 1px;
    background: linear-gradient(90deg, transparent 10%, rgba(255,160,40,0.08) 50%, transparent 90%);
    pointer-events: none;
    z-index: 1;
}

/* ── Tile animations ────────────── */
@keyframes tile-spawn {
    0%   { transform: scale(0);    opacity: 0; }
    65%  { transform: scale(1.12); opacity: 1; }
    100% { transform: scale(1); }
}
/* ── Tiles ──────────────────────── */
.tile {
    position: absolute;
    border-radius: 6px;
    display: flex;
    justify-content: center;
    align-items: center;
    font-family: 'DM Sans', sans-serif;
    font-weight: 700;
    will-change: transform;
}

.tile-new { animation: tile-spawn 140ms cubic-bezier(0.34,1.56,0.64,1) both; }

/* Cream tier */
.tile-2    { background: #f0e6d5; color: #4a3820; box-shadow: 0 2px 8px rgba(0,0,0,0.30); }
.tile-4    { background: #e8d4a8; color: #4a3820; box-shadow: 0 2px 8px rgba(0,0,0,0.30); }

/* Ember tier */
.tile-8    { background: #e8955a; color: #fff; box-shadow: 0 2px 10px rgba(0,0,0,0.35); }
.tile-16   { background: #e07840; color: #fff; box-shadow: 0 2px 10px rgba(0,0,0,0.35); }
.tile-32   { background: #d95f30; color: #fff; box-shadow: 0 2px 12px rgba(217,95,48,0.40); }
.tile-64   { background: #cc3e18; color: #fff; box-shadow: 0 2px 14px rgba(204,62,24,0.45); }

/* Gold tier — the prize tiles */
.tile-128  {
    background: linear-gradient(135deg, #c89010, #b07808);
    color: #fff;
    box-shadow: 0 3px 16px rgba(200,144,16,0.50), 0 0 0 1px rgba(255,200,60,0.15);
}
.tile-256  {
    background: linear-gradient(135deg, #d4a018, #bc8a10);
    color: #fff;
    box-shadow: 0 3px 18px rgba(212,160,24,0.55), 0 0 0 1px rgba(255,210,70,0.18);
}
.tile-512  {
    background: linear-gradient(135deg, #dcb020, #c49818);
    color: #fff;
    box-shadow: 0 3px 22px rgba(220,176,32,0.60), 0 0 0 1px rgba(255,218,80,0.20);
}
.tile-1024 {
    background: linear-gradient(135deg, #e8c028, #d0a820);
    color: #fff;
    box-shadow: 0 4px 28px rgba(232,192,40,0.65), 0 0 0 1px rgba(255,228,90,0.25);
}
.tile-2048 {
    background: linear-gradient(135deg, #f5cc30, #e0b020);
    color: #2a1800;
    box-shadow: 0 4px 36px rgba(245,204,48,0.80), 0 0 0 1px rgba(255,238,100,0.35), 0 0 60px rgba(245,204,48,0.25);
}

/* Beyond — vivid spectrum */
.tile-4096   { background: #14b0a8; color: #fff; box-shadow: 0 3px 20px rgba(20,176,168,0.55); }
.tile-8192   { background: #8820cc; color: #fff; box-shadow: 0 3px 22px rgba(136,32,204,0.55); }
.tile-16384  { background: #de1848; color: #fff; box-shadow: 0 3px 22px rgba(222,24,72,0.55); }
.tile-32768  { background: #f06010; color: #fff; box-shadow: 0 3px 22px rgba(240,96,16,0.55); }
.tile-65536  { background: #1058bc; color: #fff; box-shadow: 0 3px 22px rgba(16,88,188,0.55); }
.tile-131072 { background: #166828; color: #fff; box-shadow: 0 3px 22px rgba(22,104,40,0.55); }

/* ── Game Over overlay ──────────── */
#game-over {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: min(300px, 88%);
    background: rgba(12,9,6,0.92);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    display: none;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    padding: 34px 24px;
    z-index: 10;
    backdrop-filter: blur(16px);
    -webkit-backdrop-filter: blur(16px);
    box-shadow:
        0 0 0 1px var(--border-dim),
        0 30px 70px rgba(0,0,0,0.85);
}

#game-over div {
    font-family: 'Bebas Neue', sans-serif;
    font-size: 52px;
    letter-spacing: 0.06em;
    color: var(--gold-bright);
    margin-bottom: 24px;
    line-height: 1;
    text-shadow: 0 0 30px rgba(240,176,48,0.4);
}

#game-over button {
    height: 46px;
    padding: 0 28px;
    background: var(--surface-3);
    color: var(--text);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    font-family: 'DM Mono', monospace;
    font-size: 12px;
    font-weight: 500;
    letter-spacing: 0.16em;
    text-transform: uppercase;
    cursor: pointer;
    touch-action: manipulation;
    transition: background 100ms, border-color 100ms;
    min-height: 44px;
}

#game-over button:active {
    background: rgba(210,150,30,0.22);
    border-color: var(--gold);
}
