:root {
  --bg: #141820;
  --panel: #243044;
  --panel-hover: #2e3d56;
  --wood: #c9a06a;
  --wood-dark: #a67c52;
  --line: #5c3d1e;
  --text: #f0e6d8;
  --muted: #a8b0c0;
  --accent: #e8a87c;
  --accent-soft: rgba(232, 168, 124, 0.18);
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --tap: 44px;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  -webkit-tap-highlight-color: transparent;
}

html {
  height: 100%;
}

body {
  min-height: 100%;
  min-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding:
    max(0.5rem, var(--safe-top))
    max(0.5rem, var(--safe-right))
    max(0.5rem, var(--safe-bottom))
    max(0.5rem, var(--safe-left));
  font-family: "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  background:
    radial-gradient(ellipse at 18% 8%, #2a3550 0%, transparent 48%),
    radial-gradient(ellipse at 82% 92%, #3a2a1c 0%, transparent 42%),
    var(--bg);
  color: var(--text);
  overflow-x: hidden;
}

.screen[hidden] {
  display: none !important;
}

/* ========== 首页 ========== */

.home {
  width: min(92vw, 420px);
  padding: 1.25rem 0.75rem 1.75rem;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1.75rem;
  animation: fade-up 0.35s ease-out;
}

.home-brand {
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.75rem;
}

.home-board {
  width: 88px;
  height: 88px;
  border-radius: 10px;
  background:
    linear-gradient(90deg, transparent 48%, var(--line) 48%, var(--line) 52%, transparent 52%),
    linear-gradient(0deg, transparent 48%, var(--line) 48%, var(--line) 52%, transparent 52%),
    radial-gradient(circle at 30% 30%, #ddb67e, var(--wood) 55%, var(--wood-dark));
  background-size: 22px 22px, 22px 22px, auto;
  box-shadow:
    0 10px 28px rgba(0, 0, 0, 0.35),
    inset 0 0 0 3px rgba(92, 61, 30, 0.35);
  position: relative;
}

.home-board::before,
.home-board::after {
  content: "";
  position: absolute;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  box-shadow: 1px 1px 2px rgba(0, 0, 0, 0.25);
}

.home-board::before {
  top: 28px;
  left: 28px;
  background: radial-gradient(circle at 35% 30%, #555, #111);
}

.home-board::after {
  bottom: 26px;
  right: 26px;
  background: radial-gradient(circle at 35% 30%, #fff, #d0d0d0);
}

.home-brand h1 {
  font-size: clamp(1.7rem, 7vw, 2.2rem);
  font-weight: 650;
  letter-spacing: 0.35em;
  text-indent: 0.35em;
}

.home-sub {
  color: var(--muted);
  font-size: 0.95rem;
  padding: 0 0.5rem;
}

.mode-list {
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 0.7rem;
}

.mode-card {
  width: 100%;
  min-height: var(--tap);
  text-align: left;
  padding: 1rem 1.1rem;
  border: 1px solid #35465f;
  border-radius: 14px;
  background: linear-gradient(145deg, #2a3850 0%, var(--panel) 100%);
  color: var(--text);
  cursor: pointer;
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  transition: transform 0.15s, border-color 0.15s, background 0.15s, box-shadow 0.15s;
  touch-action: manipulation;
}

@media (hover: hover) and (pointer: fine) {
  .mode-card:hover {
    transform: translateY(-2px);
    border-color: var(--accent);
    background: linear-gradient(145deg, #334660 0%, #2a3a52 100%);
    box-shadow: 0 10px 28px rgba(0, 0, 0, 0.28);
  }
}

.mode-card:active {
  transform: scale(0.985);
  border-color: var(--accent);
}

.mode-card-title {
  font-size: 1.1rem;
  font-weight: 600;
  letter-spacing: 0.06em;
}

.mode-card-desc {
  color: var(--muted);
  font-size: 0.88rem;
  line-height: 1.4;
}

.mode-card[data-mode="ai"] .mode-card-title {
  color: var(--accent);
}

/* ========== 联机子页面 ========== */

.lobby,
.room-page {
  width: min(92vw, 420px);
  max-width: 100%;
  animation: fade-up 0.28s ease-out;
}

.page {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 1rem;
  padding: 0.35rem 0.15rem 1.25rem;
}

.page-header {
  display: grid;
  grid-template-columns: auto 1fr;
  align-items: center;
  gap: 0.35rem;
  min-height: var(--tap);
}

.page-header h2 {
  text-align: center;
  font-size: clamp(1.15rem, 4.5vw, 1.35rem);
  font-weight: 600;
  letter-spacing: 0.14em;
  margin-right: 2.2rem;
}

.page-sub {
  text-align: center;
  color: var(--muted);
  font-size: 0.92rem;
  margin-top: -0.2rem;
}

.page-hint {
  text-align: center;
  color: var(--muted);
  font-size: 0.86rem;
  line-height: 1.45;
  padding: 0 0.25rem;
}

.page-hint.error {
  color: #e89090;
}

.room-card {
  margin-top: 0.35rem;
  padding: 1.35rem 1rem 1.2rem;
  border-radius: 16px;
  border: 1px solid #35465f;
  background: linear-gradient(160deg, #2a3850 0%, var(--panel) 100%);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.85rem;
  box-shadow: 0 12px 36px rgba(0, 0, 0, 0.28);
}

.room-card .primary,
.room-card .btn-ghost {
  width: min(100%, 260px);
  min-height: var(--tap);
}

.room-label {
  color: var(--muted);
  font-size: 0.9rem;
  letter-spacing: 0.06em;
}

.room-code-lg {
  font-size: clamp(2rem, 10vw, 2.6rem);
  font-weight: 700;
  letter-spacing: 0.28em;
  text-indent: 0.28em;
  color: var(--accent);
  line-height: 1.15;
  user-select: all;
  word-break: keep-all;
}

.wait-text {
  display: inline-flex;
  align-items: center;
  gap: 0.55rem;
  color: var(--muted);
  font-size: 0.92rem;
}

.dot-pulse {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--accent);
  animation: pulse 1.1s ease-in-out infinite;
  flex-shrink: 0;
}

.join-input {
  width: 100%;
  max-width: 240px;
  min-height: var(--tap);
  padding: 0.75rem 0.9rem;
  border: 1px solid #3a4a66;
  border-radius: 12px;
  background: #1a2436;
  color: var(--text);
  /* ≥16px 避免 iOS 聚焦自动放大 */
  font-size: 1.25rem;
  letter-spacing: 0.28em;
  text-transform: uppercase;
  text-align: center;
}

.join-input:focus {
  outline: 1px solid var(--accent);
}

.btn-ghost {
  align-self: center;
  background: transparent;
  color: var(--muted);
  border-color: transparent;
}

@media (hover: hover) and (pointer: fine) {
  .btn-ghost:hover:not(:disabled) {
    background: var(--panel);
    color: var(--text);
  }
}

@keyframes pulse {
  0%,
  100% {
    opacity: 0.35;
    transform: scale(0.85);
  }
  50% {
    opacity: 1;
    transform: scale(1);
  }
}

/* ========== 对局页：顶栏 / 中间棋盘 / 底栏按钮 ========== */

.game {
  position: fixed;
  inset: 0;
  z-index: 10;
  width: 100%;
  max-width: none;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  background:
    radial-gradient(ellipse at 18% 8%, #2a3550 0%, transparent 48%),
    radial-gradient(ellipse at 82% 92%, #3a2a1c 0%, transparent 42%),
    var(--bg);
  animation: fade-up 0.28s ease-out;
  overflow: hidden;
}

.game-topbar {
  flex: 0 0 auto;
  z-index: 20;
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 0.4rem;
  width: 100%;
  min-height: calc(var(--tap) + 0.35rem);
  padding:
    max(0.35rem, var(--safe-top))
    max(0.65rem, var(--safe-right))
    0.4rem
    max(0.25rem, var(--safe-left));
  background: rgba(20, 24, 32, 0.94);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  border-bottom: 1px solid rgba(58, 74, 102, 0.45);
}

.game-topbar .status {
  margin: 0;
  width: auto;
  min-width: 0;
  font-size: clamp(0.88rem, 3.2vw, 1rem);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.game-stage {
  flex: 1 1 auto;
  min-height: 0;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0.45rem 0.55rem;
  /* 顶栏约 3.2rem + 底栏约 4rem，给棋盘留出中间全部空间 */
}

.game-bottombar {
  flex: 0 0 auto;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.65rem;
  width: 100%;
  padding:
    0.55rem
    max(0.75rem, var(--safe-right))
    max(0.55rem, var(--safe-bottom))
    max(0.75rem, var(--safe-left));
  background: rgba(20, 24, 32, 0.94);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  border-top: 1px solid rgba(58, 74, 102, 0.45);
}

.game-bottombar button {
  flex: 1 1 0;
  max-width: 280px;
  min-height: var(--tap);
}

.btn-back {
  min-height: var(--tap);
  min-width: var(--tap);
  padding: 0.4rem 0.75rem;
  border: 1px solid transparent;
  border-radius: 8px;
  background: transparent;
  color: var(--muted);
  font-size: 0.92rem;
  cursor: pointer;
  touch-action: manipulation;
  justify-self: start;
}

@media (hover: hover) and (pointer: fine) {
  .btn-back:hover {
    color: var(--text);
    border-color: #3a4a66;
    background: var(--panel);
  }
}

.mode-tag {
  justify-self: end;
  padding: 0.35rem 0.75rem;
  border-radius: 999px;
  background: var(--accent-soft);
  color: var(--accent);
  font-size: 0.82rem;
  letter-spacing: 0.04em;
  white-space: nowrap;
}

.status {
  color: var(--muted);
  font-size: clamp(0.95rem, 3.5vw, 1.05rem);
  min-height: 1.4em;
  text-align: center;
  width: 100%;
}

.status.win {
  color: var(--accent);
  font-weight: 600;
}

.main {
  padding: clamp(5px, 1.5vw, 12px);
  background: var(--panel);
  border-radius: 12px;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.35);
  line-height: 0;
  box-sizing: border-box;
  /* 兜底：按视口估算中间区域最大正方形 */
  width: min(100%, calc(100dvh - 7.5rem), 900px);
  max-width: 100%;
  max-height: 100%;
  aspect-ratio: 1;
}

@supports (width: 1cqw) {
  .game-stage {
    container-type: size;
  }

  .game-stage .main {
    width: min(100cqw, 100cqh, 900px);
    height: min(100cqw, 100cqh, 900px);
    max-width: 100%;
    max-height: 100%;
  }
}

/* 仅手机/平板触控横屏：左右放控制，中间棋盘尽量大
   PC（鼠标 pointer:fine）始终用上下布局，不套用横屏样式 */
@media (orientation: landscape) and (pointer: coarse),
       (orientation: landscape) and (hover: none) and (max-height: 520px) {
  .game {
    display: grid;
    grid-template-columns:
      minmax(4.75rem, 6rem)
      minmax(0, 1fr)
      minmax(5.25rem, 6.5rem);
    grid-template-rows: minmax(0, 1fr);
    align-items: stretch;
  }

  .game-topbar {
    display: flex;
    flex-direction: column;
    align-items: stretch;
    justify-content: flex-start;
    gap: 0.55rem;
    height: 100%;
    min-height: 0;
    width: auto;
    border-bottom: none;
    border-right: 1px solid rgba(58, 74, 102, 0.45);
    padding:
      max(0.35rem, var(--safe-top))
      0.4rem
      max(0.35rem, var(--safe-bottom))
      max(0.35rem, var(--safe-left));
  }

  .game-topbar .btn-back {
    width: 100%;
    justify-content: center;
    padding: 0.45rem 0.35rem;
  }

  .game-topbar .status {
    flex: 1 1 auto;
    display: flex;
    align-items: center;
    justify-content: center;
    white-space: normal;
    text-align: center;
    font-size: clamp(0.78rem, 2.2vh, 0.95rem);
    line-height: 1.35;
    overflow: visible;
    text-overflow: unset;
  }

  .game-topbar .mode-tag {
    justify-self: stretch;
    align-self: stretch;
    text-align: center;
    padding: 0.4rem 0.3rem;
    font-size: 0.75rem;
    letter-spacing: 0;
    white-space: normal;
    line-height: 1.25;
  }

  .game-stage {
    padding: 0.2rem;
    min-width: 0;
    min-height: 0;
    height: 100%;
  }

  .game-bottombar {
    flex-direction: column;
    justify-content: center;
    gap: 0.55rem;
    height: 100%;
    width: auto;
    border-top: none;
    border-left: 1px solid rgba(58, 74, 102, 0.45);
    padding:
      max(0.35rem, var(--safe-top))
      max(0.4rem, var(--safe-right))
      max(0.35rem, var(--safe-bottom))
      0.4rem;
  }

  .game-bottombar button {
    flex: 0 0 auto;
    width: 100%;
    max-width: none;
    min-height: 2.6rem;
    padding: 0.55rem 0.35rem;
    font-size: 0.88rem;
  }

  /* 横屏优先吃满可视高度 */
  .main {
    width: min(100%, calc(100dvh - 0.4rem), 960px);
    padding: clamp(3px, 0.8vw, 8px);
  }

  @supports (width: 1cqw) {
    .game-stage .main {
      width: min(100cqw, 100cqh, 960px);
      height: min(100cqw, 100cqh, 960px);
    }
  }
}

#board {
  display: block;
  width: 100%;
  height: auto;
  aspect-ratio: 1;
  background: var(--wood);
  border-radius: 4px;
  cursor: pointer;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  /* 避免浏览器再缩放画布导致糊线 */
  image-rendering: auto;
}

button {
  padding: 0.65rem 1.2rem;
  border: 1px solid transparent;
  border-radius: 10px;
  background: var(--panel);
  color: var(--text);
  font-size: 0.95rem;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s;
  touch-action: manipulation;
}

@media (hover: hover) and (pointer: fine) {
  button:hover:not(:disabled) {
    background: var(--panel-hover);
    border-color: var(--accent);
  }
}

button:active:not(:disabled) {
  transform: translateY(1px);
}

button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

button.primary {
  background: #3d5a80;
  border-color: var(--accent);
}

@media (hover: hover) and (pointer: fine) {
  button.primary:hover:not(:disabled) {
    background: #4a6d99;
  }
}

/* ========== 弹窗 ========== */

.modal[hidden] {
  display: none;
}

.modal {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  padding:
    max(1rem, var(--safe-top))
    max(1rem, var(--safe-right))
    max(1rem, var(--safe-bottom))
    max(1rem, var(--safe-left));
}

.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(8, 12, 20, 0.65);
}

.modal-panel {
  position: relative;
  width: min(92vw, 360px);
  padding: 1.35rem 1.2rem 1.15rem;
  border-radius: 14px;
  background: var(--panel);
  box-shadow: 0 16px 48px rgba(0, 0, 0, 0.45);
  text-align: center;
  animation: modal-in 0.22s ease-out;
}

.modal-panel h2 {
  font-size: 1.2rem;
  letter-spacing: 0.1em;
  margin-bottom: 0.7rem;
}

.modal-msg {
  color: var(--accent);
  font-size: 1.05rem;
  font-weight: 600;
  margin-bottom: 1.15rem;
  line-height: 1.4;
}

.modal-actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 0.65rem;
}

.modal-actions button {
  flex: 1 1 120px;
  min-height: var(--tap);
}

@keyframes modal-in {
  from {
    opacity: 0;
    transform: translateY(8px) scale(0.97);
  }
  to {
    opacity: 1;
    transform: none;
  }
}

@keyframes fade-up {
  from {
    opacity: 0;
    transform: translateY(12px);
  }
  to {
    opacity: 1;
    transform: none;
  }
}

/* ========== 手机 / 窄屏 ========== */

@media (max-width: 480px) {
  .home {
    gap: 1.35rem;
    padding-top: 0.75rem;
  }

  .home-board {
    width: 72px;
    height: 72px;
    background-size: 18px 18px, 18px 18px, auto;
  }

  .home-board::before,
  .home-board::after {
    width: 13px;
    height: 13px;
  }

  .home-board::before {
    top: 22px;
    left: 22px;
  }

  .home-board::after {
    bottom: 20px;
    right: 20px;
  }

  .home-brand h1 {
    letter-spacing: 0.22em;
    text-indent: 0.22em;
  }

  .mode-card {
    padding: 0.95rem 1rem;
  }

  .game-topbar {
    padding-left: max(0.2rem, var(--safe-left));
    padding-right: max(0.5rem, var(--safe-right));
  }

  .game-stage {
    padding: 0.35rem;
  }

  .main {
    padding: 4px;
    border-radius: 10px;
  }

  #board {
    border-radius: 3px;
  }

  .game-bottombar {
    gap: 0.5rem;
  }

  .game-bottombar button {
    padding: 0.7rem 0.5rem;
  }

  .room-code-lg {
    letter-spacing: 0.2em;
    text-indent: 0.2em;
  }

  .page-header h2 {
    margin-right: 1.8rem;
    letter-spacing: 0.1em;
  }
}

/* 首页等在矮横屏时略压缩 */
@media (max-height: 520px) and (orientation: landscape) {
  body {
    align-items: flex-start;
  }

  .home,
  .lobby,
  .room-page {
    padding-top: 0.35rem;
  }

  .home-board {
    display: none;
  }
}
