/* KIWI — assistant souverain.
   Encre et papier. Pas de teinte dominante : une couleur vive étalée sur toute une
   interface la date en six mois et fatigue à la lecture. L'accent sert aux actions.
   Clair par défaut, sombre uniquement si l'utilisateur le demande. */

:root {
  /* Le front injecte --accent-light et --accent-dark depuis tenant.yaml.
     `--accent` en dérive : sinon une valeur inline sur :root gagnerait contre
     le bloc sombre. */
  --accent-light: #16181d;
  --accent-dark: #e9eaec;
  --accent: var(--accent-light);
  --on-accent: #ffffff;

  --bg: #ffffff;
  --panel: #fafafa;
  --surface: #ffffff;
  --surface-alt: #f4f4f5;
  --border: #e8e8ea;
  --border-strong: #d6d6da;
  --text: #16181d;
  --text-muted: #6c7280;
  --danger: #b3261e;
  --warn: #b26b00;

  --shadow-sm: 0 1px 2px rgb(16 18 24 / 0.05);
  --shadow: 0 1px 3px rgb(16 18 24 / 0.06), 0 12px 32px -16px rgb(16 18 24 / 0.18);

  --radius: 10px;
  --radius-lg: 16px;

  --font-sans: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --font-mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;

  --measure: 45rem;
  --side-width: 17rem;
}

/* Le sombre ne s'impose pas : il se choisit. Le réglage système ne bascule rien. */
:root[data-theme="dark"] {
  --accent: var(--accent-dark);
  --on-accent: #16181d;

  --bg: #0f1115;
  --panel: #13151a;
  --surface: #181b21;
  --surface-alt: #21242b;
  --border: #262a32;
  --border-strong: #3a3f49;
  --text: #e9eaec;
  --text-muted: #9aa1ac;
  --danger: #f2b8b5;
  --warn: #e8b04b;

  --shadow-sm: 0 1px 2px rgb(0 0 0 / 0.4);
  --shadow: 0 1px 3px rgb(0 0 0 / 0.4), 0 12px 32px -16px rgb(0 0 0 / 0.8);
}

* { box-sizing: border-box; }

/* `hidden` doit gagner : `.app { display: grid }` l'écrasait, et les trois vues
   se superposaient — le formulaire de connexion était enterré sous le chat. */
[hidden] { display: none !important; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-sans);
  font-size: 15px;
  line-height: 1.65;
  -webkit-font-smoothing: antialiased;
}

.view { height: 100dvh; }

/* ─────────────────────────────────────────────────────────── contrôles */

button {
  font: inherit;
  cursor: pointer;
  border-radius: var(--radius);
  border: 1px solid var(--border-strong);
  background: var(--surface);
  color: var(--text);
  padding: 0.6rem 0.9rem;
  transition: background 0.12s ease, border-color 0.12s ease, opacity 0.12s ease;
}

button:hover:not(:disabled) { background: var(--surface-alt); }
button:disabled { opacity: 0.4; cursor: not-allowed; }

button.primary {
  background: var(--accent);
  border-color: transparent;
  color: var(--on-accent);
  font-weight: 550;
}

button.primary:hover:not(:disabled) { opacity: 0.86; background: var(--accent); }

button.subtle {
  background: none;
  border-color: transparent;
  color: var(--text-muted);
}

button.subtle:hover:not(:disabled) { background: var(--surface-alt); color: var(--text); }

/* Boutons de barre : 36 px de cible, icône SVG à 18 px. Un glyphe ◐ de 12 px
   n'est ni cliquable ni lisible. */
.tool {
  width: 2.25rem;
  height: 2.25rem;
  padding: 0;
  display: grid;
  place-items: center;
  border: none;
  background: none;
  border-radius: 9px;
  color: var(--text-muted);
}

.tool:hover:not(:disabled) { background: var(--surface-alt); color: var(--text); }
.tool svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 1.6; stroke-linecap: round; stroke-linejoin: round; }

.account {
  display: flex;
  align-items: center;
  gap: 0.45rem;
  height: 2.25rem;
  padding: 0 0.7rem;
  font-size: 0.875rem;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface);
  color: var(--text);
}

.account .avatar {
  width: 1.35rem;
  height: 1.35rem;
  display: grid;
  place-items: center;
  border-radius: 50%;
  background: var(--accent);
  color: var(--on-accent);
  font-size: 0.68rem;
  font-weight: 600;
  text-transform: uppercase;
}

:where(button, input, textarea, a):focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* ────────────────────────────────────────────────────────── connexion */

.auth {
  display: grid;
  place-content: center;
  padding: 2rem 1.25rem;
}

.auth-card {
  width: min(24rem, 100%);
  display: flex;
  flex-direction: column;
  gap: 1.1rem;
}

.brand {
  text-align: center;
  margin-bottom: 0.5rem;
}

.wordmark {
  margin: 0;
  font-size: 1.6rem;
  font-weight: 600;
  letter-spacing: 0.16em;
  color: var(--text);
}

.tagline {
  margin: 0.5rem 0 0;
  font-size: 0.9rem;
  line-height: 1.5;
  color: var(--text-muted);
  text-wrap: pretty;
}

.auth-form {
  display: flex;
  flex-direction: column;
  gap: 1rem;
  padding: 1.75rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow);
}

.auth-form h2 {
  margin: 0;
  font-size: 1rem;
  font-weight: 600;
}

/* `display: block` et non `flex column` : sinon le <span class="hint"> tombe à la
   ligne au lieu de suivre le libellé. */
.auth-form label {
  display: block;
  font-size: 0.82rem;
  font-weight: 550;
  color: var(--text-muted);
}

.auth-form label input { margin-top: 0.35rem; }

.auth-form input {
  width: 100%;
  padding: 0.6rem 0.75rem;
  font: inherit;
  font-size: 0.95rem;
  font-weight: 400;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--border-strong);
  border-radius: 9px;
}

.auth-actions {
  display: flex;
  gap: 0.5rem;
}

.auth-actions .primary { flex: 1; }

.hint { color: var(--text-muted); font-weight: 400; opacity: 0.8; }
.error { margin: 0; font-size: 0.85rem; color: var(--danger); }
.notice { margin: 0; font-size: 0.85rem; color: var(--text-muted); }

/* ──────────────────────────────────────────────────────────── modale */

.modal {
  border: none;
  padding: 0;
  background: none;
  max-width: min(24rem, calc(100vw - 2rem));
  margin: auto;
}

.modal::backdrop { background: rgb(10 12 16 / 0.45); }

.modal .auth-form { margin: 0; }

.modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
}

.modal-head h2 {
  margin: 0;
  font-size: 1rem;
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* ------------------------------------------------------------- aperçu */

.modal-wide { max-width: min(56rem, calc(100vw - 2rem)); }

.preview {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  padding: 1.1rem 1.25rem 1.25rem;
  /* Un <dialog> s'ajuste à son contenu : sans largeur explicite, l'iframe en
     `width: 100%` d'un parent auto ne pousse rien et la modale reste étroite. */
  width: min(56rem, calc(100vw - 2rem));
  height: min(80vh, 48rem);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow);
}

.preview-actions { display: flex; gap: 0.1rem; flex: none; }

.preview-body {
  flex: 1;
  min-height: 0;
  border: 1px solid var(--border);
  border-radius: 10px;
  overflow: auto;
  background: var(--bg);
}

.preview-body iframe {
  width: 100%;
  height: 100%;
  border: none;
  display: block;
}

.preview-body pre {
  margin: 0;
  padding: 1rem;
  font-family: var(--font-mono);
  font-size: 0.82rem;
  line-height: 1.6;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.preview-body .error { padding: 1rem; }

/* ───────────────────────────────────────────────────────── application */

.app {
  display: grid;
  grid-template-columns: var(--side-width) minmax(0, 1fr);
}

/* -------------------------------------------------- panneau latéral */

.side {
  display: flex;
  flex-direction: column;
  min-height: 0;
  background: var(--panel);
  border-right: 1px solid var(--border);
}

.side.dragover { outline: 2px dashed var(--border-strong); outline-offset: -8px; }

.side-top { padding: 0.7rem 0.7rem 0.5rem; }

.new-chat {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.55rem 0.75rem;
  font-size: 0.88rem;
  font-weight: 550;
  border-radius: 10px;
  background: var(--surface);
}

.new-chat svg { width: 16px; height: 16px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; }

/* Deux onglets : l'historique des discussions, et les documents. */
.tabs {
  display: flex;
  gap: 0.15rem;
  padding: 0 0.7rem;
  border-bottom: 1px solid var(--border);
}

.tabs button {
  flex: 1;
  padding: 0.45rem 0.5rem 0.55rem;
  font-size: 0.82rem;
  font-weight: 550;
  color: var(--text-muted);
  background: none;
  border: none;
  border-bottom: 2px solid transparent;
  border-radius: 0;
  margin-bottom: -1px;
}

.tabs button:hover { color: var(--text); background: none; }

.tabs button[aria-selected="true"] {
  color: var(--text);
  border-bottom-color: var(--accent);
}

.pane {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  overflow-y: auto;
}

.search { padding: 0.6rem 0.7rem 0.2rem; }

.search input {
  width: 100%;
  padding: 0.45rem 0.65rem;
  font: inherit;
  font-size: 0.83rem;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 9px;
}

.search input::placeholder { color: var(--text-muted); }

.list { list-style: none; margin: 0; padding: 0.5rem; flex: 1; }
.list li { position: relative; }

.pane-empty,
.pane-note {
  margin: 0;
  padding: 0.5rem 1rem 1rem;
  font-size: 0.75rem;
  line-height: 1.5;
  color: var(--text-muted);
}

.pane-note { border-top: 1px solid var(--border); padding-top: 0.75rem; }
.pane-note strong { color: var(--text); font-weight: 600; }

/* -------------------------------------------------------- discussions */

.chat-item {
  width: 100%;
  text-align: left;
  padding: 0.5rem 2rem 0.5rem 0.65rem;
  border: 1px solid transparent;
  background: none;
  border-radius: 9px;
  color: var(--text);
  font-size: 0.86rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chat-item:hover { background: var(--surface-alt); }

.chat-item[aria-current="true"] {
  background: var(--surface);
  border-color: var(--border-strong);
  box-shadow: var(--shadow-sm);
}

.chat-del {
  position: absolute;
  top: 50%;
  right: 0.25rem;
  transform: translateY(-50%);
  width: 1.7rem;
  height: 1.7rem;
  opacity: 0;
}

.list li:hover .chat-del,
.chat-del:focus-visible { opacity: 1; }

/* ----------------------------------------------------------- documents */

.doc {
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 0.15rem;
  text-align: left;
  padding: 0.5rem 0.6rem;
  margin-bottom: 0.1rem;
  border: 1px solid transparent;
  background: none;
  border-radius: 9px;
  color: var(--text);
}

.doc:hover:not(:disabled) { background: var(--surface-alt); }

.doc[aria-pressed="true"] {
  background: var(--surface);
  border-color: var(--border-strong);
  box-shadow: var(--shadow-sm);
}

.doc-name { font-size: 0.86rem; line-height: 1.35; overflow-wrap: anywhere; }

.doc-meta {
  font-size: 0.72rem;
  color: var(--text-muted);
  display: flex;
  align-items: center;
  gap: 0.35rem;
}

.dot { width: 6px; height: 6px; border-radius: 50%; background: var(--text-muted); flex: none; }
.dot.indexed { background: #1a7f47; }
.dot.quarantine { background: var(--warn); }
.dot.error { background: var(--danger); }

/* Un document en quarantaine reste consultable — c'est ce qu'un admin doit lire
   avant de l'approuver. Il n'est simplement pas joignable à une question. */
.doc[data-status="quarantine"] .doc-name,
.doc[data-status="error"] .doc-name { color: var(--text-muted); }

/* ---------------------------------------------------------------- chat */

.chat {
  display: flex;
  flex-direction: column;
  min-width: 0;
  background: var(--bg);
}

.bar {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  padding: 0.6rem 0.9rem;
  border-bottom: 1px solid var(--border);
}

.bar h1 {
  margin: 0;
  flex: 1;
  font-size: 0.95rem;
  font-weight: 600;
  letter-spacing: 0.14em;
}

.bar nav { display: flex; align-items: center; gap: 0.15rem; }
.burger { display: none; }
.scrim { display: none; }

.thread {
  flex: 1;
  overflow-y: auto;
  padding: 1.5rem 1.25rem 0.5rem;
  scroll-behavior: smooth;
}

.thread > * { max-width: var(--measure); margin-inline: auto; }

/* ------------------------------------------------------------ messages */

.msg { margin-bottom: 1.6rem; }

.msg[data-role="user"] { display: flex; justify-content: flex-end; }

.msg[data-role="user"] .bubble {
  background: var(--surface-alt);
  color: var(--text);
  padding: 0.6rem 0.95rem;
  border-radius: var(--radius-lg);
  border-bottom-right-radius: 6px;
  max-width: 80%;
  overflow-wrap: anywhere;
}

/* L'assistant ne parle pas dans une bulle : sa réponse est le contenu de la page.
   Une bulle autour d'un long texte le fait ressembler à une citation. */
.msg[data-role="assistant"] .bubble { overflow-wrap: anywhere; }

.msg[data-role="assistant"]::before {
  content: attr(data-author);
  display: block;
  font-size: 0.7rem;
  font-weight: 600;
  letter-spacing: 0.12em;
  color: var(--text-muted);
  margin-bottom: 0.45rem;
}

.bubble > :first-child { margin-top: 0; }
.bubble > :last-child { margin-bottom: 0; }

.bubble pre {
  background: var(--surface-alt);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 0.75rem;
  overflow-x: auto;
  font-family: var(--font-mono);
  font-size: 0.85em;
}

.bubble ul { margin: 0.5rem 0; padding-left: 1.15rem; }
.bubble li { margin: 0.2rem 0; }
.bubble strong { font-weight: 620; }

.bubble code {
  background: var(--surface-alt);
  border-radius: 5px;
  padding: 0.1em 0.32em;
  font-family: var(--font-mono);
  font-size: 0.88em;
}

.sources { margin-top: 0.7rem; display: flex; flex-wrap: wrap; gap: 0.35rem; }

.source {
  font-size: 0.72rem;
  color: var(--text-muted);
  background: var(--surface-alt);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 0.15rem 0.6rem;
}

/* Trois points, pas une phrase. Un libellé qui change tout seul ment sur ce que
   fait la machine. */
.thinking { display: flex; gap: 0.28rem; padding: 0.35rem 0; }

.thinking span {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--text-muted);
  animation: bounce 1.2s ease-in-out infinite;
}

.thinking span:nth-child(2) { animation-delay: 0.15s; }
.thinking span:nth-child(3) { animation-delay: 0.3s; }

@keyframes bounce {
  0%, 60%, 100% { opacity: 0.25; transform: translateY(0); }
  30% { opacity: 1; transform: translateY(-3px); }
}

.feedback {
  display: flex;
  gap: 0.1rem;
  margin-top: 0.6rem;
  opacity: 0;
  transition: opacity 0.15s ease;
}

.msg:hover .feedback,
.feedback:focus-within,
.feedback:has(.chosen) { opacity: 1; }

.feedback .tool { width: 1.9rem; height: 1.9rem; }
.feedback .tool svg { width: 15px; height: 15px; }
.feedback .tool.chosen { color: var(--text); }
.feedback .tool:disabled { opacity: 1; }
.feedback .tool:disabled:not(.chosen) { opacity: 0.28; }

.streaming .bubble::after {
  content: "";
  display: inline-block;
  width: 0.5ch;
  height: 1em;
  vertical-align: text-bottom;
  background: currentColor;
  opacity: 0.55;
  animation: blink 1.1s steps(2, start) infinite;
}

@keyframes blink { to { visibility: hidden; } }

@media (prefers-reduced-motion: reduce) {
  .streaming .bubble::after,
  .thinking span { animation: none; }
  .thread { scroll-behavior: auto; }
}

/* ----------------------------------------------------------- composeur */

.composer { padding: 0.5rem 1.25rem 1.25rem; }
.composer form, .scope { max-width: var(--measure); margin-inline: auto; }

.scope {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  margin-bottom: 0.5rem;
  font-size: 0.78rem;
  color: var(--text-muted);
  width: fit-content;
  padding: 0.15rem 0.3rem 0.15rem 0.7rem;
  background: var(--surface-alt);
  border: 1px solid var(--border);
  border-radius: 999px;
}

.scope strong { color: var(--text); font-weight: 550; }
.scope .tool { width: 1.5rem; height: 1.5rem; }
.scope .tool svg { width: 13px; height: 13px; }

.upload-status {
  max-width: var(--measure);
  margin: 0 auto 0.5rem;
  font-size: 0.78rem;
  color: var(--text-muted);
  background: var(--surface-alt);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 0.4rem 0.7rem;
}

.composer form {
  display: flex;
  gap: 0.35rem;
  align-items: flex-end;
  padding: 0.35rem;
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow);
  transition: border-color 0.12s ease;
}

.composer form:focus-within { border-color: var(--accent); }

.attach-wrap { position: relative; flex: none; }
.attach { margin-bottom: 0; }

/* Menu du trombone : ouvert vers le haut, le composeur est en bas de l'écran. */
.attach-menu {
  position: absolute;
  bottom: calc(100% + 0.5rem);
  left: 0;
  z-index: 3;
  width: 17rem;
  max-height: 20rem;
  overflow-y: auto;
  padding: 0.35rem;
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: 12px;
  box-shadow: var(--shadow);
}

.attach-menu > button,
.attach-menu li button {
  width: 100%;
  text-align: left;
  padding: 0.45rem 0.6rem;
  font-size: 0.85rem;
  border: none;
  background: none;
  border-radius: 8px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.attach-menu > button:hover,
.attach-menu li button:hover { background: var(--surface-alt); }

.attach-menu ul { list-style: none; margin: 0; padding: 0; }

.attach-title,
.attach-empty {
  margin: 0.35rem 0 0.2rem;
  padding: 0 0.6rem;
  font-size: 0.72rem;
  font-weight: 600;
  letter-spacing: 0.03em;
  color: var(--text-muted);
}

.attach-empty { font-weight: 400; padding-bottom: 0.4rem; }

.composer textarea {
  flex: 1;
  resize: none;
  min-height: 2.3rem;
  max-height: 12rem;
  padding: 0.5rem 0.25rem;
  font: inherit;
  color: inherit;
  background: none;
  border: none;
}

.composer textarea:focus-visible { outline: none; }

.send {
  flex: none;
  width: 2.3rem;
  height: 2.3rem;
  padding: 0;
  display: grid;
  place-items: center;
  border: none;
  border-radius: 50%;
  background: var(--accent);
  color: var(--on-accent);
}

.send svg { width: 17px; height: 17px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.send:hover:not(:disabled) { background: var(--accent); opacity: 0.86; }

.hints {
  max-width: var(--measure);
  margin: 0.5rem auto 0;
  font-size: 0.72rem;
  color: var(--text-muted);
  text-align: center;
}

/* ---------------------------------------------------------- accueil */

.welcome { margin-top: 14vh; text-align: center; }

.welcome p {
  font-size: 1.45rem;
  font-weight: 600;
  letter-spacing: -0.01em;
  color: var(--text);
  margin: 0;
}

.welcome-logo {
  display: block;
  width: 96px;
  height: auto;
  margin: 1.75rem auto 0;
  opacity: 0.9;
}

/* Le corps du kiwi est gris moyen : sur fond sombre il s'éteint. On l'éclaircit
   sans toucher au bleu et au rouge de l'aile. */
:root[data-theme="dark"] .welcome-logo { filter: brightness(1.55); opacity: 1; }

.suggestions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-top: 1.5rem;
  justify-content: center;
}

.suggestions button {
  font-size: 0.85rem;
  padding: 0.45rem 0.85rem;
  border-radius: 999px;
  border-color: var(--border);
  color: var(--text-muted);
}

/* ------------------------------------------------------------- mobile */

@media (max-width: 52rem) {
  .app { grid-template-columns: minmax(0, 1fr); }
  .burger { display: grid; }

  .side {
    position: fixed;
    inset: 0 auto 0 0;
    width: min(var(--side-width), 82vw);
    z-index: 2;
    transform: translateX(-100%);
    transition: transform 0.18s ease;
  }

  .side.open { transform: none; }

  .scrim {
    display: block;
    position: fixed;
    inset: 0;
    z-index: 1;
    background: rgb(0 0 0 / 0.4);
  }

  .thread, .composer { padding-inline: 1rem; }
}

@media (prefers-reduced-motion: reduce) {
  .side { transition: none; }
}
