:root {
  --bg: #0b0908;
  --panel: rgba(17, 13, 10, 0.84);
  --panel-line: rgba(212, 173, 98, 0.18);
  --ink: #efe7d8;
  --muted: #a69a86;
  --gold: #d4ad62;
  --gold-hi: #f0cf86;
  --red: #e5483a;
  --amber: #e2a43a;
  --green: #5fd39a;
  --btn: rgba(255, 245, 225, 0.06);
  --btn-hi: rgba(255, 245, 225, 0.12);
  --panel-w: 372px;
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, "Book Antiqua", Georgia, serif;
  --sans: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", "Helvetica Neue", Arial, sans-serif;
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--ink); font-family: var(--sans); overflow: hidden; -webkit-font-smoothing: antialiased; }
button { font: inherit; color: inherit; cursor: pointer; }
#scene { position: fixed; inset: 0; width: 100vw; height: 100vh; display: block; outline: none; touch-action: none; }

.loading { position: fixed; inset: 0; display: grid; place-content: center; gap: 18px; justify-items: center; background: var(--bg); z-index: 50; transition: opacity .6s ease; color: var(--muted); letter-spacing: .04em; }
.loading.done { opacity: 0; pointer-events: none; }
.loader { width: 44px; height: 44px; border-radius: 50%; border: 2px solid rgba(212,173,98,.18); border-top-color: var(--gold); animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* Panneau */
.panel { position: fixed; top: 16px; right: 16px; bottom: 16px; width: var(--panel-w); display: flex; flex-direction: column; gap: 14px; padding: 20px 20px 14px; background: var(--panel); border: 1px solid var(--panel-line); border-radius: 18px; backdrop-filter: blur(16px) saturate(1.2); -webkit-backdrop-filter: blur(16px) saturate(1.2); box-shadow: 0 30px 80px rgba(0,0,0,.55); z-index: 10; }
.brand { display: flex; align-items: center; gap: 14px; }
.mark { width: 46px; height: 46px; border-radius: 12px; display: grid; place-items: center; font-size: 30px; color: var(--gold); background: linear-gradient(160deg, rgba(212,173,98,.18), rgba(212,173,98,.04)); border: 1px solid var(--panel-line); }
.brand h1 { margin: 0; font-family: var(--serif); font-weight: 600; font-size: 24px; letter-spacing: .02em; }
.brand p { margin: 2px 0 0; color: var(--muted); font-size: 13px; }

.status { display: flex; align-items: center; gap: 10px; padding: 11px 14px; border-radius: 12px; background: rgba(255,245,225,.05); border: 1px solid rgba(255,245,225,.07); font-size: 15px; font-weight: 600; min-height: 46px; transition: background .3s, border-color .3s; }
.status-icon { width: 10px; height: 10px; border-radius: 50%; background: var(--gold); flex: none; }
.status[data-kind="thinking"] .status-icon { background: var(--amber); animation: pulse 1.2s ease-in-out infinite; }
.status[data-kind="check"] { background: rgba(229,72,58,.14); border-color: rgba(229,72,58,.55); color: #ffd4cf; }
.status[data-kind="check"] .status-icon { width: auto; height: auto; background: none; color: var(--red); font-size: 17px; }
.status[data-kind="check"] .status-icon::before { content: "⚠"; }
.status[data-kind="mate"] { background: rgba(229,72,58,.22); border-color: var(--red); color: #fff; }
.status[data-kind="mate"] .status-icon { width: auto; height: auto; background: none; }
.status[data-kind="mate"] .status-icon::before { content: "♚"; color: var(--red); font-size: 18px; }
.status[data-kind="draw"] { background: rgba(226,164,58,.12); border-color: rgba(226,164,58,.5); }
.status[data-kind="draw"] .status-icon { width: auto; height: auto; background: none; }
.status[data-kind="draw"] .status-icon::before { content: "½"; color: var(--amber); font-size: 17px; }
.status[data-kind="win"] { background: rgba(95,211,154,.12); border-color: rgba(95,211,154,.5); }
.status[data-kind="win"] .status-icon { background: var(--green); }
.hg-flag { margin-left: auto; color: var(--amber); opacity: 0; transition: opacity .3s; }
.hg-flag[data-hourglass="visible"] { opacity: 1; animation: pulse 1.2s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: .35; } }

.players { display: flex; flex-direction: column; gap: 8px; }
.player { padding: 9px 12px; border-radius: 10px; background: rgba(255,245,225,.035); border: 1px solid transparent; transition: border-color .3s, background .3s; }
.player.to-move { border-color: rgba(212,173,98,.45); background: rgba(212,173,98,.07); }
.p-head { display: flex; align-items: center; gap: 9px; font-size: 14px; }
.p-dot { width: 12px; height: 12px; border-radius: 50%; border: 1px solid rgba(255,255,255,.35); flex: none; }
.p-dot.w { background: #f1e3c4; } .p-dot.b { background: #1b120c; }
.p-name { font-weight: 600; }
.p-clock { margin-left: auto; font-variant-numeric: tabular-nums; font-size: 17px; font-weight: 600; padding: 2px 8px; border-radius: 6px; background: rgba(0,0,0,.35); }
.p-clock.low { color: var(--red); }
.p-captured { min-height: 20px; margin-top: 4px; font-size: 17px; letter-spacing: -1px; color: var(--muted); }
.p-captured .adv { font-size: 12px; letter-spacing: 0; margin-left: 6px; color: var(--gold); font-weight: 600; }

.evalbar { display: flex; align-items: center; gap: 10px; padding: 0 2px; }
.eval-track { flex: 1; height: 6px; border-radius: 3px; background: #1b120c; overflow: hidden; border: 1px solid rgba(255,255,255,.08); }
.eval-fill { height: 100%; width: 50%; background: linear-gradient(90deg, #efe2c2, #fff7e4); transition: width .6s ease; }
#evalText { font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; min-width: 40px; text-align: right; }

.moves { flex: 1; min-height: 120px; display: flex; flex-direction: column; }
.sec-title { display: flex; align-items: baseline; justify-content: space-between; }
.sec-title h2 { margin: 0 0 8px; font-size: 12px; text-transform: uppercase; letter-spacing: .14em; color: var(--muted); font-weight: 600; }
#moveCount { font-size: 12px; color: var(--muted); }
.move-list { flex: 1; overflow-y: auto; margin: 0; padding: 0; list-style: none; display: grid; grid-template-columns: 34px 1fr 1fr; align-content: start; gap: 2px 4px; font-variant-numeric: tabular-nums; font-size: 14px; scrollbar-width: thin; }
.move-list .num { color: var(--muted); padding: 4px 0; text-align: right; padding-right: 6px; }
.move-list .mv { padding: 4px 8px; border-radius: 6px; cursor: pointer; font-family: var(--serif); font-size: 15px; }
.move-list .mv:hover { background: var(--btn); }
.move-list .mv.current { background: rgba(212,173,98,.2); color: var(--gold-hi); }
.review { display: flex; align-items: center; justify-content: space-between; margin-top: 8px; padding: 8px 10px; border-radius: 8px; background: rgba(212,173,98,.1); font-size: 13px; }

.actions { display: grid; grid-template-columns: 1fr 1fr; gap: 7px; }
.btn { padding: 9px 12px; border-radius: 9px; border: 1px solid rgba(255,245,225,.09); background: var(--btn); font-size: 13.5px; font-weight: 500; transition: background .15s, border-color .15s, transform .1s; }
.btn:hover { background: var(--btn-hi); border-color: rgba(212,173,98,.35); }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .4; cursor: default; }
.btn.primary { background: linear-gradient(180deg, #dcb66a, #b98d3f); color: #1a120a; border-color: transparent; font-weight: 650; }
.btn.primary:hover { background: linear-gradient(180deg, #e8c47a, #c49849); }
.btn.ghost { background: transparent; }
.btn.small { padding: 5px 10px; font-size: 12.5px; }
.actions .primary { grid-column: span 2; }

.views { display: flex; align-items: center; gap: 6px; }
.views-label { font-size: 12px; text-transform: uppercase; letter-spacing: .14em; color: var(--muted); margin-right: 4px; }
.chip { padding: 5px 10px; border-radius: 999px; border: 1px solid rgba(255,245,225,.1); background: transparent; font-size: 12.5px; color: var(--muted); }
.chip.active, .chip:hover { color: var(--ink); border-color: rgba(212,173,98,.5); background: rgba(212,173,98,.1); }
.panel-foot { display: flex; justify-content: space-between; align-items: center; border-top: 1px solid rgba(255,245,225,.06); padding-top: 10px; }
.muted { color: var(--muted); font-size: 12px; }

/* Modales */
.modal { position: fixed; inset: 0; display: grid; place-items: center; background: rgba(5,4,3,.55); backdrop-filter: blur(6px); -webkit-backdrop-filter: blur(6px); z-index: 30; }
.modal.light { background: rgba(5,4,3,.25); }
.modal[hidden], .banner[hidden] { display: none; }
.card { width: min(480px, calc(100vw - 32px)); padding: 26px; border-radius: 18px; background: #16110d; border: 1px solid var(--panel-line); box-shadow: 0 30px 90px rgba(0,0,0,.6); }
.card.wide { width: min(640px, calc(100vw - 32px)); }
.card h2 { margin: 0 0 18px; font-family: var(--serif); font-weight: 600; font-size: 24px; }
.field { margin-bottom: 16px; display: flex; flex-direction: column; gap: 8px; }
.field label { font-size: 13px; color: var(--muted); }
.field strong { color: var(--gold-hi); }
.seg { display: flex; gap: 6px; flex-wrap: wrap; }
.seg button { flex: 1; padding: 9px 10px; border-radius: 9px; border: 1px solid rgba(255,245,225,.1); background: var(--btn); font-size: 13.5px; white-space: nowrap; }
.seg button.active { border-color: var(--gold); background: rgba(212,173,98,.16); color: var(--gold-hi); }
input[type="range"] { width: 100%; accent-color: var(--gold); }
.scale { display: flex; justify-content: space-between; font-size: 11.5px; color: var(--muted); }
input[type="text"], textarea { width: 100%; padding: 10px; border-radius: 9px; border: 1px solid rgba(255,245,225,.12); background: #0e0b08; color: var(--ink); font-family: ui-monospace, Menlo, monospace; font-size: 12.5px; resize: vertical; }
.check { display: flex; gap: 10px; align-items: center; font-size: 14px; margin: 10px 0; }
.check input { accent-color: var(--gold); width: 16px; height: 16px; }
.row, .row-end { display: flex; gap: 10px; margin-top: 8px; }
.row-end { justify-content: flex-end; margin-top: 18px; }
.err { color: var(--red); font-size: 13px; }
.promo { width: auto; text-align: center; }
.promo-row { display: flex; gap: 12px; }
.promo-row button { width: 84px; height: 96px; border-radius: 14px; border: 1px solid var(--panel-line); background: linear-gradient(180deg, rgba(255,245,225,.08), rgba(255,245,225,.02)); font-size: 54px; line-height: 1; display: grid; place-items: center; transition: transform .15s, border-color .15s; }
.promo-row button:hover { transform: translateY(-3px); border-color: var(--gold); }
.promo-row button span { font-size: 11px; color: var(--muted); display: block; margin-top: 2px; }

.banner { position: fixed; left: 0; right: calc(var(--panel-w) + 32px); top: 9%; display: flex; justify-content: center; z-index: 20; pointer-events: none; }
.banner-card { pointer-events: auto; text-align: center; padding: 24px 40px; border-radius: 18px; background: rgba(17,13,10,.9); border: 1px solid var(--panel-line); box-shadow: 0 30px 80px rgba(0,0,0,.6); animation: rise .5s ease; }
.banner-card h2 { margin: 6px 0 4px; font-family: var(--serif); font-size: 32px; font-weight: 600; }
.banner-card p { margin: 0 0 16px; color: var(--muted); }
.banner-card .row { justify-content: center; }
.banner-icon { font-size: 34px; color: var(--gold); }
@keyframes rise { from { opacity: 0; transform: translateY(12px); } }

.toast { position: fixed; left: calc((100vw - var(--panel-w) - 32px) / 2); bottom: 28px; transform: translateX(-50%); padding: 10px 18px; border-radius: 999px; background: rgba(17,13,10,.92); border: 1px solid var(--panel-line); font-size: 14px; z-index: 40; }
.server-down { position: fixed; top: 16px; left: 16px; padding: 10px 16px; border-radius: 10px; background: rgba(229,72,58,.15); border: 1px solid var(--red); color: #ffd4cf; z-index: 40; font-size: 14px; }
.fatal { position: fixed; inset: 0; display: grid; place-items: center; background: var(--bg); z-index: 60; }
.fatal[hidden] { display: none; }

@media (max-width: 1100px) { :root { --panel-w: 320px; } }
/* Écran tactile (iPad) : cibles au doigt, pas de sélection ni de menu contextuel sur appui long */
@media (pointer: coarse) {
  html, body { -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; -webkit-tap-highlight-color: transparent; touch-action: manipulation; }
  .btn { padding: 12px 12px; font-size: 14.5px; }
  .chip { padding: 9px 14px; font-size: 13.5px; }
  .seg button { padding: 12px 10px; }
  .move-list .mv { padding: 7px 8px; }
  .check input { width: 22px; height: 22px; }
}
/* Mode application plein écran (écran d'accueil) : respect des zones sûres */
.panel { top: max(16px, env(safe-area-inset-top)); right: max(16px, env(safe-area-inset-right)); bottom: max(16px, env(safe-area-inset-bottom)); }

/* Portrait (iPad vertical) ou écran étroit : panneau compact en bas, plateau au-dessus */
@media (orientation: portrait), (max-width: 820px) {
  .panel {
    top: auto; left: max(12px, env(safe-area-inset-left)); right: max(12px, env(safe-area-inset-right)); bottom: max(12px, env(safe-area-inset-bottom));
    width: auto; max-height: 42vh; overflow-y: auto; padding: 14px 16px 10px;
    display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 10px 14px;
    grid-template-areas: "brand status" "players moves" "actions actions" "views foot";
  }
  .brand { grid-area: brand; } .brand h1 { font-size: 19px; } .brand p { font-size: 12px; } .mark { width: 38px; height: 38px; font-size: 24px; }
  .status { grid-area: status; }
  .players { grid-area: players; }
  .moves { grid-area: moves; min-height: 0; max-height: 150px; }
  .actions { grid-area: actions; grid-template-columns: repeat(4, minmax(0, 1fr)); }
  .actions .primary { grid-column: span 1; }
  .views { grid-area: views; }
  .panel-foot { grid-area: foot; border-top: 0; padding-top: 0; justify-content: flex-end; }
  .banner { right: 0; top: 6%; }
  .toast { left: 50%; bottom: auto; top: max(16px, env(safe-area-inset-top)); }
}
@media (max-width: 560px) {
  .panel { grid-template-columns: minmax(0, 1fr); grid-template-areas: "brand" "status" "players" "moves" "actions" "views" "foot"; }
  .actions { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
@media (prefers-reduced-motion: reduce) { * { animation: none !important; transition: none !important; } }
