:root {
  --bg: #f4f6f8; --surface: #ffffff; --surface-2: #eef2f5; --ink: #142029; --muted: #5d6b76;
  --line: #d8dfe5; --accent: #0b5f73; --accent-ink: #ffffff; --accent-soft: #dcEEF2;
  --user: #0b5f73; --user-ink: #ffffff; --warn: #a8640c; --error: #b3261e;
  --radius: 14px; --shadow: 0 1px 2px rgba(0,0,0,.06), 0 4px 14px rgba(0,0,0,.05);
  --mono: ui-monospace, SFMono-Regular, Consolas, "Liberation Mono", monospace;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1519; --surface: #161e24; --surface-2: #1d272e; --ink: #e3e9ed; --muted: #93a2ad;
    --line: #2a363f; --accent: #4fb3c9; --accent-ink: #062027; --accent-soft: #173640;
    --user: #1f5966; --user-ink: #eaf6f9; --warn: #e0a34a; --error: #f2938c;
    --shadow: none; color-scheme: dark;
  }
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg); color: var(--ink); -webkit-font-smoothing: antialiased;
}
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
[hidden] { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.error { color: var(--error); min-height: 1.4em; margin: 0; }

.btn-primary {
  background: var(--accent); color: var(--accent-ink); border: 0; border-radius: 10px;
  padding: 10px 14px; font-weight: 600;
}
.btn-primary:hover { filter: brightness(1.08); }
.wide { width: 100%; }
.icon-btn { background: none; border: 0; font-size: 20px; padding: 6px 10px; border-radius: 8px; }
.icon-btn:hover { background: var(--surface-2); }
.link-btn { background: none; border: 0; color: var(--muted); text-decoration: underline; padding: 0; }

/* ---------- Connexion ---------- */
.login { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.login-card {
  width: min(360px, 100%); background: var(--surface); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 28px 22px; display: grid; gap: 12px; text-align: center;
  box-shadow: var(--shadow);
}
.login-card img { margin: 0 auto; }
.login-card h1 { margin: 0; font-size: 22px; }
.login-card p { margin: 0; }
.login-card input {
  padding: 12px; border: 1px solid var(--line); border-radius: 10px; background: var(--bg);
}

/* ---------- Mise en page ---------- */
.app { height: 100dvh; display: grid; grid-template-columns: 280px 1fr; }
.drawer {
  background: var(--surface); border-right: 1px solid var(--line); display: flex; flex-direction: column;
  gap: 12px; padding: 14px; min-height: 0;
}
.drawer-head { display: flex; align-items: center; justify-content: space-between; }
.conv-list { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; display: grid; align-content: start; gap: 2px; }
.conv-list li { display: flex; align-items: center; border-radius: 8px; }
.conv-list li.active, .conv-list li:hover { background: var(--surface-2); }
.conv-list .conv-open {
  flex: 1; text-align: left; background: none; border: 0; padding: 8px 10px; min-width: 0;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.conv-list .conv-del { background: none; border: 0; color: var(--muted); padding: 6px 10px; visibility: hidden; }
.conv-list li:hover .conv-del, .conv-list li.active .conv-del { visibility: visible; }
.drawer-foot { display: grid; gap: 8px; border-top: 1px solid var(--line); padding-top: 10px; }
.drawer-foot select { display: block; width: 100%; margin-top: 4px; padding: 6px; border-radius: 8px; border: 1px solid var(--line); background: var(--bg); }
.only-mobile { display: none; }
.backdrop { display: none; }

.main { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.topbar {
  display: flex; align-items: center; gap: 8px; padding: 8px 16px; padding-top: max(8px, env(safe-area-inset-top));
  border-bottom: 1px solid var(--line); background: var(--surface);
}
.spacer { flex: 1; }
.conv-title { max-width: 40%; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: 13px; }
.tabs { display: flex; gap: 4px; background: var(--surface-2); padding: 3px; border-radius: 10px; }
.tab { border: 0; background: none; padding: 6px 14px; border-radius: 8px; color: var(--muted); font-weight: 600; }
.tab.active { background: var(--surface); color: var(--ink); box-shadow: var(--shadow); }
.tab-panel { flex: 1; min-height: 0; display: flex; flex-direction: column; }

/* ---------- Chat ---------- */
.messages { flex: 1; overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 14px; }
.messages > * { width: min(820px, 100%); margin-inline: auto; }
.welcome { text-align: center; margin-top: 8vh; }
.welcome h2 { margin: 0 0 6px; }
.chips { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; margin-top: 18px; }
.chip {
  border: 1px solid var(--line); background: var(--surface); border-radius: 999px; padding: 8px 14px;
  font-size: 14px; text-align: left;
}
.chip:hover { border-color: var(--accent); }

.msg { display: flex; flex-direction: column; gap: 6px; }
.msg.user { align-items: flex-end; }
.bubble { max-width: 100%; overflow-wrap: anywhere; }
.msg.user .bubble {
  background: var(--user); color: var(--user-ink); padding: 9px 14px; border-radius: 16px 16px 4px 16px;
  max-width: 85%; white-space: pre-wrap;
}
.msg.assistant .bubble { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 4px 16px; box-shadow: var(--shadow); }
.msg.assistant.error .bubble { border-color: var(--error); }
.msg-meta { font-size: 12px; color: var(--muted); display: flex; gap: 10px; flex-wrap: wrap; }
.copy-btn { background: none; border: 0; color: var(--muted); font-size: 12px; padding: 0; text-decoration: underline; }

.tools { display: flex; flex-wrap: wrap; gap: 6px; }
.tool-pill { font-size: 12px; color: var(--muted); background: var(--surface-2); border-radius: 999px; padding: 2px 10px; }
.typing { display: inline-flex; gap: 4px; padding: 12px 2px; }
.typing span { width: 7px; height: 7px; border-radius: 50%; background: var(--muted); animation: blink 1.2s infinite; }
.typing span:nth-child(2) { animation-delay: .2s; } .typing span:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 0%, 80%, 100% { opacity: .25 } 40% { opacity: 1 } }

/* Markdown */
.md h1, .md h2, .md h3 { line-height: 1.25; margin: 16px 0 8px; }
.md h1 { font-size: 20px; } .md h2 { font-size: 18px; } .md h3 { font-size: 16px; }
.md p, .md ul, .md ol { margin: 8px 0; }
.md ul, .md ol { padding-left: 22px; }
.md code { font-family: var(--mono); font-size: 13px; background: var(--surface-2); padding: 1px 5px; border-radius: 5px; }
.md pre { background: var(--surface-2); padding: 10px 12px; border-radius: 10px; overflow-x: auto; }
.md pre code { background: none; padding: 0; }
.md table { border-collapse: collapse; display: block; overflow-x: auto; max-width: 100%; font-size: 14px; margin: 10px 0; }
.md th, .md td { border: 1px solid var(--line); padding: 5px 9px; text-align: left; vertical-align: top; }
.md th { background: var(--surface-2); }
.md blockquote { margin: 8px 0; padding: 2px 12px; border-left: 3px solid var(--accent); color: var(--muted); }
.md a { color: var(--accent); }

.composer {
  display: flex; gap: 8px; align-items: flex-end; padding: 10px 16px;
  padding-bottom: max(10px, env(safe-area-inset-bottom)); border-top: 1px solid var(--line); background: var(--surface);
}
.composer textarea {
  flex: 1; resize: none; max-height: 40vh; padding: 10px 14px; border: 1px solid var(--line);
  border-radius: 20px; background: var(--bg); line-height: 1.4; font-size: 16px; /* 16px : pas de zoom iOS */
}
.composer textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.send-btn {
  width: 42px; height: 42px; border-radius: 50%; border: 0; background: var(--accent); color: var(--accent-ink);
  font-size: 18px; flex: none;
}
.send-btn.stop { background: var(--error); }
.send-btn:disabled { opacity: .45; cursor: default; }

/* ---------- States ---------- */
.states-view { flex: 1; overflow-y: auto; padding: 16px; }
.family-title { width: min(1000px, 100%); margin: 18px auto 8px; font-size: 13px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }
.family-title:first-child { margin-top: 0; }
.states-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 10px; width: min(1000px, 100%); margin: 0 auto; }
.state-card {
  display: flex; gap: 12px; align-items: center; text-align: left; background: var(--surface);
  border: 1px solid var(--line); border-radius: 12px; padding: 10px 12px;
}
.state-card:hover { border-color: var(--accent); }
.state-code {
  flex: none; width: 40px; height: 40px; border-radius: 10px; display: grid; place-items: center;
  background: var(--accent-soft); color: var(--accent); font: 700 18px var(--mono);
}
.state-code.wide { font-size: 12px; }
.state-card small { display: block; color: var(--muted); }
.state-detail { width: min(900px, 100%); margin: 0 auto; }
.state-detail .detail-head { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; margin-bottom: 4px; }
.state-detail h2 { margin: 0; font-size: 20px; }
.state-detail section { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 4px 16px 10px; margin: 12px 0; }
.state-detail h3 { font-size: 15px; margin: 12px 0 6px; }
.back-btn { background: none; border: 0; color: var(--accent); padding: 0 0 10px; font-weight: 600; }
.ask-btn { margin-left: auto; }
.type-tag { font: 12px var(--mono); padding: 1px 6px; border-radius: 6px; background: var(--surface-2); color: var(--muted); white-space: nowrap; }
.type-tag.next_state { background: var(--accent-soft); color: var(--accent); }
.type-tag.screen { color: var(--warn); }

/* ---------- Mobile ---------- */
@media (max-width: 760px) {
  .app { grid-template-columns: 1fr; }
  .only-mobile { display: inline-block; }
  .drawer {
    position: fixed; inset: 0 auto 0 0; width: min(300px, 85vw); z-index: 20;
    transform: translateX(-100%); transition: transform .2s ease; padding-top: max(14px, env(safe-area-inset-top));
  }
  .drawer.open { transform: none; box-shadow: 0 0 40px rgba(0,0,0,.3); }
  .backdrop { display: block; position: fixed; inset: 0; background: rgba(0,0,0,.35); z-index: 10; }
  .topbar { padding-inline: 8px; }
  .messages, .states-view { padding: 12px; }
  .composer { padding-inline: 10px; }
  .conv-title { display: none; }
  .conv-list .conv-del { visibility: visible; }
}
