/* ai-chat.css — Assistant IA (bouton + panneau). Chargé sur toutes les
   pages qui ont la sidebar (.rail). Utilise uniquement les variables CSS
   déjà définies dans styles.css : thème clair/sombre géré automatiquement. */

.ai-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.45);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.18s ease;
  z-index: 900;
}
.ai-backdrop.show { opacity: 1; pointer-events: auto; }

.ai-panel {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  width: 400px;
  max-width: 100vw;
  background: var(--bg);
  border-left: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  transform: translateX(100%);
  transition: transform 0.22s ease;
  z-index: 901;
  padding-top: env(safe-area-inset-top, 0px);
  padding-bottom: env(safe-area-inset-bottom, 0px);
}
.ai-backdrop.show .ai-panel { transform: translateX(0); }

@media (max-width: 720px) {
  .ai-panel {
    top: auto;
    left: 0;
    right: 0;
    width: auto;
    height: 88vh;
    height: 88dvh;
    border-left: none;
    border-top: 1px solid var(--border);
    border-radius: 16px 16px 0 0;
    transform: translateY(100%);
  }
  .ai-backdrop.show .ai-panel { transform: translateY(0); }
}

.ai-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border-soft);
  flex-shrink: 0;
}
.ai-head h2 { font-size: 14.5px; font-weight: 600; color: var(--text); margin: 0; }
.ai-head-actions { display: flex; align-items: center; gap: 6px; }
.ai-icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 30px;
  height: 30px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text-2);
  cursor: pointer;
}
.ai-icon-btn:hover { background: var(--surface); color: var(--text); }
.ai-icon-btn svg { width: 16px; height: 16px; }

.ai-body { flex: 1; min-height: 0; display: flex; flex-direction: column; overflow: hidden; }

/* ---- Étape 1 : consentement ---- */
.ai-consent { padding: 18px 18px 16px; overflow-y: auto; }
.ai-consent p { font-size: 13px; line-height: 1.55; color: var(--text-2); margin: 0 0 10px; }
.ai-consent ul { margin: 0 0 14px; padding-left: 18px; }
.ai-consent li { font-size: 13px; line-height: 1.55; color: var(--text-2); margin-bottom: 6px; }
.ai-consent-check {
  display: flex;
  align-items: flex-start;
  gap: 9px;
  padding: 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  margin-bottom: 14px;
  cursor: pointer;
}
.ai-consent-check input { margin-top: 2px; flex-shrink: 0; }
.ai-consent-check span { font-size: 12.5px; line-height: 1.5; color: var(--text); }
.ai-btn-primary {
  width: 100%;
  padding: 11px;
  border: none;
  border-radius: var(--radius);
  background: var(--accent);
  color: #fff;
  font-weight: 600;
  font-size: 13.5px;
  cursor: pointer;
}
.ai-btn-primary:disabled { opacity: 0.45; cursor: not-allowed; }
.ai-btn-link {
  display: block;
  width: 100%;
  text-align: center;
  background: none;
  border: none;
  color: var(--text-3);
  font-size: 12px;
  margin-top: 10px;
  cursor: pointer;
}

/* ---- Étape 2 : chat ---- */
.ai-messages { flex: 1; overflow-y: auto; padding: 14px 16px; display: flex; flex-direction: column; gap: 10px; }
.ai-msg { max-width: 88%; padding: 9px 12px; border-radius: 12px; font-size: 13.5px; line-height: 1.5; white-space: pre-wrap; word-wrap: break-word; }
.ai-msg-user { align-self: flex-end; background: var(--accent); color: #fff; border-bottom-right-radius: 3px; }
.ai-msg-assistant { align-self: flex-start; background: var(--surface); color: var(--text); border-bottom-left-radius: 3px; white-space: normal; min-width: 0; }

/* Markdown rendu dans les réponses de l'assistant */
.ai-msg-assistant > :first-child { margin-top: 0; }
.ai-msg-assistant > :last-child { margin-bottom: 0; }
.ai-msg-assistant p { margin: 0 0 8px; }
.ai-msg-assistant .ai-md-heading { font-weight: 600; }
.ai-msg-assistant strong { font-weight: 600; }
.ai-msg-assistant ul,
.ai-msg-assistant ol { margin: 0 0 8px; padding-left: 20px; }
.ai-msg-assistant li { margin-bottom: 3px; }
.ai-msg-assistant a { color: var(--accent); text-decoration: underline; }
.ai-msg-assistant hr { border: none; border-top: 1px solid var(--border); margin: 10px 0; }
.ai-msg-assistant code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 12.5px;
  background: var(--bg);
  border: 1px solid var(--border-soft);
  border-radius: 5px;
  padding: 1px 5px;
}
.ai-msg-assistant pre {
  margin: 0 0 8px;
  padding: 9px 11px;
  background: var(--bg);
  border: 1px solid var(--border-soft);
  border-radius: 8px;
  overflow-x: auto;
  white-space: pre;
}
.ai-msg-assistant pre code { background: none; border: none; padding: 0; }
.ai-table-wrap { margin: 0 0 8px; overflow-x: auto; }
.ai-msg-assistant table { border-collapse: collapse; font-size: 12.5px; width: 100%; }
.ai-msg-assistant th,
.ai-msg-assistant td { border: 1px solid var(--border); padding: 5px 8px; text-align: left; vertical-align: top; }
.ai-msg-assistant th { background: var(--bg); font-weight: 600; }
.ai-msg-error { align-self: center; background: transparent; color: var(--text-3); font-size: 12px; text-align: center; }
.ai-typing { align-self: flex-start; color: var(--text-3); font-size: 12.5px; padding: 0 4px; }

.ai-suggestions { display: flex; flex-wrap: wrap; gap: 6px; padding: 0 16px 10px; flex-shrink: 0; }
.ai-suggestion {
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text-2);
  border-radius: 999px;
  padding: 6px 11px;
  font-size: 12px;
  cursor: pointer;
}
.ai-suggestion:hover { color: var(--text); border-color: var(--accent); }

.ai-input-row {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 10px 12px;
  border-top: 1px solid var(--border-soft);
  flex-shrink: 0;
}
.ai-input-row textarea {
  flex: 1;
  resize: none;
  max-height: 96px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  font-family: inherit;
  font-size: 13.5px;
  padding: 9px 11px;
}
.ai-input-row textarea:focus { outline: none; border-color: var(--accent); }
.ai-send-btn {
  flex-shrink: 0;
  width: 36px;
  height: 36px;
  border-radius: 10px;
  border: none;
  background: var(--accent);
  color: #fff;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}
.ai-send-btn:disabled { opacity: 0.45; cursor: not-allowed; }
.ai-send-btn svg { width: 16px; height: 16px; }

/* Bouton robot verrouillé/indisponible : reste dans la sidebar mais inerte. */
.nav-icon.ai-unavailable { opacity: 0.4; pointer-events: none; }

/* ---------- barre de quota ---------- */
.ai-quota {
  display: flex;
  flex-direction: column;
  gap: 5px;
  padding: 8px 16px 2px;
  border-top: 1px solid var(--border-soft);
  flex-shrink: 0;
  font-size: 11px;
  color: var(--text-3);
}
.ai-quota-row { display: flex; align-items: center; gap: 8px; }
.ai-quota-label { width: 44px; flex-shrink: 0; }
.ai-quota-track { flex: 1; height: 5px; border-radius: 999px; background: var(--surface); border: 1px solid var(--border-soft); overflow: hidden; }
.ai-quota-fill { height: 100%; width: 100%; border-radius: 999px; background: var(--accent); transition: width .45s ease, background-color .3s ease; }
.ai-quota-count { width: 38px; text-align: right; font-variant-numeric: tabular-nums; color: var(--text-2); }
.ai-quota-reset { width: 78px; text-align: right; font-variant-numeric: tabular-nums; }
.ai-quota-row.low .ai-quota-fill { background: var(--warn); }
.ai-quota-row.low .ai-quota-count { color: var(--warn); }
.ai-quota-row.empty .ai-quota-fill { background: var(--bad); }
.ai-quota-row.empty .ai-quota-count { color: var(--bad); }
.ai-quota + .ai-input-row { border-top: none; }
