:root {
  --felt: #0d3b2c;
  --felt-2: #11513b;
  --felt-edge: #082a1f;
  --ink: #15171a;
  --paper: #fffdf8;
  --red: #c8102e;
  --gold: #f2c14e;
  --sky: #6cc4ff;
  --muted: #b9d3c7;
  --cw: 72px;
  --ch: calc(var(--cw) * 1.4);
  --radius: calc(var(--cw) * 0.11);
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
  --ease: cubic-bezier(.2, .8, .2, 1);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
}
body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  color: #f4f7f5;
  background: radial-gradient(120% 80% at 50% 35%, var(--felt-2), var(--felt) 60%, var(--felt-edge));
  background-color: var(--felt);
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none;
  user-select: none;
  touch-action: manipulation;
  -webkit-text-size-adjust: 100%;
}
input { -webkit-user-select: text; user-select: text; }
[hidden] { display: none !important; }
button { font: inherit; color: inherit; }

.screen {
  position: fixed;
  inset: 0;
  padding: calc(var(--safe-t) + 12px) calc(var(--safe-r) + 16px) calc(var(--safe-b) + 12px) calc(var(--safe-l) + 16px);
  display: flex;
  flex-direction: column;
}

/* ---------- Buttons and fields ---------- */
.btn {
  appearance: none;
  border: 2px solid rgba(255, 255, 255, .25);
  background: rgba(255, 255, 255, .08);
  border-radius: 14px;
  padding: 10px 16px;
  font-weight: 700;
  cursor: pointer;
  touch-action: manipulation;
  transition: transform .12s var(--ease), background .2s, opacity .2s;
}
.btn:active:not(:disabled) { transform: scale(.97); }
.btn:disabled { opacity: .4; cursor: default; }
.btn.big { min-height: 58px; font-size: 1.15rem; width: 100%; }
.btn.small { min-height: 44px; font-size: .95rem; padding: 8px 12px; }
.btn.primary { background: var(--gold); color: #1d1606; border-color: var(--gold); }
.btn.ghost { background: transparent; border-color: transparent; color: var(--muted); }
.btn.win:not(:disabled) {
  background: var(--gold); color: #1d1606; border-color: var(--gold);
  animation: glow 1.4s ease-in-out infinite alternate;
}
@keyframes glow { from { box-shadow: 0 0 0 0 rgba(242, 193, 78, .0); } to { box-shadow: 0 0 18px 2px rgba(242, 193, 78, .65); } }

.icon-btn {
  appearance: none; border: 0; background: rgba(0, 0, 0, .25); border-radius: 50%;
  width: 40px; height: 40px; font-size: 1rem; cursor: pointer;
}

/* ---------- Home ---------- */
#home { overflow-y: auto; }
.home-inner, .lobby-inner { width: 100%; max-width: 420px; margin: auto; display: flex; flex-direction: column; gap: 14px; }
h1 { margin: 0; font-size: 2.3rem; letter-spacing: -.02em; text-align: center; }
.tag { margin: -6px 0 6px; text-align: center; color: var(--muted); }
.logo { display: flex; justify-content: center; height: 92px; margin-top: 8px; }
.logo-card {
  width: 58px; height: 82px; border-radius: 8px; background: var(--paper); color: var(--ink);
  font-weight: 800; font-size: 1.6rem; display: flex; flex-direction: column; align-items: center; justify-content: center;
  line-height: 1; box-shadow: 0 6px 14px rgba(0, 0, 0, .35); margin: 0 -8px;
}
.logo-card i { font-style: normal; font-size: 1.5rem; }
.logo-card.red { color: var(--red); }
.logo-card.c1 { transform: rotate(-12deg) translateY(6px); }
.logo-card.c3 { transform: rotate(12deg) translateY(6px); }
.field { display: flex; flex-direction: column; gap: 6px; font-weight: 600; color: var(--muted); }
.field input, .join input {
  appearance: none; border: 2px solid rgba(255, 255, 255, .25); background: rgba(0, 0, 0, .22);
  color: #fff; border-radius: 14px; padding: 12px 14px; font-size: 1.15rem; min-height: 54px; width: 100%;
}
.field input:focus, .join input:focus { outline: none; border-color: var(--gold); }
.or { display: flex; align-items: center; gap: 10px; color: var(--muted); font-size: .9rem; }
.or::before, .or::after { content: ""; flex: 1; height: 1px; background: rgba(255, 255, 255, .2); }
.join { display: flex; gap: 10px; }
.join input {
  flex: 1; min-width: 0; text-align: center; text-transform: uppercase; letter-spacing: .4em;
  font-size: 1.6rem; font-weight: 800; padding-left: .4em;
}
.join .btn { width: auto; flex: 0 0 110px; }
.normal-home { display: flex; flex-direction: column; gap: 14px; }
.invite { display: flex; flex-direction: column; gap: 12px; text-align: center; background: rgba(0, 0, 0, .18); border-radius: 18px; padding: 16px 14px 8px; }
.invite h2 { margin: 0; font-size: 1.5rem; }
.small-code { margin: 2px 0 4px; }
.big-code.small-code span { width: 56px; height: 72px; font-size: 40px; }
.lobby-hint { margin: 4px 0 0; color: var(--muted); font-size: .9rem; line-height: 1.4; }
.rules { background: rgba(0, 0, 0, .18); border-radius: 14px; padding: 10px 14px; color: #dbe8e2; }
.rules summary { cursor: pointer; font-weight: 700; min-height: 32px; display: flex; align-items: center; }
.rules ul { margin: 6px 0 4px; padding-left: 18px; line-height: 1.45; font-size: .95rem; }
.rules li + li { margin-top: 6px; }

/* ---------- Lobby ---------- */
.lobby-inner { text-align: center; }
.eyebrow { margin: 0; color: var(--muted); text-transform: uppercase; letter-spacing: .15em; font-size: .85rem; font-weight: 700; }
.big-code { display: flex; justify-content: center; gap: 10px; margin: 6px 0 12px; }
.big-code span {
  width: min(19vw, 78px); height: min(25vw, 102px); display: grid; place-items: center;
  background: var(--paper); color: var(--ink); border-radius: 12px; font-size: min(13vw, 56px); font-weight: 900;
  box-shadow: 0 6px 16px rgba(0, 0, 0, .35);
}
.share-link { margin: 0; color: var(--muted); font-size: .95rem; word-break: break-all; -webkit-user-select: text; user-select: text; }
.waiting { margin-top: 10px; display: flex; align-items: center; justify-content: center; gap: 10px; font-weight: 600; }
.dots { display: inline-flex; gap: 4px; }
.dots i { width: 8px; height: 8px; border-radius: 50%; background: var(--gold); animation: bounce 1s infinite ease-in-out; }
.dots i:nth-child(2) { animation-delay: .15s; }
.dots i:nth-child(3) { animation-delay: .3s; }
@keyframes bounce { 0%, 80%, 100% { transform: translateY(0); opacity: .5; } 40% { transform: translateY(-6px); opacity: 1; } }

/* ---------- Game layout ---------- */
#game { padding-top: calc(var(--safe-t) + 8px); gap: 6px; }
.topbar { display: flex; align-items: center; gap: 10px; min-height: 44px; }
.room-chip {
  font-weight: 800; letter-spacing: .2em; background: rgba(0, 0, 0, .25); border-radius: 10px;
  padding: 6px 8px 6px 11px; font-size: .95rem;
}
.score { margin-left: auto; font-weight: 700; font-size: .95rem; text-align: right; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 65vw; }
.score b { color: var(--gold); font-size: 1.15rem; }

.opponent { display: flex; flex-direction: column; align-items: center; gap: 6px; min-height: 0; }
.opp-head { display: flex; align-items: center; gap: 8px; font-weight: 700; }
.dot { width: 9px; height: 9px; border-radius: 50%; background: #3ddc84; box-shadow: 0 0 6px #3ddc84; }
.dot.off { background: #888; box-shadow: none; }
.opp-hand { display: flex; justify-content: center; height: calc(var(--cw) * .7); }
.opp-hand .back { width: calc(var(--cw) * .5); height: calc(var(--cw) * .7); margin: 0 calc(var(--cw) * -.14); border-radius: 5px; }
.last-move { margin: 0; min-height: 1.3em; font-size: .9rem; color: var(--muted); text-align: center; }

.table {
  --cw: var(--pcw, 84px); --ch: calc(var(--cw) * 1.4); --radius: calc(var(--cw) * .11);
  flex: 1; min-height: calc(var(--ch) + 24px);
  display: flex; align-items: center; justify-content: center; gap: max(28px, 9vw);
}
.pile {
  appearance: none; border: 0; padding: 0; background: none; cursor: pointer; position: relative;
  width: var(--cw); height: var(--ch); border-radius: var(--radius);
}
.pile:disabled { cursor: default; }
.pile.ready { animation: pulse 1.6s ease-in-out infinite; }
@keyframes pulse { 0%, 100% { box-shadow: 0 0 0 0 rgba(242, 193, 78, .0); } 50% { box-shadow: 0 0 0 6px rgba(242, 193, 78, .55); } }
.pile.target { box-shadow: 0 0 0 4px var(--sky); }
.draw-pile .stack {
  position: absolute; inset: 0; border-radius: var(--radius);
  box-shadow: 2px 2px 0 #e9e3d6, 4px 4px 0 #cfc8b9, 6px 6px 0 #b8b1a1, 0 10px 18px rgba(0, 0, 0, .4);
}
.draw-pile .stack, .back {
  background:
    repeating-linear-gradient(45deg, rgba(255, 255, 255, .12) 0 4px, transparent 4px 9px),
    linear-gradient(135deg, #a3182e, #6d0e1e);
  border: 3px solid var(--paper);
}
.draw-pile.empty .stack { background: rgba(0, 0, 0, .2); border-style: dashed; box-shadow: none; }
.count {
  position: absolute; left: 50%; bottom: -12px; transform: translateX(-50%);
  background: var(--ink); color: #fff; border-radius: 999px; padding: 2px 9px; font-size: .8rem; font-weight: 800;
  border: 2px solid rgba(255, 255, 255, .3);
}
.discard-pile .top, .discard-pile .under { position: absolute; inset: 0; }
.discard-pile .under .card { position: absolute; inset: 0; }
.discard-pile .empty-slot { position: absolute; inset: 0; border-radius: var(--radius); border: 2px dashed rgba(255, 255, 255, .3); }

.status {
  text-align: center; font-weight: 800; font-size: 1.1rem; min-height: 1.6em; padding: 4px 8px;
  border-radius: 12px; transition: background .25s, color .25s;
}
.status.go { background: var(--gold); color: #1d1606; }
.status.can-win { background: #fff3c9; color: #3b2b00; }
.status.away { background: rgba(0, 0, 0, .3); }

.mine { display: flex; flex-direction: column; gap: 10px; padding-bottom: 4px; }
.actions { display: flex; gap: 8px; justify-content: center; }
.actions .btn { flex: 1 1 0; max-width: 170px; padding: 8px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.actions .btn.win { flex-grow: 1.4; }
@media (max-width: 360px) { .actions .btn { font-size: .88rem; padding: 8px 4px; } .status { font-size: 1rem; } }
.hand {
  position: relative; height: calc(var(--ch) + 22px); margin: 0 auto; touch-action: none;
}

/* ---------- Cards ---------- */
.card {
  position: absolute; width: var(--cw); height: var(--ch); border-radius: var(--radius);
  background: var(--paper); color: var(--ink); box-shadow: 0 2px 0 rgba(0, 0, 0, .12), 0 6px 14px rgba(0, 0, 0, .35);
  border: 1px solid rgba(0, 0, 0, .18);
  font-weight: 800; line-height: 1; overflow: hidden;
}
.card.red { color: var(--red); }
.card .idx {
  position: absolute; top: calc(var(--cw) * .06); left: calc(var(--cw) * .07);
  display: flex; flex-direction: column; align-items: center; width: calc(var(--cw) * .3);
}
.card .idx b { font-size: calc(var(--cw) * .32); letter-spacing: -.06em; }
.card .idx i { font-style: normal; font-size: calc(var(--cw) * .27); margin-top: 1px; }
.card .idx.br { top: auto; left: auto; bottom: calc(var(--cw) * .05); right: calc(var(--cw) * .05); transform: rotate(180deg); width: calc(var(--cw) * .2); }
.card .idx.br b { font-size: calc(var(--cw) * .17); }
.card .idx.br i { font-size: calc(var(--cw) * .15); }
.card .pip {
  position: absolute; left: 56%; top: 50%; transform: translate(-50%, -50%);
  font-size: calc(var(--cw) * .48); line-height: 1;
}
.card.face .pip { font-size: calc(var(--cw) * .42); }
.hand .card {
  top: 18px; cursor: grab;
  transition: left .22s var(--ease), top .18s var(--ease), transform .18s var(--ease), box-shadow .18s;
}
.hand .card.selected { top: 0; box-shadow: 0 0 0 3px var(--sky), 0 10px 20px rgba(0, 0, 0, .45); }
.hand .card.dragging { transition: none; z-index: 50 !important; cursor: grabbing; transform: scale(1.06); top: 4px; }
.hand .card.fresh { animation: arrive .35s var(--ease); }
@keyframes arrive { from { transform: translateY(-60px) scale(.9); opacity: 0; } to { transform: none; opacity: 1; } }
.hand .card.locked::after {
  content: "taken"; position: absolute; left: 0; right: 0; bottom: 0; text-align: center; font-size: calc(var(--cw) * .14);
  background: rgba(21, 23, 26, .78); color: #fff; padding: 2px 0; letter-spacing: .05em; text-transform: uppercase;
}
/* Winning melds: a soft band along the bottom of each card. */
.card.m1 { background: linear-gradient(to top, #ffe8a3 0 18%, var(--paper) 18%); }
.card.m2 { background: linear-gradient(to top, #cfe9ff 0 18%, var(--paper) 18%); }
.card.out { filter: brightness(.82); }
.card.out::after {
  content: "discard"; position: absolute; left: 0; right: 0; bottom: 0; text-align: center; font-size: calc(var(--cw) * .14);
  background: rgba(21, 23, 26, .78); color: #fff; padding: 2px 0; letter-spacing: .05em; text-transform: uppercase;
}

.mini { --cw: 40px; --ch: calc(var(--cw) * 1.4); --radius: calc(var(--cw) * .11); }
.mini .card, .row .card { position: relative; display: inline-block; flex: 0 0 auto; }
.row { display: flex; justify-content: center; flex-wrap: wrap; gap: 4px; }
.row.tight .card + .card { margin-left: calc(var(--cw) * -.45); }
.row .card { box-shadow: 0 2px 6px rgba(0, 0, 0, .3); }

/* ---------- Overlay ---------- */
.overlay {
  position: fixed; inset: 0; background: rgba(4, 20, 14, .72); display: flex; align-items: flex-end; justify-content: center;
  padding: 16px calc(var(--safe-r) + 12px) calc(var(--safe-b) + 12px) calc(var(--safe-l) + 12px); z-index: 100;
  animation: fade .25s ease;
}
@keyframes fade { from { opacity: 0; } }
.sheet {
  width: 100%; max-width: 460px; max-height: 100%; overflow-y: auto; background: #fbf8f1; color: var(--ink);
  border-radius: 22px; padding: 18px 16px 14px; display: flex; flex-direction: column; gap: 12px;
  animation: rise .35s var(--ease);
}
@keyframes rise { from { transform: translateY(40px); opacity: 0; } }
.sheet h2 { margin: 0; text-align: center; font-size: 1.6rem; }
.sheet h3 { margin: 4px 0 6px; font-size: .85rem; text-transform: uppercase; letter-spacing: .1em; color: #5b6660; text-align: center; }
.sheet .meld-label { font-size: .75rem; color: #5b6660; text-align: center; margin-top: 2px; }
.melds { display: flex; justify-content: center; gap: 14px; flex-wrap: wrap; }
.over-score { text-align: center; font-weight: 800; font-size: 1.1rem; }
.again-status { margin: 0; text-align: center; color: #5b6660; min-height: 1.2em; }
.sheet .card { border-color: rgba(0, 0, 0, .25); }

/* ---------- Banner and toast ---------- */
.banner {
  position: fixed; top: calc(var(--safe-t) + 8px); left: 50%; transform: translateX(-50%); z-index: 200;
  background: #f5d76e; color: #2b2000; font-weight: 800; padding: 8px 16px; border-radius: 999px;
  box-shadow: 0 6px 16px rgba(0, 0, 0, .35);
}
.toast {
  position: fixed; left: 50%; bottom: calc(var(--safe-b) + 24px); transform: translateX(-50%); z-index: 200;
  background: var(--ink); color: #fff; padding: 12px 16px; border-radius: 14px; max-width: calc(100vw - 32px);
  font-weight: 600; text-align: center; box-shadow: 0 8px 20px rgba(0, 0, 0, .45); animation: fade .2s ease;
}
.noscript { padding: 24px; }

@media (min-height: 760px) { .opp-hand { margin: 4px 0; } }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
