:root {
  color-scheme: dark;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
}

body {
  min-height: 100vh;
  /* 모바일 브라우저 주소창을 뺀 실제 높이(dvh)를 지원하면 그 값을 쓴다. */
  min-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 12px;
  /* 노치·홈 표시줄이 있는 기기에서 조작부가 가리지 않게 안전 영역을 둔다. */
  padding: max(12px, env(safe-area-inset-top)) max(12px, env(safe-area-inset-right))
    max(12px, env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left));
  overscroll-behavior: none;
  background: #0b1120;
  color: #e6edf6;
  font-family: system-ui, -apple-system, "Segoe UI", "Malgun Gothic", sans-serif;
}

.game {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  /* 좁은 화면(vmin)과 낮은 화면(남은 높이) 모두에 맞춰 보드 크기를 줄인다. */
  width: min(92vmin, 480px, calc(100vh - 140px));
  width: min(92vmin, 480px, calc(100dvh - 140px));
}

/* 터치 기기에서는 방향 버튼 높이만큼 자리를 비워 보드가 버튼을 밀어내지 않게 한다.
   데스크톱은 방향 버튼이 없으므로 보드를 더 크게 쓴다. */
html.is-touch .game {
  width: min(92vmin, 480px, calc(100vh - 230px));
  width: min(92vmin, 480px, calc(100dvh - 230px));
}

/* JS를 쓰지 못하는 경우에도 동작하도록 터치 판정 미디어 쿼리에도 같은 여유를 준다. */
@media (hover: none) and (pointer: coarse) {
  .game {
    width: min(92vmin, 480px, calc(100vh - 230px));
    width: min(92vmin, 480px, calc(100dvh - 230px));
  }
}

.hud-item {
  display: flex;
  align-items: baseline;
  gap: 6px;
}

/* 지금 원소가 정확히 가운데 오도록 5칸을 같은 너비로 나눈다. */
.order {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 4px;
  width: 100%;
}

.order .chip {
  min-width: 0;
  padding: 3px 2px;
  font-size: 0.66rem;
  font-weight: 600;
  line-height: 1.15;
  text-align: center;
  word-break: break-all;
  color: #9fb3cc;
  background: #16213a;
  border: 1px solid #22304d;
  border-radius: 8px;
}

.order .chip.ghost {
  visibility: hidden;
}

.order .chip.done {
  color: #4c5c74;
  background: #0f1728;
  border-color: #172033;
  text-decoration: line-through;
}

.order .chip.current {
  color: #3a2600;
  background: #ffd166;
  border-color: #ffd166;
}

.hud {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  width: 100%;
}

.hud-label {
  color: #8fa3bf;
  font-size: 0.9rem;
  letter-spacing: 0.05em;
}

.hud-score {
  font-size: 1.25rem;
  font-weight: 700;
}

.board {
  position: relative;
  width: 100%;
  aspect-ratio: 1 / 1;
  border-radius: 14px;
  overflow: hidden;
  box-shadow: 0 0 0 2px #1e2b45, 0 18px 40px rgba(0, 0, 0, 0.45);
}

canvas {
  display: block;
  width: 100%;
  height: 100%;
  touch-action: none;
}

.overlay {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: 20px;
  text-align: center;
  background: rgba(8, 14, 26, 0.82);
}

.overlay.hidden {
  display: none;
}

.overlay h1 {
  margin: 0;
  font-size: 1.6rem;
}

.overlay p {
  margin: 0;
  color: #a9bcd6;
  font-size: 0.95rem;
  line-height: 1.5;
  /* 문구에 넣은 줄바꿈(\n)을 그대로 표시한다. */
  white-space: pre-line;
}

.overlay .ranking-status {
  min-height: 1.2em;
  color: #ffd166;
  font-size: 0.9rem;
  font-weight: 600;
}

#start-btn {
  margin-top: 6px;
  padding: 10px 28px;
  font-size: 1rem;
  font-weight: 600;
  color: #06210f;
  background: #6ee78f;
  border: none;
  border-radius: 999px;
  cursor: pointer;
}

#start-btn:active {
  background: #57c979;
}

/* 방향 버튼은 스마트폰·태블릿처럼 터치로 조작하는 기기에서만 보인다. */
.dpad {
  display: none;
  grid-template-columns: repeat(3, 1fr);
  gap: 6px;
  width: min(64vmin, 260px);
}

@media (hover: none) and (pointer: coarse) {
  .dpad {
    display: grid;
  }
}

/* 미디어 쿼리 판정이 어긋나는 브라우저를 위해 JS가 붙인 클래스도 함께 쓴다. */
html.is-touch .dpad {
  display: grid;
}

.dpad-btn {
  min-height: 52px;
  padding: 12px 0;
  font-size: 1.25rem;
  color: #cfe0f5;
  background: #16213a;
  border: 1px solid #22304d;
  border-radius: 10px;
  cursor: pointer;
  touch-action: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

.dpad-btn:active {
  background: #24365c;
}

.dpad .up {
  grid-column: 2;
  grid-row: 1;
}

.dpad .left {
  grid-column: 1;
  grid-row: 2;
}

.dpad .down {
  grid-column: 2;
  grid-row: 2;
}

.dpad .right {
  grid-column: 3;
  grid-row: 2;
}
