/*
  SPDX-FileCopyrightText: 2026 Oraclia SAS
  SPDX-License-Identifier: LicenseRef-Oraclia-Proprietary
  RAG-PF — présentation de la conversation (docs/UI_CHAT.md).
  Sobre, aérée, typographie système, clair et sombre automatiques, mouvements discrets.
*/

:root {
  --font: -apple-system, BlinkMacSystemFont, "SF Pro Text", "SF Pro", Inter, system-ui, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, "SF Mono", SFMono-Regular, Menlo, Consolas, monospace;
  --bg: #ffffff;
  --fg: #1d1d1f;
  --muted: #6e6e73;
  --faint: #aeaeb2;
  --line: rgba(0, 0, 0, 0.09);
  --bubble: #f2f2f7;
  --panel: #f5f5f7;
  --accent: #0071e3;
  --accent-fg: #ffffff;
  --danger: #c0392b;
  --logo-filter: invert(1) brightness(0.15);
  --radius: 20px;
  --max: 720px;
  --pad: 16px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --ease: cubic-bezier(0.2, 0.8, 0.2, 1);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #000000;
    --fg: #f5f5f7;
    --muted: #98989d;
    --faint: #636366;
    --line: rgba(255, 255, 255, 0.12);
    --bubble: #2c2c2e;
    --panel: #1c1c1e;
    --accent: #0a84ff;
    --accent-fg: #ffffff;
    --danger: #ff6b5e;
    --logo-filter: none;
  }
}

* { box-sizing: border-box; }
html { height: 100%; -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--fg);
  font: 16px/1.6 var(--font);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
  overflow: hidden;
}
button, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 6px; }
.visually-hidden { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

/* ---------------------------------------------------------------- squelette */
#app {
  height: 100vh;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  min-width: 0;
}

#topbar {
  flex: none;
  display: flex;
  align-items: center;
  gap: 12px;
  height: 52px;
  padding: 0 var(--pad);
  padding-top: env(safe-area-inset-top, 0px);
  border-bottom: 1px solid var(--line);
  background: var(--bg);
  background: color-mix(in srgb, var(--bg) 80%, transparent);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  backdrop-filter: saturate(180%) blur(20px);
  z-index: 2;
}
#topbar .logo { height: 20px; width: auto; display: block; filter: var(--logo-filter); }
#topbar .sep { width: 1px; height: 18px; background: var(--line); }
#topbar .project { margin: 0; font-size: 15px; font-weight: 600; letter-spacing: 0.02em; color: var(--fg); }

#thread {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  scroll-behavior: smooth;
}
#messages, #welcome { width: 100%; max-width: var(--max); margin: 0 auto; padding: 20px var(--pad) 12px; }
#messages { display: flex; flex-direction: column; gap: 22px; }
#messages:empty { padding: 0; }

/* ---------------------------------------------------------------- accueil */
#welcome { padding-top: 12vh; text-align: left; }
#app[data-state="chat"] #welcome { display: none; }
#welcome h2 { margin: 0; font-size: 34px; font-weight: 700; letter-spacing: -0.02em; line-height: 1.15; }
#welcome p { margin: 8px 0 28px; font-size: 19px; color: var(--muted); }
#samples { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 10px; }
.sample {
  width: 100%;
  text-align: left;
  background: var(--panel);
  border: 1px solid transparent;
  border-radius: 16px;
  padding: 12px 16px;
  color: var(--fg);
  line-height: 1.45;
  transition: transform 0.18s var(--ease), background 0.18s;
}
.sample:hover { background: var(--bubble); }
.sample:active { transform: scale(0.985); }

/* ---------------------------------------------------------------- messages */
.msg { min-width: 0; animation: rise 0.36s var(--ease) both; }
.msg-user { display: flex; justify-content: flex-end; }
.msg-user .bubble {
  margin: 0;
  max-width: 85%;
  padding: 11px 16px;
  border-radius: var(--radius);
  border-bottom-right-radius: 6px;
  background: var(--accent);
  color: var(--accent-fg);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}
.msg-assistant { padding: 0 2px; }

.status { display: flex; align-items: center; gap: 10px; margin: 2px 0 0; color: var(--muted); font-size: 15px; min-height: 24px; }
.status[hidden] { display: none; }
.status-text { animation: fade 0.3s var(--ease) both; overflow-wrap: anywhere; }
.dots { display: inline-flex; gap: 4px; }
.dots i { width: 6px; height: 6px; border-radius: 50%; background: var(--faint); animation: pulse 1.4s ease-in-out infinite; }
.dots i:nth-child(2) { animation-delay: 0.2s; }
.dots i:nth-child(3) { animation-delay: 0.4s; }

/* La réponse : du texte, aéré, sans carte. */
.answer { overflow-wrap: anywhere; }
.answer:empty { display: none; }
.answer > :first-child { margin-top: 0; }
.answer > :last-child { margin-bottom: 0; }
.answer p, .answer ul, .answer ol, .answer blockquote { margin: 0 0 12px; }
.answer h2, .answer h3, .answer h4 { margin: 18px 0 8px; font-weight: 600; letter-spacing: -0.01em; line-height: 1.3; }
.answer h2 { font-size: 18px; }
.answer h3 { font-size: 16.5px; }
.answer h4 { font-size: 16px; }
.answer ul, .answer ol { padding-left: 22px; }
.answer li { margin: 4px 0; }
.answer blockquote { padding: 2px 0 2px 14px; border-left: 2px solid var(--line); color: var(--muted); }
.answer code { font-family: var(--mono); font-size: 0.92em; background: var(--panel); padding: 1px 5px; border-radius: 5px; }
.answer hr { border: 0; border-top: 1px solid var(--line); margin: 16px 0; }
.answer .table-wrap { overflow-x: auto; max-width: 100%; margin: 0 0 12px; }
.answer table { border-collapse: collapse; font-size: 15px; min-width: 100%; }
.answer th, .answer td { text-align: left; padding: 6px 10px; border-bottom: 1px solid var(--line); vertical-align: top; }
.answer .refwrap, .sources-body .refwrap { white-space: nowrap; }
.answer .ref {
  font-size: 0.72em;
  line-height: 0;
  vertical-align: super;
  color: var(--accent);
  background: none;
  border: 0;
  padding: 0 2px;
  margin: 0 0 0 1px;
  border-radius: 6px;
  transition: background 0.15s, color 0.15s;
}
.answer .ref:hover, .answer .ref:focus-visible { background: var(--accent); color: var(--accent-fg); outline: none; }
/* Deux appels qui se suivent se lisent « 1,2 », jamais « 12 » (relevé du 2026-09-29 : « 0,5 %2345 »). */
.answer .ref + .ref::before { content: ","; margin-right: 1px; }
.answer .err { color: var(--muted); }
.answer .caret::after { content: ""; display: inline-block; width: 2px; height: 1em; margin-left: 2px; vertical-align: -0.15em; background: var(--faint); animation: blink 1s steps(2) infinite; }

/* « Sources » : une affordance, repliée. */
.sources { margin-top: 12px; }
.sources[hidden] { display: none; }
.sources summary {
  list-style: none;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  user-select: none;
  color: var(--muted);
  font-size: 14px;
  font-weight: 500;
  padding: 6px 12px;
  border-radius: 999px;
  background: var(--panel);
  transition: background 0.18s, color 0.18s;
}
.sources summary::-webkit-details-marker { display: none; }
.sources summary::before { content: ""; width: 6px; height: 6px; border-right: 1.5px solid currentColor; border-bottom: 1.5px solid currentColor; transform: rotate(-45deg); transition: transform 0.2s var(--ease); margin-right: 2px; }
.sources[open] summary::before { transform: rotate(45deg); }
.sources summary:hover { background: var(--bubble); color: var(--fg); }
.sources-count { color: var(--faint); font-variant-numeric: tabular-nums; }
.sources-count:empty { display: none; }
.sources-body { margin-top: 12px; padding: 14px 16px; border-radius: 16px; background: var(--panel); font-size: 15px; line-height: 1.55; color: var(--fg); animation: fade 0.25s var(--ease) both; overflow-wrap: anywhere; }
.sources-body > :first-child { margin-top: 0; }
.sources-body > :last-child { margin-bottom: 0; }
.sources-body p, .sources-body ul, .sources-body ol, .sources-body blockquote { margin: 0 0 10px; }
.sources-body h2, .sources-body h3, .sources-body h4 { margin: 14px 0 6px; font-size: 15px; font-weight: 600; }
.sources-body ul, .sources-body ol { padding-left: 20px; }
.sources-body li { margin: 4px 0; }
.sources-body blockquote { padding-left: 12px; border-left: 2px solid var(--line); color: var(--muted); }
.sources-body .table-wrap { overflow-x: auto; max-width: 100%; }
.sources-body table { border-collapse: collapse; }
.sources-body th, .sources-body td { text-align: left; padding: 4px 8px; border-bottom: 1px solid var(--line); vertical-align: top; }
.sources-body .ref { font-size: 0.75em; vertical-align: super; line-height: 0; color: var(--accent); background: none; border: 0; padding: 0 1px; }
.sources-body .cit { list-style: none; padding: 0; margin: 0; }
.sources-body .cit li { padding: 8px 0; border-top: 1px solid var(--line); margin: 0; }
.sources-body .cit li:first-child { border-top: 0; padding-top: 0; }
.sources-body .cit .n { color: var(--faint); font-variant-numeric: tabular-nums; margin-right: 6px; }
.sources-body .cit .label { font-weight: 600; }
.sources-body .cit .meta { display: block; color: var(--muted); font-size: 13.5px; }
.sources-body .cit q { display: block; margin: 4px 0; color: var(--muted); quotes: "« " " »"; }
.sources-body .cit a { display: inline-block; font-size: 14px; overflow-wrap: anywhere; }
.sources-body .attribution { margin: 10px 0 0; font-size: 12.5px; color: var(--faint); }
/* Les entrées qui ne sont pas des pièces citées (genre ≠ piece) : un intitulé discret, une liste sans numéro. */
.sources-body .sources-group { margin: 14px 0 4px; font-size: 12.5px; font-weight: 600; color: var(--muted); letter-spacing: 0.01em; }
.sources-body .sources-group:first-child { margin-top: 0; }
.sources-body .cit-plan li { padding: 6px 0; }
.sources-body .cit-plan .label { font-weight: 500; }
.sources-body li.hit { animation: hit 1.2s var(--ease); }

/* ---------------------------------------------------------------- saisie */
#composer {
  flex: none;
  display: flex;
  align-items: flex-end;
  gap: 8px;
  width: 100%;
  max-width: var(--max);
  margin: 0 auto;
  padding: 8px var(--pad) 6px;
}
#question {
  flex: 1 1 auto;
  min-width: 0;
  resize: none;
  max-height: 168px;
  padding: 11px 16px;
  border: 1px solid var(--line);
  border-radius: 22px;
  background: var(--panel);
  line-height: 1.45;
  outline: none;
  transition: border-color 0.18s, background 0.18s;
}
#question:focus { border-color: var(--accent); background: var(--bg); }
#question::placeholder { color: var(--faint); }
#send, #stop {
  flex: none;
  width: 44px;
  height: 44px;
  border: 0;
  border-radius: 50%;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: opacity 0.18s, transform 0.18s var(--ease), background 0.18s;
}
#send { background: var(--accent); color: var(--accent-fg); }
#send:disabled { opacity: 0.35; cursor: default; }
#send:not(:disabled):active { transform: scale(0.92); }
#stop { background: var(--fg); color: var(--bg); }
#send[hidden], #stop[hidden] { display: none; }
#foot {
  flex: none;
  display: flex;
  justify-content: center;
  gap: 8px;
  padding: 0 var(--pad) calc(8px + var(--safe-b));
  font-size: 11.5px;
  color: var(--faint);
}
#foot a { color: inherit; }

/* ---------------------------------------------------------------- clé d'accès */
#key-dialog {
  border: 0;
  border-radius: 22px;
  padding: 26px 24px 22px;
  width: min(92vw, 380px);
  background: var(--bg);
  color: var(--fg);
  box-shadow: 0 24px 70px rgba(0, 0, 0, 0.28);
}
#key-dialog::backdrop { background: rgba(0, 0, 0, 0.35); -webkit-backdrop-filter: blur(6px); backdrop-filter: blur(6px); }
#key-dialog h2 { margin: 0 0 6px; font-size: 20px; font-weight: 700; letter-spacing: -0.01em; }
#key-dialog .note { margin: 0 0 16px; color: var(--muted); font-size: 14.5px; }
#key-dialog .note.bad { color: var(--danger); }
#key-input { width: 100%; padding: 11px 14px; border: 1px solid var(--line); border-radius: 12px; background: var(--panel); outline: none; font-family: var(--mono); font-size: 15px; }
#key-input:focus { border-color: var(--accent); }
#key-ok { width: 100%; margin-top: 12px; padding: 12px; border: 0; border-radius: 12px; background: var(--accent); color: var(--accent-fg); font-weight: 600; }
#key-ok:active { opacity: 0.85; }

/* ---------------------------------------------------------------- mouvements */
@keyframes rise { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }
@keyframes pulse { 0%, 80%, 100% { opacity: 0.25; transform: scale(0.85); } 40% { opacity: 1; transform: scale(1); } }
@keyframes blink { to { visibility: hidden; } }
@keyframes hit { from { background: var(--bubble); } to { background: transparent; } }
@media (prefers-reduced-motion: reduce) {
  .msg, .status-text, .sources-body, .sources-body li.hit { animation: none; }
  .dots i { animation: none; opacity: 0.6; }
  #thread { scroll-behavior: auto; }
  .answer .caret::after { animation: none; }
}

/* ---------------------------------------------------------------- écrans larges */
@media (min-width: 720px) {
  :root { --pad: 24px; }
  body { font-size: 16.5px; }
  #topbar { height: 56px; }
  #topbar .logo { height: 22px; }
  #welcome { padding-top: 16vh; }
  #messages { gap: 26px; padding-top: 28px; }
  .msg-user .bubble { max-width: 72%; }
  #composer { padding-bottom: 8px; }
}
