* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", sans-serif;
  background: linear-gradient(135deg, #0f172a 0%, #1e293b 100%);
  color: #e2e8f0;
  min-height: 100vh;
}

.app {
  max-width: 1200px;
  margin: 0 auto;
  padding: 24px;
}

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

h1 {
  margin: 0 0 8px;
  font-size: 28px;
  background: linear-gradient(135deg, #60a5fa, #a78bfa);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}

.subtitle {
  color: #94a3b8;
  font-size: 14px;
}

main {
  display: grid;
  grid-template-columns: 1fr 380px;
  gap: 24px;
  margin-bottom: 24px;
}

.board-section, .side-panel > div {
  background: rgba(15, 23, 42, 0.6);
  border: 1px solid rgba(99, 102, 241, 0.2);
  border-radius: 12px;
  padding: 20px;
}

.board {
  position: relative;
  margin: 0 auto 16px;
  width: 100%;
  max-width: 470px;
  /* Keep the container at the source image's exact aspect ratio so the
     stretched background and the measured point coordinates stay in sync. */
  aspect-ratio: 769 / 1024;
  background-image: url('/static/board-bg.jpg');
  background-size: 100% 100%;
  background-position: center;
  background-repeat: no-repeat;
  border-radius: 6px;
  box-shadow: 0 0 0 2px rgba(239, 68, 68, 0.5);
}

/* Cells are positioned absolutely (left/top set inline from the measured
   image coordinates in game.js) and stay transparent so the board image's
   own squares, camps and rail lines show through. */
.cell {
  position: absolute;
  transform: translate(-50%, -50%);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  border-radius: 4px;
  user-select: none;
  transition: background 0.15s, box-shadow 0.15s;
}

.cell:hover {
  background: rgba(148, 163, 184, 0.18);
  box-shadow: 0 0 0 1.5px rgba(148, 163, 184, 0.5);
}

.cell.own.selected {
  background: rgba(59, 130, 246, 0.3);
  box-shadow: 0 0 0 2px #3b82f6, 0 0 12px rgba(59, 130, 246, 0.5);
}

.cell.movable {
  background: rgba(34, 197, 94, 0.25);
  box-shadow: 0 0 0 2px rgba(34, 197, 94, 0.8), 0 0 8px rgba(34, 197, 94, 0.3);
}

.cell.history {
  /* Deliberately left visually "empty" — this square has no piece on it.
     The history-tag is a tiny corner note, not a piece render. */
  cursor: default;
}

.cell.setup-candidate {
  background: rgba(251, 191, 36, 0.15);
  box-shadow: 0 0 0 1.5px rgba(251, 191, 36, 0.4);
}

.cell.setup-invalid {
  background: rgba(239, 68, 68, 0.15);
  box-shadow: 0 0 0 1.5px rgba(239, 68, 68, 0.4);
}

/* Pieces are horizontal rectangular tiles, like real 军棋 pieces. */
.piece-badge {
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: center;
  gap: 3px;
  width: 96%;
  height: 86%;
  border-radius: 3px;
  font-weight: 700;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.6),
              inset 0 1px 0 rgba(255, 255, 255, 0.28);
}

.piece-badge.badge-own {
  background: linear-gradient(180deg, #4f8ff7, #1d4ed8);
  border: 1px solid #93c5fd;
}

.piece-badge.badge-enemy {
  background: linear-gradient(180deg, #ef5350, #8e1414);
  border: 1px solid #fca5a5;
}

.cell.revealed .piece-badge {
  background: linear-gradient(180deg, #a855f7, #6d28d9);
  border-color: #d8b4fe;
}

/* Face-down enemy tile: plain tile back, no text. */
.piece-badge.face-down::after {
  content: "";
  width: 62%;
  height: 55%;
  border: 1px solid rgba(255, 255, 255, 0.35);
  border-radius: 2px;
}

.cell .piece-icon {
  font-size: 12px;
  line-height: 1;
}

.cell .piece-type {
  font-size: 12px;
  color: #f8fafc;
  text-shadow: 0 1px 1px rgba(0, 0, 0, 0.7);
  letter-spacing: 0.5px;
  white-space: nowrap;
}

.ctl-sel {
  background: #1e293b;
  color: #f8fafc;
  border: 1px solid #334155;
  border-radius: 4px;
  padding: 2px 4px;
  font-size: 12px;
}

/* Deduction markers (">团长", "同级/雷/弹", "非地雷") can be longer than a
   rank name — shrink them so they stay inside the badge. */
.cell.revealed .piece-type {
  font-size: 10px;
  letter-spacing: 0;
}

.cell .history-tag {
  position: absolute;
  bottom: 1px;
  right: 2px;
  font-size: 6px;
  line-height: 1;
  color: #94a3b8;
  opacity: 0.55;
  pointer-events: none;
}

.exp-stats {
  font-size: 11px;
  color: #94a3b8;
  padding: 8px 0;
  border-top: 1px solid rgba(99, 102, 241, 0.15);
  margin-top: 8px;
}

.exp-stats b { color: #c4b5fd; }

.legend {
  display: flex;
  gap: 12px;
  align-items: center;
  font-size: 12px;
  color: #94a3b8;
  flex-wrap: wrap;
}

.leg {
  padding: 4px 10px;
  border-radius: 6px;
  border: 1px solid;
}

.leg.own { color: #60a5fa; border-color: rgba(59, 130, 246, 0.4); }
.leg.enemy { color: #f87171; border-color: rgba(239, 68, 68, 0.4); }
.leg.revealed { color: #c084fc; border-color: rgba(168, 85, 247, 0.4); }
.leg.movable { color: #4ade80; border-color: rgba(34, 197, 94, 0.4); }
.leg.history { color: #94a3b8; border-color: rgba(100, 116, 139, 0.5); border-style: dashed; }

.reset-btn {
  margin-left: auto;
  padding: 6px 14px;
  background: rgba(99, 102, 241, 0.2);
  border: 1px solid rgba(99, 102, 241, 0.4);
  border-radius: 6px;
  color: #a5b4fc;
  cursor: pointer;
  font-size: 12px;
}

.reset-btn:hover { background: rgba(99, 102, 241, 0.4); }

.status {
  margin-top: 12px;
  padding: 10px 14px;
  background: rgba(15, 23, 42, 0.6);
  border-radius: 6px;
  font-size: 13px;
  text-align: center;
  color: #cbd5e1;
  min-height: 36px;
}

/* Side panel */
.side-panel > div {
  margin-bottom: 12px;
}

.side-panel h2 {
  margin: 0 0 12px;
  font-size: 16px;
  color: #c4b5fd;
}

.side-panel h3 {
  margin: 12px 0 8px;
  font-size: 13px;
  color: #94a3b8;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.hint {
  font-size: 11px;
  color: #94a3b8;
  line-height: 1.5;
  margin: 0 0 10px;
}

/* Preset formations */
.formations {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-bottom: 14px;
}

.formation-item {
  padding: 8px 10px;
  background: rgba(99, 102, 241, 0.1);
  border: 1px solid rgba(99, 102, 241, 0.3);
  border-radius: 8px;
  cursor: pointer;
  font-size: 12px;
}

.formation-item:hover {
  background: rgba(99, 102, 241, 0.25);
}

.formation-item.selected {
  background: rgba(74, 222, 128, 0.15);
  border-color: rgba(74, 222, 128, 0.6);
}

.formation-item .fname {
  font-weight: 700;
  color: #c4b5fd;
}

.formation-item .fdesc {
  color: #94a3b8;
  font-size: 11px;
  margin-top: 2px;
}

.formation-item.custom {
  border-color: rgba(251, 191, 36, 0.4);
  background: rgba(251, 191, 36, 0.08);
}

.formation-item.custom .fname {
  color: #fcd34d;
}

.save-formation-row {
  display: flex;
  gap: 8px;
  margin-bottom: 6px;
}

.save-formation-row input {
  flex: 1;
  min-width: 0;
  padding: 7px 10px;
  border-radius: 8px;
  border: 1px solid rgba(148, 163, 184, 0.35);
  background: rgba(15, 23, 42, 0.6);
  color: #e2e8f0;
  font-size: 12px;
}

.save-btn {
  padding: 7px 12px;
  border-radius: 8px;
  border: 1px solid rgba(251, 191, 36, 0.5);
  background: rgba(251, 191, 36, 0.15);
  color: #fcd34d;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
}

.save-btn:hover {
  background: rgba(251, 191, 36, 0.28);
}

.save-formation-status {
  font-size: 11px;
  color: #94a3b8;
  min-height: 14px;
  margin-bottom: 10px;
}

.save-formation-status.ok { color: #4ade80; }
.save-formation-status.err { color: #f87171; }

/* Setup pool */
.pool {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 12px;
}

.pool-item {
  padding: 6px 10px;
  background: rgba(99, 102, 241, 0.15);
  border: 1px solid rgba(99, 102, 241, 0.4);
  border-radius: 6px;
  font-size: 12px;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 4px;
}

.pool-item:hover {
  background: rgba(99, 102, 241, 0.3);
}

.pool-item.selected {
  background: rgba(251, 191, 36, 0.25);
  border-color: rgba(251, 191, 36, 0.6);
  box-shadow: 0 0 8px rgba(251, 191, 36, 0.3);
}

.pool-item.empty {
  opacity: 0.3;
  cursor: not-allowed;
}

.setup-stats {
  font-size: 11px;
  color: #94a3b8;
  padding: 6px 0;
  border-top: 1px solid rgba(99, 102, 241, 0.15);
  margin: 8px 0;
}

.start-btn {
  width: 100%;
  padding: 10px;
  background: linear-gradient(135deg, #6366f1, #a78bfa);
  border: none;
  border-radius: 8px;
  color: white;
  cursor: pointer;
  font-size: 14px;
  font-weight: 600;
}

.start-btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.start-btn:not(:disabled):hover {
  filter: brightness(1.1);
}

/* AI panel */
.ai-empty {
  color: #64748b;
  font-size: 13px;
  text-align: center;
  padding: 40px 0;
}

.thinking-block {
  margin-bottom: 14px;
}

.thinking-block h4 {
  margin: 0 0 6px;
  font-size: 12px;
  color: #94a3b8;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.bar-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 4px 0;
  font-size: 12px;
}

.bar-label {
  flex: 0 0 50%;
  text-align: right;
  color: #cbd5e1;
  font-size: 11px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.bar-track {
  flex: 1;
  height: 8px;
  background: rgba(30, 41, 59, 0.8);
  border-radius: 4px;
  overflow: hidden;
}

.bar-fill {
  height: 100%;
  background: linear-gradient(90deg, #6366f1, #a78bfa);
  transition: width 0.3s;
}

.bar-fill.attack { background: linear-gradient(90deg, #ef4444, #f59e0b); }
.bar-fill.move { background: linear-gradient(90deg, #3b82f6, #06b6d4); }

.bar-pct {
  flex: 0 0 40px;
  font-size: 11px;
  color: #94a3b8;
  text-align: right;
}

.confidence-row {
  display: flex;
  justify-content: space-between;
  padding: 6px 0;
  font-size: 12px;
  border-top: 1px solid rgba(99, 102, 241, 0.15);
  margin-top: 8px;
}

.confidence-row span:first-child { color: #94a3b8; }
.confidence-row span:last-child { color: #c4b5fd; font-weight: 600; }

/* Jev request/response viewer in the side panel */
.jev-io {
  max-height: 340px;
  overflow: auto;
  background: rgba(2, 6, 23, 0.85);
  border: 1px solid rgba(99, 102, 241, 0.25);
  border-radius: 8px;
  padding: 10px;
  font-size: 10px;
  line-height: 1.45;
  color: #7dd3fc;
  white-space: pre-wrap;
  word-break: break-all;
  margin: 0;
}

/* Replay bar under the board */
.replay-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 10px;
  padding: 8px 10px;
  background: rgba(168, 85, 247, 0.08);
  border: 1px solid rgba(168, 85, 247, 0.35);
  border-radius: 8px;
}

.replay-bar input[type="range"] {
  flex: 1;
  accent-color: #a855f7;
}

.replay-btn {
  padding: 5px 10px;
  border-radius: 6px;
  border: 1px solid rgba(168, 85, 247, 0.5);
  background: rgba(168, 85, 247, 0.15);
  color: #d8b4fe;
  font-size: 11px;
  cursor: pointer;
  white-space: nowrap;
}

.replay-btn:hover { background: rgba(168, 85, 247, 0.3); }
.replay-btn.exit { border-color: rgba(148, 163, 184, 0.4); color: #94a3b8; }

.replay-label {
  font-size: 11px;
  color: #c4b5fd;
  min-width: 120px;
}

/* Battle log — deliberately narrower than the main row */
.bottom-row {
  max-width: 620px;
}

.log-section {
  background: rgba(15, 23, 42, 0.6);
  border: 1px solid rgba(99, 102, 241, 0.2);
  border-radius: 12px;
  padding: 20px;
}

.log-section h3 {
  margin: 0 0 12px;
  font-size: 14px;
  color: #c4b5fd;
}

.log {
  list-style: none;
  padding: 0;
  margin: 0;
  max-height: 200px;
  overflow-y: auto;
  font-size: 12px;
  color: #94a3b8;
}

.log li {
  padding: 4px 0;
  border-bottom: 1px solid rgba(99, 102, 241, 0.1);
}

.log .badge-player { color: #60a5fa; }
.log .badge-ai { color: #f87171; }
.log .badge-combat { color: #fbbf24; font-weight: 600; }
.log .badge-flag { color: #f0abfc; font-weight: 600; }
.log .badge-winner { color: #4ade80; font-weight: 700; }

.winner-banner {
  margin-top: 12px;
  padding: 16px;
  background: linear-gradient(135deg, #4ade80, #06b6d4);
  color: #0f172a;
  border-radius: 8px;
  font-size: 16px;
  font-weight: 700;
  text-align: center;
}

.winner-banner.player { background: linear-gradient(135deg, #4ade80, #06b6d4); }
.winner-banner.ai { background: linear-gradient(135deg, #f87171, #f59e0b); }

/* Move / combat animation */
.cell.move-src {
  background: rgba(251, 191, 36, 0.35);
  box-shadow: 0 0 0 2.5px #fbbf24, 0 0 16px rgba(251, 191, 36, 0.8);
  z-index: 5;
}

.cell.move-dst {
  background: rgba(74, 222, 128, 0.32);
  box-shadow: 0 0 0 2.5px #4ade80, 0 0 16px rgba(74, 222, 128, 0.75);
  z-index: 5;
}

.move-ghost {
  position: fixed;
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 19px;
  pointer-events: none;
  transition: transform 0.4s cubic-bezier(0.3, 0, 0.2, 1);
  background: rgba(251, 191, 36, 0.3);
  border: 2px solid rgba(251, 191, 36, 0.95);
  border-radius: 4px;
  box-shadow: 0 0 14px rgba(251, 191, 36, 0.8), 0 4px 10px rgba(0, 0, 0, 0.5);
}

.cell.combat-flash {
  animation: combat-shake 0.25s ease-in-out;
  box-shadow: 0 0 14px 4px rgba(248, 113, 113, 0.85);
}

.combat-boom {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  pointer-events: none;
  animation: combat-pop 0.26s ease-out;
}

@keyframes combat-shake {
  0%, 100% { transform: translate(-50%, -50%); }
  25% { transform: translate(calc(-50% - 2px), calc(-50% + 1px)); }
  50% { transform: translate(calc(-50% + 2px), calc(-50% - 1px)); }
  75% { transform: translate(calc(-50% - 1px), calc(-50% - 1px)); }
}

@keyframes combat-pop {
  0% { transform: scale(0.4); opacity: 0; }
  50% { transform: scale(1.3); opacity: 1; }
  100% { transform: scale(1); opacity: 0.9; }
}