* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    -webkit-tap-highlight-color: transparent;
}

:root {
    --frame-bottom: 0px;
    --frame-left: 0px;
    --frame-right: 0px;
    --board-bg: #bbada0;
    --cell-bg: rgba(238, 228, 218, 0.35);
    --text-dark: #776e65;
    --accent: #e8743b;
}

html, body {
    height: 100%;
}

/* Inside the site iframe a fixed footer bar (~40px, "Etusivu / Tietoa pelistä") covers the bottom.
   The site hides that bar when height <= 400px. */
html.in-frame { --frame-bottom: 44px; }
@media (max-height: 400px) {
    html.in-frame { --frame-bottom: 0px; }
}

body {
    font-family: 'Arial', sans-serif;
    background: linear-gradient(135deg, #f7b267 0%, #f25c54 100%);
    background-attachment: fixed;
    color: #fff;
    overflow: hidden;
    user-select: none;
    -webkit-user-select: none;
}

#app {
    position: relative;
    width: 100%;
    max-width: 500px;
    height: calc(100vh - var(--frame-bottom));
    height: calc(100dvh - var(--frame-bottom));
    margin: 0 auto;
}

.screen {
    display: none;
    position: absolute;
    inset: 0 var(--frame-right) 0 var(--frame-left);
    flex-direction: column;
    align-items: center;
}

.screen.active {
    display: flex;
}

/* ---------- Start screen ---------- */
#start-screen {
    justify-content: center;
    padding: 20px;
    text-align: center;
}

.logo {
    display: flex;
    gap: 8px;
    margin-bottom: 16px;
}

.logo-tile {
    width: 58px;
    height: 58px;
    border-radius: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 32px;
    font-weight: bold;
    box-shadow: 0 4px 10px rgba(0, 0, 0, 0.25);
}

.logo-tile.v2 { background: #eee4da; color: var(--text-dark); }
.logo-tile.v0 { background: #f2b179; color: #fff; }
.logo-tile.v4 { background: #f67c5f; color: #fff; }
.logo-tile.v8 { background: #edc22e; color: #fff; }

#start-screen h1 {
    font-size: 64px;
    letter-spacing: 3px;
    margin-bottom: 12px;
    text-shadow: 3px 3px 6px rgba(0, 0, 0, 0.3);
}

#start-screen .desc {
    font-size: 17px;
    line-height: 1.5;
    max-width: 340px;
    margin-bottom: 24px;
}

.start-best {
    margin-top: 14px;
    font-size: 15px;
    opacity: 0.95;
    min-height: 1.2em;
}

/* ---------- Buttons ---------- */
.btn {
    display: block;
    width: 240px;
    max-width: 100%;
    border: none;
    padding: 14px 20px;
    margin: 7px auto;
    font-size: 19px;
    font-weight: bold;
    border-radius: 50px;
    cursor: pointer;
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
    transition: transform 0.15s ease, box-shadow 0.15s ease;
    font-family: inherit;
}

.btn:hover { transform: translateY(-2px); box-shadow: 0 6px 20px rgba(0, 0, 0, 0.3); }
.btn:active { transform: translateY(0); }

.btn-primary { background: #fff; color: #d9534f; }
.btn-secondary { background: rgba(255, 255, 255, 0.18); color: #fff; border: 2px solid rgba(255, 255, 255, 0.7); }
.btn-small { width: auto; padding: 10px 26px; font-size: 17px; margin: 0 auto; background: #fff; color: #d9534f; }

.icon-btn {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    border: 2px solid rgba(255, 255, 255, 0.7);
    background: rgba(255, 255, 255, 0.18);
    color: #fff;
    font-size: 20px;
    font-weight: bold;
    cursor: pointer;
    flex-shrink: 0;
}

/* ---------- Game screen ---------- */
#game-screen {
    padding: 8px 8px calc(10px + env(safe-area-inset-bottom));
    touch-action: none;
}

.topbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    width: 100%;
    padding: 4px 4px 10px;
}

.scores {
    display: flex;
    gap: 8px;
}

.score-box {
    position: relative;
    min-width: 92px;
    padding: 6px 12px;
    background: rgba(0, 0, 0, 0.18);
    border-radius: 10px;
    text-align: center;
}

.score-box .lbl {
    font-size: 12px;
    text-transform: uppercase;
    letter-spacing: 1px;
    opacity: 0.9;
}

.score-box .val {
    font-size: 22px;
    font-weight: bold;
}

.score-add {
    position: absolute;
    left: 0;
    right: 0;
    top: 22px;
    font-size: 18px;
    font-weight: bold;
    color: #fff;
    opacity: 0;
    pointer-events: none;
}

.score-add.show { animation: scoreUp 0.7s ease-out; }

#board-wrap {
    flex: 1;
    min-height: 0;
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
}

#board {
    position: relative;
    width: var(--board, 320px);
    height: var(--board, 320px);
    background: var(--board-bg);
    border-radius: 12px;
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.25);
}

#cells, #tiles {
    position: absolute;
    inset: 0;
}

.cell, .tile {
    position: absolute;
    left: 0;
    top: 0;
    width: var(--cell, 70px);
    height: var(--cell, 70px);
    border-radius: 8px;
}

.cell { background: var(--cell-bg); }

.tile {
    transition: transform 0.1s ease-in-out;
    z-index: 1;
}

.tile.no-anim { transition: none; }

.tile-inner {
    width: 100%;
    height: 100%;
    border-radius: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: bold;
    font-size: calc(var(--cell, 70px) * 0.45);
    color: var(--text-dark);
    background: #eee4da;
}

.tile.new .tile-inner { animation: appear 0.2s ease 0.1s backwards; }
.tile.merged { z-index: 2; }
.tile.merged .tile-inner { animation: pop 0.2s ease 0.1s backwards; }

.tile.d3 .tile-inner { font-size: calc(var(--cell, 70px) * 0.38); }
.tile.d4 .tile-inner { font-size: calc(var(--cell, 70px) * 0.3); }
.tile.d5 .tile-inner { font-size: calc(var(--cell, 70px) * 0.25); }

.tile.v2 .tile-inner { background: #eee4da; }
.tile.v4 .tile-inner { background: #ede0c8; }
.tile.v8 .tile-inner { background: #f2b179; color: #fff; }
.tile.v16 .tile-inner { background: #f59563; color: #fff; }
.tile.v32 .tile-inner { background: #f67c5f; color: #fff; }
.tile.v64 .tile-inner { background: #f65e3b; color: #fff; }
.tile.v128 .tile-inner { background: #edcf72; color: #fff; box-shadow: 0 0 12px 2px rgba(243, 215, 116, 0.5); }
.tile.v256 .tile-inner { background: #edcc61; color: #fff; box-shadow: 0 0 14px 3px rgba(243, 215, 116, 0.55); }
.tile.v512 .tile-inner { background: #edc850; color: #fff; box-shadow: 0 0 16px 4px rgba(243, 215, 116, 0.6); }
.tile.v1024 .tile-inner { background: #edc53f; color: #fff; box-shadow: 0 0 18px 5px rgba(243, 215, 116, 0.65); }
.tile.v2048 .tile-inner { background: #edc22e; color: #fff; box-shadow: 0 0 22px 6px rgba(243, 215, 116, 0.75); }
.tile.vbig .tile-inner { background: #3c3a32; color: #fff; }

.bottombar {
    padding-top: 12px;
}

/* ---------- Modals ---------- */
.modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.55);
    display: none;
    align-items: center;
    justify-content: center;
    z-index: 400;
    padding: 16px calc(16px + var(--frame-right)) calc(16px + var(--frame-bottom)) calc(16px + var(--frame-left));
}

.modal-overlay.show { display: flex; }

.modal {
    background: #fff;
    color: #374151;
    border-radius: 16px;
    padding: 24px 20px;
    width: 100%;
    max-width: 380px;
    max-height: 100%;
    overflow-y: auto;
    text-align: center;
    box-shadow: 0 15px 35px rgba(0, 0, 0, 0.4);
    animation: modalIn 0.25s ease;
}

.modal h2 {
    font-size: 26px;
    margin-bottom: 10px;
    color: #d9534f;
}

.modal .btn-primary { background: #e8743b; color: #fff; }
.modal .btn-secondary { background: #fff; color: #e8743b; border-color: #e8743b; }

.result-text { font-size: 15px; line-height: 1.45; margin-bottom: 8px; }
.result-score { font-size: 24px; font-weight: bold; color: var(--text-dark); }
.result-tile { font-size: 15px; color: #6b7280; margin-top: 4px; }
.result-best { font-size: 16px; font-weight: bold; color: #16a34a; margin-top: 6px; min-height: 1em; }
.result-text:empty, .result-score:empty, .result-tile:empty, .result-best:empty { display: none; }

.modal-buttons { margin-top: 14px; }
.modal-buttons .btn { width: 100%; max-width: 260px; }

.modal.help { text-align: left; }
.modal.help h2 { text-align: center; }
.modal.help p { font-size: 15px; line-height: 1.45; margin: 8px 0; }

/* ---------- Animations ---------- */
@keyframes appear {
    from { transform: scale(0); opacity: 0; }
    to { transform: scale(1); opacity: 1; }
}

@keyframes pop {
    0% { transform: scale(0.8); }
    50% { transform: scale(1.15); }
    100% { transform: scale(1); }
}

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

@keyframes modalIn {
    from { transform: scale(0.9); opacity: 0; }
    to { transform: scale(1); opacity: 1; }
}

/* ---------- Small / short screens ---------- */
@media (max-width: 360px) {
    .score-box { min-width: 78px; padding: 5px 8px; }
    .score-box .val { font-size: 19px; }
    #start-screen h1 { font-size: 52px; }
}

@media (max-height: 600px) {
    #start-screen h1 { font-size: 44px; margin-bottom: 6px; }
    .logo-tile { width: 44px; height: 44px; font-size: 24px; }
    #start-screen .desc { margin-bottom: 12px; font-size: 15px; }
    .btn { padding: 10px 18px; font-size: 17px; margin: 5px auto; }
}
