/* Midnight Dash. Logical properties only, so RTL and LTR share one file. Keyframes are prefixed md-. */
:root {
    --bg: #070b1f; --night: #0a1030; --surface: rgba(8, 12, 36, .74); --border: rgba(255, 255, 255, .16);
    --text: #f3f5ff; --muted: #aab3d6; --amber: #ffc53d; --amber2: #ff8a1f; --cyan: #4cd6ff; --hot: #ff4d6d; --ink: #10091c;
    --font: system-ui, "Segoe UI", Tahoma, "Noto Sans Arabic", "Noto Sans", Arial, sans-serif;
}
* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); color: var(--text); font-family: var(--font); }
body { -webkit-tap-highlight-color: transparent; user-select: none; -webkit-user-select: none; overscroll-behavior: none; }
button { font: inherit; color: inherit; }
[hidden] { display: none !important; }

.game { max-inline-size: 980px; margin-inline: auto; padding: 8px; }
.game:focus { outline: none; }   /* focused on Play only so keys reach the game */

.stage {
    position: relative; overflow: hidden; border-radius: 16px; inline-size: 100%; aspect-ratio: 16 / 10; max-block-size: min(620px, calc(100vh - 16px));
    background: var(--night); box-shadow: inset 0 0 0 1px var(--border); touch-action: none; isolation: isolate; cursor: default;
}
@media (max-width: 559px) { .stage { aspect-ratio: 3 / 4; } }
:fullscreen .stage, .stage:fullscreen { aspect-ratio: auto; max-block-size: none; block-size: 100vh; border-radius: 0; }
.stage.run { cursor: none; }

canvas { position: absolute; inset: 0; inline-size: 100%; block-size: 100%; display: block; }

/* Speed glow at the edges (opacity set from the game), blue while nitro burns. */
.vig { position: absolute; inset: 0; z-index: 1; pointer-events: none; opacity: 0; transition: opacity .25s;
    background: radial-gradient(130% 95% at 50% 55%, transparent 58%, rgba(255, 140, 40, .32) 100%); }
.vig::after { content: ""; position: absolute; inset: 0; opacity: 0; transition: opacity .3s;
    background: radial-gradient(130% 95% at 50% 55%, transparent 42%, rgba(76, 214, 255, .55) 100%); }
.stage.boost .vig { opacity: 1 !important; }
.stage.boost .vig::after { opacity: 1; }
.flash { position: absolute; inset: 0; z-index: 1; pointer-events: none; opacity: 0; }
.flash.crash, .flash.blue, .flash.gold, .flash.l, .flash.r { animation: md-flash .45s ease-out; }
.flash.crash { background: radial-gradient(120% 90% at 50% 60%, rgba(255, 255, 255, .5), rgba(255, 40, 80, .55)); animation-duration: .7s; }
.flash.blue { background: radial-gradient(130% 95% at 50% 55%, rgba(76, 214, 255, 0) 30%, rgba(76, 214, 255, .55) 100%); }
.flash.gold { background: radial-gradient(130% 95% at 50% 55%, rgba(255, 197, 61, 0) 35%, rgba(255, 197, 61, .45) 100%); }
.flash.l { background: linear-gradient(90deg, rgba(76, 214, 255, .5), transparent 30%); }
.flash.r { background: linear-gradient(-90deg, rgba(76, 214, 255, .5), transparent 30%); }
@keyframes md-flash { from { opacity: 1; } to { opacity: 0; } }

/* HUD */
.hud { position: absolute; inset-block-start: 8px; inset-inline: 8px; z-index: 3; display: flex; align-items: center; gap: 6px; pointer-events: none; }
.hud .gap { flex: 1; }
.pill {
    display: flex; flex-direction: column; align-items: center; min-inline-size: 84px; padding: 3px 12px;
    background: var(--surface); border: 1px solid var(--border); border-radius: 12px; backdrop-filter: blur(4px);
}
.pill .k { font-size: .66rem; color: var(--muted); line-height: 1.2; }
.pill b { font-size: 1.3rem; font-variant-numeric: tabular-nums; line-height: 1.25; }
.pill.bump b { animation: md-bump .25s ease-out; }
.pill.mult { min-inline-size: 0; padding: 6px 12px; border-color: rgba(255, 197, 61, .6); }
.pill.mult b { color: var(--amber); text-shadow: 0 0 12px rgba(255, 197, 61, .7); font-size: 1.2rem; font-weight: 900; }
.pill.mult.bump { animation: md-bump .3s ease-out; }
.lives { display: flex; gap: 3px; padding: 6px 9px; border-radius: 12px; background: var(--surface); border: 1px solid var(--border); }
.lives i { inline-size: 18px; block-size: 18px; display: block; transition: opacity .3s, transform .3s;
    background: no-repeat center / contain url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M12 21s-8-5.2-8-11a4.5 4.5 0 0 1 8-2.8A4.5 4.5 0 0 1 20 10c0 5.8-8 11-8 11z' fill='%23ff4d6d'/%3E%3C/svg%3E"); }
.lives i.off { opacity: .22; transform: scale(.75); filter: grayscale(1); }
.lives i.gain { animation: md-bump .5s ease-out; }
.icon {
    pointer-events: auto; flex: none; inline-size: 40px; block-size: 40px; border-radius: 12px; cursor: pointer; padding: 0;
    background: var(--surface); border: 1px solid var(--border); display: grid; place-items: center;
}
.icon svg { inline-size: 20px; block-size: 20px; }
.icon:focus-visible, .btn:focus-visible, .nitro:focus-visible, .paint:focus-visible { outline: 3px solid var(--cyan); outline-offset: 2px; }

.dash { position: absolute; inset-block-end: 10px; inset-inline-start: 10px; z-index: 3; display: flex; align-items: end; gap: 8px; pointer-events: none; }
.spd { display: flex; align-items: baseline; gap: 4px; padding: 4px 12px; border-radius: 12px; background: var(--surface); border: 1px solid var(--border); }
.spd b { font-size: 1.6rem; font-style: italic; font-variant-numeric: tabular-nums; min-inline-size: 3ch; text-align: end; color: var(--amber); text-shadow: 0 0 10px rgba(255, 197, 61, .55); }
.spd span { font-size: .75rem; color: var(--muted); }
.dist { padding: 6px 10px; border-radius: 12px; background: var(--surface); border: 1px solid var(--border); font-size: .85rem; font-variant-numeric: tabular-nums; color: var(--muted); }
.nitro {
    position: absolute; inset-block-end: 10px; inset-inline-end: 10px; z-index: 3;
    display: grid; gap: 4px; justify-items: center; inline-size: 128px; padding: 9px 10px; border-radius: 14px; cursor: pointer;
    background: var(--surface); border: 1px solid var(--border); font-weight: 800; letter-spacing: .04em; font-size: .82rem;
}
.nitro .fill { display: block; inline-size: 100%; block-size: 8px; border-radius: 99px; background: rgba(255, 255, 255, .12); overflow: hidden; position: relative; }
.nitro .fill::before { content: ""; position: absolute; inset-block: 0; inset-inline-start: 0; inline-size: calc(var(--n, 0) * 100%);
    background: linear-gradient(90deg, #4c8dff, var(--cyan)); border-radius: inherit; }
[dir="rtl"] .nitro .fill::before { background: linear-gradient(-90deg, #4c8dff, var(--cyan)); }
.nitro.ready { color: var(--ink); background: var(--cyan); border-color: var(--cyan); box-shadow: 0 0 22px rgba(76, 214, 255, .7); animation: md-ready .8s ease-in-out infinite alternate; }
.nitro.ready .fill { background: rgba(0, 0, 0, .2); }
.nitro.ready .fill::before { background: var(--ink); }
.nitro.burn { color: var(--ink); background: #8ab4ff; border-color: #8ab4ff; }
@keyframes md-ready { to { transform: scale(1.06); } }
@keyframes md-bump { 40% { transform: scale(1.3); } }

/* District name, coaching tips, countdown. */
.zone { position: absolute; inset-inline: 0; inset-block-start: 17%; z-index: 2; display: grid; justify-items: center; pointer-events: none; }
.zone span { grid-area: 1 / 1; display: grid; justify-items: center; gap: 2px; animation: md-zone 2.6s ease-out forwards; }
.zone small { font-size: .72rem; letter-spacing: .3em; color: var(--muted); text-transform: uppercase; }
.zone b { font-size: clamp(1.4rem, 5.5vw, 2.3rem); font-weight: 900; font-style: italic; color: var(--text);
    text-shadow: 0 0 22px var(--zc, var(--amber)), 0 2px 0 rgba(0, 0, 0, .5); }
@keyframes md-zone { 0% { opacity: 0; transform: translateY(10px) scale(.9); letter-spacing: .3em; } 15% { opacity: 1; transform: none; letter-spacing: .02em; } 75% { opacity: 1; } 100% { opacity: 0; transform: translateY(-10px); } }
.tip { position: absolute; inset-inline: 0; inset-block-start: 31%; z-index: 2; display: grid; justify-items: center; pointer-events: none; padding-inline: 16px; }
.tip span { grid-area: 1 / 1; padding: 8px 16px; border-radius: 99px; background: rgba(8, 12, 36, .78); border: 1px solid rgba(255, 197, 61, .45);
    font-weight: 700; font-size: clamp(.85rem, 3.2vw, 1.02rem); text-align: center; animation: md-tip 3.2s ease-out forwards; }
@keyframes md-tip { 0% { opacity: 0; transform: translateY(10px); } 10% { opacity: 1; transform: none; } 85% { opacity: 1; } 100% { opacity: 0; } }
.count { position: absolute; inset: 0; z-index: 2; display: grid; place-items: center; pointer-events: none; }
.count span { position: relative; font-size: clamp(3rem, 16vw, 7rem); font-weight: 900; font-style: italic; color: var(--text);
    text-shadow: 0 0 24px var(--amber2), 0 0 4px var(--amber2); animation: md-count .7s ease-out forwards; }
.count span.go { color: var(--cyan); text-shadow: 0 0 26px var(--cyan); }

/* Menus */
.overlay { position: absolute; inset: 0; z-index: 4; display: grid; place-items: center; padding: 16px; overflow-y: auto; }
.overlay:empty { display: none; }
.overlay.dim { background: rgba(5, 8, 26, .55); backdrop-filter: blur(3px); }
.panel {
    inline-size: min(100%, 440px); text-align: center; padding: 20px 20px; border-radius: 20px;
    background: rgba(8, 12, 36, .84); border: 1px solid var(--border); box-shadow: 0 20px 60px rgba(0, 0, 0, .45); backdrop-filter: blur(6px);
    animation: md-rise .45s cubic-bezier(.2, .9, .3, 1.2) both;
}
.panel h2 { margin: 0 0 6px; font-size: clamp(1.5rem, 6vw, 2.2rem); font-style: italic; letter-spacing: .02em; line-height: 1.15;
    background: linear-gradient(90deg, var(--amber), var(--cyan), var(--amber)); background-size: 200% 100%; -webkit-background-clip: text; background-clip: text; color: transparent;
    animation: md-shimmer 4s linear infinite; }
.panel p { margin: 6px 0; color: var(--muted); line-height: 1.5; font-size: .95rem; }
.panel .keys { font-size: .84rem; }
.panel .big { font-size: 2.7rem; font-weight: 900; color: var(--text); margin: 4px 0; font-variant-numeric: tabular-nums; }
.panel .big.land { animation: md-land .4s ease-out; }
.panel .best { color: var(--amber); font-weight: 700; }
.stats { display: grid; grid-template-columns: repeat(2, 1fr); gap: 6px; margin: 12px 0 4px; }
.stats div { padding: 8px 4px; border-radius: 12px; background: rgba(255, 255, 255, .06); animation: md-rise .4s cubic-bezier(.2, .9, .3, 1.2) both; }
.stats div:nth-child(2) { animation-delay: .1s; } .stats div:nth-child(3) { animation-delay: .2s; } .stats div:nth-child(4) { animation-delay: .3s; }
.stats b { display: block; font-size: 1.15rem; font-variant-numeric: tabular-nums; }
.stats span { font-size: .72rem; color: var(--muted); }
.btns { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; margin-block-start: 12px; }
.btn {
    display: inline-flex; align-items: center; justify-content: center; gap: 8px; min-inline-size: 180px; padding: 13px 26px;
    border: 0; border-radius: 14px; cursor: pointer; font-weight: 800; font-size: 1.1rem; color: var(--ink);
    background: linear-gradient(90deg, var(--amber), #ffe08a); box-shadow: 0 0 24px rgba(255, 197, 61, .45);
}
.overlay:not(.dim) .btn:not(:disabled):not(.ghost) { animation: md-glow 1.8s ease-in-out .5s infinite; }
.btn:disabled { opacity: .7; cursor: wait; }
.btn.ghost { background: transparent; color: var(--text); box-shadow: inset 0 0 0 1px var(--border); min-inline-size: 0; font-size: .95rem; padding: 10px 18px; }
.badge { display: inline-block; margin-block-start: 6px; padding: 4px 12px; border-radius: 99px; background: var(--amber); color: var(--ink); font-weight: 800; font-size: .85rem;
    box-shadow: 0 0 18px rgba(255, 197, 61, .6); animation: md-badge .5s cubic-bezier(.3, 1.7, .5, 1) .4s both; }

/* Car paint picker */
.paints { display: flex; justify-content: center; gap: 10px; margin: 10px 0 2px; flex-wrap: wrap; }
.paint { position: relative; inline-size: 44px; block-size: 44px; border-radius: 50%; border: 2px solid transparent; cursor: pointer; padding: 0;
    background: radial-gradient(circle at 35% 30%, var(--a), var(--b)); box-shadow: 0 2px 8px rgba(0, 0, 0, .4); transition: transform .15s; }
.paint:hover { transform: scale(1.08); }
.paint.on { border-color: #fff; box-shadow: 0 0 0 3px rgba(255, 255, 255, .25), 0 0 16px var(--a); }
.paint:disabled { cursor: not-allowed; filter: grayscale(.85) brightness(.55); transform: none; }
.paint .lock { position: absolute; inset: 0; display: grid; place-items: center; font-size: .62rem; font-weight: 800; color: #fff; text-shadow: 0 1px 2px #000; }
.paints-k { font-size: .78rem; color: var(--muted); margin: 4px 0 0; }

@keyframes md-count { 0% { opacity: 0; transform: scale(1.8); } 25% { opacity: 1; transform: scale(1); } 100% { opacity: 0; transform: scale(.85); } }
@keyframes md-rise { from { opacity: 0; transform: translateY(14px) scale(.96); } }
@keyframes md-shimmer { to { background-position: -200% 0; } }
@keyframes md-glow { 50% { box-shadow: 0 0 0 6px rgba(255, 197, 61, .15), 0 0 34px rgba(255, 197, 61, .7); } }
@keyframes md-badge { from { opacity: 0; transform: scale(.3) rotate(-8deg); } }
@keyframes md-land { 35% { transform: scale(1.15); color: var(--amber); } }

.hint { margin: 8px 4px 0; text-align: center; color: var(--muted); font-size: .85rem; }

@media (max-width: 420px) {
    .pill { min-inline-size: 66px; padding-inline: 8px; }
    .icon { inline-size: 36px; block-size: 36px; }
    .nitro { inline-size: 108px; }
    .spd b { font-size: 1.3rem; }
    .lives i { inline-size: 15px; block-size: 15px; }
    .dist { display: none; }
}
@media (max-height: 420px) { .panel { padding: 12px 16px; } .panel h2 { font-size: 1.4rem; } .panel p { margin: 3px 0; } .btns { margin-block-start: 8px; } }
@media (prefers-reduced-motion: reduce) {
    .panel, .panel h2, .btn, .stats div, .badge, .panel .big.land, .count span, .zone span, .tip span, .nitro.ready, .pill.bump b, .pill.mult.bump, .flash.crash, .flash.blue, .flash.gold, .flash.l, .flash.r { animation: none !important; }
}
