:root {
  --bg: #0f1a0f;
  --bg-panel: #162016;
  --border: #3a5538;
  --text: #e8f0e0;
  --text-muted: #8fa088;
  --accent: #6aaa4a;
  --accent-hover: #82c45e;
  --danger: #e85d5d;
  --warn: #e0b84a;
  --felt: #1e6b3a;
  --felt-dark: #14502c;
  --felt-light: #2a8a4e;
  --card-face: #f7f4ec;
  --card-back: #1a3a6e;
  --red: #c41e3a;
  --black: #1a1a1a;
  --radius: 12px;
  --font: "Segoe UI", "PingFang TC", "Noto Sans TC", "Microsoft JhengHei",
    "Source Han Sans TC", system-ui, sans-serif;
  --card-w: clamp(28px, 6.5vw, 52px);
  --card-h: calc(var(--card-w) * 1.4);
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
  min-height: 100%;
  background:
    radial-gradient(ellipse at 30% 0%, #1e3a22 0%, transparent 55%),
    radial-gradient(ellipse at 80% 100%, #142818 0%, transparent 45%),
    var(--bg);
  color: var(--text);
  font-family: var(--font);
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

body {
  display: flex;
  justify-content: center;
  padding: 12px 10px 20px;
}

.app {
  width: min(1200px, 100%);
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.header {
  text-align: center;
}

.title {
  margin: 0;
  font-size: clamp(1.4rem, 4vw, 1.9rem);
  font-weight: 700;
  letter-spacing: 0.2em;
  color: #d4e8c8;
  text-shadow: 0 2px 10px rgba(0, 0, 0, 0.45);
}

.subtitle {
  margin: 4px 0 0;
  color: var(--text-muted);
  font-size: 0.85rem;
  letter-spacing: 0.18em;
}

.layout {
  display: grid;
  grid-template-columns: minmax(140px, 200px) minmax(280px, 1fr) minmax(160px, 220px);
  gap: 12px;
  align-items: start;
}

.panel {
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.stat-block {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.stat {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.stat-label {
  font-size: 0.72rem;
  color: var(--text-muted);
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.stat-value {
  font-size: 1rem;
  font-weight: 600;
  color: #d8ecd0;
}

.toggle {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.9rem;
  cursor: pointer;
  margin-top: 2px;
}

.actions {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.btn {
  appearance: none;
  border: 1px solid var(--border);
  background: #1e2e1c;
  color: var(--text);
  border-radius: 8px;
  padding: 8px 12px;
  font: inherit;
  font-size: 0.92rem;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s, transform 0.08s;
}

.btn:hover:not(:disabled) {
  background: #2a3e28;
  border-color: var(--accent);
}

.btn:active:not(:disabled) {
  transform: scale(0.98);
}

.btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.btn-primary {
  background: linear-gradient(180deg, #3d8a48, #2a6a38);
  border-color: #4aaa58;
  font-weight: 600;
}

.btn-primary:hover:not(:disabled) {
  background: linear-gradient(180deg, #4aaa58, #358a44);
}

.score-pad {
  border-top: 1px solid var(--border);
  padding-top: 10px;
}

.score-totals {
  display: flex;
  justify-content: space-between;
  margin: 6px 0 8px;
  font-size: 0.95rem;
}

.score-totals strong {
  color: var(--accent-hover);
}

.score-list {
  list-style: none;
  margin: 0;
  padding: 0;
  max-height: 180px;
  overflow-y: auto;
  font-size: 0.78rem;
  color: var(--text-muted);
}

.score-list li {
  padding: 4px 0;
  border-bottom: 1px solid rgba(58, 85, 56, 0.4);
}

/* —— Table —— */
.table-section {
  position: relative;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.table {
  position: relative;
  background:
    radial-gradient(ellipse at 50% 45%, var(--felt-light) 0%, var(--felt) 45%, var(--felt-dark) 100%);
  border: 6px solid #8b6914;
  box-shadow:
    inset 0 0 40px rgba(0, 0, 0, 0.35),
    0 8px 28px rgba(0, 0, 0, 0.5);
  border-radius: 20px;
  min-height: min(62vh, 520px);
  padding: 8px;
  display: grid;
  grid-template-columns: 72px 1fr 72px;
  grid-template-rows: auto 1fr auto;
  grid-template-areas:
    ". north ."
    "west trick east"
    ". south .";
  gap: 4px;
}

.seat-n { grid-area: north; }
.seat-s { grid-area: south; }
.seat-w { grid-area: west; align-self: center; }
.seat-e { grid-area: east; align-self: center; }

.seat {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  min-width: 0;
}

.seat-w, .seat-e {
  writing-mode: horizontal-tb;
}

.seat-tag {
  font-size: 0.75rem;
  color: #d0e8c8;
  background: rgba(0, 0, 0, 0.35);
  padding: 2px 8px;
  border-radius: 999px;
  white-space: nowrap;
}

.seat-tag.active {
  background: var(--warn);
  color: #1a1a10;
  font-weight: 700;
}

.seat-tag.declarer::after {
  content: " · 莊";
}

.seat-tag.dummy::after {
  content: " · 明手";
}

.hand {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 2px;
  max-width: 100%;
}

.hand-w, .hand-e {
  flex-direction: column;
  max-height: 280px;
  overflow: hidden;
}

.hand-w .card, .hand-e .card {
  margin: -6px 0;
}

.hand-n .card, .hand-s .card {
  margin: 0 -6px;
}

.hand-s .card {
  margin: 0 -4px;
}

.hand-s .card:hover {
  transform: translateY(-8px);
  z-index: 5;
}

.hand-s .card.legal {
  box-shadow: 0 0 0 2px var(--accent-hover), 0 4px 8px rgba(0, 0, 0, 0.3);
  cursor: pointer;
}

.hand-s .card.legal:hover {
  transform: translateY(-12px);
}

.hand-s .card.selected {
  transform: translateY(-14px);
  box-shadow: 0 0 0 2px var(--warn);
}

/* 明手／旋轉後非南方 DOM 位亦可點選 */
.card.legal {
  box-shadow: 0 0 0 2px var(--accent-hover), 0 4px 8px rgba(0, 0, 0, 0.3);
  cursor: pointer;
}
.card.selected {
  box-shadow: 0 0 0 2px var(--warn);
}

.card {
  width: var(--card-w);
  height: var(--card-h);
  border-radius: 6px;
  background: var(--card-face);
  border: 1px solid #bbb;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.35);
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  justify-content: flex-start;
  padding: 3px 4px;
  font-weight: 700;
  font-size: calc(var(--card-w) * 0.32);
  line-height: 1.1;
  user-select: none;
  position: relative;
  transition: transform 0.12s;
  color: var(--black);
  flex-shrink: 0;
}

.card.red {
  color: var(--red);
}

.card.back {
  background:
    repeating-linear-gradient(
      45deg,
      #1a3a6e,
      #1a3a6e 4px,
      #2a4a8e 4px,
      #2a4a8e 8px
    );
  color: transparent;
  border-color: #0a2040;
}

.card .suit {
  font-size: 1.15em;
}

.card.tiny {
  width: calc(var(--card-w) * 0.85);
  height: calc(var(--card-h) * 0.85);
  font-size: calc(var(--card-w) * 0.28);
}

/* Trick center */
.trick-area {
  grid-area: trick;
  position: relative;
  min-height: 140px;
  display: grid;
  place-items: center;
}

.trick-card {
  position: absolute;
  width: var(--card-w);
  height: var(--card-h);
}

.trick-card[data-seat="N"] { top: 8%; left: 50%; transform: translateX(-50%); }
.trick-card[data-seat="S"] { bottom: 8%; left: 50%; transform: translateX(-50%); }
.trick-card[data-seat="W"] { left: 8%; top: 50%; transform: translateY(-50%); }
.trick-card[data-seat="E"] { right: 8%; top: 50%; transform: translateY(-50%); }

.trick-center {
  text-align: center;
  font-size: 0.85rem;
  color: rgba(255, 255, 255, 0.85);
  text-shadow: 0 1px 3px rgba(0, 0, 0, 0.6);
  max-width: 70%;
  pointer-events: none;
}

/* Bidding bar */
.bid-bar {
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.bid-levels {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.bid-row {
  display: flex;
  gap: 4px;
  flex-wrap: wrap;
  align-items: center;
}

.bid-row .lvl {
  width: 1.4rem;
  font-size: 0.8rem;
  color: var(--text-muted);
  text-align: center;
}

.bid-btn {
  min-width: 2.6rem;
  padding: 6px 8px;
  font-size: 0.88rem;
}

.bid-btn.suit-red {
  color: #ff8a9a;
}

.bid-btn.disabled-bid,
.bid-btn:disabled {
  opacity: 0.35;
}

.bid-special {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}

.status-bar {
  text-align: center;
  font-size: 0.92rem;
  color: #c8e0c0;
  min-height: 1.4em;
  padding: 4px;
}

/* Log */
.log-section {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.auction-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 2px;
  font-size: 0.75rem;
  text-align: center;
}

.auction-grid .head {
  color: var(--text-muted);
  font-weight: 600;
  padding-bottom: 4px;
  border-bottom: 1px solid var(--border);
}

.auction-grid .cell {
  padding: 3px 2px;
  background: rgba(0, 0, 0, 0.2);
  border-radius: 3px;
  min-height: 1.4em;
}

.auction-grid .cell.bid {
  color: #b8e898;
  font-weight: 600;
}

.play-log {
  margin: 0;
  padding: 0 0 0 1.2rem;
  max-height: 320px;
  overflow-y: auto;
  font-size: 0.78rem;
  color: var(--text-muted);
}

.play-log li {
  margin-bottom: 4px;
}

.play-log .winner {
  color: var(--accent-hover);
  font-weight: 600;
}

/* Overlay */
.overlay {
  position: absolute;
  inset: 0;
  background: rgba(8, 16, 8, 0.72);
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 20px;
  z-index: 20;
}

.overlay.hidden {
  display: none;
}

.overlay-card {
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 24px 28px;
  max-width: 360px;
  text-align: center;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.5);
}

.overlay-card h2 {
  margin: 0 0 10px;
  letter-spacing: 0.15em;
}

.overlay-card p {
  margin: 0 0 16px;
  color: var(--text-muted);
  line-height: 1.5;
  font-size: 0.92rem;
}

.help {
  text-align: center;
  color: var(--text-muted);
  font-size: 0.78rem;
  margin: 0;
}

.help p {
  margin: 0;
}

/* Mobile */
@media (max-width: 860px) {
  .layout {
    grid-template-columns: 1fr;
  }

  .panel-left, .panel-right {
    order: 2;
  }

  .table-section {
    order: 1;
  }

  .table {
    grid-template-columns: 48px 1fr 48px;
    min-height: 420px;
  }

  .hand-w, .hand-e {
    max-height: 200px;
  }

  .score-list, .play-log {
    max-height: 120px;
  }

  .panel-right {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 12px;
  }
}

@media (max-width: 480px) {
  :root {
    --card-w: clamp(24px, 7vw, 40px);
  }

  .panel-right {
    grid-template-columns: 1fr;
  }

  .hand-n .card, .hand-s .card {
    margin: 0 -8px;
  }
}
