:root {
  --felt-1: #2f8f5b;
  --felt-2: #17603a;
  --felt-3: #0c3a23;
  --wood: #5b3a1e;
  --ink: #1d1d1d;
  --gold: #f5c542;
  --wan: #c0392b;
  --tiao: #1e8449;
  --tong: #1f5fa8;
  --panel: rgba(0, 0, 0, .28);
  --panel-line: rgba(255, 255, 255, .12);
  --text: #f3f7f2;
  --muted: rgba(243, 247, 242, .65);
  --tw: clamp(26px, 4.4vw, 50px);
  --th: calc(var(--tw) * 1.38);
  --sw: clamp(17px, 2.2vw, 30px);
  --sh: calc(var(--sw) * 1.36);
  --top-h: 48px;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: "PingFang SC", "Microsoft YaHei", "Noto Sans SC", system-ui, sans-serif;
  color: var(--text);
  background: var(--felt-3);
  overflow: hidden;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
}
button { font: inherit; cursor: pointer; }
[hidden] { display: none !important; }

#app { height: 100vh; height: 100dvh; display: flex; flex-direction: column; }

/* ---------- 顶栏 ---------- */
.topbar {
  height: var(--top-h);
  flex: none;
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 0 16px;
  background: linear-gradient(#4a2f17, #3a2410);
  border-bottom: 2px solid #23160a;
  box-shadow: 0 2px 10px rgba(0, 0, 0, .4);
  z-index: 5;
}
.brand { font-size: 20px; font-weight: 800; letter-spacing: 2px; color: var(--gold); white-space: nowrap; }
.brand span { font-size: 12px; font-weight: 500; color: var(--muted); margin-left: 8px; letter-spacing: 1px; }
.meta { flex: 1; font-size: 13px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.meta b { color: var(--text); font-weight: 600; }
.tools { display: flex; gap: 6px; }
.tool {
  border: 1px solid rgba(255, 255, 255, .2);
  background: rgba(255, 255, 255, .07);
  color: var(--text);
  border-radius: 8px;
  padding: 5px 12px;
  font-size: 13px;
}
.tool:hover { background: rgba(255, 255, 255, .15); }
.tool.on { background: var(--gold); color: #3a2410; border-color: var(--gold); font-weight: 700; }

/* ---------- 牌桌布局 ---------- */
#table {
  position: relative;
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(64px, auto) 1fr minmax(64px, auto);
  grid-template-rows: auto 1fr auto;
  grid-template-areas:
    "top top top"
    "left center right"
    "bottom bottom bottom";
  gap: 6px;
  padding: 8px 16px calc(14px + env(safe-area-inset-bottom, 0px));
  background:
    radial-gradient(ellipse at 50% 42%, var(--felt-1), var(--felt-2) 60%, var(--felt-3) 100%);
}
#table::after {
  content: "";
  position: absolute;
  inset: 0;
  pointer-events: none;
  background-image: radial-gradient(rgba(255, 255, 255, .025) 1px, transparent 1px);
  background-size: 4px 4px;
}
.seat-top { grid-area: top; }
.seat-left { grid-area: left; }
.seat-right { grid-area: right; }
.seat-bottom { grid-area: bottom; }
.center { grid-area: center; }

.seat { display: flex; align-items: center; gap: 10px; min-width: 0; z-index: 1; }
.seat-top { justify-content: center; }
.seat-left, .seat-right { flex-direction: column; justify-content: center; }
.seat-bottom { flex-direction: column; align-items: stretch; gap: 6px; }

/* ---------- 玩家信息 ---------- */
.pinfo {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 5px 10px 5px 5px;
  background: var(--panel);
  border: 1px solid var(--panel-line);
  border-radius: 999px;
  white-space: nowrap;
  transition: box-shadow .2s, border-color .2s;
}
.pinfo.turn { border-color: var(--gold); box-shadow: 0 0 0 2px rgba(245, 197, 66, .35), 0 0 18px rgba(245, 197, 66, .35); }
.seat-left .pinfo, .seat-right .pinfo { flex-direction: column; border-radius: 14px; padding: 8px; gap: 4px; text-align: center; }
.avatar {
  width: 34px; height: 34px; border-radius: 50%;
  display: grid; place-items: center;
  font-weight: 800; font-size: 15px; color: #fff;
  border: 2px solid rgba(255, 255, 255, .7);
  flex: none;
}
.av-0 { background: linear-gradient(135deg, #e67e22, #b9561b); }
.av-1 { background: linear-gradient(135deg, #8e44ad, #5b2c6f); }
.av-2 { background: linear-gradient(135deg, #2980b9, #1a5276); }
.av-3 { background: linear-gradient(135deg, #c0392b, #7b241c); }
.pname { font-size: 13px; font-weight: 700; display: flex; align-items: center; gap: 4px; flex-wrap: wrap; justify-content: center; }
.pscore { font-size: 13px; color: var(--gold); font-weight: 700; font-variant-numeric: tabular-nums; }
.pscore small { color: var(--muted); font-weight: 500; margin-left: 4px; }
.badge { display: inline-block; font-size: 11px; font-weight: 700; padding: 1px 5px; border-radius: 4px; line-height: 1.4; }
.badge.dealer { background: var(--gold); color: #5b3a1e; }
.badge.que { background: #fff; }
.badge.q-wan { color: var(--wan); }
.badge.q-tiao { color: var(--tiao); }
.badge.q-tong { color: var(--tong); }
.badge.ting { background: #16a085; color: #fff; }
.hu-badge {
  font-size: 12px; font-weight: 800; color: #fff;
  background: linear-gradient(#e74c3c, #a93226);
  padding: 2px 8px; border-radius: 999px;
}

/* ---------- 牌 ---------- */
.tile {
  position: relative;
  width: var(--tw);
  height: var(--th);
  flex: none;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  line-height: 1;
  font-size: calc(var(--tw) * .5);
  background: linear-gradient(180deg, #fffef9 0%, #f4efdc 85%, #e6dfc4 100%);
  border-radius: calc(var(--tw) * .14);
  border: 1px solid #cfc6a6;
  box-shadow: 0 calc(var(--tw) * .07) 0 #2a7a4b, 0 calc(var(--tw) * .1) calc(var(--tw) * .14) rgba(0, 0, 0, .4);
  color: var(--ink);
  transition: transform .12s ease, box-shadow .12s ease, opacity .2s;
}
.tile .tn { font-weight: 800; font-family: "STKaiti", "KaiTi", "Kaiti SC", serif; }
.tile .ts { font-size: .62em; font-weight: 700; margin-top: .14em; color: #3b3b3b; }
.tile.s-wan .tn { color: var(--wan); }
.tile.s-tiao .tn { color: var(--tiao); }
.tile.s-tong .tn { color: var(--tong); }
.tile.s-tiao .ts { color: var(--tiao); }
.tile.s-tong .ts { color: var(--tong); }
.tile.yaoji {
  background: linear-gradient(180deg, #fff9df 0%, #fbe7a1 100%);
  border-color: #d4a017;
}
.tile.yaoji .tn { color: var(--wan); }
.tile.yaoji .ts { color: var(--tiao); }
.tile.yaoji::after {
  content: "★";
  position: absolute;
  top: 1px; right: 2px;
  font-size: .32em;
  color: #d4a017;
}
.tile.sm { width: var(--sw); height: var(--sh); font-size: calc(var(--sw) * .5); border-radius: calc(var(--sw) * .14);
  box-shadow: 0 calc(var(--sw) * .07) 0 #2a7a4b, 0 1px 3px rgba(0, 0, 0, .35); }
.tile.back {
  background: linear-gradient(160deg, #47b07a, #1f6b43);
  border-color: #175a37;
  box-shadow: 0 calc(var(--sw) * .07) 0 #e8e0c4, 0 1px 3px rgba(0, 0, 0, .35);
}
.tile.back::after { content: none; }
.tile.last { box-shadow: 0 0 0 2px var(--gold), 0 0 12px 2px rgba(245, 197, 66, .85); animation: pop .35s ease; }
.tile.hu { box-shadow: 0 0 0 2px #e74c3c, 0 0 10px rgba(231, 76, 60, .8); }
@keyframes pop { from { transform: scale(1.35); } to { transform: scale(1); } }

/* ---------- 电脑手牌 / 副露 ---------- */
.ptiles { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; justify-content: center; }
.seat-left .ptiles, .seat-right .ptiles { flex-direction: column; }
.phand { display: flex; gap: 1px; flex-wrap: wrap; justify-content: center; }
.seat-left .phand, .seat-right .phand { max-width: calc(var(--sw) * 4 + 3px); }
.melds { display: flex; gap: 6px; flex-wrap: wrap; justify-content: center; }
.seat-left .melds, .seat-right .melds { flex-direction: column; align-items: center; }
.meld { display: flex; gap: 1px; padding: 2px; border-radius: 6px; background: rgba(0, 0, 0, .18); }
.hu-tile { display: flex; align-items: center; gap: 4px; font-size: 12px; font-weight: 700; color: #ffb4a8; }

/* ---------- 中央：牌河 + 罗盘 ---------- */
.center {
  min-height: 0;
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  grid-template-rows: 1fr auto 1fr;
  grid-template-areas:
    ". r2 ."
    "r3 cp r1"
    ". r0 .";
  gap: 6px;
  align-items: center;
  justify-items: center;
}
.river { display: flex; flex-wrap: wrap; gap: 2px; align-content: flex-start; }
.river-0 { grid-area: r0; align-self: start; width: calc(var(--sw) * 9 + 16px); }
.river-2 { grid-area: r2; align-self: end; width: calc(var(--sw) * 9 + 16px); flex-direction: row-reverse; flex-wrap: wrap-reverse; }
.river-3 { grid-area: r3; justify-self: end; width: calc(var(--sw) * 5 + 8px); justify-content: flex-end; }
.river-1 { grid-area: r1; justify-self: start; width: calc(var(--sw) * 5 + 8px); }

.compass {
  grid-area: cp;
  position: relative;
  width: clamp(96px, 13vw, 140px);
  aspect-ratio: 1;
  border-radius: 18px;
  background: linear-gradient(145deg, #1b4d33, #0f3221);
  border: 2px solid rgba(255, 255, 255, .14);
  box-shadow: inset 0 2px 12px rgba(0, 0, 0, .5), 0 4px 14px rgba(0, 0, 0, .35);
}
.cp {
  position: absolute;
  font-size: 12px;
  font-weight: 800;
  color: rgba(255, 255, 255, .45);
  padding: 2px 6px;
  border-radius: 6px;
  transition: all .2s;
}
.cp.on { color: #3a2410; background: var(--gold); box-shadow: 0 0 12px rgba(245, 197, 66, .7); }
.cp.done { text-decoration: line-through; opacity: .5; }
.cp-b { bottom: 6px; left: 50%; transform: translateX(-50%); }
.cp-t { top: 6px; left: 50%; transform: translateX(-50%); }
.cp-l { left: 6px; top: 50%; transform: translateY(-50%); }
.cp-r { right: 6px; top: 50%; transform: translateY(-50%); }
.cp-center { position: absolute; inset: 0; display: grid; place-content: center; text-align: center; }
.wall-n { font-size: clamp(22px, 3vw, 32px); font-weight: 800; color: var(--gold); font-variant-numeric: tabular-nums; line-height: 1; }
.wall-l { font-size: 11px; color: var(--muted); margin-top: 3px; }

/* ---------- 我的区域 ---------- */
.my-row { display: flex; align-items: flex-end; justify-content: center; gap: 12px; }
.my-tiles { display: flex; align-items: flex-end; gap: 14px; min-width: 0; flex-wrap: wrap; justify-content: center; }
.hand { display: flex; align-items: flex-end; gap: 2px; padding-top: 14px; }
.hand .tile { cursor: default; }
.hand .tile.clickable { cursor: pointer; }
.hand .tile.clickable:hover { transform: translateY(-6px); }
.hand .tile.selected { transform: translateY(-14px); box-shadow: 0 0 0 2px var(--gold), 0 8px 14px rgba(0, 0, 0, .45); }
.hand .tile.disabled { filter: brightness(.62) saturate(.6); }
.hand .tile.que-tile::before {
  content: "缺";
  position: absolute; left: 1px; top: 1px;
  font-size: 9px; font-weight: 800; color: #fff; background: #888; border-radius: 3px; padding: 0 2px; line-height: 1.3;
}
.hand .tile.fresh { box-shadow: 0 0 0 2px #5dade2, 0 4px 8px rgba(0, 0, 0, .4); }
.hand .tile.can-ting::before {
  content: "听";
  position: absolute; left: 50%; top: calc(var(--tw) * -.32);
  transform: translateX(-50%);
  font-size: 10px; font-weight: 800; color: #fff; background: #16a085; border-radius: 4px; padding: 1px 3px; line-height: 1.2;
}
.hand .gap { width: calc(var(--tw) * .35); }
.hand .hu-tile .tile { margin-left: 8px; }

/* ---------- 提示条 / 按钮 ---------- */
.prompt {
  min-height: 44px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  flex-wrap: wrap;
  font-size: 14px;
}
.prompt .ptext { color: var(--text); }
.prompt .ptext b { color: var(--gold); }
.prompt .muted { color: var(--muted); }
.prompt .inline-tiles { display: inline-flex; gap: 2px; vertical-align: middle; margin: 0 2px; }
.ting-info { display: inline-flex; align-items: center; gap: 6px; padding: 4px 10px; border-radius: 10px; background: rgba(22, 160, 133, .22); border: 1px solid rgba(22, 160, 133, .6); }
.ting-info .cnt { font-size: 11px; color: var(--muted); margin-left: 1px; }
.ting-item { display: inline-flex; align-items: flex-end; }
.timer { font-variant-numeric: tabular-nums; font-weight: 800; color: var(--gold); min-width: 30px; text-align: center; }
.btn {
  border: none;
  border-radius: 10px;
  padding: 8px 18px;
  font-size: 15px;
  font-weight: 800;
  color: #fff;
  background: linear-gradient(#5d6d7e, #34495e);
  box-shadow: 0 3px 0 rgba(0, 0, 0, .35);
  transition: transform .08s, filter .15s;
}
.btn:hover { filter: brightness(1.12); }
.btn:active { transform: translateY(2px); box-shadow: 0 1px 0 rgba(0, 0, 0, .35); }
.btn:disabled { filter: grayscale(1) brightness(.7); cursor: not-allowed; }
.btn.primary { background: linear-gradient(#f5c542, #d49b0e); color: #3a2410; }
.btn.hu { background: linear-gradient(#ff6b5a, #c0392b); font-size: 17px; padding: 8px 24px; animation: glow 1.2s ease-in-out infinite; }
.btn.gang { background: linear-gradient(#a569bd, #6c3483); }
.btn.peng { background: linear-gradient(#5dade2, #2471a3); }
.btn.ghost { background: rgba(255, 255, 255, .12); box-shadow: none; border: 1px solid rgba(255, 255, 255, .25); }
.btn.suit { background: #fdfaf0; min-width: 72px; }
.btn.suit.s-wan { color: var(--wan); }
.btn.suit.s-tiao { color: var(--tiao); }
.btn.suit.s-tong { color: var(--tong); }
.btn.suit small { color: #777; font-weight: 500; font-size: 12px; margin-left: 4px; }
.btn.suit.rec { box-shadow: 0 0 0 3px var(--gold), 0 3px 0 rgba(0, 0, 0, .35); }
@keyframes glow { 50% { box-shadow: 0 0 18px rgba(255, 107, 90, .9), 0 3px 0 rgba(0, 0, 0, .35); } }

/* ---------- 喊话动画 ---------- */
.callouts { position: absolute; inset: 0; pointer-events: none; z-index: 4; }
.callout {
  position: absolute;
  transform: translate(-50%, -50%);
  font-size: clamp(28px, 5vw, 52px);
  font-weight: 900;
  font-family: "STKaiti", "KaiTi", serif;
  color: #fff;
  -webkit-text-stroke: 1px rgba(0, 0, 0, .3);
  text-shadow: 0 3px 0 rgba(0, 0, 0, .35), 0 0 22px currentColor;
  animation: callout 1.4s ease forwards;
  white-space: nowrap;
}
.callout.peng { color: #7fd3ff; }
.callout.gang { color: #e3a8ff; }
.callout.hu { color: #ffd166; font-size: clamp(36px, 6.5vw, 68px); }
.co-0 { left: 50%; top: 70%; }
.co-1 { left: 82%; top: 45%; }
.co-2 { left: 50%; top: 18%; }
.co-3 { left: 18%; top: 45%; }
@keyframes callout {
  0% { opacity: 0; transform: translate(-50%, -50%) scale(2.2); }
  18% { opacity: 1; transform: translate(-50%, -50%) scale(1); }
  75% { opacity: 1; }
  100% { opacity: 0; transform: translate(-50%, -80%) scale(1); }
}

/* ---------- 记录面板 ---------- */
.logpanel {
  position: fixed;
  top: var(--top-h); right: 0; bottom: 0;
  width: min(320px, 86vw);
  background: rgba(14, 30, 21, .96);
  border-left: 1px solid var(--panel-line);
  z-index: 8;
  display: flex;
  flex-direction: column;
  box-shadow: -6px 0 20px rgba(0, 0, 0, .4);
}
.logpanel-head { display: flex; justify-content: space-between; align-items: center; padding: 10px 14px; font-weight: 700; border-bottom: 1px solid var(--panel-line); }
.x { background: none; border: none; color: var(--text); font-size: 22px; line-height: 1; }
#log { flex: 1; overflow: auto; margin: 0; padding: 8px 14px 16px 34px; font-size: 13px; line-height: 1.7; }
#log li { color: var(--muted); }
#log li.s-0 { color: #f8c471; }
#log li.s-1 { color: #d2b4de; }
#log li.s-2 { color: #a9cce3; }
#log li.s-3 { color: #f5b7b1; }

/* ---------- 弹窗 ---------- */
.modal {
  position: fixed; inset: 0; z-index: 10;
  display: grid; place-items: center;
  padding: 16px;
  background: rgba(0, 0, 0, .55);
  backdrop-filter: blur(2px);
}
.modal-card {
  width: min(760px, 100%);
  max-height: calc(100dvh - 32px);
  overflow: auto;
  background: linear-gradient(#fdfaf0, #f1ead2);
  color: var(--ink);
  border-radius: 16px;
  padding: 22px 24px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, .55);
  user-select: text;
}
.modal-card h2 { margin: 0 0 4px; font-size: 22px; color: #5b3a1e; }
.modal-card h3 { margin: 18px 0 8px; font-size: 15px; color: #5b3a1e; }
.modal-card .sub { margin: 0 0 14px; color: #7a6a4f; font-size: 13px; }
.modal-card p, .modal-card li { font-size: 14px; line-height: 1.7; }
.modal-card .actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 18px; }
.modal-card .btn.ghost { color: #5b3a1e; border-color: #cbb98d; background: transparent; }
.res-table { width: 100%; border-collapse: collapse; font-size: 14px; }
.res-table th, .res-table td { padding: 8px 6px; border-bottom: 1px solid #e2d7b8; text-align: left; vertical-align: middle; }
.res-table th { font-size: 12px; color: #8a7a5c; font-weight: 600; }
.res-table td.num { text-align: right; font-weight: 800; font-variant-numeric: tabular-nums; }
.pos { color: #c0392b; }
.neg { color: #1e8449; }
.res-hand { display: flex; gap: 6px; flex-wrap: wrap; align-items: center; margin-top: 4px; }
.res-hand .tile { --sw: 22px; }
.ledger { columns: 2 240px; font-size: 13px; color: #5b4a30; padding-left: 18px; margin: 0; }
.ledger li { break-inside: avoid; }
.fan-items { font-size: 12px; color: #7a6a4f; }
.tag { display: inline-block; font-size: 12px; font-weight: 700; padding: 1px 6px; border-radius: 4px; margin-right: 4px; }
.tag.hu { background: #c0392b; color: #fff; }
.tag.pig { background: #8e44ad; color: #fff; }
.tag.ting { background: #16a085; color: #fff; }
.tag.no { background: #95a5a6; color: #fff; }

.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 12px 18px; margin-top: 8px; }
.field { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: #5b4a30; font-weight: 600; }
.field select { font: inherit; font-weight: 500; padding: 6px 8px; border-radius: 8px; border: 1px solid #cbb98d; background: #fffdf5; color: var(--ink); }
.check { flex-direction: row; align-items: center; gap: 8px; }
.hero { text-align: center; }
.hero .big { font-size: 40px; font-weight: 900; color: #5b3a1e; letter-spacing: 6px; margin: 4px 0; font-family: "STKaiti", "KaiTi", serif; }
.hero .demo { display: flex; justify-content: center; gap: 3px; margin: 12px 0 6px; }
.rules-body ul { padding-left: 20px; margin: 6px 0; }
.rules-body table { border-collapse: collapse; font-size: 13px; margin: 6px 0; }
.rules-body td, .rules-body th { border: 1px solid #e2d7b8; padding: 4px 10px; }

/* ---------- 窄屏 ---------- */
@media (max-width: 760px) {
  :root { --tw: min(calc((100vw - 36px) / 14.6), 44px); --sw: clamp(15px, 4.2vw, 22px); --top-h: 44px; }
  .brand span, .meta { display: none; }
  .topbar { justify-content: space-between; padding: 0 10px; }
  .tool { padding: 4px 8px; font-size: 12px; }
  #table { padding: 6px 8px calc(10px + env(safe-area-inset-bottom, 0px)); grid-template-columns: auto 1fr auto; }
  .seat-left .phand, .seat-right .phand { display: none; }
  .seat-left .pinfo, .seat-right .pinfo { padding: 6px 4px; }
  .pinfo .hu-badge { font-size: 10px; }
  .river-0, .river-2 { width: calc(var(--sw) * 7 + 12px); }
  .river-1, .river-3 { width: calc(var(--sw) * 3 + 4px); }
  .compass { width: 92px; border-radius: 14px; }
  .cp { font-size: 10px; padding: 1px 4px; }
  .cp-b { bottom: 3px; } .cp-t { top: 3px; } .cp-l { left: 3px; } .cp-r { right: 3px; }
  .wall-n { font-size: 20px; }
  .wall-l { display: none; }
  .my-row { flex-direction: column; align-items: center; gap: 4px; }
  #info-0 .pinfo { padding: 3px 8px 3px 3px; }
  .avatar { width: 28px; height: 28px; font-size: 13px; }
  .btn { padding: 7px 12px; font-size: 14px; }
}
