:root {
  color-scheme: light;
  --border: #d8d5ce;
  --panel: #faf9f6;
  --board: #ead6a8;
  --board-line: #7d6840;
  --first-result: #1468d4;
  --second-result: #d23c37;
  --accent: #775c1f;
  --text: #242424;
  --muted: #6d6d6d;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: transparent;
  color: var(--text);
  font-family: system-ui, -apple-system, "Yu Gothic", "Meiryo", sans-serif;
}

button {
  font: inherit;
}

.dobutsu-app {
  width: min(100%, 980px);
  margin: 0 auto;
  padding: 10px 12px;
}

.topbar {
  display: grid;
  grid-template-columns: auto minmax(180px, 1fr) minmax(230px, auto);
  gap: 8px 14px;
  align-items: center;
  margin-bottom: 9px;
}

.title {
  margin: 0;
  font-size: 20px;
  font-weight: 700;
  white-space: nowrap;
}

.position-status {
  min-width: 0;
  font-size: 13px;
  color: var(--muted);
}

.position-result {
  min-height: 34px;
  display: flex;
  align-items: baseline;
  justify-content: center;
  gap: 9px;
  padding: 6px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--panel);
  font-weight: 700;
  white-space: nowrap;
}

.position-result small {
  color: var(--muted);
  font-weight: 400;
}

.first-result { color: var(--first-result); }
.second-result { color: var(--second-result); }

.workspace {
  display: grid;
  grid-template-columns: minmax(330px, 380px) minmax(0, 1fr);
  gap: 18px;
  align-items: start;
}

.play-panel {
  min-width: 0;
}

.hand-wrap {
  display: grid;
  grid-template-columns: 62px minmax(0, 1fr);
  gap: 7px;
  align-items: center;
  margin: 5px 0;
}

.hand-label {
  font-size: 13px;
  color: var(--muted);
  white-space: nowrap;
}

.hand {
  display: flex;
  flex-direction: row;
  flex-wrap: nowrap;
  gap: 6px;
  min-height: 36px;
  overflow: hidden;
}

.hand-piece {
  flex: 0 1 72px;
  min-width: 54px;
  height: 34px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  border: 1px solid var(--border);
  border-radius: 7px;
  background: #fff;
  font-size: 18px;
  font-weight: 700;
  cursor: pointer;
}

.hand-piece:disabled {
  opacity: .43;
  cursor: default;
}

.hand-piece.selected {
  outline: 3px solid #d3ad49;
}

.hand-glyph {
  display: inline-block;
  line-height: 1;
}

/* 持ち駒は先手・後手とも正立表示。盤上の後手駒だけ180度回転する。 */

.count {
  color: var(--muted);
  font-size: 12px;
  font-weight: 400;
}

.board {
  width: min(100%, 330px);
  aspect-ratio: 3 / 4;
  margin: 7px auto;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(4, 1fr);
  border: 2px solid var(--board-line);
  background: var(--board);
}

.square {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 0;
  border-right: 1px solid var(--board-line);
  border-bottom: 1px solid var(--board-line);
  background: transparent;
  color: #242424;
  cursor: pointer;
}

.square:nth-child(3n) { border-right: 0; }
.square:nth-child(n+10) { border-bottom: 0; }

.square.selected {
  box-shadow: inset 0 0 0 4px #cda438;
}

.square.legal-target::after {
  content: "";
  position: absolute;
  width: 17px;
  height: 17px;
  border-radius: 50%;
  background: rgba(65, 65, 65, .22);
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}

.piece-char {
  position: relative;
  z-index: 1;
  display: inline-block;
  font-size: clamp(28px, 5vw, 38px);
  font-weight: 700;
  line-height: 1;
}

.piece-second .piece-char {
  transform: rotate(180deg);
}

.target-eval {
  position: absolute;
  z-index: 2;
  right: 4px;
  bottom: 3px;
  padding: 1px 4px;
  border-radius: 5px;
  background: rgba(255,255,255,.92);
  font-size: 11px;
  font-weight: 800;
}

.analysis-panel {
  min-width: 0;
}

.analysis-toolbar {
  min-height: 38px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin: 2px 0 7px;
}

.analysis-title {
  margin: 0;
  font-size: 15px;
  white-space: nowrap;
}

.controls {
  display: flex;
  gap: 6px;
  flex-wrap: nowrap;
  justify-content: flex-end;
}

.controls button {
  min-width: 82px;
  padding: 6px 9px;
  border: 1px solid var(--border);
  border-radius: 7px;
  background: #fff;
  cursor: pointer;
  font-size: 12px;
  white-space: nowrap;
}

.controls button:disabled {
  opacity: .45;
  cursor: default;
}

.move-list {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 5px;
  max-height: 394px;
  overflow: auto;
  padding-right: 2px;
}

.move-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 42px 20px;
  gap: 5px;
  align-items: center;
  min-height: 36px;
  padding: 4px 7px;
  border: 1px solid var(--border);
  border-radius: 7px;
  background: #fff;
  text-align: left;
  cursor: pointer;
}

.move-row.best-move {
  border-color: #b68b20;
  background: #fffaf0;
}

.move-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 13px;
}

.eval {
  text-align: right;
  font-weight: 800;
  font-size: 16px;
}

.eval.pending { color: var(--muted); }

.best-mark {
  color: #9b7108;
  text-align: center;
}

.move-placeholder {
  grid-column: 1 / -1;
  padding: 12px;
  color: var(--muted);
  text-align: center;
  border: 1px dashed var(--border);
  border-radius: 7px;
}

.legend {
  margin-top: 8px;
  display: flex;
  gap: 6px 12px;
  flex-wrap: wrap;
  color: var(--muted);
  font-size: 11px;
}

.message {
  min-height: 18px;
  margin-top: 6px;
  color: #a42925;
  font-size: 13px;
}

.rule-note {
  margin: 6px 0 0;
  color: var(--muted);
  font-size: 11px;
  line-height: 1.45;
}

@media (max-width: 680px) {
  .topbar {
    grid-template-columns: 1fr auto;
    gap: 6px 10px;
  }

  .title {
    grid-column: 1 / -1;
  }

  .position-result {
    min-width: 190px;
  }
}

@media (max-width: 620px) {
  .dobutsu-app {
    width: min(100%, 560px);
    padding: 8px;
  }

  .workspace {
    grid-template-columns: 1fr;
    gap: 8px;
  }

  .board {
    width: min(78vw, 300px);
  }

  .move-list {
    max-height: 300px;
  }
}

@media (max-width: 480px) {
  .topbar {
    grid-template-columns: 1fr;
  }

  .title,
  .position-status,
  .position-result {
    grid-column: 1;
  }

  .position-result {
    justify-content: flex-start;
  }

  .analysis-toolbar {
    align-items: flex-start;
  }

  .analysis-title {
    padding-top: 6px;
  }

  .controls {
    gap: 4px;
  }

  .controls button {
    min-width: 72px;
    padding: 6px 7px;
    font-size: 11px;
  }

  .move-list {
    grid-template-columns: 1fr;
  }

  .board {
    width: min(82vw, 288px);
  }
}
