html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: #111;
  color: #eee;
  font-family: system-ui, sans-serif;
}
.raceWrap {
  position: relative;
  width: 100vw;
  height: 100vh;
}
#race {
  position: absolute;
  inset: 0;
  display: block;
}
/* 웹캠 미리보기: 화면 우하단에 작은 PIP로 고정 표시. 내부 추적 해상도(640x360)는
   그대로 두고 화면에 보이는 크기만 CSS로 축소한다(랜드마크는 정규화 좌표라 무관). */
.stage {
  position: fixed;
  right: 16px;
  top: 16px;
  width: 440px;
  height: 248px;
  background: #000;
  border: 2px solid #444;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.5);
  z-index: 5;
}
video, canvas.overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  transform: scaleX(-1); /* 거울 모드: 보기 편하도록 좌우 반전 표시만, 판별 로직은 원본 좌표 사용 */
}
.topBar {
  position: absolute;
  top: 12px;
  left: 12px;
  z-index: 5;
  display: flex;
  align-items: center;
  gap: 12px;
  background: rgba(0, 0, 0, 0.5);
  padding: 8px 12px;
  border-radius: 8px;
}
.topBar h1 {
  margin: 0;
  font-size: 16px;
  font-weight: 600;
  white-space: nowrap;
}
#winnerBanner {
  position: absolute;
  inset: 0;
  display: none;
  background: rgba(0, 0, 0, 0.6);
  text-align: center;
  z-index: 4;
}
#winnerBanner.visible {
  display: block;
}
/* 우승 이미지: 화면 전체를 채우도록 확대(비율 유지, object-fit: contain이라 잘리지 않음). */
#medal {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: contain;
  display: none;
  transform: scale(0);
}
#medal.pop {
  display: block;
  animation: medalPop 0.6s cubic-bezier(0.34, 1.56, 0.64, 1) forwards;
}
@keyframes medalPop {
  0% { transform: scale(0) rotate(-15deg); opacity: 0; }
  60% { transform: scale(1.15) rotate(5deg); opacity: 1; }
  100% { transform: scale(1) rotate(0deg); opacity: 1; }
}
/* 승패 표시: 멀리서 찍어도 보이게 크게. 이긴 경우 우승 그림 위 상단, 진 경우 화면 가운데. */
#winnerText {
  position: absolute;
  top: 3vh;
  left: 0;
  right: 0;
  padding-right: 470px; /* 오른쪽 위 웹캠 미리보기(440px)에 가리지 않게 남은 영역 가운데 */
  font-size: clamp(64px, 11vh, 140px);
  font-weight: 900;
  line-height: 1;
  color: #ffe14d;
  text-shadow: 0 0 4px #000, 0 4px 0 #1a1a1a, 0 8px 22px rgba(0, 0, 0, 0.9);
}
/* 이번 기록 / 최고 기록: 하단 반투명 띠 위(왼쪽). 시상대 그림 글씨와 겹치지 않게 띠로 가린다. */
#recordText {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  height: clamp(120px, 20vh, 220px);
  box-sizing: border-box;
  padding: 0 calc(clamp(260px, 30vw, 520px) + 48px) 0 4vw;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: flex-start;
  background: rgba(0, 0, 0, 0.72);
  font-size: clamp(40px, 7vh, 76px);
  font-weight: 900;
  line-height: 1.15;
  color: #fff;
  text-align: left;
}
#recordText .best {
  font-size: clamp(26px, 4.4vh, 48px);
  color: #ffe14d;
}
#recordText .newBest {
  color: #ff5ad1;
}
/* 다시 시작 버튼: 하단 띠 오른쪽에 크게. */
#restartButton {
  position: absolute;
  right: 4vw;
  bottom: calc(clamp(120px, 20vh, 220px) / 2);
  transform: translateY(50%);
  width: clamp(260px, 30vw, 520px);
  font-size: clamp(36px, 6.5vh, 72px);
  font-weight: 900;
  padding: 0.25em 0.5em;
  background: #ffcc00;
  color: #1a1a1a;
  border: 4px solid #fff;
  border-radius: 18px;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.6);
  cursor: pointer;
}
/* 중간 지점(position 0.5) 통과 연출: 화면 전체에 1초간 떴다 사라진다. */
#milestoneBanner {
  position: absolute;
  inset: 0;
  display: none;
  z-index: 4;
  pointer-events: none;
}
#milestoneBanner.visible {
  display: block;
}
#milestoneImage {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: contain;
  opacity: 0;
  animation: milestonePop 1.5s ease-out forwards;
}
@keyframes milestonePop {
  0% { opacity: 0; transform: scale(0.92); }
  15% { opacity: 1; transform: scale(1); }
  80% { opacity: 1; transform: scale(1); }
  100% { opacity: 0; transform: scale(1.03); }
}
button {
  font-size: 16px;
  padding: 8px 16px;
}

/* 시작 실패 안내(카메라 거부·없음·모델 로드 실패): 멀리서도 보이게 화면 전체를 덮고 크게. */
#startError {
  position: absolute;
  inset: 0;
  z-index: 10;
  display: none;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4vh;
  padding: 0 6vw;
  background: rgba(0, 20, 60, 0.88);
  text-align: center;
}
#startError.visible {
  display: flex;
}
#startErrorTitle {
  font-size: clamp(32px, 9vh, 96px);
  font-weight: 800;
  color: #ffe14d;
  line-height: 1.15;
  word-break: keep-all;
}
#startErrorDetail {
  font-size: clamp(18px, 4.2vh, 44px);
  color: #fff;
  line-height: 1.4;
  max-width: 1400px;
  word-break: keep-all;
}
#startErrorRetry {
  font-size: clamp(22px, 6.5vh, 64px);
  font-weight: 800;
  padding: 0.35em 1.2em;
  border: none;
  border-radius: 0.4em;
  background: #ffd400;
  color: #111;
  cursor: pointer;
}
