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

body {
    background: #F3F4F6;
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 100vh;
    min-height: 100dvh;
    font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
    overflow: auto;
    padding: 16px;
    overscroll-behavior: none;
    -webkit-touch-callout: none;
}

.game-shell {
    position: relative;
    width: min(800px, calc((100vh - 32px) * 0.7547), calc(100vw - 32px));
    width: min(800px, calc((100dvh - 32px) * 0.7547), calc(100vw - 32px));
    -webkit-user-select: none;
    user-select: none;
    -webkit-touch-callout: none;
}

#gameCanvas {
    display: block;
    width: 100%;
    height: auto;
    background: #FFFFFF;
    border: 1px solid #E5E7EB;
    box-shadow: 0 4px 24px rgba(0,0,0,0.08);
    cursor: pointer;
    object-fit: contain;
    touch-action: none;
    -webkit-user-select: none;
    user-select: none;
}

.canvas-action {
    position: absolute;
    display: none;
    align-items: center;
    justify-content: center;
    gap: 10px;
    width: 22.5%;
    height: 4.15%;
    min-height: 34px;
    background: #FFFFFF;
    border: 2px solid #111827;
    border-radius: 8px;
    color: #111827;
    cursor: pointer;
    font: 800 15px/1 system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
    transform: translate(-50%, 0);
    z-index: 2;
    touch-action: manipulation;
    -webkit-user-select: none;
    user-select: none;
}

.game-shell[data-screen="menu"] .canvas-action-account,
.game-shell[data-screen="menu"] .canvas-action-leaders,
.game-shell[data-screen="gameover"] .canvas-action-save,
.game-shell[data-screen="gameover"] .canvas-action-gameover-leaders {
    display: flex;
}

.game-shell[data-achievements="open"] .canvas-action {
    display: none;
}

.game-shell[data-info="open"] .canvas-action {
    display: none;
}

.creator-link {
    position: absolute;
    left: 50%;
    top: 16.4%;
    color: #374151;
    display: none;
    font: 800 13px/1 system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
    letter-spacing: 0;
    text-decoration: none;
    transform: translate(-50%, 0);
    z-index: 3;
    touch-action: manipulation;
}

.creator-link:hover {
    color: #111827;
    text-decoration: underline;
}

.game-shell[data-screen="menu"] .creator-link {
    display: block;
}

.game-shell[data-achievements="open"] .creator-link {
    display: none;
}

.game-shell[data-info="open"] .creator-link {
    display: none;
}

.canvas-action-account {
    left: 50%;
    top: 37.92%;
}

.canvas-action-leaders {
    left: 50%;
    top: 43.2%;
}

.canvas-action-save {
    left: 50%;
    top: 43.96%;
    width: 25%;
}

.canvas-action-gameover-leaders {
    left: 50%;
    top: 49.25%;
}

.button-icon {
    fill: none;
    flex: 0 0 auto;
    height: 20px;
    stroke: currentColor;
    stroke-linecap: round;
    stroke-linejoin: round;
    stroke-width: 2.2;
    width: 22px;
}

.account-overlay {
    position: absolute;
    inset: 0;
    display: grid;
    place-items: center;
    background: rgba(17,24,39,0.55);
    padding: 24px;
    z-index: 4;
}

.account-overlay[hidden] {
    display: none;
}

.account-dialog {
    width: min(640px, 100%);
    background: #FFFFFF;
    border: 2px solid #111827;
    border-radius: 10px;
    color: #111827;
    padding: 20px;
}

.account-header {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 16px;
    margin-bottom: 16px;
}

.account-header h2 {
    font-size: 28px;
    font-weight: 800;
    line-height: 1;
}

#accountStatus,
#accountMessage {
    color: #4B5563;
    font-size: 14px;
    line-height: 1.35;
}

#accountMessage {
    min-height: 20px;
    margin-top: 12px;
}

#accountMessage.success-message {
    color: #16A34A;
    font-weight: 800;
}

.account-tabs,
.account-actions {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px;
}

.account-tabs {
    margin-bottom: 14px;
}

.account-form {
    display: grid;
    gap: 10px;
}

.account-form input {
    width: 100%;
    background: #FFFFFF;
    border: 2px solid #D1D5DB;
    border-radius: 8px;
    color: #111827;
    font: 700 15px/1 system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
    min-height: 42px;
    padding: 10px 12px;
}

.account-form input:focus {
    border-color: #111827;
    outline: none;
}

.account-dialog button {
    border: 2px solid #111827;
    border-radius: 8px;
    background: #FFFFFF;
    color: #111827;
    cursor: pointer;
    font: 800 14px/1 system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
    min-height: 40px;
    padding: 9px 12px;
}

.account-dialog button[aria-selected="true"],
.account-dialog button[type="submit"],
#accountCreate {
    background: #111827;
    color: #FFFFFF;
}

.icon-button {
    width: 34px;
    min-height: 34px;
    padding: 0;
}

.wide-button {
    width: 100%;
    margin-top: 10px;
}

.account-switch {
    color: #4B5563;
    font-size: 13px;
    font-weight: 700;
    line-height: 1.4;
    margin-top: 10px;
    text-align: center;
}

.account-switch button {
    border: 0;
    background: transparent;
    color: #111827;
    min-height: auto;
    padding: 0;
    text-decoration: underline;
}

.leaderboard-list {
    display: grid;
    gap: 8px;
    list-style-position: inside;
    min-height: 220px;
}

.leaderboard-list li {
    display: grid;
    grid-template-columns: 46px 1fr auto;
    gap: 10px;
    align-items: center;
    border: 2px solid #E5E7EB;
    border-radius: 8px;
    font: 800 15px/1 system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
    min-height: 42px;
    padding: 9px 12px;
}

.leaderboard-rank {
    display: inline-grid;
    place-items: center;
    justify-self: start;
    min-width: 34px;
    height: 28px;
    border-radius: 999px;
    color: #374151;
    font: 900 13px/1 system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

.leaderboard-row.rank-1 {
    border-color: #F59E0B;
    background: #FFFBEB;
}

.leaderboard-row.rank-2 {
    border-color: #94A3B8;
    background: #F8FAFC;
}

.leaderboard-row.rank-3 {
    border-color: #B45309;
    background: #FFF7ED;
}

.leaderboard-row.rank-1 .leaderboard-rank,
.leaderboard-row.rank-2 .leaderboard-rank,
.leaderboard-row.rank-3 .leaderboard-rank {
    border: 2px solid currentColor;
    box-shadow: inset 0 -3px 0 rgba(17, 24, 39, 0.12);
}

.leaderboard-row.rank-1 .leaderboard-rank {
    background: #FDE68A;
    color: #92400E;
}

.leaderboard-row.rank-2 .leaderboard-rank {
    background: #E2E8F0;
    color: #475569;
}

.leaderboard-row.rank-3 .leaderboard-rank {
    background: #FDBA74;
    color: #7C2D12;
}

.leaderboard-list .empty-row {
    grid-template-columns: 1fr;
    color: #6B7280;
    font-weight: 700;
    text-align: center;
}

.stats-list {
    display: grid;
    gap: 8px;
}

.stats-list div {
    display: grid;
    grid-template-columns: 1fr auto;
    gap: 12px;
    align-items: center;
    border: 2px solid #E5E7EB;
    border-radius: 8px;
    min-height: 42px;
    padding: 9px 12px;
}

.stats-list dt {
    color: #4B5563;
    font-size: 13px;
    font-weight: 800;
}

.stats-list dd {
    color: #111827;
    font-size: 15px;
    font-weight: 800;
    margin: 0;
}

.empty-stats {
    color: #6B7280;
    font-weight: 700;
    text-align: center;
}

@media (max-width: 520px) {
    body {
        align-items: center;
        min-height: 100svh;
        padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
    }

    .game-shell {
        width: min(calc(100svh * 0.7547), 100vw);
    }

    #gameCanvas {
        border: 0;
        box-shadow: none;
    }

    .creator-link {
        top: 17.2%;
        font-size: 12px;
    }

    .canvas-action {
        width: 30%;
        min-height: 32px;
        font-size: 13px;
        gap: 6px;
    }

    .canvas-action-account {
        top: 37.9%;
    }

    .canvas-action-leaders {
        top: 43.2%;
    }

    .button-icon {
        height: 17px;
        width: 18px;
    }

    .account-overlay {
        padding: 8px;
    }

    .account-dialog {
        max-height: 100%;
        overflow: auto;
        padding: 12px;
    }

    .account-header h2 {
        font-size: 22px;
    }

    .account-tabs,
    .account-actions {
        gap: 8px;
    }

    .leaderboard-list {
        min-height: 180px;
    }

    .leaderboard-list li {
        grid-template-columns: 32px minmax(0, 1fr) auto;
        gap: 8px;
        padding: 9px 10px;
    }
}
