:root {
  --bg: #0c1f17;
  --bg-2: #13291f;
  --panel: rgba(255, 255, 255, 0.06);
  --panel-solid: #163025;
  --line: rgba(255, 255, 255, 0.12);
  --text: #eef5f1;
  --muted: #9db3a8;
  --gold: #e8b44a;
  --gold-2: #c9922a;
  --red: #e05555;
  --green: #46c08a;
  --radius: 14px;
  --shadow: 0 8px 30px rgba(0, 0, 0, 0.35);
}

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

html, body {
  margin: 0;
  padding: 0;
  min-height: 100%;
  background: radial-gradient(120% 80% at 50% 0%, #1b3d2c 0%, var(--bg) 55%, #071410 100%);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  font-size: 15px;
  line-height: 1.5;
}

body { min-height: 100dvh; }

a { color: var(--gold); text-decoration: none; }
button { font-family: inherit; }

.wrap {
  max-width: 520px;
  margin: 0 auto;
  padding: 16px 16px 32px;
}

.wrap.wide { max-width: 960px; }

.center-screen {
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  padding: 24px 16px;
}

.logo {
  font-size: 26px;
  font-weight: 700;
  letter-spacing: 2px;
  color: var(--gold);
  text-align: center;
  margin-bottom: 4px;
}
.logo small {
  display: block;
  font-size: 12px;
  letter-spacing: 4px;
  color: var(--muted);
  font-weight: 400;
  margin-top: 6px;
}

.card-panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 18px;
  backdrop-filter: blur(8px);
}

.card-panel + .card-panel { margin-top: 14px; }

h1, h2, h3 { margin: 0 0 12px; font-weight: 600; }
h2 { font-size: 17px; }
h3 { font-size: 15px; color: var(--muted); font-weight: 500; }

.field { margin-bottom: 14px; }
.field label {
  display: block;
  font-size: 13px;
  color: var(--muted);
  margin-bottom: 6px;
}

input[type="text"], input[type="password"], input[type="number"] {
  width: 100%;
  height: 46px;
  padding: 0 14px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: rgba(0, 0, 0, 0.25);
  color: var(--text);
  font-size: 16px;
  outline: none;
  transition: border-color .15s;
}
input:focus { border-color: var(--gold); }
input::placeholder { color: #6d8378; }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  height: 46px;
  padding: 0 18px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.07);
  color: var(--text);
  font-size: 15px;
  cursor: pointer;
  transition: transform .06s, background .15s, opacity .15s;
  user-select: none;
}
.btn:active { transform: scale(0.98); }
.btn:disabled { opacity: .4; cursor: not-allowed; }
.btn.primary {
  background: linear-gradient(180deg, var(--gold) 0%, var(--gold-2) 100%);
  border-color: transparent;
  color: #241a05;
  font-weight: 700;
}
.btn.danger { border-color: rgba(224, 85, 85, .5); color: #ff9b9b; }
.btn.block { width: 100%; }
.btn.sm { height: 34px; padding: 0 12px; font-size: 13px; border-radius: 8px; }

.row { display: flex; gap: 10px; }
.row > * { flex: 1; }
.row.tight { gap: 8px; }

.tabs { display: flex; gap: 8px; margin-bottom: 16px; }
.tabs button {
  flex: 1;
  height: 40px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: transparent;
  color: var(--muted);
  font-size: 14px;
  cursor: pointer;
}
.tabs button.on { background: rgba(232, 180, 74, .15); border-color: var(--gold); color: var(--gold); }

.hint { font-size: 12px; color: var(--muted); line-height: 1.6; }
.error { color: #ff9b9b; font-size: 13px; min-height: 18px; }
.ok { color: var(--green); }

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 10px 0 14px;
}
.user-chip {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  color: var(--muted);
}
.coins {
  color: var(--gold);
  font-weight: 700;
  font-size: 16px;
}

.game-card {
  position: relative;
  overflow: hidden;
  padding: 20px 18px;
  border-radius: var(--radius);
  border: 1px solid var(--line);
  background: linear-gradient(135deg, rgba(232,180,74,.14), rgba(255,255,255,.03));
}
.game-card h3 {
  color: var(--text);
  font-size: 19px;
  margin-bottom: 6px;
  font-weight: 700;
}
.game-card p { margin: 0 0 14px; font-size: 12.5px; color: var(--muted); line-height: 1.6; }
.game-card .badge {
  position: absolute;
  top: 14px;
  right: 14px;
  font-size: 11px;
  padding: 2px 8px;
  border-radius: 20px;
  background: rgba(255, 255, 255, .1);
  color: var(--muted);
}

.room-code {
  font-size: 40px;
  letter-spacing: 8px;
  font-weight: 700;
  color: var(--gold);
  text-align: center;
  margin: 10px 0 6px;
  font-variant-numeric: tabular-nums;
}

.divider { height: 1px; background: var(--line); margin: 16px 0; }

.history-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 0;
  border-bottom: 1px solid rgba(255,255,255,.06);
  font-size: 13px;
}
.history-item:last-child { border-bottom: 0; }
.history-item .res-win { color: var(--red); font-weight: 600; }
.history-item .res-lose { color: var(--green); font-weight: 600; }
.history-item .res-tie { color: var(--muted); }
.history-item .res-void { color: var(--muted); text-decoration: line-through; }

/* ============ 牌桌 ============ */
.table-page {
  height: 100dvh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: radial-gradient(120% 70% at 50% 28%, #1f4a35 0%, #0f2a1f 55%, #071410 100%);
}

.table-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 14px;
  font-size: 12.5px;
  color: var(--muted);
  gap: 8px;
  flex: 0 0 auto;
}
.table-top .room {
  color: var(--gold);
  font-weight: 600;
  letter-spacing: 1px;
}
.table-top .top-right {
  display: inline-flex;
  align-items: center;
  gap: 1px;
  white-space: nowrap;
}

/* 音效开关：静音时只剩喇叭 + 斜杠 */
.sound-btn {
  width: 28px;
  height: 28px;
  padding: 0;
  margin-left: 2px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  transition: color .15s, background .15s;
}
.sound-btn:hover { color: var(--gold); background: rgba(255, 255, 255, .07); }
.sound-btn .slash { display: none; }
.sound-btn.muted { color: rgba(157, 179, 168, .65); }
.sound-btn.muted .wave { display: none; }
.sound-btn.muted .slash { display: inline; }

.seat {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 14px;
  flex: 0 0 auto;
}
.seat.me { margin-top: auto; }
.seat .avatar {
  width: 34px; height: 34px; border-radius: 50%;
  background: rgba(255,255,255,.1);
  display: flex; align-items: center; justify-content: center;
  font-size: 13px; color: var(--gold);
  border: 1px solid var(--line);
  flex: 0 0 auto;
}
.seat .who { min-width: 0; }
.seat .name { font-size: 13px; color: var(--text); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 40vw; }
.seat .meta { font-size: 11.5px; color: var(--muted); }
.seat .turn-dot {
  width: 7px; height: 7px; border-radius: 50%;
  background: var(--gold);
  box-shadow: 0 0 10px var(--gold);
  animation: pulse 1.2s infinite;
  flex: 0 0 auto;
}
@keyframes pulse { 0%,100% { opacity: 1 } 50% { opacity: .35 } }

.center-area {
  flex: 1 1 auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  position: relative;
  min-height: 0;
  padding: 8px 12px;
}

.deck-info {
  position: absolute;
  left: 14px;
  top: 8px;
  font-size: 11.5px;
  color: var(--muted);
  line-height: 1.7;
}
.score-info {
  position: absolute;
  right: 14px;
  top: 8px;
  font-size: 11.5px;
  color: var(--muted);
  text-align: right;
  line-height: 1.7;
}
.score-info b { color: var(--gold); }

.play-area {
  display: flex;
  gap: 6px;
  align-items: center;
  justify-content: center;
  min-height: 78px;
  flex-wrap: wrap;
}

.pool-badge {
  font-size: 12px;
  color: var(--gold);
  background: rgba(232,180,74,.12);
  border: 1px solid rgba(232,180,74,.3);
  padding: 3px 10px;
  border-radius: 20px;
}

.table-tip {
  font-size: 13px;
  color: var(--muted);
  min-height: 20px;
  text-align: center;
}
.table-tip strong { color: var(--gold); }

/* 牌面 */
.card {
  width: 42px;
  height: 60px;
  border-radius: 6px;
  background: #fbfbf7;
  color: #1c1c1c;
  border: 1px solid rgba(0,0,0,.18);
  box-shadow: 0 2px 6px rgba(0,0,0,.35);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  position: relative;
  flex: 0 0 auto;
  user-select: none;
  transition: transform .12s, box-shadow .12s;
}
.card .rank { font-size: 17px; line-height: 1; }
.card .suit { font-size: 12px; line-height: 1; margin-top: 2px; }
/* 手牌会互相叠压，只露出左边一条：点数必须放左上角，否则叠起来全是白条看不清 */
.hand > .card {
  align-items: flex-start;
  justify-content: flex-start;
  padding: 4px 0 0 3px;
}
.hand > .card .rank { font-size: 15px; letter-spacing: -0.4px; }
.hand > .card .suit { font-size: 11px; margin-top: 1px; }
.card.red { color: #cf3a3a; }
.card.black { color: #1c1c1c; }
.card.joker .rank { font-size: 12px; }
.card.joker .suit { font-size: 11px; }
.card.flower {
  background: linear-gradient(160deg, #fff6d8, #f3d488);
  color: #8a5a00;
}
.card.back {
  background: repeating-linear-gradient(45deg, #1d4a72, #1d4a72 5px, #16395a 5px, #16395a 10px);
  border-color: #0f2c47;
}
.card.mini { width: 34px; height: 48px; }
.card.mini .rank { font-size: 14px; }
.card.mini .suit { font-size: 10px; }

.hand {
  display: flex;
  /* safe center：放得下就居中，放不下就退化成左对齐。
     直接写 center 会在手牌超宽时把最左边几张顶出可视区，且滚不回去 */
  justify-content: safe center;
  align-items: flex-end;
  /* 顶部留白给「选中上浮」：overflow 只认 padding box，不留白就会把抬起的牌裁掉 */
  padding: 20px 14px 10px;
  min-height: 92px;
  overflow-x: auto;
  overflow-y: hidden;   /* 不能写 visible：与 auto 搭配时浏览器会按 auto 处理，仍然裁切 */
  flex: 0 0 auto;
  scrollbar-width: none;
}
.hand::-webkit-scrollbar { display: none; }
.hand .card { cursor: pointer; }
/* 重叠步进由 fitHand() 按容器宽度算出来写进变量：放得下就留缝，放不下就压紧，不横向滚动 */
.hand .card + .card { margin-left: var(--hand-step-extra, -14px); }
.hand .card.sel {
  transform: translateY(-14px);
  box-shadow: 0 0 0 2px var(--gold), 0 8px 18px rgba(0,0,0,.45);
  z-index: 3;           /* 负 margin 叠加时，后面的牌会盖住前一张，选中必须提到最上层 */
}
.hand .card.dim { opacity: .45; }

.hand.opp { padding: 6px 14px 4px; min-height: 56px; }
.hand.opp .card { margin-left: -26px; }
.hand.opp .card:first-child { margin-left: 0; }

/* 手牌折行：一行挤到看不清点数时，由 fitHand() 拆成两行（第一行略多） */
.hand.rows-2 { flex-wrap: wrap; min-height: 0; }
.hand-row {
  display: flex;
  flex: 0 0 100%;
  justify-content: safe center;
  align-items: flex-end;
  min-width: 0;
}
/* 两行之间留一点缝：完全贴住时上下两行会糊成一张牌（一张脸两个点数） */
.hand.rows-2 .hand-row + .hand-row { margin-top: 4px; }

.actions {
  display: flex;
  gap: 10px;
  padding: 10px 14px calc(14px + env(safe-area-inset-bottom));
  flex: 0 0 auto;
  background: linear-gradient(0deg, rgba(0,0,0,.35), transparent);
}
.actions .btn { flex: 1; height: 50px; font-size: 16px; }
/* 求和 / 认输是次要操作，宽度固定、字号小一档，不抢「出牌」的视觉重心 */
.actions .btn.ghost {
  flex: 0 0 auto;
  width: 58px;
  padding: 0;
  font-size: 13.5px;
  color: var(--muted);
  background: rgba(255, 255, 255, .05);
}
.actions .btn.ghost:active { background: rgba(255, 255, 255, .12); }
.actions .btn.ghost.danger { color: #ff9b9b; border-color: rgba(224, 85, 85, .4); }

/* 对手求和请求条：内联在牌桌里，不遮挡手牌 */
.draw-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 0 14px 2px;
  padding: 8px 12px;
  border-radius: 10px;
  background: rgba(232, 180, 74, .12);
  border: 1px solid rgba(232, 180, 74, .32);
  color: var(--gold);
  font-size: 13px;
  flex: 0 0 auto;
}
.draw-bar .txt { flex: 1; line-height: 1.4; }
.draw-bar .btn {
  flex: 0 0 auto;
  height: 32px;
  padding: 0 14px;
  font-size: 13px;
  border-radius: 8px;
}

/* 求和 / 认输的二次确认 */
.confirm-mask { z-index: 80; }
.confirm-lines { font-size: 13px; color: var(--muted); line-height: 1.85; margin-bottom: 14px; }
.confirm-lines b { color: var(--text); }

/* 花牌选择器 */
.sheet-mask {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, .55);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  z-index: 50;
}
.sheet {
  width: 100%;
  max-width: 520px;
  background: var(--panel-solid);
  border-radius: 18px 18px 0 0;
  padding: 16px 16px calc(20px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--line);
}
.sheet h3 { margin-bottom: 4px; color: var(--text); }
.sheet .sub { font-size: 12px; color: var(--muted); margin-bottom: 12px; }
.rank-grid {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 8px;
}
.rank-grid button {
  height: 44px;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: rgba(255,255,255,.06);
  color: var(--text);
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
}
.rank-grid button.on { background: var(--gold); color: #241a05; border-color: transparent; }

/* 结算弹层 */
.result-mask {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.65);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 60;
  padding: 20px;
}
.result {
  width: 100%;
  max-width: 340px;
  background: var(--panel-solid);
  border: 1px solid var(--line);
  border-radius: 18px;
  padding: 22px;
  text-align: center;
  box-shadow: var(--shadow);
}
.result h2 { font-size: 22px; margin-bottom: 4px; }
.result .delta { font-size: 30px; font-weight: 800; margin: 10px 0; }
.result .delta.plus { color: var(--red); }
.result .delta.minus { color: var(--green); }
.result .detail { font-size: 13px; color: var(--muted); line-height: 1.8; margin-bottom: 16px; }

/* 等待遮罩 */
.waiting-box { text-align: center; padding: 10px 0; }
.waiting-box .dots { display: inline-flex; gap: 6px; margin: 10px 0; }
.waiting-box .dots i {
  width: 8px; height: 8px; border-radius: 50%; background: var(--gold);
  animation: bounce 1.1s infinite;
}
.waiting-box .dots i:nth-child(2) { animation-delay: .15s; }
.waiting-box .dots i:nth-child(3) { animation-delay: .3s; }
@keyframes bounce { 0%,100% { transform: translateY(0); opacity: .4 } 50% { transform: translateY(-6px); opacity: 1 } }

.dice {
  width: 54px; height: 54px; border-radius: 12px;
  background: #fbfbf7; color: #1c1c1c;
  display: flex; align-items: center; justify-content: center;
  font-size: 30px; font-weight: 800;
  box-shadow: 0 4px 14px rgba(0,0,0,.4);
}
.dice.rolling { animation: shake .4s infinite; }
@keyframes shake {
  0%,100% { transform: rotate(-6deg) }
  50% { transform: rotate(6deg) }
}

/* 摇骰：双方点数并排展示（等待页与揭晓层共用同一套样式） */
.roll-pair {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 22px;
  margin: 22px 0 4px;
}
.roll-side {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
}
/* 还没摇出来时压暗，摇完点亮，一眼能分清谁摇过了 */
.roll-side .dice { opacity: .4; transition: opacity .18s, box-shadow .2s, transform .2s; }
.roll-side.settled .dice { opacity: 1; }
.roll-side.win .dice {
  opacity: 1;
  box-shadow: 0 0 0 3px var(--gold), 0 8px 22px rgba(232,180,74,.45);
  transform: translateY(-3px);
}
.roll-side.win .roll-who { color: var(--gold); }
.roll-who { font-size: 12.5px; color: var(--muted); }
.roll-vs { font-size: 13px; font-weight: 700; letter-spacing: 1px; color: rgba(232,180,74,.5); }
.roll-tip { line-height: 1.7; }
.roll-tip b { color: var(--gold); }

/* 比点揭晓层：牌桌已在底下渲染好，这层撤掉就是完整牌桌 */
.roll-mask {
  position: fixed;
  inset: 0;
  z-index: 70;
  background: rgba(4, 12, 9, .9);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  /* 不能复用 toast 的 fadeIn：那个动画带 translateX(-50%)，会把整层遮罩左移半屏 */
  animation: maskIn .18s;
}
@keyframes maskIn { from { opacity: 0 } to { opacity: 1 } }
.roll-mask.out { opacity: 0; transition: opacity .26s; pointer-events: none; }
.roll-reveal {
  width: 100%;
  max-width: 330px;
  text-align: center;
  background: var(--panel-solid);
  border: 1px solid var(--line);
  border-radius: 18px;
  padding: 22px 18px 18px;
  box-shadow: var(--shadow);
}
.roll-title { font-size: 13px; letter-spacing: 2px; color: var(--muted); }
.roll-reveal .roll-pair { margin: 18px 0 10px; }
.roll-result {
  min-height: 22px;
  font-size: 14px;
  color: var(--muted);
  opacity: 0;
  transition: opacity .22s;
}
.roll-result.show { opacity: 1; }
.roll-result b { color: var(--gold); }

.toast {
  position: fixed;
  left: 50%;
  top: 22%;
  transform: translateX(-50%);
  background: rgba(0,0,0,.82);
  color: #fff;
  padding: 10px 18px;
  border-radius: 10px;
  font-size: 14px;
  z-index: 100;
  pointer-events: none;
  max-width: 80vw;
  text-align: center;
  animation: fadeIn .15s;
}
@keyframes fadeIn { from { opacity: 0; transform: translate(-50%, -6px) } to { opacity: 1 } }

.hidden { display: none !important; }

/* 结算页 · 再来一局状态提示 */
.rematch-hint {
  min-height: 18px;
  font-size: 12.5px;
  color: var(--gold);
  margin-bottom: 10px;
}

/* ============ 紧凑布局 ============ */
/* 不再引导横屏：手牌能否放全交给 JS 自适应重叠（fitHand）。
   这里只按「可用高度」压缩纵向留白，横屏手机、小窗口会自动受益，竖屏手机不触发。 */
@media (max-height: 560px) {
  .table-top { padding: 2px 18px; }
  .seat { padding: 0 18px; }
  .seat .avatar { width: 28px; height: 28px; font-size: 12px; }
  .center-area { padding: 2px 12px; gap: 4px; }
  .play-area { min-height: 58px; }
  .card.mini { width: 30px; height: 43px; }
  .hand { padding: 18px 18px 6px; min-height: 80px; }
  .card { width: 40px; height: 57px; }
  .actions { padding: 4px 18px 6px; }
  .actions .btn { height: 40px; font-size: 15px; }
  .actions .btn.ghost { width: 54px; font-size: 12.5px; }
  .draw-bar { margin: 0 18px; padding: 6px 10px; font-size: 12px; }
  .draw-bar .btn { height: 28px; padding: 0 12px; font-size: 12px; }
}
@media (max-height: 420px) {
  .hand { padding: 16px 16px 4px; min-height: 72px; }
  .card { width: 36px; height: 51px; }
  .actions { padding: 2px 16px 4px; }
  .actions .btn { height: 36px; font-size: 14px; }
  .actions .btn.ghost { width: 50px; font-size: 12px; }
  .draw-bar { margin: 0 16px; padding: 4px 8px; font-size: 11.5px; }
  .draw-bar .btn { height: 26px; padding: 0 10px; font-size: 11.5px; }
  .seat .meta { font-size: 11px; }
}

@media (max-width: 380px) {
  .card { width: 38px; height: 55px; }
  .card .rank { font-size: 15px; }
  .room-code { font-size: 34px; letter-spacing: 6px; }
}
