* { margin: 0; padding: 0; box-sizing: border-box; }

:root {
  --bg: #f2f5ff;
  --card: #ffffff;
  --primary: #5b6cff;
  --primary-dark: #4353e8;
  --secondary: #ff8a3d;
  --text: #262b40;
  --muted: #7a80a0;
  --radius: 16px;
  --shadow: 0 4px 16px rgba(60, 70, 140, 0.12);
}

html { height: 100%; }

/* 앱 셸: 상단바 / 콘텐츠 / 광고배너를 세로 플렉스로 고정 배치.
   게임 화면은 남은 공간에 정확히 맞춰지므로 어떤 기기에서도 스크롤·잘림이 없다. */
body {
  font-family: "Apple SD Gothic Neo", "Malgun Gothic", "Noto Sans KR", system-ui, sans-serif;
  background: linear-gradient(160deg, #eef2ff 0%, #fdf3ff 100%);
  color: var(--text);
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
  overscroll-behavior: none;
}

/* ===== 상단바 ===== */
#topbar {
  flex: none;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  padding: 6px 12px;
  background: rgba(255, 255, 255, 0.85);
  backdrop-filter: blur(8px);
  box-shadow: 0 1px 8px rgba(60, 70, 140, 0.08);
  z-index: 20;
}

.logo { font-weight: 800; font-size: 1rem; cursor: pointer; color: var(--primary-dark); white-space: nowrap; }

.topbar-right { display: flex; gap: 6px; min-width: 0; }

.chip {
  background: #f0f2fe;
  border-radius: 999px;
  padding: 4px 10px;
  font-size: 0.8rem;
  white-space: nowrap;
}
.chip.nick { cursor: pointer; overflow: hidden; text-overflow: ellipsis; }

/* ===== 메인 ===== */
#app {
  flex: 1 1 0;
  min-height: 0;
  width: 100%;
  max-width: 1100px;
  margin: 0 auto;
  padding: 10px 10px 12px;
  display: flex;
  flex-direction: column;
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
}

.view { text-align: center; flex: 0 0 auto; }
.hidden { display: none !important; }

/* 화면을 꽉 채우는 뷰 (홈/솔로/협동) */
#view-home, #view-solo, #view-coop {
  flex: 1 1 0;
  min-height: 0;
}
#view-home { display: flex; flex-direction: column; justify-content: center; overflow-y: auto; }
#view-solo { display: flex; flex-direction: column; gap: 8px; overflow: hidden; }
#view-coop { display: flex; flex-direction: column; overflow-y: auto; }
#coop-menu, #coop-searching, #coop-lobby { margin-top: auto; margin-bottom: auto; }
#coop-game { flex: 1 1 0; min-height: 0; display: flex; flex-direction: column; gap: 8px; overflow: hidden; }

.title { font-size: clamp(1.6rem, 6vw, 2.2rem); color: var(--primary-dark); }
.subtitle { color: var(--muted); margin: 6px 0 18px; font-size: 0.9rem; }
.section-title { font-size: 1.4rem; margin: 10px 0 16px; }

/* ===== 모드 카드 (홈: 2x2 그리드, 한 화면에 들어감) ===== */
.mode-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
  width: 100%;
  max-width: 560px;
  margin: 0 auto;
}

.mode-card {
  background: var(--card);
  border: none;
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px 10px;
  cursor: pointer;
  transition: transform 0.15s, box-shadow 0.15s;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  font-family: inherit;
}
.mode-card:hover { transform: translateY(-3px); box-shadow: 0 8px 24px rgba(60, 70, 140, 0.18); }
.mode-icon { font-size: 2rem; }
.mode-name { font-size: 1.05rem; font-weight: 700; }
.mode-desc { font-size: 0.75rem; color: var(--muted); line-height: 1.45; }

@media (max-height: 560px) {
  .mode-desc { display: none; }
  .subtitle { display: none; }
}

/* ===== 버튼 ===== */
.btn {
  font-family: inherit;
  border: none;
  border-radius: 12px;
  padding: 10px 20px;
  font-size: 0.95rem;
  font-weight: 700;
  cursor: pointer;
  transition: filter 0.15s, transform 0.1s;
}
.btn:active { transform: scale(0.97); }
.btn:disabled { opacity: 0.55; cursor: not-allowed; }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover:not(:disabled) { background: var(--primary-dark); }
.btn-secondary { background: var(--secondary); color: #fff; }
.btn-secondary:hover:not(:disabled) { filter: brightness(0.95); }
.btn-ghost { background: transparent; color: var(--muted); }
.btn-ghost:hover { color: var(--text); }
.btn-big { padding: 13px 26px; font-size: 1rem; }
.btn-small { padding: 7px 14px; font-size: 0.85rem; background: #eef0fc; color: var(--primary-dark); }
.btn-hint { background: #ffd54d; color: #6b5200; }
.btn-hint:hover:not(:disabled) { filter: brightness(0.97); }

/* ===== 게임 상단 바 (HUD + 버튼 한 줄) ===== */
.game-top {
  flex: none;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  align-items: center;
  gap: 6px;
}
.hud-item {
  background: var(--card);
  border-radius: 8px;
  padding: 5px 10px;
  box-shadow: var(--shadow);
  font-size: 0.82rem;
  white-space: nowrap;
}
.hud-item.timer { font-variant-numeric: tabular-nums; font-weight: 800; color: var(--primary-dark); min-width: 62px; }
.hud-item.timer.low { color: #e53935; animation: pulse 0.6s infinite alternate; }
@keyframes pulse { from { transform: scale(1); } to { transform: scale(1.07); } }
.game-top .btn { padding: 5px 12px; font-size: 0.82rem; border-radius: 8px; }

/* ===== 보드: 남은 공간을 전부 쓰되 절대 넘치지 않음 =====
   세로 화면: 위아래 배치 / 가로 화면: 좌우 배치.
   svg는 컨테이너 안에서 비율 유지 축소(letterbox)되므로 잘림 없음. */
.boards {
  flex: 1 1 0;
  min-height: 0;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
@media (orientation: landscape) {
  .boards { flex-direction: row; }
}

.board {
  flex: 1 1 0;
  min-height: 0;
  min-width: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}
.board svg {
  display: block;
  width: 100%;
  height: 100%;
  cursor: crosshair;
  touch-action: manipulation;
}

/* 보드 오버레이 마크 */
.mark-found { fill: none; stroke: #e53935; stroke-width: 4; }
.mark-hint { fill: rgba(255, 213, 77, 0.25); stroke: #ffb300; stroke-width: 3; stroke-dasharray: 8 6; animation: hintPulse 1s infinite alternate; }
@keyframes hintPulse { from { opacity: 0.5; } to { opacity: 1; } }
.mark-miss { stroke: #e53935; stroke-width: 5; stroke-linecap: round; }

.game-actions { margin-top: 10px; display: flex; justify-content: center; gap: 12px; align-items: center; flex-wrap: wrap; }

/* ===== 협동 ===== */
.coop-options { display: flex; flex-direction: column; gap: 12px; max-width: 340px; margin: 0 auto 14px; }
.join-row { display: flex; gap: 8px; }
.join-row input {
  flex: 1;
  min-width: 0;
  font-family: inherit;
  font-size: 1rem;
  padding: 12px 14px;
  border: 2px solid #dde1f5;
  border-radius: 12px;
  text-transform: uppercase;
  letter-spacing: 2px;
  text-align: center;
  outline: none;
}
.join-row input:focus { border-color: var(--primary); }

.invite-box {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px;
  max-width: 360px;
  margin: 0 auto 14px;
}
.invite-code-label { color: var(--muted); font-size: 0.85rem; }
.invite-code { font-size: 1.8rem; font-weight: 800; letter-spacing: 6px; color: var(--primary-dark); margin: 6px 0 10px; }
.invite-actions { display: flex; justify-content: center; gap: 8px; margin-bottom: 10px; }
.qr { width: min(160px, 34vh); height: auto; border-radius: 8px; }

.player-list { list-style: none; max-width: 360px; margin: 0 auto 14px; display: flex; flex-direction: column; gap: 8px; }
.player-list li {
  background: var(--card);
  border-radius: 10px;
  box-shadow: var(--shadow);
  padding: 10px 14px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.player-list .host-badge { font-size: 0.75rem; background: #ffe9a8; color: #6b5200; border-radius: 6px; padding: 2px 8px; }
.player-list.compact { flex-direction: row; flex-wrap: wrap; justify-content: center; max-width: none; margin: 0; gap: 6px; }
.player-list.compact li { padding: 4px 10px; font-size: 0.78rem; gap: 6px; }

/* 협동 게임 하단: 참가자 + 피드 한 줄 (넘치지 않게 고정 높이) */
.game-bottom {
  flex: none;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  min-height: 0;
  overflow: hidden;
}
.feed {
  flex: 0 1 auto;
  min-width: 0;
  max-height: 1.4em;
  overflow: hidden;
  font-size: 0.78rem;
  color: var(--muted);
  display: flex;
  flex-direction: column-reverse;
}
.feed div { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.spinner {
  width: 44px; height: 44px;
  border: 5px solid #dde1f5;
  border-top-color: var(--primary);
  border-radius: 50%;
  margin: 20px auto;
  animation: spin 0.9s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.hint-text { color: var(--muted); font-size: 0.82rem; margin: 8px 0 12px; line-height: 1.6; }

/* ===== 랭킹 ===== */
.tabs { display: flex; justify-content: center; gap: 8px; margin-bottom: 14px; }
.tab {
  font-family: inherit;
  border: none;
  background: #e6e9fa;
  color: var(--muted);
  border-radius: 999px;
  padding: 8px 22px;
  font-weight: 700;
  cursor: pointer;
}
.tab.active { background: var(--primary); color: #fff; }

.rank-list { list-style: none; max-width: 480px; margin: 0 auto 16px; display: flex; flex-direction: column; gap: 8px; }
.rank-list li {
  background: var(--card);
  border-radius: 10px;
  box-shadow: var(--shadow);
  padding: 10px 14px;
  display: flex;
  align-items: center;
  gap: 10px;
  text-align: left;
}
.rank-num { font-weight: 800; width: 32px; flex: none; color: var(--primary-dark); }
.rank-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.rank-score { font-weight: 800; }
.rank-level { color: var(--muted); font-size: 0.8rem; }
.rank-empty { color: var(--muted); padding: 24px 0; }

/* ===== 상점 ===== */
.shop-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 10px;
  max-width: 700px;
  margin: 0 auto 12px;
}
.shop-item {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px 12px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
}
.shop-icon { font-size: 2rem; }
.shop-name { font-weight: 700; font-size: 0.95rem; }
.shop-desc { font-size: 0.78rem; color: var(--muted); margin-bottom: 4px; }
.shop-item .btn { padding: 8px 14px; font-size: 0.88rem; }

/* ===== 광고 배너 (플렉스 자식: 게임 영역과 절대 겹치지 않음) ===== */
#ad-banner {
  flex: none;
  min-height: 50px;
  background: #21263c;
  color: #aab;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  font-size: 0.82rem;
  padding: 6px 10px calc(6px + env(safe-area-inset-bottom));
}
.ad-label {
  background: #ffd54d;
  color: #333;
  font-weight: 800;
  font-size: 0.7rem;
  border-radius: 4px;
  padding: 2px 6px;
}

.ad-modal { max-width: 380px; }
.ad-video {
  background: #111;
  border-radius: 12px;
  aspect-ratio: 16 / 10;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 16px;
  color: #ccc;
}
.ad-video-inner { text-align: center; font-size: 0.9rem; line-height: 1.8; }
.ad-play { font-size: 2.5rem; display: block; }
.ad-note { font-size: 0.75rem; color: #888; }

/* ===== 모달 ===== */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(20, 24, 45, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
  padding: 16px;
}
.modal {
  background: var(--card);
  border-radius: 18px;
  padding: 22px;
  width: 100%;
  max-width: 420px;
  max-height: 90vh;
  max-height: 90dvh;
  overflow-y: auto;
  text-align: center;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.25);
}
.modal h3 { font-size: 1.25rem; margin-bottom: 12px; }
#modal-body { color: var(--text); line-height: 1.7; margin-bottom: 16px; }
#modal-body .big-score { font-size: 2rem; font-weight: 800; color: var(--primary-dark); }
#modal-body input {
  font-family: inherit;
  font-size: 1rem;
  padding: 10px 14px;
  border: 2px solid #dde1f5;
  border-radius: 10px;
  width: 100%;
  text-align: center;
  outline: none;
}
#modal-body input:focus { border-color: var(--primary); }
.modal-actions { display: flex; justify-content: center; gap: 10px; flex-wrap: wrap; }

/* ===== 토스트 ===== */
#toasts {
  position: fixed;
  top: 52px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 60;
  display: flex;
  flex-direction: column;
  gap: 8px;
  pointer-events: none;
  max-width: calc(100vw - 24px);
}
.toast {
  background: rgba(38, 43, 64, 0.92);
  color: #fff;
  border-radius: 999px;
  padding: 9px 18px;
  font-size: 0.85rem;
  animation: toastIn 0.25s ease, toastOut 0.3s ease 2.2s forwards;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
@keyframes toastIn { from { opacity: 0; transform: translateY(-10px); } }
@keyframes toastOut { to { opacity: 0; transform: translateY(-10px); } }

/* ===== 큰 화면 보정 ===== */
@media (min-width: 768px) {
  .logo { font-size: 1.15rem; }
  .chip { font-size: 0.85rem; padding: 5px 12px; }
  .hud-item { font-size: 0.95rem; padding: 7px 14px; }
  .game-top .btn { font-size: 0.9rem; padding: 7px 16px; }
  .mode-icon { font-size: 2.4rem; }
  .mode-card { padding: 24px 16px; }
  .mode-desc { font-size: 0.85rem; }
  #app { padding: 16px 14px; }
}

/* ===== 내 계정 시트 ===== */
#account-modal { align-items: flex-end; padding: 0; }
@media (min-width: 600px) { #account-modal { align-items: center; padding: 16px; } }

.account-sheet {
  background: var(--bg);
  width: 100%;
  max-width: 420px;
  max-height: 92dvh;
  border-radius: 22px 22px 0 0;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  box-shadow: 0 -8px 40px rgba(0, 0, 0, 0.25);
  animation: sheetUp 0.22s ease;
}
@media (min-width: 600px) { .account-sheet { border-radius: 22px; animation: none; } }
@keyframes sheetUp { from { transform: translateY(40px); opacity: 0.5; } }

.account-header {
  flex: none;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  padding: 14px;
  font-size: 1.05rem;
  font-weight: 800;
  color: #fff;
  background: linear-gradient(135deg, var(--primary) 0%, #7f6bff 100%);
}
.account-close {
  position: absolute;
  right: 10px;
  top: 50%;
  transform: translateY(-50%);
  border: none;
  background: rgba(255, 255, 255, 0.22);
  color: #fff;
  width: 30px;
  height: 30px;
  border-radius: 50%;
  font-size: 0.9rem;
  cursor: pointer;
}

.account-body {
  overflow-y: auto;
  padding: 14px 14px calc(14px + env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 12px;
}

/* 프로필 카드 */
.profile-card {
  background: var(--card);
  border-radius: 14px;
  box-shadow: var(--shadow);
  padding: 14px;
  display: flex;
  align-items: center;
  gap: 12px;
  text-align: left;
}
.avatar {
  flex: none;
  width: 54px;
  height: 54px;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--primary), #9b6bff);
  color: #fff;
  font-size: 1.5rem;
  font-weight: 800;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 3px 10px rgba(91, 108, 255, 0.35);
}
.profile-info { flex: 1; min-width: 0; }
.profile-name-row { display: flex; align-items: center; gap: 6px; }
.profile-name-row b { font-size: 1.1rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.edit-btn {
  border: none;
  background: #eef0fc;
  border-radius: 8px;
  width: 28px;
  height: 28px;
  font-size: 0.8rem;
  cursor: pointer;
  flex: none;
}
.profile-sub {
  color: var(--muted);
  font-size: 0.78rem;
  margin-top: 3px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.role-badge {
  flex: none;
  font-size: 0.7rem;
  font-weight: 800;
  border-radius: 999px;
  padding: 4px 10px;
  background: #e6e9fa;
  color: var(--muted);
}
.role-badge.linked { background: #dff5e4; color: #1d7a3d; }
.role-badge.admin { background: #ffe9a8; color: #6b5200; }

/* 보유 자산 */
.asset-row { display: flex; gap: 10px; }
.asset {
  flex: 1;
  background: var(--card);
  border-radius: 12px;
  box-shadow: var(--shadow);
  padding: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  font-size: 1rem;
}
.asset b { font-size: 1.05rem; font-variant-numeric: tabular-nums; }
.asset span { color: var(--muted); font-size: 0.75rem; }

/* 계정 연동 */
.link-section {
  background: var(--card);
  border-radius: 14px;
  box-shadow: var(--shadow);
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.link-title { font-weight: 800; font-size: 0.95rem; }
.link-desc { color: var(--muted); font-size: 0.78rem; line-height: 1.5; margin-bottom: 2px; }
.provider-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  border: none;
  border-radius: 12px;
  padding: 12px;
  font-family: inherit;
  font-size: 0.95rem;
  font-weight: 700;
  cursor: pointer;
  transition: filter 0.15s, transform 0.1s;
}
.provider-btn:active { transform: scale(0.98); }
.provider-btn.kakao { background: #FEE500; color: #191919; }
.provider-btn.google { background: #fff; color: #333; border: 1.5px solid #dde1f5; }
.provider-btn.done {
  background: #f0f2f7;
  color: #1d7a3d;
  border: 1.5px solid #cde9d6;
  cursor: default;
}
.provider-btn.off { opacity: 0.45; cursor: default; }

/* 메뉴 그리드 */
.account-menu {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 8px;
}
.account-menu button {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  border: none;
  background: var(--card);
  box-shadow: var(--shadow);
  border-radius: 12px;
  padding: 12px 8px;
  font-family: inherit;
  font-size: 0.88rem;
  font-weight: 700;
  color: var(--text);
  cursor: pointer;
  transition: transform 0.1s;
}
.account-menu button:active { transform: scale(0.97); }
.account-menu button span { font-size: 1.05rem; }

.logout-btn {
  border: none;
  background: transparent;
  color: var(--muted);
  font-family: inherit;
  font-size: 0.85rem;
  font-weight: 700;
  padding: 8px;
  cursor: pointer;
  text-decoration: underline;
  text-underline-offset: 3px;
}
