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

:root {
    --wingo-dark: #165F84;
    --wingo-mid: #336F91;
    --wingo-light: #4A8BAD;
    --wingo-red: #E43312;
    --wingo-red-hover: #C62B0F;
    --board-bg: #0E4562;
    --cell-bg: #1B6E96;
    --board-radius: 12px;
    --cell-radius: 8px;
    --gap: 2.4%;
    --cell-size: 22%;
}

html, body {
    height: 100%;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Arial, sans-serif;
    background-color: var(--wingo-dark);
    color: #FFF;
    overflow: hidden;
    -webkit-tap-highlight-color: transparent;
}

#game-wrap {
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    max-width: 420px;
    margin: 0 auto;
    padding: 12px 16px;
}

#game-header {
    width: 100%;
    display: flex;
    align-items: center;
    gap: 12px;
    margin-bottom: 16px;
    flex-shrink: 0;
}

.back-link {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background: var(--wingo-mid);
    color: #FFF;
    text-decoration: none;
    transition: background 0.2s;
    flex-shrink: 0;
}

.back-link:hover {
    background: var(--wingo-light);
}

.header-logo {
    height: 36px;
    width: auto;
}

#game-container {
    width: 100%;
    flex: 1;
    display: flex;
    flex-direction: column;
    min-height: 0;
}

.top-bar {
    display: flex;
    align-items: stretch;
    gap: 8px;
    margin-bottom: 12px;
    flex-shrink: 0;
}

.score-box {
    flex: 1;
    background: var(--board-bg);
    border-radius: 8px;
    padding: 8px 12px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    min-width: 0;
}

.score-label {
    font-size: 11px;
    text-transform: uppercase;
    letter-spacing: 1px;
    opacity: 0.6;
    line-height: 1;
}

.score-value {
    font-size: 22px;
    font-weight: 700;
    line-height: 1.2;
    letter-spacing: -0.5px;
}

.new-game-btn {
    background: var(--wingo-red);
    color: #FFF;
    border: none;
    border-radius: 8px;
    padding: 8px 16px;
    font-size: 14px;
    font-weight: 700;
    cursor: pointer;
    transition: background 0.2s;
    white-space: nowrap;
    font-family: inherit;
}

.new-game-btn:hover {
    background: var(--wingo-red-hover);
}

#board {
    position: relative;
    width: 100%;
    padding-bottom: 100%;
    background: var(--board-bg);
    border-radius: var(--board-radius);
    overflow: hidden;
    flex-shrink: 0;
}

.grid-row {
    position: absolute;
    left: 0;
    width: 100%;
    height: var(--cell-size);
    padding: 0 var(--gap);
    display: flex;
    gap: var(--gap);
}

.grid-row:nth-child(1) { top: var(--gap); }
.grid-row:nth-child(2) { top: calc(var(--gap) + 1 * (var(--cell-size) + var(--gap))); }
.grid-row:nth-child(3) { top: calc(var(--gap) + 2 * (var(--cell-size) + var(--gap))); }
.grid-row:nth-child(4) { top: calc(var(--gap) + 3 * (var(--cell-size) + var(--gap))); }

.cell {
    flex: 1;
    background: var(--cell-bg);
    border-radius: var(--cell-radius);
}

#tile-layer {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
}

.tile {
    position: absolute;
    width: var(--cell-size);
    height: var(--cell-size);
    border-radius: var(--cell-radius);
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 700;
    font-size: 2rem;
    transition: top 0.12s ease, left 0.12s ease;
    z-index: 1;
}

.tile.tile-new {
    animation: pop-in 0.2s ease forwards;
}

.tile.tile-merged {
    animation: merge-pop 0.2s ease forwards;
    z-index: 2;
}

@keyframes pop-in {
    0%   { transform: scale(0); opacity: 0; }
    60%  { transform: scale(1.1); }
    100% { transform: scale(1); opacity: 1; }
}

@keyframes merge-pop {
    0%   { transform: scale(1); }
    40%  { transform: scale(1.2); }
    100% { transform: scale(1); }
}

/* Tile positions */
.tile-pos-0-0 { left: 2.4%;  top: 2.4%; }
.tile-pos-1-0 { left: 26.8%; top: 2.4%; }
.tile-pos-2-0 { left: 51.2%; top: 2.4%; }
.tile-pos-3-0 { left: 75.6%; top: 2.4%; }
.tile-pos-0-1 { left: 2.4%;  top: 26.8%; }
.tile-pos-1-1 { left: 26.8%; top: 26.8%; }
.tile-pos-2-1 { left: 51.2%; top: 26.8%; }
.tile-pos-3-1 { left: 75.6%; top: 26.8%; }
.tile-pos-0-2 { left: 2.4%;  top: 51.2%; }
.tile-pos-1-2 { left: 26.8%; top: 51.2%; }
.tile-pos-2-2 { left: 51.2%; top: 51.2%; }
.tile-pos-3-2 { left: 75.6%; top: 51.2%; }
.tile-pos-0-3 { left: 2.4%;  top: 75.6%; }
.tile-pos-1-3 { left: 26.8%; top: 75.6%; }
.tile-pos-2-3 { left: 51.2%; top: 75.6%; }
.tile-pos-3-3 { left: 75.6%; top: 75.6%; }

/* Tile colors - Wingo palette */
.tile-2    { background: #D0ECF5; color: #165F84; }
.tile-4    { background: #A8D8EA; color: #165F84; }
.tile-8    { background: #F4A261; color: #FFF; }
.tile-16   { background: #E76F51; color: #FFF; }
.tile-32   { background: #E43312; color: #FFF; }
.tile-64   { background: #C62B0F; color: #FFF; }
.tile-128  { background: #FFD166; color: #5A3E00; font-size: 1.7rem; }
.tile-256  { background: #F4B942; color: #5A3E00; font-size: 1.7rem; }
.tile-512  { background: #EF8A17; color: #FFF; font-size: 1.7rem; }
.tile-1024 { background: #336F91; color: #FFF; font-size: 1.4rem; }
.tile-2048 { background: #165F84; color: #FFF; font-size: 1.4rem; box-shadow: 0 0 20px rgba(22,95,132,0.6); }
.tile-super { background: #0E4562; color: #FFD166; font-size: 1.2rem; box-shadow: 0 0 20px rgba(255,209,102,0.4); }

/* Overlays */
#game-over-overlay,
#win-overlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 20px;
    border-radius: var(--board-radius);
    z-index: 100;
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.4s ease;
    pointer-events: none;
}

#game-over-overlay.visible,
#win-overlay.visible {
    opacity: 1;
    visibility: visible;
    pointer-events: auto;
}

#game-over-overlay {
    background: rgba(14, 69, 98, 0.85);
}

#win-overlay {
    background: rgba(22, 95, 132, 0.9);
}

#game-over-overlay span,
#win-overlay span {
    font-size: 1.8rem;
    font-weight: 700;
    text-shadow: 0 2px 8px rgba(0,0,0,0.3);
}

/* Score pop animation */
.score-pop {
    position: absolute;
    font-size: 14px;
    font-weight: 700;
    color: #FFD166;
    pointer-events: none;
    animation: score-float 0.6s ease forwards;
    z-index: 50;
}

@keyframes score-float {
    0%   { opacity: 1; transform: translateY(0); }
    100% { opacity: 0; transform: translateY(-30px); }
}

@media (max-width: 420px) {
    #game-wrap {
        padding: 8px 10px;
    }
    #game-header {
        margin-bottom: 10px;
    }
    .header-logo {
        height: 30px;
    }
    .score-value {
        font-size: 18px;
    }
    .tile {
        font-size: 1.5rem;
    }
    .tile-128, .tile-256, .tile-512 {
        font-size: 1.3rem;
    }
    .tile-1024, .tile-2048 {
        font-size: 1.1rem;
    }
    .tile-super {
        font-size: 1rem;
    }
}

@media (min-width: 421px) {
    #game-wrap {
        justify-content: center;
    }
}
