/* ==========================================================================
   五子棋 · 界面样式
   主题色由 JS 按当前主题注入：--paper/--ink/--accent/--paper2/--font/--font-hand
   两种模式：stone（棋子）/ hand（手写）
   布局原则：任何屏幕上都"正好一屏"，不出现上下滚动
   ========================================================================== */

:root {
  --paper: #f6f1e4;
  --paper2: #ece2cc;
  --ink: #2b2622;
  --accent: #8c3b26;
  --p-black: #2b2622;
  --p-white: #7a6f5c;
  --panel: rgba(255, 255, 255, .55);
  --line: rgba(120, 105, 80, .35);
  --shadow: rgba(50, 40, 25, .18);
  --radius: 14px;
  --font: "Songti SC", "Kaiti SC", "STSong", serif;
  --font-hand: "Xingkai SC", "STXingkai", "Kaiti SC", "KaiTi", cursive;

  /* 对局页的高度预算：棋盘 = 视口 − 上下栏（含手机安全区）
     --board-w 是宽度上限（两侧各留 8px），--board-side 取两者小的那个，
     这样窄屏按宽度定、矮屏按高度定，永远正方形、永远一屏 */
  --bar-h: 44px;
  --acts-h: 62px;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --pad-y: 8px;
  --gap-y: 12px;
  --board-w: calc(100vw - 16px);
  --board-h: calc(100vh - var(--bar-h) - var(--acts-h) - var(--safe-t) - var(--safe-b) - var(--pad-y) * 2 - var(--gap-y));
  --board-h: calc(100dvh - var(--bar-h) - var(--acts-h) - var(--safe-t) - var(--safe-b) - var(--pad-y) * 2 - var(--gap-y));
  --board-side: min(var(--board-w), var(--board-h), 640px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0; padding: 0;
  height: 100%;
  overflow: hidden;              /* 全站不滚动 */
  overscroll-behavior: none;
  background: var(--paper);
  color: var(--ink);
  font-family: var(--font);
  -webkit-font-smoothing: antialiased;
  transition: background .35s ease, color .35s ease;
}

body::before {
  content: ""; position: fixed; inset: 0; z-index: 0; pointer-events: none;
  background:
    radial-gradient(circle at 22% 10%, rgba(255,255,255,.14), transparent 60%),
    radial-gradient(circle at 82% 84%, rgba(0,0,0,.07), transparent 55%);
}

.screen { position: relative; z-index: 1; height: 100%; }
.hidden { display: none !important; }

input {
  width: 100%; padding: 12px 14px; font-size: 16px;
  font-family: inherit; color: var(--ink);
  background: rgba(255,255,255,.5);
  border: 1.5px solid var(--line);
  border-radius: 10px; outline: none;
  transition: border-color .2s, background .2s;
}
body[data-theme="cyber"] input { background: rgba(10,20,35,.75); color: #cfe9ff; }
body[data-theme="chalk"] input { background: rgba(255,255,255,.08); color: #eef3ec; }
input:focus { border-color: var(--accent); background: rgba(255,255,255,.78); }
body[data-theme="cyber"] input:focus { background: rgba(15,30,50,.9); }
body[data-theme="chalk"] input:focus { background: rgba(255,255,255,.14); }

.btn {
  font-family: inherit; font-size: 15px; letter-spacing: .1em;
  color: var(--ink); background: rgba(255,255,255,.6);
  border: 1.5px solid var(--line); border-radius: 10px;
  padding: 11px 16px; cursor: pointer;
  transition: transform .08s, background .2s, border-color .2s;
}
body[data-theme="cyber"] .btn { background: rgba(15,30,50,.7); color: #cfe9ff; border-color: rgba(57,216,255,.4); }
body[data-theme="chalk"] .btn { background: rgba(255,255,255,.1); color: #eef3ec; border-color: rgba(255,255,255,.3); }
.btn:hover { border-color: var(--accent); }
.btn:active { transform: scale(.97); }
.btn.primary { background: var(--accent); color: #fff; border-color: transparent; }
body[data-theme="chalk"] .btn.primary { background: #f2c14e; color: #1e2b28; }
body[data-theme="cyber"] .btn.primary { background: rgba(57,216,255,.16); color: #7fe9ff; border-color: #39d8ff; }
.btn.big { width: 100%; padding: 15px; font-size: 17px; font-weight: 600; }
.btn.small { padding: 9px 14px; font-size: 14px; letter-spacing: .04em; }

/* ======================================================== 大厅（一屏） */

.lobby-screen {
  display: flex; flex-direction: column;
  padding: calc(var(--safe-t) + 10px) 16px calc(var(--safe-b) + 10px);
  gap: 10px;
  max-width: 560px; margin: 0 auto;
}

.lobby-top { flex: none; text-align: center; }
.lobby-top h1 {
  margin: 4px 0 0; font-size: 30px; font-weight: 600;
  letter-spacing: .32em; text-indent: .32em;
}

.lobby-body {
  flex: 1 1 auto; min-height: 0;
  display: flex; flex-direction: column; gap: 10px;
  justify-content: center;
}

.lobby-fields { display: flex; flex-direction: column; gap: 8px; }
.join-row { display: flex; gap: 8px; }
.join-row input { flex: 1; }
.join-row .btn { white-space: nowrap; }

.pick-row { display: flex; align-items: center; gap: 10px; }
.pick-label { font-size: 13px; opacity: .6; letter-spacing: .1em; flex: none; }
.seg {
  display: flex; flex: 1; gap: 4px; padding: 3px;
  border: 1.5px solid var(--line); border-radius: 10px;
  background: rgba(255,255,255,.28);
}
body[data-theme="cyber"] .seg { background: rgba(12,24,40,.6); }
.seg-btn {
  flex: 1; font-family: inherit; font-size: 14px; letter-spacing: .06em;
  padding: 8px 4px; border: 0; border-radius: 7px; cursor: pointer;
  background: transparent; color: inherit; opacity: .65;
  transition: all .16s;
}
.seg-btn.on { background: var(--accent); color: #fff; opacity: 1; }
body[data-theme="cyber"] .seg-btn.on { background: rgba(57,216,255,.2); color: #7fe9ff; box-shadow: inset 0 0 0 1px #39d8ff; }
body[data-theme="chalk"] .seg-btn.on { background: #f2c14e; color: #1e2b28; }

/* 主题卡片：高度自适应，保证整页不滚 */
.theme-grid {
  flex: 0 1 auto; min-height: 0;
  display: grid; grid-template-columns: repeat(5, 1fr); gap: 6px;
}
.theme-card {
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  padding: 5px 3px 6px; min-width: 0;
  background: rgba(255,255,255,.4);
  border: 2px solid transparent; border-radius: 10px;
  cursor: pointer; font-family: inherit; color: inherit;
  transition: border-color .18s, transform .1s, background .18s;
}
body[data-theme="cyber"] .theme-card { background: rgba(12,24,40,.6); }
body[data-theme="chalk"] .theme-card { background: rgba(255,255,255,.07); }
.theme-card:hover { transform: translateY(-2px); }
.theme-card.on { border-color: var(--accent); background: rgba(255,255,255,.72); }
body[data-theme="cyber"] .theme-card.on { background: rgba(20,45,70,.85); }
body[data-theme="chalk"] .theme-card.on { background: rgba(255,255,255,.16); }
.theme-card .mini {
  width: 100%; height: auto; aspect-ratio: 1; border-radius: 7px; display: block;
}
.theme-card .tname { font-size: 11px; font-weight: 600; letter-spacing: .04em; }
.theme-card .tdesc { display: none; }

.lobby-foot { flex: none; }
.foot-hint {
  margin: 8px 0 0; font-size: 12px; text-align: center; opacity: .5; line-height: 1.5;
}

/* 横屏 / 矮屏：把卡片缩小，仍保证一屏 */
@media (max-height: 640px) {
  .lobby-top h1 { font-size: 24px; }
  .theme-card { padding: 3px 2px 4px; }
  .theme-card .tname { font-size: 10px; }
}

/* ======================================================== 对局（一屏） */

.game-screen {
  display: flex; flex-direction: column;
  align-items: center;
  padding: var(--safe-t) 8px calc(var(--safe-b) + var(--pad-y));
  gap: 6px;
}

.game-bar {
  flex: none; width: 100%; max-width: 640px; height: var(--bar-h);
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: 0 4px;
}
.side { display: flex; align-items: center; gap: 6px; min-width: 0; flex: 1; }
.side:last-child { justify-content: flex-end; }
.chip {
  width: 13px; height: 13px; border-radius: 50%;
  border: 1.5px solid var(--line); flex: none;
}
.chip.black { background: var(--p-black); }
.chip.white { background: #fff; border-color: #b9ad95; }
body[data-theme="chalk"] .chip.black { background: #f4f7f1; }
body[data-theme="chalk"] .chip.white { background: #f2c14e; }
body[data-theme="cyber"] .chip.black { background: #39d8ff; box-shadow: 0 0 8px #39d8ff; }
body[data-theme="cyber"] .chip.white { background: #ff4fd8; box-shadow: 0 0 8px #ff4fd8; }
.pname {
  font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  opacity: .85; max-width: 26vw;
}
.status {
  font-size: 13px; letter-spacing: .02em; flex: none; text-align: center;
  max-width: 44vw; opacity: .95;
}
.status.win  { color: #2e9e5b; font-weight: 600; }
.status.lose { color: var(--accent); font-weight: 600; }

/* 棋盘：永远正方形。
   只用 --board-side 一个变量同时决定宽和高，
   不用 100%、不用 aspect-ratio —— 那两者在 flex 里会被父容器高度干扰，
   导致棋盘被拉成长方形（格子和棋子都会变形）。 */
.board-stage {
  flex: none;
  width: var(--board-side);
  height: var(--board-side);
  min-width: 0; min-height: 0;
  display: flex; align-items: center; justify-content: center;
}
canvas#board {
  display: block;
  width: 100%; height: 100%;
  border-radius: 10px;
  touch-action: manipulation;
  cursor: pointer;
  box-shadow: 0 14px 40px -20px rgba(0,0,0,.62);
}
canvas#board.my-turn { cursor: crosshair; }

/* 底部动作条 */
.game-acts {
  flex: none; width: 100%; max-width: 640px; height: var(--acts-h);
  display: grid; grid-template-columns: repeat(5, 1fr); gap: 6px;
  padding: 0 2px;
}
.ibtn {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 1px;
  font-family: inherit; font-size: 11px; letter-spacing: .04em;
  color: inherit; background: rgba(255,255,255,.42);
  border: 1.5px solid var(--line); border-radius: 10px;
  cursor: pointer; transition: transform .08s, background .2s, border-color .2s;
  position: relative;
}
body[data-theme="cyber"] .ibtn { background: rgba(15,30,50,.7); border-color: rgba(57,216,255,.35); }
body[data-theme="chalk"] .ibtn { background: rgba(255,255,255,.1); border-color: rgba(255,255,255,.28); }
.ibtn .ic { font-size: 17px; line-height: 1; }
.ibtn:active { transform: scale(.95); }
.ibtn:hover { border-color: var(--accent); }
.ibtn.on { border-color: var(--accent); color: var(--accent); }
.ibtn.off, .ibtn:disabled {
  opacity: .34; cursor: not-allowed;
}
.ibtn.off:active, .ibtn:disabled:active { transform: none; }
.ibtn[data-badge]::after {
  content: attr(data-badge);
  position: absolute; top: 3px; right: 3px;
  min-width: 15px; height: 15px; padding: 0 3px;
  border-radius: 8px; background: var(--accent); color: #fff;
  font-size: 10px; line-height: 15px; text-align: center;
}

/* ======================================================== 申请弹层 */

.ask-layer {
  position: fixed; inset: 0; z-index: 50;
  display: flex; align-items: center; justify-content: center;
  background: rgba(0,0,0,.42); backdrop-filter: blur(3px);
  padding: 20px;
}
.ask-card {
  width: 100%; max-width: 320px;
  background: var(--paper); color: var(--ink);
  border: 2px solid var(--accent); border-radius: 16px;
  padding: 20px 18px 16px; text-align: center;
  box-shadow: 0 24px 60px -20px rgba(0,0,0,.6);
}
.ask-text { margin: 0 0 16px; font-size: 16px; line-height: 1.6; }
.ask-btns { display: flex; gap: 8px; }
.ask-btns .btn { flex: 1; }

/* ======================================================== 聊天抽屉 */

.chat-toggle {
  position: fixed; right: 12px; bottom: calc(var(--safe-b) + var(--acts-h) + 10px);
  z-index: 30;
  width: 44px; height: 44px; border-radius: 50%;
  font-size: 19px; line-height: 1;
  color: var(--ink); background: rgba(255,255,255,.7);
  border: 1.5px solid var(--line);
  cursor: pointer; backdrop-filter: blur(8px);
  box-shadow: 0 6px 20px -8px rgba(0,0,0,.4);
}
body[data-theme="cyber"] .chat-toggle { background: rgba(15,30,50,.85); border-color: rgba(57,216,255,.4); }
body[data-theme="chalk"] .chat-toggle { background: rgba(255,255,255,.12); }
.chat-toggle .dot {
  position: absolute; top: 6px; right: 6px;
  width: 9px; height: 9px; border-radius: 50%;
  background: var(--accent);
  animation: pulse 1.1s ease-in-out infinite;
}
@keyframes pulse { 0%,100% { opacity: 1; } 50% { opacity: .35; } }

.chat-pane {
  position: fixed; z-index: 40;
  right: 8px; bottom: calc(var(--safe-b) + 6px);
  width: min(92vw, 380px);
  max-height: 62vh;
  display: flex; flex-direction: column;
  background: var(--paper); color: var(--ink);
  border: 1.5px solid var(--line); border-radius: 16px;
  overflow: hidden;
  box-shadow: 0 20px 60px -18px rgba(0,0,0,.55);
}
.chat-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 9px 12px; border-bottom: 1px solid var(--line);
  font-size: 13px; letter-spacing: .1em; opacity: .8;
}
.chat-x {
  border: 0; background: transparent; color: inherit;
  font-size: 15px; cursor: pointer; opacity: .6; padding: 2px 6px;
}
.chat-log {
  flex: 1 1 auto; min-height: 120px; overflow-y: auto;
  padding: 10px 12px; font-size: 14px; line-height: 1.7;
}
.chat-log > div { margin-bottom: 3px; word-break: break-word; }
.chat-log .who-name { font-weight: 600; margin-right: 2px; }
.chat-log .sys { opacity: .55; font-style: italic; font-size: 13px; }
.chat-input { display: flex; gap: 6px; padding: 8px; border-top: 1px solid var(--line); }
.chat-input input { flex: 1; padding: 9px 12px; font-size: 15px; }
.chat-input .btn { flex: none; padding: 9px 14px; }

/* 表情栏：一横排，点一下就发 */
.emote-bar {
  display: flex; gap: 3px; padding: 7px 8px 6px;
  border-top: 1px solid var(--line);
  overflow-x: auto; scrollbar-width: none;
}
.emote-bar::-webkit-scrollbar { display: none; }
.emote-btn {
  flex: none; width: 36px; height: 36px; border-radius: 9px;
  font-size: 21px; line-height: 1; cursor: pointer;
  background: rgba(255,255,255,.4);
  border: 1.5px solid transparent;
  transition: transform .1s, background .16s, border-color .16s;
}
body[data-theme="cyber"] .emote-btn { background: rgba(20,40,65,.7); }
body[data-theme="chalk"] .emote-btn { background: rgba(255,255,255,.1); }
.emote-btn:hover { border-color: var(--accent); }
.emote-btn:active { transform: scale(.88); }

/* 表情飘字：从下往上飘 + 淡出 */
.emote-layer {
  position: fixed; inset: 0; z-index: 45; pointer-events: none;
  overflow: hidden;
}
.float-emote {
  position: absolute;
  font-size: 62px; line-height: 1;
  will-change: transform, opacity;
  animation: floatUp 2.2s cubic-bezier(.22,.7,.3,1) forwards;
  filter: drop-shadow(0 4px 10px rgba(0,0,0,.45));
}
.float-emote .who {
  display: block; font-size: 12px; text-align: center;
  opacity: .8; margin-top: 4px; letter-spacing: .04em;
}
@keyframes floatUp {
  0%   { transform: translateY(0) scale(.5) rotate(-8deg); opacity: 0; }
  18%  { transform: translateY(-24px) scale(1.18) rotate(4deg); opacity: 1; }
  35%  { transform: translateY(-42px) scale(1) rotate(0deg); opacity: 1; }
  100% { transform: translateY(-190px) scale(1.05) rotate(6deg); opacity: 0; }
}

/* 聊天里的表情消息 */
.chat-log .emote-msg { font-size: 30px; line-height: 1.2; }

/* ======================================================== 小屏微调 */

@media (max-width: 400px) {
  .lobby-top h1 { font-size: 25px; }
  .ibtn { font-size: 10px; }
  .ibtn .ic { font-size: 16px; }
  .theme-card .tname { font-size: 10px; }
}

/* 横屏：棋盘居中，用高度 */
@media (max-height: 520px) and (orientation: landscape) {
  :root { --bar-h: 36px; --acts-h: 50px; }
  .lobby-screen { max-width: 720px; }
  .theme-grid { grid-template-columns: repeat(5, 1fr); }
}
