:root {
  --bg: #ffffff;
  --bg-soft: #f4f4f5;
  --fg: #18181b;
  --muted: #71717a;
  --border: #e4e4e7;
  --accent: #2563eb;
  --accent-fg: #ffffff;
  --bubble: #eef2ff;
  --code-bg: #f6f8fa;
  --danger: #b91c1c;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #18181b;
    --bg-soft: #232326;
    --fg: #e4e4e7;
    --muted: #a1a1aa;
    --border: #3f3f46;
    --accent: #3b82f6;
    --bubble: #27304a;
    --code-bg: #0d1117;
    --danger: #f87171;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #18181b;
  --bg-soft: #232326;
  --fg: #e4e4e7;
  --muted: #a1a1aa;
  --border: #3f3f46;
  --accent: #3b82f6;
  --bubble: #27304a;
  --code-bg: #0d1117;
  --danger: #f87171;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--fg);
  font: 16px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
}
h1 { font-size: 1.25rem; margin: 0; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: .875rem; }
.center { text-align: center; }

/* Vues plein écran : même logique sur mobile et ordinateur */
.view { display: none; height: 100vh; height: 100dvh; flex-direction: column; }
.view.active { display: flex; }
.page { width: 100%; max-width: 820px; margin: 0 auto; padding: 0 16px; }
.scroll { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; }

.bar {
  display: flex; align-items: center; gap: 8px;
  padding: 8px 12px; padding-top: max(8px, env(safe-area-inset-top));
  border-bottom: 1px solid var(--border); background: var(--bg);
}
.bar-title { flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.btn {
  font: inherit; border: 1px solid var(--border); background: var(--bg-soft); color: var(--fg);
  padding: 10px 16px; border-radius: 10px; cursor: pointer;
}
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-fg); }
.btn.wide { width: 100%; }
.btn:disabled { opacity: .6; cursor: default; }
.icon-btn {
  font: inherit; font-size: 1.25rem; line-height: 1; width: 40px; height: 40px; flex: none;
  border: none; border-radius: 10px; background: transparent; color: var(--fg); cursor: pointer;
}
.icon-btn:hover, .btn:hover { filter: brightness(.95); }
.tag { font-size: .75rem; border: 1px solid var(--border); border-radius: 6px; padding: 2px 6px; color: var(--muted); }

input[type=email], textarea {
  font: inherit; color: var(--fg); background: var(--bg); width: 100%;
  border: 1px solid var(--border); border-radius: 10px; padding: 10px 12px;
}

/* Authentification */
.auth-box { width: 100%; max-width: 420px; margin: auto; padding: 24px 16px; }
.auth-box h1 { font-size: 1.75rem; margin-bottom: 24px; text-align: center; }
.auth-panel { display: flex; flex-direction: column; gap: 12px; }
.stack { display: flex; flex-direction: column; gap: 8px; margin-top: 12px; }
details summary { cursor: pointer; color: var(--accent); }
.auth-message { min-height: 1.5em; margin-top: 16px; }
.auth-message.error { color: var(--danger); }

/* Liste */
.list-tools { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 12px; margin: 16px 0; }
.check { display: flex; align-items: center; gap: 6px; color: var(--muted); }
.conv-list { list-style: none; margin: 0 0 24px; padding: 0; }
.conv-item { display: flex; align-items: center; border-bottom: 1px solid var(--border); }
.conv-link { flex: 1; min-width: 0; padding: 12px 4px; color: inherit; text-decoration: none; }
.conv-title { display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv-date { display: block; font-size: .8rem; color: var(--muted); }
.conv-item.archived .conv-title { color: var(--muted); }

/* Menu contextuel */
.menu {
  position: fixed; z-index: 10; min-width: 180px; padding: 4px;
  background: var(--bg); border: 1px solid var(--border); border-radius: 10px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, .18);
}
.menu button {
  display: block; width: 100%; text-align: left; font: inherit; color: var(--fg);
  background: none; border: none; padding: 10px 12px; border-radius: 6px; cursor: pointer;
}
.menu button:hover { background: var(--bg-soft); }
.menu button.danger { color: var(--danger); }

/* Messages */
.messages { padding-top: 16px; padding-bottom: 16px; }
.msg { margin-bottom: 20px; }
.msg.user { display: flex; flex-direction: column; align-items: flex-end; }
.msg.user .content {
  max-width: 85%; background: var(--bubble); border-radius: 16px 16px 4px 16px;
  padding: 10px 14px; white-space: pre-wrap; overflow-wrap: anywhere;
}
.msg.assistant .content { overflow-wrap: anywhere; }
.msg .time { font-size: .75rem; color: var(--muted); margin-top: 4px; font-variant-numeric: tabular-nums; }
.msg .note { font-size: .8rem; color: var(--danger); margin-top: 4px; }
.thinking { color: var(--muted); font-style: italic; }
.thinking::after { content: ""; animation: dots 1.2s steps(4, end) infinite; }
@keyframes dots { 0% { content: ""; } 25% { content: "."; } 50% { content: ".."; } 75% { content: "..."; } }

/* Markdown de l'assistant */
.content > :first-child { margin-top: 0; }
.content > :last-child { margin-bottom: 0; }
.content pre { margin: 0; padding: 12px; overflow-x: auto; background: var(--code-bg); border-radius: 0 0 8px 8px; }
.content code { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: .9em; }
.content :not(pre) > code { background: var(--bg-soft); padding: 1px 4px; border-radius: 4px; }
.content pre code.hljs { padding: 0; background: transparent; }
.code-block { margin: 12px 0; border: 1px solid var(--border); border-radius: 8px; }
.code-head {
  display: flex; justify-content: space-between; align-items: center;
  padding: 4px 8px 4px 12px; font-size: .8rem; color: var(--muted); border-bottom: 1px solid var(--border);
}
.code-head button { font: inherit; background: none; border: none; color: var(--muted); cursor: pointer; padding: 4px 6px; }
.table-wrap { overflow-x: auto; margin: 12px 0; }
.content table { border-collapse: collapse; }
.content th, .content td { border: 1px solid var(--border); padding: 6px 10px; text-align: left; vertical-align: top; }
.content img { max-width: 100%; }
.content blockquote { margin: 0; padding-left: 12px; border-left: 3px solid var(--border); color: var(--muted); }

/* Zone de saisie */
.composer { border-top: 1px solid var(--border); background: var(--bg); padding: 10px 0; padding-bottom: max(10px, env(safe-area-inset-bottom)); }
.composer-inner { display: flex; gap: 8px; align-items: flex-end; }
.composer textarea { resize: none; max-height: 40vh; }
.composer .btn { flex: none; }
