/* ============ 全局重置 ============ */
* { margin: 0; padding: 0; box-sizing: border-box; }

:root {
  --bg: #f0ece2;
  --board-bg: #e8c885;
  --board-line: #5c3a1e;
  --panel-bg: #ffffff;
  --panel-border: #e0d6c6;
  --text: #2c2416;
  --text-light: #6b5d4f;
  --accent: #c0392b;
  --accent-dark: #962d22;
  --btn-primary: #c0392b;
  --btn-primary-hover: #a93226;
  --btn-sec: #8b7355;
  --btn-sec-hover: #6b5d4f;
  --red-piece: #d9483a;
  --red-piece-dark: #a93226;
  --black-piece: #3a3a3a;
  --black-piece-dark: #1a1a1a;
  --gold: #f39c12;
  --green: #27ae60;
  --shadow: 0 2px 8px rgba(0,0,0,0.12);
  --topbar-h: 50px;
  --statusbar-h: 40px;
}

html, body {
  height: 100%;
  overflow: hidden;
}

body {
  font-family: "Microsoft YaHei", "PingFang SC", "Helvetica Neue", sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-tap-highlight-color: transparent;
  -webkit-touch-callout: none;
  touch-action: manipulation;
}

#app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
}

/* ============ 顶栏 ============ */
#topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 16px;
  background: var(--panel-bg);
  border-bottom: 2px solid var(--panel-border);
  box-shadow: var(--shadow);
  flex-shrink: 0;
  height: var(--topbar-h);
}

.logo {
  display: flex;
  align-items: center;
  gap: 8px;
}

.logo-icon {
  font-size: 24px;
  font-weight: bold;
  color: var(--accent);
  font-family: serif;
}

.logo-text {
  font-size: 17px;
  font-weight: bold;
  color: var(--text);
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

/* ============ 按钮 ============ */
.btn {
  padding: 6px 14px;
  border: none;
  border-radius: 6px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
  background: var(--btn-sec);
  color: #fff;
  white-space: nowrap;
}

.btn-sm {
  padding: 5px 12px;
  font-size: 12px;
}

.btn:hover {
  background: var(--btn-sec-hover);
  transform: translateY(-1px);
}

.btn:active {
  transform: translateY(0);
}

.btn-primary {
  background: var(--btn-primary);
}

.btn-primary:hover {
  background: var(--btn-primary-hover);
}

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

.btn-icon {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border: none;
  border-radius: 6px;
  background: transparent;
  color: var(--text);
  cursor: pointer;
  transition: background 0.2s;
  flex-shrink: 0;
}

.btn-icon:hover {
  background: rgba(0,0,0,0.06);
}

.btn-icon:active {
  background: rgba(0,0,0,0.12);
}

/* ============ 状态栏 ============ */
#statusBar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 16px;
  background: var(--panel-bg);
  border-bottom: 1px solid var(--panel-border);
  flex-shrink: 0;
  height: var(--statusbar-h);
  gap: 8px;
}

.status-left {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

.status-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--red-piece);
  box-shadow: 0 0 6px rgba(217,72,58,0.5);
  flex-shrink: 0;
  transition: background 0.3s;
}

.status-dot.black {
  background: var(--black-piece);
  box-shadow: 0 0 6px rgba(58,58,58,0.5);
}

.status-dot.thinking {
  background: var(--gold);
  box-shadow: 0 0 6px rgba(243,156,18,0.6);
  animation: blink 0.8s ease-in-out infinite;
}

@keyframes blink {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.3; }
}

#statusText {
  font-size: 14px;
  font-weight: 600;
}

.status-right {
  display: flex;
  align-items: center;
  gap: 12px;
  overflow: hidden;
}

.captured-side {
  display: flex;
  align-items: center;
  gap: 4px;
  min-width: 0;
}

.captured-label {
  font-size: 10px;
  color: var(--text-light);
  white-space: nowrap;
  font-weight: 600;
}

.captured-pieces {
  display: flex;
  flex-wrap: wrap;
  gap: 2px;
  min-height: 18px;
  max-width: 120px;
  overflow: hidden;
}

.captured-piece {
  width: 18px;
  height: 18px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 10px;
  font-family: "KaiTi", serif;
  font-weight: bold;
  flex-shrink: 0;
}

.captured-piece.red {
  background: var(--red-piece);
  color: #fff;
}

.captured-piece.black {
  background: var(--black-piece);
  color: #fff;
}

/* ============ 主区域 ============ */
#main {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 8px;
  overflow: hidden;
  min-height: 0;
}

.board-wrap {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
}

/*
 * 棋盘自适应核心：
 * 宽度 = min(可用宽度, 可用高度 * 9/10, 540px)
 * 高度 = 宽度 * 10/9 (aspect-ratio)
 */
#boardContainer {
  position: relative;
  width: min(
    calc(100vw - 16px),
    calc((100dvh - var(--topbar-h) - var(--statusbar-h) - 24px) * 0.9),
    540px
  );
  aspect-ratio: 9 / 10;
  background: var(--board-bg);
  border-radius: 8px;
  box-shadow: 0 4px 20px rgba(0,0,0,0.15), inset 0 0 40px rgba(139,90,43,0.15);
  overflow: hidden;
}

#boardSvg {
  position: absolute;
  top: 0; left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
}

/*
 * 棋子层 & 提示层：内部坐标系固定 540x600，
 * 通过 JS 设置 transform: scale() 缩放到容器实际大小
 */
#piecesLayer {
  position: absolute;
  top: 0; left: 0;
  width: 540px;
  height: 600px;
  transform-origin: 0 0;
  pointer-events: none;
}

#moveHints {
  position: absolute;
  top: 0; left: 0;
  width: 540px;
  height: 600px;
  transform-origin: 0 0;
  pointer-events: none;
}

/* ============ 棋子 ============ */
.piece {
  position: absolute;
  width: 52px;
  height: 52px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 26px;
  font-weight: bold;
  font-family: "KaiTi", "STKaiti", serif;
  cursor: pointer;
  transition: transform 0.3s cubic-bezier(0.4,0,0.2,1), box-shadow 0.2s, opacity 0.3s;
  user-select: none;
  z-index: 2;
  transform: translate(-50%, -50%);
  pointer-events: auto;
}

.piece-red {
  background: radial-gradient(circle at 35% 35%, #e8554a, var(--red-piece) 50%, var(--red-piece-dark));
  color: #fff;
  text-shadow: 0 1px 2px rgba(0,0,0,0.3);
  border: 2px solid var(--red-piece-dark);
  box-shadow: 0 2px 6px rgba(0,0,0,0.3), inset 0 1px 3px rgba(255,255,255,0.3);
}

.piece-black {
  background: radial-gradient(circle at 35% 35%, #5a5a5a, var(--black-piece) 50%, var(--black-piece-dark));
  color: #fff;
  text-shadow: 0 1px 2px rgba(0,0,0,0.3);
  border: 2px solid var(--black-piece-dark);
  box-shadow: 0 2px 6px rgba(0,0,0,0.3), inset 0 1px 3px rgba(255,255,255,0.2);
}

.piece-hidden {
  background: radial-gradient(circle at 35% 35%, #a67c3a, #8b6914 50%, #5c4a1e);
  color: transparent;
  border: 2px solid #5c4a1e;
  box-shadow: 0 2px 6px rgba(0,0,0,0.3), inset 0 1px 3px rgba(255,255,255,0.15);
}

.piece-hidden::after {
  content: "棋";
  color: rgba(255,255,255,0.15);
  font-size: 22px;
  font-family: serif;
}

.piece.selected {
  box-shadow: 0 0 0 3px var(--gold), 0 4px 12px rgba(243,156,18,0.5);
  transform: translate(-50%, -50%) scale(1.1);
  z-index: 5;
}

.piece.last-move {
  box-shadow: 0 0 0 3px rgba(39,174,96,0.6), 0 2px 6px rgba(0,0,0,0.3);
}

.piece.captured {
  opacity: 0;
  transform: translate(-50%, -50%) scale(0.3);
  pointer-events: none;
}

.piece:hover:not(.disabled) {
  filter: brightness(1.1);
}

.piece.disabled {
  cursor: default;
}

.piece.flipping {
  animation: flipAnim 0.4s ease;
}

.piece.flip-pending {
  box-shadow: 0 0 0 3px #3498db, 0 4px 12px rgba(52,152,219,0.5), 0 2px 6px rgba(0,0,0,0.3);
  transform: translate(-50%, -50%) scale(1.08);
  z-index: 5;
  animation: flipPendingPulse 1s ease-in-out infinite;
}

@keyframes flipPendingPulse {
  0%, 100% { box-shadow: 0 0 0 3px #3498db, 0 4px 12px rgba(52,152,219,0.5); }
  50% { box-shadow: 0 0 0 5px #3498db, 0 6px 18px rgba(52,152,219,0.7); }
}

@keyframes flipAnim {
  0% { transform: translate(-50%, -50%) rotateY(0deg) scale(1); }
  50% { transform: translate(-50%, -50%) rotateY(90deg) scale(1.15); }
  100% { transform: translate(-50%, -50%) rotateY(0deg) scale(1); }
}

/* ============ 走法提示 ============ */
.move-hint {
  position: absolute;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: rgba(39,174,96,0.5);
  border: 2px solid rgba(39,174,96,0.8);
  transform: translate(-50%, -50%);
  z-index: 1;
  pointer-events: none;
  animation: pulse 1.2s ease-in-out infinite;
}

.move-hint.capture {
  width: 50px;
  height: 50px;
  background: transparent;
  border: 3px solid rgba(231,76,60,0.7);
  border-radius: 50%;
}

@keyframes pulse {
  0%, 100% { opacity: 0.6; }
  50% { opacity: 1; }
}

.last-move-marker {
  position: absolute;
  width: 56px;
  height: 56px;
  border-radius: 50%;
  background: rgba(243,156,18,0.15);
  border: 2px dashed rgba(243,156,18,0.5);
  transform: translate(-50%, -50%);
  z-index: 0;
  pointer-events: none;
}

/* ============ 抽屉 ============ */
.drawer-overlay {
  position: fixed;
  top: 0; left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0,0,0,0.4);
  z-index: 40;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.3s;
}

.drawer-overlay.show {
  opacity: 1;
  pointer-events: auto;
}

.drawer {
  position: fixed;
  top: 0; right: 0;
  height: 100%;
  width: 320px;
  max-width: 85vw;
  background: var(--bg);
  z-index: 50;
  transform: translateX(100%);
  transition: transform 0.3s ease;
  display: flex;
  flex-direction: column;
  box-shadow: -4px 0 20px rgba(0,0,0,0.15);
}

.drawer.open {
  transform: translateX(0);
}

.drawer-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  background: var(--panel-bg);
  border-bottom: 2px solid var(--panel-border);
  flex-shrink: 0;
}

.drawer-header h3 {
  font-size: 16px;
  color: var(--text);
}

.drawer-body {
  flex: 1;
  overflow-y: auto;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.drawer-body::-webkit-scrollbar {
  width: 5px;
}

.drawer-body::-webkit-scrollbar-thumb {
  background: var(--panel-border);
  border-radius: 3px;
}

/* ============ 面板 ============ */
.panel {
  background: var(--panel-bg);
  border: 1px solid var(--panel-border);
  border-radius: 8px;
  padding: 12px 14px;
  box-shadow: var(--shadow);
}

.panel h3 {
  font-size: 13px;
  color: var(--text);
  margin-bottom: 8px;
  padding-bottom: 6px;
  border-bottom: 1px solid var(--panel-border);
}

/* 设置面板 */
.settings-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 8px;
}

.settings-row:last-child {
  margin-bottom: 0;
}

.settings-row label {
  font-size: 13px;
  color: var(--text-light);
  font-weight: 600;
}

.settings-row select {
  padding: 5px 10px;
  border: 1px solid var(--panel-border);
  border-radius: 5px;
  font-size: 13px;
  background: var(--panel-bg);
  color: var(--text);
  cursor: pointer;
  outline: none;
  transition: border-color 0.2s;
  min-width: 130px;
}

.settings-row select:hover {
  border-color: var(--accent);
}

.settings-row select:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px rgba(192,57,43,0.15);
}

/* 走棋记录 */
.history-panel {
  flex: 1;
  min-height: 150px;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.move-list {
  flex: 1;
  overflow-y: auto;
  font-size: 13px;
  line-height: 1.8;
}

.move-list::-webkit-scrollbar {
  width: 5px;
}

.move-list::-webkit-scrollbar-thumb {
  background: var(--panel-border);
  border-radius: 3px;
}

.move-entry {
  display: flex;
  gap: 8px;
  padding: 2px 4px;
  border-radius: 3px;
}

.move-entry:nth-child(odd) {
  background: rgba(0,0,0,0.02);
}

.move-num {
  color: var(--text-light);
  min-width: 24px;
}

.move-red { color: var(--accent); }
.move-black { color: var(--text); }

/* 规则说明 */
.rules-panel {
  flex-shrink: 0;
}

.rules-text {
  font-size: 12px;
  line-height: 1.7;
  color: var(--text-light);
}

.rules-text p {
  margin-bottom: 4px;
}

.rules-text strong {
  color: var(--text);
}

.rule-highlight {
  color: var(--accent);
  font-weight: bold;
}

/* ============ 弹窗 ============ */
.modal {
  position: fixed;
  top: 0; left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0,0,0,0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  animation: fadeIn 0.3s;
}

.modal.hidden {
  display: none;
}

.modal-content {
  background: var(--panel-bg);
  border-radius: 12px;
  padding: 28px 40px;
  text-align: center;
  box-shadow: 0 8px 40px rgba(0,0,0,0.3);
  animation: slideUp 0.3s;
  max-width: 90vw;
}

.modal-content h2 {
  font-size: 24px;
  margin-bottom: 8px;
}

.modal-content p {
  font-size: 14px;
  color: var(--text-light);
  margin-bottom: 20px;
}

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

@keyframes slideUp {
  from { transform: translateY(30px); opacity: 0; }
  to { transform: translateY(0); opacity: 1; }
}

/* ============ 移动端适配 ============ */
@media (max-width: 480px) {
  .logo-text {
    font-size: 15px;
  }

  .logo-icon {
    font-size: 20px;
  }

  .btn-sm {
    padding: 4px 10px;
    font-size: 11px;
  }

  .btn-icon {
    width: 32px;
    height: 32px;
  }

  #statusBar {
    padding: 4px 10px;
  }

  #statusText {
    font-size: 12px;
  }

  .captured-label {
    font-size: 9px;
  }

  .captured-piece {
    width: 16px;
    height: 16px;
    font-size: 9px;
  }

  .captured-pieces {
    max-width: 80px;
  }

  .modal-content {
    padding: 20px 24px;
  }

  .modal-content h2 {
    font-size: 20px;
  }
}

/* ============ 平板适配 ============ */
@media (min-width: 481px) and (max-width: 768px) {
  .logo-text {
    font-size: 16px;
  }
}

/* ============ 横屏高度受限 ============ */
@media (max-height: 500px) and (orientation: landscape) {
  :root {
    --topbar-h: 40px;
    --statusbar-h: 32px;
  }

  .logo-text {
    font-size: 14px;
  }

  #statusText {
    font-size: 11px;
  }
}
