:root {
  --ink: #4A3543;
  --ink-soft: #9A7A83;
  --orange: #FF8A3D;
  --orange-deep: #F06D1E;
  --cream: #FFF9F0;
  --line: rgba(74, 53, 67, 0.12);
  --card-shadow: 0 4px 0 rgba(174, 105, 66, 0.16), 0 12px 24px rgba(151, 89, 61, 0.13);
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html,
body {
  min-height: 100%;
}

body {
  margin: 0;
  min-height: 100vh;
  min-height: 100dvh;
  color: var(--ink);
  background: #FFE8D2;
  font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", -apple-system, BlinkMacSystemFont, sans-serif;
  overflow-x: hidden;
  -webkit-font-smoothing: antialiased;
  touch-action: manipulation;
  overscroll-behavior-y: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

.sky {
  position: fixed;
  inset: 0;
  z-index: -2;
  background: #FFE8D2 url("bg.svg") center / cover no-repeat;
}

.sky::after {
  content: "";
  position: absolute;
  inset: 0;
  background: radial-gradient(120% 90% at 50% 30%, transparent 55%, rgba(166, 88, 76, 0.08) 100%);
  pointer-events: none;
}

.sprite {
  position: absolute;
  width: 0;
  height: 0;
  overflow: hidden;
  pointer-events: none;
}

.game {
  width: 100%;
  max-width: 460px;
  min-height: 100vh;
  min-height: 100dvh;
  margin: 0 auto;
  padding: calc(env(safe-area-inset-top, 0px) + 12px) 16px calc(env(safe-area-inset-bottom, 0px) + 14px);
  display: flex;
  flex-direction: column;
}

/* ---------- HUD ---------- */
.hud {
  display: grid;
  grid-template-columns: minmax(0, 1.42fr) minmax(0, 1fr);
  gap: 10px;
  width: 100%;
}

.stat {
  position: relative;
  overflow: hidden;
  min-height: 68px;
  padding: 10px 12px;
  border: 2px solid rgba(255, 255, 255, 0.92);
  border-radius: 21px;
  background: rgba(255, 252, 246, 0.94);
  box-shadow: var(--card-shadow);
}

.stat::after {
  content: "";
  position: absolute;
  inset: 0;
  background: linear-gradient(125deg, rgba(255, 255, 255, 0.7), transparent 44%);
  pointer-events: none;
}

.stat-score {
  display: flex;
  align-items: center;
  gap: 10px;
}

.stat-icon {
  position: relative;
  z-index: 1;
  flex: 0 0 auto;
  width: 40px;
  height: 40px;
  display: grid;
  place-items: center;
  border-radius: 14px;
  color: #FFFFFF;
  background: linear-gradient(160deg, #FFD36B, #FF9A3B);
  box-shadow: inset 0 2px 0 rgba(255, 255, 255, 0.6), 0 3px 0 #E7792B;
}

.stat-icon .mp-icon {
  width: 22px;
  height: 22px;
}

.stat-body {
  position: relative;
  z-index: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  flex: 1;
}

.stat-label {
  font-size: 10.5px;
  font-weight: 800;
  letter-spacing: 0.1em;
  color: var(--ink-soft);
}

.stat-value {
  display: block;
  margin-top: 1px;
  font-size: 27px;
  line-height: 1.04;
  font-weight: 900;
  letter-spacing: -0.03em;
  color: var(--ink);
  font-variant-numeric: tabular-nums;
  text-shadow: 0 1px 0 #FFFFFF;
}

.stat-value small {
  margin-left: 2px;
  font-size: 11px;
  font-weight: 800;
  color: var(--ink-soft);
  letter-spacing: 0;
}

.best-score {
  position: absolute;
  z-index: 2;
  top: 9px;
  right: 9px;
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 3px 7px;
  border: 1px solid #FFD9B0;
  border-radius: 999px;
  background: #FFF0DC;
  color: #A77A55;
  font-size: 9.5px;
  font-weight: 800;
  line-height: 1;
}

.best-score b {
  color: #8E5A3D;
  font-size: 11px;
  font-variant-numeric: tabular-nums;
}

.stat-moves {
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 6px;
}

.stat-moves .stat-value {
  font-size: 29px;
}

.moves-track {
  position: relative;
  z-index: 1;
  width: 100%;
  height: 6px;
  border-radius: 999px;
  background: #F3E1D4;
  box-shadow: inset 0 1px 2px rgba(98, 55, 42, 0.14);
  overflow: hidden;
}

#movesFill {
  display: block;
  width: 100%;
  height: 100%;
  border-radius: inherit;
  background: linear-gradient(90deg, #FFC176, #FF8A3D);
  transition: width 0.35s ease, background 0.25s ease;
}

.stat-moves.low .stat-value {
  color: #E44863;
}

.stat-moves.low #movesFill {
  background: linear-gradient(90deg, #FF9BAE, #E44863);
}

.stat-score.bump {
  animation: cardBump 0.24s ease-out;
}

/* ---------- Board ---------- */
.board-area {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  width: 100%;
  margin-top: 13px;
}

.board-shell {
  position: relative;
  width: 100%;
  width: min(100%, max(190px, calc((100dvh - 140px) * 0.7)));
  padding: 9px;
  border: 2px solid #FFFFFF;
  border-radius: 31px;
  background: linear-gradient(180deg, #FFFDF9 0%, #FFEEDC 100%);
  box-shadow: 0 6px 0 rgba(197, 121, 68, 0.22), 0 18px 36px rgba(151, 84, 50, 0.18),
    inset 0 0 0 2px #FFD9B8;
}

.board-shell::before {
  content: "";
  position: absolute;
  inset: 5px;
  border: 1px dashed rgba(231, 159, 111, 0.5);
  border-radius: 26px;
  pointer-events: none;
}

.board {
  position: relative;
  width: 100%;
  aspect-ratio: 7 / 10;
  --cols: 7;
  --rows: 10;
  overflow: hidden;
  border-radius: 23px;
  background: #FFF7EB;
  box-shadow: inset 0 2px 10px rgba(173, 105, 65, 0.12);
  touch-action: manipulation;
}

.cells {
  position: absolute;
  inset: 0;
  display: grid;
  grid-template-columns: repeat(var(--cols, 7), 1fr);
  grid-template-rows: repeat(var(--rows, 10), 1fr);
}

.cell {
  margin: 2px;
  border-radius: 24%;
  background: rgba(255, 255, 255, 0.46);
  box-shadow: inset 0 1px 2px rgba(166, 112, 82, 0.09);
}

.cell.alt {
  background: rgba(255, 226, 203, 0.48);
}

.tiles {
  position: absolute;
  inset: 0;
}

.tile {
  position: absolute;
  width: calc(100% / var(--cols, 7));
  height: calc(100% / var(--rows, 10));
  left: 0;
  top: 0;
  padding: 2px;
  z-index: 2;
  touch-action: manipulation;
  cursor: pointer;
  transition: transform 0.23s cubic-bezier(0.22, 0.82, 0.28, 1.08);
  will-change: transform;
}

.tile-inner {
  position: relative;
  width: 100%;
  height: 100%;
  display: grid;
  place-items: center;
  overflow: hidden;
  border: 2px solid rgba(74, 53, 67, 0.52);
  border-radius: 24%;
  background: var(--tile-bg, #FFF5E8);
  box-shadow: inset 0 2px 0 rgba(255, 255, 255, 0.88), inset 0 -4px 0 rgba(84, 48, 60, 0.09),
    0 3px 0 rgba(90, 55, 55, 0.16);
  transition: transform 0.13s ease, box-shadow 0.15s ease, filter 0.18s ease;
}

.tile-inner::before {
  content: "";
  position: absolute;
  top: 7%;
  left: 10%;
  width: 50%;
  height: 22%;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.52);
  filter: blur(1px);
  transform: rotate(-20deg);
  pointer-events: none;
}

.tile .char {
  position: relative;
  z-index: 1;
  width: 94%;
  height: 94%;
  margin-bottom: -2%;
  pointer-events: none;
  filter: drop-shadow(0 1px 0 rgba(255, 255, 255, 0.3));
}

.tile:active .tile-inner {
  transform: scale(0.91);
}

.tile.selected {
  z-index: 6;
}

.tile.selected .tile-inner {
  border-color: #FF8A3D;
  box-shadow: 0 0 0 2px #FFFFFF, 0 0 0 5px rgba(255, 138, 61, 0.5), 0 6px 10px rgba(150, 80, 30, 0.25);
  animation: selectedPulse 0.9s ease-in-out infinite;
}

.tile.hint .tile-inner {
  animation: hintPulse 0.85s ease-in-out 3;
}

.tile.clearing {
  z-index: 7;
}

.tile.clearing .tile-inner {
  animation: clearPop 0.27s ease-out forwards;
}

.tile.bad .tile-inner {
  animation: tileShake 0.3s ease-in-out;
  border-color: #E44863;
}

.tile.spawn .tile-inner {
  animation: tileSpawn 0.4s cubic-bezier(0.2, 0.9, 0.3, 1.25) both;
  animation-delay: var(--spawn-delay, 0ms);
}

.tile.flip .tile-inner {
  animation: tileFlip 0.34s ease-in-out;
}

.tile-kuromi { --tile-bg: linear-gradient(160deg, #F7DDFA, #DDB2F0); }
.tile-cinnamoroll { --tile-bg: linear-gradient(160deg, #E4F8FF, #B9E2FF); }
.tile-mymelody { --tile-bg: linear-gradient(160deg, #FFE7F0, #FFB9D2); }
.tile-purin { --tile-bg: linear-gradient(160deg, #FFF8D9, #FFE08E); }
.tile-pochacco { --tile-bg: linear-gradient(160deg, #E8FBEF, #BCEBCF); }
.tile-cloud { --tile-bg: linear-gradient(160deg, #F1F2FF, #CCD8FF); }

.combo {
  position: absolute;
  inset: 0;
  z-index: 15;
  display: grid;
  place-items: center;
  pointer-events: none;
}

.combo-card {
  padding: 9px 22px 11px;
  text-align: center;
  color: #FFFFFF;
  border: 2px solid #FFFFFF;
  border-radius: 18px;
  background: linear-gradient(165deg, #FFD066, #FF8F3F);
  box-shadow: 0 5px 0 #D9651D, 0 15px 28px rgba(140, 60, 20, 0.28);
  transform: rotate(-3deg);
  opacity: 0;
}

.combo.show .combo-card {
  animation: comboPop 0.82s cubic-bezier(0.2, 0.9, 0.3, 1.2) both;
}

.combo-x {
  display: block;
  font-size: 12px;
  font-weight: 900;
  letter-spacing: 0.14em;
  text-shadow: 0 1px 0 rgba(150, 70, 20, 0.25);
}

.combo-pts {
  display: block;
  margin-top: 1px;
  font-size: 26px;
  font-weight: 900;
  line-height: 1.05;
  font-variant-numeric: tabular-nums;
  text-shadow: 0 2px 0 rgba(150, 70, 20, 0.22);
}

.board-toast {
  position: absolute;
  z-index: 20;
  left: 50%;
  bottom: 8%;
  padding: 7px 13px;
  border-radius: 999px;
  background: rgba(74, 53, 67, 0.9);
  color: #FFFFFF;
  font-size: 11.5px;
  font-weight: 700;
  line-height: 1.3;
  white-space: nowrap;
  opacity: 0;
  transform: translate(-50%, 8px);
  transition: opacity 0.24s ease, transform 0.24s ease;
  pointer-events: none;
}

.board-toast.show {
  opacity: 1;
  transform: translate(-50%, 0);
}

.fx {
  position: absolute;
  z-index: 8;
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--fx-color, #FFD66E);
  pointer-events: none;
  animation: fxFly 0.6s cubic-bezier(0.15, 0.7, 0.3, 1) forwards;
}

.fx.star {
  border-radius: 0;
  clip-path: polygon(50% 0%, 61% 35%, 98% 35%, 68% 57%, 79% 94%, 50% 72%, 21% 94%, 32% 57%, 2% 35%, 39% 35%);
}

.fx.heart {
  width: 8px;
  height: 8px;
  border-radius: 0;
  transform: rotate(45deg);
}

.fx.heart::before,
.fx.heart::after {
  content: "";
  position: absolute;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: inherit;
}

.fx.heart::before { left: -4px; }
.fx.heart::after { top: -4px; }

.board-meta {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  width: 100%;
  width: min(100%, max(190px, calc((100dvh - 140px) * 0.7)));
  padding: 0 3px;
}

.meta-pill {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  min-width: 0;
  padding: 7px 11px;
  border: 1px solid rgba(255, 255, 255, 0.92);
  border-radius: 999px;
  background: rgba(255, 252, 246, 0.86);
  color: #8D6572;
  font-size: 11px;
  font-weight: 700;
  line-height: 1;
  box-shadow: 0 2px 0 rgba(170, 100, 70, 0.12);
  white-space: nowrap;
}

.meta-pill .mp-icon {
  flex: 0 0 auto;
  width: 15px;
  height: 15px;
  color: #F5974D;
}

.meta-pill b {
  color: #F07A2E;
  font-size: 12.5px;
  font-weight: 900;
  font-variant-numeric: tabular-nums;
}

/* ---------- Game over ---------- */
.overlay {
  position: fixed;
  inset: 0;
  z-index: 60;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px 20px;
  background: rgba(73, 41, 57, 0.46);
  backdrop-filter: blur(7px);
  -webkit-backdrop-filter: blur(7px);
}

.overlay[hidden] {
  display: none;
}

.end-card {
  position: relative;
  width: 100%;
  max-width: 344px;
  padding: 0 22px 22px;
  text-align: center;
  border: 3px solid #FFFFFF;
  border-radius: 31px;
  background: linear-gradient(180deg, #FFFDF8 0%, #FFF2E2 100%);
  box-shadow: 0 10px 0 rgba(180, 105, 70, 0.24), 0 30px 60px rgba(80, 35, 50, 0.32),
    inset 0 0 0 2px #FFE0C3;
  animation: cardIn 0.42s cubic-bezier(0.2, 0.9, 0.3, 1.2) both;
}

.end-friends {
  display: flex;
  justify-content: center;
  margin-top: -38px;
  margin-bottom: 7px;
}

.friend-bubble {
  width: 66px;
  height: 66px;
  display: grid;
  place-items: center;
  overflow: hidden;
  border: 3px solid #FFFFFF;
  border-radius: 50%;
  background: #FFFFFF;
  box-shadow: 0 4px 0 rgba(170, 100, 70, 0.14), 0 8px 18px rgba(120, 60, 50, 0.14);
}

.friend-bubble + .friend-bubble {
  margin-left: -11px;
}

.friend-bubble svg {
  width: 88%;
  height: 88%;
}

.fb1 {
  z-index: 1;
  background: #FFE3EE;
  transform: rotate(-7deg);
}

.fb2 {
  z-index: 2;
  background: #EADDF7;
  transform: translateY(-8px) rotate(2deg);
}

.fb3 {
  z-index: 3;
  background: #DDF4FF;
  transform: rotate(7deg);
}

.end-kicker {
  margin: 7px 0 3px;
  color: #D58A63;
  font-size: 9.5px;
  font-weight: 900;
  letter-spacing: 0.24em;
}

.end-card h2 {
  margin: 0;
  color: var(--ink);
  font-size: 26px;
  font-weight: 900;
  letter-spacing: -0.02em;
}

.end-sub {
  margin: 6px 0 14px;
  color: #9B7B82;
  font-size: 12px;
  font-weight: 500;
  line-height: 1.5;
}

.end-score-wrap {
  position: relative;
  padding: 11px 12px 12px;
  border: 2px dashed #FFD0A8;
  border-radius: 20px;
  background: #FFF7EA;
}

.end-score-wrap > span {
  display: block;
  color: #B08A73;
  font-size: 10px;
  font-weight: 800;
  letter-spacing: 0.12em;
}

.end-score-wrap strong {
  display: block;
  margin-top: 1px;
  color: #F07A2E;
  font-size: 41px;
  font-weight: 900;
  line-height: 1.04;
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.03em;
  text-shadow: 0 2px 0 #FFFFFF;
}

.new-best {
  position: absolute;
  top: 8px;
  right: 9px;
  padding: 3px 7px;
  border: 1px solid #FFFFFF;
  border-radius: 999px;
  background: #FFE46F;
  color: #8A5A16;
  font-size: 9.5px;
  font-style: normal;
  font-weight: 900;
  transform: rotate(5deg);
}

.end-stats {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 20px;
  margin: 14px 0 17px;
}

.end-stats div {
  display: flex;
  flex-direction: column;
  gap: 1px;
}

.end-stats span {
  color: #A5848C;
  font-size: 10px;
  font-weight: 700;
}

.end-stats b {
  color: var(--ink);
  font-size: 17px;
  font-weight: 900;
  font-variant-numeric: tabular-nums;
}

.end-divider {
  width: 1px;
  height: 25px;
  background: #EAD7CD;
}

.play-again {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  padding: 15px 18px;
  border: 0;
  border-radius: 20px;
  background: linear-gradient(180deg, #FFB24F, #FF822F);
  color: #FFFFFF;
  font-family: inherit;
  font-size: 16px;
  font-weight: 900;
  line-height: 1;
  letter-spacing: 0.02em;
  box-shadow: 0 5px 0 #D9651D, 0 12px 24px rgba(210, 95, 20, 0.28);
  cursor: pointer;
  transition: transform 0.1s ease, box-shadow 0.1s ease;
}

.play-again:active {
  transform: translateY(3px);
  box-shadow: 0 2px 0 #D9651D;
}

.play-again .mp-icon {
  width: 20px;
  height: 20px;
}

/* ---------- Animations ---------- */
@keyframes tileSpawn {
  0% { transform: scale(0.22) rotate(-14deg); opacity: 0; }
  68% { transform: scale(1.07) rotate(3deg); opacity: 1; }
  100% { transform: scale(1) rotate(0deg); opacity: 1; }
}

@keyframes selectedPulse {
  0%, 100% { transform: translateY(-2px) scale(1.03); }
  50% { transform: translateY(-3px) scale(1.06); }
}

@keyframes hintPulse {
  0%, 100% { transform: scale(1); filter: brightness(1); }
  50% { transform: scale(1.1); filter: brightness(1.18); box-shadow: 0 0 0 3px #FFFFFF, 0 0 0 6px rgba(255, 138, 61, 0.4); }
}

@keyframes clearPop {
  0% { transform: scale(1); opacity: 1; }
  38% { transform: scale(1.16) rotate(4deg); filter: brightness(1.25); opacity: 1; }
  100% { transform: scale(0.18) rotate(12deg); filter: brightness(1.4); opacity: 0; }
}

@keyframes tileShake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-3px) rotate(-2deg); }
  55% { transform: translateX(3px) rotate(2deg); }
  80% { transform: translateX(-2px); }
}

@keyframes tileFlip {
  0% { transform: rotateY(0deg) scale(1); }
  50% { transform: rotateY(90deg) scale(0.94); }
  100% { transform: rotateY(0deg) scale(1); }
}

@keyframes comboPop {
  0% { transform: scale(0.45) rotate(-10deg); opacity: 0; }
  18% { transform: scale(1.1) rotate(2deg); opacity: 1; }
  30% { transform: scale(1) rotate(-3deg); opacity: 1; }
  76% { transform: translateY(0) scale(1) rotate(-3deg); opacity: 1; }
  100% { transform: translateY(-20px) scale(0.96) rotate(-1deg); opacity: 0; }
}

@keyframes fxFly {
  0% { transform: translate(0, 0) scale(0.5) rotate(0deg); opacity: 1; }
  100% { transform: translate(var(--fx-x, 10px), var(--fx-y, -20px)) scale(0) rotate(130deg); opacity: 0; }
}

@keyframes cardBump {
  0% { transform: scale(1); }
  45% { transform: scale(1.025); }
  100% { transform: scale(1); }
}

@keyframes cardIn {
  0% { transform: translateY(16px) scale(0.9); opacity: 0; }
  70% { transform: translateY(-2px) scale(1.015); opacity: 1; }
  100% { transform: translateY(0) scale(1); opacity: 1; }
}

/* ---------- Responsive ---------- */
@media (max-width: 359px) {
  .game {
    padding-left: 12px;
    padding-right: 12px;
  }

  .hud {
    gap: 8px;
  }

  .stat {
    min-height: 62px;
    padding: 8px 10px;
    border-radius: 18px;
  }

  .stat-icon {
    width: 34px;
    height: 34px;
    border-radius: 12px;
  }

  .stat-value {
    font-size: 23px;
  }

  .stat-moves .stat-value {
    font-size: 25px;
  }

  .best-score {
    font-size: 8.5px;
    padding: 2px 5px;
  }

  .best-score b {
    font-size: 10px;
  }

  .board-shell {
    padding: 7px;
    border-radius: 27px;
  }

  .board {
    border-radius: 20px;
  }

  .meta-pill {
    padding: 6px 8px;
    font-size: 10px;
  }

  .meta-pill .mp-icon {
    width: 13px;
    height: 13px;
  }
}

@media (max-height: 660px) {
  .game {
    padding-top: calc(env(safe-area-inset-top, 0px) + 8px);
  }

  .board-area {
    margin-top: 9px;
    gap: 7px;
  }

  .board-shell {
    padding: 7px;
  }

  .stat {
    min-height: 60px;
  }

  .meta-pill {
    padding-top: 5px;
    padding-bottom: 5px;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}
