:root {
  --bg: #1f1f22;
  --panel: #27272b;
  --panel-2: #2f2f34;
  --border: #3a3a40;
  --text: #ececf1;
  --muted: #a0a0ab;
  --accent: #c96442;
  --accent-ink: #fff;
  --user-bubble: #34343a;
  --radius: 12px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; overflow: hidden; overscroll-behavior: none; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg); color: var(--text);
  -webkit-font-smoothing: antialiased;
}
.hidden { display: none !important; }
.muted { color: var(--muted); font-size: 0.85rem; }
button { font: inherit; cursor: pointer; }
.link { background: none; border: none; color: var(--muted); text-decoration: underline; padding: 4px; }
.link:hover { color: var(--text); }

/* --- Connexion --- */
.login { position: fixed; inset: 0; display: grid; place-items: center; padding: 24px; overflow-y: auto; }
.login-card {
  width: 100%; max-width: 360px; background: var(--panel);
  border: 1px solid var(--border); border-radius: 16px; padding: 28px 24px;
}
.login-card h1 { margin: 0 0 4px; font-size: 1.5rem; }
.stack { display: flex; flex-direction: column; gap: 10px; margin-top: 18px; }
label { font-size: 0.85rem; color: var(--muted); }
input, textarea {
  width: 100%; background: var(--panel-2); color: var(--text);
  border: 1px solid var(--border); border-radius: 10px; padding: 12px; font-size: 1rem;
}
input:focus, textarea:focus { outline: 2px solid var(--accent); border-color: transparent; }
button.primary, .stack button[type="submit"] {
  background: var(--accent); color: var(--accent-ink); border: none;
  border-radius: 10px; padding: 12px; font-weight: 600;
}
.stack button[type="submit"]:disabled { opacity: 0.5; }
.msg { margin: 14px 0 0; font-size: 0.9rem; min-height: 1.2em; }
.msg.err { color: #ff8f7a; }
.msg.ok { color: #7ad19b; }

/* --- App layout --- */
/* position:fixed + inset:0 = couvre tout l'écran (safe-area incluse via
   viewport-fit=cover), plus fiable que 100dvh sur iOS. Flex pour que .chat
   remplisse toute la hauteur. */
.app { position: fixed; inset: 0; display: flex; }
.sidebar {
  background: var(--panel); border-right: 1px solid var(--border);
  display: flex; flex-direction: column; min-height: 0;
  width: 280px; flex-shrink: 0;
}
.sidebar-head { display: flex; gap: 8px; padding: 12px; padding-top: calc(12px + var(--safe-t)); border-bottom: 1px solid var(--border); }
.sidebar-head .primary { flex: 1; }
.conv-list { list-style: none; margin: 0; padding: 6px; overflow-y: auto; flex: 1; overscroll-behavior: contain; -webkit-overflow-scrolling: touch; }
.conv-list li {
  padding: 10px 12px; border-radius: 8px; cursor: pointer; font-size: 0.92rem;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; color: var(--text);
}
.conv-list li:hover { background: var(--panel-2); }
.conv-list li.active { background: var(--accent); color: var(--accent-ink); }
.sidebar-foot { padding: 10px 12px; border-top: 1px solid var(--border); }
.sidebar-foot .version { font-size: 0.72rem; opacity: 0.65; margin-top: 3px; }

.chat { display: flex; flex-direction: column; min-width: 0; flex: 1; }
.chat-head {
  display: flex; align-items: center; gap: 10px; padding: 12px 14px;
  padding-top: calc(12px + var(--safe-t));  /* sous la barre d'état iOS (safe-area) */
  border-bottom: 1px solid var(--border); background: var(--panel);
}
.chat-head #chat-title { font-weight: 600; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.icon-btn { background: none; border: none; color: var(--text); font-size: 1.2rem; padding: 4px 8px; }
#menu-btn { display: none; }

.messages { flex: 1; overflow-y: auto; padding: 18px; display: flex; flex-direction: column; gap: 14px; overscroll-behavior: contain; -webkit-overflow-scrolling: touch; }
.bubble { max-width: 80%; padding: 12px 14px; border-radius: var(--radius); line-height: 1.5; white-space: pre-wrap; word-wrap: break-word; }
.bubble.user { align-self: flex-end; background: var(--user-bubble); border-bottom-right-radius: 4px; }
/* Réponses de Claude : pleine largeur, style « document » (sans cadre de bulle). */
.bubble.assistant { align-self: stretch; max-width: 100%; background: none; border: none; padding: 2px 2px; white-space: normal; }
.bubble.error { align-self: flex-start; background: #3a2020; border: 1px solid #5a2b2b; color: #ffb4a2; }
.bubble pre { background: #17171a; border: 1px solid var(--border); border-radius: 8px; padding: 10px; overflow-x: auto; }
.bubble code { background: #17171a; padding: 1px 5px; border-radius: 4px; font-size: 0.9em; }
.bubble pre code { background: none; padding: 0; }
/* Éléments Markdown rendus dans les bulles assistant */
.bubble.assistant > :first-child { margin-top: 0; }
.bubble.assistant > :last-child { margin-bottom: 0; }
.bubble p { margin: 8px 0; }
.bubble h1, .bubble h2, .bubble h3, .bubble h4, .bubble h5, .bubble h6 { margin: 12px 0 6px; line-height: 1.25; }
.bubble h1 { font-size: 1.3em; } .bubble h2 { font-size: 1.18em; } .bubble h3 { font-size: 1.07em; }
.bubble h4, .bubble h5, .bubble h6 { font-size: 1em; }
.bubble ul, .bubble ol { margin: 8px 0; padding-left: 22px; }
.bubble li { margin: 3px 0; }
.bubble a { color: #7aa2ff; }
.bubble blockquote { margin: 8px 0; padding: 2px 12px; border-left: 3px solid var(--border); color: var(--muted); }
.bubble hr { border: none; border-top: 1px solid var(--border); margin: 12px 0; }
.bubble table { border-collapse: collapse; margin: 10px 0; display: block; max-width: 100%; overflow-x: auto; font-size: 0.9em; }
.bubble th, .bubble td { border: 1px solid var(--border); padding: 6px 10px; text-align: left; white-space: nowrap; }
.bubble th { background: var(--panel-2); font-weight: 600; }
.meta { font-size: 0.72rem; color: var(--muted); margin-top: 6px; }
.empty { margin: auto; color: var(--muted); text-align: center; }
.typing { display: inline-flex; gap: 4px; }
.typing span { width: 7px; height: 7px; border-radius: 50%; background: var(--muted); animation: blink 1.2s infinite both; }
.typing span:nth-child(2) { animation-delay: .2s; }
.typing span:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 0%, 80%, 100% { opacity: .2; } 40% { opacity: 1; } }

.composer { display: flex; gap: 8px; padding: 12px; padding-bottom: calc(12px + var(--safe-b)); border-top: 1px solid var(--border); background: var(--panel); }
.composer textarea { flex: 1; resize: none; max-height: 160px; }
.composer button { background: var(--accent); color: var(--accent-ink); border: none; border-radius: 10px; width: 46px; font-size: 1.1rem; }
.composer button:disabled { opacity: 0.5; }

/* --- Mobile --- */
@media (max-width: 720px) {
  .app { grid-template-columns: 1fr; }
  .sidebar {
    position: fixed; inset: 0 30% 0 0; z-index: 20; transform: translateX(-100%);
    transition: transform .2s ease; box-shadow: 2px 0 20px rgba(0,0,0,.4);
  }
  .app.menu-open .sidebar { transform: translateX(0); }
  #menu-btn { display: inline-block; }
}
