:root {
  --ink: #f4f6fa;
  --dim: #aab3c2;
  --panel: rgba(12, 16, 24, 0.72);
  --line: rgba(255, 255, 255, 0.12);
  --ahead: #4aa8ff;
  --behind: #ff5a5a;
  --accent: #ffd24a;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; overflow: hidden; background: #8fc3ea; }
body {
  font-family: "Segoe UI", system-ui, -apple-system, Roboto, sans-serif;
  color: var(--ink);
  user-select: none;
}
#view { position: fixed; inset: 0; width: 100%; height: 100%; display: block; }
.hidden { display: none !important; }

#hud { position: fixed; inset: 0; pointer-events: none; }
#top { position: absolute; top: 14px; left: 50%; transform: translateX(-50%); text-align: center; }
#timer {
  font: 700 44px/1 "Consolas", "SF Mono", ui-monospace, monospace;
  text-shadow: 0 2px 6px rgba(0, 0, 0, 0.55);
  letter-spacing: 1px;
}
#cps { margin-top: 4px; font-size: 15px; color: var(--dim); text-shadow: 0 1px 3px #000; }
.split {
  margin-top: 6px; display: inline-block; min-width: 120px; padding: 3px 12px; border-radius: 4px;
  font: 700 22px/1.2 "Consolas", ui-monospace, monospace; background: var(--panel);
  opacity: 0; transition: opacity 0.25s;
}
.split.show { opacity: 1; }
.split.ahead { color: var(--ahead); }
.split.behind { color: var(--behind); }

#countdown {
  position: absolute; top: 38%; width: 100%; text-align: center;
  font: 800 72px/1 "Segoe UI", system-ui, sans-serif; text-shadow: 0 3px 10px rgba(0, 0, 0, 0.6);
}
#speed {
  position: absolute; bottom: 22px; left: 24px; display: flex; align-items: flex-end; gap: 10px;
  font: 700 42px/1 "Consolas", ui-monospace, monospace; text-shadow: 0 2px 8px rgba(0, 0, 0, 0.6);
}
#speed small { font-size: 11px; margin-left: 5px; color: var(--dim); }
/* Four digits always: the unused leading ones are dim, so the box never stretches as the speed climbs. */
#speedZ { color: rgba(255, 255, 255, 0.28); }
#speedBox { white-space: nowrap; }
/* Gear: a big number beside the speed. Revs: a bar under it, the last fifth is the redline. */
#gear {
  min-width: 29px; height: 42px; display: flex; align-items: center; justify-content: center;
  font: 800 32px/1 "Segoe UI", system-ui, sans-serif; color: var(--accent); background: var(--panel); border-radius: 6px;
}
#gear.shift { color: var(--dim); }
#rpm { position: relative; width: 100%; height: 5px; margin-top: 4px; border-radius: 3px; background: rgba(255, 255, 255, 0.15); overflow: hidden; }
#rpmFill { position: absolute; left: 0; top: 0; bottom: 0; width: 0; background: #fff; }
#rpmFill.red { background: var(--behind); }
#rpmRed { position: absolute; right: 0; top: 0; bottom: 0; width: 18%; background: rgba(255, 70, 70, 0.28); }
/* Speedslide meter: the slide width against the ideal (centre tick); the band is where it pushes. */
#slide { position: relative; width: 100%; height: 4px; margin: 5px auto 0; border-radius: 3px; background: rgba(255, 255, 255, 0.15); opacity: 0; transition: opacity 0.3s ease; }
#slide.on { opacity: 1; }
#slide::after { content: ""; position: absolute; left: 50%; top: -2px; width: 2px; height: 10px; margin-left: -1px; background: rgba(255, 255, 255, 0.6); }
#slideZone { position: absolute; left: 25%; width: 50%; top: 0; bottom: 0; border-radius: 3px; background: rgba(255, 255, 255, 0.18); }
#slideMark { position: absolute; top: -3px; width: 6px; height: 12px; margin-left: -3px; border-radius: 2px; background: var(--dim); transition: background-color 0.2s ease; }
#slideMark.push { background: var(--accent); }
#debug {
  position: absolute; left: 12px; top: 12px; padding: 8px 10px; background: var(--panel);
  font: 12px/1.45 "Consolas", ui-monospace, monospace; white-space: pre; border-radius: 4px;
}

#replayTag {
  position: absolute; top: 118px; left: 50%; transform: translateX(-50%); padding: 3px 12px;
  background: var(--panel); border-radius: 4px; color: var(--accent); font-size: 14px; white-space: nowrap;
}
#inputs {
  position: absolute; bottom: 86px; left: 50%; transform: translateX(-50%); display: flex; gap: 8px;
  align-items: center; font: 700 11px/1 "Consolas", ui-monospace, monospace;
}
#inputs span { padding: 4px 6px; border-radius: 3px; background: var(--panel); color: var(--dim); }
#inputs span.on { background: var(--ink); color: #111; }
#inputs .steer { position: relative; width: 160px; height: 10px; background: var(--panel); border-radius: 3px; overflow: hidden; }
#inSteer { position: absolute; top: 0; bottom: 0; left: 50%; width: 0; background: var(--accent); }
#help .link { text-decoration: underline; cursor: pointer; }
#board {
  position: fixed; right: 12px; top: 12px; width: 250px; max-height: calc(100% - 80px); overflow: auto;
  background: var(--panel); border: 1px solid var(--line); border-radius: 6px; padding: 10px 12px;
  font-size: 13px;
}
#board header { display: flex; justify-content: space-between; align-items: baseline; margin-bottom: 6px; }
#board .title { font-weight: 700; text-transform: uppercase; letter-spacing: 1px; font-size: 12px; color: var(--accent); }
#mapDate { color: var(--dim); font-family: "Consolas", ui-monospace, monospace; }
#boardList, #lbList { list-style: none; margin: 0; padding: 0; }
#boardList li, #boardMe .row, #lbList li, #lbMe .row {
  display: grid; grid-template-columns: 28px 1fr auto; gap: 6px; padding: 3px 0;
  border-bottom: 1px solid var(--line);
}
#boardList li.me, #lbList li.me { color: var(--accent); }
#boardList li:not(.empty):not(.noreplay), #lbList li:not(.empty):not(.noreplay) { cursor: pointer; }
#boardList li:not(.empty):not(.noreplay):hover, #lbList li:not(.empty):not(.noreplay):hover { background: rgba(255, 255, 255, 0.06); }
#boardList .rk, #boardMe .rk, #lbList .rk, #lbMe .rk { color: var(--dim); }
#boardList .nm, #lbList .nm { display: flex; align-items: center; gap: 5px; min-width: 0; }
#boardList .nm .tx, #lbList .nm .tx { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }
/* Replay kept on the server: a pixel-art outline of a video-play badge. */
.play { display: inline-block; flex: none; width: 14px; height: 10px; vertical-align: middle; image-rendering: pixelated;
  background: url("data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%2014%2010'%20shape-rendering='crispEdges'%3E%3Cpath%20fill='%23c8ccd2'%20d='M2%200h1v1h-1zM3%200h1v1h-1zM4%200h1v1h-1zM5%200h1v1h-1zM6%200h1v1h-1zM7%200h1v1h-1zM8%200h1v1h-1zM9%200h1v1h-1zM10%200h1v1h-1zM11%200h1v1h-1zM1%201h1v1h-1zM12%201h1v1h-1zM0%202h1v1h-1zM5%202h1v1h-1zM6%202h1v1h-1zM13%202h1v1h-1zM0%203h1v1h-1zM5%203h1v1h-1zM7%203h1v1h-1zM13%203h1v1h-1zM0%204h1v1h-1zM5%204h1v1h-1zM8%204h1v1h-1zM13%204h1v1h-1zM0%205h1v1h-1zM5%205h1v1h-1zM8%205h1v1h-1zM13%205h1v1h-1zM0%206h1v1h-1zM5%206h1v1h-1zM7%206h1v1h-1zM13%206h1v1h-1zM0%207h1v1h-1zM5%207h1v1h-1zM6%207h1v1h-1zM13%207h1v1h-1zM1%208h1v1h-1zM12%208h1v1h-1zM2%209h1v1h-1zM3%209h1v1h-1zM4%209h1v1h-1zM5%209h1v1h-1zM6%209h1v1h-1zM7%209h1v1h-1zM8%209h1v1h-1zM9%209h1v1h-1zM10%209h1v1h-1zM11%209h1v1h-1z'/%3E%3C/svg%3E") center / 100% 100% no-repeat; }
#boardList .tm, #boardMe .tm, #lbList .tm, #lbMe .tm { font-family: "Consolas", ui-monospace, monospace; }
#boardList .empty, #lbList .empty { display: block; color: var(--dim); border: 0; }
#boardMe, #lbMe { margin-top: 6px; color: var(--accent); }
#driver { display: flex; justify-content: space-between; align-items: center; margin-top: 10px; color: var(--dim); }
#driver button, .panel button {
  font: inherit; color: var(--ink); background: rgba(255, 255, 255, 0.08); border: 1px solid var(--line);
  border-radius: 4px; padding: 3px 10px; cursor: pointer;
}
#driver button:hover, .panel button:hover { background: rgba(255, 255, 255, 0.16); }

#help {
  position: fixed; left: 12px; bottom: 12px; max-width: calc(100% - 300px); font-size: 12px; color: var(--ink);
  background: var(--panel); padding: 6px 10px; border-radius: 4px; opacity: 0.85;
}

.overlay { position: fixed; inset: 0; display: grid; place-items: center; pointer-events: none; }
.overlay .panel {
  pointer-events: auto; min-width: 300px; text-align: center; background: var(--panel);
  border: 1px solid var(--line); border-radius: 8px; padding: 18px 26px;
}
.overlay .label { text-transform: uppercase; letter-spacing: 2px; color: var(--accent); font-size: 13px; font-weight: 700; }
.overlay .big { font: 700 52px/1.15 "Consolas", ui-monospace, monospace; margin: 6px 0; }
.overlay .hint { margin-top: 12px; color: var(--dim); font-size: 13px; }
#finPb.ahead { color: var(--ahead); }
#finPb.behind { color: var(--behind); }
#finRank { margin-top: 6px; font-size: 18px; }
#nameDlg { background: rgba(0, 0, 0, 0.35); pointer-events: auto; }
#nameInput {
  width: 100%; margin-top: 10px; padding: 8px 10px; font: 18px "Segoe UI", system-ui, sans-serif;
  color: var(--ink); background: rgba(0, 0, 0, 0.35); border: 1px solid var(--line); border-radius: 4px;
}
.panel .row { display: flex; gap: 8px; justify-content: center; margin-top: 12px; }
.err { color: var(--behind); min-height: 1.2em; font-size: 13px; margin-top: 4px; }
.fine { color: var(--dim); font-size: 11px; max-width: 280px; margin: 12px auto 0; }

@media (max-width: 700px) {
  #replayTag {
  position: absolute; top: 118px; left: 50%; transform: translateX(-50%); padding: 3px 12px;
  background: var(--panel); border-radius: 4px; color: var(--accent); font-size: 14px; white-space: nowrap;
}
#inputs {
  position: absolute; bottom: 86px; left: 50%; transform: translateX(-50%); display: flex; gap: 8px;
  align-items: center; font: 700 11px/1 "Consolas", ui-monospace, monospace;
}
#inputs span { padding: 4px 6px; border-radius: 3px; background: var(--panel); color: var(--dim); }
#inputs span.on { background: var(--ink); color: #111; }
#inputs .steer { position: relative; width: 160px; height: 10px; background: var(--panel); border-radius: 3px; overflow: hidden; }
#inSteer { position: absolute; top: 0; bottom: 0; left: 50%; width: 0; background: var(--accent); }
#help .link { text-decoration: underline; cursor: pointer; }
#board { width: 190px; font-size: 12px; }
  #help { display: none; }
  #timer { font-size: 32px; }
  #speed { font-size: 40px; }
}

/* Pause menu: a column of full-width buttons, each showing its key. */
#pause .panel, #finish .panel { pointer-events: auto; min-width: 300px; }
#pause .menu, #finish .menu { display: flex; flex-direction: column; gap: 8px; }
#pause .big, #finish .big { font-size: 34px; }
#finish .menu > div:not(.label):not(.big) { text-align: center; }
#pause button, #finish button { display: flex; justify-content: space-between; align-items: center; padding: 10px 14px; font-size: 15px; text-align: left; }
#pause button:disabled, #finish button:disabled { opacity: 0.4; cursor: default; }
#pause kbd, #finish kbd { font: 12px "Consolas", ui-monospace, monospace; color: var(--dim); border: 1px solid rgba(255, 255, 255, 0.2); border-radius: 4px; padding: 1px 6px; }

/* World / region tabs on the side board and the full leaderboard. */
.tabs { display: flex; gap: 4px; margin: 0 0 6px; flex-wrap: wrap; }
.tabs button { flex: 1; padding: 3px 6px; font-size: 11px; border-radius: 4px; border: 1px solid rgba(255, 255, 255, 0.15);
  background: transparent; color: var(--dim); cursor: pointer; white-space: nowrap; }
.tabs button.on { background: rgba(255, 255, 255, 0.14); color: inherit; }
#lb .panel { pointer-events: auto; width: min(440px, calc(100vw - 32px)); max-height: calc(100vh - 48px); overflow-y: auto; text-align: left; }
#lb .label { text-align: center; }
#lb .tabs { margin: 10px 0; }
#lb .tabs button { font-size: 12px; padding: 5px 8px; }
#lb .tabs button:last-child { flex-basis: 100%; } /* History: its own full row, not a stray label under the regions */
#lbList li { font-size: 14px; }

/* Paint dialog: panel list and colour presets on the left, the car in view. */
#paint { place-items: center start; padding-left: 20px; }
#paint .panel { pointer-events: auto; width: 270px; text-align: left; }
#paint .label { text-align: center; }
#paintParts { display: grid; grid-template-columns: 1fr 1fr; gap: 4px; margin: 10px 0 6px; }
#paintParts button { display: flex; align-items: center; gap: 6px; padding: 4px 6px; font-size: 11px; text-align: left; white-space: nowrap; overflow: hidden; }
#paintParts button.on { background: rgba(255, 255, 255, 0.22); outline: 1px solid var(--accent); }
#paintParts .dot, #paintSwatches button { border: 1px solid rgba(255, 255, 255, 0.35); }
#paintParts .dot { flex: none; width: 11px; height: 11px; border-radius: 3px; }
#paintSwatches { display: grid; grid-template-columns: repeat(6, 1fr); gap: 6px; margin: 4px 0 12px; }
#paintSwatches button { height: 28px; padding: 0; border-radius: 5px; }
#paintSwatches button.on { outline: 2px solid var(--accent); outline-offset: 1px; }
#paintPicked { margin: 2px 0; text-align: left; }
#paintBtn { margin-top: 8px; width: 100%; font-size: 12px; padding: 4px 6px; }
#paint .row button kbd { margin-left: 8px; }

/* History: a minimap, the day's top times and a replay link per past day. */
#lbHistory.hidden, #lbList.hidden { display: none; }
#lbHistory .day { display: flex; gap: 10px; padding: 8px 0; border-top: 1px solid rgba(255, 255, 255, 0.1); }
#lbHistory canvas { width: 150px; height: 110px; flex: none; background: #1a1e24; border-radius: 6px; }
#lbHistory .info { flex: 1; min-width: 0; font-size: 12px; }
#lbHistory .info b { color: var(--accent); }
#lbHistory ol { list-style: none; margin: 4px 0 0; padding: 0; }
#lbHistory li { display: grid; grid-template-columns: 1fr auto; gap: 6px; padding: 1px 0; }
#lbHistory li .nm { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#lbHistory li .tm, #lbHistory .archive .tm { font-family: "Consolas", ui-monospace, monospace; }
#lbHistory .play { margin-left: 5px; }
#lbHistory .archive { margin-top: 8px; padding-top: 8px; border-top: 1px solid rgba(255, 255, 255, 0.1); font-size: 12px; }
#lbHistory .archive > div:not(.label) { display: grid; grid-template-columns: 82px 1fr auto; gap: 6px; padding: 1px 0; color: var(--dim); }
#lbHistory .empty { color: var(--dim); padding: 8px 0; }

/* Start menu: the logo on a white band, today's map beside the top 5 and the menu. */
#start { background: rgba(6, 9, 14, 0.45); pointer-events: auto; }
#start .panel.start { width: min(760px, calc(100vw - 32px)); max-height: calc(100vh - 32px); overflow-y: auto; padding: 0; text-align: left; }
#start .logo { background: #fff; border-radius: 8px 8px 0 0; padding: 14px 24px 10px; text-align: center; }
#start .logo img { width: min(420px, 100%); height: auto; display: inline-block; }
#start .startBody { display: flex; gap: 18px; padding: 16px 20px 20px; }
#start .mapCol { flex: 1.2; min-width: 0; }
#start .sideCol { flex: 1; min-width: 0; display: flex; flex-direction: column; }
#startMap { display: block; width: 100%; aspect-ratio: 640 / 460; margin-top: 8px; background: #1a1e24; border-radius: 6px; }
#startStats { margin-top: 8px; text-align: left; }
#startTop { list-style: none; margin: 8px 0 0; padding: 0; min-height: 6.5em; }
#startTop li { display: grid; grid-template-columns: 22px 1fr auto; gap: 6px; padding: 3px 0; font-size: 14px; border-bottom: 1px solid var(--line); cursor: pointer; }
#startTop li.noreplay { cursor: default; }
#startTop li.me { color: var(--accent); }
#startTop .rk { color: var(--dim); }
#startTop .nm { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#startTop .tm { font-family: "Consolas", ui-monospace, monospace; }
#startTop .empty { display: block; color: var(--dim); border: 0; cursor: default; }
#startMe { margin-top: 6px; color: var(--accent); font-size: 13px; }
#startMe .row { display: grid; grid-template-columns: 22px 1fr auto; gap: 6px; }
#start .menu { display: flex; flex-direction: column; gap: 8px; margin-top: auto; padding-top: 14px; }
#start button { display: flex; justify-content: space-between; align-items: center; padding: 10px 14px; font-size: 15px; text-align: left; }
#start button.go { background: #e30613; border-color: #e30613; color: #fff; font-weight: 700; font-size: 17px; }
#start button.go:hover { background: #ff1f2c; }
#start kbd { font: 12px "Consolas", ui-monospace, monospace; color: var(--dim); border: 1px solid rgba(255, 255, 255, 0.2); border-radius: 4px; padding: 1px 6px; }
#start button.go kbd { color: #fff; border-color: rgba(255, 255, 255, 0.5); }
@media (max-width: 640px) {
  #start .startBody { flex-direction: column; }
}
/* The start menu stands alone: no HUD, side board or key help behind it. */
body.atStart #hud, body.atStart #board, body.atStart #help { display: none; }

/* Broadcast banner, maintenance bar, spectator strip, map ratings. */
#banner {
  position: fixed; top: 12px; left: 50%; transform: translateX(-50%); z-index: 20; max-width: min(640px, 92vw);
  display: flex; align-items: center; gap: 10px; padding: 7px 8px 7px 14px; border-radius: 6px;
  background: rgba(12, 16, 24, 0.86); border: 1px solid var(--accent); color: var(--ink); font-size: 14px;
}
#banner button { pointer-events: auto; background: none; border: 0; color: var(--dim); font-size: 20px; line-height: 1; cursor: pointer; padding: 0 4px; }
#maint {
  position: fixed; top: 0; left: 0; right: 0; z-index: 19; padding: 5px 12px; text-align: center; font-size: 13px;
  background: #7a4b00; color: #fff3d6;
}
body.atStart #banner { top: 62px; }
#spec {
  position: fixed; bottom: 14px; left: 50%; transform: translateX(-50%); z-index: 18; padding: 6px 14px; border-radius: 6px;
  background: var(--panel); color: var(--accent); font: 600 14px/1.4 "Consolas", ui-monospace, monospace; white-space: nowrap;
}
#wrongWay { position: fixed; top: 22%; left: 50%; transform: translateX(-50%); z-index: 12; padding: 6px 22px; border-radius: 6px; background: rgba(150, 20, 28, 0.82); color: #fff; font: 800 28px/1.2 system-ui, sans-serif; letter-spacing: 3px; text-transform: uppercase; pointer-events: none; }
#specExit { position: fixed; top: 14px; left: 50%; transform: translateX(-50%); z-index: 19; padding: 6px 12px; border-radius: 6px; border: 0; background: var(--panel); color: var(--accent); font: 600 14px/1.4 "Consolas", ui-monospace, monospace; cursor: pointer; }
#specExit:hover { background: #26324a; }
#specMap { position: fixed; right: 14px; bottom: 14px; z-index: 19; width: 260px; height: 260px; border-radius: 8px; background: rgba(12, 17, 26, 0.82); cursor: crosshair; }
#bug { z-index: 40; background: rgba(0, 0, 0, 0.5); }
#bug:not(.hidden) { pointer-events: auto; }
#bug .panel { pointer-events: auto; background: #151b27; width: min(460px, 92vw); }
#bugText { width: 100%; box-sizing: border-box; margin: 10px 0 6px; padding: 8px; border-radius: 6px; border: 1px solid rgba(255, 255, 255, 0.2); background: rgba(0, 0, 0, 0.35); color: var(--ink, #fff); font: 14px/1.4 system-ui, sans-serif; resize: vertical; }
#bug .check { display: flex; gap: 8px; align-items: center; font-size: 13px; color: var(--dim); }
#bug .row { display: flex; gap: 8px; justify-content: flex-end; margin-top: 10px; }
#bugBtn.quiet { opacity: 0.7; }
.stars { display: inline-flex; gap: 2px; align-items: center; }
.stars button { background: none; border: 0; padding: 0 1px; font-size: 20px; line-height: 1; color: rgba(255, 255, 255, 0.28); cursor: pointer; }
.stars button.on { color: var(--accent); }
.stars button:hover { color: #fff; }
.rate { display: flex; align-items: center; gap: 8px; margin: 2px 0 4px; }
.rate small { color: var(--dim); }
#finRate { margin-top: 8px; display: flex; align-items: center; gap: 8px; justify-content: center; }
#finRate .hint { margin: 0; }

#consent {
  position: fixed; left: 50%; bottom: 14px; transform: translateX(-50%); z-index: 40; width: min(760px, 94vw);
  display: flex; align-items: center; gap: 16px; padding: 12px 16px; border-radius: 8px;
  background: rgba(12, 16, 24, 0.94); border: 1px solid var(--accent); color: var(--ink); font-size: 13px; line-height: 1.45;
}
#consent.hidden { display: none; }
#consent .cBtns { display: flex; flex-direction: column; gap: 8px; flex: none; }
#consent button { pointer-events: auto; cursor: pointer; padding: 7px 18px; border-radius: 5px; border: 1px solid var(--line); background: rgba(255, 255, 255, 0.08); color: var(--ink); font: inherit; font-weight: 600; }
#consent button.go { background: var(--accent); color: #1a1a1a; border-color: var(--accent); }
@media (max-width: 560px) { #consent { flex-direction: column; align-items: stretch; } #consent .cBtns { flex-direction: row; } #consent .cBtns button { flex: 1; } }
