/* ───────── IA Assistant Drawer (carte) ───────── */

/* Floating button in bottom area */
.ia-assistant-fab {
  position: fixed;
  bottom: 64px;
  right: 14px;
  z-index: 9998;
  width: 48px;
  height: 48px;
  border-radius: 50%;
  background: var(--green-700);
  color: #fff;
  border: 2px solid rgba(255,255,255,.2);
  box-shadow: var(--shadow-lg);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.4rem;
  transition: all .3s cubic-bezier(.34,1.56,.64,1);
}
.ia-assistant-fab:hover {
  transform: scale(1.12);
  background: var(--green-600);
  box-shadow: 0 6px 24px rgba(26,74,46,.3);
}
.ia-assistant-fab:active {
  transform: scale(.95);
}
.ia-assistant-fab.active {
  right: 320px;
}

/* Drawer overlay */
.ia-drawer-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 9995;
  background: rgba(0,0,0,.3);
  display: none;
  opacity: 0;
  transition: opacity .3s ease;
}
.ia-drawer-overlay.open {
  display: block;
  opacity: 1;
}

/* Drawer panel */
.ia-drawer {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  max-height: 60vh;
  max-height: 60dvh; /* mobile: suit le clavier (dvh) */
  z-index: 9996;
  background: #fff;
  border-radius: var(--radius-lg) var(--radius-lg) 0 0;
  box-shadow: 0 -8px 40px rgba(0,0,0,.15);
  transform: translateY(100%);
  transition: transform .35s cubic-bezier(.22,1,.36,1);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  overscroll-behavior: contain;
}
.ia-drawer.open {
  transform: translateY(0);
}
.ia-drawer::before {
  content: '';
  display: block;
  width: 36px;
  height: 4px;
  border-radius: 2px;
  background: var(--stone-300);
  margin: 8px auto 0;
  flex-shrink: 0;
}

.ia-drawer-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 16px 0;
  flex-shrink: 0;
}
.ia-drawer-header h3 {
  font-family: var(--font-display);
  font-size: .95rem;
  color: var(--green-800);
  margin: 0;
}
.ia-drawer-close {
  background: none;
  border: none;
  font-size: 1.2rem;
  cursor: pointer;
  color: var(--text-muted);
  padding: 4px 8px;
  border-radius: 6px;
  transition: all .15s;
}
.ia-drawer-close:hover {
  background: var(--stone-100);
  color: var(--text-primary);
}

.ia-drawer-body {
  flex: 1;
  overflow-y: auto;
  padding: 8px 16px 16px;
}

.ia-search-bar {
  display: flex;
  gap: 6px;
  margin-bottom: 10px;
}
.ia-search-bar input {
  flex: 1;
  border: 2px solid var(--stone-200);
  border-radius: 20px;
  padding: 8px 14px;
  font-size: .82rem;
  font-family: var(--font-body);
  outline: none;
  transition: border-color .2s;
}
.ia-search-bar input:focus {
  border-color: var(--green-500);
  box-shadow: 0 0 0 3px rgba(61,161,92,.12);
}
.ia-search-bar button {
  background: var(--green-700);
  color: #fff;
  border: none;
  border-radius: 20px;
  padding: 8px 16px;
  font-size: .78rem;
  font-weight: 600;
  cursor: pointer;
  transition: all .2s;
  font-family: var(--font-body);
  white-space: nowrap;
}
.ia-search-bar button:hover {
  background: var(--green-600);
}
.ia-search-bar button:disabled {
  opacity: .5;
  cursor: not-allowed;
}

.ia-drawer-result {
  font-size: .82rem;
  line-height: 1.6;
  color: var(--text-secondary);
}
.ia-drawer-result .ia-result-title {
  font-weight: 600;
  color: var(--green-800);
  margin-bottom: 4px;
}
.ia-drawer-result .ia-result-item {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 4px 8px;
  margin: 3px 0;
  background: var(--green-50);
  border-radius: 8px;
  font-size: .78rem;
  font-weight: 500;
  color: var(--green-800);
  cursor: pointer;
  transition: all .15s;
}
.ia-drawer-result .ia-result-item:hover {
  background: var(--green-100);
  transform: translateX(2px);
}

.ia-drawer-loading {
  text-align: center;
  padding: 1rem;
  color: var(--text-muted);
  font-size: .78rem;
  font-style: italic;
  animation: pulse 1s infinite;
}

.ia-drawer-error {
  background: #fef2f2;
  color: #dc2626;
  padding: 8px 12px;
  border-radius: 8px;
  font-size: .78rem;
  margin-top: 6px;
}

/* Desktop: drawer slides from left instead */
@media(min-width: 769px) {
  .ia-drawer {
    bottom: auto;
    left: 0;
    top: 0;
    width: 340px;
    max-height: 100vh;
    max-height: 100dvh;
    border-radius: 0 var(--radius-lg) var(--radius-lg) 0;
    transform: translateX(-100%);
  }
  .ia-drawer.open {
    transform: translateX(0);
  }
  .ia-drawer::before {
    display: none;
  }
  .ia-assistant-fab {
    bottom: 16px;
    right: 16px;
  }
  .ia-assistant-fab.active {
    right: 356px;
  }
}

/* Dark mode */
@media(prefers-color-scheme: dark) {
  body.dark-auto .ia-drawer {
    background: #1a1a2e;
  }
  body.dark-auto .ia-drawer-header h3 {
    color: var(--green-100);
  }
  body.dark-auto .ia-drawer-close:hover {
    background: #222240;
  }
  body.dark-auto .ia-search-bar input {
    background: #222240;
    color: #e0e0e8;
    border-color: #333350;
  }
  body.dark-auto .ia-drawer-result {
    color: #a0a0b8;
  }
  body.dark-auto .ia-drawer-result .ia-result-title {
    color: var(--green-100);
  }
  body.dark-auto .ia-drawer-result .ia-result-item {
    background: #0d2818;
    color: var(--green-100);
  }
  body.dark-auto .ia-drawer-error {
    background: #2a1a1a;
  }
}

/* ───────── Historique conversation ───────── */
.ia-messages {
  flex: 1;
  overflow-y: auto;
  padding: 0.5rem;
  min-height: 60px;
  max-height: calc(70vh - 120px);
  max-height: calc(70dvh - 120px);
  overscroll-behavior: contain;
}

.ia-msg-placeholder {
  color: var(--text-muted);
  font-size: 0.78rem;
  text-align: center;
  padding: 1rem 0.5rem;
}

.ia-msg {
  margin-bottom: 0.5rem;
  padding: 0.5rem 0.7rem;
  border-radius: 12px;
  font-size: 0.82rem;
  line-height: 1.5;
  max-width: 85%;
  animation: fadeSlideIn 0.2s ease;
  word-wrap: break-word;
}

.ia-msg-user {
  background: var(--green-700);
  color: #fff;
  margin-left: auto;
  border-bottom-right-radius: 4px;
}

.ia-msg-assistant {
  background: var(--stone-100);
  color: var(--text-primary);
  border-bottom-left-radius: 4px;
  border: 1px solid var(--stone-200);
}

.ia-msg-assistant strong {
  color: var(--green-800);
}

/* ───────── Input bar en bas ───────── */
.ia-input-bar {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 0.5rem;
  padding-bottom: calc(0.5rem + env(safe-area-inset-bottom, 0px));
  border-top: 1px solid var(--stone-200);
  background: #fff;
  flex-shrink: 0;
}

.ia-input-bar input {
  flex: 1;
  min-width: 0;
  border: 1.5px solid var(--stone-200);
  border-radius: 20px;
  padding: 0.45rem 0.7rem;
  font-size: 0.82rem;
  font-family: var(--font-body);
  background: var(--stone-100);
  outline: none;
  transition: all 0.2s;
}

.ia-input-bar input:focus {
  border-color: var(--green-500);
  background: #fff;
  box-shadow: 0 0 0 3px rgba(61,161,92,0.12);
}

.ia-input-bar button {
  background: var(--green-700);
  color: #fff;
  border: none;
  border-radius: 50%;
  width: 34px;
  height: 34px;
  font-size: 0.9rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s;
  flex-shrink: 0;
}

.ia-input-bar button:hover {
  background: var(--green-600);
  transform: scale(1.05);
}

.ia-input-bar button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
  transform: none;
}

/* ───────── Bouton effacer ───────── */
.ia-help-btn {
  background: none;
  border: none;
  font-size: 0.85rem;
  cursor: pointer;
  color: var(--text-muted);
  padding: 4px 6px;
  border-radius: 6px;
  transition: all 0.15s;
  margin-left: auto;
}

.ia-help-btn:hover {
  background: var(--stone-100);
  color: var(--text-primary);
}

/* ───────── Drawer body scrollable ───────── */
.ia-drawer-body {
  display: flex;
  flex-direction: column;
  flex: 1;
  overflow: hidden;
  min-height: 0;
  max-height: calc(80vh - 56px);
  max-height: calc(80dvh - 56px);
}

/* ───────── Loading centré ───────── */
.ia-drawer-loading {
  text-align: center;
  padding: 0.4rem;
  color: var(--text-muted);
  font-size: 0.78rem;
  font-style: italic;
  flex-shrink: 0;
}

@media(max-width:768px) {
  .ia-drawer-body {
    max-height: calc(90vh - 46px - 56px);
    max-height: calc(90dvh - 46px - 56px);
  }
  .ia-messages {
    max-height: calc(70vh - 100px);
    max-height: calc(70dvh - 100px);
  }
}

/* ───────── Fix clavier mobile : le drawer reste au-dessus du clavier ─────────
   S'applique quand le clavier virtuel est ouvert (JS ajoute .ia-keyboard-open).
   dvh = dynamic viewport height (rétrécit avec le clavier sur Chrome 108+/Safari). */
.ia-drawer.ia-keyboard-open {
  bottom: 0;
  max-height: 100dvh;
}

@media(max-width:768px) {
  .ia-drawer.ia-keyboard-open {
    max-height: 100dvh;
  }
  .ia-drawer.ia-keyboard-open .ia-drawer-body {
    max-height: calc(100dvh - 46px - 56px);
  }
  .ia-drawer.ia-keyboard-open .ia-messages {
    max-height: none;
    flex: 1;
  }
}
