@import url('https://fonts.googleapis.com/css2?family=Montserrat:wght@400;500;600;700&display=swap');

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    font-family: 'Montserrat', sans-serif;
    -webkit-tap-highlight-color: transparent;
    user-select: none;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    -webkit-touch-callout: none;
    -webkit-text-size-adjust: none;
}

body {
    background: var(--tg-theme-bg-color);
    color: var(--tg-theme-text-color);
    min-height: 100vh;
    display: flex;
    flex-direction: column;
    padding: 16px;
    width: 100%;
    overflow-y: auto;
    overflow-x: hidden;
}

.game-container {
    width: 100%;
    max-width: 600px;
    margin: 0 auto;
    background: var(--tg-theme-secondary-bg-color);
    border-radius: 24px;
    padding: 20px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
    animation: fadeIn 0.3s ease-out;
    display: flex;
    flex-direction: column;
    height: auto;
    min-height: 80vh;
}

.header {
    text-align: center;
    margin-bottom: 16px;
}

.title {
    font-size: 24px;
    font-weight: bold;
    color: var(--tg-theme-text-color);
    margin-bottom: 12px;
}

.score-container {
    display: flex;
    justify-content: center;
    gap: 32px;
    margin-bottom: 12px;
}

.score-box {
    display: flex;
    flex-direction: column;
    align-items: center;
    background: var(--tg-theme-bg-color);
    padding: 8px 16px;
    border-radius: 12px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
    position: relative;
    overflow: hidden;
}

.score-box::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: linear-gradient(45deg, rgba(255,255,255,0.1), rgba(255,255,255,0));
    opacity: 0;
    transition: opacity 0.2s ease;
}

.score-box:active {
    transform: scale(0.95);
    box-shadow: 0 1px 4px rgba(0, 0, 0, 0.1);
}

.score-box:active::before {
    opacity: 1;
}

.player-name {
    font-size: 14px;
    font-weight: 500;
    color: var(--tg-theme-hint-color);
    margin-bottom: 4px;
}

.score {
    font-size: 16px;
    color: var(--tg-theme-hint-color);
    margin-bottom: 16px;
    text-align: center;
}

.game-area {
    background: var(--tg-theme-bg-color);
    border-radius: 16px;
    margin-bottom: 16px;
    box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.1);
    position: relative;
    overflow: hidden;
    display: flex;
    justify-content: center;
    align-items: center;
    width: 100%;
    padding-bottom: 100%;
    transform-style: preserve-3d;
    transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    touch-action: none;
    -webkit-user-select: none;
    user-select: none;
    z-index: 1000;
}

.game-area:active {
    transform: scale(0.98);
}

.grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    grid-template-rows: repeat(4, 1fr);
    gap: 8px;
    background: #bbada0;
    padding: 8px;
    border-radius: 8px;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
    position: absolute;
    top: 8px;
    left: 8px;
    right: 8px;
    bottom: 8px;
    transform: perspective(1500px) rotateX(3deg);
    transform-origin: center center;
    transition: transform 0.3s ease;
}

.grid.tilt-left  { transform: perspective(1500px) rotateY(5deg); }
.grid.tilt-right { transform: perspective(1500px) rotateY(-5deg); }
.grid.tilt-up    { transform: perspective(1500px) rotateX(-5deg); }
.grid.tilt-down  { transform: perspective(1500px) rotateX(5deg); }

.cell {
    background: rgba(238, 228, 218, 0.35);
    border-radius: 8px;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 24px;
    font-weight: bold;
    position: relative;
    aspect-ratio: 1;
    transition: transform 0.2s ease-out, opacity 0.2s ease-out;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
    transform-origin: center;
    will-change: transform;
    z-index: 1;
    cursor: pointer;
}

.cell[data-value] {
    z-index: 2;
    background: var(--tile-color);
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
    transform: scale(1);
    transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
    position: relative;
    overflow: hidden;
}

.cell[data-value]::after {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: linear-gradient(45deg, rgba(255,255,255,0.2), rgba(255,255,255,0));
    opacity: 0;
    transition: opacity 0.2s ease;
}

.cell[data-value]:active::after {
    opacity: 1;
}

.cell[data-value="0"] {
    opacity: 0;
}

.cell:not([data-value="0"]) {
    opacity: 1;
}

.game-controls {
    display: flex;
    gap: 8px;
    width: 100%;
}

.game-controls button {
    flex: 1;
    background: var(--tg-theme-button-color);
    color: var(--tg-theme-button-text-color);
    border: none;
    padding: 12px 24px;
    border-radius: 12px;
    font-size: 16px;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
    -webkit-tap-highlight-color: transparent;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    position: relative;
    overflow: hidden;
}

.game-controls button::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: linear-gradient(45deg, rgba(255,255,255,0.1), rgba(255,255,255,0));
    opacity: 0;
    transition: opacity 0.2s ease;
}

.game-controls button:active {
    transform: scale(0.95);
    box-shadow: 0 1px 4px rgba(0, 0, 0, 0.1);
}

.game-controls button:active::before {
    opacity: 1;
}

.game-controls button:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

@keyframes fadeIn {
    from {
        opacity: 0;
        transform: translateY(10px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

@keyframes tileAppear {
    0% {
        opacity: 0;
        transform: scale(0.5);
    }
    100% {
        opacity: 1;
        transform: scale(1);
    }
}

@keyframes tileMerge {
    0% {
        transform: scale(1);
    }
    50% {
        transform: scale(1.1);
        box-shadow: 0 8px 16px rgba(0, 0, 0, 0.2);
    }
    100% {
        transform: scale(1);
        box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
    }
}

@keyframes tilePop {
    0% {
        transform: scale(1);
    }
    50% {
        transform: scale(1.1);
    }
    100% {
        transform: scale(1);
    }
}

@keyframes tileGlow {
    0% {
        box-shadow: 0 0 5px rgba(237, 194, 46, 0.5);
    }
    50% {
        box-shadow: 0 0 20px rgba(237, 194, 46, 0.8);
    }
    100% {
        box-shadow: 0 0 5px rgba(237, 194, 46, 0.5);
    }
}

@keyframes tileMove {
    0% {
        transform: translate(0, 0);
        z-index: 3;
    }
    100% {
        transform: translate(var(--move-x), var(--move-y));
        z-index: 3;
    }
}

@keyframes scoreChange {
    0% {
        transform: scale(1);
    }
    50% {
        transform: scale(1.2);
        color: #f65e3b;
    }
    100% {
        transform: scale(1);
    }
}

.score-updated {
    animation: scoreChange 0.3s ease-out;
}

.rating-section {
    margin-top: 24px;
    background: var(--tg-theme-bg-color);
    border-radius: 16px;
    padding: 16px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    animation: fadeIn 0.3s ease-out;
}

.rating-title {
    font-size: 18px;
    font-weight: 600;
    color: var(--tg-theme-text-color);
    margin-bottom: 16px;
    text-align: center;
    animation: slideInDown 0.5s ease-out;
}

.rating-table {
    width: 100%;
    border-collapse: collapse;
    background: var(--tg-theme-secondary-bg-color);
    border-radius: 12px;
    overflow: hidden;
    animation: fadeInUp 0.5s ease-out;
}

.rating-table th,
.rating-table td {
    padding: 12px;
    text-align: center;
    border-bottom: 1px solid rgba(0, 0, 0, 0.05);
    animation: fadeIn 0.5s ease-out forwards;
    opacity: 0;
}

.rating-table th {
    background: var(--tg-theme-button-color);
    color: var(--tg-theme-button-text-color);
    font-weight: 600;
    font-size: 14px;
    text-transform: uppercase;
}

.rating-table td {
    font-size: 14px;
    color: var(--tg-theme-text-color);
}

.rating-table tr:last-child td {
    border-bottom: none;
}

.rating-table tbody tr {
    transition: background-color 0.2s ease;
}

.rating-table tbody tr:hover {
    background-color: rgba(0, 0, 0, 0.02);
}

.rating-table tbody tr:nth-child(1) td:first-child {
    position: relative;
    color: #ffd700;
    font-weight: 700;
}

.rating-table tbody tr:nth-child(2) td:first-child {
    color: #c0c0c0;
    font-weight: 600;
}

.rating-table tbody tr:nth-child(3) td:first-child {
    color: #cd7f32;
    font-weight: 600;
}

@keyframes fadeIn {
    from {
        opacity: 0;
    }
    to {
        opacity: 1;
    }
}

@keyframes fadeInUp {
    from {
        opacity: 0;
        transform: translateY(20px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

@keyframes slideInDown {
    from {
        opacity: 0;
        transform: translateY(-20px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

@media (max-width: 480px) {
    .game-container {
        padding: 16px;
        border-radius: 20px;
        min-height: 70vh;
    }

    .title {
        font-size: 20px;
    }

    .score-container {
        gap: 24px;
    }

    .score-box {
        padding: 6px 12px;
    }

    .player-name {
        font-size: 12px;
    }

    .grid {
        gap: 6px;
        padding: 6px;
    }

    .cell {
        font-size: 20px;
    }

    .game-controls button {
        padding: 10px 20px;
        font-size: 14px;
    }
}

@media (max-width: 360px) {
    .game-container {
        padding: 12px;
        border-radius: 16px;
        min-height: 60vh;
    }

    .title {
        font-size: 18px;
    }

    .score-container {
        gap: 16px;
    }

    .score-box {
        padding: 4px 8px;
    }

    .player-name {
        font-size: 11px;
    }

    .score {
        font-size: 24px;
    }

    .grid {
        gap: 4px;
        padding: 4px;
    }

    .cell {
        font-size: 18px;
    }

    .game-controls button {
        padding: 8px 16px;
        font-size: 13px;
    }
}

.confirm-modal {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.5);
    display: none;
    justify-content: center;
    align-items: center;
    z-index: 1000;
    opacity: 0;
    transition: opacity 0.3s ease;
    backdrop-filter: blur(5px);
}

.confirm-modal-content {
    background: var(--tg-theme-bg-color);
    padding: 24px;
    border-radius: 12px;
    text-align: center;
    max-width: 90%;
    width: 300px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.confirm-modal h2 {
    margin-bottom: 12px;
    color: var(--tg-theme-text-color);
    font-size: 20px;
    font-weight: 600;
}

.confirm-modal p {
    margin-bottom: 20px;
    color: var(--tg-theme-hint-color);
    font-size: 16px;
}

.confirm-buttons {
    display: flex;
    gap: 12px;
    justify-content: center;
}

.confirm-buttons button {
    padding: 12px 24px;
    border: none;
    border-radius: 8px;
    font-size: 16px;
    font-weight: 500;
    cursor: pointer;
    transition: transform 0.2s ease;
}

#confirmYes {
    background: var(--tg-theme-button-color);
    color: var(--tg-theme-button-text-color);
}

#confirmNo {
    background: var(--tg-theme-secondary-bg-color);
    color: var(--tg-theme-text-color);
}

.confirm-buttons button:active {
    transform: scale(0.95);
}
