:root {
  --bg: #161a38;
  --off: #262a52;
  --on: #ffb020;
  --dim: #7b81b4;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  height: 100%;
  background-color: var(--bg);
  color: #fff;
  font: 500 16px/1.4 system-ui, -apple-system, "Segoe UI", sans-serif;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  overscroll-behavior: none;
}

body {
  display: flex;
  flex-direction: column;
  padding: env(safe-area-inset-top) 0 env(safe-area-inset-bottom);
}

/* 배경 이미지는 배경색과 따로 선언한다. 이 규칙이 통째로 실패해도 위의 배경색과
   화면 내용은 그대로 남는다. 배경 하나 때문에 게임이 안 보이는 일은 없어야 한다. */
body {
  background-image: linear-gradient(rgba(22, 26, 56, .82), rgba(22, 26, 56, .92)),
                    url("../assets/bg.webp");
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
}

header {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  padding: 1.2rem 1.4rem;
  color: var(--dim);
  font-variant-numeric: tabular-nums;
}
#level { color: #fff; font-weight: 700; }
#stars {
  flex-basis: 100%;
  margin-top: .35rem;
  color: #ffd36a;
  font-size: .82rem;
  text-align: right;
}

main {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2rem;
}

#board {
  display: grid;
  grid-template-columns: repeat(var(--n, 3), 1fr);
  grid-template-rows: repeat(var(--n, 3), 1fr);
  gap: 3%;
  /* 52vh 상한이 없으면 넓고 낮은 창에서 판이 커져 아래 버튼이 잘린다 */
  width: min(90vw, 400px, 52vh);
  aspect-ratio: 1;
  transition: transform .25s;
}

.cell {
  border: 0;
  padding: 0;
  border-radius: 18%;
  background: var(--off);
  cursor: pointer;
  transition: background-color .18s, box-shadow .18s, transform .12s;
}
.cell.on {
  background: var(--on);
  box-shadow: 0 0 1.5rem -0.4rem var(--on);
}
.cell:active { transform: scale(.92); }
.cell.hint { box-shadow: inset 0 0 0 3px #fff; }

#board.cleared { transform: scale(1.06); }
#board.cleared .cell { background: var(--on); box-shadow: 0 0 2rem -0.3rem var(--on); }
#board.perfect { transform: scale(1.06) rotate(-2deg); }

#tip {
  color: var(--dim);
  font-size: .9rem;
  text-align: center;
  padding: 0 1.5rem;
}
#tip[hidden] { display: none; }

footer {
  display: flex;
  gap: .6rem;
  padding: 1rem 1.2rem 1.6rem;
}
footer button {
  flex: 1;
  padding: .95rem 0;
  border: 0;
  border-radius: .9rem;
  background: var(--off);
  color: #fff;
  font: inherit;
  font-size: .92rem;
  cursor: pointer;
}
footer button:disabled { opacity: .35; cursor: default; }
footer button:active:not(:disabled) { transform: scale(.97); }

/* ---- 설명 오버레이 (세 게임 공통 구조) ---- */
#help-open {
  background: none;
  border: 0;
  color: var(--dim);
  font: inherit;
  font-weight: 700;
  cursor: pointer;
  padding: 0 .2rem;
}
.overlay {
  position: fixed;
  inset: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1.4rem;
  background: rgba(0, 0, 0, .72);
}
.overlay[hidden] { display: none; }
.sheet {
  width: 100%;
  max-width: 22rem;
  background: var(--off);
  border-radius: 1.2rem;
  padding: 1.5rem 1.3rem;
}
/* 작은 화면에서 설명이 화면보다 길어져도 버튼에 닿을 수 있어야 한다 */
.sheet { max-height: 100%; overflow-y: auto; }

.sheet h1 { font-size: 1.15rem; margin-bottom: 1rem; }
.sheet > div {
  background: var(--bg);
  border-radius: .7rem;
  padding: .6rem;
}
.sheet ul {
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: .6rem;
  font-size: .9rem;
  line-height: 1.5;
  color: var(--dim);
  margin: 1.1rem 0 1.3rem;
}
.sheet li b { color: #fff; font-weight: 600; }
.sheet button {
  width: 100%;
  padding: .95rem;
  border: 0;
  border-radius: .9rem;
  background: var(--on);
  color: var(--bg);
  font: inherit;
  font-weight: 700;
  cursor: pointer;
}

.result-sheet { text-align: center; }
.result-sheet h1 { margin-bottom: .35rem; }
#result-stars {
  color: #ffd36a;
  font-size: 2.4rem;
  letter-spacing: .18rem;
  text-shadow: 0 0 1.2rem rgba(255, 176, 32, .5);
}
#result-best {
  display: block;
  min-height: 1.4rem;
  margin: .2rem 0 .45rem;
  color: var(--on);
  font-size: .86rem;
}
#result-summary {
  margin-bottom: 1.1rem;
  color: var(--dim);
  font-size: .9rem;
}

/* 세로 전용. 폰을 눕히면 게임 대신 안내만 띄운다.
   실제 앱은 AndroidManifest 의 screenOrientation="portrait" 로 못 눕히게 막는다. */
#rotate { display: none; }
@media (orientation: landscape) and (max-height: 520px) {
  header, main, footer, .overlay { display: none !important; }
  #rotate {
    display: flex;
    position: fixed;
    inset: 0;
    align-items: center;
    justify-content: center;
    padding: 2rem;
    text-align: center;
    color: var(--dim);
    font-size: 1.05rem;
    line-height: 1.6;
  }
}

/* 폰 크기를 넘어가면 가운데로 모은다 (데스크톱에서 늘어지지 않게) */
header, footer { width: 100%; max-width: 26rem; margin-inline: auto; }
