@import url('https://fonts.googleapis.com/css2?family=Press+Start+2P&display=swap');

:root {
    --gold: #ffd700;
    --cyan: #9be7ff;
    --dark-overlay: rgba(0, 0, 0, 0.5);
    --dark-overlay-light: rgba(0, 0, 0, 0.4);
    --font-arcade: 'Press Start 2P', monospace;
}

body {
    background-color: #222;
    color: #fff;
    font-family: var(--font-arcade);
    display: flex;
    flex-direction: column;
    align-items: center;
    /* justify-content: center; removed to allow natural flow */
    min-height: 100vh;
    /* Changed from height to min-height */
    margin: 0;
    overflow-y: auto;
    /* Enable vertical scrolling */
    padding-top: 2rem;
    /* Add some space at the top so game isn't flush against viewport */
}

/* Mobile interaction hardening:
 * prevent browser text-selection/tap-flash overlays while playing.
 */
#game-container,
#gameCanvas,
#ui-layer,
#message-box,
#hud,
#run-timer,
#next-arrow,
#mobile-controls,
#mobile-controls button {
    -webkit-user-select: none;
    user-select: none;
    -webkit-touch-callout: none;
    -webkit-tap-highlight-color: transparent;
}

#game-container,
#gameCanvas,
#ui-layer,
#mobile-controls,
#mobile-controls button {
    touch-action: none;
}

/* Keep profile form fields editable/selectable on mobile keyboards. */
#profile-form input,
#profile-form textarea {
    -webkit-user-select: text;
    user-select: text;
    -webkit-touch-callout: default;
    touch-action: manipulation;
}

/* RTL Support
 * When RTL languages are selected, the entire game interface flips to RTL direction.
 * EXCEPTIONS: Player Profile modal and Ranking/Leaderboard tables remain in LTR and English.
 */
body.dir-rtl {
    direction: rtl;
}

body.dir-rtl #hud {
    left: auto;
    right: 10px;
}

body.dir-rtl #next-arrow {
    right: auto;
    left: 15px;
}

body.dir-rtl #run-timer {
    right: auto;
    left: 10px;
}

/* Language Specific Overrides */
body.lang-ar #message-box,
body.lang-ur #message-box,
body.lang-bn #message-box {
    font-size: 13px;
    line-height: 20px;
}

body.lang-ar #hud,
body.lang-ur #hud {
    font-size: 12px;
    top: 8px;
}

body.lang-bn #hud {
    font-size: 12px;
    top: 7px;
}

body.lang-ar #ui-layer,
body.lang-ur #ui-layer,
body.lang-bn #ui-layer {
    padding-top: 10px;
}

#game-container {
    position: relative;
    border: 4px solid #555;
    box-shadow: 0 0 20px var(--dark-overlay);
    width: 100%;
    max-width: 640px;
    background: #000;
}

canvas {
    display: block;
    width: 100%;
    height: auto;
    image-rendering: pixelated;
}

#ui-layer {
    position: relative;
    width: 90%;
    margin: 10px auto;
    height: 90px;
    background: #000;
    border: 4px double #fff;
    padding: 15px;
    box-sizing: border-box;
    display: flex;
    flex-direction: column;
    justify-content: center;
    border-radius: 4px;
    overflow: hidden;
}

.hidden {
    display: none !important;
}

#next-arrow {
    position: absolute;
    bottom: 10px;
    right: 15px;
    font-size: 14px;
    color: #fff;
    animation: bounce 0.6s infinite alternate;
}

@keyframes bounce {
    from {
        transform: translateY(0);
    }

    to {
        transform: translateY(4px);
    }
}

#message-box {
    font-size: 11px;
    /* Slightly smaller base font */
    line-height: 16px;
    height: 48px;
    /* 3 lines of 16px */
    color: #fff;
    text-shadow: 1px 1px 0 #000;
    padding-right: 20px;
    overflow: hidden;
    overflow-wrap: break-word;
}

#message-box .btn-a-icon,
#message-box .btn-b-icon {
    display: inline-block;
    width: 1.25em;
    height: 1.25em;
    padding: 0;
    margin: 0;
    vertical-align: -0.12em;
}

/* Mobile Adjustments */
@media screen and (max-width: 600px) {
    #ui-layer {
        height: auto;
        min-height: 80px;
        padding: 10px;
    }

    #message-box {
        font-size: 9px;
        line-height: 14px;
        height: 56px;
        /* 4 lines of 14px for more space on mobile */
    }

    #hud {
        font-size: 8px;
    }

    /* Show mobile controls when profile modal is active on touch devices */
    body.modal-open #mobile-controls {
        display: flex;
        position: fixed;
        bottom: 0;
        left: 0;
        z-index: 50;
        background: rgba(0, 0, 0, 0.8);
    }
}

#hud {
    position: absolute;
    top: 10px;
    left: 10px;
    font-size: 10px;
    line-height: 1;
    white-space: nowrap;
    color: var(--gold);
    text-shadow: 1px 1px 0 #000;
    z-index: 10;
    pointer-events: none;
}

#hud.multiline {
    white-space: normal;
    line-height: 1.2;
}

#hud.multiline .hud-line+.hud-line {
    margin-top: 3px;
}

#run-timer {
    position: absolute;
    top: 10px;
    right: 10px;
    font-size: 10px;
    line-height: 1;
    white-space: nowrap;
    color: var(--cyan);
    text-shadow: 1px 1px 0 #000;
    z-index: 10;
    pointer-events: none;
}

/* Keep HUD and timer behind the farewell leaderboard overlay layer. */
#game-container.results-overlay-active #hud,
#game-container.results-overlay-active #run-timer {
    z-index: 0;
    opacity: 0;
}

#profile-modal {
    position: fixed;
    inset: 0;
    background: #000;
    display: flex;
    align-items: flex-start;
    justify-content: center;
    z-index: 40;
    padding: 16px;
    padding-top: 40px;
    box-sizing: border-box;
    pointer-events: none;
    /* Force LTR for profile modal regardless of page direction */
    direction: ltr !important;
}

#profile-card {
    width: 100%;
    max-width: 420px;
    background: #111;
    border: 4px double #fff;
    border-radius: 6px;
    padding: 16px;
    box-sizing: border-box;
    box-shadow: 0 0 18px var(--dark-overlay);
    pointer-events: auto;
    /* Ensure all text within profile card is LTR */
    direction: ltr !important;
}

#profile-card h2 {
    margin: 0 0 8px;
    font-size: 12px;
    color: var(--gold);
    text-align: center;
}

#profile-subtitle {
    margin: 0 0 14px;
    font-size: 9px;
    color: #ccc;
    text-align: center;
    line-height: 1.5;
}

#profile-challenge {
    margin: 0 0 12px;
    font-size: 8px;
    line-height: 1.5;
    color: var(--cyan);
    text-align: center;
    border: 1px solid #3e6e7d;
    border-radius: 4px;
    padding: 8px;
    background: rgba(22, 44, 52, 0.55);
}

#profile-form {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

#profile-form label {
    font-size: 8px;
    color: #ddd;
}

#profile-form input {
    font-family: var(--font-arcade);
    font-size: 10px;
    padding: 10px;
    border: 2px solid #666;
    border-radius: 4px;
    background: #000;
    color: #fff;
    text-transform: uppercase;
}

#profile-form input#player-name {
    text-transform: none;
    width: 100%;
    box-sizing: border-box;
    text-align: center;
    letter-spacing: 1px;
}

#player-name-entry {
    margin: 0;
    padding: 10px;
    border: 2px solid #666;
    border-radius: 4px;
    background: #000;
}

#player-name-help {
    margin: 8px 0 0;
    font-size: 7px;
    line-height: 1.4;
    color: var(--cyan);
    text-align: center;
}

#player-country-display {
    margin: 0;
    min-height: 14px;
    font-size: 9px;
    line-height: 1.5;
    padding: 10px;
    border: 2px solid #666;
    border-radius: 4px;
    background: #000;
    color: #fff;
}

#profile-badges-wrap {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

#profile-badges-wrap label {
    font-size: 8px;
    color: #ddd;
}

#profile-stars-tally {
    display: flex;
    align-items: flex-start;
    gap: 8px;
}

#profile-stars-icon {
    position: relative;
    width: 14px;
    height: 14px;
    flex: 0 0 14px;
    margin-top: 4px;
}

#profile-stars-icon::before {
    content: '';
    position: absolute;
    left: 0;
    top: 0;
    width: 2px;
    height: 2px;
    background: #ffd700;
    box-shadow:
        6px 0 #ffd700,
        4px 2px #ffd700, 6px 2px #ffd700, 8px 2px #ffd700,
        2px 4px #ffd700, 4px 4px #ffd700, 6px 4px #ffd700, 8px 4px #ffd700, 10px 4px #ffd700,
        0 6px #ffd700, 2px 6px #ffd700, 4px 6px #ffd700, 6px 6px #ffd700, 8px 6px #ffd700, 10px 6px #ffd700, 12px 6px #ffd700,
        2px 8px #ffd700, 4px 8px #ffd700, 6px 8px #ffd700, 8px 8px #ffd700, 10px 8px #ffd700,
        4px 10px #ffd700, 6px 10px #ffd700, 8px 10px #ffd700,
        6px 12px #ffd700;
    animation: pixel-star-twinkle 0.9s steps(2, end) infinite;
}

#profile-stars-icon::after {
    content: '';
    position: absolute;
    left: 6px;
    top: 6px;
    width: 2px;
    height: 2px;
    background: #fff8d4;
    box-shadow:
        -2px 0 #fff8d4,
        2px 0 #fff8d4,
        0 -2px #fff8d4,
        0 2px #fff8d4;
    animation: pixel-star-core 0.9s steps(2, end) infinite;
}

#profile-badges {
    margin: 0;
    min-height: 20px;
    font-size: 8px;
    line-height: 1.5;
    white-space: pre-line;
    padding: 8px;
    border: 1px solid #665e2b;
    border-radius: 4px;
    background: rgba(48, 33, 0, 0.45);
    color: #ffe8a6;
    flex: 1;
}

@keyframes pixel-star-twinkle {

    0%,
    100% {
        transform: scale(1);
        opacity: 0.9;
    }

    50% {
        transform: scale(1.15);
        opacity: 1;
    }
}

@keyframes pixel-star-core {

    0%,
    100% {
        opacity: 0.6;
    }

    50% {
        opacity: 1;
    }
}

#profile-status {
    margin: 6px 0 0;
    min-height: 14px;
    font-size: 8px;
    color: var(--cyan);
}

#profile-error {
    margin: 0;
    min-height: 14px;
    font-size: 8px;
    color: #ff9aa2;
}

#profile-submit {
    margin-top: 6px;
    font-family: var(--font-arcade);
    font-size: 9px;
    padding: 10px 12px;
    border: 2px solid var(--gold);
    border-radius: 4px;
    background: #302100;
    color: var(--gold);
    cursor: pointer;
}

#profile-submit:disabled {
    opacity: 0.6;
    cursor: not-allowed;
}

#results-actions {
    position: absolute;
    left: 50%;
    transform: translateX(-50%);
    bottom: 122px;
    z-index: 30;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 8px;
    width: min(92%, 460px);
}

#game-container.results-overlay-compact #results-actions {
    top: 46%;
    bottom: auto;
    width: min(72%, 420px);
    gap: 6px;
}

#results-actions button {
    font-family: var(--font-arcade);
    font-size: 8px;
    padding: 9px 12px;
    border-radius: 4px;
    border: 2px solid var(--gold);
    background: rgba(25, 25, 25, 0.94);
    color: var(--gold);
    cursor: pointer;
    width: 100%;
}

#results-actions button:disabled {
    opacity: 0.65;
    cursor: not-allowed;
}

#results-share-status {
    margin: 2px 0 0;
    min-height: 12px;
    font-size: 7px;
    color: var(--cyan);
    text-align: center;
    text-shadow: 1px 1px 0 #000;
}

/* --- MOBILE CONTROLS --- */
#mobile-controls {
    width: 100%;
    height: 190px;
    background:
        linear-gradient(180deg,
            rgba(35, 40, 48, 0.95) 0%,
            rgba(9, 11, 15, 0.95) 8%,
            rgba(9, 11, 15, 0.95) 92%,
            rgba(5, 6, 9, 0.95) 100%);
    border-top: 2px solid #2b3138;
    margin-top: 20px;
    pointer-events: none;
    /* Let clicks pass through empty areas */
    display: none;
    /* Hidden by default (desktop) */
    justify-content: space-between;
    align-items: center;
    padding: 14px 22px calc(14px + env(safe-area-inset-bottom, 0px));
    box-sizing: border-box;
    /* Force LTR to prevent flipping in RTL languages */
    direction: ltr !important;
}

@media (hover: none) and (pointer: coarse) {
    #mobile-controls {
        display: flex;
        /* Show on touch devices */
    }
}

@media screen and (max-width: 600px) {
    #profile-card {
        padding: 12px;
    }

    #profile-card h2 {
        font-size: 10px;
    }

    #profile-subtitle,
    #profile-challenge,
    #profile-form label,
    #profile-badges-wrap label,
    #profile-status,
    #profile-error {
        font-size: 7px;
    }

    #profile-form input {
        font-size: 9px;
        padding: 8px;
    }

    #player-name-entry {
        padding: 8px;
    }

    #player-name-help {
        font-size: 6px;
    }

    #player-country-display {
        font-size: 8px;
        padding: 8px;
    }

    #profile-badges {
        font-size: 7px;
        padding: 7px;
    }

    #profile-stars-tally {
        gap: 6px;
    }

    #profile-stars-icon {
        margin-top: 3px;
    }

    #results-actions {
        bottom: 104px;
        gap: 6px;
    }

    #game-container.results-overlay-compact #results-actions {
        top: 45%;
        bottom: auto;
        width: min(78%, 320px);
    }

    #results-actions button {
        font-size: 7px;
        padding: 8px 10px;
    }

    #results-share-status {
        font-size: 6px;
    }
}

.d-pad {
    position: relative;
    width: 142px;
    height: 142px;
    pointer-events: auto;
    filter: none;
    background: radial-gradient(circle at 50% 48%,
            #060a0f 0%,
            #0a1018 60%,
            #0d1520 80%,
            transparent 81%);
    border-radius: 50%;
    box-shadow:
        inset 0 3px 6px rgba(0, 0, 0, 0.7),
        inset 0 1px 1px rgba(0, 0, 0, 0.5),
        inset 0 -2px 3px rgba(255, 255, 255, 0.04),
        0 1px 0 rgba(255, 255, 255, 0.03);
}

.d-pad::before {
    content: '';
    position: absolute;
    left: 50%;
    top: 50%;
    width: 28px;
    height: 28px;
    transform: translate(-50%, -50%);
    border-radius: 50%;
    background:
        radial-gradient(circle at 45% 40%, rgba(255, 255, 255, 0.12) 0%, transparent 60%),
        linear-gradient(180deg, #3a5069 0%, #253447 60%, #1c2838 100%);
    border: 2.5px solid #101b29;
    box-shadow:
        inset 0 1px 1px rgba(255, 255, 255, 0.18),
        inset 0 -2px 2px rgba(0, 0, 0, 0.5),
        0 1px 2px rgba(0, 0, 0, 0.5),
        0 0 0 1px rgba(0, 0, 0, 0.3);
    box-sizing: border-box;
    pointer-events: none;
    z-index: 2;
}

.d-row {
    display: contents;
}

.d-btn {
    position: absolute;
    box-sizing: border-box;
    border-radius: 10px;
    color: rgba(200, 210, 225, 0.7);
    font-size: 16px;
    font-weight: bold;
    background:
        radial-gradient(circle at 45% 38%, rgba(255, 255, 255, 0.10) 0%, transparent 55%),
        linear-gradient(180deg, #2c394a 0%, #263345 55%, #1e2b3b 100%);
    border: 2px solid #0f1a27;
    box-shadow:
        inset 0 1px 0 rgba(255, 255, 255, 0.16),
        inset 0 -2px 0 rgba(0, 0, 0, 0.4),
        0 2px 0 #080e16,
        0 3px 4px rgba(0, 0, 0, 0.35);
    display: inline-flex;
    align-items: center;
    justify-content: center;
    touch-action: none;
    user-select: none;
    -webkit-user-select: none;
    -webkit-touch-callout: none;
    text-shadow:
        0 -1px 0 rgba(0, 0, 0, 0.5),
        0 1px 0 rgba(255, 255, 255, 0.08);
    z-index: 3;
    transition: transform 0.06s, box-shadow 0.06s, background 0.06s;
}

.d-btn:active {
    background:
        radial-gradient(circle at 50% 50%, rgba(255, 255, 255, 0.04) 0%, transparent 50%),
        linear-gradient(180deg, #243040 0%, #1e2b3b 55%, #182433 100%);
    color: rgba(200, 210, 225, 0.9);
    transform: translateY(2px);
    box-shadow:
        inset 0 1px 0 rgba(255, 255, 255, 0.08),
        inset 0 -1px 0 rgba(0, 0, 0, 0.3),
        0 0 0 #080e16,
        0 1px 2px rgba(0, 0, 0, 0.25);
    text-shadow: none;
}

#btn-up {
    width: 46px;
    height: 36px;
    left: calc((100% - 46px) / 2);
    top: 10px;
}

#btn-left {
    width: 36px;
    height: 46px;
    left: 10px;
    top: calc((100% - 46px) / 2);
}

#btn-right {
    width: 36px;
    height: 46px;
    right: 10px;
    top: calc((100% - 46px) / 2);
}

#btn-down {
    width: 46px;
    height: 36px;
    left: calc((100% - 46px) / 2);
    bottom: 10px;
}

.action-grp {
    position: relative;
    width: 152px;
    height: 128px;
    pointer-events: auto;
}

.action-grp::before {
    content: '';
    position: absolute;
    top: 2px;
    left: -2px;
    right: -2px;
    bottom: 2px;
    border-radius: 60px;
    background: radial-gradient(ellipse at 50% 48%,
            #060a0f 0%,
            #0a1018 70%,
            #0d1520 90%,
            transparent 91%);
    box-shadow:
        inset 0 3px 5px rgba(0, 0, 0, 0.6),
        inset 0 1px 1px rgba(0, 0, 0, 0.4),
        inset 0 -2px 3px rgba(255, 255, 255, 0.03),
        0 1px 0 rgba(255, 255, 255, 0.03);
    z-index: 0;
}

.action-btn {
    position: absolute;
    width: 60px;
    height: 60px;
    border-radius: 50%;
    color: rgba(245, 247, 251, 0.95);
    font-size: 17px;
    font-weight: bold;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    touch-action: none;
    user-select: none;
    -webkit-user-select: none;
    -webkit-touch-callout: none;
    z-index: 1;
    transition: transform 0.06s, box-shadow 0.06s;
}

.action-btn.action-a {
    background:
        radial-gradient(circle at 45% 38%, rgba(255, 255, 255, 0.18) 0%, transparent 55%),
        radial-gradient(circle at 55% 65%, rgba(0, 0, 0, 0.15) 0%, transparent 50%),
        linear-gradient(180deg, #c45454 0%, #b24747 40%, #8f3636 100%);
    border: 2.5px solid #6c2323;
    right: 8px;
    top: 10px;
    box-shadow:
        inset 0 2px 1px rgba(255, 255, 255, 0.25),
        inset 0 -2px 2px rgba(0, 0, 0, 0.35),
        0 3px 0 #5a1c1c,
        0 4px 4px rgba(0, 0, 0, 0.4),
        0 6px 12px rgba(0, 0, 0, 0.25);
    text-shadow:
        0 -1px 0 rgba(0, 0, 0, 0.4),
        0 1px 0 rgba(255, 255, 255, 0.1);
}

.action-btn.action-a:active {
    background:
        radial-gradient(circle at 50% 50%, rgba(255, 255, 255, 0.06) 0%, transparent 50%),
        linear-gradient(180deg, #a84040 0%, #9f3e3e 40%, #7d2e2e 100%);
    border-color: #531b1b;
    transform: translateY(2px);
    box-shadow:
        inset 0 1px 1px rgba(255, 255, 255, 0.12),
        inset 0 -1px 1px rgba(0, 0, 0, 0.25),
        0 1px 0 #5a1c1c,
        0 2px 3px rgba(0, 0, 0, 0.3);
}

.action-btn.action-b {
    background:
        radial-gradient(circle at 45% 38%, rgba(255, 255, 255, 0.18) 0%, transparent 55%),
        radial-gradient(circle at 55% 65%, rgba(0, 0, 0, 0.15) 0%, transparent 50%),
        linear-gradient(180deg, #4d78aa 0%, #3f6699 40%, #2d5080 100%);
    border: 2.5px solid #1f3f67;
    left: 10px;
    top: 52px;
    box-shadow:
        inset 0 2px 1px rgba(255, 255, 255, 0.25),
        inset 0 -2px 2px rgba(0, 0, 0, 0.35),
        0 3px 0 #162f50,
        0 4px 4px rgba(0, 0, 0, 0.4),
        0 6px 12px rgba(0, 0, 0, 0.25);
    text-shadow:
        0 -1px 0 rgba(0, 0, 0, 0.4),
        0 1px 0 rgba(255, 255, 255, 0.1);
}

.action-btn.action-b:active {
    background:
        radial-gradient(circle at 50% 50%, rgba(255, 255, 255, 0.06) 0%, transparent 50%),
        linear-gradient(180deg, #3a5d8a 0%, #355783 40%, #264369 100%);
    border-color: #18314f;
    transform: translateY(2px);
    box-shadow:
        inset 0 1px 1px rgba(255, 255, 255, 0.12),
        inset 0 -1px 1px rgba(0, 0, 0, 0.25),
        0 1px 0 #162f50,
        0 2px 3px rgba(0, 0, 0, 0.3);
}

#mobile-controls button:focus,
#mobile-controls button:focus-visible {
    outline: none;
}

@media screen and (max-width: 600px) {
    .d-pad {
        width: 134px;
        height: 134px;
    }

    .d-pad::before {
        width: 23px;
        height: 23px;
    }

    .d-btn {
        font-size: 15px;
        border-radius: 8px;
    }

    #btn-up {
        width: 42px;
        height: 34px;
        left: calc((100% - 42px) / 2);
        top: 9px;
    }

    #btn-left {
        width: 34px;
        height: 42px;
        left: 9px;
        top: calc((100% - 42px) / 2);
    }

    #btn-right {
        width: 34px;
        height: 42px;
        right: 9px;
        top: calc((100% - 42px) / 2);
    }

    #btn-down {
        width: 42px;
        height: 34px;
        left: calc((100% - 42px) / 2);
        bottom: 9px;
    }

    .action-grp {
        width: 140px;
        height: 118px;
    }

    .action-btn {
        width: 54px;
        height: 54px;
        font-size: 16px;
    }

    .action-btn.action-b {
        top: 48px;
    }
}

/* Final desktop overrides for profile readability (kept at the same 640x480 panel size). */
@media screen and (min-width: 601px) {
    #profile-card {
        width: min(640px, calc(100vw - 32px));
        max-width: 640px;
        height: min(480px, calc(100vh - 32px));
        max-height: 480px;
        overflow-y: auto;
        padding: 14px 16px;
    }

    #profile-card h2 {
        margin: 0 0 10px;
        font-size: 24px;
    }

    #profile-subtitle {
        margin: 0 0 10px;
        font-size: 16px;
        line-height: 1.35;
    }

    #profile-challenge {
        margin: 0 0 10px;
        font-size: 13px;
        line-height: 1.35;
        padding: 8px 10px;
    }

    #profile-form label,
    #profile-badges-wrap label {
        font-size: 14px;
    }

    #profile-form input {
        font-size: 15px;
        padding: 11px;
    }

    #profile-form input#player-name {
        font-size: 18px;
    }

    #profile-submit {
        font-size: 14px;
        padding: 11px 14px;
    }

    #player-name-entry {
        padding: 10px;
    }

    #player-name-help {
        margin-top: 8px;
        font-size: 15px;
        line-height: 1.2;
    }

    #player-country-display {
        font-size: 15px;
        line-height: 1.3;
    }

    #profile-badges {
        font-size: 14px;
        line-height: 1.28;
    }

    #profile-status,
    #profile-error {
        min-height: 14px;
        font-size: 15px;
        line-height: 1.2;
    }
}
