* {
  box-sizing: border-box;
  user-select: none;
  -webkit-user-select: none;
  /* タブレット連打時の拡大防止 */
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

html {
  margin: 0;
  height: 100%;
}

body {
  margin: 0;
  height: 100vh;
  font-family: -apple-system,
    BlinkMacSystemFont,
    "Hiragino Sans",
    "Yu Gothic",
    "Meiryo",
    sans-serif;
  background: #f2f2f2;
  display: flex;
  justify-content: center;
  align-items: center;
}

.container {
  margin: 0 auto;
  border: 2px solid #333;
  width: 100%;
  max-width: 1196px;
  height: 540px;
  display: flex;
  flex-direction: column;
  position: relative;
  color: var(--paper);
  background: radial-gradient(circle at 88% 16%,#fff3b0 0 34px,#ffd23f 35px 52px,rgba(255,210,63,.18) 53px 110px,transparent 111px),radial-gradient(ellipse at 50% 120%,#5b2a86 0 40%,transparent 41%),linear-gradient(180deg,var(--night),var(--night-2) 70%,#6a2f7f);
}

.game-header {
  height: 50px;
  flex: 0 0 50px;
  align-items: center;
  background: transparent;
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  gap: 8px;
  padding: 4px 10px;
  z-index: 20;
}

.info {
  display: flex;
  font-weight: bold;
  flex-wrap: wrap;
  width: auto;
  justify-content: flex-start;
  font-size: 16px;
  gap: 2px 12px;
}

.game-main {
  position: relative;
  flex: 1;
  overflow: hidden;
  display: grid;
  place-items: center;
  background: transparent;
  min-height: 0;
}

:root {
  --night: #1d1240;
  --night-2:#3b1d6e;
  --pumpkin: #ff8a1f;
  --pumpkin-dark: #c2530a;
  --candy: #ffd23f;
  --slime: #8be04e;
  --paper: #fff6e0;
  --puzzle-size: 420px;
  --preview-size: 120px;
  --gap: 24px;
}

.screen {
  position: absolute;
  inset: 0;
  display: none;
  flex-direction: column;
}

.screen.active {
  display: flex;
}

.deco {
  position: absolute;
  pointer-events: none;
  line-height: 1;
}

.deco.bat1 {
  top: 22px;
  left: 6%;
  font-size: 44px;
  animation: flap 2.4s ease-in-out infinite;
}

.deco.bat2 {
  top: 70px;
  left: 80%;
  font-size: 34px;
  animation: flap 2.9s ease-in-out infinite .6s;
}

.deco.ghost {
  top: 110px;
  left: 4%;
  font-size: 56px;
  animation: float 3.6s ease-in-out infinite;
}

.deco.pumpkinL {
  bottom: 8px;
  left: 14px;
  font-size: 76px;
}

.deco.pumpkinR {
  bottom: 8px;
  right: 14px;
  font-size: 76px;
}

@keyframes flap {
  0%,100% {
    transform: translateY(0) rotate(-6deg);
  }
  50% {
    transform: translateY(-10px) rotate(6deg);
  }
}

@keyframes float {
  0%,100% {
    transform: translateY(0);
  }
  50% {
    transform: translateY(-14px);
  }
}

.btn {
  min-height: 72px;
  padding: 14px 32px;
  font-family: inherit;
  font-size: 28px;
  font-weight: bold;
  color: #3a1500;
  background: var(--candy);
  border: 4px solid #3a1500;
  border-radius: 24px;
  box-shadow: 0 8px 0 #3a1500;
  cursor: pointer;
  transition: transform .08s, box-shadow .08s;
}

.btn:hover {
  background: #ffe37a;
}

.btn:active {
  transform: translateY(6px);
  box-shadow: 0 2px 0 #3a1500;
}

.btn:focus-visible {
  outline: 4px solid #fff;
  outline-offset: 3px;
}

.btn.green {
  background: var(--slime);
}

.btn.green:hover {
  background: #a6ee72;
}

#titleScreen {
  align-items: center;
  justify-content: center;
  gap: 18px;
}

.game-title {
  margin: 0;
  font-size: 64px;
  letter-spacing: .06em;
  color: var(--pumpkin);
  -webkit-text-stroke: 3px #3a1500;
  paint-order: stroke fill;
  text-shadow: 0 6px 0 #3a1500, 0 0 28px rgba(255,138,31,.7);
}

.sub-title {
  margin: 0;
  font-size: 26px;
  font-weight: bold;
  color: var(--paper);
}

.levels {
  display: flex;
  gap: 26px;
  margin-top: 10px;
}

.level-btn {
  width: 230px;
  height: 190px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 32px 8px 8px;
  font-family: inherit;
  color: #3a1500;
  background: var(--paper);
  border: 5px solid #3a1500;
  border-radius: 32px;
  box-shadow: 0 10px 0 #3a1500;
  cursor: pointer;
  transition: transform .08s, box-shadow .08s, background .15s;
}

.level-btn:hover {
  background: #fff;
}

.level-btn:active {
  transform: translateY(8px);
  box-shadow: 0 2px 0 #3a1500;
}

.level-btn:focus-visible {
  outline: 4px solid #fff;
  outline-offset: 4px;
}

.level-btn .name {
  font-size: 34px;
  line-height: 1.3;
  white-space: nowrap;
  font-weight: bold;
}

.level-btn .cnt {
  font-size: 24px;
  font-weight: bold;
}

.level-btn[data-grid="2"] {
  background: #d9f7b8;
}

.level-btn[data-grid="3"] {
  background: #ffe2a8;
}

.level-btn[data-grid="4"] {
  background: #f3c9ff;
}

.timer-box {
  font-weight: bold;
  color: #3a1500;
  background: var(--paper);
  border: 4px solid #3a1500;
  border-radius: 20px;
  text-align: center;
}

.puzzle-main {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
}

.game-area {
  position: relative;
  width: var(--puzzle-size);
  height: var(--puzzle-size);
  margin-top: 0;
}

.preview {
  position: absolute;
  left: calc(-1*(var(--preview-size) + var(--gap)));
  top: 0;
  width: var(--preview-size);
  text-align: center;
}

.preview-title {
  font-size: 26px;
  font-weight: bold;
  color: var(--candy);
  margin-bottom: 8px;
  text-shadow: 0 3px 0 #3a1500;
}

.preview-img {
  width: var(--preview-size);
  height: var(--preview-size);
  border-radius: 14px;
  border: 4px solid var(--paper);
  background-color: #fff;
  background-size: cover;
  background-position: center;
  box-shadow: 0 6px 0 rgba(0,0,0,.4);
}

.puzzle-board {
  width: var(--puzzle-size);
  height: var(--puzzle-size);
  border: 4px solid var(--pumpkin);
  border-radius: 26px;
  background: #000;
  display: grid;
  overflow: hidden;
  touch-action: none;
  box-shadow: 0 0 0 4px #3a1500, 0 12px 28px rgba(0,0,0,.55);
}

.piece {
  border: 1px solid rgba(255,255,255,.55);
  background-repeat: no-repeat;
  cursor: grab;
  touch-action: none;
  background-origin: border-box;
}

.piece.drag-source {
  opacity: .3;
}

.piece.drop-target {
  outline: 5px solid var(--candy);
  outline-offset: -5px;
}

.drag-clone {
  position: fixed;
  z-index: 9999;
  pointer-events: none;
  transform: translate(-50%,-50%) scale(1.06);
  opacity: .92;
  cursor: grabbing;
  border: 2px solid #fff;
  box-shadow: 0 10px 24px rgba(0,0,0,.5);
}

.puzzle-board.clear-glow {
  animation: frameGlow 1.2s ease-in-out 1;
}

.puzzle-board.clear-glow .piece {
  border-color: transparent;
}

@keyframes frameGlow {
  0% {
    border-color: var(--pumpkin);
    box-shadow: 0 0 0 4px #3a1500, 0 0 0 0 rgba(255,226,90,0), 0 12px 28px rgba(0,0,0,.55);
  }
  40% {
    border-color: #fff3b0;
    box-shadow: 0 0 0 4px #ffd23f, 0 0 50px 22px rgba(255,226,90,.95), 0 12px 28px rgba(0,0,0,.55);
  }
  100% {
    border-color: var(--pumpkin);
    box-shadow: 0 0 0 4px #3a1500, 0 0 0 0 rgba(255,226,90,0), 0 12px 28px rgba(0,0,0,.55);
  }
}

.result {
  position: absolute;
  inset: 0;
  z-index: 10;
  display: none;
  align-items: center;
  justify-content: center;
  background: rgba(13,7,33,.72);
}

.result.show {
  display: flex;
}

.result-card {
  width: 560px;
  padding: 26px 30px 32px;
  text-align: center;
  color: #3a1500;
  background: var(--paper);
  border: 6px solid var(--pumpkin);
  border-radius: 36px;
  box-shadow: 0 0 0 5px #3a1500, 0 16px 40px rgba(0,0,0,.6);
  animation: pop .35s cubic-bezier(.3,1.6,.5,1);
  max-width: calc(100% - 24px);
}

@keyframes pop {
  from {
    transform: scale(.6);
    opacity: 0;
  }
  to {
    transform: scale(1);
    opacity: 1;
  }
}

.result-title {
  margin: 0;
  font-size: 54px;
  color: var(--pumpkin-dark);
}

.result-time {
  margin: 8px 0 0;
  font-size: 34px;
  font-weight: bold;
}

.result-time b {
  font-size: 64px;
  color: #d6321b;
}

.result-best {
  margin: 4px 0 0;
  font-size: 26px;
  font-weight: bold;
  min-height: 36px;
  color: #2b7a00;
}

.result-btns {
  display: flex;
  gap: 20px;
  justify-content: center;
  margin-top: 18px;
}

@media (prefers-reduced-motion:reduce) {
  .deco, .result-card, .puzzle-board.clear-glow {
    animation: none;
  }
  .puzzle-board.clear-glow {
    border-color: #fff3b0;
    box-shadow: 0 0 0 4px #ffd23f, 0 0 40px 16px rgba(255,226,90,.9);
  }
}

.header-title {
  margin: 0;
  font-size: 24px;
  white-space: nowrap;
}

.header-actions {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 10px;
}

.game-header .quit {
  min-height: 36px;
  padding: 2px 14px;
  font-size: 18px;
  border-width: 3px;
  box-shadow: 0 3px 0 #3a1500;
}

.sound-btn {
  font-family: inherit;
  border: 3px solid #f2b321;
  border-radius: 50px;
  padding: 6px 14px;
  font-size: 18px;
  font-weight: 700;
  color: #694100;
  cursor: pointer;
  letter-spacing: 1px;
  background: #fff0ad;
  box-shadow: 0 3px 0 #d89500;
  min-width: 104px;
  transition: border-color .15s,color .15s;
}

.sound-btn:hover {
  background: #ffe680;
  border-color: #dc9700;
  color: #5a3500;
}

.sound-btn.sound-off {
  background: #e9edf1;
  border-color: #aeb7c0;
  color: #66717b;
  box-shadow: 0 3px 0 #929ba4;
}

.sound-btn:focus-visible {
  outline: 3px solid white;
  outline-offset: 2px;
}

.is-title .game-header {
  position: absolute;
  top: 0;
  right: 0;
  left: 0;
  pointer-events: none;
}

.is-title .game-header .info,.is-title .header-title,.is-title .quit {
  display: none;
}

.is-title .header-actions {
  grid-column: 3;
}

.is-title .sound-btn {
  pointer-events: auto;
}

@media (max-width:800px) {
  .game-title {
    font-size: clamp(28px,7vw,56px);
  }
  .sub-title {
    font-size: 20px;
  }
  .levels {
    width: 94%;
    gap: 12px;
  }
  .level-btn {
    width: calc((100% - 24px)/3);
    height: 170px;
  }
  .level-btn .name {
    font-size: 24px;
  }
  .level-btn .cnt {
    font-size: 20px;
  }
  .header-title {
    font-size: 18px;
  }
  .info {
    font-size: 12px;
    flex-direction: column;
    gap: 0;
  }
  .header-actions {
    gap: 5px;
  }
  .game-header .quit {
    font-size: 15px;
    padding: 3px 8px;
  }
  .sound-btn {
    min-width: 76px;
    padding: 6px;
    font-size: 15px;
  }
  .game-area {
    --puzzle-size: min(360px,calc(100vw - 140px));
    --preview-size: 86px;
    --gap: 14px;
    margin-left: 100px;
  }
  .preview-title {
    font-size: 20px;
  }
}

@media (max-width:480px) {
  .game-header {
    gap: 3px;
    padding: 4px;
  }
  .header-title {
    font-size: 13px;
  }
  .game-header .quit {
    font-size: 13px;
    padding: 4px;
  }
  .sound-btn {
    min-width: 65px;
    font-size: 13px;
  }
  .game-area {
    --puzzle-size: min(290px,calc(100vw - 32px));
    --preview-size: 70px;
    margin: 90px 0 0;
  }
  .preview {
    left: 0;
    top: -98px;
    display: flex;
    align-items: center;
    gap: 10px;
    width: auto;
  }
  .level-btn .name {
    font-size: clamp(15px, 4.4vw, 20px);
  }
  .level-btn .cnt {
    font-size: 16px;
  }
  .result-card {
    padding: 20px 10px 26px;
  }
  .result-title {
    font-size: 40px;
  }
  .result-best {
    font-size: 20px;
  }
  .result-btns {
    gap: 10px;
  }
  .result-btns .btn {
    font-size: 18px;
    padding: 10px;
    min-height: 56px;
  }
}

.game-header .info {
  align-items: center;
}

.game-header .timer-box {
  font-size: 22px;
  line-height: 1.2;
  padding: 2px 14px;
  min-width: 0;
  white-space: nowrap;
}

@media (max-width:800px) {
  .game-header .info {
    align-items: flex-start;
  }
  .game-header .timer-box {
    font-size: 12px;
    line-height: 1;
    padding: 2px 6px;
  }
}

.confetti-layer {
  position: absolute;
  inset: 0;
  overflow: hidden;
  pointer-events: none;
  z-index: 11;
}

.confetti-piece {
  position: absolute;
  top: -20px;
  width: 9px;
  height: 15px;
  border-radius: 2px;
  animation: confetti-fall 3s linear both;
}

@keyframes confetti-fall {
  from {
    transform: translate(0,-20px) rotate(0);
    opacity: 1;
  }
  to {
    transform: translate(var(--drift),560px) rotate(var(--turn));
    opacity: 0;
  }
}

@media (prefers-reduced-motion:reduce) {
  .confetti-piece {
    animation: none;
    display: none;
  }
}

.game-header .level-badge {
  padding: 2px 10px;
  color: #3a1500;
  background: var(--paper);
  border: 3px solid #3a1500;
  border-radius: 20px;
  font-weight: bold;
  line-height: 1.2;
  white-space: nowrap;
  text-align: center;
}

@media (max-width:800px) {
  .game-header .level-badge {
    padding: 0 6px;
    border-width: 2px;
    line-height: 1;
  }
}
