:root {
  --red: #fe5f55; --red-s: #a8362f;
  --blue: #1d95ff; --blue-s: #0a5aa3;
  --orange: #ff9a1f; --orange-s: #a35a00;
  --green: #36c170; --green-s: #1d7443;
  --slate: #4f5f68; --slate-s: #273136;
  --gold: #ffd34d;

  --panel: #2c3b42; --panel-edge: #5a6d75; --panel-dark: #1b262b;
  --ink: #ffffff; --muted: #a6bbc3;

  --t0: #e0352b; --t1: #176ee0; --t2: #ee8500; --t3: #23232f;
  --face: #f6eedc; --face-s: #c2b08f; --face-edge: #e4d7bb;

  --p0: #fe5f55; --p1: #1d95ff; --p2: #ff9a1f; --p3: #36c170;

  --tw: clamp(26px, min(8.1vw, 7.4vh), 54px);
  --th: calc(var(--tw) * 1.36);
  --bw: clamp(21px, min(6.5vw, 5.4vh), 42px);
  --bh: calc(var(--bw) * 1.36);

  --sat: env(safe-area-inset-top, 0px);
  --sab: env(safe-area-inset-bottom, 0px);
  font-family: 'Pixelify Sans', ui-monospace, monospace;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; background: #13201f; color: var(--ink); overflow: hidden; overscroll-behavior: none; }
body { user-select: none; -webkit-user-select: none; -webkit-touch-callout: none; letter-spacing: .3px; }
button, input { font-family: inherit; }

#bg { position: fixed; inset: 0; width: 100%; height: 100%; image-rendering: pixelated; z-index: 0; }
#bg.fallback { background: radial-gradient(circle at 30% 30%, #3b6b5f, #10201f); }
#crt {
  position: fixed; inset: 0; pointer-events: none; z-index: 200;
  background:
    repeating-linear-gradient(0deg, rgba(0,0,0,.10) 0 1px, transparent 1px 3px),
    radial-gradient(ellipse at center, transparent 58%, rgba(0,0,0,.5) 100%);
}

#app { position: relative; z-index: 1; height: 100%; }
.screen { display: none; height: 100%; }
.screen.on { display: flex; }

/* ---------- common UI ---------- */
.panel {
  background: var(--panel);
  border: 3px solid var(--panel-edge);
  border-radius: 14px;
  box-shadow: 0 6px 0 rgba(0,0,0,.45);
}
.card { padding: 14px; display: flex; flex-direction: column; gap: 10px; width: 100%; }
.card.center { align-items: center; }
.label { font-size: 13px; color: var(--muted); letter-spacing: 1.5px; font-weight: 600; }
.hint { color: var(--muted); text-align: center; margin: 0; min-height: 1.2em; font-size: 14px; }
.row { display: flex; gap: 8px; }
.row input { flex: 1; min-width: 0; }

input {
  background: var(--panel-dark); color: var(--ink); border: 3px solid #0e1518; border-radius: 10px;
  padding: 12px 14px; font-size: 18px; font-weight: 600; outline: none; width: 100%;
  box-shadow: inset 0 3px 0 rgba(0,0,0,.35); user-select: text; -webkit-user-select: text;
}
input:focus { border-color: var(--blue); }
#code { text-transform: uppercase; letter-spacing: 6px; text-align: center; }

.btn {
  --bc: var(--red); --bs: var(--red-s);
  font-weight: 700; font-size: 17px; letter-spacing: 1px; color: #fff;
  background: var(--bc); border: 0; border-radius: 11px; padding: 12px 14px;
  box-shadow: 0 5px 0 var(--bs), 0 7px 0 rgba(0,0,0,.3);
  text-shadow: 0 2px 0 rgba(0,0,0,.35); cursor: pointer; touch-action: manipulation;
  transition: transform .06s, box-shadow .06s, filter .15s;
  position: relative; top: 0;
}
.btn:hover { filter: brightness(1.08); }
.btn:active { transform: translateY(4px); box-shadow: 0 1px 0 var(--bs), 0 2px 0 rgba(0,0,0,.3); }
.btn.big { font-size: 22px; padding: 16px; }
.btn.small { font-size: 15px; padding: 9px 12px; }
.btn.blue { --bc: var(--blue); --bs: var(--blue-s); }
.btn.orange { --bc: var(--orange); --bs: var(--orange-s); }
.btn.green { --bc: var(--green); --bs: var(--green-s); }
.btn.slate { --bc: var(--slate); --bs: var(--slate-s); }
.btn.wa { --bc: #25d366; --bs: #128c4b; }
a.btn { display: inline-flex; align-items: center; justify-content: center; text-decoration: none; }
.wide { width: 100%; }
.row.wide > * { flex: 1; }
.invited {
  background: var(--panel); border: 3px solid var(--gold); border-radius: 12px; padding: 10px 14px; text-align: center;
  font-size: 16px; box-shadow: 0 5px 0 rgba(0,0,0,.45); animation: pulse 1.6s ease-in-out infinite;
}
.invited b { color: var(--gold); letter-spacing: 2px; }
.invite-mode #btn-create, .invite-mode #home .divider { display: none; }
.invite-mode #btn-join { flex: 1.4; font-size: 20px; }
.share-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.share-grid .btn { font-size: 15px; }
.share-link { background: var(--panel-dark); border-radius: 10px; padding: 10px; font-size: 14px; word-break: break-all; text-align: center; color: var(--muted); user-select: text; -webkit-user-select: text; }
.btn.dim, .btn:disabled { filter: saturate(.25) brightness(.75); }
.btn:disabled { pointer-events: none; }
.btn.pulse { animation: pulse 1.1s ease-in-out infinite; }
@keyframes pulse { 50% { filter: brightness(1.25); transform: scale(1.03); } }

.link { background: none; border: 0; color: var(--muted); font-size: 15px; text-decoration: underline; cursor: pointer; padding: 8px; }

.divider { display: flex; align-items: center; gap: 10px; color: var(--muted); font-size: 12px; letter-spacing: 2px; }
.divider::before, .divider::after { content: ''; flex: 1; height: 3px; background: rgba(255,255,255,.12); border-radius: 2px; }

/* ---------- home & lobby ---------- */
#home, #lobby { overflow-y: auto; justify-content: center; }
.home-inner {
  width: min(420px, 100% - 32px); margin: auto; padding: calc(var(--sat) + 20px) 0 calc(var(--sab) + 20px);
  display: flex; flex-direction: column; align-items: center; gap: 14px;
}
.logo { margin: 0; font-size: clamp(46px, 15vw, 88px); line-height: .95; text-align: center; font-weight: 700; }
.logo .w { display: block; white-space: nowrap; }
.logo span.l {
  display: inline-block; color: var(--lc);
  text-shadow: 0 5px 0 rgba(0,0,0,.55), 3px 0 0 #1a1a22, -3px 0 0 #1a1a22, 0 3px 0 #1a1a22, 0 -3px 0 #1a1a22;
  animation: wob 2.4s ease-in-out infinite; animation-delay: calc(var(--i) * -.18s);
}
@keyframes wob { 0%,100% { transform: translateY(0) rotate(-3deg); } 50% { transform: translateY(-6px) rotate(3deg); } }
.tagline { margin: 0; color: #fff; opacity: .85; text-shadow: 0 2px 0 rgba(0,0,0,.5); font-size: 15px; }
.deco-tiles { display: flex; gap: 6px; margin: 4px 0; }
.deco-tiles .tile { --w: 38px; animation: bob 2.4s ease-in-out infinite; animation-delay: calc(var(--i) * -.3s); }

.roomcode { font-size: 56px; font-weight: 700; letter-spacing: 8px; color: var(--gold); text-shadow: 0 4px 0 rgba(0,0,0,.5); }
#lobby-players { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
#lobby-players li {
  display: flex; align-items: center; gap: 10px; background: var(--panel-dark); border-radius: 10px; padding: 9px 10px;
  border-left: 6px solid var(--pc); font-size: 18px;
}
#lobby-players li .nm { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#lobby-players li .tag { font-size: 12px; color: var(--muted); letter-spacing: 1px; }
#lobby-players li .kick { background: none; border: 0; color: var(--red); font-size: 20px; cursor: pointer; padding: 0 6px; font-family: inherit; }
.seg { display: flex; gap: 6px; }
.seg button {
  flex: 1; background: var(--panel-dark); color: var(--muted); border: 0; border-radius: 9px; padding: 10px 0;
  font-size: 15px; font-weight: 700; box-shadow: 0 3px 0 #0e1518; cursor: pointer;
}
.seg button.on { background: var(--blue); color: #fff; box-shadow: 0 3px 0 var(--blue-s); }
.seg button:disabled { cursor: default; }
#btn-start { width: 100%; }

/* ---------- game ---------- */
#game { flex-direction: column; align-items: center; padding: calc(var(--sat) + 6px) 0 calc(var(--sab) + 6px); }
#game > * { width: min(1100px, 100% - 14px); flex-shrink: 0; }

#players { display: flex; gap: 6px; align-items: stretch; justify-content: center; }
.pchip {
  --pc: var(--p0);
  flex: 1; min-width: 0; max-width: 260px; padding: 5px 7px 6px; border-radius: 10px; background: var(--panel);
  border: 3px solid var(--panel-edge); box-shadow: 0 4px 0 rgba(0,0,0,.4);
  transition: transform .2s, border-color .2s; position: relative;
}
.pchip::before { content: ''; position: absolute; left: 6px; right: 6px; top: -3px; height: 4px; background: var(--pc); border-radius: 0 0 4px 4px; }
.pchip.active { border-color: var(--pc); transform: translateY(-2px) scale(1.03); box-shadow: 0 4px 0 rgba(0,0,0,.4), 0 0 14px var(--pc); }
.pchip.off { opacity: .55; }
.pname { font-size: 13px; font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.pchip.you .pname { color: var(--gold); }
.pmeta { display: flex; justify-content: space-between; align-items: center; font-size: 12px; color: var(--muted); margin-top: 2px; gap: 4px; }
.pcount { display: inline-flex; align-items: center; gap: 4px; color: #fff; font-family: 'Press Start 2P', monospace; font-size: 11px; }
.pcount i { display: inline-block; width: 8px; height: 11px; background: var(--face); border-radius: 2px; box-shadow: 0 2px 0 var(--face-s); }
.ptime { color: var(--gold); font-weight: 700; }
.pool {
  flex: 0 0 auto; display: flex; flex-direction: column; align-items: center; justify-content: center;
  background: var(--panel-dark); border-radius: 10px; padding: 3px 8px; border: 3px solid var(--panel-edge); box-shadow: 0 4px 0 rgba(0,0,0,.4);
}
.pool-n { font-family: 'Press Start 2P', monospace; font-size: 14px; color: var(--gold); line-height: 1; margin-bottom: 3px; }
.pool-l { font-size: 10px; letter-spacing: 1px; color: var(--muted); }

#timerbar { height: 6px; margin-top: 7px; border-radius: 4px; background: rgba(0,0,0,.35); overflow: hidden; }
#timerbar > div { height: 100%; width: 100%; background: var(--gold); transform-origin: left; transform: scaleX(0); }
#timerbar.low > div { background: var(--red); }

#status {
  min-height: 26px; display: flex; align-items: center; justify-content: center; gap: 8px; text-align: center;
  font-size: 15px; font-weight: 600; text-shadow: 0 2px 0 rgba(0,0,0,.6); padding: 3px 4px; flex-wrap: wrap;
}
#status .err { color: #ffb4ae; }
#status .ok { color: #8ff0b5; }
.meld {
  background: var(--panel-dark); border-radius: 8px; padding: 2px 8px; border: 2px solid var(--panel-edge);
}
.meld b { color: var(--gold); }
.meld.done b { color: #8ff0b5; }

#board {
  flex: 1 1 auto; min-height: 90px; overflow-y: auto; overflow-x: hidden;
  padding: 10px; display: flex; flex-wrap: wrap; align-content: flex-start; gap: 8px 8px;
  background: rgba(8, 34, 28, .62); border: 3px solid rgba(255,255,255,.12); border-radius: 16px;
  box-shadow: inset 0 0 40px rgba(0,0,0,.55), 0 5px 0 rgba(0,0,0,.35);
  -webkit-overflow-scrolling: touch;
}
#board .empty { margin: auto; color: rgba(255,255,255,.45); font-size: 16px; text-align: center; letter-spacing: 1px; }
.set {
  display: flex; gap: 2px; align-items: center; padding: 5px 5px 8px; border-radius: 11px;
  background: rgba(0,0,0,.25); border: 2px solid transparent; position: relative;
  min-height: calc(var(--bh) + 17px); transition: transform .12s, border-color .15s, background .15s;
}
.set.ok { border-color: rgba(110, 240, 160, .7); }
.set.bad { border-color: var(--red); background: rgba(254,95,85,.2); animation: badpulse 1s ease-in-out infinite; }
@keyframes badpulse { 50% { border-color: rgba(254,95,85,.35); } }
.set .plus {
  width: calc(var(--bw) * .9); height: calc(var(--bw) * .9); margin-left: 3px; border-radius: 50%;
  border: 0; background: var(--orange); color: #fff; font-weight: 700; font-size: 18px; line-height: 1;
  box-shadow: 0 3px 0 var(--orange-s); cursor: pointer; font-family: inherit; padding: 0;
  animation: plusin .2s ease-out;
}
@keyframes plusin { from { transform: scale(0); } }
.set.newset {
  min-width: calc(var(--bw) * 2.4); justify-content: center; border: 2px dashed rgba(255,255,255,.3);
  color: rgba(255,255,255,.55); font-size: 15px; font-weight: 700; padding: 5px 10px; cursor: pointer;
}
.set.newset.armed { border-color: var(--gold); color: var(--gold); animation: pulse 1.1s ease-in-out infinite; }
.hot { outline: 3px solid #fff; outline-offset: 2px; transform: scale(1.03); }
#rack.hot { transform: none; }

#rack {
  margin-top: 8px; padding: 10px 6px 12px; display: flex; flex-wrap: wrap; gap: 5px 4px; justify-content: center;
  align-content: flex-start;
  min-height: calc(var(--th) * 2 + 37px); max-height: calc(var(--th) * 3 + 50px); overflow-y: auto;
  background: linear-gradient(180deg, #7a5435, #5a3b23); border: 3px solid #9b7049; border-radius: 16px;
  box-shadow: inset 0 3px 0 rgba(255,255,255,.12), inset 0 -4px 0 rgba(0,0,0,.25), 0 6px 0 #2a1a0d;
}
#rack:empty::before { content: 'Your rack is empty'; color: rgba(255,255,255,.5); margin: auto; }

#actions { display: flex; gap: 7px; margin-top: 12px; }
#actions .btn { padding-left: 10px; padding-right: 10px; }
#actions .sq { flex: 0 0 auto; min-width: 46px; font-size: 20px; }
#actions .main { flex: 1; font-size: 20px; }
#btn-sort { font-size: 15px; }

/* ---------- tiles ---------- */
.tile {
  --w: var(--tw);
  --ring: 0 0 0 0 transparent;
  position: relative; flex: 0 0 auto; width: var(--w); height: calc(var(--w) * 1.36);
  border-radius: calc(var(--w) * .17);
  background: linear-gradient(180deg, #fffcf3 0%, var(--face) 55%, #ebdfc4 100%);
  box-shadow: var(--ring), inset 0 -2px 0 var(--face-edge), 0 calc(var(--w) * .09) 0 var(--face-s), 0 calc(var(--w) * .09 + 3px) 0 rgba(0,0,0,.35);
  color: var(--tc); display: grid; place-items: center;
  touch-action: none; cursor: grab;
  transition: translate .12s, box-shadow .12s, opacity .12s;
  margin-bottom: calc(var(--w) * .09);
}
.tile .num {
  font-family: 'Press Start 2P', monospace; font-size: calc(var(--w) * .42); line-height: 1; margin-top: -18%;
  text-shadow: 0 calc(var(--w) * .035) 0 rgba(0,0,0,.18);
}
.tile .num.dd { font-size: calc(var(--w) * .33); letter-spacing: -1px; }
.tile::after {
  content: ''; position: absolute; bottom: 13%; left: 50%; width: calc(var(--w) * .17); height: calc(var(--w) * .17);
  transform: translateX(-50%); border-radius: 50%; background: var(--tc); opacity: .9;
}
.tile.c0 { --tc: var(--t0); }
.tile.c1 { --tc: var(--t1); }
.tile.c2 { --tc: var(--t2); }
.tile.c3 { --tc: var(--t3); }
.tile.joker { --tc: #c23cff; }
.tile.joker .jk {
  font-size: calc(var(--w) * .62); line-height: 1; margin-top: -16%; font-weight: 700;
  background: linear-gradient(135deg, var(--t0), var(--t2), var(--t1), #c23cff);
  -webkit-background-clip: text; background-clip: text; color: transparent;
  animation: hue 3s linear infinite;
}
.tile.joker::after { background: linear-gradient(90deg, var(--t0), var(--t2), var(--t1)); }
@keyframes hue { to { filter: hue-rotate(360deg); } }

#rack .tile { animation: bob 2.8s ease-in-out infinite; animation-delay: calc(var(--i) * -.21s); }
@keyframes bob { 0%,100% { transform: translateY(0) rotate(-1.2deg); } 50% { transform: translateY(-2px) rotate(1.2deg); } }
#board .tile { --w: var(--bw); cursor: default; }
#board .tile.can { cursor: grab; }
.tile.mine { --ring: 0 0 0 2px var(--gold); }
.tile.sel { --ring: 0 0 0 3px #fff, 0 0 0 6px var(--orange); translate: 0 -9px; }
#rack .tile.sel { animation: none; }
.tile.lifted { opacity: .25; }
.tile.fresh { animation: dealin .45s cubic-bezier(.2,1.4,.4,1) both !important; animation-delay: calc(var(--k, 0) * 45ms) !important; }
@keyframes dealin { from { transform: translateY(-80px) rotate(-25deg) scale(.5); opacity: 0; } }
.tile.flash { animation: flash .8s ease-out; }
@keyframes flash { 0% { transform: scale(1.35) rotate(6deg); filter: brightness(1.5) drop-shadow(0 0 8px var(--gold)); } 100% { transform: none; } }
.tile.pop { animation: pop .3s ease-out; }
@keyframes pop { 0% { transform: scale(1.25) rotate(-6deg); } 60% { transform: scale(.94); } 100% { transform: none; } }
.tile.mini { --w: 20px; cursor: default; }

.ghost {
  position: fixed; left: 0; top: 0; z-index: 150; pointer-events: none; display: flex; gap: 2px;
  filter: drop-shadow(0 10px 8px rgba(0,0,0,.45)); will-change: transform;
}
.ghost .tile { --w: var(--tw); animation: none; }
body.dragging, body.dragging * { cursor: grabbing !important; }

/* ---------- overlays ---------- */
#banner {
  position: fixed; left: 0; right: 0; top: 38%; z-index: 120; text-align: center; pointer-events: none;
  font-size: clamp(40px, 13vw, 84px); font-weight: 700; color: var(--gold); opacity: 0;
}
#banner span { display: inline-block; text-shadow: 0 5px 0 rgba(0,0,0,.6), 3px 0 0 #1a1a22, -3px 0 0 #1a1a22, 0 3px 0 #1a1a22, 0 -3px 0 #1a1a22; }
#banner.show { animation: bannerfade 1.6s ease-in forwards; }
#banner.show span { animation: letterin .45s cubic-bezier(.2,1.6,.4,1) both; animation-delay: calc(var(--i) * 40ms); }
@keyframes bannerfade { 0%, 70% { opacity: 1; } 100% { opacity: 0; } }
@keyframes letterin { from { transform: translateY(40px) scale(.2) rotate(-20deg); opacity: 0; } }

#toasts {
  position: fixed; top: 52%; left: 0; right: 0; z-index: 130; display: flex; flex-direction: column;
  align-items: center; gap: 6px; pointer-events: none;
}
.toast {
  background: var(--panel); border: 3px solid var(--panel-edge); border-radius: 10px; padding: 7px 14px; font-size: 15px;
  font-weight: 600; box-shadow: 0 4px 0 rgba(0,0,0,.45); animation: toastin .25s cubic-bezier(.2,1.5,.4,1); max-width: 90vw; text-align: center;
  transition: opacity .3s, transform .3s;
}
.toast.bad { border-color: var(--red); background: #4a2a2a; }
.toast.good { border-color: var(--green); }
.toast.out { opacity: 0; transform: translateY(-10px); }
@keyframes toastin { from { transform: scale(.6); opacity: 0; } }

#modal {
  position: fixed; inset: 0; z-index: 140; background: rgba(5, 10, 12, .7); display: flex; align-items: center; justify-content: center;
  padding: 16px; animation: fadein .2s;
}
#modal[hidden] { display: none; }
@keyframes fadein { from { opacity: 0; } }
.modal-card {
  width: min(440px, 100%); max-height: calc(100% - 20px); overflow-y: auto; padding: 18px; display: flex; flex-direction: column; gap: 12px;
  animation: modalin .3s cubic-bezier(.2,1.4,.4,1);
}
@keyframes modalin { from { transform: translateY(40px) scale(.9); opacity: 0; } }
.modal-card h2 { margin: 0; text-align: center; font-size: 32px; color: var(--gold); text-shadow: 0 4px 0 rgba(0,0,0,.5); }
.modal-card h3 { margin: 4px 0 0; font-size: 17px; color: var(--gold); }
.modal-card p, .modal-card li { margin: 0; color: #dfe9ec; font-size: 15px; line-height: 1.4; }
.modal-card ul { margin: 0; padding-left: 18px; display: flex; flex-direction: column; gap: 6px; }
.modal-card .sub { text-align: center; color: var(--muted); }
.modal-card .ex { display: flex; gap: 3px; flex-wrap: wrap; margin: 2px 0 4px; }
.modal-card .ex .tile { --w: 26px; }
.results { display: flex; flex-direction: column; gap: 6px; }
.res-row {
  display: grid; grid-template-columns: 1fr auto auto; gap: 4px 10px; align-items: center; background: var(--panel-dark);
  border-radius: 10px; padding: 8px 10px; border-left: 6px solid var(--pc);
}
.res-row.win { box-shadow: 0 0 0 2px var(--gold); }
.res-row .nm { font-weight: 700; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.res-row .d { font-weight: 700; font-size: 18px; }
.res-row .d.pos { color: #8ff0b5; }
.res-row .d.neg { color: #ffb4ae; }
.res-row .tot { color: var(--muted); font-size: 14px; min-width: 52px; text-align: right; }
.res-row .left { grid-column: 1 / -1; display: flex; flex-wrap: wrap; gap: 2px; }
.btn-col { display: flex; flex-direction: column; gap: 10px; }

#conn {
  position: fixed; left: 50%; bottom: calc(var(--sab) + 90px); transform: translateX(-50%); z-index: 160; background: var(--red);
  padding: 8px 16px; border-radius: 10px; font-weight: 700; box-shadow: 0 4px 0 var(--red-s); letter-spacing: 1px;
}
#conn[hidden] { display: none; }
#conn span::after { content: '...'; animation: dots 1s steps(4) infinite; display: inline-block; width: 1.4em; overflow: hidden; vertical-align: bottom; }
@keyframes dots { from { width: 0; } }

/* landscape phones: tighter chrome */
@media (orientation: landscape) and (max-height: 520px) {
  #rack { min-height: calc(var(--th) + 26px); max-height: calc(var(--th) * 2 + 37px); }
  #actions { margin-top: 8px; }
  #actions .btn { padding-top: 8px; padding-bottom: 8px; }
  .pname { font-size: 12px; }
  #status { min-height: 20px; font-size: 13px; }
}

@media (prefers-reduced-motion: reduce) {
  #rack .tile, .logo span.l, .deco-tiles .tile { animation: none; }
}
