/* ── Reset & variables ───────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg:       #0f0f1a;
  --bg2:      #16162a;
  --bg3:      #1e1e34;
  --border:   #2e2e4a;
  --text:     #e2e2f0;
  --text-dim: #888aaa;
  --accent:   #e8a020;
  --accent2:  #4fc3f7;
  --green:    #4caf50;
  --orange:   #ff9800;
  --red:      #f44336;
  --gold:     #ffd700;
  --radius:   8px;
}

html, body {
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, sans-serif;
  font-size: 14px;
  line-height: 1.5;
}

/* ── Layout ──────────────────────────────────────────────────────────────── */
#app {
  display: grid;
  grid-template-rows: auto auto 1fr auto;  /* header, series-tabs, main, status */
  min-height: 100vh;
}

header {
  background: var(--bg2);
  border-bottom: 1px solid var(--border);
  padding: 10px 18px;
  display: flex;
  align-items: center;
  gap: 14px;
}

header h1 {
  font-size: 1.1rem;
  font-weight: 700;
  color: var(--accent);
  letter-spacing: .5px;
}

header .subtitle {
  font-size: .75rem;
  color: var(--text-dim);
}

main {
  display: grid;
  grid-template-columns: 1fr 300px;
  gap: 0;
  overflow: hidden;
}

@media (max-width: 768px) {
  main { grid-template-columns: 1fr; grid-template-rows: auto auto; }
  #sidebar { border-left: none; border-top: 1px solid var(--border); }
}

/* ── Canvas panel ────────────────────────────────────────────────────────── */
#canvas-panel {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 14px;
  background: var(--bg);
  position: relative;
}

#target-canvas {
  max-width: 100%;
  max-height: calc(100vh - 140px);
  border-radius: var(--radius);
  box-shadow: 0 4px 24px rgba(0,0,0,.6);
  cursor: crosshair;
  display: block;
}

/* ── Toolbar ─────────────────────────────────────────────────────────────── */
#toolbar {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  justify-content: center;
  margin-bottom: 10px;
}

/* ── Sidebar ─────────────────────────────────────────────────────────────── */
#sidebar {
  background: var(--bg2);
  border-left: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.sidebar-section {
  padding: 12px 14px;
  border-bottom: 1px solid var(--border);
}

.sidebar-title {
  font-size: .72rem;
  font-weight: 700;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: .6px;
  margin-bottom: 8px;
}

/* ── Score totaux ────────────────────────────────────────────────────────── */
.totals {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}

.total-box {
  background: var(--bg3);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 8px;
  text-align: center;
}

.total-box .label {
  font-size: .68rem;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: .4px;
}

.total-box .value {
  font-size: 1.6rem;
  font-weight: 800;
  line-height: 1.2;
  color: var(--accent);
}

.score-hi  { color: var(--green)  !important; }
.score-mid { color: var(--orange) !important; }
.score-lo  { color: var(--red)    !important; }

/* ── Table impacts ───────────────────────────────────────────────────────── */
#impact-list-wrapper {
  flex: 1;
  overflow-y: auto;
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: .8rem;
}

thead th {
  position: sticky;
  top: 0;
  background: var(--bg2);
  color: var(--text-dim);
  font-weight: 600;
  text-transform: uppercase;
  font-size: .65rem;
  letter-spacing: .4px;
  padding: 6px 6px;
  border-bottom: 1px solid var(--border);
}

tbody tr {
  cursor: pointer;
  transition: background .15s;
}

tbody tr:hover { background: rgba(255,255,255,.04); }
tbody tr.selected { background: rgba(232,160,32,.12); }

td {
  padding: 5px 6px;
  border-bottom: 1px solid rgba(255,255,255,.04);
  white-space: nowrap;
}

.score-cell {
  font-weight: 700;
  font-size: .95rem;
}

.score-cell.score-10 { color: var(--gold);   }
.score-cell.score-9  { color: #fff;          }
.score-cell.score-8  { color: #ddd;          }
.score-cell.score-7  { color: #bbb;          }
.score-cell.score-6  { color: var(--accent2); }
.score-cell.score-5  { color: #81d4fa;       }
.score-cell.score-4,
.score-cell.score-3  { color: var(--text-dim); }
.score-cell.score-2,
.score-cell.score-1  { color: var(--orange);  }
.score-cell.score-0  { color: var(--red);     }

.flag-doubtful { color: var(--orange); font-size: .85em; margin-left: 4px; }
.inner-ten-badge { color: var(--gold); font-size: .9em; }

/* ── Boutons ─────────────────────────────────────────────────────────────── */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 6px 12px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg3);
  color: var(--text);
  font-size: .8rem;
  font-weight: 600;
  cursor: pointer;
  transition: background .15s, border-color .15s;
  user-select: none;
}

.btn:hover { background: rgba(255,255,255,.08); border-color: #555; }
.btn.active { background: var(--accent); color: #000; border-color: var(--accent); }
.btn.danger { border-color: var(--red); }
.btn.danger:hover { background: rgba(244,67,54,.15); }
.btn:disabled { opacity: .4; cursor: default; }

.btn-upload {
  background: var(--accent);
  color: #000;
  border-color: var(--accent);
  font-weight: 700;
}
.btn-upload:hover { background: #f0b040; }

/* ── Status bar ──────────────────────────────────────────────────────────── */
#status-bar {
  font-size: .75rem;
  color: var(--text-dim);
  padding: 6px 18px;
  background: var(--bg2);
  border-top: 1px solid var(--border);
}

#status-bar.error { color: var(--red); }

/* ── Upload label ────────────────────────────────────────────────────────── */
label.btn { cursor: pointer; }
input[type="file"] { display: none; }

/* ── Légende ─────────────────────────────────────────────────────────────── */
.legend {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  font-size: .7rem;
}

.legend-item {
  display: flex;
  align-items: center;
  gap: 4px;
}

.legend-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  border: 1.5px solid currentColor;
}

/* ── Phase 3 : badges header ────────────────────────────────────────────── */
.header-badges { display:flex; gap:6px; margin-left:auto; flex-wrap:wrap; }
.cv-badge.off   { color:var(--text-dim); border-color:var(--border); opacity:.5; }
.cv-badge.error { color:var(--red);   border-color:var(--red);   }

/* Bouton charger modèle */
.btn-ml {
  background: linear-gradient(135deg, #1a2b1a, #0d1a0d);
  border-color: #4caf50;
  color: #4caf50;
  font-weight: 700;
}
.btn-ml:hover { background: rgba(76,175,80,.12); }

/* Petit bouton ghost (ArUco) */
.btn-ghost-small {
  font-size: .72rem;
  padding: 4px 8px;
  opacity: .8;
}
.btn-ghost-small:hover { opacity: 1; }

/* Flag watershed */
.flag-ws {
  display: inline-block;
  font-size: .65rem;
  font-weight: 700;
  color: var(--accent2);
  border: 1px solid var(--accent2);
  border-radius: 3px;
  padding: 0 3px;
  margin-left: 3px;
  vertical-align: middle;
}

/* ── Phase 2 : bouton Analyser ──────────────────────────────────────────── */
.btn-analyse {
  background: linear-gradient(135deg, #1a3a5c, #0d2137);
  border-color: var(--accent2);
  color: var(--accent2);
  font-weight: 700;
}
.btn-analyse:not(:disabled):hover {
  background: rgba(79,195,247,.15);
}
.btn-analyse:disabled {
  opacity: .35;
}

.toolbar-sep {
  width: 1px;
  height: 24px;
  background: var(--border);
  margin: 0 2px;
}

/* ── Badge CV ────────────────────────────────────────────────────────────── */
.cv-badge {
  margin-left: auto;
  font-size: .7rem;
  font-weight: 700;
  padding: 3px 8px;
  border-radius: 4px;
  border: 1px solid;
}
.cv-badge.loading { color: var(--text-dim); border-color: var(--border); }
.cv-badge.ready   { color: var(--green);   border-color: var(--green);  }

/* ── Overlay pipeline ────────────────────────────────────────────────────── */
#pipeline-overlay {
  position: absolute;
  inset: 0;
  background: rgba(10,10,20,.82);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  z-index: 10;
  border-radius: var(--radius);
}

#pipeline-status-text {
  font-size: .82rem;
  color: var(--text);
  text-align: center;
  max-width: 80%;
  line-height: 1.5;
}

#pipeline-status-text.pipeline-error { color: var(--red); }

.pipeline-spinner {
  width: 36px;
  height: 36px;
  border: 3px solid rgba(79,195,247,.2);
  border-top-color: var(--accent2);
  border-radius: 50%;
  animation: spin .7s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

/* ── Geometry info ───────────────────────────────────────────────────────── */
#geometry-info {
  font-size: .7rem;
  color: var(--text-dim);
  min-height: 1.2em;
  margin-bottom: 4px;
  font-family: monospace;
}

.legend-dot.green  { background: rgba(76,175,80,.3);  color: var(--green);  }
.legend-dot.orange { background: rgba(255,152,0,.3);  color: var(--orange); }
.legend-dot.red    { background: rgba(244,67,54,.3);  color: var(--red);    }

/* ── Phase 4 : validation des impacts douteux ───────────────────────────── */
#validation-panel {
  background: rgba(255,152,0,.06);
  border-bottom-color: rgba(255,152,0,.25);
}

.val-info-text {
  font-size: .8rem;
  color: var(--text-dim);
  margin-bottom: 8px;
  font-family: monospace;
}

.val-buttons {
  display: flex;
  gap: 8px;
}

.btn-val-confirm {
  color: var(--green);
  border-color: var(--green);
  flex: 1;
}
.btn-val-confirm:hover { background: rgba(76,175,80,.15); }

.btn-val-lower {
  color: var(--orange);
  border-color: var(--orange);
  flex: 1;
}
.btn-val-lower:hover { background: rgba(255,152,0,.15); }

/* Badges de validation dans la liste */
.val-confirmed {
  display: inline-block;
  color: var(--green);
  font-weight: 700;
  font-size: .85em;
  margin-left: 3px;
}
.val-lowered {
  display: inline-block;
  color: var(--orange);
  font-weight: 700;
  font-size: .85em;
  margin-left: 3px;
}

/* ── Phase 4 : export ────────────────────────────────────────────────────── */
.export-buttons {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}

.btn-export {
  flex: 1;
  min-width: 52px;
  justify-content: center;
  font-size: .75rem;
  padding: 5px 8px;
}
.btn-export:hover { background: rgba(255,255,255,.08); border-color: #555; }

/* ── Phase 4 : responsive mobile ────────────────────────────────────────── */
@media (max-width: 768px) {
  header h1     { font-size: .95rem; }
  header .subtitle { display: none; }

  #toolbar {
    justify-content: flex-start;
    overflow-x: auto;
    flex-wrap: nowrap;
    padding-bottom: 6px;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
  }
  #toolbar::-webkit-scrollbar { display: none; }

  .btn           { font-size: .73rem; padding: 5px 9px; white-space: nowrap; }
  .toolbar-sep   { flex-shrink: 0; }

  #target-canvas { max-height: calc(100vw - 28px); }

  .totals        { grid-template-columns: repeat(3, 1fr); }
  .total-box .value { font-size: 1.3rem; }

  .val-buttons, .export-buttons { flex-wrap: nowrap; }
}

/* ── Boutons mode alignement (Phase 6) ───────────────────────────────────── */
.btn-align {
  background: #3b82f6;
  color: #fff;
  font-weight: 600;
  border-color: transparent;
}
.btn-align:hover { background: #2563eb; }

.btn-validate-align {
  background: #22c55e;
  color: #fff;
  font-weight: 700;
  border-color: transparent;
}
.btn-validate-align:hover { background: #16a34a; }

.btn-readjust {
  background: none;
  border-color: #6b7280;
  color: #9ca3af;
}
.btn-readjust:hover { border-color: #d1d5db; color: var(--text); }

/* ── Contrôles d'alignement (zoom + stretch X/Y + rotation) ─────────────── */
#align-controls {
  display: flex;
  flex-direction: column;
  gap: 5px;
  padding: 8px 12px;
  background: rgba(59, 130, 246, 0.08);
  border-bottom: 1px solid rgba(59, 130, 246, 0.25);
}

.slider-row {
  display: flex;
  align-items: center;
  gap: 8px;
}

.slider-row label {
  font-size: .78rem;
  color: #93c5fd;
  white-space: nowrap;
  min-width: 80px;
}

.slider-row input[type="range"] {
  flex: 1;
  accent-color: #3b82f6;
  height: 4px;
  cursor: pointer;
}

.slider-row span {
  font-size: .78rem;
  color: #e2e8f0;
  min-width: 48px;
  text-align: right;
  font-variant-numeric: tabular-nums;
}

/* ── Séance : boutons archive / historique ───────────────────────────────── */
.session-actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.btn-archive {
  background: linear-gradient(135deg, var(--accent) 0%, #c97010 100%);
  color: #000;
  font-weight: 700;
  border-color: transparent;
  flex: 1;
}
.btn-archive:hover {
  background: linear-gradient(135deg, #f0b030 0%, var(--accent) 100%);
}

.hist-count {
  display: inline-block;
  background: var(--accent);
  color: #000;
  font-size: .7rem;
  font-weight: 700;
  border-radius: 10px;
  padding: 0 5px;
  margin-left: 4px;
  line-height: 1.5;
  vertical-align: middle;
}

/* ── Modal input ──────────────────────────────────────────────────────────── */
.modal-input {
  width: 100%;
  padding: 8px 10px;
  background: var(--bg3);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  font-size: .9rem;
  outline: none;
  transition: border-color .15s;
}
.modal-input:focus {
  border-color: var(--accent);
}

/* ── Liste historique ─────────────────────────────────────────────────────── */
.hist-list {
  max-height: 50vh;
  overflow-y: auto;
}

.hist-card {
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
}
.hist-card:last-child { border-bottom: none; }

.hist-card-info {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 8px;
  margin-bottom: 3px;
}

.hist-label {
  font-size: .88rem;
  font-weight: 600;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 180px;
}

.hist-date {
  font-size: .72rem;
  color: var(--text-dim);
  white-space: nowrap;
  flex-shrink: 0;
}

.hist-card-score {
  font-size: .8rem;
  color: var(--accent);
  margin-bottom: 7px;
}

.hist-score {
  font-size: 1.1rem;
  font-weight: 700;
}

.hist-card-actions {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}

/* Bouton primaire : Charger */
.hist-btn-load {
  background: var(--accent);
  color: #000;
  font-weight: 600;
  border-color: transparent;
  font-size: .78rem;
  padding: 3px 10px;
}
.hist-btn-load:hover { background: #f0b030; }

/* Boutons ghost : JSON / CSV */
.hist-btn-json,
.hist-btn-csv {
  background: none;
  border-color: var(--border);
  color: var(--text-dim);
  font-size: .78rem;
  padding: 3px 8px;
}
.hist-btn-json:hover,
.hist-btn-csv:hover  { border-color: var(--accent2); color: var(--accent2); }

/* Bouton danger : Supprimer */
.hist-btn-del {
  background: none;
  border-color: var(--red);
  color: var(--red);
  font-size: .78rem;
  padding: 3px 8px;
}
.hist-btn-del:hover { background: var(--red); color: #fff; }

.hist-empty {
  text-align: center;
  color: var(--text-dim);
  padding: 20px;
  font-size: .85rem;
}

/* ── Onglets séries ──────────────────────────────────────────────────────── */
#series-tabs {
  display: flex;
  background: var(--bg2);
  border-bottom: 1px solid var(--border);
  overflow-x: auto;
  scrollbar-width: none;
  flex-shrink: 0;
}
#series-tabs::-webkit-scrollbar { display: none; }

.series-tab {
  flex: 1;
  min-width: 88px;
  padding: 9px 10px;
  background: none;
  border: none;
  border-bottom: 2px solid transparent;
  color: var(--text-dim);
  font-size: .82rem;
  font-weight: 600;
  cursor: pointer;
  transition: color .15s, border-color .15s, background .15s;
  white-space: nowrap;
  text-align: center;
}
.series-tab:hover  { color: var(--text); background: rgba(255,255,255,.04); }
.series-tab.active { color: var(--accent); border-bottom-color: var(--accent); }
.series-tab.done   { color: var(--green); }

/* ── Tableau total général ──────────────────────────────────────────────── */
.tg-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 1px;
  background: var(--border);
  border-radius: 6px;
  overflow: hidden;
  font-variant-numeric: tabular-nums;
}
.tg-cell {
  background: var(--bg3);
  padding: 5px 3px;
  text-align: center;
  font-size: .78rem;
  font-weight: 600;
  color: var(--text);
}
.tg-head {
  background: var(--bg2);
  font-size: .6rem;
  font-weight: 700;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: .4px;
  padding: 4px 2px;
}
.tg-score { font-size: .88rem; }
.tg-total { color: var(--accent); font-weight: 800; font-size: .9rem; }
.tg-sub   { font-size: .62rem; color: var(--text-dim); font-weight: 400; }

/* ── Boutons galerie / appareil photo ───────────────────────────────────── */
.btn-gallery {
  background: var(--accent);
  color: #000;
  border-color: var(--accent);
  font-weight: 700;
}
.btn-gallery:hover { background: #f0b040; }

.btn-camera {
  background: linear-gradient(135deg, #1a2b4a, #0d1a2d);
  border-color: var(--accent2);
  color: var(--accent2);
  font-weight: 700;
}
.btn-camera:hover { background: rgba(79,195,247,.15); }

/* ── Avertissement nombre d'impacts ─────────────────────────────────────── */
.impact-count-warning {
  margin-top: 7px;
  padding: 5px 8px;
  border-radius: 5px;
  font-size: .75rem;
  font-weight: 600;
  text-align: center;
}
.impact-count-ok   { background:rgba(76,175,80,.12);  color:var(--green);  border:1px solid rgba(76,175,80,.3); }
.impact-count-low  { background:rgba(255,152,0,.12);  color:var(--orange); border:1px solid rgba(255,152,0,.3); }
.impact-count-high { background:rgba(244,67,54,.12);  color:var(--red);    border:1px solid rgba(244,67,54,.3); }

/* ── Badges source / suspect dans la liste ──────────────────────────────── */
.flag-suspect { color:#888; font-size:.85em; margin-left:3px; cursor:help; }
.flag-manual  { color:#3b82f6; font-size:.85em; margin-left:3px; }

/* ── Sélecteur de score inline (édition manuelle) ───────────────────────── */
.score-inline-select {
  background: var(--bg3);
  border: 1px solid var(--accent);
  border-radius: 4px;
  color: var(--text);
  font-size: .85rem;
  font-weight: 700;
  padding: 1px 3px;
  cursor: pointer;
  width: 100%;
}

/* ── Impression / PDF ────────────────────────────────────────────────────── */
@media print {
  header, #toolbar, #status-bar, #validation-panel,
  .export-buttons, #geometry-info { display: none !important; }

  #app    { display: block; }
  main    { display: block; }
  #canvas-panel { padding: 0; }
  #sidebar { border: none; }
  #target-canvas { max-height: none; width: 50vw; height: 50vw; }

  tbody tr:hover { background: none; }
  tbody tr.selected { background: none; }
}
