: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: 16px;
}

/* 점수바: SCORE | TIME | 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; }

/* 시간 표시 */
.time-unit { font-size: 13px; font-weight: 700; color: #a0a0a0; }
.score-box.low .time-val,
.score-box.low .time-unit { color: #ff5d5d; }

/* 게임 영역 — 보드 + 키패드 세로 스택 */
#game {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 0;
}

.kak {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: clamp(12px, 3vh, 22px);
  width: 100%;
}

/* 보드 — 마스크 비율(--c / --r) 유지, 높이 62vh 이내로 제한 */
.kak-board {
  --c: 5;
  --r: 5;
  display: grid;
  grid-template-columns: repeat(var(--c), 1fr);
  grid-template-rows: repeat(var(--r), 1fr);
  width: min(92vw, 60vh * var(--c) / var(--r), 420px);
  aspect-ratio: var(--c) / var(--r);
  gap: 2px;
  padding: 2px;
  background: #2c2c38;
  border-radius: 8px;
  overflow: hidden;
}

/* 흰 칸 */
.kak-cell {
  position: relative;
  background: #1b1b26;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.kak-num {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: clamp(18px, 6.5vw, 30px);
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  color: #e9edff;
}
.kak-cell.selected {
  background: #1f3556;
  box-shadow: inset 0 0 0 2px #4f9eff;
}
.kak-cell.bad { background: #4a2230; }
.kak-cell.bad .kak-num { color: #ff6b6b; }

/* 클루(검은) 칸 — 합이 있으면 대각선 + 숫자 */
.kak-clue-cell {
  position: relative;
  background: #0e0e14;
}
.kak-clue-cell.has-clue {
  background-color: #0e0e14;
  background-image: linear-gradient(
    to bottom right,
    transparent calc(50% - 0.6px),
    #555568 calc(50% - 0.6px),
    #555568 calc(50% + 0.6px),
    transparent calc(50% + 0.6px)
  );
}
.kak-down,
.kak-right {
  position: absolute;
  font-size: clamp(9px, 3vw, 13px);
  font-weight: 800;
  line-height: 1;
  font-variant-numeric: tabular-nums;
}
.kak-down { left: 3px; bottom: 3px; color: #9fb4ff; }   /* 세로(아래) 합 */
.kak-right { right: 3px; top: 3px; color: #ffd166; }     /* 가로(오른쪽) 합 */

/* 완성 플래시 */
.kak-board.solved-flash .kak-cell { background: #1d3a2a; }
.kak-board.solved-flash .kak-num { color: #6ee7a0; }

/* 키패드 — 1~9 + 지우기 (5열 2행) */
.kak-pad {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 7px;
  width: min(92vw, 420px);
}
.kak-key {
  font: inherit;
  color: #f5f5f5;
  background: #1c1c26;
  border: 1px solid #34343f;
  border-radius: 10px;
  min-height: clamp(44px, 7vh, 56px);
  font-size: clamp(18px, 5.5vw, 24px);
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.kak-key:active { background: #2a2a38; }
.kak-erase {
  color: #ff9d9d;
  font-size: clamp(16px, 4.5vw, 20px);
}

.overlay-sub { margin: -4px 0 4px; font-size: 14px; font-weight: 600; color: #a0a0a0; }
.overlay-howto { margin: 0 0 4px; font-size: 13px; line-height: 1.45; color: #b8b8b8; }
.overlay-card .start-btn { background: #4f9eff; border-color: #4f9eff; }

/* 좌상단 ← 홈, 우상단 🏆 명예의 전당 — 모든 게임 공통 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;
}
