/* NC Analyzer — Astor Logistics v2 */
:root {
  --primary: #2a4b7e;
  --primary-light: #3f6caf;
  --accent: #e8a838;
  --success: #27ae60;
  --danger: #e74c3c;
  --warning: #f39c12;
  --bg: #f0f2f5;
  --card: #ffffff;
  --text: #2c3e50;
  --text-light: #7f8c8d;
  --border: #dce1e8;
  --sidebar-w: 260px;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

body {
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
}

/* ── Login ── */
.login-page {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: linear-gradient(135deg, var(--primary) 0%, var(--primary-light) 100%);
}

.login-card {
  background: white;
  border-radius: 12px;
  padding: 40px;
  width: 400px;
  max-width: 90vw;
  box-shadow: 0 20px 60px rgba(0,0,0,0.3);
}

.login-card h1 { color: var(--primary); font-size: 1.5rem; margin-bottom: 8px; }
.login-card .subtitle { color: var(--text-light); font-size: 0.9rem; margin-bottom: 28px; }
.login-card .logo-text { font-size: 0.8rem; font-weight: 600; color: var(--primary); text-transform: uppercase; letter-spacing: 2px; margin-bottom: 24px; }

/* ── Layout ── */
.app-layout { display: flex; min-height: 100vh; }

.sidebar {
  width: var(--sidebar-w);
  background: var(--primary);
  color: white;
  display: flex;
  flex-direction: column;
  position: fixed;
  top: 0; left: 0; bottom: 0;
  z-index: 100;
}

.sidebar-brand {
  padding: 20px;
  border-bottom: 1px solid rgba(255,255,255,0.15);
  display: flex;
  align-items: center;
  gap: 12px;
}

.sidebar-brand .back-home {
  color: rgba(255,255,255,0.6);
  text-decoration: none;
  font-size: 1.2rem;
  width: 32px; height: 32px;
  display: flex; align-items: center; justify-content: center;
  border-radius: 8px;
  transition: all 0.2s;
  flex-shrink: 0;
}

.sidebar-brand .back-home:hover { background: rgba(255,255,255,0.15); color: white; }
.sidebar-brand h2 { font-size: 1rem; font-weight: 700; letter-spacing: 1px; }
.sidebar-brand small { opacity: 0.7; font-size: 0.75rem; }

.sidebar-nav { flex: 1; padding: 12px 0; }

.sidebar-nav a {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 20px;
  color: rgba(255,255,255,0.8);
  text-decoration: none;
  font-size: 0.9rem;
  transition: all 0.2s;
}

.sidebar-nav a:hover, .sidebar-nav a.active { background: rgba(255,255,255,0.15); color: white; }
.sidebar-nav a .icon { font-size: 1.2rem; width: 24px; text-align: center; }

.sidebar-user { padding: 16px 20px; border-top: 1px solid rgba(255,255,255,0.15); font-size: 0.85rem; }
.sidebar-user .user-name { font-weight: 600; }
.sidebar-user .user-email { opacity: 0.7; font-size: 0.75rem; }

.main-content { margin-left: var(--sidebar-w); flex: 1; padding: 24px; min-height: 100vh; }

/* ── Cards ── */
.card {
  background: var(--card);
  border-radius: 10px;
  box-shadow: 0 1px 3px rgba(0,0,0,0.08);
  border: 1px solid var(--border);
  margin-bottom: 20px;
}

.card-header {
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.card-header h3 { font-size: 1rem; color: var(--primary); }
.card-body { padding: 20px; }

/* ── Forms ── */
.form-group { margin-bottom: 16px; }
.form-group label { display: block; font-size: 0.85rem; font-weight: 600; margin-bottom: 6px; color: var(--text); }

.form-control {
  width: 100%;
  padding: 10px 14px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 0.9rem;
  font-family: inherit;
  transition: border-color 0.2s;
}

.form-control:focus { outline: none; border-color: var(--primary-light); box-shadow: 0 0 0 3px rgba(63,108,175,0.15); }
textarea.form-control { min-height: 60px; resize: vertical; }
.form-row { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }

/* ── Buttons ── */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 10px 20px;
  border: none;
  border-radius: 8px;
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
  font-family: inherit;
}

.btn-primary { background: var(--primary); color: white; }
.btn-primary:hover { background: var(--primary-light); }
.btn-success { background: var(--success); color: white; }
.btn-success:hover { background: #219a52; }
.btn-danger { background: var(--danger); color: white; }
.btn-warning { background: var(--warning); color: white; }
.btn-outline { background: transparent; border: 1px solid var(--border); color: var(--text); }
.btn-outline:hover { background: var(--bg); }
.btn-sm { padding: 6px 14px; font-size: 0.8rem; }
.btn-lg { padding: 14px 28px; font-size: 1rem; }
.btn:disabled { opacity: 0.6; cursor: not-allowed; }

/* ── Badges ── */
.badge { display: inline-block; padding: 3px 10px; border-radius: 12px; font-size: 0.75rem; font-weight: 600; }
.badge-brouillon { background: #eee; color: #666; }
.badge-en_analyse, .badge-extraction { background: #fff3cd; color: #856404; }
.badge-fiche_prete { background: #d1ecf1; color: #0c5460; }
.badge-investigation, .badge-investigation_complete { background: #e2d4f0; color: #5b2c8b; }
.badge-analyse_complete { background: #d4edda; color: #155724; }
.badge-reponse_envoyee { background: #cce5ff; color: #004085; }
.badge-G1 { background: #d4edda; color: #155724; }
.badge-G2 { background: #fff3cd; color: #856404; }
.badge-G3 { background: #f8d7da; color: #721c24; }
.badge-G4 { background: #721c24; color: white; }

/* ── Phase bar ── */
.phase-bar {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0;
  margin-bottom: 24px;
  padding: 20px;
  background: white;
  border-radius: 10px;
  box-shadow: 0 1px 3px rgba(0,0,0,0.08);
  border: 1px solid var(--border);
}

.phase-step {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  min-width: 120px;
}

.phase-circle {
  width: 36px; height: 36px;
  border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-weight: 700;
  font-size: 0.85rem;
  border: 2px solid var(--border);
  color: var(--text-light);
  background: white;
  transition: all 0.3s;
}

.phase-step.done .phase-circle { background: var(--success); color: white; border-color: var(--success); }
.phase-step.current .phase-circle { background: var(--primary); color: white; border-color: var(--primary); animation: pulse 2s infinite; }

.phase-label { font-size: 0.75rem; font-weight: 600; color: var(--text-light); text-align: center; }
.phase-step.done .phase-label { color: var(--success); }
.phase-step.current .phase-label { color: var(--primary); }

.phase-line { flex: 1; height: 2px; background: var(--border); max-width: 60px; }
.phase-line.done { background: var(--success); }

.phase-indicator { font-size: 0.75rem; font-weight: 700; color: var(--primary); }

@keyframes pulse { 0%, 100% { box-shadow: 0 0 0 0 rgba(42,75,126,0.3); } 50% { box-shadow: 0 0 0 8px rgba(42,75,126,0); } }

/* ── NC table ── */
.nc-table { width: 100%; border-collapse: collapse; }
.nc-table th { text-align: left; padding: 12px 16px; font-size: 0.8rem; text-transform: uppercase; letter-spacing: 0.5px; color: var(--text-light); border-bottom: 2px solid var(--border); }
.nc-table td { padding: 12px 16px; border-bottom: 1px solid var(--border); font-size: 0.9rem; }
.nc-table tr:hover td { background: #f8f9fa; }
.nc-table tr { cursor: pointer; }

/* ── Upload zone ── */
.upload-zone { border: 2px dashed var(--border); border-radius: 10px; padding: 24px; text-align: center; transition: all 0.2s; cursor: pointer; }
.upload-zone:hover, .upload-zone.dragover { border-color: var(--primary-light); background: rgba(63,108,175,0.05); }
.upload-zone p { color: var(--text-light); font-size: 0.85rem; }
.file-list { margin-top: 12px; }
.file-item { display: flex; align-items: center; gap: 10px; padding: 8px 12px; background: #f8f9fa; border-radius: 6px; margin-bottom: 6px; font-size: 0.85rem; }
.file-item .remove { color: var(--danger); cursor: pointer; margin-left: auto; }

/* ── Timeline ── */
.timeline { position: relative; padding-left: 24px; }
.timeline::before { content: ''; position: absolute; left: 7px; top: 4px; bottom: 4px; width: 2px; background: var(--border); }
.timeline-item { position: relative; margin-bottom: 16px; display: flex; gap: 12px; }
.timeline-dot { position: absolute; left: -20px; top: 6px; width: 10px; height: 10px; border-radius: 50%; background: var(--primary); border: 2px solid white; box-shadow: 0 0 0 2px var(--primary); }
.timeline-content { flex: 1; }
.timeline-date { font-size: 0.75rem; font-weight: 700; color: var(--primary); margin-bottom: 2px; }

/* ── Source badges ── */
.source-badge { display: inline-block; padding: 1px 8px; border-radius: 10px; font-size: 0.65rem; font-weight: 700; margin-left: 4px; }
.source-DOC { background: #cce5ff; color: #004085; }
.source-PHOTO { background: #e8d5f5; color: #5b2c8b; }
.source-\? { background: #fff3cd; color: #856404; }
.source-REPONSE { background: #d4edda; color: #155724; }
.source-INFERENCE { background: #e2e3e5; color: #383d41; }

/* ── Type badges (FAIT/HYPO/INF) ── */
.type-badge { display: inline-block; padding: 1px 8px; border-radius: 10px; font-size: 0.65rem; font-weight: 700; }

/* ── Fact blocks ── */
.fact-block { padding: 12px 16px; background: #f8f9fa; border-radius: 8px; }
.fact-block h4 { font-size: 0.8rem; color: var(--primary); margin-bottom: 6px; text-transform: uppercase; letter-spacing: 0.5px; }
.fact-block p { font-size: 0.9rem; line-height: 1.6; }

.fact-item { display: flex; align-items: flex-start; gap: 8px; padding: 8px 12px; margin-bottom: 6px; background: #f8f9fa; border-radius: 6px; font-size: 0.85rem; }

.missing-elements { padding: 16px; background: #fff8e1; border-left: 3px solid var(--warning); border-radius: 0 8px 8px 0; }
.missing-elements ul { list-style: none; padding: 0; }
.missing-elements li { padding: 4px 0; font-size: 0.85rem; }
.missing-elements li::before { content: "? "; color: var(--warning); font-weight: bold; }

/* ── Investigation ── */
.progress-bar-container { width: 100%; height: 6px; background: var(--border); border-radius: 3px; margin-bottom: 20px; overflow: hidden; }
.progress-bar-fill { height: 100%; background: var(--success); border-radius: 3px; transition: width 0.3s; }

.investigation-card { padding: 16px; border: 1px solid var(--border); border-radius: 10px; margin-bottom: 16px; transition: all 0.2s; }
.investigation-card.answered { border-color: var(--success); background: #f9fefb; }
.investigation-header { display: flex; align-items: center; gap: 8px; margin-bottom: 8px; flex-wrap: wrap; }
.investigation-question { font-weight: 600; font-size: 0.95rem; margin-bottom: 6px; }
.investigation-context { font-size: 0.8rem; color: var(--text-light); font-style: italic; margin-bottom: 12px; line-height: 1.5; }
.investigation-answer { display: flex; flex-direction: column; gap: 8px; }

.cat-badge { display: inline-block; padding: 2px 10px; border-radius: 10px; font-size: 0.7rem; font-weight: 600; text-transform: uppercase; letter-spacing: 0.5px; }
.priority-high { font-size: 0.7rem; font-weight: 700; color: var(--danger); }

.nsp-toggle { display: flex; align-items: center; gap: 6px; font-size: 0.8rem; color: var(--text-light); cursor: pointer; }
.nsp-toggle input { cursor: pointer; }

.terrain-points { list-style: none; padding: 0; }
.terrain-points li { padding: 10px 14px; background: #f0f9ff; border-left: 3px solid var(--primary); border-radius: 0 6px 6px 0; margin-bottom: 8px; font-size: 0.9rem; }
.terrain-points li::before { content: ">> "; color: var(--primary); font-weight: bold; }

/* -- Cause tree (horizontal: causes LEFT -> NC RIGHT) -- */
.cause-tree-container {
  overflow-x: auto;
  padding: 20px 0;
}

.cause-tree {
  display: flex;
  flex-direction: row-reverse;
  align-items: center;
  gap: 0;
  min-width: max-content;
}

.tree-node-wrap {
  display: flex;
  flex-direction: row-reverse;
  align-items: center;
}

.tree-node-box {
  min-width: 220px;
  max-width: 320px;
  padding: 10px 12px;
  border-radius: 8px;
  border: 2px solid #ddd;
  background: white;
  box-shadow: 0 1px 4px rgba(0,0,0,0.08);
  position: relative;
}

.tree-node-box.type-FAIT { border-color: #27ae60; background: #f0faf4; }
.tree-node-box.type-HYPOTHESE { border-color: #e67e22; background: #fef8f0; }
.tree-node-box.type-INFERENCE { border-color: #3498db; background: #f0f6fc; }
.tree-node-box.root-node { border-width: 3px; border-color: #e74c3c; background: #fdf2f2; }

.tree-connector {
  display: flex;
  align-items: center;
  padding: 0 8px;
}

.tree-connector-line {
  width: 30px;
  height: 2px;
  background: #bbb;
}

.tree-logic-badge {
  font-size: 0.65rem;
  font-weight: 700;
  color: white;
  background: var(--primary);
  padding: 2px 8px;
  border-radius: 10px;
  margin: 0 4px;
  white-space: nowrap;
}

.tree-children {
  display: flex;
  flex-direction: column;
  gap: 12px;
  position: relative;
}

/* Vertical line connecting siblings */
.tree-children::before {
  content: '';
  position: absolute;
  right: -12px;
  top: 50%;
  bottom: 50%;
  width: 2px;
  background: #bbb;
}

.tree-children > .tree-node-wrap:first-child ~ .tree-node-wrap ~ * {
  /* More than 2 children */
}

.tree-children-bracket {
  position: relative;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.tree-children-bracket::after {
  content: '';
  position: absolute;
  right: -12px;
  top: 0;
  bottom: 0;
  width: 2px;
  background: #bbb;
}

.tree-node-actions {
  display: flex;
  gap: 4px;
  margin-top: 6px;
}

.tree-node-actions button {
  font-size: 0.65rem;
  padding: 2px 6px;
  border-radius: 4px;
  cursor: pointer;
  border: 1px solid;
}

.tree-node-actions .btn-add-child { background: #edf4fc; border-color: #3498db; color: #3498db; }
.tree-node-actions .btn-remove-node { background: #fdf2f2; border-color: #e74c3c; color: #e74c3c; }

@media (max-width: 768px) {
  .cause-tree { flex-direction: column; align-items: flex-start; }
  .tree-node-box { min-width: 180px; max-width: none; width: 100%; }
  .tree-connector-line { width: 2px; height: 20px; }
  .tree-connector { padding: 4px 0; flex-direction: column; }
  .tree-children { flex-direction: column; }
  .tree-children-bracket::after { display: none; }
}

/* ── 5 Pourquoi ── */
.pourquoi-chain { display: flex; flex-direction: column; gap: 12px; }
.pourquoi-item { padding: 14px; border-radius: 8px; border-left: 4px solid; }
.pourquoi-item:nth-child(1) { border-color: #e74c3c; background: #fdf2f2; }
.pourquoi-item:nth-child(2) { border-color: #e67e22; background: #fef5ec; }
.pourquoi-item:nth-child(3) { border-color: #f1c40f; background: #fefcec; }
.pourquoi-item:nth-child(4) { border-color: #2ecc71; background: #edfcf2; }
.pourquoi-item:nth-child(5) { border-color: #3498db; background: #edf4fc; }
.pourquoi-item .level { font-weight: 700; font-size: 0.8rem; margin-bottom: 4px; }

/* ── Ishikawa ── */
.ishikawa-grid { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 12px; }
.ishikawa-cat { padding: 12px; border-radius: 8px; background: #f8f9fa; }
.ishikawa-cat h4 { font-size: 0.85rem; color: var(--primary); margin-bottom: 8px; padding-bottom: 4px; border-bottom: 2px solid var(--primary-light); }

/* ── Actions table ── */
.actions-table { width: 100%; border-collapse: collapse; font-size: 0.85rem; }
.actions-table th { background: var(--primary); color: white; padding: 10px; text-align: left; font-size: 0.8rem; }
.actions-table td { padding: 8px; border-bottom: 1px solid var(--border); vertical-align: top; }
.actions-table .type-c { color: var(--danger); font-weight: 700; }
.actions-table .type-p { color: var(--primary); font-weight: 700; }

/* Action cells: same height, word wrap */
.action-cell {
  min-height: 60px;
  word-break: break-word;
  white-space: pre-wrap;
  padding: 8px;
  font-size: 0.85rem;
  resize: vertical;
}

/* Priority score badges */
.priority-score { display: inline-block; width: 24px; height: 24px; border-radius: 50%; font-size: 0.7rem; font-weight: 700; text-align: center; line-height: 24px; color: white; }
.priority-high { background: var(--danger); }
.priority-med { background: var(--warning); }
.priority-low { background: var(--success); }

/* ── Defaillance cards ── */
.defaillance-card {
  margin-bottom: 12px;
  padding: 14px;
  background: #fef5ec;
  border-left: 4px solid #e67e22;
  border-radius: 0 8px 8px 0;
}

/* ── Questions list ── */
.questions-list { list-style: none; padding: 0; }
.questions-list li { padding: 10px 14px; background: #fff8e1; border-left: 3px solid var(--warning); border-radius: 0 6px 6px 0; margin-bottom: 8px; font-size: 0.9rem; }

/* ── Synthese box ── */
.synthese-box { padding: 20px; background: linear-gradient(135deg, #e8f5e9 0%, #f1f8e9 100%); border-radius: 10px; border-left: 4px solid var(--success); }

/* ── Refine ── */
.refine-box { display: flex; gap: 10px; margin-top: 16px; }
.refine-box textarea { flex: 1; min-height: 50px; }

/* ── Page header ── */
.page-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 24px; }
.page-header h1 { font-size: 1.4rem; color: var(--primary); }

/* ── Tab navigation ── */
.tab-nav { display: flex; gap: 4px; background: #f0f2f5; border-radius: 8px; padding: 4px; margin-bottom: 20px; overflow-x: auto; }
.tab-nav button {
  flex: 1;
  padding: 10px 12px;
  border: none;
  border-radius: 6px;
  background: transparent;
  font-size: 0.8rem;
  font-weight: 600;
  cursor: pointer;
  color: var(--text-light);
  transition: all 0.2s;
  font-family: inherit;
  white-space: nowrap;
}
.tab-nav button.active { background: white; color: var(--primary); box-shadow: 0 1px 3px rgba(0,0,0,0.1); }
.tab-panel { display: none; }
.tab-panel.active { display: block; }

/* ── Spinner / Loading ── */
.spinner { display: inline-block; width: 20px; height: 20px; border: 3px solid rgba(255,255,255,0.3); border-radius: 50%; border-top-color: white; animation: spin 0.8s linear infinite; }
.spinner-dark { border-color: rgba(0,0,0,0.1); border-top-color: var(--primary); }
@keyframes spin { to { transform: rotate(360deg); } }

.loading-overlay { position: fixed; inset: 0; background: rgba(0,0,0,0.5); display: flex; justify-content: center; align-items: center; z-index: 1000; }
.loading-card { background: white; border-radius: 12px; padding: 40px; text-align: center; max-width: 400px; }
.loading-card .spinner-lg { width: 48px; height: 48px; border-width: 4px; margin: 0 auto 16px; }

/* ── Toast ── */
.toast { position: fixed; bottom: 24px; right: 24px; padding: 14px 24px; border-radius: 8px; color: white; font-weight: 600; font-size: 0.9rem; z-index: 2000; animation: slideIn 0.3s ease; box-shadow: 0 4px 12px rgba(0,0,0,0.2); }
.toast-success { background: var(--success); }
.toast-error { background: var(--danger); }
@keyframes slideIn { from { transform: translateY(20px); opacity: 0; } to { transform: translateY(0); opacity: 1; } }

/* ── Responsive ── */
@media (max-width: 1024px) {
  .form-row { grid-template-columns: 1fr; }
  .ishikawa-grid { grid-template-columns: 1fr 1fr; }
}

@media (max-width: 768px) {
  .sidebar { transform: translateX(-100%); transition: transform 0.3s ease; }
  .sidebar.open { transform: translateX(0); }
  .sidebar-overlay { display: none; position: fixed; inset: 0; background: rgba(0,0,0,0.5); z-index: 99; }
  .sidebar-overlay.open { display: block; }
  .main-content { margin-left: 0; }
  .mobile-header {
    display: flex; align-items: center; gap: 12px;
    padding: 12px 16px; background: var(--primary); color: white;
    margin: -24px -24px 24px;
  }
  .mobile-header .hamburger { background: none; border: none; color: white; font-size: 1.4rem; cursor: pointer; padding: 4px; }
  .mobile-header h2 { font-size: 1rem; font-weight: 700; letter-spacing: 1px; }
  .mobile-header .back-home-mobile { margin-left: auto; color: rgba(255,255,255,0.7); text-decoration: none; font-size: 0.75rem; }
  .ishikawa-grid { grid-template-columns: 1fr; }
  .tab-nav { flex-wrap: nowrap; }
  .tab-nav button { font-size: 0.7rem; padding: 8px 6px; }
  .phase-bar { flex-wrap: wrap; gap: 8px; }
  .phase-step { min-width: 80px; }
  .phase-line { max-width: 30px; }
}

@media (min-width: 769px) {
  .mobile-header { display: none; }
  .sidebar-overlay { display: none; }
}
