:root {
  --bg: #f3f1ec;
  --surface: #ffffff;
  --text: #1f2328;
  --muted: #5f6670;
  --border: #d9d5cc;
  --cell: #e6e2d8;
  --cell-stroke: #c9c3b6;
  --cell-hover: #d6eadb;
  --tile-bg: #18181b;
  --accent: #2f6fdb;
  --accent-text: #ffffff;
  --danger: #d62828;
  --ok: #2e7d32;
  --radius: 10px;
  --shadow: 0 1px 3px rgb(0 0 0 / 0.08), 0 4px 14px rgb(0 0 0 / 0.05);
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #15171a;
    --surface: #1f2226;
    --text: #e8e6e3;
    --muted: #a3a8af;
    --border: #33373d;
    --cell: #262a2f;
    --cell-stroke: #3a3f46;
    --cell-hover: #2c4433;
    --tile-bg: #0b0b0d;
    --accent: #5b8ff0;
  }
}

* { box-sizing: border-box; }

html, body { margin: 0; }

body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

button, select, input { font: inherit; }

/* ---------- barra superiore ---------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  flex-wrap: wrap;
}
.brand { display: flex; align-items: center; gap: 10px; }
.brand h1 { font-size: 1.25rem; margin: 0; letter-spacing: 0.02em; }
.badge {
  font-size: 0.75rem;
  padding: 2px 8px;
  border-radius: 999px;
  background: #fff3cd;
  color: #7a5b00;
  border: 1px solid #f0d58a;
  white-space: nowrap;
}
.level-picker { display: flex; align-items: center; gap: 8px; color: var(--muted); }
.level-picker select {
  padding: 6px 8px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
}

/* ---------- impaginazione ---------- */
.layout {
  flex: 1;
  display: grid;
  grid-template-columns: minmax(0, 1fr) 360px;
  gap: 16px;
  padding: 16px;
  max-width: 1280px;
  width: 100%;
  margin: 0 auto;
}
@media (max-width: 860px) {
  .layout { grid-template-columns: minmax(0, 1fr); padding: 10px; gap: 10px; }
}

.board-wrap {
  position: relative;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 10px;
  display: flex;
  flex-direction: column;
  align-items: center;
  min-width: 0;
}

.goal {
  align-self: flex-start;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  font-weight: 600;
  padding: 6px 12px;
  border-radius: 999px;
  border: 1px solid var(--border);
}
.goal-swatch {
  width: 22px;
  height: 22px;
  border-radius: 50%;
  border: 3px solid var(--tile-bg);
  box-shadow: 0 0 0 1px var(--border);
}

.board {
  width: 100%;
  max-width: 720px;
  height: auto;
  aspect-ratio: 1 / 1;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}

.status { margin: 6px 0 0; color: var(--muted); min-height: 1.4em; text-align: center; }
.status.ok { color: var(--ok); font-weight: 600; }

/* ---------- griglia e tessere ---------- */
.cell polygon {
  fill: var(--cell);
  stroke: var(--cell-stroke);
  stroke-width: 1.5;
  transition: fill 0.12s;
}
.cell:hover polygon, .cell.drop-target polygon { fill: var(--cell-hover); }
.cell.origin polygon { stroke-dasharray: 3 3; }

.tile-rot { transition: transform 0.18s ease-out; }
.board-tile { cursor: grab; transition: transform 0.18s ease-out, opacity 0.12s; }
.board-tile.dragging { opacity: 0.25; }
.tile-bg { fill: var(--tile-bg); stroke: #000; stroke-width: 1; }
.path { fill: none; stroke-linecap: butt; }
.path-under { fill: none; stroke: var(--tile-bg); stroke-linecap: butt; }
.tile-label-bg { fill: rgb(0 0 0 / 0.65); }
.tile-label { fill: #fff; font-weight: 700; pointer-events: none; }
.board-tile.selected .tile-bg { stroke: var(--accent); stroke-width: 4; }

.mismatch {
  stroke: #fff;
  stroke-width: 7;
  stroke-linecap: round;
  pointer-events: none;
}
.mismatch-inner {
  stroke: var(--danger);
  stroke-width: 4;
  stroke-dasharray: 5 4;
  stroke-linecap: round;
  pointer-events: none;
  animation: blink 1s steps(2) infinite;
}
@keyframes blink { 50% { opacity: 0.35; } }

.board.solved .path.on-loop {
  filter: drop-shadow(0 0 4px rgb(255 255 255 / 0.9));
  animation: glow 1.2s ease-in-out infinite alternate;
}
@keyframes glow { from { stroke-opacity: 1; } to { stroke-opacity: 0.6; } }

.hint-ghost { opacity: 0.55; cursor: pointer; }
.hint-ghost .tile-bg { stroke: var(--accent); stroke-width: 4; stroke-dasharray: 6 4; }

@media (prefers-reduced-motion: reduce) {
  .tile-rot, .board-tile { transition: none; }
  .mismatch-inner, .board.solved .path.on-loop { animation: none; }
}

/* ---------- pannello ---------- */
.panel { display: flex; flex-direction: column; gap: 12px; min-width: 0; }
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 12px;
}
.card h2 {
  font-size: 0.95rem;
  margin: 0 0 10px;
  display: flex;
  justify-content: space-between;
  gap: 8px;
}
.count { color: var(--muted); font-weight: 500; }

.inventory {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(64px, 1fr));
  gap: 6px;
  min-height: 70px;
}
.inv-tile {
  border: 2px solid transparent;
  border-radius: 10px;
  background: var(--bg);
  padding: 2px;
  cursor: grab;
  touch-action: none;
  display: flex;
  align-items: center;
  justify-content: center;
  aspect-ratio: 1 / 1;
}
.inv-tile:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.inv-tile.selected { border-color: var(--accent); background: color-mix(in srgb, var(--accent) 14%, var(--bg)); }
.inv-tile svg { width: 100%; height: 100%; pointer-events: none; }
.inv-empty { grid-column: 1 / -1; color: var(--muted); font-size: 0.9rem; margin: 0; align-self: center; }

.hint-text { color: var(--muted); font-size: 0.8rem; margin: 10px 0 0; line-height: 1.4; }

.row { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.hints-row { margin-top: 10px; }

button {
  padding: 8px 12px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  min-height: 40px;
}
button:hover:not(:disabled) { border-color: var(--accent); }
button:disabled { opacity: 0.45; cursor: not-allowed; }
button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }

.toggle { display: inline-flex; align-items: center; gap: 6px; color: var(--muted); }

.messages { margin-top: 10px; font-size: 0.9rem; line-height: 1.45; }
.messages:empty { display: none; }
.messages ul { margin: 6px 0 0; padding-left: 18px; }
.msg-ok { color: var(--ok); font-weight: 600; }
.msg-err { color: var(--danger); font-weight: 600; }
.msg-info { color: var(--muted); }

/* ---------- trascinamento ---------- */
.drag-ghost {
  position: fixed;
  left: 0;
  top: 0;
  pointer-events: none;
  z-index: 1000;
  filter: drop-shadow(0 6px 12px rgb(0 0 0 / 0.35));
}

/* ---------- dialogo vittoria ---------- */
.win-dialog {
  border: none;
  border-radius: 14px;
  padding: 20px 22px;
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 20px 50px rgb(0 0 0 / 0.3);
  max-width: min(420px, calc(100vw - 32px));
}
.win-dialog::backdrop { background: rgb(0 0 0 / 0.35); }
.win-dialog h2 { margin-top: 0; color: var(--ok); }

.footer {
  text-align: center;
  color: var(--muted);
  font-size: 0.8rem;
  padding: 12px 16px 18px;
}
.footer a { color: var(--accent); }
