:root {
  --umber: #1a110b;
  --umber-2: #2a1b12;
  --sand: #ecc9a0;
  --cream: #f6ecda;
  --red: #9a2117;
  --gold: #e0ac48;
  --glass: rgba(24, 14, 8, 0.52);
  --line: rgba(246, 236, 218, 0.16);
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --kufi: "Reem Kufi", "Tajawal", system-ui, sans-serif;
  --ui: "Tajawal", system-ui, sans-serif;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body {
  margin: 0; height: 100%; overflow: hidden; background: #1a110b; color: var(--cream);
  font-family: var(--ui); user-select: none; -webkit-user-select: none; touch-action: none;
}
#scene { position: fixed; inset: 0; width: 100%; height: 100%; display: block; }
[hidden] { display: none !important; }
button { font-family: inherit; color: inherit; border: 0; cursor: pointer; }

/* ---------------------------------------------------------------- HUD */

#hud { position: fixed; inset: 0; pointer-events: none; }
#hud button { pointer-events: auto; }

#top {
  position: absolute; top: calc(10px + var(--safe-t)); left: 10px; right: 10px;
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
}
.chip {
  display: flex; flex-direction: column; align-items: center; min-width: 70px; padding: 6px 12px 5px;
  background: var(--glass); border: 1px solid var(--line); border-radius: 14px;
  backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
}
.chip b { font-size: 22px; font-weight: 900; line-height: 1; font-variant-numeric: tabular-nums; letter-spacing: 0.5px; }
.chip small { font-size: 11px; opacity: 0.7; margin-top: 2px; }
#clockChip.late b { color: #ff9b6a; animation: pulse 1s ease-in-out infinite; }

#compass {
  position: relative; flex: 1; max-width: 320px; height: 40px; overflow: hidden;
  background: linear-gradient(90deg, transparent, var(--glass) 18%, var(--glass) 82%, transparent);
  border-radius: 12px;
}
#compassMarks { position: absolute; inset: 0; }
.needle-tick { position: absolute; left: 50%; top: 4px; bottom: 4px; width: 2px; margin-left: -1px; background: rgba(246, 236, 218, 0.5); border-radius: 2px; }
.mark {
  position: absolute; left: 50%; top: 4px; width: 0; display: flex; flex-direction: column; align-items: center;
  transition: transform 0.08s linear;
}
.mark i { display: block; font-style: normal; width: 12px; height: 12px; margin-left: -6px; border-radius: 50%; }
.mark span { font-size: 10px; font-weight: 700; white-space: nowrap; margin-top: 2px; transform: translateX(-50%); position: absolute; top: 18px; left: 0; }
.mark.camel i { background: #f0c27a; box-shadow: 0 0 0 2px rgba(26, 17, 11, 0.6); }
.mark.camel.nearest i { width: 18px; height: 18px; margin-left: -9px; margin-top: -2px; background: none; box-shadow: none; }
.mark.camel.nearest i::before { content: "🐪"; font-size: 17px; line-height: 18px; display: block; text-align: center; }
.mark.camp i { width: 18px; height: 18px; margin-left: -9px; margin-top: -2px; background: none; }
.mark.camp i::before { content: "⛺"; font-size: 16px; line-height: 18px; display: block; text-align: center; }
.mark.camp.hot i { animation: pulse 0.9s ease-in-out infinite; }
.mark.dallah i { width: 7px; height: 7px; margin-left: -3.5px; margin-top: 3px; background: var(--gold); opacity: 0.9; }
.mark.edge { opacity: 0.6; }

#herd {
  position: absolute; top: calc(62px + var(--safe-t)); left: 50%; transform: translateX(-50%);
  padding: 5px 14px; border-radius: 999px; background: var(--glass); border: 1px solid var(--line);
  font-size: 14px; white-space: nowrap; backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px);
}
#herd b { font-size: 16px; color: var(--gold); font-variant-numeric: tabular-nums; }
#herd .sep { opacity: 0.45; margin: 0 4px; }

#toasts { position: absolute; top: calc(130px + var(--safe-t)); left: 50%; transform: translateX(-50%); width: min(92vw, 420px); display: flex; flex-direction: column; align-items: center; gap: 6px; }
.toast {
  padding: 7px 16px; border-radius: 14px; background: rgba(26, 17, 11, 0.72); border: 1px solid var(--line);
  font-size: 16px; font-weight: 700; text-align: center; animation: toastIn 0.35s cubic-bezier(0.22, 1, 0.36, 1) both;
  transition: opacity 0.5s, transform 0.5s;
}
.toast b { color: var(--gold); margin-inline-start: 6px; font-variant-numeric: tabular-nums; }
.toast.out { opacity: 0; transform: translateY(-8px); }
.toast.deliver { background: linear-gradient(135deg, rgba(154, 33, 23, 0.85), rgba(110, 50, 16, 0.85)); font-size: 18px; }
.toast.camel { border-color: rgba(240, 194, 122, 0.6); }
.toast.warn { border-color: rgba(255, 120, 80, 0.7); }
.toast.hint { font-weight: 500; font-size: 15px; }

#driftMeter {
  position: absolute; top: 46%; left: 50%; transform: translate(-50%, -50%) scale(0.85); opacity: 0;
  font-family: var(--kufi); font-size: 22px; text-shadow: 0 2px 10px rgba(0, 0, 0, 0.5); transition: opacity 0.2s, transform 0.2s;
}
#driftMeter b { font-size: 30px; color: var(--gold); font-variant-numeric: tabular-nums; }
#driftMeter.on { opacity: 1; transform: translate(-50%, -50%) scale(1); }

#banner {
  position: absolute; top: 36%; left: 50%; transform: translateX(-50%); padding: 10px 18px; border-radius: 16px;
  background: rgba(120, 24, 14, 0.82); border: 1px solid rgba(255, 170, 120, 0.5); font-size: 17px; font-weight: 900;
  white-space: nowrap; display: flex; align-items: center; gap: 12px;
}
#bannerText { animation: pulse 1.2s ease-in-out infinite; }
#banner .dig {
  padding: 8px 14px; border-radius: 12px; font-size: 16px; font-weight: 900; color: #241406;
  background: linear-gradient(180deg, #f0be5c, #c08128); box-shadow: inset 0 -3px 0 rgba(0, 0, 0, 0.2);
}
#banner .dig:active { transform: scale(0.96); }
@media (max-width: 520px) { #banner { flex-direction: column; gap: 8px; white-space: normal; text-align: center; font-size: 15px; } }

#corner { position: absolute; top: calc(108px + var(--safe-t)); left: 10px; display: flex; flex-direction: column; gap: 8px; }
.icon-btn {
  width: 44px; height: 44px; border-radius: 14px; background: var(--glass); border: 1px solid var(--line);
  font-size: 19px; display: grid; place-items: center; backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px);
}
.icon-btn.tires { height: 54px; display: flex; flex-direction: column; gap: 0; }
.icon-btn.tires small { font-size: 10px; font-weight: 700; }
.icon-btn.tires.low { background: rgba(224, 172, 72, 0.35); border-color: var(--gold); }
.icon-btn.pulse { animation: pulseRing 1s ease-in-out infinite; }

/* small speedometer, bottom centre */
#gaugeWrap { position: absolute; bottom: calc(10px + var(--safe-b)); left: 50%; transform: translateX(-50%); width: 84px; height: 84px; }
#gauge { width: 100%; height: 100%; }
.g-bg { fill: rgba(20, 12, 7, 0.62); stroke: rgba(246, 236, 218, 0.18); stroke-width: 1.5; }
.g-track { fill: none; stroke: rgba(246, 236, 218, 0.1); stroke-width: 5; stroke-dasharray: 176 264; transform: rotate(150deg); transform-origin: 60px 60px; }
.g-rpm { fill: none; stroke: var(--gold); stroke-width: 5; stroke-linecap: round; stroke-dasharray: 176 264; stroke-dashoffset: 176; transform: rotate(150deg); transform-origin: 60px 60px; transition: stroke-dashoffset 0.08s linear; }
.g-ticks line { stroke: rgba(246, 236, 218, 0.55); stroke-width: 2; }
.g-needle { stroke: #ff6a3a; stroke-width: 2.6; stroke-linecap: round; transform-origin: 60px 60px; transform: rotate(-120deg); transition: transform 0.08s linear; }
.g-hub { fill: var(--cream); }
.g-read { position: absolute; left: 0; right: 0; bottom: 10px; display: flex; flex-direction: column; align-items: center; line-height: 1; }
.g-read b { font-size: 18px; font-weight: 900; font-variant-numeric: tabular-nums; }
.g-read small { font-size: 9px; opacity: 0.7; }
.g-read em { font-style: normal; font-size: 10px; font-weight: 900; color: var(--gold); margin-top: 2px; }

#controls {
  position: absolute; left: 0; right: 0; bottom: calc(12px + var(--safe-b)); padding: 0 12px;
  display: flex; justify-content: space-between; align-items: flex-end;
}
.cluster { display: flex; gap: 8px; align-items: flex-end; }
.cluster.drive { position: relative; }
.pad {
  width: 64px; height: 68px; border-radius: 22px; background: rgba(26, 17, 11, 0.55); border: 1px solid rgba(246, 236, 218, 0.22);
  display: grid; place-items: center; backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px);
  transition: transform 0.08s, background 0.08s;
}
.pad svg { width: 30px; height: 30px; fill: none; stroke: var(--cream); stroke-width: 3; stroke-linecap: round; stroke-linejoin: round; }
.minis { position: absolute; right: 0; bottom: calc(100% + 10px); display: flex; gap: 8px; }
.mini {
  width: 64px; height: 46px; border-radius: 14px; background: rgba(26, 17, 11, 0.55); border: 1px solid rgba(246, 236, 218, 0.2);
  font-size: 16px; display: flex; flex-direction: column; align-items: center; justify-content: center; line-height: 1.05;
  backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px);
}
.mini small { font-size: 10px; font-weight: 700; opacity: 0.85; }
.pedal {
  width: 64px; border-radius: 18px; display: flex; flex-direction: column; align-items: center; justify-content: center;
  font-weight: 900; font-size: 15px; line-height: 1.1; transition: transform 0.08s, filter 0.08s;
  box-shadow: inset 0 -6px 0 rgba(0, 0, 0, 0.28);
}
.pedal small { font-size: 10px; font-weight: 700; opacity: 0.8; }
.pedal.brake { height: 80px; background: linear-gradient(180deg, #6d2a1d, #3f160f); border: 1px solid rgba(255, 150, 120, 0.35); }
.pedal.gas { height: 108px; background: linear-gradient(180deg, #e0ac48, #a26e1d); color: #241406; border: 1px solid rgba(255, 230, 170, 0.6); }
.down { transform: scale(0.94); filter: brightness(1.2); }
.pad.down { background: rgba(224, 172, 72, 0.4); }

#keys { display: none; position: absolute; bottom: 14px; left: 14px; font-size: 12px; opacity: 0.65; direction: rtl; }

@media (pointer: fine) and (min-width: 820px) {
  #controls { display: none; }
  #keys { display: block; }
  #corner { top: auto; bottom: 18px; left: auto; right: 16px; flex-direction: row; }
  .chip b { font-size: 26px; }
  #compass { max-width: 420px; height: 44px; }
  #gaugeWrap { width: 124px; height: 124px; bottom: 16px; }
  .g-read b { font-size: 24px; }
}
@media (max-width: 380px) {
  .pad { width: 56px; height: 62px; }
  .pedal { width: 58px; }
  .mini { width: 58px; }
  #gaugeWrap { width: 72px; height: 72px; }
  .g-read b { font-size: 15px; }
}

/* ---------------------------------------------------------------- menus */

#menu, #end {
  position: fixed; inset: 0; display: flex; align-items: flex-end; justify-content: center;
  padding: 16px 16px calc(22px + var(--safe-b));
  background: linear-gradient(180deg, transparent 30%, rgba(20, 11, 6, 0.55) 62%, rgba(20, 11, 6, 0.85));
}
.card {
  position: relative; width: min(100%, 440px); padding: 26px 22px 20px; border-radius: 26px; text-align: center;
  background: rgba(26, 16, 10, 0.72); border: 1px solid rgba(246, 236, 218, 0.14);
  backdrop-filter: blur(16px); -webkit-backdrop-filter: blur(16px); overflow: hidden;
  animation: rise 0.6s cubic-bezier(0.2, 0.9, 0.3, 1) both;
}
.sadu {
  position: absolute; top: 0; left: 0; right: 0; height: 12px;
  background:
    linear-gradient(135deg, transparent 33%, #efe4cc 33%, #efe4cc 50%, transparent 50%) 0 0 / 16px 12px,
    linear-gradient(225deg, transparent 33%, #efe4cc 33%, #efe4cc 50%, transparent 50%) 0 0 / 16px 12px,
    linear-gradient(180deg, #141010 0 2px, var(--red) 2px 10px, #141010 10px);
}
h1, h2 { font-family: var(--kufi); margin: 6px 0 8px; line-height: 1.1; }
h1 { font-size: clamp(40px, 11vw, 56px); color: var(--sand); text-shadow: 0 3px 24px rgba(224, 120, 40, 0.35); }
h2 { font-size: 28px; color: var(--sand); }
.lede { margin: 0 auto 14px; max-width: 34ch; font-size: 15.5px; line-height: 1.6; opacity: 0.9; }
.how { list-style: none; margin: 0 0 16px; padding: 0; display: grid; gap: 7px; text-align: start; font-size: 14px; }
.how li { display: flex; align-items: center; gap: 10px; padding: 7px 10px; border-radius: 12px; background: rgba(246, 236, 218, 0.06); }
.how li span { font-size: 18px; width: 22px; text-align: center; }
.best { margin-bottom: 12px; font-size: 14px; opacity: 0.85; }
.best b { color: var(--gold); font-size: 18px; margin-inline-start: 6px; }
.primary {
  width: 100%; padding: 15px 18px; border-radius: 18px; font-size: 21px; font-weight: 900;
  background: linear-gradient(180deg, #f0be5c, #c08128); color: #241406;
  box-shadow: 0 10px 30px rgba(224, 140, 50, 0.35), inset 0 -4px 0 rgba(0, 0, 0, 0.2);
  transition: transform 0.12s, filter 0.12s;
}
.primary:active { transform: scale(0.97); filter: brightness(1.08); }
.ghost { margin-top: 10px; background: none; color: var(--cream); opacity: 0.7; font-size: 15px; padding: 8px 14px; }
.rank { font-family: var(--kufi); font-size: 22px; color: var(--gold); margin-bottom: 6px; }
.big b { font-size: 48px; font-weight: 900; font-variant-numeric: tabular-nums; }
.big small { display: block; font-size: 13px; opacity: 0.7; margin-top: -4px; }
.new { display: inline-block; margin: 8px 0 0; padding: 4px 12px; border-radius: 999px; background: var(--red); font-weight: 900; font-size: 14px; }
.bonus { margin-top: 6px; color: var(--gold); font-weight: 700; }
.stats { margin: 14px 0 16px; display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.stats div { padding: 8px 10px; border-radius: 12px; background: rgba(246, 236, 218, 0.06); }
.stats div:last-child { grid-column: 1 / -1; }
.stats dt { font-size: 12px; opacity: 0.7; }
.stats dd { margin: 2px 0 0; font-size: 19px; font-weight: 900; font-variant-numeric: tabular-nums; }

@media (max-width: 520px) {
  .card { padding: 22px 16px 16px; }
  .lede { font-size: 14.5px; margin-bottom: 10px; }
  .how { gap: 5px; font-size: 13px; margin-bottom: 12px; }
  .how li { padding: 6px 9px; }
  .primary { padding: 13px 16px; font-size: 20px; }
}
@media (min-width: 820px) {
  #menu, #end { align-items: center; justify-content: flex-start; padding-inline-start: 7vw; background: linear-gradient(270deg, rgba(20, 11, 6, 0.78), rgba(20, 11, 6, 0.2) 55%, transparent); }
}

/* ---------------------------------------------------------------- online */

.online-row { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-top: 10px; }
.secondary {
  padding: 12px 10px; border-radius: 15px; font-size: 16px; font-weight: 800;
  background: rgba(246, 236, 218, 0.1); border: 1px solid rgba(246, 236, 218, 0.22); transition: transform 0.12s, background 0.12s;
}
.secondary:active { transform: scale(0.97); background: rgba(246, 236, 218, 0.18); }
.secondary:disabled { opacity: 0.5; }
.secondary.wide { width: 100%; margin-top: 10px; }
.live-status { min-height: 1.2em; margin: 8px 0 0; font-size: 13.5px; line-height: 1.5; opacity: 0.85; }
#lobby { position: fixed; inset: 0; display: flex; align-items: center; justify-content: center; padding: 16px; background: rgba(20, 11, 6, 0.6); }
.roster { padding: 12px; border-radius: 14px; background: rgba(246, 236, 218, 0.07); font-size: 16px; font-weight: 700; }
.roster::before { content: ""; display: inline-block; width: 9px; height: 9px; margin-inline-end: 8px; border-radius: 50%; background: var(--gold); animation: pulse 1s ease-in-out infinite; }
.invite-note { margin-top: 8px; font-size: 13px; opacity: 0.85; }
.invite-note input { width: 100%; padding: 8px; border-radius: 8px; border: 1px solid var(--line); background: rgba(0, 0, 0, 0.3); color: var(--cream); font: 12px monospace; }
#rivalChip {
  position: absolute; top: calc(98px + var(--safe-t)); left: 50%; transform: translateX(-50%); padding: 4px 12px; border-radius: 999px;
  background: rgba(60, 64, 70, 0.6); border: 1px solid rgba(200, 210, 220, 0.3); font-size: 13px; white-space: nowrap;
}
#rivalChip b { font-variant-numeric: tabular-nums; margin-inline-start: 4px; }
#rivalChip .dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; background: #c9d1d9; margin-inline-end: 6px; }
#rivalChip.off .dot { background: #e0604a; animation: pulse 0.8s infinite; }
#countdown {
  position: absolute; top: 38%; left: 50%; transform: translate(-50%, -50%); min-width: 120px; height: 120px; padding: 0 22px;
  display: grid; place-items: center; border-radius: 60px; background: rgba(24, 14, 8, 0.6); border: 2px solid rgba(224, 172, 72, 0.6);
  font-family: var(--ui); font-weight: 900; font-size: 64px; color: var(--sand); backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px);
}
.vs { margin-top: 10px; padding: 10px; border-radius: 14px; background: rgba(246, 236, 218, 0.07); font-size: 16px; line-height: 1.7; }
.vs b { color: var(--gold); }
.mark.rival i { width: 18px; height: 18px; margin-left: -9px; margin-top: -2px; background: none; }
.mark.rival i::before { content: "🚙"; font-size: 15px; line-height: 18px; display: block; text-align: center; }

/* ---------------------------------------------------------------- loader */

#loader {
  position: fixed; inset: 0; display: grid; place-items: center; background: radial-gradient(ellipse at 50% 70%, #6b3518, #1a110b 70%);
  transition: opacity 0.6s; z-index: 10;
}
#loader.done { opacity: 0; pointer-events: none; }
.loader-inner { text-align: center; width: min(78vw, 300px); }
.loader-title { font-family: var(--kufi); font-size: 40px; color: var(--sand); margin-bottom: 18px; }
.loader-bar { height: 4px; border-radius: 4px; background: rgba(246, 236, 218, 0.15); overflow: hidden; }
.loader-fill { height: 100%; width: 100%; background: var(--gold); transform: scaleX(0); transform-origin: right; transition: transform 0.3s cubic-bezier(0.22, 1, 0.36, 1); }
.loader-sub { margin-top: 12px; font-size: 14px; opacity: 0.75; }

@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.55; } }
@keyframes pulseRing { 0%, 100% { box-shadow: 0 0 0 0 rgba(224, 172, 72, 0.7); } 50% { box-shadow: 0 0 0 7px rgba(224, 172, 72, 0); } }
@keyframes toastIn { from { opacity: 0; transform: translateY(10px) scale(0.92); } to { opacity: 1; transform: none; } }
@keyframes rise { from { opacity: 0; transform: translateY(24px); } to { opacity: 1; transform: none; } }
@media (prefers-reduced-motion: reduce) { *, *::before, *::after { animation: none !important; transition: none !important; } }
