:root {
  --bg: #f6f7f9;
  --panel: #ffffff;
  --border: #e2e5ea;
  --text: #1f2430;
  --muted: #6b7280;
  --accent: #3b6ef5;
  --accent-dark: #2c54c4;
  --ok: #1a9e63;
  --warn: #c77700;
  --err: #d33a3a;
  --radius: 12px;
  --shadow: 0 1px 3px rgba(16, 24, 40, .08), 0 1px 2px rgba(16, 24, 40, .04);
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.5;
}
button { font: inherit; cursor: pointer; }
input, textarea, select { font: inherit; color: inherit; }

/* --- Connexion --- */
.login-body { display: flex; align-items: center; justify-content: center; min-height: 100vh; padding: 16px; }
.login-card {
  background: var(--panel); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 32px; width: 100%; max-width: 380px;
}
.login-card h1 { margin: 0 0 4px; font-size: 22px; }
.login-card .subtitle { color: var(--muted); margin: 0 0 20px; }
.login-card label { display: block; margin-bottom: 6px; font-size: 13px; color: var(--muted); }
.login-card input {
  width: 100%; padding: 10px 12px; border: 1px solid var(--border);
  border-radius: 8px; margin-bottom: 14px;
}
.login-card button { width: 100%; }
.error { color: var(--err); font-size: 13px; min-height: 18px; }
.hint { color: var(--muted); font-size: 13px; }

/* --- Boutons --- */
.primary {
  background: var(--accent); color: #fff; border: 0; padding: 9px 16px;
  border-radius: 8px; font-weight: 600;
}
.primary:hover { background: var(--accent-dark); }
.ghost {
  background: transparent; color: var(--text); border: 1px solid var(--border);
  padding: 8px 14px; border-radius: 8px;
}
.ghost:hover { background: #f0f2f5; }
.small { padding: 5px 10px; font-size: 13px; }

/* --- Application --- */
.app { display: flex; height: 100vh; }
.sidebar {
  width: 240px; background: var(--panel); border-right: 1px solid var(--border);
  display: flex; flex-direction: column; flex-shrink: 0;
}
.brand { padding: 20px 18px 12px; font-weight: 700; font-size: 17px; }
.nav { padding: 6px 10px; display: flex; flex-direction: column; gap: 2px; flex: 1; }
.nav-btn {
  text-align: left; background: transparent; border: 0; padding: 10px 12px;
  border-radius: 8px; color: var(--text); font-size: 14px;
}
.nav-btn:hover { background: #f0f2f5; }
.nav-btn.active { background: #e9effd; color: var(--accent-dark); font-weight: 600; }
.sidebar-footer { padding: 12px 14px; border-top: 1px solid var(--border); display: flex; align-items: center; justify-content: space-between; }
.conn { font-size: 12px; color: var(--muted); }
.conn.ok { color: var(--ok); }

.main { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.view { display: none; flex-direction: column; height: 100%; min-height: 0; }
.view.active { display: flex; }
.view-header {
  padding: 14px 20px; background: var(--panel); border-bottom: 1px solid var(--border);
  display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap;
}
.view-header h2 { margin: 0; font-size: 18px; }
.title-row { display: flex; align-items: center; gap: 8px; }
.subtitle { color: var(--muted); font-size: 13px; margin: 2px 0 0; }
.header-actions { display: flex; gap: 8px; align-items: center; }

/* --- Chat --- */
.chat-log { flex: 1; overflow-y: auto; padding: 20px; display: flex; flex-direction: column; gap: 12px; }
.msg { max-width: 78%; padding: 10px 14px; border-radius: 12px; word-wrap: break-word; }
.msg.user { align-self: flex-end; background: var(--accent); color: #fff; border-bottom-right-radius: 4px; }
.msg.assistant { align-self: flex-start; background: var(--panel); border: 1px solid var(--border); border-bottom-left-radius: 4px; box-shadow: var(--shadow); }
.msg.system { align-self: center; background: transparent; color: var(--muted); font-size: 12px; border: 0; }
.msg img { max-width: 220px; border-radius: 8px; display: block; margin-top: 6px; }
.msg .file-chip { display: inline-block; background: #eef1f6; border-radius: 6px; padding: 3px 8px; margin: 2px; font-size: 12px; }
.msg .msg-actions { margin-top: 10px; padding-top: 8px; border-top: 1px dashed var(--border); display: flex; flex-wrap: wrap; gap: 6px; }
.msg .gmail-btn { background: #fff; border: 1px solid var(--border); color: var(--accent); border-radius: 18px; padding: 5px 12px; font-size: 12px; cursor: pointer; }
.msg .gmail-btn:hover { background: #eef4ff; border-color: var(--accent); }

/* Rendu Markdown dans les messages */
.msg p { margin: 0 0 8px; }
.msg p:last-child { margin-bottom: 0; }
.msg h1, .msg h2, .msg h3, .msg h4, .msg h5, .msg h6 { margin: 10px 0 6px; line-height: 1.3; font-weight: 700; }
.msg h1 { font-size: 1.35em; }
.msg h2 { font-size: 1.25em; }
.msg h3 { font-size: 1.15em; }
.msg h4, .msg h5, .msg h6 { font-size: 1.05em; }
.msg ul, .msg ol { margin: 4px 0 8px; padding-left: 22px; }
.msg li { margin: 2px 0; }
.msg blockquote { border-left: 3px solid var(--border); padding: 2px 0 2px 12px; margin: 6px 0; color: var(--muted); }
.msg hr { border: 0; border-top: 1px solid var(--border); margin: 10px 0; }
.msg a { color: var(--accent); text-decoration: underline; overflow-wrap: anywhere; }
.msg a:hover { opacity: .85; }
.msg code {
  background: #eef1f6; border-radius: 5px; padding: 1px 5px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 0.9em;
}
.msg pre {
  background: #f1f3f7; border: 1px solid var(--border); border-radius: 8px;
  padding: 10px 12px; margin: 8px 0; overflow-x: auto;
}
.msg pre code { background: none; padding: 0; border-radius: 0; font-size: 0.88em; white-space: pre; }
.msg table { border-collapse: collapse; margin: 8px 0; max-width: 100%; }
.msg th, .msg td { border: 1px solid var(--border); padding: 4px 10px; text-align: left; }
.msg th { background: #f1f3f7; font-weight: 600; }
/* Adaptation au fond coloré des messages utilisateur */
.msg.user code { background: rgba(255, 255, 255, .18); color: #fff; }
.msg.user pre { background: rgba(255, 255, 255, .12); border-color: rgba(255, 255, 255, .3); }
.msg.user pre code { background: none; }
.msg.user a { color: #fff; text-decoration: underline; }
.msg.user blockquote { border-color: rgba(255, 255, 255, .45); color: rgba(255, 255, 255, .9); }
.msg.user th { background: rgba(255, 255, 255, .15); }
.thinking { color: var(--muted); font-style: italic; padding: 8px 4px; }
.chat-tools { padding: 0 20px; display: flex; flex-wrap: wrap; gap: 6px; }
.tool-chip { background: #eef6ef; color: var(--ok); border: 1px solid #cfe8d8; border-radius: 20px; padding: 3px 10px; font-size: 12px; }
.tool-chip.err { background: #fdecec; color: var(--err); border-color: #f3c6c6; }
.chat-input-area {
  display: flex; gap: 8px; padding: 12px 20px; background: var(--panel);
  border-top: 1px solid var(--border); align-items: flex-end;
}
.chat-input-area textarea {
  flex: 1; resize: none; border: 1px solid var(--border); border-radius: 10px;
  padding: 10px 12px; max-height: 160px; min-height: 42px;
}
.upload-label { cursor: pointer; font-size: 20px; padding: 6px 8px; }
.upload-status { padding: 0 20px 10px; color: var(--muted); font-size: 13px; }

/* --- Listes --- */
.list { flex: 1; overflow-y: auto; padding: 16px 20px; display: flex; flex-direction: column; gap: 8px; }
.list-item {
  background: var(--panel); border: 1px solid var(--border); border-radius: 10px;
  padding: 12px 14px; cursor: pointer; box-shadow: var(--shadow);
  display: flex; justify-content: space-between; align-items: center; gap: 10px;
}
.list-item:hover { border-color: var(--accent); }
.list-item .title { font-weight: 600; }
.list-item .meta { color: var(--muted); font-size: 12px; margin-top: 2px; }
.badge { font-size: 11px; padding: 2px 8px; border-radius: 12px; background: #eef1f6; color: var(--muted); }
.badge.ouverte { background: #e9effd; color: var(--accent-dark); }
.badge.en-attente { background: #fdf1e3; color: var(--warn); }
.badge.a-finaliser { background: #fdecec; color: var(--err); }
.badge.finalisee { background: #eef6ef; color: var(--ok); }
.badge.archivee { background: #eef1f6; color: var(--muted); }
.badge.active { background: #eef6ef; color: var(--ok); }
.badge.inactive { background: #eef1f6; color: var(--muted); }
.badge.proposed { background: #fdf1e3; color: var(--warn); }
.badge.rejected { background: #fdecec; color: var(--err); }
.badge.disabled { background: #eef1f6; color: var(--muted); }
.section-sub { border-top: 1px solid var(--border); margin-top: 14px; padding-top: 14px; }
.section-sub .view-header { padding: 6px 20px 2px; }
.section-sub h3 { margin: 0; font-size: 15px; }
.section-sub .hint { padding: 4px 20px 8px; margin: 0; }
.section-sub .list { flex: 0 1 auto; max-height: 260px; }

.detail { flex: 1; overflow-y: auto; padding: 20px; }
.detail pre, .detail .markdown { background: var(--panel); border: 1px solid var(--border); border-radius: 10px; padding: 14px; white-space: pre-wrap; word-wrap: break-word; box-shadow: var(--shadow); }
.detail-actions { display: flex; gap: 8px; margin-bottom: 12px; flex-wrap: wrap; }
.empty { color: var(--muted); text-align: center; padding: 40px 20px; }

/* --- Recherche / select --- */
input[type="search"], select.select, #session-select, #mem-category, #mem-title, #spec-title {
  border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px; background: var(--panel);
}
#memory-search { width: 240px; }
.row { display: flex; gap: 8px; }

/* --- Journal --- */
.journal-entry { background: var(--panel); border: 1px solid var(--border); border-radius: 8px; padding: 8px 12px; font-size: 13px; box-shadow: var(--shadow); }
.journal-entry .ts { color: var(--muted); font-size: 12px; }
.journal-entry .cat { font-weight: 600; }
.journal-entry .lvl-error { color: var(--err); }

/* --- Modal --- */
.modal { position: fixed; inset: 0; background: rgba(16, 24, 40, .4); display: flex; align-items: center; justify-content: center; padding: 16px; z-index: 50; }
.modal-card { background: var(--panel); border-radius: var(--radius); padding: 20px; width: 100%; max-width: 620px; max-height: 90vh; overflow-y: auto; box-shadow: 0 10px 30px rgba(0,0,0,.2); }
.modal-card.wide { max-width: 920px; }
.modal-card .code { background: #f6f7f9; border: 1px solid var(--border); border-radius: 8px; padding: 12px; max-height: 40vh; overflow: auto; white-space: pre-wrap; word-wrap: break-word; font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 12px; margin: 8px 0; }
.modal-card h3 { margin-top: 0; }
.modal-card input, .modal-card textarea, .modal-card select { width: 100%; margin-bottom: 10px; padding: 10px; border: 1px solid var(--border); border-radius: 8px; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; }
.hidden { display: none !important; }

/* --- Liens vers des fichiers locaux (rendus cliquables dans le chat) --- */
.msg a.file-link { text-decoration: underline dotted; }

/* --- Vue de fichier locale (/file) --- */
.file-page { min-height: 100vh; }
.file-view { max-width: 920px; margin: 0 auto; padding: 28px 20px 60px; }
.file-view-head { margin-bottom: 18px; }
.file-view-head h1 { margin: 6px 0 2px; font-size: 20px; word-break: break-all; }
.file-back { color: var(--accent); text-decoration: none; font-size: 14px; }
.file-back:hover { text-decoration: underline; }
.file-body {
  background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 20px 24px; overflow-wrap: break-word;
}
.file-body h1, .file-body h2, .file-body h3, .file-body h4, .file-body h5, .file-body h6 { margin: 16px 0 8px; line-height: 1.3; }
.file-body h1 { font-size: 1.5em; border-bottom: 1px solid var(--border); padding-bottom: 6px; }
.file-body h2 { font-size: 1.3em; }
.file-body h3 { font-size: 1.15em; }
.file-body h4, .file-body h5, .file-body h6 { font-size: 1.05em; }
.file-body p { margin: 0 0 10px; }
.file-body ul, .file-body ol { margin: 6px 0 10px; padding-left: 24px; }
.file-body li { margin: 3px 0; }
.file-body blockquote { border-left: 3px solid var(--border); padding: 2px 0 2px 12px; margin: 8px 0; color: var(--muted); }
.file-body hr { border: 0; border-top: 1px solid var(--border); margin: 14px 0; }
.file-body a { color: var(--accent); }
.file-body a.file-link { text-decoration: underline dotted; }
.file-body code {
  background: #eef1f6; border-radius: 5px; padding: 1px 5px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 0.9em;
}
.file-body pre { background: #f1f3f7; border: 1px solid var(--border); border-radius: 8px; padding: 12px 14px; margin: 10px 0; overflow-x: auto; }
.file-body pre code { background: none; padding: 0; border-radius: 0; font-size: 0.88em; white-space: pre; }
.file-body table { border-collapse: collapse; margin: 10px 0; max-width: 100%; }
.file-body th, .file-body td { border: 1px solid var(--border); padding: 5px 12px; text-align: left; }
.file-body th { background: #f1f3f7; font-weight: 600; }
.file-body.image-view { text-align: center; }
.file-body.image-view img { max-width: 100%; height: auto; border-radius: 8px; }
.file-body.image-view .meta { color: var(--muted); font-size: 13px; margin-top: 10px; }

/* --- Responsive --- */
@media (max-width: 720px) {
  .sidebar { width: 64px; }
  .brand { font-size: 13px; padding: 16px 8px; text-align: center; }
  .nav-btn { font-size: 0; padding: 12px; text-align: center; }
  .nav-btn::after { content: attr(data-emoji); font-size: 20px; }
  .sidebar-footer { flex-direction: column; gap: 6px; }
  .msg { max-width: 92%; }
  .view-header { padding: 10px 12px; }
  .chat-input-area { padding: 8px 10px; }
}
