:root {
  --ink: #f4f1ff; --dim: #a9a3d6; --panel: rgba(28, 23, 66, .78); --line: rgba(255, 255, 255, .12);
  --accent: #7c5cff; --mint: #3ee6b0; --gold: #ffd75e;
  font-family: ui-rounded, "SF Pro Rounded", "Nunito", system-ui, -apple-system, "Segoe UI", sans-serif;
}
* { box-sizing: border-box; margin: 0; }
[hidden] { display: none !important; }
html, body { height: 100%; overflow: hidden; background: #120f2e; color: var(--ink);
  -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; -webkit-tap-highlight-color: transparent;
  touch-action: none; overscroll-behavior: none; }
body { position: fixed; inset: 0; height: 100dvh; }
button { font: inherit; color: inherit; border: 0; cursor: pointer; touch-action: manipulation; }

/* ---------- background ---------- */
.bg { position: absolute; inset: 0; overflow: hidden; background: radial-gradient(120% 90% at 50% 0%, #2a1f6b 0%, #120f2e 60%, #0b0920 100%); }
.bg i { position: absolute; width: 70vmax; height: 70vmax; border-radius: 50%; opacity: .5; will-change: transform;
  animation: drift 26s ease-in-out infinite alternate; }
.bg i:nth-child(1) { left: -25vmax; top: -30vmax; background: radial-gradient(circle, rgba(124, 92, 255, .55), transparent 62%); }
.bg i:nth-child(2) { right: -30vmax; top: 10vmax; background: radial-gradient(circle, rgba(62, 230, 176, .28), transparent 62%); animation-duration: 34s; animation-direction: alternate-reverse; }
.bg i:nth-child(3) { left: 0; bottom: -45vmax; background: radial-gradient(circle, rgba(255, 92, 157, .3), transparent 62%); animation-duration: 40s; }
@keyframes drift { to { transform: translate(12vmax, 8vmax) scale(1.15); } }

/* ---------- playing layout ---------- */
#game { position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; gap: 12px;
  padding: calc(12px + env(safe-area-inset-top)) calc(14px + env(safe-area-inset-right)) calc(12px + env(safe-area-inset-bottom)) calc(14px + env(safe-area-inset-left)); }
#hud { width: 100%; max-width: 470px; min-height: 76px; display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; gap: 8px; }
.stat { min-width: 0; display: flex; flex-direction: column; justify-content: center; gap: 2px; }
.stat.gear { align-items: flex-end; }
#tally { display: none; grid-column: 1 / -1; flex-direction: column; align-items: center; justify-content: center; }
#tally b { font-size: clamp(34px, 10vw, 46px); font-weight: 900; line-height: 1; font-variant-numeric: tabular-nums;
  background: linear-gradient(180deg, #fff, #ffd75e); -webkit-background-clip: text; background-clip: text; color: transparent; }
#tally b.tick { animation: tick .25s; }
#hud.tallying > * { display: none; }
#hud.tallying > #tally { display: flex; }
.stat span, #tally span, .final span, .over-stats span { font-size: 11px; letter-spacing: .12em; text-transform: uppercase; color: var(--dim); font-weight: 700; }
.stat b { font-size: clamp(20px, 5.6vw, 28px); font-weight: 800; font-variant-numeric: tabular-nums; line-height: 1.15; }
.stat b.tick { animation: tick .25s; }
@keyframes tick { 40% { transform: scale(1.14); color: var(--gold); } }
.stat.next { align-items: center; gap: 4px; }
#next { --nw: 54px; --nh: 82px; display: flex; gap: 6px; height: var(--nh); align-items: center; }
#next .chip { width: var(--nw); height: var(--nh); border-radius: calc(var(--nw) * .22); font-size: calc(var(--nw) * .46); }
#next.multi .chip { width: calc(var(--nw) * .78); height: calc(var(--nh) * .78); font-size: calc(var(--nw) * .34); }
#next .chip[data-len="3"], #next .chip[data-len="4"] { font-size: calc(var(--nw) * .28); }
.chip { width: 32px; height: 40px; border-radius: 9px; display: grid; place-items: center; font-weight: 800; font-size: 17px;
  background: var(--bg, #333); color: var(--fg, #fff); box-shadow: inset 0 -4px 0 rgba(0, 0, 0, .18), 0 0 14px color-mix(in srgb, var(--glow, #000) 60%, transparent); }
.chip.swap { animation: pop .28s; }
.icon { width: 52px; height: 52px; border-radius: 16px; background: var(--panel); border: 1px solid var(--line); display: grid; place-items: center; }
.icon svg { fill: var(--ink); }
.icon:active, .btn:active { transform: scale(.95); }

#board-wrap { flex: 1; width: 100%; min-height: 0; container-type: size; display: grid; place-items: center; }
#board { --bs: min(100cqw, 100cqh / 1.45, var(--bmax, 470px)); --gap: calc(var(--bs) * .03); --round: calc(var(--bs) * .05); position: relative; width: var(--bs); height: calc(var(--bs) * 1.45);
  border-radius: calc(var(--bs) * .075); background: rgba(13, 10, 40, .72); border: 1px solid var(--line);
  box-shadow: 0 20px 60px rgba(0, 0, 0, .45), inset 0 0 0 1px rgba(255, 255, 255, .03); transition: box-shadow .4s; }
#board.danger { box-shadow: 0 20px 60px rgba(0, 0, 0, .45), 0 0 0 2px rgba(255, 92, 120, .55), 0 0 34px rgba(255, 70, 110, .45); animation: danger 1.1s ease-in-out infinite; }
@keyframes danger { 50% { box-shadow: 0 20px 60px rgba(0, 0, 0, .45), 0 0 0 2px rgba(255, 92, 120, .25), 0 0 14px rgba(255, 70, 110, .2); } }
#slots { position: absolute; inset: 0; padding: var(--gap); display: grid; grid-template: repeat(4, 1fr) / repeat(4, 1fr); gap: var(--gap); }
#slots i { border-radius: var(--round); background: rgba(255, 255, 255, .045); }
#tiles { position: absolute; inset: 0; }
.tile { position: absolute; left: var(--gap); top: var(--gap); width: calc((100% - 5 * var(--gap)) / 4); height: calc((100% - 5 * var(--gap)) / 4);
  transform: translate(calc(var(--x) * (100% + var(--gap)) + var(--dx, 0px)), calc(var(--y) * (100% + var(--gap)) + var(--dy, 0px)));
  transition: transform 120ms cubic-bezier(.25, .9, .35, 1.2); will-change: transform; }
.dragging .tile { transition: none; }
.tile.moving { z-index: 3; }
.tile.gone { z-index: 0; }
.face { position: absolute; inset: 0; border-radius: var(--round); display: grid; place-items: center; font-weight: 800;
  font-size: calc(var(--bs) * .11); letter-spacing: -.02em; background: var(--bg); color: var(--fg);
  box-shadow: inset 0 calc(var(--bs) * -.012) 0 rgba(0, 0, 0, .2), inset 0 2px 0 rgba(255, 255, 255, var(--shine, .3)), 0 0 calc(var(--bs) * .035) color-mix(in srgb, var(--glow) calc(var(--shine, .3) * 100%), transparent);
  text-shadow: 0 0 calc(var(--bs) * .03) color-mix(in srgb, var(--glow) 45%, transparent); }
.tile[data-len="3"] .face { font-size: calc(var(--bs) * .085); }
.tile[data-len="4"] .face { font-size: calc(var(--bs) * .068); }
.tile.hot .face::after { content: ""; position: absolute; inset: -3px; border-radius: inherit; border: 2px solid rgba(255, 255, 255, .7); animation: ring 1.6s ease-out infinite; }
@keyframes ring { from { opacity: .9; transform: scale(.96); } to { opacity: 0; transform: scale(1.16); } }
.tile.enter .face { animation: enter 200ms cubic-bezier(.2, .9, .3, 1.15) backwards; animation-delay: var(--delay, 0ms); }
@keyframes enter { from { opacity: 0; transform: translate(calc(var(--ex, 0) * 70%), calc(var(--ey, 0) * 70%)) scale(var(--es, 1)); } }
.tile.pop { z-index: 2; }
.tile.pop .face { animation: pop 260ms cubic-bezier(.2, .9, .3, 1.3); }
@keyframes pop { 35% { transform: scale(1.24); filter: brightness(1.5); } }
.tile.crown { z-index: 4; }
.tile.crown .face { animation: crown 900ms ease-out; }
.tile.crown::after { content: ""; position: absolute; inset: 0; border-radius: var(--round); border: 3px solid var(--glow); animation: crownring 900ms ease-out forwards; pointer-events: none; }
@keyframes crown { 0% { transform: scale(1); } 20% { transform: scale(1.3); filter: brightness(1.8); } 45% { transform: scale(.96); } 70% { transform: scale(1.08); filter: brightness(1.3); } }
@keyframes crownring { from { opacity: 1; transform: scale(1); } to { opacity: 0; transform: scale(2.1); } }
.tile.dud { opacity: .35; transition: transform 120ms, opacity .4s; }
.plus { position: absolute; left: 50%; top: 0; transform: translate(-50%, -45%); z-index: 6; white-space: nowrap; padding: 2px 8px; border-radius: 99px;
  font-weight: 800; font-size: calc(var(--bs) * .042); color: #2a1500; background: linear-gradient(90deg, #ffe07a, #ffb938); box-shadow: 0 2px 10px rgba(0, 0, 0, .5);
  animation: plus .3s cubic-bezier(.2, .9, .3, 1.5) backwards; }
@keyframes plus { from { transform: translate(-50%, 40%) scale(.3); opacity: 0; } }
.tile.wave { z-index: 5; }
.tile.wave .face { animation: wave 420ms ease-out; }
@keyframes wave { 40% { transform: scale(1.16) rotate(-4deg); filter: brightness(1.4); } }

#board.bump-left { animation: bl .18s; } #board.bump-right { animation: br .18s; }
#board.bump-up { animation: bu .18s; } #board.bump-down { animation: bd .18s; }
@keyframes bl { 40% { transform: translateX(-9px); } } @keyframes br { 40% { transform: translateX(9px); } }
@keyframes bu { 40% { transform: translateY(-9px); } } @keyframes bd { 40% { transform: translateY(9px); } }
#board.shake { animation: shake .3s; }
@keyframes shake { 20% { transform: translate(-5px, 3px) rotate(-.6deg); } 45% { transform: translate(5px, -3px) rotate(.6deg); } 70% { transform: translate(-3px, -2px); } }

#hint { font-size: 13px; color: var(--dim); text-align: center; }
#hint.go { font-size: 16px; font-weight: 700; color: var(--ink); animation: breathe 1.4s ease-in-out infinite; }
/* computers with room get a board up to half as big again */
@media (pointer: fine) and (min-width: 800px) { #board { --bmax: 705px; } #hud { max-width: 705px; } }

/* ---------- effects ---------- */
#fx { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; z-index: 30; }
/* ---------- overlays ---------- */
.overlay { position: absolute; inset: 0; z-index: 20; display: grid; place-items: center; overflow: hidden;
  padding: calc(12px + env(safe-area-inset-top)) calc(16px + env(safe-area-inset-right)) calc(12px + env(safe-area-inset-bottom)) calc(16px + env(safe-area-inset-left));
  background: rgba(10, 8, 30, .62); backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px); animation: fade .2s; }
#title { background: rgba(10, 8, 30, .35); }
@keyframes fade { from { opacity: 0; } }
.panel { width: min(100%, 400px); max-height: 100%; display: flex; flex-direction: column; align-items: stretch; gap: 12px; text-align: center;
  padding: 22px 20px; border-radius: 26px; background: var(--panel); border: 1px solid var(--line); box-shadow: 0 30px 80px rgba(0, 0, 0, .5); animation: rise .28s cubic-bezier(.2, .9, .3, 1.2); }
.panel.clear { background: none; border: 0; box-shadow: none; width: min(100%, 440px); }
@keyframes rise { from { transform: translateY(24px) scale(.96); opacity: 0; } }
h1 { font-size: clamp(40px, 12vmin, 68px); font-weight: 900; letter-spacing: -.03em; line-height: 1; }
h1 em { font-style: normal; background: linear-gradient(90deg, #5ff5c8, #9b6bff 55%, #ff5c9d); -webkit-background-clip: text; background-clip: text; color: transparent; }
h2 { font-size: 26px; font-weight: 800; }
.sub { color: var(--dim); font-size: 16px; margin-bottom: 6px; }
.small { color: var(--dim); font-size: 14px; }
.small b { color: var(--ink); }
.btn { min-height: 48px; padding: 10px 14px; border-radius: 15px; font-weight: 700; font-size: 15px; background: rgba(255, 255, 255, .09);
  border: 1px solid var(--line); transition: transform .08s, background .15s; }
.btn:hover { background: rgba(255, 255, 255, .15); }
.btn.primary { background: linear-gradient(180deg, #9478ff, #6a45f5); border: 0; font-size: 18px; font-weight: 800; box-shadow: 0 6px 0 #3f239f, 0 12px 30px rgba(110, 70, 255, .5); margin-bottom: 6px; }
.btn.primary:active { transform: translateY(4px); box-shadow: 0 2px 0 #3f239f; }
.btn.big { min-height: 66px; font-size: 26px; border-radius: 22px; animation: breathe 1.8s ease-in-out infinite; }
@keyframes breathe { 50% { filter: brightness(1.18); } }
.btn.gold { background: linear-gradient(180deg, #ffe07a, #ffb938); color: #3a2400; border: 0; font-weight: 800; }
.row { display: flex; gap: 8px; }
.row .btn { flex: 1; padding: 10px 6px; }

.logo-tiles, .eg { display: flex; align-items: center; justify-content: center; gap: 8px; font-weight: 800; }
.logo-tiles i, .eg i { font-style: normal; color: var(--dim); font-size: 22px; }
.lt { width: 46px; height: 60px; border-radius: 13px; display: grid; place-items: center; font-size: 26px; box-shadow: inset 0 -5px 0 rgba(0, 0, 0, .2), inset 0 2px 0 rgba(255, 255, 255, .35); }
.logo-tiles .lt { animation: bob 2.4s ease-in-out infinite; }
.logo-tiles .l2 { animation-delay: .2s; } .logo-tiles .l3 { animation-delay: .4s; }
@keyframes bob { 50% { transform: translateY(-7px); } }
.l1 { background: linear-gradient(145deg, #6ff7cf, #19c9a0); color: #053b30; box-shadow: inset 0 -5px 0 rgba(0, 0, 0, .2), 0 0 18px rgba(46, 230, 182, .6); }
.l2 { background: linear-gradient(145deg, #b596ff, #7a4dff); color: #fff; box-shadow: inset 0 -5px 0 rgba(0, 0, 0, .2), 0 0 18px rgba(143, 107, 255, .6); }
.l3, .l6, .lx { background: linear-gradient(160deg, #5b6083, #3d4160); box-shadow: inset 0 -5px 0 rgba(0, 0, 0, .2), inset 0 2px 0 rgba(255, 255, 255, .18); }
.l3 { color: hsl(48 100% 68%); } .l6 { color: hsl(34 100% 68%); }
.lx { color: hsl(352 100% 68%); font-size: 22px; }
.ln { background: rgba(255, 255, 255, .12); color: var(--ink); border: 2px dashed rgba(255, 255, 255, .4); }

.rules { list-style: none; padding: 0; display: flex; flex-direction: column; gap: 10px; text-align: left; font-size: 14.5px; line-height: 1.35; color: #ddd8ff; overflow: auto; }
.rules li { display: flex; align-items: center; gap: 12px; }
.rules .eg { flex: 0 0 132px; gap: 4px; }
.rules .lt { width: 30px; height: 38px; border-radius: 9px; font-size: 16px; }
.rules .lx { font-size: 14px; }
.rules .eg i { font-size: 15px; }

.slider { display: grid; grid-template-columns: 1fr 44px; align-items: center; gap: 0 8px; text-align: left; font-weight: 700; font-size: 15px; }
.slider span { grid-column: 1 / -1; }
.slider output { text-align: right; color: var(--dim); font-variant-numeric: tabular-nums; font-size: 14px; }
input[type=range] { -webkit-appearance: none; appearance: none; width: 100%; height: 44px; background: none; touch-action: pan-x; cursor: pointer; }
input[type=range]::-webkit-slider-runnable-track { height: 10px; border-radius: 9px; background: linear-gradient(90deg, var(--accent) var(--fill, 50%), rgba(255, 255, 255, .14) var(--fill, 50%)); }
input[type=range]::-moz-range-track { height: 10px; border-radius: 9px; background: linear-gradient(90deg, var(--accent) var(--fill, 50%), rgba(255, 255, 255, .14) var(--fill, 50%)); }
input[type=range]::-webkit-slider-thumb { -webkit-appearance: none; width: 28px; height: 28px; margin-top: -9px; border-radius: 50%; background: #fff; box-shadow: 0 2px 10px rgba(0, 0, 0, .5); }
input[type=range]::-moz-range-thumb { width: 28px; height: 28px; border: 0; border-radius: 50%; background: #fff; }
.toggle { display: flex; align-items: center; justify-content: space-between; min-height: 44px; font-weight: 700; font-size: 15px; text-align: left; cursor: pointer; position: relative; }
.toggle input { position: absolute; right: 0; width: 56px; height: 44px; opacity: 0; margin: 0; cursor: pointer; }
.toggle i { width: 56px; height: 32px; border-radius: 99px; background: rgba(255, 255, 255, .16); position: relative; transition: background .15s; pointer-events: none; }
.toggle i::after { content: ""; position: absolute; left: 3px; top: 3px; width: 26px; height: 26px; border-radius: 50%; background: #fff; transition: transform .15s; }
.toggle input:checked + i { background: var(--mint); }
.toggle input:checked + i::after { transform: translateX(24px); }
#pause.from-title .in-game { display: none; }

.final b { display: block; font-size: 54px; font-weight: 900; line-height: 1.05; font-variant-numeric: tabular-nums;
  background: linear-gradient(180deg, #fff, #ffd75e); -webkit-background-clip: text; background-clip: text; color: transparent; }
.over-stats { display: flex; justify-content: space-around; align-items: flex-start; }
.over-stats > div { display: flex; flex-direction: column; align-items: center; gap: 4px; }
.over-stats b { font-size: 22px; font-weight: 800; line-height: 38px; }
.rank { font-weight: 700; font-size: 16px; padding: 9px 12px; border-radius: 13px; background: rgba(255, 255, 255, .08); }
.rank.podium { color: #3a2400; background: linear-gradient(90deg, #ffe07a, #ffb938); animation: pop .5s; }
.rank.quiet { font-weight: 500; font-size: 14px; color: var(--dim); }

/* ---------- sideways phones: ask to turn upright ---------- */
#rotate { display: none; position: absolute; inset: 0; z-index: 50; flex-direction: column; align-items: center; justify-content: center; gap: 10px;
  text-align: center; background: #120f2e; color: var(--dim); }
#rotate h2 { color: var(--ink); }
#rotate .phone { width: 44px; height: 76px; border-radius: 10px; border: 4px solid var(--ink); margin-bottom: 6px; animation: turn 2.2s ease-in-out infinite; }
@keyframes turn { 0%, 25% { transform: rotate(90deg); } 60%, 100% { transform: rotate(0); } }
@media (orientation: landscape) and (pointer: coarse) and (max-height: 540px) { #rotate { display: flex; } }

@media (max-height: 460px) { #hint:not(.go) { display: none; } #game { gap: 8px; } #hud { min-height: 60px; } }
@media (orientation: landscape) and (max-height: 520px) {
  .panel { width: min(100%, 620px); padding: 14px 18px; gap: 8px; }
  .panel.clear { display: grid; grid-template-columns: 1fr 1fr; align-items: center; column-gap: 24px; width: min(100%, 680px); }
  .panel.clear .logo-tiles, .panel.clear h1, .panel.clear .sub { grid-column: 1; }
  .panel.clear .btn.big, .panel.clear .row, .panel.clear .small { grid-column: 2; }
  .panel.clear .logo-tiles { grid-row: 1; } .panel.clear h1 { grid-row: 2; } .panel.clear .sub { grid-row: 3; }
  .panel.clear .btn.big { grid-row: 1 / span 1; } .panel.clear .row { grid-row: 2; } .panel.clear .small { grid-row: 3; }
  .lt { width: 38px; height: 48px; font-size: 21px; }
  h2 { font-size: 21px; }
  .final b { font-size: 38px; }
  #over .panel { display: grid; grid-template-columns: 1fr 1fr; column-gap: 18px; align-items: center; }
  #over h2, #over .rank { grid-column: 1 / -1; }
  #pause .panel { display: grid; grid-template-columns: 1fr 1fr; column-gap: 18px; }
  #pause h2, #pause .row, #pause #btn-resume { grid-column: 1 / -1; }
  .rules { font-size: 13px; gap: 6px; }
  .btn { min-height: 44px; }
  .btn.primary { margin-bottom: 4px; }
}
@media (max-width: 360px) { .row { flex-wrap: wrap; } .rules .eg { flex-basis: 112px; } }
@media (prefers-reduced-motion: reduce) { .bg i, .btn.big, .logo-tiles .lt { animation: none; } }
