/* Couleurs en variables : un seul endroit à changer, et le mode sombre
   ne fait que redéfinir ces variables. */
:root {
  --bg: #faf9f5;
  --surface: #ffffff;
  --user-bubble: #f0eee6;
  --text: #1f1e1d;
  --muted: #73716b;
  --border: #e3e0d6;
  --accent: #c96442;
  --accent-text: #ffffff;
  --code-bg: #f3f1ea;
  --error: #b3261e;
  --sidebar: #f5f4ee;
  --hover: #ebe9e0;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #262624;
    --surface: #30302e;
    --user-bubble: #3a3936;
    --text: #ecebe6;
    --muted: #a19f97;
    --border: #45443f;
    --accent: #d97757;
    --code-bg: #1f1f1d;
    --error: #f2b8b5;
    --sidebar: #1f1e1d;
    --hover: #353431;
  }
}

* { box-sizing: border-box; }

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

body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.6 ui-sans-serif, system-ui, -apple-system, "Segoe UI", sans-serif;
  display: flex;   /* barre latérale à gauche, conversation à droite */
}

/* Étape 10 : barre latérale */
#sidebar {
  flex: none;
  width: 260px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 10px;
  background: var(--sidebar);
  border-right: 1px solid var(--border);
}
.sidebar-head { display: flex; align-items: center; justify-content: space-between; padding: 4px 6px 8px; }
.brand { font-family: ui-serif, Georgia, serif; font-size: 19px; }
.new-chat {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  border: none;
  border-radius: 8px;
  background: none;
  color: var(--text);
  font: inherit;
  font-size: 14px;
  cursor: pointer;
  text-align: left;
}
.new-chat span {
  width: 22px; height: 22px;
  display: grid;
  place-items: center;
  border-radius: 50%;
  background: var(--accent);
  color: var(--accent-text);
  font-size: 16px;
}
.new-chat:hover { background: var(--hover); }
.sidebar-label { margin: 14px 10px 2px; font-size: 12px; color: var(--muted); }
#conversation-list { flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 1px; }
.conv-item { display: flex; align-items: center; border-radius: 8px; }
.conv-item:hover, .conv-item.current { background: var(--hover); }
.conv-link {
  flex: 1;
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 10px;
  color: var(--text);
  text-decoration: none;
  font-size: 14px;
}
.conv-title { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
/* Pastille qui pulse : l'agent travaille encore sur cette conversation */
.conv-running {
  flex: none;
  width: 7px; height: 7px;
  border-radius: 50%;
  background: var(--accent);
  animation: pulse 1.2s infinite ease-in-out;
}
.conv-delete {
  flex: none;
  visibility: hidden;   /* n'apparaît qu'au survol */
  border: none;
  background: none;
  color: var(--muted);
  font-size: 18px;
  line-height: 1;
  padding: 4px 8px;
  cursor: pointer;
}
.conv-item:hover .conv-delete { visibility: visible; }
.conv-delete:hover { color: var(--error); }

#main { flex: 1; min-width: 0; display: flex; flex-direction: column; height: 100%; }

/* Barre du haut : titre de la conversation */
.topbar {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 52px;
  padding: 8px 16px;
}
.conversation-title { font-size: 15px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.icon-button {
  border: none;
  background: none;
  color: var(--text);
  font-size: 22px;
  line-height: 1;
  padding: 4px 6px;
  border-radius: 8px;
  cursor: pointer;
  display: grid;
  place-items: center;
}
.icon-button:hover { background: var(--hover); }

/* Petit écran : la barre latérale devient un tiroir qui glisse par-dessus */
.mobile-only { display: none; }
@media (max-width: 800px) {
  .mobile-only { display: grid; }
  #backdrop[hidden] { display: none; }
  #sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    z-index: 10;
    transform: translateX(-100%);
    transition: transform 0.2s;
    box-shadow: 4px 0 24px rgb(0 0 0 / 0.2);
  }
  body.sidebar-open #sidebar { transform: none; }
  #backdrop { position: fixed; inset: 0; z-index: 5; background: rgb(0 0 0 / 0.35); }
}

/* Zone des messages : seule partie qui défile */
#chat { flex: 1; overflow-y: auto; }
#messages {
  max-width: 760px;
  margin: 0 auto;
  padding: 16px 16px 32px;
}

.empty { text-align: center; margin-top: 18vh; }
.empty h1 { font-family: ui-serif, Georgia, serif; font-weight: 400; font-size: 34px; margin: 0; }
.empty p { color: var(--muted); }

.msg { margin: 20px 0; overflow-wrap: anywhere; }

/* Message utilisateur : bulle à droite */
.msg.user {
  margin-left: auto;
  width: fit-content;
  max-width: 85%;
  background: var(--user-bubble);
  border-radius: 16px;
  padding: 10px 16px;
  white-space: pre-wrap;
}

/* Réponse de l'agent : une suite de blocs (réflexion, texte, outils) */
.msg.assistant { display: flex; flex-direction: column; gap: 10px; }

/* Texte rendu depuis le Markdown */
.text > :first-child { margin-top: 0; }
.text > :last-child { margin-bottom: 0; }
.text pre {
  background: var(--code-bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 12px;
  overflow-x: auto;
  font-size: 14px;
}
.text code, .block pre { font-family: ui-monospace, "SFMono-Regular", Consolas, monospace; }
.text code { font-size: 0.9em; }
.text :not(pre) > code { background: var(--code-bg); padding: 1px 5px; border-radius: 4px; }
.text table { border-collapse: collapse; display: block; overflow-x: auto; }
.text th, .text td { border: 1px solid var(--border); padding: 6px 10px; }
.text a { color: var(--accent); }

/* Blocs dépliables : réflexion et appels d'outils */
.block {
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  font-size: 14px;
}
.block summary {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 7px 12px;
  cursor: pointer;
  color: var(--muted);
  list-style: none;          /* masque le triangle par défaut… */
}
.block summary::-webkit-details-marker { display: none; }
.block summary::after {      /* …remplacé par un chevron à droite */
  content: "›";
  margin-left: auto;
  display: inline-block;     /* sinon la rotation ne s'applique pas */
  transition: transform 0.15s;
}
.block[open] summary::after { transform: rotate(90deg); }
.block-body {
  border-top: 1px solid var(--border);
  padding: 10px 12px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.reasoning .block-body {
  color: var(--muted);
  white-space: pre-wrap;
  max-height: 320px;
  overflow-y: auto;
}
.block pre {
  margin: 0;
  padding: 10px;
  border-radius: 6px;
  background: var(--code-bg);
  font-size: 13px;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  max-height: 360px;
  overflow-y: auto;
}
.block pre.result { color: var(--muted); }

.tool-label { color: var(--text); font-weight: 500; }
.tool-detail { font-family: ui-monospace, Consolas, monospace; font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tool-duration { font-size: 12px; }

/* Pastille d'état d'un outil : tourne pendant l'exécution, puis vert ou rouge */
.status { flex: none; width: 10px; height: 10px; border-radius: 50%; }
.status.running {
  border: 2px solid var(--border);
  border-top-color: var(--accent);
  animation: spin 0.8s linear infinite;
}
.status.ok { background: #3a9a5b; }
.status.failed { background: var(--error); }
@keyframes spin { to { transform: rotate(360deg); } }

.error { color: var(--error); }

/* Indicateur "l'agent travaille" : trois points qui pulsent */
.thinking[hidden] { display: none; }
.thinking { color: var(--muted); display: flex; gap: 5px; align-items: center; height: 26px; }
.thinking span {
  width: 7px; height: 7px; border-radius: 50%;
  background: var(--accent);
  animation: pulse 1.2s infinite ease-in-out;
}
.thinking span:nth-child(2) { animation-delay: 0.2s; }
.thinking span:nth-child(3) { animation-delay: 0.4s; }
@keyframes pulse { 0%, 80%, 100% { opacity: 0.25; } 40% { opacity: 1; } }

/* Zone de saisie en bas */
#composer { padding: 0 16px 12px; }
.composer-box {
  max-width: 760px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 8px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 10px 10px 10px 16px;
  box-shadow: 0 2px 12px rgb(0 0 0 / 0.05);
}
.composer-row { display: flex; align-items: flex-end; gap: 8px; }
#attach {
  flex: none;
  width: 34px; height: 34px;
  border: none;
  border-radius: 9px;
  background: none;
  color: var(--muted);
  cursor: pointer;
  display: grid;
  place-items: center;
  margin-left: -8px;
}
#attach:hover { background: var(--user-bubble); color: var(--text); }
#input {
  flex: 1;
  border: none;
  outline: none;
  resize: none;
  background: transparent;
  color: var(--text);
  font: inherit;
  max-height: 220px;
  padding: 4px 0;
}
#send, .send-button {
  flex: none;
  width: 34px; height: 34px;
  border: none;
  border-radius: 9px;
  background: var(--accent);
  color: var(--accent-text);
  cursor: pointer;
  display: grid;
  place-items: center;
}
#send:disabled, .send-button:disabled { opacity: 0.4; cursor: default; }
.hint { max-width: 760px; margin: 6px auto 0; text-align: center; font-size: 12px; color: var(--muted); }

/* Étape 9 : pièces jointes */
.attachments { display: flex; flex-wrap: wrap; gap: 6px; }
.attachments[hidden] { display: none; }
.chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  max-width: 100%;
  padding: 4px 6px 4px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  font-size: 13px;
  overflow-wrap: anywhere;
}
.chip.uploading { opacity: 0.6; }
.chip.failed { color: var(--error); border-color: var(--error); }
.chip-remove {
  border: none;
  background: none;
  color: var(--muted);
  cursor: pointer;
  font-size: 16px;
  line-height: 1;
  padding: 0 4px;
}
.user-files { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 6px; margin: 20px 0 -12px; }
.user-files .chip { padding-right: 10px; background: var(--surface); }

/* Carte de téléchargement d'un fichier livré */
.file-card {
  display: flex;
  align-items: center;
  gap: 12px;
  max-width: 420px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
  color: var(--text);
  text-decoration: none;
}
.file-card:hover { border-color: var(--accent); }
.file-icon {
  flex: none;
  width: 40px; height: 40px;
  display: grid;
  place-items: center;
  border-radius: 8px;
  background: var(--accent);
  color: var(--accent-text);
  font-size: 10px;
  font-weight: 700;
}
.file-info { display: flex; flex-direction: column; min-width: 0; flex: 1; }
.file-name { font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.file-meta { font-size: 12px; color: var(--muted); }
.file-action { font-size: 13px; color: var(--accent); flex: none; }

/* Glisser-déposer : on souligne la zone de saisie */
body.dragging .composer-box { border-color: var(--accent); box-shadow: 0 0 0 3px rgb(201 100 66 / 0.25); }

/* Étape 11 : connexion */
.logout { color: var(--muted); }
.login-page { align-items: center; justify-content: center; min-height: 100%; padding: 16px; }
.login-box {
  width: 100%;
  max-width: 340px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 28px;
  border: 1px solid var(--border);
  border-radius: 16px;
  background: var(--surface);
}
.login-box h1 { margin: 0 0 8px; font-weight: 400; font-size: 28px; text-align: center; }
.login-box label { font-size: 14px; color: var(--muted); }
.login-box input {
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg);
  color: var(--text);
  font: inherit;
}
.login-box input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.login-box button {
  margin-top: 6px;
  padding: 10px;
  border: none;
  border-radius: 10px;
  background: var(--accent);
  color: var(--accent-text);
  font: inherit;
  cursor: pointer;
}
.login-box button:disabled { opacity: 0.5; }
.login-box .error { margin: 0; font-size: 14px; }

/* Mémoire : vue qui remplace le chat (body.memory-mode) */
#memory-view { display: none; flex: 1; overflow-y: auto; }
body.memory-mode #memory-view { display: block; }
body.memory-mode #chat, body.memory-mode #composer { display: none; }
.side-link svg { flex: none; width: 22px; color: var(--muted); }
.side-link.current { background: var(--hover); }

.memory-page { max-width: 760px; margin: 0 auto; padding: 6vh 16px 48px; }
.memory-page h1 { font-family: ui-serif, Georgia, serif; font-weight: 400; font-size: 34px; margin: 0; }
.memory-help { color: var(--muted); margin: 6px 0 24px; }
#memory-input, .memory-edit {
  flex: 1;
  border: none;
  outline: none;
  resize: none;
  background: transparent;
  color: var(--text);
  font: inherit;
  padding: 4px 0;
}
.memory-edit { flex: none; }  /* en colonne flex, flex: 1 écraserait la hauteur calculée */
.memory-actions[hidden] { display: none; }
.memory-status { margin: 8px 4px 20px; font-size: 12px; color: var(--muted); }
.memory-status.error { color: var(--error); }

.memory-list { margin: 0; padding: 0; list-style: none; display: flex; flex-direction: column; gap: 8px; }
.memory-note {
  display: flex;
  align-items: flex-start;
  gap: 12px;
  padding: 12px 10px 12px 16px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
}
.memory-note.editing { border-color: var(--accent); }
.memory-number { flex: none; min-width: 18px; color: var(--muted); font-size: 14px; line-height: 1.6; padding-top: 1px; }
.memory-body { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 8px; }
.memory-text { white-space: pre-wrap; overflow-wrap: anywhere; }
.memory-actions { flex: none; display: flex; gap: 2px; visibility: hidden; margin-top: -2px; }
.memory-note:hover .memory-actions, .memory-note:focus-within .memory-actions { visibility: visible; }
.memory-actions button {
  width: 30px; height: 30px;
  border: none;
  border-radius: 8px;
  background: none;
  color: var(--muted);
  cursor: pointer;
  display: grid;
  place-items: center;
}
.memory-actions button:hover { background: var(--hover); color: var(--text); }
.memory-actions .delete:hover { color: var(--error); }
.memory-edit-buttons { display: flex; justify-content: flex-end; gap: 6px; }
.memory-edit-buttons button {
  padding: 6px 14px;
  border: 1px solid var(--border);
  border-radius: 9px;
  background: none;
  color: var(--text);
  font: inherit;
  font-size: 14px;
  cursor: pointer;
}
.memory-edit-buttons button:hover { background: var(--hover); }
.memory-edit-buttons .primary { border-color: var(--accent); background: var(--accent); color: var(--accent-text); }
.memory-edit-buttons .primary:hover { background: var(--accent); opacity: 0.9; }
.memory-empty { padding: 28px 16px; border: 1px dashed var(--border); border-radius: 12px; text-align: center; color: var(--muted); font-size: 14px; }
/* Écran tactile : pas de survol, les boutons restent visibles */
@media (hover: none) { .memory-actions { visibility: visible; } }

/* Choix du modèle, à droite de la barre du haut */
.model-select {
  margin-left: auto;
  flex: none;
  max-width: 45vw;
  padding: 5px 8px;
  border: 1px solid transparent;
  border-radius: 8px;
  background: none;
  color: var(--muted);
  font: inherit;
  font-size: 14px;
  cursor: pointer;
}
.model-select:hover:not(:disabled) { background: var(--hover); color: var(--text); }
.model-select:disabled { cursor: default; opacity: 1; appearance: none; }
.model-select option { background: var(--surface); color: var(--text); }
body.memory-mode .model-select { visibility: hidden; }

/* Interrupteur Rapide / Approfondi, à gauche du bouton d'envoi */
.thinking-toggle {
  flex: none;
  height: 34px;
  padding: 0 10px;
  border: 1px solid var(--border);
  border-radius: 9px;
  background: none;
  color: var(--muted);
  font: inherit;
  font-size: 13px;
  cursor: pointer;
}
.thinking-toggle:hover { background: var(--user-bubble); color: var(--text); }
.thinking-toggle[aria-pressed="true"] { border-color: var(--accent); color: var(--accent); }
.thinking-toggle[hidden] { display: none; }
