:root {
  --bg: #efe5cf;
  --panel: #f8f2e4;
  --line: #5e3f26;
  --board: #d8b073;
  --black-adv: rgb(52, 180, 121);
  --white-adv: rgb(238, 82, 66);
  --even: #e6dcc9;
  --text: #2f2418;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: "Hiragino Sans", "Yu Gothic", sans-serif;
  background:
    radial-gradient(circle at 15% 10%, #fff7e3, transparent 38%),
    radial-gradient(circle at 82% 20%, #f4e2c0, transparent 45%),
    var(--bg);
  color: var(--text);
}

.app {
  max-width: 1080px;
  margin: 0 auto;
  padding: 24px 16px 40px;
  display: grid;
  gap: 8px;
}

.app-header h1 {
  margin: 0;
  font-size: 1.7rem;
}

.app-header p {
  margin: 8px 0 0;
}

.overview {
  margin: 0;
  color: rgba(47, 36, 24, 0.78);
}

.legend-row {
  display: flex;
  align-items: center;
  gap: 12px;
}

button {
  font: inherit;
  padding: 8px 10px;
  border: 1px solid #bca178;
  border-radius: 8px;
  background: #fff;
}

button {
  cursor: pointer;
}

.legend {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  align-items: center;
  font-size: 0.95rem;
  flex: 1;
}

.chip {
  display: inline-block;
  width: 14px;
  height: 14px;
  border: 1px solid #8f7658;
  vertical-align: -2px;
}

.chip.black {
  background: var(--black-adv);
}

.chip.white {
  background: var(--white-adv);
}

.chip.even {
  background: var(--even);
}

.hint {
  margin-left: auto;
}

.board-wrap {
  flex: 1;
  min-width: 320px;
  overflow-x: auto;
}

.board-frame {
  width: fit-content;
  margin: 0 auto;
}

.board-main {
  display: grid;
  grid-template-columns: auto 22px;
  gap: 6px;
  align-items: stretch;
}

.file-labels {
  margin-right: 28px;
  width: min(92vw, 720px);
  display: grid;
  grid-template-columns: repeat(9, 1fr);
  text-align: center;
  font-family: "Hiragino Sans", "Yu Gothic", sans-serif;
  font-size: clamp(11px, 1.3vw, 14px);
  font-weight: 700;
  color: rgba(47, 36, 24, 0.78);
  margin-bottom: 4px;
}

.rank-labels {
  display: grid;
  grid-template-rows: repeat(9, 1fr);
  height: min(92vw, 720px);
  text-align: center;
  align-items: center;
  font-family: "Hiragino Sans", "Yu Gothic", sans-serif;
  font-size: clamp(11px, 1.3vw, 14px);
  font-weight: 700;
  color: rgba(47, 36, 24, 0.78);
}

.play-area {
  display: flex;
  gap: 14px;
  align-items: flex-start;
}

.board {
  width: min(92vw, 720px);
  aspect-ratio: 1;
  display: grid;
  grid-template-columns: repeat(9, 1fr);
  grid-template-rows: repeat(9, 1fr);
  border: 3px solid var(--line);
  background:
    linear-gradient(90deg, rgba(120, 78, 45, 0.08) 0%, rgba(120, 78, 45, 0) 20%, rgba(120, 78, 45, 0.08) 40%, rgba(120, 78, 45, 0) 60%, rgba(120, 78, 45, 0.07) 80%, rgba(120, 78, 45, 0) 100%),
    linear-gradient(180deg, #e2be86 0%, var(--board) 45%, #cfa468 100%);
  margin: 0 auto;
  box-shadow: 0 8px 18px rgba(66, 38, 17, 0.24);
}

.piece-stand {
  width: 220px;
  display: grid;
  gap: 10px;
}

.stand-display,
.stand-controls {
  border: 1px solid #ccb590;
  border-radius: 10px;
  background: #f8f2e6;
  padding: 12px;
}

.stand-display .stand-section:first-child {
  margin-top: 0;
  padding-top: 0;
  border-top: 0;
}

.stand-controls .kif-panel {
  margin-top: 8px;
}

.stand-button {
  width: 100%;
  font-weight: 700;
  background: #fffaf2;
  margin-bottom: 8px;
}

.history-buttons {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-bottom: 8px;
}

.history-buttons .stand-button {
  margin-bottom: 0;
}

.stand-button:disabled {
  opacity: 0.45;
  cursor: default;
}

.stand-button.secondary {
  background: #eef6ff;
}

.stand-section {
  border-top: 1px dashed #d7c3a4;
  padding-top: 8px;
  margin-top: 8px;
}

.stand-section h3 {
  margin: 0 0 6px;
  font-size: 0.92rem;
}

.kif-text {
  width: 100%;
  min-height: 84px;
  resize: vertical;
  font: inherit;
  font-size: 0.86rem;
  border: 1px solid #bca178;
  border-radius: 8px;
  background: #fffdf8;
  padding: 8px;
  margin-bottom: 8px;
}

.kif-panel {
  margin-top: 10px;
  border-top: 1px dashed #d7c3a4;
  padding-top: 8px;
}

.kif-panel summary {
  cursor: pointer;
  font-size: 0.86rem;
  color: rgba(47, 36, 24, 0.72);
  list-style: none;
}

.kif-panel summary::-webkit-details-marker {
  display: none;
}

.kif-panel summary::before {
  content: "▸ ";
}

.kif-panel[open] summary::before {
  content: "▾ ";
}

.kif-body {
  margin-top: 8px;
}

.kif-body .stand-button {
  margin-bottom: 0;
}

.stand-list {
  min-height: 24px;
  font-size: 0.95rem;
  line-height: 1.5;
}

.stand-piece {
  display: inline-block;
  margin-right: 8px;
  margin-bottom: 6px;
  font-weight: 700;
  padding: 4px 8px;
  background: #fff;
}

.stand-piece.selected {
  background: #dceaf9;
  border-color: #6d9ecf;
}

.cell {
  border: 1px solid var(--line);
  position: relative;
  padding: 3px;
  user-select: none;
  background: var(--even);
  border-radius: 0;
}

.cell.selected {
  outline: 3px solid #2e70b8;
  outline-offset: -3px;
}

.cell.inspect-target {
  box-shadow: inset 0 0 0 3px rgba(60, 94, 135, 0.85);
}

.piece {
  position: absolute;
  left: 50%;
  top: 48%;
  transform: translate(-50%, -50%);
  width: clamp(30px, 4.4vw, 40px);
  height: clamp(36px, 5.3vw, 49px);
  display: flex;
  align-items: center;
  justify-content: center;
  isolation: isolate;
}

.piece::before,
.piece::after {
  content: "";
  position: absolute;
  clip-path: polygon(50% 0%, 94% 26%, 100% 100%, 0% 100%, 6% 26%);
  pointer-events: none;
}

.piece::before {
  inset: 0;
  background: #8a6948;
}

.piece::after {
  inset: 2px;
  background: #f4dfb6;
  box-shadow:
    inset 0 0 0 1px rgba(255, 255, 255, 0.2),
    0 1px 2px rgba(64, 36, 16, 0.15);
}

.piece.size-s {
  width: clamp(28px, 4.1vw, 37px);
  height: clamp(34px, 4.9vw, 46px);
}

.piece.size-m {
  width: clamp(30px, 4.4vw, 40px);
  height: clamp(36px, 5.3vw, 49px);
}

.piece.size-ml {
  width: clamp(32px, 4.8vw, 43px);
  height: clamp(38px, 5.7vw, 52px);
}

.piece.size-l {
  width: clamp(34px, 5.1vw, 46px);
  height: clamp(40px, 6.1vw, 55px);
}

.piece.attacker-black::after {
  box-shadow:
    inset 0 0 0 2px rgba(32, 120, 92, 0.95),
    0 0 0 2px rgba(32, 120, 92, 0.4),
    0 0 8px rgba(32, 120, 92, 0.5);
}

.piece.attacker-white::after {
  box-shadow:
    inset 0 0 0 2px rgba(178, 76, 66, 0.95),
    0 0 0 2px rgba(178, 76, 66, 0.38),
    0 0 8px rgba(178, 76, 66, 0.48);
}

.board.inspect-active .piece {
  opacity: 0.38;
  filter: saturate(0.55) brightness(0.9);
}

.board.inspect-active .piece.attacker-black,
.board.inspect-active .piece.attacker-white {
  opacity: 1;
  filter: none;
}

.piece.white {
  transform: translate(-50%, -50%) rotate(180deg);
}

.piece.white::after {
  background: #f1d9aa;
}

.piece-face {
  position: relative;
  z-index: 1;
  font-family: "Yu Mincho", "Hiragino Mincho ProN", serif;
  font-size: clamp(15px, 2.1vw, 21px);
  font-weight: 700;
  line-height: 1;
  color: #1d130a;
  transform: translateY(2px);
}

.influence {
  position: absolute;
  right: 3px;
  bottom: 2px;
  font-size: clamp(6px, 0.75vw, 7px);
  font-weight: 500;
  color: rgba(47, 36, 24, 0.55);
  background: rgba(255, 255, 255, 0.32);
  padding: 0 2px;
  border-radius: 4px;
}

.status {
  min-height: 20px;
  color: rgba(47, 36, 24, 0.62);
}

.status p {
  margin: 0;
}

@media (max-width: 680px) {
  .legend-row {
    flex-direction: column;
    align-items: flex-start;
  }

  .play-area {
    flex-direction: column;
  }

  .piece-stand {
    width: 100%;
  }

  .hint {
    width: 100%;
    margin-left: 0;
  }
}
