:root {
  --bg: #f6f2ea;
  --surface: #ffffff;
  --ink: #2a2520;
  --muted: #7a7065;
  --line: #e6dfd3;
  --accent: #d9572b;
  --accent-ink: #ffffff;
  --accent-soft: #fbe6dc;
  --ok: #2f8a5b;
  --danger: #c0392b;
  --tray: #3d6b5a;
  --tray-dark: #2c5144;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(42, 37, 32, .06), 0 6px 20px rgba(42, 37, 32, .07);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color: var(--ink);
}

* { box-sizing: border-box; }
html, body { margin: 0; }
html { -webkit-text-size-adjust: 100%; -webkit-tap-highlight-color: transparent; }
body { overflow-x: hidden; }
body { background: var(--bg); min-height: 100vh; }
[hidden] { display: none !important; }
h1, h2, h3 { margin: 0; line-height: 1.2; }
h1 { font-size: 1.6rem; }
h2 { font-size: 1.1rem; }
h3 { font-size: .95rem; margin-bottom: .75rem; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }

/* ---------- barre du haut ---------- */
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 1rem;
  padding: .75rem 1.25rem; background: var(--surface); border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 5;
}
.brand { display: flex; align-items: center; gap: .6rem; font-weight: 700; font-size: 1.15rem; color: var(--ink); text-decoration: none; }
.brand-mark {
  width: 26px; height: 26px; border-radius: 46% 54% 50% 50% / 55% 45% 55% 45%;
  background: conic-gradient(#f4f1ea, #cfc9bb, #fbfaf6, #d8d2c5, #f4f1ea);
  box-shadow: inset -3px -3px 6px rgba(0,0,0,.18), 0 2px 4px rgba(0,0,0,.15);
}
.user-box { display: flex; align-items: center; gap: .5rem; flex-wrap: wrap; justify-content: flex-end; }
.user-name { font-weight: 600; }

/* ---------- boutons & champs ---------- */
.btn {
  font: inherit; font-weight: 600; font-size: .9rem; cursor: pointer; text-decoration: none;
  border: 1px solid transparent; border-radius: 10px; padding: .55rem 1rem;
  display: inline-flex; align-items: center; justify-content: center; gap: .4rem;
  transition: background .15s, border-color .15s, transform .1s; white-space: nowrap;
}
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn-primary { background: var(--accent); color: var(--accent-ink); }
.btn-primary:hover:not(:disabled) { background: #c24a22; }
.btn-ghost { background: transparent; color: var(--ink); border-color: var(--line); }
.btn-ghost:hover { background: #f1ece3; }
.btn-danger-ghost { background: transparent; color: var(--danger); border-color: #f0cfcb; }
.btn-danger-ghost:hover { background: #fbeceb; }
.btn-sm { padding: .3rem .6rem; font-size: .8rem; }
.btn-block { width: 100%; }
:focus-visible { outline: 3px solid #f0a383; outline-offset: 2px; }

label { display: flex; flex-direction: column; gap: .35rem; font-size: .875rem; font-weight: 600; }
input {
  font: inherit; font-weight: 400; padding: .6rem .75rem; border: 1px solid var(--line);
  border-radius: 10px; background: #fffdf9; color: var(--ink);
}
input:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.form-error { color: var(--danger); font-size: .85rem; min-height: 1.2em; margin: 0; }

.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); }
.badge { font-size: .72rem; font-weight: 700; padding: .15rem .5rem; border-radius: 99px; }
.badge-admin { background: var(--accent-soft); color: var(--accent); }
.badge-open { background: #def2e7; color: var(--ok); }
.badge-closed { background: #ece7df; color: var(--muted); }

/* ---------- connexion ---------- */
.auth { display: grid; place-items: center; padding: 3rem 1rem; }
.auth-card { width: 100%; max-width: 400px; padding: 2rem; display: flex; flex-direction: column; gap: 1rem; }
.auth-card form { display: flex; flex-direction: column; gap: .9rem; }
.auth-card p { margin: 0; }
.tabs { display: grid; grid-template-columns: 1fr 1fr; background: #f1ece3; border-radius: 10px; padding: 4px; }
.tab { font: inherit; font-weight: 600; border: 0; background: transparent; padding: .5rem; border-radius: 8px; cursor: pointer; color: var(--muted); }
.tab.active { background: var(--surface); color: var(--ink); box-shadow: 0 1px 3px rgba(0,0,0,.1); }

/* ---------- mise en page ---------- */
.layout { display: grid; grid-template-columns: 280px minmax(0, 1fr); gap: 1.25rem; padding: 1.25rem; max-width: 1400px; margin: 0 auto; }
.sidebar { display: flex; flex-direction: column; gap: .75rem; min-width: 0; }
.draw-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: .5rem; }
.draw-item {
  display: block; padding: .8rem .9rem; border-radius: 12px; background: var(--surface);
  border: 1px solid var(--line); color: inherit; text-decoration: none; transition: border-color .15s, box-shadow .15s;
}
.draw-item:hover { border-color: #d6cbb9; }
.draw-item.active { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.draw-item-top { display: flex; justify-content: space-between; align-items: center; gap: .5rem; }
.draw-item-title { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.draw-item-sub { display: flex; justify-content: space-between; font-size: .8rem; color: var(--muted); margin-top: .4rem; }
.progress { height: 4px; background: #eee7dc; border-radius: 4px; margin-top: .5rem; overflow: hidden; }
.progress > span { display: block; height: 100%; background: var(--accent); border-radius: 4px; transition: width .3s; }
.my-num { color: var(--accent); font-weight: 700; }

.content { min-width: 0; }
.empty { display: grid; place-items: center; gap: 1rem; padding: 5rem 1rem; color: var(--muted); text-align: center; }
.empty-ball { width: 70px; height: 70px; border-radius: 46% 54% 50% 50% / 55% 45% 55% 45%;
  background: conic-gradient(#f4f1ea, #cfc9bb, #fbfaf6, #d8d2c5, #f4f1ea);
  box-shadow: inset -6px -8px 14px rgba(0,0,0,.18), 0 8px 16px rgba(0,0,0,.12); }

.draw-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 1rem; flex-wrap: wrap; margin-bottom: 1rem; }
.draw-head p { margin: .3rem 0 0; }
.draw-actions { display: flex; gap: .5rem; flex-wrap: wrap; }

.banner {
  padding: .9rem 1.1rem; border-radius: 12px; margin-bottom: 1rem; font-weight: 600;
  background: var(--accent-soft); color: #8a3415; border: 1px solid #f4c9b6;
}
.banner.info { background: #eef4f1; color: var(--tray-dark); border-color: #cfe1d8; }
.banner strong { font-size: 1.25rem; }

/* ---------- le plateau et les papiers froissés ---------- */
.tray {
  display: flex; flex-wrap: wrap; justify-content: center; align-content: flex-start; gap: 18px 14px;
  padding: 2rem 1.25rem; border-radius: 22px; min-height: 260px; margin-bottom: 1.25rem;
  background:
    radial-gradient(ellipse at 50% 0%, rgba(255,255,255,.12), transparent 60%),
    repeating-linear-gradient(45deg, rgba(0,0,0,.025) 0 2px, transparent 2px 6px),
    var(--tray);
  box-shadow: inset 0 6px 24px rgba(0,0,0,.35), inset 0 -2px 0 rgba(255,255,255,.08), var(--shadow);
}

.paper {
  --rot: 0deg; --a: 0deg; --b: 30deg; --r: 46% 54% 50% 50% / 55% 45% 55% 45%;
  position: relative; width: 78px; height: 78px; padding: 0; border: 0; cursor: pointer;
  border-radius: var(--r);
  transform: rotate(var(--rot));
  background:
    radial-gradient(circle at 32% 28%, rgba(255,255,255,.95) 0 12%, transparent 38%),
    conic-gradient(from var(--a), #f5f2eb, #d6d0c2, #fcfbf7, #cbc4b5, #f2eee5, #dad4c7, #f9f7f2, #d0c9bb, #f5f2eb);
  box-shadow:
    inset -7px -9px 14px rgba(0,0,0,.2),
    inset 6px 6px 10px rgba(255,255,255,.85),
    0 10px 14px rgba(0,0,0,.28);
  transition: transform .2s cubic-bezier(.3,1.6,.5,1), box-shadow .2s, filter .2s;
  animation: drop .45s both cubic-bezier(.3,1.4,.5,1);
  animation-delay: var(--delay, 0s);
}
.paper::before, .paper::after {
  content: ""; position: absolute; inset: 10%; border-radius: inherit; pointer-events: none;
  background: repeating-linear-gradient(var(--b), transparent 0 9px, rgba(0,0,0,.06) 9px 10px, rgba(255,255,255,.5) 10px 11px, transparent 11px 17px);
  mix-blend-mode: multiply;
}
.paper::after { inset: 22%; transform: rotate(70deg); opacity: .8; }
.paper:active:not(:disabled) { transform: rotate(var(--rot)) scale(.94); }
@media (hover: hover) {
.paper:hover:not(:disabled) { transform: rotate(calc(var(--rot) + 8deg)) translateY(-6px) scale(1.08); box-shadow: inset -7px -9px 14px rgba(0,0,0,.2), inset 6px 6px 10px rgba(255,255,255,.85), 0 18px 22px rgba(0,0,0,.3); }
}
.paper:disabled { cursor: default; filter: saturate(.6) brightness(.92); }
.paper.picking { animation: wobble .6s infinite; }

@keyframes drop { from { opacity: 0; transform: translateY(-30px) rotate(var(--rot)) scale(.7); } }
@keyframes wobble {
  0%, 100% { transform: rotate(var(--rot)) scale(1.05); }
  50% { transform: rotate(calc(var(--rot) + 14deg)) scale(1.12); }
}

/* papier déplié (déjà tiré) */
.slip {
  --rot: 0deg;
  width: 78px; height: 78px; border-radius: 4px; transform: rotate(var(--rot));
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  background:
    linear-gradient(135deg, transparent 48%, rgba(0,0,0,.05) 50%, transparent 52%),
    linear-gradient(45deg, transparent 48%, rgba(0,0,0,.04) 50%, transparent 52%),
    #fbf9f4;
  box-shadow: 0 3px 6px rgba(0,0,0,.25); opacity: .78;
  animation: unfold .5s both cubic-bezier(.3,1.3,.5,1);
}
.slip-num { font-size: 1.6rem; font-weight: 800; color: var(--ink); line-height: 1; }
.slip-name { font-size: .68rem; color: var(--muted); max-width: 70px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.slip.mine { opacity: 1; background-color: #fff; outline: 3px solid var(--accent); outline-offset: 2px; }
.slip.mine .slip-num { color: var(--accent); }
@keyframes unfold { from { transform: rotate(var(--rot)) scale(.4); border-radius: 50%; } }

/* ---------- résultats ---------- */
.results { padding: 1.1rem 1.25rem; }
.results-head { display: flex; justify-content: space-between; align-items: center; gap: 1rem; flex-wrap: wrap; margin-bottom: .75rem; }
.seg { display: inline-flex; background: #f1ece3; border-radius: 9px; padding: 3px; }
.seg-btn { font: inherit; font-size: .8rem; font-weight: 600; border: 0; background: transparent; padding: .35rem .7rem; border-radius: 7px; cursor: pointer; color: var(--muted); }
.seg-btn.active { background: var(--surface); color: var(--ink); box-shadow: 0 1px 2px rgba(0,0,0,.1); }
.table { width: 100%; border-collapse: collapse; font-size: .9rem; }
.table th { text-align: left; font-size: .75rem; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); font-weight: 600; padding: .5rem .6rem; border-bottom: 1px solid var(--line); }
.table td { padding: .55rem .6rem; border-bottom: 1px solid #f1ece3; }
.table tr.mine td { background: var(--accent-soft); font-weight: 600; }
.table td.num { font-weight: 800; font-variant-numeric: tabular-nums; width: 4rem; }
.table td.right, .table th.right { text-align: right; }
#noResults { margin: .75rem 0 0; }

/* ---------- boîtes de dialogue ---------- */
.dialog {
  border: 0; border-radius: 18px; padding: 1.5rem; width: min(440px, calc(100vw - 32px));
  box-shadow: 0 20px 60px rgba(0,0,0,.25); color: var(--ink);
}
.dialog::backdrop { background: rgba(30, 25, 20, .45); backdrop-filter: blur(2px); }
.dialog h2 { margin-bottom: .5rem; }
.dialog-actions { display: flex; justify-content: flex-end; gap: .5rem; margin-top: 1.25rem; }
.dialog-actions.center { justify-content: center; }

.reveal-dialog { background: transparent; box-shadow: none; overflow: visible; }
.reveal-stage { display: grid; place-items: center; perspective: 800px; }
.reveal-paper {
  width: 240px; height: 240px; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: .4rem;
  background:
    linear-gradient(135deg, transparent 49%, rgba(0,0,0,.06) 50%, transparent 51%),
    linear-gradient(45deg, transparent 49%, rgba(0,0,0,.05) 50%, transparent 51%),
    linear-gradient(0deg, transparent 49.5%, rgba(0,0,0,.04) 50%, transparent 50.5%),
    #fffdf8;
  border-radius: 6px; box-shadow: 0 25px 50px rgba(0,0,0,.35);
  animation: reveal .9s both cubic-bezier(.2,1.2,.4,1);
}
.reveal-label { font-size: .85rem; text-transform: uppercase; letter-spacing: .1em; color: var(--muted); font-weight: 600; }
.reveal-number { font-size: 6rem; font-weight: 900; color: var(--accent); line-height: 1; font-variant-numeric: tabular-nums; }
@keyframes reveal {
  0% { transform: scale(.25) rotate(-200deg); border-radius: 46% 54% 50% 50%; filter: brightness(.85); }
  60% { transform: scale(1.06) rotate(4deg); border-radius: 8px; }
  100% { transform: none; }
}
.reveal-dialog .dialog-actions { margin-top: 1.5rem; }

.admin-dialog { width: min(620px, calc(100vw - 32px)); max-height: calc(100vh - 32px); }
.admin-dialog section { padding-top: 1.1rem; margin-top: 1.1rem; border-top: 1px solid var(--line); }
.admin-head { display: flex; justify-content: space-between; align-items: center; }
.admin-head h2 { margin: 0; }
.grid-form { display: grid; grid-template-columns: 1fr 1fr; gap: .8rem; }
.span-2 { grid-column: span 2; }
.switch { flex-direction: row; align-items: center; gap: .6rem; font-weight: 500; cursor: pointer; }
.switch input { width: 18px; height: 18px; accent-color: var(--accent); }
.row-actions { display: flex; gap: .4rem; justify-content: flex-end; }

/* ---------- notifications ---------- */
.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translate(-50%, 20px); opacity: 0; pointer-events: none;
  background: var(--ink); color: #fff; padding: .7rem 1.1rem; border-radius: 10px; font-size: .9rem;
  transition: opacity .2s, transform .2s; max-width: calc(100vw - 32px); z-index: 50;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.error { background: var(--danger); }

/* ---------- mobile ---------- */
@media (max-width: 800px) {
  .layout { grid-template-columns: minmax(0, 1fr); padding: 1rem; gap: 1rem; }
  /* liste des tirages : défilement horizontal, cartes de largeur fixe */
  .sidebar .draw-list {
    flex-direction: row; overflow-x: auto; padding: 3px 3px .5rem; margin: -3px;
    scroll-snap-type: x mandatory; -webkit-overflow-scrolling: touch;
  }
  .draw-list li { flex: 0 0 min(250px, 78%); scroll-snap-align: start; min-width: 0; }
  .draw-item { height: 100%; }
  h1 { font-size: 1.3rem; overflow-wrap: anywhere; }
  .draw-head { margin-bottom: .75rem; }
  .draw-actions { width: 100%; }
  .draw-actions .btn { flex: 1 1 auto; padding: .5rem .7rem; }
  .banner { padding: .75rem .9rem; font-size: .95rem; }
  .paper, .slip { width: 64px; height: 64px; }
  .slip-num { font-size: 1.3rem; }
  .slip-name { max-width: 58px; }
  .tray { padding: 1.25rem .5rem; gap: 14px 12px; border-radius: 18px; min-height: 200px; }
  .results { padding: .9rem; }
  .results-head h2 { font-size: 1rem; }
  .table { font-size: .875rem; }
  .table th, .table td { padding: .5rem .35rem; }
  .topbar { padding: .6rem 1rem; }
  .brand { font-size: 1.05rem; }
  .user-name, .user-box .badge-admin { display: none; }
  .user-box .btn { padding: .45rem .7rem; font-size: .85rem; }
  .lbl-long { display: none; }
  .grid-form { grid-template-columns: 1fr 1fr; }
  .dialog { padding: 1.25rem; }
  .admin-dialog { width: calc(100vw - 24px); max-height: calc(100dvh - 24px); }
  /* comptes : une carte par ligne (nom + rôle, puis actions) */
  .admin-dialog .table thead { display: none; }
  .admin-dialog .table tr { display: flex; flex-wrap: wrap; align-items: center; gap: .5rem; padding: .7rem 0; border-bottom: 1px solid #f1ece3; }
  .admin-dialog .table td { padding: 0; border: 0; }
  .admin-dialog .table td:first-child { flex: 1 1 auto; min-width: 0; overflow-wrap: anywhere; }
  .admin-dialog .table td:last-child:not(:empty) { flex: 1 0 100%; }
  .row-actions { justify-content: flex-start; }
  .grid-form { align-items: end; }
  .reveal-paper { width: min(240px, 70vw); height: min(240px, 70vw); }
  .reveal-number { font-size: 5rem; }
}
@media (min-width: 801px) { .lbl-short { display: none; } }
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
@media print {
  .topbar, .sidebar, .tray, .draw-actions, .seg, .banner { display: none !important; }
  .layout { display: block; }
}
