:root {
  --x-color: #2563eb;
  --o-color: #dc2626;
  --accent: #f59e0b;
  --accent-soft: #fef3c7;
  --bg: #eef2f7;
  --panel: #ffffff;
  --text: #1f2937;
  --muted: #6b7280;
  --line: #e5e7eb;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  font-family: "PingFang SC", "Microsoft YaHei", "Noto Sans SC", -apple-system,
    BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.app-header {
  text-align: center;
  padding: 22px 16px 14px;
  background: linear-gradient(135deg, #4f46e5, #7c3aed);
  color: #fff;
  box-shadow: 0 2px 10px rgba(76, 29, 149, 0.25);
}

.app-title h1 {
  font-size: 1.6rem;
  font-weight: 800;
  letter-spacing: 1px;
}

.app-title .en {
  font-size: 0.85rem;
  font-weight: 500;
  opacity: 0.85;
  margin-left: 8px;
}

.subtitle {
  font-size: 0.85rem;
  opacity: 0.85;
  margin-top: 4px;
}

.layout {
  flex: 1;
  display: flex;
  justify-content: center;
  align-items: flex-start;
  gap: 24px;
  padding: 24px 16px 40px;
  max-width: 1060px;
  width: 100%;
  margin: 0 auto;
}

.panel {
  width: 300px;
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.card {
  background: var(--panel);
  border-radius: 12px;
  padding: 18px;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.08);
}

.card h2 {
  font-size: 0.95rem;
  font-weight: 700;
  color: var(--muted);
  letter-spacing: 1px;
  margin-bottom: 12px;
  border-bottom: 1px solid var(--line);
  padding-bottom: 8px;
}

.field {
  margin-bottom: 14px;
}

.field label {
  display: block;
  font-size: 0.85rem;
  font-weight: 600;
  margin-bottom: 6px;
}

.field select {
  width: 100%;
  padding: 8px 10px;
  font-size: 0.9rem;
  border: 1px solid #cbd5e1;
  border-radius: 8px;
  background: #fff;
  cursor: pointer;
}

.hint {
  font-size: 0.75rem;
  color: var(--muted);
  margin-top: 6px;
  line-height: 1.5;
}

.side-buttons {
  display: flex;
  gap: 8px;
}

.side-btn {
  flex: 1;
  padding: 8px 0;
  font-size: 0.9rem;
  font-weight: 700;
  border: 1.5px solid #cbd5e1;
  border-radius: 8px;
  background: #fff;
  cursor: pointer;
  transition: all 0.15s;
}

.side-btn:hover {
  border-color: #94a3b8;
}

.side-btn.active {
  border-color: var(--accent);
  background: var(--accent-soft);
  box-shadow: 0 0 0 1px var(--accent);
}

.primary-btn {
  width: 100%;
  padding: 10px 0;
  font-size: 0.95rem;
  font-weight: 700;
  color: #fff;
  background: linear-gradient(135deg, #4f46e5, #7c3aed);
  border: none;
  border-radius: 8px;
  cursor: pointer;
  transition: transform 0.1s, box-shadow 0.15s;
}

.primary-btn:hover {
  box-shadow: 0 4px 12px rgba(79, 70, 229, 0.4);
  transform: translateY(-1px);
}

.score-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 7px 0;
  font-size: 0.9rem;
}

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

.score-num {
  font-weight: 800;
  font-size: 1.05rem;
}

.rules {
  padding-left: 18px;
  font-size: 0.82rem;
  line-height: 1.75;
  color: #374151;
}

.rules li {
  margin-bottom: 6px;
}

/* ---------- 棋盤 ---------- */

.board-area {
  flex: 1;
  max-width: 560px;
  min-width: 320px;
  display: flex;
  flex-direction: column;
  align-items: center;
  position: relative;
}

.status-bar {
  width: 100%;
  text-align: center;
  font-size: 1rem;
  font-weight: 700;
  padding: 10px 14px;
  border-radius: 10px;
  background: var(--panel);
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.08);
  margin-bottom: 16px;
}

.board {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
  width: min(92vw, 480px);
}

.local {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 2px;
  background: var(--line);
  padding: 4px;
  border-radius: 10px;
  position: relative;
  transition: box-shadow 0.2s, opacity 0.2s, outline 0.2s;
  user-select: none;
}

.cell {
  aspect-ratio: 1;
  background: #fff;
  border-radius: 3px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: default;
}

.cell.playable {
  cursor: pointer;
}

.cell.playable:hover {
  background: #eff6ff;
}

.mark {
  font-weight: 800;
  font-size: clamp(0.9rem, 3.6vw, 1.4rem);
  line-height: 1;
  visibility: hidden;
}

.cell.x .mark {
  color: var(--x-color);
  visibility: visible;
}

.cell.o .mark {
  color: var(--o-color);
  visibility: visible;
}

.cell.last {
  background: var(--accent-soft);
}

.cell.x.playable:hover .mark::before,
.cell.playable:hover {
  /* 預覽效果由 JS 控制 */
}

/* 可落子的棋盤（自由選擇） */
.local.open {
  outline: 2px dashed #93c5fd;
}

/* 必下的棋盤 */
.local.active {
  box-shadow: 0 0 0 3px var(--accent), 0 6px 16px rgba(245, 158, 11, 0.35);
}

/* 非必下棋盤變淡 */
.local.dim {
  opacity: 0.42;
}

/* 已被贏下的棋盤 */
.local.won-x,
.local.won-o {
  opacity: 1;
}

.local.won-x .cell,
.local.won-o .cell {
  opacity: 0.12;
}

.local.won-x::after,
.local.won-o::after {
  content: attr(data-mark);
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: clamp(2.4rem, 10vw, 3.6rem);
  font-weight: 900;
  pointer-events: none;
}

.local.won-x {
  background: #dbeafe;
}

.local.won-x::after {
  color: var(--x-color);
}

.local.won-o {
  background: #fee2e2;
}

.local.won-o::after {
  color: var(--o-color);
}

/* ---------- 圖例 / 浮層 ---------- */

.board-legend {
  margin-top: 14px;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px 18px;
  font-size: 0.78rem;
  color: var(--muted);
}

.legend-item {
  display: flex;
  align-items: center;
  gap: 6px;
}

.mark-x,
.mark-o {
  font-style: normal;
  font-weight: 800;
}

.mark-x {
  color: var(--x-color);
}

.mark-o {
  color: var(--o-color);
}

.legend-swatch {
  width: 14px;
  height: 14px;
  border-radius: 3px;
  display: inline-block;
}

.legend-amber {
  background: var(--accent);
}

.legend-dash {
  border: 2px dashed #93c5fd;
  background: #fff;
}

.overlay {
  position: absolute;
  inset: 0;
  background: rgba(30, 41, 59, 0.55);
  backdrop-filter: blur(3px);
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 10;
}

.overlay.hidden {
  display: none;
}

.overlay-box {
  background: #fff;
  border-radius: 14px;
  padding: 26px 34px;
  text-align: center;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.3);
  display: flex;
  flex-direction: column;
  gap: 18px;
  align-items: center;
}

.overlay-title {
  font-size: 1.35rem;
  font-weight: 800;
}

.overlay-box .primary-btn {
  width: auto;
  padding: 10px 34px;
}

.app-footer {
  text-align: center;
  font-size: 0.75rem;
  color: var(--muted);
  padding: 14px;
}

/* ---------- 響應式 ---------- */

@media (max-width: 820px) {
  .layout {
    flex-direction: column;
    align-items: center;
  }

  .panel {
    width: min(92vw, 480px);
    order: 2;
  }

  .board-area {
    order: 1;
  }
}
