:root {
  --bg: #f3f4f8; --surface: #ffffff; --surface-2: #f1f3f8; --ink: #15171c; --muted: #6a6f7b;
  --line: #e3e5ec; --grid-line: #d6d9e2; --thick: #2a2e37;
  --accent: #2457d6; --accent-ink: #ffffff; --accent-soft: #e7edfd;
  --peer: #eef2fa; --same: #d5e0fb; --sel: #bccdf8; --bad: #ffe0e0; --bad-ink: #d42f35; --ok: #17924d;
  --user-ink: #2457d6; --note-ink: #7a8090;
  --shadow: 0 1px 2px rgba(20, 23, 28, .06), 0 4px 14px rgba(20, 23, 28, .06);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1115; --surface: #181b22; --surface-2: #1f232c; --ink: #eceef3; --muted: #9aa0ad;
    --line: #2a2e38; --grid-line: #2f3440; --thick: #c3c8d2;
    --accent: #7c9cff; --accent-ink: #0f1115; --accent-soft: #1f2a48;
    --peer: #1d222c; --same: #26345a; --sel: #33467e; --bad: #46222a; --bad-ink: #ff7378; --ok: #42d488;
    --user-ink: #93aeff; --note-ink: #8c92a2;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 4px 14px rgba(0, 0, 0, .25);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body { margin: 0; background: var(--bg); color: var(--ink); -webkit-font-smoothing: antialiased;
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; }
main { max-width: 1080px; margin: 0 auto; padding: max(12px, env(safe-area-inset-top)) 16px max(24px, env(safe-area-inset-bottom)); }
a { color: var(--accent); }
h1, h2, h3 { margin: 0; letter-spacing: -.01em; line-height: 1.2; }
h3 { font-size: 1rem; }
p { margin: 8px 0; }
.muted { color: var(--muted); }
.small { font-size: .875rem; }
.center { text-align: center; margin-top: 15vh; }
.err { color: var(--bad-ink); }
.mt { margin-top: 20px; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* controlli */
button { font: inherit; color: inherit; cursor: pointer; border: 0; background: none; padding: 0; }
button:disabled { cursor: default; opacity: .5; }
.primary, .secondary { display: inline-flex; align-items: center; justify-content: center; min-height: 44px; padding: 0 18px;
  border-radius: 12px; font-weight: 600; white-space: nowrap; }
.primary { background: var(--accent); color: var(--accent-ink); }
.secondary { background: var(--accent-soft); color: var(--accent); }
input { font: inherit; color: inherit; min-height: 44px; padding: 0 14px; width: 100%; min-width: 0;
  border: 1px solid var(--line); border-radius: 12px; background: var(--surface); }
input:focus { outline: 2px solid var(--accent); outline-offset: 0; border-color: transparent; }
label { font-weight: 600; font-size: .9rem; }
.inline { display: flex; gap: 8px; margin-top: 12px; }
.stack { display: flex; flex-direction: column; gap: 10px; text-align: left; }
kbd { font: inherit; font-size: .75rem; padding: 1px 6px; border-radius: 6px; background: var(--surface-2); border: 1px solid var(--line); }

/* logo */
.logo { width: 30px; height: 30px; flex: none; }
.logo rect { fill: var(--accent); }
.logo path { stroke: var(--accent-ink); stroke-width: 2.2; stroke-linecap: round; }

/* barre in alto */
.appbar { display: flex; align-items: center; gap: 10px; padding: 8px 0; font-size: 1.3rem; }
.avatar { margin-left: auto; width: 40px; height: 40px; border-radius: 50%; display: grid; place-items: center;
  background: var(--accent-soft); color: var(--accent); font-weight: 700; font-size: 1rem; text-decoration: none; }
.topbar { display: flex; align-items: center; gap: 12px; padding: 8px 0 14px; }
.topbar h1 { font-size: 1.25rem; }
.topbar small { color: var(--muted); font-size: .85rem; }
.iconbtn { width: 40px; height: 40px; flex: none; display: grid; place-items: center; border-radius: 12px;
  background: var(--surface); box-shadow: var(--shadow); color: var(--ink); text-decoration: none; font-size: 1.6rem; line-height: 1; padding-bottom: 3px; }

/* home */
.section { margin: 22px 0 10px; font-size: .78rem; font-weight: 700; letter-spacing: .08em; text-transform: uppercase; color: var(--muted); }
.cards { display: grid; gap: 12px; }
.cards.three { grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; }
.cards.two { grid-template-columns: minmax(0, 1fr); }
@media (min-width: 640px) {
  .cards.three { gap: 12px; }
  .cards.two { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
.card { display: flex; flex-direction: column; gap: 6px; padding: 14px; border-radius: 16px; background: var(--surface);
  box-shadow: var(--shadow); color: inherit; text-decoration: none; transition: transform .15s; }
.card:hover { transform: translateY(-2px); }
.card h3 { font-size: 1.05rem; }
.card small { color: var(--muted); font-size: .8rem; }
.cards.three .card { padding: 12px; }
.cards.three .card h3 { font-size: 1rem; }
.card.link { flex-direction: row; align-items: center; justify-content: space-between; gap: 12px; }
.card.link > span { font-size: 1.6rem; color: var(--muted); }
.row-between { display: flex; align-items: center; justify-content: space-between; gap: 6px; }
.chip { font-size: .72rem; font-weight: 700; padding: 2px 8px; border-radius: 999px; background: var(--accent-soft); color: var(--accent); white-space: nowrap; }
.pts { font-size: .78rem; font-weight: 700; color: var(--muted); white-space: nowrap; }
.pts.ok { color: var(--ok); }
.level { display: flex; gap: 3px; }
.level i { width: 12px; height: 4px; border-radius: 2px; background: var(--line); }
.level i.on { background: var(--accent); }
.bar { height: 4px; margin-top: auto; border-radius: 2px; background: var(--line); overflow: hidden; }
.bar i { display: block; height: 100%; background: var(--accent); }

/* pannelli */
.split { display: grid; gap: 12px; }
@media (min-width: 900px) { .split { grid-template-columns: repeat(2, minmax(0, 1fr)); align-items: start; } }
.panel { padding: 16px; border-radius: 16px; background: var(--surface); box-shadow: var(--shadow); }
.panel h2.big { font-size: 1.5rem; margin: 2px 0 4px; }
.code { font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace; font-weight: 700; letter-spacing: .12em; }
.code.big { font-size: 1.5rem; color: var(--accent); }
.tabs { display: grid; grid-template-columns: repeat(4, 1fr); gap: 4px; padding: 4px; border-radius: 12px; background: var(--surface-2); }
.tabs button { min-height: 36px; border-radius: 9px; font-size: .88rem; font-weight: 600; color: var(--muted); }
.tabs button.on { background: var(--surface); color: var(--ink); box-shadow: var(--shadow); }
.lb { list-style: none; margin: 10px 0 0; padding: 0; }
.lb li { display: flex; align-items: center; gap: 12px; padding: 10px 8px; }
.lb li + li { border-top: 1px solid var(--line); }
.lb li.me { background: var(--accent-soft); border-color: transparent; border-radius: 10px; }
.lb li.me + li { border-color: transparent; }
.lb .rank { width: 28px; height: 28px; flex: none; display: grid; place-items: center; border-radius: 50%;
  font-size: .85rem; font-weight: 700; background: var(--surface-2); color: var(--muted); }
.lb li:nth-child(1) .rank { background: #f6c945; color: #4a3800; }
.lb li:nth-child(2) .rank { background: #cfd4de; color: #2d323c; }
.lb li:nth-child(3) .rank { background: #e5a874; color: #4a2700; }
.lb .name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; }
.lb .name small { color: var(--muted); }
.lb b { font-variant-numeric: tabular-nums; }
.lb .empty { justify-content: center; }
.options { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-top: 10px; }
.option { display: flex; flex-direction: column; align-items: center; gap: 2px; padding: 14px 8px; border-radius: 14px;
  background: var(--surface-2); text-align: center; }
.option b { font-size: 1.15rem; }
.option small { color: var(--muted); font-size: .78rem; }
.option:hover:not(:disabled) { background: var(--accent-soft); }
.option.wide { width: 100%; margin-top: 10px; }
.rooms { display: grid; gap: 6px; margin-top: 10px; }
.room { display: flex; align-items: center; gap: 12px; padding: 10px 12px; border-radius: 12px; background: var(--surface-2);
  color: inherit; text-decoration: none; }
.room small { margin-left: auto; color: var(--muted); }
.hero { max-width: 400px; margin: 10vh auto 0; display: flex; flex-direction: column; gap: 12px; text-align: center; }
.hero .logo { width: 72px; height: 72px; margin: 0 auto 4px; }
.hero h1 { font-size: 2.1rem; }
.hero .stack { margin-top: 8px; }

/* partita */
.game-body { display: flex; flex-direction: column; gap: 12px; }
.side { display: contents; } /* su telefono: dati sopra, griglia, tasti sotto */
.info { order: 1; display: flex; flex-direction: column; gap: 10px; }
.board-area { order: 2; overflow: auto; margin: 0 -16px; padding: 2px 16px; }
.controls { order: 3; position: sticky; bottom: 0; z-index: 1; padding: 10px 0 max(8px, env(safe-area-inset-bottom));
  background: linear-gradient(to bottom, transparent, var(--bg) 10px); }
@media (min-width: 900px) {
  .game-body { display: grid; grid-template-columns: minmax(0, 1fr) 320px; gap: 28px; align-items: start; }
  .side { display: flex; flex-direction: column; gap: 16px; position: sticky; top: 16px; }
  .board-area { order: 0; margin: 0; padding: 0; }
  .controls { position: static; padding: 0; background: none; }
}
.stats { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; }
.stat { padding: 10px 12px; border-radius: 14px; background: var(--surface); box-shadow: var(--shadow); }
.stat b { display: block; font-size: 1.25rem; font-variant-numeric: tabular-nums; }
.stat small { color: var(--muted); font-size: .75rem; }
.info .panel.invite { display: flex; align-items: center; justify-content: space-between; padding: 12px 14px; }
.rules { margin: 0; font-size: .8rem; color: var(--muted); }
.banner { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 10px 14px; border-radius: 12px;
  font-weight: 700; color: var(--ok); background: color-mix(in srgb, var(--ok) 14%, transparent); }
.banner.bad { color: var(--bad-ink); background: var(--bad); }
.banner .secondary { min-height: 36px; padding: 0 12px; background: var(--surface); color: var(--bad-ink); }

.grid { --board: min(100vw - 36px, 560px); --cell: max(24px, calc(var(--board) / var(--n)));
  display: grid; grid-template-columns: repeat(var(--n), var(--cell)); width: max-content; margin: 0 auto;
  border: 2px solid var(--thick); border-radius: 12px; overflow: hidden; background: var(--surface); box-shadow: var(--shadow); }
@media (min-width: 900px) { .grid { --board: min(100vh - 140px, min(100vw, 1080px) - 386px); } } /* 386 = margini + colonna laterale */
.cell { position: relative; display: grid; place-items: center; width: var(--cell); height: var(--cell); line-height: 1;
  border-right: 1px solid var(--grid-line); border-bottom: 1px solid var(--grid-line); background: var(--surface);
  color: var(--user-ink); font-size: calc(var(--cell) * .5); font-weight: 500; font-variant-numeric: tabular-nums; letter-spacing: -.03em;
  transition: background-color .12s; }
.cell.bx { border-right: 2px solid var(--thick); }
.cell.by { border-bottom: 2px solid var(--thick); }
.cell.ec { border-right: 0; }
.cell.er { border-bottom: 0; }
.cell.given { color: var(--ink); font-weight: 700; }
.cell.peer { background: var(--peer); }
.cell.same { background: var(--same); }
.cell.sel { background: var(--sel); }
.cell.bad { background: var(--bad); animation: shake .35s; }
.cell.wrong { background: var(--bad); color: var(--bad-ink); }
.cell.pop { animation: pop .2s ease-out; }
.cell:focus-visible { outline: 2px solid var(--accent); outline-offset: -3px; }
.notes { display: grid; grid-template-columns: repeat(var(--bc), 1fr); grid-template-rows: repeat(var(--br), 1fr);
  width: 100%; height: 100%; padding: 1px; color: var(--note-ink); font-size: calc(var(--cell) / (var(--bc) + 1.3)); font-weight: 600; letter-spacing: 0; }
.notes i { display: grid; place-items: center; font-style: normal; }
@keyframes shake { 20%, 60% { transform: translateX(-3px); } 40%, 80% { transform: translateX(3px); } }
@keyframes pop { from { transform: scale(.6); opacity: .4; } }

.tools { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-bottom: 8px; }
.tool { display: flex; align-items: center; justify-content: center; gap: 6px; min-height: 44px; border-radius: 12px;
  background: var(--surface); box-shadow: var(--shadow); font-size: .9rem; font-weight: 600; }
.tool[aria-pressed="true"] { background: var(--accent); color: var(--accent-ink); }
.pad { display: grid; grid-template-columns: repeat(var(--cols), minmax(0, 1fr)); gap: 6px; }
@media (min-width: 900px) { .pad { grid-template-columns: repeat(var(--dcols), minmax(0, 1fr)); gap: 8px; } }
.pad button { position: relative; min-height: 50px; border-radius: 12px; background: var(--surface); box-shadow: var(--shadow);
  color: var(--ink); font-size: 1.3rem; font-weight: 600; font-variant-numeric: tabular-nums; transition: transform .08s; }
@media (min-width: 900px) { .pad button { min-height: 58px; font-size: 1.5rem; } }
.pad button small { position: absolute; top: 3px; right: 6px; font-size: .62rem; font-weight: 600; color: var(--muted); }
.pad button:active { transform: scale(.94); }
.pad button.done { visibility: hidden; }
.pad button.full { opacity: .35; }
.notes-on .pad button b { font-size: .8em; font-weight: 500; color: var(--accent); }
.keys { display: none; margin: 12px 0 0; font-size: .8rem; color: var(--muted); }
@media (hover: hover) and (pointer: fine) { .keys { display: block; } }

@media (prefers-reduced-motion: reduce) { *, *::before, *::after { animation: none !important; transition: none !important; } }
