:root {
  --bg1: #1d0f3d;
  --bg2: #35104f;
  --ink: #ffffff;
  --muted: rgba(255, 255, 255, .66);
  --panel: rgba(255, 255, 255, .08);
  --panel-b: rgba(255, 255, 255, .14);
  --accent: #ffd23f;

  /* colori delle righe di arrivo */
  --c0: #ff5d73;
  --c1: #ffa630;
  --c2: #2fd38a;
  --c3: #33b0ff;
  --c4: #b06bff;

  --arrow: clamp(24px, 6.5vw, 40px);
  --size: min(
    calc(100vw - 32px - 2 * var(--arrow)),
    calc(100vh - 330px - 2 * var(--arrow)),
    520px
  );
}
@supports (height: 100dvh) {
  :root {
    --size: min(
      calc(100vw - 32px - 2 * var(--arrow)),
      calc(100dvh - 330px - 2 * var(--arrow)),
      520px
    );
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; }
body {
  color: var(--ink);
  font-family: "Fredoka", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background:
    radial-gradient(circle at 15% -10%, rgba(255, 93, 115, .35), transparent 45%),
    radial-gradient(circle at 110% 30%, rgba(51, 176, 255, .28), transparent 45%),
    radial-gradient(circle at 30% 120%, rgba(47, 211, 138, .25), transparent 50%),
    linear-gradient(160deg, var(--bg1), var(--bg2));
  background-color: var(--bg1);
  background-attachment: fixed;
  overscroll-behavior: none;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  -webkit-user-select: none;
}
button { font: inherit; color: inherit; cursor: pointer; }

.app {
  max-width: 640px;
  margin: 0 auto;
  min-height: 100vh;
  min-height: 100dvh;
  padding: max(12px, env(safe-area-inset-top)) 16px max(14px, env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
}

/* ---------- header ---------- */
.topbar {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.logo {
  margin: 0;
  font-size: clamp(26px, 7vw, 36px);
  font-weight: 700;
  letter-spacing: 4px;
  background: linear-gradient(90deg, var(--c0), var(--c1), var(--c2), var(--c3), var(--c4));
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}
.top-actions { display: flex; align-items: center; gap: 8px; }
.install-btn {
  height: 42px;
  padding: 0 14px 0 10px;
  border: 0;
  border-radius: 14px;
  background: var(--accent);
  color: #2a1540;
  font-weight: 600;
  font-size: 15px;
  display: flex;
  align-items: center;
  gap: 6px;
  box-shadow: 0 4px 14px rgba(255, 210, 63, .3);
  animation: rise .4s ease both;
}
.install-btn[hidden] { display: none; }
.install-btn svg { width: 20px; height: 20px; }
.install-btn:active { transform: scale(.95); }
.ios-steps {
  text-align: left;
  margin: 0 0 18px;
  padding-left: 22px;
  line-height: 1.6;
  font-size: 16px;
}
.ios-steps svg { width: 20px; height: 20px; vertical-align: -4px; color: #33b0ff; }
.icon-btn {
  width: 42px; height: 42px;
  border-radius: 14px;
  border: 1px solid var(--panel-b);
  background: var(--panel);
  display: grid; place-items: center;
}
.icon-btn svg { width: 22px; height: 22px; fill: currentColor; }
.icon-btn .ico-off { display: none; }
.icon-btn[aria-pressed="false"] .ico-on { display: none; }
.icon-btn[aria-pressed="false"] .ico-off { display: block; }
.icon-btn[aria-pressed="false"] { color: var(--muted); }

/* ---------- size chips ---------- */
.sizes {
  display: flex;
  gap: 6px;
  padding: 5px;
  border-radius: 999px;
  background: var(--panel);
  border: 1px solid var(--panel-b);
}
.chip {
  border: 0;
  background: transparent;
  padding: 7px 18px;
  border-radius: 999px;
  font-weight: 600;
  font-size: 16px;
  color: var(--muted);
  transition: background .2s, color .2s, transform .15s;
}
.chip:active { transform: scale(.95); }
.chip.active {
  background: var(--accent);
  color: #2a1540;
  box-shadow: 0 4px 14px rgba(255, 210, 63, .35);
}

/* ---------- stats ---------- */
.stats {
  width: 100%;
  max-width: 460px;
  display: grid;
  grid-template-columns: 1fr 1fr 1.3fr;
  gap: 8px;
}
.stat {
  background: var(--panel);
  border: 1px solid var(--panel-b);
  border-radius: 16px;
  padding: 6px 10px;
  display: flex;
  flex-direction: column;
  align-items: center;
  min-width: 0;
}
.stat .label {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 1.2px;
  color: var(--muted);
}
.stat .value {
  font-size: 22px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}
.stat.best .value { font-size: 17px; line-height: 26px; color: var(--accent); }

/* ---------- board ---------- */
.frame {
  display: grid;
  grid-template-columns: var(--arrow) var(--size) var(--arrow);
  grid-template-rows: var(--arrow) var(--size) var(--arrow);
  grid-template-areas:
    ".    top    ."
    "left board  right"
    ".    bottom .";
  margin: 2px 0;
}
.board {
  grid-area: board;
  position: relative;
  width: var(--size);
  height: var(--size);
  border-radius: 20px;
  background: rgba(0, 0, 0, .28);
  box-shadow: inset 0 0 0 2px rgba(255, 255, 255, .08), 0 18px 40px rgba(0, 0, 0, .35);
  overflow: hidden;
  touch-action: none;
  cursor: grab;
}
.board:active { cursor: grabbing; }
.tiles {
  position: absolute;
  inset: 0;
  display: grid;
  grid-template-columns: repeat(var(--n), 1fr);
  grid-template-rows: repeat(var(--n), 1fr);
}
.cell {
  padding: calc(var(--cell, 60px) * .05);
  min-width: 0; min-height: 0;
}
.tile {
  --c: var(--c0);
  position: relative;
  width: 100%;
  height: 100%;
  border-radius: calc(var(--cell, 60px) * .2);
  background: linear-gradient(150deg, color-mix(in srgb, var(--c) 72%, #fff) 0%, var(--c) 55%, color-mix(in srgb, var(--c) 80%, #000) 100%);
  background-color: var(--c);
  box-shadow:
    inset 0 calc(var(--cell, 60px) * -.06) 0 rgba(0, 0, 0, .18),
    inset 0 2px 0 rgba(255, 255, 255, .35),
    0 4px 10px rgba(0, 0, 0, .25);
  display: grid;
  place-items: center;
  font-size: calc(var(--cell, 60px) * .44);
  font-weight: 700;
  line-height: 1;
  color: #fff;
  text-shadow: 0 2px 0 rgba(0, 0, 0, .22);
}
/* pallino: numero già nella sua cella */
.tile.ok::after {
  content: "";
  position: absolute;
  top: 12%; right: 12%;
  width: calc(var(--cell, 60px) * .1);
  height: calc(var(--cell, 60px) * .1);
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 0 6px rgba(255, 255, 255, .9);
  opacity: .9;
}

/* striscia mobile usata durante trascinamento/animazione */
.strip {
  position: absolute;
  display: flex;
  z-index: 2;
  will-change: transform;
  pointer-events: none;
}
.strip.h { flex-direction: row; left: 0; }
.strip.v { flex-direction: column; top: 0; }
.strip .cell {
  flex: none;
  width: var(--cell);
  height: var(--cell);
}
.strip .tile {
  box-shadow: inset 0 calc(var(--cell) * -.06) 0 rgba(0,0,0,.18), inset 0 2px 0 rgba(255,255,255,.35), 0 8px 18px rgba(0,0,0,.4);
  transform: scale(1.03);
  transition: transform .18s ease, box-shadow .25s ease;
}
/* mentre si aggancia, le tessere "si appoggiano" dolcemente */
.strip.settling .tile {
  transform: scale(1);
  box-shadow: inset 0 calc(var(--cell) * -.06) 0 rgba(0,0,0,.18), inset 0 2px 0 rgba(255,255,255,.35), 0 4px 10px rgba(0,0,0,.25);
}

.board.solved .tile {
  animation: pop .6s cubic-bezier(.3, 1.6, .5, 1) both;
  animation-delay: calc(var(--i) * 45ms);
}
@keyframes pop {
  0% { transform: scale(1); }
  40% { transform: scale(1.14) rotate(-4deg); filter: brightness(1.3); }
  100% { transform: scale(1); }
}

/* ---------- arrows ---------- */
.arrows { display: flex; }
.a-top { grid-area: top; }
.a-bottom { grid-area: bottom; }
.a-left { grid-area: left; flex-direction: column; }
.a-right { grid-area: right; flex-direction: column; }
.arrow {
  flex: 1;
  border: 0;
  background: transparent;
  padding: 0;
  display: grid;
  place-items: center;
  color: var(--muted);
  border-radius: 10px;
  transition: color .15s, transform .1s, background .15s;
}
.arrow svg { width: min(26px, 70%); height: min(26px, 70%); }
.arrow:active { color: var(--accent); transform: scale(.85); background: rgba(255, 255, 255, .06); }
@media (hover: hover) {
  .arrow:hover { color: #fff; background: rgba(255, 255, 255, .06); }
}

/* ---------- buttons ---------- */
.actions { display: flex; gap: 10px; }
.btn {
  border: 0;
  border-radius: 999px;
  padding: 12px 26px;
  font-size: 17px;
  font-weight: 600;
  transition: transform .12s, box-shadow .2s;
}
.btn:active { transform: scale(.95); }
.btn.primary {
  background: linear-gradient(135deg, var(--c0), var(--c4));
  box-shadow: 0 8px 22px rgba(255, 93, 115, .35);
}
.btn.ghost { background: var(--panel); border: 1px solid var(--panel-b); }

.hint {
  margin: 0;
  text-align: center;
  font-size: 13px;
  line-height: 1.45;
  color: var(--muted);
  max-width: 420px;
}

/* ---------- win modal ---------- */
.modal {
  position: fixed;
  inset: 0;
  z-index: 10;
  display: grid;
  place-items: center;
  padding: 16px;
  background: rgba(15, 6, 32, .55);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  animation: fade .25s ease both;
}
.modal[hidden] { display: none; }
.card {
  width: min(360px, 100%);
  text-align: center;
  padding: 24px 20px 20px;
  border-radius: 26px;
  background: linear-gradient(160deg, #3b1a66, #24104a);
  border: 1px solid var(--panel-b);
  box-shadow: 0 30px 60px rgba(0, 0, 0, .5);
  animation: rise .45s cubic-bezier(.3, 1.5, .5, 1) both;
}
.trophy { font-size: 54px; line-height: 1; }
.card h2 { margin: 8px 0 16px; font-size: 30px; }
.win-stats { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 18px; }
.win-stats .stat { padding: 10px; }
.win-stats .value { font-size: 28px; }
.rec {
  font-style: normal;
  font-size: 12px;
  font-weight: 600;
  color: #2a1540;
  background: var(--accent);
  border-radius: 999px;
  padding: 2px 8px;
  margin-top: 4px;
}
.rec[hidden] { display: none; }
.card-actions { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; }

@keyframes fade { from { opacity: 0; } }
@keyframes rise { from { opacity: 0; transform: translateY(30px) scale(.9); } }

#confetti {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 20;
}

/* touch (smartphone/tablet): solo trascinamento, niente frecce, griglia più grande */
@media (pointer: coarse) {
  :root { --arrow: 0px; }
  .arrows { display: none; }
  .frame { margin: 10px 0; }
  .hint .h-arrows { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  .board.solved .tile, .card, .modal { animation: none; }
}
