/* ============ 기본 ============ */
:root {
  --bg: #0c1222;
  --card: #1a2337;
  --card-border: #2a3550;
  --text: #f1f5f9;
  --muted: #8b9bb4;
}

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

html, body { height: 100%; }

body {
  background: var(--bg);
  color: var(--text);
  font-family: 'Jua', -apple-system, 'Apple SD Gothic Neo', 'Malgun Gothic', sans-serif;
  touch-action: manipulation;
  overscroll-behavior: none;
}

button { font-family: inherit; cursor: pointer; }
input { font-family: inherit; }

.hidden { display: none !important; }

.screen { min-height: 100dvh; display: flex; flex-direction: column; }

/* ============ 팀 선택 화면 ============ */
#select-screen {
  justify-content: center;
  align-items: center;
  gap: 14px;
  padding: 28px 20px calc(28px + env(safe-area-inset-bottom));
  text-align: center;
}

.logo { font-size: 36px; letter-spacing: -1px; }
.tagline { color: var(--muted); font-size: 15px; margin-bottom: 10px; }

.team-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  width: 100%;
  max-width: 420px;
}

.team-card {
  aspect-ratio: 1.55;
  border: none;
  border-radius: 18px;
  background: var(--team-color);
  color: #fff;
  font-size: 23px;
  transition: transform .15s, opacity .15s, box-shadow .15s;
  text-shadow: 0 1px 3px rgb(0 0 0 / .25);
}
.team-grid.has-sel .team-card:not(.selected) { opacity: .35; transform: scale(.95); }
.team-card.selected {
  transform: scale(1.03);
  box-shadow: 0 0 0 3px #fff, 0 10px 26px -8px var(--team-color);
}

#nickname {
  width: 100%;
  max-width: 420px;
  padding: 14px 16px;
  border-radius: 14px;
  border: 1.5px solid var(--card-border);
  background: var(--card);
  color: var(--text);
  font-size: 16px;
  text-align: center;
  outline: none;
  margin-top: 6px;
}
#nickname:focus { border-color: #4a5b80; }
#nickname::placeholder { color: #5b6b8a; }

.primary-btn {
  width: 100%;
  max-width: 420px;
  padding: 16px;
  border: none;
  border-radius: 14px;
  background: #2c3a5c;
  color: #fff;
  font-size: 19px;
  transition: transform .1s, background .2s;
}
.primary-btn:not(:disabled):active { transform: scale(.97); }
.primary-btn:disabled { opacity: .5; }

/* ============ 게임 화면 ============ */
#game-header {
  position: sticky;
  top: 0;
  z-index: 5;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: calc(10px + env(safe-area-inset-top)) 14px 10px;
  background: rgb(12 18 34 / .92);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
}

#score-bar { display: flex; gap: 7px; }

.chip {
  min-width: 46px;
  height: 36px;
  padding: 0 10px;
  border-radius: 11px;
  background: var(--team-color);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 17px;
  position: relative;
  text-shadow: 0 1px 2px rgb(0 0 0 / .3);
}
.chip.mine { box-shadow: 0 0 0 2.5px #fff; }
.chip.mine::after {
  content: '나';
  position: absolute;
  top: -8px;
  right: -5px;
  background: #fff;
  color: #111;
  font-size: 10px;
  line-height: 1;
  padding: 3px 5px;
  border-radius: 7px;
}
.chip.lead::before {
  content: '👑';
  position: absolute;
  top: -13px;
  left: -6px;
  font-size: 13px;
  transform: rotate(-20deg);
}

.header-right { display: flex; align-items: center; gap: 7px; }
#progress { color: var(--muted); font-size: 13px; }

.icon-btn {
  background: var(--card);
  border: 1px solid var(--card-border);
  color: var(--text);
  border-radius: 10px;
  padding: 7px 10px;
  font-size: 13px;
  white-space: nowrap;
}

/* ---- 퀴즈 판 ---- */
#board {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 9px;
  width: 100%;
  max-width: 520px;
  margin: 0 auto;
  padding: 14px 14px calc(24px + env(safe-area-inset-bottom));
}

.tile {
  aspect-ratio: 1;
  border-radius: 16px;
  border: 1.5px solid var(--card-border);
  background: var(--card);
  color: var(--muted);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  transition: transform .1s;
}
.tile:active { transform: scale(.93); }
.tile-num { font-size: 25px; }

.tile.claimed {
  background: var(--team-color);
  border-color: transparent;
  color: #fff;
  box-shadow: 0 5px 16px -7px var(--team-color);
  text-shadow: 0 1px 2px rgb(0 0 0 / .3);
}
.tile.claimed .tile-owner {
  font-size: 10px;
  opacity: .95;
  max-width: 92%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.tile.flip-in { animation: flipIn .55s ease; }
@keyframes flipIn {
  0%   { transform: perspective(500px) rotateY(180deg); }
  100% { transform: perspective(500px) rotateY(0deg); }
}

/* ============ 문제 시트 ============ */
#sheet-backdrop {
  position: fixed;
  inset: 0;
  z-index: 20;
  background: rgb(0 0 0 / .55);
  display: flex;
  align-items: flex-end;
}

#sheet {
  width: 100%;
  max-width: 560px;
  margin: 0 auto;
  background: #141c30;
  border-radius: 22px 22px 0 0;
  padding: 18px 18px calc(20px + env(safe-area-inset-bottom));
  max-height: 85dvh;
  overflow: auto;
  animation: slideUp .25s ease;
}
@keyframes slideUp {
  from { transform: translateY(45%); opacity: .4; }
}
#sheet.shake { animation: shake .4s; }
@keyframes shake {
  0%, 100% { transform: translateX(0); }
  20% { transform: translateX(-9px); }
  40% { transform: translateX(9px); }
  60% { transform: translateX(-6px); }
  80% { transform: translateX(6px); }
}

.sheet-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 12px; }
#sheet-num {
  color: var(--muted);
  font-size: 14px;
  background: var(--card);
  padding: 5px 11px;
  border-radius: 9px;
}
#sheet-close {
  background: none;
  border: none;
  color: var(--muted);
  font-size: 21px;
  padding: 4px 8px;
}

#sheet-question { font-size: 22px; line-height: 1.45; margin: 4px 2px 18px; }

#answer-area { display: flex; gap: 8px; }
#answer-input {
  flex: 1;
  min-width: 0;
  padding: 14px 15px;
  border-radius: 13px;
  border: 1.5px solid var(--card-border);
  background: var(--card);
  color: var(--text);
  font-size: 16px;
  outline: none;
}
#answer-input:focus { border-color: #4a5b80; }
#submit-btn {
  border: none;
  border-radius: 13px;
  padding: 0 20px;
  background: var(--my-color, #2c3a5c);
  color: #fff;
  font-size: 17px;
  white-space: nowrap;
  transition: transform .1s;
  text-shadow: 0 1px 2px rgb(0 0 0 / .25);
}
#submit-btn:not(:disabled):active { transform: scale(.95); }
#submit-btn:disabled { opacity: .55; }

#sheet-msg { margin-top: 12px; font-size: 16px; min-height: 20px; }
#sheet-msg.good { color: #4ade80; }
#sheet-msg.bad { color: #f87171; }

#claim-info { display: flex; flex-direction: column; gap: 10px; }
.claim-badge {
  background: var(--team-color);
  color: #fff;
  border-radius: 12px;
  padding: 12px 14px;
  font-size: 17px;
  text-shadow: 0 1px 2px rgb(0 0 0 / .3);
}
.answer-reveal {
  background: var(--card);
  border-radius: 12px;
  padding: 12px 14px;
  font-size: 17px;
  color: #a5f3b4;
}

/* ============ 토스트 ============ */
#toast {
  position: fixed;
  top: calc(14px + env(safe-area-inset-top));
  left: 50%;
  transform: translateX(-50%);
  z-index: 30;
  background: rgb(31 42 68 / .96);
  border: 1px solid var(--card-border);
  padding: 10px 17px;
  border-radius: 999px;
  font-size: 14px;
  display: flex;
  align-items: center;
  gap: 8px;
  max-width: 92vw;
  white-space: nowrap;
  overflow: hidden;
  box-shadow: 0 8px 22px -8px rgb(0 0 0 / .6);
  animation: dropIn .25s ease;
}
@keyframes dropIn {
  from { transform: translate(-50%, -18px); opacity: 0; }
}
#toast .dot { width: 11px; height: 11px; border-radius: 50%; flex: none; }

/* ============ 게임 종료 ============ */
#win-overlay {
  position: fixed;
  inset: 0;
  z-index: 40;
  background: rgb(8 12 24 / .85);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}
#confetti { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; }

#win-card {
  position: relative;
  background: #161f36;
  border: 1px solid var(--card-border);
  border-radius: 26px;
  padding: 26px 22px;
  width: 100%;
  max-width: 380px;
  text-align: center;
  display: flex;
  flex-direction: column;
  gap: 16px;
  animation: popIn .35s cubic-bezier(.2, 1.4, .4, 1);
}
@keyframes popIn {
  from { transform: scale(.7); opacity: 0; }
}
#win-trophy { font-size: 52px; }
#win-title { font-size: 27px; }

#win-ranks { display: flex; flex-direction: column; gap: 8px; }
.rank-row {
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--card);
  border-radius: 13px;
  padding: 10px 14px;
  font-size: 17px;
}
.rank-row .medal { width: 26px; font-size: 18px; }
.rank-row .dot { width: 14px; height: 14px; border-radius: 5px; flex: none; }
.rank-row .name { flex: 1; text-align: left; }
.rank-row .score { color: var(--muted); }
.rank-row.top { box-shadow: inset 0 0 0 1.5px var(--rank-color); }

.win-note { color: var(--muted); font-size: 12px; }

/* ============ 대기 화면 (게임 닫힘) ============ */
#wait-screen {
  justify-content: center;
  align-items: center;
  gap: 14px;
  padding: 28px 22px;
  text-align: center;
}
.wait-emoji { font-size: 56px; animation: waitPulse 2s ease-in-out infinite; }
@keyframes waitPulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.12); }
}
#wait-screen h2 { font-size: 24px; }
#wait-screen p { color: var(--muted); font-size: 15px; }
.wait-chip {
  margin-top: 10px;
  padding: 10px 18px;
  border-radius: 999px;
  color: #fff;
  font-size: 16px;
  text-shadow: 0 1px 2px rgb(0 0 0 / .3);
}

/* ============ 연결 끊김 배너 ============ */
#offline-banner {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 50;
  background: #b45309;
  color: #fff;
  text-align: center;
  font-size: 13px;
  padding: calc(6px + env(safe-area-inset-top)) 10px 6px;
}

/* ============ 호스트 화면 ============ */
.host-body { padding: 26px 18px 40px; }

#host-wrap {
  max-width: 620px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 20px;
  text-align: center;
}
.host-title { font-size: 32px; }
.host-tag {
  font-size: 13px;
  vertical-align: middle;
  background: var(--card);
  border: 1px solid var(--card-border);
  color: var(--muted);
  border-radius: 8px;
  padding: 4px 8px;
  margin-left: 6px;
}
#host-progress { color: var(--muted); font-size: 15px; }
#host-winner { font-size: 24px; color: #fbbf24; }

#host-scores {
  display: flex;
  justify-content: center;
  gap: 12px;
}
.host-chip {
  min-width: 68px;
  height: 54px;
  padding: 0 16px;
  border-radius: 15px;
  background: var(--team-color);
  color: #fff;
  font-size: 26px;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  text-shadow: 0 1px 2px rgb(0 0 0 / .3);
}
.host-chip.lead::before {
  content: '👑';
  position: absolute;
  top: -15px;
  left: -5px;
  font-size: 17px;
  transform: rotate(-20deg);
}

#host-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  width: 100%;
}
#host-grid .tile { border-radius: 12px; }
#host-grid .tile-num { font-size: 22px; }

#host-actions {
  display: flex;
  gap: 10px;
  width: 100%;
}

#qr-btn {
  flex: 1;
  border: none;
  border-radius: 14px;
  padding: 15px;
  background: #2c3a5c;
  color: #fff;
  font-size: 17px;
  transition: transform .1s;
}
#qr-btn:active { transform: scale(.97); }

#qr-overlay {
  position: fixed;
  inset: 0;
  z-index: 60;
  background: rgb(8 12 24 / .93);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 18px;
  padding: 24px;
  cursor: pointer;
}
.qr-label { color: var(--text); font-size: 17px; text-align: center; }
.qr-card { background: #fff; border-radius: 22px; padding: 16px; }
.qr-card img, .qr-card canvas { display: block; width: min(72vw, 55vh, 440px) !important; height: auto !important; }
#qr-overlay-url {
  font-size: clamp(20px, 4vw, 32px);
  color: #7dd3fc;
  word-break: break-all;
  text-align: center;
}
.qr-hint { color: var(--muted); font-size: 14px; }

#reset-btn {
  flex: 1;
  padding: 15px;
  border: none;
  border-radius: 14px;
  background: #2c3a5c;
  color: #fff;
  font-size: 17px;
  transition: background .2s, transform .1s;
}
#reset-btn:active { transform: scale(.98); }
#reset-btn.danger { background: #dc2626; }
.host-note { color: var(--muted); font-size: 12px; }

#admin-link {
  color: var(--muted);
  font-size: 13px;
  text-decoration: none;
  padding: 8px 14px;
  border: 1px solid var(--card-border);
  border-radius: 10px;
}
#admin-link:active { color: var(--text); }

/* ============ 관리자 화면 ============ */
.admin-body { padding: 26px 16px 60px; }

#admin-wrap {
  max-width: 560px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.admin-card {
  background: var(--card);
  border: 1px solid var(--card-border);
  border-radius: 18px;
  padding: 18px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

#admin-login {
  max-width: 360px;
  margin: 12vh auto 0;
  text-align: center;
}
.admin-lock { font-size: 44px; }
.admin-title { font-size: 25px; text-align: center; }

.admin-input {
  width: 100%;
  padding: 13px 14px;
  border-radius: 12px;
  border: 1.5px solid var(--card-border);
  background: #0e1526;
  color: var(--text);
  font-size: 16px;
  font-family: inherit;
  outline: none;
}
.admin-input:focus { border-color: #4a5b80; }
.admin-input::placeholder { color: #5b6b8a; }

.admin-btn {
  border: none;
  border-radius: 12px;
  padding: 13px 18px;
  background: #2c3a5c;
  color: #fff;
  font-size: 16px;
  font-family: inherit;
  transition: transform .1s, background .2s;
}
.admin-btn:not(:disabled):active { transform: scale(.97); }
.admin-btn:disabled { opacity: .5; }
.admin-btn.primary { background: #3b82f6; }
.admin-btn.danger { background: #dc2626; }
.admin-btn.small { padding: 8px 14px; font-size: 14px; border-radius: 10px; }

.status-row { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }

#group-tabs { display: flex; gap: 8px; flex-wrap: wrap; }
.group-tab {
  border: 1.5px solid var(--card-border);
  background: #0e1526;
  color: var(--text);
  border-radius: 11px;
  padding: 9px 14px;
  font-size: 15px;
  font-family: inherit;
  transition: transform .1s;
}
.group-tab:active { transform: scale(.96); }
.group-tab.sel { border-color: #3b82f6; box-shadow: 0 0 0 1.5px #3b82f6; }
.group-tab.playing { background: #14532d; color: #86efac; }
.group-tab.add { color: var(--muted); border-style: dashed; }
.status-badge {
  font-size: 16px;
  padding: 8px 14px;
  border-radius: 999px;
  background: #14532d;
  color: #86efac;
}
.status-badge.closed { background: #58151c; color: #fda4af; }
.status-progress { color: var(--muted); font-size: 14px; }

.field-label { color: var(--muted); font-size: 13px; }

#q-list { display: flex; flex-direction: column; gap: 9px; }
.q-row {
  display: flex;
  flex-direction: column;
  gap: 8px;
  background: #0e1526;
  border: 1px solid var(--card-border);
  border-radius: 13px;
  padding: 12px;
}
.q-head { display: flex; gap: 9px; align-items: baseline; }
.q-num {
  color: var(--muted);
  font-size: 13px;
  flex: none;
  background: var(--card);
  padding: 3px 9px;
  border-radius: 7px;
}
.q-text { font-size: 16px; flex: 1; line-height: 1.4; }
.q-answers { color: #86efac; font-size: 13px; }
.q-actions { display: flex; gap: 8px; justify-content: flex-end; }

.admin-note { color: var(--muted); font-size: 12px; line-height: 1.5; }
.admin-msg { min-height: 18px; font-size: 14px; text-align: center; }
.admin-msg.bad { color: #f87171; }
.admin-msg.good { color: #4ade80; }

.shake-it { animation: shake .4s; }
