:root {
  --bg1: #1d2b53;
  --bg2: #2c3e7a;
  --panel: rgba(255, 255, 255, 0.1);
  --text: #f4f6ff;
  --muted: #b9c2e6;
  --accent: #ffb938;
  --danger: #ff5a5a;
  --red: #ec4d4d;
  --blue: #3f8cff;
  --green: #35b86b;
  --yellow: #f7c62f;
  --purple: #a86be8;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  height: 100%;
  overflow: hidden;
}

body {
  background: linear-gradient(160deg, var(--bg1), var(--bg2));
  color: var(--text);
  font-family: "Hiragino Maru Gothic ProN", "Hiragino Sans", "Yu Gothic UI", "Meiryo", system-ui, sans-serif;
  /* ダブルタップ拡大・テキスト選択・長押しメニューを無効化 */
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
}

#app {
  position: relative;
  height: 100vh;
  height: 100dvh;
  max-width: 560px;
  margin: 0 auto;
}

.screen {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  padding: env(safe-area-inset-top) 10px env(safe-area-inset-bottom);
}

.hidden {
  display: none !important;
}

/* ---- ボタン ---- */
.btn {
  appearance: none;
  border: none;
  border-radius: 12px;
  padding: 10px 18px;
  font: inherit;
  font-size: 16px;
  font-weight: bold;
  color: var(--text);
  background: rgba(255, 255, 255, 0.16);
  box-shadow: 0 3px 0 rgba(0, 0, 0, 0.25);
  cursor: pointer;
  touch-action: manipulation;
}

.btn:active {
  transform: translateY(2px);
  box-shadow: 0 1px 0 rgba(0, 0, 0, 0.25);
}

.btn.primary {
  background: var(--accent);
  color: #3a2500;
}

.btn.big {
  font-size: 22px;
  padding: 14px 48px;
}

.btn.small {
  font-size: 14px;
  padding: 6px 12px;
}

/* ---- タイトル ---- */
.title-screen {
  align-items: center;
  justify-content: center;
  gap: 40px;
  text-align: center;
}

.title-screen h1 {
  margin: 16px 0 8px;
  font-size: clamp(32px, 10vw, 48px);
  line-height: 1.2;
  text-shadow: 0 4px 0 rgba(0, 0, 0, 0.3);
}

.title-screen .sub {
  margin: 0;
  color: var(--muted);
}

.logo-blocks {
  display: flex;
  justify-content: center;
  gap: 8px;
}

.logo-blocks .face {
  position: relative;
  inset: auto;
  width: 48px;
  height: 48px;
  font-size: 24px;
}

.back-link {
  color: var(--muted);
  font-size: 14px;
}

/* ---- 上下のバー ---- */
.bar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 0;
}

.bar h2 {
  margin: 0;
  font-size: 20px;
}

.bar.bottom {
  justify-content: center;
  padding-bottom: 12px;
}

/* ---- ステージ選択 ---- */
.stage-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(96px, 1fr));
  gap: 10px;
  overflow-y: auto;
  padding: 4px 0 16px;
}

.stage-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 12px 4px;
  border: none;
  border-radius: 14px;
  font: inherit;
  color: var(--text);
  background: rgba(255, 255, 255, 0.14);
  box-shadow: 0 3px 0 rgba(0, 0, 0, 0.25);
  cursor: pointer;
}

.stage-btn.cleared {
  box-shadow: inset 0 0 0 2px rgba(255, 185, 56, 0.7), 0 3px 0 rgba(0, 0, 0, 0.25);
}

.stage-btn.locked {
  opacity: 0.45;
  cursor: default;
}

.stage-btn .no {
  font-size: 26px;
  font-weight: bold;
}

.stage-btn .name {
  font-size: 11px;
  color: var(--muted);
  white-space: nowrap;
}

.stage-btn .stars {
  font-size: 16px;
  color: var(--accent);
  letter-spacing: 1px;
  min-height: 1.2em;
}

/* ---- ゲーム画面の上部 ---- */
.hud {
  padding: 8px 0 6px;
}

.stage-name {
  font-weight: bold;
  font-size: 15px;
  margin-bottom: 6px;
  text-align: center;
}

.hud-row {
  display: flex;
  align-items: stretch;
  gap: 6px;
}

.moves-box,
.score-box {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-width: 64px;
  padding: 4px 6px;
  border-radius: 12px;
  background: var(--panel);
}

.moves-box .value {
  font-size: 30px;
  font-weight: bold;
  line-height: 1.1;
}

.moves-box.danger .value {
  color: var(--danger);
  animation: pulse 0.8s ease-in-out infinite alternate;
}

.score-box .value {
  font-size: 16px;
  font-weight: bold;
}

.label {
  font-size: 10px;
  color: var(--muted);
}

.goals {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 3px;
  padding: 4px 8px;
  border-radius: 12px;
  background: var(--panel);
  min-width: 0;
}

.goal {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 14px;
}

.goal-label {
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.goal-count {
  display: inline-block;
  font-weight: bold;
  font-variant-numeric: tabular-nums;
}

.goal-count.bump {
  animation: bump 0.3s ease-out;
}

.goal.done .goal-count::after {
  content: " ✔";
  color: #6ee08f;
}

.goal-icon {
  position: relative;
  flex: none;
  width: 18px;
  height: 18px;
  border-radius: 5px;
  font-size: 10px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.goal-icon.text {
  background: rgba(255, 255, 255, 0.25);
  font-weight: bold;
}

.goal-icon.ice {
  background: linear-gradient(135deg, rgba(225, 245, 255, 0.95), rgba(140, 200, 240, 0.8));
}

.goal-icon.wood {
  background: #a0662f;
  border: 2px solid #6b3f17;
}

/* ---- 盤面 ---- */
.board-wrap {
  flex: 1;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}

.board {
  --cell: 44px;
  position: relative;
  width: calc(var(--cols) * var(--cell));
  height: calc(var(--rows) * var(--cell));
  touch-action: none;
  cursor: pointer;
}

.board-clip {
  position: absolute;
  inset: 0;
  overflow: hidden;
  border-radius: 10px;
  background: rgba(0, 0, 0, 0.25);
}

.layer {
  position: absolute;
  inset: 0;
  pointer-events: none;
}

.piece {
  position: absolute;
  left: 0;
  top: 0;
  width: var(--cell);
  height: var(--cell);
  transform: translate(calc(var(--x) * var(--cell)), calc(var(--y) * var(--cell)));
}

.cell {
  background: rgba(255, 255, 255, 0.04);
}

.cell.alt {
  background: rgba(255, 255, 255, 0.08);
}

.block {
  transition: transform 250ms cubic-bezier(0.3, 1.25, 0.6, 1);
}

.board.shuffling .block {
  transition: transform 350ms ease-in-out;
}

.board.no-anim .piece,
.board.no-anim .face {
  transition: none !important;
}

.face {
  position: absolute;
  inset: 6%;
  border-radius: 22%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: calc(var(--cell) * 0.42);
  color: rgba(255, 255, 255, 0.92);
  box-shadow: inset 0 -3px 0 rgba(0, 0, 0, 0.22), inset 0 3px 0 rgba(255, 255, 255, 0.3);
  transition: transform 200ms ease-in, opacity 200ms ease-in;
}

.face .sym {
  line-height: 1;
  text-shadow: 0 1px 1px rgba(0, 0, 0, 0.35);
}

.face.red { background: var(--red); }
.face.blue { background: var(--blue); }
.face.green { background: var(--green); }
.face.yellow { background: var(--yellow); color: rgba(90, 60, 0, 0.85); }
.face.yellow .sym { text-shadow: none; }
.face.purple { background: var(--purple); }

.goal-icon.face {
  position: relative;
  inset: auto;
  font-size: 10px;
  border-radius: 5px;
  box-shadow: none;
}

.block.removing .face {
  transform: scale(0.2);
  opacity: 0;
}

.face.shake {
  animation: shake 0.3s ease-in-out;
}

/* 氷：マスに張った半透明の膜（ブロックの上に重ねる） */
.ice {
  border-radius: 10%;
  background: linear-gradient(135deg, rgba(235, 250, 255, 0.55), rgba(160, 215, 245, 0.35) 55%, rgba(235, 250, 255, 0.5));
  border: 2px solid rgba(235, 250, 255, 0.8);
  transition: opacity 200ms ease-out, transform 200ms ease-out;
}

.ice::after {
  content: "";
  position: absolute;
  left: 18%;
  top: 14%;
  width: 30%;
  height: 12%;
  border-radius: 6px;
  background: rgba(255, 255, 255, 0.75);
  transform: rotate(-30deg);
}

.ice.breaking,
.fixed.breaking {
  opacity: 0;
  transform: translate(calc(var(--x) * var(--cell)), calc(var(--y) * var(--cell))) scale(1.3);
}

.fixed {
  transition: opacity 200ms ease-out, transform 200ms ease-out;
}

.wood::before,
.stone::before {
  content: "";
  position: absolute;
  inset: 4%;
  border-radius: 12%;
}

.wood::before {
  background:
    linear-gradient(45deg, transparent 44%, rgba(70, 35, 10, 0.5) 45%, rgba(70, 35, 10, 0.5) 55%, transparent 56%),
    linear-gradient(-45deg, transparent 44%, rgba(70, 35, 10, 0.5) 45%, rgba(70, 35, 10, 0.5) 55%, transparent 56%),
    #b5753a;
  border: calc(var(--cell) * 0.08) solid #7a4719;
}

.wood .hp {
  position: absolute;
  right: 10%;
  bottom: 6%;
  font-size: calc(var(--cell) * 0.3);
  font-weight: bold;
  color: #fff;
  text-shadow: 0 0 3px #000, 0 0 3px #000;
}

.wood.hit::before {
  animation: hit 0.25s ease-out;
}

.stone::before {
  background: radial-gradient(circle at 35% 30%, #9da3ad, #6c727c 60%, #4b5058);
  border-radius: 30%;
  box-shadow: inset 0 -3px 0 rgba(0, 0, 0, 0.3);
}

/* 獲得点・トースト */
.fx {
  z-index: 2;
}

.popup {
  position: absolute;
  left: 0;
  top: 0;
  width: var(--cell);
  height: var(--cell);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: bold;
  font-size: calc(var(--cell) * 0.45);
  color: #fff;
  text-shadow: 0 2px 0 rgba(0, 0, 0, 0.5);
  white-space: nowrap;
  transform: translate(calc(var(--x) * var(--cell)), calc(var(--y) * var(--cell)));
  animation: popup 0.8s ease-out forwards;
}

.toast {
  position: absolute;
  left: 50%;
  top: 45%;
  padding: 10px 22px;
  border-radius: 999px;
  background: rgba(0, 0, 0, 0.7);
  font-weight: bold;
  font-size: 20px;
  white-space: nowrap;
  transform: translate(-50%, -50%);
  animation: fade 1.2s ease-out forwards;
}

/* ---- 結果 ---- */
.overlay {
  position: absolute;
  inset: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background: rgba(10, 15, 35, 0.65);
  animation: fadein 0.25s ease-out;
}

.card {
  width: 100%;
  max-width: 340px;
  padding: 24px 20px;
  border-radius: 20px;
  background: #2f3f7d;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.4);
  text-align: center;
  animation: popin 0.3s cubic-bezier(0.3, 1.4, 0.6, 1);
}

.card h2 {
  margin: 0 0 8px;
  font-size: 26px;
}

.card.win h2 { color: var(--accent); }
.card.lose h2 { color: #ff9a9a; }

.result-stars {
  font-size: 44px;
  color: var(--accent);
  letter-spacing: 4px;
  min-height: 0;
}

.result-score {
  color: var(--muted);
  margin: 8px 0 18px;
}

.card-buttons {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

/* ---- アニメーション ---- */
@keyframes shake {
  0%, 100% { transform: translateX(0); }
  20% { transform: translateX(-12%); }
  40% { transform: translateX(10%); }
  60% { transform: translateX(-7%); }
  80% { transform: translateX(4%); }
}

@keyframes hit {
  0% { transform: scale(1); }
  40% { transform: scale(0.85) rotate(-4deg); }
  100% { transform: scale(1); }
}

@keyframes bump {
  0% { transform: scale(1); }
  40% { transform: scale(1.5); color: var(--accent); }
  100% { transform: scale(1); }
}

@keyframes pulse {
  from { transform: scale(1); }
  to { transform: scale(1.12); }
}

@keyframes popup {
  0% { opacity: 0; margin-top: 0; }
  20% { opacity: 1; }
  100% { opacity: 0; margin-top: calc(var(--cell) * -1.2); }
}

@keyframes fade {
  0% { opacity: 0; }
  15%, 75% { opacity: 1; }
  100% { opacity: 0; }
}

@keyframes fadein {
  from { opacity: 0; }
  to { opacity: 1; }
}

@keyframes popin {
  from { transform: scale(0.7); opacity: 0; }
  to { transform: scale(1); opacity: 1; }
}

@media (max-height: 640px) {
  .stage-name { margin-bottom: 3px; font-size: 13px; }
  .moves-box .value { font-size: 24px; }
  .goal { font-size: 12px; }
  .bar.bottom { padding: 6px 0 8px; }
  .btn { padding: 7px 14px; font-size: 14px; }
}
