:root {
  --sky-1: #7ed6f2;
  --sky-2: #a9e6ff;
  --turf-1: #7bc94a;
  --turf-2: #5faa33;
  --cream: #fff7e6;
  --ink: #2a1a0f;
  --accent: #ff5a3c;
  --accent-2: #ffc300;
  --pink: #ff4d94;
  --shadow: 0 6px 0 rgba(0,0,0,.22);
  --shadow-lg: 0 10px 0 rgba(0,0,0,.22);
  --radius: 20px;
  --border: 4px solid var(--ink);
  --font-disp: 'Jua', 'Pretendard Variable', sans-serif;
  --font-num: 'Fredoka', 'Pretendard Variable', sans-serif;
  --font-body: 'Pretendard Variable', Pretendard, -apple-system, sans-serif;
}

* { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; }

/* [hidden] 속성이 항상 우선하도록 (author display 규칙에 덮이는 문제 방지) */
[hidden] { display: none !important; }

html, body {
  height: 100%;
  font-family: var(--font-body);
  color: var(--ink);
  background: linear-gradient(180deg, var(--sky-1) 0%, var(--sky-2) 55%, #cdeeff 100%);
  overflow: hidden;
}

.grain {
  position: fixed; inset: 0; pointer-events: none; z-index: 999; opacity: .05;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='120' height='120'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='.9' numOctaves='3'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)'/%3E%3C/svg%3E");
}

.screen { position: fixed; inset: 0; display: none; overflow-y: auto; }
.screen.active { display: block; }

/* 버튼 */
.btn {
  font-family: var(--font-disp); font-size: 18px; color: var(--ink);
  border: var(--border); border-radius: 999px; padding: 12px 22px; cursor: pointer;
  background: #fff; box-shadow: var(--shadow); transition: transform .08s, box-shadow .08s;
  user-select: none; white-space: nowrap;
}
.btn:active { transform: translateY(4px); box-shadow: 0 2px 0 rgba(0,0,0,.22); }
.btn.big { font-size: 22px; padding: 16px 28px; width: 100%; }
.btn.small { font-size: 15px; padding: 8px 14px; box-shadow: 0 4px 0 rgba(0,0,0,.2); }
.btn-primary { background: var(--accent); color: #fff; }
.btn-accent { background: var(--accent-2); }
.btn-ghost { background: var(--cream); }
.btn:disabled { opacity: .5; cursor: not-allowed; filter: grayscale(.4); }

/* 카드 */
.card {
  background: var(--cream); border: var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow-lg); padding: 22px;
}

/* ===== 홈 ===== */
.home-wrap { min-height: 100%; display: flex; flex-direction: column; align-items: center;
  justify-content: center; gap: 22px; padding: 32px 20px; }
.title-block { text-align: center; }
.galloping { font-size: 54px; animation: bob 1.1s ease-in-out infinite; }
@keyframes bob { 0%,100%{transform:translateX(-6px) rotate(-3deg)} 50%{transform:translateX(6px) rotate(3deg)} }
.game-title {
  font-family: var(--font-disp); font-size: clamp(40px, 11vw, 76px); line-height: .95;
  color: #fff; text-shadow: 4px 4px 0 var(--ink), 8px 8px 0 rgba(0,0,0,.15); margin-top: 6px;
}
.game-title span { color: var(--accent-2); }
.subtitle { margin-top: 14px; font-size: 17px; color: var(--ink); background: rgba(255,255,255,.6);
  display: inline-block; padding: 6px 16px; border-radius: 999px; border: 2px solid rgba(0,0,0,.15); }

.home-card { width: min(440px, 100%); display: flex; flex-direction: column; gap: 16px; }
.fld { display: flex; flex-direction: column; gap: 6px; font-family: var(--font-disp); font-size: 16px; }
.fld input, .join-row input, .add-row input {
  font-family: var(--font-body); font-size: 18px; font-weight: 600;
  border: 3px solid var(--ink); border-radius: 14px; padding: 13px 16px; background: #fff; color: var(--ink);
  outline: none; width: 100%;
}
.fld input:focus, .join-row input:focus, .add-row input:focus { border-color: var(--accent); }
.divider { display: flex; align-items: center; gap: 10px; color: #8a7a66; font-size: 14px; }
.divider::before, .divider::after { content:''; flex:1; height: 2px; background: rgba(0,0,0,.12); }
.join-row { display: flex; gap: 10px; }
.join-row input { text-align: center; letter-spacing: 6px; font-family: var(--font-num); text-transform: uppercase; }
.err-msg { color: #d3341d; font-weight: 700; min-height: 18px; font-size: 14px; text-align: center; }
.foot { color: rgba(0,0,0,.5); font-size: 13px; text-align: center; }

/* ===== 로비 ===== */
.lobby-wrap { max-width: 1000px; margin: 0 auto; padding: 18px 16px 40px; }
.lobby-head { display: flex; align-items: center; justify-content: space-between; gap: 12px;
  flex-wrap: wrap; margin-bottom: 16px; }
.room-badge { display: flex; align-items: center; gap: 10px; background: var(--ink); color: #fff;
  border-radius: 999px; padding: 8px 18px; box-shadow: var(--shadow); }
.rb-label { font-size: 13px; opacity: .7; }
.rb-code { font-family: var(--font-num); font-size: 28px; font-weight: 700; letter-spacing: 4px; color: var(--accent-2); }
.head-actions { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.member-count { font-family: var(--font-disp); background: #fff; border: 2px solid var(--ink);
  padding: 6px 12px; border-radius: 999px; font-size: 14px; }

.lobby-grid { display: grid; grid-template-columns: 1.2fr 1fr; gap: 16px; align-items: start; }
@media (max-width: 780px) { .lobby-grid { grid-template-columns: 1fr; } }

.panel-title { font-family: var(--font-disp); font-size: 20px; display: flex; align-items: center;
  justify-content: space-between; margin-bottom: 14px; }
.host-tag { font-size: 12px; background: var(--accent); color: #fff; padding: 3px 10px; border-radius: 999px; display: none; }
body.is-host .host-tag { display: inline-block; }

.add-row { display: flex; gap: 8px; margin-bottom: 14px; }
.participant-list { display: flex; flex-wrap: wrap; gap: 8px; max-height: 46vh; overflow-y: auto; }
.p-chip { display: flex; align-items: center; gap: 8px; background: #fff; border: 3px solid var(--ink);
  border-radius: 999px; padding: 6px 8px 6px 12px; box-shadow: 0 3px 0 rgba(0,0,0,.18); animation: pop .2s ease; }
@keyframes pop { from { transform: scale(.6); opacity: 0; } to { transform: scale(1); opacity: 1; } }
.p-chip .dot { width: 16px; height: 16px; border-radius: 50%; border: 2px solid var(--ink); flex: none; }
.p-chip .p-emoji { font-size: 18px; }
.p-chip .p-name { font-weight: 700; font-size: 15px; max-width: 120px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.p-chip .p-weight { font-family: var(--font-num); font-size: 13px; background: var(--accent-2);
  border: 2px solid var(--ink); border-radius: 999px; padding: 0 7px; display: none; }
body.use-weights .p-chip .p-weight { display: inline-block; }
.p-chip .wbtn { display: none; width: 20px; height: 20px; border-radius: 50%; border: 2px solid var(--ink);
  background: #fff; font-weight: 800; cursor: pointer; line-height: 1; padding: 0; }
body.use-weights.is-host .p-chip .wbtn { display: inline-flex; align-items: center; justify-content: center; }
.p-chip .x { border: none; background: none; cursor: pointer; font-size: 16px; color: #b33; padding: 0 4px; font-weight: 800; }

/* 설정 */
.setting { display: flex; align-items: center; justify-content: space-between; gap: 10px;
  padding: 11px 0; border-bottom: 2px dashed rgba(0,0,0,.12); flex-wrap: wrap; }
.setting:last-of-type { border-bottom: none; }
.s-label { font-family: var(--font-disp); font-size: 16px; }
.seg { display: flex; background: #fff; border: 3px solid var(--ink); border-radius: 999px; overflow: hidden; }
.seg-btn { font-family: var(--font-disp); border: none; background: none; padding: 8px 14px; cursor: pointer; font-size: 15px; }
.seg-btn.active { background: var(--accent); color: #fff; }
.stepper { display: flex; align-items: center; gap: 10px; font-family: var(--font-num); font-size: 20px; }
.st-btn { width: 34px; height: 34px; border-radius: 50%; border: 3px solid var(--ink); background: #fff;
  font-size: 20px; font-weight: 800; cursor: pointer; line-height: 1; }
.st-suffix { font-size: 15px; }
body:not(.is-host) .seg-btn, body:not(.is-host) .st-btn,
body:not(.is-host) .switch input { pointer-events: none; opacity: .8; }

.switch { position: relative; width: 54px; height: 30px; }
.switch input { opacity: 0; width: 0; height: 0; }
.slider { position: absolute; inset: 0; background: #ccc; border: 3px solid var(--ink); border-radius: 999px; transition: .2s; }
.slider::before { content:''; position: absolute; width: 20px; height: 20px; left: 3px; top: 3px;
  background: #fff; border: 2px solid var(--ink); border-radius: 50%; transition: .2s; }
.switch input:checked + .slider { background: var(--turf-1); }
.switch input:checked + .slider::before { transform: translateX(24px); }

.hint { font-size: 13px; color: #8a7a66; margin-top: 8px; }
.hint.center { text-align: center; }
.start-btn, .ready-btn { margin-top: 16px; }
.ready-btn.is-ready { background: var(--turf-1); color: #fff; }

/* 링크 입장 뷰 */
.join-room-badge { font-family: var(--font-disp); font-size: 20px; text-align: center;
  background: var(--ink); color: #fff; border-radius: 14px; padding: 10px; margin-bottom: 4px; }
.join-room-badge b { color: var(--accent-2); font-family: var(--font-num); letter-spacing: 3px; }
#btn-back-home { align-self: center; }

/* 방장 전용 안내 + 잠금 */
.notice { background: #fff3d6; border: 2px solid var(--ink); border-radius: 12px;
  padding: 10px 12px; font-size: 14px; font-weight: 700; margin-bottom: 10px; }
.settings-body.locked { opacity: .65; pointer-events: none; filter: grayscale(.2); }

/* 참가자 태그 */
.ready-count { font-size: 13px; background: var(--ink); color: #fff; padding: 3px 10px; border-radius: 999px; }
.p-chip.me { border-color: var(--accent); box-shadow: 0 0 0 2px rgba(255,90,60,.25), 0 3px 0 rgba(0,0,0,.18); }
.p-chip.ready { background: #eafbe6; }
.tag-host { font-size: 11px; background: var(--accent); color: #fff; padding: 1px 7px; border-radius: 999px; }
.tag-me { font-size: 11px; background: var(--accent-2); padding: 1px 7px; border-radius: 999px; }
.ready-badge { font-size: 14px; }
.w-rank { margin-left: auto; font-family: var(--font-num); color: #8a7a66; }
.fr-title { font-family: var(--font-disp); }

/* ===== 레이스 ===== */
#screen-race { background: linear-gradient(180deg, var(--sky-1), var(--sky-2)); }
.race-topbar { position: fixed; top: 0; left: 0; right: 0; height: 52px; z-index: 5;
  display: flex; align-items: center; justify-content: space-between; padding: 0 18px;
  background: rgba(42,26,15,.85); color: #fff; font-family: var(--font-disp); }
.race-status { font-size: 17px; }
.race-timer { font-family: var(--font-num); font-size: 20px; color: var(--accent-2); }
#race-canvas { position: fixed; top: 52px; left: 0; width: 100vw; height: calc(100vh - 52px); display: block; }

.countdown { position: fixed; inset: 0; z-index: 20; display: flex; align-items: center; justify-content: center;
  background: rgba(0,0,0,.35); }
.countdown span { font-family: var(--font-disp); font-size: 30vh; color: #fff;
  text-shadow: 6px 6px 0 var(--accent); animation: cd .95s ease-in-out; }
@keyframes cd { 0% { transform: scale(2.4); opacity: 0; } 40% { transform: scale(1); opacity: 1; } 100% { transform: scale(.4); opacity: 0; } }

/* 결과 */
.result-modal { position: fixed; inset: 0; z-index: 30; display: flex; align-items: center; justify-content: center;
  background: rgba(0,0,0,.5); padding: 20px; }
.result-card { position: relative; background: var(--cream); border: var(--border); border-radius: 26px;
  box-shadow: var(--shadow-lg); padding: 26px 24px; width: min(460px, 100%); text-align: center; overflow: hidden;
  animation: pop .3s ease; max-height: 92vh; display: flex; flex-direction: column; }
.trophy { font-size: 66px; animation: bob 1s ease-in-out infinite; }
.result-title { font-family: var(--font-disp); font-size: 34px; margin: 4px 0 16px; color: var(--accent); }
.winner-list { display: flex; flex-direction: column; gap: 10px; margin-bottom: 14px; max-height: 34vh; overflow-y: auto; }
.winner-item { display: flex; align-items: center; gap: 12px; background: #fff; border: 3px solid var(--ink);
  border-radius: 16px; padding: 12px 16px; box-shadow: 0 4px 0 rgba(0,0,0,.18); }
.winner-item .medal { font-size: 26px; }
.winner-item .w-emoji { font-size: 26px; }
.winner-item .w-name { font-family: var(--font-disp); font-size: 22px; }
.result-actions { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; }
.full-rank { margin-top: 12px; text-align: left; border-top: 2px dashed rgba(0,0,0,.2); padding-top: 12px;
  max-height: 32vh; overflow-y: auto; }
.rank-row { display: flex; align-items: center; gap: 10px; padding: 6px 4px; font-size: 15px; }
.rank-row .rank-no { font-family: var(--font-num); font-weight: 700; width: 30px; }
.rank-row .rank-dot { width: 12px; height: 12px; border-radius: 50%; border: 2px solid var(--ink); }

.confetti { position: fixed; inset: 0; pointer-events: none; z-index: 31; overflow: hidden; }
.confetti i { position: absolute; width: 10px; height: 14px; top: -20px; opacity: .9;
  animation: fall linear forwards; }
@keyframes fall { to { transform: translateY(120vh) rotate(720deg); } }

/* 토스트 */
.toast { position: fixed; bottom: 26px; left: 50%; transform: translateX(-50%); z-index: 50;
  background: var(--ink); color: #fff; padding: 12px 20px; border-radius: 999px; font-weight: 700;
  box-shadow: var(--shadow); animation: pop .2s ease; }

/* ===== 채팅 ===== */
.chat { position: fixed; left: 12px; bottom: 12px; z-index: 25; width: 280px; max-width: calc(100vw - 24px);
  background: rgba(255,247,230,.96); border: 3px solid var(--ink); border-radius: 16px;
  box-shadow: var(--shadow); overflow: hidden; font-family: var(--font-body); }
.chat-head { background: var(--ink); color: #fff; font-family: var(--font-disp); padding: 6px 12px;
  cursor: pointer; font-size: 15px; display: flex; justify-content: space-between; align-items: center; }
.chat.collapsed .chat-body { display: none; }
.chat-log { height: 150px; overflow-y: auto; padding: 8px 10px; display: flex; flex-direction: column;
  gap: 4px; font-size: 13px; }
.chat-msg { line-height: 1.35; word-break: break-word; }
.chat-form { display: flex; gap: 6px; padding: 8px; border-top: 2px solid rgba(0,0,0,.1); }
.chat-form input { flex: 1; min-width: 0; border: 2px solid var(--ink); border-radius: 10px;
  padding: 7px 10px; font-size: 14px; font-family: var(--font-body); outline: none; }
.chat-form input:focus { border-color: var(--accent); }
.chat-send { border: 2px solid var(--ink); background: var(--accent-2); border-radius: 10px;
  padding: 0 12px; font-family: var(--font-disp); cursor: pointer; font-size: 13px; }

/* ===== 아이템 버튼 ===== */
.btn-item { position: fixed; right: 18px; bottom: 18px; z-index: 24; font-family: var(--font-disp);
  font-size: 22px; color: #fff; background: var(--pink); border: 4px solid var(--ink);
  border-radius: 999px; padding: 12px 24px; cursor: pointer; box-shadow: var(--shadow);
  display: flex; align-items: center; gap: 8px; animation: itembob .8s ease-in-out infinite; }
.btn-item span { font-size: 17px; }
.btn-item:disabled { opacity: .6; }
@keyframes itembob { 0%,100% { transform: translateY(0) scale(1) rotate(-3deg); } 50% { transform: translateY(-6px) scale(1.06) rotate(3deg); } }
