/* kokonot（ここのつ）- サイモン風の記憶ミニゲーム */

:root {
  --bg: #20212b;
  --panel: #2a2c3a;
  --tile: #343650;
  --text: #e8e6f0;
  --text-dim: #9a97ab;
  --accent: #d9b26f;
  --correct: #84a59d;
  --wrong: #c98d94;
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Hiragino Sans", "Yu Gothic", sans-serif;
  touch-action: manipulation;
  overscroll-behavior: none;
  overflow: hidden;
}

#app {
  display: flex;
  flex-direction: column;
  align-items: center;
  height: 100dvh;
  padding: calc(env(safe-area-inset-top, 0px) + 12px) 16px calc(env(safe-area-inset-bottom, 0px) + 12px);
}

/* ========================================
   ヘッダー・タイトル
   ======================================== */

#header {
  position: relative;
  width: 100%;
  display: flex;
  justify-content: center;
  flex: 0 0 auto;
}

#game-title {
  font-family: Georgia, "Times New Roman", "Hiragino Mincho ProN", serif;
  font-size: 20px;
  font-weight: 400;
  letter-spacing: 0.4em;
  text-indent: 0.4em; /* letter-spacing 分の中央ずれ補正 */
  color: var(--text-dim);
  margin: 8px 0 0;
}

#giveup-button {
  position: fixed;
  top: calc(env(safe-area-inset-top, 0px) + 12px);
  right: 16px;
  padding: 6px 12px;
  font-size: 11px;
  letter-spacing: 0.1em;
  border: none;
  border-radius: 6px;
  background: none;
  color: var(--text-dim);
  opacity: 0.6;
  cursor: pointer;
  /* 改訂1: #pop-overlay（z-index 40）より上に置き、Clear!/Oops... 表示中も押せるようにする */
  z-index: 50;
}

/* ========================================
   スコア表示（stage / best）
   ======================================== */

#score-display {
  flex: 0 0 auto;
  display: flex;
  gap: 32px;
  margin: 10px 0 4px;
}

.score-block {
  text-align: center;
}

.score-label {
  font-size: 12px;
  letter-spacing: 0.1em;
  color: var(--text-dim);
  text-transform: uppercase;
}

#stage-value,
#best-value {
  font-size: 24px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

#best-value {
  color: var(--accent);
}

/* ========================================
   盤面
   ======================================== */

/* 改訂3: hint（直上）・盤面・結果パネル（直下）を縦に並べて中央に置く。
   hint と結果パネルは常に高さを確保し、表示切替でレイアウトが動かないようにする */
#board-wrap {
  flex: 1 1 auto;
  width: 100%;
  min-height: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  position: relative;
}

/* 改訂3: show（なぞり）中だけ盤面の直上に小さく出す（game.js の updateResultPanel が制御） */
#hint-text {
  flex: 0 0 auto;
  height: 20px;
  line-height: 20px;
  margin-bottom: 8px;
  font-size: 13px;
  letter-spacing: 0.15em;
  color: var(--text-dim);
  visibility: hidden;
}

#hint-text.show {
  visibility: visible;
}

#board {
  /* 40px = padding 10px×2 + gap 10px×2（1マス分の内訳）。tile-number の font-size 計算もこの内訳を前提にしている */
  --board-size: min(88vw, 58dvh, 380px);
  position: relative;
  width: var(--board-size);
  height: var(--board-size);
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  gap: 10px;
  background: var(--panel);
  border-radius: 16px;
  padding: 10px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.35);
  /* 改訂1: Clear!/Oops... 表示中は盤面をぼかす（game.js が #board に blurred を付与） */
  transition: filter 0.15s ease;
}

/* 改訂4: PC 用。タップが意味を持つ状態（show / recall。game.js の updateResultPanel が #board に tappable を付与）で、
   まだ押していないパネルの上だけカーソルを pointer にする。done / revealed 済みは押しても揺れるだけなので default のまま */
.board.tappable .tile:not(.done):not(.revealed) {
  cursor: pointer;
}

.board.blurred {
  filter: blur(6px);
}

.tile {
  position: relative;
  overflow: hidden;
  border-radius: 12px;
  /* 改訂4: 数字はテキストなので、放っておくと I 字カーソルになり文字選択もできてしまう。
     常に default にして選択不可にし、押せるパネルだけ .board.tappable の規則で pointer にする */
  cursor: default;
  user-select: none;
  -webkit-user-select: none;
  background: var(--tile);
  box-shadow: none;
  /* clear-glow の発光（CONFIG.clearGlowMs=300 と同期）、.tile.revealed への opacity 変化（CONFIG.revealMs=200 と同期） */
  transition: box-shadow 0.3s ease, background 0.2s ease, opacity 0.2s ease; /* background 0.2s: なぞり周回リセット時の done 解除（CONFIG.revealMs と同期） */
}

.tile-number,
.tile-mask {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  /* 数字と ? はパネル高さの 55%（1マス=(--board-size - 40px)/3）。改訂1で 65% にしたが大きすぎたので改訂3で 55% に */
  font-size: calc((var(--board-size) - 40px) / 3 * 0.55);
}

.tile-number {
  color: var(--text);
  opacity: 1;
  /* masked への切り替え（「?」へのクロスフェード。CONFIG.maskMs=150 と同期）。
     .revealed / .oops-reveal がより長い transition-duration で上書きする */
  transition: opacity 0.15s ease;
}

/* 新ステージ突入時の数字フェードイン（CONFIG.enterMs=150 と同期。buildBoard が #board に entering を付ける） */
.board.entering .tile-number {
  animation: number-enter 0.15s ease;
}

@keyframes number-enter {
  from { opacity: 0; }
  to { opacity: 1; }
}

.tile-mask {
  color: var(--text-dim);
  opacity: 0;
  /* masked への切り替え（CONFIG.maskMs=150 と同期） */
  transition: opacity 0.15s ease;
}

/* hiding の終盤〜recall: 1枚ずつ「?」へクロスフェード（game.js が transition-delay を inline で付与する） */
.tile.masked .tile-number {
  opacity: 0;
}

.tile.masked .tile-mask {
  opacity: 1;
}

/* recall で正タップしたパネル: ?→数字へクロスフェード（CONFIG.revealMs=200 と同期）
   .tile.masked より後ろに置いて同specificityのタイブレークで勝たせる */
.tile.revealed .tile-number {
  opacity: 1;
  transition: opacity 0.2s ease;
  transition-delay: 0s;
}

.tile.revealed .tile-mask {
  opacity: 0;
  transition: opacity 0.2s ease;
  transition-delay: 0s;
}

/* oops: 全パネルの数字を見せて正解配置を明かす（CONFIG.revealMs=200 と同期）
   .tile.revealed より後ろに置いて優先させる */
.tile.oops-reveal .tile-number {
  opacity: 1;
  transition: opacity 0.2s ease;
  transition-delay: 0s;
}

.tile.oops-reveal .tile-mask {
  opacity: 0;
  transition: opacity 0.2s ease;
  transition-delay: 0s;
}

/* 改訂1: show段階のdoneパネルは数字が見えなくなるくらい暗くする（recallの revealed は対象外）。
   :not(.oops-reveal) はレビュー修正: .tile.oops-reveal .tile-number { opacity: 1 } は詳細度3クラス、
   こちらは元々 :not(.revealed) を含め詳細度4クラスで常に上回ってしまうため、oops-reveal時は
   このルール自体を不成立にして oops-reveal 側を確実に勝たせる（改訂2で show の誤タップは
   oops にならなくなったため実際には起きない組み合わせだが、堅牢性のため残す） */
.tile.done:not(.revealed):not(.oops-reveal) {
  background: #181923;
}

.tile.done:not(.revealed):not(.oops-reveal) .tile-number {
  opacity: 0.15;
}

/* recall で正タップ済みのパネル: 思い出した数字は読める薄さで残す（改訂前と同じ扱い） */
.tile.revealed {
  opacity: 0.55;
}

/* 誤タップしたパネル: 赤く揺れる（CONFIG.shakeMs=400 と同期） */
.tile.wrong {
  background: rgba(201, 141, 148, 0.35);
  animation: tile-shake 0.4s ease;
}

@keyframes tile-shake {
  0%, 100% { transform: translateX(0); }
  20%      { transform: translateX(-6px); }
  40%      { transform: translateX(5px); }
  60%      { transform: translateX(-3px); }
  80%      { transform: translateX(2px); }
}

/* done済みパネルへの再タップ: 小さな揺れのみ（CONFIG.nudgeMs=150 と同期） */
.tile.nudge {
  animation: tile-nudge 0.15s ease;
}

@keyframes tile-nudge {
  0%   { transform: rotate(0deg); }
  25%  { transform: rotate(-3deg); }
  50%  { transform: rotate(0deg); }
  75%  { transform: rotate(3deg); }
  100% { transform: rotate(0deg); }
}

/* clear成功時の一斉発光（CONFIG.clearGlowMs=300 と同期） */
.board.clear-glow .tile {
  box-shadow: 0 0 14px rgba(132, 165, 157, 0.85);
}

/* ========================================
   Clear! / Oops... ポップオーバーレイ
   ======================================== */

/* 改訂1: ビューポート全体を覆うオーバーレイ。pop-active の間だけクリックを受け取り、
   どこをクリックしても解除できるようにする（#giveup-button だけ z-index で上に出す） */
#pop-overlay {
  position: fixed;
  inset: 0;
  z-index: 40;
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: none;
  font-size: clamp(32px, 11vw, 56px);
  font-weight: 800;
  letter-spacing: 0.02em;
  opacity: 0;
  transform: scale(0.3);
}

#pop-overlay.pop-active {
  pointer-events: auto;
  cursor: pointer;
}

#pop-overlay.pop-clear {
  color: var(--correct);
  text-shadow: 0 2px 12px rgba(132, 165, 157, 0.5);
}

#pop-overlay.pop-oops {
  color: var(--wrong);
  text-shadow: 0 2px 12px rgba(201, 141, 148, 0.5);
}

/* ポップイン（CONFIG.popMs=450 と同期） */
@keyframes pop-in {
  0%   { transform: scale(0.3); opacity: 0; }
  60%  { transform: scale(1.15); opacity: 1; }
  100% { transform: scale(1); opacity: 1; }
}

#pop-overlay.pop-show {
  animation: pop-in 0.45s ease forwards;
}

/* ポップアウト（CONFIG.popFadeMs=150 と同期） */
#pop-overlay.pop-fade {
  transition: opacity 0.15s ease;
  /* opacity 自体は game.js の dismissPop が inline で 0 にする（クラスの opacity は inline に負けるため） */
}

/* ========================================
   結果パネル（改訂2: show=Try待ち / cleared=Next待ち / over=Game Over + Restart待ち）
   盤面は隠さない。常時高さを確保する（visibility: hidden でも高さは保持される）
   ======================================== */

#result-panel {
  flex: 0 0 auto;
  display: flex;
  flex-wrap: wrap; /* 文言が伸びて 375px 幅に収まらなくなっても画面外に切れないための安全弁 */
  justify-content: center;
  align-items: center;
  gap: 20px;
  min-height: 56px;
  margin-top: 10px; /* 改訂3: 盤面の直下に置く（#board-wrap 内） */
  visibility: hidden;
}

#result-panel.show {
  visibility: visible;
}

#result-text {
  font-size: 20px;
  font-weight: 700;
  letter-spacing: 0.05em;
  color: var(--wrong);
}

#try-button,
#next-button,
#restart-button {
  padding: 10px 28px;
  font-size: 16px;
  border: none;
  border-radius: 999px;
  background: var(--accent);
  color: #2a2410;
  font-weight: 600;
  cursor: pointer;
}

#try-button:active,
#next-button:active,
#restart-button:active {
  transform: scale(0.97);
}
