:root {
    --bg-a: #dcecf5;
    --bg-b: #cfe6f0;
    --panel: #e7f0f5;
    --panel-dark: #c7d6de;
    --ink: #2c5470;
    --ink-soft: #5c7d92;
    --orange: #f6a83c;
    --orange-dark: #e08a1f;
    --radius-lg: 26px;
    --radius-md: 16px;
}

* {
    box-sizing: border-box;
    -webkit-tap-highlight-color: transparent;
    user-select: none;
}

html,
body {
    margin: 0;
    padding: 0;
    height: 100%;
    font-family: 'Baloo 2', 'Trebuchet MS', 'Segoe UI', sans-serif;
    background: var(--bg-a);
    overflow: hidden;
    touch-action: none;
}

#app {
    position: relative;
    width: 100%;
    height: 100%;
    max-width: 560px;
    margin: 0 auto;
    background: radial-gradient(120% 100% at 50% 0%, var(--bg-b), var(--bg-a) 70%);
    overflow: hidden;
}

.diamonds {
    position: absolute;
    inset: 0;
    background-image:
        linear-gradient(45deg, rgba(255, 255, 255, 0.55) 25%, transparent 25%, transparent 75%, rgba(255, 255, 255, 0.55) 75%),
        linear-gradient(45deg, rgba(255, 255, 255, 0.55) 25%, transparent 25%, transparent 75%, rgba(255, 255, 255, 0.55) 75%);
    background-size: 70px 70px;
    background-position: 0 0, 35px 35px;
    opacity: .5;
    pointer-events: none;
}

/* ---------- PRELOADER ---------- */
#preloader {
    position: absolute;
    inset: 0;
    z-index: 100;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    background: linear-gradient(160deg, #bfe0ee, #8fc7dd);
    transition: opacity .5s ease, visibility .5s ease;
}

#preloader.hide {
    opacity: 0;
    visibility: hidden;
    pointer-events: none;
}

.logo-badge {
    width: 110px;
    height: 110px;
    border-radius: 26px;
    background: conic-gradient(from 220deg, #f6a83c, #e8577c, #8a6fd6, #4fb3d9, #4caf6a, #e3ac3d, #f6a83c);
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 10px 0 rgba(0, 0, 0, 0.12), 0 14px 24px rgba(0, 0, 0, 0.18);
    border: 4px solid rgba(255, 255, 255, 0.85);
    animation: floaty 2.2s ease-in-out infinite;
}

.logo-badge span {
    font-size: 44px;
    font-weight: 800;
    color: #fff;
    text-shadow: 0 3px 0 rgba(0, 0, 0, 0.2);
}

@keyframes floaty {

    0%,
    100% {
        transform: translateY(0) rotate(-2deg);
    }

    50% {
        transform: translateY(-10px) rotate(2deg);
    }
}

.preload-title {
    margin-top: 22px;
    font-size: 26px;
    font-weight: 800;
    color: #215170;
    letter-spacing: .5px;
    text-shadow: 0 2px 0 rgba(255, 255, 255, 0.5);
}

.preload-sub {
    margin-top: 4px;
    font-size: 14px;
    color: #3a7391;
    font-weight: 600;
}

.preload-bar-track {
    margin-top: 26px;
    width: 220px;
    height: 16px;
    border-radius: 10px;
    background: rgba(255, 255, 255, 0.55);
    box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.15);
    overflow: hidden;
}

.preload-bar-fill {
    height: 100%;
    width: 0%;
    border-radius: 10px;
    background: linear-gradient(180deg, #ffd27a, var(--orange-dark));
    transition: width .18s linear;
}

.preload-pct {
    margin-top: 10px;
    font-size: 13px;
    font-weight: 700;
    color: #215170;
}

/* ---------- HOW TO PLAY ---------- */
#howto {
    position: absolute;
    inset: 0;
    z-index: 90;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(30, 60, 80, 0.45);
    opacity: 0;
    visibility: hidden;
    pointer-events: none;
    transition: opacity .3s ease, visibility .3s ease;
}

#howto.show {
    opacity: 1;
    visibility: visible;
    pointer-events: auto;
}

.howto-card {
    width: 86%;
    max-width: 380px;
    background: var(--panel);
    border-radius: var(--radius-lg);
    padding: 26px 22px 22px;
    text-align: center;
    box-shadow: 0 18px 40px rgba(0, 0, 0, 0.25);
    transform: translateY(10px) scale(.96);
    transition: transform .3s ease;
}

#howto.show .howto-card {
    transform: translateY(0) scale(1);
}

.howto-card h2 {
    margin: 0 0 14px;
    color: var(--ink);
    font-size: 22px;
}

.howto-stage {
    position: relative;
    height: 190px;
    border-radius: var(--radius-md);
    background: linear-gradient(180deg, #f4f9fb, #e3eef3);
    box-shadow: inset 0 0 0 2px #d5e4ea;
    margin-bottom: 18px;
    overflow: hidden;
}

.howto-grid-row {
    position: absolute;
    left: 10px;
    right: 10px;
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 6px;
}

.howto-mini-cell {
    height: 60px;
    width: 60px;
    border-radius: 8px;
    background: #fbfdfe;
    box-shadow: inset 0 0 0 2px #e2edf1;
    margin: 5px;
}

.howto-tile {
    position: absolute;
    width: 45px;
    height: 45px;
    border-radius: 11px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 800;
    font-size: 20px;
    color: #fff;
    box-shadow: 0 4px 0 rgba(0, 0, 0, 0.18);
    margin-top: 10px;
}

.howto-arrow-row {
    position: absolute;
    left: 0;
    right: 0;
    bottom: 8px;
    text-align: center;
    font-size: 11px;
    font-weight: 700;
    color: var(--ink-soft);
    animation: pulse 1.4s ease-in-out infinite;
}

@keyframes pulse {

    0%,
    100% {
        opacity: .5;
    }

    50% {
        opacity: 1;
    }
}

.howto-hand {
    position: absolute;
    font-size: 36px;
    filter: drop-shadow(0 4px 4px rgba(0, 0, 0, 0.25));
    animation: handdrag 1.8s ease-in-out infinite;
}

@keyframes handdrag {
    0% {
        transform: translate(0, 0);
    }

    35% {
        transform: translate(56px, 0);
    }

    55% {
        transform: translate(56px, 0);
    }

    100% {
        transform: translate(0, 0);
    }
}

.howto-card p {
    color: var(--ink-soft);
    font-size: 14px;
    line-height: 1.5;
    margin: 0 0 20px;
}

.btn {
    border: none;
    cursor: pointer;
    font-family: inherit;
    font-weight: 800;
    border-radius: 14px;
    padding: 14px 20px;
    font-size: 17px;
    color: #fff;
    background: linear-gradient(180deg, #ffcf7a, var(--orange-dark));
    box-shadow: 0 5px 0 #b96f14, 0 8px 14px rgba(0, 0, 0, 0.2);
    transition: transform .08s ease, box-shadow .08s ease;
    width: 100%;
}

.btn:active {
    transform: translateY(4px);
    box-shadow: 0 1px 0 #b96f14, 0 3px 8px rgba(0, 0, 0, 0.2);
}

/* ---------- HEADER ---------- */
#header {
    position: relative;
    z-index: 5;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 10px;
    padding: 16px 14px 6px;
}

.stat-pill {
    flex: 1;
    max-width: 150px;
    background: var(--panel);
    border-radius: 20px;
    padding: 8px 6px 10px;
    text-align: center;
    box-shadow: 0 4px 0 var(--panel-dark), inset 0 0 0 1px rgba(255, 255, 255, 0.6);
}

.stat-num {
    font-size: 26px;
    font-weight: 800;
    color: var(--ink);
    line-height: 1;
    transition: transform .15s ease;
}

.stat-num.bump {
    transform: scale(1.25);
}

.stat-label {
    font-size: 11px;
    font-weight: 700;
    color: var(--ink-soft);
    letter-spacing: 1px;
    margin-top: 2px;
}

.mid-badge {
    width: 64px;
    height: 64px;
    border-radius: 16px;
    flex-shrink: 0;
    background: conic-gradient(from 220deg, #f6a83c, #e8577c, #8a6fd6, #4fb3d9, #4caf6a, #e3ac3d, #f6a83c);
    display: flex;
    align-items: center;
    justify-content: center;
    border: 4px solid #fff;
    box-shadow: 0 5px 0 var(--panel-dark), 0 6px 10px rgba(0, 0, 0, 0.15);
    cursor: pointer;
}

.mid-badge span {
    font-size: 24px;
    font-weight: 800;
    color: #fff;
    text-shadow: 0 2px 0 rgba(0, 0, 0, .2);
}

/* ---------- TIMER (progress until next row) ---------- */
#timer-wrap {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 20px 4px;
    position: relative;
    z-index: 5;
}

.clock-icon {
    width: 34px;
    height: 34px;
    border-radius: 50%;
    flex-shrink: 0;
    background: #fff8ec;
    border: 3px solid var(--orange-dark);
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 3px 0 #b96f14;
}

.clock-icon svg {
    width: 16px;
    height: 16px;
}

.timer-track {
    flex: 1;
    height: 14px;
    border-radius: 10px;
    background: #dfeaf0;
    box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.12);
    overflow: hidden;
    position: relative;
}

.timer-fill {
    height: 100%;
    width: 0%;
    border-radius: 10px;
    background: linear-gradient(180deg, #ffcf7a, var(--orange));
    transition: width .22s linear, background .3s ease;
}

.timer-fill.hot {
    background: linear-gradient(180deg, #ff8f7a, #e0442f);
}

/* ---------- BOARD (scaled to 70% size) ---------- */
#board-wrap {
    position: relative;
    z-index: 5;
    margin: 10px auto 14px;
    background: var(--panel);
    border-radius: var(--radius-lg);
    padding: 10px;
    width: 70%;
    box-shadow: 0 8px 0 var(--panel-dark), inset 0 0 0 1px rgba(255, 255, 255, 0.6);
    container-type: inline-size;
}

#grid {
    display: grid;
    grid-template-columns: repeat(6, 1fr);
    gap: 6px;
}

.cell {
    position: relative;
    aspect-ratio: 1/1;
    border-radius: 9px;
    background: #fbfdfe;
    box-shadow: inset 0 0 0 2px #e2edf1, inset 0 -2px 0 #dbe7ec;
    transition: box-shadow .15s ease;
}

.cell.hover-ok {
    box-shadow: inset 0 0 0 3px #7bd88a;
}

.cell.hover-bad {
    box-shadow: inset 0 0 0 3px #f08a7a;
}

.cell.danger {
    box-shadow: inset 0 0 0 2px #f4b8ae, inset 0 -3px 0 #edA294;
}

/* ---------- TILES ---------- */
.tile {
    position: absolute;
    inset: 0;
    border-radius: 9px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 800;
    color: #fff;
    font-size: clamp(10px, 4.4cqw, 18px);
    box-shadow: 0 3px 0 rgba(0, 0, 0, 0.18), inset 0 2px 0 rgba(255, 255, 255, 0.35);
    cursor: grab;
    animation: pop-in .18s cubic-bezier(.2, 1.4, .5, 1);
    z-index: 2;
}

.tile.dragging {
    position: fixed;
    z-index: 200;
    pointer-events: none;
    transition: none;
    cursor: grabbing;
    box-shadow: 0 10px 18px rgba(0, 0, 0, 0.3);
}

.tile.merging {
    animation: merge-pop .28s ease;
}

@keyframes pop-in {
    0% {
        transform: scale(.3);
        opacity: 0;
    }

    70% {
        transform: scale(1.12);
        opacity: 1;
    }

    100% {
        transform: scale(1);
    }
}

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

    40% {
        transform: scale(1.35);
    }

    100% {
        transform: scale(1);
    }
}

@keyframes rowShiftUp {
    0% {
        transform: translateY(calc(var(--cellH, 44px) + 8px));
    }

    100% {
        transform: translateY(0);
    }
}

.tile.shifting {
    animation: rowShiftUp .32s cubic-bezier(.3, .9, .4, 1);
}

@keyframes newRowIn {
    0% {
        transform: translateY(28px);
        opacity: 0;
    }

    100% {
        transform: translateY(0);
        opacity: 1;
    }
}

.tile.spawning {
    animation: newRowIn .32s cubic-bezier(.2, 1.3, .4, 1);
}

.floater {
    position: absolute;
    left: 50%;
    top: 0;
    transform: translate(-50%, 0);
    font-weight: 800;
    color: var(--orange-dark);
    font-size: 16px;
    pointer-events: none;
    animation: floatup .7s ease forwards;
    z-index: 50;
}

@keyframes floatup {
    0% {
        opacity: 1;
        transform: translate(-50%, 0);
    }

    100% {
        opacity: 0;
        transform: translate(-50%, -38px);
    }
}

/* ---------- TOP BUTTONS ---------- */
#topbtns {
    position: absolute;
    top: 14px;
    right: 14px;
    z-index: 10;
    display: flex;
    gap: 8px;
}

.icon-btn {
    width: 34px;
    height: 34px;
    border-radius: 50%;
    background: var(--panel);
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 3px 0 var(--panel-dark);
    font-size: 16px;
    color: var(--ink);
}

.icon-btn:active {
    transform: translateY(2px);
    box-shadow: none;
}

/* ---------- GAME OVER ---------- */
#gameover {
    position: absolute;
    inset: 0;
    z-index: 95;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(20, 40, 55, 0.55);
    opacity: 0;
    visibility: hidden;
    pointer-events: none;
    transition: opacity .35s ease, visibility .35s ease;
}

#gameover.show {
    opacity: 1;
    visibility: visible;
    pointer-events: auto;
}

.go-card {
    width: 84%;
    max-width: 340px;
    background: var(--panel);
    border-radius: var(--radius-lg);
    padding: 26px 22px;
    text-align: center;
    box-shadow: 0 18px 40px rgba(0, 0, 0, 0.3);
    transform: scale(.9);
    transition: transform .3s ease;
}

#gameover.show .go-card {
    transform: scale(1);
}

.go-card h2 {
    margin: 0 0 6px;
    color: var(--ink);
    font-size: 24px;
}

.go-card .go-score {
    font-size: 44px;
    font-weight: 800;
    color: var(--orange-dark);
    margin: 10px 0 2px;
}

.go-card .go-best {
    font-size: 13px;
    color: var(--ink-soft);
    font-weight: 700;
    margin-bottom: 20px;
}

@media (max-height:680px) {
    .logo-badge {
        width: 84px;
        height: 84px;
    }

    .mid-badge {
        width: 52px;
        height: 52px;
    }

    #board-wrap {
        padding: 8px;
    }
}