:root {
  --ink: #3a2e5c; --pink: #ff6f91; --blue: #4aa8ff; --yellow: #ffd23f; --mint: #5fd6a4;
  --card: rgba(255, 255, 255, 0.94); --shadow: 0 6px 0 rgba(58, 46, 92, 0.18);
  --font: 'Jua', 'Apple SD Gothic Neo', 'Malgun Gothic', system-ui, sans-serif;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; overflow: hidden; background: #9fd3ff; color: var(--ink); font-family: var(--font); }
body { touch-action: none; user-select: none; -webkit-user-select: none; }
#cv { position: fixed; inset: 0; width: 100vw; height: 100vh; height: 100dvh; display: block; }
.hidden { display: none !important; }

/* ---------- 메뉴 ---------- */
#ui { position: fixed; inset: 0; pointer-events: none; }
.screen { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; padding: 16px; pointer-events: auto;
  background: linear-gradient(180deg, rgba(159, 211, 255, 0.15), rgba(255, 220, 240, 0.35)); overflow-y: auto; }
.card { background: var(--card); border-radius: 26px; padding: 22px; width: min(460px, 100%); box-shadow: var(--shadow), 0 20px 50px rgba(58, 46, 92, 0.15);
  border: 3px solid #fff; max-height: calc(100dvh - 32px); overflow-y: auto; }
.title-card { text-align: center; }
.logo { font-size: clamp(48px, 13vw, 72px); margin: 0; letter-spacing: 2px; color: var(--pink);
  text-shadow: 0 4px 0 #c2406b, 0 8px 0 rgba(0, 0, 0, 0.1); transform: rotate(-3deg); }
.logo span { color: var(--yellow); text-shadow: 0 4px 0 #c79a00; }
.sub { margin: 6px 0 16px; opacity: 0.75; }
h2 { margin: 0; font-size: 24px; }
.field { display: block; text-align: left; margin: 10px 0; font-size: 15px; }
input { font-family: var(--font); font-size: 18px; padding: 11px 14px; border-radius: 14px; border: 3px solid #e6defa; width: 100%;
  outline: none; color: var(--ink); background: #fff; user-select: text; -webkit-user-select: text; }
input:focus { border-color: var(--blue); }
.field input { margin-top: 6px; }
.colors { display: flex; gap: 8px; margin-top: 8px; flex-wrap: wrap; }
.colors button { width: 38px; height: 38px; border-radius: 50%; border: 4px solid #fff; box-shadow: 0 0 0 2px #e6defa; cursor: pointer; padding: 0; }
.colors button.sel { box-shadow: 0 0 0 3px var(--ink); transform: scale(1.1); }
button { font-family: var(--font); font-size: 17px; border: none; border-radius: 14px; padding: 11px 16px; cursor: pointer; color: var(--ink);
  background: #fff; box-shadow: 0 4px 0 #d9d0f0; transition: transform 0.05s; white-space: nowrap; }
button:active { transform: translateY(3px); box-shadow: 0 1px 0 #d9d0f0; }
button.primary { background: var(--pink); color: #fff; box-shadow: 0 4px 0 #c2406b; }
button.primary:active { box-shadow: 0 1px 0 #c2406b; }
button:disabled { opacity: 0.45; pointer-events: none; }
button.big { font-size: 20px; padding: 14px; width: 100%; }
button.small { font-size: 14px; padding: 7px 11px; }
.btns { display: flex; flex-direction: column; gap: 10px; margin-top: 14px; }
.btns.row { flex-direction: row; }
.row { display: flex; gap: 8px; align-items: center; margin: 10px 0; }
.row.head { margin-top: 0; }
.back { padding: 8px 14px; font-size: 20px; }
.status { font-size: 14px; margin: 14px 0 4px; opacity: 0.8; }
.status.ok { color: #239a6a; } .status.warn { color: #c77a00; } .status.bad { color: #d23b5c; }
.help { text-align: left; font-size: 14px; margin-top: 8px; background: #f6f2ff; border-radius: 14px; padding: 8px 12px; }
.help summary { cursor: pointer; }
.help p { margin: 6px 0; }
.hint { font-size: 13px; opacity: 0.7; margin: 12px 0 0; }
.list { display: flex; flex-direction: column; gap: 8px; margin-top: 12px; }
.list .empty { text-align: center; opacity: 0.6; }
.roomitem { display: flex; justify-content: space-between; align-items: center; background: #f6f2ff; border-radius: 16px; padding: 12px 14px; cursor: pointer; border: 3px solid transparent; }
.roomitem:hover { border-color: var(--blue); }
.roomitem small { opacity: 0.65; }
.roomitem .badge { font-size: 13px; padding: 3px 8px; border-radius: 10px; background: var(--mint); color: #fff; }
.roomitem .badge.racing { background: var(--pink); }
.code-row { justify-content: space-between; background: #fff7d6; border-radius: 14px; padding: 8px 12px; }
.code-row b { font-size: 22px; letter-spacing: 3px; color: var(--pink); }
.grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px; margin-top: 8px; }
.slot { background: #f6f2ff; border-radius: 16px; padding: 10px; display: flex; align-items: center; gap: 8px; min-height: 56px; font-size: 16px; position: relative; }
.slot.empty { opacity: 0.4; justify-content: center; }
.slot .av { width: 34px; height: 34px; border-radius: 50%; border: 3px solid #fff; box-shadow: 0 2px 0 rgba(0, 0, 0, 0.1); flex: none; }
.slot .nm { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.slot .st { font-size: 13px; padding: 2px 7px; border-radius: 9px; background: #e6defa; }
.slot .st.ready { background: var(--mint); color: #fff; }
.slot.me { outline: 3px solid var(--yellow); }
.slot.off { opacity: 0.6; }

/* 대기실 채팅 */
.chat { margin-top: 12px; background: #f6f2ff; border-radius: 16px; padding: 8px; }
.chatlog { height: 130px; overflow-y: auto; font-size: 15px; padding: 2px 4px; touch-action: pan-y; user-select: text; -webkit-user-select: text; }
.chatlog .cm { margin: 3px 0; line-height: 1.35; word-break: break-all; }
.chatlog .cm b { margin-right: 4px; }
.chatlog .cm.sys { opacity: 0.55; font-size: 13px; text-align: center; }
.chatlog .empty { opacity: 0.5; text-align: center; font-size: 13px; margin-top: 46px; }
.chatrow { margin: 6px 0 0; }
.chatrow input { font-size: 16px; padding: 8px 12px; }
.chatrow button { padding: 9px 14px; }

/* ---------- HUD ---------- */
#hud { position: fixed; inset: 0; pointer-events: none; }
#tags { position: absolute; inset: 0; overflow: hidden; }
.tag { position: absolute; left: 0; top: 0; font-size: 13px; background: rgba(255, 255, 255, 0.85); padding: 2px 8px; border-radius: 10px;
  border: 2px solid #fff; white-space: nowrap; will-change: transform; }
.hud-top { position: absolute; top: max(10px, env(safe-area-inset-top)); left: 10px; right: 10px; display: flex; gap: 10px; align-items: center; }
.pill { background: var(--card); border-radius: 16px; padding: 6px 14px; box-shadow: var(--shadow); font-size: 20px; }
.rank b { font-size: 34px; color: var(--pink); } .rank small { font-size: 16px; opacity: 0.6; }
.timer { font-variant-numeric: tabular-nums; min-width: 104px; text-align: center; }
.progress { flex: 1; height: 14px; background: rgba(255, 255, 255, 0.7); border-radius: 8px; position: relative; box-shadow: inset 0 2px 0 rgba(0, 0, 0, 0.08); margin-right: 18px; }
#progress { position: absolute; inset: 0; }
#progress i { position: absolute; top: 50%; width: 14px; height: 14px; border-radius: 50%; transform: translate(-50%, -50%); border: 2px solid #fff; transition: left 0.15s linear; }
#progress i.me { width: 22px; height: 22px; border-width: 3px; z-index: 2; box-shadow: 0 0 0 2px var(--ink); }
.progress .flag { position: absolute; right: -22px; top: 50%; transform: translateY(-55%); font-size: 18px; }
.board { position: absolute; top: calc(max(10px, env(safe-area-inset-top)) + 62px); left: 10px; font-size: 13px; display: flex; flex-direction: column; gap: 3px; }
.board div { background: rgba(255, 255, 255, 0.72); border-radius: 9px; padding: 2px 9px 2px 4px; display: flex; align-items: center; gap: 5px; width: max-content; }
.board div.me { background: #fff7d6; font-weight: bold; }
.board span { width: 18px; text-align: center; color: var(--pink); }
.board i { width: 10px; height: 10px; border-radius: 50%; display: inline-block; }
.exit, .mute { position: absolute; pointer-events: auto; top: calc(max(10px, env(safe-area-inset-top)) + 58px); right: 10px; padding: 6px 12px; font-size: 16px; }
.mute { right: 60px; }
.msg { position: absolute; top: 34%; left: 0; right: 0; text-align: center; font-size: 34px; color: #fff; text-shadow: 0 4px 0 var(--ink), 0 0 20px rgba(0, 0, 0, 0.2); transform: translateY(-50%); }
.msg.count { font-size: 110px; animation: pop 0.5s; color: var(--yellow); }
.msg.go { color: var(--pink); }
.msg.small { font-size: 28px; }
.subline { position: absolute; top: calc(max(10px, env(safe-area-inset-top)) + 64px); left: 50%; transform: translateX(-50%); font-size: 18px; background: rgba(255, 111, 145, 0.9); color: #fff; border-radius: 14px; padding: 4px 14px; }
.subline:empty { display: none; }
@keyframes pop { from { transform: translateY(-50%) scale(1.8); opacity: 0; } to { transform: translateY(-50%) scale(1); opacity: 1; } }
.toast { position: absolute; top: 22%; left: 50%; transform: translateX(-50%); font-size: 26px; background: rgba(58, 46, 92, 0.75); color: #fff; padding: 8px 22px; border-radius: 20px; opacity: 0; }
.toast.show { animation: toast 2.2s; }
@keyframes toast { 0% { opacity: 0; transform: translate(-50%, 20px); } 12% { opacity: 1; transform: translate(-50%, 0); } 80% { opacity: 1; } 100% { opacity: 0; } }
.hud-bottom { position: absolute; left: 50%; transform: translateX(-50%); bottom: max(16px, env(safe-area-inset-bottom)); display: flex; align-items: center; gap: 12px; }
.dash { width: 220px; height: 22px; background: rgba(255, 255, 255, 0.75); border-radius: 12px; position: relative; overflow: hidden; border: 3px solid #fff; box-shadow: var(--shadow); }
#dash { height: 100%; background: linear-gradient(90deg, var(--blue), var(--mint)); width: 100%; }
.dash.low #dash { background: var(--pink); }
.dash span { position: absolute; inset: 0; text-align: center; font-size: 13px; line-height: 16px; color: var(--ink); }
.itemslot { width: 62px; height: 62px; border-radius: 18px; background: var(--card); border: 3px solid #fff; box-shadow: var(--shadow); font-size: 36px; display: flex; align-items: center; justify-content: center; }
.itemslot.pop { animation: ipop 0.35s; }
@keyframes ipop { 0% { transform: scale(1.6) rotate(-15deg); } 100% { transform: scale(1); } }
.flash { position: absolute; inset: 0; background: #fff8b0; opacity: 0; }
.flash.on { animation: flash 0.45s; }
@keyframes flash { 0% { opacity: 0.85; } 100% { opacity: 0; } }

/* 터치 컨트롤 */
.touch { display: none; position: absolute; inset: 0; }
body.is-touch .touch { display: block; }
body.is-touch .hud-bottom { left: auto; transform: none; right: max(18px, env(safe-area-inset-right)); bottom: calc(max(22px, env(safe-area-inset-bottom)) + 190px); }
body.is-touch .itemslot { display: none; }
body.is-touch .dash { width: 150px; height: 18px; }
body.is-touch .dash span { line-height: 12px; font-size: 11px; }
body.is-touch .board { display: none; }
.joyzone { position: absolute; left: 0; bottom: 0; width: 55%; height: 75%; pointer-events: auto; }
.joy { position: absolute; width: 120px; height: 120px; border-radius: 50%; background: rgba(255, 255, 255, 0.35); border: 3px solid rgba(255, 255, 255, 0.8);
  transform: translate(-50%, -50%); left: 22%; top: 75%; opacity: 0.6; pointer-events: none; }
.joy.on { opacity: 1; }
.nub { position: absolute; left: 50%; top: 50%; width: 54px; height: 54px; border-radius: 50%; background: #fff; box-shadow: 0 3px 0 rgba(0, 0, 0, 0.15); transform: translate(-50%, -50%); }
.tbtn { position: absolute; pointer-events: auto; border-radius: 50%; font-size: 18px; display: flex; align-items: center; justify-content: center; opacity: 0.92; }
.tbtn.press { transform: scale(0.92); }
.tbtn.jump { width: 92px; height: 92px; right: max(18px, env(safe-area-inset-right)); bottom: max(22px, env(safe-area-inset-bottom)); background: var(--pink); color: #fff; box-shadow: 0 5px 0 #c2406b; font-size: 22px; }
.tbtn.dashb { width: 72px; height: 72px; right: calc(max(18px, env(safe-area-inset-right)) + 104px); bottom: max(26px, env(safe-area-inset-bottom)); background: var(--blue); color: #fff; box-shadow: 0 5px 0 #2a7ccc; }
.tbtn.item { width: 66px; height: 66px; right: max(30px, env(safe-area-inset-right)); bottom: calc(max(22px, env(safe-area-inset-bottom)) + 108px); font-size: 30px; }
.tbtn.item.has { background: var(--yellow); box-shadow: 0 5px 0 #c79a00; }

/* 결과 */
.results { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; pointer-events: auto; background: rgba(58, 46, 92, 0.25); padding: 16px; }
.res { display: flex; align-items: center; gap: 10px; background: #f6f2ff; border-radius: 14px; padding: 8px 12px; font-size: 18px; }
.res.me { background: #fff7d6; outline: 3px solid var(--yellow); }
.res .no { width: 34px; font-size: 22px; color: var(--pink); text-align: center; }
.res .av { width: 24px; height: 24px; border-radius: 50%; border: 2px solid #fff; }
.res .nm { flex: 1; }
.res .tm { font-variant-numeric: tabular-nums; opacity: 0.8; }
.err { position: fixed; left: 50%; bottom: 20px; transform: translateX(-50%); background: #d23b5c; color: #fff; padding: 10px 18px; border-radius: 14px; z-index: 10; max-width: 90vw; font-size: 15px; }
@media (max-height: 500px) {
  .msg.count { font-size: 72px; }
  .tbtn.jump { width: 78px; height: 78px; }
}
