:root {
  --bg: #f6f7f9;
  --panel: #ffffff;
  --text: #1c1f24;
  --muted: #6b7280;
  --border: #e3e5e8;
  --accent: #2563eb;
  --user-bg: #2563eb;
  --user-text: #ffffff;
  --error: #dc2626;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111317;
    --panel: #1a1d23;
    --text: #e6e8eb;
    --muted: #9aa1ab;
    --border: #2a2e36;
    --accent: #3b82f6;
    --user-bg: #2b4a8a;
    --user-text: #eef2ff;
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.55 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

[hidden] { display: none !important; }

button {
  font: inherit;
  border: 0;
  border-radius: 8px;
  padding: 8px 14px;
  background: var(--accent);
  color: #fff;
  cursor: pointer;
}
button.ghost {
  background: transparent;
  color: var(--text);
  border: 1px solid var(--border);
}
input, select, textarea {
  font: inherit;
  color: var(--text);
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 10px;
}

/* Connexion */
.login {
  min-height: 100%;
  display: grid;
  place-items: center;
  padding: 16px;
}
.card {
  width: min(360px, 100%);
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 28px;
  display: grid;
  gap: 14px;
}
.card h1 { margin: 0 0 6px; font-size: 22px; }
.card label { display: grid; gap: 6px; color: var(--muted); font-size: 14px; }
.error { color: var(--error); margin: 0; }
.success { color: #16a34a; margin: 0; }

dialog {
  padding: 0;
  border: 0;
  background: transparent;
  color: var(--text);
  width: min(380px, calc(100% - 32px));
}
dialog::backdrop { background: #0008; }
dialog .card { width: 100%; }
.card h2 { margin: 0 0 6px; font-size: 19px; }
.row { display: flex; justify-content: flex-end; gap: 10px; }

/* Chat */
.app {
  height: 100%;
  display: flex;
  flex-direction: column;
}
header {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px;
  padding: 10px 16px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
}
.quota { color: var(--muted); font-size: 13px; }
.spacer { flex: 1; }
.icon { padding: 6px 10px; line-height: 1; }
#toggle-sidebar { display: none; }

/* Colonne historique + zone de chat */
.layout {
  flex: 1;
  display: flex;
  min-height: 0;
  position: relative;
}
.sidebar {
  width: 260px;
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 12px;
  background: var(--panel);
  border-right: 1px solid var(--border);
  overflow-y: auto;
}
.chat-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  gap: 2px;
}
.chat-list li {
  display: flex;
  align-items: center;
  border-radius: 8px;
}
.chat-list li:hover, .chat-list li.active { background: var(--bg); }
.chat-list li.active .chat-title { font-weight: 600; }
.chat-list li.muted { color: var(--muted); font-size: 13px; padding: 6px 8px; }
.chat-title {
  flex: 1;
  min-width: 0;
  text-align: left;
  background: transparent;
  color: var(--text);
  padding: 7px 8px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.chat-delete {
  background: transparent;
  color: var(--muted);
  padding: 4px 9px;
  font-size: 17px;
  visibility: hidden;
}
.chat-list li:hover .chat-delete, .chat-list li.active .chat-delete { visibility: visible; }
.chat-delete:hover { color: var(--error); }
.sidebar-backdrop { display: none; }

.chat {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
}

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 20px 16px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  width: 100%;
  max-width: 900px;
  margin: 0 auto;
}
.empty { color: var(--muted); text-align: center; margin-top: 20vh; }

.msg {
  max-width: 100%;
  padding: 10px 14px;
  border-radius: 12px;
  overflow-wrap: anywhere;
}
.msg.user {
  align-self: flex-end;
  max-width: 85%;
  background: var(--user-bg);
  color: var(--user-text);
  white-space: pre-wrap;
}
.msg.assistant {
  background: var(--panel);
  border: 1px solid var(--border);
}
.msg.assistant > :first-child { margin-top: 0; }
.msg.assistant > :last-child { margin-bottom: 0; }
.msg.pending:empty::after { content: "…"; color: var(--muted); }
.msg blockquote { margin: 8px 0; padding-left: 10px; border-left: 3px solid var(--border); color: var(--muted); }
.msg code { font-family: ui-monospace, "Cascadia Code", Consolas, monospace; font-size: 13.5px; }
.msg :not(pre) > code { background: var(--bg); padding: 1px 5px; border-radius: 4px; }
.msg pre {
  position: relative;
  margin: 10px 0;
  border-radius: 8px;
  overflow: hidden;
}
.msg pre code.hljs { padding: 14px; overflow-x: auto; display: block; }
.msg table { border-collapse: collapse; }
.msg th, .msg td { border: 1px solid var(--border); padding: 4px 8px; }

.copy {
  position: absolute;
  top: 6px;
  right: 6px;
  padding: 3px 9px;
  font-size: 12px;
  background: #ffffff22;
  color: #fff;
  opacity: 0.7;
}
.copy:hover { opacity: 1; }

.composer {
  display: flex;
  gap: 10px;
  padding: 12px 16px 16px;
  width: 100%;
  max-width: 900px;
  margin: 0 auto;
}
.composer textarea { flex: 1; resize: vertical; min-height: 56px; max-height: 40vh; }
.actions { display: flex; flex-direction: column; justify-content: flex-end; }

/* Petits écrans : l'historique devient un panneau qui s'ouvre avec ☰ */
@media (max-width: 800px) {
  #toggle-sidebar { display: inline-block; }
  .sidebar {
    position: absolute;
    inset: 0 auto 0 0;
    z-index: 10;
    width: min(280px, 85%);
    transform: translateX(-100%);
    transition: transform 0.2s;
  }
  .sidebar-open .sidebar { transform: none; }
  .sidebar-open .sidebar-backdrop {
    display: block;
    position: absolute;
    inset: 0;
    z-index: 9;
    background: #0006;
  }
  .chat-delete { visibility: visible; }
}

@media (max-width: 600px) {
  header { gap: 8px; }
  .quota { order: 10; width: 100%; }
  .msg.user { max-width: 95%; }
}
