:root {
  --fond: #f3f5f8;
  --carte: #ffffff;
  --texte: #1c2430;
  --doux: #5b6675;
  --primaire: #1f4e79;
  --primaire-texte: #ffffff;
  --bord: #26313f;
  --def-fond: #dce8f5;
  --def-texte: #10243a;
  --def-active: #ffd666;
  --mot: #fff3c4;
  --sel: #ffc94a;
  --aide: #1d6fd1;
  --faux: #d42a2a;
  --bouton: #e4e9f0;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --fond: #11161d; --carte: #1b222c; --texte: #e6ebf2; --doux: #9aa6b5;
    --primaire: #3d7fc4; --bord: #8796a8; --def-fond: #263a52; --def-texte: #e2ecf7;
    --def-active: #8a6d1d; --mot: #3b3620; --sel: #8a6d1d; --aide: #6fb1ff; --faux: #ff6b6b; --bouton: #2a3340;
  }
}
:root[data-theme="dark"] {
  --fond: #11161d; --carte: #1b222c; --texte: #e6ebf2; --doux: #9aa6b5;
  --primaire: #3d7fc4; --bord: #8796a8; --def-fond: #263a52; --def-texte: #e2ecf7;
  --def-active: #8a6d1d; --mot: #3b3620; --sel: #8a6d1d; --aide: #6fb1ff; --faux: #ff6b6b; --bouton: #2a3340;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--fond); color: var(--texte);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
  padding-bottom: env(safe-area-inset-bottom);
}

.entete {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: 10px 16px; background: var(--primaire); color: var(--primaire-texte);
}
.entete h1 { font-size: 1.15rem; margin: 0; }
.infos { display: flex; gap: 14px; font-size: .95rem; font-variant-numeric: tabular-nums; }

.barre, .actions {
  display: flex; flex-wrap: wrap; gap: 6px; padding: 8px 16px; justify-content: center;
}
button, select {
  font: inherit; font-size: .9rem; border: 0; border-radius: 8px; padding: 8px 12px;
  background: var(--bouton); color: var(--texte); cursor: pointer;
}
button.principal { background: var(--primaire); color: var(--primaire-texte); }
button:active { filter: brightness(.9); }

main { max-width: 800px; margin: 0 auto; }
.banniere {
  margin: 4px 16px 8px; padding: 10px 12px; min-height: 2.6em;
  background: var(--carte); border-radius: 8px; font-size: 1rem; line-height: 1.3;
  box-shadow: 0 1px 3px rgba(0,0,0,.12);
}

.zone-grille { position: relative; padding: 0 16px; display: flex; justify-content: center; }
.grille {
  display: grid; border: 2px solid var(--bord); background: var(--bord); gap: 1px;
  user-select: none; -webkit-user-select: none; touch-action: manipulation;
}
.case { width: var(--t); height: var(--t); position: relative; background: var(--carte); }
.case.lettre { cursor: pointer; display: flex; align-items: center; justify-content: center; }
.case .l { font-size: calc(var(--t) * .58); font-weight: 600; line-height: 1; }
.case.mot { background: var(--mot); }
.case.sel { background: var(--sel); }
.case.aide .l { color: var(--aide); }
.case.faux .l { color: var(--faux); text-decoration: line-through; }

.case.def {
  background: var(--def-fond); color: var(--def-texte); cursor: pointer;
  display: flex; flex-direction: column;
}
.case.def.vide { background: var(--bord); }
.case.def .d {
  flex: 1 1 0; min-height: 0; overflow: hidden; display: flex; align-items: center; justify-content: center;
  text-align: center; line-height: 1.05; padding: 1px;
  word-break: normal; overflow-wrap: normal;
}
.case.def .d + .d { border-top: 1px solid var(--bord); }
.case.def .d.active { background: var(--def-active); }

/* Flèches de départ des mots */
.fl {
  position: absolute; font-style: normal; color: var(--bord); line-height: 1;
  font-size: calc(var(--t) * .22); pointer-events: none;
}
.fl-D { left: 0; top: 50%; transform: translateY(-50%); }
.fl-B { top: 0; left: 50%; transform: translateX(-50%); }
.fl-DB { left: 1px; top: 1px; }
.fl-BD { left: 1px; top: 0; }

.attente {
  position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center;
  background: color-mix(in srgb, var(--fond) 85%, transparent); font-size: 1rem;
}
.attente[hidden] { display: none; }
.roue {
  width: 36px; height: 36px; border-radius: 50%; border: 4px solid var(--bouton); border-top-color: var(--primaire);
  animation: tourne 1s linear infinite;
}
@keyframes tourne { to { transform: rotate(360deg); } }

.clavier { padding: 6px 4px 10px; position: sticky; bottom: 0; background: var(--fond); }
.clavier .rang { display: flex; justify-content: center; gap: 4px; margin-bottom: 5px; }
.clavier button {
  flex: 1 1 0; max-width: 44px; padding: 12px 0; font-size: 1.05rem; font-weight: 600;
  background: var(--carte); box-shadow: 0 1px 2px rgba(0,0,0,.25);
}
.clavier button.large { max-width: 64px; flex-grow: 1.5; }

dialog { border: 0; border-radius: 12px; padding: 20px; background: var(--carte); color: var(--texte); max-width: 90vw; }
dialog::backdrop { background: rgba(0,0,0,.4); }
dialog h2 { margin-top: 0; }
.boutons-dialogue { display: flex; gap: 8px; justify-content: flex-end; }

@media print {
  .entete, .barre, .actions, .clavier, .banniere { display: none !important; }
  body { background: #fff; }
  .case.mot, .case.sel { background: #fff !important; }
  .case.def { background: #e8e8e8 !important; color: #000; }
  .case.def .d.active { background: transparent !important; }
  body.impression-vierge .case .l { visibility: hidden; }
}
