:root {
  --white: #fff;
  --text-shadow: rgba(0, 0, 0, 0.25);
  --ink-2: #2c3a52;
  --ink-3: #3a4760;
  --track: rgba(255, 255, 255, 0.6);
  --yellow: #ffd23f;
  --white-edge: rgba(255, 255, 255, 0.85);
  --accent-deep: #c8451f;
  --blue-deep: #1f6fd1;
  --emoji-shadow: rgba(0, 0, 0, 0.2);
  --accent-soft: #ff8c6b;
  --veil-top: rgba(255, 255, 255, 0.35);
  --veil-bot: rgba(95, 130, 180, 0.35);
  --yellow-deep: #c98a00;
  --accent-light: #ff8c5a;
  --red-deep: #d63a3a;
  --toast-bg: rgba(40, 38, 48, 0.92);
  --boost-bg: rgba(255, 140, 0, 0.85);
  --count-shadow: rgba(0, 0, 0, 0.3);
  --blue: #3d9bff;
  --error: #c62828;
  --brick-inset: rgba(0, 0, 0, 0.18);
  --lego-green: #2fa84f;
  --start-veil: rgba(255, 255, 255, 0.3);
  --clear: rgba(255, 255, 255, 0);
  --fs-xs: 15px;
  --fs-s: 17px;
  --fs-m: 22px;
  --fs-l: 26px;
  --fs-xl: 30px;
  --fs-h2: clamp(26px, 8vw, 34px);
  --fs-h1: clamp(40px, 13vw, 68px);
  --fs-count: clamp(64px, 26vw, 120px);
  --bg-top: #bfe6ff;
  --bg-bot: #fff3da;
  --ink: #34323d;
  --accent: #ff6a3d;
  --accent-2: #2ec4b6;
  --panel: rgba(255, 255, 255, 0.78);
  --panel-stroke: rgba(255, 255, 255, 0.9);
  --shadow: 0 10px 30px rgba(73, 96, 140, 0.22);
  --shadow-sm: 0 4px 12px rgba(73, 96, 140, 0.18);
}

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

html,
body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  position: fixed;
  inset: 0;
  overscroll-behavior: none;
  font-family: "Nunito", system-ui, sans-serif;
  color: var(--ink);
  background: linear-gradient(180deg, var(--bg-top) 0%, var(--bg-bot) 100%);
  touch-action: none;
}

#app {
  position: fixed;
  inset: 0;
}

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

.hidden {
  display: none !important;
}

/* ---------- 운전 HUD ---------- */
#hud {
  position: fixed;
  top: calc(env(safe-area-inset-top) + 12px);
  left: 0;
  right: 0;
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  padding: 0 14px;
  z-index: 10;
  pointer-events: none;
}
#hearts {
  font-size: var(--fs-l);
  letter-spacing: 2px;
  filter: drop-shadow(0 2px 3px var(--emoji-shadow));
}
.stats {
  display: flex;
  flex-direction: column;
  gap: 6px;
  align-items: flex-end;
}
.chip {
  font-family: "Fredoka", sans-serif;
  font-weight: 700;
  font-size: var(--fs-s);
  padding: 6px 14px;
  background: var(--panel);
  border: 2px solid var(--panel-stroke);
  border-radius: 999px;
  box-shadow: var(--shadow-sm);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
}

/* ---------- 색칠 HUD (터치 통과) ---------- */
#paintHud {
  position: fixed;
  top: calc(env(safe-area-inset-top) + 14px);
  left: 50%;
  transform: translateX(-50%);
  z-index: 10;
  pointer-events: none;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  width: min(360px, 88%);
}
.paint-card {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 18px;
  background: var(--panel);
  border: 2px solid var(--panel-stroke);
  border-radius: 999px;
  box-shadow: var(--shadow);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  animation: dropIn 0.4s cubic-bezier(0.2, 1.3, 0.4, 1) both;
}
.paint-chip {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  border: 3px solid var(--white);
  box-shadow: var(--shadow-sm);
}
.paint-msg {
  font-family: "Fredoka", sans-serif;
  font-weight: 700;
  font-size: var(--fs-s);
}
.timer-bar {
  width: 100%;
  height: 12px;
  border-radius: 999px;
  background: var(--track);
  overflow: hidden;
  box-shadow: var(--shadow-sm);
}
.timer-fill {
  height: 100%;
  width: 100%;
  border-radius: 999px;
  background: linear-gradient(90deg, var(--yellow), var(--accent));
  transition: width 0.1s linear;
}
@keyframes dropIn {
  from { opacity: 0; transform: translateY(-16px); }
}

/* ---------- 둥근 버튼 ---------- */
.round-btn {
  width: 56px;
  height: 56px;
  border: 2px solid var(--panel-stroke);
  border-radius: 50%;
  background: var(--panel);
  box-shadow: var(--shadow-sm);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  font-size: var(--fs-xl);
  font-family: "Fredoka", sans-serif;
  color: var(--ink);
  display: grid;
  place-items: center;
  cursor: pointer;
  transition: transform 0.08s ease;
  line-height: 1;
  padding-bottom: 3px;
}
.round-btn:active {
  transform: scale(0.88);
}
.round-btn.save {
  font-size: var(--fs-l);
  background: linear-gradient(180deg, var(--accent-soft), var(--accent));
  color: var(--white);
  border-color: var(--white-edge);
}

/* ---------- 시작 / 게임오버 화면 ---------- */
.screen {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 18px;
  padding: 24px;
  text-align: center;
  background: radial-gradient(
    120% 90% at 50% 10%,
    var(--veil-top),
    var(--veil-bot)
  );
  backdrop-filter: blur(3px);
  -webkit-backdrop-filter: blur(3px);
  animation: fadeIn 0.4s ease both;
}
@keyframes fadeIn {
  from { opacity: 0; }
}
.title {
  font-family: "Fredoka", sans-serif;
  font-weight: 700;
  font-size: var(--fs-h1);
  margin: 0;
  color: var(--white);
  text-shadow: 0 4px 0 var(--accent), 0 8px 18px var(--text-shadow);
  letter-spacing: -1px;
}
.title span {
  color: var(--yellow);
  text-shadow: 0 4px 0 var(--yellow-deep), 0 8px 18px var(--text-shadow);
}
.sub {
  margin: 0;
  font-weight: 800;
  font-size: var(--fs-xs);
  color: var(--ink-2);
  max-width: 320px;
}
.car-pick {
  display: flex;
  align-items: center;
  gap: 14px;
  margin-top: 4px;
}
.car-name {
  display: flex;
  align-items: center;
  gap: 8px;
  font-family: "Fredoka", sans-serif;
  font-weight: 700;
  font-size: var(--fs-l);
  padding: 10px 22px;
  min-width: 150px;
  justify-content: center;
  background: var(--panel);
  border: 2px solid var(--panel-stroke);
  border-radius: 999px;
  box-shadow: var(--shadow-sm);
}
.big-btn {
  font-family: "Fredoka", sans-serif;
  font-weight: 700;
  font-size: var(--fs-l);
  color: var(--white);
  border: none;
  border-radius: 22px;
  padding: 16px 40px;
  background: linear-gradient(180deg, var(--accent-light), var(--accent));
  box-shadow: 0 6px 0 var(--accent-deep), var(--shadow);
  cursor: pointer;
  transition: transform 0.08s ease, box-shadow 0.08s ease;
  animation: pulse 1.6s ease-in-out infinite;
}
.big-btn:active {
  transform: translateY(4px);
  box-shadow: 0 2px 0 var(--accent-deep), var(--shadow-sm);
}
@keyframes pulse {
  50% { transform: scale(1.04); }
}
.howto {
  font-weight: 800;
  color: var(--ink-3);
  font-size: var(--fs-xs);
  margin: 4px 0 0;
}
.over-title {
  font-family: "Fredoka", sans-serif;
  font-size: var(--fs-h1);
  margin: 0;
  color: var(--white);
  text-shadow: 0 4px 0 var(--red-deep), 0 8px 18px var(--text-shadow);
}
.score-box {
  display: flex;
  flex-direction: column;
  gap: 8px;
  font-family: "Fredoka", sans-serif;
  font-weight: 600;
  font-size: var(--fs-m);
  color: var(--ink-2);
  background: var(--panel);
  border: 2px solid var(--panel-stroke);
  border-radius: 22px;
  padding: 18px 32px;
  box-shadow: var(--shadow);
}
.score-box b {
  color: var(--accent);
  font-size: var(--fs-l);
}
.over-actions {
  display: flex;
  align-items: center;
  gap: 12px;
}

/* ---------- 토스트 ---------- */
.toast {
  position: fixed;
  bottom: calc(env(safe-area-inset-bottom) + 30px);
  left: 50%;
  transform: translateX(-50%);
  background: var(--toast-bg);
  color: var(--white);
  font-weight: 700;
  padding: 12px 22px;
  border-radius: 999px;
  z-index: 30;
  opacity: 0;
  transition: opacity 0.3s ease, transform 0.3s ease;
  pointer-events: none;
}
.toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(-8px);
}

/* ====================== 레고 레이서 추가분 ====================== */
:root {
  --lego-red: #e3262b;
  --lego-yellow: #ffcd03;
  --sp-1: 6px;
  --sp-2: 10px;
  --sp-3: 14px;
  --r-pill: 999px;
  --r-card: 22px;
}
.hud-left {
  display: flex;
  flex-direction: column;
  gap: var(--sp-1);
  align-items: flex-start;
}
#hud .progress {
  position: fixed;
  left: 50%;
  transform: translateX(-50%);
  bottom: calc(env(safe-area-inset-bottom) + 22px);
  width: min(300px, 70%);
  height: 10px;
  border-radius: var(--r-pill);
  background: var(--track);
  overflow: hidden;
  box-shadow: var(--shadow-sm);
}
.progress-fill {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, var(--lego-yellow), var(--lego-red));
}
.boost-flash,
.countdown {
  position: fixed;
  left: 50%;
  transform: translateX(-50%);
  z-index: 15;
  pointer-events: none;
  font-family: "Fredoka", sans-serif;
  font-weight: 700;
  color: var(--white);
}
.boost-flash {
  top: 34%;
  font-size: var(--fs-m);
  padding: var(--sp-2) var(--sp-3);
  border-radius: var(--r-pill);
  background: var(--boost-bg);
  animation: dropIn 0.3s ease both;
}
.countdown {
  top: 30%;
  font-size: var(--fs-count);
  text-shadow: 0 6px 0 var(--accent), 0 12px 24px var(--count-shadow);
  animation: pop 0.9s ease both;
}
@keyframes pop {
  from { transform: translateX(-50%) scale(1.6); opacity: 0; }
  30% { transform: translateX(-50%) scale(1); opacity: 1; }
}
.turn-row {
  display: flex;
  gap: var(--sp-2);
}
.pill-btn,
.photo-btn {
  font-family: "Nunito", sans-serif;
  font-weight: 800;
  font-size: var(--fs-xs);
  color: var(--ink);
  padding: var(--sp-2) var(--sp-3);
  border-radius: var(--r-pill);
  border: 2px solid var(--panel-stroke);
  background: var(--panel);
  box-shadow: var(--shadow-sm);
  cursor: pointer;
}
.photo-btn {
  font-size: var(--fs-s);
  color: var(--white);
  background: linear-gradient(180deg, var(--blue), var(--blue-deep));
  border-color: var(--white-edge);
}
.pill-btn:active,
.photo-btn:active {
  transform: scale(0.94);
}
.back-link {
  font-weight: 800;
  font-size: var(--fs-xs);
  color: var(--ink-3);
}
.screen-title {
  font-family: "Fredoka", sans-serif;
  font-weight: 700;
  font-size: var(--fs-h2);
  margin: 0;
  color: var(--white);
  text-shadow: 0 3px 0 var(--accent), 0 6px 14px var(--text-shadow);
}
#uploadScreen,
#waitScreen {
  justify-content: flex-start;
  padding-top: calc(env(safe-area-inset-top) + 40px);
  overflow-y: auto;
  -webkit-user-select: auto;
  user-select: auto;
}
.thumbs {
  display: flex;
  gap: var(--sp-2);
  flex-wrap: wrap;
  justify-content: center;
  min-height: 72px;
}
.thumbs img {
  width: 72px;
  height: 72px;
  object-fit: cover;
  border-radius: var(--sp-3);
  border: 3px solid var(--white);
  box-shadow: var(--shadow-sm);
}
.field {
  display: flex;
  flex-direction: column;
  gap: var(--sp-1);
  width: min(320px, 100%);
  text-align: left;
  font-weight: 800;
  font-size: var(--fs-xs);
  color: var(--ink-2);
}
.field input {
  font: inherit;
  font-size: var(--fs-s);
  padding: var(--sp-2) var(--sp-3);
  border-radius: var(--sp-3);
  border: 2px solid var(--panel-stroke);
  background: var(--white);
  color: var(--ink);
  -webkit-user-select: text;
  user-select: text;
}
.err {
  min-height: 1.2em;
  margin: 0;
  font-weight: 800;
  font-size: var(--fs-xs);
  color: var(--error);
}
.score-box .best {
  font-size: var(--fs-xs);
  color: var(--ink-3);
}
.bricks {
  display: flex;
  gap: var(--sp-1);
}
.bricks i {
  width: 34px;
  height: 22px;
  border-radius: var(--sp-1);
  background: var(--lego-red);
  box-shadow: inset 0 -4px 0 var(--brick-inset);
  animation: hop 1s ease-in-out infinite;
}
.bricks i:nth-child(2) { background: var(--lego-yellow); animation-delay: 0.15s; }
.bricks i:nth-child(3) { background: var(--blue-deep); animation-delay: 0.3s; }
.bricks i:nth-child(4) { background: var(--lego-green); animation-delay: 0.45s; }
@keyframes hop {
  50% { transform: translateY(-12px); }
}

/* 시작 화면: 차가 또렷이 보이도록 흐림 없이, 글은 위쪽에 */
#startScreen {
  justify-content: flex-start;
  padding-top: calc(env(safe-area-inset-top) + 36px);
  backdrop-filter: none;
  -webkit-backdrop-filter: none;
  background: linear-gradient(180deg, var(--start-veil), var(--clear) 70%);
}
#startScreen .start-bottom {
  margin-top: auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--sp-2);
  padding-bottom: calc(env(safe-area-inset-bottom) + 12px);
}

.screen {
  word-break: keep-all;
}
#waitTitle {
  white-space: pre-line;
}
