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

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: #7fb0de;
  color: #fff;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  overscroll-behavior: none;
}
[hidden] { display: none !important; }

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

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

.btn {
  position: absolute;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  border: 2px solid rgba(255, 255, 255, 0.6);
  background: rgba(18, 22, 30, 0.45);
  color: #fff;
  font: 800 13px/1 system-ui, sans-serif;
  letter-spacing: 0.06em;
  touch-action: none;
  padding: 0;
  outline: none;
  transition: transform 0.06s, background 0.06s;
}
.btn.pressed { background: rgba(255, 255, 255, 0.38); transform: scale(0.93); }

#btn-brake {
  width: 96px; height: 96px;
  right: calc(24px + env(safe-area-inset-right, 0px));
  bottom: calc(22px + env(safe-area-inset-bottom, 0px));
  background: rgba(190, 30, 30, 0.55);
  font-size: 15px;
}
#btn-horn {
  width: 56px; height: 56px;
  right: calc(132px + env(safe-area-inset-right, 0px));
  bottom: calc(30px + env(safe-area-inset-bottom, 0px));
  font-size: 10px;
}

/* top bar */
#topbar {
  position: absolute;
  top: calc(10px + env(safe-area-inset-top, 0px));
  right: calc(10px + env(safe-area-inset-right, 0px));
  display: flex;
  align-items: center;
  gap: 8px;
}
#topbar .btn { position: static; width: 42px; height: 42px; }
#btn-sound .mute { display: none; }
#btn-sound.muted .mute { display: inline; }
#btn-sound.muted .wave { display: none; }
#score { display: flex; gap: 6px; margin-right: 4px; }
.pill {
  display: flex; align-items: baseline; gap: 4px;
  padding: 6px 10px; border-radius: 18px;
  background: rgba(0, 0, 0, 0.5);
  font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.05em;
}
.pill b { font-size: 17px; }
.pill.minor b { color: #ffc35a; }
.pill.serious b { color: #ff5a5a; }

#faultlog {
  position: absolute;
  top: calc(62px + env(safe-area-inset-top, 0px));
  right: calc(10px + env(safe-area-inset-right, 0px));
  width: min(300px, 40vw);
  display: flex; flex-direction: column; gap: 6px;
}
.fault {
  padding: 7px 10px; border-radius: 10px;
  background: rgba(20, 20, 24, 0.82);
  border-left: 5px solid #ffc35a;
  font-size: 12px; line-height: 1.3;
  transition: opacity 0.6s, transform 0.6s;
}
.fault b { color: #ffc35a; margin-right: 6px; font-size: 11px; letter-spacing: 0.06em; }
.fault span { display: block; opacity: 0.9; }
.fault.serious { border-left-color: #ff4242; }
.fault.serious b { color: #ff6b6b; }
.fault.fade { opacity: 0; transform: translateX(20px); }

/* instruction banner */
#instr {
  position: absolute;
  top: calc(10px + env(safe-area-inset-top, 0px));
  left: 50%;
  transform: translateX(-50%);
  display: flex; align-items: center; gap: 10px;
  max-width: min(46vw, 440px);
  padding: 8px 14px 8px 10px;
  border-radius: 14px;
  background: #fff;
  color: #14213d;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.3);
}
#instr.alert { background: #e3222b; color: #fff; }
#instr-arrow {
  flex: none; width: 42px; height: 42px; border-radius: 10px;
  background: #1d3f8f; color: #fff;
  display: flex; align-items: center; justify-content: center;
}
#instr.alert #instr-arrow { background: #fff; color: #e3222b; }
#instr-text { font-weight: 700; font-size: 14px; line-height: 1.25; }
#instr-dist { font-size: 12px; font-weight: 700; color: #5470a8; margin-top: 2px; }
#instr-dist:empty { display: none; }

#subtitle {
  position: absolute;
  top: calc(78px + env(safe-area-inset-top, 0px));
  left: 50%;
  transform: translateX(-50%);
  max-width: min(50vw, 480px);
  padding: 6px 12px;
  border-radius: 10px;
  background: rgba(0, 0, 0, 0.55);
  font-size: 13px; font-style: italic; text-align: center;
  opacity: 0; transition: opacity 0.4s;
}
#subtitle.show { opacity: 1; }

#minimap {
  position: absolute;
  top: calc(10px + env(safe-area-inset-top, 0px));
  left: calc(10px + env(safe-area-inset-left, 0px));
  width: 120px; height: 120px;
  border-radius: 50%;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.35);
}

/* dashboard */
#dash {
  position: absolute;
  left: 50%;
  bottom: calc(12px + env(safe-area-inset-bottom, 0px));
  transform: translateX(-50%);
  display: flex; align-items: center; gap: 12px;
}
#dash .btn.ind { position: static; width: 58px; height: 58px; color: rgba(255, 255, 255, 0.85); }
#dash .btn.ind.armed { border-color: #3ddc6b; }
#dash .btn.ind.on { background: #2fbf5a; color: #fff; }
#speedo {
  min-width: 74px; text-align: center;
  font: 800 36px/1 system-ui, sans-serif;
  text-shadow: 0 2px 6px rgba(0, 0, 0, 0.55);
  font-variant-numeric: tabular-nums;
}
#speedo small { display: block; font-size: 11px; letter-spacing: 0.14em; opacity: 0.85; }
#limit {
  width: 46px; height: 46px; border-radius: 50%;
  background: #fff; border: 6px solid #d4111a;
  display: flex; align-items: center; justify-content: center;
  color: #111; font: 800 17px/1 Arial, sans-serif;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.35);
}

#joy-base {
  position: absolute; left: 0; top: 0;
  width: 124px; height: 124px; margin: -62px 0 0 -62px;
  border-radius: 50%;
  border: 2px solid rgba(255, 255, 255, 0.55);
  background: rgba(18, 22, 30, 0.25);
}
#joy-base.idle { opacity: 0.45; }
#joy-knob {
  position: absolute; left: 50%; top: 50%;
  width: 56px; height: 56px; margin: -28px 0 0 -28px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.8);
}

#stopflash {
  position: absolute; inset: 0;
  display: flex; align-items: center; justify-content: center;
  font: 900 clamp(60px, 16vw, 140px)/1 system-ui, sans-serif;
  color: #fff; letter-spacing: 0.08em;
  background: rgba(220, 20, 30, 0.35);
  text-shadow: 0 6px 30px rgba(120, 0, 0, 0.6);
  animation: flash 0.5s ease-in-out infinite alternate;
}
@keyframes flash { from { background: rgba(220, 20, 30, 0.4); } to { background: rgba(220, 20, 30, 0.1); } }

/* screens */
.screen {
  position: fixed; inset: 0; z-index: 10;
  display: flex; align-items: center; justify-content: center;
  padding: max(14px, env(safe-area-inset-top, 0px)) max(16px, env(safe-area-inset-right, 0px)) 14px max(16px, env(safe-area-inset-left, 0px));
  background: rgba(10, 18, 36, 0.62);
  overflow-y: auto;
  touch-action: pan-y;
}
.screen-inner { width: min(980px, 100%); margin: auto; }
#menu header { display: flex; gap: 16px; align-items: center; margin-bottom: 14px; }
.lplate {
  flex: none; width: 64px; height: 64px; border-radius: 8px;
  background: #fff; color: #d4111a;
  display: flex; align-items: center; justify-content: center;
  font: 900 50px/1 Arial, sans-serif;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.35);
}
#menu h1 { margin: 0; font-size: clamp(26px, 4.5vw, 40px); letter-spacing: 0.04em; }
#menu .sub { margin: 4px 0 0; font-size: 13px; line-height: 1.45; opacity: 0.92; max-width: 70ch; }
#menu .sub b { color: #ffc35a; }
#missions { display: grid; grid-template-columns: repeat(auto-fill, minmax(min(250px, 100%), 1fr)); gap: 10px; }
.card {
  display: flex; flex-direction: column; gap: 4px; align-items: flex-start; text-align: left;
  padding: 12px 14px; border-radius: 14px; border: none;
  background: rgba(255, 255, 255, 0.95); color: #14213d;
  font: inherit; cursor: pointer;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.25);
}
.card:active { transform: scale(0.98); }
.card strong { font-size: 16px; }
.card > span { font-size: 12.5px; line-height: 1.35; color: #42506e; }
.card .best { margin-top: 4px; font-size: 11px; font-weight: 800; letter-spacing: 0.05em; padding: 2px 8px; border-radius: 10px; }
.card .best.pass { background: #d9f7e2; color: #116b33; }
.card .best.fail { background: #fde0e0; color: #9c1c1c; }
.help { font-size: 12px; opacity: 0.75; margin: 12px 0 0; line-height: 1.5; }

.dialog {
  width: min(360px, 100%);
  margin: auto;
  padding: 20px;
  border-radius: 18px;
  background: #fff; color: #14213d;
  display: flex; flex-direction: column; gap: 10px;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.4);
}
.dialog.wide { width: min(620px, 100%); }
.dialog h2 { margin: 0; font-size: 20px; }
.dialog button {
  font: 700 15px/1 system-ui, sans-serif;
  padding: 13px 18px; border-radius: 12px;
  border: 2px solid #1d3f8f; background: #fff; color: #1d3f8f;
}
.dialog button.primary { background: #1d3f8f; color: #fff; }
.dialog .row { display: flex; gap: 10px; }
.dialog .row button { flex: 1; }
#res-badge {
  align-self: flex-start;
  font: 900 30px/1 system-ui, sans-serif; letter-spacing: 0.1em;
  padding: 8px 16px; border-radius: 10px;
}
#res-badge.pass { background: #19a34a; color: #fff; }
#res-badge.fail { background: #d4111a; color: #fff; }
#res-sub { margin: 0; color: #42506e; font-size: 14px; }
#res-stats { font-size: 13px; color: #14213d; display: flex; flex-direction: column; gap: 3px; }
#res-stats:empty { display: none; }
#res-faults {
  list-style: none; margin: 0; padding: 0;
  max-height: 34vh; overflow-y: auto;
  display: flex; flex-direction: column; gap: 6px;
  touch-action: pan-y;
}
#res-faults li { display: flex; gap: 8px; align-items: flex-start; font-size: 13px; line-height: 1.35; }
#res-faults li b {
  flex: none; width: 22px; height: 22px; border-radius: 6px;
  display: flex; align-items: center; justify-content: center;
  font-size: 12px; color: #fff; background: #e0a020;
}
#res-faults li.serious b { background: #d4111a; }
#res-faults li em { font-style: normal; font-weight: 700; margin-right: 4px; }
#res-faults li.none { color: #116b33; font-weight: 600; }

/* copyright footer + licences screen */
.legal {
  display: flex; flex-wrap: wrap; align-items: center; gap: 6px 14px;
  margin-top: 14px; padding-top: 10px;
  border-top: 1px solid rgba(255, 255, 255, 0.2);
  font-size: 12px; opacity: 0.85;
}
.legal button {
  padding: 0; border: none; background: none; color: #ffc35a;
  font: 700 12px/1.4 system-ui, sans-serif; text-decoration: underline; cursor: pointer;
}
.legal-doc { gap: 4px; user-select: text; -webkit-user-select: text; }
.legal-doc h3 { margin: 10px 0 2px; font-size: 14px; color: #1d3f8f; }
.legal-doc p, .legal-doc li { margin: 4px 0; font-size: 13px; line-height: 1.5; color: #2a3550; }
.legal-doc ul { margin: 4px 0; padding-left: 20px; }
.legal-doc summary { font-size: 13px; font-weight: 700; color: #1d3f8f; cursor: pointer; margin-top: 4px; }
.legal-doc pre {
  margin: 6px 0 0; padding: 10px; border-radius: 8px;
  background: #f1f3f8; color: #2a3550;
  font-size: 10.5px; line-height: 1.45; white-space: pre-wrap;
}
.legal-doc .row { margin-top: 10px; }

/* keyboard help on computers, thumb help on touch screens */
.help kbd {
  display: inline-block; min-width: 1.6em; padding: 1px 5px; border-radius: 5px;
  background: rgba(255, 255, 255, 0.16); border: 1px solid rgba(255, 255, 255, 0.35);
  font: 700 11px/1.4 system-ui, sans-serif; text-align: center;
}
.help.keys { display: none; }
@media (hover: hover) and (pointer: fine) {
  .help.keys { display: block; }
  .help.touch { display: none; }
  #joy-base { display: none; } /* steering is on the keyboard */
}

/* portrait phones: the game needs landscape */
#rotate { display: none; }
@media (orientation: portrait) and (pointer: coarse) {
  #rotate {
    position: fixed; inset: 0; z-index: 50;
    display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 14px;
    background: #1d3f8f; color: #fff; text-align: center; padding: 24px;
    font: 700 18px/1.4 system-ui, sans-serif;
  }
  #rotate .phone { transform-origin: 32px 30px; animation: tilt 2.2s ease-in-out infinite; }
}
@keyframes tilt { 0%, 30% { transform: rotate(0deg); } 60%, 100% { transform: rotate(-90deg); } }

@media (max-height: 420px) {
  #btn-brake { width: 84px; height: 84px; }
  #dash .btn.ind { width: 52px; height: 52px; }
  .card { padding: 9px 12px; }
}
