:root {
  --paper: #faf8f3;
  --band: #f2eee4;
  --line: #cfc7b5;
  --road: #e7e1d3;
  --ink: #1f3a5f;
  --accent: #e07a2e;
  --ok: #2f8f5b;
  --err: #c8453a;
  --text: #2a2a2a;
  --muted: #7a7366;
  --card: #ffffff;
  --shadow: 0 2px 10px rgba(60, 50, 30, .10);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--paper);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
  overscroll-behavior: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
}

.screen {
  height: 100%;
  display: flex;
  flex-direction: column;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}
.screen[hidden] { display: none; }

/* ---- Barre du haut ---- */
.bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 16px;
  min-height: 80px;
}
.title {
  margin: 0;
  font-size: 28px;
  font-weight: 600;
  color: var(--ink);
}
#home .bar { justify-content: center; }
.nav, .tools { display: flex; align-items: center; gap: 10px; }
.current {
  min-width: 64px;
  text-align: center;
  font-size: 40px;
  font-weight: 500;
  color: var(--ink);
}

.btn {
  width: 60px;
  height: 60px;
  flex: none;
  display: inline-grid;
  place-items: center;
  border: none;
  border-radius: 16px;
  background: var(--card);
  box-shadow: var(--shadow);
  color: var(--ink);
  cursor: pointer;
  text-decoration: none;
}
.btn:active { transform: scale(.95); }
.btn:disabled { opacity: .3; }
.btn svg {
  width: 30px;
  height: 30px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.btn.big { width: 84px; height: 84px; border-radius: 22px; }
.btn.big svg { width: 40px; height: 40px; }
.btn.primary { background: var(--ok); color: #fff; }
.fun-toggle[aria-pressed="true"] {
  background: linear-gradient(135deg, #ff5f8f, #ffb347, #4fd18b, #4f8cff, #b16cff);
  background-size: 300% 300%;
  color: #fff;
  animation: flow 4s linear infinite;
}
@keyframes flow {
  50% { background-position: 100% 100%; }
}
.btn.pulse { animation: pulse .9s ease-in-out 2; }
.btn-space { width: 60px; }

@keyframes pulse {
  50% { transform: scale(1.12); box-shadow: 0 0 0 8px rgba(224, 122, 46, .25); }
}

/* ---- Accueil ---- */
.sets {
  flex: 1;
  display: flex;
  flex-wrap: wrap;
  align-content: center;
  justify-content: center;
  gap: 24px;
  padding: 16px;
}
.set {
  width: 260px;
  padding: 28px 16px 20px;
  border-radius: 24px;
  background: var(--card);
  box-shadow: var(--shadow);
  text-decoration: none;
  color: var(--ink);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
}
.set:active { transform: scale(.97); }
.set canvas { width: 220px; height: 110px; }
.set .label { font-size: 22px; font-weight: 600; }
.set-test { box-shadow: var(--shadow), inset 0 0 0 3px var(--accent); }
.set-icon {
  width: 220px;
  height: 110px;
  display: grid;
  place-items: center;
  color: var(--accent);
}
.set-icon svg { width: 76px; height: 76px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.set .count { font-size: 17px; color: var(--muted); }

/* ---- Grille des lettres ---- */
.grid {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(110px, 1fr));
  gap: 16px;
  padding: 8px 20px 24px;
  align-content: start;
}
.tile {
  position: relative;
  aspect-ratio: 1;
  border-radius: 20px;
  background: var(--card);
  box-shadow: var(--shadow);
  display: grid;
  place-items: center;
  overflow: hidden;
}
.tile:active { transform: scale(.95); }
.tile canvas { width: 100%; height: 100%; }
.tile.done::after {
  content: "";
  position: absolute;
  top: 8px;
  right: 8px;
  width: 26px;
  height: 26px;
  border-radius: 50%;
  background: var(--ok) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M6 12.5l4 4L18 8' fill='none' stroke='white' stroke-width='3' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E") center / 20px no-repeat;
}

.tile.mastered::before {
  content: "";
  position: absolute;
  top: 8px;
  left: 8px;
  width: 26px;
  height: 26px;
  border-radius: 50%;
  background: var(--accent) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M12 4.5l2.2 4.6 5 .6-3.7 3.5 1 5L12 15.8l-4.5 2.4 1-5-3.7-3.5 5-.6z' fill='white'/%3E%3C/svg%3E") center / 20px no-repeat;
}

/* ---- Tracé ---- */
.board {
  position: relative;
  flex: 1;
  margin: 0 16px 16px;
  border-radius: 24px;
  background: var(--card);
  box-shadow: var(--shadow);
  overflow: hidden;
}
#canvas, #test-canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  touch-action: none;
}
.win {
  position: absolute;
  right: 20px;
  top: 50%;
  transform: translateY(-50%);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 20px;
  padding: 20px 14px;
  border-radius: 28px;
  background: rgba(255, 255, 255, .95);
  box-shadow: 0 6px 24px rgba(60, 50, 30, .18);
  animation: rise .35s ease-out;
}
.win[hidden] { display: none; }
.win-check {
  width: 64px;
  height: 64px;
  border-radius: 50%;
  background: var(--ok);
  display: grid;
  place-items: center;
}
.win-check svg { width: 40px; height: 40px; fill: none; stroke: #fff; stroke-width: 3; stroke-linecap: round; stroke-linejoin: round; }

@keyframes rise {
  from { transform: translate(30px, -50%); opacity: 0; }
}

/* ---- Test ---- */
.test-btn { color: var(--accent); }
.dots { display: flex; gap: 12px; }
.dots span {
  width: 24px;
  height: 24px;
  border-radius: 50%;
  border: 3px solid var(--line);
  background: var(--card);
}
.dots span.cur { border-color: var(--accent); box-shadow: 0 0 0 4px rgba(224, 122, 46, .2); }
.dots span.pass { border-color: var(--ok); background: var(--ok); }
.dots span.miss { border-color: var(--line); background: var(--road); }

.result {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  background: rgba(250, 248, 243, .85);
  animation: appear .3s ease-out;
}
.result[hidden] { display: none; }
.result-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 28px;
  padding: 28px;
  border-radius: 28px;
  background: var(--card);
  box-shadow: 0 6px 24px rgba(60, 50, 30, .18);
}
.result-row { display: flex; flex-wrap: wrap; justify-content: center; gap: 16px; }
.result-item { display: flex; flex-direction: column; align-items: center; gap: 10px; }
.result-item canvas { width: 96px; height: 96px; border-radius: 16px; background: var(--paper); }
.result-item .mark {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  border: 3px solid var(--line);
}
.result-item.pass .mark {
  border-color: var(--ok);
  background: var(--ok) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M6 12.5l4 4L18 8' fill='none' stroke='white' stroke-width='3' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E") center / 24px no-repeat;
}
.result-buttons { display: flex; gap: 24px; }
@keyframes appear { from { opacity: 0; } }

@media (max-width: 600px) {
  .btn { width: 48px; height: 48px; border-radius: 12px; }
  .btn svg { width: 24px; height: 24px; }
  .bar { gap: 6px; padding: 8px; min-height: 64px; }
  .nav, .tools { gap: 6px; }
  .current { min-width: 40px; font-size: 30px; }
  .board { margin: 0 8px 8px; }
  .dots { gap: 6px; }
  .dots span { width: 16px; height: 16px; border-width: 2px; }
  .result-item canvas { width: 64px; height: 64px; }
}
