body {
  font-family: system-ui, -apple-system, sans-serif;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  background-color: #f4f4f4;
}

.body-wrapper {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  width: 100%
}

/* Header */
.site-header {
  text-align: center;
  padding: 16px;
  border-bottom: 1px solid #ddd;
  background: #fff;
}

.site-header h1 {
  margin-bottom: 0;
}

.subtitle {
  margin: 2px 0;
  font-size: 0.8rem;
  color: #555;
}

.site-description {
  margin: 5px 0 0.67rem;
  font-size: 0.6rem;
  color: #555;
}

.site-caution {
  margin: 5px 50px 0.67rem;
  font-size: 0.8rem;
  color: #8b0000;
}

/* Footer */
.site-footer {
  position: static;
  text-align: center;
  padding: 16px;
  border-top: 1px solid #ddd;
  background: #fff;
  font-size: 0.8rem;
}

.site-footer a {
  color: #0066cc;
  text-decoration: none;
}

.site-footer a:hover {
  text-decoration: underline;
}

.footer-sep {
  margin: 0 6px;
  color: #aaa;
}

.site-footer p {
  color: #555;
}

/* Main Wrapper */
.content {
  flex: 1;
  margin: 24px auto;
  width: fit-content;

  --margin-size: 10px;
  --button-size: min(42px, calc(100vh * 0.1));
  --board-width: calc(95vw - 30px - var(--button-size) - var(--margin-size));/*ボタン幅とgap 30px＋余裕10px＋contentのpaddingを差し引く*/
  --board-height: calc(98vh - 40px - 30px - var(--margin-size)); /*captured-area*2とgap 15px*2＋余裕10pxを差し引く*/
  --board-size: min(var(--board-width), var(--board-height));
}

#main-wrapper {
  display: flex;
  flex-direction: row;
  /* 横並び */
  justify-content: center;
  align-items: center;
  max-width: 100%;
  max-height: 100%;
  margin-top: 10px;
  gap: 30px;
}

#play-wrapper {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  gap: 15px;
}

/* --------------- */
/* Player Area */
/* --------------- */
.player-area {
  width: 90%;
  height: 20px;
  display: flex;
  justify-content: center;
}

.captured {
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: flex-start;
  align-items: center;
  flex-wrap: wrap;
  padding: 2px;
}

#captured-top {
  background-color: #333;
  /* 白背景に白い駒が見やすい */
  transform: rotate(180deg);
  /* 180度回転 */
}

#captured-bottom {
  background-color: #fff;
  /* 黒に近い灰色 */
}

.captured-piece {
  width: 15px;
  /* 駒のサイズ調整 */
  height: 15px;
  /* 駒のサイズ調整 */
  object-fit: contain;
  display: inline-block;
}


/* --------------- */
/* Board Wrapper */
/* --------------- */
#board-wrapper {
  width: var(--board-size);
  --width-5: calc(var(--board-size) * 0.05 - 4px); /*ラベルの幅 5%-gap分*/
  --width-90: calc(var(--board-size) * 0.9);

  display: grid;
  grid-template-columns: var(--width-5) var(--width-90) var(--width-5);
  grid-template-rows: var(--width-5) var(--width-90) var(--width-5);
  gap: 4px;
  justify-content: center;
  align-items: center;
}

#play-wrapper.rotated #board-wrapper {
  transform: rotateZ(-180deg);
  transform-origin: center;
}

/* --------------- */
/* Board Label     */
/* --------------- */
/* Top and Bottom */
.file-labels {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  font-size: 14px;
  text-align: center;
  color: #444;
}

/* Left and Right */
.rank-labels {
  height: 100%;
  display: grid;
  grid-template-rows: repeat(8, 1fr);
  font-size: 14px;
  align-items: center;
  text-align: center;
  color: #444;
}

/* Top  */
.top-files {
  grid-column: 2;
  grid-row: 1;
  transform: rotate(180deg);
}

/* Bottom */
.bottom-files {
  grid-column: 2;
  grid-row: 3;
}

/* Left */
.left-ranks {
  grid-column: 1;
  grid-row: 2;
}

/* Right */
.right-ranks {
  grid-column: 3;
  grid-row: 2;
  transform: rotate(180deg);
}

/* --------------- */
/* Board           */
/* --------------- */
#board {
  width: 100%;
  height: 100%;
  display: grid;
  grid-column: 2;
  grid-row: 2;
  grid-template-columns: repeat(8, 1fr);
  grid-template-rows: repeat(8, 1fr);
  background-color: #ddd;
  user-select: none;
  position: relative;
}

.square {
  width: 100%;
  height: 100%;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background-color 0.3s;
  /* 背景色変更のスムーズなアニメーション */
}

.square.light {
  background-color: #f0d9b5;
}

.square.dark {
  background-color: #b58863;
}

.piece {
  width: 90%;
  height: 90%;
  background-size: contain;
  background-repeat: no-repeat;
  background-position: center;
  cursor: pointer;
}

.selected {
  outline: 3px solid orange;
  border-radius: 6px;
}

.move-dot {
  position: absolute;
  width: 30%;
  height: 30%;
  border-radius: 50%;
  background-color: rgba(0, 0, 0, 0.3);
  top: 35%;
  left: 35%;
  pointer-events: none;
  z-index: 999;
}

/* --------------- */
/* Board Rotated   */
/* --------------- */
.rotated #captured-top {
  background-color: #fff;
}

.rotated #captured-bottom {
  background-color: #333;
}

/* --------------- */
/* Attack Area */
/* --------------- */
.square.white-attack {
  background-color: rgba(255, 255, 255, 0.6);
}

.square.black-attack {
  background-color: rgba(170, 170, 170, 0.3);
}

.square.both-attack {
  background-color: rgba(170, 0, 0, 0.3);
}

.square.none {
  background-color: #f0d9b5;
}


/* --------------- */
/* Button 共通     */
/* --------------- */
button {
  width: var(--button-size);
  height: var(--button-size);
  padding: 6px;

  border: none;
  border-radius: 6px;
  cursor: pointer;

  display: flex;
  align-items: center;
  justify-content: center;

  transition: background-color 0.25s ease, transform 0.1s ease;
}

button img {
  width: 70%;
  height: 70%;
  object-fit: contain;
  display: block;
  pointer-events: none;
}

button:disabled {
  background-color: #999;
  color: #eee;
  cursor: not-allowed;
  opacity: 0.6;
}

/* --------------- */
/* Button pane     */
/* --------------- */
#button-pane {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  gap: 10px;
}

#button-pane button {
  background-color: #4CAF50;
  color: white;
}

/* hover */
#button-pane button:hover {
  background-color: #45a049;
  transform: scale(1.07);
}

#button-pane #qa-btn {
  background-color: transparent;
}

#button-pane #bot-btn {
  background-color: transparent;
}

#button-pane #bot-btn img {
  width: 100%;
  height: 100%;
  display: none;
}

#button-pane #bot-btn.bot-off .bot-off {
  display: block;
}

#button-pane #bot-btn.bot-on .bot-on {
  display: block;
}

#button-pane #analysis-btn {
  background-color: #f08080;
}

#button-pane #analysis-btn:hover {
  background-color: #f07878;
}


/* --------------- */
/* ShowAttackBtn   */
/* --------------- */
#show-attack-btn {
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  touch-action: none;
}

/* --------------- */
/* Navigate Button */
/* --------------- */
#button-navigate {
  display: flex;
  flex-direction: row;
  justify-content: center;
  align-items: center;
  margin: 30px 0;
  gap: 10px;
}

#button-navigate button {
  background-color: #797979;
}

/* hover */
#button-navigate button:hover {
  background-color: #4a4a4a;
  transform: scale(1.07);
}

/* --------------- */
/* Moves pane      */
/* --------------- */
#moves-pane {
  text-align: left;
  margin-bottom: 30px;
}

#moves-pane h3 {
  font-size: 20px;
  text-align: center;
  margin: 0px;
}

#pgn {
  max-height: 300px;
  overflow-y: auto;
  padding: 10px;
  margin-top: 0px;
}

#pgn-table {
  width: 150px;
  border-collapse: collapse;
  border: 1px solid #333;
  margin-left: auto;
  margin-right: auto;
}

#pgn-table th,
#pgn-table td {
  border: 1px solid #333;
  padding: 4px;
}

#pgn-table th {
  text-align: center;
}

#pgn-table td {
  text-align: left;
}

#button-pgn {
  display: flex;
  flex-direction: row;
  justify-content: center;
  align-items: center;
}

#moves-pane #button-pgn button {
  background-color: #2196F3;
  margin: 0 10px;
}

/* hover */
#moves-pane #button-pgn button:hover {
  background-color: #0b7dda;
  transform: scale(1.07);
}


/* --------------- */
/* Promotion PopUp */
/* --------------- */
#promotion-popup {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  border-radius: 10px;
  padding: 20px;
  z-index: 1100;
  display: none;
  /* 最初は非表示 */
}

/* 白番 → 背景を黒に */
.promo-dark {
  background-color: #222;
}

/* 黒番 → 背景を白に */
.promo-light {
  background-color: #fff;
  border: 1px solid #ccc;
}

#promotion-popup button {
  width: 60px;
  height: 60px;
  border: none;
  background: none;
  cursor: pointer;
}

#promotion-popup button img {
  width: 100%;
  height: 100%;
}

/* --------------- */
/* Error PopUp    */
/* --------------- */
#error-popup {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  border-radius: 10px;
  background: #fff;
  padding: 20px 24px;
  width: min(70vmin, 300px);
  text-align: center;
  z-index: 1101;
  display: none;
}

#error-text {
  margin-bottom: 20px;
  font-size: 14px;
  line-height: 1.6;
  white-space: pre-wrap;
  word-break: break-word;
}


/* Close Button */
#closePopupBtn {
  justify-content: center;
  align-self: center;
  text-align: center;
  padding: 8px 0;
  margin: 0 auto;
  border-radius: 4px;
  border: none;
  cursor: pointer;
  width: 80px;

  background: #e0e0e0;
}


/* --------------- */
/* BotLevel  PopUp */
/* --------------- */
#botlevel-popup {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  border-radius: 10px;
  background-color: white;
  padding: 20px;
  z-index: 1102;
  display: none;
  /* 最初は非表示 */
}

.bot-color-wrapper {
  margin: 0 auto 15px;
  display: flex;
  flex-direction: row;
  gap: 10px;
  width: fit-content;
}

.bot-level-wrapper {
  display: flex;
  flex-direction: column;
  gap: 6px;
  text-align: center;
}

#botlevel-popup button {
  display: flex;
  flex-direction: column;
  gap: 5px;
  width: 60px;
  height: 70px;
  border: none;
  background-color: #fff;
  cursor: pointer;
}

.bot-level-label {
  font-size: 0.6rem;
  color: #333;
}

#botlevel-popup button img {
  width: 90%;
  height: 90%;
}

/* --------------- */
/* Overlay共通 */
/* --------------- */
#popup-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.4);
  z-index: 1000;
  display: none;
  overflow: hidden;
}

#move-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0);
  z-index: 1001;
  display: none;
  overflow: hidden;
}

/* --------------- */
/* Bot Thinking Spinner */
/* --------------- */
.hidden {
  display: none !important;
}

#engine-spinner {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.4);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1200;
}

.spinner {
  width: 48px;
  height: 48px;
  border: 5px solid #ddd;
  border-top-color: #333;
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

.bot-thinking {
  position: absolute;
  top: 50%;
  left: 50%;
  width: 32px;
  /* アイコンサイズ */
  height: 32px;
  transform: translate(-50%, -50%);
  pointer-events: none;
  /* クリック透過 */
}

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


/* スマホ用（画面幅768px以下） */
@media (max-width: 768px) {

  .content {
    --button-size: min(42px, calc((90vw - 70px)/8));
    --board-width: calc(100vw - var(--margin-size));
  }

  #main-wrapper {
    flex-direction: column;
    /* play-wrapperとbutton-paneを縦並び */
  }

  #button-pane {
    flex-direction: row;
    /* ボタンを横並び */
    flex-wrap: wrap;
    /* 画面幅に収まらなければ折り返す */
    justify-content: center;
  }
}