:root {
  --bg-main: #0c0e14;
  --bg-card: rgba(22, 27, 34, 0.75);
  --bg-card-border: rgba(255, 255, 255, 0.08);
  --text-primary: #f0f6fc;
  --text-secondary: #8b949e;
  --accent-blue: #388bfd;
  --accent-cyan: #39c5cf;
  --accent-green: #3fb950;
  --accent-orange: #d29922;
  --accent-red: #f85149;
  --user-bubble: #1f6feb;
  --assistant-bubble: rgba(27, 32, 43, 0.85);
  --font-stack: -apple-system, BlinkMacSystemFont, "SF Pro Display", "SF Pro Text", "Segoe UI", Roboto, sans-serif;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  -webkit-tap-highlight-color: transparent;
}

html, body {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  height: 100dvh;
  height: 100svh;
  height: -webkit-fill-available;
  margin: 0;
  padding: 0;
  overflow: hidden;
  overscroll-behavior: none;
  -webkit-text-size-adjust: 100%;
}

body {
  font-family: var(--font-stack);
  background-color: var(--bg-main);
  color: var(--text-primary);
  display: flex;
  flex-direction: column;
  padding-top: env(safe-area-inset-top, 0px);
}

/* --- HEADER --- */
header {
  flex-shrink: 0;
  height: 60px;
  background: rgba(12, 14, 20, 0.8);
  backdrop-filter: blur(25px);
  -webkit-backdrop-filter: blur(25px);
  border-bottom: 1px solid var(--bg-card-border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  z-index: 100;
}

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

.brand-icon {
  width: 32px;
  height: 32px;
  border-radius: 8px;
  background: linear-gradient(135deg, #388bfd, #8957e5);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: bold;
  font-size: 18px;
  color: white;
  box-shadow: 0 0 15px rgba(56, 139, 253, 0.4);
}

.brand-title {
  font-size: 17px;
  font-weight: 600;
  letter-spacing: -0.3px;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.status-pill {
  font-size: 12px;
  padding: 4px 10px;
  border-radius: 20px;
  background: rgba(63, 185, 80, 0.15);
  color: var(--accent-green);
  border: 1px solid rgba(63, 185, 80, 0.3);
  display: flex;
  align-items: center;
  gap: 6px;
}

.status-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--accent-green);
  box-shadow: 0 0 8px var(--accent-green);
}

.icon-btn {
  background: transparent;
  border: 1px solid var(--bg-card-border);
  color: var(--text-secondary);
  border-radius: 8px;
  width: 34px;
  height: 34px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.2s;
}

.icon-btn:hover {
  background: rgba(255, 255, 255, 0.05);
  color: var(--text-primary);
}

/* --- BANNIÈRE FILE D'ATTENTE --- */
#queue-banner {
  display: none;
  background: linear-gradient(90deg, rgba(210, 153, 34, 0.2), rgba(248, 81, 73, 0.15));
  border-bottom: 1px solid rgba(210, 153, 34, 0.4);
  padding: 10px 16px;
  font-size: 13px;
  color: #e3b341;
  align-items: center;
  justify-content: space-between;
  animation: slideDown 0.3s ease;
}

#queue-banner.active {
  display: flex;
}

.queue-details {
  display: flex;
  align-items: center;
  gap: 8px;
}

.spinner {
  width: 14px;
  height: 14px;
  border: 2px solid rgba(227, 179, 65, 0.3);
  border-top-color: #e3b341;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

/* --- CHAT AREA --- */
#chat-container {
  flex: 1 1 0%;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
  touch-action: pan-y;
  overscroll-behavior-y: contain;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 16px;
  scroll-behavior: smooth;
}

.message-row {
  display: flex;
  flex-direction: column;
  max-width: 88%;
  animation: fadeIn 0.2s ease;
}

.message-row.user {
  align-self: flex-end;
}

.message-row.assistant {
  align-self: flex-start;
}

.bubble {
  padding: 12px 16px;
  border-radius: 18px;
  font-size: 15px;
  line-height: 1.5;
  word-break: break-word;
}

.message-row.user .bubble {
  background: var(--user-bubble);
  color: white;
  border-bottom-right-radius: 4px;
}

.message-row.assistant .bubble {
  background: var(--assistant-bubble);
  border: 1px solid var(--bg-card-border);
  color: var(--text-primary);
  border-bottom-left-radius: 4px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.2);
}

.cursor {
  display: inline-block;
  width: 8px;
  height: 15px;
  background: var(--accent-blue);
  margin-left: 4px;
  vertical-align: middle;
  animation: blink 0.8s infinite;
}

/* --- BADGE RECHERCHE WEB & DOCS --- */
.context-badge {
  font-size: 11px;
  padding: 3px 8px;
  border-radius: 12px;
  margin-bottom: 6px;
  display: inline-flex;
  align-items: center;
  gap: 4px;
  align-self: flex-start;
}
.context-badge.web {
  background: rgba(56, 139, 253, 0.15);
  color: var(--accent-blue);
  border: 1px solid rgba(56, 139, 253, 0.3);
}
.context-badge.doc {
  background: rgba(210, 153, 34, 0.15);
  color: var(--accent-orange);
  border: 1px solid rgba(210, 153, 34, 0.3);
}

/* --- BOTTOM CONTROLS --- */
#controls-container {
  flex-shrink: 0;
  background: rgba(12, 14, 20, 0.92);
  backdrop-filter: blur(25px);
  -webkit-backdrop-filter: blur(25px);
  border-top: 1px solid var(--bg-card-border);
  padding: 10px 16px calc(12px + env(safe-area-inset-bottom, 0px)) 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  z-index: 50;
}

/* JAUGE DE TEMPÉRATURE VISUELLE */
.temp-bar-container {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: rgba(255, 255, 255, 0.03);
  border: 1px solid var(--bg-card-border);
  padding: 6px 12px;
  border-radius: 12px;
}

.temp-meta {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 12px;
}

.temp-meter {
  display: flex;
  align-items: center;
  gap: 3px;
  height: 16px;
}

.temp-segment {
  width: 8px;
  height: 12px;
  border-radius: 2px;
  background: rgba(255, 255, 255, 0.1);
  transition: all 0.2s;
}

.temp-segment.active-blue {
  background: var(--accent-blue);
  box-shadow: 0 0 8px var(--accent-blue);
}

.temp-segment.active-green {
  background: var(--accent-green);
  box-shadow: 0 0 8px var(--accent-green);
}

.temp-segment.active-orange {
  background: var(--accent-orange);
  box-shadow: 0 0 8px var(--accent-orange);
}

.temp-segment.active-red {
  background: var(--accent-red);
  box-shadow: 0 0 8px var(--accent-red);
}

.temp-slider {
  width: 110px;
  accent-color: var(--accent-blue);
  cursor: pointer;
}

/* BARRE D'OPTIONS */
.options-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.toggle-pill {
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid var(--bg-card-border);
  color: var(--text-secondary);
  border-radius: 20px;
  padding: 5px 12px;
  font-size: 12px;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 6px;
  transition: all 0.2s;
}

.toggle-pill.active {
  background: rgba(56, 139, 253, 0.2);
  color: var(--accent-blue);
  border-color: var(--accent-blue);
}

.model-select {
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid var(--bg-card-border);
  color: var(--text-primary);
  border-radius: 8px;
  padding: 4px 8px;
  font-size: 12px;
  outline: none;
}

/* ZONE DE SAISIE */
.input-row {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid var(--bg-card-border);
  border-radius: 20px;
  padding: 6px 12px;
  transition: border-color 0.2s;
}

.input-row:focus-within {
  border-color: var(--accent-blue);
}

#chat-input {
  flex: 1;
  background: transparent;
  border: none;
  outline: none;
  color: var(--text-primary);
  font-size: 16px; /* Impératif iOS : >= 16px pour éliminer le zoom automatique Safari */
  line-height: 1.4;
  resize: none;
  max-height: 120px;
  font-family: inherit;
}

.send-btn {
  background: var(--user-bubble);
  color: white;
  border: none;
  border-radius: 50%;
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: opacity 0.2s;
}

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

/* --- MODALS --- */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.7);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  display: none;
  align-items: center;
  justify-content: center;
  padding: 16px;
  z-index: 1000;
  animation: fadeIn 0.2s ease;
}

.modal-card {
  background: #161b22;
  border: 1px solid var(--bg-card-border);
  border-radius: 20px;
  width: 100%;
  max-width: 440px;
  padding: 24px;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.5);
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.modal-title {
  font-size: 18px;
  font-weight: 600;
}

.modal-desc {
  font-size: 14px;
  color: var(--text-secondary);
  line-height: 1.5;
}

.btn-primary {
  background: var(--user-bubble);
  color: white;
  border: none;
  padding: 12px;
  border-radius: 12px;
  font-weight: 600;
  font-size: 15px;
  cursor: pointer;
}

.btn-secondary {
  background: transparent;
  color: var(--text-secondary);
  border: 1px solid var(--bg-card-border);
  padding: 10px;
  border-radius: 12px;
  cursor: pointer;
}

/* --- BANNIÈRE DOCUMENT ACTIF & EMBEDDING --- */
.active-file-banner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: rgba(210, 153, 34, 0.12);
  border: 1px solid rgba(210, 153, 34, 0.4);
  border-radius: 12px;
  padding: 8px 12px;
  animation: fadeIn 0.25s ease;
  gap: 8px;
}

.active-file-left {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
  flex: 1;
}

.file-icon {
  font-size: 18px;
  flex-shrink: 0;
}

.active-file-meta {
  display: flex;
  flex-direction: column;
  min-width: 0;
  gap: 2px;
}

.active-file-name {
  font-size: 13px;
  font-weight: 600;
  color: #f0f6fc;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.active-file-embed-tag {
  font-size: 11px;
  color: var(--accent-orange);
  font-weight: 500;
}

.icon-close-btn {
  background: transparent;
  border: none;
  color: var(--text-secondary);
  cursor: pointer;
  font-size: 14px;
  padding: 4px 8px;
  border-radius: 6px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s;
  flex-shrink: 0;
}

.icon-close-btn:hover {
  color: var(--accent-red);
  background: rgba(248, 81, 73, 0.15);
}

/* --- ADMIN & HISTORIQUES UTILISATEURS (SSD) --- */
.admin-card {
  background: rgba(255, 255, 255, 0.03);
  border: 1px solid var(--bg-card-border);
  padding: 12px;
  border-radius: 12px;
  display: flex;
  flex-direction: column;
}

.history-user-card {
  background: rgba(255, 255, 255, 0.02);
  border: 1px solid var(--bg-card-border);
  border-radius: 10px;
  padding: 10px 12px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  transition: background 0.2s, border-color 0.2s;
}

.history-user-card:hover {
  background: rgba(255, 255, 255, 0.05);
  border-color: rgba(56, 139, 253, 0.3);
}

.history-user-info {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
}

.history-user-name {
  font-weight: 600;
  font-size: 13px;
  color: var(--text-primary);
}

.history-user-meta {
  font-size: 11px;
  color: var(--text-secondary);
}

.history-session-header {
  font-size: 12px;
  font-weight: 600;
  color: var(--accent-cyan);
  padding: 6px 10px;
  background: rgba(57, 197, 207, 0.1);
  border: 1px solid rgba(57, 197, 207, 0.25);
  border-radius: 8px;
  margin-top: 10px;
  margin-bottom: 6px;
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.history-msg-row {
  display: flex;
  flex-direction: column;
  gap: 4px;
  margin-bottom: 8px;
}

.history-msg-row.user {
  align-self: flex-end;
  max-width: 90%;
}

.history-msg-row.assistant {
  align-self: flex-start;
  max-width: 92%;
}

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

.history-msg-row.user .history-msg-bubble {
  background: var(--user-bubble);
  color: white;
  border-bottom-right-radius: 4px;
}

.history-msg-row.assistant .history-msg-bubble {
  background: var(--assistant-bubble);
  border: 1px solid var(--bg-card-border);
  color: var(--text-primary);
  border-bottom-left-radius: 4px;
}

.history-msg-time {
  font-size: 10px;
  color: var(--text-secondary);
  align-self: flex-end;
  margin-top: 2px;
}

.history-doc-tag {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  font-size: 11px;
  background: rgba(210, 153, 34, 0.18);
  border: 1px solid rgba(210, 153, 34, 0.35);
  color: var(--accent-orange);
  padding: 3px 8px;
  border-radius: 8px;
  margin-top: 4px;
}

/* Protection inputs iOS contre zoom forcé */
@media screen and (max-width: 768px) {
  input[type="text"],
  input[type="password"],
  select,
  textarea {
    font-size: 16px !important;
  }
}

/* ANIMATIONS */
@keyframes spin {
  to { transform: rotate(360deg); }
}
@keyframes blink {
  50% { opacity: 0; }
}
@keyframes fadeIn {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: translateY(0); }
}
@keyframes slideDown {
  from { opacity: 0; transform: translateY(-10px); }
  to { opacity: 1; transform: translateY(0); }
}
