:root {
  --encre: #2b2118;
  --papier: #fff6e4;
  --sable: #f1d9a6;
  --accent: #e8743b;
  --ombre: rgba(43, 33, 24, 0.35);
}
* { box-sizing: border-box; }
html, body {
  margin: 0; height: 100%; overflow: hidden; background: #7ec8e3;
  font-family: "Trebuchet MS", "Segoe UI", system-ui, sans-serif; color: var(--encre);
  touch-action: none; user-select: none; -webkit-user-select: none;
}
canvas { display: block; }
#chargement {
  position: fixed; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 18px; background: linear-gradient(#7ec8e3, #f1d9a6); z-index: 10; transition: opacity .5s;
}
#chargement.fini { opacity: 0; pointer-events: none; }
#chargement .titre { font-size: clamp(28px, 7vw, 56px); font-weight: 800; color: var(--papier);
  text-shadow: 0 3px 0 var(--accent), 0 6px 14px var(--ombre); }
#chargement .barre { width: min(320px, 70vw); height: 12px; border-radius: 6px; background: rgba(255,255,255,.5); overflow: hidden; }
#progression { height: 100%; width: 0; background: var(--accent); transition: width .2s; }
#etape-chargement { font-size: 14px; opacity: .8; }
#hud { position: fixed; inset: 0; pointer-events: none; z-index: 4; }
#hud .info { position: absolute; top: max(10px, env(safe-area-inset-top)); right: 12px; font-size: 12px;
  background: rgba(255,246,228,.75); padding: 4px 8px; border-radius: 8px; }
#zone-joystick { position: fixed; left: 0; bottom: 0; width: 50%; height: 60%; z-index: 3; }
#joystick { position: absolute; width: 120px; height: 120px; margin: -60px 0 0 -60px; border-radius: 50%;
  background: rgba(255,246,228,.25); border: 2px solid rgba(255,246,228,.6); display: none; }
#manette { position: absolute; left: 50%; top: 50%; width: 52px; height: 52px; margin: -26px 0 0 -26px;
  border-radius: 50%; background: rgba(255,246,228,.85); box-shadow: 0 2px 6px var(--ombre); }
#hud .horloge { position: absolute; top: max(10px, env(safe-area-inset-top)); left: 12px; display: flex; gap: 8px;
  align-items: center; background: rgba(255,246,228,.85); padding: 6px 12px; border-radius: 14px;
  font-weight: 700; box-shadow: 0 2px 8px var(--ombre); }
#hud .horloge .astre { font-size: 18px; color: var(--accent); }
#hud .horloge .heure { font-variant-numeric: tabular-nums; opacity: .75; }
#hud .info { top: auto; bottom: 8px; right: 8px; opacity: .6; }
#hud .inventaire { position: absolute; left: 50%; transform: translateX(-50%); bottom: max(10px, env(safe-area-inset-bottom));
  display: flex; gap: 6px; max-width: calc(100vw - 260px); overflow-x: auto; padding: 6px;
  background: rgba(43,33,24,.35); border-radius: 14px; }
#hud .inventaire:empty { display: none; }
#hud .case { position: relative; width: 48px; height: 48px; flex: none; border-radius: 10px; background: rgba(255,246,228,.9); }
#hud .case img { width: 100%; height: 100%; padding: 4px; }
#hud .case span { position: absolute; right: 3px; bottom: 1px; font-size: 13px; font-weight: 800;
  text-shadow: 0 0 3px #fff, 0 0 3px #fff; }
#hud .case.flash { animation: flash .35s; }
@keyframes flash { 50% { transform: scale(1.15); } }
#hud .message { position: absolute; top: 18%; left: 50%; transform: translate(-50%, -10px); opacity: 0;
  background: rgba(43,33,24,.8); color: var(--papier); padding: 8px 16px; border-radius: 12px; font-weight: 700;
  transition: opacity .25s, transform .25s; text-align: center; max-width: 80vw; }
#hud .message.visible { opacity: 1; transform: translate(-50%, 0); }
#hud .gain { position: absolute; transform: translate(-50%, -100%); font-weight: 800; color: #fff;
  text-shadow: 0 2px 4px rgba(0,0,0,.6); display: flex; align-items: center; gap: 2px; animation: monte 1.4s forwards; }
#hud .gain img { width: 28px; height: 28px; }
@keyframes monte { to { transform: translate(-50%, -260%); opacity: 0; } }
#bouton-action { position: fixed; right: max(18px, env(safe-area-inset-right)); bottom: max(18px, env(safe-area-inset-bottom));
  width: 96px; height: 96px; border-radius: 50%; border: 3px solid var(--papier); background: var(--accent);
  color: var(--papier); font: 700 13px inherit; display: none; flex-direction: column; align-items: center;
  justify-content: center; gap: 2px; box-shadow: 0 4px 12px var(--ombre); z-index: 4; padding: 6px; }
#bouton-action.visible { display: flex; }
#bouton-action.impossible { background: #9c8f80; }
#bouton-action img { width: 44px; height: 44px; }
#bouton-action:active { transform: scale(.94); }
#bouton-fabrication { position: fixed; right: max(18px, env(safe-area-inset-right)); bottom: calc(max(18px, env(safe-area-inset-bottom)) + 108px);
  width: 66px; height: 66px; border-radius: 50%; border: 3px solid var(--papier); background: #6d8f3a; color: var(--papier);
  font: 700 10px inherit; display: flex; flex-direction: column; align-items: center; justify-content: center;
  box-shadow: 0 4px 12px var(--ombre); z-index: 4; padding: 4px; }
#bouton-fabrication img { width: 32px; height: 32px; }
#fabrication { position: fixed; inset: 0; z-index: 6; display: none; align-items: center; justify-content: center;
  background: rgba(43,33,24,.35); }
#fabrication.ouvert { display: flex; }
#fabrication .cadre { width: min(720px, 94vw); max-height: 86vh; display: flex; flex-direction: column;
  background: var(--papier); border-radius: 18px; box-shadow: 0 10px 30px var(--ombre); overflow: hidden; }
#fabrication .entete { display: flex; align-items: center; justify-content: space-between; padding: 10px 12px;
  background: var(--sable); }
#fabrication .onglets { display: flex; gap: 6px; }
#fabrication .onglets button, #fabrication .fermer { border: 0; border-radius: 10px; padding: 8px 14px; font: 700 14px inherit;
  background: rgba(255,255,255,.5); color: var(--encre); cursor: pointer; }
#fabrication .onglets button.actif { background: var(--accent); color: var(--papier); }
#fabrication .liste { overflow-y: auto; padding: 10px; display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 8px; }
#fabrication .carte { display: flex; gap: 10px; align-items: center; padding: 8px; border-radius: 12px; background: #fff; }
#fabrication .carte.impossible { background: #f6efe2; }
#fabrication .carte.verrou { opacity: .55; }
#fabrication .carte .icone { width: 54px; height: 54px; flex: none; }
#fabrication .corps { flex: 1; min-width: 0; }
#fabrication .nom { font-weight: 800; }
#fabrication .nom small { opacity: .6; }
#fabrication .ingrs { display: flex; flex-wrap: wrap; gap: 4px; margin: 3px 0; }
#fabrication .ingr { display: inline-flex; align-items: center; gap: 1px; font-size: 13px; font-weight: 700;
  background: #eaf3df; border-radius: 8px; padding: 1px 6px 1px 1px; }
#fabrication .ingr.manque { background: #f8d8cc; color: #a3361c; }
#fabrication .ingr img { width: 22px; height: 22px; }
#fabrication .etat { font-size: 12px; opacity: .7; }
#fabrication .boutons { display: flex; flex-direction: column; gap: 4px; }
#fabrication .boutons button { border: 0; border-radius: 10px; padding: 8px 10px; background: var(--accent);
  color: var(--papier); font: 700 13px inherit; cursor: pointer; }
#bouton-construction { position: fixed; right: max(18px, env(safe-area-inset-right)); bottom: calc(max(18px, env(safe-area-inset-bottom)) + 184px);
  width: 66px; height: 66px; border-radius: 50%; border: 3px solid var(--papier); background: #3d7ea6; color: var(--papier);
  font: 700 10px inherit; display: flex; flex-direction: column; align-items: center; justify-content: center;
  box-shadow: 0 4px 12px var(--ombre); z-index: 4; padding: 4px; }
#bouton-construction img { width: 32px; height: 32px; }
#bouton-construction.actif { background: var(--accent); }
#construction { position: fixed; left: 50%; transform: translateX(-50%); top: calc(max(10px, env(safe-area-inset-top)) + 44px);
  z-index: 5; display: none; flex-direction: column; gap: 6px; align-items: center; padding: 8px;
  background: rgba(255,246,228,.92); border-radius: 16px; box-shadow: 0 4px 14px var(--ombre); max-width: calc(100vw - 330px); }
#construction.ouvert { display: flex; }
#construction .materiaux, #construction .pieces, #construction .outils { display: flex; gap: 4px; flex-wrap: wrap; justify-content: center; }
#construction button { border: 0; border-radius: 10px; padding: 6px 10px; background: #fff; font: 700 12px inherit;
  color: var(--encre); cursor: pointer; display: flex; align-items: center; gap: 4px; }
#construction button img { width: 22px; height: 22px; }
#construction button.actif { background: var(--accent); color: var(--papier); }
#construction button.verrou { opacity: .45; }
#construction .cout { display: flex; gap: 4px; align-items: center; flex-wrap: wrap; justify-content: center; min-height: 26px; }
#construction .ingr { display: inline-flex; align-items: center; gap: 1px; font-size: 13px; font-weight: 700;
  background: #eaf3df; border-radius: 8px; padding: 1px 6px 1px 1px; }
#construction .ingr.manque { background: #f8d8cc; color: #a3361c; }
#construction .ingr img { width: 22px; height: 22px; }
#construction .raison { font-size: 12px; color: #a3361c; font-weight: 700; }
#hud .alerte { position: absolute; top: max(10px, env(safe-area-inset-top)); left: 50%; transform: translateX(-50%);
  padding: 7px 16px; border-radius: 14px; font-weight: 800; background: rgba(255,246,228,.92); color: var(--encre);
  box-shadow: 0 2px 8px var(--ombre); display: none; white-space: nowrap; }
#hud .alerte.visible { display: block; }
#hud .alerte.danger { background: #c4432b; color: #fff; animation: clignote 1s infinite; }
#hud .alerte.ok { background: #4f8a3c; color: #fff; }
@keyframes clignote { 50% { opacity: .65; } }
#hud .fin { position: absolute; inset: 0; display: none; align-items: center; justify-content: center;
  background: rgba(20,26,34,.55); pointer-events: auto; z-index: 8; }
#hud .fin.visible { display: flex; }
#hud .fin .carte { background: var(--papier); border-radius: 18px; padding: 22px 26px; max-width: min(440px, 88vw); text-align: center;
  box-shadow: 0 10px 30px var(--ombre); }
#hud .fin .titre { font-size: 24px; font-weight: 800; margin-bottom: 8px; }
#hud .fin button { margin-top: 14px; border: 0; border-radius: 12px; padding: 10px 18px; background: var(--accent);
  color: var(--papier); font: 800 15px inherit; cursor: pointer; }
#chargement .choix { display: flex; flex-direction: column; gap: 10px; align-items: center; }
#chargement .choix button { border: 0; border-radius: 14px; padding: 12px 26px; font: 800 17px inherit; cursor: pointer;
  background: var(--accent); color: var(--papier); box-shadow: 0 4px 12px var(--ombre); min-width: 230px; }
#chargement .choix button.secondaire { background: var(--papier); color: var(--encre); }
@media (max-width: 640px) {
  #hud .alerte { top: calc(max(10px, env(safe-area-inset-top)) + 46px); white-space: normal; text-align: center;
    max-width: 92vw; font-size: 13px; }
  #hud .inventaire { left: 8px; right: 8px; transform: none; max-width: none; bottom: auto;
    top: calc(max(10px, env(safe-area-inset-top)) + 88px); justify-content: flex-start; }
  #hud .case { width: 42px; height: 42px; }
  #bouton-construction, #bouton-fabrication { width: 58px; height: 58px; font-size: 8.5px; }
  #bouton-construction img, #bouton-fabrication img { width: 28px; height: 28px; }
  #bouton-fabrication { bottom: calc(max(18px, env(safe-area-inset-bottom)) + 104px); }
  #bouton-construction { bottom: calc(max(18px, env(safe-area-inset-bottom)) + 170px); }
  #bouton-action { width: 88px; height: 88px; font-size: 12px; }
  #construction { left: 8px; right: 8px; transform: none; max-width: none; bottom: auto;
    top: calc(max(10px, env(safe-area-inset-top)) + 146px); }
  #construction button { padding: 5px 7px; font-size: 11px; }
  #hud .message { top: 30%; }
}
#bouton-son { position: fixed; top: max(10px, env(safe-area-inset-top)); right: 12px; z-index: 4; width: 40px; height: 40px;
  border-radius: 50%; border: 0; background: rgba(255,246,228,.85); font-size: 18px; cursor: pointer;
  box-shadow: 0 2px 8px var(--ombre); }
#hud .progression { position: absolute; width: 46px; height: 46px; transform: translate(-50%, -50%) rotate(-90deg);
  display: none; filter: drop-shadow(0 1px 3px rgba(0,0,0,.45)); }
#hud .progression.visible { display: block; }
#hud .progression circle { fill: none; stroke-width: 5; }
#hud .progression .fond { stroke: rgba(255,255,255,.28); }
#hud .progression .barre { stroke: #fff; stroke-linecap: round; stroke-dasharray: 100.5; stroke-dashoffset: 100.5; }
#construction.demolition .materiaux, #construction.demolition .pieces { opacity: .35; pointer-events: none; }
#hud .jauges { position: absolute; top: calc(max(10px, env(safe-area-inset-top)) + 46px); left: 12px; display: flex;
  flex-direction: column; gap: 5px; padding: 7px 10px; border-radius: 12px; background: rgba(255,246,228,.88);
  box-shadow: 0 2px 8px rgba(0,0,0,.15); }
#hud .jauge { display: flex; align-items: center; gap: 6px; }
#hud .jauge .sym { width: 18px; text-align: center; font-size: 14px; line-height: 1; }
#hud .jauge .fond { width: 110px; height: 9px; border-radius: 5px; background: rgba(43,33,24,.18); overflow: hidden; }
#hud .jauge .plein { height: 100%; border-radius: 5px; transition: width .3s; }
#hud .jauge.vie .plein { background: #d9433a; }
#hud .jauge.faim .plein { background: #e8962e; }
#hud .jauge.soif .plein { background: #2f8fd6; }
#hud .jauge.basse .fond, #hud .jauge.vie.danger .fond { animation: jauge-clignote .7s infinite alternate; }
@keyframes jauge-clignote { from { box-shadow: 0 0 0 0 rgba(217,67,58,0); } to { box-shadow: 0 0 0 2px rgba(217,67,58,.9); } }
#hud .case { pointer-events: auto; }
@media (max-width: 640px) {
  #hud .jauges { padding: 5px 8px; gap: 4px; top: calc(max(8px, env(safe-area-inset-top)) + 40px); }
  #hud .jauge .fond { width: 80px; height: 7px; }
}
#hud .jauge.air .plein { background: #7fd3e6; }
#sous-eau { position: fixed; inset: 0; z-index: 1; pointer-events: none; opacity: 0; transition: opacity .25s;
  background: radial-gradient(ellipse at center, rgba(20,120,150,.12) 30%, rgba(8,60,90,.5) 100%); }
#sous-eau.visible { opacity: 1; }
#sous-eau.trouble { backdrop-filter: blur(7px); background: rgba(30,110,120,.3); }
#hud .jauge.reserve .plein { background: #9aa7b3; }
#fabrication .titre-machine { font: 800 17px inherit; flex: 1; }
#fabrication .statut { padding: 8px 14px 0; font-size: 13px; font-weight: 700; color: #6b5a44; }
#hud .barre-machine { position: absolute; width: 64px; height: 8px; transform: translate(-50%, -50%); border-radius: 5px;
  background: rgba(43,33,24,.55); overflow: hidden; box-shadow: 0 0 0 1.5px rgba(255,246,228,.8); }
#hud .barre-machine div { height: 100%; background: #f2a53a; }
#hud .case.plein span { color: #c4432b; }
#hud .objectif { position: absolute; top: max(10px, env(safe-area-inset-top)); left: 50%; transform: translateX(-50%);
  max-width: min(560px, 58vw); padding: 6px 14px; border-radius: 14px; background: rgba(30,48,58,.72); color: #fff;
  font-size: 13px; font-weight: 600; line-height: 1.25; text-align: center; pointer-events: none; }
#hud .objectif::before { content: 'Objectif — '; color: #ffd27a; font-weight: 800; }
#hud .objectif.nouveau { animation: objectif-nouveau 1.6s; }
@keyframes objectif-nouveau { 0%, 40% { background: rgba(232,138,44,.95); transform: translateX(-50%) scale(1.06); } }
#hud .alerte { top: calc(max(10px, env(safe-area-inset-top)) + 58px); }
#hud .decouverte { position: absolute; inset: 0; display: none; align-items: center; justify-content: center;
  background: rgba(20,32,40,.45); pointer-events: auto; z-index: 30; }
#hud .decouverte.visible { display: flex; }
#hud .decouverte .carte { background: #fff6e4; border-radius: 18px; padding: 18px 20px; width: min(420px, 88vw);
  box-shadow: 0 8px 30px rgba(0,0,0,.35); text-align: center; animation: decouverte .35s; }
@keyframes decouverte { from { transform: scale(.8); opacity: 0; } }
#hud .decouverte .titre { font-size: 22px; font-weight: 900; color: #c9631c; margin-bottom: 10px; }
#hud .decouverte .ligne { display: flex; gap: 12px; align-items: center; text-align: left; padding: 7px 0; border-top: 1px solid #ecdcbd; }
#hud .decouverte .ligne img { width: 52px; height: 52px; flex: none; }
#hud .decouverte .ligne b { display: block; font-size: 15px; }
#hud .decouverte .ligne span { font-size: 13px; opacity: .8; }
#hud .decouverte button { margin-top: 12px; padding: 9px 26px; border: 0; border-radius: 12px; background: #e88a2c; color: #fff;
  font-weight: 800; font-size: 15px; cursor: pointer; }
@media (max-width: 700px) {
  #hud .objectif { font-size: 11.5px; max-width: 52vw; padding: 4px 10px; }
  #hud .alerte { top: calc(max(10px, env(safe-area-inset-top)) + 96px); }
}
#bouton-sac { position: fixed; right: max(18px, env(safe-area-inset-right)); bottom: calc(max(18px, env(safe-area-inset-bottom)) + 260px);
  width: 66px; height: 66px; border-radius: 50%; border: 3px solid var(--papier); background: #a8743a; color: var(--papier);
  font: 700 10px inherit; display: flex; flex-direction: column; align-items: center; justify-content: center;
  box-shadow: 0 4px 12px var(--ombre); z-index: 4; padding: 4px; cursor: pointer; }
#bouton-sac .dessin { font-size: 26px; line-height: 1; }
#bouton-sac.plein { border-color: #e2553a; }
#sac { position: fixed; inset: 0; z-index: 6; display: none; align-items: center; justify-content: center; background: rgba(43,33,24,.35); }
#sac.ouvert { display: flex; }
#sac .carte { background: #fff6e4; border-radius: 18px; padding: 16px 18px; width: min(460px, 92vw); max-height: 86vh; overflow: auto;
  box-shadow: 0 8px 30px rgba(0,0,0,.35); color: var(--encre); }
#sac .titre { font-size: 20px; font-weight: 900; margin-bottom: 6px; }
#sac .poches { font-size: 13px; margin-bottom: 8px; }
#sac .poches .plein { color: #c4432b; }
#sac .rubrique { font-weight: 800; margin: 10px 0 6px; }
#sac .rubrique small { font-weight: 500; opacity: .65; }
#sac .rangee { display: flex; flex-wrap: wrap; gap: 8px; }
#sac .objet { width: 84px; padding: 6px 4px; border-radius: 12px; border: 2px solid #e0cfae; background: #fffdf6; cursor: pointer;
  display: flex; flex-direction: column; align-items: center; gap: 2px; font: 600 11px inherit; color: inherit; }
#sac .objet img { width: 44px; height: 44px; }
#sac .objet.actif { border-color: #e88a2c; background: #ffe6c2; box-shadow: 0 0 0 2px #e88a2c inset; }
#sac .vide, #sac .note { font-size: 12px; opacity: .7; }
#sac .note { margin-top: 10px; }
#sac .fermer { display: block; margin: 12px auto 0; padding: 8px 24px; border: 0; border-radius: 12px; background: #e88a2c; color: #fff;
  font-weight: 800; cursor: pointer; }
@media (max-width: 700px) {
  #bouton-sac { width: 58px; height: 58px; font-size: 8.5px; bottom: calc(max(18px, env(safe-area-inset-bottom)) + 236px); }
  #bouton-sac .dessin { font-size: 22px; }
}
#fondu-grotte { position: fixed; inset: 0; background: #000; opacity: 0; pointer-events: none; z-index: 20; transition: opacity .35s; }
#fondu-grotte.noir { opacity: 1; }
#choix-perso { position: fixed; inset: 0; z-index: 50; display: flex; align-items: center; justify-content: center;
  background: linear-gradient(#7cc8e6, #f3d9a0); }
#choix-perso .carte { background: #fff6e4; border-radius: 20px; padding: 20px 24px; text-align: center; box-shadow: 0 8px 30px rgba(0,0,0,.3); }
#choix-perso .titre { font-size: 24px; font-weight: 900; color: #c9631c; margin-bottom: 14px; }
#choix-perso .choix { display: flex; gap: 16px; }
#choix-perso button { border: 3px solid #e0cfae; border-radius: 16px; background: #fffdf6; padding: 10px 14px; cursor: pointer;
  display: flex; flex-direction: column; align-items: center; gap: 6px; font: 800 15px inherit; color: #3b2c1e; }
#choix-perso button:hover { border-color: #e88a2c; background: #ffe6c2; }
#choix-perso img { width: min(34vw, 170px); height: min(34vw, 170px); object-fit: contain; }
#editeur { position: fixed; top: 10px; left: 10px; width: 250px; z-index: 30; background: rgba(255,246,228,.96); border-radius: 14px;
  padding: 12px; box-shadow: 0 4px 18px rgba(0,0,0,.3); color: #3b2c1e; font-size: 13px; }
#editeur .titre { font-weight: 900; font-size: 16px; color: #c9631c; margin-bottom: 6px; }
#editeur .info { min-height: 52px; margin-bottom: 8px; line-height: 1.35; }
#editeur .rang { display: flex; gap: 6px; margin-bottom: 6px; }
#editeur button, #editeur select { flex: 1; padding: 7px 6px; border-radius: 9px; border: 2px solid #e0cfae; background: #fffdf6;
  font: 700 12px inherit; color: inherit; cursor: pointer; min-width: 0; }
#editeur button:hover { border-color: #e88a2c; background: #ffe6c2; }
#editeur button.fort { background: #e88a2c; border-color: #e88a2c; color: #fff; }
#editeur .aide { font-size: 11px; opacity: .7; line-height: 1.35; margin-top: 4px; }
#hud .jauges { pointer-events: auto; cursor: pointer; }
#hud .case.aliment { box-shadow: 0 0 0 2px #6fae3c inset; cursor: pointer; }
#construction .resume { display: none; }
#construction.compacte { flex-direction: row; flex-wrap: wrap; justify-content: center; gap: 4px 6px; padding: 5px 6px;
  left: 50%; right: auto; transform: translateX(-50%); width: max-content; max-width: calc(100vw - 16px);
  top: calc(max(10px, env(safe-area-inset-top)) + 46px); background: rgba(255,246,228,.88); }
#construction.compacte .resume { display: block; }
#construction.compacte .resume button { background: var(--accent); color: var(--papier); }
#construction.compacte .materiaux, #construction.compacte .pieces { display: none; flex-basis: 100%; }
#construction.compacte.choix .materiaux, #construction.compacte.choix .pieces { display: flex; }
#construction.compacte .cout { min-height: 0; }
#construction.compacte button { padding: 5px 8px; font-size: 11.5px; }
#construction.compacte button img { width: 18px; height: 18px; }
#construction.compacte .ingr img { width: 18px; height: 18px; }
#construction.compacte .ingr { font-size: 12px; }
