:root {
  --bg: #10161c;
  --felt: #14532d;
  --felt-2: #0f3d22;
  --line: rgba(255, 255, 255, 0.12);
  --text: #e8eef4;
  --muted: #9fb0c0;
  --gold: #f5c451;
  --danger: #ef5350;
  --card-bg: #fdfdfb;
  --card-red: #c62828;
  --card-black: #1f2933;
  --shadow: 0 10px 30px rgba(0, 0, 0, 0.35);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  height: 100%;
  background: radial-gradient(1200px 700px at 50% -10%, #1d2b38 0%, var(--bg) 60%);
  color: var(--text);
  font-family: "PingFang SC", "Microsoft YaHei", "Noto Sans SC", system-ui, sans-serif;
  overflow: hidden;
}

#app {
  height: 100%;
  display: flex;
  flex-direction: column;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

/* ---------------------------------------------------------------- 顶栏 */

.topbar {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 8px 14px;
  background: rgba(0, 0, 0, 0.25);
  border-bottom: 1px solid var(--line);
  font-size: 13px;
  flex-wrap: wrap;
}

.brand { font-weight: 700; letter-spacing: 0.5px; color: var(--gold); }
.brand small { color: var(--muted); font-weight: 400; margin-left: 6px; }

.pill {
  padding: 3px 10px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.08);
  border: 1px solid var(--line);
  color: var(--muted);
  white-space: nowrap;
}
.pill strong { color: var(--text); font-weight: 600; }
.pill.gold { color: var(--gold); border-color: rgba(245, 196, 81, 0.35); background: rgba(245, 196, 81, 0.1); }

.spacer { flex: 1; }

button {
  font: inherit;
  color: var(--text);
  background: rgba(255, 255, 255, 0.1);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 7px 14px;
  cursor: pointer;
  transition: transform 0.08s ease, background 0.15s ease, opacity 0.15s ease;
}
button:hover:not(:disabled) { background: rgba(255, 255, 255, 0.18); }
button:active:not(:disabled) { transform: translateY(1px); }
button:disabled { opacity: 0.35; cursor: not-allowed; }
button.primary { background: linear-gradient(180deg, #2e7d32, #1b5e20); border-color: #43a047; font-weight: 600; }
button.primary:hover:not(:disabled) { background: linear-gradient(180deg, #388e3c, #2e7d32); }
button.ghost { background: transparent; }

/* ---------------------------------------------------------------- 牌桌 */

.table {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(96px, 14%) 1fr minmax(96px, 14%);
  grid-template-rows: auto 1fr auto;
  grid-template-areas:
    ".    top    ."
    "left center right"
    ".    bottom .";
  gap: 6px;
  padding: 10px clamp(8px, 2vw, 28px);
  background:
    radial-gradient(ellipse at 50% 45%, var(--felt) 0%, var(--felt-2) 55%, #0a2415 100%);
  border-radius: 18px;
  margin: 8px clamp(6px, 1.5vw, 20px) 0;
  border: 1px solid rgba(0, 0, 0, 0.5);
  box-shadow: inset 0 0 80px rgba(0, 0, 0, 0.45);
  position: relative;
}

.seat {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  padding: 6px;
  border-radius: 12px;
  border: 1px solid transparent;
  min-width: 0;
  transition: background 0.2s ease, border-color 0.2s ease;
}
.seat.active { background: rgba(245, 196, 81, 0.1); border-color: rgba(245, 196, 81, 0.45); }
.seat.finished { opacity: 0.55; }
.seat.team-mate .seat-name { color: #8bd4a2; }

.seat-top { grid-area: top; }
.seat-left { grid-area: left; }
.seat-right { grid-area: right; }
.seat-bottom { grid-area: bottom; }

.seat-name {
  font-size: 13px;
  font-weight: 600;
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.seat-meta { font-size: 11px; color: var(--muted); display: flex; gap: 6px; align-items: center; }
.seat-count {
  min-width: 22px;
  text-align: center;
  padding: 1px 6px;
  border-radius: 999px;
  background: rgba(0, 0, 0, 0.35);
  color: var(--text);
  font-variant-numeric: tabular-nums;
}
.seat-count.low { color: #ffd54f; }
.seat-crown { color: var(--gold); font-size: 12px; }

.avatar {
  width: 42px; height: 42px;
  border-radius: 50%;
  display: grid; place-items: center;
  font-weight: 700;
  background: linear-gradient(160deg, #37474f, #263238);
  border: 1px solid rgba(255, 255, 255, 0.16);
  color: #cfd8dc;
  flex: 0 0 auto;
}

/* ---------------------------------------------------------------- 中央 */

.center {
  grid-area: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  min-height: 0;
  padding: 4px;
}

.trick {
  min-height: 92px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  flex-wrap: wrap;
  padding: 8px 12px;
  border-radius: 14px;
  background: rgba(0, 0, 0, 0.18);
  border: 1px dashed rgba(255, 255, 255, 0.14);
  max-width: 100%;
}
.trick-label { font-size: 12px; color: var(--muted); width: 100%; text-align: center; }
.trick-label strong { color: var(--gold); font-weight: 600; }
.trick-empty { color: rgba(255, 255, 255, 0.35); font-size: 13px; }

.center-hint { font-size: 12px; color: var(--muted); text-align: center; min-height: 18px; }
.center-hint.warn { color: #ffb74d; }

/* ---------------------------------------------------------------- 牌 */

.card {
  width: 56px;
  height: 78px;
  border-radius: 8px;
  background: linear-gradient(170deg, #ffffff, #eceff1);
  color: var(--card-black);
  border: 1px solid rgba(0, 0, 0, 0.25);
  box-shadow: 0 3px 8px rgba(0, 0, 0, 0.35);
  position: relative;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  padding: 5px 6px;
  font-weight: 700;
  user-select: none;
  flex: 0 0 auto;
  transition: transform 0.1s ease, box-shadow 0.1s ease;
}
.card.red { color: var(--card-red); }
.card .rank { font-size: 15px; line-height: 1; }
.card .suit { font-size: 18px; line-height: 1; align-self: flex-end; }
.card .corner { font-size: 11px; line-height: 1; align-self: flex-end; opacity: 0.7; }
.card.joker .rank { font-size: 12px; writing-mode: vertical-rl; letter-spacing: 1px; }
.card.wild { outline: 2px solid var(--gold); outline-offset: -2px; }
.card.wild::after {
  content: "配";
  position: absolute;
  top: 2px; right: 3px;
  font-size: 9px;
  background: var(--gold);
  color: #3b2b00;
  border-radius: 3px;
  padding: 0 2px;
}
.card.level-card { box-shadow: 0 0 0 2px rgba(245, 196, 81, 0.55), 0 3px 8px rgba(0,0,0,0.35); }

/* ---------------------------------------------------------------- 手牌 */

.dock {
  padding: 6px clamp(8px, 2vw, 24px) calc(8px + env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 6px;
  background: linear-gradient(180deg, rgba(0,0,0,0) 0%, rgba(0,0,0,0.35) 100%);
}

.hand {
  display: flex;
  justify-content: center;
  align-items: flex-end;
  min-height: 92px;
  padding: 6px 4px 0;
  overflow-x: auto;
  overflow-y: hidden;
  scrollbar-width: thin;
}
.hand .card { margin-left: -16px; cursor: pointer; }
.hand .card:first-child { margin-left: 0; }
.hand .card.selected { transform: translateY(-14px); box-shadow: 0 8px 18px rgba(0,0,0,0.5); }
.hand.disabled .card { cursor: default; }

.actions {
  display: flex;
  gap: 8px;
  justify-content: center;
  flex-wrap: wrap;
}

/* ---------------------------------------------------------------- 弹窗 / 提示 */

.modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  display: grid;
  place-items: center;
  z-index: 50;
  padding: 16px;
}
.modal[hidden] { display: none; }
.modal-card {
  width: min(440px, 100%);
  background: #16212b;
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 20px;
  box-shadow: var(--shadow);
  max-height: calc(100vh - 32px);
  overflow-y: auto;
}
.modal-card h2 { margin: 0 0 10px; font-size: 18px; color: var(--gold); }
.modal-card p { margin: 6px 0; color: var(--muted); font-size: 14px; line-height: 1.6; }
.modal-card .actions { margin-top: 16px; }
.rank-list { display: grid; gap: 6px; margin: 10px 0; }
.rank-row {
  display: flex; align-items: center; gap: 8px;
  padding: 6px 10px; border-radius: 8px;
  background: rgba(255, 255, 255, 0.05);
  font-size: 13px;
}
.rank-row .place { color: var(--gold); font-weight: 700; width: 24px; }
.rank-row .who { flex: 1; }
.rank-row.mine { background: rgba(245, 196, 81, 0.12); }

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(120px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  background: rgba(20, 30, 40, 0.95);
  border: 1px solid var(--line);
  color: var(--text);
  padding: 8px 16px;
  border-radius: 999px;
  font-size: 13px;
  z-index: 60;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease, transform 0.2s ease;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(-6px); }

/* ---------------------------------------------------------------- 表单 */

.field {
  display: flex;
  align-items: center;
  gap: 10px;
  margin: 10px 0;
  font-size: 13px;
  color: var(--muted);
}
.field > span { flex: 0 0 42px; }
.field input {
  flex: 1;
  min-width: 0;
  padding: 9px 11px;
  border-radius: 9px;
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid var(--line);
  color: var(--text);
  font: inherit;
  outline: none;
}
.field input:focus { border-color: rgba(245, 196, 81, 0.5); }

/* ---------------------------------------------------------------- 房间列表 */

.room-list-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-top: 18px;
  font-size: 13px;
  color: var(--muted);
}
.room-list-head button { padding: 4px 10px; font-size: 12px; }
.room-list {
  list-style: none;
  margin: 8px 0 0;
  padding: 0;
  display: grid;
  gap: 6px;
  max-height: 190px;
  overflow-y: auto;
}
.room-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  border-radius: 9px;
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid transparent;
  font-size: 13px;
  cursor: pointer;
}
.room-item:hover { border-color: rgba(245, 196, 81, 0.45); background: rgba(245, 196, 81, 0.08); }
.room-item.started { opacity: 0.75; }
.room-code {
  font-family: ui-monospace, Consolas, monospace;
  letter-spacing: 1px;
  color: var(--gold);
  font-weight: 600;
}
.room-host { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--muted); }
.room-count { color: var(--muted); font-variant-numeric: tabular-nums; }
.room-tag {
  padding: 1px 7px;
  border-radius: 999px;
  font-size: 11px;
  background: rgba(255, 255, 255, 0.1);
}
.room-item:not(.started) .room-tag { background: rgba(67, 160, 71, 0.25); color: #9be49f; }
.room-empty {
  padding: 10px;
  font-size: 13px;
  color: var(--muted);
  text-align: center;
  list-style: none;
}

/* ---------------------------------------------------------------- 观战 */

/* 观战者不显示手牌区和出牌按钮，也不允许“重新开始” */
body.spectating .dock { display: none; }
body.spectating #btn-new { display: none; }
body.spectating .center-hint { color: var(--gold); }

/* ---------------------------------------------------------------- 移动端 */

@media (max-width: 720px) {
  .table {
    grid-template-columns: 78px 1fr 78px;
    padding: 6px 6px;
    margin: 6px 4px 0;
    border-radius: 14px;
  }
  .card { width: 44px; height: 62px; border-radius: 6px; padding: 4px; }
  .card .rank { font-size: 12px; }
  .card .suit { font-size: 14px; }
  .card.joker .rank { font-size: 10px; }
  .hand .card { margin-left: -22px; }
  .hand .card.selected { transform: translateY(-10px); }
  .hand { min-height: 76px; }
  .trick { min-height: 70px; padding: 6px; }
  .avatar { width: 34px; height: 34px; font-size: 13px; }
  .seat-name { font-size: 12px; }
  .dock { padding: 4px 6px calc(6px + env(safe-area-inset-bottom)); }
  .actions button { padding: 8px 12px; }
  .topbar { gap: 8px; padding: 6px 10px; font-size: 12px; }
}

@media (max-height: 620px) {
  .hand { min-height: 70px; }
  .card { width: 42px; height: 58px; }
  .trick { min-height: 64px; }
}
