/* Widget embarqué dans le panneau latéral droit de previsionnel.ch.
 *
 * Contrainte de départ : le panneau fait 380px de large et toute la hauteur
 * utile (`calc(100vh - 3.5rem)`), et il est redimensionnable par l'utilisateur.
 * Tout est donc fluide, et rien ne suppose une largeur confortable.
 *
 * Les tokens reprennent ceux des pages démo pour rester dans la charte Fondetec.
 */

:root {
  --coral: #FB5058;
  --coral-hover: #E94850;
  --dark: #3C3C3B;
  --title: #3A3A3A;
  --beige: #FAF9F5;
  --border: #E5E5E5;
  --muted: #8A8A8A;
  --radius: 12px;
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
}

body {
  font-family: "Open Sans", -apple-system, system-ui, "Segoe UI", Roboto, sans-serif;
  font-size: 14px;
  line-height: 21px;
  color: var(--dark);
  background: var(--beige);
  /* Le chat gère son propre défilement : la page, elle, ne bouge jamais. */
  overflow: hidden;
}

.w-sr {
  position: absolute;
  width: 1px; height: 1px;
  padding: 0; margin: -1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

.w {
  display: flex;
  flex-direction: column;
  height: 100%;
  min-height: 0;
}

/* ---------- Retour au hub (écran de chat) ---------- */

.w-back {
  flex: none;
  padding: 6px 14px 0;
  background: #fff;
}
.w-back-link {
  font-size: 11.5px;
  font-weight: 600;
  color: var(--muted);
  text-decoration: none;
}
.w-back-link:hover { color: var(--coral); }

/* ---------- Hub : liste des conversations ---------- */

.hub-head {
  flex: none;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 14px;
  background: #fff;
  border-bottom: 1px solid var(--border);
}

.hub-title {
  margin: 0;
  font-size: 14px;
  font-weight: 700;
  color: var(--title);
}

.hub-new {
  flex: none;
  padding: 7px 12px;
  border: 0;
  border-radius: 999px;
  background: var(--coral);
  color: #fff;
  font: inherit;
  font-size: 12.5px;
  font-weight: 600;
  cursor: pointer;
  transition: background .15s ease;
}
.hub-new:hover { background: var(--coral-hover); }

.hub-error {
  margin: 0;
  padding: 7px 14px;
  font-size: 12px;
  color: var(--coral);
  background: #fff;
  border-bottom: 1px solid var(--border);
}

.hub-list {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 10px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.hub-item {
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: #fff;
  text-decoration: none;
  color: inherit;
  transition: border-color .15s ease;
}
.hub-item:hover { border-color: var(--coral); }

.hub-item-title {
  font-size: 13px;
  font-weight: 700;
  color: var(--title);
}
.hub-item[data-archived="true"] .hub-item-title::after {
  content: " · archivée";
  font-size: 10.5px;
  font-weight: 600;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: .04em;
}

.hub-item-meta {
  font-size: 11px;
  color: var(--muted);
}

.hub-item-preview {
  font-size: 12.5px;
  color: var(--dark);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.hub-empty {
  margin: 0;
  padding: 14px;
  font-size: 12.5px;
  color: var(--muted);
  text-align: center;
}

/* ---------- En-tête : progression repliée par défaut ---------- */

.w-head {
  flex: none;
  background: #fff;
  border-bottom: 1px solid var(--border);
}

.w-progress {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  padding: 10px 14px;
  background: none;
  border: 0;
  cursor: pointer;
  font: inherit;
  color: inherit;
  text-align: left;
}
.w-progress:hover { background: rgba(0, 0, 0, .02); }

.w-dots {
  display: flex;
  gap: 4px;
  flex: none;
}
.w-dot {
  width: 16px;
  height: 4px;
  border-radius: 2px;
  background: var(--border);
  transition: background .2s ease;
}
.w-dot.is-done { background: var(--coral); opacity: .45; }
.w-dot.is-current { background: var(--coral); }

.w-stage {
  flex: 1;
  min-width: 0;
  display: flex;
  align-items: baseline;
  gap: 6px;
}
.w-stage-num {
  flex: none;
  font-size: 11px;
  font-weight: 700;
  color: var(--muted);
  letter-spacing: .04em;
}
.w-stage-label {
  font-size: 13px;
  font-weight: 600;
  color: var(--title);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.w-chev {
  flex: none;
  color: var(--muted);
  font-size: 11px;
  transition: transform .2s ease;
}
.w-progress[aria-expanded="true"] .w-chev { transform: rotate(180deg); }

/* ---------- Liste dépliée des étapes ---------- */

.w-steps {
  list-style: none;
  margin: 0;
  padding: 4px 8px 10px;
  border-top: 1px solid var(--border);
  max-height: 45vh;
  overflow-y: auto;
}

.w-step-head {
  display: flex;
  align-items: center;
  gap: 9px;
  width: 100%;
  padding: 7px 8px;
  background: none;
  border: 0;
  border-radius: 8px;
  cursor: default;
  font: inherit;
  color: inherit;
  text-align: left;
}
.w-step.has-summary .w-step-head { cursor: pointer; }
.w-step.has-summary .w-step-head:hover { background: rgba(0, 0, 0, .03); }

.w-step-num {
  flex: none;
  display: grid;
  place-items: center;
  width: 20px; height: 20px;
  border-radius: 50%;
  border: 1.5px solid var(--border);
  font-size: 11px;
  font-weight: 700;
  color: var(--muted);
}
.w-step.is-done .w-step-num {
  background: var(--coral);
  border-color: var(--coral);
  color: #fff;
}
.w-step.is-current .w-step-num {
  border-color: var(--coral);
  color: var(--coral);
}

.w-step-label {
  font-size: 13px;
  color: var(--muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.w-step.is-current .w-step-label { color: var(--title); font-weight: 600; }
.w-step.is-done .w-step-label { color: var(--dark); }

.w-step-detail {
  padding: 2px 8px 10px 37px;
  font-size: 12.5px;
  line-height: 19px;
  color: var(--dark);
}
.w-step-detail dl { margin: 0; }
.w-step-detail dt {
  font-size: 10.5px;
  text-transform: uppercase;
  letter-spacing: .06em;
  color: var(--muted);
  margin-top: 8px;
}
.w-step-detail dd { margin: 1px 0 0; }
.w-step-detail ul { margin: 2px 0 0; padding-left: 16px; }

/* ---------- Bandeau de contexte projet ---------- */

.w-context {
  flex: none;
  margin: 0;
  padding: 7px 14px;
  font-size: 11.5px;
  line-height: 16px;
  color: var(--muted);
  background: #fff;
  border-bottom: 1px solid var(--border);
}
.w-context.is-error { color: var(--coral); }

/* ---------- Conversation ---------- */

.w-log {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.msg {
  max-width: 100%;
  padding: 9px 12px;
  border-radius: var(--radius);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}
.msg.assistant {
  background: #fff;
  border: 1px solid var(--border);
  border-bottom-left-radius: 4px;
}
.msg.user {
  align-self: flex-end;
  background: var(--coral);
  color: #fff;
  border-bottom-right-radius: 4px;
  max-width: 88%;
}
.msg.error {
  background: #FFF2F2;
  border: 1px solid #F5C6C6;
  color: #B3261E;
  font-size: 13px;
}
.msg.loading { color: var(--muted); }

.msg p:first-child { margin-top: 0; }
.msg p:last-child { margin-bottom: 0; }
.msg ul, .msg ol { margin: 4px 0; padding-left: 18px; }
.msg li { margin: 2px 0; }
.msg code {
  background: rgba(0, 0, 0, .05);
  padding: 1px 4px;
  border-radius: 4px;
  font-size: .92em;
}
.msg .md-h { display: block; margin: 8px 0 2px; }
.msg .md-h:first-child { margin-top: 0; }
.msg a { color: inherit; }

/* Le premier tour est un peu long à venir : on anime les points de suite. */
.msg.loading::after {
  content: "";
  animation: w-ellipsis 1.2s steps(4, end) infinite;
}
@keyframes w-ellipsis {
  0%   { content: "·"; }
  25%  { content: "··"; }
  50%  { content: "···"; }
  75%  { content: "····"; }
}

/* ---------- Matrice / étude finale ---------- */

.msg.matrix {
  background: #fff;
  border: 1px solid var(--border);
  padding: 12px;
  white-space: normal;
}
.mx-title {
  margin: 0 0 10px;
  font-size: 13px;
  font-weight: 700;
  color: var(--title);
}
.mx-cards { display: flex; flex-direction: column; gap: 10px; }
.mx-card {
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px;
}
.mx-card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 8px;
}
.mx-channel {
  font-weight: 700;
  font-size: 13px;
  text-transform: capitalize;
}
.mx-ratio {
  flex: none;
  font-size: 11px;
  font-weight: 700;
  padding: 2px 7px;
  border-radius: 999px;
}
.mx-ratio.good { background: #E6F4EA; color: #1E7B34; }
.mx-ratio.mid  { background: #FEF7E0; color: #9A6800; }
.mx-ratio.bad  { background: #FCE8E6; color: #B3261E; }

/* 3 colonnes tient dans 380px ; en dessous on retombe sur 2. */
.mx-kpis {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 6px;
}
@media (max-width: 340px) {
  .mx-kpis { grid-template-columns: repeat(2, 1fr); }
}
.mx-kpi, .mx-horizon {
  background: var(--beige);
  border-radius: 8px;
  padding: 6px 4px;
  text-align: center;
}
.mx-kpi-value {
  font-size: 13px;
  font-weight: 700;
  color: var(--title);
  overflow-wrap: anywhere;
}
.mx-kpi-label {
  font-size: 10px;
  color: var(--muted);
  margin-top: 1px;
}

.mx-funnel {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 4px;
  margin-top: 8px;
  font-size: 11.5px;
  color: var(--muted);
}
.mx-funnel .arrow { opacity: .5; }
.mx-effort {
  margin-top: 8px;
  font-size: 11.5px;
  line-height: 17px;
  color: var(--muted);
}
.mx-assumptions {
  margin: 8px 0 0;
  padding-left: 16px;
  font-size: 11.5px;
  line-height: 17px;
  color: var(--muted);
}
.mx-horizons {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 6px;
  margin-top: 10px;
}
.mx-summary {
  margin: 10px 0 0;
  font-size: 12.5px;
  line-height: 19px;
}

/* ---------- Suggestions ---------- */

.w-suggestions {
  flex: none;
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  padding: 0 14px;
}
.w-suggestions:empty { display: none; }

.chip {
  padding: 6px 11px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: #fff;
  font: inherit;
  font-size: 12.5px;
  color: var(--dark);
  cursor: pointer;
  transition: border-color .15s ease, color .15s ease;
}
.chip:hover { border-color: var(--coral); color: var(--coral); }
.chip:disabled { opacity: .5; cursor: default; }
.chip-next {
  background: var(--coral);
  border-color: var(--coral);
  color: #fff;
  font-weight: 600;
}
.chip-next:hover { background: var(--coral-hover); border-color: var(--coral-hover); color: #fff; }

/* ---------- Composer ---------- */

.w-composer {
  flex: none;
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 10px 14px 6px;
  background: var(--beige);
}

.w-composer textarea {
  flex: 1;
  min-width: 0;
  resize: none;
  max-height: 140px;
  padding: 9px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: #fff;
  font: inherit;
  line-height: 20px;
  color: inherit;
}
.w-composer textarea:focus {
  outline: none;
  border-color: var(--coral);
}

.w-composer button {
  flex: none;
  display: grid;
  place-items: center;
  width: 38px; height: 38px;
  border: 0;
  border-radius: 50%;
  background: var(--coral);
  color: #fff;
  cursor: pointer;
  transition: background .15s ease;
}
.w-composer button:hover:not(:disabled) { background: var(--coral-hover); }
.w-composer button:disabled { opacity: .45; cursor: default; }

.w-footnote {
  flex: none;
  margin: 0;
  padding: 0 14px 10px;
  font-size: 10.5px;
  line-height: 15px;
  color: var(--muted);
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; }
}
