:root {
  --bg: #f3efe6;
  --surface: #ffffff;
  --surface-2: #e8e2d4;
  --text: #1f1d19;
  --muted: #6f685b;
  --border: #d8d0bf;
  --accent: #2f6b4f;
  --accent-text: #ffffff;
  --accent-soft: #d6e8dd;
  --danger: #b3261e;
  --sq-light: #f0d9b5;
  --sq-dark: #b58863;
  --key: #ffffff;
  --shadow: 0 1px 0 rgba(0, 0, 0, .08), 0 1px 3px rgba(0, 0, 0, .08);
  --radius: 14px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #16181c;
    --surface: #1f2228;
    --surface-2: #2a2e36;
    --text: #ecebe7;
    --muted: #9ba1ab;
    --border: #353a44;
    --accent: #62bd8e;
    --accent-text: #0e1a13;
    --accent-soft: #24392e;
    --danger: #ff8a80;
    --key: #2a2e36;
    --shadow: 0 1px 0 rgba(0, 0, 0, .4);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #16181c; --surface: #1f2228; --surface-2: #2a2e36; --text: #ecebe7; --muted: #9ba1ab;
  --border: #353a44; --accent: #62bd8e; --accent-text: #0e1a13; --accent-soft: #24392e;
  --danger: #ff8a80; --key: #2a2e36; --shadow: 0 1px 0 rgba(0, 0, 0, .4); color-scheme: dark;
}

*, *::before, *::after { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}
[hidden] { display: none !important; }
button { font: inherit; color: inherit; touch-action: manipulation; cursor: pointer; }
button:disabled { cursor: default; }
:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }
.muted { color: var(--muted); }
svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

.screen {
  max-width: 560px;
  margin: 0 auto;
  padding: max(10px, env(safe-area-inset-top)) max(12px, env(safe-area-inset-right))
           max(12px, env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left));
}

/* --- boutons communs --- */
.btn {
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: var(--radius);
  padding: 12px 16px;
  font-weight: 600;
  box-shadow: var(--shadow);
}
.btn.primary { background: var(--accent); color: var(--accent-text); border-color: transparent; }
.btn.big { width: 100%; padding: 18px; font-size: 1.15rem; display: flex; flex-direction: column; align-items: center; gap: 2px; }
.btn.big small { font-weight: 400; font-size: .85rem; opacity: .8; }
.btn:active:not(:disabled) { transform: scale(.98); }
.icon-btn {
  display: grid; place-items: center;
  width: 44px; height: 44px;
  border: 0; border-radius: 12px; background: transparent;
}
.icon-btn:active:not(:disabled) { background: var(--surface-2); }
.icon-btn:disabled { opacity: .3; }
.icon-btn.small { width: 36px; height: 36px; }
.icon-btn.small svg { width: 18px; height: 18px; }

/* --- menu --- */
.menu { min-height: 100%; display: flex; flex-direction: column; gap: 24px; }
.brand { text-align: center; padding-top: 4vh; }
.brand img { background: var(--sq-light); border-radius: 20px; padding: 6px; }
.brand h1 { margin: 10px 0 4px; font-size: 2rem; letter-spacing: -.02em; }
.brand p { margin: 0; color: var(--muted); }
.menu-actions { display: grid; gap: 12px; }
.history h2 { font-size: 1rem; color: var(--muted); font-weight: 600; margin: 0 0 8px; }
.history ul { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.history li { display: flex; align-items: center; gap: 4px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding-right: 4px; }
.history-item { flex: 1; min-width: 0; text-align: left; border: 0; background: none; padding: 12px 14px; display: grid; }
.h-title { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.h-meta { font-size: .85rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.credits { margin-top: auto; text-align: center; font-size: .75rem; }

/* --- saisie : grille qui remplit l'écran, sans défilement --- */
.play {
  height: 100vh;
  height: 100dvh;
  display: grid;
  grid-template-rows: auto minmax(44px, 1fr) 52px minmax(150px, 2.6fr) minmax(52px, 1fr) minmax(52px, 1fr);
  gap: 8px;
  overflow: hidden;
  position: relative;
}
.bar { display: flex; align-items: center; gap: 4px; min-height: 44px; }
.bar h2 { margin: 0 0 0 4px; font-size: 1.15rem; flex: 1; }
.turn { flex: 1; display: flex; align-items: center; gap: 8px; padding-left: 4px; font-size: 1.05rem; }
.turn-dot { width: 18px; height: 18px; border-radius: 50%; border: 2px solid var(--text); background: #fff; flex: none; }
[data-turn="b"] .turn-dot { background: #111; border-color: var(--muted); }
.btn.end { padding: 9px 16px; }

.moves {
  list-style: none; margin: 0; padding: 6px 10px;
  display: flex; flex-wrap: wrap; align-content: flex-start; gap: 2px 12px;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  overflow-y: auto; min-height: 0;
  font: 15px/1.6 ui-monospace, "SF Mono", Menlo, Consolas, monospace;
}
.moves li { display: flex; gap: 6px; white-space: nowrap; }
.moves .n { color: var(--muted); }
.moves .last { background: var(--accent-soft); border-radius: 4px; padding: 0 3px; margin: 0 -3px; font-weight: 700; }
.moves .hint { font-family: system-ui, sans-serif; font-size: .9rem; }

.composer { display: flex; align-items: center; gap: 10px; padding: 0 4px; min-width: 0; }
.preview { display: flex; align-items: center; gap: 6px; font-size: 1.35rem; font-weight: 700; white-space: nowrap; }
.preview img { width: 36px; height: 36px; }
.status { flex: 1; text-align: right; color: var(--muted); font-size: .95rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.status.ok { color: var(--accent); font-weight: 600; }
.status.check { color: var(--danger); font-weight: 700; }

.pieces { display: grid; grid-template-columns: repeat(3, 1fr); grid-template-rows: repeat(2, 1fr); gap: 8px; min-height: 0; }
.piece {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  min-height: 0; padding: 4px;
  background: var(--sq-light); color: #3a2a17;
  border: 3px solid transparent; border-radius: var(--radius);
  box-shadow: var(--shadow);
  font-weight: 600; font-size: .95rem;
}
.piece:nth-child(even) { background: #e6c79a; }
.piece img { width: auto; height: 100%; max-height: 64px; min-height: 0; flex: 1 1 0; aspect-ratio: 1; object-fit: contain; pointer-events: none; }
.piece.on, .piece.on:nth-child(even) { border-color: var(--accent); background: #cfe3b0; box-shadow: 0 0 0 3px var(--accent); }
.piece:disabled { opacity: .3; box-shadow: none; }

.keys { display: grid; grid-template-columns: repeat(8, 1fr); gap: 6px; min-height: 0; }
.key {
  min-width: 0; min-height: 0; padding: 0;
  border: 1px solid var(--border); border-radius: 12px;
  background: var(--key); box-shadow: var(--shadow);
  font-size: 1.5rem; font-weight: 700;
}
.key.on { background: var(--accent); color: var(--accent-text); border-color: transparent; }
.key:disabled { opacity: .25; box-shadow: none; }
.key:active:not(:disabled), .piece:active:not(:disabled) { transform: scale(.95); }

.over {
  position: absolute; left: 12px; right: 12px; bottom: max(12px, env(safe-area-inset-bottom));
  background: var(--surface); border: 2px solid var(--accent); border-radius: var(--radius);
  padding: 16px; text-align: center; box-shadow: 0 8px 30px rgba(0, 0, 0, .25);
}
.over p { margin: 0 0 12px; font-size: 1.2rem; font-weight: 700; }
.over .btn { width: 100%; }

/* --- fin de partie --- */
.end-screen {
  height: 100vh; height: 100dvh;
  display: grid; grid-template-rows: auto minmax(0, 1fr) auto; gap: 10px;
}
.end-body { overflow-y: auto; display: grid; gap: 14px; align-content: start; min-height: 0; }
.results { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; }
.result {
  display: grid; justify-items: center; padding: 8px 2px;
  border: 1px solid var(--border); border-radius: 12px; background: var(--surface);
}
.result b { font-size: 1.15rem; }
.result small { font-size: .72rem; color: var(--muted); }
.result.on { background: var(--accent); color: var(--accent-text); border-color: transparent; }
.result.on small { color: inherit; opacity: .85; }
.players { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.players label { display: grid; gap: 4px; font-size: .85rem; color: var(--muted); }
.players input {
  font: inherit; color: var(--text); background: var(--surface);
  border: 1px solid var(--border); border-radius: 10px; padding: 10px; min-width: 0;
}
.movelist {
  list-style: none; margin: 0; padding: 4px 0;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  columns: 2; column-gap: 0;
  font: 15px/1.9 ui-monospace, "SF Mono", Menlo, Consolas, monospace;
}
.movelist li { display: grid; grid-template-columns: 3.2em 1fr 1fr; padding: 0 12px; break-inside: avoid; }
.movelist li:nth-child(odd) { background: var(--surface-2); }
.movelist .n { color: var(--muted); }
.raw summary { color: var(--muted); font-size: .9rem; padding: 4px 0; }
.raw pre {
  white-space: pre-wrap; word-break: break-word; font-size: .8rem;
  background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: 10px; margin: 8px 0 0;
}
.end-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }

/* --- feuille de choix --- */
.sheet { position: fixed; inset: 0; background: rgba(0, 0, 0, .45); display: flex; align-items: flex-end; justify-content: center; z-index: 10; }
.sheet-panel {
  width: 100%; max-width: 560px; background: var(--bg);
  border-radius: 20px 20px 0 0; padding: 18px 16px max(16px, env(safe-area-inset-bottom));
  display: grid; gap: 12px;
}
.sheet h3 { margin: 0; text-align: center; }
.sheet-options { display: grid; grid-template-columns: repeat(auto-fit, minmax(120px, 1fr)); gap: 8px; }
.option {
  display: grid; justify-items: center; gap: 2px; padding: 12px 6px;
  border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); box-shadow: var(--shadow);
}
.option img { width: 52px; height: 52px; background: var(--sq-light); border-radius: 10px; }
.option small { color: var(--muted); font-family: ui-monospace, monospace; }

.toast {
  position: fixed; left: 50%; bottom: calc(24px + env(safe-area-inset-bottom)); transform: translate(-50%, 20px);
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 999px;
  font-size: .95rem; opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s; z-index: 20;
  max-width: calc(100% - 32px); text-align: center;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* Paysage sur téléphone : on accepte le défilement plutôt que des touches illisibles */
@media (max-height: 480px) {
  .play { height: auto; min-height: 100dvh; grid-template-rows: auto 60px 48px 180px 56px 56px; }
}
