:root {
  --felt: #0f5a3e;
  --felt-dark: #082f21;
  --ink: #1d1d1d;
  --ivory: #fbf6e9;
  --jade: #1e8a62;
  --jade-dark: #126244;
  --gold: #f4c04e;
  --gold-dark: #c9921c;
  --red: #c62828;
  --panel: rgba(4, 30, 20, .42);
  --panel-line: rgba(255, 255, 255, .09);
  --tile-w: clamp(44px, calc((100vw - 48px) / 15.5), 84px);
  --small-w: calc(var(--tile-w) * 0.55);
  --text: 1;
  --font: "Nunito", "Segoe UI", system-ui, -apple-system, Roboto, sans-serif;
  --cjk: "Noto Serif TC", "Microsoft JhengHei", "PingFang TC", serif;
}
body.text-l { --text: 1.2; }
body.text-xl { --text: 1.4; }
body.xl { --tile-w: clamp(52px, calc((100vw - 40px) / 14.5), 100px); }

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  font-family: var(--font);
  font-size: calc(18px * var(--text));
  color: #fff;
  background-color: var(--felt-dark);
  background-image:
    radial-gradient(ellipse 80% 70% at 50% 45%, rgba(46, 160, 110, .35), transparent 70%),
    url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='160' height='160'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='.9' numOctaves='2' stitchTiles='stitch'/%3E%3CfeColorMatrix values='0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 .07 0'/%3E%3C/filter%3E%3Crect width='160' height='160' filter='url(%23n)'/%3E%3C/svg%3E"),
    radial-gradient(ellipse at center, var(--felt) 0%, var(--felt-dark) 100%);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.hidden { display: none !important; }

/* ---------- buttons ---------- */
button {
  font: inherit;
  font-size: calc(21px * var(--text));
  font-weight: 800;
  letter-spacing: .01em;
  padding: 12px 28px;
  border-radius: 999px;
  border: none;
  background: linear-gradient(180deg, #fffdf6, #ece3cb);
  color: var(--ink);
  cursor: pointer;
  box-shadow: 0 4px 0 rgba(0,0,0,.28), 0 8px 16px rgba(0,0,0,.25);
  min-height: 56px;
  transition: transform .08s, box-shadow .08s, filter .15s;
}
button:hover { filter: brightness(1.05); }
button:active { transform: translateY(3px); box-shadow: 0 1px 0 rgba(0,0,0,.28), 0 3px 8px rgba(0,0,0,.25); }
button:disabled { opacity: .45; cursor: default; filter: none; }
button.primary { background: linear-gradient(180deg, #ffe08a, var(--gold) 60%, #e9aa2c); color: #3a2400; }
button.secondary { background: linear-gradient(180deg, #fffdf6, #e4dac0); }
button.win {
  background: linear-gradient(180deg, #ef5350, var(--red) 60%, #9e1b1b);
  color: #fff3c4; font-size: calc(25px * var(--text));
  animation: glow 1.4s ease-in-out infinite;
}
button.small { font-size: calc(15px * var(--text)); padding: 7px 16px; min-height: 38px; box-shadow: 0 3px 0 rgba(0,0,0,.28); }
@keyframes glow {
  0%, 100% { box-shadow: 0 4px 0 #6d1010, 0 0 0 0 rgba(255, 82, 82, .6); }
  50% { box-shadow: 0 4px 0 #6d1010, 0 0 0 12px rgba(255, 82, 82, 0); }
}

/* ---------- top bar ---------- */
.topbar {
  display: flex; align-items: center; gap: 16px;
  padding: 6px 16px;
  background: linear-gradient(180deg, rgba(0,0,0,.45), rgba(0,0,0,.2));
  border-bottom: 1px solid var(--panel-line);
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 800; font-size: calc(20px * var(--text)); }
.seal {
  font-family: var(--cjk); font-weight: 900; color: #fff3c4;
  background: linear-gradient(180deg, #d84343, #9e1b1b); border-radius: 8px;
  padding: 2px 8px; box-shadow: inset 0 0 0 2px rgba(255, 220, 150, .5);
}
.status { flex: 1; text-align: center; font-size: calc(17px * var(--text)); opacity: .85; }
.top-buttons { display: flex; gap: 8px; }

/* ---------- table layout ---------- */
.table {
  flex: 1; min-height: 0;
  display: grid;
  grid-template-columns: minmax(130px, 15%) 1fr minmax(130px, 15%);
  grid-template-rows: auto 1fr auto;
  grid-template-areas: "left top right" "left center right" "me me me";
  gap: 8px; padding: 8px;
}
.top { grid-area: top; }
.left { grid-area: left; }
.right { grid-area: right; }
.center { grid-area: center; min-height: 0; overflow: hidden; }
.me { grid-area: me; }

.opponent, .me, .center {
  background: var(--panel);
  border: 1px solid var(--panel-line);
  border-radius: 18px;
  box-shadow: inset 0 1px 0 rgba(255,255,255,.06), 0 10px 30px rgba(0,0,0,.25);
}
.opponent {
  padding: 8px 10px;
  display: flex; flex-direction: column; gap: 8px; align-items: center; overflow: hidden;
  transition: box-shadow .3s, border-color .3s;
}
.opponent.top { flex-direction: row; flex-wrap: wrap; justify-content: center; gap: 14px; }
.opponent.active, .me.active {
  border-color: rgba(244, 192, 78, .8);
  box-shadow: 0 0 0 2px rgba(244, 192, 78, .35), 0 0 28px rgba(244, 192, 78, .25), 0 10px 30px rgba(0,0,0,.25);
}

/* player card: avatar, wind badge, name, points */
.pname { display: flex; align-items: center; gap: 10px; }
.avatar {
  position: relative; flex: none;
  width: calc(44px * var(--text)); height: calc(44px * var(--text));
  border-radius: 50%;
  display: grid; place-items: center;
  font-weight: 800; font-size: calc(20px * var(--text)); color: #fff;
  box-shadow: inset 0 -3px 0 rgba(0,0,0,.2), 0 3px 8px rgba(0,0,0,.35);
  border: 2px solid rgba(255,255,255,.75);
}
.seat-0 .avatar { background: linear-gradient(160deg, #f6a04d, #d4561c); }
.seat-1 .avatar { background: linear-gradient(160deg, #6fb8ff, #2a6fc9); }
.seat-2 .avatar { background: linear-gradient(160deg, #c89bff, #7b45c9); }
.seat-3 .avatar { background: linear-gradient(160deg, #ff8fa8, #c73c62); }
.pname .wind {
  position: absolute; right: -8px; bottom: -6px;
  width: calc(22px * var(--text)); height: calc(26px * var(--text));
  display: grid; place-items: center;
  font-family: var(--cjk); font-weight: 900; font-size: calc(15px * var(--text));
  color: #1d2a5c; background: var(--ivory); border-radius: 5px;
  box-shadow: 0 2px 0 var(--jade-dark), 0 3px 6px rgba(0,0,0,.4);
}
.pname .wind.dealer { color: #3a2400; background: linear-gradient(180deg, #ffe08a, var(--gold)); }
.who { display: flex; flex-direction: column; line-height: 1.15; }
.who .nm { font-weight: 800; font-size: calc(18px * var(--text)); }
.who .pts { font-weight: 700; font-size: calc(15px * var(--text)); color: #ffe08a; }
.who .tag { font-size: calc(12px * var(--text)); font-weight: 800; color: #3a2400; background: var(--gold); border-radius: 999px; padding: 0 8px; align-self: flex-start; margin-top: 2px; }

.backs { display: flex; flex-wrap: wrap; gap: 3px; justify-content: center; }
.back {
  width: calc(var(--small-w) * .62); height: calc(var(--small-w) * .84);
  background: linear-gradient(180deg, #3cc391 0%, var(--jade) 45%, var(--jade-dark) 100%);
  border-radius: 4px;
  box-shadow: inset 0 1px 0 rgba(255,255,255,.35), 0 2px 0 #e9dfc4, 0 3px 4px rgba(0,0,0,.35);
}
.left .backs, .right .backs { max-width: 150px; }
.melds { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; align-items: flex-end; }
.meld { display: flex; gap: 1px; }

/* ---------- centre: discards around the compass ---------- */
.center {
  display: grid; padding: 8px; gap: 6px;
  grid-template-columns: 1fr minmax(170px, 28%) 1fr;
  grid-template-rows: minmax(0, 1fr) auto minmax(0, 1fr);
  grid-template-areas: "p2 p2 p2" "p3 compass p1" "p0 p0 p0";
}
.pile-2 { grid-area: p2; } .pile-1 { grid-area: p1; } .pile-3 { grid-area: p3; } .pile-0 { grid-area: p0; }
.compass {
  grid-area: compass; align-self: center;
  display: flex; flex-direction: column; align-items: center; gap: 4px; text-align: center;
  padding: 10px 12px; border-radius: 16px;
  background: radial-gradient(circle at 50% 30%, rgba(255,255,255,.12), rgba(0,0,0,.35));
  border: 1px solid rgba(244, 192, 78, .35);
  box-shadow: inset 0 0 20px rgba(0,0,0,.35);
}
.compass .round { font-family: var(--cjk); font-weight: 900; font-size: calc(34px * var(--text)); color: var(--gold); line-height: 1; text-shadow: 0 2px 6px rgba(0,0,0,.5); }
.compass .sub { font-size: calc(14px * var(--text)); opacity: .85; font-weight: 700; }
.message { font-size: calc(17px * var(--text)); font-weight: 700; min-height: 1.3em; color: #fff; }
.message.fresh { animation: fadein .35s ease-out; }
.pile { display: flex; flex-direction: column; gap: 3px; min-height: 0; overflow-y: auto; }
.pile-1, .pile-3 { align-self: center; max-height: 100%; }
.pile-0, .pile-2 { flex-direction: row; align-items: flex-start; gap: 10px; }
.pile-0 .pile-title, .pile-2 .pile-title { flex: none; width: 5.5em; padding-top: 6px; text-align: right; }
.pile-title { font-size: calc(14px * var(--text)); font-weight: 700; opacity: .7; }
.pile-tiles { display: flex; flex-wrap: wrap; gap: 3px; align-content: flex-start; padding-bottom: 5px; }
.pile .tile.small { --w: calc(clamp(24px, 2.4vw, 36px) * var(--text)); }
.tile.last { box-shadow: 0 0 0 3px var(--gold), 0 0 16px rgba(244,192,78,.8), 0 3px 0 var(--jade-dark); }
.tile.pop { animation: drop .45s cubic-bezier(.2, 1.4, .4, 1); }
@keyframes drop {
  from { transform: translateY(-40%) scale(1.5); opacity: 0; }
  to { transform: none; opacity: 1; }
}
@keyframes fadein { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }

/* ---------- my area ---------- */
.me { padding: 8px 12px 12px; display: flex; flex-direction: column; gap: 8px; }
.me-top { display: flex; align-items: center; gap: 16px; flex-wrap: wrap; }
.prompt { font-size: calc(20px * var(--text)); flex: 1; min-width: 200px; font-weight: 600; }
.prompt b { color: #ffe08a; }
.hint {
  display: inline-block; margin-top: 4px; color: #3a2400; font-weight: 800;
  background: linear-gradient(180deg, #ffe08a, var(--gold)); border-radius: 999px; padding: 2px 12px;
  font-size: calc(16px * var(--text));
}
.hand { display: flex; gap: 4px; justify-content: center; align-items: flex-end; flex-wrap: nowrap; min-height: calc(var(--tile-w) * 1.5); padding-top: calc(var(--tile-w) * .2); }
.hand .gap { width: calc(var(--tile-w) * .35); }
.actions { display: flex; gap: 12px; justify-content: center; flex-wrap: wrap; min-height: 56px; }
.actions > * { animation: fadein .25s ease-out; }
.choice { display: inline-flex; flex-direction: column; align-items: center; gap: 6px; padding: 8px 16px; border-radius: 18px; }
.choice .meld { pointer-events: none; }

/* ---------- tiles ---------- */
.tile {
  --w: var(--tile-w);
  width: var(--w); height: calc(var(--w) * 1.36);
  background: linear-gradient(165deg, #ffffff 0%, var(--ivory) 55%, #ece1c4 100%);
  border: 1px solid #d9cca9;
  border-radius: calc(var(--w) * .13);
  box-shadow:
    inset 0 0 0 calc(var(--w) * .03) rgba(255,255,255,.8),
    0 calc(var(--w) * .07) 0 var(--jade),
    0 calc(var(--w) * .07 + 1px) 0 var(--jade-dark),
    0 calc(var(--w) * .1) calc(var(--w) * .16) rgba(0,0,0,.4);
  margin-bottom: calc(var(--w) * .07);
  color: var(--ink);
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  line-height: 1; position: relative; user-select: none; flex: none;
}
.tile.small { --w: var(--small-w); }
.tile .pic { width: 78%; height: auto; max-height: 72%; }
.tile .main { font-family: var(--cjk); font-size: calc(var(--w) * .6); font-weight: 900; }
.tile .sub { font-family: var(--cjk); font-size: calc(var(--w) * .32); font-weight: 900; margin-top: calc(var(--w) * .02); }
.tile .label { font-size: calc(var(--w) * .19); margin-top: calc(var(--w) * .05); font-weight: 800; color: #5d5440; white-space: nowrap; }
.tile.small .label { display: none; }
body.no-labels .tile .label { display: none; }
body.no-labels .tile .pic { max-height: 86%; }
.tile.small .sub { font-size: calc(var(--w) * .34); }
.tile.small .pic { max-height: 86%; width: 84%; }

.tile.chars .main { color: #1d2a5c; }
.tile.chars .sub { color: var(--red); }
.tile.wind .main { color: #1d2a5c; }
.tile.dragon-red .main { color: var(--red); }
.tile.dragon-green .main { color: #1f8a4c; }
.tile.dragon-white .main { color: #1a5fb4; border: calc(var(--w) * .05) solid #1a5fb4; border-radius: calc(var(--w) * .08); padding: calc(var(--w) * .04) calc(var(--w) * .07); font-size: calc(var(--w) * .42); }
.tile.flower .main, .tile.flower .sub { color: #8e24aa; }
.tile.season .main, .tile.season .sub { color: #e65100; }

.hand .tile { cursor: pointer; transition: transform .15s cubic-bezier(.2, 1.2, .4, 1), box-shadow .15s; }
.hand:not(.waiting) .tile:hover { transform: translateY(calc(var(--tile-w) * -.08)); }
.hand .tile.selected {
  transform: translateY(calc(var(--tile-w) * -.22));
  box-shadow:
    inset 0 0 0 calc(var(--w) * .03) rgba(255,255,255,.8),
    0 0 0 3px var(--gold), 0 0 22px rgba(244,192,78,.75),
    0 calc(var(--w) * .07) 0 var(--jade),
    0 calc(var(--w) * .3) calc(var(--w) * .3) rgba(0,0,0,.45);
}
.hand.waiting .tile { cursor: default; }

/* ---------- pop-ups ---------- */
.modal {
  position: fixed; inset: 0; background: rgba(2, 18, 12, .62); backdrop-filter: blur(3px);
  display: flex; align-items: center; justify-content: center; padding: 16px; z-index: 10;
}
.modal-box {
  background: linear-gradient(180deg, #fffdf6, #f6efdc);
  color: var(--ink); border-radius: 22px; padding: 26px 30px;
  max-width: 820px; width: 100%; max-height: 92vh; overflow: auto;
  border: 2px solid rgba(201, 146, 28, .55);
  box-shadow: 0 30px 80px rgba(0,0,0,.5), inset 0 0 0 6px rgba(255,255,255,.6);
  animation: popin .3s cubic-bezier(.2, 1.3, .4, 1);
}
@keyframes popin { from { transform: scale(.9); opacity: 0; } to { transform: none; opacity: 1; } }
.modal-box h2 { margin: 0 0 12px; font-size: calc(32px * var(--text)); color: #8b1a1a; font-weight: 900; }
.modal-box p { font-size: calc(20px * var(--text)); line-height: 1.4; margin: 8px 0; }
.modal-box .row {
  display: flex; gap: 12px; flex-wrap: wrap; margin: 20px -30px -26px; padding: 14px 30px 20px;
  position: sticky; bottom: -26px; background: linear-gradient(180deg, rgba(246,239,220,.85), #f6efdc 40%);
}
.modal-box label { font-size: calc(19px * var(--text)); display: block; margin: 14px 0 6px; font-weight: 800; color: #5d4a1c; }
.modal-box input[type=text] { font: inherit; font-size: calc(22px * var(--text)); padding: 10px 16px; border-radius: 14px; border: 2px solid #d9cca9; background: #fff; width: 100%; max-width: 360px; }
.modal-box input[type=text]:focus { outline: none; border-color: var(--gold-dark); box-shadow: 0 0 0 4px rgba(244,192,78,.35); }
.seg { display: flex; gap: 8px; flex-wrap: wrap; }
.seg button { min-height: 48px; padding: 8px 22px; box-shadow: 0 3px 0 rgba(0,0,0,.18); }
.seg button.on { background: linear-gradient(180deg, #ffe08a, var(--gold)); color: #3a2400; }
.faan-list { font-size: calc(20px * var(--text)); border-collapse: collapse; margin: 8px 0; }
.faan-list td { padding: 4px 18px 4px 0; }
.faan-list tr.total td { font-weight: 900; border-top: 2px solid #c9b98c; color: #8b1a1a; }
.scores { font-size: calc(20px * var(--text)); border-collapse: collapse; width: 100%; max-width: 480px; }
.scores td { padding: 7px 10px; border-bottom: 1px solid #e6dcc2; }
.scores tr:first-child td { font-weight: 800; }
.scores .up { color: #1f8a4c; font-weight: 800; }
.scores .down { color: var(--red); font-weight: 800; }
.win-hand { display: flex; flex-wrap: wrap; gap: 10px; align-items: flex-end; margin: 12px 0; padding: 12px; background: var(--felt); border-radius: 14px; box-shadow: inset 0 2px 10px rgba(0,0,0,.4); }
.win-hand .meld { flex-wrap: wrap; }
.win-hand .tile { --w: min(calc(var(--tile-w) * .7), 52px); }
.win-hand .tile.small { --w: min(var(--small-w), 40px); }
.rules-table { font-size: calc(17px * var(--text)); border-collapse: collapse; width: 100%; }
.rules-table td { padding: 5px 8px; border-bottom: 1px solid #e6dcc2; }
.hero { display: flex; gap: 8px; justify-content: center; margin: 4px 0 14px; }
.hero .tile { --w: min(64px, 14vw); animation: drop .5s cubic-bezier(.2, 1.4, .4, 1) backwards; }
.hero .tile:nth-child(2) { animation-delay: .08s; } .hero .tile:nth-child(3) { animation-delay: .16s; }
.hero .tile:nth-child(4) { animation-delay: .24s; } .hero .tile:nth-child(5) { animation-delay: .32s; }
.title-cn { font-family: var(--cjk); font-weight: 900; color: var(--red); margin-right: 8px; }

@media (max-width: 900px), (max-height: 600px) {
  body { font-size: calc(16px * var(--text)); }
  .table { grid-template-columns: minmax(100px, 14%) 1fr minmax(100px, 14%); }
  button { font-size: calc(18px * var(--text)); padding: 10px 20px; min-height: 48px; }
  .avatar { width: calc(36px * var(--text)); height: calc(36px * var(--text)); }
}
@media (max-height: 700px) {
  .compass { padding: 6px 10px; gap: 2px; }
  .compass .round { font-size: calc(26px * var(--text)); }
  .message { font-size: calc(15px * var(--text)); }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
