:root {
  --btn: 56px;
  --ink: #26324a;
  font-family: "Trebuchet MS", "Avenir Next Rounded", system-ui, sans-serif;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; }
html, body { margin: 0; height: 100vh; height: 100dvh; overflow: hidden; color: var(--ink); user-select: none; -webkit-user-select: none; }
body { background: #7ec8f5; }

#bg {
  position: fixed; inset: 0;
  background: linear-gradient(180deg, #5db7f0 0%, #a9ddfb 55%, #d6f0c8 100%);
  background-size: cover; background-position: center;
}
#gl { position: fixed; inset: 0; width: 100%; height: 100%; touch-action: none; display: none; }
body.in-game #gl { display: block; }
#confetti { position: fixed; inset: 0; pointer-events: none; overflow: hidden; z-index: 30; }
.confetti {
  position: absolute; top: -20px; width: 12px; height: 18px; border-radius: 3px;
  animation: fall linear forwards;
}
@keyframes fall {
  to { transform: translate(var(--drift), 110vh) rotate(var(--spin)); }
}

#ui { position: fixed; inset: 0; pointer-events: none; }
.screen { position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; }
#screen-game { display: block; }
button { font: inherit; color: inherit; cursor: pointer; border: 0; pointer-events: auto; touch-action: manipulation; }

.tool {
  width: var(--btn); height: var(--btn); min-width: 56px; min-height: 56px;
  border-radius: 16px; background: #fff; font-size: 28px; line-height: 1;
  box-shadow: 0 4px 0 rgba(0,0,0,.18), 0 6px 14px rgba(0,0,0,.15);
}
.tool:active { transform: translateY(3px); box-shadow: 0 1px 0 rgba(0,0,0,.18); }
.tool.on { background: #ffd23f; outline: 5px solid var(--ink); }
.corner { position: absolute; top: 14px; right: 14px; }
.corner-left { position: absolute; top: 14px; left: 14px; }

.big {
  min-height: 60px; min-width: 110px; padding: 0 24px; font-size: 26px; font-weight: 800; color: #fff;
  border-radius: 26px; box-shadow: 0 6px 0 rgba(0,0,0,.25), 0 10px 18px rgba(0,0,0,.2);
  text-shadow: 0 2px 0 rgba(0,0,0,.2);
}
.big:active { transform: translateY(4px); box-shadow: 0 2px 0 rgba(0,0,0,.25); }
.green { background: #34b34a; } .orange { background: #ff8a1f; } .blue { background: #2f86e6; }

/* titre */
#screen-title { background-size: cover; background-position: center; gap: 36px; }
#screen-title h1 {
  margin: 0; font-size: clamp(40px, 8vw, 84px); color: #fff; text-align: center;
  text-shadow: 0 5px 0 #d6451f, 0 9px 18px rgba(0,0,0,.3); letter-spacing: 1px;
}
.big-buttons { display: flex; gap: 28px; flex-wrap: wrap; justify-content: center; }
.big-buttons .big { min-width: min(300px, 80vw); min-height: 130px; font-size: 42px; border-radius: 36px; }

/* choix du défi */
#screen-pick { justify-content: flex-start; padding-top: 16px; gap: 18px; }
#screen-pick h2 { margin: 6px 0 0; font-size: 40px; color: #fff; text-shadow: 0 3px 0 rgba(0,0,0,.25); }
#cards { display: flex; flex-wrap: wrap; gap: 20px; justify-content: center; align-content: flex-start; padding: 10px; flex: 1; max-width: 1100px; overflow: auto; pointer-events: auto; }
.card {
  width: 210px; padding: 12px; border-radius: 28px; background: #fff; text-align: center;
  display: flex; flex-direction: column; align-items: center; gap: 6px;
  box-shadow: 0 6px 0 rgba(0,0,0,.18), 0 12px 22px rgba(0,0,0,.18);
}
.card:active { transform: translateY(3px); }
.card .pic { width: 170px; height: 150px; border-radius: 20px; background: linear-gradient(160deg, #cdeeff, #fff6c9); display: flex; align-items: center; justify-content: center; font-size: 82px; overflow: hidden; }
.card .pic img { width: 100%; height: 100%; object-fit: cover; }
.card b { font-size: 26px; }
.card .stars { font-size: 36px; color: #ffb703; letter-spacing: 3px; }
.card .stars .off { color: #cfd6e2; }
.card .dots { font-size: 18px; color: #2f86e6; letter-spacing: 4px; }

/* jeu */
#tools { position: absolute; left: 12px; top: 12px; display: flex; flex-direction: column; gap: 8px; }
#bubble {
  position: absolute; top: 16px; left: 50%; transform: translate(-50%, -20px); opacity: 0;
  max-width: min(60vw, 560px); padding: 14px 26px; border-radius: 26px; background: #fff; font-size: 26px; font-weight: 800; text-align: center;
  box-shadow: 0 6px 18px rgba(0,0,0,.22); transition: opacity .2s, transform .2s; pointer-events: none;
}
#bubble.show { opacity: 1; transform: translate(-50%, 0); }
#fiche {
  position: absolute; right: 12px; top: 12px; width: 196px; padding: 8px; pointer-events: none;
}
#fiche-head { background: #fff; border-radius: 16px; padding: 0 8px; margin-bottom: 14px; box-shadow: 0 6px 16px rgba(0,0,0,.22); display: flex; align-items: center; gap: 8px; height: 44px; font-size: 22px; }
#fiche-pic { font-size: 34px; width: 44px; height: 44px; display: flex; align-items: center; justify-content: center; flex: none; }
#fiche-pic img { width: 44px; height: 44px; object-fit: cover; border-radius: 10px; }
#fiche-view { width: 180px; height: 180px; outline: 6px solid #fff; box-shadow: 0 6px 16px rgba(0,0,0,.22); }
#zoom { position: absolute; right: 14px; top: 50%; transform: translateY(-50%); display: flex; flex-direction: column; gap: 10px; }
#piece-actions {
  position: absolute; left: 0; top: 0; display: flex; gap: 10px; align-items: center; padding: 10px; border-radius: 28px;
  background: rgba(255,255,255,.92); box-shadow: 0 8px 22px rgba(0,0,0,.28); pointer-events: auto; z-index: 15;
}
#piece-actions .tool { width: 64px; height: 64px; font-size: 34px; }
#piece-actions .tool.small { width: 52px; height: 52px; font-size: 22px; background: #eef1f6; }
#btn-finish { position: absolute; right: 14px; bottom: 160px; }
#bottom {
  position: absolute; left: 50%; bottom: 8px; transform: translateX(-50%);
  display: flex; flex-direction: column; gap: 6px; align-items: center;
  padding: 8px 10px; border-radius: 28px; background: rgba(255,255,255,.82); pointer-events: auto; max-width: calc(100vw - 16px); box-sizing: border-box;
}
#palette, #colors { display: flex; gap: 6px; max-width: 100%; overflow-x: auto; touch-action: pan-x; scrollbar-width: none; padding: 5px; }
#palette > *, #colors > * { flex: none; }
.piece {
  width: 60px; height: 60px; border-radius: 16px; background: #fff; display: flex; flex-direction: column;
  align-items: center; justify-content: center; gap: 4px; font-size: 12px; font-weight: 800;
  box-shadow: 0 3px 0 rgba(0,0,0,.18);
}
.piece .slot { height: 42px; display: flex; align-items: center; justify-content: center; }
.ico {
  display: block; border-radius: 5px; border: 2px solid rgba(0,0,0,.35); background-color: var(--c, #e53935);
  background-image: radial-gradient(circle at 50% 50%, rgba(255,255,255,.65) 0 26%, transparent 29%);
  background-size: 12px 12px;
}
.ico.tile { background-image: none; opacity: .9; border-style: dashed; }
.ico.roof { background-image: linear-gradient(180deg, rgba(255,255,255,.1), rgba(0,0,0,.35)); }
.swatch { width: 52px; height: 52px; min-width: 52px; border-radius: 50%; border: 4px solid rgba(0,0,0,.2); box-shadow: 0 3px 0 rgba(0,0,0,.18); }
.piece.sel, .swatch.sel { outline: 5px solid var(--ink); outline-offset: 1px; }

/* résultat */
#result { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; background: rgba(20,30,60,.35); pointer-events: auto; z-index: 20; }
.panel { background: #fff; border-radius: 36px; padding: 24px 32px; text-align: center; box-shadow: 0 12px 36px rgba(0,0,0,.35); max-width: 94vw; animation: pop .35s both; }
.panel p { font-size: 30px; font-weight: 800; margin: 8px 0 18px; }
.panel .row { display: flex; gap: 14px; flex-wrap: wrap; justify-content: center; }
#res-stars { display: flex; justify-content: center; gap: 8px; }
.star { font-size: 92px; line-height: 1; color: #d6dce8; transform: scale(.7); }
.star.on { color: #ffc107; text-shadow: 0 4px 0 #e69100; animation: pop .55s both; animation-delay: calc(var(--i) * .4s + .2s); }
@keyframes pop { 0% { transform: scale(.2); opacity: 0; } 70% { transform: scale(1.25); opacity: 1; } 100% { transform: scale(1); opacity: 1; } }

@media (max-height: 640px) {
  #fiche { width: 150px; } #fiche-view { width: 134px; height: 134px; } #fiche-head { height: 36px; font-size: 18px; }
  .big-buttons .big { min-height: 96px; }
}

@media (max-width: 480px) {
  :root { --btn: 56px; }
  .tool { min-width: 48px; min-height: 48px; font-size: 28px; }
  #tools { gap: 6px; left: max(8px, env(safe-area-inset-left)); top: 8px; }
  #screen-title { gap: 20px; padding: 12px; }
  #screen-title h1 { font-size: 38px; }
  .big-buttons .big { min-height: 96px; font-size: 34px; }
  .card { width: 150px; padding: 8px; }
  .card .pic { width: 130px; height: 110px; font-size: 60px; }
  .card b { font-size: 20px; }
  .card .stars { font-size: 28px; }
  #cards { gap: 12px; }
  #screen-pick h2 { font-size: 30px; }
  #bottom { bottom: max(8px, env(safe-area-inset-bottom)); }
  .piece { width: 56px; height: 56px; } .piece .slot { height: 38px; }
  .swatch { width: 48px; height: 48px; min-width: 48px; }
  #btn-finish { bottom: 170px; min-height: 60px; font-size: 24px; padding: 0 18px; }
  #bubble { font-size: 20px; padding: 8px 14px; max-width: 70vw; }
  .panel { padding: 16px; } .star { font-size: 64px; } .panel p { font-size: 22px; }
}
@media (max-height: 500px) and (orientation: landscape) {
  :root { --btn: 52px; }
  .tool { min-width: 48px; min-height: 48px; font-size: 26px; border-radius: 16px; }
  #tools { flex-direction: row; flex-wrap: wrap; width: calc(var(--btn) * 2 + 6px); gap: 6px; left: max(8px, env(safe-area-inset-left)); top: 8px; }
  #fiche { top: 6px; right: max(8px, env(safe-area-inset-right)); width: 124px; padding: 0; }
  #fiche-head { height: 30px; font-size: 14px; margin-bottom: 6px; }
  #fiche-pic, #fiche-pic img { width: 30px; height: 30px; font-size: 22px; }
  #fiche-view { width: 108px; height: 108px; outline-width: 4px; }
  #zoom { top: auto; bottom: 8px; right: max(8px, env(safe-area-inset-right)); transform: none; flex-direction: column; gap: 6px; }
  #btn-finish { bottom: 124px; right: max(10px, env(safe-area-inset-right)); min-height: 56px; min-width: 100px; font-size: 22px; padding: 0 16px; }
  #bottom { flex-direction: row; align-items: center; gap: 8px; bottom: 6px; padding: 4px 8px; border-radius: 22px;
    width: calc(100vw - 2 * (var(--btn) * 2 + 24px)); max-width: none; }
  #palette { flex: 1 1 auto; min-width: 0; padding: 3px; }
  #colors { flex: 0 0 auto; width: 206px; flex-wrap: wrap; overflow: visible; padding: 3px; gap: 6px; justify-content: center; }
  .piece { width: 58px; height: 58px; border-radius: 14px; font-size: 11px; gap: 0; }
  .piece .slot { height: 38px; }
  .swatch { width: 44px; height: 44px; min-width: 44px; border-width: 3px; }
  #bubble { top: 6px; font-size: 18px; padding: 6px 14px; max-width: 46vw; }
  #piece-actions .tool { width: 56px; height: 56px; font-size: 30px; }
  #piece-actions .tool.small { width: 48px; height: 48px; }
  #screen-title { gap: 12px; }
  #screen-title h1 { font-size: 36px; }
  .big-buttons { gap: 16px; }
  .big-buttons .big { min-height: 80px; font-size: 32px; min-width: 200px; }
  #screen-pick { padding-top: 8px; gap: 6px; }
  #screen-pick h2 { font-size: 28px; margin: 0; }
  #cards { gap: 10px; padding: 6px; }
  .card { width: 140px; padding: 6px; gap: 2px; border-radius: 20px; }
  .card .pic { width: 120px; height: 80px; font-size: 48px; }
  .card b { font-size: 18px; } .card .stars { font-size: 22px; }
  .panel { padding: 12px 20px; } .star { font-size: 52px; } .panel p { font-size: 20px; margin: 4px 0 10px; }
  .panel .big { min-height: 56px; font-size: 22px; min-width: 90px; padding: 0 16px; }
}
