:root {
  --bg: #f4e6c8;
  --bg2: #ead3a6;
  --ink: #4a2f17;
  --ink-soft: #8a6640;
  --card: #fff8ea;
  --accent: #e2733a;
  --green: #3fae5a;
  --btn: #c98a4b;
  --btn-dark: #8e5a2b;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; }

html, body {
  margin: 0;
  height: 100%;
  background: #3a2614;
  color: var(--ink);
  font-family: "Hiragino Maru Gothic ProN", "Hiragino Sans", "Yu Gothic", "Noto Sans JP", system-ui, sans-serif;
  overscroll-behavior: none;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}

body {
  display: flex;
  align-items: center;
  justify-content: center;
}

#app {
  position: relative;
  width: min(100vw, calc(100dvh * 9 / 16));
  aspect-ratio: 9 / 16;
  max-height: 100dvh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  background:
    radial-gradient(circle at 20% 15%, rgba(255,255,255,.5), transparent 40%),
    repeating-linear-gradient(45deg, var(--bg) 0 14px, var(--bg2) 14px 15px);
  container-type: inline-size;
}

/* HUD */
#hud {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 2cqw;
  padding: 3.2cqw 4cqw 1.6cqw;
}
.hud-cell {
  background: var(--card);
  border: .6cqw solid var(--btn-dark);
  border-radius: 3cqw;
  padding: 1.2cqw 2cqw;
  text-align: center;
  box-shadow: 0 .8cqw 0 var(--btn-dark);
}
.hud-cell .lbl { display: block; font-size: 2.8cqw; color: var(--ink-soft); }
.hud-cell b { font-size: 6cqw; line-height: 1.15; font-variant-numeric: tabular-nums; }
#hud.best #hud-moves { color: var(--green); }

#stage-name {
  margin: 1.4cqw 0 0;
  text-align: center;
  font-size: 4cqw;
  font-weight: bold;
  color: var(--ink-soft);
}

#stage-wrap {
  position: relative;
  flex: 1 1 auto;
  min-height: 0;
}
#cv { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }


/* 操作 */
#controls {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 2cqw;
  padding: 1cqw 4cqw 5cqw;
}
#pad {
  display: grid;
  grid-template-columns: repeat(3, 15cqw);
  grid-template-rows: repeat(2, 15cqw);
  gap: 1cqw;
}
#pad [data-dir="up"] { grid-column: 2; grid-row: 1; }
#pad [data-dir="left"] { grid-column: 1; grid-row: 2; }
#pad .pad-center { grid-column: 2; grid-row: 2; border-radius: 50%; background: rgba(74,47,23,.12); margin: 3cqw; }
#pad [data-dir="right"] { grid-column: 3; grid-row: 2; }

button {
  font: inherit;
  color: inherit;
  cursor: pointer;
  touch-action: none;
}
.dir {
  border: .6cqw solid var(--btn-dark);
  border-radius: 3.5cqw;
  background: linear-gradient(#e0a663, var(--btn));
  color: #fff8ea;
  font-size: 6cqw;
  box-shadow: 0 1.2cqw 0 var(--btn-dark);
  transition: transform .06s, box-shadow .06s;
  text-shadow: 0 .3cqw 0 rgba(0,0,0,.25);
}
.dir.held {
  transform: translateY(1cqw);
  box-shadow: 0 .2cqw 0 var(--btn-dark);
  background: linear-gradient(#f1bb73, #d99a4e);
}
.side-btn {
  border: .6cqw solid var(--btn-dark);
  border-radius: 3cqw;
  background: var(--card);
  font-size: 3.4cqw;
  padding: 2cqw 1cqw;
  box-shadow: 0 1cqw 0 var(--btn-dark);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: .5cqw;
}
.side-btn .ico { font-size: 6cqw; line-height: 1; }
.side-btn:active { transform: translateY(.8cqw); box-shadow: 0 .2cqw 0 var(--btn-dark); }
.side-btn:disabled { opacity: .4; }

/* オーバーレイ */
.overlay {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 6cqw;
  background: rgba(58,38,20,.55);
  backdrop-filter: blur(2px);
  z-index: 10;
}
.card {
  width: 100%;
  background: var(--card);
  border: .8cqw solid var(--btn-dark);
  border-radius: 5cqw;
  padding: 6cqw 6cqw 5cqw;
  text-align: center;
  box-shadow: 0 2cqw 0 var(--btn-dark);
}
.kicker { margin: 0; color: var(--accent); font-size: 3.6cqw; font-weight: bold; letter-spacing: .1em; }
.card h1 { margin: 2cqw 0 4cqw; font-size: 11cqw; line-height: 1.2; letter-spacing: .04em; }
/* タイトルの絵（跳ねるハムスターと積み木） */
#title-art { display: block; width: 100%; height: 44cqw; margin: -2cqw 0 -1cqw; }
.card h2 { margin: 2cqw 0; font-size: 7.4cqw; }
.card h1 + .lead { margin: 0 0 7cqw; font-size: 4.4cqw; line-height: 1.6; color: var(--ink-soft); }
.big-btn {
  display: block;
  width: 100%;
  border: .8cqw solid #a8481d;
  border-radius: 4cqw;
  background: linear-gradient(#f2944f, var(--accent));
  color: #fff;
  font-size: 6.4cqw;
  font-weight: bold;
  padding: 3cqw;
  box-shadow: 0 1.4cqw 0 #a8481d;
}
.big-btn:active { transform: translateY(1cqw); box-shadow: 0 .4cqw 0 #a8481d; }
#btn-continue small { display: block; font-size: 3.6cqw; font-weight: normal; opacity: .9; }
/* つづきがあるときの「はじめから」は控えめに */
#btn-start.alt {
  margin-top: 3.4cqw;
  border-color: var(--btn-dark);
  background: var(--card);
  color: var(--ink);
  font-size: 4.6cqw;
  padding: 2.2cqw;
  box-shadow: 0 1cqw 0 var(--btn-dark);
}
.sub-btn {
  margin-top: 4cqw;
  border: none;
  background: none;
  color: var(--ink-soft);
  font-size: 3.8cqw;
  text-decoration: underline;
}
#res-list { list-style: none; padding: 0; margin: 0 0 5cqw; font-size: 3.8cqw; text-align: left; }
#res-list li { display: flex; justify-content: space-between; padding: 1.4cqw 0; border-top: .3cqw dashed rgba(74,47,23,.25); }
#res-list li .star { color: var(--green); font-weight: bold; }

/* ---- 全クリアのお祝い ---- */
#fx { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; z-index: 11; }
/* 光の筋とハムスターはカードの後ろ */
#fx-back { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; }
#result .card { position: relative; }
#result.party {
  align-items: flex-end;
  padding-bottom: 8cqw;
  background: radial-gradient(circle at 50% 24%, rgba(255,214,120,.6), rgba(58,38,20,.7) 72%);
  backdrop-filter: none;
}
#result.party .card { animation: cardIn .75s cubic-bezier(.2,1.5,.4,1) .35s both; }
@keyframes cardIn {
  from { transform: translateY(80%) scale(.7) rotate(-4deg); opacity: 0; }
  to { transform: none; opacity: 1; }
}
.bounce-title {
  background: linear-gradient(#f7a24a, #e2533a);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  font-size: 9cqw !important;
}
.bounce-title span {
  display: inline-block;
  background: inherit;
  -webkit-background-clip: text;
  background-clip: text;
  animation:
    hop .6s cubic-bezier(.3,1.7,.5,1) calc(.7s + var(--i) * .07s) both,
    wave 1.4s ease-in-out calc(1.6s + var(--i) * .1s) infinite;
}
@keyframes hop {
  from { transform: translateY(-140%) scale(.3); opacity: 0; }
  to { transform: none; opacity: 1; }
}
@keyframes wave {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-14%); }
}
#result.party #res-list li { animation: rowIn .38s ease-out calc(1.1s + var(--i) * .28s) both; }
@keyframes rowIn {
  from { transform: translateX(-30%); opacity: 0; }
  to { transform: none; opacity: 1; }
}
#result.party #res-list .star {
  display: inline-block;
  animation: starPop .55s cubic-bezier(.2,1.9,.4,1) calc(1.25s + var(--i) * .28s) both;
}
@keyframes starPop {
  from { transform: scale(0) rotate(-120deg); }
  to { transform: none; }
}
#res-summary {
  margin: -2cqw 0 4cqw;
  font-size: 6cqw;
  font-weight: bold;
  color: var(--green);
}
#result.party #res-summary { animation: summaryPop .6s cubic-bezier(.2,1.8,.4,1) var(--d, 2.6s) both; }
@keyframes summaryPop {
  from { transform: scale(0); opacity: 0; }
  to { transform: none; opacity: 1; }
}

#toast {
  position: absolute;
  left: 50%;
  top: 13%;
  transform: translate(-50%, -50%) scale(.6);
  opacity: 0;
  pointer-events: none;
  background: var(--card);
  border: .7cqw solid var(--btn-dark);
  border-radius: 4cqw;
  padding: 2.4cqw 5cqw;
  font-size: 5.6cqw;
  font-weight: bold;
  white-space: nowrap;
  z-index: 5;
  box-shadow: 0 1.2cqw 0 var(--btn-dark);
}
#toast.mid { top: 42%; font-size: 4.8cqw; }
#toast.show { animation: toast 1.3s ease-out forwards; }
@keyframes toast {
  0% { opacity: 0; transform: translate(-50%, -50%) scale(.6); }
  12% { opacity: 1; transform: translate(-50%, -50%) scale(1.12); }
  22% { transform: translate(-50%, -50%) scale(1); }
  80% { opacity: 1; }
  100% { opacity: 0; transform: translate(-50%, -70%) scale(1); }
}

.card h1 { font-size: 8cqw; }
#stage-choice { width:100%; margin-top:3cqw; padding:1.5cqw; border:1px solid var(--btn-dark); border-radius:2cqw; background:var(--card); color:var(--ink); font:inherit; font-size:4cqw; }
#btn-next { position:absolute; right:2%; top:10%; z-index:3; background:var(--card); color:var(--ink); border-radius:2cqw; padding:1cqw; }

.stage-tools { display:flex; align-items:center; justify-content:space-between; padding:0 4cqw; gap:1cqw; }
#btn-sound { background:var(--card); color:var(--ink); border:1px solid var(--btn-dark); border-radius:2cqw; font-size:3cqw; padding:1.2cqw; }
#btn-next { top:17%; }
