/* 北方麻将 · 桌面绒布绿 + 牙白牌 + 朱金点缀 */
:root {
  --felt: #14382c;
  --felt-deep: #0d2a21;
  --ink: #22364a;
  --ivory: #f2ecd9;
  --ivory-dim: #c9c2ab;
  --gold: #c9a227;
  --red: #b3382e;
  --green: #2f7d46;
  --wood: #7a5230;
  --tw: clamp(38px, 9.5vw, 58px);          /* 手牌宽 */
  --rw: clamp(20px, 4.8vw, 30px);          /* 河牌宽 */
  --mw: clamp(30px, 7vw, 44px);            /* 副露牌宽 */
  --serif: "Songti SC", "Noto Serif SC", "STSong", serif;
}
* { margin: 0; padding: 0; box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html { height: 100%; }
body {
  position: fixed; inset: 0;         /* 锁死视口：手机上地址栏收放不再顶出滚动 */
  overflow: hidden;
  background: var(--felt);
  color: var(--ivory);
  font-family: -apple-system, "PingFang SC", "Noto Sans SC", sans-serif;
  overscroll-behavior: none;
  user-select: none;
  touch-action: manipulation;
}
#app { height: 100vh; height: 100dvh; }
.screen { height: 100vh; height: 100dvh; display: flex; align-items: center; justify-content: center; }
.screen { display: flex; align-items: center; justify-content: center; }
.hidden { display: none !important; }

/* ---------- 通用控件 ---------- */
.btn {
  appearance: none; border: 1px solid rgba(242,236,217,.35);
  background: rgba(242,236,217,.08); color: var(--ivory);
  border-radius: 10px; padding: 10px 18px; font-size: 16px; cursor: pointer;
  transition: transform .08s ease, background .15s ease;
}
.btn:active { transform: scale(.97); }
.btn.primary { background: var(--gold); border-color: var(--gold); color: #241c05; font-weight: 700; }
.btn.danger { background: var(--red); border-color: var(--red); color: #fff; font-weight: 700; }
.btn.ghost { background: none; border-color: rgba(242,236,217,.25); color: var(--ivory-dim); }
.btn.small { padding: 6px 12px; font-size: 13px; }
.btn.big { padding: 13px 20px; font-size: 18px; }
.btn:disabled { opacity: .4; pointer-events: none; }
.btn-row { display: flex; gap: 10px; justify-content: center; }
.stack { display: flex; flex-direction: column; gap: 10px; }
.err { color: #ff9d94; font-size: 14px; min-height: 20px; margin-top: 8px; }
.hint { color: var(--ivory-dim); font-size: 13px; line-height: 1.6; }

/* ---------- 大厅 / 等待房 ---------- */
.lobby-card {
  width: min(92vw, 380px);
  background: rgba(13, 42, 33, .82);
  border: 1px solid rgba(201, 162, 39, .35);
  border-radius: 18px;
  padding: 30px 26px;
  display: flex; flex-direction: column; gap: 14px;
  box-shadow: 0 18px 50px rgba(0,0,0,.45);
}
.logo {
  font-family: var(--serif); font-size: 40px; letter-spacing: 8px;
  text-align: center; color: var(--ivory); font-weight: 700;
}
.tagline { text-align: center; color: var(--gold); font-size: 14px; letter-spacing: 2px; }
.field { display: flex; flex-direction: column; gap: 6px; font-size: 14px; color: var(--ivory-dim); }
input {
  background: rgba(242,236,217,.07); border: 1px solid rgba(242,236,217,.25);
  color: var(--ivory); border-radius: 10px; padding: 12px 14px; font-size: 17px;
  outline: none; width: 100%;
}
input:focus { border-color: var(--gold); }
.divider { text-align: center; color: var(--ivory-dim); font-size: 13px; }
.join-row { display: flex; gap: 10px; }
.join-row input { flex: 1; text-transform: uppercase; letter-spacing: 6px; text-align: center; font-weight: 700; }
.wait-label { text-align: center; color: var(--ivory-dim); font-size: 14px; }
.room-code {
  font-family: var(--serif); text-align: center; font-size: 54px; letter-spacing: 14px;
  color: var(--gold); text-indent: 14px;
}
.seat-list { list-style: none; display: flex; flex-direction: column; gap: 8px; margin: 6px 0; }
.seat-list li {
  display: flex; justify-content: space-between; align-items: center;
  border: 1px dashed rgba(242,236,217,.25); border-radius: 10px; padding: 10px 14px; font-size: 15px;
}
.seat-list .tag { font-size: 12px; color: var(--gold); border: 1px solid rgba(201,162,39,.5); border-radius: 6px; padding: 1px 7px; }
.seat-list .empty { color: var(--ivory-dim); }

/* ---------- 牌桌布局 ---------- */
#scr-table { align-items: stretch; justify-content: stretch; position: relative; }
#table {
  flex: 1; height: 100vh; height: 100dvh; position: relative;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  grid-template-columns: auto minmax(0, 1fr) auto;
  grid-template-areas: "top top top" "left center right" "me me me";
  background:
    radial-gradient(ellipse at 50% 42%, rgba(255,255,255,.05), rgba(0,0,0,.28) 78%),
    var(--felt);
  padding: max(6px, env(safe-area-inset-top)) 4px max(4px, env(safe-area-inset-bottom)) 4px;
  gap: 2px;
}

/* 右上角悬浮控制 */
#hud { position: absolute; top: calc(6px + env(safe-area-inset-top)); right: 8px; display: flex; gap: 6px; z-index: 30; }
.hud-btn {
  appearance: none; cursor: pointer;
  font-size: 12px; padding: 5px 10px; border-radius: 9px;
  background: rgba(0, 0, 0, .32); border: 1px solid rgba(242, 236, 217, .22);
  color: var(--ivory-dim);
}
.hud-btn.on { color: var(--gold); border-color: rgba(201, 162, 39, .55); }
.hud-btn.exit { color: #ff9d94; border-color: rgba(255, 157, 148, .3); }

/* ---------- 快捷语气泡 ---------- */
.chat-bubble {
  position: absolute; z-index: 40;
  max-width: min(72vw, 300px);
  pointer-events: none;
}
.chat-bubble .cb-in {
  background: var(--ivory); color: var(--ink);
  border-radius: 14px; padding: 9px 13px;
  font-size: 13px; line-height: 1.55;
  box-shadow: 0 6px 18px rgba(0, 0, 0, .38);
  animation: bubble-pop .18s ease;
}
.chat-bubble .chat-name { display: block; font-size: 11px; color: #7a7260; margin-bottom: 2px; }
.chat-bubble.out .cb-in { animation: bubble-out .5s ease forwards; }
@keyframes bubble-pop { from { transform: translateY(8px) scale(.85); opacity: 0; } }
@keyframes bubble-out { to { opacity: 0; transform: translateY(-10px); } }
.cb-seat0 { bottom: calc(var(--tw) * 1.42 + 74px); left: 50%; transform: translateX(-50%); }
.cb-seat2 { top: 58px; left: 50%; transform: translateX(-50%); }
.cb-seat1 { right: clamp(56px, 15vw, 96px); top: 38%; }
.cb-seat3 { left: clamp(56px, 15vw, 96px); top: 38%; }
#chat-list { display: flex; flex-direction: column; gap: 8px; }
#chat-list button {
  appearance: none; cursor: pointer; text-align: left;
  background: rgba(242, 236, 217, .07); color: var(--ivory);
  border: 1px solid rgba(242, 236, 217, .22); border-radius: 10px;
  padding: 10px 13px; font-size: 14px; line-height: 1.5;
}
#chat-list button:active { background: rgba(201, 162, 39, .25); }

/* 对家（上） */
#opp-top { grid-area: top; }
/* 左 / 右家 */
#opp-left { grid-area: left; }
#opp-right { grid-area: right; }
.opp { display: flex; gap: 6px; align-items: center; padding: 4px 6px; min-width: 0; }
.opp-h { flex-direction: row; justify-content: center; }
.opp-v { flex-direction: column; justify-content: flex-start; width: clamp(52px, 14vw, 84px); }
.opp-v .opp-name { writing-mode: vertical-rl; letter-spacing: 3px; }
.opp-card {
  display: flex; gap: 6px; align-items: center;
  background: rgba(0,0,0,.22); border-radius: 10px; padding: 4px 10px;
}
.opp-v .opp-card { flex-direction: column; padding: 10px 4px; }
.opp-name { font-size: 13px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 70px; }
.opp-v .opp-name { max-width: none; max-height: 64px; }
.opp-sub { font-size: 11px; color: var(--ivory-dim); display: flex; gap: 6px; align-items: center; }
.opp-v .opp-sub { flex-direction: column; gap: 2px; }
.badge-dealer, .badge-ting {
  font-size: 10px; border-radius: 5px; padding: 1px 5px; line-height: 1.5;
}
.badge-dealer { color: #241c05; background: var(--gold); font-weight: 700; }
.badge-ting { color: #fff; background: var(--red); font-weight: 700; }
.badge-off { color: #ff9d94; font-size: 10px; }
.backs { display: flex; }
.opp-v .backs { flex-direction: column; }
.backs img { width: calc(var(--rw) * .78); display: block; }
.opp-v .backs img { width: calc(var(--rw) * .92); }
.opp.acting .opp-card { outline: 2px solid var(--gold); }

/* ---------- 中央 ---------- */
#center {
  grid-area: center; display: grid; min-width: 0; min-height: 0;
  grid-template-columns: 1fr auto 1fr;
  grid-template-rows: 1fr auto 1fr;
  grid-template-areas: ". r2 ." "r3 info r1" ". r0 .";
  gap: 2px; padding: 2px 0;
}
.river { display: flex; flex-wrap: wrap; gap: 2px; align-content: flex-start; min-height: 0; overflow: hidden; padding: 3px; }
.river img { width: var(--rw); display: block; border-radius: 3px; }
#river-2 { grid-area: r2; justify-content: center; }
#river-0 { grid-area: r0; justify-content: center; }
#river-3 { grid-area: r3; justify-content: flex-end; flex-direction: column; flex-wrap: wrap; align-content: flex-end; }
#river-1 { grid-area: r1; flex-direction: column; flex-wrap: wrap; align-content: flex-start; }
.river .fresh { outline: 2px solid var(--gold); border-radius: 3px; }
#info-box {
  grid-area: info; align-self: center; justify-self: center;
  text-align: center; padding: 6px 14px;
  background: rgba(0,0,0,.3); border: 1px solid rgba(201,162,39,.3); border-radius: 12px;
  min-width: 108px;
}
#info-round { font-family: var(--serif); font-size: 14px; color: var(--gold); }
#info-wall { font-size: 12px; color: var(--ivory-dim); }
#info-state { font-size: 12px; margin-top: 2px; color: var(--ivory); }

/* ---------- 我的区域 ---------- */
#me-area { grid-area: me; display: flex; flex-direction: column; gap: 4px; padding: 2px 6px 4px; }
#toast {
  min-height: 20px; text-align: center; font-size: 13px; color: var(--gold);
  transition: opacity .3s; opacity: 0;
}
#toast.show { opacity: 1; }
.melds-row { display: flex; gap: 10px; justify-content: center; min-height: calc(var(--mw) * .62); align-items: flex-end; }
.opp .melds-row { gap: 4px; }
.meld { display: flex; }
.meld img { width: var(--mw); display: block; }
.meld.steal img:first-child { outline: 2px solid var(--gold); border-radius: 3px; }
#hand {
  display: flex; justify-content: center; align-items: flex-end;
  min-height: calc(var(--tw) * 1.42); padding-bottom: 2px;
}
.htile {
  appearance: none; background: none; border: none; padding: 0; cursor: pointer;
  margin: 0 -3px; transition: transform .12s ease; position: relative;
}
.htile img { width: var(--tw); display: block; border-radius: 6px; box-shadow: 0 3px 8px rgba(0,0,0,.4); }
.htile.sel { transform: translateY(-16px); }
.htile.sel img { outline: 3px solid var(--gold); }
.htile.tingmark::after {
  content: "听"; position: absolute; top: -4px; right: -2px;
  background: var(--red); color: #fff; font-size: 10px; border-radius: 6px; padding: 0 4px;
}
.hand-gap { width: 16px; }
#drawn-label { font-size: 11px; color: var(--ivory-dim); align-self: flex-end; margin-bottom: 4px; }

/* ---------- 操作条 ---------- */
#action-bar {
  display: flex; gap: 10px; justify-content: center; align-items: center;
  min-height: 48px;
}
#action-bar .btn { min-width: 86px; }
.countdown {
  height: 3px; background: rgba(242,236,217,.18); border-radius: 2px; overflow: hidden;
  position: absolute; left: 8px; right: 8px; bottom: 6px;
}
.countdown i { display: block; height: 100%; background: var(--gold); }
.btn { position: relative; }

/* ---------- 浮层 ---------- */
.overlay {
  position: fixed; inset: 0; background: rgba(5, 18, 14, .78);
  display: flex; align-items: center; justify-content: center; z-index: 50;
  padding: 20px;
}
.panel {
  width: min(92vw, 420px); max-height: 86vh; overflow: auto;
  background: #10241d; border: 1px solid rgba(201,162,39,.4); border-radius: 18px;
  padding: 24px 22px; text-align: center;
  display: flex; flex-direction: column; gap: 14px;
  box-shadow: 0 22px 60px rgba(0,0,0,.5);
}
.panel h3 { font-family: var(--serif); font-size: 26px; letter-spacing: 4px; }
.result-type { font-size: 15px; color: var(--ivory-dim); }
.result-fans { display: flex; gap: 8px; justify-content: center; flex-wrap: wrap; }
.result-fans span {
  border: 1px solid rgba(201,162,39,.55); color: var(--gold);
  border-radius: 8px; padding: 3px 10px; font-size: 14px;
}
.result-total { font-size: 20px; }
.result-table { width: 100%; border-collapse: collapse; font-size: 15px; }
.result-table th { color: var(--ivory-dim); font-weight: 400; font-size: 12px; padding: 4px; }
.result-table td { padding: 7px 4px; border-top: 1px solid rgba(242,236,217,.12); }
.delta-plus { color: #7fd79a; font-weight: 700; }
.delta-minus { color: #ff9d94; font-weight: 700; }
.win-tile { display: flex; justify-content: center; margin: 2px 0; }
.win-tile img { width: calc(var(--tw) * .9); border-radius: 5px; }

@media (max-height: 700px) {
  :root {
    --tw: clamp(32px, 8vw, 48px);
    --rw: clamp(16px, 3.8vw, 24px);
    --mw: clamp(24px, 5.6vw, 36px);
  }
  #toast { min-height: 16px; font-size: 12px; }
  #action-bar { min-height: 40px; }
  #action-bar .btn { padding: 8px 14px; font-size: 15px; min-width: 72px; }
  .lobby-card { padding: 20px 18px; gap: 10px; }
  .logo { font-size: 30px; }
}
@media (max-height: 600px) {
  :root {
    --tw: clamp(28px, 7vw, 42px);
    --rw: clamp(14px, 3.4vw, 21px);
    --mw: clamp(20px, 5vw, 30px);
  }
  #hand { min-height: calc(var(--tw) * 1.35); }
  .opp-card { padding: 2px 6px; }
}

@media (orientation: landscape) and (max-height: 520px) {
  :root {
    --tw: clamp(28px, 9.5vh, 46px);
    --rw: clamp(13px, 4.2vh, 22px);
    --mw: clamp(19px, 5.6vh, 32px);
  }
  #table { grid-template-rows: auto minmax(0, 1fr) auto; }
  .opp-h { padding: 1px 10px; }
  .opp-card { padding: 2px 8px; }
  .opp-v { width: clamp(40px, 11vh, 68px); padding: 2px 2px; }
  .opp-v .opp-name { max-height: 52px; }
  #toast { min-height: 14px; font-size: 11px; }
  .melds-row { min-height: calc(var(--mw) * .55); }
  #action-bar { min-height: 34px; }
  #action-bar .btn { padding: 6px 12px; font-size: 14px; min-width: 64px; }
  #hand { min-height: calc(var(--tw) * 1.32); }
  #info-box { padding: 2px 10px; min-width: 90px; }
  #info-round { font-size: 12px; }
  #info-wall, #info-state { font-size: 10px; }
  .river { gap: 1px; padding: 2px; }
}

@media (min-width: 760px) {
  #table { max-width: 1020px; margin: 0 auto; width: 100%; }
  .logo { font-size: 46px; }
}
