:root {
  --bg: #0e1016; --bg2: #141720; --panel: #181c26; --panel2: #1f2430; --line: #2a3040;
  --text: #e8eaf0; --muted: #9aa3b5; --accent: #a78bfa; --accent2: #22d3ee; --accent-ink: #0e1016;
  --danger: #f87171; --ok: #34d399; --warn: #fbbf24; --code: #0b0d12;
  --user: #262c3b; --radius: 14px; --shadow: 0 10px 30px rgba(0,0,0,.35);
  --grad: linear-gradient(135deg, var(--accent), var(--accent2));
  color-scheme: dark;
}
:root[data-theme="light"] {
  --bg: #f6f7fb; --bg2: #eef0f6; --panel: #ffffff; --panel2: #f1f3f8; --line: #dde1ea;
  --text: #1a1d26; --muted: #5d6576; --accent: #7c5cf0; --accent2: #0891b2; --accent-ink: #ffffff;
  --danger: #dc2626; --ok: #059669; --warn: #b45309; --code: #f3f4f8; --user: #e8e5fb;
  --shadow: 0 10px 30px rgba(20,24,40,.12);
  color-scheme: light;
}
@media (prefers-color-scheme: light) {
  :root:not([data-theme="dark"]):not([data-theme="light"]) {
    --bg: #f6f7fb; --bg2: #eef0f6; --panel: #ffffff; --panel2: #f1f3f8; --line: #dde1ea;
    --text: #1a1d26; --muted: #5d6576; --accent: #7c5cf0; --accent2: #0891b2; --accent-ink: #ffffff;
    --danger: #dc2626; --ok: #059669; --warn: #b45309; --code: #f3f4f8; --user: #e8e5fb;
    --shadow: 0 10px 30px rgba(20,24,40,.12);
    color-scheme: light;
  }
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { background: var(--bg); color: var(--text); font: 15px/1.55 system-ui, -apple-system, "Segoe UI", Roboto, Ubuntu, sans-serif;
  -webkit-font-smoothing: antialiased; overflow: hidden; }
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--accent2); }
[hidden] { display: none !important; }
.muted { color: var(--muted); }

/* ---------- boutons ---------- */
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; padding: 9px 14px; border-radius: 10px;
  border: 1px solid var(--line); background: var(--panel2); cursor: pointer; text-decoration: none; color: var(--text);
  min-height: 40px; transition: background .15s, border-color .15s; }
.btn:hover { border-color: var(--accent); }
.btn.primary { background: var(--grad); color: #0b0d12; border: 0; font-weight: 600; }
.btn.danger { border-color: color-mix(in srgb, var(--danger) 60%, transparent); color: var(--danger); }
.btn.wide { width: 100%; }
.btn:disabled { opacity: .55; cursor: default; }
.btn.small { min-height: 30px; padding: 4px 10px; font-size: 13px; }
.icon-btn { background: none; border: 0; width: 40px; height: 40px; border-radius: 10px; cursor: pointer; font-size: 18px;
  display: inline-flex; align-items: center; justify-content: center; color: var(--muted); }
.icon-btn:hover { background: var(--panel2); color: var(--text); }
.link-btn { background: none; border: 0; color: var(--muted); cursor: pointer; text-decoration: underline; padding: 6px; }
.input { width: 100%; background: var(--bg2); border: 1px solid var(--line); border-radius: 10px; padding: 9px 12px; min-height: 40px; }
.input:focus, textarea:focus { outline: 2px solid color-mix(in srgb, var(--accent) 60%, transparent); outline-offset: 0; }
label { display: grid; gap: 6px; color: var(--muted); font-size: 13px; }
label.check { display: flex; align-items: center; gap: 8px; color: var(--text); font-size: 14px; }

/* ---------- connexion ---------- */
.login-body { display: grid; place-items: center; overflow: auto; padding: 16px;
  background: radial-gradient(circle at 20% 10%, color-mix(in srgb, var(--accent) 18%, transparent), transparent 40%),
              radial-gradient(circle at 80% 90%, color-mix(in srgb, var(--accent2) 14%, transparent), transparent 45%), var(--bg); }
.login-card { width: min(380px, 100%); background: var(--panel); border: 1px solid var(--line); border-radius: 22px; padding: 32px 26px;
  box-shadow: var(--shadow); text-align: center; }
.login-card h1 { margin: 10px 0 2px; font-size: 30px; letter-spacing: .18em; }
.login-sub { color: var(--muted); margin: 0 0 22px; }
.login-card form { display: grid; gap: 14px; text-align: left; }
.login-card input { width: 100%; background: var(--bg2); border: 1px solid var(--line); border-radius: 10px; padding: 11px 12px; }
.login-error { color: var(--danger); margin: 0; text-align: center; }

/* ---------- structure ---------- */
.app { display: grid; grid-template-columns: 290px 1fr; height: 100dvh; }
.sidebar { background: var(--bg2); border-right: 1px solid var(--line); display: flex; flex-direction: column; gap: 10px; padding: 14px 12px;
  min-height: 0; padding-top: max(14px, env(safe-area-inset-top)); }
.side-head { display: flex; align-items: center; justify-content: space-between; }
.brand { display: flex; align-items: center; gap: 10px; color: var(--text); text-decoration: none; font-weight: 700; letter-spacing: .2em; font-size: 18px; }
.side-nav { display: grid; gap: 2px; }
.nav-btn { text-align: left; background: none; border: 0; padding: 9px 10px; border-radius: 10px; cursor: pointer; color: var(--muted); }
.nav-btn.active, .nav-btn:hover { background: var(--panel2); color: var(--text); }
.conv-list { flex: 1; overflow-y: auto; display: grid; align-content: start; gap: 2px; margin: 0 -4px; padding: 0 4px; }
.conv-item { display: flex; align-items: center; gap: 4px; border-radius: 10px; padding: 2px 2px 2px 10px; cursor: pointer; color: var(--muted); }
.conv-item:hover, .conv-item.active { background: var(--panel2); color: var(--text); }
.conv-item .t { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; padding: 7px 0; }
.conv-item .icon-btn { width: 32px; height: 32px; font-size: 15px; opacity: 0; }
.conv-item:hover .icon-btn, .conv-item.active .icon-btn { opacity: 1; }
.conv-group { font-size: 12px; color: var(--muted); padding: 10px 10px 4px; text-transform: uppercase; letter-spacing: .06em; }
.side-foot { display: flex; justify-content: space-between; align-items: center; font-size: 13px; padding-bottom: env(safe-area-inset-bottom); }
.main { min-width: 0; min-height: 0; display: flex; flex-direction: column; }
.view { display: flex; flex-direction: column; min-height: 0; flex: 1; }
.topbar { display: flex; align-items: center; gap: 10px; padding: 10px 16px; border-bottom: 1px solid var(--line);
  padding-top: max(10px, env(safe-area-inset-top)); min-height: 58px; }
.topbar-title { flex: 1; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.model-btn { display: flex; align-items: center; gap: 8px; max-width: min(460px, 60vw); background: var(--panel); border: 1px solid var(--line);
  padding: 7px 12px; border-radius: 12px; cursor: pointer; min-height: 40px; }
.model-btn:hover { border-color: var(--accent); }
.model-btn-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.model-btn-src { color: var(--muted); font-size: 12px; white-space: nowrap; }
.only-mobile { display: none; }
.scrim { position: fixed; inset: 0; background: rgba(0,0,0,.45); z-index: 9; }

/* ---------- messages ---------- */
.messages { flex: 1; overflow-y: auto; padding: 20px 16px 8px; scroll-behavior: smooth; }
.msg { max-width: 860px; margin: 0 auto 18px; display: flex; flex-direction: column; gap: 6px; }
.msg.user { align-items: flex-end; }
.bubble { padding: 11px 15px; border-radius: 16px; max-width: 100%; overflow-wrap: anywhere; }
.msg.user .bubble { background: var(--user); border-bottom-right-radius: 6px; white-space: pre-wrap; max-width: min(720px, 92%); }
.msg.assistant .bubble { background: transparent; padding-left: 2px; padding-right: 2px; width: 100%; }
.msg-meta { font-size: 12px; color: var(--muted); display: flex; align-items: center; gap: 4px; flex-wrap: wrap; }
.msg-actions { display: flex; gap: 2px; }
.msg-actions .icon-btn { width: 32px; height: 32px; font-size: 14px; }
.msg-error { color: var(--danger); background: color-mix(in srgb, var(--danger) 10%, transparent); border: 1px solid color-mix(in srgb, var(--danger) 35%, transparent);
  padding: 8px 12px; border-radius: 10px; font-size: 14px; }
.msg-note { color: var(--warn); font-size: 13px; }
.msg-atts { display: flex; flex-wrap: wrap; gap: 6px; justify-content: flex-end; }
.msg-atts img { width: 120px; height: 120px; object-fit: cover; border-radius: 10px; border: 1px solid var(--line); cursor: zoom-in; }
.file-chip { display: inline-flex; align-items: center; gap: 6px; padding: 6px 10px; border-radius: 10px; background: var(--panel2);
  border: 1px solid var(--line); font-size: 13px; max-width: 260px; }
.file-chip span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.reasoning { border-left: 3px solid var(--line); padding: 2px 10px; color: var(--muted); font-size: 13px; white-space: pre-wrap; margin-bottom: 6px; }
details.reasoning-box summary { cursor: pointer; color: var(--muted); font-size: 13px; }
.typing { display: inline-flex; gap: 4px; padding: 6px 2px; }
.typing i { width: 7px; height: 7px; border-radius: 50%; background: var(--muted); animation: blink 1.2s infinite both; }
.typing i:nth-child(2) { animation-delay: .2s; } .typing i:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 0%, 80%, 100% { opacity: .25; } 40% { opacity: 1; } }
.empty { max-width: 520px; margin: 12vh auto 0; text-align: center; color: var(--muted); }
.empty h2 { color: var(--text); margin: 12px 0 6px; }

/* rendu markdown */
.md > :first-child { margin-top: 0; } .md > :last-child { margin-bottom: 0; }
.md p, .md ul, .md ol { margin: .55em 0; }
.md h1, .md h2, .md h3 { margin: 1em 0 .4em; line-height: 1.3; }
.md h1 { font-size: 1.35em; } .md h2 { font-size: 1.2em; } .md h3 { font-size: 1.05em; }
.md code { background: var(--panel2); padding: .12em .38em; border-radius: 6px; font-size: .92em; font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace; }
.md pre { position: relative; background: var(--code); border: 1px solid var(--line); border-radius: 12px; padding: 0; overflow: hidden; margin: .8em 0; }
.md pre code { display: block; padding: 14px; overflow-x: auto; background: none; font-size: 13.5px; line-height: 1.5; }
.code-head { display: flex; justify-content: space-between; align-items: center; padding: 4px 6px 4px 12px; font-size: 12px; color: var(--muted);
  border-bottom: 1px solid var(--line); background: var(--panel2); }
.code-head button { background: none; border: 0; color: var(--muted); cursor: pointer; padding: 4px 8px; border-radius: 6px; font-size: 12px; }
.code-head button:hover { color: var(--text); background: var(--panel); }
.md table { border-collapse: collapse; display: block; overflow-x: auto; max-width: 100%; margin: .8em 0; font-size: 14px; }
.md th, .md td { border: 1px solid var(--line); padding: 6px 10px; text-align: left; vertical-align: top; }
.md th { background: var(--panel2); }
.md blockquote { border-left: 3px solid var(--accent); margin: .6em 0; padding: 2px 12px; color: var(--muted); }
.md img { max-width: 100%; }
.md a { overflow-wrap: anywhere; }

/* ---------- saisie ---------- */
.composer-wrap { padding: 8px 16px calc(12px + env(safe-area-inset-bottom)); position: relative; }
.composer { max-width: 860px; margin: 0 auto; display: flex; align-items: flex-end; gap: 6px; background: var(--panel); border: 1px solid var(--line);
  border-radius: 18px; padding: 6px; box-shadow: var(--shadow); }
.composer:focus-within { border-color: color-mix(in srgb, var(--accent) 70%, transparent); }
.composer textarea { flex: 1; resize: none; border: 0; background: transparent; padding: 9px 4px; max-height: 40vh; min-height: 40px; outline: none; }
.composer textarea:focus { outline: none; }
.send-btn { width: 44px; height: 44px; border-radius: 13px; border: 0; background: var(--grad); color: #0b0d12; font-size: 18px; cursor: pointer; flex: none; }
.send-btn:disabled { opacity: .4; cursor: default; }
.send-btn.stop { background: var(--danger); color: #fff; }
.composer-help { text-align: center; font-size: 12px; margin-top: 6px; }
.composer-note { max-width: 860px; margin: 0 auto 6px; font-size: 13px; color: var(--warn); background: color-mix(in srgb, var(--warn) 10%, transparent);
  border: 1px solid color-mix(in srgb, var(--warn) 35%, transparent); border-radius: 10px; padding: 8px 12px; }
.composer-note button { margin: 4px 6px 0 0; }
.attach-row { max-width: 860px; margin: 0 auto 6px; display: flex; gap: 8px; flex-wrap: wrap; }
.att { position: relative; border-radius: 12px; border: 1px solid var(--line); background: var(--panel); }
.att img { width: 72px; height: 72px; object-fit: cover; border-radius: 12px; display: block; }
.att .file-chip { border: 0; height: 72px; }
.att .rm { position: absolute; top: -8px; right: -8px; width: 26px; height: 26px; border-radius: 50%; border: 1px solid var(--line);
  background: var(--panel2); cursor: pointer; font-size: 13px; color: var(--text); }
.att.loading { opacity: .6; }
.att .att-state { position: absolute; inset: auto 4px 4px 4px; font-size: 11px; background: rgba(0,0,0,.6); color: #fff; border-radius: 6px; text-align: center; }
.attach-menu { position: relative; }
.menu { position: absolute; bottom: 48px; left: 0; background: var(--panel); border: 1px solid var(--line); border-radius: 12px; box-shadow: var(--shadow);
  padding: 6px; display: grid; min-width: 220px; z-index: 5; }
.menu button { background: none; border: 0; text-align: left; padding: 10px 12px; border-radius: 8px; cursor: pointer; }
.menu button:hover { background: var(--panel2); }
.drop-hint { position: absolute; inset: 0; display: grid; place-items: center; background: color-mix(in srgb, var(--accent) 18%, var(--bg));
  border: 2px dashed var(--accent); border-radius: 18px; z-index: 4; font-weight: 600; }

/* ---------- dialogues ---------- */
.dialog { border: 1px solid var(--line); background: var(--panel); color: var(--text); border-radius: 18px; padding: 0; width: min(760px, 96vw);
  max-height: 86dvh; box-shadow: var(--shadow); }
.dialog[open] { display: flex; flex-direction: column; }
.dialog::backdrop { background: rgba(0,0,0,.5); }
.dialog.small { width: min(420px, 94vw); padding: 18px; }
.dialog.small form { display: grid; gap: 12px; }
.dialog-head { display: flex; justify-content: space-between; align-items: center; padding: 12px 14px 4px 18px; }
.dialog-head h3 { margin: 0; }
.dialog-foot { padding: 8px 18px 12px; font-size: 12px; }
.model-filters { display: flex; flex-wrap: wrap; gap: 8px; padding: 8px 18px; align-items: center; }
.model-filters .input { flex: 1 1 200px; }
.model-list { overflow-y: auto; padding: 4px 10px 8px; min-height: 200px; }
.model-group { font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: .06em; padding: 12px 8px 4px; display: flex; justify-content: space-between; gap: 8px; }
.model-item { display: flex; align-items: center; gap: 10px; padding: 8px; border-radius: 10px; cursor: pointer; }
.model-item:hover, .model-item.active { background: var(--panel2); }
.model-item .mi-main { flex: 1; min-width: 0; }
.model-item .mi-name { font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.model-item .mi-id { font-size: 12px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.model-item.off { opacity: .5; }
.badges { display: flex; gap: 4px; flex-wrap: wrap; }
.badge { font-size: 11px; padding: 2px 7px; border-radius: 99px; border: 1px solid var(--line); color: var(--muted); white-space: nowrap; }
.badge.ok { color: var(--ok); border-color: color-mix(in srgb, var(--ok) 45%, transparent); }
.badge.warn { color: var(--warn); border-color: color-mix(in srgb, var(--warn) 45%, transparent); }
.badge.bad { color: var(--danger); border-color: color-mix(in srgb, var(--danger) 45%, transparent); }
.fav { background: none; border: 0; cursor: pointer; font-size: 18px; color: var(--muted); width: 34px; height: 34px; border-radius: 8px; }
.fav.on { color: var(--warn); }

/* ---------- pages ---------- */
.page { overflow-y: auto; padding: 18px 16px 40px; display: grid; gap: 16px; align-content: start; max-width: 1000px; width: 100%; margin: 0 auto; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; display: grid; gap: 12px; min-width: 0; }
.card h3 { margin: 0; }
.card-head { display: flex; justify-content: space-between; align-items: center; gap: 10px; flex-wrap: wrap; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.span2 { grid-column: 1 / -1; }
.row-end { display: flex; justify-content: flex-end; gap: 8px; flex-wrap: wrap; }
.form-error { color: var(--danger); margin: 0; }
.form-ok { color: var(--ok); margin: 0; }
.table-wrap { overflow-x: auto; }
table.tbl { width: 100%; border-collapse: collapse; font-size: 14px; }
table.tbl th, table.tbl td { border-bottom: 1px solid var(--line); padding: 8px 6px; text-align: left; vertical-align: top; }
table.tbl th { color: var(--muted); font-weight: 500; font-size: 12px; text-transform: uppercase; letter-spacing: .05em; }
table.tbl td.actions { white-space: nowrap; }
.diag { background: var(--code); border: 1px solid var(--line); border-radius: 10px; padding: 12px; overflow: auto; font-size: 12px; max-height: 420px; }
.agent-cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 10px; }
.agent-card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 14px; display: grid; gap: 6px; }
.agent-card b { font-size: 15px; }
.agent-card p { margin: 0; font-size: 13px; color: var(--muted); }
.run-head { display: flex; justify-content: space-between; gap: 12px; flex-wrap: wrap; }
.run-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.run-events { display: grid; gap: 6px; max-height: 60vh; overflow-y: auto; }
.ev { font-size: 14px; padding: 6px 10px; border-radius: 8px; background: var(--bg2); overflow-wrap: anywhere; }
.ev.tool, .ev.tool_result { font-family: ui-monospace, Menlo, Consolas, monospace; font-size: 12.5px; color: var(--muted); white-space: pre-wrap; }
.ev.tool::before { content: '⚙ '; }
.ev.status, .ev.log { color: var(--muted); font-size: 13px; white-space: pre-wrap; }
.ev.error, .ev.denied { color: var(--danger); }
.ev.result { border: 1px solid color-mix(in srgb, var(--ok) 40%, transparent); }
.ev.end { font-weight: 600; }
.run-list { display: grid; gap: 6px; }
.run-item { display: flex; gap: 10px; align-items: center; padding: 10px 12px; background: var(--panel); border: 1px solid var(--line); border-radius: 12px; cursor: pointer; }
.run-item:hover { border-color: var(--accent); }
.run-item .t { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.toast { position: fixed; left: 50%; bottom: calc(24px + env(safe-area-inset-bottom)); transform: translateX(-50%); background: var(--panel2);
  border: 1px solid var(--line); padding: 10px 16px; border-radius: 12px; box-shadow: var(--shadow); z-index: 50; max-width: 90vw; }
.lightbox { position: fixed; inset: 0; background: rgba(0,0,0,.85); display: grid; place-items: center; z-index: 60; cursor: zoom-out; }
.lightbox img { max-width: 94vw; max-height: 90dvh; border-radius: 8px; }

/* ---------- mobile ---------- */
@media (max-width: 820px) {
  .app { grid-template-columns: 1fr; }
  .sidebar { position: fixed; inset: 0 auto 0 0; width: min(320px, 86vw); z-index: 10; transform: translateX(-102%); transition: transform .2s ease; }
  .sidebar.open { transform: none; }
  .only-mobile { display: inline-flex; }
  .only-desktop { display: none; }
  .topbar { padding-left: 8px; padding-right: 8px; gap: 6px; }
  .topbar-title { display: none; }
  #view-agents .topbar-title, #view-settings .topbar-title { display: block; }
  .model-btn { flex: 1; max-width: none; }
  .messages { padding: 14px 10px 6px; }
  .composer-wrap { padding: 6px 8px calc(8px + env(safe-area-inset-bottom)); }
  .composer textarea { font-size: 16px; }   /* évite le zoom automatique sur iOS */
  .send-btn { width: 48px; height: 48px; }
  .grid2 { grid-template-columns: 1fr; }
  .conv-item .icon-btn { opacity: 1; }
  .dialog { width: 100vw; max-height: 92dvh; border-radius: 18px 18px 0 0; margin-bottom: 0; }
}

/* sélecteur de modèles sur téléphone : nom sur toute la largeur, badges en dessous */
@media (max-width: 820px) {
  .model-item { display: grid; grid-template-columns: 1fr auto; grid-template-areas: "main fav" "badges fav"; row-gap: 4px; }
  .model-item .mi-main { grid-area: main; }
  .model-item .badges { grid-area: badges; }
  .model-item .fav { grid-area: fav; }
  .model-item .mi-name, .model-item .mi-id { white-space: normal; overflow-wrap: anywhere; }
}
