/* Mise en page mobile-first (380 px), puis 2-3 colonnes sur ordinateur. */
:root {
  --bg: #efe4cc;
  --bg-2: #e6d7b6;
  --paper: #fbf5e6;
  --paper-dark: #e9dcc0;
  --card-edge: #b99a66;
  --ink: #3b2414;
  --ink-soft: #6a4c33;
  --muted: #7c6a55;
  --banner: #5b2a1f;
  --gold: #e3b34c;
  --gold-dark: #a77b23;
  --atk: #c0392b;
  --def: #2f5f9e;
  --xp: #d9a41e;
  --hp: #3a8a3f;
  --focus: #2a9d8f;
  --danger: #b3261e;
  --font-title: Georgia, "Times New Roman", serif;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root { --bg: #2a2119; --bg-2: #352a20; --ink: #f1e6d2; --ink-soft: #d9c6a8; --muted: #b7a58c; color-scheme: dark; }
  .card { --ink: #3b2414; --ink-soft: #6a4c33; }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
/* Pas de « tirer pour rafraîchir » ni de rebond qui interromprait un glisser */
html { overscroll-behavior-y: none; }
body {
  font: 15px/1.4 var(--font);
  background: var(--bg);
  color: var(--ink);
  -webkit-tap-highlight-color: transparent;
}
button { font: inherit; color: inherit; cursor: pointer; }
h1, h2, h3 { font-family: var(--font-title); margin: 0; letter-spacing: .02em; }

.app { max-width: 1200px; margin: 0 auto; min-height: 100%; display: flex; flex-direction: column; }

/* Barre du haut + bandeau de phase */
.topbar {
  position: sticky; top: 0; z-index: 20;
  background: var(--banner); color: #f6e7c8;
  padding: 8px 16px; display: flex; align-items: center; gap: 8px;
}
.topbar h1 { font-size: 1rem; color: var(--gold); flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar .round { font-size: .85rem; opacity: .9; }
.icon-btn { background: none; border: 0; padding: 6px 8px; border-radius: 8px; color: inherit; font-size: 1.2rem; line-height: 1; }
.sync { font-size: .9rem; min-width: 1.2em; text-align: center; opacity: .85; }
.sync.error, .sync.offline { color: #ffb4a8; opacity: 1; }
.icon-btn:disabled { opacity: .35; cursor: default; }
.icon-btn:hover { background: rgb(255 255 255 / .1); }

.phase-banner {
  position: sticky; top: 42px; z-index: 19;
  background: var(--bg-2); border-bottom: 1px solid var(--card-edge);
  padding: 8px 16px; display: flex; align-items: center; gap: 10px;
}
.phase-banner .phase-text { flex: 1; min-width: 0; font-size: .9rem; line-height: 1.3; }
.phase-banner .btn { flex: none; }
.phase-banner .phase-name { font-family: var(--font-title); font-weight: 700; }
.phase-banner .hint { color: var(--ink-soft); }
.phase-banner.alert { background: #f7d9a8; }
@media (prefers-color-scheme: dark) { .phase-banner.alert { background: #5a4220; } }

.btn {
  border: 0; border-radius: 10px; padding: 9px 14px; font-weight: 600;
  background: var(--paper); color: #3b2414; box-shadow: inset 0 0 0 1px var(--card-edge);
}
.btn.primary { background: var(--banner); color: #f6e7c8; box-shadow: none; }
.btn.danger { background: var(--danger); color: #fff; box-shadow: none; }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn.small { padding: 5px 9px; font-size: .85rem; }

/* Grille de la table */
.table { display: grid; gap: 12px; padding: 12px 16px 120px; }
.zone { background: rgb(255 255 255 / .25); border-radius: 14px; padding: 10px; }
@media (prefers-color-scheme: dark) { .zone { background: rgb(255 255 255 / .05); } }
.zone h2 { font-size: .8rem; text-transform: uppercase; letter-spacing: .1em; color: var(--muted); margin-bottom: 8px; }

/* Épreuve + compteurs */
.trial-zone { display: flex; flex-wrap: wrap; gap: 12px; align-items: flex-start; }
.counters { display: grid; gap: 6px; flex: 1 1 100px; }
.counters .mod { font-size: .8rem; color: var(--danger); font-weight: 600; }
.pill { display: inline-grid; place-items: center; min-width: 2.2em; padding: 2px 8px; border-radius: 99px; color: #fff; }
.pill.attack { background: var(--atk); } .pill.defense { background: var(--def); } .pill.hp { background: var(--hp); } .pill.xp { background: var(--xp); }
.capacity { font-size: .85rem; color: var(--ink-soft); font-style: italic; }

/* Posture : même taille que les cartes de la pile ; la place réservée permet de l'incliner à 90°.
   Son texte est lisible sur la carte elle-même. */
.posture-zone { display: flex; flex-direction: column; gap: 10px; align-items: center; }
.posture-slot { --card-w: min(200px, 52vw); width: calc(var(--card-w) * 1029 / 662); height: calc(var(--card-w) * 1029 / 662); display: grid; place-items: center; flex: none; }

/* Ressources du joueur */
.resources { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; }
.resources .res { display: flex; align-items: center; gap: 6px; font-weight: 700; font-size: 1.1rem; }
.resources .value { display: flex; gap: 14px; flex-wrap: wrap; }
.resources .note { color: var(--muted); font-size: .8rem; }
.resources .outcome { font-size: .85rem; font-weight: 600; display: inline-flex; gap: 3px; align-items: center; }
.resources .outcome.hit { color: var(--hp); }
.resources .outcome.miss { color: var(--muted); }
.wound-mark {
  display: inline-grid; place-items: center; width: 1.1em; height: 1.6em; border-radius: 3px;
  background: var(--danger) center / cover no-repeat; color: #fff; font-size: .9rem; line-height: 1;
  box-shadow: 0 0 0 1px rgb(0 0 0 / .25);
}

/* Pile jouée en cascade (chaque carte recouvre la moitié basse de la précédente) */
.pile { --card-w: min(200px, 52vw); --band: calc(var(--card-w) * .36); display: flex; flex-direction: column; align-items: flex-start; padding: 4px 0 8px; min-height: 120px; position: relative; }
.pile-row { position: relative; display: flex; align-items: flex-start; gap: 10px; height: var(--band); width: 100%; }
.pile-row:last-child { height: auto; }
.pile-row .card { cursor: grab; }
.pile-row .meta {
  display: flex; flex-direction: column; gap: 4px; padding-top: 4px; font-size: .85rem; min-width: 0;
}
.pile-row .meta .value { display: flex; gap: 6px; align-items: center; font-weight: 700; }
.pile-row .meta .times { color: var(--muted); font-weight: 600; }
.pile-row .meta-line { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.pile-row .order-btns { display: flex; gap: 4px; }
.pile-row .jokers { display: flex; gap: 4px; }
.pile-row .jokers button { border: 0; background: none; padding: 0; border-radius: 50%; }
.pile-row .jokers button.on { outline: 3px solid var(--gold); }
.drop-line { position: absolute; left: 0; right: 0; height: 4px; border-radius: 2px; background: var(--focus); z-index: 5; pointer-events: none; }
.pile.empty { display: grid; place-items: center; color: var(--muted); min-height: 80px; }

/* Perfectionnement : Réserve et Défausse */
.reserve-grid { display: flex; flex-wrap: wrap; gap: 8px; }
.reserve-item { display: flex; flex-direction: column; align-items: center; gap: 4px; }
.reserve-item .cost { font-size: .8rem; font-weight: 700; color: var(--gold-dark); }
.reserve-item.disabled .card { opacity: .5; filter: grayscale(.4); }

/* Barre du bas : Deck, Défausse, Posture, Réserve */
.dock {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 30;
  background: var(--banner); color: #f6e7c8;
  padding: 8px 12px calc(8px + env(safe-area-inset-bottom));
  display: flex; justify-content: center; gap: 6px; align-items: flex-end;
}
.slot { display: flex; flex-direction: column; align-items: center; gap: 4px; font-size: .75rem; border-radius: 12px; padding: 4px; flex: 1 1 0; min-width: 0; max-width: 76px; background: none; border: 0; color: inherit; }
.slot .card { --card-w: 52px; }
.slot.posture .card { --card-w: 52px; }
.slot .count { font-weight: 700; }
.slot.drop-ok { outline: 2px dashed var(--gold); }
.slot.drop-hover { background: rgb(227 179 76 / .3); }
.slot-empty { width: 52px; height: 73px; border: 2px dashed rgb(255 255 255 / .35); border-radius: 6px; }

/* Feuilles (bas d'écran) et modales */
.sheet-backdrop { position: fixed; inset: 0; background: rgb(0 0 0 / .45); z-index: 50; display: flex; align-items: flex-end; justify-content: center; }
.sheet {
  background: var(--bg); color: var(--ink); width: 100%; max-width: 640px; max-height: 85vh; overflow: auto;
  border-radius: 18px 18px 0 0; padding: 16px 16px calc(16px + env(safe-area-inset-bottom));
  display: flex; flex-direction: column; gap: 12px;
}
.sheet h2 { font-size: 1.1rem; }
.sheet .actions { display: flex; gap: 8px; flex-wrap: wrap; justify-content: flex-end; }
@media (min-width: 700px) { .sheet-backdrop { align-items: center; } .sheet { border-radius: 18px; } }

.log { list-style: none; margin: 0; padding: 0; display: grid; gap: 4px; font-size: .9rem; }
.log li { padding: 4px 8px; border-radius: 8px; background: rgb(255 255 255 / .35); }
@media (prefers-color-scheme: dark) { .log li { background: rgb(255 255 255 / .06); } }
.log li.bad { color: var(--danger); font-weight: 600; }
.log li.good { color: var(--hp); font-weight: 600; }

.toast {
  position: fixed; left: 50%; bottom: 110px; transform: translateX(-50%); z-index: 60;
  background: var(--danger); color: #fff; padding: 8px 14px; border-radius: 10px; max-width: 90vw; font-size: .9rem;
}

/* Écrans plein page (accueil, deck de départ, fin de partie) */
.screen { padding: 16px; display: grid; gap: 14px; max-width: 640px; margin: 0 auto; width: 100%; }
.screen .lead { color: var(--ink-soft); }
.stepper-row { display: flex; align-items: center; gap: 12px; background: rgb(255 255 255 / .3); padding: 8px; border-radius: 12px; }
.stepper-row .card { --card-w: 72px; }
.stepper-row .name { flex: 1; font-weight: 600; }
.stepper { display: flex; align-items: center; gap: 8px; }
.stepper output { min-width: 1.5em; text-align: center; font-weight: 700; font-size: 1.1rem; }
.prep-group { display: grid; gap: 6px; }
.prep-group .cards { display: flex; flex-wrap: wrap; gap: 8px; }

/* Mobile : zones réordonnées (la pile avant la Posture), Épreuve compacte */
@media (max-width: 899px) {
  .table > div { display: contents; }
  .z-trial { order: 1; } .z-res { order: 2; } .z-improve { order: 3; } .z-pile { order: 4; } .z-posture { order: 5; }
  .trial-zone .trial-stage { --card-w: 116px; }
}
.slot.dock-posture .card { transform: none; }
.slot.dock-posture .card.tapped { transform: rotate(90deg) scale(.8); }
.slot:disabled { opacity: .55; cursor: default; }
.slot .posture-card .posture-text { display: none; }

/* Ordinateur : 3 colonnes */
@media (min-width: 900px) {
  .table { grid-template-columns: 350px 1fr 330px; align-items: start; padding-bottom: 130px; }
  .table .col-left, .table .col-right { display: grid; gap: 12px; position: sticky; top: 100px; }
  .pile, .posture-slot { --card-w: 210px; }
  .dock { left: 50%; transform: translateX(-50%); right: auto; border-radius: 16px 16px 0 0; padding-inline: 24px; }
  .slot .card, .slot.posture .card { --card-w: 64px; }
  .slot-empty { width: 64px; height: 89px; }
}

/* Connexion */
.login-form { display: grid; gap: 10px; max-width: 360px; }
.login-form input[type=password] { font: inherit; padding: 10px 12px; border-radius: 10px; border: 1px solid var(--card-edge); background: var(--paper); color: #3b2414; }
.form-error { color: var(--danger); font-weight: 600; margin: 0; }

/* Écrans tactiles : cibles d'au moins 40 px */
@media (pointer: coarse) {
  .btn.small { min-width: 40px; min-height: 40px; padding: 6px 10px; }
  .icon-btn { min-width: 40px; min-height: 40px; }
  .pile-row .jokers button .tok { --s: 30px; }
}

