/* 파이프 잇기 — 형제 게임들의 헤더·푸터·오버레이 구조를 그대로 쓰고, 판만 갈았다.
   판은 DOM 격자다. 탭 회전(0.15s transform 전환)과 물드는 색 전환을 CSS 가 공짜로 해 준다 —
   캔버스면 그 애니메이션을 손으로 짜야 한다. 칸이 최대 81개라 DOM 이 무겁지도 않다. */
:root {
  /* 팔레트는 힉스필드 컨셉(청록 설계도 + 구리 파이프 + 물빛)에 맞췄다 —
     design/neverending-blueprints 문서와 scratchpad hf/c-pipes.png 참고 */
  --bg-top: #143038;
  --bg-bottom: #0a1a20;
  --pill: #cfe6e3;
  --pill-ink: #21403e;
  --ink: #e4f2f0;
  --dim: #8fb2b8;
  --accent: #5ad0c8;

  /* 파이프 색: 마른 조각은 구리빛, 물이 닿으면 물빛으로 전환된다(app.js 가 .wet 을 붙인다) */
  --pipe: #8a6a4c;
  --pipe-wet: #4fd6f7;
  --house: #ffc46b;
  --source: #4fd6f7;
}

* { 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 — 청록 설계도) 위에 어두운 그라디언트를 겹쳐 판·글자가 뜨게 한다.
     그림이 안 실리면 그라디언트만 남으므로 조용히 안전하다. */
  background-image:
    radial-gradient(120% 80% at 50% 0%, rgba(20, 48, 56, 0.55), rgba(10, 26, 32, 0.8) 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, #moves, #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 {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: .8rem;
  min-height: 0;
  width: 100%;
}

/* ---- 판 ---- */
#board {
  display: grid;
  grid-template-columns: repeat(var(--n, 4), 1fr);
  gap: 0;
  width: min(92vw, 440px, calc(100vh - 260px));
  aspect-ratio: 1;
  padding: .5rem;
  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); }

#board .cell {
  position: relative;
  border: 0;
  padding: 0;
  background: none;
  cursor: pointer;
  color: var(--pipe);
  /* 탭 회전: 0.15초 전환. 물빛 전환은 svg 쪽(stroke=currentColor)이라 color 도 전환한다. */
  transition: transform .15s ease-out, color .3s;
  -webkit-tap-highlight-color: transparent;
}
#board .cell svg { display: block; width: 100%; height: 100%; }
/* 칸 경계가 살짝 보여야 조각 단위가 읽힌다 */
#board .cell::before {
  content: "";
  position: absolute;
  inset: 1px;
  border-radius: 6px;
  box-shadow: inset 0 0 0 1px rgba(216, 224, 255, .07);
  pointer-events: none;
}

#board .cell.wet { color: var(--pipe-wet); }
#board .cell.src { color: var(--source); }
/* 집 지붕은 별색 — 물이 닿으면 물빛으로 (svg 안의 .roof 가 fill=currentColor 가 아니라
   별도 변수라서 클래스로 갈아 준다) */
#board .cell .roof { fill: var(--house); transition: fill .3s; }
#board .cell.wet .roof { fill: var(--pipe-wet); }

/* 힌트: 틀린 칸 하나 반짝 */
@keyframes hintblink {
  0%, 100% { filter: none; }
  50% { filter: brightness(2.1) drop-shadow(0 0 6px rgba(255, 215, 106, .9)); }
}
#board .cell.hint-flash { animation: hintblink .45s 3; }

#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, #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;
}

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

/* 볼트 조각 — 네 귀에 리벳을 박아 "고정" 을 모양으로 알린다(색만으론 안 읽힌다). */
.cell.bolt::before {
  /* 잠긴 조각은 황동 테두리 판 — 리벳만으론 폰에서 안 읽힌다 */
  box-shadow: inset 0 0 0 2px rgba(202, 169, 111, 0.45);
}
.cell.bolt {
  background-image:
    radial-gradient(circle 3px at 14% 14%, #caa96f 0 45%, transparent 55%),
    radial-gradient(circle 3px at 86% 14%, #caa96f 0 45%, transparent 55%),
    radial-gradient(circle 3px at 14% 86%, #caa96f 0 45%, transparent 55%),
    radial-gradient(circle 3px at 86% 86%, #caa96f 0 45%, transparent 55%);
  cursor: default;
}
