/* 2048ゲーム固有スタイル */

.g2048-stage {
  position: relative;
  width: 100%;
  max-width: min(100%, 480px);
  margin: 0 auto 14px;
}

/* 盤面は視認性を優先し、Neon Skyの夜空側の色でまとめる */
.g2048-board {
  --g2048-gap: clamp(6px, 2vw, 12px);
  position: relative;
  width: 100%;
  aspect-ratio: 1 / 1;
  padding: var(--g2048-gap);
  border: 1px solid rgba(96, 154, 214, 0.35);
  border-radius: var(--md-sys-shape-corner-large);
  background:
    radial-gradient(circle at 18% 0%, rgba(63, 196, 255, 0.16), transparent 62%),
    radial-gradient(circle at 86% 100%, rgba(150, 106, 255, 0.16), transparent 60%),
    linear-gradient(160deg, #16264a 0%, #101c38 55%, #0d1730 100%);
  box-shadow: 0 14px 34px rgba(28, 52, 96, 0.24), inset 0 0 0 1px rgba(255, 255, 255, 0.04);
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  outline: none;
}

.g2048-board:focus-visible {
  border-color: rgba(64, 200, 255, 0.85);
  box-shadow: 0 0 0 3px rgba(64, 200, 255, 0.28), 0 14px 34px rgba(28, 52, 96, 0.24);
}

.g2048-cells,
.g2048-tiles {
  position: absolute;
  top: var(--g2048-gap);
  right: var(--g2048-gap);
  bottom: var(--g2048-gap);
  left: var(--g2048-gap);
}

.g2048-cells {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  grid-template-rows: repeat(4, minmax(0, 1fr));
  gap: var(--g2048-gap);
}

.g2048-cell {
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.05);
}

.g2048-tiles {
  pointer-events: none;
}

.g2048-tile {
  position: absolute;
  top: 0;
  left: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  width: calc((100% - var(--g2048-gap) * 3) / 4);
  height: calc((100% - var(--g2048-gap) * 3) / 4);
  border-radius: 10px;
  background: rgba(226, 238, 255, 0.14);
  color: #eaf2ff;
  font-family: "Outfit", "Noto Sans JP", sans-serif;
  font-size: clamp(1.05rem, 7vw, 2.25rem);
  font-weight: 700;
  line-height: 1;
  letter-spacing: -0.02em;
  /* translateの100%はタイル自身のサイズ = 1マス分 */
  transform: translate(calc((100% + var(--g2048-gap)) * var(--col)), calc((100% + var(--g2048-gap)) * var(--row)));
  transition: transform 110ms ease-in-out;
  will-change: transform;
}

.g2048-tile.is-len3 { font-size: clamp(0.95rem, 6.1vw, 1.95rem); }
.g2048-tile.is-len4 { font-size: clamp(0.8rem, 5vw, 1.6rem); }
.g2048-tile.is-len5 { font-size: clamp(0.68rem, 4.1vw, 1.3rem); }

/* 合体で消えるタイルは新しいタイルの下へ重ねる */
.g2048-tile.is-consumed {
  z-index: 0;
}

/* fill-modeをbackwardsにして、アニメーション後は素のtransformへ戻す（次の移動のtransitionを妨げないため） */
.g2048-tile.is-new {
  animation: g2048-appear 130ms ease-out 110ms backwards;
}

.g2048-tile.is-merged {
  z-index: 1;
  animation: g2048-pop 150ms ease-out 110ms backwards;
}

@keyframes g2048-appear {
  from { opacity: 0; transform: translate(calc((100% + var(--g2048-gap)) * var(--col)), calc((100% + var(--g2048-gap)) * var(--row))) scale(0.4); }
  to { opacity: 1; transform: translate(calc((100% + var(--g2048-gap)) * var(--col)), calc((100% + var(--g2048-gap)) * var(--row))) scale(1); }
}

@keyframes g2048-pop {
  0% { transform: translate(calc((100% + var(--g2048-gap)) * var(--col)), calc((100% + var(--g2048-gap)) * var(--row))) scale(0.6); }
  60% { transform: translate(calc((100% + var(--g2048-gap)) * var(--col)), calc((100% + var(--g2048-gap)) * var(--row))) scale(1.14); }
  100% { transform: translate(calc((100% + var(--g2048-gap)) * var(--col)), calc((100% + var(--g2048-gap)) * var(--row))) scale(1); }
}

/* 数字が大きくなるほど発光を強める。数字の可読性を優先し文字色は明るいまま保つ */
.g2048-tile[data-value="2"] {
  background: rgba(226, 238, 255, 0.13);
  color: #dfe9fb;
}

.g2048-tile[data-value="4"] {
  background: rgba(226, 238, 255, 0.2);
  color: #eef4ff;
}

.g2048-tile[data-value="8"] {
  background: linear-gradient(150deg, rgba(31, 196, 232, 0.4), rgba(31, 142, 230, 0.32));
  box-shadow: 0 0 10px rgba(43, 199, 240, 0.22);
}

.g2048-tile[data-value="16"] {
  background: linear-gradient(150deg, rgba(31, 196, 232, 0.58), rgba(31, 142, 230, 0.46));
  box-shadow: 0 0 14px rgba(43, 199, 240, 0.3);
}

.g2048-tile[data-value="32"] {
  background: linear-gradient(150deg, #1f9fe4, #1f6fd6);
  box-shadow: 0 0 18px rgba(43, 165, 240, 0.42);
}

.g2048-tile[data-value="64"] {
  background: linear-gradient(150deg, #3d86ec, #5a5fe0);
  box-shadow: 0 0 20px rgba(76, 128, 240, 0.48);
}

.g2048-tile[data-value="128"] {
  background: linear-gradient(150deg, #6f6bec, #8a5cf0);
  box-shadow: 0 0 22px rgba(138, 92, 240, 0.5);
}

.g2048-tile[data-value="256"] {
  background: linear-gradient(150deg, #9a5cf0, #c257e0);
  box-shadow: 0 0 24px rgba(170, 88, 240, 0.55);
}

.g2048-tile[data-value="512"] {
  background: linear-gradient(150deg, #c257e0, #e85ec8);
  box-shadow: 0 0 26px rgba(232, 94, 200, 0.55);
}

.g2048-tile[data-value="1024"] {
  background: linear-gradient(150deg, #e8953a, #e85e8f);
  box-shadow: 0 0 28px rgba(232, 140, 80, 0.6);
}

.g2048-tile[data-value="2048"],
.g2048-tile.is-super {
  background: linear-gradient(150deg, #ffd166, #ff8fd2 55%, #7cd6ff 100%);
  color: #16233b;
  text-shadow: 0 1px 0 rgba(255, 255, 255, 0.5);
  box-shadow: 0 0 18px rgba(255, 214, 120, 0.6), 0 0 34px rgba(255, 143, 210, 0.4);
}

.g2048-tile.is-super {
  border: 1px solid rgba(255, 255, 255, 0.7);
}

.g2048-overlay {
  position: absolute;
  inset: 0;
  z-index: 2;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 20px 16px;
  border-radius: var(--md-sys-shape-corner-large);
  background: rgba(11, 20, 42, 0.92);
  backdrop-filter: blur(5px);
  -webkit-backdrop-filter: blur(5px);
  text-align: center;
  animation: g2048-fade 180ms ease-out;
}

@keyframes g2048-fade {
  from { opacity: 0; }
  to { opacity: 1; }
}

.g2048-overlay__title {
  margin: 0;
  color: #ffffff;
  font-family: "Outfit", "Noto Sans JP", sans-serif;
  font-size: clamp(1.5rem, 7vw, 2.1rem);
  font-weight: 700;
  letter-spacing: 0.02em;
}

.g2048-overlay.is-win .g2048-overlay__title {
  color: #ffe6a3;
  text-shadow: 0 0 18px rgba(255, 205, 110, 0.7), 0 0 34px rgba(255, 143, 210, 0.45);
}

.g2048-overlay__detail {
  margin: 0;
  color: rgba(230, 240, 255, 0.86);
  font-size: 0.9375rem;
  line-height: 1.6;
}

.g2048-overlay__actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 10px;
  margin-top: 12px;
}

.g2048-overlay__actions .btn {
  min-width: 8.5rem;
  justify-content: center;
}

.g2048-guide {
  margin: 0 auto 4px;
  max-width: min(100%, 480px);
  color: var(--md-sys-color-on-surface-variant);
  font-size: 0.8125rem;
  line-height: 1.6;
  text-align: center;
}

.game-panel {
  margin-top: 20px;
}

.game-controls {
  justify-content: center;
  max-width: min(100%, 480px);
  margin-left: auto;
  margin-right: auto;
}

.game-controls .btn {
  min-width: 9rem;
  justify-content: center;
}

@media (max-width: 480px) {
  .g2048-overlay__actions .btn {
    min-width: 7.5rem;
  }
}

@media (prefers-reduced-motion: reduce) {
  .g2048-tile {
    transition: none;
  }

  .g2048-tile.is-new,
  .g2048-tile.is-merged,
  .g2048-overlay {
    animation: none;
  }
}

/* 外枠・HUD・ボタンは共通テーマ。4×4盤面だけ高さに合わせて縮める。 */
html.gs-playing .game-panel {
  --g2048-size: min(480px, calc(100vw - 28px), max(140px, calc(100dvh - 220px)));
}

html.gs-playing .game-panel > .gs-bar,
html.gs-playing .game-panel > .gs-hud,
html.gs-playing .game-panel > .game-controls {
  flex: none;
  width: 100%;
  max-width: 640px;
  margin-left: auto;
  margin-right: auto;
}

html.gs-playing .g2048-stage {
  flex: none;
  width: var(--g2048-size);
  max-width: 100%;
  margin-top: auto;
  margin-bottom: 14px;
}

html.gs-playing .g2048-board {
  border-color: var(--play-border);
}

html.gs-playing .g2048-tile {
  font-size: clamp(1rem, var(--g2048-size) * .075, 2.25rem);
}
html.gs-playing .g2048-tile.is-len3 { font-size: clamp(.85rem, var(--g2048-size) * .065, 1.95rem); }
html.gs-playing .g2048-tile.is-len4 { font-size: clamp(.7rem, var(--g2048-size) * .053, 1.6rem); }
html.gs-playing .g2048-tile.is-len5 { font-size: clamp(.6rem, var(--g2048-size) * .043, 1.3rem); }

html.gs-playing .game-panel .game-controls {
  margin-top: 0;
  margin-bottom: auto;
}

html.gs-playing .game-controls .gs-icon-btn { min-width: 44px; }

html.gs-playing .g2048-guide {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

@media (min-width: 768px) and (min-height: 700px) {
  html.gs-playing .game-panel { padding-top: 24px; padding-bottom: 24px; }
  html.gs-playing .game-panel > .gs-bar { margin-bottom: 18px; }
  html.gs-playing .g2048-stage { margin-bottom: 22px; }
}

@media (orientation: landscape) and (max-height: 500px) {
  html.gs-playing .game-panel {
    --g2048-size: min(480px, calc(100dvh - 28px), calc(100vw - 230px));
    display: grid;
    grid-template-columns: minmax(140px, 180px) var(--g2048-size);
    grid-template-rows: auto auto 1fr;
    grid-template-areas: "bar board" "status board" "controls board";
    gap: 10px 16px;
    justify-content: center;
    align-content: center;
  }
  html.gs-playing .game-panel > .gs-bar { grid-area: bar; margin-bottom: 0; }
  html.gs-playing .game-panel > .gs-hud { grid-area: status; margin-bottom: 0; }
  html.gs-playing .g2048-stage { grid-area: board; margin: 0; align-self: center; }
  html.gs-playing .game-panel .game-controls { grid-area: controls; margin-bottom: 0; }
  html.gs-playing .g2048-overlay { padding: 10px; }
  html.gs-playing .g2048-overlay__actions { gap: 6px; margin-top: 6px; }
}
