* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html,
body {
  width: 100%;
  height: 100%;
  height: 100dvh;
  overflow: hidden;
  background: #1a3040;
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
  color: #f2f6f8;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
  position: fixed;
  inset: 0;
}

#c {
  display: block;
  width: 100%;
  height: 100%;
  height: 100dvh;
  touch-action: none;
}

#loader {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  background: radial-gradient(ellipse at center, #2a4a5c 0%, #0e1a22 100%);
  z-index: 20;
  font-size: 1.1rem;
  letter-spacing: 0.04em;
  transition: opacity 0.4s ease;
}

#loader.hidden {
  opacity: 0;
  pointer-events: none;
}

#hud {
  position: fixed;
  top: max(12px, env(safe-area-inset-top));
  left: max(12px, env(safe-area-inset-left));
  z-index: 10;
  pointer-events: none;
  text-shadow: 0 2px 8px rgba(0, 0, 0, 0.55);
  max-width: min(360px, 90vw);
}

#hud .title {
  font-size: 1.25rem;
  font-weight: 700;
  letter-spacing: 0.06em;
}

#hud .sub {
  opacity: 0.75;
  font-size: 0.85rem;
  margin-bottom: 8px;
}

#speed {
  font-size: 1.6rem;
  font-variant-numeric: tabular-nums;
  font-weight: 600;
  color: #ffe08a;
}

#race-hud {
  margin-top: 10px;
  padding: 8px 10px;
  background: rgba(0, 0, 0, 0.35);
  border-radius: 10px;
  border: 1px solid rgba(255, 255, 255, 0.12);
  font-size: 0.88rem;
  display: grid;
  gap: 4px;
}

#race-hud .lbl {
  opacity: 0.65;
  display: inline-block;
  min-width: 2.6rem;
}

#race-status {
  margin-top: 4px;
  color: #9fe8c2;
  font-size: 0.8rem;
}

#hud .hints {
  margin-top: 10px;
  display: flex;
  flex-direction: column;
  gap: 3px;
  font-size: 0.72rem;
  opacity: 0.8;
}

kbd {
  display: inline-block;
  padding: 1px 6px;
  border-radius: 4px;
  background: rgba(255, 255, 255, 0.12);
  border: 1px solid rgba(255, 255, 255, 0.22);
  font-size: 0.72rem;
}

/* Single top-right row: cameras + race actions */
#top-right {
  position: fixed;
  top: max(10px, env(safe-area-inset-top));
  right: max(10px, env(safe-area-inset-right));
  z-index: 10;
  display: flex;
  flex-direction: row;
  flex-wrap: nowrap;
  align-items: center;
  gap: 8px;
  max-width: calc(100vw - 24px);
  pointer-events: auto;
  padding: 6px 8px;
  border-radius: 999px;
  background: rgba(8, 18, 28, 0.55);
  border: 1px solid rgba(255, 255, 255, 0.12);
  backdrop-filter: blur(8px);
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.28);
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

#cam-bar,
#race-bar {
  position: static;
  display: flex;
  flex-direction: row;
  flex-wrap: nowrap;
  align-items: center;
  gap: 5px;
  max-width: none;
  top: auto;
  right: auto;
}

#race-bar {
  padding-left: 8px;
  margin-left: 2px;
  border-left: 1px solid rgba(255, 255, 255, 0.18);
}

.cam-btn {
  border: 1px solid rgba(255, 255, 255, 0.2);
  background: rgba(10, 24, 36, 0.45);
  color: #e8f2f8;
  font-size: 0.78rem;
  font-weight: 600;
  padding: 0;
  width: 34px;
  height: 34px;
  min-height: 34px;
  min-width: 34px;
  border-radius: 50%;
  cursor: pointer;
  backdrop-filter: blur(6px);
  -webkit-user-select: none;
  touch-action: manipulation;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex: 0 0 auto;
}

.cam-btn:hover {
  background: rgba(30, 60, 90, 0.7);
}

.cam-btn.active {
  background: linear-gradient(180deg, #5b8def, #2a4f9e);
  border-color: transparent;
  font-weight: 700;
}

#btn-race,
#btn-watch {
  border: none;
  border-radius: 999px;
  padding: 0 12px;
  height: 34px;
  min-height: 34px;
  font-size: 0.72rem;
  font-weight: 700;
  cursor: pointer;
  letter-spacing: 0.03em;
  box-shadow: none;
  touch-action: manipulation;
  -webkit-user-select: none;
  flex: 0 0 auto;
  white-space: nowrap;
}

#btn-race {
  background: linear-gradient(180deg, #ff6b6b, #b71c1c);
  color: #fff;
}

#btn-race.recording {
  background: linear-gradient(180deg, #ff3b3b, #7f0000);
  animation: pulse 1s ease infinite;
}

#btn-watch {
  background: rgba(20, 40, 60, 0.75);
  color: #cfe6f5;
  border: 1px solid rgba(255, 255, 255, 0.18);
}

#btn-watch.playing {
  background: linear-gradient(180deg, #7ec8ff, #2a6fa8);
  color: #fff;
}

#btn-watch:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

#replay-bar {
  position: fixed;
  left: 50%;
  bottom: max(120px, calc(env(safe-area-inset-bottom) + 110px));
  transform: translateX(-50%);
  z-index: 12;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 14px;
  background: rgba(8, 18, 28, 0.82);
  border: 1px solid rgba(255, 255, 255, 0.18);
  border-radius: 999px;
  box-shadow: 0 8px 28px rgba(0, 0, 0, 0.4);
  backdrop-filter: blur(8px);
  min-width: min(520px, 92vw);
}

#replay-bar.hidden {
  display: none;
}

#replay-bar button {
  border: none;
  border-radius: 999px;
  padding: 8px 14px;
  font-size: 0.78rem;
  font-weight: 700;
  cursor: pointer;
  background: linear-gradient(180deg, #5b8def, #2a4f9e);
  color: #fff;
  white-space: nowrap;
}

#replay-bar #btn-replay-stop {
  background: linear-gradient(180deg, #ff6b6b, #b71c1c);
}

#replay-scrub {
  flex: 1;
  accent-color: #7ec8ff;
  cursor: pointer;
}

body.replay-mode #pedals,
body.replay-mode #steer-pad {
  opacity: 0.35;
  pointer-events: none;
}

@keyframes pulse {
  50% {
    filter: brightness(1.15);
  }
}

#pedals {
  position: fixed;
  right: max(16px, env(safe-area-inset-right));
  bottom: max(18px, env(safe-area-inset-bottom));
  z-index: 10;
  display: flex;
  gap: 14px;
  align-items: flex-end;
}

#steer-pad {
  position: fixed;
  left: max(16px, env(safe-area-inset-left));
  bottom: max(18px, env(safe-area-inset-bottom));
  z-index: 10;
  display: flex;
  gap: 14px;
}

#pedals button,
#steer-pad button {
  border: none;
  cursor: pointer;
  color: #fff;
  font-weight: 700;
  letter-spacing: 0.08em;
  touch-action: none;
  -webkit-user-select: none;
  transition: transform 0.08s ease, filter 0.08s ease, box-shadow 0.08s ease;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.35);
  /* Big enough for thumbs on iPad */
  -webkit-tap-highlight-color: transparent;
}

#btn-gas {
  width: 104px;
  height: 128px;
  border-radius: 18px 18px 28px 28px;
  background: linear-gradient(180deg, #3ecf7a 0%, #1a8f4a 100%);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 6px;
}

#btn-brake {
  width: 92px;
  height: 108px;
  border-radius: 16px 16px 24px 24px;
  background: linear-gradient(180deg, #ff6b6b 0%, #c62828 100%);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 6px;
}

#btn-left,
#btn-right {
  width: 84px;
  height: 84px;
  border-radius: 50%;
  background: linear-gradient(180deg, #5b8def 0%, #2a4f9e 100%);
  font-size: 1.5rem;
}

#pedals button:active,
#pedals button.active,
#steer-pad button:active,
#steer-pad button.active {
  transform: scale(0.94) translateY(2px);
  filter: brightness(1.08);
  box-shadow: 0 3px 12px rgba(0, 0, 0, 0.4);
}

.pedal-label {
  font-size: 0.85rem;
}

.pedal-key {
  font-size: 0.7rem;
  opacity: 0.85;
  font-weight: 600;
}

/* iPad / tablet landscape & portrait */
@media (max-width: 1100px) {
  #hud .hints {
    display: none;
  }

  #top-right {
    left: max(8px, env(safe-area-inset-left));
    right: max(8px, env(safe-area-inset-right));
    top: auto;
    bottom: max(150px, calc(env(safe-area-inset-bottom) + 140px));
    justify-content: center;
    max-width: none;
    width: auto;
    margin: 0 auto;
  }

  #btn-gas {
    width: 110px;
    height: 130px;
  }

  #btn-brake {
    width: 96px;
    height: 112px;
  }

  #btn-left,
  #btn-right {
    width: 92px;
    height: 92px;
  }
}

@media (orientation: portrait) and (max-width: 900px) {
  #top-right {
    bottom: max(200px, calc(env(safe-area-inset-bottom) + 190px));
  }
}

/* main.js still toggles btn-race text — keep short labels readable */
#btn-race.recording {
  letter-spacing: 0;
}
