:root {
  color-scheme: dark;
}

* {
  box-sizing: border-box;
  touch-action: manipulation;
}

html, body {
  margin: 0;
  padding: 0;
  height: 100dvh;
  overflow: hidden;
  background: #0a0a0a;
  color: #f5f5f5;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Apple SD Gothic Neo", "Noto Sans KR", sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  -webkit-user-select: none;
  overscroll-behavior: none;
}

main {
  height: 100%;
  max-width: 480px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  padding:
    max(60px, env(safe-area-inset-top))
    16px
    max(20px, env(safe-area-inset-bottom));
  gap: 10px;
}

/* 점수바: SCORE | BEST | New 버튼 */
.score-bar {
  display: flex;
  gap: 8px;
  align-items: stretch;
}

.score-box {
  flex: 1;
  background: #161616;
  border: 1px solid #2a2a2a;
  border-radius: 12px;
  padding: 8px 12px;
  text-align: center;
  min-width: 0;
}

.score-label {
  display: block;
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: #a0a0a0;
}

.score-val,
.best-val {
  display: block;
  font-size: 22px;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.02em;
  color: #f5f5f5;
  margin-top: 2px;
  line-height: 1.1;
}

.new-btn {
  flex: 0 0 auto;
  font: inherit;
  color: #fff;
  background: #4f9eff;
  border: none;
  border-radius: 12px;
  padding: 0 18px;
  min-height: 56px;
  cursor: pointer;
  font-weight: 700;
  font-size: 14px;
  -webkit-tap-highlight-color: transparent;
}

.new-btn:active { background: #3d8ae8; }

/* 게임 영역 — 보드/캠버스/그리드 등을 여기에 */
#game {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 0;   /* 작은 폰에서 아래 조작 요소가 화면 밖으로 밀리지 않게 */
}

/* 좌상단 ← 홈, 우상단 🏆 명예의 전당 — 모든 게임 공통 chrome */
.home-link,
.hof-link {
  position: fixed;
  top: max(12px, env(safe-area-inset-top));
  z-index: 10;
  width: 40px;
  height: 40px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 999px;
  background: rgba(0, 0, 0, 0.35);
  color: #fff;
  text-decoration: none;
  font-size: 20px;
  font-weight: 600;
  line-height: 1;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  transition: background 0.15s ease;
  border: none;
  cursor: pointer;
  font-family: inherit;
}

.home-link { left:  max(12px, env(safe-area-inset-left)); }
.hof-link  { right: max(12px, env(safe-area-inset-right)); }

@media (hover: hover) {
  .home-link:hover,
  .hof-link:hover {
    background: rgba(0, 0, 0, 0.55);
  }
}

/* 오버레이 (게임 종료 / 완료 시 점수 + 저장 버튼) */
.overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 20;
  padding: 20px;
  backdrop-filter: blur(2px);
  -webkit-backdrop-filter: blur(2px);
}

.overlay[hidden] { display: none; }

.overlay-card {
  background: #161616;
  border: 1px solid #2a2a2a;
  border-radius: 18px;
  padding: 24px 22px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  align-items: center;
  text-align: center;
  min-width: 240px;
  max-width: 320px;
  width: 100%;
}

.overlay-card h2 {
  margin: 0;
  font-size: 22px;
  font-weight: 800;
  letter-spacing: -0.02em;
}

.overlay-score {
  margin: 0;
  font-size: 56px;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.04em;
  line-height: 1;
}

.overlay-card button {
  font: inherit;
  color: #fff;
  background: rgba(255, 255, 255, 0.10);
  border: 1px solid rgba(255, 255, 255, 0.22);
  border-radius: 999px;
  padding: 12px 20px;
  min-height: 44px;
  cursor: pointer;
  font-weight: 600;
  font-size: 14px;
  width: 100%;
  -webkit-tap-highlight-color: transparent;
}

.overlay-card button:active {
  background: rgba(255, 255, 255, 0.18);
}

.overlay-card .save-btn {
  background: #4f9eff;
  border-color: #4f9eff;
}

.overlay-card .save-btn:active {
  background: #3d8ae8;
}


/* ─── Number Neighbors ─────────────────────────────────── */
:root {
  --v1: #5cc8ff;
  --v2: #ffd04d;
  --v3: #ff8f5e;
  --v4: #c99bff;
  --bad: #ff5d6c;
  --ok: #47d98a;
}

.score-box { padding: 8px 6px; }
.score-val, .best-val { font-size: 20px; }
.time-val.low { color: var(--bad); }

.clock-track {
  height: 6px;
  border-radius: 999px;
  background: #1c1f26;
  overflow: hidden;
  flex: 0 0 auto;
}
.clock-fill {
  height: 100%;
  width: 100%;
  border-radius: inherit;
  background: linear-gradient(90deg, #3fbf7f, #5cc8ff);
  transform-origin: left center;
  transform: scaleX(1);
}
.clock-fill.warn { background: linear-gradient(90deg, #f0a030, #ffd04d); }
.clock-fill.danger { background: var(--bad); animation: nn-pulse 0.6s ease-in-out infinite alternate; }
@keyframes nn-pulse { from { opacity: 1; } to { opacity: 0.45; } }

#game { position: relative; flex-direction: column; gap: 10px; }

.board {
  --n: 5;
  --side: 300px;
  flex: 0 0 auto;
  width: var(--side);
  height: var(--side);
  display: grid;
  grid-template-columns: repeat(var(--n), 1fr);
  grid-template-rows: repeat(var(--n), 1fr);
  gap: 3px;
  padding: 5px;
  border-radius: 14px;
  background: #12151b;
  box-shadow: 0 0 0 1px #262b35, 0 10px 30px rgba(0, 0, 0, 0.45);
}

.cell {
  position: relative;
  appearance: none;
  border: none;
  margin: 0;
  padding: 0;
  border-radius: 8px;
  background: #222834;
  color: #f2f5fa;
  font: inherit;
  font-weight: 700;
  font-size: calc(var(--side) / var(--n) * 0.46);
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  font-variant-numeric: tabular-nums;
  transition: background 0.12s ease, transform 0.12s ease;
  -webkit-tap-highlight-color: transparent;
}
.cell.v1 { color: var(--v1); }
.cell.v2 { color: var(--v2); }
.cell.v3 { color: var(--v3); }
.cell.v4 { color: var(--v4); }

/* given numbers: solid tile in the value colour */
.cell.given { font-weight: 900; color: #10131a; cursor: default; }
.cell.given.v1 { background: var(--v1); }
.cell.given.v2 { background: var(--v2); }
.cell.given.v3 { background: var(--v3); }
.cell.given.v4 { background: var(--v4); }

/* player numbers: dark tile with a coloured inner ring */
.cell.num { background: #1a1f29; box-shadow: inset 0 0 0 2px currentColor; }

.cell.empty::after {
  content: "";
  width: 18%;
  height: 18%;
  background:
    linear-gradient(45deg, transparent 40%, #4b5262 40%, #4b5262 60%, transparent 60%),
    linear-gradient(-45deg, transparent 40%, #4b5262 40%, #4b5262 60%, transparent 60%);
}
.cell.empty { background: #171b22; }

.cell.sel { outline: 3px solid #ffffff; outline-offset: -1px; z-index: 1; transform: scale(1.04); }
.cell.given.sel { outline-color: rgba(255, 255, 255, 0.55); }

.cell.bad { box-shadow: inset 0 0 0 3px var(--bad); animation: nn-shake 0.25s ease; }
.cell.given.bad { box-shadow: 0 0 0 3px var(--bad), inset 0 0 0 2px #10131a; z-index: 1; }
@keyframes nn-shake {
  0%, 100% { translate: 0 0; } 25% { translate: -2px 0; } 75% { translate: 2px 0; }
}
.cell.done::before {
  content: "";
  position: absolute;
  right: 9%;
  top: 9%;
  width: 16%;
  height: 16%;
  min-width: 6px;
  min-height: 6px;
  border-radius: 50%;
  background: var(--ok);
  box-shadow: 0 0 0 2px #10131a;
}

/* revealed solution after time-out */
.cell.reveal { background: #161a21; opacity: 0.85; outline: 2px dashed currentColor; outline-offset: -5px; }

/* level clear wave */
.board.won .cell.num, .board.won .cell.given { animation: nn-pop 0.5s ease both; animation-delay: var(--d, 0s); }
@keyframes nn-pop {
  0% { transform: scale(1); }
  40% { transform: scale(1.16); filter: brightness(1.35); }
  100% { transform: scale(1); }
}
.board.won { box-shadow: 0 0 0 2px var(--ok), 0 0 28px rgba(71, 217, 138, 0.35); }

.hint {
  margin: 0;
  min-height: 2.6em;
  font-size: 12.5px;
  line-height: 1.3;
  color: #aeb6c4;
  text-align: center;
  flex: 0 0 auto;
  width: 100%;
}
.hint.err { color: #ff8a95; }
.hint b { color: #fff; }

.pad {
  flex: 0 0 auto;
  width: 100%;
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: 6px;
}
.pad-btn {
  appearance: none;
  font: inherit;
  font-size: 22px;
  font-weight: 800;
  min-height: 48px;
  border-radius: 12px;
  border: 1px solid #2c3240;
  background: #1a1e27;
  color: #fff;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.pad-btn:active { background: #2a3140; transform: scale(0.96); }
.pad-btn.v1 { color: var(--v1); }
.pad-btn.v2 { color: var(--v2); }
.pad-btn.v3 { color: var(--v3); }
.pad-btn.v4 { color: var(--v4); }
.pad-btn.vx, .pad-btn.vdel { color: #b9c1cf; font-size: 20px; }
.pad-btn:disabled { opacity: 0.4; }

/* toast between levels */
.toast {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  background: rgba(14, 22, 18, 0.97);
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.5);
  border: 1px solid #2f6b4c;
  color: #fff;
  border-radius: 16px;
  padding: 14px 22px;
  text-align: center;
  font-weight: 800;
  font-size: 20px;
  z-index: 3;
  pointer-events: none;
  animation: nn-toast 0.3s ease both;
  white-space: nowrap;
}
.toast small { display: block; font-size: 13px; font-weight: 600; color: #9fe6bf; margin-top: 4px; }
@keyframes nn-toast { from { opacity: 0; transform: translate(-50%, -40%); } to { opacity: 1; transform: translate(-50%, -50%); } }

/* start / result cards */
.overlay-howto { margin: 0; font-size: 13px; line-height: 1.45; color: #c8cfdb; text-align: left; }
.overlay-howto li { margin: 2px 0; }
.overlay-howto ol { margin: 0; padding-left: 18px; }
.overlay-sub { margin: 0; font-size: 14px; color: #aeb6c4; }
.example {
  display: grid;
  grid-template-columns: repeat(3, 30px);
  gap: 3px;
  padding: 4px;
  background: #12151b;
  border-radius: 9px;
}
.example span {
  width: 30px; height: 30px;
  border-radius: 6px;
  display: flex; align-items: center; justify-content: center;
  font-weight: 900; font-size: 16px; color: #10131a;
  background: #171b22;
}
.example .v1 { background: var(--v1); }
.example .v2 { background: var(--v2); }
.example .v3 { background: var(--v3); }
.overlay-card .start-btn { background: #4f9eff; border-color: #4f9eff; }

/* short phones: keep the start card clear of the fixed title bar */
@media (max-height: 640px) {
  .overlay-card { gap: 8px; padding: 16px 18px; }
  .overlay-card h2 { font-size: 19px; }
  .example { grid-template-columns: repeat(3, 24px); }
  .example span { width: 24px; height: 24px; font-size: 13px; }
  .overlay-howto { font-size: 12px; line-height: 1.35; }
  .overlay-card .overlay-sub { font-size: 12.5px; }
  .overlay-card button { min-height: 40px; padding: 9px 18px; }
}
