:root {
  --bg: #f6ead7;
  --bg-deep: #efdcc0;
  --panel: #fffaf1;
  --ink: #3b2a1e;
  --ink-soft: #7a6150;
  --line: #eadbc6;
  --cookie: #e9a955;
  --cookie-dark: #b9712c;
  --choco: #5a3520;
  --violet: #8e4fd6;
  --ok: #5f9a3e;
  --ko: #c4573f;
  --radius: 16px;
  --shadow: 0 1px 0 rgba(255, 255, 255, 0.7) inset, 0 10px 30px -18px rgba(90, 53, 32, 0.45);
  --font-title: 'Fredoka', 'Trebuchet MS', system-ui, sans-serif;
  --font-body: 'Nunito', system-ui, -apple-system, 'Segoe UI', sans-serif;
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  min-height: 100vh;
  color: var(--ink);
  font-family: var(--font-body);
  font-size: 15px;
  line-height: 1.45;
  background:
    radial-gradient(circle at 15% 10%, rgba(255, 255, 255, 0.6), transparent 40%),
    radial-gradient(circle at 85% 90%, rgba(233, 169, 85, 0.18), transparent 45%),
    var(--bg);
}

button, input { font: inherit; color: inherit; }

/* ---------- En-tête ---------- */

.site-header {
  max-width: 1400px;
  margin: 0 auto;
  padding: 16px 16px 4px;
}
.brand {
  display: inline-flex;
  align-items: center;
  gap: 12px;
  color: inherit;
  text-decoration: none;
}
.brand img { display: block; filter: drop-shadow(0 4px 6px rgba(90, 53, 32, 0.25)); }
.brand-name {
  display: block;
  font-family: var(--font-title);
  font-weight: 700;
  font-size: 28px;
  line-height: 1;
  letter-spacing: 0.2px;
  color: var(--choco);
}
.brand-tag { display: block; font-size: 13px; color: var(--ink-soft); }

/* ---------- Mise en page ---------- */

.layout {
  max-width: 1400px;
  margin: 0 auto;
  padding: 12px 16px 24px;
  display: grid;
  grid-template-columns: minmax(0, 1fr) 340px;
  gap: 20px;
  align-items: start;
}

.stage, .panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.stage { padding: 14px; min-width: 0; }
.panel { padding: 16px; position: sticky; top: 16px; }

/* ---------- Contrôles ---------- */

.controls {
  display: flex;
  flex-wrap: wrap;
  gap: 12px 20px;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
}
.control.ants { display: grid; gap: 2px; min-width: 200px; flex: 1 1 220px; max-width: 360px; font-weight: 600; }
.control.ants output {
  display: inline-block;
  min-width: 2.2em;
  margin-left: 6px;
  padding: 0 8px;
  border-radius: 999px;
  background: var(--bg-deep);
  text-align: center;
  font-variant-numeric: tabular-nums;
}
input[type='range'] { width: 100%; accent-color: var(--cookie-dark); }
input[type='range']:disabled { opacity: 0.5; }

.control-group { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }

.btn, .segmented button {
  border: 1px solid var(--cookie-dark);
  background: var(--cookie);
  color: var(--choco);
  font-weight: 700;
  border-radius: 999px;
  padding: 7px 16px;
  cursor: pointer;
  box-shadow: 0 2px 0 var(--cookie-dark);
  transition: transform 0.08s ease, background 0.15s ease;
}
.btn:hover:not(:disabled) { background: #f0b866; }
.btn:active:not(:disabled) { transform: translateY(2px); box-shadow: none; }
.btn:disabled { opacity: 0.45; cursor: not-allowed; }
.btn-ghost { background: transparent; border-color: var(--line); box-shadow: 0 2px 0 var(--line); }
.btn-ghost:hover:not(:disabled) { background: var(--bg-deep); }

.seg { display: inline-flex; align-items: center; gap: 6px; }
.seg-label { font-size: 12px; font-weight: 700; color: var(--ink-soft); }

.segmented { display: inline-flex; border-radius: 999px; box-shadow: 0 2px 0 var(--line); }
.segmented button {
  border-radius: 0;
  border-color: var(--line);
  border-left-width: 0;
  background: var(--panel);
  box-shadow: none;
  padding: 7px 12px;
  font-variant-numeric: tabular-nums;
}
.segmented button:first-child { border-left-width: 1px; border-radius: 999px 0 0 999px; }
.segmented button:last-child { border-radius: 0 999px 999px 0; }
.segmented button[aria-pressed='true'] { background: var(--choco); color: #fff3dc; border-color: var(--choco); }

button:focus-visible, input:focus-visible { outline: 3px solid rgba(142, 79, 214, 0.45); outline-offset: 2px; }

/* ---------- Carte ---------- */

.world-wrap {
  position: relative;
  display: flex;
  justify-content: center;
  border-radius: 12px;
  background: radial-gradient(ellipse at 50% 40%, #fbf1e1, var(--bg-deep));
  overflow: hidden;
  touch-action: manipulation;
}
#world { display: block; max-width: 100%; }
#world.placing { cursor: crosshair; }
#world.pannable { cursor: grab; touch-action: none; }
#world.pannable.placing { cursor: crosshair; }
#world.dragging, #world.dragging.placing { cursor: grabbing; }

.recenter {
  position: absolute;
  right: 12px;
  bottom: 12px;
  z-index: 1;
  padding: 6px 12px;
  font-size: 13px;
}
.recenter[hidden] { display: none; }

.hint {
  position: absolute;
  left: 50%;
  top: 12px;
  transform: translateX(-50%);
  width: max-content;
  max-width: calc(100% - 24px);
  margin: 0;
  padding: 8px 14px;
  border-radius: 999px;
  background: rgba(59, 42, 30, 0.85);
  color: #fff3dc;
  font-size: 13px;
  font-weight: 600;
  text-align: center;
  pointer-events: none;
}
.hint.shake { animation: shake 0.35s ease; }
@keyframes shake {
  25% { transform: translateX(calc(-50% - 6px)); }
  50% { transform: translateX(calc(-50% + 6px)); }
  75% { transform: translateX(calc(-50% - 3px)); }
}

.banner {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  display: grid;
  gap: 8px;
  justify-items: center;
  width: min(360px, calc(100% - 32px));
  padding: 20px;
  border-radius: var(--radius);
  background: var(--panel);
  border: 2px solid var(--cookie);
  box-shadow: 0 20px 40px -20px rgba(59, 42, 30, 0.6);
  text-align: center;
  animation: pop 0.35s cubic-bezier(0.2, 1.4, 0.4, 1);
}
.banner[hidden] { display: none; }
.banner strong { font-family: var(--font-title); font-size: 22px; color: var(--choco); }
@keyframes pop { from { transform: translate(-50%, -50%) scale(0.85); opacity: 0; } }

.legend {
  display: flex;
  flex-wrap: wrap;
  gap: 6px 18px;
  margin: 10px 4px 0;
  padding: 0;
  list-style: none;
  font-size: 13px;
  color: var(--ink-soft);
}
.legend li { display: inline-flex; align-items: center; gap: 6px; }
.dot { display: inline-block; width: 12px; height: 12px; border-radius: 50%; }
.dot.ant { background: #2a1a12; }
.dot.tired { background: #7a3424; }
.dot.route { background: #4b2d6e; }
.dot.scent { background: #24485a; }
.dot.crumb { background: var(--cookie); border: 1px solid var(--cookie-dark); }
.dot.phero { background: rgba(142, 79, 214, 0.6); border-radius: 3px; transform: rotate(45deg) scale(0.85); }

/* ---------- Panneau ---------- */

.panel h2 {
  margin: 0 0 10px;
  font-family: var(--font-title);
  font-weight: 600;
  font-size: 18px;
  color: var(--choco);
}
.panel h2:not(:first-child) { margin-top: 20px; }

.progress {
  height: 10px;
  border-radius: 999px;
  background: var(--bg-deep);
  overflow: hidden;
  margin-bottom: 12px;
}
.progress span {
  display: block;
  height: 100%;
  width: 0;
  background: linear-gradient(90deg, var(--cookie), var(--violet));
  transition: width 0.3s ease;
}

.stat-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin: 0;
}
.stat-grid > div {
  padding: 8px 10px;
  border-radius: 10px;
  background: #fdf3e3;
  border: 1px solid var(--line);
}
.stat-grid dt { font-size: 12px; color: var(--ink-soft); }
.stat-grid dd {
  margin: 0;
  font-family: var(--font-title);
  font-size: 20px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}
.stat-grid dd small { font-size: 13px; color: var(--ink-soft); font-weight: 500; }

.chart { margin: 14px 0 0; }
.chart figcaption { font-size: 13px; font-weight: 700; color: var(--ink-soft); margin-bottom: 4px; }
.chart-box { position: relative; height: 130px; }
.chart-box.tall { height: 230px; }
.chart-fallback { font-size: 13px; color: var(--ink-soft); font-style: italic; }

.leaderboard { width: 100%; border-collapse: collapse; font-size: 13px; font-variant-numeric: tabular-nums; }
.leaderboard th { text-align: left; font-size: 11px; font-weight: 700; color: var(--ink-soft); padding: 0 6px 4px; }
.leaderboard td { padding: 5px 6px; border-top: 1px solid var(--line); }
.leaderboard th:nth-child(n + 3), .leaderboard td:nth-child(n + 3) { text-align: right; }
.leaderboard tbody tr:not(.empty) { cursor: default; }
.leaderboard tbody tr:not(.empty):hover, .leaderboard tbody tr.active { background: #fdf3e3; }
.leaderboard tr.empty td { color: var(--ink-soft); font-style: italic; }
.leaderboard .rank { color: var(--ink-soft); width: 2em; }
.leaderboard .medal-1 .rank, .leaderboard .medal-2 .rank, .leaderboard .medal-3 .rank { font-weight: 700; color: var(--cookie-dark); }

.ant-tip {
  position: absolute;
  z-index: 2;
  min-width: 190px;
  padding: 10px 12px;
  border-radius: 12px;
  background: rgba(59, 42, 30, 0.94);
  color: #fff3dc;
  font-size: 12px;
  line-height: 1.35;
  pointer-events: none;
  transform: translate(-50%, calc(-100% - 14px));
  box-shadow: 0 10px 24px -12px rgba(0, 0, 0, 0.6);
}
.ant-tip[hidden] { display: none; }
.ant-tip::after {
  content: '';
  position: absolute;
  left: 50%;
  bottom: -6px;
  width: 12px;
  height: 12px;
  background: inherit;
  transform: translateX(-50%) rotate(45deg);
}
.ant-tip.below { transform: translate(-50%, 14px); }
.ant-tip.below::after { top: -6px; bottom: auto; }
.ant-tip strong { font-family: var(--font-title); font-size: 15px; font-weight: 600; }
.tip-mode { display: block; margin-bottom: 6px; color: #e9c79a; }
.ant-tip dl { display: grid; grid-template-columns: auto auto; gap: 1px 12px; margin: 0; }
.ant-tip dt { opacity: 0.75; }
.ant-tip dd { margin: 0; text-align: right; font-variant-numeric: tabular-nums; font-weight: 700; }
.tip-energy { height: 5px; margin-top: 6px; border-radius: 999px; background: rgba(255, 243, 220, 0.2); overflow: hidden; }
.tip-energy span { display: block; height: 100%; background: var(--cookie); }
#world.over-ant { cursor: pointer; }

.history { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; }
.history li {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 2px 8px;
  align-items: center;
  padding: 8px 10px;
  border-radius: 10px;
  border: 1px solid var(--line);
  font-size: 13px;
}
.history li.empty { display: block; color: var(--ink-soft); font-style: italic; border-style: dashed; }
.history time { color: var(--ink-soft); text-align: right; }
.history .meta { grid-column: 1 / -1; font-variant-numeric: tabular-nums; }
.badge {
  justify-self: start;
  padding: 1px 8px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.4px;
}
.badge.terminee { background: rgba(95, 154, 62, 0.16); color: var(--ok); }
.badge.abandonnee { background: rgba(196, 87, 63, 0.14); color: var(--ko); }

.site-footer {
  max-width: 1400px;
  margin: 0 auto;
  padding: 0 16px 24px;
  font-size: 13px;
  color: var(--ink-soft);
  text-align: center;
}

/* ---------- Petits écrans : le panneau passe sous la carte ---------- */

@media (max-width: 960px) {
  .layout { grid-template-columns: minmax(0, 1fr); }
  .panel { position: static; }
}
@media (max-width: 520px) {
  .brand-name { font-size: 22px; }
  .brand img { width: 44px; height: 44px; }
  .stage { padding: 10px; }
  .hint { font-size: 12px; border-radius: 12px; }
}
