@import url('https://fonts.cdnfonts.com/css/open-dyslexic');

/* === CSS Variables & Reset === */
:root {
  --ansu-primary: #2563eb;
  --ansu-primary-light: #3b82f6;
  --ansu-bg: #9ab0bc;
  --ansu-chat-bg: #ffffff;
  --ansu-bubble-user: #2563eb;
  --ansu-bubble-user-text: #ffffff;
  --ansu-bubble-assistant: #f7f5f2;
  --ansu-bubble-assistant-text: #2c2c2c;
  --ansu-bubble-assistant-border: #e8e4df;
  --ansu-bubble-user-border: rgba(0, 0, 0, 0.15);
  --ansu-text-main: #37352f;
  --ansu-input-border: #d4d0ca;
  --ansu-header-text: #ffffff;
  --ansu-frost-border: rgba(37, 99, 235, 0.16);

  /* Couleur du texte posé SUR --ansu-primary. Chaque thème qui éclaircit son
     primaire doit la redéfinir, sinon le libellé blanc d'un bouton plein
     devient illisible (le cas de nuit, dont le primaire est un bleu clair). */
  --ansu-primary-contrast: #ffffff;

  /* Couleurs sémantiques : elles ne suivent PAS le thème, parce qu'un danger
     doit rester rouge et une réussite verte quel que soit l'habillage. Les
     nommer évite qu'elles se mélangent aux couleurs de marque, qui elles
     doivent suivre le thème. */
  --ansu-danger: #ef4444;
  --ansu-danger-strong: #dc2626;
  --ansu-success: #16a34a;

  /* Texte secondaire : dérivé du texte principal, donc juste sur les quatre
     thèmes là où un gris fixe ne l'était que sur fond clair. */
  --ansu-text-soft: color-mix(in srgb, var(--ansu-text-main) 62%, transparent);

  /* Spacing scale (4px grid) */
  --space-1: 4px;
  --space-2: 8px;
  --space-3: 12px;
  --space-4: 16px;
  --space-5: 20px;
  --space-6: 24px;
  --space-8: 32px;

  /* Radius scale */
  --radius-xs: 4px;
  --radius-sm: 8px;
  --radius-md: 12px;
  --radius-lg: 16px;
  --radius-xl: 24px;
  --radius-full: 50%;

  /* Font scale */
  --text-xs: 0.75rem;
  --text-sm: 0.875rem;
  --text-base: 1rem;
  --text-lg: 1.125rem;
  --text-xl: 1.25rem;
  --text-2xl: 1.5rem;

  /* Elevation */
  --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.04);
  --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.08);
  --shadow-lg: 0 10px 30px rgba(0, 0, 0, 0.12);

  /* Dys settings variables (defaults) */
  --dys-font: system-ui, -apple-system, sans-serif;
  --dys-spacing: normal;
  --dys-word-spacing: normal;
  --dys-line-height: 1.5;
  --dys-font-size: 16px;
}

* {
  box-sizing: border-box;
}

/* Scrollbars */
::-webkit-scrollbar {
  width: 8px;
  height: 8px;
}

::-webkit-scrollbar-track {
  background: transparent;
}

::-webkit-scrollbar-thumb {
  background: color-mix(in srgb, var(--ansu-primary) 50%, transparent);
  border-radius: 4px;
}

::-webkit-scrollbar-thumb:hover {
  background: var(--ansu-primary);
}

html,
body {
  margin: 0;
  padding: 0;
  height: 100%;
  background-color: var(--ansu-bg);
  font-family: var(--dys-font);
  font-size: var(--dys-font-size);
  letter-spacing: var(--dys-spacing);
  word-spacing: var(--dys-word-spacing);
  line-height: var(--dys-line-height);
  color: var(--ansu-text-main);
  display: flex;
  justify-content: center;
}

#ansu-app {
  width: 100%;
  max-width: min(900px, 95%);
  height: 100%;
  background: var(--ansu-chat-bg);
  display: flex;
  flex-direction: row;
  box-shadow: 0 0 20px rgba(0, 0, 0, 0.05);
  position: relative;
  overflow: hidden;
}

#main-wrapper {
  flex: 1;
  display: flex;
  flex-direction: column;
  position: relative;
  min-width: 0;
  min-height: 0;
}

/* === Themes === */
html[data-theme="nuit"] {
  --ansu-bg: #000000;
  --ansu-chat-bg: #000000;
  --ansu-primary: #3b9eff;
  --ansu-primary-light: #60b4ff;
  --ansu-frost-border: rgba(214, 235, 253, 0.22);
  --ansu-bubble-user: rgba(59, 158, 255, 0.22);
  --ansu-bubble-user-text: #f0f0f0;
  --ansu-bubble-user-border: rgba(59, 158, 255, 0.35);
  --ansu-bubble-assistant: rgba(255, 255, 255, 0.07);
  --ansu-bubble-assistant-text: #e2e8f0;
  --ansu-bubble-assistant-border: rgba(214, 235, 253, 0.25);
  --ansu-text-main: #f0f0f0;
  --ansu-input-border: rgba(214, 235, 253, 0.22);
  --ansu-header-text: #f0f0f0;
  /* Le primaire de nuit est un bleu clair : un libellé blanc dessus tombe à
     2,4:1. Le texte des boutons pleins passe donc en sombre. */
  --ansu-primary-contrast: #0a1929;
}

html[data-theme="nuit"] #ansu-app {
  box-shadow: none;
}

html[data-theme="abeille"] {
  --ansu-primary: #d97706;
  --ansu-primary-light: #f59e0b;
  --ansu-bg: #fffbeb;
  --ansu-chat-bg: #fff8f0;
  --ansu-bubble-user: #d97706;
  --ansu-bubble-assistant: #fef3c7;
  --ansu-bubble-assistant-text: #78350f;
  --ansu-bubble-assistant-border: #fde68a;
  --ansu-bubble-user-border: rgba(0, 0, 0, 0.15);
}

html[data-theme="nature"] {
  --ansu-primary: #166534;
  --ansu-primary-light: #22c55e;
  --ansu-bg: #f0fdf4;
  --ansu-chat-bg: #f8fafc;
  --ansu-bubble-user: #166534;
  --ansu-bubble-assistant: #f0f7f2;
  --ansu-bubble-assistant-text: #14532d;
  --ansu-bubble-assistant-border: #c8e6cf;
  --ansu-bubble-user-border: rgba(0, 0, 0, 0.15);
}

/* === Dys modes === */
html[data-dys-font="true"] {
  --dys-font: 'OpenDyslexic', 'Lexend', 'Comic Sans MS', sans-serif;
  --dys-font-size: 14px;
}

html[data-dys-spacing="true"] {
  --dys-spacing: 0.08em;
  --dys-word-spacing: 0.16em;
  --dys-line-height: 1.75;
}

html[data-dys-size="large"] {
  --dys-font-size: 20px;
}

html[data-dys-size="large"] #sidebar {
  font-size: 16px;
}

html[data-dys-size="xlarge"] {
  --dys-font-size: 24px;
}

html[data-dys-size="xlarge"] #sidebar {
  font-size: 18px;
}

html[data-dys-contrast="true"] {
  --ansu-bg: #111111;
  --ansu-chat-bg: #fffdd0;
  --ansu-text-main: #000000;
  --ansu-bubble-assistant: #fffdd0;
  --ansu-bubble-assistant-border: #222222;
  --ansu-bubble-assistant-text: #000000;
  --ansu-bubble-user: #000000;
  --ansu-bubble-user-text: #fffdd0;
  --ansu-bubble-user-border: transparent;
  --ansu-primary: #000000;
  --ansu-header-text: #fffdd0;
  --ansu-primary-contrast: #fffdd0;
}

html[data-dys-contrast="true"] .msg.assistant .msg-bubble {
  border-left: 4px solid #000000;
}

html[data-dys-contrast="true"] .msg.user .msg-bubble {
  border-right: 4px solid #fffdd0;
}

html[data-dys-contrast="true"] button:hover,
html[data-dys-contrast="true"] button:focus-visible,
html[data-dys-contrast="true"] .icon-btn:hover,
html[data-dys-contrast="true"] .icon-btn:focus-visible,
html[data-dys-contrast="true"] .mem-btn:hover,
html[data-dys-contrast="true"] .btn-primary:hover,
html[data-dys-contrast="true"] #send-btn:hover {
  border: 3px solid #ffffff !important;
  transform: scale(1.05);
  box-shadow: 0 0 0 2px #fffdd0;
}

/* === Animations === */
@keyframes wave-animation {
  0%   { transform: rotate(0deg); }
  10%  { transform: rotate(14deg); }
  20%  { transform: rotate(-8deg); }
  30%  { transform: rotate(14deg); }
  40%  { transform: rotate(-4deg); }
  50%  { transform: rotate(10deg); }
  60%  { transform: rotate(0deg); }
  100% { transform: rotate(0deg); }
}

.wave-emoji {
  display: inline-block;
  animation: wave-animation 2.5s infinite;
  transform-origin: 70% 70%;
  margin-right: 4px;
}

@keyframes popIn {
  0%   { transform: scale(0.5); opacity: 0; }
  60%  { transform: scale(1.2); opacity: 1; }
  100% { transform: scale(1); opacity: 1; }
}

.status-icon {
  display: inline-block;
  animation: popIn 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275) both;
  margin-right: 4px;
  vertical-align: middle;
}

@keyframes fadeIn {
  from { opacity: 0; }
  to   { opacity: 1; }
}

/* === Accessibility - région d'annonce lecteur d'écran (invisible, hors flux) === */
.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

/* === Accessibility - Focus visible === */
*:focus-visible {
  outline: 2px solid var(--ansu-primary);
  outline-offset: 2px;
  border-radius: 4px;
}

button:focus-visible {
  outline-offset: 1px;
}

/* === Streaming Cursor === */
.streaming-cursor {
  display: inline;
  animation: blink 0.8s step-end infinite;
  color: var(--ansu-primary);
  font-weight: bold;
}

@keyframes blink {
  0%, 100% { opacity: 1; }
  50%       { opacity: 0; }
}

/* === Dynamic Viewport Height fix for mobile browsers === */
@supports (height: 100dvh) {
  html, body {
    height: 100dvh;
  }
}

/* === Toast === */
#ansu-toast {
  position: fixed;
  top: 20px;
  right: 20px;
  background: var(--ansu-text-main);
  color: var(--ansu-chat-bg);
  padding: 10px 18px;
  border-radius: var(--radius-sm);
  font-size: var(--text-sm);
  z-index: 9999;
  max-width: 90vw;
  text-align: center;
  transition: opacity 0.3s;
  pointer-events: none;
  opacity: 0;
  box-shadow: var(--shadow-md);
}

@media (max-width: 640px) {
  #ansu-toast {
    top: auto;
    bottom: 90px;
    left: 50%;
    right: auto;
    transform: translateX(-50%);
  }
}

/* === Respect des préférences de réduction de mouvement === */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
}

/* === Widget URL (page pleine page) — force override CSS de la page hôte Moodle === */
html,
body {
  height: 100vh !important;
  height: 100dvh !important;
  margin: 0 !important;
  padding: 0 !important;
  overflow: hidden !important;
  display: block !important;
  width: 100% !important;
}

/* Fond perdu — thème par défaut */
html,
body {
  background-color: var(--ansu-bg) !important;
}

html[data-theme="nuit"],
html[data-theme="nuit"] body {
  background-color: #000000 !important;
}

html[data-theme="abeille"],
html[data-theme="abeille"] body {
  background-color: #c4900a !important;
}

html[data-theme="nature"],
html[data-theme="nature"] body {
  background-color: #9cbeaa !important;
}

#ansu-app {
  max-width: 900px !important;
  width: 100% !important;
  height: 100vh !important;
  height: 100dvh !important;
  box-shadow: none !important;
  display: flex !important;
  flex-direction: row !important;
  margin-left: auto !important;
  margin-right: auto !important;
  background: var(--ansu-chat-bg);
  overflow: hidden !important;
}

#main-wrapper {
  min-height: 0 !important;
  min-width: 0 !important;
  width: 100% !important;
}

#chat-area {
  min-height: 0 !important;
  min-width: 0 !important;
  width: 100% !important;
  overflow-x: hidden !important;
}
