/* 넘버러시 — 전자칠판 우선 스타일 (kingsmath-track.md §2)
   수치 근거: 1920×1080 / 관람거리 6~8m / 적외선 터치
   - 타이머·점수 ≥96px  → clamp(28px, 9vmin, 140px)  (1080p에서 97px)
   - 격자 숫자  ≥48px   → 셀의 50%              (1080p에서 ~56px)
   - 터치 타겟  ≥88px   → 1080p 5×5에서 112px
   - 필수 터치는 화면 세로 25~75% 구간           (상단 미도달 / 하단 OS툴바 회피)
   - hover 의존 0 · 정밀 드래그 0 · 명도 대비 7:1 이상 */

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

:root {
  --bg: #12161c;
  --surface: #1c232d;
  --tile: #f2f5f8;
  --tile-ink: #12161c;
  --ink: #f2f5f8;
  --ink-dim: #9aa7b6;
  --accent: #ffc531;
  --danger: #ff5545;
  --ok: #34d17d;
  --radius: clamp(6px, 1.2vmin, 18px);
  --hud-font: clamp(28px, 9vmin, 140px);
  --target-font: clamp(22px, 7vmin, 112px);
  --label-font: clamp(13px, 2.4vmin, 34px);
  --btn-font: clamp(17px, 3.4vmin, 46px);
  /* 터치 타겟 하한 — 전자칠판은 손가락 + 지연 + 서서 조작이라 웹 기본 44px의 2배 */
  --tap-min: 88px;
}

html,
body {
  height: 100%;
}

body {
  background: var(--bg);
  color: var(--ink);
  /* 웹폰트 0KB — 숫자만 쓰므로 시스템 폰트로 충분하다. 이게 언어무관 게임의 실질 이득 */
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue',
    'Apple SD Gothic Neo', 'Malgun Gothic', sans-serif;
  font-variant-numeric: tabular-nums;
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
  /* 전자칠판/모바일 공통: 더블탭 확대·롱프레스 선택·풀투리프레시 차단 */
  touch-action: manipulation;
  -webkit-user-select: none;
  user-select: none;
  overscroll-behavior: none;
  -webkit-tap-highlight-color: transparent;
}

#app {
  position: relative;
  height: 100dvh;
  width: 100vw;
}

.screen {
  position: absolute;
  inset: 0;
  display: none;
}
#app[data-screen='setup'] #scr-setup,
#app[data-screen='play'] #scr-play,
#app[data-screen='handoff'] #scr-handoff,
#app[data-screen='results'] #scr-results {
  display: grid;
}

/* ── 공용 버튼 ───────────────────────────────────────────── */
.btn {
  min-height: var(--tap-min);
  min-width: var(--tap-min);
  padding: 0.5em 1.1em;
  border: 3px solid transparent;
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--ink);
  font: inherit;
  font-size: var(--btn-font);
  font-weight: 700;
  cursor: pointer;
  transition: transform 0.08s ease;
}
.btn:active {
  transform: scale(0.95);
}
.btn:focus-visible {
  outline: 4px solid var(--accent);
  outline-offset: 3px;
}
.btn--primary {
  background: var(--accent);
  color: #12161c;
}
.btn--on {
  border-color: var(--accent);
}
/* hover 스타일은 실제 hover 가능한 기기에서만 — 터치엔 hover가 없다 */
@media (hover: hover) and (pointer: fine) {
  .btn:hover {
    border-color: var(--accent);
  }
}

/* ── 시작 화면 ───────────────────────────────────────────── */
#scr-setup {
  grid-template-rows: 1fr auto auto 1fr;
  justify-items: center;
  align-content: center;
  gap: 3vmin;
  padding: 4vmin;
}
.title {
  grid-row: 2;
  font-size: clamp(38px, 11vmin, 170px);
  font-weight: 800;
  letter-spacing: -0.02em;
  line-height: 1;
}
.title b {
  color: var(--accent);
}
.subtitle {
  font-size: var(--target-font);
  color: var(--ink-dim);
  font-weight: 600;
  text-align: center;
}
.pick {
  grid-row: 3;
  display: flex;
  gap: 2.5vmin;
  flex-wrap: wrap;
  justify-content: center;
}
.pick .btn {
  min-width: max(var(--tap-min), 22vmin);
  font-size: clamp(20px, 5vmin, 70px);
}
.sunhaengssam-link {
  grid-row: 4;
  align-self: end;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 44px;
  padding: 0.5em 1.4em;
  margin-bottom: 1vmin;
  border-radius: 999px;
  background: var(--surface);
  color: var(--ink-dim);
  font-size: clamp(12px, 1.8vmin, 16px);
  font-weight: 600;
  text-decoration: none;
  text-align: center;
}

/* ── 플레이 화면 ─────────────────────────────────────────── */
/* 25% HUD / 50% 격자 / 13% 목표 / 12% 여백(전자칠판 하단 툴바 회피)
   검산(1920×1080): 격자행 270~810px, 격자 48dvh=518px → 상단 281px(26%) 하단 799px(74%)
   → 필수 터치가 25~75% 구간 안에 들어간다. 20%로 두면 상단이 21.5%로 새어나간다(실측 검산) */
#scr-play {
  grid-template-rows: 25% 50% 13% 12%;
}

/* ── 소리 토글 (전 화면 고정) ────────────────────────────── */
.sound-bar {
  position: fixed;
  top: clamp(6px, 1.4vmin, 20px);
  right: clamp(6px, 1.4vmin, 20px);
  z-index: 10;
  display: flex;
  gap: clamp(4px, 0.8vmin, 12px);
}
.sound-btn {
  display: flex;
  align-items: center;
  gap: 0.45em;
  /* 설정 컨트롤이라 격자(88px)보다는 작아도 되지만 터치는 확실히 되어야 한다 */
  min-height: clamp(52px, 6vmin, 84px);
  min-width: 0;
  padding: 0.35em 0.8em;
  font-size: clamp(13px, 2.2vmin, 30px);
  font-weight: 700;
  background: var(--surface);
  border: 3px solid var(--ok);
  color: var(--ink);
}
.sound-btn.is-off {
  border-color: transparent;
  color: var(--ink-dim);
  opacity: 0.6;
}
.sound-btn.is-off .ico {
  filter: grayscale(1);
}
.sound-btn .ico {
  font-size: 1.15em;
  line-height: 1;
}

.hud {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  /* 소리 토글은 화면 우상단에 고정돼 있다. 좌우 패딩으로 피하면 HUD가 가운데로 짓눌리고
     한쪽만 주면 가운데 컬럼이 격자 중심과 어긋난다 → **위쪽을 비워서** 아래로 내린다.
     검산(1920×1080): HUD행 0~270px, 토글이 ~95px까지 차지 → 상단 92px를 비우면 겹치지 않는다. */
  padding: clamp(58px, 8.5vmin, 110px) 3vmin 0;
  gap: 2vmin;
}
.hud-slot {
  display: flex;
  flex-direction: column;
  align-items: center;
  min-width: 0;
}
.hud-slot:first-child {
  align-items: flex-start;
}
.hud-slot:last-child {
  align-items: flex-end;
}
.hud-label {
  font-size: var(--label-font);
  color: var(--ink-dim);
  font-weight: 700;
  letter-spacing: 0.08em;
}
.hud-value {
  font-size: var(--hud-font);
  font-weight: 800;
  line-height: 1;
}
#hud-time.is-countdown {
  color: var(--danger);
  animation: pulse 1s steps(2, end) infinite;
}
.hud-player {
  display: flex;
  align-items: center;
  gap: 1.5vmin;
  font-size: var(--hud-font);
  font-weight: 800;
  line-height: 1;
  white-space: nowrap;
}
.hud-player .mark {
  font-size: 0.85em;
}

/* 격자 — 필수 터치 영역. 세로 25%~75% 구간 안에 있다.
   좌우는 1fr로 비워 격자를 중앙에 두고, 왼쪽 여백에 순서 로스터를 얹는다. */
.board {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  min-height: 0;
}

.roster {
  justify-self: end;
  margin-right: clamp(12px, 3vmin, 56px);
  display: flex;
  flex-direction: column;
  gap: clamp(6px, 1.2vmin, 18px);
}
.roster-item {
  display: flex;
  align-items: center;
  gap: clamp(6px, 1.2vmin, 16px);
  /* 본문 하한 48px — 1080p에서 4.6vmin ≈ 50px.
     2.6vmin(28px)로 두면 교실 뒤에서 안 읽힌다(실측 스크린샷에서 발견) */
  font-size: clamp(15px, 4.6vmin, 62px);
  font-weight: 700;
  line-height: 1;
  white-space: nowrap;
  opacity: 0.5;
}
.roster-item.is-done {
  opacity: 0.85;
}
.roster-item.is-current {
  opacity: 1;
  transform: scale(1.12);
  transform-origin: right center;
}
.roster-item .mark {
  font-size: 1.15em;
}
.roster-item .sc {
  color: var(--accent);
  font-variant-numeric: tabular-nums;
}
.roster-item .sc.skipped {
  color: var(--ink-dim);
}
/* 좁은 화면에서는 격자가 우선 — 로스터는 접는다 */
@media (max-width: 900px) {
  .roster {
    display: none;
  }
  .board {
    grid-template-columns: 1fr;
    justify-items: center;
  }
}
.grid {
  --gw: min(88vw, 48dvh);
  width: var(--gw);
  height: var(--gw);
  display: grid;
  grid-template-columns: repeat(var(--n), 1fr);
  grid-template-rows: repeat(var(--n), 1fr);
  gap: clamp(4px, 0.9vmin, 14px);
}
.cell {
  display: grid;
  place-items: center;
  background: var(--tile);
  color: var(--tile-ink);
  border: 0;
  border-radius: var(--radius);
  font: inherit;
  /* 셀의 50% — 1080p 5×5에서 약 56px (하한 48px 충족) */
  font-size: calc(var(--gw) / var(--n) * 0.5);
  font-weight: 800;
  line-height: 1;
  cursor: pointer;
  transition: transform 0.09s ease, opacity 0.16s ease, background-color 0.16s ease;
  will-change: transform;
}
.cell:active {
  transform: scale(0.93);
}
.cell.is-cleared {
  opacity: 0;
  pointer-events: none;
  transform: scale(0.6);
}
.cell.is-miss {
  background: var(--danger);
  color: #fff;
  animation: shake 0.28s ease;
}
.target {
  display: grid;
  place-items: center;
}
.target-inner {
  display: flex;
  align-items: baseline;
  gap: 2vmin;
  font-size: var(--target-font);
  font-weight: 700;
  color: var(--ink-dim);
}
.target-inner b {
  font-size: 1.5em;
  color: var(--accent);
  font-weight: 800;
}
#scr-play.is-countdown {
  box-shadow: inset 0 0 0 clamp(6px, 1.4vmin, 22px) var(--danger);
}

/* ── 교대 화면 ───────────────────────────────────────────── */
#scr-handoff {
  grid-template-rows: 1fr auto auto 1fr;
  justify-items: center;
  align-content: center;
  gap: 3vmin;
  padding: 4vmin;
  cursor: pointer;
}
.handoff-next {
  grid-row: 2;
  display: flex;
  align-items: center;
  gap: 2.5vmin;
  font-size: clamp(38px, 12vmin, 180px);
  font-weight: 800;
  line-height: 1;
}
.handoff-actions {
  grid-row: 3;
  display: flex;
  gap: 2.5vmin;
  flex-wrap: wrap;
  justify-content: center;
}

/* ── 결과 화면 ───────────────────────────────────────────── */
/* 세로 플렉스 — 목록만 스크롤하고 버튼은 고정.
   overflow:auto 하나로 다 덮으면 "스크롤하면 보임"이라 QA가 잘림 버그를 통과시킨다 */
#scr-results {
  grid-template-rows: auto minmax(0, 1fr) auto;
  gap: 2vmin;
  padding: 3vmin 3vmin 5vmin;
}
.results-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 2vmin;
  flex-wrap: wrap;
}
.results-title {
  font-size: clamp(24px, 6vmin, 84px);
  font-weight: 800;
}
.mode-tabs {
  display: flex;
  gap: 1.5vmin;
  flex-wrap: wrap;
}
.mode-tabs .btn {
  font-size: clamp(15px, 2.8vmin, 38px);
  padding: 0.45em 0.9em;
  min-height: max(56px, 7vmin);
}
.results-body {
  overflow-y: auto;
  overflow-x: hidden;
  display: flex;
  flex-direction: column;
  gap: 1.6vmin;
  padding-right: 1vmin;
}
.row {
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 2vmin;
  background: var(--surface);
  border-radius: var(--radius);
  padding: 1.4vmin 2.4vmin;
}
.row-mark {
  font-size: clamp(20px, 4.2vmin, 58px);
  line-height: 1;
}
.row-name {
  font-size: clamp(18px, 3.6vmin, 50px);
  font-weight: 700;
}
.row-score {
  font-size: clamp(22px, 5vmin, 68px);
  font-weight: 800;
}
.bar {
  grid-column: 1 / -1;
  height: clamp(8px, 1.4vmin, 20px);
  border-radius: 999px;
  background: #0d1117;
  overflow: hidden;
}
.bar > i {
  display: block;
  height: 100%;
  border-radius: 999px;
}
.team {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 1.8vmin 2.4vmin;
}
.team-head {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  font-size: clamp(18px, 3.6vmin, 50px);
  font-weight: 800;
  margin-bottom: 1vmin;
}
.team-head span:last-child {
  color: var(--accent);
}
.team-members {
  display: flex;
  flex-wrap: wrap;
  gap: 1.5vmin;
  font-size: clamp(16px, 3vmin, 42px);
  font-weight: 700;
}
.team-members span {
  display: inline-flex;
  align-items: center;
  gap: 0.5vmin;
}
.ord {
  display: inline-grid;
  place-items: center;
  min-width: 1.9em;
  height: 1.9em;
  border-radius: 999px;
  background: var(--accent);
  color: #12161c;
  font-weight: 800;
  font-size: clamp(16px, 3vmin, 42px);
}
.results-actions {
  display: flex;
  gap: 2vmin;
  flex-wrap: wrap;
  justify-content: center;
}

/* ── 애니메이션 ──────────────────────────────────────────── */
@keyframes shake {
  0%,
  100% {
    transform: translateX(0);
  }
  25% {
    transform: translateX(-6%);
  }
  75% {
    transform: translateX(6%);
  }
}
@keyframes pulse {
  50% {
    opacity: 0.35;
  }
}
@media (prefers-reduced-motion: reduce) {
  * {
    animation-duration: 0.01ms !important;
    transition-duration: 0.01ms !important;
  }
}

/* 좁은 화면: 소리 토글은 아이콘만 (텍스트를 두면 HUD를 다 먹는다) */
@media (max-width: 900px) {
  .sound-btn span:not(.ico) {
    display: none;
  }
  .sound-btn {
    padding: 0.35em 0.55em;
  }
}

/* ── 모바일: 격자 셀 하한 64px 확보 ──────────────────────── */
@media (max-width: 560px) {
  #scr-play {
    grid-template-rows: 18% 58% 13% 11%;
  }
  .grid {
    --gw: min(92vw, 56dvh);
  }
}
