* {
  box-sizing: border-box;
}

html {
  /* 連打でズームさせない(iPhone対策)。ピンチズームは残す。 */
  touch-action: manipulation;
}

:root {
  --bg-color: #fcf8f2;
  --panel-bg: #fffdfa;
  --text-main: #5c4b37;
  --text-sub: #8c7b67;
  --board-border: #ebdcd0;
  --cell-bg: #ffffff;
  --cell-hover: #fcf1e6;
  --accent: #d96b27;
  --accent-soft: #f5d6be;
  --accent-bg: #fff3ec;
  --me-color: #3a86ff;
  --me-bg: rgba(112, 181, 255, 0.22);
  --rival-color: #e63946;
  --rival-bg: rgba(255, 133, 133, 0.22);
  --font-rounded: "M PLUS Rounded 1c", -apple-system, "Hiragino Sans", "Yu Gothic", sans-serif;
}

body {
  margin: 0;
  min-height: 100dvh;
  font-family: var(--font-rounded);
  background: var(--bg-color);
  color: var(--text-main);
  padding-bottom: 92px; /* 常時表示フッターの高さぶん確保する */
}

button {
  font-family: inherit;
}

.hidden {
  display: none !important;
}

.screen {
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 20px 14px 16px;
}

.panel {
  width: 100%;
  max-width: 560px;
  background: var(--panel-bg);
  border-radius: 24px;
  padding: 24px;
  border: 3px solid #f2e6dc;
  box-shadow: 0 6px 16px rgba(217, 149, 93, 0.1);
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  background-color: #fff;
  color: var(--text-main);
  border: 2px solid #e2d2c3;
  padding: 8px 18px;
  border-radius: 20px;
  cursor: pointer;
  font-size: 13px;
  font-weight: 700;
  box-shadow: 0 4px 0 #e2d2c3;
  transition: all 0.1s;
}

.btn:hover {
  background-color: #fff9f0;
}

.btn:active {
  transform: translateY(2px);
  box-shadow: 0 2px 0 #e2d2c3;
}

.primary-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  width: 100%;
  padding: 14px;
  margin-top: 8px;
  border: none;
  border-radius: 999px;
  background: var(--accent);
  color: #fff;
  font-size: 16px;
  font-weight: 800;
  cursor: pointer;
  box-shadow: 0 4px 0 #b5551c;
}

.primary-button:hover {
  filter: brightness(1.06);
}

.primary-button:active {
  transform: translateY(2px);
  box-shadow: 0 2px 0 #b5551c;
}

.primary-button:disabled {
  opacity: 0.6;
  cursor: default;
}

/* ---- 参加画面 --------------------------------------------------------- */

.join-panel {
  text-align: center;
}

.hero-quokka {
  width: 110px;
  height: 110px;
  margin: 0 auto 4px;
}

.game-title {
  margin: 0 0 4px;
  font-size: 25px;
  font-weight: 800;
  color: var(--accent);
  text-shadow: 2px 2px 0 #ffe5d0;
  letter-spacing: 1px;
}

.catch-copy {
  margin: 0 0 18px;
  color: var(--text-sub);
  font-size: 13px;
  font-weight: 700;
}

.field {
  display: block;
  text-align: left;
  margin-bottom: 12px;
}

.field > span {
  display: block;
  font-size: 12px;
  color: var(--text-sub);
  font-weight: 700;
  margin-bottom: 4px;
}

.field input {
  width: 100%;
  padding: 10px 12px;
  border: 2px solid var(--board-border);
  border-radius: 12px;
  font-size: 16px;
  font-family: inherit;
  background: #fff;
  color: var(--text-main);
}

/* 難易度(色の数)の選択 */
.mode-row {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  margin-bottom: 12px;
}

.mode-option {
  border: 2px solid var(--board-border);
  border-radius: 14px;
  background: #fff;
  padding: 10px 6px;
  cursor: pointer;
  text-align: center;
  font-family: inherit;
  color: var(--text-main);
}

.mode-option .mode-name {
  display: block;
  font-size: 13px;
  font-weight: 800;
}

.mode-option .mode-desc {
  display: block;
  font-size: 10px;
  color: var(--text-sub);
  font-weight: 700;
  margin-top: 2px;
}

.mode-option.active {
  border-color: var(--accent);
  background: var(--accent-bg);
  box-shadow: 0 0 8px rgba(255, 153, 102, 0.3);
}

.start-buttons {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.secondary-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  width: 100%;
  padding: 12px;
  border: 2px solid var(--accent-soft);
  border-radius: 999px;
  background: #fff;
  color: var(--accent);
  font-size: 14px;
  font-weight: 800;
  cursor: pointer;
}

.secondary-button:hover {
  background: var(--accent-bg);
}

.cpu-level-note {
  font-size: 11px;
  color: var(--text-sub);
  font-weight: 700;
  margin: 6px 0 0;
}

.link-row {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px;
  margin: 16px 0;
}

.rules-link,
.link-row .home-link,
.share-button {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 5px 12px;
  border: 2px solid var(--accent-soft);
  border-radius: 999px;
  background: #ffffff;
  color: var(--accent);
  font-size: 12px;
  font-weight: 700;
  font-family: inherit;
  text-decoration: none;
  cursor: pointer;
}

.rules-link:hover,
.link-row .home-link:hover,
.share-button:hover {
  border-color: var(--accent);
  background: var(--accent-bg);
}

.ranking-panels {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  margin-top: 16px;
}

.ranking-panel {
  background: var(--accent-bg);
  border-radius: 16px;
  padding: 12px;
  text-align: left;
}

.ranking-panel h2 {
  margin: 0 0 6px;
  font-size: 13px;
  color: var(--accent);
}

.ranking-list {
  list-style: none;
  margin: 0;
  padding: 0;
  font-size: 12px;
  font-weight: 700;
}

.ranking-list li {
  display: flex;
  justify-content: space-between;
  padding: 3px 0;
  border-bottom: 1px dashed var(--accent-soft);
}

.ranking-list li:last-child {
  border-bottom: none;
}

.ranking-empty {
  color: var(--text-sub);
  font-size: 12px;
  padding: 4px 0;
}

/* ---- マッチング(待機)画面 ------------------------------------------- */

.waiting-panel {
  text-align: center;
}

.waiting-quokka {
  width: 90px;
  height: 90px;
  margin: 0 auto 8px;
  animation: waiting-hop 1s ease-in-out infinite alternate;
}

@keyframes waiting-hop {
  0% {
    transform: translateY(0);
  }
  100% {
    transform: translateY(-6px);
  }
}

.waiting-text {
  font-size: 15px;
  font-weight: 800;
  color: var(--accent);
  margin: 0 0 4px;
}

.waiting-sub {
  font-size: 12px;
  color: var(--text-sub);
  font-weight: 700;
  margin: 0 0 16px;
}

/* ---- ゲーム画面 ------------------------------------------------------- */

#game-screen {
  width: 100%;
}

.hud {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  justify-content: center;
  margin-bottom: 10px;
  /* 右上固定のサウンドボタン(36px×2+余白)と重ならないようにする */
  padding-right: 88px;
  padding-left: 88px;
}

.pill {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 6px 12px;
  border-radius: 999px;
  background: var(--panel-bg);
  border: 2px solid var(--board-border);
  font-size: 12px;
  font-weight: 700;
}

.pill.turn-pill.my-turn {
  border-color: var(--me-color);
  color: var(--me-color);
}

.pill.turn-pill.rival-turn {
  border-color: var(--rival-color);
  color: var(--rival-color);
}

#message {
  min-height: 20px;
  font-weight: 800;
  color: var(--accent);
  font-size: 13px;
  text-align: center;
  margin-bottom: 8px;
}

.game-body {
  display: flex;
  align-items: flex-start;
  justify-content: center;
  gap: 16px;
  width: 100%;
}

.side-panel {
  display: flex;
  flex-direction: column;
  gap: 8px;
  width: 190px;
  background: var(--panel-bg);
  padding: 12px;
  border-radius: 16px;
  box-shadow: 0 6px 16px rgba(217, 149, 93, 0.1);
  border: 3px solid #f2e6dc;
}

.side-panel h3 {
  margin: 0;
  font-size: 13px;
  text-align: center;
  color: var(--accent);
  font-weight: 800;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.side-panel .avatar {
  width: 54px;
  height: 54px;
  margin: 0 auto;
}

.total-area-box {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 12px;
  border-radius: 10px;
  border: 2px dashed;
}

.total-area-box.me-box {
  background: rgba(112, 181, 255, 0.12);
  border-color: rgba(112, 181, 255, 0.6);
}

.total-area-box.rival-box {
  background: rgba(255, 133, 133, 0.12);
  border-color: rgba(255, 133, 133, 0.6);
}

.total-area-box .label {
  font-size: 11px;
  color: var(--text-sub);
  font-weight: 700;
}

.total-area-box.me-box .value {
  font-size: 18px;
  font-weight: 800;
  color: var(--me-color);
}

.total-area-box.rival-box .value {
  font-size: 18px;
  font-weight: 800;
  color: var(--rival-color);
}

/* 獲得数が増えたときの演出(値のポップ) */
.total-area-box .value.gain-pop {
  animation: gain-pop 0.5s ease-out;
}

@keyframes gain-pop {
  0% {
    transform: scale(1);
  }
  40% {
    transform: scale(1.6);
  }
  100% {
    transform: scale(1);
  }
}

.chips-container {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.chip-status {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 5px 10px;
  border-radius: 10px;
  font-size: 11px;
  font-weight: 700;
  border: 2px solid transparent;
  background: #fff;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.03);
}

.chip-status.selectable {
  cursor: pointer;
  transition: all 0.15s;
}

.chip-status.selectable:hover {
  background: #fff8f0;
  transform: scale(1.02);
}

.chip-status.active {
  border-color: #ff9966;
  background-color: var(--accent-bg);
  box-shadow: 0 0 8px rgba(255, 153, 102, 0.3);
}

.chip-status.depleted {
  opacity: 0.45;
}

.dot {
  width: 14px;
  height: 12px;
  border-radius: 50% / 40%;
  display: inline-block;
  margin-right: 6px;
  vertical-align: middle;
  box-shadow: 0 2px 3px rgba(0, 0, 0, 0.15);
  border-bottom: 2px solid rgba(0, 0, 0, 0.1);
}

/* ---- 盤面 ------------------------------------------------------------ */

#board-wrapper {
  display: flex;
  flex-direction: column;
  align-items: center;
  order: 2;
}

#me-panel {
  order: 1;
}

#rival-panel {
  order: 3;
}

.board-grid-container {
  display: grid;
  grid-template-columns: repeat(8, 44px);
  grid-template-rows: repeat(8, 44px);
  gap: 3px;
  background-color: var(--board-border);
  padding: 6px;
  border-radius: 16px;
  box-shadow: 0 10px 25px rgba(140, 115, 93, 0.15);
}

.cell {
  width: 44px;
  height: 44px;
  background-color: var(--cell-bg);
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  position: relative;
  transition: background-color 0.15s, transform 0.1s;
}

.cell:hover {
  background-color: var(--cell-hover);
  transform: scale(1.05);
  z-index: 10;
}

.cell.owner-me {
  background-color: var(--me-bg);
}

.cell.owner-rival {
  background-color: var(--rival-bg);
}

.cell.valid-move::after {
  content: "🐾";
  position: absolute;
  font-size: 12px;
  opacity: 0.6;
  animation: paw-bounce 1.2s infinite alternate;
}

@keyframes paw-bounce {
  0% {
    transform: translateY(0);
    opacity: 0.4;
  }
  100% {
    transform: translateY(-4px);
    opacity: 0.9;
  }
}

.cell.last-move::before {
  content: "";
  position: absolute;
  top: 2px;
  left: 2px;
  right: 2px;
  bottom: 2px;
  border: 2px dashed #ffb703;
  border-radius: 6px;
  pointer-events: none;
  z-index: 2;
}

.piece {
  width: 36px;
  height: 30px;
  border-radius: 50% / 45%;
  z-index: 1;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.15), inset 0 2px 3px rgba(255, 255, 255, 0.7);
  position: relative;
}

.piece::after {
  content: "";
  position: absolute;
  top: 50%;
  left: 2px;
  right: 2px;
  height: 4px;
  background: rgba(255, 255, 255, 0.85);
  transform: translateY(-50%);
  border-radius: 2px;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
}

/* ひっくり返した(獲得した)ときの演出 */
.piece.flip-pop {
  animation: flip-pop 0.45s ease-out;
}

@keyframes flip-pop {
  0% {
    transform: scaleX(1);
  }
  50% {
    transform: scaleX(0.1) translateY(-4px);
  }
  100% {
    transform: scaleX(1);
  }
}

.white {
  background: linear-gradient(135deg, #ffffff 0%, #f0ebe1 100%);
  border: 1px solid #dcd4c9;
}

.blue {
  background: linear-gradient(135deg, #a2d2ff 0%, #70b5ff 100%);
  border: 1px solid #5294db;
}

.yellow {
  background: linear-gradient(135deg, #ffe599 0%, #ffd666 100%);
  border: 1px solid #e0be43;
}

.green {
  background: linear-gradient(135deg, #b5e48c 0%, #79d279 100%);
  border: 1px solid #60bd60;
}

.red {
  background: linear-gradient(135deg, #ffb3b3 0%, #ff8585 100%);
  border: 1px solid #e06666;
}

.gold {
  background: linear-gradient(135deg, #ffd166 0%, #ff9933 100%);
  border: 1px solid #e07a1a;
  box-shadow: 0 4px 8px rgba(255, 153, 51, 0.4), inset 0 2px 3px rgba(255, 255, 255, 0.8);
}

/* 強さ凡例 */
#strength-legend {
  margin-top: 12px;
  background: var(--panel-bg);
  padding: 10px 15px;
  border-radius: 14px;
  border: 2px solid #f2e6dc;
  display: flex;
  flex-direction: column;
  gap: 6px;
  width: 100%;
  max-width: 430px;
}

.legend-title {
  font-size: 11px;
  font-weight: 800;
  color: var(--accent);
  text-align: center;
}

.legend-row {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  flex-wrap: wrap;
}

.legend-item-sample {
  display: flex;
  align-items: center;
  gap: 3px;
  font-size: 10px;
  font-weight: 700;
  color: var(--text-sub);
}

.legend-mini-piece {
  width: 22px;
  height: 18px;
  border-radius: 50% / 45%;
  box-shadow: 0 2px 3px rgba(0, 0, 0, 0.1), inset 0 1px 2px rgba(255, 255, 255, 0.7);
  position: relative;
}

.legend-mini-piece::after {
  content: "";
  position: absolute;
  top: 50%;
  left: 1px;
  right: 1px;
  height: 3px;
  background: rgba(255, 255, 255, 0.85);
  transform: translateY(-50%);
  border-radius: 2px;
}

.arrow-icon {
  font-size: 10px;
  color: var(--accent);
  font-weight: 800;
}

.game-controls {
  display: flex;
  gap: 8px;
  justify-content: center;
  margin-top: 10px;
}

/* ---- 結果モーダル ------------------------------------------------------ */

.overlay {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(92, 75, 55, 0.4);
  padding: 16px;
}

.modal-card {
  background: var(--panel-bg);
  border: 4px solid #f2e6dc;
  padding: 24px 32px;
  border-radius: 20px;
  text-align: center;
  box-shadow: 0 10px 30px rgba(217, 149, 93, 0.2);
  animation: pop-up 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275);
  max-width: 320px;
  width: 92%;
}

@keyframes pop-up {
  0% {
    transform: scale(0.8);
    opacity: 0;
  }
  100% {
    transform: scale(1);
    opacity: 1;
  }
}

.modal-title {
  font-size: 22px;
  font-weight: 800;
  color: var(--accent);
  margin-bottom: 10px;
}

.modal-score {
  font-size: 14px;
  font-weight: 700;
  margin-bottom: 20px;
  line-height: 1.6;
}

.modal-buttons {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

/* ---- サウンドパネル ----------------------------------------------------- */

.sound-controls {
  position: fixed;
  top: 12px;
  right: 12px;
  z-index: 60;
  display: flex;
  gap: 6px;
}

.sound-toggle-btn,
.sound-settings-btn {
  width: 36px;
  height: 36px;
  border-radius: 999px;
  border: 2px solid var(--board-border);
  background: var(--panel-bg);
  font-size: 16px;
  cursor: pointer;
}

.sound-panel {
  position: absolute;
  top: 44px;
  right: 0;
  background: var(--panel-bg);
  border: 2px solid var(--board-border);
  border-radius: 16px;
  padding: 12px;
  width: 200px;
  box-shadow: 0 8px 20px rgba(140, 115, 93, 0.15);
}

.sound-row {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  font-weight: 700;
  margin-bottom: 8px;
}

.sound-row:last-child {
  margin-bottom: 0;
}

.sound-row input[type="range"] {
  flex: 1;
}

.mute-chip {
  border: none;
  background: none;
  cursor: pointer;
  font-size: 14px;
}

.sound-row.hide-slider input[type="range"] {
  display: none;
}

/* ---- 常時表示フッター ---------------------------------------------------- */

.site-footer {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 55;
  background: var(--panel-bg);
  border-top: 2px solid var(--board-border);
  padding: 6px 12px;
  text-align: center;
  font-size: 11px;
  color: var(--text-sub);
}

.site-footer a {
  color: var(--text-sub);
  text-decoration: none;
}

.site-footer a:hover {
  color: var(--accent);
}

.footer-links,
.footer-meta {
  margin: 2px 0;
}

.version-link {
  font-size: 10px;
  color: var(--text-sub);
  text-decoration: none;
  border-bottom: 1px dotted currentColor;
}

.version-link:hover {
  color: var(--accent);
}

/* ---- 法務モーダル ------------------------------------------------------- */

.legal-modal {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}

.legal-modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(92, 75, 55, 0.5);
}

.legal-modal-body {
  position: relative;
  background: var(--panel-bg);
  border-radius: 16px;
  width: 100%;
  max-width: 640px;
  height: 80vh;
  padding: 12px;
}

.legal-modal-close {
  position: absolute;
  top: 8px;
  right: 8px;
  border: none;
  background: var(--accent-bg);
  border-radius: 999px;
  width: 32px;
  height: 32px;
  cursor: pointer;
  z-index: 1;
}

.legal-modal-body iframe {
  width: 100%;
  height: 100%;
  border: none;
  border-radius: 10px;
  background: #fff;
}

/* ---- 通信エラー -------------------------------------------------------- */

.disconnect-banner {
  position: fixed;
  inset: 0;
  z-index: 90;
  background: rgba(92, 75, 55, 0.9);
  color: #fff;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  text-align: center;
  padding: 24px;
  font-weight: 700;
}

.disconnect-banner button {
  padding: 10px 20px;
  border-radius: 999px;
  border: none;
  background: var(--accent);
  color: #fff;
  cursor: pointer;
  font-weight: 800;
}

/* ---- レスポンシブ ------------------------------------------------------- */

@media (max-width: 850px) {
  .screen {
    padding: 12px 8px 16px;
  }

  .game-body {
    flex-direction: column;
    align-items: center;
    gap: 10px;
    width: 100%;
    max-width: 450px;
    margin: 0 auto;
  }

  .side-panel {
    width: 100%;
    max-width: 380px;
    padding: 8px 12px;
    gap: 6px;
    flex-direction: row;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
  }

  .side-panel h3 {
    font-size: 12px;
    flex: 1;
    text-align: left;
  }

  .side-panel .avatar {
    width: 38px;
    height: 38px;
    margin: 0;
  }

  .side-panel .total-area-box {
    flex: 1 1 100%;
  }

  .chips-container {
    flex-direction: row;
    flex-wrap: wrap;
    gap: 4px;
    flex: 1 1 100%;
  }

  .chip-status {
    flex: 1 1 45%;
    padding: 4px 8px;
    font-size: 10px;
  }

  /* 対面感が出るよう、相手を上・盤面を真ん中・自分を下に並べ替える */
  #board-wrapper {
    width: 100%;
    order: 2;
  }

  #me-panel {
    order: 3;
  }

  #rival-panel {
    order: 1;
  }

  .board-grid-container {
    grid-template-columns: repeat(8, minmax(0, 1fr));
    grid-template-rows: repeat(8, minmax(0, 1fr));
    max-width: 380px;
    width: 100%;
    aspect-ratio: 1 / 1;
    padding: 4px;
    gap: 2px;
  }

  .cell {
    width: 100%;
    height: 100%;
  }

  .piece {
    width: 80%;
    height: 70%;
  }

  .ranking-panels {
    grid-template-columns: 1fr;
  }
}
