:root {
  color-scheme: dark;
  --ink: #292a23;
  --cream: #fff6e0;
  --sun: #f2b94b;
  --leaf: #2f9e5a;
  --danger: #d9472b;
  --panel: rgba(34, 28, 20, 0.82);
  --panel-solid: #221c14;
  --edge: rgba(255, 246, 224, 0.28);
  --pad: rgba(34, 28, 20, 0.55);
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  /* No double-tap zoom on the cards (pinch stays, for readability); the canvas and buttons say none. */
  touch-action: manipulation;
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
  background: #f2b94b;
  color: var(--cream);
  -webkit-tap-highlight-color: transparent;
  -webkit-text-size-adjust: 100%;
}

#scene {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
}

#ui {
  position: fixed;
  inset: 0;
  pointer-events: none;
  user-select: none;
  -webkit-user-select: none;
  /* A long press on a control must not open the iOS callout. */
  -webkit-touch-callout: none;
}
#ui button { pointer-events: auto; touch-action: none; font-family: inherit; font-size: inherit; color: inherit; }

button:focus-visible {
  outline: 3px solid #ffd45e;
  outline-offset: 3px;
}

/* ---- chips: the dark pills the HUD is made of, readable over any sky */
.chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-height: 44px;
  padding: 0 14px;
  border: 1px solid var(--edge);
  border-radius: 999px;
  background: var(--panel);
  color: var(--cream);
  font-weight: 700;
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
}
.chip.icon { width: 48px; height: 48px; padding: 0; justify-content: center; }

/* ---- HUD: stats, order card, pause. One grid so it reflows between portrait and landscape. */
#hud {
  position: absolute;
  top: calc(var(--safe-top) + 12px);
  left: calc(var(--safe-left) + 12px);
  right: calc(var(--safe-right) + 12px);
  display: none;
  grid-template-columns: auto 1fr auto;
  grid-template-areas: "stats . pause" "order order order";
  gap: 8px 10px;
  align-items: start;
}
body[data-state="playing"] #hud,
body[data-state="paused"] #hud { display: grid; }
#pause { grid-area: pause; justify-self: end; }

.stack { flex-direction: column; align-items: flex-start; gap: 2px; padding: 8px 14px 10px; border-radius: 18px; min-width: 132px; }
#stats { grid-area: stats; }
.earn { font-size: 1.45rem; line-height: 1.1; font-variant-numeric: tabular-nums; }
.naira { color: var(--sun); margin-right: 2px; }
.sub { display: flex; gap: 12px; font-size: 0.85rem; font-variant-numeric: tabular-nums; }
.rating { color: var(--sun); }
.streak { color: #8ff0c4; font-weight: 700; font-size: 0.85rem; }
.streak[hidden] { display: none; }
.bar { height: 7px; width: 100%; margin-top: 4px; border-radius: 99px; background: rgba(255, 246, 224, 0.22); overflow: hidden; }
.bar i { display: block; height: 100%; width: 100%; transform-origin: left; background: var(--leaf); border-radius: inherit; }
.health i { background: linear-gradient(90deg, #d9472b, #f2b94b 55%, #6fcf8a); }

#order {
  grid-area: order;
  display: grid;
  grid-template-columns: 1fr auto;
  grid-template-areas: "head head" "name pay" "hint pay" "bar bar";
  gap: 1px 12px;
  min-height: 0;
  padding: 8px 14px 10px;
  border-radius: 18px;
  border-left: 6px solid #ffb020;
  align-items: center;
}
#order[hidden] { display: none; }

/* ---- two orders on offer after a delivery */
#offers {
  grid-area: order;
  display: grid;
  grid-template-columns: 1fr;
  gap: 6px;
  padding: 8px 10px 10px;
  border-radius: 18px;
  border-left: 6px solid #ffb020;
}
#offers[hidden] { display: none; }
.offers-title { font-size: 0.85rem; }
.offers-keys { margin-left: 6px; font-weight: 600; opacity: 0.9; }
.offer {
  display: grid;
  grid-template-columns: auto 1fr auto;
  grid-template-areas: "key name pay" "key meta pay";
  gap: 0 10px;
  align-items: center;
  min-height: 52px;
  padding: 6px 12px 6px 8px;
  border: 1px solid var(--edge);
  border-radius: 14px;
  background: rgba(255, 246, 224, 0.1);
  text-align: left;
}
.offer[hidden] { display: none; }
.offer:active { background: rgba(242, 185, 75, 0.45); }
.o-key { grid-area: key; display: grid; place-items: center; width: 28px; height: 28px; border-radius: 50%; background: var(--cream); color: var(--ink); font-weight: 800; }
.o-name { grid-area: name; font-weight: 700; font-size: 0.92rem; }
.o-meta { grid-area: meta; font-size: 0.78rem; opacity: 0.9; }
.o-pay { grid-area: pay; font-weight: 800; font-variant-numeric: tabular-nums; color: var(--sun); }
#offers .bar i { transform-origin: left; }
/* On a narrow screen the two cards run under the minimap, so it steps down while they show. */
@media (max-width: 699px) {
  body[data-offers="true"] #minimap { top: calc(var(--safe-top) + 290px); }
  body[data-offers="true"] #toast { top: calc(var(--safe-top) + 430px); }
}
#order[data-stage="delivery"] { border-left-color: #35d69a; }
.order-head { grid-area: head; display: flex; gap: 8px; align-items: center; font-size: 0.78rem; font-weight: 700; letter-spacing: 0.04em; }
.tag { padding: 1px 8px; border-radius: 99px; background: var(--cream); color: var(--ink); text-transform: uppercase; font-size: 0.68rem; }
.step { text-transform: uppercase; opacity: 0.85; }
.tag.fragile { background: var(--leaf); color: var(--cream); }
.tag.fragile[data-level="worn"] { background: var(--sun); color: var(--ink); }
.tag.fragile[data-level="spoiled"] { background: var(--danger); color: var(--cream); }
.tag[hidden] { display: none; }
.dist { margin-left: auto; font-variant-numeric: tabular-nums; }
.order-name { grid-area: name; font-size: 1.02rem; line-height: 1.2; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.order-hint { grid-area: hint; font-size: 0.8rem; font-weight: 500; opacity: 0.85; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.order-pay { grid-area: pay; text-align: right; font-weight: 800; font-size: 1.05rem; font-variant-numeric: tabular-nums; display: grid; }
.tip { font-size: 0.72rem; font-weight: 600; color: #8ff0c4; min-height: 1em; }
.clock-bar { grid-area: bar; }
.clock-bar i { background: var(--leaf); transition: background-color 0.3s; }
#order[data-stage="pickup"] .clock-bar i { background: rgba(255, 246, 224, 0.35); }
#order[data-urgency="warn"] .clock-bar i { background: #f2b94b; }
#order[data-urgency="late"] .clock-bar i { background: var(--danger); }
#order[data-urgency="late"] .tip { color: #ff9d8a; }

#minimap {
  position: absolute;
  right: calc(var(--safe-right) + 12px);
  top: calc(var(--safe-top) + 196px);
  width: 124px;
  height: 124px;
  border-radius: 50%;
  border: 3px solid var(--cream);
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.4);
  background: #d8c08c;
  display: none;
}
body[data-state="playing"] #minimap,
body[data-state="paused"] #minimap { display: block; }

#arrow-zone {
  position: absolute;
  top: calc(var(--safe-top) + 330px);
  bottom: calc(var(--safe-bottom) + 190px);
  left: calc(var(--safe-left) + 30px);
  right: calc(var(--safe-right) + 30px);
  display: none;
}
body[data-state="playing"] #arrow-zone { display: block; }
#target-arrow { position: absolute; left: 0; top: 0; width: 44px; height: 44px; margin: -22px 0 0 -22px; color: #ffb020; filter: drop-shadow(0 2px 4px rgba(0, 0, 0, 0.5)); }
#target-arrow[hidden] { display: none; }
#target-arrow[data-kind="drop"] { color: #35d69a; }

#toast {
  position: absolute;
  left: 50%;
  top: calc(var(--safe-top) + 330px);
  transform: translateX(-50%);
  max-width: 90vw;
  padding: 8px 18px;
  border-radius: 999px;
  background: var(--panel);
  border: 1px solid var(--edge);
  font-weight: 800;
  font-size: 1.05rem;
  text-align: center;
  opacity: 0;
  transition: opacity 0.2s;
}
#toast.show { opacity: 1; }
#toast[data-tone="good"] { color: #8ff0c4; }
#toast[data-tone="bad"] { color: #ff9d8a; }

#hit-flash { position: absolute; inset: 0; pointer-events: none; opacity: 0; box-shadow: inset 0 0 90px 30px rgba(217, 71, 43, 0.75); }
#hit-flash.flash { animation: flash 0.45s ease-out; }
@keyframes flash { from { opacity: 1; } to { opacity: 0; } }

.sr-only { position: absolute; width: 1px; height: 1px; margin: -1px; padding: 0; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0; }

#speed {
  position: absolute;
  left: 50%;
  bottom: calc(var(--safe-bottom) + 16px);
  transform: translateX(-50%);
  min-height: 36px;
  font-variant-numeric: tabular-nums;
}
#speed b { font-size: 1.15rem; min-width: 2ch; text-align: right; }
#speed span { font-size: 0.75rem; opacity: 0.9; }

/* ---- profile chip: the player's face, name and sign-in, shown between shifts */
.profile {
  position: absolute;
  top: calc(var(--safe-top) + 12px);
  left: calc(var(--safe-left) + 12px);
  display: none;
  padding: 5px 14px 5px 5px;
  max-width: min(70vw, 340px);
}
body[data-state="ready"] .profile:not([hidden]),
body[data-state="gameover"] .profile:not([hidden]) { display: inline-flex; }
.profile img { width: 36px; height: 36px; border-radius: 50%; background: var(--sun); object-fit: cover; flex: none; }
.profile-text { display: grid; line-height: 1.15; min-width: 0; }
.profile-text b { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.profile-text span { font-size: 0.72rem; font-weight: 500; opacity: 0.8; }
.pill { margin-left: 4px; min-height: 32px; padding: 0 12px; border: 0; border-radius: 999px; background: var(--sun); color: var(--ink); font-weight: 800; cursor: pointer; }
.pill[hidden] { display: none; }

/* ---- cards: title and pause */
.card {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  width: min(92vw, 440px);
  max-height: calc(100% - var(--safe-top) - var(--safe-bottom) - 24px);
  overflow: auto;
  padding: 22px 22px 18px;
  border-radius: 20px;
  background: var(--panel-solid);
  border: 1px solid var(--edge);
  box-shadow: 0 18px 50px rgba(0, 0, 0, 0.45);
  text-align: center;
  pointer-events: auto;
  display: none;
}
.card h1 { margin: 0 0 6px; font-size: clamp(2rem, 9vw, 2.8rem); letter-spacing: 0.02em; color: var(--sun); line-height: 1.05; }
.card h2 { margin: 0 0 10px; font-size: 1.2rem; }
.lead { margin: 0 0 16px; font-size: 1.05rem; line-height: 1.4; }

.primary {
  display: block;
  width: 100%;
  min-height: 52px;
  border: 0;
  border-radius: 14px;
  background: var(--sun);
  color: var(--ink);
  font-size: 1.15rem;
  font-weight: 800;
  cursor: pointer;
}
.primary:active { transform: translateY(1px); }

.best { margin: -8px 0 14px; font-weight: 700; color: var(--sun); }
.best[hidden] { display: none; }
.results { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin: 0 0 12px; }
.results div { padding: 10px; border-radius: 14px; background: rgba(255, 246, 224, 0.08); }
.results dt { font-size: 0.72rem; text-transform: uppercase; letter-spacing: 0.08em; opacity: 0.75; }
.results dd { margin: 2px 0 0; font-size: 1.3rem; font-weight: 800; font-variant-numeric: tabular-nums; }
.status { min-height: 1.3em; margin: 0 0 12px; font-weight: 600; }
.primary:disabled { opacity: 0.5; cursor: default; }
.board-wrap { margin-bottom: 12px; text-align: left; }
.board-wrap h3 { margin: 0 0 4px; font-size: 0.8rem; text-transform: uppercase; letter-spacing: 0.1em; opacity: 0.75; }
.board-wrap .status { margin: 0 0 4px; min-height: 0; font-size: 0.85rem; font-weight: 500; opacity: 0.85; }
.board { list-style: none; margin: 0; padding: 0; max-height: 24vh; overflow: auto; font-size: 0.9rem; font-variant-numeric: tabular-nums; }
.board li { display: grid; grid-template-columns: 2.2em 1fr auto auto; gap: 8px; align-items: baseline; padding: 5px 8px; border-radius: 10px; }
.board li:nth-child(odd) { background: rgba(255, 246, 224, 0.06); }
.board li.mine { background: rgba(242, 185, 75, 0.22); outline: 1px solid var(--sun); font-weight: 700; }
.board .rank { opacity: 0.7; }
.board .who { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.board .count { opacity: 0.7; font-size: 0.8rem; }
.howto { margin-top: 16px; text-align: left; font-size: 0.92rem; line-height: 1.45; }
.howto h2 { font-size: 0.8rem; text-transform: uppercase; letter-spacing: 0.1em; opacity: 0.75; margin-bottom: 6px; }
.howto ul { margin: 0; padding-left: 1.1em; }
.howto li { margin-bottom: 4px; }

body[data-state="ready"] #title,
body[data-state="gameover"] #over,
body[data-state="paused"] #paused { display: block; }
body[data-state="loading"] #speed,
body[data-state="ready"] #speed,
body[data-state="gameover"] #speed { display: none; }

/* ---- settings switches on the title and pause cards */
.settings { display: flex; justify-content: center; gap: 10px; margin-top: 12px; }
.switch {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  min-height: 44px;
  padding: 0 16px 0 10px;
  border: 1px solid var(--edge);
  border-radius: 999px;
  background: rgba(255, 246, 224, 0.08);
  font-weight: 700;
  cursor: pointer;
}
.switch::before {
  content: '';
  width: 34px;
  height: 20px;
  border-radius: 999px;
  background: rgba(255, 246, 224, 0.28) radial-gradient(circle at 10px 10px, var(--cream) 7px, transparent 8px);
  transition: background-color 0.15s;
}
.switch[aria-checked="true"]::before {
  background: var(--leaf) radial-gradient(circle at 24px 10px, var(--cream) 7px, transparent 8px);
}

/* ---- speech bubbles over the people and vehicles that speak */
#bubbles { position: absolute; inset: 0; pointer-events: none; display: none; }
body[data-state="playing"] #bubbles,
body[data-state="paused"] #bubbles { display: block; }
.bubble {
  position: absolute;
  left: 0;
  top: 0;
  max-width: min(60vw, 240px);
  padding: 6px 12px;
  border-radius: 14px;
  background: #fff6e0;
  color: var(--ink);
  font-weight: 700;
  font-size: 0.95rem;
  line-height: 1.2;
  text-align: center;
  box-shadow: 0 3px 10px rgba(0, 0, 0, 0.35);
  will-change: transform, opacity;
}
.bubble[hidden] { display: none; }

/* ---- touch controls */
#controls {
  position: absolute;
  inset: auto 0 0 0;
  display: none;
  align-items: flex-end;
  justify-content: space-between;
  padding: 0 calc(var(--safe-right) + 14px) calc(var(--safe-bottom) + 14px) calc(var(--safe-left) + 14px);
}
body[data-touch="true"][data-state="playing"] #controls { display: flex; }
.steer { display: flex; gap: 12px; }
.pad {
  display: grid;
  place-items: center;
  width: 84px;
  height: 84px;
  border: 2px solid var(--edge);
  border-radius: 24px;
  background: var(--pad);
  color: var(--cream);
  font-weight: 800;
  opacity: 0.8;
}
.pad:active { background: rgba(242, 185, 75, 0.7); color: var(--ink); opacity: 1; }
.pad.brake { width: 100px; height: 100px; border-radius: 50%; background: rgba(217, 71, 43, 0.6); }
.stop { display: flex; flex-direction: column; align-items: flex-end; gap: 12px; }
.pad.horn { width: 64px; height: 64px; border-radius: 50%; font-size: 0.85rem; }
.recover { position: absolute; left: 50%; bottom: calc(var(--safe-bottom) + 110px); transform: translateX(-50%); }
.recover[hidden] { display: none; }
/* Stuck on a keyboard: only the recover button shows, with its key. */
body[data-state="playing"][data-stuck="true"] #controls { display: flex; }
body[data-touch="false"] .steer, body[data-touch="false"] .stop { display: none; }
body[data-touch="false"] .recover::after { content: " (R)"; }
body[data-touch="true"] #speed { bottom: calc(var(--safe-bottom) + 118px); }

@media (min-width: 700px), (orientation: landscape) and (max-height: 520px) {
  #hud { grid-template-areas: "stats order pause"; grid-template-columns: auto minmax(0, 380px) auto; justify-content: space-between; }
  #minimap { top: calc(var(--safe-top) + 72px); }
  #arrow-zone { top: calc(var(--safe-top) + 150px); bottom: calc(var(--safe-bottom) + 100px); }
  #toast { top: calc(var(--safe-top) + 110px); }
  .stack { min-width: 118px; padding: 6px 12px 8px; }
  .earn { font-size: 1.25rem; }
}

/* Wide screens: the card sits on the left and the bike shows on the right. */
@media (min-aspect-ratio: 13/10) {
  .card { left: calc(var(--safe-left) + max(20px, 4vw)); transform: translateY(-50%); }
}

/* On a tall phone the card sits low so the bike shows above it. */
@media (max-aspect-ratio: 4/5) {
  .card { top: auto; bottom: calc(var(--safe-bottom) + 14px); transform: translateX(-50%); max-height: 62%; }
}

@media (max-height: 460px) {
  #minimap { width: 100px; height: 100px; }
  #arrow-zone { top: calc(var(--safe-top) + 120px); bottom: calc(var(--safe-bottom) + 90px); }
  .card h1 { font-size: 1.8rem; }
  .lead { margin-bottom: 10px; font-size: 0.95rem; }
  .primary { min-height: 46px; }
  .howto { font-size: 0.82rem; line-height: 1.35; }
  .howto li { margin-bottom: 2px; }
  .pad { width: 68px; height: 68px; border-radius: 20px; }
  .pad.brake { width: 80px; height: 80px; }
  .pad.horn { width: 52px; height: 52px; }
  .stop { flex-direction: row; align-items: flex-end; }
  .recover { bottom: calc(var(--safe-bottom) + 14px); }
  body[data-touch="true"] #speed { bottom: calc(var(--safe-bottom) + 14px); }
  .card { padding: 14px 18px 12px; }
  .howto { margin-top: 10px; }
}

#fallback {
  position: absolute;
  inset: 0;
  display: grid;
  place-content: center;
  gap: 6px;
  padding: 24px;
  text-align: center;
  background: #221c14;
  pointer-events: auto;
}
#fallback[hidden] { display: none; }

@media (prefers-reduced-motion: reduce) {
  .primary:active { transform: none; }
  #toast, .clock-bar i { transition: none; }
}

@media (min-width: 700px), (orientation: landscape) and (max-height: 520px) {
  body[data-offers="true"] #toast { top: calc(var(--safe-top) + 200px); }
}
