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

html,
body {
  overscroll-behavior: none;
}

body {
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #0f1020;
  color: #e8e8f0;
  font-family: "Segoe UI", "Malgun Gothic", sans-serif;
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

.game {
  display: flex;
  gap: 24px;
  padding: 16px;
}

.board-wrap {
  position: relative;
  border: 3px solid #3a3d6b;
  border-radius: 6px;
  box-shadow: 0 0 30px rgba(90, 100, 255, 0.25);
  line-height: 0;
  align-self: flex-start;
}

#board {
  display: block;
  background: #15172e;
  touch-action: none;
}

.overlay {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  padding: 16px;
  background: rgba(10, 10, 25, 0.8);
  line-height: 1.4;
  text-align: center;
  cursor: pointer;
}

.overlay.hidden {
  display: none;
}

.overlay h2 {
  font-size: 32px;
  letter-spacing: 4px;
}

.overlay p {
  color: #b0b3d6;
}

.side {
  width: 180px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.side h1 {
  font-size: 28px;
  letter-spacing: 6px;
  background: linear-gradient(90deg, #00f0f0, #a000f0, #f0a000);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}

.panel {
  background: #1b1d3a;
  border: 1px solid #3a3d6b;
  border-radius: 6px;
  padding: 12px;
}

.panel h3 {
  font-size: 13px;
  letter-spacing: 2px;
  color: #8e92c8;
  margin-bottom: 8px;
}

#next {
  display: block;
  margin: 0 auto;
}

.stats div {
  display: flex;
  justify-content: space-between;
  padding: 4px 0;
}

.stats span {
  color: #8e92c8;
}

.controls ul {
  list-style: none;
  font-size: 13px;
}

.controls li {
  padding: 3px 0;
}

kbd {
  display: inline-block;
  min-width: 22px;
  padding: 1px 5px;
  border: 1px solid #50548a;
  border-radius: 4px;
  background: #262950;
  font-family: inherit;
  font-size: 11px;
  text-align: center;
}

/* ---------- 터치 버튼 ---------- */

.touch-controls {
  display: none;
  grid-template-columns: repeat(3, 1fr) 1.2fr;
  grid-template-rows: 1fr 1fr;
  gap: 8px;
  width: 100%;
  touch-action: none;
}

.touch-controls button {
  min-height: 56px;
  border: 1px solid #50548a;
  border-radius: 12px;
  background: #262950;
  color: #e8e8f0;
  font-size: 22px;
  font-family: inherit;
  touch-action: none;
  cursor: pointer;
}

.touch-controls button:active,
.touch-controls button.pressed {
  background: #3d4180;
  transform: scale(0.96);
}

/* 왼쪽 3칸: 이동/내리기, 오른쪽 칸: 회전 */
.touch-controls [data-action="left"] { grid-column: 1; grid-row: 1; }
.touch-controls [data-action="down"] { grid-column: 2; grid-row: 1; }
.touch-controls [data-action="right"] { grid-column: 3; grid-row: 1; }
.touch-controls [data-action="rotate"] { grid-column: 4; grid-row: 1 / span 2; font-size: 34px; }
.touch-controls [data-action="drop"] { grid-column: 1 / span 2; grid-row: 2; font-size: 16px; letter-spacing: 2px; }
.touch-controls [data-action="pause"] { grid-column: 3; grid-row: 2; font-size: 18px; }

/* ---------- 모바일 / 터치 기기 ---------- */

@media (max-width: 700px), (pointer: coarse) {
  body {
    align-items: flex-start;
  }

  .game {
    flex-direction: column;
    align-items: center;
    gap: 10px;
    width: 100%;
    max-width: 420px;
    padding: 10px 12px calc(10px + env(safe-area-inset-bottom));
  }

  /* 점수판을 보드 위 가로 한 줄로 */
  .side {
    order: -1;
    width: 100%;
    flex-direction: row;
    align-items: stretch;
    gap: 8px;
  }

  .side h1,
  .controls {
    display: none;
  }

  .panel {
    padding: 6px 10px;
  }

  .panel h3 {
    font-size: 11px;
    margin-bottom: 2px;
  }

  .next-panel {
    flex: 0 0 auto;
  }

  #next {
    width: 56px;
    height: 56px;
  }

  .stats {
    flex: 1;
    display: flex;
    justify-content: space-around;
    align-items: center;
  }

  .stats div {
    flex-direction: column;
    align-items: center;
    padding: 0;
    font-size: 13px;
  }

  .stats strong {
    font-size: 18px;
  }

  .board-wrap {
    align-self: center;
  }

  /* 화면 높이에 맞춰 보드 크기 조절 (점수판 + 버튼 영역을 뺀 나머지) */
  #board {
    height: min(600px, calc(100dvh - 250px));
    width: auto;
    aspect-ratio: 1 / 2;
  }

  .overlay h2 {
    font-size: 26px;
  }

  .touch-controls {
    display: grid;
  }
}
