/* Scacchi con la lumaca immortale — tema scuro, stile autonomo. */

:root {
  --bg: #101318;
  --bg-2: #151a21;
  --card: #1a2029;
  --card-2: #212833;
  --line: #2b3441;
  --text: #e9ecef;
  --muted: #98a2b0;
  --accent: #7be3a8;        /* bava */
  --accent-strong: #3fcf86;
  --shell: #eaa452;         /* guscio */
  --danger: #ff6b6b;
  --warn: #ffcf5c;
  --sq-light: #e7e2d0;
  --sq-dark: #8ea67e;
  --radius: 14px;
  --shadow: 0 10px 30px rgba(0, 0, 0, .35);
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  min-height: 100svh;
  background:
    radial-gradient(1200px 600px at 80% -10%, rgba(123, 227, 168, .08), transparent 60%),
    radial-gradient(900px 500px at -10% 110%, rgba(234, 164, 82, .07), transparent 60%),
    var(--bg);
  color: var(--text);
  font: 16px/1.45 var(--font);
  -webkit-tap-highlight-color: transparent;
}
[hidden] { display: none !important; }
button, input { font: inherit; color: inherit; }
h1, h2, h3 { margin: 0; line-height: 1.2; }
.muted { color: var(--muted); }

/* ---- barra superiore ---- */
.topbar {
  display: flex;
  align-items: center;
  padding: 10px max(16px, env(safe-area-inset-left));
  border-bottom: 1px solid var(--line);
  background: rgba(16, 19, 24, .8);
  backdrop-filter: blur(8px);
  position: sticky;
  top: 0;
  z-index: 20;
}
.brand { display: flex; align-items: center; gap: 10px; color: var(--text); text-decoration: none; font-weight: 700; }
.brand em { font-style: normal; color: var(--muted); font-weight: 500; }
.brand img { display: block; }

main { padding: 16px; padding-bottom: max(24px, env(safe-area-inset-bottom)); }

/* ---- card e bottoni ---- */
.card {
  background: linear-gradient(180deg, var(--card), var(--bg-2));
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--shadow);
}
.card h3 { font-size: 13px; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); margin-bottom: 10px; }

.btn {
  appearance: none;
  border: 1px solid var(--line);
  background: var(--card-2);
  border-radius: 10px;
  padding: 10px 16px;
  min-height: 44px;
  cursor: pointer;
  font-weight: 600;
  transition: background .15s, border-color .15s, transform .05s;
}
.btn:hover { border-color: #3d4858; background: #283141; }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn.primary { background: var(--accent-strong); border-color: var(--accent-strong); color: #08140d; }
.btn.primary:hover { background: var(--accent); }
.btn.danger { background: var(--danger); border-color: var(--danger); color: #1b0606; }
.btn.ghost { background: transparent; }
.btn.ghost-danger { background: transparent; color: #ff9a9a; border-color: #5a2e33; }
.btn.small { min-height: 34px; padding: 6px 10px; font-size: 14px; }
.btn.wide { width: 100%; }
.btn:focus-visible, input:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.msg { margin: 0 0 12px; padding: 10px 12px; border-radius: 10px; background: #3a1f24; color: #ffd0d0; border: 1px solid #5a2e33; }
.msg[data-tone="info"] { background: #1b2f26; color: #c9f5dc; border-color: #2c5a43; }

/* ---- home: menu a sinistra, spiegazione a destra ---- */
.home {
  max-width: 1120px; margin: 0 auto; display: grid; gap: 24px;
  grid-template-columns: minmax(320px, 430px) minmax(0, 1fr); align-items: start;
}
.home-menu { display: grid; gap: 16px; position: sticky; top: 72px; }
.hero { text-align: center; padding-top: 8px; }
.hero h1 { font-size: clamp(24px, 6vw, 32px); margin: 6px 0 8px; }
.lead { color: var(--muted); margin: 0; }
.hero-snail { width: 120px; height: 120px; animation: crawl 6s ease-in-out infinite; filter: drop-shadow(0 6px 14px rgba(123, 227, 168, .25)); }
@keyframes crawl {
  0%, 100% { transform: translateX(-10px) scaleX(1); }
  45% { transform: translateX(10px) scaleX(1); }
  50% { transform: translateX(10px) scaleX(-1); }
  95% { transform: translateX(-10px) scaleX(-1); }
}

.home-card { display: grid; gap: 14px; }
.field { display: grid; gap: 6px; }
.field span { font-size: 14px; color: var(--muted); }
input[type="text"] {
  width: 100%;
  min-height: 46px;
  padding: 10px 12px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: #0e1217;
  font-size: 17px;
}
.block { display: grid; gap: 10px; }
.block h2 { font-size: 16px; }
.seg { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; border: 0; padding: 0; margin: 0; }
.seg label { position: relative; }
.seg input { position: absolute; opacity: 0; inset: 0; }
.seg span {
  display: flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 42px; border: 1px solid var(--line); border-radius: 10px; background: #0e1217; cursor: pointer; font-weight: 600;
}
.seg.levels { grid-template-columns: repeat(4, 1fr); }
.seg.levels span { font-size: 14px; text-align: center; line-height: 1.15; padding: 0 4px; }
.seg input:checked + span { border-color: var(--accent); background: #17271f; color: var(--accent); }
.seg input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 2px; }
.swatch { display: inline-block; width: 14px; height: 14px; border-radius: 50%; border: 1px solid #000; }
.swatch.w { background: #f4f4f4; }
.swatch.b { background: #111; border-color: #666; }
.or { display: flex; align-items: center; gap: 10px; color: var(--muted); font-size: 13px; }
.or::before, .or::after { content: ""; flex: 1; height: 1px; background: var(--line); }
.join-row { display: grid; grid-template-columns: 1fr auto; gap: 8px; }
#code { font-size: 22px; letter-spacing: .3em; text-align: center; font-variant-numeric: tabular-nums; }
.hint { margin: 0; font-size: 13px; color: var(--muted); }
.rules { display: grid; gap: 18px; }
.rules h2 { font-size: 20px; }
.rule { display: grid; grid-template-columns: 168px minmax(0, 1fr); gap: 18px; align-items: center; }
.rule + .rule { border-top: 1px solid var(--line); padding-top: 18px; }
.card .rule h3 { font-size: 16px; margin-bottom: 6px; text-transform: none; letter-spacing: 0; color: var(--text); }
.rule p { margin: 0; color: var(--muted); font-size: 15px; }
.rule p strong { color: var(--text); }
.diagram { width: 168px; }
.diagram .board { border-radius: 6px; box-shadow: 0 8px 18px rgba(0, 0, 0, .4), 0 0 0 1px #000; }
.diagram .coord { font-size: 7px; }
.dots-demo-wrap {
  width: 168px; aspect-ratio: 2 / 1; display: grid; place-items: center;
  border-radius: 6px; background: #12171e; border: 1px solid var(--line);
}

/* ---- pagina "entra" (da link) ---- */
.join-view { max-width: 420px; margin: 24px auto 0; }
.join-card { display: grid; gap: 12px; text-align: center; }
.join-card .field { text-align: left; }
.join-card p { margin: 0; }
.join-info { color: var(--muted); min-height: 1.4em; }
.join-info strong { color: var(--text); }

/* ---- layout partita ---- */
.game {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 340px;
  gap: 20px;
  max-width: 1160px;
  margin: 0 auto;
  align-items: start;
}
.board-col { display: grid; gap: 10px; justify-items: center; }
.board-wrap { width: min(100%, calc(100svh - 250px)); min-width: 280px; transition: opacity .3s, filter .3s; }
.board-wrap.dim { opacity: .55; filter: saturate(.6); }
.board-status { margin: 0; font-weight: 700; font-size: 17px; text-align: center; min-height: 1.4em; }
.board-status[data-tone="turn"] { color: var(--accent); }
.board-status[data-tone="win"] { color: var(--accent); }
.board-status[data-tone="lose"] { color: var(--danger); }
.board-status[data-tone="draw"], .board-status[data-tone="end"] { color: var(--warn); }

.waiting { width: min(100%, 460px); text-align: center; display: grid; gap: 8px; justify-items: center; }
.waiting p { margin: 0; }
.big-code { font-size: clamp(56px, 16vw, 84px); font-weight: 800; letter-spacing: .12em; font-variant-numeric: tabular-nums; color: var(--shell); line-height: 1; }
.waiting-text { display: flex; align-items: center; gap: 8px; }
.spinner { width: 14px; height: 14px; border: 2px solid var(--line); border-top-color: var(--accent); border-radius: 50%; animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---- scacchiera ---- */
.board {
  position: relative;
  width: 100%;
  aspect-ratio: 1 / 1;
  border-radius: 8px;
  overflow: hidden;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  box-shadow: 0 18px 40px rgba(0, 0, 0, .45), 0 0 0 1px #000;
}
.squares { position: absolute; inset: 0; display: grid; grid-template: repeat(8, 1fr) / repeat(8, 1fr); }
.sq { position: relative; }
.sq.light { background-color: var(--sq-light); }
.sq.dark { background-color: var(--sq-dark); }
.sq.last::before { content: ""; position: absolute; inset: 0; background: rgba(255, 214, 92, .42); }
.sq.sel::before { content: ""; position: absolute; inset: 0; background: rgba(63, 207, 134, .5); }
.sq.check { background-image: radial-gradient(circle, rgba(255, 40, 40, .95) 0%, rgba(230, 30, 30, .6) 35%, transparent 72%); }
.sq.hover { box-shadow: inset 0 0 0 4px rgba(255, 255, 255, .7); }
.sq.move::after {
  content: ""; position: absolute; left: 50%; top: 50%; width: 30%; height: 30%;
  transform: translate(-50%, -50%); border-radius: 50%; background: rgba(20, 60, 35, .45); z-index: 3;
}
.sq.capture::after {
  content: ""; position: absolute; inset: 4%; border-radius: 50%;
  box-shadow: inset 0 0 0 5px rgba(20, 60, 35, .5); z-index: 3;
}
.sq.danger { box-shadow: inset 0 0 0 4px var(--danger); animation: danger 1.1s ease-in-out infinite; }
@keyframes danger { 50% { box-shadow: inset 0 0 0 7px rgba(255, 107, 107, .35); } }

/* prossimo passo delle lumache: casella rossa */
.sq.snail-next::before { content: ""; position: absolute; inset: 0; background: rgba(225, 62, 38, .62); }

/* scia viscida: argentata per la lumaca bianca, scura per la nera */
.slime { position: absolute; inset: 0; opacity: 0; pointer-events: none; transition: opacity 1.2s; }
.sq.trail-w .slime, .sq.trail-b .slime { opacity: var(--slime, .5); filter: blur(.3px); }
.sq.trail-w .slime {
  background:
    radial-gradient(ellipse 30% 18% at 35% 35%, rgba(255, 255, 255, .7), transparent 70%),
    radial-gradient(ellipse 22% 14% at 68% 62%, rgba(255, 255, 255, .55), transparent 70%),
    radial-gradient(ellipse 60% 55% at 50% 52%, rgba(235, 245, 255, .6), rgba(210, 225, 240, .25) 60%, transparent 75%);
}
.sq.trail-b .slime {
  background:
    radial-gradient(ellipse 30% 18% at 35% 35%, rgba(255, 255, 255, .35), transparent 70%),
    radial-gradient(ellipse 22% 14% at 68% 62%, rgba(255, 255, 255, .25), transparent 70%),
    radial-gradient(ellipse 60% 55% at 50% 52%, rgba(30, 30, 40, .55), rgba(20, 20, 30, .25) 60%, transparent 75%);
}
.sq.trail-w.trail-b .slime {
  background:
    radial-gradient(ellipse 60% 55% at 40% 45%, rgba(235, 245, 255, .55), transparent 70%),
    radial-gradient(ellipse 60% 55% at 60% 58%, rgba(30, 30, 40, .5), transparent 72%);
}

.coord { position: absolute; font-size: clamp(8px, 1.6vw, 12px); font-weight: 700; pointer-events: none; z-index: 2; }
.coord.file { right: 4%; bottom: 2%; }
.coord.rank { left: 4%; top: 2%; }
.sq.light .coord { color: var(--sq-dark); }
.sq.dark .coord { color: var(--sq-light); }

.pieces { position: absolute; inset: 0; pointer-events: none; z-index: 4; }
.piece {
  position: absolute; left: 0; top: 0; width: 12.5%; height: 12.5%;
  transition: transform .22s ease-out;
  will-change: transform;
}
.piece.no-anim, .snail.no-anim { transition: none !important; }
.piece.dragging { opacity: .3; }
.piece.eaten { animation: eaten .6s ease-in forwards; z-index: 5; }
@keyframes eaten {
  to { opacity: 0; transform-origin: center; filter: blur(2px) saturate(0); }
}
.drag-ghost { position: fixed; left: 0; top: 0; z-index: 100; pointer-events: none; filter: drop-shadow(0 8px 10px rgba(0, 0, 0, .5)); }

/* lumache */
.snail {
  position: absolute; left: 0; top: 0; width: 12.5%; height: 12.5%;
  z-index: 6; pointer-events: none;
  transition: transform 1.4s cubic-bezier(.45, .05, .35, 1);
}
.snail-img {
  width: 100%; height: 100%; display: block;
  filter: drop-shadow(0 2px 3px rgba(0, 0, 0, .35));
  animation: snail-idle 2.6s ease-in-out infinite;
  transform-origin: 50% 90%;
}
.snail-b .snail-img { animation-delay: -1.3s; }
.snail.facing-left .snail-img { animation-name: snail-idle-left; }
.snail.moving .snail-img { animation: snail-move .7s ease-in-out infinite; }
.snail.moving.facing-left .snail-img { animation-name: snail-move-left; }
@keyframes snail-idle { 0%, 100% { transform: scale(1, 1); } 50% { transform: scale(1.02, .97); } }
@keyframes snail-idle-left { 0%, 100% { transform: scale(-1, 1); } 50% { transform: scale(-1.02, .97); } }
@keyframes snail-move { 0%, 100% { transform: scale(1, 1); } 50% { transform: scale(1.12, .9); } }
@keyframes snail-move-left { 0%, 100% { transform: scale(-1, 1); } 50% { transform: scale(-1.12, .9); } }


/* promozione */
.promo {
  position: absolute; top: 30%; width: 50%; z-index: 30;
  background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 8px; box-shadow: var(--shadow);
}
.promo-title { font-size: 13px; color: var(--muted); text-align: center; margin-bottom: 6px; }
.promo-row { display: grid; grid-template-columns: repeat(4, 1fr); gap: 4px; }
.promo-btn { background: var(--sq-light); border: 0; border-radius: 8px; padding: 2px; cursor: pointer; aspect-ratio: 1; }
.promo-btn:hover { background: #fff; }
.promo-btn img { width: 100%; height: 100%; display: block; }
.promo-cancel { width: 100%; margin-top: 6px; background: transparent; border: 0; color: var(--muted); cursor: pointer; padding: 6px; }

/* puntini dei round: uno per round, al terzo le lumache fanno un passo */
@keyframes pulse { 70% { box-shadow: 0 0 0 8px rgba(123, 227, 168, 0); } 100% { box-shadow: 0 0 0 0 rgba(123, 227, 168, 0); } }
.snail-dots { display: flex; align-items: center; justify-content: center; gap: 12px; min-height: 34px; color: #d9d4c7; }
.snail-dots .dot {
  width: 13px; height: 13px; border-radius: 50%;
  border: 2px solid currentColor; opacity: .55;
  transition: background .35s, opacity .35s, box-shadow .35s;
}
.snail-dots .dot.on { background: currentColor; opacity: 1; box-shadow: 0 0 10px rgba(255, 244, 214, .55); }
.dots-icon { display: inline-flex; opacity: .75; transition: color .35s, opacity .35s, transform .35s; }
.dots-snail { width: 30px; height: 30px; }
.dots-icon.on { opacity: 1; color: #fff4d6; transform: scale(1.12); filter: drop-shadow(0 0 8px rgba(255, 244, 214, .6)); }
.snail-dots.stepping .dot.on { animation: dots-flash .6s ease-in-out 3; }
@keyframes dots-flash { 50% { box-shadow: 0 0 16px rgba(255, 244, 214, .9); } }

/* ---- pannello ---- */
.panel { display: grid; gap: 12px; }
.lobby-card { display: grid; gap: 8px; }
.lobby-line { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.lobby-line .btn { margin-left: auto; }
.code-chip { font-size: 22px; letter-spacing: .12em; color: var(--shell); font-variant-numeric: tabular-nums; }
.meta { font-size: 14px; color: var(--muted); }
.conn { display: inline-flex; align-items: center; gap: 6px; margin-right: auto; }
.conn::before { content: ""; width: 8px; height: 8px; border-radius: 50%; background: var(--accent); }
.conn[data-state="reconnecting"] { color: var(--warn); }
.conn[data-state="reconnecting"]::before { background: var(--warn); animation: pulse 1.2s infinite; }

.players { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; }
.player { display: flex; align-items: center; gap: 10px; padding: 8px 10px; border-radius: 10px; background: #12171e; border: 1px solid transparent; }
.player.to-move { border-color: var(--accent); }
.player .pname { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1; min-width: 0; }
.player .pname em { color: var(--muted); font-style: normal; font-weight: 500; }
.player .pcolor { font-size: 13px; color: var(--muted); }
.online { width: 9px; height: 9px; border-radius: 50%; flex: none; }
.online.on { background: var(--accent); }
.online.off { background: #4b5563; }

.status { margin: 12px 0 0; font-weight: 600; }
.status[data-tone="win"], .status[data-tone="turn"] { color: var(--accent); }
.status[data-tone="lose"] { color: var(--danger); }
.status[data-tone="draw"], .status[data-tone="end"] { color: var(--warn); }
.actions { display: grid; gap: 8px; margin-top: 10px; }
.actions:empty { display: none; }
.actions p { margin: 0; }
.actions .row { display: flex; gap: 8px; flex-wrap: wrap; }
.actions .row .btn { flex: 1; }
.offer { color: var(--shell); font-weight: 600; }

.eaten { display: flex; flex-wrap: wrap; gap: 2px; min-height: 34px; align-items: center; }
.eaten .mini-piece { width: 32px; height: 32px; }
.mini-piece { width: 18px; height: 18px; vertical-align: -4px; }

.moves-card { display: grid; }
.moves {
  list-style: none; margin: 0; padding: 0; max-height: 280px; overflow-y: auto;
  font-variant-numeric: tabular-nums; font-size: 15px;
}
.moves li { display: grid; grid-template-columns: 36px 1fr 1fr; gap: 4px 8px; padding: 4px 6px; border-radius: 6px; }
.moves li:nth-child(odd) { background: rgba(255, 255, 255, .03); }
.moves li.muted { display: block; }
.mv-n { color: var(--muted); }
.mv { font-weight: 600; }
.mv-snail { grid-column: 2 / -1; font-size: 13px; color: var(--muted); display: flex; flex-wrap: wrap; gap: 4px 14px; }
.mv-snail span { display: inline-flex; align-items: center; gap: 4px; }
.mini-snail { width: 20px; height: 20px; background: var(--sq-dark); border-radius: 4px; }

/* ---- toast ---- */
.toast {
  position: fixed; left: 50%; bottom: max(20px, env(safe-area-inset-bottom)); transform: translateX(-50%);
  max-width: min(92vw, 480px); z-index: 200;
  padding: 12px 16px; border-radius: 12px; background: #3a1f24; color: #ffe0e0; border: 1px solid #6b3239;
  box-shadow: var(--shadow); font-weight: 600; text-align: center;
}
.toast[data-tone="ok"] { background: #17311f; color: #d4fbe2; border-color: #2c5a43; }

/* ---- mobile ---- */
@media (max-width: 900px) {
  .game { grid-template-columns: 1fr; gap: 14px; }
  .board-wrap { width: 100%; min-width: 0; }
  .moves { max-height: 220px; }
  .home { grid-template-columns: 1fr; max-width: 560px; }
  .home-menu { position: static; }
}
@media (max-width: 480px) {
  main { padding: 12px 8px; }
  .brand em { display: none; }
  .card { padding: 14px; }
  .board { border-radius: 4px; }
  .rule { grid-template-columns: 120px minmax(0, 1fr); gap: 12px; }
  .diagram, .dots-demo-wrap { width: 120px; }
  .rule p { font-size: 14px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; animation-iteration-count: 1 !important; }
  .snail { transition-duration: .2s; }
}
