* { box-sizing: border-box; margin: 0; padding: 0; }
:root {
  --felt: linear-gradient(160deg, #0a6b3c, #0c7a46 40%, #0a5a33);
  --gold: #f3c350;
  --card-w: 64px; --card-h: 90px;
}
html, body { height: 100%; }
body {
  font-family: "PingFang SC", "Microsoft YaHei", system-ui, sans-serif;
  background: var(--felt);
  color: #fdf6e3;
  overflow: hidden;
  user-select: none;
}

.screen { position: absolute; inset: 0; display: none; }
.screen.active { display: flex; }
#screen-lobby, #screen-waiting { align-items: center; justify-content: center; }

/* ---------- lobby ---------- */
.lobby-box, .waiting-box {
  background: rgba(0,0,0,.4); backdrop-filter: blur(6px);
  border: 1px solid rgba(255,255,255,.15); border-radius: 18px;
  padding: 40px 48px; width: min(420px, 92vw); text-align: center;
  box-shadow: 0 20px 60px rgba(0,0,0,.4);
}
.title { font-size: 40px; color: var(--gold); margin-bottom: 6px; text-shadow: 0 2px 10px rgba(0,0,0,.5); }
.subtitle { color: #bcd9c9; margin-bottom: 28px; font-size: 13px; }
.field { margin: 14px 0; text-align: left; }
.field label { font-size: 13px; color: #c9e0d3; display: block; margin-bottom: 6px; }
.field.row { display: flex; gap: 8px; }
input {
  width: 100%; padding: 12px 14px; font-size: 16px; border-radius: 10px;
  border: 1px solid rgba(255,255,255,.2); background: rgba(255,255,255,.12);
  color: #fff; outline: none;
}
input:focus { border-color: var(--gold); }
.divider { margin: 22px 0 12px; font-size: 12px; color: #8fb3a1; display: flex; align-items: center; gap: 10px; }
.divider::before, .divider::after { content: ''; flex: 1; height: 1px; background: rgba(255,255,255,.15); }
.btn {
  padding: 12px 18px; font-size: 16px; border: none; border-radius: 12px;
  cursor: pointer; background: rgba(255,255,255,.14); color: #fff;
  transition: .15s; font-weight: 600;
}
.btn:hover { filter: brightness(1.1); transform: translateY(-1px); }
.btn.primary { background: linear-gradient(135deg, #f5a623, #e07b1a); color: #3a2400; }
.btn.ghost { background: transparent; border: 1px solid rgba(255,255,255,.3); }
.btn.small { padding: 7px 12px; font-size: 13px; border-radius: 8px; }
.btn.full { width: 100%; margin-top: 6px; }
.msg { color: #ffd27a; font-size: 13px; margin-top: 12px; min-height: 18px; }

/* ---------- waiting ---------- */
.room-code { font-size: 18px; margin-bottom: 20px; }
.code { color: var(--gold); font-weight: 800; letter-spacing: 3px; font-size: 26px; cursor: pointer; border-bottom: 2px dashed rgba(243,195,80,.5); user-select: all; }
.code:hover { filter: brightness(1.15); }
.code-btns { display: flex; gap: 8px; justify-content: center; margin-top: 12px; }
.waiting-players { display: flex; gap: 12px; justify-content: center; margin-bottom: 18px; }
.waiter {
  background: rgba(255,255,255,.08); border-radius: 12px; padding: 16px 14px; width: 110px;
}
.waiter .avatar { font-size: 34px; }
.waiter .nm { font-size: 14px; margin-top: 6px; }
.waiter.empty { opacity: .4; }
.waiter.waiting .status { color: #ffd27a; font-size: 12px; }
.status { color: #7fd6a3; font-size: 12px; }
.waiter .isai { color: #ffd27a; font-size: 11px; margin-top: 2px; }

#ai-controls { margin: 14px 0 6px; }
.ai-row { display: flex; align-items: center; gap: 8px; justify-content: center; flex-wrap: wrap; }
.ai-label { font-size: 13px; color: #c9e0d3; }
#ai-level {
  padding: 8px 10px; font-size: 14px; border-radius: 10px; border: 1px solid rgba(255,255,255,.25);
  background: rgba(255,255,255,.16); color: #fff; outline: none;
}
#ai-level option { color: #222; }

/* ---------- game ---------- */
#screen-game { flex-direction: column; }
.opponents { display: flex; justify-content: space-around; padding: 14px 20px 4px; }
.opp { background: rgba(0,0,0,.3); border-radius: 12px; padding: 8px 16px; min-width: 140px; text-align: center; }
.opp .nm { font-size: 15px; font-weight: 700; }
.opp .status { font-size: 12px; }
.opp.active { outline: 2px solid var(--gold); }
.opp.landlord::after { content: ' 👑'; }
.count-badge { color: #ffd27a; font-weight: 700; }

#bottom-area { display: flex; align-items: center; gap: 10px; justify-content: center; margin: 8px 0 2px; }
#bottom-area .label { color: #b9c9c9; font-size: 13px; }
.bottom-cards { display: flex; }

#center { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 6px; min-height: 120px; }
.notice { min-height: 26px; font-size: 18px; font-weight: 700; color: #ffe08a; text-shadow: 0 1px 4px rgba(0,0,0,.6); }
.notice.sub { font-size: 13px; color: #b4d3c3; }
.trick { display: flex; gap: 6px; min-height: 90px; align-items: center; justify-content: center; flex-wrap: wrap; }
.trick .by { font-size: 13px; color: #cfe6d9; margin-right: 8px; }

.bar { display: flex; gap: 12px; justify-content: center; align-items: center; padding: 8px 0 4px; }
.bar.hidden { display: none; }
.bid-btn {
  width: 88px; padding: 12px 0; font-size: 20px; font-weight: 800; border-radius: 12px;
  border: none; cursor: pointer; background: linear-gradient(180deg, #fff3c4, #e6b84d); color: #5a3a00;
  box-shadow: 0 4px 10px rgba(0,0,0,.3);
}
.bid-btn.pass { background: linear-gradient(180deg, #d8d8d8, #a9a9a9); color: #444; }
.bid-btn:hover { transform: translateY(-2px); filter: brightness(1.05); }

.me { padding: 6px 16px 16px; }
.me-info { text-align: center; font-size: 14px; margin-bottom: 6px; }
.me-info .role { color: var(--gold); font-weight: 700; }
.score { color: #ffe08a; font-weight: 700; }
.hand { display: flex; justify-content: center; min-height: 100px; padding-bottom: 26px; position: relative; overflow: visible; }
/* 选中的牌上移后不能被裁掉：容器不裁剪，水平适配由 fitHand() 保证 */
.card {
  width: var(--card-w); height: var(--card-h); border-radius: 8px; margin-left: -26px; transition: transform .1s, margin-top .1s;
  background: #fff; color: #222; position: relative; box-shadow: 0 3px 8px rgba(0,0,0,.4);
  cursor: pointer; flex-shrink: 0;
}
.hand .card:first-child { margin-left: 0; }
.card .r { position: absolute; top: 5px; left: 9px; font-size: 20px; font-weight: 800; line-height: 1; }
.card .s { position: absolute; bottom: 5px; right: 8px; font-size: 22px; }
.card.red { color: #d32f2f; }
.card.black { color: #222; }
.card.selected { transform: translateY(-22px); }
.card .jt { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; font-size: 13px; font-weight: 800; }
.card.back { background: repeating-linear-gradient(45deg, #1565c0, #1565c0 6px, #0d47a1 6px, #0d47a1 12px); }

/* small screens / phones: shrink cards so a full hand stays usable */
@media (max-width: 560px) {
  :root { --card-w: 44px; --card-h: 62px; }
  .card { margin-left: -30px; }
  .card .r { font-size: 16px; top: 4px; left: 5px; }
  .card .s { font-size: 17px; bottom: 4px; right: 5px; }
  .card .jt { font-size: 11px; }
  .hand { min-height: 74px; padding-bottom: 8px; }
  .trick, .trick { min-height: 66px; }
  .trick .card, .bottom-cards .card, .opp .card { width: 34px; height: 48px; }
  .trick .card { margin-left: -12px; }
  #center { min-height: 84px; }
  .opp { min-width: 96px; padding: 6px 10px; }
  /* emoji bar: horizontal strip BELOW the hand, never on top of cards */
  .emoji-bar {
    position: static;
    flex-direction: row;
    justify-content: center;
    gap: 6px;
    flex-wrap: wrap;
    padding: 2px 0 8px;
  }
  .emoji-bar button { width: 34px; height: 28px; font-size: 15px; }
  /* chat log: pin to the TOP-left so it doesn't cover the hand */
  .chat { position: absolute; left: 8px; top: 6px; bottom: auto; max-width: 55vw; }
}

/* small cards (others / trick / bottom) */
.trick .card, .bottom-cards .card, .opp .card {
  width: 46px; height: 64px; cursor: default;
}
.trick .card .s { font-size: 16px; bottom: 3px; right: 5px; }
.trick .card .r { font-size: 16px; top: 3px; left: 6px; }

/* ---------- result overlay ---------- */
.overlay { position: absolute; inset: 0; background: rgba(0,0,0,.7); align-items: center; justify-content: center; z-index: 30; }
.overlay.hidden { display: none; }
.overlay:not(.hidden) { display: flex; }
.overlay-x { position: absolute; top: 12px; right: 12px; z-index: 32; }
.again-bar {
  position: absolute; left: 50%; transform: translateX(-50%);
  bottom: 150px; z-index: 25; display: flex; gap: 10px;
}
.again-bar.hidden { display: none; }

/* result hand grids / cards */
.result-box {
  background: linear-gradient(180deg, #2c9e63, #1b7a49); border-radius: 20px;
  padding: 30px 44px; text-align: center; min-width: 320px;
  box-shadow: 0 20px 60px rgba(0,0,0,.5); border: 2px solid var(--gold);
  max-height: 88vh; overflow-y: auto;
}
.result-box .win { font-size: 30px; font-weight: 800; color: #fff; }
.result-box.wretched { background: linear-gradient(180deg, #9e3d3d, #7a1b1b); }
.result-box .detail { color: #eaf7ef; margin: 8px 0; font-size: 14px; line-height: 1.8; }
.result-box .delta { font-size: 20px; font-weight: 700; margin: 10px 0; }

/* reveal hands at game over */
.hand-grid { margin: 12px 0; display: flex; flex-direction: column; gap: 8px; }
.reshand { background: rgba(0,0,0,.25); border-radius: 10px; padding: 6px 10px; text-align: left; }
.reshand-name { font-size: 13px; font-weight: 700; }
.reshand-name.landlord { color: var(--gold); }
.reshand-count { font-size: 12px; opacity: .85; margin-left: 8px; }
.reshand-cards { display: flex; justify-content: center; margin-top: 4px; min-height: 34px; flex-wrap: wrap; }
.reshand-cards .card { width: 30px; height: 42px; margin-left: -14px; cursor: default; }
.reshand-cards .card:first-child { margin-left: 0; }
.reshand-cards .card .r { font-size: 12px; top: 2px; left: 4px; }
.reshand-cards .card .s { font-size: 12px; bottom: 2px; right: 4px; }
.reshand-cards .card .jt { font-size: 9px; }
@media (max-width: 560px) {
  .reshand-cards .card { width: 26px; height: 36px; margin-left: -12px; }
}

/* ---------- chat / emoji ---------- */
.emoji-bar { position: absolute; right: 14px; bottom: 14px; display: flex; flex-direction: column; gap: 4px; }
.emoji-bar button { width: 40px; height: 32px; font-size: 18px; border: none; background: rgba(0,0,0,.35); border-radius: 8px; cursor: pointer; }
.emoji-bar button:hover { background: rgba(0,0,0,.6); }
.chat { position: absolute; left: 14px; bottom: 14px; max-width: 260px; display: flex; flex-direction: column; gap: 4px; pointer-events: none; }
.chat .cq { background: rgba(0,0,0,.5); border-radius: 8px; padding: 5px 9px; font-size: 13px; max-width: 100%; }

.hidden { display: none !important; }