:root {
  --bg: #f7f7f8; --panel: #ffffff; --side: #f0f1f4; --text: #1d2330; --muted: #6b7280; --border: #e3e5ea;
  --accent: #4f46e5; --accent-soft: #eef0ff; --accent-text: #fff; --user: #4f46e5; --user-text: #fff;
  --danger: #dc2626; --code: #f3f4f6; --tool: #f5f3ff; --ok: #059669; --shadow: 0 1px 3px rgba(0,0,0,.06);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111318; --panel: #1a1d24; --side: #15181e; --text: #e6e8ec; --muted: #9aa3b2; --border: #2a2f3a;
    --accent: #818cf8; --accent-soft: #23264a; --accent-text: #0b0d12; --user: #4f46e5; --user-text: #fff;
    --danger: #f87171; --code: #232733; --tool: #1f1d33; --ok: #34d399; --shadow: none;
    color-scheme: dark;
  }
}
* { box-sizing: border-box; }
html, body { height: 100%; }
body { margin: 0; background: var(--bg); color: var(--text); font: 15px/1.55 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; }
button { font: inherit; color: inherit; cursor: pointer; }
[hidden] { display: none !important; }

/* Connexion */
.login-page { display: grid; place-items: center; padding: 16px; }
.login-card { width: 100%; max-width: 360px; background: var(--panel); border: 1px solid var(--border); border-radius: 16px; padding: 28px; box-shadow: var(--shadow); display: grid; gap: 14px; }
.login-card .logo { color: var(--accent); }
.login-card h1 { margin: 0 0 4px; font-size: 22px; }
.login-card label { display: grid; gap: 4px; font-weight: 600; font-size: 13px; }
.login-card input { padding: 10px 12px; border: 1px solid var(--border); border-radius: 10px; background: var(--bg); color: var(--text); font: inherit; }
.alert { background: #fdecea; color: #b42318; padding: 8px 12px; border-radius: 8px; font-size: 14px; }
.primary { background: var(--accent); color: var(--accent-text); border: 0; border-radius: 10px; padding: 11px; font-weight: 600; }

/* Structure */
.app { display: flex; height: 100dvh; }
.sidebar { width: 270px; flex: none; background: var(--side); border-right: 1px solid var(--border); display: flex; flex-direction: column; padding: 12px; padding-top: max(12px, env(safe-area-inset-top)); gap: 10px; }
.new-chat { display: flex; align-items: center; gap: 8px; justify-content: center; padding: 10px; border-radius: 10px; border: 1px solid var(--border); background: var(--panel); font-weight: 600; }
.conv-list { flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 2px; }
.conv { display: flex; align-items: center; border-radius: 8px; }
.conv a { flex: 1; padding: 8px 10px; color: var(--text); text-decoration: none; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: 14px; }
.conv:hover, .conv.active { background: var(--panel); }
.conv .del { opacity: 0; border: 0; background: none; color: var(--muted); padding: 6px 8px; }
.conv:hover .del, .conv.active .del { opacity: 1; }
.side-foot { display: flex; justify-content: space-between; align-items: center; border-top: 1px solid var(--border); padding-top: 10px; font-size: 14px; padding-bottom: env(safe-area-inset-bottom); }
.who { font-weight: 600; overflow: hidden; text-overflow: ellipsis; }
.link { background: none; border: 0; color: var(--muted); }
.backdrop { display: none; }
.main { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.topbar { display: flex; align-items: center; gap: 10px; padding: 10px 16px; padding-top: max(10px, env(safe-area-inset-top)); border-bottom: 1px solid var(--border); background: var(--panel); }
.topbar h1 { font-size: 16px; margin: 0; flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#menuBtn { display: none; }
.status { display: flex; align-items: center; gap: 6px; font-size: 13px; color: var(--muted); }
.status .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--accent); animation: pulse 1.2s infinite; }
@keyframes pulse { 50% { opacity: .3; } }

/* Messages */
.messages { flex: 1; overflow-y: auto; padding: 20px 16px; display: flex; flex-direction: column; gap: 16px; }
.messages > * { width: 100%; max-width: 820px; margin-inline: auto; }
.empty { text-align: center; color: var(--muted); margin-top: 12vh; }
.empty h2 { color: var(--text); }
.msg { display: flex; flex-direction: column; gap: 6px; }
.msg.user { align-items: flex-end; }
.bubble { padding: 10px 14px; border-radius: 16px; max-width: 85%; overflow-wrap: anywhere; }
.msg.user .bubble { background: var(--user); color: var(--user-text); border-bottom-right-radius: 4px; white-space: pre-wrap; }
.msg.assistant .bubble { background: var(--panel); border: 1px solid var(--border); border-bottom-left-radius: 4px; max-width: 100%; }
.msg.voice .bubble::before { content: "🎙 "; }
.bubble p { margin: 0 0 .6em; } .bubble p:last-child { margin-bottom: 0; }
.bubble pre { background: var(--code); padding: 10px; border-radius: 8px; overflow-x: auto; font-size: 13px; }
.bubble code { background: var(--code); padding: 1px 5px; border-radius: 4px; font-size: 13px; }
.bubble pre code { background: none; padding: 0; }
.bubble table { border-collapse: collapse; display: block; overflow-x: auto; }
.bubble th, .bubble td { border: 1px solid var(--border); padding: 4px 8px; }
.bubble a { color: var(--accent); }
.bubble.typing::after { content: "▍"; animation: pulse 1s infinite; color: var(--accent); }
.error-msg { color: var(--danger); font-size: 14px; }
.notice { color: var(--muted); font-size: 13px; font-style: italic; }
details.tool, details.reasoning { background: var(--tool); border-radius: 10px; padding: 6px 10px; font-size: 13px; max-width: 100%; }
details.tool summary, details.reasoning summary { cursor: pointer; color: var(--muted); }
details.tool.err summary { color: var(--danger); }
details.tool pre, details.reasoning div { white-space: pre-wrap; overflow-wrap: anywhere; margin: 6px 0 0; font-size: 12px; max-height: 260px; overflow: auto; }
.msg-actions { display: flex; gap: 4px; }
.msg-actions button { border: 0; background: none; color: var(--muted); padding: 2px 6px; border-radius: 6px; font-size: 13px; }
.msg-actions button:hover { background: var(--code); }

/* Saisie */
.composer { border-top: 1px solid var(--border); background: var(--panel); padding: 10px 16px; padding-bottom: max(10px, env(safe-area-inset-bottom)); }
.composer > * { max-width: 820px; margin-inline: auto; }
.input-row { display: flex; align-items: flex-end; gap: 6px; background: var(--bg); border: 1px solid var(--border); border-radius: 14px; padding: 6px; }
.input-row textarea { flex: 1; resize: none; border: 0; background: none; color: var(--text); font: inherit; padding: 8px; max-height: 200px; outline: none; }
.icon { border: 0; background: none; width: 40px; height: 40px; border-radius: 10px; display: grid; place-items: center; color: var(--muted); flex: none; }
.icon:hover { background: var(--code); color: var(--text); }
.icon.send { background: var(--accent); color: var(--accent-text); }
.icon.send:disabled { opacity: .4; cursor: default; }
.icon.stop { background: var(--text); color: var(--panel); }
.icon.recording { background: var(--danger); color: #fff; animation: pulse 1.2s infinite; }
.icon.active { background: var(--accent-soft); color: var(--accent); }
.option { display: flex; align-items: center; gap: 6px; font-size: 13px; color: var(--muted); margin-top: 6px; }

/* Mode conversation */
.voice-bar { display: flex; align-items: center; gap: 14px; background: var(--accent-soft); border-radius: 14px; padding: 10px 14px; margin-bottom: 10px; }
.voice-text { flex: 1; display: grid; }
.voice-text small { color: var(--muted); }
.orb { width: 44px; height: 44px; border-radius: 50%; background: var(--accent); display: grid; place-items: center; flex: none; transition: transform .08s; }
.orb span { width: 14px; height: 14px; border-radius: 50%; background: var(--accent-text); }
.orb.speaking { animation: pulse 1s infinite; }
.orb.busy span { animation: spin 1s linear infinite; border-radius: 3px; }
@keyframes spin { to { transform: rotate(360deg); } }
.pill { border: 1px solid var(--border); background: var(--panel); border-radius: 99px; padding: 6px 14px; }

@media (max-width: 760px) {
  #menuBtn { display: grid; }
  .sidebar { position: fixed; z-index: 20; inset: 0 auto 0 0; transform: translateX(-100%); transition: transform .2s; }
  .app.menu-open .sidebar { transform: none; }
  .app.menu-open .backdrop { display: block; position: fixed; inset: 0; background: rgba(0,0,0,.35); z-index: 10; }
  .bubble { max-width: 92%; }
}
