:root {
  --board-bg: #d9b585;
  --board-bg-dark: #b8915d;
  --line: #4a2a14;
  --red: #b22222;
  --black: #1a1a1a;
  --piece-bg: #f1d8a8;
  --piece-bg-dark: #c8a472;
  --select: #ffd700;
  --hint: rgba(50, 180, 80, 0.7);
  --capture: rgba(220, 50, 50, 0.7);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  font-family: 'Noto Serif TC', 'Songti TC', '宋體', serif;
  background:
    radial-gradient(ellipse at top, #3a2517 0%, #1a0e08 100%);
  color: #e8d5b0;
  min-height: 100vh;
}

.app {
  max-width: 720px;
  margin: 0 auto;
  padding: 16px;
}

header {
  text-align: center;
  margin-bottom: 16px;
}

h1 {
  font-family: 'Ma Shan Zheng', 'Noto Serif TC', cursive;
  font-size: 2.4rem;
  margin: 0 0 4px 0;
  color: #f0d9a8;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.5);
  letter-spacing: 0.2em;
}

.subtitle {
  font-size: 0.9rem;
  opacity: 0.7;
}

.controls {
  display: flex;
  justify-content: center;
  gap: 12px;
  margin-bottom: 12px;
  flex-wrap: wrap;
}

.controls button {
  font-family: inherit;
  background: linear-gradient(180deg, #6b3f22, #432615);
  color: #f0d9a8;
  border: 1px solid #2a1810;
  padding: 8px 18px;
  border-radius: 4px;
  font-size: 0.95rem;
  cursor: pointer;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.4), inset 0 1px 0 rgba(255, 255, 255, 0.1);
  transition: transform 0.1s;
}
.controls button:hover { transform: translateY(-1px); }
.controls button:active { transform: translateY(1px); }

.status-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
  font-size: 1rem;
  padding: 0 8px;
}
#turn-indicator.red-turn { color: #ff8a8a; font-weight: bold; }
#turn-indicator.black-turn { color: #b0b0b0; font-weight: bold; }
#status { color: #ffd373; font-weight: bold; }

.board-wrapper {
  position: relative;
  width: 100%;
  max-width: 540px;
  margin: 0 auto;
  aspect-ratio: 9 / 10;
  padding: 4% 6%;
  background:
    repeating-linear-gradient(
      45deg,
      transparent 0,
      transparent 8px,
      rgba(110, 70, 30, 0.04) 8px,
      rgba(110, 70, 30, 0.04) 16px
    ),
    radial-gradient(ellipse at 30% 20%, #e9c896 0%, #c89a64 60%, #a07440 100%);
  border-radius: 8px;
  box-shadow:
    0 8px 24px rgba(0, 0, 0, 0.6),
    inset 0 0 32px rgba(80, 40, 20, 0.4),
    inset 0 0 0 2px rgba(60, 30, 10, 0.3);
}

#board {
  position: relative;
  width: 100%;
  height: 100%;
  background-image:
    linear-gradient(to right, var(--line) 1px, transparent 1px),
    linear-gradient(to bottom, var(--line) 1px, transparent 1px);
  background-size: calc(100% / 8) calc(100% / 9);
  background-position: 0 0;
  /* 河界中間清空 */
}

/* 在河界上下加邊框模擬棋盤外框 */
#board::before, #board::after {
  content: '';
  position: absolute;
  left: 0;
  right: 0;
  height: 1px;
  background: var(--line);
}
#board::before { top: 0; }
#board::after { bottom: 0; }

.river {
  position: absolute;
  left: 0;
  right: 0;
  top: calc(100% * 4 / 9);
  height: calc(100% / 9);
  display: flex;
  justify-content: space-around;
  align-items: center;
  font-family: 'Ma Shan Zheng', 'Noto Serif TC', cursive;
  font-size: clamp(1rem, 4vw, 1.6rem);
  color: rgba(74, 42, 20, 0.6);
  letter-spacing: 0.4em;
  font-style: italic;
  pointer-events: none;
  background:
    linear-gradient(to bottom,
      transparent 0%,
      var(--board-bg) 4%,
      var(--board-bg) 96%,
      transparent 100%);
  z-index: 1;
}

.cell {
  position: absolute;
  width: calc(100% / 8);
  height: calc(100% / 9);
  transform: translate(-50%, -50%);
  cursor: pointer;
  z-index: 2;
}

.piece {
  position: absolute;
  width: 11%;
  aspect-ratio: 1;
  transform: translate(-50%, -50%);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: 'Noto Serif TC', 'Songti TC', serif;
  font-weight: 900;
  font-size: clamp(1.2rem, 4.5vw, 2rem);
  cursor: pointer;
  z-index: 5;
  background:
    radial-gradient(circle at 30% 30%, var(--piece-bg) 0%, var(--piece-bg-dark) 80%, #8b6a3d 100%);
  box-shadow:
    inset 0 -2px 4px rgba(0, 0, 0, 0.3),
    inset 0 2px 3px rgba(255, 255, 255, 0.4),
    0 3px 6px rgba(0, 0, 0, 0.5),
    0 0 0 2px rgba(60, 30, 10, 0.5);
  transition: left 0.25s ease-out, top 0.25s ease-out, transform 0.15s, box-shadow 0.15s;
  user-select: none;
}

.piece.red { color: var(--red); text-shadow: 0 1px 1px rgba(0, 0, 0, 0.2); }
.piece.black { color: var(--black); text-shadow: 0 1px 1px rgba(255, 255, 255, 0.2); }

.piece:hover {
  transform: translate(-50%, -50%) scale(1.05);
}

.piece.selected {
  box-shadow:
    inset 0 -2px 4px rgba(0, 0, 0, 0.3),
    inset 0 2px 3px rgba(255, 255, 255, 0.4),
    0 0 12px 4px var(--select),
    0 0 0 2px rgba(60, 30, 10, 0.5);
  animation: glow 1.2s ease-in-out infinite alternate;
}

@keyframes glow {
  from { box-shadow: inset 0 -2px 4px rgba(0,0,0,0.3), inset 0 2px 3px rgba(255,255,255,0.4), 0 0 8px 2px var(--select); }
  to   { box-shadow: inset 0 -2px 4px rgba(0,0,0,0.3), inset 0 2px 3px rgba(255,255,255,0.4), 0 0 16px 6px var(--select); }
}

.piece.shake {
  animation: shake 0.4s;
}
@keyframes shake {
  0%, 100% { transform: translate(-50%, -50%); }
  20% { transform: translate(-58%, -50%); }
  40% { transform: translate(-42%, -50%); background: radial-gradient(circle at 30% 30%, #ffaaaa, #cc6666); }
  60% { transform: translate(-58%, -50%); }
  80% { transform: translate(-42%, -50%); }
}

.hint {
  position: absolute;
  width: 4%;
  aspect-ratio: 1;
  transform: translate(-50%, -50%);
  background: var(--hint);
  border-radius: 50%;
  z-index: 4;
  cursor: pointer;
  pointer-events: auto;
  animation: pulse 1s ease-in-out infinite alternate;
}
.hint.capture {
  background: transparent;
  border: 3px solid var(--capture);
  width: 11%;
  border-radius: 50%;
}
@keyframes pulse {
  from { opacity: 0.5; }
  to { opacity: 1; }
}

.last-move {
  position: absolute;
  width: 11%;
  aspect-ratio: 1;
  transform: translate(-50%, -50%);
  background: rgba(255, 215, 0, 0.18);
  border-radius: 6px;
  z-index: 3;
  pointer-events: none;
}

/* 九宮斜線 */
.board-wrapper svg.diag {
  position: absolute;
  inset: 4% 6%;
  pointer-events: none;
  z-index: 1;
}

.spinner {
  position: absolute;
  inset: 0;
  display: none;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, 0.3);
  z-index: 10;
  border-radius: 8px;
  font-family: 'Ma Shan Zheng', cursive;
  color: #ffd373;
  font-size: 1.2rem;
}
.spinner-inner {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
}
.hourglass {
  width: 32px;
  height: 32px;
  border: 3px solid #ffd373;
  border-top-color: transparent;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin {
  to { transform: rotate(360deg); }
}

.log-section {
  margin-top: 16px;
  background: rgba(20, 12, 8, 0.5);
  border: 1px solid #432615;
  border-radius: 6px;
  padding: 12px;
  max-height: 180px;
  overflow-y: auto;
}
.log-section h3 {
  margin: 0 0 8px 0;
  font-size: 0.95rem;
  color: #d4b58a;
}
#move-log {
  list-style: none;
  padding: 0;
  margin: 0;
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 4px 12px;
  font-size: 0.9rem;
}
.log-red { color: #ff9a9a; }
.log-black { color: #c8c8c8; }

footer {
  text-align: center;
  margin-top: 16px;
  font-size: 0.8rem;
  opacity: 0.5;
}

@media (max-width: 480px) {
  h1 { font-size: 1.8rem; }
  .controls button { padding: 6px 12px; font-size: 0.85rem; }
  .status-bar { font-size: 0.85rem; }
}
