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


/* ─── Tatami Numbers ───────────────────────────────────── */
:root {
  --straw: #d3c68a;
  --straw-hi: #e4d9a3;
  --straw-line: #e9dfae;
  --straw-dark: rgba(96, 82, 34, 0.16);
  --cloth: #26351f;
  --seam: #7a6630;
  --wood: #4a3420;
  --v1: #0f5f78;
  --v2: #a5321b;
  --v3: #433a9a;
  --v4: #2f6a22;
  --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, #8fae4a, #d3c68a);
  transform-origin: left center;
  transform: scaleX(1);
}
.clock-fill.warn { background: linear-gradient(90deg, #f0a030, #ffd04d); }
.clock-fill.danger { background: var(--bad); animation: tt-pulse 0.6s ease-in-out infinite alternate; }
@keyframes tt-pulse { from { opacity: 1; } to { opacity: 0.45; } }

#game { position: relative; flex-direction: column; gap: 10px; justify-content: flex-end; }
/* board centred in the free space, hint + pad kept low for the thumb */
#game .board { margin: auto 0; }
.nw { white-space: nowrap; }

/* the room: a wooden frame around the mats */
.board {
  --n: 6;
  --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);
  padding: 6px;
  border-radius: 10px;
  background: linear-gradient(135deg, #5a4027, var(--wood) 55%, #3b2918);
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.08), 0 10px 30px rgba(0, 0, 0, 0.5);
}

.cell {
  position: relative;
  appearance: none;
  margin: 0;
  padding: 0;
  border: 0 solid transparent;
  border-radius: 0;
  min-width: 0;
  min-height: 0;
  background-color: var(--straw);
  background-image: repeating-linear-gradient(0deg, transparent 0 3px, var(--straw-dark) 3px 4px);
  color: #2a2414;
  font: inherit;
  font-weight: 700;
  font-size: calc(var(--side) / var(--n) * 0.5);
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  font-variant-numeric: tabular-nums;
  transition: background-color 0.12s ease;
  -webkit-tap-highlight-color: transparent;
}
/* the weave runs along the mat */
.cell[data-d="v"] { background-image: repeating-linear-gradient(90deg, transparent 0 3px, var(--straw-dark) 3px 4px); }
.cell.hl-line { background-color: #dccf95; }
.cell.hl-mat { background-color: var(--straw-hi); }

.cell.v1 { color: var(--v1); }
.cell.v2 { color: var(--v2); }
.cell.v3 { color: var(--v3); }
.cell.v4 { color: var(--v4); }

/* given numbers: a round ink seal in the value colour */
.cell.given { color: #fbf6e4; font-weight: 800; cursor: default; font-size: calc(var(--side) / var(--n) * 0.42); }
.cell.given::before {
  content: "";
  position: absolute;
  width: 72%;
  height: auto;
  max-height: 72%;
  aspect-ratio: 1;
  border-radius: 50%;
  background: currentColor;
  z-index: 0;
}
.cell.given.v1::before { background: var(--v1); }
.cell.given.v2::before { background: var(--v2); }
.cell.given.v3::before { background: var(--v3); }
.cell.given.v4::before { background: var(--v4); }
.cell.given { isolation: isolate; }
.cell.given::before { z-index: -1; box-shadow: inset 0 -2px 0 rgba(0, 0, 0, 0.25); }

.cell.num { font-weight: 800; animation: tt-in 0.14s ease; }
@keyframes tt-in { from { transform: scale(0.6); opacity: 0.4; } to { transform: scale(1); opacity: 1; } }

.cell.sel { box-shadow: inset 0 0 0 3px #fffbe8, 0 0 0 2px rgba(255, 246, 200, 0.55), 0 0 14px rgba(255, 236, 150, 0.55); z-index: 2; background-color: #f1e9c0; }

.cell.bad { color: #c0142a; background-color: #f0b2a6; animation: tt-shake 0.25s ease; }
.cell.given.bad::before { box-shadow: 0 0 0 3px #ff3048; }
.cell.given.bad { color: #fbf6e4; background-color: #f0b2a6; }
@keyframes tt-shake {
  0%, 100% { translate: 0 0; } 25% { translate: -2px 0; } 75% { translate: 2px 0; }
}

/* revealed solution after time-out */
.cell.reveal { opacity: 0.75; font-weight: 600; text-decoration: underline dotted; text-underline-offset: 3px; }

/* level clear wave */
.board.won .cell { animation: tt-pop 0.5s ease both; animation-delay: var(--d, 0s); }
@keyframes tt-pop {
  0% { filter: brightness(1); }
  40% { filter: brightness(1.22) saturate(1.2); }
  100% { filter: brightness(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: #c5bf9f;
  text-align: center;
  flex: 0 0 auto;
  width: 100%;
}
.hint.err { color: #ff8a95; }
.hint b { color: #fff; }

.pad {
  --cols: 4;
  flex: 0 0 auto;
  width: 100%;
  display: grid;
  grid-template-columns: repeat(var(--cols), 1fr);
  gap: 8px;
}
.pad-btn {
  appearance: none;
  font: inherit;
  font-size: 24px;
  font-weight: 800;
  min-height: 48px;
  border-radius: 12px;
  border: 0;
  border-bottom: 3px solid var(--cloth);
  background-color: var(--straw);
  background-image: repeating-linear-gradient(0deg, transparent 0 3px, var(--straw-dark) 3px 4px);
  color: #2a2414;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.pad-btn:active { filter: brightness(1.12); transform: translateY(1px) scale(0.97); }
.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.vdel { background: #22252d; border-bottom-color: #14161b; color: #c9cfda; font-size: 20px; }

/* toast between levels */
.toast {
  position: absolute;
  left: 50%;
  top: 40%;
  transform: translate(-50%, -50%);
  background: rgba(20, 26, 16, 0.97);
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.5);
  border: 1px solid #6c7a3c;
  color: #fff;
  border-radius: 16px;
  padding: 14px 22px;
  text-align: center;
  font-weight: 800;
  font-size: 20px;
  z-index: 3;
  pointer-events: none;
  animation: tt-toast 0.3s ease both;
  white-space: nowrap;
}
.toast small { display: block; font-size: 13px; font-weight: 600; color: #d9e3a8; margin-top: 4px; }
@keyframes tt-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: #d2cdb6; 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: #b9b39a; }
.example {
  display: grid;
  grid-template-columns: repeat(4, 32px);
  grid-template-rows: repeat(3, 32px);
  grid-auto-flow: row;
  padding: 5px;
  background: var(--wood);
  border-radius: 8px;
}
.example span {
  display: flex; align-items: center; justify-content: center;
  font-weight: 800; font-size: 17px;
  background-color: var(--straw);
  border: 0 solid var(--seam);
}
.example span.v1 { color: var(--v1); }
.example span.v2 { color: var(--v2); }
.example span.v3 { color: var(--v3); }
.example .h { border-top: 3px solid var(--cloth); border-bottom: 3px solid var(--cloth);
  background-image: repeating-linear-gradient(0deg, transparent 0 3px, var(--straw-dark) 3px 4px); }
.example .h.e-l { border-left: 2px solid var(--seam); }
.example .h.e-r { border-right: 2px solid var(--seam); }
.example .v { border-left: 3px solid var(--cloth); border-right: 3px solid var(--cloth);
  background-image: repeating-linear-gradient(90deg, transparent 0 3px, var(--straw-dark) 3px 4px); }
.example .v.e-t { border-top: 2px solid var(--seam); }
.example .v.e-b { border-bottom: 2px solid var(--seam); }
.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(4, 25px); grid-template-rows: repeat(3, 25px); }
  .example span { 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; }
  .pad-btn { min-height: 44px; }
}
