* { box-sizing: border-box; }
:root {
  color-scheme: dark;
  --background: #101010;
  --panel: #202020;
  --text: #d8dbcf;
  --muted: #a2a59b;
  --orange: #ff601e;
  --yellow: #e5dc60;
}
html, body { width: 100%; height: 100%; margin: 0; }
body {
  display: grid;
  grid-template-rows: 48px minmax(0, 1fr) 24px;
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
  background: var(--background);
  color: var(--text);
  font: 14px/1.4 "RoadRashFutura", "Helvetica Neue", Helvetica, sans-serif;
}
body.playing { grid-template-rows: 48px minmax(0, 1fr); }
body.playing footer { display: none; }
h1, h2 {
  font-family: "RoadRashBadloc", "Arial Narrow", sans-serif;
  font-weight: 400;
  letter-spacing: 0;
}
button, .button, summary {
  font-family: "RoadRashFuturaBold", "Arial Narrow", sans-serif;
  font-weight: 400;
  letter-spacing: 0;
}
h1, h2 { color: var(--orange); text-shadow: 2px 2px #000; }
h1 { margin: 0; font-size: 24px; white-space: nowrap; }
h2 { font-size: 32px; margin: 0 0 14px; }
.toolbar {
  display: flex;
  align-items: center;
  gap: 22px;
  padding: 0 16px;
  background: #1a1a1a;
  border-bottom: 2px solid #353535;
}
#state { font-size: 12px; color: var(--muted); }
#state::before {
  content: "";
  display: inline-block;
  margin-right: 8px;
  border-top: 5px solid transparent;
  border-bottom: 5px solid transparent;
  border-left: 6px solid var(--yellow);
  vertical-align: -1px;
}
nav { display: flex; align-items: center; gap: 8px; margin-left: auto; }
button, .button, summary, select {
  border: 1px solid #626262;
  border-right-color: #303030;
  border-bottom-color: #303030;
  border-radius: 0;
  background: #292929;
  color: var(--text);
  font-size: 16px;
  line-height: 1.2;
  padding: 9px 16px;
  cursor: pointer;
}
button:hover, .button:hover, summary:hover { color: var(--yellow); background: #343434; }
.primary { color: var(--yellow); background: #33332a; border-top-color: #8a8972; }
button:disabled, .button.disabled { opacity: .4; pointer-events: none; }
:focus-visible { outline: 2px solid var(--yellow); outline-offset: 2px; }
.toolbar button, summary { padding: 5px 12px; font-size: 15px; }
main { min-width: 0; min-height: 0; }
.viewport {
  position: relative;
  display: grid;
  place-items: center;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: #000;
  container-type: size;
}
#canvas {
  display: block;
  /* Fit the entire 4:3 frame to the viewport, with no fixed pixel-size cap.
     Keep the element itself at 4:3 so SDL mouse coordinates stay correct. */
  /* SDL writes inline pixel sizes when its Window Size option changes.
     The browser controls presentation; SDL's size controls the render buffer. */
  width: min(100cqw, calc(100cqh * 4 / 3)) !important;
  height: min(100cqh, calc(100cqw * 3 / 4)) !important;
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
  touch-action: none;
}
#canvas:focus-visible { outline: none; }
.viewport:fullscreen { width: 100vw; height: 100dvh; }
#setup {
  width: min(480px, calc(100% - 32px));
  max-height: calc(100% - 32px);
  overflow-y: auto;
  padding: 28px;
  border: 1px solid #666;
  border-right-color: #303030;
  border-bottom-color: #303030;
  background: var(--panel);
  box-shadow: 5px 5px #080808;
}
#setup p { margin: 0 0 20px; color: var(--muted); }
.actions { display: flex; flex-wrap: wrap; gap: 10px; margin: 16px 0; }
input[type=file] { position: absolute; width: 1px; height: 1px; opacity: 0; overflow: hidden; }
.button:has(+ input:focus-visible) { outline: 2px solid var(--yellow); outline-offset: 2px; }
#ready { margin-top: 24px; }
.start-game {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  min-height: 56px;
  padding: 14px 18px;
  background: var(--yellow);
  color: #191a14;
  border-color: #fff8aa #8e8941 #8e8941 #fff8aa;
  box-shadow: 3px 3px #0d0d0b;
  font-size: 22px;
  text-align: left;
}
.start-game:hover { background: #f3e97d; color: #191a14; }
.start-game:active { box-shadow: none; transform: translate(1px, 1px); }
.start-game span { font-family: sans-serif; font-size: 24px; }
#setup .start-description { margin: 10px 0 0; font-size: 13px; }
.quick-race { margin-top: 26px; padding-top: 22px; border-top: 1px solid #444; }
.quick-race h3 {
  margin: 0 0 14px;
  color: var(--text);
  font-family: "RoadRashFuturaBold", sans-serif;
  font-size: 18px;
  font-weight: 400;
}
.quick-race-actions { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 10px; }
.quick-race-actions select { min-width: 0; }
.quick-race-actions button { white-space: nowrap; font-size: 14px; }
.course-label { display: block; color: var(--muted); margin-bottom: 6px; }
select { width: 100%; font-family: inherit; font-size: 14px; }
#setup #status { margin: 20px 0 0; font-size: 12px; overflow-wrap: anywhere; }
#status.error { color: #ffb19a; }
progress { display: block; width: 100%; height: 8px; margin-top: 14px; accent-color: var(--yellow); }
#cancel { margin-top: 16px; }
#loading {
  position: absolute;
  inset: 0;
  z-index: 2;
  display: grid;
  place-items: center;
  padding: 24px;
  background: #101010;
}
.loading-panel { width: min(360px, 100%); }
#loading-title { font-size: clamp(32px, 5vw, 46px); margin: 0 0 16px; }
#loading-message { margin: 0; color: var(--text); font-size: 17px; }
.loading-track { height: 4px; margin-top: 26px; background: #34352c; overflow: hidden; }
.loading-track::after {
  content: "";
  display: block;
  width: 35%;
  height: 100%;
  background: var(--yellow);
  animation: loading-pulse 1.5s ease-in-out infinite alternate;
}
@keyframes loading-pulse { from { transform: translateX(-100%); } to { transform: translateX(285%); } }
@media (prefers-reduced-motion: reduce) {
  .loading-track::after { animation: none; transform: translateX(90%); }
}
.controls { position: relative; }
summary { list-style: none; }
summary::-webkit-details-marker { display: none; }
.controls[open] summary { color: var(--yellow); }
.controls-panel {
  position: absolute;
  top: 40px;
  right: 0;
  z-index: 10;
  width: min(340px, calc(100vw - 24px));
  padding: 22px;
  background: #202020;
  border: 1px solid #666;
  box-shadow: 4px 4px #000;
}
.controls-panel h2 { font-size: 26px; }
dl { display: grid; grid-template-columns: 95px 1fr; gap: 10px; margin: 0; font-size: 13px; }
dt { color: var(--yellow); font-family: "RoadRashFuturaDemi", sans-serif; }
dd { margin: 0; }
.controls-panel p { margin: 20px 0 0; color: var(--muted); font-size: 12px; }
footer { display: flex; justify-content: space-between; align-items: center; padding: 0 12px; font-size: 10px; color: #85877f; }
a { color: inherit; }
[hidden] { display: none !important; }
@media (max-width: 600px) {
  .toolbar { gap: 10px; padding: 0 8px; }
  h1 { font-size: 20px; }
  #state { display: none; }
  nav { gap: 5px; }
  .toolbar button, summary { font-size: 13px; padding: 6px 8px; }
  #setup { padding: 22px; }
  .quick-race-actions { grid-template-columns: 1fr; }
  footer { font-size: 9px; }
}
@media (max-height: 500px) {
  body { grid-template-rows: 40px minmax(0, 1fr) 0; }
  body.playing { grid-template-rows: 40px minmax(0, 1fr); }
  footer { display: none; }
  #setup { padding: 18px; }
  #setup h2 { font-size: 28px; margin-bottom: 12px; }
  #ready { margin-top: 0; }
  #setup:has(#ready:not([hidden])) #setup-description,
  #setup:has(#ready:not([hidden])) #status:not(.error),
  #setup .start-description { display: none; }
  .start-game { min-height: 44px; padding: 8px 16px; font-size: 20px; }
  .quick-race { margin-top: 16px; padding-top: 12px; }
  .quick-race h3 { font-size: 16px; margin-bottom: 8px; }
}
