/* 도미노 나누기 — 형제 게임들의 헤더·푸터·오버레이 구조를 그대로 쓰고, 판만 갈았다.
   판은 DOM 격자다. 칸 하나가 탭 버튼이라 캔버스보다 DOM 이 맞다(좌표 계산이 없다).
   놓인 도미노는 같은 그리드에서 2트랙을 스팬하는 오버레이 div — 픽셀 계산이 없다.
   자산 0 — 숫자·도미노 전부 DOM/CSS 로 그린다. */
:root {
  --bg-top: #3a2b20;
  --bg-bottom: #221712;
  --pill: #f0e4d2;
  --pill-ink: #4a3a28;
  --ink: #f6eee2;
  --dim: #c4ad92;
  --accent: #e0a35c;

  --cell: #4a3828;       /* 빈 칸 */
  --cell-top: #5a4633;
  --tile: #f4eee0;       /* 놓인 도미노(상아색 타일) */
  --tile-edge: #c9bfa5;
  --tile-ink: #2e2a44;
  --sel: #ffd76a;        /* 선택된 칸 */
  --bad: #a03a30;        /* 중복 조합 경고 */
}

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

html, body {
  height: 100%;
  background-color: var(--bg-bottom);
  color: var(--ink);
  font: 700 16px/1.4 system-ui, -apple-system, "Segoe UI", sans-serif;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  overscroll-behavior: none;
  touch-action: manipulation;
}

body {
  display: flex;
  flex-direction: column;
  padding: env(safe-area-inset-top) 0 env(safe-area-inset-bottom);
  /* 힉스필드 배경(assets/bg.webp — 에스프레소 리넨) 위에 어두운 그라디언트를 겹친다.
     url 은 CSS 파일 기준 상대경로(../) — pipes 에서 밟은 함정. */
  background-image:
    radial-gradient(120% 80% at 50% 0%, rgba(58, 43, 32, 0.45), rgba(34, 23, 18, 0.75) 72%),
    url("../assets/bg.webp");
  background-size: cover;
  background-position: center;
}

header {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  align-items: center;
  gap: .5rem;
  padding: 1rem 1.2rem .4rem;
  color: var(--dim);
  font-variant-numeric: tabular-nums;
}
#level, #left, #tokens {
  padding: .4rem .8rem;
  border-radius: 999px;
  background: rgba(232, 226, 246, .12);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, .18);
  font-size: .85rem;
}
#level { color: var(--ink); }
#stars {
  flex-basis: 100%;
  margin-top: .3rem;
  color: #ffd76a;
  font-size: .78rem;
  font-weight: 600;
  text-align: center;
}

main {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: .7rem;
  min-height: 0;
  padding: 0 .6rem;
}

/* ---- 판 ---- */
/* 격자 열 수·최대 폭은 JS 가 레벨마다 넣는다(N7 은 8×9 — 세로가 길어 폭을 dvh 로도 죈다). */
#board {
  display: grid;
  gap: 4px;
  width: 100%;
  max-width: 420px;
  padding: 8px;
  border-radius: .9rem;
  background: rgba(10, 7, 24, .5);
  box-shadow: inset 0 0 0 1px rgba(220, 208, 255, .14), 0 1rem 1.6rem rgba(0, 0, 0, .45);
  transition: transform .25s;
}
#board.cleared { transform: scale(1.02); }

#board .cell {
  aspect-ratio: 1;
  border: 0;
  border-radius: 16%;
  background: linear-gradient(180deg, var(--cell-top), var(--cell));
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, .1);
  font: inherit;
  font-size: clamp(.85rem, 4.4vw, 1.5rem);
  font-weight: 800;
  line-height: 1;
  color: var(--ink);
  cursor: pointer;
  padding: 0;
  touch-action: manipulation;
}
#board .cell:active { filter: brightness(1.2); }
#board .cell.selected {
  outline: 3px solid var(--sel);
  outline-offset: -3px;
  filter: brightness(1.25);
}
#board .cell.pulse { animation: pulse .55s ease-in-out 3; }
@keyframes pulse {
  0%, 100% { filter: brightness(1); }
  50% { filter: brightness(1.9); outline: 3px solid var(--sel); outline-offset: -3px; }
}

/* 놓인 도미노 — 살짝 밝은 상아색 판 + 테두리. 같은 그리드에서 2트랙을 스팬한다. */
#board .domino {
  display: flex;
  align-items: center;
  justify-content: space-around;
  z-index: 2;
  border: 0;
  border-radius: 14px;
  background: linear-gradient(180deg, #fffbf0, var(--tile));
  box-shadow: inset 0 0 0 2px var(--tile-edge), inset 0 2px 0 rgba(255, 255, 255, .9),
    0 3px 8px rgba(0, 0, 0, .4);
  color: var(--tile-ink);
  font: inherit;
  font-size: clamp(.85rem, 4.4vw, 1.5rem);
  font-weight: 800;
  line-height: 1;
  cursor: pointer;
  padding: 0;
  touch-action: manipulation;
  animation: drop .18s ease-out;
}
#board .domino.v { flex-direction: column; }
/* 반 나누는 선 — 진짜 도미노처럼 */
#board .domino::before {
  content: '';
  position: absolute;
  background: rgba(46, 42, 68, .25);
}
#board .domino { position: relative; }
#board .domino.h::before { left: 50%; top: 18%; bottom: 18%; width: 2px; }
#board .domino.v::before { top: 50%; left: 18%; right: 18%; height: 2px; }
#board .domino.dup {
  animation: shake .3s;
  box-shadow: inset 0 0 0 3px var(--bad), 0 3px 8px rgba(0, 0, 0, .4);
  background: linear-gradient(180deg, #ffd9d2, #f3b8ae);
}
@keyframes drop {
  from { transform: scale(1.15); opacity: .4; }
  to { transform: scale(1); opacity: 1; }
}
@keyframes shake {
  0%, 100% { transform: none; }
  30% { transform: translateX(-3px); }
  60% { transform: translateX(3px); }
}

/* ---- 남은 조합 목록 (이 게임 고유 UI — 남은 칩이 곧 힌트다) ---- */
#combos {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 5px;
  width: 100%;
  max-width: 420px;
  padding: 6px 8px;
  border-radius: .7rem;
  background: rgba(10, 7, 24, .35);
}
#combos .chip {
  display: inline-flex;
  align-items: center;
  border-radius: 6px;
  background: linear-gradient(180deg, #fffbf0, var(--tile));
  box-shadow: inset 0 0 0 1px var(--tile-edge);
  color: var(--tile-ink);
  font-size: .72rem;
  font-weight: 800;
  line-height: 1;
  font-variant-numeric: tabular-nums;
  transition: opacity .25s;
}
#combos .chip span {
  padding: .32em .45em;
}
#combos .chip span + span { border-left: 1px solid rgba(46, 42, 68, .3); }
#combos .chip.used { opacity: .12; }   /* 확정된 조합은 목록에서 지워진다 */

#tip {
  color: var(--dim);
  font-size: .88rem;
  font-weight: 600;
  text-align: center;
  padding: 0 1.5rem;
  min-height: 2.6em;
}
#tip.warn { color: #ffb27a; }

footer {
  display: flex;
  gap: .6rem;
  padding: .8rem 1.2rem 1.4rem;
}
footer button {
  flex: 1;
  padding: .9rem 0;
  border: 0;
  border-radius: 999px;
  background: linear-gradient(180deg, #f6f2fd, var(--pill));
  color: var(--pill-ink);
  font: inherit;
  font-size: .88rem;
  cursor: pointer;
  box-shadow: inset 0 2px 0 rgba(255, 255, 255, .9), inset 0 -3px 0 #b8adcf, 0 3px 6px rgba(0, 0, 0, .35);
}
footer button:disabled { opacity: .3; cursor: default; }
footer button:active:not(:disabled) {
  transform: translateY(2px);
  box-shadow: inset 0 2px 0 rgba(255, 255, 255, .9), inset 0 -1px 0 #b8adcf;
}

/* ---- 설명 오버레이 (형제 게임들과 공통 구조) ---- */
#help-open {
  padding: .4rem .7rem;
  border: 0;
  border-radius: 999px;
  background: rgba(232, 226, 246, .12);
  color: var(--dim);
  font: inherit;
  font-size: .85rem;
  cursor: pointer;
}
.overlay {
  position: fixed;
  inset: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1.4rem;
  background: rgba(8, 5, 20, .82);
}
.overlay[hidden] { display: none; }
.sheet {
  width: 100%;
  max-width: 22rem;
  max-height: 100%;
  overflow-y: auto;
  background: linear-gradient(180deg, #3b3266, #262047);
  border-radius: 1.4rem;
  padding: 1.5rem 1.3rem;
  box-shadow: inset 0 2px 0 rgba(255, 255, 255, .12), 0 1rem 2rem rgba(0, 0, 0, .5);
}
.sheet h1 { font-size: 1.15rem; margin-bottom: 1rem; color: var(--ink); }
.sheet > div {
  background: rgba(0, 0, 0, .28);
  border-radius: .9rem;
  padding: .6rem;
}
.sheet ul {
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: .6rem;
  font-size: .9rem;
  font-weight: 600;
  line-height: 1.5;
  color: var(--dim);
  margin: 1.1rem 0 1.3rem;
}
.sheet li b { color: var(--ink); }
.sheet button {
  width: 100%;
  padding: .95rem;
  border: 0;
  border-radius: 999px;
  background: linear-gradient(180deg, #d3c2fa, var(--accent));
  color: #241a45;
  font: inherit;
  font-weight: 800;
  cursor: pointer;
  box-shadow: inset 0 2px 0 rgba(255, 255, 255, .8), inset 0 -3px 0 #8467c4;
}
.sheet button.ghost {
  margin-bottom: .6rem;
  background: linear-gradient(180deg, #f6f2fd, var(--pill));
  color: var(--pill-ink);
  box-shadow: inset 0 2px 0 rgba(255, 255, 255, .9), inset 0 -3px 0 #b8adcf;
}
.sheet button.ghost[hidden] { display: none; }

.result-sheet { text-align: center; }
.result-sheet h1 { margin-bottom: .35rem; }
#result-stars {
  color: #ffd76a;
  font-size: 2.4rem;
  letter-spacing: .18rem;
  text-shadow: 0 0 1.2rem rgba(255, 215, 106, .55);
}
#result-best {
  display: block;
  min-height: 1.4rem;
  margin: .2rem 0 .45rem;
  color: var(--accent);
  font-size: .86rem;
}
#result-summary {
  margin-bottom: 1.1rem;
  color: var(--dim);
  font-size: .9rem;
  font-weight: 600;
}

/* 세로 전용. 실제 앱은 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; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition: none !important; animation: none !important; }
}

/* 잠긴 도구는 회색이 아니라 **자물쇠와 레벨**로 알린다 — 회색만이면 고장으로 오해한다. */
button.locked { opacity: .55; letter-spacing: .02em; }

/* 마일스톤 진행 바 — 다음 상자까지 얼마나 왔나. */
.chest-bar {
  width: 100%;
  height: .5rem;
  border-radius: .25rem;
  background: rgba(255, 255, 255, .14);
  overflow: hidden;
  margin: .1rem 0 .3rem;
}
.chest-bar > span {
  display: block;
  height: 100%;
  width: 0;
  border-radius: inherit;
  background: linear-gradient(90deg, var(--accent), #ffd76a);
  transition: width .5s ease-out;
}
#result-chest { color: #ffd76a; font-weight: 800; }
#result-next-chest { opacity: .82; font-size: .92em; }
