/* ─── Design system ──────────────────────────────────────────────────────────── */
:root {
  /* Palette sauge / terre */
  --phemonoe-sage: #5e8a7a;
  --phemonoe-sage-deep: #2d4f43;
  --phemonoe-sage-light: #e8f0ec;
  --phemonoe-sage-dark: #3d6356;
  --phemonoe-sage-muted: #94b5a8;
  --phemonoe-warm: #f7f4f0;
  --phemonoe-warm-dark: #ebe5dd;
  --phemonoe-text: #2c2c2c;
  --phemonoe-text-secondary: #6b6b6b;
  --phemonoe-border: #e2ddd6;
  --phemonoe-radius: 16px;
  --phemonoe-radius-sm: 10px;
  --phemonoe-radius-xs: 6px;

  --bs-body-font-family: 'DM Sans', system-ui, -apple-system, sans-serif;
  --bs-font-sans-serif: 'DM Sans', system-ui, -apple-system, sans-serif;
  --bs-body-font-size: 1.05rem;
  --bs-primary: var(--phemonoe-sage);
  --bs-primary-rgb: 94, 138, 122;
}

[data-bs-theme="dark"] {
  --phemonoe-sage: #7aaf9c;
  --phemonoe-sage-deep: #1a3a2f;
  --phemonoe-sage-light: #1e2e28;
  --phemonoe-sage-dark: #a5d1c0;
  --phemonoe-sage-muted: #5a8a78;
  --phemonoe-warm: #1a1a1a;
  --phemonoe-warm-dark: #222222;
  --phemonoe-text: #e4e0dc;
  --phemonoe-text-secondary: #9a9590;
  --phemonoe-border: #333330;
}

/* ─── Base ───────────────────────────────────────────────────────────────────── */
body {
  font-family: 'DM Sans', system-ui, -apple-system, sans-serif;
  font-size: 1.05rem;
  line-height: 1.65;
  color: var(--phemonoe-text);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

::selection {
  background: var(--phemonoe-sage-light);
  color: var(--phemonoe-sage-dark);
}

/* ─── Brand ──────────────────────────────────────────────────────────────────── */
.brand-name {
  font-family: 'Lora', serif;
  font-size: 1.2rem;
  font-weight: 600;
  letter-spacing: -0.02em;
  color: var(--phemonoe-text);
}

.logo-mark {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 34px;
  background: var(--phemonoe-sage-deep);
  color: #f5f5f3;
  border-radius: var(--phemonoe-radius-sm);
  font-family: 'Lora', serif;
  font-weight: 600;
  font-size: 16px;
  box-shadow: 0 2px 8px rgba(45, 79, 67, 0.25);
}

/* ─── Navbar ─────────────────────────────────────────────────────────────────── */
.navbar {
  background: var(--phemonoe-warm) !important;
  border-bottom: 1px solid var(--phemonoe-border) !important;
  padding: 0.6rem 0;
}

.navbar .nav-link {
  color: var(--phemonoe-text-secondary);
  font-size: 0.92rem;
  font-weight: 450;
  padding: 0.4rem 0.75rem;
  border-radius: var(--phemonoe-radius-xs);
  transition: color 0.15s, background 0.15s;
}

.navbar .nav-link:hover {
  color: var(--phemonoe-sage-dark);
  background: var(--phemonoe-sage-light);
}

/* ─── Sidebar ────────────────────────────────────────────────────────────────── */
#sidebar {
  width: 280px;
  min-width: 280px;
  flex-shrink: 0;
  background: var(--phemonoe-warm) !important;
  border-right: 1px solid var(--phemonoe-border) !important;
}

#conv-list {
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 4px;
}

.conv-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  cursor: pointer;
  border-radius: var(--phemonoe-radius-sm);
  transition: background 0.15s;
  text-decoration: none;
  color: inherit;
  border: none;
}

.conv-item:hover {
  background: var(--phemonoe-sage-light);
}

.conv-item.active {
  background: var(--phemonoe-sage-light);
  box-shadow: inset 3px 0 0 var(--phemonoe-sage);
}

.conv-item-text {
  flex: 1;
  min-width: 0;
}

.conv-item-title {
  font-size: 0.9rem;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  color: var(--phemonoe-text);
}

.conv-item-date {
  font-size: 0.75rem;
  color: var(--phemonoe-text-secondary);
  margin-top: 1px;
}

.conv-item-delete,
.conv-item-rename {
  background: none;
  border: none;
  color: var(--phemonoe-text-secondary);
  cursor: pointer;
  padding: 4px;
  border-radius: var(--phemonoe-radius-xs);
  display: flex;
  align-items: center;
  opacity: 0;
  transition: opacity 0.15s, color 0.15s, background 0.15s;
  flex-shrink: 0;
}

.conv-item:hover .conv-item-rename,
.conv-item:hover .conv-item-delete { opacity: 1; }
.conv-item-rename:hover { color: var(--phemonoe-sage); background: var(--phemonoe-sage-light); }
.conv-item-delete:hover { color: #c0392b; background: #fdf0ee; }

[data-bs-theme="dark"] .conv-item-delete:hover { background: #3a2020; }

.conv-item-rename-input {
  font-size: 0.85rem;
  padding: 2px 6px;
  border: 1.5px solid var(--phemonoe-sage);
  border-radius: var(--phemonoe-radius-xs);
  outline: none;
  width: 100%;
  font-family: inherit;
  background: var(--bs-body-bg);
  color: var(--phemonoe-text);
}

/* New conversation button */
#new-conv-btn {
  background: var(--phemonoe-sage-deep) !important;
  border: none !important;
  border-radius: var(--phemonoe-radius-sm) !important;
  font-weight: 500;
  font-size: 0.88rem;
  padding: 8px 12px;
  transition: background 0.15s, box-shadow 0.15s;
}

#new-conv-btn:hover {
  background: var(--phemonoe-sage-dark) !important;
  box-shadow: 0 2px 8px rgba(45, 79, 67, 0.3);
}

/* Sidebar export buttons */
#sidebar .btn-outline-secondary,
#sidebar .btn-outline-primary {
  border-color: var(--phemonoe-border);
  color: var(--phemonoe-text-secondary);
  font-size: 0.78rem;
  border-radius: var(--phemonoe-radius-xs);
}

#sidebar .btn-outline-secondary:hover,
#sidebar .btn-outline-primary:hover {
  background: var(--phemonoe-sage-light);
  border-color: var(--phemonoe-sage-muted);
  color: var(--phemonoe-sage-dark);
}

/* ─── Search ─────────────────────────────────────────────────────────────────── */
#conv-search {
  padding-right: 28px;
  border-radius: var(--phemonoe-radius-sm);
  border-color: var(--phemonoe-border);
  font-size: 0.85rem;
}

#conv-search:focus {
  border-color: var(--phemonoe-sage-muted);
  box-shadow: 0 0 0 3px rgba(94, 138, 122, 0.12);
}

.conv-search-clear {
  position: absolute;
  right: 4px;
  top: 50%;
  transform: translateY(-50%);
  background: none;
  border: none;
  font-size: 1.1rem;
  line-height: 1;
  color: var(--phemonoe-text-secondary);
  cursor: pointer;
  padding: 2px 6px;
}

.conv-search-clear:hover { color: var(--phemonoe-text); }

#sidebar-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.35);
  backdrop-filter: blur(2px);
  z-index: 1040;
}

/* ─── Welcome card ───────────────────────────────────────────────────────────── */
#welcome-card .card {
  border: none !important;
  background: var(--phemonoe-warm);
  border-radius: var(--phemonoe-radius) !important;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.04), 0 4px 16px rgba(0, 0, 0, 0.03);
}

#welcome-card .card-body {
  padding: 2.5rem !important;
}

#welcome-card .card-title {
  font-family: 'Lora', serif;
  font-weight: 600;
  color: var(--phemonoe-text);
  font-size: 1.5rem;
}

#welcome-card .card-text {
  color: var(--phemonoe-text-secondary);
  line-height: 1.7;
}

#welcome-card .card-text.lead {
  color: var(--phemonoe-text);
  font-size: 1.1rem;
  font-weight: 400;
}

#welcome-card hr {
  border-color: var(--phemonoe-border);
  opacity: 0.6;
}

#welcome-card .text-primary {
  color: var(--phemonoe-sage) !important;
}

/* ─── Module cards ───────────────────────────────────────────────────────────── */
.module-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  width: 100%;
  padding: 18px 14px;
  background: transparent;
  border: 1.5px solid var(--phemonoe-border);
  border-radius: var(--phemonoe-radius-sm);
  cursor: pointer;
  transition: all 0.2s ease;
  color: inherit;
  font-family: inherit;
  font-size: 0.9rem;
  text-align: center;
}

.module-card:hover {
  border-color: var(--phemonoe-sage);
  background: var(--phemonoe-sage-light);
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(94, 138, 122, 0.12);
}

.module-card svg {
  color: var(--phemonoe-sage);
  margin-bottom: 4px;
}

.module-card strong {
  font-size: 0.9rem;
  font-weight: 600;
  color: var(--phemonoe-text);
}

.module-card small {
  color: var(--phemonoe-text-secondary);
  font-size: 0.8rem;
  line-height: 1.3;
}

/* ─── Chat area ──────────────────────────────────────────────────────────────── */
#chat-area {
  scroll-behavior: smooth;
  background: var(--bs-body-bg);
}

#chat-area > .container {
  max-width: 740px;
}

/* ─── Messages ───────────────────────────────────────────────────────────────── */
.message {
  display: flex;
  gap: 12px;
  animation: fadeIn 0.25s ease;
  margin-bottom: 1.25rem;
}

.message:last-child { margin-bottom: 0; }

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(6px); }
  to   { opacity: 1; transform: translateY(0); }
}

.message.user { flex-direction: row-reverse; }

.message-col {
  min-width: 0;
  max-width: 85%;
}

.message.user .message-col {
  text-align: right;
}

.message-avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.65rem;
  font-weight: 600;
  flex-shrink: 0;
  margin-top: 2px;
  letter-spacing: 0.02em;
}

.message.user .message-avatar {
  background: var(--phemonoe-sage-deep);
  color: #fff;
}

.message.ai .message-avatar {
  background: var(--phemonoe-sage-light);
  color: var(--phemonoe-sage-dark);
  border: 1.5px solid var(--phemonoe-sage-muted);
}

.bubble {
  padding: 12px 16px;
  border-radius: var(--phemonoe-radius);
  font-family: 'DM Sans', system-ui, sans-serif;
  font-size: 1rem;
  line-height: 1.7;
  overflow-wrap: break-word;
}

.message.user .bubble {
  background: var(--phemonoe-sage-deep);
  color: #f5f5f3;
  border-bottom-right-radius: var(--phemonoe-radius-xs);
  box-shadow: 0 1px 4px rgba(45, 79, 67, 0.2);
  display: inline-block;
  text-align: left;
}

.message.ai .bubble {
  background: var(--phemonoe-warm);
  border: 1px solid var(--phemonoe-border);
  border-bottom-left-radius: var(--phemonoe-radius-xs);
}

/* Markdown dans bulles IA */
.message.ai .bubble p { margin-bottom: 0.5rem; }
.message.ai .bubble p:last-child { margin-bottom: 0; }
.message.ai .bubble ul, .message.ai .bubble ol { padding-left: 1.3rem; margin: 0.4rem 0; }
.message.ai .bubble li { margin-bottom: 0.2rem; }
.message.ai .bubble strong { color: var(--phemonoe-sage-dark); font-weight: 600; }
.message.ai .bubble h2, .message.ai .bubble h3 {
  font-family: 'Lora', serif;
  font-size: 1rem;
  font-weight: 600;
  margin: 0.8rem 0 0.3rem;
  color: var(--phemonoe-text);
}
.message.ai .bubble h2:first-child, .message.ai .bubble h3:first-child { margin-top: 0; }
.message.ai .bubble hr { border-top: 1px solid var(--phemonoe-border); margin: 0.7rem 0; }
.message.ai .bubble code {
  background: var(--phemonoe-sage-light);
  padding: 2px 6px;
  border-radius: var(--phemonoe-radius-xs);
  font-size: 0.88rem;
  color: var(--phemonoe-sage-dark);
}

.message-meta {
  font-size: 0.65rem;
  color: var(--phemonoe-text-secondary);
  margin-top: 3px;
  padding: 0 4px;
}

.message.user .message-meta { text-align: right; }

.error-bubble {
  background: #fdf0ee !important;
  border-color: #e8c4be !important;
  color: #9b3025 !important;
}

[data-bs-theme="dark"] .error-bubble {
  background: #3a2020 !important;
  border-color: #5a3030 !important;
  color: #e8a8a0 !important;
}

/* ─── Streaming cursor ───────────────────────────────────────────────────────── */
.bubble.streaming::after {
  content: '▋';
  display: inline;
  animation: blink 0.7s step-end infinite;
  color: var(--phemonoe-sage-muted);
}

@keyframes blink {
  50% { opacity: 0; }
}

/* ─── Typing indicator ───────────────────────────────────────────────────────── */
#typing {
  display: none;
  margin-bottom: 1.25rem;
}
#typing.visible { display: flex; }

.typing-bubble {
  background: var(--phemonoe-warm);
  border: 1px solid var(--phemonoe-border);
  border-radius: var(--phemonoe-radius);
  border-bottom-left-radius: var(--phemonoe-radius-xs);
  padding: 14px 18px;
  display: flex;
  gap: 5px;
  align-items: center;
}

.dot {
  width: 6px;
  height: 6px;
  background: var(--phemonoe-sage-muted);
  border-radius: 50%;
  animation: bounce 1.2s infinite;
}
.dot:nth-child(2) { animation-delay: 0.2s; }
.dot:nth-child(3) { animation-delay: 0.4s; }

@keyframes bounce {
  0%, 80%, 100% { transform: translateY(0); opacity: 0.3; }
  40%            { transform: translateY(-5px); opacity: 1; }
}

/* ─── Input ──────────────────────────────────────────────────────────────────── */
#input {
  resize: none;
  min-height: 48px;
  max-height: 160px;
  border-radius: var(--phemonoe-radius);
  border-color: var(--phemonoe-border);
  font-size: 1rem;
  padding: 12px 16px;
  transition: border-color 0.15s, box-shadow 0.15s;
}

#input:focus {
  border-color: var(--phemonoe-sage-muted);
  box-shadow: 0 0 0 3px rgba(94, 138, 122, 0.1);
}

#input::placeholder {
  color: var(--phemonoe-text-secondary);
  opacity: 0.6;
}

.send-btn {
  width: 48px;
  height: 48px;
  flex-shrink: 0;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--phemonoe-radius) !important;
  background: var(--phemonoe-sage-deep) !important;
  border: none !important;
  color: #f5f5f3 !important;
  transition: background 0.15s, transform 0.1s, box-shadow 0.15s;
}

.send-btn:hover {
  background: var(--phemonoe-sage-dark) !important;
  box-shadow: 0 2px 8px rgba(45, 79, 67, 0.3);
}

.send-btn:active {
  transform: scale(0.95);
}

/* Helper text sous input */
.border-top .text-muted.small {
  font-size: 0.72rem;
  opacity: 0.7;
}

/* ─── Buttons override ───────────────────────────────────────────────────────── */
.btn-primary {
  background-color: var(--phemonoe-sage-deep) !important;
  border-color: var(--phemonoe-sage-deep) !important;
  color: #f5f5f3 !important;
}

.btn-primary:hover {
  background-color: var(--phemonoe-sage-dark) !important;
  border-color: var(--phemonoe-sage-dark) !important;
}

.btn-outline-secondary {
  border-color: var(--phemonoe-border);
  color: var(--phemonoe-text-secondary);
}

.btn-outline-secondary:hover {
  background: var(--phemonoe-sage-light);
  border-color: var(--phemonoe-sage-muted);
  color: var(--phemonoe-sage-dark);
}

.btn-outline-warning {
  border-color: #c49a3c;
  color: #c49a3c;
  border-radius: var(--phemonoe-radius-sm);
}

.btn-outline-warning:hover {
  background: #c49a3c;
  color: #fff;
}

[data-bs-theme="dark"] .btn-outline-warning {
  border-color: #d4a84a;
  color: #d4a84a;
}

[data-bs-theme="dark"] .btn-outline-warning:hover {
  background: #d4a84a;
  color: #1a1a1a;
}

.btn-outline-danger {
  border-color: #d9534f;
  color: #d9534f;
  border-radius: var(--phemonoe-radius-sm);
}

.btn-outline-danger:hover {
  background: #d9534f;
  color: #fff;
}

/* ─── Urgent panel ───────────────────────────────────────────────────────────── */
#urgent-panel .bg-danger-subtle {
  background: #fdf0ee !important;
  border-color: var(--phemonoe-border) !important;
}

[data-bs-theme="dark"] #urgent-panel .bg-danger-subtle {
  background: #3a2020 !important;
}

/* ─── Footer ─────────────────────────────────────────────────────────────────── */
footer {
  background: var(--phemonoe-warm) !important;
  border-top: 1px solid var(--phemonoe-border) !important;
}

footer small, footer a {
  font-size: 0.75rem;
}

/* ─── Modals ─────────────────────────────────────────────────────────────────── */
.modal-content {
  border-radius: var(--phemonoe-radius) !important;
  border: none !important;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.12);
}

.modal-header {
  padding: 1.5rem 1.5rem 0.5rem;
}

.modal-body {
  padding: 0.5rem 1.5rem 1.5rem;
  line-height: 1.7;
  color: var(--phemonoe-text-secondary);
}

.modal-title {
  font-family: 'Lora', serif !important;
  color: var(--phemonoe-text);
}

/* ─── Scrollbar ──────────────────────────────────────────────────────────────── */
::-webkit-scrollbar {
  width: 6px;
}

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

::-webkit-scrollbar-thumb {
  background: var(--phemonoe-border);
  border-radius: 3px;
}

::-webkit-scrollbar-thumb:hover {
  background: var(--phemonoe-text-secondary);
}

/* ─── Login page ─────────────────────────────────────────────────────────────── */
body.bg-body-tertiary {
  background: var(--phemonoe-warm) !important;
}

.bg-body-tertiary {
  background: var(--phemonoe-warm) !important;
}

/* Login card */
body > div > .card {
  border: none !important;
  border-radius: var(--phemonoe-radius) !important;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.04), 0 8px 24px rgba(0, 0, 0, 0.06);
  background: var(--bs-body-bg);
}

/* ─── Form controls ──────────────────────────────────────────────────────────── */
.form-control {
  border-color: var(--phemonoe-border);
  border-radius: var(--phemonoe-radius-sm);
}

.form-control:focus {
  border-color: var(--phemonoe-sage-muted);
  box-shadow: 0 0 0 3px rgba(94, 138, 122, 0.1);
}

/* ─── Dark mode ──────────────────────────────────────────────────────────────── */
[data-bs-theme="dark"] .message.user .bubble {
  background: var(--phemonoe-sage-deep);
  color: #e8e6e2;
}

[data-bs-theme="dark"] .message.ai .bubble {
  background: var(--phemonoe-warm-dark);
  border-color: var(--phemonoe-border);
}

[data-bs-theme="dark"] .typing-bubble {
  background: var(--phemonoe-warm-dark);
  border-color: var(--phemonoe-border);
}

[data-bs-theme="dark"] .message.ai .bubble strong {
  color: var(--phemonoe-sage);
}

[data-bs-theme="dark"] .message.ai .bubble code {
  background: var(--phemonoe-sage-light);
  color: var(--phemonoe-sage);
}

[data-bs-theme="dark"] #welcome-card .card {
  background: var(--phemonoe-warm-dark);
}

[data-bs-theme="dark"] .module-card {
  border-color: var(--phemonoe-border);
}

[data-bs-theme="dark"] .module-card:hover {
  border-color: var(--phemonoe-sage);
  background: var(--phemonoe-sage-light);
}

/* ─── Responsive ─────────────────────────────────────────────────────────────── */
@media (max-width: 767.98px) {
  #sidebar {
    position: fixed;
    top: 0;
    left: -280px;
    height: 100dvh;
    z-index: 1045;
    transition: left 0.25s ease;
  }

  #sidebar.open { left: 0; }

  .message-avatar { width: 28px; height: 28px; font-size: 0.6rem; }

  .bubble { max-width: 92%; }

  #welcome-card .card-body { padding: 1.5rem !important; }
  #welcome-card .card-title { font-size: 1.25rem; }
}

@media (min-width: 768px) {
  #sidebar-toggle { display: none !important; }
  #sidebar-overlay { display: none !important; }
}

/* ─── Transitions globales ───────────────────────────────────────────────────── */
a, button, input, .conv-item, .module-card {
  transition-timing-function: ease;
}

/* ─── Accessibility: reduced motion ──────────────────────────────────────────── */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}
