/* 2048 - style.css (Midnight Nebula / Cosmic Plasma Theme) */

body:has(.g48) {
  background: #0a0e1a;
  background-image: radial-gradient(circle at 50% 15%, #171c36 0%, #0a0e1a 80%);
  background-repeat: no-repeat;
  background-attachment: fixed;
  background-size: cover;
  color: #e2e8f0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  margin: 0;
  padding: 0;
}

body:has(.g48) > main {
  padding: 0;
  background: transparent;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: var(--yz-stage-h, 100vh);
  box-sizing: border-box;
}

.g48 {
  width: 100%;
  max-width: 480px;
  margin: 0 auto;
  padding: 12px;
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.g48-container {
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

/* ヘッダー */
.g48-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  gap: 12px;
}

.g48-title-area {
  flex: 1;
}

.g48-title {
  font-size: 42px;
  font-weight: 900;
  line-height: 1;
  margin: 0;
  letter-spacing: -1px;
  background: linear-gradient(135deg, #38bdf8 0%, #818cf8 50%, #c084fc 100%);
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
  filter: drop-shadow(0 2px 8px rgba(129, 140, 248, 0.35));
}

.g48-subtitle {
  font-size: 13px;
  margin: 4px 0 0 0;
  color: #94a3b8;
  font-weight: 500;
}

.g48-scores {
  display: flex;
  gap: 8px;
}

.g48-score-box {
  background: rgba(26, 33, 56, 0.7);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  border: 1px solid rgba(255, 255, 255, 0.08);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.25);
  color: #fff;
  padding: 6px 14px;
  border-radius: 8px;
  min-width: 72px;
  text-align: center;
  position: relative;
  box-sizing: border-box;
}

.g48-score-label {
  display: block;
  font-size: 10px;
  font-weight: 800;
  color: #64748b;
  letter-spacing: 1px;
}

.g48-score-val {
  display: block;
  font-size: 20px;
  font-weight: 800;
  line-height: 1.2;
  font-feature-settings: "tnum";
}

#g48Score {
  color: #38bdf8;
  text-shadow: 0 0 10px rgba(56, 189, 248, 0.3);
}

#g48Best {
  color: #fbbf24;
  text-shadow: 0 0 10px rgba(251, 191, 36, 0.3);
}

/* スコア加算フロート表示 */
.g48-score-add {
  position: absolute;
  top: 10px;
  right: 12px;
  font-size: 15px;
  font-weight: 900;
  color: #38bdf8;
  opacity: 0;
  pointer-events: none;
  transform: translateY(0);
  text-shadow: 0 0 12px rgba(56, 189, 248, 0.8);
}

.g48-score-add.is-active {
  animation: g48-score-rise 600ms ease-out;
}

@keyframes g48-score-rise {
  0% {
    opacity: 1;
    transform: translateY(0) scale(1.1);
  }
  100% {
    opacity: 0;
    transform: translateY(-26px) scale(0.9);
  }
}

/* ツールバー */
.g48-toolbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
}

.g48-actions {
  display: flex;
  gap: 8px;
}

.g48-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 44px;
  min-width: 44px;
  padding: 0 16px;
  border: none;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 700;
  cursor: pointer;
  transition: all 150ms ease;
  user-select: none;
  touch-action: manipulation;
}

.g48 [hidden],
.g48-btn[hidden] {
  display: none !important;
}

.g48-btn:active {
  transform: scale(0.96);
}

.g48-btn-primary {
  background: linear-gradient(135deg, #0284c7 0%, #4f46e5 100%);
  color: #ffffff;
  box-shadow: 0 4px 14px rgba(14, 165, 233, 0.3);
  border: 1px solid rgba(255, 255, 255, 0.15);
}

.g48-btn-primary:hover:not(:disabled) {
  background: linear-gradient(135deg, #0ea5e9 0%, #6366f1 100%);
  box-shadow: 0 6px 18px rgba(14, 165, 233, 0.45);
}

.g48-btn-secondary {
  background: rgba(30, 41, 59, 0.85);
  color: #cbd5e1;
  border: 1px solid rgba(148, 163, 184, 0.2);
}

.g48-btn-secondary:hover:not(:disabled) {
  background: rgba(51, 65, 85, 0.95);
  color: #f8fafc;
  border-color: rgba(148, 163, 184, 0.35);
}

.g48-btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
  transform: none;
  box-shadow: none;
}

.g48-hint {
  font-size: 12px;
  color: #64748b;
  text-align: right;
  letter-spacing: 0.2px;
}

/* ボード定義 */
.g48-board-wrapper {
  position: relative;
  width: 100%;
  display: flex;
  justify-content: center;
}

.g48-board {
  --board-gap: 12px;
  --board-size: min(calc(100vw - 32px), calc(var(--yz-stage-h, 100vh) - 180px), 460px);
  --cell-size: calc((var(--board-size) - (var(--board-gap) * 5)) / 4);

  position: relative;
  width: var(--board-size);
  height: var(--board-size);
  background: #111827;
  border: 1px solid rgba(99, 102, 241, 0.25);
  border-radius: 12px;
  padding: var(--board-gap);
  box-sizing: border-box;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  outline: none;
  box-shadow: 0 16px 36px -8px rgba(0, 0, 0, 0.7), 0 0 24px rgba(99, 102, 241, 0.12);
}

.g48-board:focus-visible {
  border-color: #38bdf8;
  box-shadow: 0 0 0 3px rgba(56, 189, 248, 0.4), 0 16px 36px -8px rgba(0, 0, 0, 0.7);
}

/* グリッド背景セル */
.g48-grid {
  display: flex;
  flex-direction: column;
  gap: var(--board-gap);
  width: 100%;
  height: 100%;
}

.g48-grid-row {
  display: flex;
  gap: var(--board-gap);
  flex: 1;
}

.g48-grid-cell {
  flex: 1;
  background: rgba(30, 41, 59, 0.45);
  border: 1px solid rgba(255, 255, 255, 0.03);
  border-radius: 6px;
}

/* タイルレイヤー */
.g48-tiles {
  position: absolute;
  top: var(--board-gap);
  left: var(--board-gap);
  right: var(--board-gap);
  bottom: var(--board-gap);
  pointer-events: none;
}

/* タイル要素 */
.g48-tile {
  position: absolute;
  top: 0;
  left: 0;
  width: var(--cell-size);
  height: var(--cell-size);
  border-radius: 6px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: calc(var(--cell-size) * 0.42);
  font-weight: 800;
  line-height: 1;
  box-sizing: border-box;
  z-index: 10;
  font-feature-settings: "tnum";
  transform: translate(
    calc(var(--c) * (var(--cell-size) + var(--board-gap))),
    calc(var(--r) * (var(--cell-size) + var(--board-gap)))
  );
  transition: transform 110ms cubic-bezier(0.25, 1, 0.5, 1);
  will-change: transform;
}

/* 数字桁数ごとの文字サイズ縮小 */
.g48-tile[data-value="128"],
.g48-tile[data-value="256"],
.g48-tile[data-value="512"] {
  font-size: calc(var(--cell-size) * 0.36);
}

.g48-tile[data-value="1024"],
.g48-tile[data-value="2048"] {
  font-size: calc(var(--cell-size) * 0.30);
}

.g48-tile[data-digits="5"],
.g48-tile[data-digits="6"] {
  font-size: calc(var(--cell-size) * 0.24);
}

/* アニメーション: 新規出現 */
.g48-tile.is-new {
  animation: g48-appear 190ms cubic-bezier(0.18, 0.89, 0.32, 1.28);
}

@keyframes g48-appear {
  0% {
    opacity: 0;
    transform: translate(
      calc(var(--c) * (var(--cell-size) + var(--board-gap))),
      calc(var(--r) * (var(--cell-size) + var(--board-gap)))
    ) scale(0.15);
  }
  100% {
    opacity: 1;
    transform: translate(
      calc(var(--c) * (var(--cell-size) + var(--board-gap))),
      calc(var(--r) * (var(--cell-size) + var(--board-gap)))
    ) scale(1);
  }
}

/* アニメーション: 合体ポップ */
.g48-tile.is-merged {
  animation: g48-pop 210ms cubic-bezier(0.18, 0.89, 0.32, 1.28);
  z-index: 20;
}

@keyframes g48-pop {
  0% {
    transform: translate(
      calc(var(--c) * (var(--cell-size) + var(--board-gap))),
      calc(var(--r) * (var(--cell-size) + var(--board-gap)))
    ) scale(0.8);
    filter: brightness(1.4);
  }
  50% {
    transform: translate(
      calc(var(--c) * (var(--cell-size) + var(--board-gap))),
      calc(var(--r) * (var(--cell-size) + var(--board-gap)))
    ) scale(1.18);
    filter: brightness(1.2);
  }
  100% {
    transform: translate(
      calc(var(--c) * (var(--cell-size) + var(--board-gap))),
      calc(var(--r) * (var(--cell-size) + var(--board-gap)))
    ) scale(1);
    filter: brightness(1);
  }
}

/* タイルの独自カラーリング（Cosmic Plasma / Energy Level パレット） */
.g48-tile-2 {
  background: #1e293b;
  color: #94a3b8;
  border: 1px solid rgba(148, 163, 184, 0.15);
  box-shadow: inset 0 1px 1px rgba(255, 255, 255, 0.05);
}

.g48-tile-4 {
  background: #0f3b46;
  color: #38bdf8;
  border: 1px solid rgba(56, 189, 248, 0.3);
  box-shadow: 0 0 10px rgba(56, 189, 248, 0.2);
}

.g48-tile-8 {
  background: #064e3b;
  color: #34d399;
  border: 1px solid rgba(52, 211, 153, 0.35);
  box-shadow: 0 0 14px rgba(52, 211, 153, 0.28);
}

.g48-tile-16 {
  background: #14532d;
  color: #4ade80;
  border: 1px solid rgba(74, 222, 128, 0.4);
  box-shadow: 0 0 16px rgba(74, 222, 128, 0.32);
}

.g48-tile-32 {
  background: #1d4ed8;
  color: #bfdbfe;
  border: 1px solid rgba(147, 197, 253, 0.45);
  box-shadow: 0 0 18px rgba(59, 130, 246, 0.42);
}

.g48-tile-64 {
  background: #4338ca;
  color: #c7d2fe;
  border: 1px solid rgba(199, 210, 254, 0.5);
  box-shadow: 0 0 22px rgba(99, 102, 241, 0.5);
}

.g48-tile-128 {
  background: #6d28d9;
  color: #f5d0fe;
  border: 1px solid rgba(216, 180, 254, 0.55);
  box-shadow: 0 0 25px rgba(139, 92, 246, 0.58);
}

.g48-tile-256 {
  background: #9d174d;
  color: #fbcfe8;
  border: 1px solid rgba(244, 114, 182, 0.6);
  box-shadow: 0 0 28px rgba(236, 72, 153, 0.65);
}

.g48-tile-512 {
  background: #be123c;
  color: #ffe4e6;
  border: 1px solid rgba(251, 113, 133, 0.65);
  box-shadow: 0 0 32px rgba(244, 63, 94, 0.7);
}

.g48-tile-1024 {
  background: #c2410c;
  color: #ffedd5;
  border: 1px solid rgba(251, 146, 60, 0.7);
  box-shadow: 0 0 36px rgba(249, 115, 22, 0.78);
}

.g48-tile-2048 {
  background: linear-gradient(135deg, #f59e0b 0%, #ec4899 50%, #8b5cf6 100%);
  color: #ffffff;
  border: 1px solid rgba(255, 255, 255, 0.85);
  box-shadow: 0 0 45px rgba(236, 72, 153, 0.85), 0 0 15px rgba(245, 158, 11, 0.9);
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.5);
}

.g48-tile-super {
  background: linear-gradient(135deg, #06b6d4 0%, #3b82f6 35%, #ec4899 70%, #f59e0b 100%);
  color: #ffffff;
  border: 1px solid rgba(255, 255, 255, 0.9);
  box-shadow: 0 0 50px rgba(6, 182, 212, 0.9), 0 0 20px rgba(236, 72, 153, 0.9);
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.6);
}

/* オーバーレイ */
.g48-overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(10, 14, 26, 0.88);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
  animation: g48-fade-in 260ms ease-in-out;
}

.g48-overlay[hidden] {
  display: none !important;
}

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

.g48-overlay-content {
  text-align: center;
  padding: 24px;
}

.g48-overlay-title {
  font-size: 38px;
  font-weight: 900;
  color: #f8fafc;
  margin: 0 0 8px 0;
  background: linear-gradient(135deg, #38bdf8, #c084fc);
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
  filter: drop-shadow(0 2px 10px rgba(56, 189, 248, 0.4));
}

.g48-overlay-msg {
  font-size: 16px;
  color: #94a3b8;
  margin: 0 0 20px 0;
  font-weight: 500;
}

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

.g48-btn-large {
  min-height: 48px;
  padding: 0 24px;
  font-size: 16px;
}

/* 幅が狭い画面（375px〜） */
@media (max-width: 440px) {
  .g48 {
    padding: 8px;
  }
  .g48-board {
    --board-gap: 8px;
  }
  .g48-title {
    font-size: 34px;
  }
  .g48-score-box {
    padding: 4px 8px;
    min-width: 60px;
  }
  .g48-score-val {
    font-size: 17px;
  }
  .g48-hint {
    display: none;
  }
}

/* prefers-reduced-motion 対応 */
@media (prefers-reduced-motion: reduce) {
  .g48-tile,
  .g48-btn,
  .g48-overlay,
  .g48-score-add {
    animation: none !important;
    transition: none !important;
  }
}
