:root {
  --ink: #1f2a44;
  --muted: #5b6b8c;
  --paper: rgba(255, 255, 255, 0.86);
  --line: rgba(31, 42, 68, 0.14);
  --accent: #ff5d3b;
  --accent2: #2f9bff;
  --gold: #ffb300;
  --font-display: 'Black Han Sans', 'Jua', system-ui, sans-serif;
  --font-body: 'Jua', system-ui, -apple-system, 'Segoe UI', sans-serif;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; overflow: hidden; background: #8fd3ff; color: var(--ink); font-family: var(--font-body); }
body { touch-action: none; user-select: none; -webkit-user-select: none; }
#cv { position: fixed; inset: 0; width: 100%; height: 100%; display: block; }
button { font: inherit; color: inherit; cursor: pointer; border: 0; background: none; }

.back-btn {
  position: fixed; top: 12px; left: 12px; z-index: 40;
  font-size: 13px; color: var(--muted); text-decoration: none;
  padding: 8px 12px; border-radius: 12px; background: var(--paper); border: 1px solid var(--line);
  backdrop-filter: blur(6px);
}
.back-btn:hover { color: var(--ink); }

/* 화면 공통 */
.screen { position: fixed; inset: 0; z-index: 30; display: flex; flex-direction: column; align-items: center; justify-content: center; padding: 20px; }
.screen[hidden] { display: none; }
.eyebrow { margin: 0; font-size: 12px; letter-spacing: 0.22em; color: var(--muted); }
.logo { margin: 4px 0 0; font-family: var(--font-display); line-height: 0.92; text-align: center; }
.logo span { display: block; font-size: clamp(38px, 9vw, 72px); color: #fff; -webkit-text-stroke: 2px #1f2a44; text-shadow: 4px 5px 0 #1f2a44; }
.logo span + span { color: var(--gold); }
.kr { margin: 10px 0 0; font-size: 18px; color: var(--ink); }

.loading { background: linear-gradient(#8fd3ff, #d8f1ff); }
.load-box { text-align: center; }
.bar { width: min(320px, 70vw); height: 12px; margin: 22px auto 10px; border-radius: 8px; background: rgba(31, 42, 68, 0.15); overflow: hidden; }
.bar i { display: block; height: 100%; width: 0; background: var(--accent); border-radius: 8px; transition: width 0.15s; }
#loadText { margin: 0; font-size: 14px; color: var(--muted); }

/* 타이틀 */
.title { justify-content: space-between; padding-top: 48px; padding-bottom: max(20px, env(safe-area-inset-bottom)); background: linear-gradient(rgba(143, 211, 255, 0.35), rgba(143, 211, 255, 0) 40%); pointer-events: none; }
.title > * { pointer-events: auto; }
.title-top { text-align: center; }
.title-panel {
  width: min(460px, 100%); padding: 16px 18px 14px; border-radius: 20px;
  background: var(--paper); border: 1px solid var(--line); box-shadow: 0 16px 40px rgba(31, 42, 68, 0.22);
  backdrop-filter: blur(10px); text-align: center;
}
.label { margin: 0 0 8px; font-size: 13px; color: var(--muted); }
.kart-pick { display: flex; gap: 6px; justify-content: center; margin-bottom: 10px; }
.kart-pick button {
  width: 62px; padding: 7px 0 5px; border-radius: 14px; border: 2px solid var(--line); background: #fff;
  font-size: 13px; line-height: 1.1; color: var(--ink);
}
.kart-pick button i { display: block; width: 22px; height: 22px; margin: 0 auto 5px; border-radius: 50%; border: 3px solid #fff; box-shadow: 0 0 0 2px var(--line); }
.kart-pick button.on { border-color: var(--accent); background: #fff3ef; }
.seg { display: flex; gap: 4px; padding: 4px; margin: 0 auto 10px; border-radius: 14px; background: rgba(31, 42, 68, 0.08); width: fit-content; }
.seg button { padding: 7px 14px; border-radius: 10px; font-size: 14px; color: var(--muted); }
.seg button.on { background: #fff; color: var(--ink); box-shadow: 0 2px 6px rgba(31, 42, 68, 0.15); }
.circuit-pick { display: flex; align-items: center; justify-content: center; gap: 8px; margin-bottom: 4px; }
.circuit-pick > div { min-width: 0; flex: 0 1 300px; }
.circuit-pick b { display: block; font-family: var(--font-display); font-size: 20px; }
.circuit-pick small { display: block; font-size: 12px; color: var(--muted); }
.circuit-pick button { width: 34px; height: 34px; flex: none; border-radius: 10px; background: #fff; border: 1px solid var(--line); font-size: 13px; color: var(--muted); }
.circuit-pick button[hidden] { display: block; visibility: hidden; }
.best { margin: 4px 0 10px; font-size: 14px; color: var(--muted); }
.best b { color: var(--ink); font-variant-numeric: tabular-nums; }
.chips { display: flex; gap: 6px; justify-content: center; flex-wrap: wrap; margin: -4px 0 10px; }
.chips .chip { margin: 0; }
.chip { display: block; margin: -4px auto 10px; padding: 5px 12px; border-radius: 999px; font-size: 12px; color: var(--muted); background: #fff; border: 1px solid var(--line); }
.chip[hidden] { display: none; }
.chip.on { color: #3550b8; border-color: #9fb7ff; background: #eef2ff; }
.chip:disabled { cursor: default; opacity: 0.8; }
.cta {
  display: inline-block; padding: 12px 34px; border-radius: 999px; background: var(--accent); color: #fff;
  font-family: var(--font-display); font-size: 22px; letter-spacing: 0.04em; box-shadow: 0 6px 0 #c33f22;
  transition: transform 0.08s, box-shadow 0.08s;
}
.cta.small { padding: 9px 18px; font-size: 17px; box-shadow: 0 4px 0 #c33f22; }
.cta:disabled { opacity: 0.6; }
.cta[hidden], .circuit-pick[hidden], .best[hidden], .online[hidden] { display: none; }
.online { margin: 2px 0 6px; }
.nick { display: flex; align-items: center; justify-content: center; gap: 8px; margin: 0 0 10px; font-size: 13px; color: var(--muted); }
.nick input, .code-input { font: inherit; font-size: 16px; color: var(--ink); padding: 7px 10px; border-radius: 10px; border: 1px solid var(--line); background: #fff; width: 9em; user-select: text; -webkit-user-select: text; }
.code-input { width: 6.2em; text-transform: uppercase; letter-spacing: 0.12em; text-align: center; }
.join-row { display: flex; gap: 8px; justify-content: center; align-items: center; flex-wrap: wrap; }
.btn, .mini { padding: 8px 14px; border-radius: 10px; background: #fff; border: 1px solid var(--line); }
.mini { padding: 3px 8px; font-size: 12px; }
.hint { margin: 8px 0 0; font-size: 12px; color: var(--muted); }
.net-status { margin: 4px 0 0; font-size: 11px; color: var(--muted); min-height: 1em; }
.net-status.ok { color: #2e9e5b; }
.code { font-family: var(--font-display); letter-spacing: 0.12em; color: var(--accent); }
.players { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; text-align: left; }
.players li { display: flex; align-items: center; gap: 8px; padding: 8px 10px; border-radius: 12px; background: #fff; border: 1px solid var(--line); }
.players li i { width: 12px; height: 12px; border-radius: 50%; flex: none; }
.players li small { margin-left: auto; font-size: 12px; color: var(--muted); }
.players li.me { border-color: var(--accent); }
.cta:active { transform: translateY(4px); box-shadow: 0 2px 0 #c33f22; }
.howto { margin: 12px 0 0; font-size: 12px; color: var(--muted); }
.snd { font-size: 18px; width: 40px; height: 40px; border-radius: 12px; background: var(--paper); border: 1px solid var(--line); }
.snd.ghost { position: fixed; top: 12px; right: 12px; }

/* HUD */
.hud { position: fixed; inset: 10px 10px auto; z-index: 20; display: flex; justify-content: space-between; align-items: flex-start; pointer-events: none; }
.hud[hidden] { display: none; }
.hud > * { pointer-events: auto; }
.hud-left { display: flex; gap: 10px; align-items: stretch; margin-left: 100px; }
.hud-left > div, .hud-right > div { padding: 8px 12px; border-radius: 14px; background: var(--paper); border: 1px solid var(--line); backdrop-filter: blur(8px); }
.hud small { display: block; font-size: 10px; letter-spacing: 0.15em; color: var(--muted); }
.hud b { font-family: var(--font-display); font-size: 22px; line-height: 1; font-variant-numeric: tabular-nums; }
.lap { display: flex; flex-direction: column; justify-content: center; }
.pos { display: flex; flex-direction: column; justify-content: center; background: var(--ink) !important; color: #fff; }
.pos[hidden] { display: none; }
.pos small { color: rgba(255, 255, 255, 0.7) !important; }
.pos b { font-size: 30px !important; color: var(--gold); }
.pos b.bump { animation: bump 0.35s ease-out; }
.pos span { font-size: 13px; color: rgba(255, 255, 255, 0.75); }
@keyframes bump { from { transform: scale(1.5); } to { transform: scale(1); } }
.lap span { font-size: 13px; color: var(--muted); }
.times { display: flex; gap: 14px; }
.times b { font-size: 18px; }
.hud-right { display: flex; flex-direction: column; align-items: flex-end; gap: 6px; margin-right: 150px; }
.speed { display: flex; align-items: baseline; gap: 4px; }
.speed b { font-size: 26px; min-width: 2.2ch; text-align: right; }
.charge { width: 110px; height: 10px; padding: 0 !important; overflow: hidden; }
.charge i { display: block; height: 100%; width: 0; background: var(--accent2); transition: width 0.08s linear, background 0.2s; }
.charge i.lv1 { background: var(--accent2); }
.charge i.lv2 { background: var(--gold); }
.coins { display: flex; align-items: center; gap: 6px; padding: 5px 10px !important; }
.coins i { width: 16px; height: 16px; border-radius: 50%; background: radial-gradient(circle at 35% 35%, #fff3b0, #ffc928 45%, #c98a00); box-shadow: inset 0 0 0 2px #e0a400; }
.coins b { font-size: 18px; }
.coins small { display: inline !important; letter-spacing: 0 !important; }
.coins.max b { color: #e0a400; }
.coins.bump { animation: bump 0.3s ease-out; }
.minimap { width: 128px; height: 128px; border-radius: 16px; background: rgba(255, 255, 255, 0.55); border: 1px solid var(--line); backdrop-filter: blur(6px); }
.hud-btns { position: fixed; top: 10px; right: 10px; display: flex; gap: 6px; }
.hud-btns button { width: 40px; height: 40px; border-radius: 12px; background: var(--paper); border: 1px solid var(--line); font-size: 16px; }

.center-msg {
  position: fixed; left: 50%; top: 30%; transform: translate(-50%, -50%); z-index: 25;
  font-family: var(--font-display); font-size: clamp(56px, 14vw, 120px); color: #fff;
  -webkit-text-stroke: 3px var(--ink); text-shadow: 6px 7px 0 var(--ink); pointer-events: none;
  animation: pop 0.5s ease-out;
}
.center-msg.small { font-size: clamp(30px, 7vw, 56px); -webkit-text-stroke: 2px var(--ink); text-shadow: 4px 5px 0 var(--ink); }
.center-msg[hidden] { display: none; }
@keyframes pop { from { transform: translate(-50%, -50%) scale(1.5); opacity: 0; } to { transform: translate(-50%, -50%) scale(1); opacity: 1; } }
.split {
  position: fixed; left: 50%; top: calc(30% + 52px); transform: translateX(-50%); z-index: 25; padding: 4px 14px; border-radius: 12px;
  font-family: var(--font-display); font-size: 24px; font-variant-numeric: tabular-nums; color: #fff; pointer-events: none; animation: pop2 0.4s;
}
.split[hidden] { display: none; }
.split.fast { background: rgba(46, 158, 91, 0.92); }
.split.slow { background: rgba(229, 57, 53, 0.92); }
.wrong {
  position: fixed; left: 50%; top: 45%; transform: translateX(-50%); z-index: 25; padding: 8px 18px; border-radius: 12px;
  background: rgba(255, 93, 59, 0.92); color: #fff; font-family: var(--font-display); font-size: 22px; animation: blink 0.6s infinite alternate;
}
.wrong[hidden] { display: none; }
@keyframes blink { from { opacity: 0.55; } to { opacity: 1; } }

/* 터치 조작 */
.touch { position: fixed; left: 0; right: 0; bottom: 0; z-index: 22; display: flex; justify-content: space-between; align-items: flex-end; padding: 0 14px calc(14px + env(safe-area-inset-bottom)); pointer-events: none; }
.touch[hidden] { display: none; }
.t-left, .t-right { display: flex; gap: 10px; align-items: flex-end; pointer-events: auto; }
.t-btn {
  width: 76px; height: 76px; border-radius: 22px; background: rgba(255, 255, 255, 0.78); border: 2px solid rgba(31, 42, 68, 0.2);
  font-family: var(--font-display); font-size: 26px; color: var(--ink); box-shadow: 0 4px 0 rgba(31, 42, 68, 0.25);
}
.t-btn.small { width: 56px; height: 56px; font-size: 16px; border-radius: 16px; }
.t-btn.drift { width: 96px; height: 96px; font-size: 18px; background: rgba(47, 155, 255, 0.85); color: #fff; border-color: #1c6fc2; border-radius: 50%; }
.t-btn.pressed { transform: translateY(3px); box-shadow: 0 1px 0 rgba(31, 42, 68, 0.25); filter: brightness(0.92); }

/* 오버레이 */
.overlay { background: rgba(31, 42, 68, 0.35); backdrop-filter: blur(4px); }
.panel {
  width: min(400px, 100%); padding: 22px 20px; border-radius: 22px; background: var(--paper); border: 1px solid var(--line);
  box-shadow: 0 20px 50px rgba(31, 42, 68, 0.3); text-align: center; display: flex; flex-direction: column; gap: 10px;
}
.panel h2 { margin: 0; font-family: var(--font-display); font-size: 34px; }
.panel button:not(.cta) { padding: 10px; border-radius: 12px; background: #fff; border: 1px solid var(--line); }
.panel button.mini { padding: 3px 8px; border-radius: 8px; }
.panel .chip { padding: 5px 12px; border-radius: 999px; margin: 0 auto; }
.panel .row { display: flex; gap: 8px; justify-content: center; flex-wrap: wrap; }
.record { margin: 0; color: var(--accent); font-family: var(--font-display); font-size: 22px; animation: pop2 0.5s; }
.r-total small { display: block; font-size: 11px; letter-spacing: 0.15em; color: var(--muted); }
.r-total b { font-family: var(--font-display); font-size: 40px; font-variant-numeric: tabular-nums; }
.r-laps { margin: 0; padding: 0 0 0 20px; text-align: left; display: grid; gap: 2px; font-variant-numeric: tabular-nums; }
.r-laps li b { float: right; }
.r-laps li.hi { color: var(--accent2); }
.r-laps[hidden], .r-table[hidden] { display: none; }
.r-table { width: 100%; border-collapse: collapse; font-size: 15px; font-variant-numeric: tabular-nums; }
.r-table td { padding: 4px 6px; border-bottom: 1px solid var(--line); text-align: left; }
.r-table td.rk { width: 1.6em; font-family: var(--font-display); color: var(--muted); text-align: center; }
.r-table td.num { text-align: right; white-space: nowrap; }
.r-table td.name i { display: inline-block; width: 10px; height: 10px; margin-right: 6px; border-radius: 50%; vertical-align: 1px; }
.r-table tr.me td { color: var(--accent); font-weight: bold; }
.r-table tr:first-child td.rk { color: var(--gold); }
.trophy { font-size: 64px; line-height: 1; animation: pop2 0.6s; }
@keyframes pop2 { from { transform: scale(1.5); opacity: 0; } to { transform: scale(1); opacity: 1; } }
.panel { max-height: calc(100% - 20px); overflow-y: auto; }

@media (max-width: 720px) {
  .hud-left { margin-left: 0; margin-top: 44px; }
  .hud-right { margin-right: 0; margin-top: 44px; }
  .times { gap: 8px; }
  .times b { font-size: 15px; }
  .hud b { font-size: 18px; }
  .speed b { font-size: 22px; }
  .charge { width: 80px; }
  .minimap { width: 100px; height: 100px; }
  .coins b { font-size: 15px; }
  .title { padding-top: 36px; }
  .title-panel { padding: 12px 12px 10px; }
  .kart-pick { flex-wrap: nowrap; overflow-x: auto; justify-content: flex-start; padding-bottom: 2px; }
  .kart-pick button { flex: none; width: 50px; padding: 6px 0 4px; font-size: 11px; }
  .kart-pick button i { width: 20px; height: 20px; }
  .seg button { padding: 6px 10px; font-size: 13px; }
  .circuit-pick b { font-size: 17px; }
  .pos b { font-size: 24px !important; }
  .howto { display: none; }
}
@media (max-width: 440px) {
  .hud-left { gap: 6px; }
  .hud-left > div, .hud-right > div { padding: 6px 9px; }
  .times > div:nth-child(2) { display: none; }
  .speed b { font-size: 20px; }
}
@media (max-height: 520px) and (orientation: landscape) {
  .title-top .kr { display: none; }
  .logo span { font-size: 34px; }
  .title { padding-top: 10px; }
}
