/* ---------- Thèmes ---------- */
:root,
[data-theme="light"] {
  --bg: #f6f7f9;
  --grid: #dfe3e9;
  --grid-strong: #c3c9d3;
  --x: #d6336c;
  --o: #1c7ed6;
  --win: rgba(250, 176, 5, 0.32);
  --win-line: #e67700;
  --hover: rgba(0, 0, 0, 0.06);
  --last: rgba(0, 0, 0, 0.35);

  --panel: rgba(255, 255, 255, 0.84);
  --text: #1b1f27;
  --muted: #5c6573;
  --border: rgba(0, 0, 0, 0.12);
  --btn: #ffffff;
  --btn-hover: #eef1f5;
  --btn-active: #e2e6ec;
  --accent: #3b5bdb;
  --accent-text: #ffffff;
  --focus: #3b5bdb;
  --shadow: 0 6px 24px rgba(20, 30, 50, 0.16);
}

[data-theme="dark"] {
  --bg: #14171c;
  --grid: #262b33;
  --grid-strong: #3a414c;
  --x: #ff6b9a;
  --o: #5aa9ff;
  --win: rgba(255, 196, 40, 0.26);
  --win-line: #ffc428;
  --hover: rgba(255, 255, 255, 0.07);
  --last: rgba(255, 255, 255, 0.4);

  --panel: rgba(28, 32, 39, 0.82);
  --text: #eceff4;
  --muted: #9aa3b2;
  --border: rgba(255, 255, 255, 0.14);
  --btn: #262b33;
  --btn-hover: #2f353f;
  --btn-active: #3a414c;
  --accent: #748ffc;
  --accent-text: #10131a;
  --focus: #91a7ff;
  --shadow: 0 6px 24px rgba(0, 0, 0, 0.5);
}

/* ---------- Base ---------- */
* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 15px;
  -webkit-tap-highlight-color: transparent;
}

#board {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
  cursor: crosshair;
  user-select: none;
  -webkit-user-select: none;
}

#board.dragging {
  cursor: grabbing;
}

/* ---------- Superposition des réglages ---------- */
#overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  display: flex;
  justify-content: center;
  padding: max(10px, env(safe-area-inset-top)) max(10px, env(safe-area-inset-right)) 0 max(10px, env(safe-area-inset-left));
  pointer-events: none;
  z-index: 10;
}

#panel {
  pointer-events: auto;
  width: min(980px, 100%);
  padding: 8px 10px 8px 14px;
  background: var(--panel);
  -webkit-backdrop-filter: blur(14px) saturate(1.3);
  backdrop-filter: blur(14px) saturate(1.3);
  border: 1px solid var(--border);
  border-radius: 16px;
  box-shadow: var(--shadow);
}

.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  min-height: 40px;
}

#status {
  font-size: 1.05rem;
  font-weight: 600;
}

#status .sym {
  display: inline-block;
  min-width: 1.4em;
  text-align: center;
  font-weight: 800;
}

#status .sym.x { color: var(--x); }
#status .sym.o { color: var(--o); }

#status .hint {
  margin-left: 6px;
  font-weight: 400;
  color: var(--muted);
  font-size: 0.9rem;
}

#panel-body {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px 22px;
  margin-top: 6px;
  padding: 10px 4px 4px 0;
  border-top: 1px solid var(--border);
}

#panel-body[hidden] {
  display: none;
}

.field {
  display: flex;
  align-items: center;
  gap: 10px;
}

.field.actions {
  margin-left: auto;
  flex-wrap: wrap;
}

.label {
  color: var(--muted);
  font-size: 0.88rem;
  white-space: nowrap;
}

/* ---------- Contrôles ---------- */
button,
input {
  font: inherit;
  color: inherit;
}

button {
  cursor: pointer;
}

button:focus-visible,
input:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
}

.btn,
.stepper button,
.segmented button,
.icon-btn {
  min-height: 40px;
  border: 1px solid var(--border);
  background: var(--btn);
  transition: background-color 0.12s ease, transform 0.06s ease;
}

.btn {
  padding: 0 14px;
  border-radius: 10px;
}

.btn:hover,
.stepper button:hover,
.segmented button:hover,
.icon-btn:hover {
  background: var(--btn-hover);
}

.btn:active,
.stepper button:active,
.segmented button:active,
.icon-btn:active {
  background: var(--btn-active);
  transform: translateY(1px);
}

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

.btn-primary {
  background: var(--accent);
  border-color: transparent;
  color: var(--accent-text);
  font-weight: 600;
}

.btn-primary:hover {
  background: var(--accent);
  filter: brightness(1.08);
}

.btn-primary:active {
  background: var(--accent);
  filter: brightness(0.94);
}

.icon-btn {
  width: 40px;
  padding: 0;
  display: inline-grid;
  place-items: center;
  border-radius: 10px;
}

.icon-btn svg {
  transition: transform 0.18s ease;
}

.icon-btn[aria-expanded="false"] svg {
  transform: rotate(180deg);
}

.stepper {
  display: inline-flex;
  align-items: stretch;
}

.stepper button {
  width: 40px;
  padding: 0;
  font-size: 1.25rem;
  line-height: 1;
}

.stepper button:first-child { border-radius: 10px 0 0 10px; }
.stepper button:last-child { border-radius: 0 10px 10px 0; }

.stepper input {
  width: 3.2em;
  min-height: 40px;
  text-align: center;
  font-weight: 600;
  background: var(--btn);
  border: 1px solid var(--border);
  border-left: 0;
  border-right: 0;
  border-radius: 0;
  -moz-appearance: textfield;
  appearance: textfield;
}

.stepper input::-webkit-outer-spin-button,
.stepper input::-webkit-inner-spin-button {
  -webkit-appearance: none;
  margin: 0;
}

/* Zoom : deux boutons seuls, séparés */
.stepper button + button:last-child {
  border-left: 0;
}

.segmented {
  display: inline-flex;
}

.segmented button {
  padding: 0 14px;
}

.segmented button:first-child { border-radius: 10px 0 0 10px; }
.segmented button:last-child { border-radius: 0 10px 10px 0; border-left: 0; }

.segmented button[aria-checked="true"] {
  background: var(--accent);
  border-color: transparent;
  color: var(--accent-text);
  font-weight: 600;
}

/* ---------- Petits écrans ---------- */
@media (max-width: 640px) {
  #panel-body {
    gap: 10px 14px;
  }

  .field.actions {
    margin-left: 0;
    width: 100%;
  }

  .field.actions .btn {
    flex: 1 1 auto;
  }
}

@media (prefers-reduced-motion: reduce) {
  * {
    transition: none !important;
  }
}