:root {
  --ink: #16161d; --paper: #fffdf5; --sun: #ffcf33; --blue: #2b4bff; --red: #ff4d6d; --green: #22c98a;
  --line: 3px solid var(--ink);
  font-family: "Avenir Next", "Arial Black", "Segoe UI Black", "Helvetica Neue", system-ui, sans-serif; font-weight: 900;
}
* { box-sizing: border-box; margin: 0; }
[hidden] { display: none !important; }
html, body { height: 100%; overflow: hidden; background: var(--sun); 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; cursor: pointer; touch-action: manipulation; }

/* ---------- background: drifting halftone dots on sunshine yellow ---------- */
.bg { position: absolute; inset: -80px; background:
  radial-gradient(circle at 50% 50%, rgba(255, 120, 40, .30) 0 5px, transparent 6px) 0 0 / 34px 34px,
  radial-gradient(120% 80% at 80% 0%, #ffe680 0%, transparent 60%),
  radial-gradient(90% 70% at 0% 100%, #ffab2e 0%, transparent 60%), var(--sun);
  animation: drift 14s linear infinite; }
@keyframes drift { to { transform: translate(34px, 34px); } }

/* ---------- 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: var(--bmax, 500px); display: flex; align-items: stretch; gap: 8px; }
.word { display: flex; flex-direction: column; justify-content: center; font-size: 17px; line-height: .95; letter-spacing: -.02em; font-style: italic; transform: rotate(-4deg); margin-right: 2px; }
.word b { font-size: 30px; color: var(--blue); -webkit-text-stroke: 1.5px var(--ink); text-shadow: 2px 2px 0 var(--ink); }
.stat { flex: 1; min-width: 0; position: relative; background: var(--paper); border: var(--line); border-radius: 14px; padding: 5px 10px 6px;
  display: flex; flex-direction: column; justify-content: center; box-shadow: 0 4px 0 var(--ink); }
.stat span, .final span, .over-stats span { font-size: 11px; letter-spacing: .1em; text-transform: uppercase; opacity: .6; }
.stat b { font-size: clamp(20px, 6vw, 28px); line-height: 1.1; font-variant-numeric: tabular-nums; }
.stat b.tick { animation: tick .22s; }
@keyframes tick { 40% { transform: scale(1.18) rotate(-3deg); color: var(--red); } }
#gain { position: absolute; right: 8px; top: 2px; font-style: normal; font-size: 15px; color: var(--green); -webkit-text-stroke: .5px var(--ink); opacity: 0; pointer-events: none; }
#gain.go { animation: gain .7s ease-out; }
@keyframes gain { 0% { opacity: 1; transform: translateY(10px) scale(.7); } 30% { opacity: 1; transform: translateY(0) scale(1.15); } 100% { opacity: 0; transform: translateY(-16px); } }
.icon { flex: 0 0 54px; min-height: 54px; border: var(--line); border-radius: 14px; background: var(--paper); box-shadow: 0 4px 0 var(--ink); display: grid; place-items: center; }
.icon svg { fill: var(--ink); }
.icon:active, .btn:active { transform: translateY(4px); box-shadow: 0 0 0 var(--ink) !important; }

#board-wrap { flex: 1; width: 100%; min-height: 0; container-type: size; display: grid; place-items: center; }
#board { --bs: min(100cqw, 100cqh - 10px, var(--bmax, 500px)); --gap: calc(var(--bs) * .03); --round: calc(var(--bs) * .035); position: relative; width: var(--bs); height: var(--bs);
  border-radius: calc(var(--bs) * .06); background: var(--blue); border: 4px solid var(--ink); box-shadow: 0 8px 0 var(--ink); }
#board.danger { animation: danger .9s ease-in-out infinite; }
@keyframes danger { 50% { background: #5a3df0; } }
#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(10, 16, 80, .32); box-shadow: inset 0 3px 0 rgba(0, 0, 0, .22); }
#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))), calc(var(--y) * (100% + var(--gap))));
  transition: transform 110ms cubic-bezier(.3, .8, .4, 1); will-change: transform; z-index: 1; }
.tile.gone { z-index: 0; }
.face { position: absolute; inset: 0; border-radius: var(--round); display: grid; place-items: center; overflow: hidden;
  font-size: calc(var(--bs) * .11); letter-spacing: -.03em; background: var(--bg); color: var(--fg);
  border: max(2px, calc(var(--bs) * .007)) solid var(--ink); box-shadow: 0 calc(var(--bs) * .011) 0 var(--ink); }
.tile[data-len="3"] .face { font-size: calc(var(--bs) * .086); }
.tile[data-len="4"] .face { font-size: calc(var(--bs) * .068); }
.tile[data-len="5"] .face, .tile[data-len="6"] .face { font-size: calc(var(--bs) * .052); }
.tile.goal .face::after { content: ""; position: absolute; inset: -20% -60%; background: linear-gradient(110deg, transparent 42%, rgba(255, 255, 255, .85) 50%, transparent 58%); animation: shine 2.2s ease-in-out infinite; }
@keyframes shine { from { transform: translateX(-60%); } to { transform: translateX(60%); } }
.tile.enter .face { animation: enter 190ms cubic-bezier(.2, .9, .3, 1.5) backwards; animation-delay: var(--delay, 0ms); }
@keyframes enter { from { transform: scale(0) rotate(-12deg); } }
.tile.pop { z-index: 3; }
.tile.pop .face { animation: pop 240ms cubic-bezier(.2, .9, .3, 1.4); }
@keyframes pop { 35% { transform: scale(1.22) rotate(3deg); } 70% { transform: scale(.96) rotate(-1deg); } }
.tile.crown { z-index: 4; }
.tile.crown::before { content: ""; position: absolute; inset: -45%; border-radius: 50%; pointer-events: none;
  background: repeating-conic-gradient(#fff 0 9deg, transparent 9deg 18deg); -webkit-mask: radial-gradient(circle, #000 30%, transparent 70%); mask: radial-gradient(circle, #000 30%, transparent 70%);
  animation: burst 800ms ease-out forwards; }
@keyframes burst { from { opacity: 1; transform: scale(.4) rotate(0); } to { opacity: 0; transform: scale(1.5) rotate(50deg); } }

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

#stamp { position: absolute; left: 50%; top: 50%; z-index: 9; white-space: nowrap; pointer-events: none; padding: .15em .5em; font-style: italic;
  font-size: calc(var(--bs) * .13); color: #fff; background: var(--red); border: 4px solid var(--ink); border-radius: 14px; box-shadow: 6px 6px 0 var(--ink);
  transform: translate(-50%, -50%) rotate(-8deg); animation: stamp .35s cubic-bezier(.2, .9, .3, 1.5); }
#stamp.win { background: #ffd60a; color: var(--ink); animation: stamp .35s cubic-bezier(.2, .9, .3, 1.5), fadeout .5s 1.6s forwards; }
/* the end-of-game stamp sits on the board's top edge so the tiles stay readable */
#stamp.end { top: 0; font-size: calc(var(--bs) * .075); border-width: 3px; box-shadow: 4px 4px 0 var(--ink); transform: translate(-50%, -55%) rotate(-4deg); animation: stampend .35s cubic-bezier(.2, .9, .3, 1.5); }
@keyframes stampend { from { transform: translate(-50%, -55%) rotate(-4deg) scale(2.6); opacity: 0; } }
@keyframes stamp { from { transform: translate(-50%, -50%) rotate(-8deg) scale(2.6); opacity: 0; } }
@keyframes fadeout { to { opacity: 0; transform: translate(-50%, -90%) rotate(-8deg); } }
#hint { font-size: 13px; text-align: center; font-weight: 700; opacity: .75; }
#hint.go { font-size: 17px; font-weight: 900; opacity: 1; animation: nudge 1s ease-in-out infinite; }
@keyframes nudge { 50% { transform: scale(1.06); } }
#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(255, 207, 51, .82); animation: fade .15s; }
#title { background: var(--sun) radial-gradient(circle at 50% 50%, rgba(255, 120, 40, .30) 0 5px, transparent 6px) 0 0 / 34px 34px; }
@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: 20px 18px 22px; border-radius: 20px; background: var(--paper); border: 4px solid var(--ink); box-shadow: 8px 8px 0 var(--ink); animation: rise .25s cubic-bezier(.2, .9, .3, 1.3); }
.panel.clear { background: none; border: 0; box-shadow: none; width: min(100%, 440px); gap: 14px; }
@keyframes rise { from { transform: translateY(26px) rotate(-2deg); opacity: 0; } }
h1 { font-size: clamp(44px, 14vmin, 78px); line-height: .88; letter-spacing: -.04em; font-style: italic; transform: rotate(-4deg); margin: 6px 0; text-shadow: 4px 4px 0 #fff; }
h1 em { display: block; color: var(--blue); -webkit-text-stroke: 3px var(--ink); text-shadow: 5px 5px 0 var(--ink); font-size: 1.35em; }
h2 { font-size: 28px; font-style: italic; letter-spacing: -.02em; }
.sub { font-size: 16px; font-weight: 700; }
.small { font-size: 14px; font-weight: 700; }
.btn { min-height: 50px; padding: 8px 12px; border-radius: 14px; font-size: 15px; background: var(--paper); border: var(--line); box-shadow: 0 5px 0 var(--ink); transition: transform .06s; }
.btn.primary { background: var(--blue); color: #fff; font-size: 19px; margin-bottom: 4px; }
.btn.big { min-height: 72px; font-size: 32px; font-style: italic; border-width: 4px; border-radius: 18px; box-shadow: 0 7px 0 var(--ink); animation: wobble 1.6s ease-in-out infinite; }
@keyframes wobble { 0%, 100% { transform: rotate(-1.5deg); } 50% { transform: rotate(1.5deg) scale(1.03); } }
.btn.gold { background: #ffd60a; }
.row { display: flex; gap: 8px; }
.row .btn { flex: 1; padding: 8px 4px; }

.logo, .eg { display: flex; align-items: center; justify-content: center; gap: 8px; }
.lt { width: 56px; height: 56px; border-radius: 12px; display: grid; place-items: center; font-size: 26px; border: var(--line); box-shadow: 0 4px 0 var(--ink); }
.logo .lt { animation: hop 1.8s ease-in-out infinite; }
.logo .b { animation-delay: .15s; } .logo .c { animation-delay: .3s; } .logo .d { animation-delay: .45s; }
@keyframes hop { 0%, 60%, 100% { transform: translateY(0) rotate(0); } 30% { transform: translateY(-12px) rotate(-6deg); } }
.lt.a { background: #fff; } .lt.b { background: #ffe58a; } .lt.c { background: #ffb43d; } .lt.d { background: #ff7b3d; }
.lt.g { background: #ffd60a; } .lt.n { background: var(--blue); color: #fff; }
.chip { width: 44px; height: 44px; border-radius: 10px; display: grid; place-items: center; font-size: 17px; background: var(--bg, #fff); color: var(--fg, #16161d); border: var(--line); box-shadow: 0 3px 0 var(--ink); }
.chip[data-len="4"], .chip[data-len="5"] { font-size: 12px; }

.rules { list-style: none; padding: 0; display: flex; flex-direction: column; gap: 10px; text-align: left; font-size: 14.5px; line-height: 1.3; font-weight: 700; overflow: auto; }
.rules li { display: flex; align-items: center; gap: 12px; }
.rules .eg { flex: 0 0 128px; gap: 4px; }
.rules .lt { width: 32px; height: 32px; border-radius: 8px; font-size: 15px; border-width: 2px; box-shadow: 0 3px 0 var(--ink); }
.rules .lt.g { width: 52px; font-size: 14px; }
.rules .eg i { font-style: normal; font-size: 16px; }

.slider { display: grid; grid-template-columns: 1fr 48px; align-items: center; gap: 0 8px; text-align: left; font-size: 15px; }
.slider span { grid-column: 1 / -1; }
.slider output { text-align: right; 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: 14px; border-radius: 9px; border: var(--line); background: linear-gradient(90deg, var(--blue) var(--fill, 50%), #fff var(--fill, 50%)); }
input[type=range]::-moz-range-track { height: 10px; border-radius: 9px; border: var(--line); background: linear-gradient(90deg, var(--blue) var(--fill, 50%), #fff var(--fill, 50%)); }
input[type=range]::-webkit-slider-thumb { -webkit-appearance: none; width: 30px; height: 30px; margin-top: -11px; border-radius: 50%; background: #ffd60a; border: var(--line); box-shadow: 0 3px 0 var(--ink); }
input[type=range]::-moz-range-thumb { width: 26px; height: 26px; border-radius: 50%; background: #ffd60a; border: var(--line); }
.toggle { display: flex; align-items: center; justify-content: space-between; min-height: 44px; font-size: 15px; text-align: left; cursor: pointer; position: relative; }
.toggle input { position: absolute; right: 0; width: 60px; height: 44px; opacity: 0; margin: 0; cursor: pointer; }
.toggle i { width: 60px; height: 34px; border-radius: 99px; background: #fff; border: var(--line); position: relative; transition: background .15s; pointer-events: none; }
.toggle i::after { content: ""; position: absolute; left: 3px; top: 2px; width: 24px; height: 24px; border-radius: 50%; background: var(--ink); transition: transform .15s; }
.toggle input:checked + i { background: var(--green); }
.toggle input:checked + i::after { transform: translateX(26px); }
#pause.from-title .in-game { display: none; }

.final b { display: block; font-size: 58px; line-height: 1; font-style: italic; color: var(--blue); -webkit-text-stroke: 2px var(--ink); text-shadow: 4px 4px 0 var(--ink); font-variant-numeric: tabular-nums; }
.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; line-height: 44px; }
.rank { font-size: 15px; padding: 9px 12px; border-radius: 12px; border: var(--line); background: #fff; }
.rank.podium { background: #ffd60a; animation: pop .5s; }
.rank.quiet { font-weight: 700; font-size: 14px; border: 0; background: none; padding: 0; }

/* ---------- computers get a bigger board; sideways puts the HUD beside it ---------- */
@media (pointer: fine) and (min-width: 800px) { #game { --bmax: 640px; } }
@media (orientation: landscape) and (max-height: 640px), (orientation: landscape) and (pointer: coarse) {
  #game { flex-direction: row; justify-content: center; align-items: center; gap: 18px; }
  #hud { flex: 0 0 clamp(130px, 20vw, 190px); width: auto; flex-direction: column; }
  .stat { flex: 0 0 auto; }
  .word { margin: 0 0 6px 6px; }
  .icon { flex: 0 0 54px; }
  #board-wrap { flex: 0 1 660px; height: 100%; width: auto; }
  #hint { position: absolute; left: 0; right: 0; bottom: calc(6px + env(safe-area-inset-bottom)); }
  #hint:not(.go) { display: none; }
}
@media (orientation: landscape) and (max-height: 520px) {
  .panel { width: min(100%, 620px); padding: 12px 16px 16px; gap: 8px; }
  .panel.clear { display: grid; grid-template-columns: 1fr 1fr; align-items: center; column-gap: 28px; width: min(100%, 700px); }
  .panel.clear .logo, .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 { grid-row: 1; } .panel.clear h1 { grid-row: 2; } .panel.clear .sub { grid-row: 3; }
  .panel.clear .btn.big { grid-row: 1; } .panel.clear .row { grid-row: 2; } .panel.clear .small { grid-row: 3; }
  h1 { font-size: 44px; } .lt { width: 44px; height: 44px; font-size: 20px; }
  h2 { font-size: 22px; } .final b { font-size: 40px; }
  #over .panel, #pause .panel { display: grid; grid-template-columns: 1fr 1fr; column-gap: 18px; align-items: center; }
  #over h2, #over .rank, #pause h2, #pause .row, #pause #btn-resume { grid-column: 1 / -1; }
  .rules { font-size: 13px; gap: 6px; }
  .btn { min-height: 44px; } .btn.big { min-height: 60px; font-size: 26px; }
}
@media (max-width: 370px) { .word { display: none; } .row { flex-wrap: wrap; } .rules .eg { flex-basis: 112px; } }
@media (prefers-reduced-motion: reduce) { .bg, .btn.big, .logo .lt { animation: none; } }
