* { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  touch-action: manipulation; /* iOSでのダブルタップ拡大を防ぐ */
}

/* スクロールバー: 背景に馴染む細くて目立たないデザインに */
* {
  scrollbar-width: thin;
  scrollbar-color: #3d5530 transparent;
}
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: #3d5530; border-radius: 4px; }
::-webkit-scrollbar-thumb:hover { background: #4a6030; }

body {
  font-family: 'Hiragino Kaku Gothic ProN', 'メイリオ', sans-serif;
  background: #1b2714;
  color: #dde8c0;
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  overflow-x: hidden;
}
/* 背景パターン: タイル継ぎ目のズレ(Safariでの線)を避けるため整数pxサイズ・ラスタ画像(PNG)を使用。
   background-position ではなく transform で動かし、GPU合成でカクつきを防ぐ。
   要素はビューポートよりタイル1枚分大きく取り、はみ出す分をあらかじめ確保しておく。 */
body::before {
  content: '';
  position: fixed;
  top: -100px; left: -100px; right: -100px; bottom: -100px;
  z-index: -1;
  background-image: url('bg_pattern.png');
  background-repeat: repeat;
  background-size: 100px 66px;
  opacity: 0.1;
  pointer-events: none;
  will-change: transform;
  -webkit-backface-visibility: hidden;
  backface-visibility: hidden;
  animation: bgPatternDrift 60s linear infinite;
}
@keyframes bgPatternDrift {
  from { transform: translate3d(0, 0, 0); }
  to { transform: translate3d(100px, 66px, 0); }
}

.hidden { display: none !important; }

/* 音声コントロール（右上固定） */
#audioControls {
  position: fixed;
  top: 12px;
  right: 12px;
  display: flex;
  gap: 6px;
  z-index: 200;
}
#audioControls button {
  width: 36px;
  height: 36px;
  border-radius: 8px;
  border: 2px solid #5a7a2a;
  background: #3d5530;
  color: #e8d5a0;
  font-size: 18px;
  cursor: pointer;
  padding: 0;
  line-height: 1;
  transition: background 0.15s, opacity 0.15s;
}
#audioControls button:hover { background: #5a7a2a; }
#audioControls button.off {
  opacity: 0.35;
  background: #222;
  border-color: #444;
}

/* 左上のホームロゴ（タイトル以外の画面で表示、押すとタイトルに戻る） */
#homeLogo {
  position: fixed;
  top: 12px;
  left: 12px;
  width: 100px;
  height: auto;
  z-index: 200;
  cursor: pointer;
  transition: opacity 0.15s;
}
#homeLogo:hover { opacity: 0.8; }

/* ロビー */
#lobby {
  text-align: center;
  padding: 40px;
}
#logo { max-width: 800px; width: 100%; margin-bottom: 24px; display: block; margin-inline: auto; }
#lobbyControls { max-width: 320px; margin-inline: auto; }
#lobby button {
  display: block;
  width: 100%;
  padding: 12px 24px;
  margin: 8px 0;
  background: #7a9e40;
  color: #fff;
  border: none;
  border-radius: 8px;
  font-size: 1rem;
  cursor: pointer;
  transition: background 0.2s;
}
#lobby button:hover { background: #5a7a2a; }
#cpuDifficultyRow {
  display: flex;
  gap: 8px;
  margin: 8px 0;
}
#cpuDifficultyRow select {
  flex: 1;
  padding: 12px;
  border-radius: 8px;
  border: 1px solid #4a6030;
  background: #263318;
  color: #dde8c0;
  font-size: 1rem;
}
#cpuDifficultyRow button {
  display: block;
  width: auto;
  margin: 0;
  padding: 12px 20px;
  background: #7a9e40;
  color: #fff;
  border: none;
  border-radius: 8px;
  font-size: 1rem;
  cursor: pointer;
  transition: background 0.2s;
  white-space: nowrap;
}
#cpuDifficultyRow button:hover { background: #5a7a2a; }
#lobbyMsg { margin-top: 16px; color: #8fa870; }

/* ゲーム画面 */
#gameArea {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  padding: 16px;
}

#gameContent {
  display: flex;
  gap: 16px;
  align-items: flex-start;
}

#gameLeft {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
}
#roomInfo { font-size: 0.85rem; color: #8fa870; }
#statusBar {
  font-size: 1rem;
  color: #dde8c0;
  font-weight: bold;
  min-height: 24px;
}
#statusBar.phase-label {
  font-size: 1.4rem;
  font-family: 'Noto Serif JP', 'Yu Mincho', 'Hiragino Mincho ProN', serif;
}

/* 検証モード（プレイ中の操作パネル） */
#verifyPlayControls {
  display: flex;
  gap: 8px;
}
#verifyPlayControls button, #forcePlaceSideRow button {
  padding: 4px 12px;
  border-radius: 8px;
  border: 2px solid #5a7a2a;
  background: #3d5530;
  color: #dde8c0;
  font-size: 0.8rem;
  font-weight: bold;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s, color 0.15s;
}
#verifyPlayControls button:hover { background: #5a7a2a; }
#btnForcePlaceToggle.active {
  background: #a8471f;
  border-color: #e8925a;
  color: #ffe6c8;
}
#forcePlacePanel {
  background: #263318;
  border-radius: 10px;
  padding: 10px 14px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  max-width: 398px;
  margin: 0 auto 8px;
}
#forcePlaceSideRow { display: flex; gap: 8px; }
.side-toggle {
  padding: 4px 12px;
  border-radius: 8px;
  border: 2px solid #4a6030;
  background: #1b2714;
  color: #8fa870;
  font-size: 0.78rem;
  font-weight: bold;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s, color 0.15s;
}
.side-toggle:hover { border-color: #5a7a2a; }
.side-toggle.active {
  background: #a8471f !important;
  border-color: #e8925a !important;
  color: #ffe6c8 !important;
}
#forcePlacePiecesList { display: flex; gap: 8px; flex-wrap: wrap; justify-content: center; }

#verifySetupSideRow {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 4px;
}

#mainLayout {
  display: flex;
  gap: 16px;
  align-items: center;
}

#board {
  display: block;
  border: 3px solid #8b6914;
  border-radius: 4px;
  cursor: pointer;
  image-rendering: pixelated;
  max-width: 100%;
  height: auto;
}

.hand-panel {
  width: 120px;
  background: #263318;
  border-radius: 8px;
  padding: 8px;
  min-height: 200px;
}
.hand-label {
  font-size: 0.75rem;
  color: #8fa870;
  margin-bottom: 8px;
  text-align: center;
}
.hand-pieces { display: flex; flex-wrap: wrap; gap: 6px; justify-content: center; }
.hand-piece {
  width: 50px;
  height: 56px;
  background: transparent;
  border: 2px solid #8b6914;
  border-radius: 4px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  font-size: 0.75rem;
  color: #1a1a1a;
  transition: border-color 0.15s, box-shadow 0.15s;
  user-select: none;
  overflow: hidden;
}
.hand-piece canvas { display: block; }
.hand-piece.mine:hover, .hand-piece.mine.selected { border-color: #7a9e40; box-shadow: 0 0 6px #7a9e40; }
.hand-piece.opponent { cursor: default; }
.hand-piece .count { font-size: 0.6rem; color: #8fa870; margin-top: 1px; }
.hand-piece.disabled {
  opacity: 0.35;
  cursor: default !important;
  pointer-events: none;
}

/* 準備フェーズパネル */
#setupPanel {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  margin-top: 12px;
  padding: 14px 20px;
  background: #263318;
  border-radius: 10px;
  min-width: 300px;
}
.setup-label { font-size: 0.82rem; color: #8fa870; text-align: center; }
#deckPanel {
  display: flex;
  flex-direction: column;
  gap: 6px;
  width: 100%;
  max-width: 398px;
  padding-bottom: 10px;
  margin-bottom: 4px;
  border-bottom: 1px solid #3d5530;
}
#deckSlotRow {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  justify-content: center;
}
.deck-slot {
  width: 34px;
  height: 34px;
  padding: 0;
  background: #263318;
  color: #7a8f60;
  border: 1px solid #4a6030;
  border-radius: 6px;
  font-size: 0.9rem;
  cursor: pointer;
  transition: background 0.15s, color 0.15s, border-color 0.15s;
}
.deck-slot:hover { background: #3d5530; }
.deck-slot.filled { color: #dde8c0; border-color: #5a7a2a; }
.deck-slot.active { background: #5a7a2a; color: #fff; border-color: #7a9e40; }
#btnSaveDeck, #btnDeleteDeck {
  padding: 6px 12px;
  background: #3d5530;
  color: #dde8c0;
  border: 1px solid #5a7a2a;
  border-radius: 6px;
  font-size: 0.78rem;
  cursor: pointer;
  white-space: nowrap;
  transition: background 0.15s;
}
#btnSaveDeck:hover, #btnDeleteDeck:hover { background: #4a6030; }
#btnSaveDeck { background: #7a9e40; border-color: #7a9e40; color: #fff; }
#btnSaveDeck:hover { background: #5a7a2a; }
#setupPiecesList { display: flex; gap: 8px; flex-wrap: wrap; justify-content: center; max-width: 398px; }
#setupStatus { font-size: 0.9rem; color: #dde8c0; min-height: 1.2em; }
#btnReady {
  padding: 10px 28px;
  background: #7a9e40;
  color: #fff;
  border: none;
  border-radius: 8px;
  font-size: 1rem;
  cursor: pointer;
  transition: background 0.2s;
}
#btnReady:hover { background: #5a7a2a; }

/* 駒情報パネル */
#pieceInfo {
  width: 160px;
  background: #263318;
  border-radius: 10px;
  padding: 12px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  height: 450px;
  overflow-y: auto;
}
.info-empty {
  color: #4a6030;
  font-size: 0.78rem;
  text-align: center;
  margin-top: 20px;
  line-height: 1.6;
}
#infoContent {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  width: 100%;
}
#infoPieceName {
  font-size: 1.1rem;
  font-weight: bold;
  color: #dde8c0;
  text-align: center;
}
#infoCanvas, #infoPromotedCanvas {
  border: 2px solid #4a6030;
  border-radius: 3px;
}
#infoPieceDesc {
  font-size: 0.73rem;
  color: #8fa870;
  line-height: 1.5;
  text-align: left;
  width: 100%;
}
#infoTimerSection {
  font-size: 0.78rem;
  color: #d4a843;
  font-weight: bold;
  text-align: center;
  width: 100%;
  border-top: 1px solid #4a6030;
  padding-top: 6px;
}
.info-promoted-label {
  font-size: 0.72rem;
  color: #990000;
  font-weight: bold;
  text-align: center;
  width: 100%;
  border-top: 1px solid #4a6030;
  padding-top: 6px;
}
#infoPromotedName {
  font-size: 1rem;
  font-weight: bold;
  color: #cc2222;
  text-align: center;
}
#infoPromotedSection {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  width: 100%;
}

/* ダイアログ */
#promoteDialog {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}

/* CPU戦の難易度選択画面 */
#cpuSetupScreen {
  text-align: center;
  padding: 40px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
}

.dialog-box {
  background: #263318;
  border: 2px solid #7a9e40;
  border-radius: 12px;
  padding: 32px;
  text-align: center;
  min-width: 240px;
}
.dialog-box p { font-size: 1.2rem; margin-bottom: 16px; }
.dialog-box button {
  margin: 4px 8px;
  padding: 10px 20px;
  border: none;
  border-radius: 8px;
  font-size: 1rem;
  cursor: pointer;
  background: #7a9e40;
  color: #fff;
}
.dialog-box button:hover { background: #5a7a2a; }
#btnNoPromote { background: #3d5530; }
#btnNoPromote:hover { background: #4a6030; }

/* ルームロビー */
#roomLobby {
  text-align: center;
  padding: 32px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 18px;
}
.lobby-share-label {
  font-size: 0.85rem;
  color: #8fa870;
  white-space: nowrap;
}
#lobbyShareRow {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  max-width: 420px;
}
#lobbyShareUrl {
  flex: 1;
  min-width: 0;
  padding: 10px 12px;
  border-radius: 8px;
  border: 1px solid #4a6030;
  background: #263318;
  color: #dde8c0;
  font-size: 0.85rem;
}
#btnCopyShareUrl {
  padding: 10px 16px;
  background: #7a9e40;
  color: #fff;
  border: none;
  border-radius: 8px;
  font-size: 0.85rem;
  cursor: pointer;
  transition: background 0.2s;
  white-space: nowrap;
}
#btnCopyShareUrl:hover { background: #5a7a2a; }
#btnCopyShareUrl.copied { background: #4a6030; }
#lobbySeats {
  display: flex;
  align-items: center;
  gap: 20px;
}
.lobby-seat-panel {
  background: #263318;
  border: 2px solid #3d5530;
  border-radius: 12px;
  padding: 18px 22px;
  min-width: 130px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  transition: border-color 0.2s;
}
.lobby-seat-panel.filled { border-color: #7a9e40; }
.lobby-seat-role {
  font-size: 0.8rem;
  color: #8fa870;
  letter-spacing: 0.05em;
}
.lobby-seat-name {
  font-size: 1rem;
  font-weight: bold;
  color: #dde8c0;
  min-height: 1.4em;
}
.lobby-vs {
  font-size: 1.3rem;
  font-weight: bold;
  color: #4a6030;
}
#btnSwapSeats {
  padding: 8px 20px;
  background: #3d5530;
  color: #dde8c0;
  border: 1px solid #5a7a2a;
  border-radius: 8px;
  font-size: 0.85rem;
  cursor: pointer;
  transition: background 0.2s;
}
#btnSwapSeats:hover { background: #4a6030; }
#lobbyResultMsg {
  font-size: 1.3rem;
  font-weight: bold;
  min-height: 1.5em;
}
#lobbyGameStatus {
  font-size: 0.9rem;
  color: #8fa870;
}
#btnLobbyStart {
  padding: 12px 36px;
  background: #7a9e40;
  color: #fff;
  border: none;
  border-radius: 8px;
  font-size: 1.05rem;
  cursor: pointer;
  transition: background 0.2s;
}
#btnLobbyStart:hover { background: #5a7a2a; }
#btnLobbyLeave {
  padding: 10px 24px;
  background: #263318;
  color: #8fa870;
  border: 1px solid #3d5530;
  border-radius: 8px;
  font-size: 0.9rem;
  cursor: pointer;
  transition: background 0.2s;
}
#btnLobbyLeave:hover { background: #3d5530; }

/* ---- スマホ向けレスポンシブ対応 ---- */
@media (max-width: 640px) {
  body {
    align-items: flex-start;
    padding: 12px 0;
  }

  #lobby, #roomLobby, #cpuSetupScreen {
    padding: 24px 16px;
    width: 100%;
  }

  #lobbySeats {
    gap: 10px;
  }
  .lobby-seat-panel {
    min-width: 0;
    flex: 1;
    padding: 14px 10px;
  }

  #gameArea {
    padding: 10px;
    width: 100%;
  }

  #gameContent {
    flex-direction: column;
    align-items: center;
    width: 100%;
  }

  #gameLeft {
    width: 100%;
  }

  #mainLayout {
    flex-direction: column;
    width: 100%;
    gap: 8px;
  }

  #board {
    width: 100%;
    max-width: 400px;
    margin: 0 auto;
  }

  .hand-panel {
    width: 100%;
    max-width: 400px;
    min-height: 0;
  }

  #setupPanel {
    width: 100%;
    max-width: 400px;
    min-width: 0;
  }
  #setupPiecesList {
    max-width: 100%;
  }

  #pieceInfo {
    width: 100%;
    max-width: 400px;
    height: auto;
    max-height: 340px;
  }
}
