:root {
  --bg: #080a18;
  --panel: rgba(14, 17, 40, 0.86);
  --edge: rgba(120, 135, 255, 0.35);
  --text: #eef0ff;
  --muted: #9aa2d6;
  --you: #38d6ff;
  --bot: #ff5266;
  --gold: #ffd166;
  --accent: #6c7cff;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; }
html, body {
  margin: 0;
  min-height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
  overflow-x: hidden;
}
body { min-height: 100dvh; }
[hidden] { display: none !important; }

/* ---------- Screens ---------- */
.screen {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: start center;
  gap: 24px;
  padding: calc(16px + var(--safe-top)) 16px calc(16px + var(--safe-bottom));
  background: radial-gradient(ellipse at center, rgba(8, 10, 24, 0.35), rgba(8, 10, 24, 0.85));
  overflow-y: auto;
  align-content: start;
}
#home { position: static; min-height: 100dvh; padding-top: max(24px, 5vh); }
.game-screen {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
  gap: 8px;
  padding: calc(10px + var(--safe-top)) 10px calc(10px + var(--safe-bottom));
  overflow: hidden;
}
.overlay {
  z-index: 20;
  place-items: center;
  background: rgba(4, 5, 14, 0.7);
  backdrop-filter: blur(3px);
}

.panel {
  width: min(440px, 100%);
  padding: 26px 22px 18px;
  border-radius: 22px;
  background: var(--panel);
  border: 1px solid var(--edge);
  box-shadow: 0 20px 80px rgba(0, 0, 0, 0.55), inset 0 1px 0 rgba(255, 255, 255, 0.06);
  text-align: center;
  animation: rise 0.35s ease-out;
}
@keyframes rise { from { opacity: 0; transform: translateY(14px) scale(0.98); } to { opacity: 1; transform: none; } }

.logo {
  margin: 0;
  font-size: clamp(26px, 6.4vw, 34px);
  line-height: 1.15;
  font-weight: 900;
  letter-spacing: 0.02em;
  background: linear-gradient(90deg, var(--you), #b9a8ff 50%, var(--bot));
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  filter: drop-shadow(0 4px 24px rgba(108, 124, 255, 0.45));
}
.hook { margin: 8px 0 18px; color: var(--muted); font-size: 15px; line-height: 1.4; }

.settings { display: grid; gap: 10px; margin-bottom: 18px; }
.setting { display: grid; gap: 6px; text-align: left; }
.setting-label { font-size: 11px; text-transform: uppercase; letter-spacing: 0.12em; color: var(--muted); font-weight: 700; }
.seg { display: flex; gap: 6px; flex-wrap: wrap; }
.seg button {
  flex: 1 1 0;
  min-height: 44px;
  padding: 10px 6px;
  border-radius: 12px;
  border: 1px solid rgba(255, 255, 255, 0.08);
  background: rgba(255, 255, 255, 0.04);
  color: var(--text);
  font-weight: 700;
  font-size: 14px;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s, transform 0.1s;
}
.seg button:active { transform: scale(0.97); }
.seg button.on { background: rgba(108, 124, 255, 0.28); border-color: var(--accent); }

.toggle-row {
  display: flex;
  gap: 10px;
  align-items: flex-start;
  text-align: left;
  font-size: 14px;
  color: var(--text);
  min-height: 44px;
}
.toggle-row input { width: 22px; height: 22px; margin: 0; flex: 0 0 auto; accent-color: var(--accent); }
.toggle-row em { color: var(--muted); font-style: normal; font-size: 12px; }

.primary-btn {
  width: 100%;
  min-height: 52px;
  padding: 14px;
  border: 0;
  border-radius: 16px;
  font-size: 20px;
  font-weight: 900;
  letter-spacing: 0.1em;
  color: #fff;
  background: linear-gradient(90deg, #6c7cff, #38d6ff);
  box-shadow: 0 10px 30px rgba(108, 124, 255, 0.4);
  cursor: pointer;
}
.secondary-btn {
  width: 100%;
  min-height: 44px;
  margin-top: 10px;
  padding: 12px;
  border-radius: 14px;
  border: 1px solid var(--edge);
  background: rgba(255, 255, 255, 0.05);
  color: var(--text);
  font-weight: 700;
  font-size: 15px;
  cursor: pointer;
}
.secondary-btn.small { width: auto; flex: 1 1 0; margin-top: 0; padding: 10px 8px; font-size: 14px; }
.secondary-btn:disabled { opacity: 0.4; cursor: default; }
.row { display: flex; gap: 10px; }
.row .secondary-btn { margin-top: 10px; }
.text-btn {
  min-height: 44px;
  margin-top: 8px;
  background: none;
  border: 0;
  color: var(--muted);
  font-size: 14px;
  cursor: pointer;
  text-decoration: underline;
  text-underline-offset: 3px;
}

.howto { margin: 14px 0 0; font-size: 13px; color: var(--muted); line-height: 1.6; }
kbd {
  padding: 1px 6px;
  border-radius: 6px;
  border: 1px solid rgba(255, 255, 255, 0.2);
  background: rgba(255, 255, 255, 0.06);
  font-size: 12px;
}
.touch-only { display: none; }
@media (hover: none) and (pointer: coarse) {
  .touch-only { display: inline; }
  .kbd-only { display: none; }
}
.record { margin: 10px 0 0; font-size: 13px; color: var(--gold); min-height: 1em; }

/* ---------- Game screen ---------- */
.game-top { width: 100%; max-width: 640px; display: grid; gap: 4px; text-align: center; }
.game-top .back-link { justify-self: start; margin: 0; }
.hud { display: flex; justify-content: center; align-items: center; gap: 12px; font-weight: 800; font-size: 14px; }
.hud-info { color: var(--text); letter-spacing: 0.02em; }
.clock {
  padding: 2px 10px;
  border-radius: 999px;
  border: 1px solid var(--edge);
  color: var(--gold);
  font-variant-numeric: tabular-nums;
}
.clock.urgent { color: var(--bot); border-color: var(--bot); }
.status { margin: 0; min-height: 1.4em; color: var(--muted); font-size: 14px; font-weight: 700; }
.status.you { color: var(--you); }

.board-wrap {
  width: 100%;
  display: flex;
  justify-content: center;
  flex: 0 1 auto;
  min-height: 0;
}
#board {
  display: block;
  /* 竖屏铺满宽度；横屏或大屏受高度限制 */
  /* 手机横屏高度很小，保底 300px（此时页面可上下滚动），避免棋格过小点不中 */
  width: min(100vw - 20px, max(calc(100dvh - 230px), 300px), 640px);
  height: auto;
  aspect-ratio: 1 / 1;
  border-radius: 14px;
  border: 1px solid var(--edge);
  box-shadow: 0 16px 50px rgba(0, 0, 0, 0.5);
  touch-action: manipulation;
  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
}
.controls { width: 100%; max-width: 640px; display: flex; gap: 8px; }
.controls .secondary-btn.small { min-height: 44px; }

/* ---------- Result panel ---------- */
.result-title { margin: 0; font-size: clamp(36px, 10vw, 52px); font-weight: 950; letter-spacing: 0.05em; }
.result-title.win { color: var(--gold); text-shadow: 0 0 30px rgba(255, 209, 102, 0.45); }
.result-title.lose { color: var(--bot); text-shadow: 0 0 30px rgba(255, 82, 102, 0.45); }
.result-title.draw { color: var(--you); text-shadow: 0 0 30px rgba(56, 214, 255, 0.4); }
.stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin: 6px 0 14px; }
.stats div { padding: 10px 4px; border-radius: 12px; background: rgba(255, 255, 255, 0.04); }
.stats b { display: block; font-size: 18px; }
.stats span { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.08em; }
.challenge { margin: 0 0 14px; color: var(--gold); font-weight: 700; }

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(30px + var(--safe-bottom));
  transform: translateX(-50%);
  z-index: 30;
  padding: 10px 16px;
  border-radius: 12px;
  background: rgba(20, 24, 52, 0.95);
  border: 1px solid var(--edge);
  font-size: 14px;
  max-width: calc(100vw - 32px);
}
.fatal {
  position: fixed;
  inset: 0;
  margin: 0;
  padding: 16px;
  white-space: pre-wrap;
  color: #fff;
  background: var(--bg);
  overflow: auto;
  z-index: 40;
}

/* ---------- About article ---------- */
.back-link {
  display: inline-block;
  margin: -8px 0 8px;
  color: var(--muted);
  font-size: 13px;
  text-decoration: none;
  min-height: 24px;
}
.panel > .back-link { display: block; text-align: left; }
.back-link.center { display: block; text-align: center; margin: 10px 0 0; }
.back-link:hover { color: var(--text); }
.about {
  width: min(640px, 100%);
  padding: 22px;
  border-radius: 18px;
  background: rgba(12, 14, 34, 0.95);
  border: 1px solid rgba(120, 135, 255, 0.18);
  color: var(--muted);
  line-height: 1.6;
  font-size: 15px;
  user-select: text;
  -webkit-user-select: text;
  margin-bottom: 24px;
}
.about h2, .about h3 { color: var(--text); margin: 18px 0 8px; }
.about h2 { margin-top: 0; font-size: 22px; }
.about h3 { font-size: 17px; }
.about ul { padding-left: 20px; margin: 0; }
.about li { margin: 4px 0; }
.about strong { color: var(--text); }
.about details { padding: 10px 0; border-top: 1px solid rgba(255, 255, 255, 0.06); }
.about summary { color: var(--text); cursor: pointer; font-weight: 600; }
.about details p { margin: 8px 0 0; }
.about a { color: var(--you); }
.about .more { margin: 16px 0 0; }

@media (max-width: 480px) {
  .panel { padding: 22px 16px 14px; }
  .settings { gap: 8px; }
}
