:root {
  --felt: #155446;
  --felt-dark: #0d362d;
  --night: #0b2620;
  --cream: #fbf4e2;
  --paper: #f3e8cc;
  --ink: #1b1b1b;
  --muted: #9fb8ad;
  --red: #c8102e;
  --red-dark: #8c0b20;
  --gold: #e6b422;
  --gold-dark: #a87f10;
  --jade: #1f8a5b;
  --sword: #2a2a2a;
  --pagoda: #1f5fbf;
  --star: #c8102e;
  --team-0: #e6b422;
  --team-1: #7cc4ff;
  --radius: 14px;
  --shadow: 0 6px 18px rgba(0, 0, 0, 0.35);
  --cw: 64px;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  min-height: 100%;
}

body {
  font-family: 'Trebuchet MS', 'Segoe UI', system-ui, sans-serif;
  background: radial-gradient(circle at 50% 0%, #1d6b58, var(--night) 70%) fixed;
  color: var(--cream);
  -webkit-tap-highlight-color: transparent;
}

#root {
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
}

main {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 12px 16px 16px;
}

h1,
h2,
h3 {
  margin: 0 0 0.5em;
}

/* ---------- buttons and inputs ---------- */

button {
  font: inherit;
  cursor: pointer;
  border: 0;
  border-radius: 10px;
  padding: 0.6em 1.1em;
  font-weight: 700;
  transition: transform 0.08s, filter 0.15s, opacity 0.15s;
}
button:active:not(:disabled) {
  transform: translateY(1px) scale(0.98);
}
button:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}
button.primary {
  background: var(--gold);
  color: var(--ink);
  box-shadow: 0 4px 0 var(--gold-dark);
}
button.secondary {
  background: var(--cream);
  color: var(--ink);
  box-shadow: 0 4px 0 #bcae88;
}
button.danger {
  background: var(--red);
  color: #fff;
  box-shadow: 0 4px 0 var(--red-dark);
}
button.ghost {
  background: transparent;
  color: var(--cream);
  border: 2px solid rgba(251, 244, 226, 0.35);
}
button.ghost:hover {
  border-color: var(--cream);
}
button.link {
  background: transparent;
  color: var(--muted);
  text-decoration: underline;
  font-weight: 400;
  padding: 0.2em;
}
button.small {
  padding: 0.35em 0.8em;
  font-size: 0.9rem;
}
button.big {
  font-size: 1.15rem;
  padding: 0.8em 1.4em;
  width: 100%;
}

input,
select {
  font: inherit;
  border-radius: 10px;
  border: 2px solid transparent;
  padding: 0.6em 0.8em;
  background: var(--cream);
  color: var(--ink);
  min-width: 0;
}
input:focus,
select:focus {
  outline: none;
  border-color: var(--gold);
}

.muted {
  color: var(--muted);
}
.center-text {
  text-align: center;
}

/* ---------- frame ---------- */

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 16px;
  background: rgba(0, 0, 0, 0.25);
}
.logo {
  font-family: Georgia, 'Times New Roman', serif;
  font-weight: 700;
  font-size: 1.4rem;
  letter-spacing: 0.12em;
  color: var(--gold);
}
.logo span {
  font-family: inherit;
  letter-spacing: 0.02em;
  font-size: 0.85rem;
  color: var(--cream);
  font-weight: 400;
}
.topbar-actions {
  display: flex;
  gap: 8px;
}

.banner {
  background: var(--red);
  color: #fff;
  text-align: center;
  padding: 8px 16px;
  font-weight: 700;
}

.panel {
  background: rgba(8, 30, 25, 0.85);
  border: 1px solid rgba(251, 244, 226, 0.12);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 24px;
  width: 100%;
  max-width: 480px;
}

.code {
  font-family: ui-monospace, 'SFMono-Regular', Menlo, monospace;
  background: var(--gold);
  color: var(--ink);
  padding: 0.05em 0.4em;
  border-radius: 8px;
  letter-spacing: 0.1em;
}

.overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  z-index: 20;
}
.overlay .panel {
  max-height: calc(100dvh - 32px);
  overflow: auto;
  position: relative;
}
.close {
  position: absolute;
  top: 8px;
  right: 8px;
  background: transparent;
  color: var(--cream);
  font-size: 1.6rem;
  padding: 0 0.4em;
}
.rules {
  max-width: 640px;
  line-height: 1.5;
}
.rules h3 {
  margin-top: 1em;
  color: var(--gold);
}

.toasts {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translateX(-50%);
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 30;
  width: max-content;
  max-width: calc(100vw - 32px);
}
.toast {
  background: var(--cream);
  color: var(--ink);
  padding: 10px 16px;
  border-radius: 10px;
  box-shadow: var(--shadow);
  font-weight: 700;
  animation: pop 0.2s ease-out;
}
@keyframes pop {
  from {
    transform: translateY(8px);
    opacity: 0;
  }
}

/* ---------- home and lobby ---------- */

.home,
.lobby {
  margin-top: 4vh;
  display: flex;
  flex-direction: column;
  gap: 14px;
}
.hero {
  display: flex;
  justify-content: center;
  gap: 10px;
  margin-bottom: 4px;
}
.mini-card {
  width: 54px;
  height: 78px;
  border-radius: 8px;
  background: var(--cream);
  color: var(--red);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.4rem;
  font-weight: 700;
  box-shadow: var(--shadow);
}
.mini-card:first-child {
  transform: rotate(-8deg);
}
.mini-card:last-child {
  transform: rotate(8deg);
}
.tagline {
  text-align: center;
  font-size: 1.1rem;
  margin: 0;
}
.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.divider {
  display: flex;
  align-items: center;
  gap: 10px;
  color: var(--muted);
}
.divider::before,
.divider::after {
  content: '';
  flex: 1;
  border-top: 1px solid rgba(251, 244, 226, 0.2);
}
.join-row,
.share-row {
  display: flex;
  gap: 8px;
}
.join-row input,
.share-row input {
  flex: 1;
}
.code-input {
  text-transform: uppercase;
  letter-spacing: 0.3em;
  font-family: ui-monospace, Menlo, monospace;
  text-align: center;
}

.lobby-table {
  display: grid;
  grid-template-columns: 1fr 1.2fr 1fr;
  grid-template-areas:
    '. top .'
    'left felt right'
    '. bottom .';
  gap: 8px;
  align-items: center;
}
.lobby-felt {
  grid-area: felt;
  background: var(--felt);
  border-radius: 50%;
  aspect-ratio: 1.3;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  font-size: 0.75rem;
  color: var(--muted);
  padding: 8px;
}
.lobby-seat {
  border: 2px solid rgba(251, 244, 226, 0.15);
  border-radius: 12px;
  padding: 8px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  text-align: center;
  min-height: 86px;
  justify-content: center;
  overflow-wrap: anywhere;
}
.lobby-seat.team-0 {
  border-color: color-mix(in srgb, var(--team-0) 55%, transparent);
}
.lobby-seat.team-1 {
  border-color: color-mix(in srgb, var(--team-1) 55%, transparent);
}
.lobby-seat.mine {
  background: rgba(251, 244, 226, 0.08);
}
.lobby-seat button.link {
  overflow-wrap: normal;
  font-size: 0.85rem;
}
.lobby-seat small {
  color: var(--muted);
}
.pos-0 {
  grid-area: top;
}
.pos-1 {
  grid-area: left;
}
.pos-2 {
  grid-area: right;
}
.pos-3 {
  grid-area: bottom;
}
.team-label {
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
}
.team-0 .team-label {
  color: var(--team-0);
}
.team-1 .team-label {
  color: var(--team-1);
}
.seat-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  justify-content: center;
}
.options {
  display: flex;
  flex-wrap: wrap;
  gap: 12px 20px;
  align-items: center;
}
.options label {
  display: flex;
  gap: 8px;
  align-items: center;
}
.options .check input {
  width: 20px;
  height: 20px;
}

/* ---------- cards ---------- */

.card {
  width: var(--cw);
  height: calc(var(--cw) * 1.42);
  border-radius: calc(var(--cw) * 0.12);
  background: linear-gradient(160deg, #fffaf0, var(--paper));
  border: 1px solid rgba(0, 0, 0, 0.25);
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.35);
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  padding: calc(var(--cw) * 0.07) calc(var(--cw) * 0.1);
  font-weight: 700;
  line-height: 1;
  color: var(--ink);
  font-family: Georgia, 'Times New Roman', serif;
  transition: transform 0.12s ease-out, box-shadow 0.12s;
  user-select: none;
}
button.card {
  cursor: pointer;
}
.card .rank {
  font-size: calc(var(--cw) * 0.42);
}
.card .suit {
  font-size: calc(var(--cw) * 0.32);
  margin-top: 2px;
}
.card.jade {
  color: var(--jade);
}
.card.sword {
  color: var(--sword);
}
.card.pagoda {
  color: var(--pagoda);
}
.card.star {
  color: var(--star);
}
.card.special {
  align-items: center;
  justify-content: center;
  text-align: center;
}
.card.special .rank {
  font-size: calc(var(--cw) * 0.48);
}
.card.special .label {
  font-size: calc(var(--cw) * 0.16);
  font-family: 'Trebuchet MS', system-ui, sans-serif;
  margin-top: 4px;
}
.card.mahjong {
  background: linear-gradient(160deg, #fffaf0, #e7f3ea);
  color: var(--jade);
}
.card.dog {
  background: linear-gradient(160deg, #fffaf0, #ece6dc);
}
.card.phoenix {
  background: linear-gradient(160deg, #fff5e0, #ffd9a8);
  color: #b44a00;
}
.card.dragon {
  background: linear-gradient(160deg, #fff0ee, #ffc9c2);
  color: var(--red);
}
.card.selected {
  transform: translateY(-16px);
  box-shadow: 0 0 0 3px var(--gold), 0 8px 14px rgba(0, 0, 0, 0.4);
}
.card.small {
  --cw: 34px;
}
.card.small .label {
  display: none;
}

.card-row {
  display: flex;
  gap: 3px;
  flex-wrap: wrap;
  justify-content: center;
}

/* ---------- table ---------- */

.table {
  width: 100%;
  max-width: 1100px;
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.scoreboard {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: stretch;
}
.team-score {
  flex: 1 1 140px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  padding: 6px 12px;
  border-radius: 10px;
  background: rgba(0, 0, 0, 0.25);
  border-left: 4px solid var(--team-0);
  font-size: 0.9rem;
  min-width: 0;
}
.team-score span {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.team-score.team-1 {
  border-left-color: var(--team-1);
}
.team-score b {
  font-size: 1.3rem;
}
.round-info {
  flex: 1 1 100%;
  display: flex;
  gap: 10px;
  align-items: center;
  justify-content: center;
  color: var(--muted);
  font-size: 0.85rem;
}
.wish-badge {
  background: var(--gold);
  color: var(--ink);
  border-radius: 999px;
  padding: 2px 10px;
  font-weight: 700;
}

.felt {
  flex: 1;
  min-height: 260px;
  background: radial-gradient(ellipse at center, #1c6a57, var(--felt) 55%, var(--felt-dark));
  border-radius: 28px;
  box-shadow: inset 0 0 0 6px rgba(0, 0, 0, 0.2), var(--shadow);
  display: grid;
  grid-template-columns: minmax(80px, 1fr) minmax(0, 3fr) minmax(80px, 1fr);
  grid-template-rows: auto 1fr;
  grid-template-areas:
    'top top top'
    'left center right';
  padding: 10px;
  gap: 6px;
}

.seat {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  padding: 6px;
  border-radius: 12px;
  text-align: center;
  min-width: 0;
}
.seat-top {
  grid-area: top;
}
.seat-left {
  grid-area: left;
  align-self: center;
}
.seat-right {
  grid-area: right;
  align-self: center;
}
.seat.active,
.my-area.active {
  box-shadow: 0 0 0 3px var(--gold);
  background: rgba(230, 180, 34, 0.12);
}
.seat-name {
  font-weight: 700;
  overflow-wrap: anywhere;
}
.seat.team-0 .seat-name {
  color: var(--team-0);
}
.seat.team-1 .seat-name {
  color: var(--team-1);
}
.offline {
  color: var(--muted);
  font-weight: 400;
}
.backs {
  display: flex;
  align-items: center;
  height: 34px;
}
.back {
  width: 14px;
  height: 30px;
  margin-left: -8px;
  border-radius: 3px;
  background: repeating-linear-gradient(45deg, var(--red), var(--red) 3px, var(--red-dark) 3px, var(--red-dark) 6px);
  border: 1px solid var(--gold);
}
.back:first-child {
  margin-left: 0;
}
.count {
  margin-left: 8px;
  font-size: 1.1rem;
}
.seat-status,
.my-status {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  justify-content: center;
  align-items: center;
  min-height: 22px;
}
.call,
.out,
.passed,
.thinking,
.your-turn {
  font-size: 0.75rem;
  font-weight: 700;
  border-radius: 999px;
  padding: 2px 8px;
}
.call {
  background: var(--red);
  color: #fff;
}
.call.grand {
  background: linear-gradient(90deg, var(--red), #e2541b);
}
.out {
  background: var(--cream);
  color: var(--ink);
}
.passed {
  background: rgba(0, 0, 0, 0.35);
  color: var(--muted);
}
.thinking {
  color: var(--muted);
  font-weight: 400;
}
.your-turn {
  background: var(--gold);
  color: var(--ink);
}

.center {
  grid-area: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-width: 0;
}
.trick {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
}
.play:not(.top) {
  opacity: 0.4;
  transform: scale(0.85);
}
.play.top .card {
  --cw: 46px;
  animation: pop 0.25s ease-out;
}
.trick-info {
  font-size: 0.9rem;
  text-align: center;
}
.trick-info.lead {
  color: var(--muted);
  font-size: 1.05rem;
}
.log {
  list-style: none;
  margin: 0;
  padding: 0;
  font-size: 0.78rem;
  color: var(--muted);
  text-align: center;
}
.log li:first-child {
  color: var(--cream);
}

.my-area {
  border-radius: 16px;
  padding: 10px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  background: rgba(0, 0, 0, 0.2);
}
.my-area.team-0 .my-status b {
  color: var(--team-0);
}
.my-area.team-1 .my-status b {
  color: var(--team-1);
}
.actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: center;
  align-items: center;
  min-height: 44px;
}
.selection {
  color: var(--muted);
  font-size: 0.9rem;
}
.selection.ok {
  color: var(--cream);
}
.tichu-btn {
  background: var(--red);
  color: #fff;
  box-shadow: 0 4px 0 var(--red-dark);
}
.exchange {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: center;
  align-items: center;
}
.slot {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  background: rgba(0, 0, 0, 0.25);
  border: 2px dashed rgba(251, 244, 226, 0.35);
  color: var(--cream);
  font-weight: 400;
  min-width: 86px;
  min-height: 84px;
  padding: 6px;
}
.slot.filled {
  border-style: solid;
  border-color: var(--gold);
}
.slot-empty {
  font-size: 0.75rem;
  color: var(--muted);
  margin: auto 0;
}
.received {
  display: flex;
  flex-wrap: wrap;
  gap: 8px 12px;
  align-items: center;
  justify-content: center;
  font-size: 0.85rem;
}
.gift {
  display: inline-flex;
  align-items: center;
  gap: 4px;
}
.wish-picker {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  justify-content: center;
  align-items: center;
}
.chip {
  padding: 0.3em 0.6em;
  background: rgba(0, 0, 0, 0.3);
  color: var(--cream);
  font-size: 0.9rem;
}
.chip.on {
  background: var(--gold);
  color: var(--ink);
}

.hand {
  width: 100%;
  padding-top: 18px;
  height: calc(var(--cw) * 1.42 + 22px);
}
.hand-inner {
  position: relative;
  margin: 0 auto;
  height: 100%;
}
.hand-slot {
  position: absolute;
  top: 0;
}

.result {
  text-align: center;
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.result table {
  width: 100%;
  border-collapse: collapse;
}
.result th,
.result td {
  padding: 6px;
  border-bottom: 1px solid rgba(251, 244, 226, 0.12);
}
.result th {
  font-size: 0.8rem;
  color: var(--muted);
  font-weight: 400;
}
.result td:first-child {
  text-align: left;
  color: var(--muted);
}
.result tr.sum td {
  font-weight: 700;
}
.result tr.total td {
  font-weight: 700;
  font-size: 1.2rem;
  color: var(--gold);
  border-bottom: 0;
}
.calls {
  list-style: none;
  padding: 0;
  margin: 0;
}

@media (max-width: 560px) {
  main {
    padding: 8px 8px 12px;
  }
  .panel {
    padding: 18px;
  }
  .felt {
    grid-template-columns: minmax(64px, 1fr) minmax(0, 2.4fr) minmax(64px, 1fr);
    border-radius: 20px;
    padding: 6px;
  }
  .seat {
    padding: 4px 2px;
    font-size: 0.85rem;
  }
  .back {
    width: 10px;
    height: 22px;
    margin-left: -7px;
  }
  .backs {
    height: 26px;
  }
  .play.top .card {
    --cw: 38px;
  }
  .card.small {
    --cw: 30px;
  }
}
