:root {
  --bg: #e4e4e4;
  --ink: #121212;
  --accent: #ff1e10;
  --accent-dark: #d60f05;
  --board-shadow: 0 14px 30px rgba(0, 0, 0, 0.08);
  --tile-shadow: 0 10px 18px rgba(0, 0, 0, 0.11);
  --tile-selected: 0 0 0 4px rgba(255, 30, 16, 0.5);
  --tile-drop: 0 0 0 4px rgba(18, 18, 18, 0.25);
  --vvw: 100vw;
  --vvh: 100vh;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --screen-w: min(calc(var(--vvw) - 36px - var(--safe-left) - var(--safe-right)), 780px);
  --shell-w: var(--screen-w);
  --board-size: min(calc(var(--vvw) * 0.84), calc(var(--vvh) * 0.54), 540px);
  --board-gap: 6px;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--ink);
  font-family: "Hiragino Sans", "Yu Gothic", "Meiryo", sans-serif;
  overscroll-behavior: none;
}

body {
  min-height: var(--vvh);
  height: var(--vvh);
  position: fixed;
  inset: 0;
  touch-action: manipulation;
}

button,
input,
textarea,
select {
  font: inherit;
}

img {
  max-width: 100%;
  display: block;
  -webkit-user-drag: none;
}

.app {
  width: 100%;
  height: var(--vvh);
  position: relative;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  justify-items: center;
  overflow: hidden;
  padding:
    max(14px, calc(var(--safe-top) + 8px))
    max(14px, calc(var(--safe-right) + 10px))
    max(14px, calc(var(--safe-bottom) + 10px))
    max(14px, calc(var(--safe-left) + 10px));
}

.app[data-screen="start"] {
  --shell-w: min(calc(var(--vvw) - 36px - var(--safe-left) - var(--safe-right)), 900px);
}

.app[data-screen="play"] {
  --shell-w: min(calc(var(--vvw) - 36px - var(--safe-left) - var(--safe-right)), 1080px);
}

.app[data-screen="complete"] {
  --shell-w: min(calc(var(--vvw) - 36px - var(--safe-left) - var(--safe-right)), 860px);
}

.screen-title {
  width: min(100%, var(--shell-w));
  text-align: center;
  font-size: clamp(34px, 7.4vw, 72px);
  line-height: 1.02;
  font-weight: 700;
  letter-spacing: 0.04em;
  margin: 0 0 clamp(4px, 1vh, 10px);
  flex-shrink: 0;
}

.screen-shell {
  width: min(100%, var(--shell-w));
  min-height: 0;
  display: flex;
  align-items: stretch;
  justify-content: center;
  overflow: hidden;
}

.screen {
  width: 100%;
  height: 100%;
  min-height: 0;
  display: none;
  overflow: hidden;
}

.screen.is-active {
  display: flex;
}

.screen-start {
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: clamp(20px, 3.4vh, 34px);
  padding: clamp(8px, 1vh, 16px) 0 max(6px, 1vh);
}

.hero-visual {
  width: min(88vw, 720px);
  max-width: 100%;
  min-height: 0;
  height: min(calc(var(--vvh) * 0.46), 420px);
  display: flex;
  align-items: center;
  justify-content: center;
}

.hero-visual img {
  width: 100%;
  height: 100%;
  object-fit: contain;
}

.primary-button,
.secondary-button {
  appearance: none;
  border: 0;
  cursor: pointer;
  transition: transform 0.16s ease, box-shadow 0.16s ease, opacity 0.16s ease;
}

.primary-button:hover,
.secondary-button:hover {
  transform: translateY(-1px);
}

.primary-button:active,
.secondary-button:active {
  transform: translateY(1px) scale(0.99);
}

.primary-button {
  border-radius: 22px;
  background: var(--accent);
  color: #fff;
  padding: 14px 28px;
  min-width: 180px;
  box-shadow: 0 14px 24px rgba(255, 30, 16, 0.24);
}

.primary-button span,
.primary-button {
  font-size: clamp(26px, 6.4vw, 58px);
  line-height: 1;
  font-weight: 500;
  letter-spacing: 0.08em;
}

.primary-button-large {
  width: min(70vw, 520px);
  min-height: clamp(84px, 14vh, 140px);
}

.secondary-button {
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.58);
  color: var(--ink);
  padding: 10px 18px;
  min-width: 110px;
  border: 1px solid rgba(0, 0, 0, 0.08);
  box-shadow: 0 8px 16px rgba(0, 0, 0, 0.05);
  font-size: 16px;
}

.screen-play {
  align-items: center;
  justify-content: center;
  padding: 0;
}

.play-stage {
  width: 100%;
  height: 100%;
  min-height: 0;
  display: grid;
  grid-template-rows: minmax(0, 1fr) auto auto;
  justify-items: center;
  align-items: center;
  gap: clamp(12px, 2vh, 20px);
}

.board-wrap {
  width: 100%;
  min-height: 0;
  display: flex;
  justify-content: center;
  align-items: center;
}

.board {
  width: var(--board-size);
  aspect-ratio: 1 / 1;
  max-width: 100%;
  display: grid;
  gap: var(--board-gap);
  padding: var(--board-gap);
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  border-radius: 28px;
  background: rgba(255, 255, 255, 0.28);
  box-shadow: var(--board-shadow);
  touch-action: none;
  user-select: none;
  will-change: transform, filter, box-shadow;
  transform: translateZ(0);
  filter: brightness(1) saturate(1);
}

.board.is-complete-flash {
  animation: board-complete-flash 0.9s ease-in-out 1;
}

@keyframes board-complete-flash {
  0% {
    transform: scale(1);
    filter: brightness(1) saturate(1);
    box-shadow: var(--board-shadow);
  }
  18% {
    transform: scale(1.012);
    filter: brightness(1.2) saturate(1.08);
    box-shadow:
      0 0 0 10px rgba(255, 255, 255, 0.42),
      0 0 34px rgba(255, 255, 255, 0.28),
      var(--board-shadow);
  }
  42% {
    transform: scale(1);
    filter: brightness(1.03) saturate(1.02);
    box-shadow:
      0 0 0 0 rgba(255, 255, 255, 0),
      0 0 0 rgba(255, 255, 255, 0),
      var(--board-shadow);
  }
  68% {
    transform: scale(1.008);
    filter: brightness(1.14) saturate(1.06);
    box-shadow:
      0 0 0 6px rgba(255, 255, 255, 0.26),
      0 0 22px rgba(255, 255, 255, 0.16),
      var(--board-shadow);
  }
  100% {
    transform: scale(1);
    filter: brightness(1) saturate(1);
    box-shadow: var(--board-shadow);
  }
}

.tile {
  appearance: none;
  position: relative;
  border: 0;
  border-radius: 18px;
  overflow: hidden;
  background: transparent;
  padding: 0;
  touch-action: none;
  box-shadow: var(--tile-shadow);
  transform: translateZ(0);
}

.tile::before {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: inherit;
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.08);
  pointer-events: none;
}

.tile img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  pointer-events: none;
}

.tile.is-selected {
  box-shadow: var(--tile-shadow), var(--tile-selected);
}

.tile.is-drop-target {
  box-shadow: var(--tile-shadow), var(--tile-drop);
}

.tile.is-source {
  opacity: 0.34;
}

.tile-ghost {
  position: fixed;
  z-index: 9999;
  width: min(24vw, 150px);
  aspect-ratio: 1 / 1;
  pointer-events: none;
  border-radius: 18px;
  overflow: hidden;
  box-shadow: 0 18px 35px rgba(0, 0, 0, 0.22);
  transform: translate(-50%, -50%);
}

.tile-ghost img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.control-strip {
  width: min(100%, 620px);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
}

.status-pills,
.action-pills {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

.status-pill {
  min-width: 96px;
  padding: 10px 14px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.56);
  box-shadow: 0 8px 16px rgba(0, 0, 0, 0.05);
  border: 1px solid rgba(0, 0, 0, 0.06);
  text-align: center;
  font-size: 14px;
}

.play-hint {
  margin: 0;
  min-height: 1.4em;
  font-size: 14px;
  color: rgba(0, 0, 0, 0.72);
  text-align: center;
}

.complete-overlay {
  position: fixed;
  inset: 0;
  background: rgba(228, 228, 228, 0.94);
  backdrop-filter: blur(3px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding:
    max(18px, calc(var(--safe-top) + 10px))
    max(18px, calc(var(--safe-right) + 12px))
    max(18px, calc(var(--safe-bottom) + 12px))
    max(18px, calc(var(--safe-left) + 12px));
  z-index: 80;
  overflow: hidden;
}

.complete-card {
  width: min(100%, 760px);
  max-height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: clamp(10px, 1.8vh, 16px);
  position: relative;
  z-index: 2;
}

.complete-title,
.complete-message {
  text-align: center;
  line-height: 1.05;
  color: var(--ink);
}

.complete-title {
  font-size: clamp(32px, 6.4vw, 68px);
  font-weight: 700;
  letter-spacing: 0.04em;
}

.complete-visual {
  width: min(100%, 560px);
  height: min(calc(var(--vvh) * 0.34), 300px);
  display: flex;
  justify-content: center;
  align-items: center;
}

.complete-visual img {
  width: 100%;
  height: 100%;
  object-fit: contain;
}

.complete-message {
  font-size: clamp(32px, 8vw, 84px);
  font-weight: 700;
  letter-spacing: 0.05em;
}

.complete-stats {
  font-size: 16px;
  text-align: center;
}

.complete-actions {
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
  justify-content: center;
}

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

.confetti-piece {
  position: absolute;
  width: 14px;
  height: 20px;
  top: -10vh;
  border-radius: 2px;
  opacity: 0.9;
  animation: confetti-fall linear forwards;
}

@keyframes confetti-fall {
  0% {
    transform: translate3d(0, 0, 0) rotate(0deg);
  }
  100% {
    transform: translate3d(var(--drift-x), 110vh, 0) rotate(var(--rotate-end));
  }
}

[hidden] {
  display: none !important;
}

@media (max-width: 640px) {
  :root {
    --board-size: min(calc(var(--vvw) * 0.9), calc(var(--vvh) * 0.5), 420px);
    --board-gap: 5px;
  }

  .screen-title {
    font-size: clamp(28px, 7vw, 48px);
  }

  .control-strip {
    justify-content: center;
  }

  .primary-button {
    border-radius: 18px;
  }

  .secondary-button {
    font-size: 15px;
  }
}

@media (max-height: 760px) {
  :root {
    --board-size: min(calc(var(--vvw) * 0.82), calc(var(--vvh) * 0.46), 480px);
  }

  .screen-title {
    font-size: clamp(26px, 6vw, 44px);
  }

  .screen-start {
    gap: 16px;
  }

  .hero-visual {
    height: min(calc(var(--vvh) * 0.36), 300px);
  }

  .primary-button-large {
    min-height: clamp(72px, 12vh, 108px);
  }

  .complete-visual {
    height: min(calc(var(--vvh) * 0.25), 220px);
  }
}

@media (min-aspect-ratio: 11 / 10) {
  :root {
    --board-size: min(calc(var(--vvh) * 0.66), calc(var(--vvw) * 0.48), 560px);
  }

  .screen-title {
    font-size: clamp(28px, 4.8vw, 58px);
    margin-bottom: 4px;
  }

  .screen-start {
    gap: clamp(14px, 2.6vh, 26px);
    padding-top: 0;
  }

  .hero-visual {
    width: min(58vw, 720px);
    height: min(calc(var(--vvh) * 0.48), 360px);
  }

  .primary-button-large {
    width: min(44vw, 460px);
    min-height: clamp(76px, 12vh, 116px);
  }

  .play-stage {
    grid-template-columns: minmax(0, 1fr) minmax(220px, 290px);
    grid-template-rows: minmax(0, 1fr) auto;
    column-gap: clamp(18px, 2.8vw, 30px);
    row-gap: 16px;
  }

  .board-wrap {
    grid-column: 1;
    grid-row: 1 / span 2;
    height: 100%;
  }

  .control-strip {
    grid-column: 2;
    grid-row: 1;
    width: 100%;
    max-width: 290px;
    flex-direction: column;
    align-items: stretch;
    justify-content: center;
  }

  .status-pills,
  .action-pills {
    justify-content: center;
  }

  .play-hint {
    grid-column: 2;
    grid-row: 2;
    width: min(100%, 290px);
  }

  .complete-card {
    width: min(100%, 980px);
    display: grid;
    grid-template-columns: minmax(260px, 0.92fr) minmax(280px, 1fr);
    grid-template-areas:
      "title title"
      "visual message"
      "visual stats"
      "visual actions";
    align-items: center;
    justify-items: center;
    column-gap: clamp(18px, 3vw, 36px);
  }

  .complete-title {
    grid-area: title;
  }

  .complete-visual {
    grid-area: visual;
    width: min(44vw, 460px);
    height: min(calc(var(--vvh) * 0.46), 360px);
  }

  .complete-message {
    grid-area: message;
  }

  .complete-stats {
    grid-area: stats;
  }

  .complete-actions {
    grid-area: actions;
  }
}
