:root {
  --bg: #f7f8fb;
  --card: #ffffff;
  --border: #e4e7ee;
  --text: #1f2937;
  --muted: #6b7280;
  --primary: #2563eb;
  --primary-h: #1d4ed8;
  --ok: #16a34a;
  --warn: #d97706;
  --err: #dc2626;
}
* { box-sizing: border-box; }
body {
  margin: 0; font-family: system-ui, -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
  background: var(--bg); color: var(--text); font-size: 14px; line-height: 1.45;
}
body.plain { background: #eef1f7; min-height: 100vh; display: flex; align-items: center; }

.topnav {
  display: flex; align-items: center; gap: 1.5em;
  background: #1f2937; color: #fff; padding: 0.6em 1.2em;
  border-bottom: 3px solid var(--primary);
}
.topnav .brand { color: #fff; text-decoration: none; font-weight: 600; font-size: 1.1em; }
.topnav .brand span { color: #9ca3af; font-weight: 400; }
.topnav .links { display: flex; gap: 1em; flex: 1; }
.topnav .links a { color: #cbd5e1; text-decoration: none; padding: 0.3em 0.6em; border-radius: 4px; }
.topnav .links a:hover { background: rgba(255,255,255,0.08); color: #fff; }
.topnav .user { color: #cbd5e1; font-size: 0.9em; }
.topnav .user .link { background: none; border: 1px solid #4b5563; color: #cbd5e1; cursor: pointer; padding: 0.2em 0.6em; border-radius: 4px; margin-left: 0.6em; }

.container { max-width: 1280px; margin: 1.5em auto; padding: 0 1.2em; }
.container.narrow { max-width: 420px; margin: auto; }

h1 { font-size: 1.6em; margin: 0 0 0.8em; }
h2 { font-size: 1.15em; margin: 1.2em 0 0.5em; }
h3 { font-size: 1em; margin: 1em 0 0.4em; }

a { color: var(--primary); }

.card {
  background: var(--card); border: 1px solid var(--border); border-radius: 8px;
  padding: 1.2em; margin-bottom: 1.2em;
}
.card-warn { border-color: var(--warn); background: #fffaf2; }

details > summary { cursor: pointer; list-style: none; }
details > summary::-webkit-details-marker { display: none; }
details > summary h2 { display: inline; margin: 0; }
details > summary::before { content: "▸ "; color: var(--muted); }
details[open] > summary::before { content: "▾ "; }

label { display: block; margin: 0.6em 0; font-size: 0.92em; }
input, select, textarea {
  width: 100%; padding: 0.5em 0.7em; border: 1px solid var(--border); border-radius: 5px;
  font: inherit; background: #fff; margin-top: 0.2em;
}
textarea { font-family: ui-monospace, Menlo, Consolas, monospace; font-size: 0.9em; }
input:focus, select:focus, textarea:focus { border-color: var(--primary); outline: none; box-shadow: 0 0 0 3px rgba(37,99,235,0.15); }
label.checkbox { display: flex; align-items: center; gap: 0.5em; }
label.checkbox input { width: auto; }

button, .btn {
  display: inline-block; padding: 0.5em 1em; border: 1px solid var(--border);
  border-radius: 5px; background: #fff; cursor: pointer; font: inherit;
}
button.primary { background: var(--primary); color: #fff; border-color: var(--primary); font-weight: 500; }
button.primary:hover { background: var(--primary-h); }
button.secondary { background: #f3f4f6; }
button.secondary:hover { background: #e5e7eb; }
.btn-link { background: none; border: none; color: var(--primary); cursor: pointer; padding: 0; font: inherit; text-decoration: underline; margin-right: 0.6em; }
.btn-del { color: var(--err); }
.btn-ok { color: var(--ok); }

table { width: 100%; border-collapse: collapse; margin: 0.4em 0; }
th, td { text-align: left; padding: 0.45em 0.6em; border-bottom: 1px solid var(--border); vertical-align: top; }
th { background: #f3f4f6; font-size: 0.85em; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: 0.03em; }
.table-zebra tr:nth-child(even) { background: #fafbfc; }

.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 0.8em; margin: 0.5em 0 1.5em; }
.kpi { background: var(--card); border: 1px solid var(--border); border-radius: 8px; padding: 1em; display: flex; flex-direction: column; gap: 0.2em; }
.kpi .k-val { font-size: 1.6em; font-weight: 600; }
.kpi .k-lbl { color: var(--muted); font-size: 0.85em; }
.kpi-warn .k-val { color: var(--warn); }
.kpi-err .k-val { color: var(--err); }

.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 1em; }
@media (max-width: 720px) { .grid-2 { grid-template-columns: 1fr; } }

.chip {
  display: inline-block; padding: 0.15em 0.55em; border-radius: 10px;
  background: #e5e7eb; color: #374151; font-size: 0.82em; white-space: nowrap;
}
.chip-sub { background: #fde68a; color: #78350f; }
.chip-delete { background: #fee2e2; color: #991b1b; }
.chip-reply { background: #d1fae5; color: #065f46; }
.chip-forward { background: #dbeafe; color: #1e40af; }
.chip-reply_and_forward { background: #ede9fe; color: #5b21b6; }
.chip-flag_human { background: #fef3c7; color: #92400e; }
.chip-done { background: #d1fae5; color: #065f46; }
.chip-dry_run { background: #e0f2fe; color: #075985; }
.chip-pending { background: #fef9c3; color: #854d0e; }
.chip-error { background: #fee2e2; color: #991b1b; }
.chip-INFO { background: #e0f2fe; color: #075985; }
.chip-DEBUG { background: #f3f4f6; color: #4b5563; }
.chip-WARN { background: #fef3c7; color: #92400e; }
.chip-ERROR { background: #fee2e2; color: #991b1b; }

.banner { padding: 0.8em 1em; border-radius: 6px; margin-bottom: 1em; }
.banner-warn { background: #fffaf2; border: 1px solid var(--warn); color: #78350f; }

.flash { padding: 0.7em 1em; border-radius: 6px; margin-bottom: 1em; }
.flash-ok { background: #d1fae5; color: #065f46; border: 1px solid #6ee7b7; }
.flash-error { background: #fee2e2; color: #991b1b; border: 1px solid #fca5a5; }

.notice { background: #e0f2fe; border: 1px solid #7dd3fc; color: #075985; padding: 0.7em 1em; border-radius: 6px; margin-bottom: 1em; }

.muted { color: var(--muted); }
.small { font-size: 0.88em; }
.nowrap { white-space: nowrap; }
.ok { color: var(--ok); font-weight: 500; }
.err { color: var(--err); font-weight: 500; }

.tabs { display: flex; gap: 0; border-bottom: 1px solid var(--border); margin-bottom: 1em; }
.tabs a { padding: 0.6em 1em; color: var(--muted); text-decoration: none; border-bottom: 2px solid transparent; }
.tabs a.active { color: var(--text); border-bottom-color: var(--primary); }

.filters { display: flex; gap: 0.6em; align-items: center; margin-bottom: 1em; flex-wrap: wrap; }
.filters select, .filters input { width: auto; min-width: 140px; }

.test-result { margin-top: 0.6em; padding: 0.5em; border-radius: 5px; min-height: 1.2em; }
.test-result.ok { background: #d1fae5; color: #065f46; }
.test-result.err { background: #fee2e2; color: #991b1b; }

.mail-body { background: #f9fafb; padding: 1em; border-radius: 5px; white-space: pre-wrap; word-break: break-word; max-height: 50vh; overflow-y: auto; font-size: 0.9em; font-family: ui-monospace, Menlo, Consolas, monospace; }
.actions-row { display: flex; gap: 0.6em; margin-top: 1em; }
.back { display: inline-block; margin-bottom: 1em; color: var(--muted); text-decoration: none; }

.foot { text-align: center; color: var(--muted); padding: 2em 1em; font-size: 0.85em; }

.log-table td:nth-child(4) { max-width: 400px; }
.log-table td:nth-child(5) { max-width: 300px; font-family: ui-monospace, Menlo, Consolas, monospace; font-size: 0.78em; overflow-wrap: anywhere; }

/* ========== Aide & tooltips ========== */
.info-box {
  background: #e0f2fe; border: 1px solid #7dd3fc; color: #075985;
  padding: 0.9em 1.1em; border-radius: 6px; margin-bottom: 1em;
  display: flex; gap: 0.8em; align-items: flex-start;
}
.info-box::before {
  content: "ⓘ"; font-size: 1.4em; line-height: 1; color: #0284c7; flex-shrink: 0;
}
.info-box .info-content { flex: 1; }
.info-box strong { display: block; margin-bottom: 0.2em; }
.info-box ul { margin: 0.3em 0 0 1.2em; padding: 0; }
.info-box li { margin: 0.15em 0; }

.help {
  display: block; font-size: 0.82em; color: var(--muted);
  margin-top: 0.2em; line-height: 1.35;
}
label .help { margin-top: 0.3em; margin-bottom: 0.4em; font-weight: normal; }

.help-tip {
  display: inline-flex; align-items: center; justify-content: center;
  width: 16px; height: 16px; border-radius: 50%;
  background: #cbd5e1; color: #1f2937;
  font-size: 0.72em; font-weight: bold;
  margin-left: 0.3em; cursor: help; position: relative;
  vertical-align: middle;
}
.help-tip:hover { background: var(--primary); color: #fff; }
.help-tip::after {
  content: attr(data-tip);
  position: absolute; bottom: calc(100% + 6px); left: 50%; transform: translateX(-50%);
  background: #1f2937; color: #fff; padding: 0.5em 0.8em; border-radius: 5px;
  font-size: 0.85em; font-weight: normal; white-space: normal; width: max-content; max-width: 320px;
  line-height: 1.4; text-align: left;
  opacity: 0; pointer-events: none; transition: opacity 0.15s;
  z-index: 100;
}
.help-tip::before {
  content: ""; position: absolute; bottom: calc(100% - 1px); left: 50%; transform: translateX(-50%);
  border: 6px solid transparent; border-top-color: #1f2937;
  opacity: 0; transition: opacity 0.15s;
}
.help-tip:hover::after, .help-tip:hover::before { opacity: 1; }

.guide-card {
  background: #fff; border: 1px solid var(--border); border-left: 4px solid var(--primary);
  border-radius: 6px; padding: 1em 1.2em; margin-bottom: 1.2em;
}
.guide-card h2, .guide-card h3 { margin-top: 0; }
.guide-card .steps { padding-left: 1.4em; }
.guide-card .steps li { margin: 0.4em 0; }

.warn-box {
  background: #fef3c7; border: 1px solid #f59e0b; color: #78350f;
  padding: 0.8em 1em; border-radius: 6px; margin-bottom: 1em;
  display: flex; gap: 0.6em; align-items: flex-start;
}
.warn-box::before { content: "⚠"; font-size: 1.2em; flex-shrink: 0; }

/* ========== Section Mode TEST/PROD ========== */
.mode-section {
  border-radius: 10px; padding: 1.2em 1.4em; margin-bottom: 1.5em;
  border: 2px solid;
}
.mode-section h2 { margin-top: 0; }
.mode-section.mode-test { background: #fef3c7; border-color: #f59e0b; }
.mode-section.mode-prod { background: #d1fae5; border-color: #16a34a; }

.mode-current { margin-bottom: 1em; }
.mode-current p { margin: 0.5em 0 0; line-height: 1.55; }

.mode-badge {
  display: inline-block; padding: 0.3em 0.7em; border-radius: 6px;
  font-weight: 600; font-size: 0.9em;
}
.mode-badge-test { background: #f59e0b; color: #fff; }
.mode-badge-prod { background: #16a34a; color: #fff; }

.mode-toggle-form { background: #fff; padding: 1em; border-radius: 8px; }

.mode-option {
  display: flex; gap: 0.8em; align-items: flex-start;
  padding: 1em; margin: 0.6em 0;
  border: 2px solid var(--border); border-radius: 8px;
  cursor: pointer;
  background: #fff;
  transition: all 0.15s;
}
.mode-option:hover { border-color: var(--primary); background: #f8fafc; }
.mode-option input[type=radio] { width: auto; margin-top: 0.3em; flex-shrink: 0; }
.mode-option-content { flex: 1; }
.mode-option-title { font-weight: 600; font-size: 1.05em; margin-bottom: 0.3em; }
.mode-option-desc { font-size: 0.92em; line-height: 1.5; color: #374151; }
.mode-option-desc ul { margin: 0.4em 0 0.6em 1.4em; padding: 0; }
.mode-option-desc li { margin: 0.15em 0; }
.mode-option-desc strong { color: var(--text); }
.mode-option-active { border-color: var(--primary); background: #eff6ff; }
.mode-option-active .mode-option-title::after {
  content: "  (actuel)"; color: var(--primary); font-size: 0.85em;
}

/* ========== Chatbot ========== */
.chat-layout {
  display: grid; grid-template-columns: 260px 1fr; gap: 1em;
  height: calc(100vh - 220px); min-height: 500px;
}
@media (max-width: 720px) { .chat-layout { grid-template-columns: 1fr; height: auto; } }
.chat-sidebar { display: flex; flex-direction: column; }
.chat-sessions { flex: 1; overflow-y: auto; }
.chat-session-item {
  display: block; padding: 0.5em 0.7em; border-radius: 5px;
  background: #fff; border: 1px solid var(--border); margin-bottom: 0.4em;
  text-decoration: none; color: var(--text);
}
.chat-session-item:hover { background: #f3f4f6; }
.chat-session-item.active { background: #dbeafe; border-color: var(--primary); }
.chat-session-title { font-size: 0.9em; font-weight: 500; }
.chat-session-meta { font-size: 0.78em; color: var(--muted); margin-top: 0.15em; }

.chat-main {
  display: flex; flex-direction: column;
  padding: 0; overflow: hidden;
}
.chat-messages {
  flex: 1; overflow-y: auto; padding: 1em 1.2em;
}
.chat-bubble {
  margin-bottom: 0.8em; max-width: 80%; padding: 0.6em 0.9em;
  border-radius: 12px; line-height: 1.5;
}
.chat-bubble-user {
  background: var(--primary); color: #fff;
  margin-left: auto; border-bottom-right-radius: 3px;
}
.chat-bubble-assistant {
  background: #f3f4f6; color: var(--text);
  margin-right: auto; border-bottom-left-radius: 3px;
}
.chat-bubble-content { white-space: pre-wrap; word-break: break-word; }
.chat-bubble-meta { font-size: 0.72em; opacity: 0.7; margin-top: 0.3em; }
.chat-empty {
  text-align: left; color: var(--muted); padding: 2em;
  background: #fafbfc; border-radius: 8px;
}
.chat-empty ul { padding-left: 1.2em; }
.chat-empty li { margin: 0.25em 0; font-style: italic; }
.chat-input {
  display: flex; gap: 0.5em; padding: 0.8em; border-top: 1px solid var(--border);
  background: #fafbfc;
}
.chat-input textarea {
  flex: 1; margin: 0; resize: vertical; min-height: 40px;
}
.chat-input button { align-self: stretch; }
.chat-status { padding: 0 1em 0.5em; min-height: 1em; }

/* ========== Widget chatbot flottant ========== */
.chat-widget {
  position: fixed; bottom: 20px; right: 20px;
  z-index: 100;
  font-family: inherit;
}

.chat-widget-toggle {
  width: 56px; height: 56px; border-radius: 50%;
  background: var(--primary); color: #fff;
  border: none; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  font-size: 1.5em;
  box-shadow: 0 4px 16px rgba(37,99,235,0.4);
  transition: transform 0.15s, box-shadow 0.15s;
  padding: 0;
}
.chat-widget-toggle:hover {
  transform: scale(1.05);
  box-shadow: 0 6px 20px rgba(37,99,235,0.5);
}
.chat-widget-icon-close { display: none; }
.chat-widget.open .chat-widget-icon-open { display: none; }
.chat-widget.open .chat-widget-icon-close { display: inline; font-size: 1.6em; line-height: 1; }

.chat-widget-panel {
  position: absolute;
  bottom: 70px; right: 0;
  width: 380px;
  height: 540px;
  max-height: calc(100vh - 100px);
  background: #fff;
  border-radius: 12px;
  box-shadow: 0 8px 32px rgba(0,0,0,0.2);
  display: flex; flex-direction: column;
  overflow: hidden;
  animation: chatSlideUp 0.2s ease;
}
/* Le `hidden` HTML doit l'emporter sur le display:flex ci-dessus */
.chat-widget-panel[hidden] { display: none !important; }
@keyframes chatSlideUp {
  from { opacity: 0; transform: translateY(10px); }
  to { opacity: 1; transform: translateY(0); }
}
@media (max-width: 480px) {
  .chat-widget-panel {
    width: calc(100vw - 30px);
    height: calc(100vh - 100px);
    right: -10px;
  }
}

.chat-widget-head {
  background: var(--primary); color: #fff;
  padding: 0.7em 1em;
  display: flex; align-items: center; justify-content: space-between;
}
.chat-widget-head strong { font-size: 1em; }
.chat-widget-subtitle { font-size: 0.78em; opacity: 0.85; margin-top: 0.1em; }
.chat-widget-actions { display: flex; gap: 0.3em; }
.chat-widget-actions button, .chat-widget-actions a {
  background: rgba(255,255,255,0.15); border: none; color: #fff;
  width: 28px; height: 28px; border-radius: 5px; cursor: pointer;
  font-size: 1.1em; line-height: 1;
  display: inline-flex; align-items: center; justify-content: center;
  text-decoration: none;
  padding: 0;
}
.chat-widget-actions button:hover, .chat-widget-actions a:hover {
  background: rgba(255,255,255,0.3);
}

.chat-widget-body {
  flex: 1; overflow-y: auto;
  padding: 0.8em 0.9em;
  background: #fafbfc;
}
.chat-widget-empty {
  color: var(--muted); font-size: 0.9em;
  padding: 1em 0.5em;
  line-height: 1.5;
}
.chat-widget-empty p { margin: 0.4em 0; }
.chat-widget-empty ul { padding-left: 1.2em; margin: 0.3em 0; }
.chat-widget-empty li { font-style: italic; margin: 0.2em 0; }

.cw-bubble {
  margin-bottom: 0.6em;
  max-width: 85%;
  padding: 0.55em 0.8em;
  border-radius: 12px;
  line-height: 1.45;
  font-size: 0.92em;
}
.cw-bubble-user {
  background: var(--primary); color: #fff;
  margin-left: auto;
  border-bottom-right-radius: 3px;
}
.cw-bubble-assistant {
  background: #e5e7eb; color: var(--text);
  margin-right: auto;
  border-bottom-left-radius: 3px;
}
.cw-bubble-content { word-break: break-word; }
.cw-bubble-user .cw-bubble-content { white-space: pre-wrap; }
.cw-bubble-time { font-size: 0.7em; opacity: 0.6; margin-top: 0.25em; }

/* Markdown rendu dans les bulles assistant */
.cw-bubble-assistant .cw-bubble-content p { margin: 0.35em 0; }
.cw-bubble-assistant .cw-bubble-content p:first-child { margin-top: 0; }
.cw-bubble-assistant .cw-bubble-content p:last-child { margin-bottom: 0; }
.cw-bubble-assistant .cw-h {
  margin: 0.7em 0 0.3em; font-weight: 600; color: #111827;
}
.cw-bubble-assistant h3.cw-h { font-size: 1.05em; }
.cw-bubble-assistant h4.cw-h { font-size: 1em; }
.cw-bubble-assistant h5.cw-h { font-size: 0.95em; color: #1d4ed8; }
.cw-bubble-assistant h6.cw-h { font-size: 0.9em; color: #374151; }
.cw-bubble-assistant .cw-h:first-child { margin-top: 0; }
.cw-bubble-assistant .cw-list { margin: 0.3em 0; padding-left: 1.4em; }
.cw-bubble-assistant .cw-list li { margin: 0.15em 0; }
.cw-bubble-assistant strong { font-weight: 600; color: #111827; }
.cw-bubble-assistant em { font-style: italic; }
.cw-bubble-assistant code {
  background: rgba(0,0,0,0.08); padding: 1px 5px; border-radius: 3px;
  font-family: ui-monospace, Menlo, Consolas, monospace; font-size: 0.88em;
}
.cw-bubble-assistant .cw-code {
  background: #1f2937; color: #f9fafb;
  padding: 0.6em 0.8em; border-radius: 6px;
  margin: 0.4em 0; overflow-x: auto;
  font-size: 0.85em;
}
.cw-bubble-assistant .cw-code code { background: transparent; color: inherit; padding: 0; }
.cw-bubble-assistant .cw-bq {
  border-left: 3px solid #93c5fd; padding: 0.2em 0.7em;
  margin: 0.4em 0; color: #4b5563; font-style: italic;
}
.cw-bubble-assistant hr { border: 0; border-top: 1px solid #e5e7eb; margin: 0.6em 0; }
.cw-bubble-assistant a { color: var(--primary); text-decoration: underline; }

/* Bulle "l'IA reflechit" - 3 points qui pulsent */
.cw-typing { padding: 0.7em 1em !important; }
.cw-typing-dots { display: flex; gap: 5px; align-items: center; }
.cw-typing-dots span {
  display: inline-block; width: 8px; height: 8px; border-radius: 50%;
  background: #6b7280;
  animation: cwBounce 1.2s ease-in-out infinite;
}
.cw-typing-dots span:nth-child(2) { animation-delay: 0.15s; }
.cw-typing-dots span:nth-child(3) { animation-delay: 0.3s; }
@keyframes cwBounce {
  0%, 60%, 100% { transform: translateY(0); opacity: 0.4; }
  30% { transform: translateY(-6px); opacity: 1; }
}

.chat-widget-input {
  display: flex; gap: 0.4em;
  padding: 0.6em;
  border-top: 1px solid var(--border);
  background: #fff;
}
.chat-widget-input textarea {
  flex: 1; margin: 0;
  border: 1px solid var(--border); border-radius: 6px;
  padding: 0.45em 0.6em;
  font-size: 0.92em;
  resize: none;
  font-family: inherit;
  min-height: 38px;
  max-height: 100px;
}
.chat-widget-input button {
  padding: 0 0.9em;
  background: var(--primary); color: #fff;
  border: none; border-radius: 6px;
  cursor: pointer; font-weight: 500;
}
.chat-widget-input button:disabled { opacity: 0.5; cursor: wait; }

.chat-widget-status {
  padding: 0 0.8em 0.4em;
  font-size: 0.78em;
  color: var(--muted);
  min-height: 1.2em;
  background: #fff;
}

/* ========== Feedback ========== */
.chip-type-question { background: #dbeafe; color: #1e40af; }
.chip-type-bug { background: #fee2e2; color: #991b1b; }
.chip-type-suggestion { background: #fef3c7; color: #92400e; }
.chip-status-open { background: #fee2e2; color: #991b1b; }
.chip-status-in_progress { background: #fef3c7; color: #92400e; }
.chip-status-answered { background: #d1fae5; color: #065f46; }
.chip-status-closed { background: #e5e7eb; color: #6b7280; }
.chip-prio-low { background: #e5e7eb; color: #6b7280; }
.chip-prio-normal { background: #dbeafe; color: #1e40af; }
.chip-prio-high { background: #fee2e2; color: #991b1b; }

.feedback-thread { display: flex; flex-direction: column; gap: 0.8em; }
.feedback-msg {
  background: #fff; border: 1px solid var(--border); border-radius: 8px;
  padding: 0.8em 1em;
}
.feedback-msg-answer { background: #f0f9ff; border-color: #7dd3fc; }
.feedback-msg-head {
  display: flex; gap: 0.5em; align-items: center;
  font-size: 0.88em; margin-bottom: 0.4em;
  padding-bottom: 0.4em; border-bottom: 1px solid var(--border);
}
.feedback-msg-body { white-space: pre-wrap; line-height: 1.55; }

a.btn { text-decoration: none; }

