/* 말랑 젤리 아트 디렉션 (design/concept-jelly-01.png 기준).
   타일·벽·상자는 전부 CSS다 — 이미지로 뽑으면 200KB에 해상도가 고정되는데,
   그라데이션·라운드·하이라이트뿐이라 CSS가 더 작고 어떤 DPI에서도 선명하다. */
:root {
  --bg-top: #1e3448;
  --bg-bottom: #142434;

  --floor-hi: #e9f7ef;
  --floor-lo: #d0e9dc;
  --floor-edge: #b9d8c9;

  --wall-hi: #a3d2c4;
  --wall-lo: #7cb2a3;
  --wall-edge: #5e9184;

  --box-hi: #ffc0a1;
  --box-lo: #f28d63;
  --box-edge: #d97045;

  --done-hi: #8ff0c4;
  --done-lo: #4bd598;
  --done-edge: #2fae77;

  --stuck-hi: #ff9e93;
  --stuck-lo: #e5615a;

  --chick: #f7e8c6;
  --chick-lo: #e8d3a5;
  --beak: #f5a623;

  --goal-dent: #9fb3ad;
  --pill: #cfeadd;
  --pill-ink: #2c5449;
  --ink: #dff3e9;
  --dim: #8fb3ac;
  --accent: #ffcf6a;
}

* { 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);
}

/* 배경 그림은 배경색과 **따로** 선언한다. 이 규칙이 통째로 실패해도 위의 배경색과
   화면 내용은 그대로 남는다 — 배경 하나 때문에 게임이 안 보이는 일은 없어야 한다.
   (tapflip/css/style.css 의 같은 원칙) */
body {
  background-image:
    linear-gradient(rgba(20, 32, 58, .30), rgba(20, 32, 58, .62) 45%, rgba(20, 32, 58, .30)),
    url("../assets/bg.webp");
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  background-attachment: fixed;
}

/* ---- 헤더: 알약 모양 배지 ---- */
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, #pushes, #tokens {
  padding: .4rem .8rem;
  border-radius: 999px;
  background: rgba(207, 234, 221, .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: var(--accent);
  font-size: .78rem;
  font-weight: 600;
  text-align: center;
}

main {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1.2rem;
}

/* 판은 항상 한 화면에 다 들어와야 한다 — 카메라 이동이 없는 게임이라
   54vh 상한이 없으면 넓고 낮은 창에서 아래 버튼이 잘린다. */
#board {
  display: grid;
  grid-template-columns: repeat(var(--w, 7), 1fr);
  grid-template-rows: repeat(var(--h, 7), 1fr);
  gap: 0.6%;
  width: min(94vw, 440px, 66vh);
  aspect-ratio: var(--w, 7) / var(--h, 7);
  transition: transform .25s;
  touch-action: none; /* 스와이프를 브라우저 스크롤에 뺏기지 않게 */
  filter: drop-shadow(0 1.2rem 1.6rem rgba(0, 0, 0, .35));
}

/* ---- 판 위의 그림들 ----
   바닥·벽·목표칸·상자는 전부 생성 에셋이다(assets/*.webp, 각 4~17KB).
   자르는 절차는 scripts/make-art.py — 시트를 다시 뽑으면 그대로 재생성된다. */
.cell {
  position: relative;
  background: url("../assets/floor.webp") center / 100% 100% no-repeat;
}
/* 벽: 판의 액자. 바닥보다 훨씬 어두워야 놀이 공간이 넓어 보인다 */
.cell.wall { background-image: url("../assets/wall.webp"); }
/* 목표칸: 상자가 들어갈 자리가 파여 있다 */
.cell.goal { background-image: url("../assets/goal.webp"); }

/* ---- 상자: 바닥 칸 위에 올리는 말 ---- */
.piece {
  position: absolute;
  inset: 7%;
  transition: transform .12s;
}
.cell.box .piece {
  inset: -4% 0 0;
  background: url("../assets/crate.webp") center bottom / contain no-repeat;
  filter: drop-shadow(0 3px 4px rgba(0, 0, 0, .35));
}
/* 제자리에 든 상자는 색 + 무늬(✓) 두 가지로 표시한다 (색약 대응) */
.cell.box.done .piece {
  background-image: url("../assets/crate-done.webp");
  filter: drop-shadow(0 0 .5rem rgba(75, 213, 152, .8));
}
/* 목표칸이 아닌 구석에 박힌 상자 — 이 판은 되돌려야 한다 */
.cell.stuck .piece {
  filter: hue-rotate(-38deg) saturate(1.9) drop-shadow(0 0 .5rem rgba(229, 97, 90, .9));
}

/* ---- 힌트: 밀 상자와 방향 ---- */
.cell.hint .piece { animation: nudge .7s ease-in-out infinite; }
@keyframes nudge {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.09); }
}
.cell.hint-dir::after {
  content: "";
  position: absolute;
  inset: 30%;
  background: #fff;
  clip-path: polygon(50% 0, 100% 100%, 50% 72%, 0 100%);
  opacity: .9;
  filter: drop-shadow(0 0 .4rem rgba(255, 255, 255, .9));
  z-index: 2;
}
.cell.hint-dir.up::after { transform: rotate(0deg); }
.cell.hint-dir.right::after { transform: rotate(90deg); }
.cell.hint-dir.down::after { transform: rotate(180deg); }
.cell.hint-dir.left::after { transform: rotate(270deg); }

/* ---- 주인공 병아리 ----
   컨셉 그림(design/concept-jelly-01.png)에서 잘라낸 스프라이트. 8.4KB.
   CSS로 흉내 내는 것보다 이쪽이 훨씬 귀엽고, 이미 만든 그림이라 크레딧이 안 들었다. */
.cell.player .piece {
  inset: 4%;
  border-radius: 0;
  background: url("../assets/chick.webp") center bottom / contain no-repeat;
  box-shadow: none;
  filter: drop-shadow(0 2px 3px rgba(0, 0, 0, .35));
  animation: bob 2.4s ease-in-out infinite;
}
@keyframes bob {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-4%); }
}

/* 동작 자세. 스프라이트 3장은 같은 시트에서 나와서 몸이 안 튄다.
   좌우 반전은 transform 이라 bob 애니메이션과 겹친다 — 반전용 키프레임을 따로 둔다. */
.cell.player.pose-push .piece { background-image: url("../assets/chick-push.webp"); }
.cell.player.pose-slide .piece { background-image: url("../assets/chick-slide.webp"); }
.cell.player.face-left .piece { animation-name: bob-left; }
@keyframes bob-left {
  0%, 100% { transform: translateY(0) scaleX(-1); }
  50% { transform: translateY(-4%) scaleX(-1); }
}



#board.cleared { transform: scale(1.04); }

#tip {
  color: var(--dim);
  font-size: .88rem;
  font-weight: 600;
  text-align: center;
  padding: 0 1.5rem;
  min-height: 2.6em;
}
#tip.warn { color: #ff9d8f; }

/* ---- 알약 버튼 ---- */
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, #e2f4ea, 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 #a9cdbe, 0 3px 6px rgba(0, 0, 0, .25);
}
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 #a9cdbe;
}

/* ---- 설명 오버레이 (형제 게임들과 공통 구조) ---- */
#help-open {
  padding: .4rem .7rem;
  border: 0;
  border-radius: 999px;
  background: rgba(207, 234, 221, .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, 18, 28, .78);
}
.overlay[hidden] { display: none; }
.sheet {
  width: 100%;
  max-width: 22rem;
  max-height: 100%;
  overflow-y: auto;
  background: linear-gradient(180deg, #24405a, #1b3145);
  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, .45);
}
.sheet h1 { font-size: 1.15rem; margin-bottom: 1rem; color: var(--ink); }
.sheet > div {
  background: rgba(0, 0, 0, .22);
  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, #ffe6a8, var(--accent));
  color: #4a3410;
  font: inherit;
  font-weight: 800;
  cursor: pointer;
  box-shadow: inset 0 2px 0 rgba(255, 255, 255, .8), inset 0 -3px 0 #d9a83f;
}
.sheet button.ghost {
  margin-bottom: .6rem;
  background: linear-gradient(180deg, #e2f4ea, var(--pill));
  color: var(--pill-ink);
  box-shadow: inset 0 2px 0 rgba(255, 255, 255, .9), inset 0 -3px 0 #a9cdbe;
}
.sheet button.ghost[hidden] { display: none; }

.result-sheet { text-align: center; }
.result-sheet h1 { margin-bottom: .35rem; }
#result-stars {
  color: var(--accent);
  font-size: 2.4rem;
  letter-spacing: .18rem;
  text-shadow: 0 0 1.2rem rgba(255, 207, 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; }
}
