:root {
  --bg: #f4efe6;
  --text: #2b2118;
  --muted: #7a6a58;
  --panel: #fffaf2;
  --panel-border: #d9ccb8;
  --board: #e4bf78;
  --board-line: #5b4020;
  --piece: #f6dfae;
  --piece-edge: #b98a45;
  --piece-text: #1d140c;
  --promoted: #b3261e;
  --select: rgba(30, 110, 230, 0.5);
  --select-ring: #1a5fd0;
  --target: rgba(26, 95, 208, 0.55);
  --last: rgba(214, 110, 20, 0.5);
  --check: #b3261e;
  --active: #1f6f4a;
  --button: #3a2d20;
  --button-text: #fffaf2;
  --danger: #9c2a22;

  --gutter: 16px;
  --rank-w: 16px;
  /* 盤のマスの幅。横幅と縦幅のどちらにも収まる大きさにする。 */
  --cell: min(calc((100vw - 2 * var(--gutter) - var(--rank-w)) / 9),
              calc((100dvh - 200px) / 11.8),
              60px);
  --cell-h: calc(var(--cell) * 1.1);
  /* 読みやすさを優先し、端末に入っているゴシック体を使う。 */
  --piece-font: "Hiragino Sans", "Hiragino Kaku Gothic ProN", "Noto Sans JP", "Noto Sans CJK JP", "Yu Gothic", sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #1c1712;
    --text: #efe6d8;
    --muted: #b3a38d;
    --panel: #2a221a;
    --panel-border: #4a3d2e;
  }
}
:root[data-theme="dark"] {
  --bg: #1c1712;
  --text: #efe6d8;
  --muted: #b3a38d;
  --panel: #2a221a;
  --panel-border: #4a3d2e;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Hiragino Sans", "Noto Sans JP", sans-serif;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  overscroll-behavior: none;
}

#game {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  min-height: 100dvh;
  padding: max(8px, env(safe-area-inset-top)) var(--gutter) max(8px, env(safe-area-inset-bottom));
  justify-content: center;
  user-select: none;
  -webkit-user-select: none;
}

/* 対局者の欄 */
.player {
  width: calc(var(--cell) * 9 + var(--rank-w));
  display: grid;
  grid-template-columns: 1fr auto auto;
  align-items: center;
  gap: 8px;
  padding: 6px 10px;
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: 10px;
}
.player.to-move {
  border-color: var(--active);
  box-shadow: 0 0 0 2px var(--active) inset;
}
.player-info {
  display: flex;
  align-items: baseline;
  gap: 6px;
  min-width: 0;
  white-space: nowrap;
}
.side-mark { font-weight: 700; font-size: 15px; }
.player-name { color: var(--muted); font-size: 13px; overflow: hidden; text-overflow: ellipsis; }
.player-check {
  color: var(--check);
  font-weight: 700;
  font-size: 13px;
}

.clock {
  display: flex;
  align-items: baseline;
  gap: 4px;
  font-variant-numeric: tabular-nums;
}
.clock-label { font-size: 11px; color: var(--muted); }
.clock-time { font-size: 20px; font-weight: 700; min-width: 3.2em; text-align: right; }
.clock.byoyomi .clock-time { color: var(--danger); }
.clock.urgent .clock-time { color: var(--danger); animation: blink 1s steps(2, start) infinite; }
@keyframes blink { to { opacity: 0.35; } }

.player-actions { display: flex; gap: 6px; }
button {
  font: inherit;
  font-size: 14px;
  font-weight: 600;
  border: 0;
  border-radius: 8px;
  padding: 8px 12px;
  min-height: 36px;
  background: var(--button);
  color: var(--button-text);
  cursor: pointer;
}
button.resign { background: transparent; color: var(--danger); border: 1px solid var(--danger); }
button.declare { background: var(--active); }
button[hidden] { display: none; }
button:disabled { opacity: 0.4; cursor: default; }

/* 持ち駒 */
.hand {
  width: calc(var(--cell) * 9 + var(--rank-w));
  min-height: calc(var(--cell-h) * 0.95);
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 2px 4px;
  padding: 2px 6px;
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: 8px;
}
.hand.top { flex-direction: row-reverse; }
.hand-item {
  position: relative;
  width: calc(var(--cell) * 0.95);
  height: calc(var(--cell-h) * 0.9);
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 6px;
}
.hand-item.selected { background: var(--select); box-shadow: 0 0 0 3px var(--select-ring) inset; }
.hand-count {
  position: absolute;
  right: -2px;
  bottom: -2px;
  min-width: 16px;
  padding: 0 3px;
  font-size: 11px;
  font-weight: 700;
  line-height: 16px;
  text-align: center;
  color: var(--button-text);
  background: var(--button);
  border-radius: 8px;
}
.hand.top .hand-count { top: -2px; bottom: auto; left: -2px; right: auto; }

/* 盤 */
.board-wrap { display: flex; flex-direction: column; align-items: flex-start; }
.board-row { display: flex; }
.coords { color: var(--muted); font-size: 11px; }
.coords.files {
  display: grid;
  grid-template-columns: repeat(9, var(--cell));
  text-align: center;
  line-height: 14px;
}
.coords.ranks {
  display: grid;
  grid-template-rows: repeat(9, var(--cell-h));
  width: var(--rank-w);
  align-items: center;
  text-align: center;
}
.board {
  display: grid;
  grid-template-columns: repeat(9, var(--cell));
  grid-template-rows: repeat(9, var(--cell-h));
  background: var(--board);
  border: 2px solid var(--board-line);
  box-sizing: content-box;
}
.cell {
  position: relative;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  border-right: 1px solid var(--board-line);
  border-bottom: 1px solid var(--board-line);
}
.cell:nth-child(9n) { border-right: 0; }
.cell:nth-child(n+73) { border-bottom: 0; }
.cell.last { background: var(--last); }
.cell.selected { background: var(--select); box-shadow: 0 0 0 3px var(--select-ring) inset; }
.cell.target::after {
  content: "";
  position: absolute;
  width: 40%;
  height: 36%;
  border-radius: 50%;
  background: var(--target);
  pointer-events: none;
}
.cell.target.has-piece::after {
  width: 92%;
  height: 92%;
  border-radius: 4px;
  background: transparent;
  box-shadow: 0 0 0 3px var(--target) inset;
}
/* 星（4 か所の点） */
.cell.star::before {
  content: "";
  position: absolute;
  right: -3px;
  bottom: -3px;
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background: var(--board-line);
  z-index: 1;
}

/* 駒 */
.piece {
  width: 88%;
  height: 92%;
  display: flex;
  align-items: center;
  justify-content: center;
  padding-top: 6%;
  background: linear-gradient(170deg, #fbe9c2, var(--piece) 55%, #e9c98b);
  clip-path: polygon(50% 0, 88% 16%, 100% 100%, 0 100%, 12% 16%);
  filter: drop-shadow(0 1px 0 var(--piece-edge));
  color: var(--piece-text);
  font-family: var(--piece-font);
  font-size: calc(var(--cell) * 0.56);
  line-height: 1;
  pointer-events: none;
}
.piece span { display: inline-block; font-weight: 700; }
.piece.promoted { color: var(--promoted); }
.piece.gote { transform: rotate(180deg); }
.piece.king { font-size: calc(var(--cell) * 0.64); }

.message {
  min-height: 1.4em;
  margin: 2px 0 0;
  font-size: 14px;
  color: var(--muted);
  text-align: center;
}

/* 確認の画面 */
.modal {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: var(--gutter);
  background: rgba(0, 0, 0, 0.45);
  z-index: 10;
}
.modal[hidden] { display: none; }
.modal-box {
  width: min(340px, 100%);
  padding: 20px;
  background: var(--panel);
  color: var(--text);
  border-radius: 14px;
  text-align: center;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
}
.modal-title { margin: 0 0 8px; font-size: 18px; font-weight: 700; }
.modal-text { margin: 0 0 16px; font-size: 14px; line-height: 1.6; color: var(--muted); white-space: pre-line; }
.modal-text:empty { display: none; }
.modal-buttons { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; }
.modal-buttons button { min-width: 110px; min-height: 48px; font-size: 16px; }
.modal-buttons button.secondary { background: transparent; color: var(--text); border: 1px solid var(--panel-border); }
.modal-buttons .choice-piece {
  display: inline-block;
  margin-left: 6px;
  font-family: var(--piece-font);
  font-size: 20px;
  font-weight: 700;
}

/* 幅の狭いスマホでは、対局者の名前を印の下の行に回す。 */
@media (max-width: 374px) {
  .player { gap: 6px; padding: 5px 8px; }
  .player-info { flex-direction: column; align-items: flex-start; gap: 0; }
  .player-name { font-size: 11px; }
  button { padding: 8px 10px; }
}
