:root {
  --landscape: url("../assets/jiangnan.webp");
  --lacquer: rgb(8 35 29 / 94%);
  --panel: #123b32;
  --felt1: #1c7d53;
  --felt2: #0d4c32;
  --felt3: #062a1b;
  --gold: #f2c14e;
  --gold-ink: #3a2a00;
  --us: #6cb6ff;
  --them: #ff8a6b;
  --red: #c8202b;
  --ink: #1b1b1f;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: "PingFang SC", "Microsoft YaHei", "Noto Sans SC", "Segoe UI", system-ui, sans-serif;
  color: #f4f1e6;
  background: var(--felt3);
  overflow: hidden;
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

#app {
  height: 100vh;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  background:
    radial-gradient(120% 85% at 50% 38%, var(--felt1), var(--felt2) 58%, var(--felt3));
}

/* ---------- 顶栏 ---------- */
#topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 8px 14px;
  background: rgba(0, 0, 0, .3);
  border-bottom: 1px solid rgba(255, 255, 255, .08);
  flex: none;
}
.brand {
  font-size: 20px;
  font-weight: 800;
  letter-spacing: .18em;
  color: var(--gold);
  text-shadow: 0 1px 0 rgba(0, 0, 0, .4);
}
.scoreboard { display: flex; align-items: center; gap: 8px; font-size: 13px; }
.team-chip {
  display: flex;
  align-items: baseline;
  gap: 6px;
  padding: 3px 12px;
  border-radius: 999px;
  background: rgba(255, 255, 255, .08);
  border: 1px solid transparent;
  white-space: nowrap;
}
.team-chip b { font-size: 18px; }
.team-chip.us b { color: var(--us); }
.team-chip.them b { color: var(--them); }
.team-chip.dealer { border-color: rgba(242, 193, 78, .8); }
.level-now {
  display: flex;
  align-items: baseline;
  gap: 6px;
  padding: 3px 14px;
  border-radius: 999px;
  background: linear-gradient(#f7d06a, #e2a92c);
  color: var(--gold-ink);
  font-weight: 700;
  white-space: nowrap;
}
.level-now b { font-size: 18px; }
.wild-chip { font-weight: 600; font-size: 12px; opacity: .85; margin-left: 4px; }
.wild-chip i { font-style: normal; color: var(--red); font-weight: 800; font-size: 14px; }
.top-actions { display: flex; gap: 6px; }

/* ---------- 按钮 ---------- */
.btn {
  appearance: none;
  border: 0;
  border-radius: 999px;
  padding: 9px 24px;
  font: inherit;
  font-weight: 700;
  font-size: 15px;
  color: #fff;
  cursor: pointer;
  background: rgba(255, 255, 255, .14);
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, .24);
  transition: transform .08s, filter .15s, opacity .15s;
  white-space: nowrap;
}
.btn:hover:not(:disabled) { filter: brightness(1.15); }
.btn:active:not(:disabled) { transform: translateY(1px) scale(.98); }
.btn:disabled { opacity: .35; cursor: not-allowed; }
.btn:focus-visible { outline: 2px solid var(--gold); outline-offset: 2px; }
.btn.primary {
  background: linear-gradient(#f7d06a, #e2a92c);
  color: var(--gold-ink);
  box-shadow: 0 2px 0 #9c6f10, 0 6px 14px rgba(0, 0, 0, .3);
}
.btn.ghost { padding: 6px 14px; font-size: 13px; font-weight: 600; }
.btn[hidden] { display: none; }
a.btn { text-decoration: none; display: inline-block; }

/* ---------- 牌 ---------- */
.card {
  position: relative;
  flex: none;
  width: var(--cw);
  height: calc(var(--cw) * 1.42);
  border-radius: calc(var(--cw) * .1);
  background: linear-gradient(#ffffff, #f1eee5);
  color: var(--ink);
  box-shadow: 0 0 0 1px rgba(0, 0, 0, .28), 0 2px 4px rgba(0, 0, 0, .3);
  overflow: hidden;
}
.card.red { color: var(--red); }
.card .idx {
  position: absolute;
  left: calc(var(--cw) * .06);
  top: calc(var(--cw) * .05);
  min-width: calc(var(--cw) * .3);
  display: flex;
  flex-direction: column;
  align-items: center;
  line-height: 1;
  font-weight: 800;
}
.card .idx .r { font-size: calc(var(--cw) * .34); letter-spacing: -.06em; }
.card .idx .s { font-size: calc(var(--cw) * .26); margin-top: calc(var(--cw) * .01); }
.card .idx .jk { font-size: calc(var(--cw) * .24); line-height: 1.08; text-align: center; }
.card .pip {
  position: absolute;
  right: 7%;
  bottom: 5%;
  font-size: calc(var(--cw) * .62);
  line-height: 1;
}
.card.joker .pip { font-size: calc(var(--cw) * .5); }
.card.lvl { background: linear-gradient(#fffdf2, #fbf0c8); }
.card.wild {
  background: linear-gradient(#fff6cf, #ffdf85);
  box-shadow: 0 0 0 2px #d99a0b, 0 2px 4px rgba(0, 0, 0, .3);
}
.card .tag {
  position: absolute;
  right: 5%;
  top: 4%;
  width: calc(var(--cw) * .32);
  height: calc(var(--cw) * .32);
  display: grid;
  place-items: center;
  border-radius: 50%;
  background: #d99a0b;
  color: #fff;
  font-size: calc(var(--cw) * .19);
  font-weight: 800;
}

/* ---------- 牌桌 ---------- */
#arena { position: relative; flex: 1; min-height: 230px; }
#arena::before {
  content: '';
  position: absolute;
  inset: 5% 8%;
  border-radius: 50%;
  border: 2px solid rgba(242, 193, 78, .13);
  box-shadow: inset 0 0 90px rgba(0, 0, 0, .2);
  pointer-events: none;
}
.seat { position: absolute; display: flex; align-items: center; gap: 8px; z-index: 2; }
.pos-top { top: 10px; left: 50%; transform: translateX(-50%); }
.pos-left { left: 12px; top: 44%; transform: translateY(-50%); flex-direction: column; }
.pos-right { right: 12px; top: 44%; transform: translateY(-50%); flex-direction: column; }
.avatar {
  position: relative;
  width: 50px;
  height: 50px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  font-size: 20px;
  font-weight: 800;
  color: #fff;
  background: radial-gradient(circle at 35% 30%, #3a5f52, #16332a);
  border: 3px solid var(--them);
  box-shadow: 0 3px 8px rgba(0, 0, 0, .4);
  transition: box-shadow .2s;
}
.team-us .avatar { border-color: var(--us); }
.seat.active .avatar { animation: pulse 1.2s ease-in-out infinite; }
.seat.done .avatar { opacity: .55; }
@keyframes pulse {
  0%, 100% { box-shadow: 0 0 0 3px rgba(242, 193, 78, .95), 0 0 14px rgba(242, 193, 78, .5); }
  50% { box-shadow: 0 0 0 5px rgba(242, 193, 78, .7), 0 0 26px rgba(242, 193, 78, .8); }
}
.meta { text-align: center; font-size: 13px; line-height: 1.35; }
.pos-top .meta { text-align: left; }
.name { font-weight: 700; white-space: nowrap; }
.name small { font-weight: 400; opacity: .65; margin-left: 5px; }
.count {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 1px 9px;
  border-radius: 999px;
  background: rgba(0, 0, 0, .35);
  font-variant-numeric: tabular-nums;
}
.count .back {
  width: 10px;
  height: 14px;
  border-radius: 2px;
  background: repeating-linear-gradient(45deg, #b83a3a, #b83a3a 2px, #f0dfb8 2px, #f0dfb8 4px);
  box-shadow: 0 0 0 1px #fff;
}
.count.low { background: rgba(200, 32, 43, .85); }
.badge {
  position: absolute;
  top: -8px;
  right: -16px;
  padding: 2px 7px;
  border-radius: 999px;
  background: var(--gold);
  color: var(--gold-ink);
  font-size: 11px;
  font-weight: 800;
  white-space: nowrap;
  box-shadow: 0 2px 4px rgba(0, 0, 0, .35);
}
.badge:empty { display: none; }

.play {
  position: absolute;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  --cw: clamp(38px, 4.6vw, 56px);
  pointer-events: none;
  z-index: 1;
}
.play .cards { display: flex; animation: drop .18s ease-out; }
.play .cards .card + .card { margin-left: calc(var(--cw) * -.5); }
.play .cap {
  padding: 1px 10px;
  border-radius: 999px;
  background: rgba(200, 32, 43, .9);
  font-size: 12px;
  font-weight: 700;
  letter-spacing: .1em;
}
.play .pass {
  padding: 4px 14px;
  border-radius: 999px;
  background: rgba(0, 0, 0, .28);
  color: rgba(255, 255, 255, .8);
  font-weight: 700;
  letter-spacing: .25em;
  text-indent: .25em;
  animation: drop .18s ease-out;
}
#play-2 { top: 78px; left: 50%; transform: translateX(-50%); }
#play-3 { left: 104px; top: 44%; transform: translateY(-50%); align-items: flex-start; }
#play-1 { right: 104px; top: 44%; transform: translateY(-50%); align-items: flex-end; }
#play-0 { bottom: 4px; left: 50%; transform: translateX(-50%); flex-direction: column-reverse; }
@keyframes drop {
  from { opacity: 0; transform: scale(.85); }
  to { opacity: 1; transform: none; }
}

/* ---------- 自己 ---------- */
#me { flex: none; padding: 2px 12px calc(10px + env(safe-area-inset-bottom)); }
#status {
  min-height: 20px;
  text-align: center;
  font-size: 13px;
  color: rgba(255, 255, 255, .82);
}
#me-bar {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 48px;
}
#actions { display: flex; gap: 12px; }
#me-chip {
  position: absolute;
  left: 0;
  display: flex;
  align-items: center;
  gap: 7px;
  padding: 4px 14px 4px 10px;
  border-radius: 999px;
  background: rgba(0, 0, 0, .3);
  font-weight: 700;
  font-size: 14px;
}
#me-chip .dot { width: 10px; height: 10px; border-radius: 50%; background: var(--us); }
#me-chip.active { box-shadow: 0 0 0 2px var(--gold), 0 0 16px rgba(242, 193, 78, .6); }
#me-chip .badge { position: static; }
#me-count { position: absolute; right: 0; font-size: 13px; opacity: .75; font-variant-numeric: tabular-nums; }
#hand {
  position: relative;
  margin-top: 24px;
  --cw: clamp(44px, 5.4vw, 72px);
  touch-action: none;
  isolation: isolate;
  transition: height .2s;
}
#hand .card {
  position: absolute;
  cursor: pointer;
  transition: transform .1s ease-out, box-shadow .1s;
}
#hand .card:hover { filter: brightness(1.04); }
#hand .card.sel {
  transform: translateY(calc(var(--cw) * -.28));
  box-shadow: 0 0 0 2.5px var(--gold), 0 8px 16px rgba(0, 0, 0, .45);
}
#hand .card.fresh::after {
  content: '';
  position: absolute;
  inset: 0;
  border-radius: inherit;
  box-shadow: inset 0 0 0 3px #39c06f;
  pointer-events: none;
}
#hand.done { height: auto !important; margin-top: 6px; padding: 18px 0 14px; text-align: center; font-size: 16px; color: var(--gold); font-weight: 700; }

/* ---------- 弹窗 / 提示 ---------- */
#overlay {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: grid;
  place-items: center;
  background: rgba(3, 14, 9, .62);
  backdrop-filter: blur(3px);
}
#overlay.hidden { display: none; }
.modal {
  width: min(92vw, 470px);
  max-height: 88vh;
  overflow: auto;
  padding: 20px 22px 18px;
  border-radius: 16px;
  background: linear-gradient(#17593c, #0d3a27);
  border: 1px solid rgba(242, 193, 78, .45);
  box-shadow: 0 24px 60px rgba(0, 0, 0, .55);
  animation: drop .18s ease-out;
  -webkit-user-select: text;
  user-select: text;
}
.modal h2 { margin: 0 0 12px; text-align: center; font-size: 21px; letter-spacing: .1em; color: var(--gold); }
.modal p { margin: 8px 0; line-height: 1.6; font-size: 14px; }
.modal h3 { margin: 14px 0 4px; font-size: 14px; color: var(--gold); }
.modal ul { margin: 4px 0; padding-left: 20px; font-size: 13.5px; line-height: 1.7; }
.modal .btns { display: flex; justify-content: center; flex-wrap: wrap; gap: 10px; margin-top: 16px; }
.modal .center { text-align: center; }
.modal .big { font-size: 17px; font-weight: 700; }
.rank-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; }
.rank-list li {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 7px 14px;
  border-radius: 10px;
  background: rgba(0, 0, 0, .22);
  border-left: 4px solid var(--them);
  font-size: 15px;
}
.rank-list li.us { border-left-color: var(--us); }
.rank-list .place { width: 3em; font-weight: 800; color: var(--gold); }
.rank-list .side { margin-left: auto; font-size: 12px; opacity: .7; }
.trib-line { display: flex; align-items: center; justify-content: center; gap: 10px; margin: 10px 0; font-size: 15px; }
.trib-line .card { --cw: 40px; }
.trib-line b { color: var(--gold); }

#toast {
  position: fixed;
  left: 50%;
  top: 36%;
  transform: translate(-50%, -12px);
  z-index: 60;
  padding: 8px 20px;
  border-radius: 999px;
  background: rgba(0, 0, 0, .78);
  font-size: 14px;
  font-weight: 600;
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s, transform .2s;
  white-space: nowrap;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ---------- 小屏 ---------- */
@media (max-width: 720px) {
  .brand { display: none; }
  #topbar { padding: 6px 8px; }
  .team-chip, .level-now { padding: 3px 9px; }
  .wild-chip { display: block; margin: 0; }
  .level-now { flex-wrap: wrap; justify-content: center; border-radius: 14px; }
  .avatar { width: 40px; height: 40px; font-size: 16px; }
  .pos-left { left: 6px; }
  .pos-right { right: 6px; }
  #play-3 { left: 70px; }
  #play-1 { right: 70px; }
  #play-2 { top: 64px; }
  .btn { padding: 8px 18px; font-size: 14px; }
  .btn.ghost { padding: 5px 10px; font-size: 12px; }
  #me-count { display: none; }
  #me-chip { position: static; margin-right: 8px; padding: 4px 10px 4px 8px; }
  #actions { gap: 8px; }
}
@media (max-height: 560px) {
  #arena { min-height: 170px; }
  .pos-top { top: 4px; }
  #play-2 { top: 60px; }
  #hand { margin-top: 12px; }
}

@media (orientation: portrait) {
  #app { --landscape: none; }
  #arena { background: url("../assets/jiangnan-portrait.webp") center / cover; }
  #hand .card .idx { top: 1px; }
  #hand .card .idx .s { font-size: calc(var(--cw) * .24); margin-top: 0; }
}

/* 计牌器与联机房间 */
#arena .counter-tab { position: absolute; right: 13px; top: 10px; z-index: 3; background: var(--lacquer); }
#counter { position: fixed; z-index: 9; right: env(safe-area-inset-right); top: max(8px, env(safe-area-inset-top)); width: 205px; max-height: calc(100dvh - 16px - env(safe-area-inset-bottom)); overflow: auto; background: #12372e; padding: 12px; border: 1px solid #a18b5c; border-radius: 9px 0 0 9px; box-shadow: 0 8px 28px #0006; }
#counter header, .room-controls { display:flex; align-items:center; justify-content:space-between; gap:6px; }
#counter header strong { color:var(--gold); font: bold 20px var(--serif); }
#counter p, #room-dialog p { font-size:13px; line-height:1.55; }
#counter table { width:100%; border-collapse:collapse; text-align:center; font-size:13px; font-variant-numeric:tabular-nums; }
#counter :is(td,th) { padding:3px 2px; border-bottom:1px solid #ffffff19; }
#counter thead th, #counter tbody b { color:var(--gold); }
#counter tr.empty { opacity:.55; }
#counter thead th:nth-child(3), #counter thead th:nth-child(5) { color: #ffc0ac; }
@media (min-width: 950px) {
  .counter-open #app { margin-right:210px; }
  .counter-open #counter-tab { visibility:hidden; }
}
#room-dialog { width:min(365px,94vw); }
#room-dialog [hidden] { display:none; }
#room-entry { display:grid; grid-template-columns:1fr auto; gap:8px; align-items:end; }
#room-entry label { display:block; font-size:13px; border:none; padding:0; }
#room-entry input { display:block; width:100%; margin-top:4px; padding:8px; font:inherit; font-size:16px; border-radius:5px; border:1px solid #b6a688; }
#room-dialog footer a { color:var(--gold); }
#room-dialog footer { font-size:13px; }
#room-seats { padding-left:1.6em; line-height:1.8; }
#room-dialog .btn { padding-inline:12px; }
#room-dialog #room-message { color:var(--gold); }
@media (orientation:landscape) and (max-height:550px) { #counter { width:235px; } #counter td, #counter th { padding-block:1px; } }
