:root {
  color-scheme: dark;
  --bg: #07111f;
  --panel: #0e1c2e;
  --panel-soft: #0b1a2a;
  --border: #1e3550;
  --text: #e8f0f8;
  --muted: #8ea6c0;
  --accent: #59d3c8;
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

html,
body {
  height: 100%;
  margin: 0;
  /* 游戏界面固定在一屏内，任何情况下都不出现页面滚动。 */
  overflow: hidden;
}

body {
  background: radial-gradient(circle at 50% 0%, #0f2136 0%, var(--bg) 60%);
  color: var(--text);
  font-family:
    system-ui,
    -apple-system,
    "PingFang SC",
    "Microsoft YaHei",
    sans-serif;
  overscroll-behavior: none;
  -webkit-text-size-adjust: 100%;
}

/* svh 取「工具栏可见时」的最小视口高度，避免地址栏收放导致整页重排。 */
.app {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  height: 100vh;
  height: 100svh;
  overflow: hidden;
  padding: calc(env(safe-area-inset-top) + 0.45rem) 0.7rem
    calc(env(safe-area-inset-bottom) + 0.45rem);
}

/* ---------- 顶栏 ---------- */
.topbar {
  flex: none;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
}

.topbar h1 {
  margin: 0;
  font-size: clamp(0.9rem, 3.6vw, 1.15rem);
  letter-spacing: 0.1em;
  white-space: nowrap;
}

.topbar-actions {
  display: flex;
  gap: 0.35rem;
}

.back,
.ghost-button,
.primary-button {
  font: inherit;
  color: var(--text);
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 0.55rem;
  text-decoration: none;
  cursor: pointer;
  white-space: nowrap;
  transition:
    background 0.15s ease,
    border-color 0.15s ease;
}

.back,
.ghost-button {
  padding: 0.35rem 0.55rem;
  font-size: 0.75rem;
  color: var(--muted);
}

.ghost-button[aria-pressed="true"] {
  color: var(--accent);
  border-color: var(--accent);
}

.back:hover,
.ghost-button:hover,
.primary-button:hover {
  border-color: var(--accent);
  background: #14263c;
}

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* ---------- 主区域：flex-basis 0 让棋盘拿到「确定剩余高度」 ---------- */
.stage {
  flex: 1 1 0;
  min-height: 0;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
}

.board-wrap {
  position: relative;
  flex: none;
}

#board {
  display: block;
  width: 100%;
  height: 100%;
  border: 1px solid var(--border);
  border-radius: 0.55rem;
  background: #0a1626;
  touch-action: none;
}

.overlay {
  position: absolute;
  inset: 0;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  padding: 0.6rem;
  text-align: center;
  border-radius: 0.55rem;
  background: rgba(7, 17, 31, 0.88);
}

.overlay-title {
  margin: 0;
  font-size: clamp(0.95rem, 4vw, 1.15rem);
  font-weight: 600;
}

.overlay-text {
  margin: 0;
  max-width: 20ch;
  color: var(--muted);
  font-size: clamp(0.68rem, 3vw, 0.85rem);
  line-height: 1.5;
}

/* 棋盘过小时只保留标题与按钮，说明文字会溢出棋盘。 */
body.tiny-board .overlay-text {
  display: none;
}

body.tiny-board .overlay-title {
  font-size: 0.8rem;
}

body.tiny-board .primary-button {
  padding: 0.3rem 0.7rem;
  font-size: 0.72rem;
}

.primary-button {
  padding: 0.5rem 1.05rem;
  font-size: 0.85rem;
  font-weight: 600;
  border-color: var(--accent);
  color: var(--accent);
}

/* ---------- 信息条（窄屏：棋盘上方一条） ---------- */
.hud {
  flex: none;
  width: 100%;
  display: flex;
  align-items: center;
  gap: 0.6rem;
  padding: 0.35rem 0.55rem;
  border: 1px solid var(--border);
  border-radius: 0.55rem;
  background: var(--panel-soft);
}

.preview-row {
  flex: none;
  display: flex;
  gap: 0.35rem;
}

.preview {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.preview h2 {
  display: none;
}

.preview canvas {
  width: 2.7rem;
  height: 1.6rem;
  border: 1px solid var(--border);
  border-radius: 0.35rem;
  background: var(--panel);
}

.preview:first-child canvas {
  height: 3.2rem;
}

.stats {
  flex: 1 1 auto;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 0.1rem 0.25rem;
  margin: 0;
  text-align: center;
}

.stats dt {
  color: var(--muted);
  font-size: 0.56rem;
  letter-spacing: 0.04em;
  white-space: nowrap;
}

.stats dd {
  margin: 0.05rem 0 0;
  font-size: 0.8rem;
  font-variant-numeric: tabular-nums;
}

/* 窄屏空间紧张，操作提示交给开始覆盖层说明。 */
.hint {
  display: none;
}

/* ---------- 触控按键 ---------- */
.pad {
  flex: none;
  display: none;
  grid-template-columns: repeat(3, 1fr);
  gap: 0.4rem;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}

body.pad-visible .pad {
  display: grid;
}

.pad button {
  min-height: 2.9rem;
  font: inherit;
  font-size: 1.2rem;
  color: var(--text);
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 0.7rem;
  cursor: pointer;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
  transition:
    background 0.1s ease,
    border-color 0.1s ease,
    transform 0.06s ease;
}

.pad button[data-action="hold"] {
  font-size: 0.85rem;
  color: var(--muted);
}

.pad button.pressed,
.pad button:active {
  background: #1b3149;
  border-color: var(--accent);
  transform: scale(0.96);
}

/* ---------- 宽屏（平板横屏 / 桌面）：棋盘在左，信息变侧栏 ---------- */
body.layout-side .stage {
  flex-direction: row;
  align-items: stretch;
  justify-content: center;
  gap: 0.9rem;
}

body.layout-side .board-wrap {
  order: 1;
}

body.layout-side .hud {
  order: 2;
  width: 12rem;
  flex-direction: column;
  align-items: stretch;
  justify-content: flex-start;
  gap: 0.7rem;
  padding: 0.8rem;
}

body.layout-side .preview-row {
  justify-content: space-between;
}

body.layout-side .preview h2 {
  display: block;
  margin: 0 0 0.2rem;
  font-size: 0.68rem;
  font-weight: 500;
  color: var(--muted);
  letter-spacing: 0.06em;
}

body.layout-side .preview canvas {
  width: 4rem;
  height: 2.6rem;
}

body.layout-side .preview:first-child canvas {
  height: 5.4rem;
}

body.layout-side .stats {
  /* 侧栏里数据不参与拉伸，否则两行会被撑开到整栏高度。 */
  flex: none;
  grid-template-columns: repeat(2, 1fr);
  gap: 0.4rem;
  text-align: left;
}

body.layout-side .stats dt {
  font-size: 0.66rem;
}

body.layout-side .stats dd {
  font-size: 1.05rem;
}

body.layout-side .hint {
  display: block;
  margin: auto 0 0;
  color: var(--muted);
  font-size: 0.7rem;
  line-height: 1.5;
}

body.layout-side:not(.short-viewport) .pad {
  grid-template-columns: repeat(6, 1fr);
}

/* ---------- 横屏手机：按键挪到右侧，把高度全部让给棋盘 ---------- */
body.short-viewport .app {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  grid-template-rows: auto minmax(0, 1fr);
  gap: 0.45rem;
}

body.short-viewport .topbar {
  grid-column: 1 / -1;
}

body.short-viewport .stage {
  grid-column: 1;
  grid-row: 2;
  min-height: 0;
}

body.short-viewport .pad {
  grid-column: 2;
  grid-row: 2;
  grid-template-columns: repeat(2, 1fr);
  align-content: center;
  gap: 0.35rem;
}

body.short-viewport .pad button {
  min-width: 3.4rem;
  min-height: 2.5rem;
  font-size: 1rem;
}

body.short-viewport .hud {
  width: 10.5rem;
  gap: 0.5rem;
  padding: 0.6rem;
}

body.short-viewport .preview canvas {
  width: 3.4rem;
  height: 2.2rem;
}

body.short-viewport .preview:first-child canvas {
  height: 4.4rem;
}

body.short-viewport .hint {
  font-size: 0.62rem;
  line-height: 1.4;
}

@media (prefers-reduced-motion: reduce) {
  * {
    transition-duration: 0.01ms !important;
  }
}
