* { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; }

:root {
  --ui-font: "Nunito", "Avenir Next", "Segoe UI", system-ui, sans-serif;
}

html, body {
  height: 100%;
  overflow: hidden;
  font-family: var(--ui-font);
  touch-action: manipulation;
  user-select: none;
}

body {
  background: radial-gradient(ellipse at 50% 0%, #2c4a7c 0%, #1a2e52 45%, #101c36 100%);
  color: #fff;
}

#app { position: fixed; inset: 0; }

#scene { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }

/* ---------- Mode 2D (secours sans WebGL) ---------- */
#dice2d {
  position: absolute; inset: 0;
  display: flex; flex-wrap: wrap;
  align-items: center; justify-content: center;
  gap: 3.5vmin;
  padding: 100px 20px 140px;
  z-index: 1;
}
.die2dWrap {
  display: flex; flex-direction: column; align-items: center; gap: 16px;
  cursor: pointer;
}
.die2dWrap canvas {
  border-radius: 14%;
  box-shadow: 0 20px 45px rgba(0,0,0,.5), 0 4px 12px rgba(0,0,0,.35);
  transition: transform .15s;
}
.die2dWrap:hover canvas { transform: scale(1.03); }
.die2dLabel {
  padding: 5px 22px; border-radius: 99px;
  font-weight: 800; font-size: clamp(1rem, 2.2vw, 1.4rem);
  color: #fff; text-shadow: 0 1px 3px rgba(0,0,0,.45);
  box-shadow: 0 4px 14px rgba(0,0,0,.3);
}
canvas.rolling { animation: diceShake .16s infinite; }
canvas.landing { animation: dicePop .38s cubic-bezier(.2, 1.8, .4, 1); }
@keyframes diceShake {
  0%   { transform: translate(0, 0) rotate(0deg) scale(.96); }
  20%  { transform: translate(-8px, 6px) rotate(-8deg) scale(.96); }
  40%  { transform: translate(7px, -9px) rotate(6deg) scale(.96); }
  60%  { transform: translate(-6px, -6px) rotate(-5deg) scale(.96); }
  80%  { transform: translate(8px, 7px) rotate(7deg) scale(.96); }
  100% { transform: translate(0, 0) rotate(0deg) scale(.96); }
}
@keyframes dicePop {
  0% { transform: scale(.7); }
  100% { transform: scale(1); }
}

/* ---------- Top bar ---------- */
#topbar {
  position: absolute; top: 0; left: 0; right: 0;
  display: flex; justify-content: space-between; align-items: center;
  padding: 12px 16px;
  pointer-events: none;
  z-index: 10;
}
#logo { font-size: 1.4rem; font-weight: 800; opacity: .85; text-shadow: 0 2px 6px rgba(0,0,0,.4); }
#toolbar { display: flex; gap: 10px; pointer-events: auto; }
.tool {
  width: 58px; height: 58px; font-size: 1.6rem;
  border: none; border-radius: 16px; cursor: pointer;
  background: rgba(255,255,255,.12); color: #fff;
  backdrop-filter: blur(6px);
  transition: background .15s, transform .1s;
}
.tool:hover { background: rgba(255,255,255,.25); }
.tool:active { transform: scale(.93); }
.tool.off { opacity: .45; }

/* ---------- Result banner ---------- */
#banner {
  position: absolute; top: 84px; left: 0; right: 0;
  display: flex; justify-content: center;
  z-index: 9;
  pointer-events: none;
}
#bannerContent {
  pointer-events: auto;
  cursor: pointer;
  max-width: 96vw;
  display: flex; flex-wrap: wrap; justify-content: center; align-items: stretch;
  gap: 14px 26px;
  background: rgba(10, 18, 40, .82);
  border: 2px solid rgba(255,255,255,.18);
  border-radius: 28px;
  padding: 18px 38px;
  box-shadow: 0 12px 50px rgba(0,0,0,.5);
  animation: bannerPop .45s cubic-bezier(.2, 1.6, .4, 1);
}
@keyframes bannerPop {
  0% { transform: scale(.5) translateY(-30px); opacity: 0; }
  100% { transform: scale(1) translateY(0); opacity: 1; }
}
.bannerItem { display: flex; flex-direction: column; align-items: center; gap: 4px; }
.bannerLabel {
  font-size: clamp(.8rem, 1.6vw, 1.15rem);
  font-weight: 700; letter-spacing: .06em; text-transform: uppercase;
  padding: 2px 14px; border-radius: 99px; color: #fff;
  text-shadow: 0 1px 3px rgba(0,0,0,.5);
}
.bannerValue {
  font-size: clamp(2.4rem, 8vw, 6.4rem);
  font-weight: 900; line-height: 1.08;
  text-align: center;
  text-shadow: 0 3px 12px rgba(0,0,0,.5);
}
.bannerValue img {
  height: clamp(3.6rem, 11vw, 9rem);
  border-radius: 14px;
  vertical-align: middle;
  background: #fff;
}
.bannerSep { align-self: center; font-size: clamp(1.6rem, 4vw, 3.4rem); opacity: .5; font-weight: 900; }

/* ---------- History ---------- */
#history {
  position: absolute; top: 84px; right: 14px; bottom: 120px;
  width: min(320px, 80vw);
  background: rgba(10, 18, 40, .88);
  border: 1px solid rgba(255,255,255,.15);
  border-radius: 18px;
  padding: 14px;
  z-index: 11;
  display: flex; flex-direction: column;
  backdrop-filter: blur(8px);
}
#history h3 { display: flex; justify-content: space-between; align-items: center; font-size: 1.15rem; margin-bottom: 10px; }
#history h3 button { background: none; border: none; font-size: 1.15rem; cursor: pointer; opacity: .7; }
#history h3 button:hover { opacity: 1; }
#historyList { list-style: none; overflow-y: auto; flex: 1; }
#historyList li {
  padding: 8px 10px; margin-bottom: 6px;
  background: rgba(255,255,255,.07); border-radius: 10px;
  font-size: 1.15rem; font-weight: 700;
  display: flex; flex-wrap: wrap; gap: 6px; align-items: center;
}
#historyList li img { height: 30px; border-radius: 6px; background: #fff; }
#historyList .dot { display: inline-block; width: 12px; height: 12px; border-radius: 50%; }

/* ---------- Bottom bar ---------- */
#bottombar {
  position: absolute; bottom: 0; left: 0; right: 0;
  display: flex; flex-direction: column; align-items: center; gap: 6px;
  padding: 0 16px 20px;
  z-index: 10;
  pointer-events: none;
}
#btnRoll {
  pointer-events: auto;
  font-family: var(--ui-font);
  font-size: clamp(1.6rem, 3.4vw, 2.4rem);
  font-weight: 900; letter-spacing: .04em;
  padding: 18px 64px;
  border: none; border-radius: 99px; cursor: pointer;
  color: #fff;
  background: linear-gradient(180deg, #ff9f43, #ee5a24);
  box-shadow: 0 8px 24px rgba(238, 90, 36, .45), inset 0 2px 0 rgba(255,255,255,.35);
  transition: transform .1s, filter .15s;
}
#btnRoll:hover { filter: brightness(1.08); }
#btnRoll:active { transform: scale(.95); }
#btnRoll:disabled { filter: grayscale(.6) brightness(.8); cursor: default; }
#hint { font-size: .95rem; opacity: .55; }

/* ---------- Tableau blanc ---------- */
#whiteboard {
  position: absolute; left: 0; right: 0; bottom: 0;
  height: 46%;
  background: #fff;
  border-radius: 26px 26px 0 0;
  box-shadow: 0 -10px 40px rgba(0,0,0,.45);
  z-index: 12;
  display: flex; flex-direction: column;
}
#wbTools {
  display: flex; align-items: center; gap: 12px;
  padding: 10px 16px;
  border-bottom: 2px solid #eef1f7;
}
.wbColor {
  width: 46px; height: 46px; border-radius: 50%;
  border: 4px solid #fff; outline: 2px solid #d8deea;
  background: var(--c); cursor: pointer;
  transition: transform .12s;
}
.wbColor.active { outline: 4px solid var(--c); transform: scale(1.12); }
.wbTool {
  width: 52px; height: 52px; font-size: 1.5rem;
  border: none; border-radius: 14px; cursor: pointer;
  background: #eef1f7;
}
.wbTool:hover { background: #dfe5f0; }
.wbTool.active { background: #cdd8ec; outline: 3px solid #3b5bdb; }
#wbClose { margin-left: auto; }
#wbText b { font-size: 1.6rem; font-family: Georgia, serif; }
#wbCanvas { flex: 1; width: 100%; touch-action: none; cursor: crosshair; }
#whiteboard.textMode #wbCanvas { cursor: text; }
.wbTextInput {
  position: absolute; z-index: 13;
  background: rgba(59, 91, 219, .06);
  border: 2px dashed #b8c2dd; border-radius: 8px;
  outline: none; padding: 2px 8px;
  font-family: var(--ui-font); font-weight: 800;
}

/* Quand le tableau est ouvert, tout remonte et le bandeau devient compact */
#app.wb-open #bottombar { bottom: 46%; padding-bottom: 12px; }
#app.wb-open #hint { display: none; }
#app.wb-open #history { bottom: calc(46% + 14px); }
#app.wb-open #banner { top: 64px; }
#app.wb-open #bannerContent { padding: 10px 26px; gap: 6px 18px; border-radius: 18px; }
#app.wb-open .bannerValue { font-size: clamp(1.6rem, 4vw, 3rem); }
#app.wb-open .bannerValue img { height: clamp(2.2rem, 5vw, 3.8rem); }
#app.wb-open .bannerLabel { font-size: .78rem; padding: 1px 10px; }
#app.wb-open .bannerSep { font-size: clamp(1.2rem, 2vw, 2rem); }
#app.wb-open #dice2d { padding: 185px 20px calc(46% + 108px); }
#app.wb-open .die2dLabel { display: none; }

/* ---------- Settings overlay ---------- */
.overlay {
  position: fixed; inset: 0; z-index: 50;
  background: rgba(5, 10, 25, .75);
  backdrop-filter: blur(5px);
  display: flex; justify-content: center; align-items: center;
  padding: 18px;
}
#settingsPanel {
  width: min(1060px, 100%);
  max-height: 94vh;
  overflow-y: auto;
  background: #f4f6fb;
  color: #1d2540;
  border-radius: 22px;
  padding: 22px 26px 30px;
  box-shadow: 0 20px 80px rgba(0,0,0,.6);
}
.panelHeader { display: flex; justify-content: space-between; align-items: center; margin-bottom: 16px; }
.panelHeader h2 { font-size: 1.6rem; }
.bigClose {
  width: 52px; height: 52px; font-size: 1.5rem;
  border: none; border-radius: 14px; cursor: pointer;
  background: #e2e6f0; color: #1d2540;
}
.bigClose:hover { background: #d2d8e8; }

#settingsPanel hr { border: none; border-top: 2px solid #dde2ee; margin: 22px 0; }

#diceCards { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(430px, 100%), 1fr)); gap: 16px; }

.dieCard {
  background: #fff; border-radius: 16px; padding: 14px;
  border: 1px solid #dfe4f0;
  box-shadow: 0 3px 12px rgba(30,40,90,.07);
  min-width: 0;
}
.faceRow { min-width: 0; }
.dieCardHead {
  display: flex; align-items: center; gap: 10px; margin-bottom: 12px; flex-wrap: wrap;
}
.dieCardHead input[type="color"] {
  width: 52px; height: 44px; border: none; border-radius: 10px; cursor: pointer; background: none; padding: 0;
}
.dieCardHead input[type="text"] {
  flex: 1; min-width: 130px;
  font-family: var(--ui-font); font-size: 1.1rem; font-weight: 700;
  padding: 9px 12px; border: 2px solid #dfe4f0; border-radius: 10px;
}
.dieCardHead select {
  font-family: var(--ui-font); font-size: 1.05rem; font-weight: 700;
  padding: 9px 8px; border: 2px solid #dfe4f0; border-radius: 10px; background: #fff; cursor: pointer;
}
.dieRemove {
  width: 44px; height: 44px; font-size: 1.15rem;
  border: none; border-radius: 10px; cursor: pointer;
  background: #ffe4e4; color: #c0392b;
}
.dieRemove:hover { background: #ffd0d0; }

.facesGrid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; min-width: 0; }
@media (max-width: 560px) { .facesGrid { grid-template-columns: minmax(0, 1fr); } }
.faceRow {
  display: flex; align-items: center; gap: 6px;
  background: #f6f8fd; border-radius: 10px; padding: 6px 8px;
}
.faceRow .faceNum { font-weight: 800; opacity: .45; width: 22px; text-align: right; font-size: .95rem; }
.faceRow input[type="text"] {
  flex: 1; min-width: 0;
  font-family: var(--ui-font); font-size: 1.05rem;
  padding: 8px 10px; border: 2px solid #e2e7f2; border-radius: 8px;
}
.faceRow input[type="text"]:disabled { background: #eee; opacity: .5; }
.faceBtn {
  width: 40px; height: 40px; font-size: 1.1rem; flex: none;
  border: none; border-radius: 8px; cursor: pointer; background: #e6ebf7;
}
.faceBtn:hover { background: #d8e0f2; }
.faceThumbWrap { position: relative; flex: none; width: 40px; height: 40px; }
.faceThumbWrap img { width: 40px; height: 40px; object-fit: cover; border-radius: 8px; display: block; }
.faceThumbWrap button {
  position: absolute; top: -7px; right: -7px;
  width: 21px; height: 21px; font-size: .7rem; line-height: 1;
  border: none; border-radius: 50%; cursor: pointer;
  background: #c0392b; color: #fff;
}

#btnAddDie {
  margin-top: 14px;
  font-family: var(--ui-font); font-size: 1.15rem; font-weight: 800;
  padding: 13px 26px; border: 2px dashed #b8c2dd; border-radius: 14px;
  background: none; color: #3f4c78; cursor: pointer; width: 100%;
}
#btnAddDie:hover { background: #eef1f9; }
#btnAddDie:disabled { opacity: .4; cursor: default; }

/* Presets */
#presetsSection h3 { margin-bottom: 12px; font-size: 1.25rem; }
.presetHint {
  font-size: .98rem; line-height: 1.55; color: #3f4c78;
  background: #eef3ff; border-left: 4px solid #3b5bdb;
  padding: 12px 16px; border-radius: 10px;
  margin-bottom: 16px;
}
.presetSave { display: flex; gap: 10px; margin-bottom: 12px; flex-wrap: wrap; }
.presetSave input {
  flex: 1; min-width: 200px;
  font-family: var(--ui-font); font-size: 1.05rem;
  padding: 11px 14px; border: 2px solid #dfe4f0; border-radius: 10px;
}
#presetsSection button, .presetIO button {
  font-family: var(--ui-font); font-size: 1rem; font-weight: 700;
  padding: 11px 18px; border: none; border-radius: 10px; cursor: pointer;
  background: #3b5bdb; color: #fff;
}
#presetsSection button:hover { filter: brightness(1.1); }
#presetList { list-style: none; margin-bottom: 14px; }
#presetList li {
  display: flex; align-items: center; gap: 10px;
  background: #fff; border: 1px solid #dfe4f0; border-radius: 10px;
  padding: 9px 14px; margin-bottom: 7px;
}
#presetList .pname { flex: 1; font-weight: 700; font-size: 1.05rem; }
#presetList button.load { background: #2f9e44; }
#presetList button.del { background: #e03131; padding: 11px 14px; }
.presetIO { display: flex; gap: 10px; flex-wrap: wrap; }
.presetIO button { background: #5f6c8f; }

.hidden { display: none !important; }

/* ---------- Mobile ---------- */
@media (max-width: 720px) {
  #topbar { padding: 8px 10px; gap: 8px; }
  #logo { font-size: 1.05rem; }
  #toolbar { gap: 6px; }
  .tool { width: 46px; height: 46px; font-size: 1.2rem; border-radius: 12px; }
  #btnRoll { font-size: 1.4rem; padding: 14px 42px; }
  #hint { font-size: .8rem; }
  #banner { top: 66px; padding: 0 8px; }
  #bannerContent { padding: 12px 20px; gap: 8px 16px; border-radius: 20px; }
  #history { top: 66px; right: 8px; bottom: 110px; }
  #dice2d { padding: 76px 12px 120px; gap: 4vmin; }
  #wbTools { padding: 8px 10px; gap: 8px; }
  .wbColor { width: 38px; height: 38px; border-width: 3px; }
  .wbTool { width: 44px; height: 44px; font-size: 1.2rem; }
  #settingsPanel { padding: 16px 14px 22px; border-radius: 16px; }
  .panelHeader h2 { font-size: 1.25rem; }
  #app.wb-open #dice2d { padding: 150px 12px calc(46% + 96px); }
}

@media (max-width: 420px) {
  #logo { display: none; }
  #topbar { justify-content: flex-end; }
}
