/* =========================================================
   08 — Système d’ancres et labels dynamiques
========================================================= */

/* --- SYSTÈME D'ANCRES EN POURCENTAGE : placements stables sur l'image intacte --- */
.piece-zone {
  --neighbor-left-x: 12%;
  --neighbor-left-y: 46%;
  --neighbor-right-x: 88.8%;
  --neighbor-right-y: 46%;
  --angle-tl-x: 23.5%;
  --angle-tl-y: 27%;
  --angle-tr-x: 76.5%;
  --angle-tr-y: 27%;
  --angle-bl-x: 21.5%;
  --angle-bl-y: 68%;
  --angle-br-x: 78.5%;
  --angle-br-y: 68%;
  --cut-x: 50%;
  --cut-y: 50%;
  --pre-x: 72%;
  --pre-y: 11%;
}

/* Placement final des libellés ancrés : voisins + angles.
   Les anciens placements en pixels ont été retirés. */
.anchor-label,
.neighbor,
.angle-label {
  position: absolute !important;
  z-index: 12 !important;
  line-height: 1 !important;
  transform: translate(-50%, -50%) !important;
  right: auto !important;
  bottom: auto !important;
}

.neighbor {
  color: var(--fam, #d00000) !important;
  font-size: 24px !important;
  font-weight: 900 !important;
}

.neighbor.left {
  left: var(--neighbor-left-x) !important;
  top: var(--neighbor-left-y) !important;
}

.neighbor.right {
  left: var(--neighbor-right-x) !important;
  top: var(--neighbor-right-y) !important;
}

.angle-label {
  color: #000 !important;
  font-size: 17px !important;
  font-weight: 900 !important;
  white-space: nowrap !important;
}

.angle-label.tl {
  left: var(--angle-tl-x) !important;
  top: var(--angle-tl-y) !important;
}

.angle-label.tr {
  left: var(--angle-tr-x) !important;
  top: var(--angle-tr-y) !important;
}

.angle-label.bl {
  left: var(--angle-bl-x) !important;
  top: var(--angle-bl-y) !important;
}

.angle-label.br {
  left: var(--angle-br-x) !important;
  top: var(--angle-br-y) !important;
}

.cut-label {
  left: var(--cut-x) !important;
  top: var(--cut-y) !important;
  transform: translate(-50%, -50%) !important;
}

.prehole {
  left: var(--pre-x) !important;
  top: var(--pre-y) !important;
  right: auto !important;
  transform: translate(-50%, -50%) !important;
}

/* =========================================================
   09 — Contrôles des ancres / mode réglage
========================================================= */

/* Calques de repère activables depuis le bouton “Afficher les ancres” */
/* --- Ajustements propres : panneau paramètres + mode focus --- */
.controls {
  display: grid !important;
  grid-template-columns: 1.4fr 1fr 1fr 1fr 1fr auto auto auto;
  gap: 12px !important;
  align-items: end !important;
}

/* Structure moins large */
.controls > div:nth-child(1) {
  grid-column: 1 / 2;
  min-width: 220px;
  max-width: 320px;
}

/* Inputs bien alignés */
.controls > div:nth-child(2),
.controls > div:nth-child(3),
.controls > div:nth-child(4),
.controls > div:nth-child(5) {
  min-width: 110px;
}

/* Boutons respirent enfin */
#resetBtn,
#anchorBtn,
#anchorEditBtn {
  white-space: nowrap;
  padding: 8px 12px !important;
  font-size: 13px;
}

.controls > div:nth-child(1) {
  grid-column: span 4;
}

.controls > div:nth-child(2),
.controls > div:nth-child(3),
.controls > div:nth-child(4),
.controls > div:nth-child(5) {
  grid-column: span 2;
}

#resetBtn,
#anchorBtn,
#anchorEditBtn {
  grid-column: span 2;
  min-height: 44px;
  white-space: nowrap;
}

.controls .note {
  grid-column: 1/-1 !important;
}

#freq {
  min-width: 100%;
}

.tab {
  user-select: none;
}

.segment-card.atelier-card {
  cursor: default;
}

#globalView .segment-card.atelier-card {
  cursor: pointer;
}

#globalView .segment-card.atelier-card:hover {
  outline: 2px solid rgba(127, 90, 56, 0.22);
  outline-offset: 4px;
}

/* =========================================================
   10 — Vue focus segment
========================================================= */

#focusView:not(.hidden) {
  display: grid !important;
}

#globalView.hidden,
#focusView.hidden {
  display: none !important;
}

@media (max-width: 1000px) {
  .controls {
    grid-template-columns: repeat(6, minmax(0, 1fr)) !important;
  }

  .controls > div:nth-child(1) {
    grid-column: span 6;
  }

  .controls > div:nth-child(2),
  .controls > div:nth-child(3),
  .controls > div:nth-child(4),
  .controls > div:nth-child(5),
  #resetBtn,
  #anchorBtn,
  #anchorEditBtn {
    grid-column: span 3;
  }
}

@media (max-width: 640px) {
  .controls {
    grid-template-columns: 1fr !important;
  }

  .controls > div:nth-child(1),
  .controls > div:nth-child(2),
  .controls > div:nth-child(3),
  .controls > div:nth-child(4),
  .controls > div:nth-child(5),
  #resetBtn,
  #anchorBtn,
  #anchorEditBtn {
    grid-column: 1/-1;
  }
}

/* Correctif robuste : le bouton Afficher les ancres rend visibles des repères sur chaque image */
body.show-anchors .piece-zone {
  outline: 1px dashed rgba(224, 0, 0, 0.35);
}

.anchor-dot {
  position: absolute !important;
  width: 14px !important;
  height: 14px !important;
  border-radius: 999px !important;
  background: #ff1f1f !important;
  border: 2px solid #fff !important;
  box-shadow: 0 0 0 2px rgba(0, 0, 0, 0.22) !important;
  z-index: 999 !important;
  transform: translate(-50%, -50%) !important;
  display: none !important;
  pointer-events: none !important;
}

body.show-anchors .anchor-dot {
  display: block !important;
}

.anchor-dot.left {
  left: var(--neighbor-left-x) !important;
  top: var(--neighbor-left-y) !important;
}

.anchor-dot.right {
  left: var(--neighbor-right-x) !important;
  top: var(--neighbor-right-y) !important;
}

.anchor-dot.tl {
  left: var(--angle-tl-x) !important;
  top: var(--angle-tl-y) !important;
}

.anchor-dot.tr {
  left: var(--angle-tr-x) !important;
  top: var(--angle-tr-y) !important;
}

.anchor-dot.bl {
  left: var(--angle-bl-x) !important;
  top: var(--angle-bl-y) !important;
}

.anchor-dot.br {
  left: var(--angle-br-x) !important;
  top: var(--angle-br-y) !important;
}

.anchor-dot.ph {
  left: var(--pre-x) !important;
  top: var(--pre-y) !important;
}

.anchor-dot.cut {
  left: var(--cut-x) !important;
  top: var(--cut-y) !important;
}

body.show-anchors #anchorBtn {
  background: #d00000 !important;
  color: white !important;
  border-color: #d00000 !important;
}

/* Mode réglage des ancres : les points passent au-dessus, deviennent déplaçables et ne se comportent plus comme du texte. */
.segment-bg,
.piece-zone img {
  pointer-events: none !important;
  user-select: none !important;
  -webkit-user-select: none !important;
}

.anchor-dot {
  pointer-events: auto !important;
  cursor: grab !important;
  user-select: none !important;
  -webkit-user-select: none !important;
  touch-action: none !important;
  z-index: 5000 !important;
}

.anchor-dot:active {
  cursor: grabbing !important;
}

body.anchor-edit .anchor-dot {
  display: block !important;
}

body.anchor-edit .piece-zone {
  outline: 1px dashed rgba(224, 0, 0, 0.35);
}

body.anchor-edit #anchorEditBtn {
  background: #2f211b !important;
  color: #fff !important;
  border-color: #2f211b !important;
}

.anchor-tools {
  position: fixed;
  right: 18px;
  bottom: 18px;
  width: min(430px, calc(100vw - 36px));
  background: rgba(255, 255, 255, 0.96);
  border: 1px solid rgba(92, 70, 52, 0.25);
  border-radius: 18px;
  box-shadow: 0 16px 48px rgba(0, 0, 0, 0.16);
  padding: 14px;
  z-index: 9000;
  display: none;
  font-family:
    system-ui,
    -apple-system,
    BlinkMacSystemFont,
    "Segoe UI",
    sans-serif;
}

body.anchor-edit .anchor-tools {
  display: block;
}

.anchor-tools h3 {
  margin: 0 0 8px;
  font-size: 14px;
  color: #2f211b;
}

.anchor-tools p {
  margin: 0 0 10px;
  font-size: 12px;
  color: #6d5b4d;
  line-height: 1.35;
}

.anchor-tools textarea {
  width: 100%;
  min-height: 130px;
  resize: vertical;
  border: 1px solid rgba(92, 70, 52, 0.25);
  border-radius: 12px;
  padding: 10px;
  font:
    12px/1.35 ui-monospace,
    SFMono-Regular,
    Menlo,
    Monaco,
    Consolas,
    monospace;
  box-sizing: border-box;
}

.anchor-tools .tool-row {
  display: flex;
  gap: 8px;
  margin-top: 10px;
  flex-wrap: wrap;
}

.anchor-tools button {
  border: 1px solid rgba(92, 70, 52, 0.25);
  background: #f7f0e6;
  border-radius: 999px;
  padding: 8px 12px;
  font-weight: 700;
  cursor: pointer;
}

.anchor-coord-badge {
  position: fixed;
  left: 12px;
  bottom: 12px;
  background: #2f211b;
  color: #fff;
  padding: 7px 10px;
  border-radius: 999px;
  font:
    12px/1 system-ui,
    sans-serif;
  z-index: 9100;
  display: none;
}

body.anchor-edit .anchor-coord-badge {
  display: block;
}

/* Corrections finales — panneau réglage des ancres */
body.anchor-edit .anchor-dot {
  display: block !important;
  pointer-events: auto !important;
  cursor: grab !important;
  user-select: none !important;
  -webkit-user-select: none !important;
  touch-action: none !important;
  z-index: 5000 !important;
}

body.anchor-edit .anchor-dot:active {
  cursor: grabbing !important;
}

body.anchor-edit .piece-zone {
  outline: 1px dashed rgba(224, 0, 0, 0.35) !important;
}

#anchorEditBtn,
#anchorBtn {
  min-width: max-content;
}

.anchor-tools {
  color: #2f2922 !important;
}

.anchor-tools .tool-row {
  display: flex !important;
  gap: 10px !important;
  margin-top: 12px !important;
  flex-wrap: wrap !important;
}

.anchor-tools button {
  display: inline-flex !important;
  align-items: center !important;
  justify-content: center !important;
  gap: 7px !important;
  border: 0 !important;
  border-radius: 10px !important;
  padding: 10px 14px !important;
  font-weight: 800 !important;
  font-size: 13px !important;
  cursor: pointer !important;
  opacity: 1 !important;
  box-shadow: none !important;
  white-space: nowrap !important;
}

#copyAnchorsBtn {
  background: #2f2922 !important;
  color: #fffaf1 !important;
}

#copyAnchorsBtn:hover {
  background: #493b31 !important;
}

#copyAnchorsBtn.copied {
  background: #1f7a4f !important;
  color: #ffffff !important;
}

#resetAnchorsBtn {
  background: #dfd0bb !important;
  color: #2f2922 !important;
}

#resetAnchorsBtn:hover {
  background: #d0bea4 !important;
}

.anchor-tools textarea {
  color: #2f2922 !important;
  background: #fffdf8 !important;
}

#focusView:not(.hidden) {
  display: grid !important;
  grid-template-columns: minmax(0, 1fr) minmax(340px, 390px) !important;
  gap: 18px !important;
  align-items: start !important;
  overflow: visible !important;
}

#focusCard {
  min-width: 0 !important;
  overflow: visible !important;
}

#focusCard .segment-card.atelier-card {
  width: 100% !important;
  min-width: 0 !important;
  overflow: visible !important;
  padding: 38px 14px 54px !important;
  box-sizing: border-box !important;
}

#focusCard .atelier-row {
  display: grid !important;
  grid-template-columns: 70px minmax(0, 1fr) !important;
  gap: 10px !important;
  align-items: center !important;
  min-width: 0 !important;
}

#focusCard .piece-zone {
  width: 100% !important;
  max-width: 849px !important;
  aspect-ratio: 849 / 243 !important;
  height: auto !important;
  min-width: 0 !important;
  transform: none !important;
  margin: 0 auto !important;
  position: relative !important;
  overflow: visible !important;
}

#focusCard .segment-bg {
  position: absolute !important;
  inset: 0 !important;
  width: 100% !important;
  height: 100% !important;
  object-fit: contain !important;
}

#focusCard .side-label {
  font-size: 20px !important;
  line-height: 1.45 !important;
  padding-top: 0 !important;
}

#focusView .detail-table {
  min-width: 0 !important;
  width: 100% !important;
  box-sizing: border-box !important;
}

@media (max-width: 1150px) {
  #focusView:not(.hidden) {
    grid-template-columns: 1fr !important;
  }

  #focusCard {
    overflow: visible !important;
  }

  #focusCard .segment-card.atelier-card {
    overflow: visible !important;
  }

  #focusCard .atelier-row {
    grid-template-columns: 78px minmax(0, 1fr) !important;
  }

  #focusView .detail-table {
    max-width: 720px !important;
  }
}

@media (max-width: 760px) {
  #focusCard .atelier-row {
    grid-template-columns: 1fr !important;
  }

  #focusCard .side-label {
    display: flex !important;
    gap: 8px !important;
    align-items: center !important;
  }
}

/* ===== FIX STABLE — focus segment lisible Chrome + Safari ===== */

#focusView:not(.hidden) {
  grid-template-columns: 1fr !important;
  max-width: 720px !important;
  margin: 0 auto !important;
}

#focusCard .piece-zone {
  height: 160px !important;
  min-height: 160px !important;
}

#focusView .detail-table {
  max-width: 720px !important;
  width: 100% !important;
  margin: 0 auto !important;
}
