:root {
  --landscape: url("../assets/moonlake.webp");
  --lacquer: rgb(13 29 51 / 94%);
  --panel: #162c48;
  --felt1: #1f6aa0;
  --felt2: #134a74;
  --felt3: #0a2940;
  --gold: #f2c14e;
  --gold-ink: #3a2a00;
  --us: #7cd0ff;
  --them: #ff9a7a;
  --red: #c8202b;
  --ink: #1b1b1f;
  --ok: #3ccf7c;
}

* { 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: #f1f5f9;
  background: var(--felt3);
  overflow: hidden;
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}
[hidden] { display: none !important; }

#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));
}
#game { flex: 1; min-height: 0; display: flex; flex-direction: column; }

#counter-tab { position: absolute; z-index: 3; right: 10px; top: 6px; background: var(--lacquer); }
#counter {
  position: fixed; z-index: 9; top: max(6px, env(safe-area-inset-top)); right: env(safe-area-inset-right);
  width: min(220px, 85vw); max-height: calc(100dvh - 14px - env(safe-area-inset-bottom));
  overflow: auto; overscroll-behavior: contain; padding: 12px;
  color: #f4ead6; background: #142e43; border: 1px solid var(--hairline);
  border-radius: 9px 0 0 9px; box-shadow: 0 8px 26px #0006;
}
#counter header { display:flex; justify-content:space-between; align-items:center; }
#counter strong { color:var(--gold); font: bold 20px var(--serif); }
#counter p { font-size:12px; line-height:1.5; margin:7px 0; }
#counter table { width:100%; border-collapse:collapse; font-size:13px; text-align:center; font-variant-numeric: tabular-nums; }
#counter :is(td,th) { padding:3px 1px; border-bottom:1px solid #ffffff22; }
#counter thead th, #counter tbody th { color: var(--gold); }
#counter thead :is(:nth-child(3),:nth-child(5)) { color:#ffbaa8; }
#counter .empty { opacity:.55; }
@media (min-width: 980px) { #game:has(#counter:not([hidden])) { margin-right: 226px; } }
@media (max-height: 510px) { #counter :is(td,th) { padding-block:1px; } }

/* ---------- 按钮 ---------- */
.btn {
  appearance: none;
  border: 0;
  border-radius: 999px;
  padding: 9px 22px;
  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, input:focus-visible, a: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 13px; font-size: 13px; font-weight: 600; }
.btn.wide { display: block; width: 100%; margin-top: 10px; }

/* ---------- 首页 / 大厅 ---------- */
.screen { flex: 1; display: grid; place-items: center; padding: 16px; overflow: auto; }
.panel {
  width: min(100%, 400px);
  padding: 26px 24px 20px;
  border-radius: 18px;
  background: rgba(6, 26, 42, .62);
  border: 1px solid rgba(242, 193, 78, .35);
  box-shadow: 0 24px 60px rgba(0, 0, 0, .45);
}
.panel h1 { margin: 0; font-size: 40px; letter-spacing: .12em; color: var(--gold); text-align: center; }
.panel h1 small { font-size: 16px; letter-spacing: .1em; opacity: .85; }
.panel h2 { margin: 0; text-align: center; font-size: 20px; color: #fff; }
.panel h2 b { color: var(--gold); font-size: 28px; letter-spacing: .14em; margin-left: 6px; -webkit-user-select: text; user-select: text; }
.sub { margin: 8px 0 18px; text-align: center; font-size: 13px; opacity: .75; line-height: 1.5; }
.field { display: block; font-size: 13px; opacity: .9; margin-bottom: 6px; }
input {
  width: 100%;
  margin-top: 6px;
  padding: 10px 14px;
  border-radius: 10px;
  border: 1px solid rgba(255, 255, 255, .25);
  background: rgba(0, 0, 0, .3);
  color: #fff;
  font: inherit;
  font-size: 16px;
  -webkit-user-select: text;
  user-select: text;
}
input::placeholder { color: rgba(255, 255, 255, .4); }
.or { margin: 20px 0 2px; text-align: center; font-size: 12px; letter-spacing: .3em; opacity: .55; }
.join-row { display: flex; gap: 8px; margin-top: 10px; }
.join-row input { margin: 0; text-transform: uppercase; letter-spacing: .12em; }
.hint { min-height: 18px; margin: 12px 0 0; text-align: center; font-size: 13px; color: #ffd28a; line-height: 1.5; }
.links { margin: 12px 0 0; text-align: center; font-size: 13px; opacity: .8; }
.links a { color: #cfe8ff; }

.lobby-table {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  grid-template-areas: ". p2 ." "p3 mid p1" ". p0 .";
  gap: 8px;
  margin: 4px 0 14px;
}
.lobby-seat {
  appearance: none;
  font: inherit;
  color: #fff;
  min-height: 62px;
  padding: 8px 6px;
  border-radius: 12px;
  border: 2px solid var(--them);
  background: rgba(0, 0, 0, .28);
  font-size: 14px;
  font-weight: 700;
  line-height: 1.35;
  cursor: default;
  overflow: hidden;
}
.lobby-seat small { display: block; font-weight: 400; font-size: 11px; opacity: .7; }
.lobby-seat.us { border-color: var(--us); }
.lobby-seat.empty { border-style: dashed; opacity: .75; font-weight: 400; cursor: pointer; }
.lobby-seat.empty:hover { opacity: 1; }
.pos0 { grid-area: p0; } .pos1 { grid-area: p1; } .pos2 { grid-area: p2; } .pos3 { grid-area: p3; }
.lobby-mid { grid-area: mid; display: grid; place-items: center; border-radius: 50%; background: rgba(255, 255, 255, .06); font-size: 12px; opacity: .6; }

/* ---------- 顶栏 ---------- */
#topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 7px 12px;
  background: rgba(0, 0, 0, .3);
  border-bottom: 1px solid rgba(255, 255, 255, .08);
  flex: none;
}
.scoreboard { display: flex; align-items: center; gap: 7px; font-size: 13px; flex-wrap: wrap; }
.team-chip, .trump-chip, .points-chip {
  display: flex;
  align-items: baseline;
  gap: 5px;
  padding: 3px 11px;
  border-radius: 999px;
  background: rgba(255, 255, 255, .09);
  border: 1px solid transparent;
  white-space: nowrap;
}
.team-chip b { font-size: 17px; }
.team-chip.us b { color: var(--us); }
.team-chip.them b { color: var(--them); }
.team-chip.dealer { border-color: rgba(242, 193, 78, .85); }
.team-chip.dealer::after { content: '庄'; font-size: 11px; color: var(--gold); font-weight: 800; }
.trump-chip { background: linear-gradient(#f7d06a, #e2a92c); color: var(--gold-ink); font-weight: 700; }
.trump-chip b { font-size: 17px; }
.trump-chip b.red { color: var(--red); }
.points-chip b { font-size: 17px; color: var(--gold); font-variant-numeric: tabular-nums; }
.top-actions { display: flex; gap: 5px; flex: none; }

/* ---------- 牌 ---------- */
.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) * .05);
  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.tr { background: linear-gradient(#fff3c4 0, #fff3c4 7%, #ffffff 7%, #f1eee5); }
.card.lvl { background: linear-gradient(#ffd45e 0, #ffd45e 7%, #fffbe8 7%, #fbf0c8); }
.card.pt .idx .r { text-decoration: underline; text-decoration-thickness: 2px; text-underline-offset: 2px; }

/* ---------- 牌桌 ---------- */
#arena { position: relative; flex: 1; min-height: 220px; }
#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: 8px; left: 50%; transform: translateX(-50%); }
.pos-left { left: 10px; top: 42%; transform: translateY(-50%); flex-direction: column; }
.pos-right { right: 10px; top: 42%; transform: translateY(-50%); flex-direction: column; }
.avatar {
  position: relative;
  width: 48px;
  height: 48px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  font-size: 19px;
  font-weight: 800;
  color: #fff;
  background: radial-gradient(circle at 35% 30%, #3b5d78, #142c3d);
  border: 3px solid var(--them);
  box-shadow: 0 3px 8px rgba(0, 0, 0, .4);
}
.seat.us .avatar { border-color: var(--us); }
.seat.active .avatar { animation: pulse 1.2s ease-in-out infinite; }
@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; max-width: 9em; overflow: hidden; text-overflow: ellipsis; }
.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, #2f6fb0, #2f6fb0 2px, #dfeaf5 2px, #dfeaf5 4px);
  box-shadow: 0 0 0 1px #fff;
}
.badge {
  position: absolute;
  top: -8px;
  right: -12px;
  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(36px, 4.4vw, 54px);
  pointer-events: none;
  z-index: 1;
}
.play .cards { display: flex; border-radius: 8px; animation: drop .18s ease-out; }
.play .cards .card + .card { margin-left: calc(var(--cw) * -.5); }
.play.win .cards { box-shadow: 0 0 0 3px var(--gold), 0 0 22px rgba(242, 193, 78, .7); }
.play .cap {
  padding: 1px 10px;
  border-radius: 999px;
  background: rgba(0, 0, 0, .55);
  font-size: 12px;
  font-weight: 700;
  white-space: nowrap;
}
.play.win .cap { background: var(--gold); color: var(--gold-ink); }
#play-2 { top: 72px; left: 50%; transform: translateX(-50%); }
#play-3 { left: 96px; top: 42%; transform: translateY(-50%); align-items: flex-start; }
#play-1 { right: 96px; top: 42%; 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 10px calc(8px + env(safe-area-inset-bottom)); }
#status { min-height: 20px; text-align: center; font-size: 13px; color: rgba(255, 255, 255, .85); }
#me-bar {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 46px;
}
#actions { display: flex; gap: 10px; flex-wrap: wrap; justify-content: center; }
#me-chip {
  position: absolute;
  left: 0;
  display: flex;
  align-items: center;
  gap: 7px;
  padding: 4px 13px 4px 10px;
  border-radius: 999px;
  background: rgba(0, 0, 0, .3);
  font-weight: 700;
  font-size: 14px;
  max-width: 30vw;
}
#me-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#me-chip .dot { width: 10px; height: 10px; border-radius: 50%; background: var(--us); flex: none; }
#me-chip.active { box-shadow: 0 0 0 2px var(--gold), 0 0 16px rgba(242, 193, 78, .6); }
#me-chip .badge { position: static; }
#hand {
  position: relative;
  margin-top: 22px;
  --cw: clamp(42px, 5vw, 68px);
  touch-action: none;
  isolation: isolate;
}
#hand .card { position: absolute; cursor: pointer; transition: transform .1s ease-out, box-shadow .1s; }
#hand .card.sel {
  transform: translateY(calc(var(--cw) * -.3));
  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 var(--ok);
  pointer-events: none;
}

/* ---------- 弹窗 / 提示 ---------- */
#overlay {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: grid;
  place-items: center;
  background: rgba(2, 12, 20, .62);
  backdrop-filter: blur(3px);
}
#overlay.hidden { display: none; }
.modal {
  width: min(92vw, 480px);
  max-height: 88vh;
  overflow: auto;
  padding: 20px 22px 18px;
  border-radius: 16px;
  background: linear-gradient(#18547f, #0e3552);
  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; }
.modal .score { text-align: center; font-size: 15px; }
.modal .score b { font-size: 34px; color: var(--gold); margin: 0 6px; font-variant-numeric: tabular-nums; }
.row-cards { display: flex; justify-content: center; flex-wrap: wrap; --cw: 38px; padding-left: 16px; row-gap: 6px; }
.row-cards .card { margin-left: -16px; }
.trick-line { display: flex; align-items: center; gap: 12px; margin: 8px 0; font-size: 14px; }
.trick-line .who { width: 6em; text-align: right; font-weight: 700; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.trick-line.win .who { color: var(--gold); }
.trick-line .row-cards { justify-content: flex-start; }

#toast {
  position: fixed;
  left: 50%;
  top: 34%;
  transform: translate(-50%, -12px);
  z-index: 60;
  padding: 8px 20px;
  border-radius: 999px;
  background: rgba(0, 0, 0, .8);
  font-size: 14px;
  font-weight: 600;
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s, transform .2s;
  max-width: 92vw;
  text-align: center;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ---------- 小屏 ---------- */
@media (max-width: 720px) {
  #topbar { padding: 6px 8px; align-items: flex-start; }
  .team-chip, .trump-chip, .points-chip { padding: 2px 8px; font-size: 12px; }
  .team-chip b, .trump-chip b, .points-chip b { font-size: 15px; }
  .scoreboard { gap: 5px; }
  .avatar { width: 40px; height: 40px; font-size: 16px; }
  .pos-left { left: 4px; }
  .pos-right { right: 4px; }
  #play-3 { left: 62px; }
  #play-1 { right: 62px; }
  #play-2 { top: 62px; }
  .btn { padding: 8px 16px; font-size: 14px; }
  .btn.ghost { padding: 5px 9px; font-size: 12px; }
  .top-actions { flex-wrap: wrap; justify-content: flex-end; max-width: 42vw; }
  #me-chip { position: static; margin-right: 8px; flex: none; max-width: 24vw; }
  #actions { gap: 7px; }
}
@media (max-height: 560px) {
  #arena { min-height: 160px; }
  .pos-top { top: 2px; }
  #play-2 { top: 56px; }
  #hand { margin-top: 16px; }
}

/* 月下牌局入口 */
#home {
  background: linear-gradient(90deg, rgb(8 22 43 / 40%), transparent 75%);
  grid-template-columns: minmax(0, 1fr) minmax(285px, 375px);
  gap: 28px;
  padding: max(24px, 5vh) clamp(25px, 9vw, 150px);
}
.home-title {
  justify-self: start;
  color: #fff0ca;
  text-shadow: 0 3px 18px #09162b;
}
.home-title h1 {
  font: 700 clamp(62px, 8vw, 126px)/1.1 var(--serif);
  margin: 0 0 12px;
  letter-spacing: .12em;
}
.home-title .sub { text-align: left; margin: 0; font-size: 15px; opacity: 1; letter-spacing: .12em; }
.home-title::after { content: ''; display: block; width: 88px; margin-top: 20px; border-bottom: 3px double #dfc487; }
.screen { min-height: 0; }
.screen .panel {
  margin-block: auto;
  background: linear-gradient(145deg, rgb(29 51 80 / 97%), rgb(13 28 47 / 97%));
  border: 1px solid #b49c74;
  border-radius: 13px;
  box-shadow: 0 17px 65px #060e2066, inset 0 0 0 4px #13263c, inset 0 0 0 5px #d3b88438;
}
#home .btn.wide { min-height: 46px; }
#btn-solo small { font-size: 12px; font-weight: 500; margin-left: 8px; }
#home .or { display: flex; align-items: center; gap: 12px; color: #e4d6b9; opacity: 1; }
#home .or::before, #home .or::after { content: ''; flex: 1; border-top: 1px solid #bcac8b4d; }
.hint:empty { display: none; }
.links { margin-top: 18px; }
.links a { text-underline-offset: 4px; }
@media (orientation: portrait) and (max-width: 740px) {
  #home {
    background: linear-gradient(180deg, transparent, rgb(8 22 43 / 48%) 32%, transparent 62%);
    display: flex; flex-direction: column; align-items: center; justify-content: safe center;
    padding: max(20px, 6vh) 20px max(16px, env(safe-area-inset-bottom)); gap: clamp(20px, 5vh, 50px);
  }
  .home-title { flex: none; text-align: center; }
  .home-title .sub { text-align: center; font-size: 13px; }
  .home-title::after { margin: 12px auto 0; }
  #home .panel { margin-block: 0; }
}
@media (orientation: landscape) and (max-height: 550px) {
  #home { gap: 16px; padding: 10px 5vw max(10px, env(safe-area-inset-bottom)); }
  .screen .panel { padding: 16px; }
  #home .or { margin-top: 10px; }
  #home .btn.wide { min-height: 36px; margin-top: 7px; }
  #home .links { margin-top: 8px; }
  #lobby { place-items: start center; }
}
