:root {
  --primary: #FF591A;
  --primary-dark: #E54915;
  --secondary: #00449E;
  --volt: #ffd23f;
  --bg: #0f172a;
  --surface: rgba(15, 23, 42, 0.82);
  --surface-solid: #1e293b;
  --border: rgba(148, 163, 184, 0.25);
  --text: #f1f5f9;
  --text-muted: #94a3b8;
  --success: #10b981;
  --danger: #ef4444;
  --warning: #f59e0b;
  --radius: 16px;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; overflow: hidden; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: 'Nunito', system-ui, sans-serif;
  user-select: none;
  -webkit-user-select: none;
}
button { font-family: inherit; }

#bg { position: fixed; inset: 0; width: 100%; height: 100%; z-index: 0; }
body.world-hydro   { background: linear-gradient(#5aa9e6, #b8e1ff 60%, #3d7a3a); }
body.world-wind    { background: linear-gradient(#7cc4f0, #dff3ff 60%, #6aa84f); }
body.world-solar   { background: linear-gradient(#ffb36b, #ffe7a8 60%, #c9a35a); }
body.world-nuclear { background: linear-gradient(#5d6d8a, #b9c6d9 60%, #55705a); }
body.world-biomass { background: linear-gradient(#e9a86a, #f6dcae 60%, #7a8f3a); }

.screen { position: fixed; inset: 0; z-index: 1; display: none; }
.screen.active { display: flex; }

/* ---------- Titre ---------- */
#screen-title { align-items: center; justify-content: center; padding: 16px; }
.title-card {
  background: var(--surface);
  backdrop-filter: blur(8px);
  border: 1px solid var(--border);
  border-radius: 24px;
  padding: 40px 48px;
  text-align: center;
  max-width: 680px;
  box-shadow: 0 30px 80px rgba(0, 0, 0, 0.45);
}
.logo {
  font-family: 'Baloo 2', sans-serif;
  font-weight: 800;
  font-size: clamp(40px, 7vw, 64px);
  line-height: 1;
  color: #fff;
  text-shadow: 0 4px 0 var(--primary-dark), 0 8px 24px rgba(255, 89, 26, 0.5);
}
.bolt { color: var(--volt); display: inline-block; animation: flicker 2.6s infinite; }
@keyframes flicker {
  0%, 18%, 22%, 62%, 64%, 100% { opacity: 1; }
  20%, 63% { opacity: 0.25; }
}
.tagline { color: var(--text-muted); font-size: 18px; margin: 16px 0 28px; }
.rules { margin-top: 28px; font-size: 14px; color: var(--text-muted); display: grid; gap: 6px; }
.rules b { color: var(--text); }
.credit { margin: 22px auto 0; max-width: 460px; font-size: 11.5px; line-height: 1.45; color: var(--text-faint, #64748b); }

/* ---------- Boutons ---------- */
.btn {
  border: 0; border-radius: 12px; cursor: pointer;
  padding: 10px 18px; font-size: 16px; font-weight: 800;
  background: var(--surface-solid); color: var(--text);
  transition: transform .08s, filter .15s, box-shadow .15s;
}
.btn:hover { filter: brightness(1.12); }
.btn:active { transform: translateY(2px); }
.btn-primary { background: var(--primary); color: #fff; box-shadow: 0 5px 0 var(--primary-dark); }
.btn-primary:active { box-shadow: 0 2px 0 var(--primary-dark); transform: translateY(3px); }
.btn-ghost { background: transparent; border: 2px solid var(--border); }
.btn-big { font-size: 24px; padding: 16px 56px; border-radius: 16px; font-family: 'Baloo 2', sans-serif; }
.btn-icon { width: 42px; height: 42px; padding: 0; font-size: 14px; background: var(--surface); border: 1px solid var(--border); }

/* ---------- HUD ---------- */
#screen-game { flex-direction: column; }
.hud {
  display: grid; grid-template-columns: 1fr minmax(200px, 360px) 1fr; align-items: center; gap: 16px;
  padding: 10px 20px; margin: 12px 16px 0;
  background: var(--surface); backdrop-filter: blur(8px);
  border: 1px solid var(--border); border-radius: var(--radius);
}
.hud-level { display: flex; flex-direction: column; line-height: 1.1; }
.hud-level span { font-size: 13px; color: var(--volt); font-weight: 800; text-transform: uppercase; letter-spacing: .08em; }
.hud-level b { font-family: 'Baloo 2', sans-serif; font-size: 24px; }
.hud-timer { text-align: center; }
.timer-text { font-family: 'Baloo 2', sans-serif; font-size: 30px; font-weight: 800; line-height: 1; font-variant-numeric: tabular-nums; }
.timer-bar { height: 8px; border-radius: 4px; background: rgba(255, 255, 255, 0.15); overflow: hidden; margin-top: 4px; }
.timer-bar div { height: 100%; width: 100%; background: linear-gradient(90deg, var(--volt), var(--primary)); transition: width .1s linear; }
.hud-timer.danger .timer-text { color: var(--danger); animation: pulse .5s infinite alternate; }
.hud-timer.danger .timer-bar div { background: var(--danger); }
@keyframes pulse { from { transform: scale(1); } to { transform: scale(1.12); } }
.hud-timer.penalty .timer-text { color: var(--warning); }
.hud-right { display: flex; align-items: center; justify-content: flex-end; gap: 18px; }
.lives { display: flex; gap: 4px; font-size: 22px; }
.lives .life { filter: drop-shadow(0 0 6px rgba(255, 210, 63, .7)); }
.lives .life.lost { filter: grayscale(1) opacity(.3); }
.hud-score { text-align: right; line-height: 1.1; }
.hud-score span { display: block; font-size: 12px; color: var(--text-muted); text-transform: uppercase; letter-spacing: .08em; }
.hud-score b { font-family: 'Baloo 2', sans-serif; font-size: 24px; font-variant-numeric: tabular-nums; }

/* ---------- Plateau ---------- */
.play { flex: 1; display: flex; gap: 16px; padding: 16px; min-height: 0; justify-content: center; align-items: center; }
.board-wrap {
  padding: 10px; border-radius: 20px;
  background: linear-gradient(#6b4a2b, #4a3220);
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.45), inset 0 2px 0 rgba(255, 255, 255, 0.15);
}
#board { display: block; border-radius: 10px; cursor: pointer; touch-action: none; }

.queue {
  width: 150px; align-self: center;
  background: var(--surface); backdrop-filter: blur(8px);
  border: 1px solid var(--border); border-radius: var(--radius);
  padding: 14px; display: flex; flex-direction: column; align-items: center; gap: 8px;
}
.queue-title { font-weight: 800; font-size: 14px; text-transform: uppercase; letter-spacing: .08em; color: var(--volt); }
.queue-title.small { color: var(--text-muted); font-size: 12px; margin-top: 6px; }
.q-current { width: 104px; height: 104px; border-radius: 14px; background: #4f8a33; box-shadow: 0 0 0 3px var(--volt), 0 0 24px rgba(255, 210, 63, .45); }
.q-next { display: grid; gap: 8px; }
.q-next canvas { width: 64px; height: 64px; border-radius: 10px; background: rgba(79, 138, 51, .75); }
.q-next canvas:nth-child(n+3) { opacity: .75; }
.q-current.pop { animation: pop .25s ease-out; }
@keyframes pop { from { transform: scale(.7); } to { transform: scale(1); } }

.hidden { display: none !important; }

/* ---------- Connexion / accueil ---------- */
.login { display: grid; gap: 8px; text-align: left; }
.login label { font-weight: 800; color: var(--text-muted); font-size: 14px; }
.login-row { display: flex; gap: 8px; }
.login input, .panel select {
  flex: 1; min-width: 0; font: inherit; font-size: 18px; font-weight: 700;
  padding: 12px 14px; border-radius: 12px; border: 2px solid var(--border);
  background: rgba(2, 6, 23, .5); color: var(--text); outline: none;
}
.login input:focus { border-color: var(--primary); }
.form-error { color: #fca5a5; font-size: 14px; min-height: 18px; }
.home { display: flex; gap: 24px; align-items: center; text-align: left; }
.home-avatar { width: 130px; flex: none; }
.home-main { display: grid; gap: 10px; justify-items: start; }
.hello { font-size: 20px; }
.hello b { color: var(--volt); }
.hello .link { margin-left: 10px; font-size: 13px; vertical-align: middle; }
.home-best { color: var(--text-muted); font-size: 14px; }
.home-links { display: flex; gap: 8px; flex-wrap: wrap; }
.link { background: none; border: 0; color: var(--text-muted); text-decoration: underline; cursor: pointer; font-size: 13px; padding: 0; }
.link:hover { color: var(--text); }

/* ---------- Avatar ---------- */
.avatar { width: 100%; height: auto; display: block; overflow: visible; }
.avatar .av-body { transform-origin: 60px 190px; animation: breathe 2.4s ease-in-out infinite; }
.avatar.mood-happy .av-body { animation: jump .6s ease-in-out infinite; }
.avatar.mood-stress .av-body { animation: shake .25s linear infinite; }
.avatar.mood-sad .av-body { animation: droop 3s ease-in-out infinite; }
@keyframes breathe { 50% { transform: scaleY(1.02); } }
@keyframes jump { 50% { transform: translateY(-12px); } }
@keyframes shake { 25% { transform: translateX(-2px) rotate(-1deg); } 75% { transform: translateX(2px) rotate(1deg); } }
@keyframes droop { 50% { transform: translateY(3px) scaleY(.98); } }

/* ---------- Panneaux (atelier, carte, scores) ---------- */
#screen-workshop, #screen-map, #screen-scores { align-items: center; justify-content: center; padding: 16px; }
.panel {
  width: min(980px, 100%); max-height: calc(100vh - 32px); display: flex; flex-direction: column;
  background: var(--surface); backdrop-filter: blur(10px);
  border: 1px solid var(--border); border-radius: 24px; padding: 20px 24px;
  box-shadow: 0 30px 80px rgba(0, 0, 0, .45);
}
.panel-head { display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; gap: 12px; }
.panel-head h2 { font-family: 'Baloo 2', sans-serif; font-size: 30px; margin: 0; }
.panel-head > :last-child { justify-self: end; }
.panel-head > :first-child { justify-self: start; }
.panel-note { color: var(--text-muted); font-size: 14px; margin: 8px 0 12px; text-align: center; }
.panel-foot { display: flex; justify-content: flex-end; gap: 10px; margin-top: 14px; }

.workshop-body { display: grid; grid-template-columns: 260px 1fr; gap: 24px; margin-top: 12px; min-height: 0; overflow: auto; }
.workshop-preview { display: flex; flex-direction: column; align-items: center; gap: 12px;
  background: radial-gradient(circle at 50% 40%, rgba(255, 210, 63, .18), transparent 70%); border-radius: 18px; padding: 8px; }
#ws-avatar { width: 220px; }
.mood-row { display: flex; flex-wrap: wrap; gap: 6px; justify-content: center; }
.workshop-options { display: grid; gap: 14px; align-content: start; }
.opt-group h3 { margin: 0 0 6px; font-size: 13px; text-transform: uppercase; letter-spacing: .08em; color: var(--volt); }
.opt-row { display: flex; flex-wrap: wrap; gap: 8px; }
.swatch { width: 34px; height: 34px; border-radius: 50%; border: 3px solid transparent; cursor: pointer; box-shadow: inset 0 0 0 1px rgba(0, 0, 0, .25); padding: 0; }
.swatch.active { border-color: #fff; box-shadow: 0 0 0 2px var(--primary); }
.chip { border: 2px solid var(--border); background: rgba(2, 6, 23, .4); color: var(--text); border-radius: 999px; padding: 6px 14px; font-weight: 700; cursor: pointer; font-family: inherit; }
.chip.active { border-color: var(--primary); background: rgba(255, 89, 26, .2); }

.worlds { display: grid; gap: 12px; overflow: auto; padding-right: 4px; }
.world { display: grid; grid-template-columns: 170px 1fr; gap: 14px; align-items: center; padding: 12px 14px; border-radius: 16px; background: rgba(2, 6, 23, .35); border: 1px solid var(--border); }
.world-name { font-family: 'Baloo 2', sans-serif; font-size: 22px; line-height: 1.1; }
.world-name small { display: block; font-family: 'Nunito', sans-serif; font-size: 13px; color: var(--text-muted); }
.world-levels { display: grid; grid-template-columns: repeat(6, 1fr); gap: 8px; }
.lvl {
  height: clamp(54px, 9vh, 76px); border-radius: 14px; border: 0; cursor: pointer; font-family: 'Baloo 2', sans-serif; font-size: 24px; font-weight: 800;
  background: linear-gradient(#4f8a33, #3b6d25); color: #fff; box-shadow: 0 4px 0 #29501a; display: grid; place-items: center; align-content: center; gap: 0; line-height: 1;
}
.lvl:hover { filter: brightness(1.1); }
.lvl .lvl-stars { font-size: 11px; letter-spacing: 1px; }
.lvl .lvl-stars .off { filter: grayscale(1) opacity(.3); }
.lvl.locked { background: #334155; box-shadow: 0 4px 0 #1e293b; color: #64748b; cursor: not-allowed; }
.lvl.current { outline: 3px solid var(--volt); outline-offset: 2px; }
.map-stars { color: var(--volt); font-weight: 800; }
.world-nav { display: flex; align-items: center; gap: 10px; }
.world-nav h2 { min-width: 170px; text-align: center; }
.world-nav .btn-icon { font-size: 26px; line-height: 1; }
.world-nav .btn-icon:disabled { opacity: .3; cursor: not-allowed; }
.map-dots { display: flex; justify-content: center; gap: 6px; margin: 6px 0 10px; }
.map-dots button { width: 12px; height: 12px; border-radius: 50%; border: 0; padding: 0; background: rgba(255, 255, 255, .25); cursor: pointer; }
.map-dots button.active { background: var(--volt); }
.map-dots button:disabled { opacity: .35; cursor: not-allowed; }
.map-wrap { position: relative; border-radius: 18px; overflow: hidden; box-shadow: inset 0 0 0 1px var(--border); min-height: 0; }
.map-banner {
  position: absolute; left: 50%; top: 18%; transform: translateX(-50%); text-align: center; pointer-events: none;
  background: rgba(15, 23, 42, .82); border: 2px solid var(--volt); border-radius: 18px; padding: 12px 34px;
  animation: bannerIn .6s cubic-bezier(.2, 1.4, .4, 1);
}
.map-banner small { display: block; color: var(--volt); font-weight: 800; letter-spacing: .12em; text-transform: uppercase; font-size: 13px; }
.map-banner b { font-family: 'Baloo 2', sans-serif; font-size: 44px; line-height: 1.1; }
@keyframes bannerIn { from { transform: translateX(-50%) scale(.6); opacity: 0; } to { transform: translateX(-50%) scale(1); opacity: 1; } }
#screen-map.transit [data-back], #screen-map.transit .world-nav .btn-icon, #screen-map.transit .map-dots, #screen-map.transit .panel-note { visibility: hidden; }
#screen-map.transit #map-canvas { cursor: pointer !important; }
#map-canvas { display: block; }
.map-card { display: flex; align-items: center; gap: 16px; justify-content: center; flex-wrap: wrap; margin-top: 12px; min-height: 54px; }
.map-card .lvl-title { font-family: 'Baloo 2', sans-serif; font-size: 24px; font-weight: 800; }
.map-card .lvl-info { color: var(--text-muted); }
.map-card .stars-inline .off { filter: grayscale(1) opacity(.3); }
a.btn { text-decoration: none; display: inline-block; }

.tabs { display: flex; gap: 8px; margin: 14px 0 10px; align-items: center; flex-wrap: wrap; }
.tab { border: 0; background: rgba(2, 6, 23, .4); color: var(--text-muted); padding: 8px 16px; border-radius: 10px; font-weight: 800; cursor: pointer; font-family: inherit; }
.tab.active { background: var(--primary); color: #fff; }
.panel select { flex: none; font-size: 15px; padding: 7px 10px; }
.score-list { overflow: auto; display: grid; gap: 6px; min-height: 120px; align-content: start; }
.score-row { display: grid; grid-template-columns: 44px 40px 1fr auto auto; gap: 12px; align-items: center; padding: 6px 12px; border-radius: 12px; background: rgba(2, 6, 23, .35); }
.score-row.me { background: rgba(255, 89, 26, .22); outline: 1px solid var(--primary); }
.score-row .rank { font-family: 'Baloo 2', sans-serif; font-size: 22px; font-weight: 800; text-align: center; color: var(--text-muted); }
.score-row:nth-child(1) .rank { color: #facc15; } .score-row:nth-child(2) .rank { color: #cbd5e1; } .score-row:nth-child(3) .rank { color: #d97706; }
.score-row .mini { width: 40px; height: 40px; overflow: hidden; border-radius: 50%; background: rgba(255, 255, 255, .08); }
.score-row .mini svg { width: 64px; margin: -2px 0 0 -12px; }
.score-row .name { font-weight: 800; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.score-row .meta { color: var(--text-muted); font-size: 13px; text-align: right; }
.score-row .pts { font-family: 'Baloo 2', sans-serif; font-size: 22px; font-weight: 800; text-align: right; min-width: 80px; font-variant-numeric: tabular-nums; }
.score-mine { margin-top: 10px; color: var(--text-muted); font-size: 14px; text-align: center; }
.empty { text-align: center; color: var(--text-muted); padding: 30px; }

.game-avatar { width: 96px; margin-top: 10px; }
.hud-level small { font-size: 12px; color: var(--text-muted); }

/* ---------- Son ---------- */
.sound-ctl { position: fixed; left: 16px; bottom: 16px; z-index: 20; display: flex; gap: 6px; }
.sound-ctl .btn-icon { font-size: 18px; }
.sound-ctl .off { filter: grayscale(1); opacity: .45; }

/* ---------- Badges ---------- */
#screen-badges { align-items: center; justify-content: center; padding: 16px; }
.badge-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 10px; overflow: auto; margin-top: 14px; }
.badge-card { display: grid; grid-template-columns: 48px 1fr; gap: 10px; align-items: center; padding: 10px 12px; border-radius: 14px; background: rgba(2, 6, 23, .35); border: 1px solid var(--border); }
.badge-card .ico { font-size: 34px; text-align: center; }
.badge-card b { display: block; }
.badge-card small { color: var(--text-muted); font-size: 12px; line-height: 1.3; display: block; }
.badge-card.locked .ico { filter: grayscale(1) opacity(.3); }
.badge-card.locked b { color: var(--text-muted); }
.badge-card.got { border-color: rgba(255, 210, 63, .5); background: rgba(255, 210, 63, .08); }

.toasts { position: fixed; top: 16px; right: 16px; z-index: 30; display: grid; gap: 8px; pointer-events: none; }
.toast {
  display: grid; grid-template-columns: 44px 1fr; gap: 10px; align-items: center; min-width: 260px; max-width: 340px;
  background: var(--surface-solid); border: 2px solid var(--volt); border-radius: 14px; padding: 10px 14px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, .45); animation: toastIn .45s cubic-bezier(.2, 1.4, .4, 1), toastOut .4s ease-in 7s forwards;
  pointer-events: auto; cursor: pointer;
}
.toast:hover { animation-play-state: paused; }
.toast .toast-desc { display: block; color: var(--text-muted); font-size: 12px; line-height: 1.3; margin-top: 2px; }
.toast .ico { font-size: 32px; text-align: center; }
.toast small { display: block; color: var(--volt); font-size: 11px; text-transform: uppercase; letter-spacing: .08em; font-weight: 800; }
@keyframes toastIn { from { transform: translateX(120%); } to { transform: translateX(0); } }
@keyframes toastOut { to { transform: translateX(120%); opacity: 0; } }

/* ---------- Tutoriel ---------- */
.tuto {
  position: fixed; z-index: 15; max-width: 300px; padding: 14px 16px; border-radius: 16px;
  background: #fff; color: #1f2937; font-size: 15px; line-height: 1.4; box-shadow: 0 14px 40px rgba(0, 0, 0, .4);
  display: grid; gap: 10px; justify-items: end; transition: left .3s, top .3s;
}
.tuto #tuto-text { justify-self: stretch; }
.tuto::after { content: ''; position: absolute; width: 16px; height: 16px; background: #fff; transform: rotate(45deg); left: var(--arrow-x, 24px); }
.tuto.below::after { top: -8px; }
.tuto.above::after { bottom: -8px; }

/* ---------- Alerte fin de chrono ---------- */
#alarm {
  position: fixed; inset: 0; z-index: 5; pointer-events: none;
  background:
    radial-gradient(ellipse at center, rgba(239, 68, 68, 0) 45%, rgba(220, 38, 38, 0.55) 100%),
    rgba(239, 68, 68, 0.14);
  opacity: 0;
  transition: opacity .3s;
}
body.alarm #alarm { animation: alarm 1s ease-in-out infinite alternate; }
@keyframes alarm { from { opacity: 0.1; } to { opacity: 1; } }

/* ---------- Modale ---------- */
.overlay {
  position: fixed; inset: 0; z-index: 10; display: flex; align-items: center; justify-content: center; padding: 16px;
  background: rgba(2, 6, 23, 0.55); backdrop-filter: blur(3px);
  transition: opacity .2s;
}
.overlay.hidden { opacity: 0; pointer-events: none; }
.modal {
  background: var(--surface-solid); border: 1px solid var(--border); border-radius: 22px;
  padding: 32px 40px; min-width: min(420px, 100%); max-width: 560px; text-align: center;
  box-shadow: 0 30px 80px rgba(0, 0, 0, 0.5);
  animation: modalIn .3s cubic-bezier(.2, 1.4, .4, 1);
}
@keyframes modalIn { from { transform: scale(.85); opacity: 0; } to { transform: scale(1); opacity: 1; } }
.modal h2 { font-family: 'Baloo 2', sans-serif; font-size: 36px; margin: 0 0 4px; line-height: 1.1; }
.modal .sub { color: var(--text-muted); margin: 0 0 20px; }
.modal .actions { display: flex; gap: 12px; justify-content: center; margin-top: 24px; flex-wrap: wrap; }
.countdown { font-family: 'Baloo 2', sans-serif; font-size: 96px; font-weight: 800; color: var(--volt); line-height: 1; text-shadow: 0 0 30px rgba(255, 210, 63, .6); }
.stars { font-size: 44px; letter-spacing: 6px; margin: 4px 0 12px; }
.stars .off { filter: grayscale(1) opacity(.25); }
.stars .on { display: inline-block; animation: starIn .4s cubic-bezier(.2, 1.6, .4, 1) both; }
@keyframes starIn { from { transform: scale(0) rotate(-90deg); } to { transform: scale(1) rotate(0); } }
.breakdown { display: grid; grid-template-columns: 1fr auto; gap: 6px 24px; text-align: left; margin: 0 auto; max-width: 320px; font-size: 15px; }
.breakdown .neg { color: var(--danger); }
.breakdown .total { font-family: 'Baloo 2', sans-serif; font-size: 22px; border-top: 1px solid var(--border); padding-top: 6px; }
.breakdown span:nth-child(even) { text-align: right; font-variant-numeric: tabular-nums; font-weight: 800; }

@media (max-width: 760px) {
  .hud { grid-template-columns: 1fr 1fr; margin: 8px 8px 0; padding: 8px 12px; }
  .hud-timer { grid-column: 1 / -1; order: 3; }
  .play { flex-direction: column; padding: 8px; gap: 8px; }
  .queue { width: auto; flex-direction: row; padding: 8px 12px; }
  .q-next { grid-auto-flow: column; }
  .q-current { width: 72px; height: 72px; }
  .q-next canvas { width: 48px; height: 48px; }
  .queue-title { display: none; }
  .game-avatar { display: none; }
  .home { flex-direction: column; text-align: center; }
  .home-main { justify-items: center; }
  .workshop-body { grid-template-columns: 1fr; }
  #ws-avatar { width: 150px; }
  .world { grid-template-columns: 1fr; }
  .world-levels { grid-template-columns: repeat(3, 1fr); }
  .score-row { grid-template-columns: 34px 34px 1fr auto; }
  .score-row .meta { display: none; }
}
