/* ——— Thèmes ——— */
:root,
html[data-theme="dark"] {
  --bg-deep: #0f1a14;
  --bg-mid: #16241c;
  --bg-glow-a: rgba(79, 154, 106, 0.18);
  --bg-glow-b: rgba(196, 168, 118, 0.1);
  --bg-panel: rgba(22, 40, 30, 0.88);
  --line: rgba(196, 168, 118, 0.22);
  --text: #ede6d8;
  --muted: #a8b5a8;
  --accent: #c4a876;
  --accent-strong: #e0c48a;
  --green: #3d7a55;
  --green-bright: #4f9a6a;
  --danger: #c45c4a;
  --light-sq: #e8dcc4;
  --dark-sq: #5a7a52;
  --select: rgba(224, 196, 138, 0.55);
  --move-from: rgba(224, 196, 138, 0.35);
  --move-to: rgba(61, 122, 85, 0.55);
  --last: rgba(196, 168, 118, 0.28);
  --last-from: rgba(224, 180, 70, 0.55);
  --last-to: rgba(196, 168, 118, 0.32);
  --last-from-ring: rgba(232, 196, 90, 0.95);
  --check: rgba(196, 92, 74, 0.55);
  --shadow: 0 24px 60px rgba(0, 0, 0, 0.45);
  --input-bg: rgba(0, 0, 0, 0.25);
  --btn-bg: rgba(255, 255, 255, 0.04);
  --btn-bg-hover: rgba(255, 255, 255, 0.08);
  --row-odd: rgba(255, 255, 255, 0.03);
  --modal-bg: #15231b;
  --modal-backdrop: rgba(5, 10, 8, 0.65);
  --captured-black: #c5d0c5;
  --radius: 14px;
  --font-display: "Cormorant Garamond", Georgia, serif;
  --font-ui: "Outfit", sans-serif;
}

html[data-theme="light"] {
  --bg-deep: #e7eee6;
  --bg-mid: #f3f6f1;
  --bg-glow-a: rgba(61, 122, 85, 0.12);
  --bg-glow-b: rgba(90, 110, 80, 0.08);
  --bg-panel: rgba(255, 255, 255, 0.82);
  --line: rgba(45, 70, 50, 0.16);
  --text: #1c2a20;
  --muted: #5a6b5e;
  --accent: #3d6b4a;
  --accent-strong: #2f5a3c;
  --green: #3d7a55;
  --green-bright: #4a8f62;
  --danger: #b54a3c;
  --light-sq: #f0e6d2;
  --dark-sq: #6a8a62;
  --select: rgba(61, 122, 85, 0.35);
  --move-from: rgba(61, 122, 85, 0.22);
  --move-to: rgba(61, 122, 85, 0.55);
  --last: rgba(61, 122, 85, 0.2);
  --last-from: rgba(210, 160, 40, 0.48);
  --last-to: rgba(61, 122, 85, 0.28);
  --last-from-ring: rgba(180, 130, 20, 0.9);
  --check: rgba(196, 92, 74, 0.45);
  --shadow: 0 18px 40px rgba(40, 60, 45, 0.12);
  --input-bg: rgba(255, 255, 255, 0.9);
  --btn-bg: rgba(45, 70, 50, 0.05);
  --btn-bg-hover: rgba(45, 70, 50, 0.1);
  --row-odd: rgba(45, 70, 50, 0.04);
  --modal-bg: #f7faf6;
  --modal-backdrop: rgba(30, 45, 35, 0.4);
  --captured-black: #3a4a3e;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  min-height: 100%;
}

body {
  font-family: var(--font-ui);
  color: var(--text);
  background:
    radial-gradient(ellipse 90% 70% at 10% -10%, var(--bg-glow-a), transparent 55%),
    radial-gradient(ellipse 70% 50% at 100% 20%, var(--bg-glow-b), transparent 50%),
    linear-gradient(160deg, var(--bg-deep), var(--bg-mid) 45%, var(--bg-deep));
  background-attachment: fixed;
  transition: background 0.25s ease, color 0.2s ease;
}

.app {
  width: min(1380px, calc(100% - 2rem));
  margin: 0 auto;
  padding: 1.25rem 0 2.5rem;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  margin-bottom: 1.5rem;
  animation: rise 0.7s ease both;
}

.brand {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.brand-mark {
  width: 1.1rem;
  height: 1.1rem;
  border-radius: 3px;
  background: linear-gradient(135deg, var(--accent-strong), var(--green-bright));
  box-shadow: 0 0 0 4px color-mix(in srgb, var(--accent) 18%, transparent);
  animation: pulse-mark 3.2s ease-in-out infinite;
}

.brand-name {
  margin: 0;
  font-family: var(--font-display);
  font-size: clamp(2.4rem, 5vw, 3.4rem);
  font-weight: 600;
  letter-spacing: 0.02em;
  line-height: 1;
  color: var(--accent-strong);
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 0.55rem;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.lang-switch {
  display: inline-flex;
  gap: 0.2rem;
  padding: 0.15rem;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--btn-bg);
}

.lang-btn {
  min-width: 2.4rem;
  padding: 0.4rem 0.55rem;
  border: none;
  background: transparent;
  font-weight: 600;
  letter-spacing: 0.04em;
}

.lang-btn.active {
  background: color-mix(in srgb, var(--green) 35%, transparent);
  color: var(--text);
}

.lang-btn:hover {
  background: var(--btn-bg-hover);
  border-color: transparent;
}

.theme-toggle {
  display: inline-flex;
  align-items: center;
  gap: 0.45rem;
}

.theme-icon {
  font-size: 1rem;
  line-height: 1;
}

html[data-theme="dark"] .theme-icon-light,
html[data-theme="light"] .theme-icon-dark {
  display: none;
}

.status {
  margin: 0.85rem 0 0;
  text-align: center;
  color: var(--muted);
  font-size: 1rem;
  font-weight: 500;
  min-height: 1.4em;
}

.layout {
  display: grid;
  grid-template-columns: minmax(180px, 240px) minmax(0, 1fr) minmax(280px, 340px);
  gap: 1.25rem;
  align-items: start;
}

.history-col {
  animation: rise 0.8s ease 0.06s both;
}

.history-panel {
  height: 100%;
  min-height: 280px;
  display: flex;
  flex-direction: column;
}

.history-panel .moves {
  flex: 1;
  max-height: min(68vh, 560px);
}

.board-section {
  animation: rise 0.8s ease 0.08s both;
}

.board-frame {
  width: min(100%, 640px);
  margin: 0 auto;
}

.board-row {
  display: grid;
  grid-template-columns: 1.4rem 1fr 1.4rem;
  gap: 0.25rem;
  align-items: stretch;
}

.file-labels,
.rank-labels {
  display: grid;
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 600;
  letter-spacing: 0.04em;
  user-select: none;
}

.file-labels {
  grid-template-columns: repeat(8, 1fr);
  padding: 0 1.65rem;
  text-align: center;
}

.rank-labels {
  grid-template-rows: repeat(8, 1fr);
  align-items: center;
  justify-items: center;
}

.board {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  grid-template-rows: repeat(8, 1fr);
  width: 100%;
  aspect-ratio: 1 / 1;
  container-type: inline-size;
  border-radius: 10px;
  overflow: hidden;
  box-shadow: var(--shadow);
  border: 1px solid rgba(224, 196, 138, 0.25);
  transform-origin: center;
  animation: board-in 0.9s cubic-bezier(0.2, 0.8, 0.2, 1) both;
}

.square {
  position: relative;
  display: grid;
  place-items: center;
  width: 100%;
  height: 100%;
  min-width: 0;
  min-height: 0;
  border: none;
  margin: 0;
  padding: 0;
  cursor: pointer;
  line-height: 1;
  user-select: none;
  appearance: none;
  -webkit-appearance: none;
  transition: background-color 0.15s ease, transform 0.12s ease;
}

.square.light {
  background: var(--light-sq);
}

.square.dark {
  background: var(--dark-sq);
}

.square:hover:not(:disabled) {
  filter: brightness(1.06);
}

.square.selected {
  box-shadow: inset 0 0 0 3px var(--select);
}

.square.origin {
  background-image: linear-gradient(var(--move-from), var(--move-from));
}

.square.target::after {
  content: "";
  position: absolute;
  width: 28%;
  height: 28%;
  border-radius: 50%;
  background: var(--move-to);
}

.square.light.target::after {
  background: rgba(61, 122, 85, 0.7);
}

.square.dark.target::after {
  background: rgba(255, 252, 245, 0.85);
}

.square.target.capture::after {
  width: 78%;
  height: 78%;
  background: transparent;
  border: 4px solid var(--move-to);
  border-radius: 50%;
}

.square.light.target.capture::after {
  border-color: rgba(61, 122, 85, 0.85);
}

.square.dark.target.capture::after {
  border-color: rgba(255, 252, 245, 0.9);
}

.square.last-move-from {
  background-image: linear-gradient(var(--last-from), var(--last-from));
  box-shadow: inset 0 0 0 3px var(--last-from-ring);
}

.square.last-move-to {
  background-image: linear-gradient(var(--last-to), var(--last-to));
}

.square.last-move-from.last-move-to {
  /* Roque / cas rare : même case — privilégier le départ */
  background-image: linear-gradient(var(--last-from), var(--last-from));
  box-shadow: inset 0 0 0 3px var(--last-from-ring);
}

.square.in-check {
  background-image: radial-gradient(circle at center, var(--check), transparent 72%);
}

.square .piece {
  display: grid;
  place-items: center;
  width: 88%;
  height: 88%;
  pointer-events: none;
  transition: transform 0.18s ease;
}

.square .piece-svg {
  width: 100%;
  height: 100%;
  display: block;
  overflow: visible;
}

/* Blancs : remplissage clair + contour sombre */
.square .piece.white {
  color: #fffdf7;
  filter: drop-shadow(0 1px 1px rgba(0, 0, 0, 0.4));
}

.square .piece.white .piece-svg *[stroke] {
  stroke: #2c261c;
}

/* Noirs : remplissage sombre */
.square .piece.black {
  color: #17140f;
  filter: drop-shadow(0 1px 1px rgba(255, 255, 255, 0.1));
}

.square.selected .piece {
  transform: translateY(-3px) scale(1.05);
}

.captured {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem 0.75rem;
  justify-content: space-between;
  margin-top: 0.9rem;
  min-height: 1.6rem;
  font-size: 1.15rem;
  letter-spacing: 0.04em;
}

.captured-side.white {
  color: #f7f1e4;
  text-shadow:
    -1px -1px 0 #1c1a16,
    1px -1px 0 #1c1a16,
    -1px 1px 0 #1c1a16,
    1px 1px 0 #1c1a16;
}

.captured-side.black {
  color: var(--captured-black);
}

.sidebar {
  display: flex;
  flex-direction: column;
  gap: 1rem;
  animation: rise 0.85s ease 0.14s both;
}

.panel {
  background: var(--bg-panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 1rem 1.1rem 1.15rem;
  backdrop-filter: blur(10px);
}

.panel h2 {
  margin: 0 0 0.85rem;
  font-family: var(--font-display);
  font-size: 1.55rem;
  font-weight: 600;
  color: var(--accent-strong);
}

.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  margin-bottom: 0.5rem;
}

.panel-head h2 {
  margin: 0;
}

.btn-row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.55rem;
  margin-bottom: 0.7rem;
}

.btn-row.end {
  justify-content: flex-end;
  margin-bottom: 0;
}

.btn {
  appearance: none;
  border: 1px solid var(--line);
  background: var(--btn-bg);
  color: var(--text);
  font: inherit;
  font-size: 0.9rem;
  font-weight: 500;
  padding: 0.55rem 0.9rem;
  border-radius: 9px;
  cursor: pointer;
  transition: background 0.15s ease, border-color 0.15s ease, transform 0.12s ease;
}

.btn:hover {
  background: var(--btn-bg-hover);
  border-color: color-mix(in srgb, var(--accent) 45%, var(--line));
}

.btn:active {
  transform: translateY(1px);
}

.btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.btn-primary {
  background: linear-gradient(180deg, var(--green-bright), var(--green));
  border-color: transparent;
  color: #f4fff7;
}

.btn-primary:hover {
  filter: brightness(1.08);
  background: linear-gradient(180deg, var(--green-bright), var(--green));
}

.btn-accent {
  flex: 1;
  margin-top: 0.35rem;
  background: linear-gradient(180deg, #d4b67f, #b89455);
  border-color: transparent;
  color: #1a140a;
  font-weight: 600;
}

.ai-actions {
  margin-top: 0.35rem;
  margin-bottom: 0;
}

.ai-actions .btn-accent,
.ai-actions .btn-primary {
  margin-top: 0;
  flex: 1;
  min-width: 0;
}

.btn-sm {
  padding: 0.45rem 0.7rem;
  font-size: 0.82rem;
  white-space: nowrap;
}

.field-grid {
  display: grid;
  gap: 0.55rem;
}

.field {
  display: grid;
  gap: 0.3rem;
}

.field > span {
  font-size: 0.78rem;
  color: var(--muted);
  font-weight: 500;
  letter-spacing: 0.02em;
}

.field em {
  font-style: normal;
  color: var(--accent);
}

.field input,
.field select {
  width: 100%;
  border: 1px solid var(--line);
  background: var(--input-bg);
  color: var(--text);
  font: inherit;
  font-size: 0.9rem;
  padding: 0.5rem 0.65rem;
  border-radius: 8px;
}

.field input:focus,
.field select:focus {
  outline: 2px solid color-mix(in srgb, var(--accent) 50%, transparent);
  outline-offset: 1px;
}

.inline {
  display: flex;
  gap: 0.45rem;
}

.inline input {
  flex: 1;
  min-width: 0;
}

.moves {
  list-style: none;
  margin: 0;
  padding: 0;
  max-height: 220px;
  overflow: auto;
  display: grid;
  gap: 0.2rem;
  font-variant-numeric: tabular-nums;
}

.moves li {
  display: grid;
  grid-template-columns: 2.2rem 1fr 1fr;
  gap: 0.4rem;
  padding: 0.28rem 0.4rem;
  border-radius: 6px;
  color: var(--muted);
}

.moves li:nth-child(odd) {
  background: var(--row-odd);
}

.moves .ply {
  color: var(--text);
  font: inherit;
  text-align: left;
  background: transparent;
  border: none;
  padding: 0;
  margin: 0;
  cursor: default;
}

button.ply {
  cursor: pointer;
  border-radius: 4px;
  padding: 0.05rem 0.2rem;
}

button.ply:hover {
  background: color-mix(in srgb, var(--accent) 18%, transparent);
}

button.ply.active {
  background: color-mix(in srgb, var(--green) 40%, transparent);
  color: var(--text);
  font-weight: 600;
}

.moves .num {
  color: var(--accent);
  font-weight: 600;
}

.modal-replay {
  width: min(960px, calc(100vw - 1.5rem));
  max-height: calc(100vh - 1.5rem);
  overflow: auto;
}

.replay-body {
  padding: 1.1rem 1.2rem 1.3rem;
}

.replay-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 1rem;
  margin-bottom: 1rem;
}

.replay-header h2 {
  margin: 0 0 0.25rem;
}

.replay-meta {
  margin: 0 !important;
  font-size: 0.88rem;
}

.replay-layout {
  display: grid;
  grid-template-columns: minmax(0, 1.2fr) minmax(220px, 280px);
  gap: 1.1rem;
  align-items: start;
}

.replay-frame {
  width: min(100%, 480px);
  margin: 0 auto;
}

.replay-status {
  margin-top: 0.7rem;
}

.replay-side h3 {
  margin: 0.75rem 0 0.45rem;
  font-family: var(--font-display);
  font-size: 1.25rem;
  color: var(--accent-strong);
  font-weight: 600;
}

.replay-controls {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
}

.replay-controls .btn {
  min-width: 2.5rem;
  justify-content: center;
}

.replay-toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: end;
  gap: 0.55rem;
  margin-top: 0.65rem;
}

.replay-speed {
  flex: 1;
  min-width: 120px;
}

.replay-progress {
  margin: 0.55rem 0 0 !important;
  font-size: 0.85rem;
  color: var(--muted);
}

.replay-moves {
  max-height: min(42vh, 360px);
}

@media (max-width: 760px) {
  .replay-layout {
    grid-template-columns: 1fr;
  }

  .replay-frame {
    width: min(100%, 420px);
  }
}

.hint {
  margin: 0 0 0.85rem;
  color: var(--muted);
  font-size: 0.82rem;
  line-height: 1.45;
}

.hint code {
  color: var(--accent);
  font-size: 0.78rem;
}

.switch {
  display: inline-flex;
  align-items: center;
  gap: 0.45rem;
  font-size: 0.85rem;
  color: var(--muted);
  cursor: pointer;
  user-select: none;
}

.switch input {
  accent-color: var(--green-bright);
  width: 1rem;
  height: 1rem;
}

.variant-switch {
  margin-top: 0.75rem;
}

.ai-fields {
  border: none;
  margin: 0;
  padding: 0;
  display: grid;
  gap: 0.65rem;
}

.ai-fields:disabled {
  opacity: 0.55;
}

.ai-status {
  min-height: 1.2rem;
  margin: 0.2rem 0 0;
  font-size: 0.82rem;
  color: var(--muted);
}

.ai-status.ok {
  color: var(--green-bright);
}

.ai-status.err {
  color: #e08a7a;
}

.ai-status.busy {
  color: var(--accent);
}

.modal {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--modal-bg);
  color: var(--text);
  padding: 0;
  box-shadow: var(--shadow);
}

.modal::backdrop {
  background: var(--modal-backdrop);
}

.modal-body {
  margin: 0;
  padding: 1.25rem 1.35rem;
}

.modal-body h2 {
  margin: 0 0 0.5rem;
  font-family: var(--font-display);
  color: var(--accent-strong);
}

.modal-body p {
  margin: 0 0 1.1rem;
  color: var(--muted);
}

.promo-choices {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 0.55rem;
}

.promo-btn {
  font-size: 1.8rem;
  padding: 0.7rem 0.4rem;
  line-height: 1;
}

@keyframes rise {
  from {
    opacity: 0;
    transform: translateY(12px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes board-in {
  from {
    opacity: 0;
    transform: scale(0.96) translateY(10px);
  }
  to {
    opacity: 1;
    transform: scale(1) translateY(0);
  }
}

@keyframes pulse-mark {
  0%,
  100% {
    transform: scale(1);
    opacity: 1;
  }
  50% {
    transform: scale(1.08);
    opacity: 0.85;
  }
}

@media (max-width: 1100px) {
  .layout {
    grid-template-columns: minmax(160px, 200px) minmax(0, 1fr) minmax(260px, 300px);
  }
}

@media (max-width: 920px) {
  .layout {
    grid-template-columns: 1fr;
  }

  .board-section {
    order: 1;
  }

  .history-col {
    order: 2;
  }

  .sidebar {
    order: 3;
  }

  .board-frame {
    width: min(100%, 520px);
  }

  .history-col,
  .sidebar {
    max-width: 520px;
    margin: 0 auto;
    width: 100%;
  }

  .history-panel .moves {
    max-height: 220px;
  }
}

@media (max-width: 520px) {
  .app {
    width: calc(100% - 1rem);
  }

  .topbar {
    flex-wrap: wrap;
  }

  .brand-name {
    font-size: 2.2rem;
  }

  .theme-label {
    display: none;
  }
}
