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

body {
    font-family: 'Arial', sans-serif;
    background: #faf8ef;
    display: flex;
    flex-direction: column;
    align-items: center;
    min-height: 100vh;
    padding: 0;
    justify-content: space-between;
}

.ad-banner {
    width: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    background: #eee4da;
    border-bottom: 2px solid #bbada0;
}

.ad-banner-top {
    margin-bottom: 0;
}

.ad-banner-bottom {
    margin-top: auto;
    border-top: 2px solid #bbada0;
    border-bottom: none;
}

.ad-placeholder {
    width: 728px;
    height: 90px;
    background: #cdc1b4;
    display: flex;
    justify-content: center;
    align-items: center;
    color: #776e65;
    font-size: 14px;
    font-weight: bold;
    border-radius: 3px;
    margin: 15px 0;
}

.container {
    max-width: 500px;
    width: 100%;
    padding: 20px 20px;
    flex: 1;
    display: flex;
    flex-direction: column;
    justify-content: center;
}

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

h1 {
    font-size: 60px;
    font-weight: bold;
    color: #776e65;
}

.score-container {
    display: flex;
    gap: 10px;
}

.score-box {
    background: #bbada0;
    padding: 10px 20px;
    border-radius: 3px;
    text-align: center;
    min-width: 80px;
}

.score-label {
    color: #eee4da;
    font-size: 13px;
    font-weight: bold;
    text-transform: uppercase;
}

.score {
    color: white;
    font-size: 25px;
    font-weight: bold;
}

.game-intro {
    margin-bottom: 20px;
}

.game-intro p {
    color: #776e65;
    margin-bottom: 10px;
    line-height: 1.5;
}

.game-controls {
    display: flex;
    gap: 10px;
    align-items: center;
    flex-wrap: wrap;
}

.size-selector {
    display: flex;
    align-items: center;
    gap: 8px;
}

.size-selector label {
    color: #776e65;
    font-size: 14px;
    font-weight: bold;
}

.size-selector select {
    background: #bbada0;
    color: white;
    border: none;
    padding: 8px 12px;
    font-size: 14px;
    font-weight: bold;
    border-radius: 3px;
    cursor: pointer;
    transition: background 0.2s;
}

.size-selector select:hover {
    background: #a89989;
}

.new-game-button,
.rules-button {
    background: #8f7a66;
    color: white;
    border: none;
    padding: 10px 16px;
    font-size: 14px;
    font-weight: bold;
    border-radius: 3px;
    cursor: pointer;
    transition: background 0.2s;
    white-space: nowrap;
}

.new-game-button:hover,
.rules-button:hover {
    background: #9f8a76;
}

.game-container {
    position: relative;
    background: #bbada0;
    border-radius: 6px;
    padding: 15px;
    width: 100%;
    aspect-ratio: 1;
    touch-action: none;
    -webkit-user-select: none;
    user-select: none;
}

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

.grid-cell {
    background: rgba(238, 228, 218, 0.35);
    border-radius: 3px;
}

.tile-container {
    position: absolute;
    top: 15px;
    left: 15px;
    right: 15px;
    bottom: 15px;
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    grid-template-rows: repeat(4, 1fr);
    gap: 15px;
    touch-action: none;
}

.tile {
    position: absolute;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 35px;
    font-weight: bold;
    border-radius: 3px;
    transition: transform 0.2s ease-in-out, top 0.2s ease-in-out, left 0.2s ease-in-out;
    -webkit-transform: translate3d(0, 0, 0);
    transform: translate3d(0, 0, 0);
    will-change: top, left;
    backface-visibility: hidden;
    -webkit-backface-visibility: hidden;
    -webkit-font-smoothing: antialiased;
}

.tile.tile-new {
    animation: appear 0.2s ease-in-out;
}

.tile.tile-merged {
    animation: merge 0.2s ease-in-out;
}

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

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

.tile-2 { background: #eee4da; color: #776e65; }
.tile-4 { background: #ede0c8; color: #776e65; }
.tile-8 { background: #f2b179; color: #f9f6f2; }
.tile-16 { background: #f59563; color: #f9f6f2; }
.tile-32 { background: #f67c5f; color: #f9f6f2; }
.tile-64 { background: #f65e3b; color: #f9f6f2; }
.tile-128 { background: #edcf72; color: #f9f6f2; font-size: 30px; }
.tile-256 { background: #edcc61; color: #f9f6f2; font-size: 30px; }
.tile-512 { background: #edc850; color: #f9f6f2; font-size: 30px; }
.tile-1024 { background: #edc53f; color: #f9f6f2; font-size: 25px; }
.tile-2048 { background: #edc22e; color: #f9f6f2; font-size: 25px; }
.tile-super { background: #3c3a32; color: #f9f6f2; font-size: 20px; }

/* 2의 제곱수가 아닌 일반 숫자 타일 */
.tile-number { background: #6ab3d9; color: #f9f6f2; font-size: 30px; font-weight: bold; }

/* 연산 기호 타일 */
.tile-operator { background: #8b7ec8; color: #f9f6f2; font-size: 40px; font-weight: bold; }
.tile-operator.tile-plus { background: #8b7ec8; }
.tile-operator.tile-minus { background: #a985c7; }
.tile-operator.tile-multiply { background: #c78dc0; }
.tile-operator.tile-divide { background: #d795b8; }

.game-message {
    display: none;
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(238, 228, 218, 0.9);
    border-radius: 6px;
    z-index: 100;
    text-align: center;
    justify-content: center;
    align-items: center;
    flex-direction: column;
}

.game-message.show {
    display: flex;
}

.game-message p {
    font-size: 55px;
    font-weight: bold;
    color: #776e65;
    margin-bottom: 30px;
}

.message-buttons {
    display: flex;
    gap: 15px;
    justify-content: center;
}

.continue-button,
.retry-button {
    background: #8f7a66;
    color: white;
    border: none;
    padding: 15px 30px;
    font-size: 18px;
    font-weight: bold;
    border-radius: 3px;
    cursor: pointer;
    transition: background 0.2s;
}

.continue-button:hover,
.retry-button:hover {
    background: #9f8a76;
}

.modal {
    display: none;
    position: fixed;
    z-index: 200;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.5);
    overflow: auto;
}

.modal.show {
    display: block;
}

.modal-content {
    background-color: #faf8ef;
    margin: 5% auto;
    padding: 30px;
    border-radius: 6px;
    max-width: 700px;
    width: 90%;
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
    position: relative;
}

.close-button {
    position: absolute;
    top: 15px;
    right: 20px;
    font-size: 35px;
    font-weight: bold;
    color: #776e65;
    cursor: pointer;
    transition: color 0.2s;
}

.close-button:hover {
    color: #8f7a66;
}

.modal-content h2 {
    color: #776e65;
    font-size: 32px;
    margin-bottom: 20px;
    text-align: center;
}

.modal-content h3 {
    color: #8f7a66;
    font-size: 20px;
    margin-top: 20px;
    margin-bottom: 10px;
}

.rules-content {
    color: #776e65;
    line-height: 1.6;
}

.rules-content ul {
    margin-left: 20px;
    margin-bottom: 15px;
}

.rules-content li {
    margin-bottom: 8px;
}

.rules-content strong {
    color: #8f7a66;
    font-weight: bold;
}

@media (max-width: 768px) {
    .ad-placeholder {
        width: 320px;
        height: 50px;
        font-size: 12px;
    }

    .ad-placeholder::after {
        content: ' (320x50)';
    }

    .ad-placeholder::before {
        content: '광고 영역 ';
    }
}

@media (max-width: 520px) {
    h1 {
        font-size: 40px;
    }

    .score {
        font-size: 20px;
    }

    .tile {
        font-size: 25px;
    }

    .tile-128, .tile-256, .tile-512 {
        font-size: 20px;
    }

    .tile-1024, .tile-2048 {
        font-size: 18px;
    }

    .modal-content {
        margin: 10% auto;
        padding: 20px;
    }

    .modal-content h2 {
        font-size: 24px;
    }

    .modal-content h3 {
        font-size: 18px;
    }

    .tab-button {
        font-size: 12px;
        padding: 6px 12px;
    }

    .new-game-button,
    .rules-button {
        padding: 8px 12px;
        font-size: 12px;
    }

    .game-controls {
        gap: 8px;
    }

    .size-selector label {
        font-size: 12px;
    }

    .size-selector select {
        padding: 6px 10px;
        font-size: 12px;
    }
}

.leaderboard-tabs {
    display: flex;
    gap: 10px;
    margin-bottom: 20px;
    justify-content: center;
    flex-wrap: wrap;
}

.tab-button {
    background: #bbada0;
    color: white;
    border: none;
    padding: 8px 16px;
    font-size: 14px;
    font-weight: bold;
    border-radius: 3px;
    cursor: pointer;
    transition: background 0.2s;
}

.tab-button:hover {
    background: #a89989;
}

.tab-button.active {
    background: #8f7a66;
}

.sort-toggle {
    display: flex;
    gap: 8px;
    margin-bottom: 15px;
    justify-content: center;
    align-items: center;
    flex-wrap: wrap;
}

.sort-toggle label {
    color: #776e65;
    font-size: 12px;
    font-weight: bold;
}

#sort-field {
    background: #bbada0;
    color: white;
    border: none;
    padding: 6px 10px;
    font-size: 12px;
    font-weight: bold;
    border-radius: 3px;
    cursor: pointer;
}

.sort-button {
    background: #cdc1b4;
    color: #776e65;
    border: none;
    padding: 6px 12px;
    font-size: 12px;
    font-weight: bold;
    border-radius: 3px;
    cursor: pointer;
    transition: background 0.2s, color 0.2s;
}

.sort-button:hover {
    background: #bbada0;
    color: white;
}

.sort-button.active {
    background: #8f7a66;
    color: white;
}

.leaderboard-content {
    min-height: 300px;
    max-height: 400px;
    overflow-y: auto;
}

.leaderboard-table {
    width: 100%;
    border-collapse: collapse;
}

.leaderboard-table th {
    background: #bbada0;
    color: white;
    padding: 10px;
    text-align: left;
    font-weight: bold;
    position: sticky;
    top: 0;
}

.leaderboard-table td {
    padding: 10px;
    border-bottom: 1px solid #eee4da;
    color: #776e65;
}

.leaderboard-table tr:hover {
    background: #faf8ef;
}

.rank-1 { color: #f59563; font-weight: bold; }
.rank-2 { color: #f67c5f; font-weight: bold; }
.rank-3 { color: #f65e3b; font-weight: bold; }

.loading {
    text-align: center;
    padding: 40px;
    color: #776e65;
    font-size: 16px;
}

.no-scores {
    text-align: center;
    padding: 40px;
    color: #776e65;
    font-size: 16px;
}

/* 닉네임 모달 스타일 */
.nickname-modal-content {
    max-width: 400px;
}

.nickname-content {
    padding: 10px 0;
}

.nickname-content p {
    color: #776e65;
    margin: 10px 0;
    line-height: 1.5;
}

.nickname-hint {
    font-size: 14px;
    color: #999;
}

#nickname-input {
    width: 100%;
    padding: 12px;
    font-size: 16px;
    border: 2px solid #bbada0;
    border-radius: 3px;
    box-sizing: border-box;
    margin: 15px 0;
    font-family: "Clear Sans", "Helvetica Neue", Arial, sans-serif;
}

#nickname-input:focus {
    outline: none;
    border-color: #8f7a66;
}

.current-nickname {
    margin: 15px 0;
    padding: 12px;
    background: #faf8ef;
    border-radius: 3px;
    color: #776e65;
    font-size: 14px;
}

.current-nickname span:first-child {
    font-weight: bold;
}

.save-nickname-button {
    background: #8f7a66;
    color: white;
    border: none;
    padding: 12px 24px;
    font-size: 16px;
    font-weight: bold;
    border-radius: 3px;
    cursor: pointer;
    width: 100%;
    margin-top: 10px;
    transition: background 0.2s;
}

.save-nickname-button:hover {
    background: #9f8a76;
}

.save-nickname-button:disabled {
    background: #bbada0;
    cursor: not-allowed;
    opacity: 0.7;
}
