/* ChesSight — Chess.com 风格深色主题 */
:root {
  --bg: #302E2B;
  --panel: #262421;
  --panel-2: #3C3936;
  --accent: #81B64C;
  --accent-hover: #8FC45F;
  --text: #FFFFFF;
  --text-dim: #989795;
  --sq-light: #EBECD0;
  --sq-dark: #739552;
  --hl-last: rgba(255, 255, 51, 0.5);
  --ctrl-w: rgba(52, 120, 246, 0.42);
  --ctrl-b: rgba(235, 64, 52, 0.42);
  --ctrl-both: rgba(150, 60, 220, 0.52);
  --safe-red: #E02828;
  --safe-green: #2FB344;
  --safe-yellow: #E8A800;
  /* 智能自适应：高度撑满上下沿；同时预留足够左右留白，使左上角 Logo 不与棋盘重叠、
     整块横向居中（右侧栏 620 + 左侧 Logo 留白 + 间距 ≈ 1020）。永不出现滚动。
     大屏由高度封顶（不受此项影响），窄屏则按此收缩棋盘让出 Logo 空间。 */
  --board-size: min(calc(100vh - 24px), calc(100vw - 1020px));
  --sq: calc(var(--board-size) / 8);
}

* { box-sizing: border-box; margin: 0; padding: 0; }
[hidden] { display: none !important; } /* 防止 display:flex 等规则覆盖 hidden 属性 */

body {
  background: var(--bg);
  color: var(--text);
  font-family: 'Inter', system-ui, -apple-system, 'Segoe UI', 'PingFang SC', 'Microsoft YaHei', sans-serif;
  min-height: 100vh;
  touch-action: manipulation; /* 禁用双击缩放（配合 viewport 的 user-scalable=no 锁死移动端缩放） */
  -webkit-text-size-adjust: 100%; /* 旋转/字号自适应时不自动放大文字 */
}

.app {
  display: flex;
  gap: 20px;
  justify-content: center;
  align-items: center; /* 两列面板对齐浏览器横轴中线 */
  min-height: 100vh;
  padding: 12px 16px;
  --sq: calc(var(--board-size) / 8); /* 在 .app 作用域重算，摆棋模式覆盖 --board-size 时同步生效 */
}
/* 摆棋模式：左侧备选框占位，棋盘相应收缩 */
.app.setup { --board-size: min(calc(100vh - 24px), calc(100vw - 786px)); }

/* 左上角品牌标 */
.brand {
  position: fixed;
  top: 12px;
  left: 14px;
  z-index: 50;
  display: flex;
  align-items: center;
  gap: 9px;
  text-decoration: none;
  color: var(--text);
  font-weight: 700;
  font-size: 20px;
  letter-spacing: 0.3px;
  background: rgba(38, 36, 33, 0.88); /* 悬浮药丸底衬：棋盘满高时叠在盘角也清晰可读 */
  backdrop-filter: blur(4px);
  padding: 6px 12px 6px 8px;
  border-radius: 11px;
  box-shadow: 0 3px 10px rgba(0, 0, 0, 0.35);
}
.brand img { width: 27px; height: 27px; display: block; }
.brand .accent { color: var(--accent); }

/* ---------- 棋盘 ---------- */
.board {
  position: relative;
  width: var(--board-size);
  height: var(--board-size);
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  grid-template-rows: repeat(8, 1fr);
  border-radius: 6px;
  overflow: hidden;
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.35);
  user-select: none;
  -webkit-user-select: none;
  flex-shrink: 0;
}

.board { touch-action: none; } /* 拖拽走子时阻止触屏滚动 */

.square { position: relative; cursor: pointer; }
.square.light { background: var(--sq-light); }
.square.dark  { background: var(--sq-dark); }

/* 层叠顺序 = DOM 顺序：底色 → 最后一步 → 攻击覆盖 → 落点提示 → 棋子 → 安全框 → 选中描边 */
.square .ly { position: absolute; inset: 0; pointer-events: none; display: none; }

.square.last .ly.last,
.square.sel  .ly.last { display: block; background: var(--hl-last); }

.square.ctrl-w  .ly.ctrl { display: block; background: var(--ctrl-w); }
.square.ctrl-b  .ly.ctrl { display: block; background: var(--ctrl-b); }
.square.ctrl-wb .ly.ctrl { display: block; background: var(--ctrl-both); }

.square.hint-dot  .ly.hint,
.square.hint-ring .ly.hint { display: block; }
/* 落点提示统一为中心小圆点（不再用环形/灰色填充）；吃子格的圆点抬到棋子之上保持可见 */
.square.hint-dot .ly.hint::before,
.square.hint-ring .ly.hint::before {
  content: '';
  position: absolute;
  inset: 34%;
  border-radius: 50%;
  background: rgba(0, 0, 0, 0.18);
}
.square.hint-ring .ly.hint { z-index: 1; }

.piece {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: none;
  pointer-events: none;
}
.piece.show { display: block; }

.square.safety-attacked   .ly.safety,
.square.safety-defended   .ly.safety,
.square.safety-undefended .ly.safety { display: block; border-radius: 4px; }
.square.safety-attacked   .ly.safety { border: 3px solid var(--safe-red); }
.square.safety-defended   .ly.safety { border: 3px solid var(--safe-green); }
.square.safety-undefended .ly.safety { border: 3px solid var(--safe-yellow); }

/* 选中描边内缩，避免遮盖最外圈 3px 的安全状态框 */
/* ---------- 终局徽章（Chess.com 式叠加图层） ---------- */
.square.badge-mate .ly.badge,
.square.badge-win .ly.badge,
.square.badge-draw .ly.badge { display: block; z-index: 2; }
.square.badge-mate .ly.badge { background: rgba(224, 40, 40, 0.42); } /* 被将杀的王：整格红罩层 */
.ly.badge::after {
  position: absolute;
  top: 3%;
  right: 3%;
  width: 36%;
  height: 36%;
  border-radius: 50%;
  color: #fff;
  font-weight: 800;
  font-size: calc(var(--sq) * 0.24);
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.45);
}
.square.badge-mate .ly.badge::after { content: '#'; background: #E02828; }
.square.badge-win .ly.badge::after { content: '♔'; background: var(--accent); font-size: calc(var(--sq) * 0.28); font-weight: 400; }
.square.badge-draw .ly.badge::after { content: '='; background: #8B8987; }

.square.sel .ly.sel {
  display: block;
  inset: 4px;
  border-radius: 4px;
  box-shadow: inset 0 0 0 calc(var(--sq) * 0.045) rgba(255, 255, 102, 0.9);
}

.coord {
  position: absolute;
  font-size: calc(var(--sq) * 0.24);
  font-weight: 700;
  line-height: 1;
  pointer-events: none;
}
.coord.rank { top: 4%; left: 5%; }
.coord.file { bottom: 4%; right: 6%; }
.square.light .coord { color: var(--sq-dark); }
.square.dark  .coord { color: var(--sq-light); }

/* ---------- 拖拽走子 ---------- */
.drag-ghost {
  position: fixed;
  pointer-events: none;
  z-index: 1000;
  transform: translate(-50%, -50%);
  filter: drop-shadow(0 5px 8px rgba(0, 0, 0, 0.45));
}
.square.drag-origin .piece { opacity: 0.35; }
.square.drag-over { box-shadow: inset 0 0 0 4px rgba(255, 255, 255, 0.8); }

/* ---------- 控制面板 ---------- */
.panel {
  background: var(--panel);
  border-radius: 10px;
  padding: 18px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

/* 右侧栏：整栏高度 = 棋盘高度；上方 [菜单卡 | 解说卡] 贴棋盘顶沿，下方扁长图例卡贴棋盘底沿 */
.side {
  width: 620px;
  height: var(--board-size);
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  gap: 14px;
}
.cards {
  display: flex;
  gap: 20px;
  align-items: flex-start; /* 菜单卡不被解说内容撑高；解说卡高度由 JS 镜像菜单卡 */
}
.menu-panel {
  width: 300px;
  max-height: calc(100vh - 24px);
  overflow-y: auto;
}
/* 解说卡片：只含 AI 实时解说；解说窗撑满整卡高度（= 菜单卡高度） */
.panel-side { width: 300px; overflow: hidden; }
/* 图例卡：扁长方形，与上方两卡整体等宽 */
.legend-card { width: 100%; }

/* ---------- 摆棋备选框 ---------- */
.tray {
  width: 116px;
  background: var(--panel);
  border-radius: 10px;
  padding: 10px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}
.tray-head { font-size: 12px; font-weight: 700; color: var(--text-dim); text-align: center; }
.tray-pieces {
  display: grid;
  grid-auto-flow: column;
  grid-template-rows: repeat(6, auto);
  grid-template-columns: 1fr 1fr;
  gap: 4px;
}
.tray-slot {
  position: relative;
  aspect-ratio: 1;
  border-radius: 6px;
  background: var(--panel-2);
  cursor: grab;
}
.tray-slot img { width: 100%; height: 100%; display: block; pointer-events: none; }
.tray-slot.zero { opacity: 0.45; } /* 无限调色板：计数 0 时半透明但仍可取用 */
.tray-badge {
  position: absolute;
  top: -3px;
  right: -3px;
  min-width: 16px;
  height: 16px;
  border-radius: 8px;
  background: var(--accent);
  color: #fff;
  font-size: 10px;
  font-weight: 700;
  line-height: 16px;
  text-align: center;
  pointer-events: none;
}
.tray-slot.zero .tray-badge { display: none; }
.tray-controls { display: flex; flex-direction: column; gap: 6px; }
.turn-pick { font-size: 12px; color: var(--text-dim); display: flex; align-items: center; gap: 6px; }
.turn-pick select {
  flex: 1;
  background: var(--panel-2);
  color: var(--text);
  border: none;
  border-radius: 6px;
  padding: 5px 6px;
  font: inherit;
  font-size: 12px;
}
.btn-mini { padding: 7px 8px; font-size: 12px; border-radius: 6px; }

.status {
  background: var(--panel-2);
  border-radius: 8px;
  padding: 12px 14px;
  font-size: 15px;
  font-weight: 600;
  min-height: 44px;
  display: flex;
  align-items: center;
}
.status.alert { color: #FFC94D; }

/* 独立的大提示按钮（快捷键 F1） */
.btn-hint-big {
  width: 100%;
  border: none;
  border-radius: 10px;
  background: #F7A600;
  color: #262421;
  font-family: inherit;
  font-size: 16px;
  font-weight: 700;
  padding: 14px 12px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  box-shadow: inset 0 -3px 0 rgba(0, 0, 0, 0.25);
  transition: background 0.12s;
}
.btn-hint-big:hover:not(:disabled) { background: #FFB424; }
.btn-hint-big:disabled { opacity: 0.5; cursor: default; }
.kbd {
  background: rgba(0, 0, 0, 0.18);
  border-radius: 4px;
  padding: 2px 8px;
  font-size: 12px;
  font-weight: 700;
}

/* 持续提示勾选 */
.auto-hint {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  color: var(--text-dim);
  cursor: pointer;
  margin-top: -6px;
}
.auto-hint input { accent-color: var(--accent); width: 15px; height: 15px; cursor: pointer; }

.buttons { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
#btn-setup { grid-column: 1 / -1; }

.btn {
  border: none;
  border-radius: 8px;
  background: var(--panel-2);
  color: var(--text);
  font-family: inherit;
  font-size: 14px;
  font-weight: 600;
  padding: 11px 10px;
  cursor: pointer;
  transition: background 0.12s, box-shadow 0.12s;
}
.btn:hover:not(:disabled) { background: #4A4642; }
.btn:disabled { opacity: 0.35; cursor: default; }

.btn-primary {
  background: var(--accent);
  box-shadow: inset 0 -3px 0 rgba(0, 0, 0, 0.22);
  text-shadow: 0 1px 1px rgba(0, 0, 0, 0.35);
}
.btn-primary:hover:not(:disabled) { background: var(--accent-hover); }

.btn-toggle.active {
  background: var(--accent);
  box-shadow: inset 0 3px 0 rgba(0, 0, 0, 0.25);
  text-shadow: 0 1px 1px rgba(0, 0, 0, 0.35);
}
.btn-toggle.active:hover:not(:disabled) { background: var(--accent-hover); }

/* ---------- SVG 箭头层（X-Ray + 引擎提示） ---------- */
svg.arrows {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
}
svg.arrows line { fill: none; stroke-linecap: round; }
.xray-w { stroke: rgba(52, 120, 246, 0.55); stroke-width: 0.045; stroke-dasharray: 0.16 0.11; }
.xray-b { stroke: rgba(235, 64, 52, 0.55); stroke-width: 0.045; stroke-dasharray: 0.16 0.11; }
.hint-arrow { stroke: #F7A600; stroke-width: 0.16; opacity: 0.92; }

/* ---------- 升变选子弹窗 ---------- */
.promo-picker {
  position: absolute;
  z-index: 40;
  display: flex;
  flex-direction: column;
  background: #F8F8F6;
  border-radius: 6px;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.5);
  overflow: hidden;
}
.promo-picker button {
  border: none;
  background: transparent;
  padding: 0;
  cursor: pointer;
  aspect-ratio: 1;
  width: 100%;
}
.promo-picker button:hover { background: #DDDDD8; }
.promo-picker button img { width: 100%; height: 100%; display: block; }
.promo-picker .promo-cancel {
  aspect-ratio: auto;
  padding: 6px 0;
  font-size: 13px;
  font-weight: 700;
  color: #666;
  background: #E8E8E5;
}

/* ---------- 引擎提示条 ---------- */
.hint {
  background: var(--panel-2);
  border-left: 3px solid #F7A600;
  border-radius: 8px;
  padding: 10px 12px;
  font-size: 14px;
  font-weight: 600;
  line-height: 1.5;
}

/* ---------- 开局库 ---------- */
.openings { display: flex; flex-direction: column; gap: 8px; }
.openings h2 { font-size: 13px; font-weight: 700; }
.openings select {
  width: 100%;
  background: var(--panel-2);
  color: var(--text);
  border: none;
  border-radius: 8px;
  padding: 10px 12px;
  font: inherit;
  font-size: 14px;
  cursor: pointer;
}
.opening-info {
  background: var(--panel-2);
  border-radius: 8px;
  padding: 12px 14px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 13px;
  color: var(--text-dim);
  line-height: 1.7;
}
.opening-info h3 { font-size: 14px; color: var(--text); }
.opening-info strong { color: var(--text); }
.opening-side {
  display: inline-block;
  font-size: 11px;
  font-weight: 700;
  padding: 2px 9px;
  border-radius: 10px;
  margin-left: 6px;
  vertical-align: 1px;
}
.opening-side.w { background: var(--sq-light); color: #3A3733; }
.opening-side.b { background: #3A3733; color: var(--sq-light); border: 1px solid #55524E; }

/* ---------- AI 实时解说 ----------
   解说卡只含解说；解说窗 flex 撑满整卡高度（= 菜单卡高度，由 .cards align-items:stretch 保证），
   条目累积时在窗内向上滚动、卡片大小不变 */
.cmt-head { display: flex; justify-content: space-between; align-items: center; flex: 0 0 auto; }
.cmt-head h2 { font-size: 13px; font-weight: 700; }
.cmt-list {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 9px;
  background: var(--panel-2);
  border-radius: 8px;
  padding: 10px 12px;
}
.cmt-item { font-size: 13px; line-height: 1.65; color: var(--text); }
.cmt-move { color: var(--accent); font-weight: 700; margin-right: 6px; }
.cmt-fail { color: var(--text-dim); }
.cmt-empty { color: var(--text-dim); font-size: 12px; }

/* ---------- 图例（扁长方形卡片：标题 + 六项横向铺开 + 注释；字号缩小一号使卡片更矮） ---------- */
.legend { display: flex; flex-wrap: wrap; align-items: center; gap: 6px 18px; }
.legend h2 { flex: 0 0 100%; font-size: 12px; font-weight: 700; color: var(--text); }
.legend-item {
  flex: 0 0 calc(33.333% - 12px); /* 一行三项 */
  display: flex;
  align-items: center;
  gap: 9px;
  font-size: 12px;
  color: var(--text-dim);
}
.swatch { width: 18px; height: 18px; border-radius: 4px; flex-shrink: 0; }
/* 覆盖色画在深绿格底上，模拟实际叠加效果 */
.swatch.ctrl-w    { background-color: var(--sq-dark); background-image: linear-gradient(var(--ctrl-w), var(--ctrl-w)); }
.swatch.ctrl-b    { background-color: var(--sq-dark); background-image: linear-gradient(var(--ctrl-b), var(--ctrl-b)); }
.swatch.ctrl-both { background-color: var(--sq-dark); background-image: linear-gradient(var(--ctrl-both), var(--ctrl-both)); }
.swatch.safe-red    { background: var(--sq-light); border: 3px solid var(--safe-red); }
.swatch.safe-green  { background: var(--sq-light); border: 3px solid var(--safe-green); }
.swatch.safe-yellow { background: var(--sq-light); border: 3px solid var(--safe-yellow); }
.legend .note { flex: 0 0 100%; font-size: 11px; line-height: 1.5; color: var(--text-dim); margin: 0; }
.legend .note strong { color: var(--text); }

footer {
  font-size: 10px;
  color: var(--text-dim);
  line-height: 1.55;
  border-top: 1px solid #3A3733;
  padding-top: 10px;
}
footer a { color: var(--text-dim); }

/* ---------- 窄屏 / 移动端：Logo 顶部居中入流，上棋盘、下面板 ---------- */
@media (max-width: 1024px) {
  :root { --board-size: min(94vw, 70vh); }
  .app, .app.setup { --board-size: min(94vw, 70vh); flex-direction: column; align-items: center; padding: 12px; min-height: auto; }
  .side { width: var(--board-size); }
  .cards { flex-direction: column; }
  .menu-panel, .legend-card { width: 100%; max-height: none; overflow: visible; }
  /* 解说卡片固定高度、解说窗内滚动；!important 覆盖桌面 JS 设的内联高度 */
  .panel-side { width: 100%; height: 320px !important; overflow: hidden; }
  .legend-item { flex-basis: calc(50% - 10px); } /* 窄屏两列 */
  .brand {
    position: static;
    width: fit-content;
    margin: 12px auto 0;
    font-size: 23px; /* 原 18px 基础上放大 30% */
  }
  .brand img { width: 40px; height: 40px; }
  .tray { width: var(--board-size); }
  .tray-pieces { grid-auto-flow: row; grid-template-rows: auto auto; grid-template-columns: repeat(6, 1fr); }
}
