:root {
  --felt: #1d4f37;
  --felt-dark: #143826;
  --felt-light: #2a6a4a;
  --wood: #5a3a22;
  --paper: #fbf5e6;
  --paper-2: #f2e8cf;
  --ink: #2c1d12;
  --muted: #7a6650;
  --red: #b3261e;
  --gold: #d9a33a;
  --team-a: #3b7bd1;
  --team-b: #e08a2a;
  --radius: 14px;
  --card-w: clamp(64px, 21vw, 104px);
  --tcard-w: clamp(56px, 19vw, 92px);
  font-family: "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, serif;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: radial-gradient(ellipse at 50% 30%, var(--felt-light), var(--felt) 55%, var(--felt-dark));
  color: var(--ink);
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}
button, input { font: inherit; color: inherit; }
[hidden] { display: none !important; }
.small { font-size: .85rem; }
.muted { color: var(--muted); }
.center { text-align: center; }

/* ---------- Accueil / salon ---------- */
.screen { min-height: 100dvh; }
#home, #lobby { display: grid; place-items: center; padding: 20px 16px calc(20px + env(safe-area-inset-bottom)); }
.home-card {
  width: min(440px, 100%);
  background: var(--paper);
  border-radius: 20px;
  padding: 24px 20px;
  box-shadow: 0 20px 50px rgba(0,0,0,.35), inset 0 0 0 1px #e3d4b0;
}
.brand { text-align: center; margin-bottom: 8px; }
.brand h1 { font-size: 2.6rem; margin: 6px 0 0; letter-spacing: .02em; color: var(--red); font-style: italic; }
.tagline { margin: 2px 0 0; color: var(--muted); }
.brand-cards { display: flex; justify-content: center; height: 92px; }
.brand-cards .card { width: 54px; margin: 0 -10px; box-shadow: 0 4px 10px rgba(0,0,0,.25); }
.brand-cards .card:nth-child(1) { transform: rotate(-14deg) translateY(10px); }
.brand-cards .card:nth-child(3) { transform: rotate(14deg) translateY(10px); }
h2 { font-size: 1.1rem; margin: 0 0 10px; }
.block { border-top: 1px dashed #d7c59d; padding-top: 16px; margin-top: 16px; }
.field { display: grid; gap: 6px; margin-top: 14px; }
.field span { font-weight: 700; }
input {
  width: 100%; padding: 12px 14px; border-radius: 12px; border: 1.5px solid #d4c29a;
  background: #fffdf7; font-size: 1.05rem;
}
input:focus { outline: 3px solid rgba(217,163,58,.45); border-color: var(--gold); }
#codeInput { text-transform: uppercase; letter-spacing: .3em; text-align: center; font-weight: 700; }
.row { display: flex; gap: 8px; align-items: center; }
.row.gap { gap: 10px; margin-top: 14px; }
.grow { flex: 1; }
.btn {
  border: 0; border-radius: 12px; padding: 11px 16px; cursor: pointer; font-weight: 700;
  background: var(--paper-2); box-shadow: inset 0 0 0 1.5px #d4c29a; white-space: nowrap;
  transition: transform .08s ease, filter .15s;
}
.btn:active { transform: scale(.97); }
.btn:hover { filter: brightness(1.03); }
.btn.primary { background: var(--red); color: #fff; box-shadow: 0 3px 0 #7d1913; }
.btn.big { width: 100%; padding: 14px; font-size: 1.1rem; margin-top: 12px; }
.btn.wide { width: 100%; margin-top: 8px; }
.btn.ghost { background: transparent; box-shadow: none; color: var(--muted); }
.btn.danger { color: var(--red); }
.btn:disabled { opacity: .5; cursor: default; }
.link { display: block; margin: 18px auto 0; background: none; border: 0; color: var(--muted); text-decoration: underline; cursor: pointer; }
.hint { color: var(--muted); font-size: .95rem; margin: 8px 0; }
.seg { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; background: var(--paper-2); padding: 4px; border-radius: 12px; }
.seg button { border: 0; background: none; padding: 10px 4px; border-radius: 9px; cursor: pointer; font-size: .92rem; }
.seg button.on { background: #fff; box-shadow: 0 1px 4px rgba(0,0,0,.15); font-weight: 700; }
.seg.small { margin: 10px 0; }

.lobby-head { display: flex; justify-content: space-between; align-items: center; }
.code { font-size: 2.2rem; font-weight: 700; letter-spacing: .25em; color: var(--red); }
.seats { display: grid; gap: 8px; margin-top: 12px; }
.seat {
  display: flex; align-items: center; gap: 10px; padding: 10px 12px; border-radius: 12px;
  background: #fffdf7; box-shadow: inset 0 0 0 1.5px #e3d4b0;
}
.seat.empty { color: var(--muted); border: 1.5px dashed #d4c29a; box-shadow: none; background: transparent; }
.seat .dot { width: 10px; height: 10px; border-radius: 50%; background: #9ab89f; flex: none; }
.seat .dot.off { background: #c9b9a0; }
.seat .nm { flex: 1; font-weight: 700; }
.seat .tag { font-size: .78rem; padding: 2px 8px; border-radius: 99px; background: var(--paper-2); color: var(--muted); }
.seat .mini { border: 0; background: none; color: var(--muted); cursor: pointer; text-decoration: underline; font-size: .85rem; }
.team-label { font-size: .8rem; font-weight: 700; text-transform: uppercase; letter-spacing: .08em; margin-top: 6px; }
.team-a { color: var(--team-a); }
.team-b { color: var(--team-b); }

/* ---------- Table ---------- */
#game {
  display: grid;
  grid-template-rows: auto auto 1fr auto auto auto;
  height: 100dvh; max-width: 900px; margin: 0 auto;
  padding: env(safe-area-inset-top) 10px calc(10px + env(safe-area-inset-bottom));
  color: #fff;
}
.topbar { display: flex; align-items: center; gap: 8px; padding: 8px 0 4px; }
.icon-btn {
  position: relative; width: 42px; height: 42px; border-radius: 12px; border: 0; cursor: pointer;
  background: rgba(0,0,0,.22); color: #fff; font-size: 1.15rem; flex: none;
}
.badge { position: absolute; top: -4px; right: -4px; background: var(--red); color: #fff; font-size: .7rem; border-radius: 99px; min-width: 18px; padding: 1px 5px; }
.scoreboard { flex: 1; display: flex; justify-content: center; gap: 6px; flex-wrap: wrap; }
.score-pill { background: rgba(0,0,0,.25); border-radius: 99px; padding: 5px 12px; font-size: .9rem; display: flex; gap: 8px; align-items: center; }
.score-pill b { font-size: 1.1rem; color: var(--gold); }
.score-pill .sw { width: 8px; height: 8px; border-radius: 50%; }

.opponents { display: flex; justify-content: center; gap: 8px; padding: 6px 0; flex-wrap: wrap; }
.opp {
  display: flex; align-items: center; gap: 8px; padding: 6px 10px 6px 6px; border-radius: 14px;
  background: rgba(0,0,0,.2); min-width: 0; transition: box-shadow .2s, background .2s;
  border-left: 4px solid transparent;
}
.opp.turn { background: rgba(255,214,120,.18); box-shadow: 0 0 0 2px var(--gold); }
.opp.off { opacity: .55; }
.opp .backs { display: flex; height: 40px; }
.opp .backs .card { width: 25px; margin-right: -14px; }
.opp .info { line-height: 1.15; margin-left: 12px; min-width: 0; }
.opp .nm { font-weight: 700; font-size: .95rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 120px; }
.opp .st { font-size: .78rem; opacity: .85; }
.side-a { border-left-color: var(--team-a); }
.side-b { border-left-color: var(--team-b); }

.felt {
  position: relative; display: flex; flex-direction: column; min-height: 0;
  margin: 6px 0; border-radius: 22px; padding: 10px;
  background: radial-gradient(ellipse at center, rgba(255,255,255,.06), rgba(0,0,0,.12));
  box-shadow: inset 0 0 0 2px rgba(255,255,255,.08), inset 0 0 40px rgba(0,0,0,.25);
}
.table-meta { display: flex; align-items: flex-start; gap: 10px; min-height: 52px; }
.deck { position: relative; width: 34px; height: 54px; flex: none; }
.deck .card { position: absolute; width: 34px; }
.deck .count { position: absolute; inset: auto 0 -2px 0; text-align: center; font-weight: 700; font-size: .8rem; text-shadow: 0 1px 2px #000; }
.last-move { flex: 1; font-size: .9rem; min-height: 1.2em; display: flex; flex-wrap: wrap; align-items: center; gap: 4px 6px; }
.last-move .mini-card { width: 26px; display: inline-block; vertical-align: middle; }
.table-cards {
  flex: 1; display: flex; flex-wrap: wrap; align-content: center; justify-content: center;
  gap: 8px; padding: 8px 0; min-height: 0; overflow: auto;
}
.table-cards .card { width: var(--tcard-w); transition: transform .2s, box-shadow .2s, opacity .2s; }
.table-cards .card.target { transform: translateY(-6px); box-shadow: 0 0 0 3px var(--gold), 0 8px 16px rgba(0,0,0,.4); }
.table-cards .card.dim { opacity: .55; }
.table-cards .card.fresh { animation: drop .35s ease-out; }
.table-empty { opacity: .6; font-style: italic; align-self: center; }

.card { display: block; aspect-ratio: 100/160; border-radius: 7%; box-shadow: 0 3px 8px rgba(0,0,0,.35); background: var(--paper); }
.card svg { display: block; width: 100%; height: 100%; }

.me-bar { display: flex; justify-content: space-between; align-items: center; font-size: .9rem; padding: 2px 4px; gap: 8px; }
.me-bar .turn-flag { font-weight: 700; color: var(--gold); }
.action { min-height: 46px; display: flex; flex-wrap: wrap; justify-content: center; align-items: center; gap: 8px; padding: 4px 0; }
.action .btn { padding: 9px 14px; }
.opt { display: flex; align-items: center; gap: 4px; }
.opt .mini-card { width: 24px; }
.hand { display: flex; justify-content: center; gap: 10px; padding: 6px 0 4px; min-height: calc(var(--card-w) * 1.6 + 14px); }
.hand .card {
  width: var(--card-w); cursor: pointer; border: 0; padding: 0;
  transition: transform .15s ease, box-shadow .15s ease;
}
.hand .card.sel { transform: translateY(-16px); box-shadow: 0 0 0 3px var(--gold), 0 12px 22px rgba(0,0,0,.45); }
.hand.wait .card { filter: saturate(.8) brightness(.92); cursor: default; }
.hand .card:focus-visible { outline: 3px solid var(--gold); outline-offset: 2px; }

.scopa-burst {
  position: absolute; inset: 0; display: grid; place-items: center; pointer-events: none;
  font-size: clamp(2.6rem, 12vw, 5rem); font-style: italic; font-weight: 700; color: var(--gold);
  text-shadow: 0 3px 0 #7d1913, 0 8px 24px rgba(0,0,0,.6);
  animation: burst 1.6s ease-out forwards;
}
@keyframes burst { 0% { transform: scale(.4); opacity: 0; } 20% { transform: scale(1.1); opacity: 1; } 75% { opacity: 1; } 100% { transform: scale(1); opacity: 0; } }
@keyframes drop { from { transform: translateY(-14px) scale(.9); opacity: 0; } to { transform: none; opacity: 1; } }

/* ---------- Modales ---------- */
.modal { position: fixed; inset: 0; background: rgba(10,20,14,.6); display: grid; place-items: end center; z-index: 20; padding: 0 0 env(safe-area-inset-bottom); }
@media (min-width: 640px) { .modal { place-items: center; } }
.sheet {
  width: min(520px, 100%); max-height: 88dvh; overflow: auto; background: var(--paper); color: var(--ink);
  border-radius: 20px 20px 0 0; padding: 20px 18px 18px; box-shadow: 0 -10px 40px rgba(0,0,0,.4);
  animation: up .25s ease-out;
}
@media (min-width: 640px) { .sheet { border-radius: 20px; } }
@keyframes up { from { transform: translateY(30px); opacity: 0; } to { transform: none; opacity: 1; } }
.rules p, .rules li { line-height: 1.45; }
.rules h3 { margin: 16px 0 4px; font-size: 1rem; }
.rules ul { padding-left: 20px; }
table.sum { width: 100%; border-collapse: collapse; margin: 10px 0; font-size: .95rem; }
table.sum th, table.sum td { padding: 7px 6px; text-align: center; border-bottom: 1px solid #e6d8b8; }
table.sum th:first-child, table.sum td:first-child { text-align: left; }
table.sum .win { font-weight: 700; color: var(--red); }
table.sum tr.total td { font-weight: 700; font-size: 1.05rem; border-bottom: 0; }
table.sum .pts { color: var(--muted); font-size: .8rem; display: block; }
.winner-title { text-align: center; font-size: 1.6rem; color: var(--red); margin: 4px 0; font-style: italic; }
.chat-sheet { display: flex; flex-direction: column; }
.chat-log { height: 40dvh; overflow: auto; background: #fffdf7; border-radius: 12px; padding: 10px; margin-bottom: 8px; box-shadow: inset 0 0 0 1.5px #e3d4b0; }
.chat-log .msg { margin: 4px 0; line-height: 1.35; }
.chat-log .msg b { margin-right: 4px; }
.quick { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.quick button { border: 0; background: var(--paper-2); border-radius: 99px; padding: 6px 10px; cursor: pointer; }

.toast {
  position: fixed; left: 50%; bottom: calc(24px + env(safe-area-inset-bottom)); transform: translateX(-50%);
  background: #22170e; color: #fff; padding: 10px 16px; border-radius: 12px; z-index: 40; max-width: 90vw;
  box-shadow: 0 8px 24px rgba(0,0,0,.4); animation: up .2s ease-out;
}
.conn { position: fixed; top: 0; left: 0; right: 0; background: var(--red); color: #fff; text-align: center; padding: 6px; z-index: 50; font-size: .9rem; }

@media (min-width: 700px) {
  :root { --card-w: 108px; --tcard-w: 86px; }
}
@media (max-height: 640px) {
  :root { --card-w: clamp(56px, 17vw, 86px); --tcard-w: clamp(44px, 12vw, 68px); }
}
@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; transition: none !important; }
}
