/* 트리플 타일 — 형제 게임들의 헤더·푸터·오버레이 구조를 그대로 쓰고, 판만 갈았다.
   판은 DOM 이다. 타일 하나가 버튼 하나고, 슬롯으로 날아가는 연출은 FLIP 으로 한다 —
   캔버스로 그리면 그 연출을 좌표 보간으로 다시 짜야 한다. */
:root {
  /* 팔레트는 힉스필드 컨셉(호두나무 탁자 + 앰버 조명)에 맞췄다 — scratchpad hf/c-tritile.png */
  --bg-top: #3a2a1c;
  --bg-bottom: #201410;
  --pill: #f0e2cf;
  --pill-ink: #4a3826;
  --ink: #f5ecdf;
  --dim: #c2a98c;
  --accent: #ffb85c;
  --tile-face: #f8f4e9;
}

* { 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, 42, 28, 0.35), rgba(32, 20, 16, 0.7) 70%),
    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(213, 220, 245, .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 {
  position: relative; /* 타일들의 좌표 기준 — 판·슬롯을 한 좌표계로 묶는다 */
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: .5rem;
  min-height: 0;
  width: 100%;
}

[hidden] { display: none !important; }

/* ---- 판 ---- */
/* 크기는 JS 가 레벨마다 직접 넣는다 — 판 비율이 레벨마다 다르다(12×8 ~ 12×20 반칸). */
#board {
  position: relative;
  border-radius: .9rem;
  background: rgba(10, 14, 30, .5);
  box-shadow: inset 0 0 0 1px rgba(216, 224, 255, .14), 0 1rem 1.6rem rgba(0, 0, 0, .45);
  transition: transform .25s;
}
#board.cleared { transform: scale(1.03); }

/* 타일 하나. 위치·크기는 JS 가 넣는다. 슬롯 안 밀림은 left 전환이 공짜로 해 준다 —
   판→슬롯 비행은 FLIP 이라 그동안 JS 가 전환을 잠깐 끈다. */
.tile {
  position: absolute;
  transition: left .16s ease-out, top .16s ease-out, width .16s, height .16s;
  border: 0;
  padding: 0;
  border-radius: 12%;
  background: linear-gradient(180deg, #fffdf6, var(--tile-face) 55%, #e3dcc8);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, .9), inset 0 -6% 0 rgba(120, 105, 70, .35),
    0 3px 6px rgba(0, 0, 0, .45);
  cursor: pointer;
  touch-action: manipulation;
}
/* 무늬는 아틀라스(assets/tiles.webp, 7×4칸) 한 장에서 잘라 쓴다.
   배경 스프라이트라 img 태그가 필요 없고, 좌표는 JS 가 %로 넣는다. */
.tile .face {
  position: absolute;
  inset: 9% 9% 12%;
  background-image: url("../assets/tiles.webp");
  background-size: 700% 400%;
  background-repeat: no-repeat;
  pointer-events: none;
}
/* 덮인 타일은 어둡다 — "집을 수 있는 게 어느 것인가" 가 이 게임의 전부라서
   같은 밝기로 두면(마작 방식) 자유 배치 판에서는 읽히지 않는다. */
.tile.covered {
  filter: brightness(.5) saturate(.75);
  cursor: default;
}
.tile.hintglow {
  animation: hintblink .36s ease-in-out 3;
}
@keyframes hintblink {
  50% { filter: brightness(1.35) drop-shadow(0 0 8px #ffd76a); }
}

/* ---- 슬롯 ---- */
#slot {
  position: relative;
  border-radius: .8rem;
  background: rgba(10, 14, 30, .62);
  box-shadow: inset 0 0 0 1px rgba(216, 224, 255, .18), inset 0 4px 10px rgba(0, 0, 0, .5);
}
#slot .cell {
  position: absolute;
  border-radius: 10%;
  background: rgba(255, 255, 255, .05);
  box-shadow: inset 0 0 0 1px rgba(216, 224, 255, .1);
}
#slot.danger { box-shadow: inset 0 0 0 2px rgba(255, 120, 100, .75), inset 0 4px 10px rgba(0, 0, 0, .5); }

#tip {
  color: var(--dim);
  font-size: .88rem;
  font-weight: 600;
  text-align: center;
  padding: 0 1.5rem;
  min-height: 1.4em;
}
#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, #eef1ff, 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 #adb6dd, 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 #adb6dd;
}

/* ---- 설명·결과 오버레이 (형제 게임들과 공통 구조) ---- */
#help-open {
  padding: .4rem .7rem;
  border: 0;
  border-radius: 999px;
  background: rgba(213, 220, 245, .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(6, 9, 22, .82);
}
.overlay[hidden] { display: none; }
.sheet {
  width: 100%;
  max-width: 22rem;
  max-height: 100%;
  overflow-y: auto;
  background: linear-gradient(180deg, #2a3157, #1b2140);
  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, #c3ccff, var(--accent));
  color: #171c38;
  font: inherit;
  font-weight: 800;
  cursor: pointer;
  box-shadow: inset 0 2px 0 rgba(255, 255, 255, .8), inset 0 -3px 0 #6474d8;
}
.sheet button.ghost {
  margin-bottom: .6rem;
  background: linear-gradient(180deg, #eef1ff, var(--pill));
  color: var(--pill-ink);
  box-shadow: inset 0 2px 0 rgba(255, 255, 255, .9), inset 0 -3px 0 #adb6dd;
}
.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;
}

/* 패배 — 왜 졌는지 슬롯을 **크게** 보여 준다. */
.fail-why { color: var(--dim); font-size: .88rem; margin-bottom: .7rem; }
#fail-slot {
  display: flex;
  justify-content: center;
  gap: .3rem;
  padding: .7rem .5rem;
  margin-bottom: .9rem;
}
#fail-slot .big {
  position: relative;
  width: 2.4rem;
  height: 3rem;
  border-radius: .4rem;
  background: linear-gradient(180deg, #fffdf6, var(--tile-face) 55%, #e3dcc8);
  box-shadow: 0 2px 5px rgba(0, 0, 0, .45);
}
#fail-slot .big .face {
  position: absolute;
  inset: 9% 9% 12%;
  background-image: url("../assets/tiles.webp");
  background-size: 700% 400%;
  background-repeat: no-repeat;
}
.fail-note { color: var(--dim); font-size: .85rem; margin-bottom: 1rem; }

/* 세로 전용. 실제 앱은 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, #7fe0b0, #ffd76a);
  transition: width .5s ease-out;
}
#result-chest { color: #ffd76a; font-weight: 800; }
#result-next-chest { opacity: .82; font-size: .92em; }
