:root {
  --bg: #0f172a; --bg2: #1e293b; --panel: #172033; --text: #e2e8f0; --muted: #94a3b8;
  --accent: #22c55e; --accent-ink: #052e16; --danger: #ef4444; --warn: #f59e0b; --line: #334155;
  --radius: 12px;
  /* Taille des cartes : bornée par la largeur ET la hauteur pour que la table tienne sans défiler */
  --card-w: clamp(40px, min(16vw, 9.6vh), 86px);
  --mini-w: clamp(16px, min(4.2vw, 3vh), 26px);
  --ease: cubic-bezier(.2, .8, .2, 1);
  --display: 'Fredoka', system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { background: radial-gradient(1200px 600px at 50% -10%, #1e3a5f 0%, var(--bg) 60%); color: var(--text); font-family: system-ui, -apple-system, Segoe UI, Roboto, sans-serif; -webkit-tap-highlight-color: transparent; overflow-x: hidden; }
/* Grain du tapis */
body::before { content: ''; position: fixed; inset: 0; pointer-events: none; z-index: 0; opacity: .45; background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='140' height='140'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='.85' numOctaves='2' stitchTiles='stitch'/%3E%3CfeColorMatrix values='0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 .07 0'/%3E%3C/filter%3E%3Crect width='140' height='140' filter='url(%23n)'/%3E%3C/svg%3E"); }
#app { position: relative; z-index: 1; min-height: 100%; }
button { font: inherit; cursor: pointer; border: 0; border-radius: 10px; padding: 12px 18px; background: var(--accent); color: var(--accent-ink); font-weight: 700; transition: transform .08s, filter .15s; }
button:hover { filter: brightness(1.08); }
button:active { transform: scale(.97); }
button:disabled { opacity: .45; cursor: not-allowed; filter: none; }
button.ghost { background: transparent; color: var(--muted); border: 1px solid var(--line); font-weight: 600; }
button.small { padding: 8px 12px; font-size: 14px; }
button.icon { padding: 6px 10px; font-size: 16px; line-height: 1; }
button:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
input { font: inherit; padding: 12px 14px; border-radius: 10px; border: 1px solid var(--line); background: var(--bg2); color: var(--text); width: 100%; }
input:focus { outline: 2px solid var(--accent); border-color: transparent; }
h1 { font-family: var(--display); font-size: 40px; letter-spacing: -.5px; margin: 0 0 4px; }
h2 { font-size: 20px; margin: 0 0 12px; }
.muted { color: var(--muted); }
.center { text-align: center; }
.row { display: flex; gap: 10px; align-items: center; }
.grow { flex: 1; }
.stack { display: flex; flex-direction: column; gap: 12px; }

/* ---------- Écrans accueil / salon ---------- */
.screen { max-width: 520px; margin: 0 auto; padding: 32px 16px 48px; min-height: 100%; display: flex; flex-direction: column; justify-content: center; gap: 20px; }
.panel { background: var(--panel); border: 1px solid var(--line); border-radius: 16px; padding: 20px; }
.logo { display: flex; align-items: center; gap: 12px; justify-content: center; }
.logo-cards { display: flex; }
.logo-cards .card { --card-w: 34px; margin-left: -10px; transform: rotate(-8deg); box-shadow: 0 4px 10px rgba(0,0,0,.4); }
.logo-cards .card:nth-child(2) { transform: rotate(2deg); }
.logo-cards .card:nth-child(3) { transform: rotate(12deg); }
.code { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 32px; letter-spacing: 6px; font-weight: 800; }
.link-box { display: flex; gap: 8px; }
.link-box input { font-family: ui-monospace, monospace; font-size: 13px; }
.players { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.players li { display: flex; align-items: center; gap: 10px; padding: 8px 12px; background: var(--bg2); border-radius: 10px; }
.badge { font-size: 11px; padding: 2px 8px; border-radius: 999px; background: var(--line); color: var(--text); font-weight: 700; text-transform: uppercase; letter-spacing: .5px; }
.badge.host { background: #facc15; color: #422006; }
.rules { font-size: 14px; color: var(--muted); line-height: 1.5; }
.rules li { margin-bottom: 6px; }

/* ---------- Avatars ---------- */
.avatar { width: 36px; height: 36px; border-radius: 50%; display: grid; place-items: center; font-family: var(--display); font-weight: 700; font-size: 15px; color: #fff; background: var(--pc, #6d28d9); position: relative; flex: none; text-shadow: 0 1px 2px rgba(0,0,0,.3); }
.avatar.off { filter: grayscale(1) brightness(.6); }
.avatar.active::before { content: ''; position: absolute; inset: -5px; border-radius: 50%; border: 3px solid var(--accent); animation: ring 1.4s ease-in-out infinite; }
@keyframes ring { 0%, 100% { transform: scale(1); opacity: 1; } 50% { transform: scale(1.14); opacity: .5; } }

/* ---------- Cartes (piste D « jeu mobile » : dégradé franc, reflet glossy, bord blanc épais) ---------- */
.card { width: var(--card-w); aspect-ratio: 2 / 3; border-radius: calc(var(--card-w) * .14); display: grid; place-items: center; font-family: var(--display); font-weight: 700; font-size: calc(var(--card-w) * .5); color: #fff; user-select: none; position: relative; border: calc(var(--card-w) * .055) solid #fff; box-shadow: 0 3px 0 rgba(0,0,0,.25), 0 8px 18px rgba(0,0,0,.35); text-shadow: 0 2px 0 rgba(0,0,0,.25); flex: none; overflow: hidden; }
.card::before { content: ''; position: absolute; inset: 0; border-radius: inherit; background: linear-gradient(180deg, rgba(255,255,255,.45), rgba(255,255,255,.08) 48%, rgba(255,255,255,0) 50%); pointer-events: none; }
.card.down { background: linear-gradient(180deg, #3b82f6, #1e3a8a); color: transparent; }
.card.down::after { content: ''; position: absolute; inset: 10%; border-radius: calc(var(--card-w) * .08); background: radial-gradient(circle at 50% 50%, #fff 0 18%, transparent 19%), repeating-conic-gradient(from 0deg, rgba(255,255,255,.22) 0 15deg, transparent 15deg 30deg); opacity: .9; }
.card.removed { background: transparent; box-shadow: none; border: 2px dashed var(--line); }
.card.removed::before { display: none; }
.card.neg { background: linear-gradient(180deg, #8b5cf6, #5b21b6); }
.card.zero { background: linear-gradient(180deg, #38bdf8, #0369a1); }
.card.low { background: linear-gradient(180deg, #4ade80, #15803d); }
.card.mid { background: linear-gradient(180deg, #fde047, #ca8a04); color: #422006; text-shadow: 0 1px 0 rgba(255,255,255,.4); }
.card.high { background: linear-gradient(180deg, #f87171, #b91c1c); }

/* Cellule de grille : deux faces, retournement 3D */
.cell { width: var(--card-w); aspect-ratio: 2 / 3; position: relative; perspective: 700px; flex: none; }
.cell .face { position: absolute; inset: 0; width: 100%; backface-visibility: hidden; -webkit-backface-visibility: hidden; transition: transform .55s var(--ease); }
.cell .face.front { transform: rotateY(-180deg); }
.cell.up .face.back { transform: rotateY(180deg); }
.cell.up .face.front { transform: rotateY(0); }
.cell.removed .face { display: none; }
.cell.removed::after { content: ''; position: absolute; inset: 0; border: 2px dashed var(--line); border-radius: calc(var(--card-w) * .13); opacity: .6; }
.cell.hidden-card .face { visibility: hidden; }
.cell.pick { cursor: pointer; }
.cell.pick .face { box-shadow: 0 0 0 3px var(--accent), 0 3px 0 rgba(0,0,0,.25), 0 6px 16px rgba(34,197,94,.45); animation: pulse 1.2s ease-in-out infinite; }
@keyframes pulse { 0%, 100% { box-shadow: 0 0 0 3px var(--accent), 0 3px 0 rgba(0,0,0,.25), 0 6px 16px rgba(34,197,94,.35); } 50% { box-shadow: 0 0 0 5px var(--accent), 0 3px 0 rgba(0,0,0,.25), 0 8px 22px rgba(34,197,94,.6); } }
.cell.pick:hover { z-index: 2; }
.cell.pick:hover .face { transform: rotateY(var(--fy, 0)) rotateX(var(--rx, 0)) rotateZ(var(--rz, 0)) translateY(-4px) scale(1.04); transition: transform .12s; }
.cell.pick:active .face { transform: rotateY(var(--fy, 0)) scale(.95); }
.cell.up.pick:hover .face.front, .cell.up.pick:active .face.front { --fy: 0; }
.cell.up.pick:hover .face.back, .cell.up.pick:active .face.back { --fy: 180deg; }
.cell:not(.up).pick:hover .face.front, .cell:not(.up).pick:active .face.front { --fy: -180deg; }
/* Distribution : la carte part de la pioche */
.cell.dealing { animation: deal .38s var(--ease) both; animation-delay: var(--delay, 0ms); }
@keyframes deal { from { transform: translate(var(--dx, 0), var(--dy, 0)) rotate(-8deg) scale(.9); opacity: 0; } to { transform: none; opacity: 1; } }
/* Fin de manche : retournement en cascade */
.cell.cascade .face { transition-delay: var(--delay, 0ms); }

.grid { display: grid; grid-template-columns: repeat(4, var(--card-w)); gap: calc(var(--card-w) * .11); justify-content: center; }
.mini { --card-w: var(--mini-w); }
.mini .grid { gap: 3px; }
.mini .card { border-radius: 5px; font-size: calc(var(--card-w) * .6); box-shadow: 0 1px 0 rgba(0,0,0,.3); text-shadow: none; border-width: 1.5px; }
.mini .card.down::after { inset: 8%; border-radius: 2px; }
.mini .cell.removed::after { border-width: 1px; border-radius: 4px; }

/* ---------- Table de jeu : tient dans l'écran ---------- */
.game { height: 100dvh; max-height: 100dvh; display: flex; flex-direction: column; gap: 8px; padding: 8px 10px calc(8px + env(safe-area-inset-bottom)); overflow: hidden; }
.topbar { display: flex; align-items: center; gap: 8px; flex: none; }
.topbar .title { font-family: var(--display); font-weight: 700; font-size: 17px; flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.lastturn { flex: none; background: var(--warn); color: #431407; font-weight: 700; font-size: 13px; text-align: center; padding: 6px 10px; border-radius: 10px; animation: slidein .4s var(--ease); }
@keyframes slidein { from { transform: translateY(-10px); opacity: 0; } }
.board { flex: 1; min-height: 0; display: flex; flex-direction: column; gap: 8px; }

/* Adversaires : bande horizontale sur mobile */
.opps { display: flex; gap: 8px; overflow-x: auto; overflow-y: hidden; flex: none; padding: 4px 2px; scrollbar-width: none; justify-content: safe center; }
.opps::-webkit-scrollbar { display: none; }
.opp { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 6px 8px; display: flex; flex-direction: column; gap: 5px; align-items: center; flex: none; transition: border-color .2s, box-shadow .2s; }
.opp.active { border-color: var(--accent); box-shadow: 0 0 0 2px rgba(34,197,94,.3); }
.opp .who { display: flex; align-items: center; gap: 6px; max-width: 150px; }
.opp .who .avatar { width: 28px; height: 28px; font-size: 12px; }
.opp .who .nm { font-size: 12px; font-weight: 700; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 90px; }
.opp .who .pts { font-size: 11px; color: var(--muted); font-variant-numeric: tabular-nums; }
.opp .sub { font-size: 10px; color: var(--muted); }
.opp .sub.away { color: var(--danger); }

/* Centre : piles */
.table { background: var(--panel); border: 1px solid var(--line); border-radius: 16px; padding: 8px 12px; display: flex; flex-direction: column; gap: 6px; align-items: center; flex: none; --card-w: clamp(36px, min(13vw, 8vh), 70px); }
.piles { display: flex; align-items: flex-end; justify-content: center; gap: clamp(12px, 5vw, 36px); width: 100%; }
.pile { display: flex; flex-direction: column; align-items: center; gap: 4px; font-size: 11px; color: var(--muted); position: relative; }
.pile .stack3 { position: relative; width: var(--card-w); aspect-ratio: 2 / 3; }
.pile .stack3 .card { position: absolute; inset: 0; }
.pile .stack3 .card.under1 { transform: translate(3px, 3px) rotate(2deg); filter: brightness(.85); }
.pile .stack3 .card.under2 { transform: translate(6px, 6px) rotate(-2deg); filter: brightness(.7); }
.pile .stack3 .card.pick { cursor: pointer; box-shadow: 0 0 0 3px var(--accent), 0 6px 16px rgba(34,197,94,.45); animation: pulse 1.2s ease-in-out infinite; }
.pile .stack3 .card.pick:hover { transform: translateY(-4px) scale(1.04); }
.pile .stack3 .card.deck { box-shadow: 0 3px 0 rgba(0,0,0,.25), 3px 3px 0 #0f1a3d, 6px 6px 0 #0b1330, 0 8px 18px rgba(0,0,0,.4); }
.pile .held-slot { width: var(--card-w); aspect-ratio: 2 / 3; border-radius: calc(var(--card-w) * .13); border: 2px dashed var(--line); display: grid; place-items: center; }
.pile .held-slot .card { transform: translateY(-6px) rotate(3deg); box-shadow: 0 10px 24px rgba(0,0,0,.5); }
.pile .held-slot .card.hidden-card { visibility: hidden; }
.prompt { text-align: center; font-weight: 700; font-size: clamp(13px, 3.6vw, 16px); min-height: 20px; line-height: 1.3; }
.prompt.me { color: var(--accent); }
.actions { display: flex; gap: 8px; justify-content: center; flex-wrap: wrap; }
.actions:empty { display: none; }

/* Ma grille */
.mine { flex: 1; min-height: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 6px; }
.mine .who { display: flex; align-items: center; gap: 8px; font-size: 13px; }
.mine .who .pts { color: var(--muted); font-variant-numeric: tabular-nums; }
.mine .meta { display: flex; gap: 12px; font-size: 12px; color: var(--muted); }

/* Ordinateur : trois colonnes */
@media (min-width: 900px) {
  :root { --card-w: clamp(52px, min(7vw, 15.5vh), 110px); --mini-w: clamp(22px, min(3vw, 5vh), 44px); }
  .game { padding: 12px 20px; gap: 12px; max-width: 1400px; margin: 0 auto; }
  .topbar .title { font-size: 20px; }
  .board { display: grid; grid-template-columns: minmax(200px, 1fr) minmax(260px, 1.1fr) auto; gap: 16px; align-items: center; }
  .opps { flex-direction: column; overflow-x: hidden; overflow-y: auto; justify-content: center; align-self: stretch; padding: 4px; }
  .opp { padding: 10px 12px; flex-direction: column; }
  .opp .who { max-width: 100%; }
  .opp .who .nm { max-width: 120px; font-size: 13px; }
  .table { padding: 18px; gap: 12px; --card-w: clamp(56px, min(6vw, 13vh), 96px); align-self: center; }
  .prompt { font-size: 17px; min-height: 44px; display: flex; align-items: center; justify-content: center; }
  .mine { padding-right: 8px; }
}

/* ---------- Couches d'effets ---------- */
#fx { position: fixed; inset: 0; pointer-events: none; z-index: 30; }
#fx .fly { position: absolute; perspective: 700px; transform-origin: center; }
#fx .fly .card { position: absolute; inset: 0; width: 100%; backface-visibility: hidden; -webkit-backface-visibility: hidden; box-shadow: 0 12px 28px rgba(0,0,0,.5); }
#fx .fly .card.front { transform: rotateY(180deg); }
#fx .fly.flipping .card.back { animation: flyBack var(--dur) var(--ease) both; }
#fx .fly.flipping .card.front { animation: flyFront var(--dur) var(--ease) both; }
@keyframes flyBack { from { transform: rotateY(0); } to { transform: rotateY(180deg); } }
@keyframes flyFront { from { transform: rotateY(180deg); } to { transform: rotateY(360deg); } }
#fx .fly.front-only .card.front { transform: none; }
#fx .fly.front-only .card.back { display: none; }
#fx .clear { position: absolute; animation: clearcol .85s ease-in both; animation-delay: var(--delay, 0ms); }
@keyframes clearcol { 0% { transform: scale(1); filter: brightness(1); } 30% { transform: scale(1.14); filter: brightness(1.7); box-shadow: 0 0 0 4px rgba(250,204,21,.7), 0 0 30px rgba(250,204,21,.7); } 100% { transform: scale(.15) rotate(25deg); opacity: 0; } }
#fx .spark { position: absolute; width: 6px; height: 6px; border-radius: 50%; background: #fde047; animation: spark .7s ease-out both; }
@keyframes spark { from { transform: translate(0, 0) scale(1); opacity: 1; } to { transform: translate(var(--sx), var(--sy)) scale(0); opacity: 0; } }

#banner { position: fixed; top: calc(12px + env(safe-area-inset-top)); left: 50%; transform: translateX(-50%) translateY(-40px); z-index: 40; background: var(--accent); color: var(--accent-ink); font-weight: 800; font-size: 18px; padding: 10px 22px; border-radius: 999px; opacity: 0; pointer-events: none; box-shadow: 0 10px 30px rgba(0,0,0,.4); white-space: nowrap; }
#banner.show { animation: banner 2.2s var(--ease) both; }
@keyframes banner { 0% { opacity: 0; transform: translateX(-50%) translateY(-40px); } 15% { opacity: 1; transform: translateX(-50%) translateY(0) scale(1.06); } 25% { transform: translateX(-50%) translateY(0) scale(1); } 80% { opacity: 1; } 100% { opacity: 0; transform: translateX(-50%) translateY(-14px); } }
#confetti { position: fixed; inset: 0; pointer-events: none; z-index: 35; display: none; }
#confetti.on { display: block; }

/* ---------- Modales ---------- */
.overlay { position: fixed; inset: 0; background: rgba(2,6,23,.78); display: grid; place-items: center; padding: 16px; z-index: 20; animation: fadein .3s; }
@keyframes fadein { from { opacity: 0; } }
.modal { background: var(--panel); border: 1px solid var(--line); border-radius: 16px; padding: 22px; width: min(480px, 100%); display: flex; flex-direction: column; gap: 14px; animation: pop .35s var(--ease); max-height: 100%; overflow: auto; }
@keyframes pop { from { transform: translateY(16px) scale(.97); opacity: 0; } }
table.score { width: 100%; border-collapse: collapse; font-size: 15px; }
table.score th, table.score td { padding: 8px 6px; text-align: left; border-bottom: 1px solid var(--line); }
table.score td:not(:first-child), table.score th:not(:first-child) { text-align: right; font-variant-numeric: tabular-nums; }
table.score tr.win td { color: var(--accent); font-weight: 700; }
table.score .pname { display: flex; align-items: center; gap: 8px; }
table.score .pname .avatar { width: 26px; height: 26px; font-size: 11px; }
.stamp { display: inline-block; margin-left: 8px; font-weight: 900; font-size: 13px; color: #f87171; border: 2px solid #f87171; border-radius: 6px; padding: 0 5px; transform: rotate(-10deg) scale(3); opacity: 0; animation: stamp .4s var(--stamp-delay, 1.1s) cubic-bezier(.3, 1.4, .5, 1) both; }
@keyframes stamp { to { transform: rotate(-10deg) scale(1); opacity: 1; } }
.toast { position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); background: #fee2e2; color: #7f1d1d; padding: 10px 16px; border-radius: 10px; font-weight: 600; box-shadow: 0 8px 24px rgba(0,0,0,.4); z-index: 50; max-width: 90vw; }
.toast.ok { background: #dcfce7; color: #14532d; }
.conn { position: fixed; top: 8px; right: 8px; font-size: 12px; background: var(--danger); color: white; padding: 4px 10px; border-radius: 999px; z-index: 45; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; animation-iteration-count: 1 !important; transition-duration: .01ms !important; }
}
