body {
  --cell-size: clamp(64px, 7.5vw, 92px);
  --board-size: calc(var(--cell-size) * 5);
  --side-width: clamp(148px, 21vw, 264px);
  font-family: sans-serif;
  margin: 0;
  padding: 12px;
  color: #1f1608;
  background:
    linear-gradient(90deg, rgba(82, 108, 38, 0.18) 1px, transparent 1px) 0 0 / 112px 112px,
    linear-gradient(0deg, rgba(82, 108, 38, 0.18) 1px, transparent 1px) 0 0 / 112px 112px,
    repeating-linear-gradient(90deg, #afbf75 0 16px, #bfcd84 16px 32px, #9fb366 32px 36px);
  min-height: 100vh;
}

h1 {
  display: inline-block;
  margin: 0 0 18px;
  padding: 14px 24px;
  background: linear-gradient(180deg, #fff4cf, #ead4a2);
  border: 2px solid rgba(104, 68, 27, 0.35);
  border-radius: 5px;
  box-shadow: 0 6px 0 rgba(91, 58, 24, 0.22);
  color: #2d1700;
  font-size: 36px;
}

#controls, #historyControls {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
}

#controls button,
#controls select {
  min-height: 42px;
  padding: 8px 12px;
  font-size: 16px;
}

#controls,
#status {
  width: max-content;
  padding: 10px 14px;
  background: rgba(255, 251, 230, 0.88);
  border-left: 5px solid #8b5a24;
  box-shadow: 0 4px 12px rgba(49, 35, 15, 0.16);
}

#status {
  margin: 10px 0 14px;
  font-weight: bold;
  min-height: 1.5em;
}

.game-layout {
  display: grid;
  grid-template-columns: var(--side-width) var(--board-size) var(--side-width);
  gap: 16px;
  align-items: start;
  width: min-content;
  max-width: calc(100vw - 24px);
  padding: clamp(10px, 2vw, 24px);
  border: 8px solid #7b5320;
  background:
    linear-gradient(90deg, rgba(255, 255, 255, 0.18), rgba(90, 63, 23, 0.08)),
    rgba(238, 229, 177, 0.52);
  box-shadow: inset 0 0 0 2px rgba(255,255,255,0.2);
}

.opponent-panel,
.own-panel {
  width: var(--side-width);
}

.own-panel {
  min-height: var(--board-size);
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
}

.board-panel {
  width: var(--board-size);
}

.board-wrap {
  position: relative;
  width: var(--board-size);
}

#board {
  display: grid;
  grid-template-columns: repeat(5, var(--cell-size));
  width: var(--board-size);
  border: 2px solid #333;
  margin-bottom: 0;
  box-shadow: 0 12px 20px rgba(52, 31, 10, 0.35);
}

.cell {
  width: var(--cell-size);
  height: var(--cell-size);
  border: 1px solid #555;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  background: #f0d9b5;
}

.cell.dark {
  background: #e6c48f;
}

.cell.selected {
  background: #ffd166;
  outline: 3px solid #0077b6;
  outline-offset: -3px;
}

.cell.legal {
  background: #90e0ef;
  box-shadow: inset 0 0 0 4px #0096c7;
}

.piece {
  font-size: 24px;
  font-weight: bold;
  user-select: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 62px;
  height: 62px;
  border-radius: 4px;
  transition: transform 120ms ease, box-shadow 120ms ease;
}

.piece.own {
  color: #1f2937;
  background: #fff7df;
  border: 1px solid #8a6a2a;
}

.piece.opponent {
  color: #f8fafc;
  background: #475569;
  border: 1px solid #1e293b;
  transform: rotate(180deg);
}

.piece.lifted {
  transform: translateY(-8px) scale(1.08);
  box-shadow: 0 8px 14px rgba(0, 0, 0, 0.25);
}

.piece.opponent.lifted {
  transform: rotate(180deg) translateY(-8px) scale(1.08);
}

.sprite-piece {
  color: inherit;
  overflow: hidden;
}

.piece-sprite-ready .sprite-piece {
  color: transparent;
  background-color: transparent;
  background-image: url("piece.png?v=20260503-37");
  background-size: 800% 400%;
  background-position: var(--sprite-x) var(--sprite-y);
  background-repeat: no-repeat;
  border: 0;
}

.piece-sprite-ready .piece.opponent {
  transform: none;
}

.piece-sprite-ready .piece.lifted {
  transform: translateY(-8px) scale(1.08);
}

.piece-sprite-ready .sprite-piece .piece-label {
  opacity: 0;
}

.piece-label {
  position: relative;
  z-index: 1;
}

.side-panel h2,
.hands-block h2 {
  margin: 0 0 8px;
  padding: 8px 10px;
  font-size: 16px;
  color: #fff7dc;
  background: linear-gradient(90deg, #4b3116, #5f421f);
  border-radius: 4px;
}

.hands {
  border: 1px solid #aaa;
  min-height: 72px;
  padding: 8px;
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  background: rgba(255, 252, 241, 0.92);
}

.hand-piece {
  border: 1px solid #666;
  background: white;
  padding: 4px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 4px;
  min-height: 42px;
}

.hand-piece.selected {
  background: #d6f5e5;
}

.hand-piece-icon,
.setup-piece {
  width: 30px;
  height: 34px;
  flex: 0 0 auto;
}

.hand-count {
  min-width: 22px;
  font-size: 14px;
  font-weight: bold;
}

.move-list-block {
  margin-top: 16px;
}

.move-list-block h2 {
  margin: 0 0 8px;
  padding: 8px 10px;
  font-size: 16px;
  color: #fff7dc;
  background: linear-gradient(90deg, #4b3116, #5f421f);
  border-radius: 4px;
}

.move-list {
  box-sizing: border-box;
  width: 100%;
  height: 190px;
  margin: 0;
  padding: 8px 8px 8px 32px;
  overflow-y: auto;
  border: 1px solid #aaa;
  background: rgba(255, 252, 241, 0.92);
}

.move-list li {
  padding: 2px 4px;
}

.move-list li.latest {
  background: #fff3bf;
  font-weight: bold;
}

#historyControls {
  margin-top: 8px;
  width: 100%;
}

.board-overlay {
  position: absolute;
  inset: 0 0 12px 0;
  display: none;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  gap: 10px;
  color: #fff8dd;
  text-align: center;
  pointer-events: none;
  text-shadow: 0 3px 8px rgba(0,0,0,0.6);
}

.board-overlay.show {
  display: flex;
}

.board-overlay.thinking {
  background:
    radial-gradient(circle at center, rgba(255, 239, 160, 0.38), transparent 58%),
    rgba(36, 24, 11, 0.22);
  font-size: 24px;
  font-weight: 900;
}

.board-overlay.thinking.deep {
  background: rgba(36, 24, 11, 0.36);
}

.thinking-stage {
  display: flex;
  gap: 12px;
  align-items: end;
}

.thinking-stage span {
  display: grid;
  place-items: center;
  width: 42px;
  height: 46px;
  color: #2c1700;
  font-size: 22px;
  background: linear-gradient(180deg, #fff18a, #e5b949);
  clip-path: polygon(50% 0, 95% 12%, 86% 100%, 14% 100%, 5% 12%);
  filter: drop-shadow(0 5px 3px rgba(0,0,0,0.35));
  animation: pieceBounce 1.05s ease-in-out infinite;
}

.thinking-stage span:nth-child(2) {
  animation-delay: 0.15s;
}

.thinking-stage span:nth-child(3) {
  animation-delay: 0.3s;
}

.thinking-text {
  padding: 6px 14px;
  border-radius: 999px;
  background: rgba(62, 37, 12, 0.72);
}

.board-overlay.level-intro {
  color: #4a2700;
  font-size: 64px;
  font-weight: 900;
  background: radial-gradient(circle at center, rgba(255,246,181,0.96), rgba(255,246,181,0.18) 62%, transparent 70%);
  animation: levelSettle 1.25s ease-out both;
}

.result-banner {
  display: none;
}

.promotion-dialog {
  position: fixed;
  inset: 0;
  z-index: 850;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(28, 18, 8, 0.28);
}

.promotion-box {
  width: min(88vw, 280px);
  padding: 18px;
  border: 3px solid #7b5320;
  border-radius: 8px;
  background: #fff8df;
  box-shadow: 0 18px 38px rgba(0,0,0,0.32);
  text-align: center;
}

.promotion-title {
  margin-bottom: 14px;
  font-size: 22px;
  font-weight: 900;
}

.promotion-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.promotion-actions button {
  min-height: 44px;
  border: 1px solid #7b5320;
  border-radius: 6px;
  background: linear-gradient(180deg, #fff3b0, #e5bd55);
  font-size: 16px;
  font-weight: 900;
  cursor: pointer;
}

.invalid-box {
  border-color: #991b1b;
}

.invalid-reason {
  margin: 4px 0 16px;
  padding: 10px 12px;
  border-radius: 6px;
  background: #fee2e2;
  color: #7f1d1d;
  font-size: 20px;
  font-weight: 900;
}

.invalid-actions {
  grid-template-columns: 1fr;
}

.result-banner.show {
  position: fixed;
  left: 50%;
  top: 50%;
  z-index: 900;
  display: block;
  min-width: 280px;
  padding: 28px 42px;
  text-align: center;
  border-radius: 6px;
  transform: translate(-50%, -50%);
  animation: resultPop 0.7s ease-out both;
  box-shadow: 0 22px 48px rgba(0, 0, 0, 0.35);
  cursor: pointer;
}

.result-banner.show::before {
  content: "";
  position: fixed;
  inset: -100vh;
  z-index: -1;
  background: radial-gradient(circle at center, rgba(255,244,188,0.45), rgba(0,0,0,0.38));
}

.result-banner.win {
  color: #3b2200;
  background: linear-gradient(180deg, #fff3b0, #f5b43b);
  border: 4px solid #7a4a00;
}

.result-banner.lose {
  color: #ffffff;
  background: linear-gradient(180deg, #374151, #111827);
  border: 4px solid #c7d2fe;
}

.result-banner.sennichite {
  color: #321700;
  background: linear-gradient(180deg, #fef08a, #f97316);
  border: 5px solid #7c2d12;
}

.result-main {
  font-size: 56px;
  font-weight: 900;
  letter-spacing: 0.12em;
  text-indent: 0.12em;
}

.result-sub {
  margin-top: 8px;
  font-size: 18px;
  font-weight: bold;
}

.result-hint {
  margin-top: 12px;
  font-size: 13px;
  font-weight: bold;
  opacity: 0.78;
}

.result-close {
  position: absolute;
  right: 8px;
  top: 8px;
  width: 28px;
  height: 28px;
  border: 1px solid rgba(0,0,0,0.2);
  border-radius: 50%;
  background: rgba(255,255,255,0.7);
  cursor: pointer;
  font-size: 18px;
  line-height: 1;
}

@keyframes resultPop {
  0% { opacity: 0; transform: translate(-50%, -50%) scale(0.55); }
  55% { opacity: 1; transform: translate(-50%, -50%) scale(1.12); }
  100% { opacity: 1; transform: translate(-50%, -50%) scale(1); }
}

@keyframes pieceBounce {
  0%, 100% { transform: translateY(0) rotate(-4deg); }
  45% { transform: translateY(-18px) rotate(5deg); }
}

@keyframes levelSettle {
  0% { opacity: 0; transform: scale(2.2); }
  58% { opacity: 1; transform: scale(1.08); }
  100% { opacity: 1; transform: scale(0.86); }
}

.copyright {
  display: inline-block;
  margin-top: 18px;
  padding: 8px 12px;
  background: rgba(255, 251, 230, 0.86);
  color: #3a2a11;
  font-size: 13px;
}

.rules,
.credits {
  box-sizing: border-box;
  width: min(100%, 1000px);
  margin-top: 18px;
  background: rgba(255, 251, 230, 0.9);
  border: 2px solid rgba(91, 58, 24, 0.42);
  box-shadow: 0 5px 12px rgba(49, 35, 15, 0.14);
}

.rules summary,
.credits summary {
  cursor: pointer;
  padding: 12px 16px;
  font-weight: 900;
  background: linear-gradient(90deg, #4b3116, #5f421f);
  color: #fff7dc;
}

.rules-body,
.credits-body {
  padding: 14px 18px 18px;
  line-height: 1.7;
}

.rules-body h2 {
  margin: 12px 0 4px;
  font-size: 16px;
}

.rules-body p,
.rules-body ul,
.credits-body p {
  margin: 0 0 8px;
}

.credits a {
  color: #1f3f73;
  font-weight: 700;
}

@media (max-width: 720px) {
  body {
    --cell-size: min(18vw, 70px);
    padding: 8px;
  }

  h1 {
    margin-bottom: 10px;
    padding: 10px 16px;
    font-size: 30px;
  }

  #controls,
  #status {
    box-sizing: border-box;
    width: 100%;
  }

  #controls {
    gap: clamp(4px, 1.2vw, 8px);
    padding: 8px 10px;
    flex-wrap: nowrap;
    overflow: hidden;
  }

  #controls button,
  #controls select {
    min-height: clamp(38px, 9vw, 44px);
    padding: 6px clamp(6px, 1.7vw, 10px);
    font-size: clamp(14px, 3.8vw, 16px);
  }

  #controls label {
    white-space: nowrap;
    font-size: clamp(14px, 3.8vw, 16px);
  }

  #controls select {
    max-width: 86px;
  }

  .game-layout {
    grid-template-columns: 1fr;
    grid-template-areas:
      "opponent"
      "board"
      "own"
      "history";
    gap: 12px;
    width: 100%;
    box-sizing: border-box;
    max-width: 100%;
    padding: 8px;
    overflow: hidden;
  }

  .opponent-panel {
    grid-area: opponent;
    width: auto;
    display: contents;
  }

  #opponentHandsTitle {
    grid-area: opponent;
  }

  #opponentHands {
    grid-area: opponent;
    margin-top: 40px;
  }

  .board-panel {
    grid-area: board;
    width: var(--board-size);
    justify-self: center;
  }

  .own-panel {
    grid-area: own;
    width: auto;
    min-height: 0;
  }

  .move-list-block {
    grid-area: history;
    margin-top: 12px;
  }

  .side-panel h2,
  .move-list-block h2 {
    font-size: 14px;
    padding: 7px 8px;
  }

  .hands {
    min-height: 54px;
    padding: 6px;
    gap: 4px;
  }

  .hand-piece {
    padding: 2px;
    min-height: 36px;
  }

  .hand-piece-icon,
  .setup-piece {
    width: 27px;
    height: 31px;
  }

  .move-list {
    height: 128px;
    font-size: 14px;
  }

  #historyControls button {
    min-height: 36px;
  }
}
