* { box-sizing: border-box; }

:root {
  --bg: #f5efe1;
  --paper: #fffdf8;
  --ink: #2f382f;
  --muted: #73776f;
  --accent: #ef8c2f;
  --accent-dark: #c66b17;
  --leaf: #5d965f;
  --grid: #ded6c4;
  --wall: #7d6549;
  --mobile: #d8943f;
  --water: #67afd0;
  --shadow: 0 14px 40px rgba(68, 56, 35, .12);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

html, body { margin: 0; min-height: 100%; }
body {
  font-family: Inter, ui-rounded, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  color: var(--ink);
  background:
    radial-gradient(circle at 15% 0%, rgba(255,255,255,.85), transparent 35%),
    linear-gradient(160deg, #f7eddb, #edf3df 60%, #e7f1ee);
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: none;
}

button { font: inherit; touch-action: manipulation; }

.app-shell {
  width: min(760px, 100%);
  margin: 0 auto;
  padding: max(12px, env(safe-area-inset-top)) 12px calc(24px + var(--safe-bottom));
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
}

.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.brand-ant {
  width: 46px; height: 46px; display: grid; place-items: center;
  border-radius: 15px; background: #fff8e7; box-shadow: 0 7px 18px rgba(80,65,40,.12);
  font-size: 28px;
}
.brand h1 { margin: 0; font-size: clamp(1.2rem, 5vw, 1.65rem); line-height: 1.05; }
.brand p { margin: 4px 0 0; color: var(--muted); font-size: .82rem; }

.icon-btn {
  width: 44px; height: 44px; flex: 0 0 44px; border: 0; border-radius: 14px;
  background: rgba(255,255,255,.8); color: var(--ink); font-size: 1.35rem; box-shadow: 0 5px 15px rgba(70,55,30,.08);
}

.game-card, .panel-card, .progress-card {
  background: rgba(255,253,248,.94);
  border: 1px solid rgba(100,85,55,.09);
  box-shadow: var(--shadow);
}
.game-card { border-radius: 24px; padding: 16px; }

.level-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; }
.eyebrow { font-size: .72rem; font-weight: 900; color: var(--accent-dark); letter-spacing: .09em; text-transform: uppercase; }
.level-head h2 { margin: 4px 0 0; font-size: 1.35rem; }
.score-pill { display: flex; align-items: baseline; gap: 2px; white-space: nowrap; background: #f6f0df; border-radius: 999px; padding: 8px 10px; font-size: .8rem; }
.score-pill strong { font-size: 1.15rem; }
.level-description { margin: 8px 0 12px; color: var(--muted); font-size: .9rem; line-height: 1.4; }

.phase-banner {
  display: flex; align-items: center; gap: 10px; padding: 10px 12px; margin-bottom: 12px;
  border-radius: 15px; background: #fff4d8; border: 1px solid #efd8a5;
}
.phase-banner > span { font-size: 1.35rem; }
.phase-banner strong, .phase-banner small { display: block; }
.phase-banner strong { font-size: .9rem; }
.phase-banner small { margin-top: 2px; color: #796d57; font-size: .74rem; }
.phase-banner.running { background: #eaf5e6; border-color: #c9e0c1; }

.board-wrap { position: relative; width: 100%; }
.trail-canvas {
  position: absolute; inset: 0; width: 100%; height: 100%; z-index: 4;
  pointer-events: none; border-radius: 16px;
}
.board {
  width: 100%; aspect-ratio: 1; display: grid; overflow: hidden;
  border-radius: 16px; border: 3px solid #745d3f; background: #eee7d4;
  box-shadow: inset 0 0 0 1px rgba(255,255,255,.5);
  touch-action: manipulation;
}

.cell {
  position: relative; display: grid; place-items: center; min-width: 0; min-height: 0;
  border-right: 1px solid var(--grid); border-bottom: 1px solid var(--grid);
  background: #f8f2df; user-select: none;
}
.cell.floor:nth-child(even) { background: #f3eedc; }
.cell.wall { background: repeating-linear-gradient(45deg, rgba(255,255,255,.05) 0 5px, rgba(0,0,0,.05) 5px 10px), var(--wall); }
.cell.mobile-wall {
  cursor: pointer; background: linear-gradient(135deg, #e5aa5a, var(--mobile));
  box-shadow: inset 0 0 0 2px rgba(255,255,255,.26);
}
.cell.mobile-wall::after { content: "↔"; color: #fff8e9; font-size: clamp(14px, 5vw, 24px); font-weight: 900; }
.cell.mobile-wall.selected { outline: 4px solid #ffe070; outline-offset: -4px; animation: selectPulse .9s infinite alternate; }
@keyframes selectPulse { to { filter: brightness(1.1); } }
.cell.move-target::before {
  content: ""; width: 42%; aspect-ratio: 1; border-radius: 50%; background: rgba(239,140,47,.28); border: 2px dashed var(--accent);
}
.cell.rock { background: #d9d9d2; }
.cell.rock::after { content: "🪨"; font-size: clamp(15px, 5vw, 29px); }
.cell.water { background: linear-gradient(135deg, #82cbe5, #4e9bc0); }
.cell.water::after { content: "≋"; color: white; font-weight: 900; font-size: clamp(18px, 6vw, 32px); }
.cell.tool-pickaxe::after { content: "⛏️"; font-size: clamp(15px, 5vw, 28px); }
.cell.tool-raft::after { content: "🛶"; font-size: clamp(15px, 5vw, 28px); }
.cell.teleporter { background: radial-gradient(circle, #e3c9ff 0 24%, #8a66cb 25% 50%, #553785 51% 100%); }
.cell.teleporter::after { content: "✦"; color: white; font-size: clamp(14px, 4vw, 24px); }
.cell.start { background: linear-gradient(135deg, #ffd477, #e3a143); }
.cell.start::before { content: "🚪"; font-size: clamp(16px, 5vw, 28px); }
.cell.goal { background: linear-gradient(135deg, #a7d872, #64a344); }
.cell.goal::before { content: "🏁"; font-size: clamp(16px, 5vw, 28px); }

/* Fourmis V3 fluide : couche independante + silhouette vue du dessus. */
.ant-layer {
  position: absolute;
  inset: 0;
  z-index: 7;
  pointer-events: none;
  overflow: hidden;
  border-radius: 16px;
}

.ant-sprite {
  position: absolute;
  transform: translate(-50%, -50%);
  transform-origin: center;
  will-change: left, top;
  transition: opacity .16s ease;
  filter: drop-shadow(0 2px 1px rgba(65, 43, 24, .18));
}

.ant-sprite.hidden { opacity: 0; }

.ant-shape {
  position: relative;
  width: 100%;
  height: 100%;
  transform: rotate(var(--ant-angle, 0deg));
  transform-origin: 50% 50%;
  transition: transform .16s ease-out;
}

.ant-part,
.ant-leg,
.ant-antenna {
  position: absolute;
  display: block;
  background: #493226;
}

.ant-head {
  z-index: 4;
  width: 34%;
  height: 25%;
  left: 33%;
  top: 4%;
  border-radius: 52% 52% 45% 45%;
  background: radial-gradient(circle at 35% 36%, #77523c 0 10%, transparent 11%), #493226;
}

.ant-thorax {
  z-index: 4;
  width: 27%;
  height: 22%;
  left: 36.5%;
  top: 34%;
  border-radius: 48%;
}

.ant-abdomen {
  z-index: 4;
  width: 42%;
  height: 36%;
  left: 29%;
  top: 58%;
  border-radius: 48% 48% 55% 55%;
  background: linear-gradient(180deg, #53382a, #3e2a21);
}

.ant-leg {
  z-index: 2;
  width: 43%;
  height: 5%;
  border-radius: 999px;
  transform-origin: 100% 50%;
}

.leg-l1 { left: 3%; top: 35%; transform: rotate(-24deg); }
.leg-r1 { right: 3%; top: 35%; transform-origin: 0 50%; transform: rotate(24deg); }
.leg-l2 { left: 1%; top: 48%; transform: rotate(0deg); }
.leg-r2 { right: 1%; top: 48%; transform-origin: 0 50%; transform: rotate(0deg); }
.leg-l3 { left: 5%; top: 60%; transform: rotate(28deg); }
.leg-r3 { right: 5%; top: 60%; transform-origin: 0 50%; transform: rotate(-28deg); }

.ant-antenna {
  z-index: 1;
  width: 30%;
  height: 3.5%;
  top: 2%;
  border-radius: 999px;
}
.antenna-l { left: 18%; transform: rotate(-52deg); transform-origin: 100% 50%; }
.antenna-r { right: 18%; transform: rotate(52deg); transform-origin: 0 50%; }

/* Petit mouvement organique sans perturber l'axe de marche. */
.ant-sprite:not(.hidden) .ant-abdomen { animation: antBob .42s ease-in-out infinite alternate; }
@keyframes antBob {
  from { transform: translateY(-1%); }
  to { transform: translateY(3%); }
}

.mobile-actions { display: grid; grid-template-columns: minmax(0, 1fr) 52px auto; gap: 9px; margin-top: 12px; }
.trail-toggle { min-width: 84px; padding-left: 10px; padding-right: 10px; font-size: .8rem; }
.btn { border: 0; border-radius: 15px; min-height: 50px; padding: 12px 15px; font-weight: 850; cursor: pointer; }
.btn.primary { color: white; background: linear-gradient(180deg, #f49a3f, #df7c24); box-shadow: 0 9px 20px rgba(224,124,36,.25); }
.btn.primary:disabled { filter: grayscale(.8); opacity: .45; box-shadow: none; }
.btn.secondary { background: #f1ecdf; color: var(--ink); }
.btn.wide { width: 100%; }
.launch { font-size: 1rem; }

.info-stack { display: grid; gap: 10px; margin-top: 12px; }
.panel-card, .progress-card { border-radius: 18px; padding: 14px 15px; }
.panel-card summary { cursor: pointer; font-weight: 900; list-style: none; }
.panel-card summary::-webkit-details-marker { display: none; }
.panel-card summary::after { content: "+"; float: right; color: var(--muted); }
.panel-card[open] summary::after { content: "−"; }
.panel-card p { margin: 10px 0 0; color: var(--muted); font-size: .88rem; line-height: 1.5; }
.legend { display: grid; gap: 8px; margin-top: 10px; }
.legend-row { display: flex; align-items: center; gap: 9px; color: var(--muted); font-size: .85rem; }
.legend-icon { width: 34px; height: 34px; flex: 0 0 34px; display: grid; place-items: center; border-radius: 10px; background: #f4efe3; }
.rules { margin: 10px 0 0; padding-left: 18px; color: var(--muted); font-size: .85rem; line-height: 1.5; }
.rules li + li { margin-top: 5px; }
.progress-top { display: flex; justify-content: space-between; color: var(--muted); font-size: .78rem; font-weight: 800; }
.progress-line { height: 8px; margin-top: 9px; border-radius: 999px; overflow: hidden; background: #e5dfd1; }
.progress-line span { display: block; width: 0; height: 100%; background: linear-gradient(90deg, #5d965f, #8abd68); transition: width .25s; }

.message-overlay { position: absolute; inset: 0; z-index: 20; display: grid; place-items: center; background: rgba(45,41,33,.42); backdrop-filter: blur(3px); border-radius: 16px; }
.message-overlay.hidden { display: none; }
.message-card { width: min(88%, 380px); padding: 24px; border-radius: 22px; background: white; text-align: center; box-shadow: 0 18px 45px rgba(0,0,0,.2); }
.message-icon { font-size: 2.8rem; }
.message-card h3 { margin: 7px 0; font-size: 1.5rem; }
.message-card p { margin: 0; color: var(--muted); line-height: 1.45; }
.message-card .btn { margin-top: 14px; }

@media (min-width: 700px) {
  .app-shell { padding-top: 24px; }
  .game-card { padding: 22px; }
  .board-wrap { max-width: 620px; margin: 0 auto; }
  .info-stack { grid-template-columns: 1fr 1fr; }
  .progress-card { grid-column: 1 / -1; }
}

/* --- V3 : dangers, regeneration et informations compactes mobile --- */
.score-group {
  display: flex;
  align-items: center;
  gap: 6px;
}

.regen-pill {
  min-width: 42px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 3px;
  padding: 8px 9px;
  border-radius: 999px;
  background: #ece8df;
  color: #858079;
  font-size: .78rem;
  transition: transform .18s ease, background .18s ease, color .18s ease;
}

.regen-pill strong { font-size: 1rem; }
.regen-pill.active {
  color: #9d4a35;
  background: #ffe8df;
  transform: scale(1.04);
}

.phase-banner.danger {
  background: #fff0e7;
  border-color: #efc5b5;
}

.cell.fire {
  overflow: hidden;
  background:
    radial-gradient(circle at 35% 75%, rgba(255,231,119,.95) 0 11%, transparent 12%),
    radial-gradient(circle at 62% 62%, rgba(255,136,46,.95) 0 18%, transparent 19%),
    linear-gradient(145deg, #6e3c2e, #a8442e);
}

.cell.fire::after {
  content: "🔥";
  position: relative;
  z-index: 1;
  font-size: clamp(16px, 5vw, 29px);
  animation: flamePulse .55s ease-in-out infinite alternate;
}

@keyframes flamePulse {
  from { transform: translateY(1px) scale(.94); filter: saturate(.95); }
  to { transform: translateY(-1px) scale(1.06); filter: saturate(1.18); }
}

.cell.tool-firecoat::after {
  content: "🧯";
  font-size: clamp(15px, 5vw, 28px);
}

.danger-fx,
.respawn-fx {
  position: absolute;
  inset: 0;
  z-index: 18;
  display: grid;
  place-items: center;
  pointer-events: none;
  font-size: clamp(20px, 7vw, 36px);
  animation: dangerPop .62s ease-out forwards;
}

.water-fx { background: rgba(60,160,210,.18); }
.fire-fx { background: rgba(224,85,35,.18); }
.respawn-fx { background: rgba(255,228,125,.16); }

@keyframes dangerPop {
  0% { opacity: 0; transform: scale(.4); }
  35% { opacity: 1; transform: scale(1.22); }
  100% { opacity: 0; transform: scale(1.55); }
}

@media (max-width: 390px) {
  .game-card { padding: 12px; }
  .level-head h2 { font-size: 1.18rem; }
  .score-pill { padding: 7px 8px; }
  .regen-pill { min-width: 38px; padding: 7px; }
  .phase-banner { padding: 9px 10px; }
}

@media (max-width: 390px) {
  .mobile-actions { grid-template-columns: minmax(0, 1fr) 50px; }
  .trail-toggle { grid-column: 1 / -1; min-height: 42px; }
}

/* --- V7 : fourmis visibles et mouvement continu --- */
.ant-layer { z-index: 9; }
.ant-sprite {
  z-index: 2;
  opacity: 1;
  min-width: 15px;
  min-height: 21px;
  filter: drop-shadow(0 1px 0 rgba(255,255,255,.45)) drop-shadow(0 2px 2px rgba(52,34,22,.32));
}
.ant-sprite.hidden { opacity: 0; visibility: hidden; }
.ant-shape { transition: transform .09s linear; }
.ant-part, .ant-leg, .ant-antenna { background: #2f211a; }
.ant-head { background: radial-gradient(circle at 36% 34%, #8b654c 0 10%, transparent 11%), #302119; }
.ant-thorax { background: #37251d; }
.ant-abdomen { background: linear-gradient(180deg, #4b3125, #251914); }
