/* ==========================================================
   九牌博弈 — 移动端样式（深色海洋主题）
   ========================================================== */
:root {
  --bg: #0b1020;
  --bg-2: #101731;
  --panel: #161e3a;
  --panel-2: #1c2547;
  --line: rgba(120, 160, 220, 0.18);
  --text: #e8eefc;
  --muted: #8fa2c8;
  --accent: #3ec6e0;       /* 青 */
  --accent-2: #7a9bff;     /* 蓝 */
  --gold: #f5c76a;         /* 金 */
  --coral: #ff6b81;        /* 对方 */
  --green: #4cd97b;        /* 成功 */
  --red: #ff5f5f;          /* 危险 */
  --card-back-1: #24305e;
  --card-back-2: #182349;
  --radius: 14px;
  --safe-b: env(safe-area-inset-bottom, 0px);
}

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

html, body {
  margin: 0; padding: 0;
  background:
    radial-gradient(1200px 600px at 50% -10%, rgba(62, 198, 224, 0.12), transparent 60%),
    radial-gradient(900px 500px at 100% 110%, rgba(122, 155, 255, 0.10), transparent 60%),
    var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "HarmonyOS Sans SC",
    "MiSans", "Microsoft YaHei", "Segoe UI", sans-serif;
  min-height: 100%;
  overscroll-behavior: none;
  user-select: none;
  -webkit-user-select: none;
}

body { min-height: 100dvh; }

#app { max-width: 480px; margin: 0 auto; padding: 0 12px calc(16px + var(--safe-b)); }

.hidden { display: none !important; }

/* ---------- 通用控件 ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 46px; padding: 10px 18px;
  border: 1px solid var(--line); border-radius: 12px;
  background: var(--panel-2); color: var(--text);
  font-size: 15px; font-weight: 600; cursor: pointer;
  transition: transform 0.08s, background 0.15s, box-shadow 0.15s;
}
.btn:active { transform: scale(0.97); }
.btn.primary {
  background: linear-gradient(135deg, #2fb6d6, #4a7cf0);
  border-color: transparent; color: #fff;
  box-shadow: 0 4px 16px rgba(58, 140, 240, 0.35);
}
.btn.danger { background: linear-gradient(135deg, #e2556b, #c33c52); border-color: transparent; color: #fff; }
.btn.ghost { background: transparent; }
.btn.big { width: 100%; margin-top: 10px; font-size: 16px; min-height: 52px; }
.btn[disabled] { opacity: 0.45; pointer-events: none; }
.btn.no { opacity: 0.55; }
.mini {
  min-height: 34px; padding: 4px 10px; font-size: 12px;
  border: 1px solid var(--line); border-radius: 9px;
  background: var(--panel-2); color: var(--muted); cursor: pointer;
}
.link {
  margin: 14px auto 0; display: block; background: none; border: none;
  color: var(--muted); font-size: 13px; text-decoration: underline; cursor: pointer;
}

input {
  width: 100%; min-height: 46px; padding: 10px 14px;
  border: 1px solid var(--line); border-radius: 12px;
  background: var(--panel-2); color: var(--text); font-size: 16px;
  outline: none;
}
input:focus { border-color: var(--accent); }
.field label { display: block; font-size: 13px; color: var(--muted); margin: 14px 0 6px; }

/* ---------- 首页 ---------- */
.screen { min-height: 100dvh; display: flex; flex-direction: column; }
.home-inner { flex: 1; display: flex; flex-direction: column; justify-content: center; padding: 8vh 6px; }
.logo { text-align: center; font-size: 64px; filter: drop-shadow(0 6px 18px rgba(62, 198, 224, 0.4)); }
.title {
  text-align: center; margin: 8px 0 2px; font-size: 34px; letter-spacing: 6px;
  background: linear-gradient(120deg, #7fe3f5, #9fb4ff, #f5c76a);
  -webkit-background-clip: text; background-clip: text; color: transparent;
}
.subtitle { text-align: center; color: var(--muted); font-size: 13px; margin: 0 0 10px; }
#fb-warning { margin-top: 14px; font-size: 13px; line-height: 1.6; }
#fb-warning code { background: rgba(0,0,0,0.35); padding: 1px 5px; border-radius: 4px; }

.banner {
  margin: 8px 0; padding: 9px 12px; border-radius: 10px; font-size: 13px;
  background: var(--panel-2); border: 1px solid var(--line); color: var(--text);
  animation: fade-in 0.25s;
}
.banner.warn { border-color: rgba(245, 199, 106, 0.4); color: #f8dc9e; background: rgba(245, 199, 106, 0.08); }
.banner.error { border-color: rgba(255, 95, 95, 0.45); color: #ffb3b3; background: rgba(255, 95, 95, 0.10); }
.banner.ok { border-color: rgba(76, 217, 123, 0.45); color: #b9f0cd; background: rgba(76, 217, 123, 0.08); }
.banner.force { border-color: rgba(255, 107, 129, 0.5); color: #ffc2cc; background: rgba(255, 107, 129, 0.12); }

/* ---------- 顶栏 ---------- */
.topbar {
  display: flex; align-items: center; gap: 8px;
  padding: 12px 2px 8px;
}
.room-chip {
  display: flex; align-items: baseline; gap: 6px;
  background: var(--panel); border: 1px solid var(--line);
  padding: 5px 10px; border-radius: 10px;
}
.room-label { font-size: 11px; color: var(--muted); }
#room-code { font-size: 18px; letter-spacing: 3px; color: var(--gold); font-variant-numeric: tabular-nums; }
.conn { margin-left: auto; display: flex; align-items: center; gap: 5px; font-size: 12px; color: var(--muted); }

.dot { width: 9px; height: 9px; border-radius: 50%; display: inline-block; background: var(--green); box-shadow: 0 0 6px rgba(76, 217, 123, 0.8); }
.dot.off { background: var(--red); box-shadow: 0 0 6px rgba(255, 95, 95, 0.8); }
.dot.wait { background: var(--gold); box-shadow: 0 0 6px rgba(245, 199, 106, 0.8); }

/* ---------- 玩家面板 ---------- */
.players { display: flex; align-items: stretch; gap: 8px; margin: 4px 0 10px; }
.player {
  flex: 1; position: relative;
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 10px 12px; transition: border-color 0.2s, box-shadow 0.2s;
}
.player.active { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent), 0 0 18px rgba(62, 198, 224, 0.18); }
.p-head { display: flex; align-items: center; gap: 6px; }
.p-name { font-size: 15px; font-weight: 700; max-width: 108px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.p-tag { font-size: 11px; padding: 2px 7px; border-radius: 20px; background: rgba(62, 198, 224, 0.15); color: var(--accent); }
.p-tag.me { background: rgba(76, 217, 123, 0.15); color: var(--green); }
.p-sub { display: flex; align-items: center; gap: 5px; margin-top: 5px; font-size: 12px; color: var(--muted); }
.p-score {
  position: absolute; top: 8px; right: 10px;
  font-size: 26px; font-weight: 800; font-variant-numeric: tabular-nums;
  color: var(--gold); line-height: 1;
}
.p-score.bump { animation: score-bump 0.5s; }
.versus { align-self: center; color: var(--muted); font-size: 13px; font-weight: 700; }

/* ---------- 阶段标签 ---------- */
.phase-label {
  text-align: center; font-size: 13px; color: var(--muted);
  margin: 2px 0 8px; min-height: 18px;
}
.phase-label .strong { color: var(--accent); font-weight: 700; }

/* ---------- 棋盘 ---------- */
#board-wrap {
  position: relative; margin: 0 auto; width: min(94vw, 400px);
  touch-action: none;
}
.board {
  position: relative; width: 100%; aspect-ratio: 1 / 1;
  background:
    linear-gradient(rgba(62,198,224,0.04) 1px, transparent 1px),
    linear-gradient(90deg, rgba(62,198,224,0.04) 1px, transparent 1px),
    radial-gradient(600px 300px at 50% 0%, rgba(62, 198, 224, 0.10), transparent 70%),
    var(--bg-2);
  background-size: 24px 24px, 24px 24px, auto, auto;
  border: 1px solid var(--line); border-radius: 16px;
  overflow: hidden;
}

/* 格子层 */
.cell-layer { position: absolute; inset: 0; pointer-events: none; }
.cell {
  position: absolute; border-radius: 10px;
  border: 1.5px dashed rgba(120, 160, 220, 0.25);
  background: rgba(122, 155, 255, 0.05);
  transition: background 0.15s, border-color 0.15s;
}
.cell.valid { border-color: rgba(76, 217, 123, 0.55); background: rgba(76, 217, 123, 0.10); }
.cell.valid.tap-mode { animation: pulse-green 1.1s infinite; }
.cell.hover-valid { border-color: #4cd97b; background: rgba(76, 217, 123, 0.22); box-shadow: 0 0 12px rgba(76, 217, 123, 0.4); }
.cell.hover-invalid { border-color: var(--red); background: rgba(255, 95, 95, 0.18); box-shadow: 0 0 12px rgba(255, 95, 95, 0.4); }
.cell.faint { opacity: 0.45; }

/* 卡牌 */
.card {
  position: absolute;
  perspective: 800px;
  cursor: pointer;
  z-index: 2;
}
.card .card-inner {
  position: relative; width: 100%; height: 100%;
  transform-style: preserve-3d;
  transition: transform 0.35s cubic-bezier(0.4, 0.2, 0.2, 1);
}
.card.flipped .card-inner { transform: rotateY(180deg); }
.card-face {
  position: absolute; inset: 0; border-radius: 12%;
  display: flex; align-items: center; justify-content: center;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  overflow: hidden;
}
.card-front {
  background: linear-gradient(160deg, #fdf6e8, #f3e7c9);
  color: #26324f; border: 2px solid #d9c48e;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.45);
  transform: rotateY(180deg);
  font-weight: 800;
}
.card-front .v { font-size: 1.55em; }
.card-front::after {
  content: "🐋"; position: absolute; right: 6%; bottom: 4%;
  font-size: 0.42em; opacity: 0.35;
}
.card-front.ten { background: linear-gradient(160deg, #fff3cd, #f2d98a); border-color: #e0b54c; color: #8a5a10; }
.card-back {
  background:
    radial-gradient(120% 100% at 50% 0%, var(--card-back-1), var(--card-back-2) 70%);
  border: 2px solid #3a4a86;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.45);
  color: rgba(140, 170, 255, 0.9);
}
.card-back::before {
  content: ""; position: absolute; inset: 12%;
  border: 1.5px solid rgba(140, 170, 255, 0.45);
  border-radius: 12%;
  background:
    repeating-linear-gradient(45deg, transparent 0 6px, rgba(140, 170, 255, 0.14) 6px 8px);
}
.card-back .back-ico { font-size: 0.9em; opacity: 0.85; }
.owner-dot {
  position: absolute; right: 8%; bottom: 7%; width: 13%; height: 13%;
  border-radius: 50%; z-index: 3;
}
.owner-dot.me { background: var(--accent); box-shadow: 0 0 6px rgba(62, 198, 224, 0.9); }
.owner-dot.opp { background: var(--coral); box-shadow: 0 0 6px rgba(255, 107, 129, 0.9); }

.card.selected { z-index: 5; }
.card.selected .card-face { outline: 3px solid var(--accent); outline-offset: -2px; }
.card.selected .card-back { outline: 3px solid var(--accent); outline-offset: -2px; }
.card.dimmed { opacity: 0.75; }
.card.shake { animation: shake 0.4s; }
.card.drop-in { animation: drop-in 0.3s; }
.card.leaving { transition: transform 0.45s, opacity 0.45s; }

/* 拖动幽灵牌 */
.drag-ghost {
  position: fixed; z-index: 60; pointer-events: none;
  width: 64px; height: 88px; border-radius: 12%;
  background: linear-gradient(160deg, #2f3f78, #1d2a55);
  border: 2px solid var(--accent);
  box-shadow: 0 12px 28px rgba(0, 0, 0, 0.55);
  display: flex; align-items: center; justify-content: center;
  font-size: 26px; font-weight: 800; color: var(--accent);
  transition: border-color 0.12s, box-shadow 0.12s;
}
.drag-ghost.invalid { border-color: var(--red); box-shadow: 0 12px 28px rgba(255, 95, 95, 0.4); }
.drag-ghost.hidden { display: none; }

/* 锁层 */
.board-lock {
  position: absolute; inset: 0; z-index: 20;
  display: flex; align-items: center; justify-content: center;
  background: rgba(11, 16, 32, 0.55);
  border-radius: 16px; backdrop-filter: blur(1px);
}
.lock-text {
  font-size: 15px; color: var(--text); background: var(--panel-2);
  padding: 10px 18px; border-radius: 12px; border: 1px solid var(--line);
}

/* ---------- 手牌区 ---------- */
.hand-area {
  display: flex; align-items: center; gap: 12px;
  margin: 10px auto 0; width: min(94vw, 400px);
}
.card.hand {
  position: relative; width: 56px; height: 78px; flex: none;
  box-shadow: 0 8px 22px rgba(62, 198, 224, 0.3);
  animation: hand-in 0.3s;
  touch-action: none;
  cursor: grab;
}
.card.hand:active { cursor: grabbing; }
.card.hand.selected { outline: 3px solid var(--accent); outline-offset: 3px; border-radius: 12%; }
.card.hand.ten-face .card-front { background: linear-gradient(160deg, #fff3cd, #f2d98a); border-color: #e0b54c; color: #8a5a10; }
.hand-hint { font-size: 12.5px; color: var(--muted); line-height: 1.5; flex: 1; }

/* ---------- 选中操作条 / 控制区 ---------- */
.selected-actions {
  display: flex; gap: 8px; margin: 10px auto 0; width: min(94vw, 400px);
}
.selected-actions .btn { flex: 1; }
.controls {
  display: flex; align-items: center; gap: 10px;
  margin: 10px auto 0; width: min(94vw, 400px);
}
.controls .ops-left {
  flex: none; font-size: 14px; font-weight: 700; color: var(--gold);
  background: var(--panel); border: 1px solid var(--line);
  padding: 8px 12px; border-radius: 10px;
}
.controls .hint-line { flex: 1; font-size: 12.5px; color: var(--muted); line-height: 1.45; }
#controls-waiting { flex-direction: column; }
#controls-waiting .hint-line { text-align: center; margin-top: 4px; }

/* ---------- 弹窗 ---------- */
.modal {
  position: fixed; inset: 0; z-index: 100;
  background: rgba(5, 8, 18, 0.72);
  display: flex; align-items: center; justify-content: center;
  padding: 20px; backdrop-filter: blur(3px);
  animation: fade-in 0.2s;
}
.modal-card {
  width: 100%; max-width: 360px; max-height: 86dvh; overflow: auto;
  background: var(--panel); border: 1px solid var(--line);
  border-radius: 18px; padding: 22px 20px;
  animation: pop-in 0.25s;
}
.modal-card h2 { margin: 0 0 6px; text-align: center; }
.modal-sub { text-align: center; color: var(--muted); font-size: 13px; margin: 0 0 12px; }
.code-input {
  text-align: center; font-size: 28px !important; letter-spacing: 10px;
  font-variant-numeric: tabular-nums; font-weight: 700;
}
.modal-actions { display: flex; gap: 10px; margin-top: 16px; }
.modal-actions .btn { flex: 1; }
.form-error { color: #ffb3b3; font-size: 13px; margin-top: 10px; text-align: center; }
.go-card { text-align: center; }
.go-emoji { font-size: 54px; margin: 4px 0; animation: bounce 1s; }
.go-scores { display: flex; justify-content: center; gap: 26px; margin: 10px 0 4px; }
.go-scores .gs { text-align: center; }
.go-scores .gs .n { font-size: 13px; color: var(--muted); }
.go-scores .gs .s { font-size: 30px; font-weight: 800; color: var(--gold); }
.rematch-status { font-size: 13px; color: var(--gold); margin-top: 10px; }
.rules-card h2 { margin-bottom: 12px; }
.rules-body { font-size: 13.5px; line-height: 1.7; color: #c6d2ec; }
.rules-body h3 { color: var(--accent); font-size: 14px; margin: 14px 0 4px; }
.rules-body p, .rules-body ul { margin: 4px 0; }
.rules-body ul { padding-left: 18px; }
.rules-body li { margin: 4px 0; }

/* ---------- Toast ---------- */
.toast {
  position: fixed; left: 50%; bottom: calc(90px + var(--safe-b));
  transform: translateX(-50%) translateY(20px);
  background: rgba(24, 32, 60, 0.96); color: var(--text);
  border: 1px solid var(--line); border-radius: 12px;
  padding: 10px 18px; font-size: 14px; z-index: 200;
  opacity: 0; pointer-events: none;
  transition: opacity 0.2s, transform 0.2s;
  max-width: 86vw; text-align: center;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.toast.error { border-color: rgba(255, 95, 95, 0.5); }

/* ---------- 动画 ---------- */
@keyframes fade-in { from { opacity: 0; } to { opacity: 1; } }
@keyframes pop-in { from { transform: scale(0.9); opacity: 0; } to { transform: scale(1); opacity: 1; } }
@keyframes bounce { 0%, 100% { transform: translateY(0); } 40% { transform: translateY(-16px); } 60% { transform: translateY(-6px); } }
@keyframes score-bump { 0% { transform: scale(1); } 40% { transform: scale(1.45); color: #fff; } 100% { transform: scale(1); } }
@keyframes pulse-green {
  0%, 100% { border-color: rgba(76, 217, 123, 0.55); background: rgba(76, 217, 123, 0.10); }
  50% { border-color: #4cd97b; background: rgba(76, 217, 123, 0.28); }
}
@keyframes shake {
  0%, 100% { transform: translateX(0); }
  20%, 60% { transform: translateX(-6px); }
  40%, 80% { transform: translateX(6px); }
}
@keyframes drop-in { from { transform: scale(1.35); opacity: 0.4; } to { transform: scale(1); opacity: 1; } }
@keyframes hand-in { from { transform: translateY(20px) scale(0.8); opacity: 0; } to { transform: translateY(0) scale(1); opacity: 1; } }
@keyframes turn-glow {
  0%, 100% { box-shadow: 0 0 0 1px var(--accent), 0 0 12px rgba(62, 198, 224, 0.15); }
  50% { box-shadow: 0 0 0 1px var(--accent), 0 0 26px rgba(62, 198, 224, 0.4); }
}
.player.active { animation: turn-glow 2s infinite; }

/* ---------- 暗色适配之外的窄屏 ---------- */
@media (max-width: 360px) {
  .p-name { font-size: 13px; max-width: 82px; }
  .p-score { font-size: 22px; }
  .title { font-size: 28px; }
}
