/* ============================================================
   RESET & BASE
   ============================================================ */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  --blue: #3b82f6;
  --blue-dark: #2563eb;
  --green: #10b981;
  --red: #ef4444;
  --orange: #f59e0b;
  --purple: #8b5cf6;

  --sidebar-bg: #1e2d4a;
  --sidebar-hover: #2a3f5f;
  --sidebar-active: #3b82f6;
  --sidebar-width: 240px;

  --content-bg: #f5f7fb;
  --card-bg: #ffffff;
  --border: #e5e7eb;
  --text: #111827;
  --text-muted: #6b7280;

  --shadow-sm: 0 1px 3px rgba(0,0,0,.08);
  --shadow: 0 4px 12px rgba(0,0,0,.10);
  --radius: 12px;
  --radius-sm: 8px;

  --font: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
}

html, body {
  height: 100%;
  font-family: var(--font);
  font-size: 14px;
  color: var(--text);
  background: var(--content-bg);
}

/* ============================================================
   LAYOUT
   ============================================================ */
.layout {
  display: flex;
  height: 100vh;
  overflow: hidden;
}

/* ============================================================
   SIDEBAR
   ============================================================ */
.sidebar {
  width: var(--sidebar-width);
  background: var(--sidebar-bg);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  transition: width .25s ease;
  overflow: hidden;
}

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 20px 16px;
  border-bottom: 1px solid rgba(255,255,255,.08);
}

.logo {
  display: flex;
  align-items: center;
  gap: 10px;
}

.logo-icon {
  font-size: 22px;
}

.logo-text {
  color: #fff;
  font-size: 16px;
  font-weight: 700;
  white-space: nowrap;
}

.sidebar-toggle {
  background: none;
  border: none;
  color: rgba(255,255,255,.6);
  font-size: 18px;
  cursor: pointer;
  padding: 4px;
  border-radius: 4px;
  transition: color .15s;
}
.sidebar-toggle:hover { color: #fff; }

.sidebar-nav {
  flex: 1;
  padding: 12px 8px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  color: rgba(255,255,255,.65);
  text-decoration: none;
  transition: background .15s, color .15s;
  white-space: nowrap;
}

.nav-item:hover {
  background: var(--sidebar-hover);
  color: #fff;
}

.nav-item.active {
  background: var(--sidebar-active);
  color: #fff;
}

.nav-icon { font-size: 16px; }
.nav-label { font-size: 14px; font-weight: 500; }

.sidebar-footer {
  padding: 16px;
  border-top: 1px solid rgba(255,255,255,.08);
}

.sidebar-info {
  color: rgba(255,255,255,.5);
  font-size: 11px;
  white-space: nowrap;
}
.sidebar-info.muted { color: rgba(255,255,255,.3); font-size: 10px; margin-top: 2px; }

/* Collapsed sidebar */
.sidebar.collapsed {
  width: 60px;
}
.sidebar.collapsed .logo-text,
.sidebar.collapsed .nav-label,
.sidebar.collapsed .sidebar-footer { display: none; }
.sidebar.collapsed .nav-item { justify-content: center; padding: 10px; }
.sidebar.collapsed .logo { justify-content: center; }

/* ============================================================
   CONTENT
   ============================================================ */
.content {
  flex: 1;
  overflow-y: auto;
  padding: 0;
  display: flex;
  flex-direction: column;
}

/* ============================================================
   VIEWS
   ============================================================ */
.view {
  display: none;
  flex-direction: column;
  flex: 1;
  padding: 28px;
  gap: 24px;
}

.view.active {
  display: flex;
}

.view-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.view-header h1 {
  font-size: 22px;
  font-weight: 700;
  color: var(--text);
}

/* ============================================================
   CARDS
   ============================================================ */
.card {
  background: var(--card-bg);
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
  border: 1px solid var(--border);
  overflow: hidden;
}

.card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px 12px;
  border-bottom: 1px solid var(--border);
}

.card-header h3 {
  font-size: 14px;
  font-weight: 600;
  color: var(--text);
}

.card-subtitle {
  font-size: 12px;
  color: var(--text-muted);
}

/* ============================================================
   KPI GRID
   ============================================================ */
.kpi-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 16px;
}

.kpi-card {
  background: var(--card-bg);
  border-radius: var(--radius);
  padding: 20px;
  box-shadow: var(--shadow-sm);
  border: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.kpi-card.skeleton {
  height: 110px;
  background: linear-gradient(90deg, #f0f0f0 25%, #e0e0e0 50%, #f0f0f0 75%);
  background-size: 200% 100%;
  animation: shimmer 1.5s infinite;
}

@keyframes shimmer {
  0% { background-position: 200% 0; }
  100% { background-position: -200% 0; }
}

.kpi-icon {
  font-size: 24px;
}

.kpi-value {
  font-size: 26px;
  font-weight: 700;
  line-height: 1;
}

.kpi-label {
  font-size: 12px;
  color: var(--text-muted);
  font-weight: 500;
}

.kpi-sub {
  font-size: 11px;
  color: var(--text-muted);
}

.kpi-green .kpi-value { color: var(--green); }
.kpi-red .kpi-value { color: var(--red); }
.kpi-orange .kpi-value { color: var(--orange); }
.kpi-blue .kpi-value { color: var(--blue); }

/* ============================================================
   CHARTS
   ============================================================ */
.charts-row {
  display: grid;
  grid-template-columns: 1fr 1.5fr;
  gap: 16px;
}

.chart-card {
  padding-bottom: 16px;
}

.chart-container {
  padding: 16px;
  height: 240px;
  position: relative;
}

.chart-empty {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-muted);
  font-size: 13px;
  font-style: italic;
}

/* ============================================================
   TABLES ROW
   ============================================================ */
.tables-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
}

/* ============================================================
   DATA TABLE
   ============================================================ */
.table-wrapper {
  overflow-x: auto;
}

.data-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}

.data-table th {
  padding: 10px 16px;
  text-align: left;
  font-size: 11px;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: .05em;
  background: #f9fafb;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}

.data-table td {
  padding: 11px 16px;
  border-bottom: 1px solid #f3f4f6;
  color: var(--text);
}

.data-table tbody tr:last-child td { border-bottom: none; }
.data-table tbody tr:hover td { background: #f9fafb; }

.empty-row td {
  text-align: center;
  color: var(--text-muted);
  padding: 32px;
  font-style: italic;
}

/* ============================================================
   BADGES
   ============================================================ */
.badge {
  display: inline-flex;
  align-items: center;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 600;
  white-space: nowrap;
}

.badge-red { background: #fee2e2; color: var(--red); }
.badge-green { background: #d1fae5; color: var(--green); }
.badge-orange { background: #fef3c7; color: var(--orange); }
.badge-blue { background: #dbeafe; color: var(--blue); }
.badge-gray { background: #f3f4f6; color: var(--text-muted); }

/* Urgence badges */
.badge-urgence-1 { background: #d1fae5; color: var(--green); }
.badge-urgence-2 { background: #dbeafe; color: var(--blue); }
.badge-urgence-3 { background: #fef3c7; color: var(--orange); }
.badge-urgence-4 { background: #ffedd5; color: #ea580c; }
.badge-urgence-5 { background: #fee2e2; color: var(--red); font-weight: 700; }

/* Statut badges */
.badge-statut-OUVERT { background: #fef3c7; color: var(--orange); }
.badge-statut-EN_COURS { background: #dbeafe; color: var(--blue); }
.badge-statut-RÉSOLU { background: #d1fae5; color: var(--green); }

/* ============================================================
   BUTTONS
   ============================================================ */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 16px;
  border-radius: var(--radius-sm);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  border: none;
  transition: background .15s, opacity .15s;
}

.btn:disabled { opacity: .5; cursor: not-allowed; }

.btn-primary {
  background: var(--blue);
  color: #fff;
}
.btn-primary:hover:not(:disabled) { background: var(--blue-dark); }

.btn-ghost {
  background: transparent;
  color: var(--text-muted);
  border: 1px solid var(--border);
}
.btn-ghost:hover:not(:disabled) { background: #f3f4f6; }

/* ============================================================
   FILTER BAR
   ============================================================ */
.filter-bar {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.filter-btn {
  padding: 6px 14px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--card-bg);
  font-size: 13px;
  cursor: pointer;
  color: var(--text-muted);
  transition: all .15s;
}

.filter-btn.active {
  background: var(--blue);
  color: #fff;
  border-color: var(--blue);
}

.filter-btn:hover:not(.active) { background: #f3f4f6; }

/* ============================================================
   SEARCH
   ============================================================ */
.search-bar { flex: 1; }

.search-input {
  width: 100%;
  max-width: 320px;
  padding: 7px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 13px;
  outline: none;
  background: #f9fafb;
  transition: border .15s, background .15s;
}

.search-input:focus {
  border-color: var(--blue);
  background: #fff;
}

.stat-label {
  font-size: 12px;
  color: var(--text-muted);
  white-space: nowrap;
}

/* ============================================================
   CHAT
   ============================================================ */
.chat-layout {
  flex: 1;
  display: flex;
  flex-direction: column;
  background: var(--card-bg);
  border-radius: var(--radius);
  border: 1px solid var(--border);
  box-shadow: var(--shadow-sm);
  overflow: hidden;
}

.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 24px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.chat-welcome {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  gap: 12px;
  padding: 40px 20px;
  color: var(--text-muted);
  margin: auto;
}

.chat-welcome-icon { font-size: 48px; }

.chat-welcome h2 {
  font-size: 20px;
  font-weight: 700;
  color: var(--text);
}

.chat-welcome p {
  font-size: 14px;
  line-height: 1.6;
}

.chat-suggestions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: center;
  margin-top: 8px;
}

.suggestion-chip {
  padding: 6px 14px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--card-bg);
  font-size: 12px;
  cursor: pointer;
  color: var(--blue);
  transition: all .15s;
}

.suggestion-chip:hover {
  background: var(--blue);
  color: #fff;
  border-color: var(--blue);
}

/* Message bubbles */
.msg {
  display: flex;
  flex-direction: column;
  gap: 4px;
  max-width: 80%;
}

.msg-user {
  align-self: flex-end;
  align-items: flex-end;
}

.msg-assistant {
  align-self: flex-start;
  align-items: flex-start;
}

.msg-bubble {
  padding: 10px 14px;
  border-radius: 16px;
  font-size: 14px;
  line-height: 1.6;
  word-break: break-word;
}

.msg-user .msg-bubble {
  white-space: pre-wrap;
}

.msg-user .msg-bubble {
  background: var(--blue);
  color: #fff;
  border-bottom-right-radius: 4px;
}

.msg-assistant .msg-bubble {
  background: #f3f4f6;
  color: var(--text);
  border-bottom-left-radius: 4px;
}

.msg-time {
  font-size: 11px;
  color: var(--text-muted);
  padding: 0 4px;
}

/* Quick replies */
.quick-replies {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  align-self: flex-start;
  padding-left: 2px;
}

.quick-reply-btn {
  padding: 5px 13px;
  border-radius: 999px;
  border: 1px solid var(--blue);
  background: transparent;
  color: var(--blue);
  font-size: 12px;
  font-family: var(--font);
  cursor: pointer;
  transition: background .15s, color .15s;
  white-space: nowrap;
}

.quick-reply-btn:hover {
  background: var(--blue);
  color: #fff;
}

/* Tool call cards */
.tool-card {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 12px;
  background: #f0f4ff;
  border: 1px solid #c7d7fe;
  border-radius: var(--radius-sm);
  font-size: 12px;
  color: var(--blue-dark);
  margin: 2px 0;
  align-self: flex-start;
  max-width: 80%;
}

.tool-card.loading .tool-icon {
  animation: spin 1s linear infinite;
  display: inline-block;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

.tool-card.done {
  background: #f0fdf4;
  border-color: #bbf7d0;
  color: #166534;
}

.tool-card.done .tool-icon { animation: none; }

.tool-name {
  font-weight: 600;
  font-family: monospace;
}

/* Chat typing indicator */
.typing-indicator {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 10px 14px;
  background: #f3f4f6;
  border-radius: 16px;
  border-bottom-left-radius: 4px;
  width: fit-content;
}

.typing-dot {
  width: 6px;
  height: 6px;
  background: var(--text-muted);
  border-radius: 50%;
  animation: bounce 1.4s infinite;
}

.typing-dot:nth-child(2) { animation-delay: .2s; }
.typing-dot:nth-child(3) { animation-delay: .4s; }

@keyframes bounce {
  0%, 80%, 100% { transform: translateY(0); }
  40% { transform: translateY(-6px); }
}

/* Chat input */
.chat-input-area {
  border-top: 1px solid var(--border);
  padding: 16px;
}

.chat-input-wrapper {
  display: flex;
  align-items: flex-end;
  gap: 10px;
  background: #f9fafb;
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 8px 8px 8px 14px;
  transition: border .15s;
}

.chat-input-wrapper:focus-within {
  border-color: var(--blue);
  background: #fff;
}

.chat-input {
  flex: 1;
  border: none;
  background: transparent;
  font-size: 14px;
  font-family: var(--font);
  resize: none;
  outline: none;
  max-height: 120px;
  line-height: 1.5;
  color: var(--text);
}

.chat-send-btn {
  width: 34px;
  height: 34px;
  border-radius: 8px;
  border: none;
  background: var(--blue);
  color: #fff;
  font-size: 16px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: background .15s, opacity .15s;
}

.chat-send-btn:disabled {
  background: #d1d5db;
  cursor: not-allowed;
}

.chat-send-btn:hover:not(:disabled) { background: var(--blue-dark); }

/* ── Markdown dans les bulles assistant ─────────────────────── */
.msg-assistant .msg-bubble p { margin-bottom: 8px; }
.msg-assistant .msg-bubble p:last-child { margin-bottom: 0; }

.msg-assistant .msg-bubble h1,
.msg-assistant .msg-bubble h2,
.msg-assistant .msg-bubble h3 {
  font-weight: 700;
  line-height: 1.3;
  margin: 10px 0 4px;
}
.msg-assistant .msg-bubble h1 { font-size: 16px; }
.msg-assistant .msg-bubble h2 { font-size: 15px; }
.msg-assistant .msg-bubble h3 { font-size: 14px; color: var(--text-muted); }

.msg-assistant .msg-bubble ul,
.msg-assistant .msg-bubble ol {
  padding-left: 18px;
  margin: 6px 0;
}
.msg-assistant .msg-bubble li { margin-bottom: 3px; }
.msg-assistant .msg-bubble li:last-child { margin-bottom: 0; }

.msg-assistant .msg-bubble strong { font-weight: 700; }
.msg-assistant .msg-bubble em { font-style: italic; }

.msg-assistant .msg-bubble code {
  background: rgba(0,0,0,.07);
  padding: 1px 5px;
  border-radius: 4px;
  font-family: 'Consolas', 'Monaco', monospace;
  font-size: 12px;
}

.msg-assistant .msg-bubble pre {
  background: #1e293b;
  color: #e2e8f0;
  border-radius: 8px;
  padding: 12px 14px;
  overflow-x: auto;
  margin: 8px 0;
}
.msg-assistant .msg-bubble pre code {
  background: none;
  padding: 0;
  color: inherit;
  font-size: 12px;
}

.msg-assistant .msg-bubble blockquote {
  border-left: 3px solid var(--blue);
  padding-left: 12px;
  color: var(--text-muted);
  font-style: italic;
  margin: 8px 0;
}

.msg-assistant .msg-bubble hr {
  border: none;
  border-top: 1px solid var(--border);
  margin: 10px 0;
}

.msg-assistant .msg-bubble table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
  margin: 8px 0;
  border-radius: 8px;
  overflow: hidden;
}
.msg-assistant .msg-bubble th {
  background: #e5e7eb;
  font-weight: 600;
  padding: 7px 10px;
  text-align: left;
  border: 1px solid var(--border);
}
.msg-assistant .msg-bubble td {
  padding: 6px 10px;
  border: 1px solid var(--border);
}
.msg-assistant .msg-bubble tr:nth-child(even) td { background: #f9fafb; }

/* ── Logs / Timeline ─────────────────────────────────────────── */
.logs-card { overflow: visible; }

.timeline {
  padding: 8px 24px 24px;
}

.logs-loading,
.logs-empty,
.logs-error {
  text-align: center;
  padding: 48px 24px;
  color: var(--text-muted);
  font-size: 14px;
}
.logs-error { color: var(--red); }

.logs-spinner {
  width: 24px;
  height: 24px;
  border: 3px solid #e5e7eb;
  border-top-color: var(--blue);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
  margin: 0 auto 12px;
}

.timeline-group { margin-bottom: 8px; }

.timeline-date-label {
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .08em;
  color: var(--text-muted);
  padding: 20px 0 10px 28px;
}

.timeline-entry {
  display: flex;
  gap: 0;
  position: relative;
  padding-left: 28px;
  padding-bottom: 2px;
}

/* Vertical line */
.timeline-entry::before {
  content: '';
  position: absolute;
  left: 7px;
  top: 20px;
  bottom: -2px;
  width: 2px;
  background: #e5e7eb;
}
.timeline-group:last-child .timeline-entry:last-child::before { display: none; }

.timeline-dot {
  position: absolute;
  left: 0;
  top: 14px;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  border: 2px solid #fff;
  flex-shrink: 0;
  z-index: 1;
}

.log-cat-comptabilite { background: #dbeafe; border-color: var(--blue); }
.log-cat-technique    { background: #ffedd5; border-color: #f97316;    }
.log-cat-juridique    { background: #ede9fe; border-color: var(--purple); }
.log-cat-crm          { background: #d1fae5; border-color: var(--green); }

.timeline-content {
  flex: 1;
  background: #f9fafb;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 10px 14px;
  margin-bottom: 8px;
  transition: background .15s;
}
.timeline-content:hover { background: #f3f4f6; }

.timeline-header {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 4px;
}

.log-icon { font-size: 14px; flex-shrink: 0; }

.log-action {
  font-size: 13px;
  font-weight: 600;
  color: var(--text);
  flex: 1;
  min-width: 0;
}

.log-badge {
  font-size: 10px;
  padding: 1px 7px;
  flex-shrink: 0;
}
.log-badge.log-cat-comptabilite { background: #dbeafe; color: var(--blue-dark); border: none; }
.log-badge.log-cat-technique    { background: #ffedd5; color: #c2410c;          border: none; }
.log-badge.log-cat-juridique    { background: #ede9fe; color: #6d28d9;          border: none; }
.log-badge.log-cat-crm          { background: #d1fae5; color: #065f46;          border: none; }

.log-montant {
  font-size: 12px;
  font-weight: 700;
  color: var(--text);
  white-space: nowrap;
  flex-shrink: 0;
}

.log-time {
  font-size: 11px;
  color: var(--text-muted);
  white-space: nowrap;
  flex-shrink: 0;
  margin-left: auto;
}

.log-detail {
  font-size: 12px;
  color: var(--text-muted);
  line-height: 1.5;
}

/* ── Dashboard — état d'erreur ─────────────────────────────── */
.kpi-error {
  grid-column: 1 / -1;
  align-items: center;
  justify-content: center;
  text-align: center;
  background: #fff7f7;
  border-color: #fca5a5;
  gap: 6px;
  padding: 32px;
}
.kpi-error .kpi-icon { font-size: 36px; }
.kpi-error .kpi-label { color: var(--red); font-weight: 600; font-size: 13px; }
.kpi-error .kpi-sub { font-size: 12px; color: var(--text-muted); max-width: 400px; }
.kpi-retry-btn { margin-top: 10px; font-size: 12px; }

/* ============================================================
   AMOUNT COLORS
   ============================================================ */
.amount-red { color: var(--red); font-weight: 600; }
.amount-green { color: var(--green); font-weight: 600; }

/* ============================================================
   RESPONSIVE (mobile)
   ============================================================ */
@media (max-width: 768px) {
  .sidebar { width: 60px; }
  .sidebar .logo-text,
  .sidebar .nav-label,
  .sidebar .sidebar-footer { display: none; }
  .sidebar .nav-item { justify-content: center; padding: 10px; }
  .sidebar .logo { justify-content: center; }

  .view { padding: 16px; gap: 16px; }
  .kpi-grid { grid-template-columns: repeat(2, 1fr); }
  .charts-row { grid-template-columns: 1fr; }
  .tables-row { grid-template-columns: 1fr; }
  .msg { max-width: 92%; }
}

@media (max-width: 480px) {
  .kpi-grid { grid-template-columns: 1fr 1fr; }
}
