:root {
  --bg: #e6f3ff;
  --card: rgba(255, 255, 255, .88);
  --card-solid: #ffffff;
  --field-bg: #f1f8ff;
  --line: rgba(31, 139, 255, .2);
  --text: #0d2a4d;
  --muted: #56779c;
  --me: #1f8bff;
  --op: #f0437a;
  --ok: #1f8bff;
  --ok-2: #00b4ff;
  --warn: #f59e0b;
  --ready: #22c55e;
}

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

html, body {
  margin: 0;
  height: 100%;
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Apple SD Gothic Neo", "Malgun Gothic", "Noto Sans KR", sans-serif;
  overscroll-behavior: none;
}

/* 밝은 파란 하늘 */
body {
  background:
    radial-gradient(ellipse 60% 40% at 20% 0%, rgba(255, 255, 255, .9), transparent 70%),
    radial-gradient(ellipse 50% 35% at 85% 10%, rgba(255, 255, 255, .6), transparent 70%),
    linear-gradient(180deg, #f4faff 0%, #dcefff 45%, #b9ddff 100%);
  background-color: var(--bg);
  background-attachment: fixed;
}

button { font: inherit; color: inherit; cursor: pointer; }
input { font: inherit; }
.hidden { display: none !important; }

.screen {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: max(16px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right)) max(16px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
  overflow-y: auto;
}

.card {
  width: 100%;
  max-width: 420px;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 20px;
  padding: 24px 20px;
  box-shadow: 0 18px 50px rgba(31, 110, 200, .18);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  margin: auto;
}
.card.center { text-align: center; }

.title-row { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
h1 { margin: 0 0 4px; font-size: 28px; }
.title-text {
  background: linear-gradient(90deg, #1f8bff, #00b4ff);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}
h2 { margin: 0 0 8px; font-size: 32px; }
.logo { display: inline-block; animation: spin 6s linear infinite; }
.sub { color: var(--muted); margin: 6px 0 14px; font-size: 14px; }

.field { display: block; margin-bottom: 14px; }
.field > span { display: block; font-size: 13px; color: var(--muted); margin-bottom: 6px; font-weight: 600; }
.field input {
  width: 100%;
  padding: 12px 14px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: var(--field-bg);
  color: var(--text);
  font-size: 16px; /* iOS 확대 방지 */
  outline: none;
}
.field input::placeholder { color: #93acc8; }
.field input:focus { border-color: var(--ok); box-shadow: 0 0 0 3px rgba(31, 139, 255, .18); background: #fff; }

.row { display: flex; gap: 8px; align-items: center; }
.row.gap { justify-content: center; margin-bottom: 8px; }

.tabs {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  background: var(--field-bg);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 4px;
  margin-bottom: 14px;
}
.tab {
  border: 0;
  background: transparent;
  padding: 10px 4px;
  white-space: nowrap;
  border-radius: 9px;
  color: var(--muted);
  font-weight: 700;
}
.tab.active { background: #fff; color: var(--ok); box-shadow: 0 2px 8px rgba(31, 110, 200, .15); }

.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chips button {
  flex: 1 0 auto;
  min-width: 56px;
  padding: 10px 8px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--field-bg);
  font-weight: 600;
}
.chips button.active { border-color: var(--ok); background: #fff; color: var(--ok); box-shadow: 0 0 0 2px rgba(31, 139, 255, .15); }

.primary {
  width: 100%;
  padding: 14px;
  border: 0;
  border-radius: 14px;
  background: linear-gradient(135deg, var(--ok-2), var(--ok));
  color: #fff;
  font-size: 17px;
  font-weight: 800;
  margin-top: 4px;
  box-shadow: 0 8px 22px rgba(31, 139, 255, .32);
}
.primary:active { transform: scale(.98); }
.primary:disabled { opacity: .5; }

.ghost {
  padding: 10px 16px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: #fff;
  color: var(--ok);
  font-weight: 700;
  margin-top: 8px;
}

.icon-btn {
  flex: none;
  width: 48px;
  height: 48px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: #fff;
  font-size: 20px;
}
.icon-btn.small { width: 36px; height: 36px; font-size: 14px; }

.help-link {
  flex: none;
  border: 1px solid var(--line);
  background: #fff;
  color: var(--ok);
  border-radius: 999px;
  padding: 6px 12px;
  font-size: 13px;
  font-weight: 800;
}

.rules { margin-top: 18px; font-size: 14px; color: var(--muted); }
.rules summary { cursor: pointer; color: var(--text); font-weight: 700; }
.rules ol { padding-left: 20px; line-height: 1.6; }
.rules b { color: var(--text); }

.conn { margin-top: 14px; font-size: 12px; color: var(--muted); text-align: center; }
.conn.ok::before { content: "● "; color: var(--ready); }
.conn.bad::before { content: "● "; color: var(--op); }

.big-code {
  font-size: 44px;
  font-weight: 900;
  letter-spacing: 6px;
  margin: 4px 0 10px;
  word-break: break-all;
  color: var(--ok);
}

.spinner {
  width: 40px;
  height: 40px;
  margin: 18px auto 8px;
  border: 4px solid rgba(31, 139, 255, .15);
  border-top-color: var(--ok);
  border-radius: 50%;
  animation: spin 1s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---- 조작법 ---- */
.help-card { max-width: 480px; max-height: calc(100dvh - 32px); overflow-y: auto; }
.help-card h2 { font-size: 24px; text-align: center; }
.help-figure {
  display: block;
  width: 100%;
  height: auto;
  margin: 4px 0 8px;
  border-radius: 14px;
  background: radial-gradient(ellipse 90% 120% at 30% -30%, rgba(255, 255, 255, .35), transparent 70%), linear-gradient(180deg, #4a9448, #3f7d3c);
}
.help-steps { list-style: none; padding: 0; margin: 0 0 12px; }
.help-steps li {
  display: grid;
  grid-template-columns: 34px 1fr;
  gap: 10px;
  align-items: start;
  padding: 8px 0;
  border-bottom: 1px solid var(--line);
  font-size: 13.5px;
  line-height: 1.5;
}
.help-steps li:last-child { border-bottom: 0; }
.help-steps .ico {
  width: 34px;
  height: 34px;
  border-radius: 10px;
  display: grid;
  place-items: center;
  background: rgba(31, 139, 255, .1);
  font-size: 17px;
}
.help-steps b { color: var(--text); }
.help-steps .me { color: var(--me); font-weight: 700; }
.help-steps .op { color: var(--op); font-weight: 700; }
.help-steps .warn { color: var(--warn); font-weight: 700; }
.sci-title { margin: 6px 0 2px; font-size: 16px; color: var(--ok); }
.sci-steps .ico { background: rgba(0, 180, 255, .14); }
.device { display: inline-block; font-size: 12px; padding: 1px 7px; border-radius: 6px; background: rgba(31, 139, 255, .1); color: var(--ok); font-weight: 700; margin-right: 4px; }

/* ---- 게임 화면 ---- */
#game {
  flex-direction: column;
  align-items: stretch;
  justify-content: stretch;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
  overflow: hidden;
}

#hud {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  background: linear-gradient(180deg, rgba(255, 255, 255, .85), rgba(255, 255, 255, 0));
}
.side { display: flex; align-items: center; gap: 10px; min-width: 0; }
.side.op { justify-content: flex-end; }
.pname { font-weight: 800; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 15px; }
.me .pname, .me .score { color: var(--me); }
.op .pname, .op .score { color: var(--op); }
.score { font-size: 32px; font-weight: 900; line-height: 1; }
.mid { text-align: center; min-width: 0; }
#timer { font-size: 26px; font-weight: 900; font-variant-numeric: tabular-nums; color: var(--text); }
#timer.urgent { color: var(--op); animation: pulse .5s ease-in-out infinite alternate; }
@keyframes pulse { to { transform: scale(1.12); } }
.wind { font-size: 11px; color: var(--muted); }
.sci { font-size: 11px; color: var(--ok); font-weight: 700; white-space: nowrap; }

#stage { flex: 1; min-height: 0; position: relative; }
#canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  display: block;
}

#cdbar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 12px 10px;
  background: linear-gradient(0deg, rgba(255, 255, 255, .88), rgba(255, 255, 255, 0));
}
.cd { flex: 1; min-width: 0; }
.cd.op { flex: .6; opacity: .9; }
.cd-label { font-size: 12px; font-weight: 700; margin-bottom: 4px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.bar { height: 10px; border-radius: 6px; background: #d6e8fa; overflow: hidden; border: 1px solid var(--line); }
.fill { height: 100%; width: 100%; background: var(--ready); transition: background .2s; }
.cd.op .fill { background: var(--op); }
.fill.cooling { background: var(--me); }
.fill.penalty { background: var(--warn); }

.overlay {
  position: absolute;
  inset: 0;
  background: rgba(190, 220, 250, .55);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  z-index: 50;
}
.overlay.fixed { position: fixed; }
.result-score { font-size: 48px; font-weight: 900; margin: 4px 0; }
.result-score #rMe { color: var(--me); }
.result-score #rOp { color: var(--op); }

.toast {
  position: fixed;
  left: 50%;
  bottom: max(80px, calc(env(safe-area-inset-bottom) + 70px));
  transform: translateX(-50%);
  background: var(--card-solid);
  border: 1px solid var(--line);
  color: var(--text);
  font-weight: 600;
  padding: 12px 18px;
  border-radius: 12px;
  z-index: 100;
  max-width: calc(100% - 32px);
  text-align: center;
  box-shadow: 0 10px 30px rgba(31, 110, 200, .2);
}
.toast.warn { background: #fff7e6; border-color: var(--warn); color: #8a5300; }

/* 가로로 짧은 화면(가로 모드 휴대폰) */
@media (max-height: 480px) {
  #hud { padding: 4px 12px; }
  .score { font-size: 24px; }
  #timer { font-size: 20px; }
  .sci { display: none; }
  #cdbar { padding: 4px 12px 6px; }
}
@media (max-width: 420px) {
  .sci { font-size: 10px; white-space: normal; }
}
.help-q { color: var(--ok); font-weight: 900; font-size: 17px; }

/* ---- 대기실 팀 명단 ---- */
.teams { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin: 6px 0 4px; text-align: left; }
.team { border: 1px solid var(--line); border-radius: 12px; padding: 8px 10px; background: var(--field-bg); }
.team h4 { margin: 0 0 6px; font-size: 13px; }
.team.mine h4 { color: var(--me); }
.team.other h4 { color: var(--op); }
.team ul { list-style: none; margin: 0; padding: 0; }
.team li { font-size: 14px; font-weight: 700; padding: 3px 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.team li.empty { color: #9fb6d0; font-weight: 500; }
.pname small { display: block; font-size: 11px; font-weight: 600; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.side.op .pname { text-align: right; }

/* 계정 칩 (아이디 · 골드 · 상점) */
.acct-slot { display: flex; justify-content: flex-end; margin: -6px -4px 10px; min-height: 40px; }
.acct-slot:empty { display: none; }
.kit-root h2 { font-size: 22px; }
.gold-line { margin: 4px 0 10px; font-weight: 800; color: #b87400; min-height: 1em; }
.gold-line:empty { display: none; }
.small-note { font-size: 13px; margin-top: 10px; text-align: center; }
