:root {
  --color-bg: #f5f6f8;
  --color-sidebar: #ffffff;
  --color-surface: #ffffff;
  --color-surface-alt: #f8fafc;
  --color-border: #e2e5ea;
  --color-text: #1f2430;
  --color-muted: #6b7280;
  --color-primary: #2563eb;
  --color-primary-hover: #1d4ed8;
  --color-primary-soft: #eff4ff;
  --color-success-bg: #ecfdf5;
  --color-success-text: #065f46;
  --color-warning-bg: #fffbeb;
  --color-warning-text: #92400e;
  --color-error-bg: #fef2f2;
  --color-error-text: #991b1b;
  --radius: 8px;
  --sidebar-width: 220px;
  --sidebar-width-collapsed: 64px;
}

[data-theme="dark"] {
  --color-bg: #0b0f19;
  --color-sidebar: #0f1522;
  --color-surface: #131a29;
  --color-surface-alt: #0f1522;
  --color-border: #232b3d;
  --color-text: #e5e9f0;
  --color-muted: #8b95a7;
  --color-primary: #3b82f6;
  --color-primary-hover: #60a5fa;
  --color-primary-soft: #1a2540;
  --color-success-bg: #062f24;
  --color-success-text: #34d399;
  --color-warning-bg: #2e2408;
  --color-warning-text: #fbbf24;
  --color-error-bg: #2c1212;
  --color-error-text: #f87171;
}

* { box-sizing: border-box; }

body.resizing { cursor: col-resize; user-select: none; }
body.resizing * { user-select: none; }

body {
  margin: 0;
  font-family: -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--color-bg);
  color: var(--color-text);
}

/* --- Coquille de l'app : sidebar + topbar --- */

.app-shell { display: flex; min-height: 100vh; }

.sidebar {
  width: var(--sidebar-width);
  flex-shrink: 0;
  background: var(--color-sidebar);
  border-right: 1px solid var(--color-border);
  display: flex;
  flex-direction: column;
  transition: width 0.15s ease;
}
.sidebar.collapsed { width: var(--sidebar-width-collapsed); }

.sidebar-logo {
  display: flex; align-items: center; gap: 10px;
  padding: 20px 18px; font-weight: 700; font-size: 16px;
  white-space: nowrap; overflow: hidden;
}
.sidebar-logo .logo-mark {
  width: 28px; height: 28px; border-radius: 8px; background: var(--color-primary);
  display: flex; align-items: center; justify-content: center; color: #fff; flex-shrink: 0; font-size: 14px;
}
.sidebar.collapsed .sidebar-logo span.logo-text { display: none; }

.sidebar-nav { list-style: none; margin: 0; padding: 8px; flex: 1; overflow-y: auto; }
.sidebar-nav-section { margin-bottom: 4px; }
.sidebar-nav-section-label {
  margin: 12px 12px 4px;
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--color-muted);
  white-space: nowrap;
  overflow: hidden;
}
.sidebar-nav-section:first-child .sidebar-nav-section-label { margin-top: 4px; }
.sidebar.collapsed .sidebar-nav-section-label { display: none; }
.sidebar-nav-item {
  display: flex; align-items: center; gap: 12px;
  padding: 10px 12px; border-radius: var(--radius);
  color: var(--color-muted); text-decoration: none; font-size: 14px; font-weight: 500;
  cursor: pointer; white-space: nowrap; overflow: hidden; margin-bottom: 2px;
}
.sidebar-nav-item:hover { background: var(--color-surface-alt); color: var(--color-text); }
.sidebar-nav-item.active { background: var(--color-primary-soft); color: var(--color-primary); }
.sidebar-nav-item .icon { flex-shrink: 0; width: 18px; height: 18px; display: flex; align-items: center; justify-content: center; }
.sidebar.collapsed .sidebar-nav-item span.label { display: none; }
.sidebar-nav-item .badge {
  margin-left: auto; background: var(--color-primary); color: #fff; border-radius: 999px;
  font-size: 11px; padding: 1px 7px; font-weight: 700;
}
.sidebar.collapsed .sidebar-nav-item .badge { display: none; }

.sidebar-collapse-btn {
  margin: 8px; background: transparent; color: var(--color-muted); border: 1px solid var(--color-border);
  padding: 8px; font-size: 13px; font-weight: 500;
}
.sidebar-collapse-btn:hover { background: var(--color-surface-alt); }

.sidebar-resize-handle {
  width: 5px;
  flex-shrink: 0;
  cursor: col-resize;
  background: transparent;
}
.sidebar-resize-handle:hover,
.sidebar-resize-handle.active { background: var(--color-primary); }
.sidebar.collapsed + .sidebar-resize-handle { display: none; }

.main-area { flex: 1; display: flex; flex-direction: column; min-width: 0; }

.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 16px 28px; border-bottom: 1px solid var(--color-border); background: var(--color-surface);
}
.topbar-title {
  font-size: 19px; font-weight: 700; margin: 0; padding-left: 14px;
  border-left: 4px solid var(--color-primary);
}
.topbar-actions { display: flex; align-items: center; gap: 12px; }

.icon-btn {
  background: transparent; border: 1px solid var(--color-border); color: var(--color-text);
  width: 38px; height: 38px; border-radius: var(--radius); margin: 0;
  display: flex; align-items: center; justify-content: center; padding: 0;
}
.icon-btn:hover { background: var(--color-surface-alt); }
.icon-btn svg, .sidebar-nav-item .icon svg, .sidebar-collapse-btn svg {
  width: 18px; height: 18px; display: block;
}

.user-chip { display: flex; align-items: center; gap: 8px; padding: 6px 12px; border: 1px solid var(--color-border); border-radius: 999px; font-size: 13px; font-weight: 600; }
.user-avatar {
  width: 26px; height: 26px; border-radius: 999px; background: var(--color-primary);
  color: #fff; display: flex; align-items: center; justify-content: center; font-size: 12px; font-weight: 700; flex-shrink: 0;
}

.page-content { flex: 1; padding: 28px; }
.page-layout { display: flex; align-items: flex-start; }
.page-main { flex: 1; min-width: 280px; }
.side-panel { width: 300px; flex-shrink: 0; min-width: 220px; max-width: 560px; }

.panel-resize-handle {
  width: 24px;
  flex-shrink: 0;
  align-self: stretch;
  cursor: col-resize;
  display: flex;
  justify-content: center;
}
.panel-resize-handle::before {
  content: '';
  width: 4px;
  border-radius: 2px;
  background: var(--color-border);
}
.panel-resize-handle:hover::before,
.panel-resize-handle.active::before { background: var(--color-primary); }

@media (max-width: 900px) {
  .page-layout { flex-direction: column; }
  .side-panel { width: 100% !important; max-width: none; }
  .panel-resize-handle { display: none; }
}

/* --- Composants --- */

.card {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  padding: 24px;
  margin-bottom: 24px;
}
.card-title { font-size: 14px; font-weight: 700; margin: 0 0 12px; padding-left: 10px; border-left: 3px solid var(--color-primary); }

label { display: block; font-weight: 600; margin: 16px 0 4px; font-size: 14px; }
label:first-child { margin-top: 0; }

input[type="text"], input[type="file"], select, textarea {
  width: 100%;
  padding: 8px 10px;
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  font-size: 14px;
  font-family: inherit;
  background: var(--color-surface);
  color: var(--color-text);
}

.help-text { font-size: 12px; color: var(--color-muted); margin-top: 4px; }

.radio-group { display: flex; gap: 16px; }
.radio-group label { font-weight: 400; display: flex; align-items: center; gap: 6px; margin: 0; }

button {
  background: var(--color-primary);
  color: #fff;
  border: none;
  border-radius: var(--radius);
  padding: 10px 18px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  margin-top: 20px;
}
button:hover { background: var(--color-primary-hover); }
button.secondary { background: transparent; color: var(--color-primary); border: 1px solid var(--color-primary); }
button.small { padding: 4px 10px; font-size: 12px; margin-top: 0; }

.feedback { padding: 12px 16px; border-radius: var(--radius); margin-top: 16px; font-size: 14px; }
.feedback.success { background: var(--color-success-bg); color: var(--color-success-text); }
.feedback.warning { background: var(--color-warning-bg); color: var(--color-warning-text); }
.feedback.error { background: var(--color-error-bg); color: var(--color-error-text); }

.hidden { display: none !important; }

table { width: 100%; border-collapse: collapse; font-size: 13px; }
th, td { text-align: left; padding: 10px 12px; border-bottom: 1px solid var(--color-border); vertical-align: top; }
th {
  color: var(--color-muted);
  font-weight: 700;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  background: var(--color-surface-alt);
  border-bottom: 2px solid var(--color-border);
  white-space: nowrap;
}
tbody tr:hover { background: var(--color-surface-alt); }
tbody tr:last-child td { border-bottom: none; }
tbody tr.row-error { background: var(--color-error-bg); box-shadow: inset 3px 0 0 var(--color-error-text); }
tbody tr.row-error:hover { background: var(--color-error-bg); }

#manual-table tbody td { vertical-align: middle; }
.row-actions-cell { white-space: nowrap; }
.row-actions-cell button { margin-top: 0; }
.row-actions-cell button + button { margin-left: 6px; }

table code {
  font-family: "SFMono-Regular", Consolas, monospace;
  font-size: 12px;
  background: var(--color-surface-alt);
  padding: 2px 6px;
  border-radius: 4px;
}

.pill {
  display: inline-block;
  padding: 2px 10px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 700;
  white-space: nowrap;
}
.pill-blue { background: var(--color-primary-soft); color: var(--color-primary); }
.pill-neutral { background: var(--color-surface-alt); color: var(--color-muted); border: 1px solid var(--color-border); }
.pill-warning { background: var(--color-warning-bg); color: var(--color-warning-text); }
.pill-error { background: var(--color-error-bg); color: var(--color-error-text); }

.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--color-border); margin-bottom: 16px; }
.tabs button {
  background: transparent; color: var(--color-muted); border: none; border-bottom: 2px solid transparent;
  border-radius: 0; margin: 0; padding: 10px 14px; font-weight: 600;
}
.tabs button.active { color: var(--color-primary); border-bottom-color: var(--color-primary); }

#history-pagination button.active { background: var(--color-primary); color: #fff; border-color: var(--color-primary); }

pre.sql-block {
  background: #0f172a; color: #e2e8f0; padding: 12px; border-radius: var(--radius);
  overflow-x: auto; font-size: 12px; white-space: pre-wrap; word-break: break-all;
}

.empty-state { color: var(--color-muted); font-style: italic; padding: 24px 0; text-align: center; }

.stat-box { background: var(--color-surface-alt); border-radius: var(--radius); padding: 16px; }
.stat-box .stat-label { font-size: 11px; text-transform: uppercase; letter-spacing: 0.04em; color: var(--color-muted); font-weight: 700; }
.stat-box .stat-value { font-size: 30px; font-weight: 700; margin-top: 6px; }

.stat-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 12px; margin-bottom: 20px; }

.divider-or { text-align: center; color: var(--color-muted); font-size: 12px; margin: 16px 0; }

.type-row { display: flex; align-items: center; gap: 20px; }
.type-row-label { margin: 0; }

/* --- Picker "Pays à exclure" (toggle In/Not in + autocomplete pays) --- */

.pays-widget { display: flex; flex-direction: column; gap: 6px; min-width: 240px; }

.pays-row { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }

.pays-toggle { display: flex; gap: 3px; flex-shrink: 0; }
.toggle-btn {
  flex: none;
  margin: 0;
  padding: 3px 7px;
  font-size: 10px;
  font-weight: 700;
  border-radius: var(--radius);
  border: 1px solid var(--color-border);
  background: var(--color-surface-alt);
  color: var(--color-muted);
  cursor: pointer;
}
.toggle-btn:hover { background: var(--color-surface); }
.toggle-btn.toggle-in.active { background: var(--color-success-text); border-color: var(--color-success-text); color: #fff; }
.toggle-btn.toggle-notin.active { background: var(--color-error-text); border-color: var(--color-error-text); color: #fff; }

.pays-chips { display: flex; flex-wrap: wrap; gap: 4px; }
.chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  padding: 3px 6px 3px 10px;
  font-size: 12px;
}
.chip-remove {
  background: transparent;
  border: none;
  color: var(--color-muted);
  cursor: pointer;
  padding: 0;
  margin: 0;
  font-size: 11px;
  line-height: 1;
}
.chip-remove:hover { color: var(--color-error-text); }

.chip-all { background: var(--color-warning-bg); color: var(--color-warning-text); border-color: transparent; font-weight: 600; }
.chip-all .chip-remove { color: var(--color-warning-text); }

.all-mode .pays-toggle,
.all-mode .pays-autocomplete { display: none; }

.pays-autocomplete { position: relative; flex: 1; min-width: 0; }
.pays-search { font-size: 12px; padding: 6px 8px; }
.pays-suggestions {
  position: absolute;
  z-index: 20;
  top: 100%;
  left: 0;
  right: 0;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  margin: 2px 0 0;
  padding: 4px;
  list-style: none;
  max-height: 180px;
  overflow-y: auto;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.12);
}
.pays-suggestions li { padding: 6px 8px; font-size: 12px; border-radius: 4px; cursor: pointer; }
.pays-suggestions li:hover,
.pays-suggestions li.highlighted { background: var(--color-primary-soft); color: var(--color-primary); }
.pays-suggestions li.empty { color: var(--color-muted); cursor: default; }
.pays-suggestions li.empty:hover { background: transparent; }
.pays-suggestions li.suggestion-all { font-weight: 700; border-bottom: 1px solid var(--color-border); border-radius: 0; margin-bottom: 2px; }
.pays-suggestions li.suggestion-preset { color: var(--color-primary); border-bottom: 1px solid var(--color-border); border-radius: 0; }
.pays-suggestions li.suggestion-preset:last-of-type { margin-bottom: 2px; }

/* --- Demandes PIQA : liste + detail --- */

.piqa-split { display: flex; gap: 24px; align-items: flex-start; margin-top: 16px; }
.piqa-list-panel { width: 320px; flex-shrink: 0; max-height: 600px; overflow-y: auto; }
.piqa-detail-panel { flex: 1; min-width: 0; }

.piqa-list-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 12px; }

.piqa-list-item { padding: 10px 12px; border-radius: var(--radius); cursor: pointer; margin-bottom: 4px; }
.piqa-list-item:hover { background: var(--color-surface-alt); }
.piqa-list-item.active { background: var(--color-primary-soft); }
.piqa-list-item-title { font-size: 13px; font-weight: 600; }
.piqa-list-item-meta { font-size: 12px; color: var(--color-muted); margin-top: 2px; }

.piqa-description {
  font-size: 14px;
  line-height: 1.6;
  white-space: pre-wrap;
  background: var(--color-surface-alt);
  border-radius: var(--radius);
  padding: 14px 16px;
  margin-top: 4px;
}
.piqa-description.collapsed {
  max-height: 110px;
  overflow: hidden;
  position: relative;
}
.piqa-description.collapsed::after {
  content: '';
  position: absolute;
  bottom: 0; left: 0; right: 0;
  height: 48px;
  background: linear-gradient(to bottom, transparent, var(--color-surface-alt));
}
.piqa-description-toggle { margin-top: 8px; }

@media (max-width: 900px) {
  .piqa-split { flex-direction: column; }
  .piqa-list-panel { width: 100%; max-height: 300px; }
}
