/* ==========================================================================
   PIXEL ROLL — style
   Palette 16 couleurs, bordures "crantées" en box-shadow, pas d'anti-aliasing
   ========================================================================== */

:root {
  --bg: #1a1c2c;
  --ink: #f4f4f4;
  --mute: #94b0c2;
  --slate: #566c86;
  --dark: #333c57;
  --navy: #29366f;
  --blue: #3b5dc9;
  --sky: #41a6f6;
  --cyan: #73eff7;
  --green: #38b764;
  --lime: #a7f070;
  --teal: #257179;
  --purple: #5d275d;
  --red: #b13e53;
  --orange: #ef7d57;
  --yellow: #ffcd75;
  --panel: #262b44;
  --px: 4px;
  --font: 'Press Start 2P', ui-monospace, 'Courier New', monospace;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  min-height: 100%;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font);
  font-size: 12px;
  line-height: 1.6;
  -webkit-font-smoothing: none;
  image-rendering: pixelated;
}

body { overflow-x: hidden; }

[hidden] { display: none !important; }

a { color: inherit; }

canvas#bg, canvas#fx {
  position: fixed;
  inset: 0;
  width: 100vw;
  height: 100vh;
  image-rendering: pixelated;
  pointer-events: none;
}
canvas#bg { z-index: 0; }
canvas#fx { z-index: 50; }

.crt {
  position: fixed;
  inset: 0;
  z-index: 60;
  pointer-events: none;
  background:
    repeating-linear-gradient(to bottom, rgba(0, 0, 0, .18) 0 1px, transparent 1px 3px),
    radial-gradient(ellipse at center, transparent 55%, rgba(0, 0, 0, .55) 100%);
  animation: flicker 6s steps(1) infinite;
}
@keyframes flicker {
  0%, 100% { opacity: 1; }
  47% { opacity: .85; }
  48% { opacity: 1; }
}

.flash {
  position: fixed;
  inset: 0;
  z-index: 55;
  pointer-events: none;
  background: var(--ink);
  opacity: 0;
}
.flash.on { animation: flash .35s steps(4) forwards; }
@keyframes flash { from { opacity: .85; } to { opacity: 0; } }

#app {
  position: relative;
  z-index: 10;
  min-height: 100vh;
  padding: 16px;
  will-change: transform;
}

.muted { color: var(--mute); }
.tiny { font-size: 8px; line-height: 1.8; }

/* ---------- Boîtes pixel ---------- */

.panel {
  background: var(--panel);
  margin: var(--px);
  padding: 16px;
  box-shadow:
    0 calc(-1 * var(--px)) 0 0 var(--ink),
    0 var(--px) 0 0 var(--ink),
    calc(-1 * var(--px)) 0 0 0 var(--ink),
    var(--px) 0 0 0 var(--ink),
    inset calc(-1 * var(--px)) calc(-1 * var(--px)) 0 0 #1f2238,
    0 calc(var(--px) * 3) 0 0 rgba(0, 0, 0, .45);
}

.panel h2 {
  font-size: 12px;
  margin: 0 0 14px;
  color: var(--yellow);
  text-shadow: 2px 2px 0 #000;
}

/* ---------- Boutons ---------- */

.btn {
  --c: var(--slate);
  --l: var(--mute);
  --d: var(--dark);
  --t: var(--ink);
  font: inherit;
  font-size: 12px;
  color: var(--t);
  background: var(--c);
  border: 0;
  margin: var(--px);
  padding: 12px 16px;
  cursor: pointer;
  text-decoration: none;
  display: inline-block;
  text-shadow: 2px 2px 0 rgba(0, 0, 0, .45);
  position: relative;
  box-shadow:
    inset calc(-1 * var(--px)) calc(-1 * var(--px)) 0 0 var(--d),
    inset var(--px) var(--px) 0 0 var(--l),
    0 calc(-1 * var(--px)) 0 0 #000,
    0 var(--px) 0 0 #000,
    calc(-1 * var(--px)) 0 0 0 #000,
    var(--px) 0 0 0 #000,
    0 calc(var(--px) * 2) 0 0 #000;
  transform: translateY(0);
  transition: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}
.btn:hover:not(:disabled) { filter: brightness(1.12); }
.btn:active:not(:disabled) {
  transform: translateY(var(--px));
  box-shadow:
    inset var(--px) var(--px) 0 0 var(--d),
    0 calc(-1 * var(--px)) 0 0 #000,
    0 var(--px) 0 0 #000,
    calc(-1 * var(--px)) 0 0 0 #000,
    var(--px) 0 0 0 #000;
}
.btn:focus-visible { outline: var(--px) dashed var(--yellow); outline-offset: 8px; }
.btn:disabled {
  --c: var(--dark);
  --l: var(--slate);
  --d: #222639;
  --t: var(--slate);
  cursor: not-allowed;
  text-shadow: none;
}

.btn-green { --c: var(--green); --l: var(--lime); --d: var(--teal); }
.btn-blue { --c: var(--blue); --l: var(--sky); --d: var(--navy); }
.btn-red { --c: var(--red); --l: var(--orange); --d: var(--purple); }
.btn-yellow { --c: var(--yellow); --l: #fff3d4; --d: var(--orange); --t: var(--bg); }
.btn-yellow { text-shadow: none; }
.btn-dark { --c: var(--dark); --l: var(--slate); --d: #222639; }

.btn-big { font-size: 16px; padding: 18px 26px; }
.btn-small { font-size: 9px; padding: 9px 10px; }

/* ---------- Inputs ---------- */

.input {
  font: inherit;
  font-size: 14px;
  text-transform: uppercase;
  color: var(--ink);
  background: var(--bg);
  border: 0;
  margin: var(--px);
  padding: 12px 14px;
  width: 100%;
  box-shadow:
    inset var(--px) var(--px) 0 0 #000,
    0 calc(-1 * var(--px)) 0 0 var(--mute),
    0 var(--px) 0 0 var(--mute),
    calc(-1 * var(--px)) 0 0 0 var(--mute),
    var(--px) 0 0 0 var(--mute);
  caret-color: var(--yellow);
}
.input:focus { outline: none; box-shadow:
    inset var(--px) var(--px) 0 0 #000,
    0 calc(-1 * var(--px)) 0 0 var(--yellow),
    0 var(--px) 0 0 var(--yellow),
    calc(-1 * var(--px)) 0 0 0 var(--yellow),
    var(--px) 0 0 0 var(--yellow); }
.input::placeholder { color: var(--slate); }

/* ---------- Accueil ---------- */

.view { animation: boot .4s steps(4) both; }
@keyframes boot {
  from { opacity: 0; transform: translateY(12px); }
  to { opacity: 1; transform: none; }
}

.home-inner {
  min-height: calc(100vh - 32px);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 18px;
  text-align: center;
}

.logo-die {
  width: 96px;
  height: 96px;
  image-rendering: pixelated;
  animation: bob 1.2s steps(2) infinite;
}
@keyframes bob { 50% { transform: translateY(-8px) rotate(-6deg); } }

.logo {
  margin: 0;
  font-size: clamp(28px, 9vw, 64px);
  line-height: 1.1;
  color: var(--yellow);
  text-shadow:
    4px 4px 0 var(--orange),
    8px 8px 0 var(--red),
    12px 12px 0 var(--purple);
  letter-spacing: 2px;
}
.logo span { display: block; color: var(--cyan); text-shadow: 4px 4px 0 var(--sky), 8px 8px 0 var(--blue), 12px 12px 0 var(--navy); }
.logo.small { font-size: clamp(24px, 7vw, 44px); }

.tagline { color: var(--mute); margin: 8px 0 18px; line-height: 2; }

.join-form { display: flex; gap: 8px; align-items: center; width: min(360px, 100%); }
.join-form .input { text-align: center; letter-spacing: 4px; }
.join-form .btn { flex-shrink: 0; }

.blink { animation: blink 1s steps(1) infinite; }
@keyframes blink { 50% { opacity: 0; } }
.insert-coin { color: var(--yellow); font-size: 10px; margin-top: 24px; }

/* ---------- Session ---------- */

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
  max-width: 1040px;
  margin: 0 auto 20px;
}
.mini-logo {
  text-decoration: none;
  font-size: 16px;
  color: var(--yellow);
  text-shadow: 3px 3px 0 var(--red);
}
.mini-logo span { color: var(--cyan); text-shadow: 3px 3px 0 var(--blue); }
.topbar-right { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.code-chip {
  font-size: 12px;
  padding: 8px 10px;
  color: var(--mute);
  background: #000;
  margin: var(--px);
}
.code-chip b { color: var(--lime); letter-spacing: 2px; }

.layout {
  display: grid;
  grid-template-columns: minmax(0, 1.35fr) minmax(0, 1fr);
  gap: 20px;
  max-width: 1040px;
  margin: 0 auto;
  align-items: start;
}
.side { display: flex; flex-direction: column; gap: 20px; }

.stage {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  padding: 18px 16px 22px;
  min-height: 520px;
}
.stage-head { width: 100%; display: flex; justify-content: space-between; align-items: center; }
.round-label { color: var(--cyan); font-size: 11px; }
.round-label.tiebreak { color: var(--orange); animation: blink .6s steps(1) infinite; }
.conn { color: var(--green); font-size: 10px; }
.conn.off { color: var(--red); animation: blink .5s steps(1) infinite; }

.stage-name {
  margin-top: 22px;
  min-height: 20px;
  font-size: 13px;
  color: var(--yellow);
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* ---------- Le dé ---------- */

.die-wrap { position: relative; margin: 26px 0 18px; }
.die {
  --face: var(--ink);
  --side: var(--mute);
  --hi: #ffffff;
  --num: var(--bg);
  width: 176px;
  height: 176px;
  display: grid;
  place-items: center;
  background: var(--face);
  position: relative;
  z-index: 1;
  box-shadow:
    inset calc(-2 * var(--px)) calc(-2 * var(--px)) 0 0 var(--side),
    inset var(--px) var(--px) 0 0 var(--hi),
    0 calc(-1 * var(--px)) 0 0 #000,
    0 var(--px) 0 0 #000,
    calc(-1 * var(--px)) 0 0 0 #000,
    var(--px) 0 0 0 #000;
}
.die span {
  font-size: 52px;
  color: var(--num);
  text-shadow: 4px 4px 0 rgba(0, 0, 0, .18);
  letter-spacing: -2px;
}
.die-shadow {
  position: absolute;
  left: 16px;
  right: 16px;
  bottom: -18px;
  height: 8px;
  background: rgba(0, 0, 0, .5);
}

.die.tier-none { --face: var(--dark); --side: #222639; --hi: var(--slate); --num: var(--mute); }
.die.tier-0 { --face: var(--red); --side: var(--purple); --hi: var(--orange); --num: var(--bg); }
.die.tier-low { --face: var(--mute); --side: var(--slate); --hi: var(--ink); --num: var(--bg); }
.die.tier-mid { --face: var(--sky); --side: var(--blue); --hi: var(--cyan); --num: var(--bg); }
.die.tier-good { --face: var(--lime); --side: var(--green); --hi: #e4ffc4; --num: var(--bg); }
.die.tier-high { --face: var(--yellow); --side: var(--orange); --hi: #fff3d4; --num: var(--bg); }
.die.tier-100 { animation: rainbow .4s steps(1) infinite; }
@keyframes rainbow {
  0% { --face: var(--yellow); --side: var(--orange); }
  25% { --face: var(--lime); --side: var(--green); }
  50% { --face: var(--cyan); --side: var(--sky); }
  75% { --face: var(--orange); --side: var(--red); }
}

.die.rolling { animation: wobble .16s steps(2) infinite; --face: var(--ink); --side: var(--mute); --num: var(--bg); }
@keyframes wobble {
  0% { transform: translate(-6px, -10px) rotate(-8deg); }
  50% { transform: translate(6px, -4px) rotate(7deg); }
}
.die.land { animation: land .5s steps(5); }
@keyframes land {
  0% { transform: scale(1.35, .7); }
  30% { transform: scale(.85, 1.2) translateY(-14px); }
  60% { transform: scale(1.08, .94); }
  100% { transform: none; }
}
.die.tier-100.land { animation: land .5s steps(5), rainbow .4s steps(1) infinite; }

.stage-msg {
  min-height: 34px;
  font-size: 16px;
  margin: 14px 0 10px;
  color: var(--ink);
  text-shadow: 3px 3px 0 #000;
}
.stage-msg.pop { animation: pop .45s steps(4); }
@keyframes pop { 0% { transform: scale(2.2); opacity: 0; } 60% { transform: scale(.9); opacity: 1; } }
.stage-msg.crit { color: var(--yellow); }
.stage-msg.fumble { color: var(--red); }
.stage-msg.winner { color: var(--lime); font-size: 14px; line-height: 1.8; animation: pop .45s steps(4), glow .6s steps(1) .45s infinite; }
.stage-msg.tie { color: var(--orange); font-size: 14px; line-height: 1.8; animation: pop .45s steps(4); }
@keyframes glow { 50% { color: var(--yellow); } }

.btn-roll { min-width: 220px; font-size: 22px; padding: 22px 30px; }
.btn-roll:not(:disabled) { animation: pulse 1s steps(2) infinite; }
@keyframes pulse { 50% { --c: #44cc72; transform: translateY(-3px); } }

.hint { color: var(--mute); font-size: 9px; min-height: 16px; margin: 14px 0 6px; }

.round-actions { display: flex; flex-wrap: wrap; justify-content: center; gap: 6px; }

/* ---------- Joueurs ---------- */

.players, .history { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }

.player {
  display: grid;
  grid-template-columns: 32px 1fr auto;
  align-items: center;
  gap: 10px;
  padding: 8px 10px;
  background: #1f2238;
  position: relative;
}
.player.me { background: #2b3156; box-shadow: inset var(--px) 0 0 0 var(--yellow); }
.player.offline { opacity: .45; }
.player.leader { background: #3a3a2a; box-shadow: inset var(--px) 0 0 0 var(--yellow), 0 0 0 2px var(--yellow); }
.player.out .p-val { color: var(--slate); }
.player.enter { animation: slidein .35s steps(4); }
@keyframes slidein { from { transform: translateX(40px); opacity: 0; } }

.avatar { width: 32px; height: 32px; image-rendering: pixelated; display: block; }
.p-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 10px;
  background: none;
  border: 0;
  color: inherit;
  font-family: inherit;
  text-align: left;
  padding: 0;
}
.player.me .p-name { cursor: pointer; color: var(--yellow); }
.player.me .p-name:hover { text-decoration: underline; text-decoration-thickness: 2px; }
.p-val { font-size: 14px; min-width: 44px; text-align: right; color: var(--mute); }
.p-val.done { color: var(--ink); }
.p-val.rolling { color: var(--yellow); }
.p-val.v-0 { color: var(--red); }
.p-val.v-100 { color: var(--yellow); }
.p-val.reveal { animation: pop .35s steps(3); }
.crown {
  position: absolute;
  left: 18px;
  top: -10px;
  width: 24px;
  height: 18px;
  image-rendering: pixelated;
  animation: bob 1s steps(2) infinite;
}

.history li { font-size: 9px; display: flex; justify-content: space-between; gap: 8px; color: var(--mute); }
.history li b { color: var(--ink); font-weight: normal; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.history li .hv { color: var(--yellow); flex-shrink: 0; }

/* ---------- Dialog ---------- */

.dialog {
  border: 0;
  padding: 26px 22px;
  background: var(--panel);
  color: var(--ink);
  font-family: var(--font);
  width: min(380px, calc(100vw - 40px));
  box-shadow:
    0 -4px 0 0 var(--ink), 0 4px 0 0 var(--ink), -4px 0 0 0 var(--ink), 4px 0 0 0 var(--ink),
    0 16px 0 0 rgba(0, 0, 0, .5);
  text-align: center;
}
.dialog::backdrop { background: rgba(10, 10, 20, .8); }
.dialog h2 { margin: 0 0 18px; color: var(--yellow); font-size: 14px; }
.dialog[open] { animation: boot .3s steps(3); }
.dialog .btn { margin-top: 18px; }
.name-preview { display: flex; justify-content: center; margin-top: 14px; min-height: 48px; }
.name-preview canvas { width: 48px; height: 48px; image-rendering: pixelated; }

/* ---------- Toasts ---------- */

.toasts {
  position: fixed;
  z-index: 70;
  left: 50%;
  bottom: 18px;
  transform: translateX(-50%);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  pointer-events: none;
  width: max-content;
  max-width: calc(100vw - 24px);
}
.toast {
  font-size: 9px;
  background: #000;
  color: var(--ink);
  padding: 10px 14px;
  box-shadow: 0 -4px 0 0 var(--yellow), 0 4px 0 0 var(--yellow), -4px 0 0 0 var(--yellow), 4px 0 0 0 var(--yellow);
  animation: toast-in .3s steps(3);
}
.toast.out { animation: toast-out .3s steps(3) forwards; }
@keyframes toast-in { from { transform: translateY(20px); opacity: 0; } }
@keyframes toast-out { to { transform: translateY(20px); opacity: 0; } }

/* ---------- Responsive ---------- */

@media (max-width: 760px) {
  #app { padding: 10px; }
  .layout { grid-template-columns: 1fr; }
  .stage { min-height: 0; }
  .die { width: 140px; height: 140px; }
  .die span { font-size: 42px; }
  .btn-roll { font-size: 18px; min-width: 200px; }
  .topbar { justify-content: center; }
  .mini-logo { font-size: 13px; }
}

@media (prefers-reduced-motion: reduce) {
  .crt, .btn-roll:not(:disabled), .logo-die, .crown, .blink { animation: none !important; }
}
