/* トランプカードゲーム - 神経衰弱 */

:root {
  --card-width: 80px;
  --card-height: 112px;
  --card-radius: 8px;
  --table-green: #0d5c2e;
  --table-green-dark: #0a4723;
  --card-face: #f5f0e6;
  --card-back: #1a4d6d;
  --accent: #e8c547;
  --text: #1a1a1a;
  --shadow: rgba(0, 0, 0, 0.35);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  height: 100vh;
  height: 100dvh; /* スマホのアドレスバー考慮 */
  min-height: -webkit-fill-available;
  overflow: hidden;
  font-family: "Hiragino Sans", "Hiragino Kaku Gothic ProN", "Yu Gothic", sans-serif;
  background: linear-gradient(160deg, var(--table-green-dark) 0%, var(--table-green) 50%, #0f6b38 100%);
  color: var(--text);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: max(12px, env(safe-area-inset-top, 0)) max(12px, env(safe-area-inset-right, 0)) max(12px, env(safe-area-inset-bottom, 0)) max(12px, env(safe-area-inset-left, 0));
}

.game-container {
  width: 100%;
  height: 100%;
  max-height: 100vh;
  max-height: 100dvh;
  max-width: 1200px;
  background: rgba(255, 255, 255, 0.06);
  border-radius: 16px;
  padding: 12px 16px;
  box-shadow: 0 12px 40px var(--shadow);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  transition: border-radius 0.2s ease, box-shadow 0.2s ease;
  isolation: isolate; /* 子の z-index を効かせる */
  -webkit-user-select: none;
  user-select: none; /* スマホでカードタップ時に文字選択しない */
}

/* 最大化時: ゲーム表示を画面全体に */
body.maximized {
  padding: 0;
}

body.maximized .game-container {
  position: fixed;
  inset: 0;
  inset: env(safe-area-inset-top, 0) env(safe-area-inset-right, 0) env(safe-area-inset-bottom, 0) env(safe-area-inset-left, 0);
  width: 100vw;
  height: 100vh;
  height: 100dvh;
  max-width: none;
  max-height: none;
  border-radius: 0;
  box-shadow: none;
}

/* 全画面表示時（ページ全体を全画面にしてボタンが使えるようにする） */
body.fullscreen-active .game-container {
  position: fixed;
  inset: 0;
  inset: env(safe-area-inset-top, 0) env(safe-area-inset-right, 0) env(safe-area-inset-bottom, 0) env(safe-area-inset-left, 0);
  width: 100vw;
  height: 100vh;
  height: 100dvh;
  max-width: none;
  max-height: none;
  border-radius: 0;
  box-shadow: none;
}

body.fullscreen-active {
  padding: 0;
}

/* 全画面表示中は「最大化」「通常表示に戻す」ボタンを非表示 */
body.fullscreen-active #btn-maximize {
  display: none !important;
}

.game-header {
  position: relative;
  z-index: 10;
  flex-shrink: 0;
  display: flex;
  flex-wrap: nowrap;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 12px;
  padding: 8px 12px;
  border-bottom: 2px solid rgba(255, 255, 255, 0.15);
  background: rgba(0, 0, 0, 0.25);
  pointer-events: auto;
}

.game-header .game-info,
.game-header .btn {
  pointer-events: auto;
}

.game-header h1 {
  margin: 0;
  flex-shrink: 0;
  font-size: 1.75rem;
  color: #fff;
  text-shadow: 0 1px 2px var(--shadow);
}

.game-info {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: nowrap;
  flex: 1 1 auto;
  min-width: 0;
  justify-content: flex-end;
}

.game-info span {
  color: rgba(255, 255, 255, 0.95);
  font-size: 1rem;
}

.game-info strong {
  color: var(--accent);
}

.btn {
  padding: 10px 20px;
  min-height: 44px; /* タッチターゲット推奨サイズ */
  font-size: 1rem;
  font-weight: 600;
  color: var(--table-green-dark);
  background: var(--accent);
  border: none;
  border-radius: 8px;
  cursor: pointer;
  transition: transform 0.15s, box-shadow 0.15s;
  touch-action: manipulation; /* ダブルタップ遅延を防ぐ */
  -webkit-tap-highlight-color: transparent;
}

.btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px var(--shadow);
}

.btn:active {
  transform: translateY(0);
}

.btn-secondary {
  background: rgba(255, 255, 255, 0.2);
  color: #fff;
  border: 1px solid rgba(255, 255, 255, 0.4);
}

.btn-secondary:hover {
  background: rgba(255, 255, 255, 0.3);
}

/* 52枚を1画面に収める。列・行数は JS の updateGridLayout() で横幅に合わせて変更 */
.game-board {
  position: relative;
  z-index: 0;
  flex: 1;
  min-height: 30vh; /* スマホで flex 計算が遅れても盤面が確実に表示される */
  display: grid;
  grid-template-columns: repeat(13, 1fr);
  grid-template-rows: repeat(4, 1fr);
  gap: 6px;
  align-content: center;
  justify-content: center;
  overflow: hidden;
  perspective: 1200px;
}

.game-board > * {
  min-width: 0;
  min-height: 0;
}

.card {
  width: 100%;
  height: 100%;
  min-width: 0;
  min-height: 0;
  border-radius: var(--card-radius);
  cursor: pointer;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  perspective: 1000px;
}

/* カードの見た目サイズに合わせる（アスペクト比 80:112）。フリップはここに適用 */
.card-inner {
  position: relative;
  width: 100%;
  height: 100%;
  max-width: 100%;
  max-height: 100%;
  min-width: 0;
  min-height: 0;
  aspect-ratio: 80 / 112;
  border-radius: var(--card-radius);
  transform-style: preserve-3d;
  transition: transform 0.35s ease;
  overflow: hidden;
}

.card.flipped {
  cursor: default;
  pointer-events: none;
}

/* ペア成立後：見えないが枠は残して位置を固定 */
.card.card-gone {
  opacity: 0;
  pointer-events: none;
}

/* ペア成立アニメーション用オーバーレイ */
.pair-anim-overlay {
  position: fixed;
  inset: 0;
  z-index: 1000;
  pointer-events: none;
}

/* ラッパー: 中央へ移動＋2回転＋300%拡大（中のカードは表のまま） */
.pair-anim-wrapper {
  position: fixed;
  transform-origin: 50% 50%;
  transition: transform 0.8s cubic-bezier(0.25, 0.46, 0.45, 0.94);
  overflow: visible;
  will-change: transform;
}

/* 2秒静止時だけ：2枚のカードの間・中心から放射状に光らせる */
.pair-glow-radial {
  position: fixed;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  width: 200vmin;
  height: 200vmin;
  border-radius: 50%;
  background: radial-gradient(
    circle at center,
    rgba(255, 252, 240, 0.95) 0%,
    rgba(255, 248, 220, 0.6) 12%,
    rgba(255, 236, 180, 0.35) 25%,
    rgba(255, 250, 240, 0.12) 40%,
    transparent 60%
  );
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.5s ease-out;
  z-index: 0;
}

.pair-anim-overlay .pair-anim-wrapper {
  z-index: 1;
}

.pair-anim-overlay.pair-glow .pair-glow-radial {
  opacity: 1;
}

.pair-anim-overlay.pair-anim-fadeout .pair-anim-wrapper {
  opacity: 0;
  transition: opacity 0.3s ease;
}

.card .face {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  border-radius: var(--card-radius);
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

/* 裏: 手前側（Z=0）、表: 奥側（180deg + translateZ でフリップ後に手前に） */
.card .face.back {
  background: transparent;
  transform: rotateY(0deg) translateZ(0);
  z-index: 0;
}

.card .face.back img,
.card .face.front img {
  width: 100%;
  height: 100%;
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
  object-position: center;
  display: block;
}

.card .face.front {
  background: transparent;
  transform: rotateY(180deg) translateZ(1px);
  z-index: 1;
}

/* フリップは .card-inner に適用 */
.card.revealed .card-inner {
  transform: rotateY(180deg);
}

/* revealed 時: 裏は非表示（backface で隠れる）。表は両面描画で確実に表示 */
.card.revealed .face.back {
  pointer-events: none;
}

.card.revealed .face.front {
  backface-visibility: visible;
  -webkit-backface-visibility: visible;
}

.message {
  flex-shrink: 0;
  margin-top: 8px;
  text-align: center;
  font-size: clamp(0.9rem, 2.5vw, 1.25rem);
  font-weight: 600;
  color: var(--accent);
  min-height: 1.5em;
  -webkit-user-select: text;
  user-select: text; /* クリアメッセージは選択可能 */
}

/* タブレット・スマホ */
@media (max-width: 600px) {
  body {
    padding: max(8px, env(safe-area-inset-top, 0)) max(8px, env(safe-area-inset-right, 0)) max(8px, env(safe-area-inset-bottom, 0)) max(8px, env(safe-area-inset-left, 0));
  }

  .game-container {
    padding: 8px 12px;
    border-radius: 12px;
  }

  .game-header {
    gap: 8px;
    margin-bottom: 8px;
    padding: 6px 10px;
  }

  .game-header h1 {
    font-size: 1.25rem;
  }

  .game-info {
    gap: 8px;
  }

  .game-info span {
    font-size: 0.9rem;
  }

  .btn {
    padding: 8px 14px;
    font-size: 0.9rem;
  }

  .game-board {
    gap: 4px;
  }

  .message {
    margin-top: 6px;
    font-size: 0.95rem;
  }
}

/* スマホ幅以下: 最大化ボタン非表示（JS の MOBILE_BREAKPOINT=1200 と合わせる。1080px幅の端末も含む） */
@media (max-width: 1200px) {
  #btn-maximize {
    display: none !important;
  }
}

/* スマホ縦持ち（狭い幅） */
@media (max-width: 400px) {
  .game-container {
    padding: 6px 8px;
  }

  .game-header {
    gap: 6px;
    padding: 5px 8px;
  }

  .game-header h1 {
    font-size: 1.1rem;
  }

  .game-info {
    gap: 6px;
  }

  .game-info span {
    font-size: 0.85rem;
  }

  .btn {
    padding: 8px 10px;
    font-size: 0.85rem;
    min-height: 40px;
  }

  .game-board {
    gap: 3px;
  }
}
