/* ===== リセット・基本設定 ===== */
*, *::before, *::after {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

html, body {
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: #000;
  font-family: 'Hiragino Kaku Gothic ProN', 'Hiragino Sans', 'Yu Gothic UI', 'Meiryo', sans-serif;
  -webkit-text-size-adjust: 100%;
  user-select: none;
  -webkit-user-select: none;
}

/* ===== ゲームコンテナ ===== */
#game-container {
  position: relative;
  width: 100%;
  height: 100%;
  max-width: 100vw;
  max-height: 100vh;
  margin: 0 auto;
  overflow: hidden;
  background: #000;
}

/* デスクトップ時: 16:9 中央配置 */
@media (min-width: 769px) {
  #game-container {
    max-width: min(100vw, calc(100vh * 16 / 9));
    max-height: min(100vh, calc(100vw * 9 / 16));
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
  }
}

/* ===== 背景レイヤー ===== */
#bg-layer {
  position: absolute;
  top: 0; left: 0;
  width: 100%;
  height: 100%;
  z-index: 1;
}

#bg-layer img {
  position: absolute;
  top: 0; left: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

/* ===== キャラクターレイヤー ===== */
#char-layer {
  position: absolute;
  top: 0; left: 0;
  width: 100%;
  height: 100%;
  z-index: 2;
  pointer-events: none;
}

.char-img {
  position: absolute;
  bottom: 0;
  height: 85%;
  max-width: 55%;
  object-fit: contain;
  object-position: bottom center;
  transition: none;
}

#char-left, #char-left-next {
  left: 0;
  object-position: bottom left;
}

#char-center, #char-center-next {
  left: 50%;
  transform: translateX(-50%);
  object-position: bottom center;
}

#char-right, #char-right-next {
  right: 0;
  object-position: bottom right;
}

/* ===== タイトルオーバーレイ ===== */
#title-overlay {
  position: absolute;
  top: 0; left: 0;
  width: 100%;
  height: 100%;
  z-index: 50;
  cursor: pointer;
  background: #000;
}

#title-overlay img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

#title-click-area {
  position: absolute;
  top: 0; left: 0;
  width: 100%;
  height: 100%;
}

/* ===== メッセージウィンドウ ===== */
#message-area {
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 25%;
  z-index: 10;
  cursor: pointer;
}

/* クリック可能エリア - 画面全体を覆う透明レイヤー */
.clickable-area::before {
  content: '';
  position: fixed;
  top: 0; left: 0;
  width: 100vw;
  height: 100vh;
  z-index: -1;
}

#msg-window {
  position: relative;
  width: 100%;
  height: 100%;
  padding: 0;
}

#msg-wnd-bg {
  width: 100%;
  height: 100%;
  display: block;
  object-fit: fill;
}

#msg-content {
  position: absolute;
  top: 10%;
  left: 4%;
  width: 90%;
  height: 76%;
  color: #fff;
  overflow: hidden;
}

#msg-name {
  font-size: clamp(18px, 2.8vw, 26px);
  font-weight: bold;
  color: #ffd700;
  margin-bottom: 0.2em;
  text-shadow: 1px 1px 3px rgba(0,0,0,0.8);
}

#msg-text {
  font-size: clamp(16px, 2.5vw, 26px);
  line-height: 1.6;
  color: #fff;
  text-shadow: 1px 1px 3px rgba(0,0,0,0.8);
  white-space: pre-wrap;
  word-wrap: break-word;
}

#msg-indicator {
  position: absolute;
  bottom: 8%;
  right: 4%;
  color: rgba(255,255,255,0.7);
  font-size: clamp(14px, 2vw, 26px);
  animation: blink 1s infinite;
}

@keyframes blink {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.3; }
}

/* ===== 選択肢 ===== */
#select-overlay {
  position: absolute;
  top: 0; left: 0;
  width: 100%;
  height: 100%;
  z-index: 30;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0,0,0,0.3);
}

#select-list {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  width: 80%;
  max-width: 500px;
}

.select-btn {
  position: relative;
  width: 100%;
  border: none;
  background: none;
  cursor: pointer;
  padding: 0;
  transition: transform 0.15s ease, filter 0.15s ease;
}

.select-btn:hover {
  transform: scale(1.03);
  filter: brightness(1.15);
}

.select-btn:active {
  transform: scale(0.97);
}

.select-btn img {
  width: 100%;
  display: block;
}

.select-btn span {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  color: #fff;
  font-size: clamp(14px, 2.5vw, 26px);
  font-weight: bold;
  text-shadow: 1px 1px 4px rgba(0,0,0,0.8);
  white-space: nowrap;
  pointer-events: none;
}

/* ===== 設定ボタン（歯車） ===== */
#settings-btn {
  position: absolute;
  top: 8px;
  left: 8px;
  z-index: 20;
  background: none;
  border: none;
  cursor: pointer;
  width: clamp(36px, 6vw, 52px);
  height: clamp(36px, 6vw, 52px);
  padding: 0;
  opacity: 0.85;
  transition: opacity 0.2s, transform 0.2s;
}

#settings-btn:hover {
  opacity: 1;
  transform: scale(1.1);
}

#settings-btn img {
  width: 100%;
  height: 100%;
}

/* ===== 設定パネル ===== */
#settings-panel {
  position: absolute;
  top: 0; left: 0;
  width: 100%;
  height: 100%;
  z-index: 40;
  background: rgba(0,0,0,0.6);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
}

#settings-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  width: 85%;
  max-width: 460px;
}

.settings-item {
  background: none;
  border: none;
  cursor: pointer;
  padding: 0;
  transition: transform 0.15s ease, filter 0.15s ease;
}

.settings-item:hover {
  transform: scale(1.05);
  filter: brightness(1.1);
}

.settings-item:active {
  transform: scale(0.95);
}

.settings-item img {
  width: 100%;
  display: block;
  border-radius: 6px;
}

#settings-close {
  margin-top: 16px;
  padding: 10px 36px;
  font-size: clamp(14px, 2.2vw, 18px);
  color: #fff;
  background: rgba(255,255,255,0.15);
  border: 1px solid rgba(255,255,255,0.4);
  border-radius: 8px;
  cursor: pointer;
  transition: background 0.2s;
}

#settings-close:hover {
  background: rgba(255,255,255,0.3);
}

/* ===== セーブ/ロード画面 ===== */
#save-screen, #load-screen {
  position: absolute;
  top: 0; left: 0;
  width: 100%;
  height: 100%;
  z-index: 42;
  background: rgba(0,0,0,0.85);
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 20px;
  overflow-y: auto;
}

.screen-title {
  color: #fff;
  font-size: clamp(18px, 3vw, 28px);
  margin-bottom: 16px;
}

.slot-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  width: 90%;
  max-width: 480px;
  flex: 1;
  overflow-y: auto;
  padding-bottom: 10px;
}

.slot-item {
  position: relative;
  border: none;
  cursor: pointer;
  padding: 12px 16px;
  border-radius: 8px;
  color: #fff;
  font-size: clamp(12px, 2vw, 16px);
  text-align: left;
  transition: filter 0.15s, transform 0.1s;
  background-size: cover;
  background-position: center;
  min-height: 60px;
}

.slot-item.save-slot {
  background-color: #a0b800;
}

.slot-item.load-slot {
  background-color: #e08860;
}

.slot-item:hover {
  filter: brightness(1.15);
  transform: scale(1.02);
}

.slot-item .slot-label {
  font-weight: bold;
  font-size: clamp(13px, 2.2vw, 17px);
}

.slot-item .slot-info {
  font-size: clamp(11px, 1.8vw, 14px);
  opacity: 0.85;
  margin-top: 4px;
}

.screen-back-btn {
  margin-top: 12px;
  padding: 10px 36px;
  font-size: clamp(14px, 2.2vw, 18px);
  color: #fff;
  background: rgba(255,255,255,0.15);
  border: 1px solid rgba(255,255,255,0.4);
  border-radius: 8px;
  cursor: pointer;
  transition: background 0.2s;
  flex-shrink: 0;
}

.screen-back-btn:hover {
  background: rgba(255,255,255,0.3);
}

/* ===== バックログ画面 ===== */
#backlog-screen {
  position: absolute;
  top: 0; left: 0;
  width: 100%;
  height: 100%;
  z-index: 42;
  background: rgba(0,0,0,0.9);
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 20px;
}

#backlog-content {
  flex: 1;
  width: 90%;
  max-width: 600px;
  overflow-y: auto;
  color: #fff;
  font-size: clamp(13px, 2vw, 16px);
  line-height: 1.8;
  padding: 10px;
  -webkit-overflow-scrolling: touch;
}

.backlog-entry {
  margin-bottom: 12px;
  padding-bottom: 8px;
  border-bottom: 1px solid rgba(255,255,255,0.15);
}

.backlog-name {
  color: #ffd700;
  font-weight: bold;
  margin-bottom: 2px;
}

.backlog-text {
  color: #e0e0e0;
}

/* ===== デバッグパネル ===== */
#debug-panel {
  position: absolute;
  top: 50px;
  right: 8px;
  z-index: 35;
  background: rgba(0,0,0,0.75);
  color: #0f0;
  font-family: 'Consolas', 'Courier New', monospace;
  font-size: clamp(9px, 1.4vw, 12px);
  padding: 8px 12px;
  border-radius: 6px;
  max-width: 280px;
  max-height: 60%;
  overflow-y: auto;
  pointer-events: none;
}

#debug-info {
  white-space: pre-wrap;
  word-break: break-all;
}

/* ===== シェイクアニメーション ===== */
@keyframes shake-instant {
  0%, 100% { transform: translate(0, 0); }
  10% { transform: translate(-6px, 4px); }
  30% { transform: translate(5px, -3px); }
  50% { transform: translate(-4px, 2px); }
  70% { transform: translate(3px, -4px); }
  90% { transform: translate(-2px, 1px); }
}

@keyframes shake-normal {
  0%, 100% { transform: translate(0, 0); }
  5% { transform: translate(-5px, 3px); }
  10% { transform: translate(4px, -4px); }
  15% { transform: translate(-3px, 2px); }
  20% { transform: translate(5px, -2px); }
  25% { transform: translate(-4px, 4px); }
  30% { transform: translate(3px, -3px); }
  35% { transform: translate(-2px, 1px); }
  40% { transform: translate(4px, -4px); }
  45% { transform: translate(-5px, 3px); }
  50% { transform: translate(3px, -2px); }
  55% { transform: translate(-3px, 4px); }
  60% { transform: translate(5px, -3px); }
  65% { transform: translate(-4px, 2px); }
  70% { transform: translate(3px, -4px); }
  75% { transform: translate(-2px, 3px); }
  80% { transform: translate(4px, -2px); }
  85% { transform: translate(-3px, 1px); }
  90% { transform: translate(2px, -3px); }
  95% { transform: translate(-1px, 2px); }
}

.shaking-instant {
  animation: shake-instant 0.3s ease-in-out;
}

.shaking-normal {
  animation: shake-normal 1.5s ease-in-out;
}

.shaking-long {
  animation: shake-normal 3.5s ease-in-out;
}

/* キャラシェイク用（transformを保持する必要があるもの） */
#char-center.shaking-instant,
#char-center.shaking-normal,
#char-center.shaking-long {
  /* center は translateX(-50%) を持つため、別アニメーションが必要 */
}

/* ===== トランジション用クラス ===== */
.transition-fade {
  transition: opacity 0.8s ease-in-out !important;
}

.transition-wipe {
  /* clip-path で実現 */
  transition: clip-path 0.8s ease-in-out !important;
}

/* ===== メッセージ非表示時 ===== */
#message-area.hidden {
  display: none;
}

/* ===== レスポンシブ: 横向き統一レイアウト ===== */
/* スマートフォンは横向き(ランドスケープ)のみ対応 */

/* 縦向きスマートフォンの場合: 回転を促す */
@media (max-width: 768px) and (orientation: portrait) {
  #game-container::after {
    content: 'スマートフォンを横向きにしてください';
    position: absolute;
    top: 0; left: 0;
    width: 100%;
    height: 100%;
    z-index: 999;
    background: #000;
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 18px;
    text-align: center;
    padding: 20px;
  }
}

/* 横向きスマートフォン */
@media (max-width: 768px) and (orientation: landscape) {
  #game-container {
    width: 100vw;
    height: 100vh;
    max-width: 100vw;
    max-height: 100vh;
  }

  #settings-grid {
    width: 92%;
    gap: 8px;
  }

  #settings-btn {
    top: 4px;
    left: 4px;
  }
}

/* ===== ファイル選択オーバーレイ（file://フォールバック） ===== */
#file-picker-overlay {
  position: absolute;
  top: 0; left: 0;
  width: 100%;
  height: 100%;
  z-index: 60;
  background: rgba(0,0,0,0.92);
  display: flex;
  align-items: center;
  justify-content: center;
}

#file-picker-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
  padding: 32px 28px;
  max-width: 420px;
  width: 88%;
  background: rgba(255,255,255,0.07);
  border: 1px solid rgba(255,255,255,0.2);
  border-radius: 14px;
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
}

#file-picker-title {
  color: #fff;
  font-size: clamp(17px, 3vw, 24px);
  font-weight: bold;
  text-align: center;
}

#file-picker-desc {
  color: rgba(255,255,255,0.7);
  font-size: clamp(12px, 2vw, 15px);
  text-align: center;
  line-height: 1.6;
}

#file-picker-drop {
  width: 100%;
  min-height: 100px;
  border: 2px dashed rgba(255,255,255,0.3);
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: border-color 0.2s, background 0.2s;
  cursor: pointer;
}

#file-picker-drop.dragover {
  border-color: #ffd700;
  background: rgba(255,215,0,0.08);
}

#file-picker-drop-label {
  color: rgba(255,255,255,0.45);
  font-size: clamp(13px, 2.2vw, 16px);
  pointer-events: none;
}

#file-picker-btn {
  display: inline-block;
  padding: 10px 32px;
  font-size: clamp(14px, 2.2vw, 17px);
  color: #fff;
  background: rgba(255,255,255,0.15);
  border: 1px solid rgba(255,255,255,0.4);
  border-radius: 8px;
  cursor: pointer;
  transition: background 0.2s;
  text-align: center;
}

#file-picker-btn:hover {
  background: rgba(255,255,255,0.28);
}

/* ===== ローディングインジケータ ===== */
.loading-overlay {
  position: absolute;
  top: 0; left: 0;
  width: 100%;
  height: 100%;
  z-index: 100;
  background: #000;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  font-size: 18px;
}

.loading-overlay .spinner {
  width: 40px;
  height: 40px;
  border: 4px solid rgba(255,255,255,0.2);
  border-top-color: #fff;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  margin-right: 12px;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}
