/* Pixel Brick — 네온 다크 UI */
* { margin: 0; padding: 0; box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

:root {
  --bg-0: #0d0d12;
  --bg-1: #15151d;
  --bg-2: #1e1e29;
  --line: #2b2b3a;
  --text: #e8e8f0;
  --text-dim: #8a8a9c;
  --green: #7ec850;
  --green-hi: #9ee76a;
  --wine: #8e2a45;
  --wine-hi: #b53a5c;
  --gray: #3a3a48;
  --gray-hi: #4d4d5e;
  --accent: #46c0d2;
}

html, body {
  height: 100%;
  overflow: hidden;
  background: var(--bg-0);
  color: var(--text);
  font-family: "Avenir Next", "Segoe UI", "Apple SD Gothic Neo", "Noto Sans KR", sans-serif;
  user-select: none;
  -webkit-user-select: none;
  touch-action: none;
  -webkit-touch-callout: none; /* iOS 길게 누르기 메뉴 방지 */
}

/* iOS Safari 더블탭 확대 방지 — 버튼/입력엔 manipulation, 게임 보드엔 none */
button, input {
  touch-action: manipulation;
}
canvas {
  touch-action: none;
}

.hidden { display: none !important; }

/* 화면 전환 — 두 화면이 겹친 채 서로 페이드되는 디졸브 */
.screen { transition: opacity 0.28s ease; }
.screen.dissolve {
  opacity: 0;
  pointer-events: none; /* 사라지는 화면은 터치를 받지 않는다 */
}

.screen {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background:
    radial-gradient(1200px 600px at 50% -10%, rgba(70, 192, 210, 0.08), transparent 60%),
    linear-gradient(180deg, var(--bg-0), #101018 60%, #0a0a10);
}

/* ---------- 브랜드 ---------- */
.brand { text-align: center; margin-bottom: 48px; }
.brand.small { margin-bottom: 28px; }
.brand-logo {
  font-size: 42px;
  font-weight: 800;
  letter-spacing: 4px;
  color: var(--text);
  text-shadow: 0 0 24px rgba(70, 192, 210, 0.35);
}
.brand.small .brand-logo { font-size: 30px; }
.brand-logo span { color: var(--accent); }
.brand-sub {
  margin-top: 8px;
  font-size: 11px;
  letter-spacing: 6px;
  color: var(--text-dim);
}
.lobby-welcome {
  margin-top: 10px;
  font-size: 13px;
  letter-spacing: 2px;
  color: var(--text-dim);
}
.lobby-welcome b { color: var(--green-hi); }
.signoff-link {
  margin-left: 10px;
  padding: 4px 10px;
  font-size: 11px;
  letter-spacing: 1px;
  color: var(--text-dim);
  background: transparent;
  border: 1px solid var(--line);
  border-radius: 4px;
  cursor: pointer;
  font-family: inherit;
}
.signoff-link:active { color: var(--text); border-color: var(--text-dim); }

/* Sign Off 팝업 */
.signoff-box { max-width: 340px; }
.signoff-msg {
  white-space: pre-line;
  font-size: 14px;
  line-height: 1.7;
  color: var(--text);
  letter-spacing: 0.5px;
}
.signoff-btns {
  display: grid;
  grid-template-columns: 1fr 1fr; /* 1행: Sign Off · Sign Up */
  gap: 8px;
  margin-top: 22px;
}
#signoff-cancel { grid-column: 1 / -1; } /* 2행: Cancel — 위 두 버튼 합친 너비 */
.signoff-nick { color: var(--green-hi); }
.btn-small {
  padding: 10px 16px;
  border: none;
  border-radius: 6px;
  font-family: inherit;
  font-size: 13px;
  font-weight: 800;
  letter-spacing: 1px;
  cursor: pointer;
  color: #fff;
}
.btn-small:active { filter: brightness(1.2); }
.btn-small-wine { background: linear-gradient(90deg, var(--wine), var(--wine-hi)); }
.btn-small-gray { background: linear-gradient(90deg, var(--gray), var(--gray-hi)); }
.btn-small-sky  { background: linear-gradient(90deg, #4aa8e0, #6ec6f2); color: #082238; }

/* 로비 RANKING 버튼 */
.btn-blue {
  background: linear-gradient(90deg, #2e6ea8, #3f8dcc);
  box-shadow: 0 4px 18px rgba(63, 141, 204, 0.35);
}

/* 랭킹 화면 */
.rank-tabs {
  display: flex;
  gap: 8px;
  width: 100%;
  max-width: 420px;
  margin-bottom: 12px;
}
.rank-tab {
  flex: 1;
  padding: 12px 0;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--bg-2);
  color: var(--text-dim);
  font-family: inherit;
  font-size: 14px;
  font-weight: 800;
  letter-spacing: 3px;
  cursor: pointer;
}
.rank-tab.active {
  background: linear-gradient(90deg, #2e6ea8, #3f8dcc);
  border-color: transparent;
  color: #fff;
}
.rank-info {
  width: 100%;
  max-width: 420px;
  min-height: 15px; /* 로딩 중 비어도 높이를 유지해 아래 요소가 흔들리지 않게 */
  margin-bottom: 10px;
  font-size: 11px;
  letter-spacing: 1px;
  color: var(--text-dim);
  text-align: center;
}
.rank-remain { color: #8b5a2b; } /* 남은 시간 — 어두운 갈색 */

.rank-list {
  width: 100%;
  max-width: 420px;
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--bg-1);
  margin-bottom: 16px;
  -webkit-overflow-scrolling: touch;
}
.rank-row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 14px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.04);
  font-size: 14px;
  /* 1등부터 아래로 한 줄씩 펼쳐진다 (지연은 JS에서 0.2초 간격으로 지정) */
  transform-origin: center center;
  animation: rank-flip 0.32s ease-out backwards;
}
/* 세로 정가운데를 기준으로 0%에서 100%로 펼쳐진다 */
@keyframes rank-flip {
  from { opacity: 0; transform: scaleY(0); }
  to   { opacity: 1; transform: scaleY(1); }
}
.rank-row .rk {
  width: 34px;
  text-align: right;
  font-weight: 800;
  color: #4e4e5e; /* 4등 이하 — 은메달 색과 확실히 구분되도록 어둡게 */
}
.rank-row:nth-child(1) .rk { color: #ffd75e; }
.rank-row:nth-child(2) .rk { color: #c9d4e0; }
.rank-row:nth-child(3) .rk { color: #d99a5b; }
.rank-row .nm {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.rank-row .val { font-weight: 800; letter-spacing: 1px; }
.rank-row .sub { font-size: 11px; color: var(--text-dim); margin-left: 6px; }
.rank-row.me { background: rgba(126, 200, 80, 0.10); }
.rank-row.me .nm { color: var(--green-hi); }

/* 상단 고정 — 내 순위 (연두색 바닥 마킹) */
.rank-me {
  display: flex;
  align-items: center;
  gap: 12px;
  transform-origin: center center;
  animation: rank-flip 0.32s ease-out backwards;
  width: 100%;
  max-width: 420px;
  padding: 12px 14px;
  margin-bottom: 8px;
  font-size: 14px;
  border-radius: 6px;
  background: linear-gradient(90deg, rgba(126, 200, 80, 0.85), rgba(158, 231, 106, 0.85));
  color: #12210a;
  box-shadow: 0 2px 14px rgba(126, 200, 80, 0.25);
}
.rank-me .rk {
  width: 34px;
  text-align: right;
  font-weight: 800;
}
.rank-me .nm {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-weight: 800;
}
.rank-me .val { font-weight: 800; letter-spacing: 1px; }
.rank-me .sub { font-size: 11px; opacity: 0.7; margin-left: 6px; }
.rank-empty {
  padding: 32px 16px;
  text-align: center;
  font-size: 13px;
  color: var(--text-dim);
  letter-spacing: 1px;
}

/* ---------- 바 버튼 ---------- */
.btn-bar {
  display: flex;
  flex-direction: column;
  align-items: center; /* 기본은 중앙 정렬, 로비 메뉴만 좌측 정렬 */
  justify-content: center;
  width: 100%;
  max-width: 420px;
  min-height: 64px;
  padding: 12px 20px;
  margin: 8px 0;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  color: #fff;
  font-family: inherit;
  transition: transform 0.06s ease, filter 0.15s ease;
}
.btn-bar:active { transform: scale(0.98); filter: brightness(1.2); }
.bar-title { font-size: 18px; font-weight: 800; letter-spacing: 3px; }
.bar-desc { font-size: 12px; margin-top: 4px; opacity: 0.75; letter-spacing: 1px; }

.btn-green {
  background: linear-gradient(90deg, var(--green), var(--green-hi));
  box-shadow: 0 4px 18px rgba(126, 200, 80, 0.35);
  color: #12210a;
}
.btn-wine {
  background: linear-gradient(90deg, var(--wine), var(--wine-hi));
  box-shadow: 0 4px 18px rgba(142, 42, 69, 0.45);
}
.btn-gray {
  background: linear-gradient(90deg, var(--gray), var(--gray-hi));
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.4);
}
.btn-green .bar-title, .btn-green .bar-desc { color: #12210a; }

.lobby-menu {
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
}
.lobby-menu .btn-bar { align-items: flex-start; }

/* ---------- 닉네임 ---------- */
.nick-box {
  width: 100%;
  max-width: 420px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}
.nick-box label {
  font-size: 13px;
  letter-spacing: 2px;
  color: var(--text-dim);
  text-align: center;
}
.nick-box input, .config-row input {
  width: 100%;
  padding: 14px 16px;
  font-size: 18px;
  text-align: center;
  letter-spacing: 2px;
  color: var(--text);
  background: var(--bg-2);
  border: 1px solid var(--line);
  border-radius: 6px;
  outline: none;
  font-family: inherit;
}
.nick-box input:focus, .config-row input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 12px rgba(70, 192, 210, 0.25);
}
.nick-field { position: relative; width: 100%; }
.nick-ghost {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 14px 16px;
  font-size: 18px;
  letter-spacing: 2px;
  color: var(--text);
  background: var(--bg-2);
  border: 1px solid var(--accent);
  border-radius: 6px;
  cursor: text;
}
.nick-ghost .suffix { color: var(--text-dim); }

/* 토스트 */
.toast {
  position: fixed;
  left: 50%;
  top: 20%;
  transform: translateX(-50%);
  padding: 12px 22px;
  background: rgba(30, 30, 41, 0.96);
  border: 1px solid var(--line);
  border-radius: 6px;
  font-size: 13px;
  letter-spacing: 1px;
  color: var(--text);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease;
  z-index: 40;
}
.toast.show { opacity: 1; }

/* ---------- 설정 ---------- */
.screen-title {
  font-size: 24px;
  font-weight: 800;
  letter-spacing: 6px;
  margin-bottom: 28px;
}
.config-list {
  width: 100%;
  max-width: 420px;
  margin-bottom: 28px;
}
.config-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 16px 4px;
  border-bottom: 1px solid var(--line);
  font-size: 15px;
}
.config-row input { max-width: 180px; font-size: 14px; padding: 10px 12px; }
.toggle {
  min-width: 72px;
  padding: 10px 0;
  border: none;
  border-radius: 6px;
  font-weight: 800;
  letter-spacing: 2px;
  cursor: pointer;
  font-family: inherit;
  background: var(--gray);
  color: var(--text-dim);
}
.toggle.on {
  background: linear-gradient(90deg, var(--green), var(--green-hi));
  color: #12210a;
}

/* ---------- 게임 화면 ---------- */
#screen-game {
  padding: 0;
  justify-content: flex-start;
  /* 노치/홈 인디케이터 회피 — env가 0인 환경에서도 최소 여백을 확보 */
  padding-top: max(env(safe-area-inset-top), 8px);
  padding-bottom: max(env(safe-area-inset-bottom), 4px);
}

/* 나가기 버튼 — HUD 행 안에서 화면 좌측 끝에 붙는다 (솔로·멀티 동일 위치) */
.btn-exit {
  position: absolute;
  left: 0;
  top: 50%;
  transform: translateY(-50%);
  width: 38px;
  height: 38px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0 0 4px;
  font-family: inherit;
  font-size: 26px;
  line-height: 1;
  color: var(--text);
  background: rgba(30, 30, 41, 0.85);
  border: 1px solid var(--line);
  border-left: none;
  border-radius: 0 8px 8px 0;
  cursor: pointer;
  z-index: 15;
}
.btn-exit:active { background: var(--bg-2); filter: brightness(1.3); }
/* 튜토리얼 중에는 SKIP 버튼 */
.btn-exit.skip {
  width: auto;
  padding: 0 14px;
  font-size: 13px;
  font-weight: 800;
  letter-spacing: 2px;
  color: var(--text-dim);
}

/* ---------- 튜토리얼 ---------- */
.tutorial {
  position: absolute;
  inset: 0;
  display: flex;
  justify-content: center;
  padding: 0 14px;
  z-index: 14;
  pointer-events: none; /* 기본은 조작이 그대로 보드로 통과 */
}
/* HUD(SCORE·LINES·NEXT)를 가리지 않도록 그 아래에서 시작 */
.tutorial.pos-top    { align-items: flex-start; padding-top: 64px; }
.tutorial.pos-bottom { align-items: flex-end;   padding-bottom: 18px; }
.tutorial.interactive { pointer-events: auto; } /* 탭해서 넘기는 단계에서만 */
.tutorial-card {
  width: 100%;
  max-width: 420px;
  padding: 16px 18px 12px;
  background: rgba(21, 21, 29, 0.93);
  border: 1px solid var(--accent);
  border-radius: 10px;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.7), 0 0 24px rgba(70, 192, 210, 0.18);
  animation: tut-in 0.25s ease-out;
}
@keyframes tut-in {
  from { opacity: 0; transform: translateY(12px); }
  to   { opacity: 1; transform: translateY(0); }
}
.tutorial-step {
  font-size: 11px;
  font-weight: 800;
  letter-spacing: 3px;
  color: var(--accent);
  margin-bottom: 8px;
}
.tutorial-text {
  font-size: 15px;
  line-height: 1.65;
  color: var(--text);
  letter-spacing: 0.3px;
  white-space: pre-line; /* 안내문의 줄바꿈 유지 */
}
.tutorial-hint:empty { display: none; }
.tutorial-hint {
  margin-top: 12px;
  font-size: 11px;
  letter-spacing: 1px;
  color: var(--text-dim);
  text-align: right;
  animation: tut-blink 1.4s ease-in-out infinite;
}
@keyframes tut-blink {
  0%, 100% { opacity: 0.35; }
  50%      { opacity: 1; }
}

/* 튜토리얼에서 특정 영역을 강조 */
.tut-focus {
  position: relative;
  border-radius: 6px;
  outline: 2px solid var(--accent);
  outline-offset: 6px;
  animation: tut-glow 1.2s ease-in-out infinite;
}
@keyframes tut-glow {
  0%, 100% { box-shadow: 0 0 0 rgba(70, 192, 210, 0); }
  50%      { box-shadow: 0 0 18px rgba(70, 192, 210, 0.75); }
}

.multi-view {
  width: 100%;
  height: 20vh; /* 상단 1/5 */
  display: flex;
  gap: 2px;
  background: var(--bg-0);
  border-bottom: 1px solid var(--line);
  flex: none;
}
.rival {
  flex: 1;
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: var(--bg-1);
  overflow: hidden;
}
.rival canvas { display: block; }
.rival-name {
  position: absolute;
  bottom: 2px;
  left: 0;
  right: 0;
  text-align: center;
  font-size: 10px;
  letter-spacing: 1px;
  color: var(--text-dim);
  text-shadow: 0 1px 2px #000;
  pointer-events: none;
}
.rival.dead::after {
  content: attr(data-rank); /* 사망/확정 시 순위 표시 (예: "3등") */
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(13, 13, 18, 0.75);
  backdrop-filter: blur(3px);
  color: var(--wine-hi);
  font-size: 18px;
  font-weight: 800;
  letter-spacing: 3px;
}
.rival.dead.winner::after { color: var(--green-hi); }

/* HUD — LINES를 화면 정중앙에 고정하고 SCORE·NEXT를 좌우 50px 간격으로.
   양옆 1fr 컬럼이 같은 폭이라, SCORE/NEXT의 폭이 달라도 가운데가 틀어지지 않는다. */
.game-hud {
  position: relative;
  width: 100%;
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  padding: 10px 16px 4px;
  flex: none;
}
.hud-label { font-size: 10px; letter-spacing: 3px; color: var(--text-dim); }
.hud-value { font-size: 20px; font-weight: 800; letter-spacing: 1px; }
.hud-left, .hud-mid, .hud-right { text-align: center; min-width: 0; }
.hud-left  { justify-self: end;    margin-right: 50px; }
.hud-mid   { justify-self: center; }
.hud-right { justify-self: start;  margin-left: 50px; }
#next-canvas { display: block; margin: 2px auto 0; }

.board-wrap {
  position: relative;
  flex: 1;
  touch-action: none; /* 보드 주변 빈 공간도 조작 영역 */
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 0;
  padding: 4px 0 8px;
}
#board-canvas {
  display: block;
  background: var(--bg-1);
  border: 1px solid var(--line);
  border-radius: 4px;
  box-shadow: 0 0 40px rgba(0, 0, 0, 0.6), 0 0 24px rgba(70, 192, 210, 0.06);
}

/* 2줄 이상 동시 클리어 — "N HIT" 오버레이 */
.clear-banner {
  position: absolute;
  top: 36%;
  left: 0;
  right: 0;
  text-align: center;
  font-size: 30pt;
  font-weight: 800;
  letter-spacing: 4px;
  text-shadow: 0 0 20px currentColor;
  pointer-events: none;
  animation: hit-pop 1s ease-out forwards;
}
.clear-banner.hit-2 { color: #9ee76a; } /* 연두 */
.clear-banner.hit-3 { color: #ff9d3d; } /* 주황 */
.clear-banner.hit-4 { color: #e0459f; } /* 빨간보라 */

/* 70% 스케일·투명에서 시작 → 0.3초에 110%·불투명 → 100%로 안착 후 사라짐 */
@keyframes hit-pop {
  0%   { opacity: 0; transform: scale(0.7); }
  30%  { opacity: 1; transform: scale(1.1); }
  45%  { opacity: 1; transform: scale(1); }
  80%  { opacity: 1; transform: scale(1); }
  100% { opacity: 0; transform: scale(1); }
}

.match-popup {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(10, 10, 16, 0.7);
  backdrop-filter: blur(3px);
  z-index: 20;
  animation: popup-fade 0.15s ease-out both;
}
@keyframes popup-fade {
  from { opacity: 0; }
  to   { opacity: 1; }
}

/* 팝업 상자 — 70% 스케일·투명에서 시작해 110%로 튀었다가 100%로 안착 (전체 0.2초) */
@keyframes popup-spring {
  0%     { opacity: 0; transform: scale(0.7); }
  66.67% { opacity: 1; transform: scale(1.1); }
  100%   { opacity: 1; transform: scale(1); }
}

.match-box {
  animation: popup-spring 0.2s ease-out both;
  min-width: 240px;
  padding: 28px 36px;
  text-align: center;
  background: var(--bg-2);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: 0 8px 40px rgba(0, 0, 0, 0.6), 0 0 24px rgba(70, 192, 210, 0.1);
}
.match-title {
  font-size: 22px;
  font-weight: 800;
  letter-spacing: 3px;
  color: var(--accent);
}
.match-title span {
  display: inline-block;
  width: 1.6em;      /* 점 개수가 변해도 제목이 흔들리지 않게 */
  text-align: left;
}
.match-sub {
  margin-top: 10px;
  font-size: 12px;
  letter-spacing: 1px;
  color: var(--text-dim);
}
.match-cancel {
  width: 100%;
  margin-top: 20px;
}

.count-banner {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 72px;
  font-weight: 800;
  letter-spacing: 4px;
  color: var(--accent);
  text-shadow: 0 0 32px rgba(70, 192, 210, 0.7);
  background: rgba(10, 10, 16, 0.35);
  pointer-events: none;
  z-index: 5;
}

/* 픽셀 폭죽 — 승리 오버레이보다 위에 그린다 */
.fx-canvas {
  position: absolute; /* 위치·크기는 보드 캔버스에 맞춰 JS에서 지정 */
  pointer-events: none;
  z-index: 12;
  image-rendering: pixelated;
}

.game-overlay {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 24px;
  background: rgba(10, 10, 16, 0.86);
  backdrop-filter: blur(4px);
  z-index: 10;
}
.overlay-title {
  font-size: 34px;
  font-weight: 800;
  letter-spacing: 6px;
  margin-bottom: 4px;
  text-shadow: 0 0 24px rgba(70, 192, 210, 0.4);
}
.overlay-sub {
  font-size: 14px;
  color: var(--text-dim);
  letter-spacing: 1px;
  margin-bottom: 20px;
  text-align: center;
  line-height: 1.6;
}
.overlay-count {
  margin-top: 16px;
  font-size: 13px;
  letter-spacing: 2px;
  color: var(--text-dim);
}
