:root {
  /* Surfaces (OKLCH — sombre neutre, second layer pour le drawer) */
  --bg: oklch(0.185 0.012 260);
  --surface: oklch(0.23 0.014 260);
  --surface-2: oklch(0.27 0.016 260);
  --drawer-bg: oklch(0.16 0.012 260);
  --border: oklch(0.32 0.014 260);
  --border-soft: oklch(0.28 0.012 260);

  /* Encre */
  --text: oklch(0.93 0.008 260);
  --muted: oklch(0.68 0.014 260);
  --faint: oklch(0.63 0.012 260); /* ≥ 4.5:1 sur toutes les surfaces */

  /* Accent + états — les couleurs vives servent de TEXTE ; les fonds pleins
     utilisent les variantes -bg plus sombres (blanc dessus ≥ 4.5:1) */
  --accent: oklch(0.66 0.17 258);
  --accent-bg: oklch(0.52 0.18 258);
  --accent-ink: oklch(0.98 0.005 258);
  --accent-soft: oklch(0.66 0.17 258 / 0.14);
  --green: oklch(0.72 0.17 150);
  --red: oklch(0.64 0.19 25);
  --red-bg: oklch(0.55 0.19 25);
  --amber: oklch(0.78 0.14 80);

  /* Bulles */
  --bubble-user: oklch(0.5 0.19 260);
  --bubble-jarvis: oklch(0.24 0.014 260);

  --radius: 18px;
  --ease-out: cubic-bezier(0.23, 1, 0.32, 1);
  --ease-drawer: cubic-bezier(0.32, 0.72, 0, 1);

  /* z-index sémantique */
  --z-drawer-scrim: 40;
  --z-drawer: 50;
  --z-sheet-scrim: 60;
  --z-sheet: 70;
  --z-toast: 80;
  --z-login: 100;
}

* { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; }

html, body {
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  overscroll-behavior: none;
}

body { height: 100dvh; overflow: hidden; }

.hidden { display: none !important; }

button { font-family: inherit; }

.sub { font-size: 12px; color: var(--muted); }
.sub.online { color: var(--green); }
.sub.offline { color: var(--red); }

.logo, .login-logo {
  width: 38px; height: 38px;
  border-radius: 10px;
  background: var(--accent-bg);
  color: #fff;
  display: flex; align-items: center; justify-content: center;
  font-weight: 700; font-size: 19px;
  flex-shrink: 0;
}

button:focus-visible, input:focus-visible, select:focus-visible, textarea:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

input::placeholder, textarea::placeholder { color: var(--faint); }

.name { font-weight: 600; font-size: 16px; }

/* ================= écran principal ================= */

.screen {
  height: 100dvh;
  display: flex;
  flex-direction: column;
  transition: transform 300ms var(--ease-drawer);
}

body.drawer-open .screen { transform: translateX(min(78vw, 320px)); }
@media (prefers-reduced-motion: reduce) {
  .screen { transition: none; }
}

/* ================= header ================= */

header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: calc(env(safe-area-inset-top) + 8px) max(12px, env(safe-area-inset-right)) 8px max(12px, env(safe-area-inset-left));
  background: color-mix(in oklab, var(--bg) 92%, transparent);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border-soft);
  flex-shrink: 0;
}

.header-agent {
  flex: 1;
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}
.header-agent-text { min-width: 0; }
.header-agent-text .name { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.header-agent-text .sub { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.agent-icon { font-size: 24px; line-height: 1; }

.icon-btn {
  width: 44px; height: 44px;
  flex-shrink: 0;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--muted);
  display: flex; align-items: center; justify-content: center;
  cursor: pointer;
  position: relative;
  transition: transform 150ms var(--ease-out);
}
.icon-btn:active { transform: scale(0.94); color: var(--text); }
.icon-btn.subtle { border-color: transparent; background: transparent; }
.icon-btn.recording { background: var(--red-bg); color: #fff; border-color: var(--red-bg); }

.burger-badge {
  position: absolute;
  top: -2px; right: -2px;
  min-width: 17px; height: 17px;
  padding: 0 4px;
  border-radius: 9px;
  background: var(--accent-bg);
  color: var(--accent-ink);
  font-size: 10.5px;
  font-weight: 700;
  display: flex; align-items: center; justify-content: center;
  border: 2px solid var(--bg);
}
.burger-badge.spin {
  background: transparent;
  border: 2px solid var(--accent-soft);
  border-top-color: var(--accent);
  animation: spin 0.9s linear infinite;
}

/* ================= drawer ================= */

.scrim {
  position: fixed;
  inset: 0;
  background: oklch(0 0 0 / 0.5);
  z-index: var(--z-drawer-scrim);
  opacity: 0;
  pointer-events: none;
  transition: opacity 250ms var(--ease-out);
}
body.drawer-open .scrim { opacity: 1; pointer-events: auto; }
#sheet-scrim { z-index: var(--z-sheet-scrim); }
#sheet-scrim:not(.hidden) { opacity: 1; pointer-events: auto; }

.drawer {
  position: fixed;
  top: 0; left: 0; bottom: 0;
  width: 78vw;
  max-width: 320px;
  background: var(--drawer-bg);
  border-right: 1px solid var(--border-soft);
  z-index: var(--z-drawer);
  display: flex;
  flex-direction: column;
  transform: translateX(-100%);
  transition: transform 250ms var(--ease-drawer);
  padding-top: env(safe-area-inset-top);
  padding-bottom: env(safe-area-inset-bottom);
  padding-left: env(safe-area-inset-left);
}
body.drawer-open .drawer { transform: translateX(0); }
@media (prefers-reduced-motion: reduce) {
  .drawer, .scrim { transition: none; }
}

.drawer-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 14px 12px;
}
.drawer-brand { display: flex; align-items: center; gap: 10px; }

.counter {
  font-size: 11.5px;
  color: var(--muted);
  background: var(--surface);
  border: 1px solid var(--border-soft);
  border-radius: 999px;
  padding: 4px 9px;
  white-space: nowrap;
}

.agent-list {
  flex: 1;
  overflow-y: auto;
  padding: 2px 8px;
  -webkit-overflow-scrolling: touch;
}

.agent-item {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 11px;
  padding: 11px 10px;
  border: none;
  background: none;
  border-radius: 12px;
  color: var(--text);
  text-align: left;
  cursor: pointer;
  transition: background 150ms var(--ease-out), transform 150ms var(--ease-out);
  min-height: 56px;
}
.agent-item:active { transform: scale(0.98); }
.agent-item.active { background: var(--surface-2); }

.agent-item .ai-icon {
  font-size: 22px;
  width: 32px;
  text-align: center;
  flex-shrink: 0;
}

.ai-body { flex: 1; min-width: 0; }
.ai-title-row { display: flex; align-items: baseline; gap: 6px; }
.ai-title {
  font-weight: 600;
  font-size: 14.5px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.ai-time { font-size: 11px; color: var(--faint); margin-left: auto; flex-shrink: 0; }
.ai-preview {
  font-size: 12.5px;
  color: var(--muted);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  margin-top: 2px;
}
.ai-preview .you { color: var(--faint); }

.ai-state { flex-shrink: 0; width: 20px; display: flex; justify-content: center; }

.ai-spinner {
  width: 15px; height: 15px;
  border: 2px solid var(--accent-soft);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.9s linear infinite;
}
.ai-queued { font-size: 13px; color: var(--amber); }

.ai-badge {
  min-width: 19px; height: 19px;
  padding: 0 5px;
  border-radius: 10px;
  background: var(--accent-bg);
  color: var(--accent-ink);
  font-size: 11px;
  font-weight: 700;
  display: flex; align-items: center; justify-content: center;
}

.ai-dot {
  width: 8px; height: 8px;
  border-radius: 50%;
  background: var(--accent);
}

.new-agent-btn {
  margin: 8px 14px;
  padding: 12px;
  border-radius: 12px;
  border: 1px dashed var(--border);
  background: none;
  color: var(--muted);
  font-size: 14px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  cursor: pointer;
  transition: transform 150ms var(--ease-out), color 150ms ease;
}
.new-agent-btn:active { transform: scale(0.98); color: var(--text); }
.new-agent-btn .plus { font-size: 17px; font-weight: 600; }

.drawer-foot {
  border-top: 1px solid var(--border-soft);
  padding: 10px 14px;
  display: flex;
  flex-direction: column;
  gap: 9px;
}

.foot-btn {
  padding: 10px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: 13.5px;
  cursor: pointer;
  transition: transform 150ms var(--ease-out);
}
.foot-btn:active { transform: scale(0.98); }

.model-row {
  display: flex;
  align-items: center;
  gap: 8px;
}
.model-label { font-size: 12px; color: var(--muted); flex-shrink: 0; }

#model-select {
  flex: 1;
  min-width: 0;
  font-size: 16px; /* ≥16px : empêche le zoom auto iOS au focus */
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 9px;
  padding: 7px 9px;
  outline: none;
  -webkit-appearance: none;
  appearance: none;
  text-overflow: ellipsis;
}
#model-select.non-default { border-color: var(--accent); color: var(--accent); }

/* ================= chat ================= */

main#chat {
  flex: 1;
  overflow-y: auto;
  padding: 14px 12px 8px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  -webkit-overflow-scrolling: touch;
}

.day-sep {
  align-self: center;
  font-size: 11px;
  color: var(--muted);
  background: var(--surface);
  border-radius: 999px;
  padding: 3px 10px;
  margin: 8px 0 2px;
}

.empty-state {
  margin: auto;
  text-align: center;
  padding: 24px;
  color: var(--muted);
  max-width: 300px;
}
.empty-state .es-icon { font-size: 40px; margin-bottom: 12px; }
.empty-state h3 { color: var(--text); font-size: 16px; margin-bottom: 6px; }
.empty-state p { font-size: 13.5px; line-height: 1.5; }

.msg {
  max-width: 86%;
  border-radius: var(--radius);
  padding: 9px 13px;
  font-size: 15px;
  line-height: 1.45;
  word-wrap: break-word;
  white-space: pre-wrap;
}

/* Motion = information : seuls les messages ENTRANTS s'animent,
   pas les re-rendus complets au changement d'agent */
.msg-new {
  transition: opacity 200ms var(--ease-out), transform 200ms var(--ease-out);
  @starting-style { opacity: 0; transform: translateY(6px); }
}
@media (prefers-reduced-motion: reduce) {
  .msg-new { @starting-style { transform: none; } }
}

.msg-skeleton {
  height: 44px;
  width: 60%;
  background: var(--surface);
  animation: skeleton-pulse 1.2s ease-in-out infinite;
}
.msg-skeleton:nth-child(even) { align-self: flex-end; width: 45%; }
@keyframes skeleton-pulse { 50% { opacity: 0.45; } }
@media (prefers-reduced-motion: reduce) {
  .msg-skeleton { animation: none; opacity: 0.6; }
}

.msg.user {
  align-self: flex-end;
  background: var(--bubble-user);
  color: #fff;
  border-bottom-right-radius: 6px;
}

.msg.jarvis {
  align-self: flex-start;
  background: var(--bubble-jarvis);
  border: 1px solid var(--border-soft);
  border-bottom-left-radius: 6px;
  white-space: normal;
}

.msg.command {
  align-self: flex-end;
  background: var(--accent-soft);
  color: oklch(0.78 0.12 258);
  border: 1px solid oklch(0.66 0.17 258 / 0.35);
  font-family: ui-monospace, "SF Mono", Menlo, monospace;
  font-size: 13.5px;
}

.msg.streaming { border-color: var(--accent); }

.msg .meta {
  display: block;
  font-size: 10.5px;
  color: oklch(1 0 0 / 0.78);
  margin-top: 5px;
  text-align: right;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.msg.jarvis .meta, .msg.command .meta { color: var(--muted); text-align: left; }

.msg img { max-width: 100%; border-radius: 10px; display: block; }
.msg audio { width: 220px; max-width: 100%; margin-top: 4px; }
.transcription { font-style: italic; opacity: 0.85; font-size: 13.5px; margin-top: 6px; }

/* markdown dans les bulles jarvis */
.msg.jarvis h1, .msg.jarvis h2, .msg.jarvis h3, .msg.jarvis h4, .msg.jarvis h5 { font-size: 15.5px; margin: 10px 0 4px; }
.msg.jarvis h1:first-child, .msg.jarvis h2:first-child, .msg.jarvis h3:first-child, .msg.jarvis h4:first-child { margin-top: 0; }
.msg.jarvis p { margin: 6px 0; }
.msg.jarvis ul, .msg.jarvis ol { margin: 6px 0 6px 20px; }
.msg.jarvis li { margin: 3px 0; }
.msg.jarvis code {
  background: var(--surface);
  border: 1px solid var(--border-soft);
  border-radius: 5px;
  padding: 1px 5px;
  font-size: 13px;
  font-family: ui-monospace, "SF Mono", Menlo, monospace;
}
.msg.jarvis pre {
  background: var(--surface);
  border: 1px solid var(--border-soft);
  border-radius: 10px;
  padding: 10px;
  overflow-x: auto;
  margin: 8px 0;
}
.msg.jarvis pre code { background: none; border: none; padding: 0; }
.msg.jarvis a { color: var(--accent); }
.msg.jarvis blockquote {
  border-left: 1px solid var(--border);
  padding-left: 10px;
  color: var(--muted);
  margin: 6px 0;
}
.msg.jarvis table { border-collapse: collapse; margin: 8px 0; font-size: 13.5px; display: block; overflow-x: auto; }
.msg.jarvis th, .msg.jarvis td { border: 1px solid var(--border-soft); padding: 4px 8px; }
.msg.jarvis hr { border: none; border-top: 1px solid var(--border-soft); margin: 10px 0; }

/* ================= run bar ================= */

.run-bar {
  display: flex;
  align-items: center;
  gap: 12px;
  margin: 0 12px 6px;
  padding: 10px 14px;
  background: var(--surface);
  border: 1px solid var(--accent);
  border-radius: 14px;
  flex-shrink: 0;
  transition: opacity 200ms var(--ease-out);
  @starting-style { opacity: 0; }
}

.run-spinner {
  width: 18px; height: 18px;
  border: 2.5px solid var(--accent-soft);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.9s linear infinite;
  flex-shrink: 0;
}
@keyframes spin { to { transform: rotate(360deg); } }

.run-info { flex: 1; min-width: 0; }
#run-label {
  font-size: 13.5px;
  font-weight: 500;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}

#run-cancel {
  background: none;
  border: 1px solid var(--border);
  color: var(--muted);
  border-radius: 10px;
  width: 40px; height: 40px;
  font-size: 15px;
  cursor: pointer;
  flex-shrink: 0;
  position: relative;
  transition: transform 150ms var(--ease-out);
}
#run-cancel:active { transform: scale(0.94); }
#run-cancel::after { content: ''; position: absolute; inset: -6px; }

/* ================= footer / composer ================= */

footer {
  border-top: 1px solid var(--border-soft);
  background: color-mix(in oklab, var(--bg) 95%, transparent);
  backdrop-filter: blur(12px);
  padding: 8px max(10px, env(safe-area-inset-right)) calc(env(safe-area-inset-bottom) + 8px) max(10px, env(safe-area-inset-left));
  flex-shrink: 0;
}

.chips {
  display: flex;
  gap: 6px;
  overflow-x: auto;
  padding-bottom: 8px;
  scrollbar-width: none;
}
.chips:empty { display: none; }
.chips::-webkit-scrollbar { display: none; }

.chip {
  flex-shrink: 0;
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text);
  font-size: 13px;
  border-radius: 999px;
  padding: 11px 15px;
  cursor: pointer;
  transition: transform 150ms var(--ease-out);
  min-height: 44px;
}
.chip:active { transform: scale(0.96); }
.chip-go { border-color: oklch(0.72 0.17 150 / 0.5); color: var(--green); }
.chip-run { border-color: oklch(0.66 0.17 258 / 0.5); color: var(--accent); }

.composer {
  display: flex;
  align-items: flex-end;
  gap: 8px;
}

#input {
  flex: 1;
  resize: none;
  max-height: 120px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 20px;
  color: var(--text);
  font-size: 16px; /* ≥16px : empêche le zoom auto iOS au focus */
  font-family: inherit;
  padding: 9px 14px;
  outline: none;
  line-height: 1.35;
}
#input:focus { border-color: var(--accent); }
#input.is-command { color: var(--accent); font-family: ui-monospace, "SF Mono", Menlo, monospace; }
#input::placeholder { color: var(--faint); }

.send-btn {
  width: 44px; height: 44px;
  flex-shrink: 0;
  border-radius: 50%;
  border: none;
  background: var(--accent-bg);
  color: var(--accent-ink);
  display: flex; align-items: center; justify-content: center;
  cursor: pointer;
  transition: transform 150ms var(--ease-out), background 150ms ease, opacity 150ms ease;
}
.send-btn:active { transform: scale(0.92); }
.send-btn:disabled { opacity: 0.45; }
.send-btn.run-mode { background: oklch(0.55 0.15 150); }

/* ================= enregistrement vocal ================= */

.rec-bar {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-top: 8px;
  padding: 8px 12px;
  background: var(--surface);
  border: 1px solid var(--red);
  border-radius: 14px;
  font-size: 14px;
}
.rec-dot {
  width: 10px; height: 10px;
  background: var(--red);
  border-radius: 50%;
  animation: blink 1s infinite;
}
@keyframes blink { 50% { opacity: 0.25; } }
.rec-hint { color: var(--muted); flex: 1; }
#rec-cancel, #rec-stop {
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  border-radius: 10px;
  padding: 11px 16px;
  font-size: 13.5px;
  cursor: pointer;
  min-height: 44px;
  transition: transform 150ms var(--ease-out);
}
#rec-cancel:active, #rec-stop:active { transform: scale(0.97); }
#rec-stop { background: var(--red-bg); border-color: var(--red-bg); color: #fff; }

/* ================= sheets (bottom) ================= */

.sheet {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  background: var(--surface);
  border-top: 1px solid var(--border);
  border-radius: 20px 20px 0 0;
  padding: 10px 18px calc(env(safe-area-inset-bottom) + 16px);
  z-index: var(--z-sheet);
  transform: translateY(0);
  transition: transform 250ms var(--ease-drawer);
  @starting-style { transform: translateY(100%); }
  max-height: 82dvh;
  overflow-y: auto;
}
.sheet.closing { transform: translateY(100%); transition-duration: 200ms; }
@media (prefers-reduced-motion: reduce) {
  .sheet { transition: opacity 200ms ease; @starting-style { transform: none; opacity: 0; } }
  .sheet.closing { transform: none; opacity: 0; }
}

.sheet-handle {
  width: 38px; height: 4px;
  background: var(--border);
  border-radius: 2px;
  margin: 2px auto 14px;
}

.sheet h2 { font-size: 17px; margin-bottom: 14px; }

.field { display: block; margin-bottom: 14px; }
.field > span { display: block; font-size: 12.5px; color: var(--muted); margin-bottom: 6px; }
.field input[type="text"], .field select {
  width: 100%;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 11px;
  color: var(--text);
  font-size: 16px;
  padding: 11px 13px;
  outline: none;
  font-family: inherit;
  -webkit-appearance: none;
  appearance: none;
}
.field input[type="text"]:focus, .field select:focus { border-color: var(--accent); }

.icon-row { display: flex; gap: 6px; flex-wrap: wrap; }
.icon-choice {
  width: 44px; height: 44px;
  font-size: 21px;
  border-radius: 11px;
  border: 1px solid var(--border);
  background: var(--bg);
  cursor: pointer;
  transition: transform 150ms var(--ease-out), border-color 150ms ease;
}
.icon-choice:active { transform: scale(0.92); }
.icon-choice.selected { border-color: var(--accent); background: var(--accent-soft); }

.sheet-actions { display: flex; gap: 10px; margin-top: 6px; }
.sheet-actions.column { flex-direction: column; }
.sheet-actions button { flex: 1; }

.btn-primary, .btn-ghost, .btn-danger {
  padding: 13px;
  border-radius: 12px;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  transition: transform 150ms var(--ease-out);
  min-height: 46px;
}
.btn-primary:active, .btn-ghost:active, .btn-danger:active { transform: scale(0.97); }
.btn-primary { background: var(--accent-bg); border: none; color: var(--accent-ink); }
.btn-primary:disabled { opacity: 0.55; }
.btn-ghost { background: var(--surface-2); border: 1px solid var(--border); color: var(--text); }
.btn-danger { background: none; border: 1px solid oklch(0.64 0.19 25 / 0.45); color: var(--red); }

/* ================= toast ================= */

.toast {
  position: fixed;
  bottom: calc(env(safe-area-inset-bottom) + 90px);
  left: 50%;
  transform: translateX(-50%);
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text);
  font-size: 13.5px;
  padding: 10px 16px;
  border-radius: 999px;
  z-index: var(--z-toast);
  max-width: 86vw;
  text-align: center;
  transition: opacity 200ms var(--ease-out), transform 200ms var(--ease-out);
  @starting-style { opacity: 0; transform: translateX(-50%) translateY(8px); }
}
.toast.leaving { opacity: 0; transition-duration: 150ms; }

/* ================= login ================= */

.login {
  position: fixed;
  inset: 0;
  background: var(--bg);
  z-index: var(--z-login);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}

.login-card {
  width: 100%;
  max-width: 340px;
  text-align: center;
  display: flex;
  flex-direction: column;
  gap: 14px;
  align-items: center;
}
.login-card h1 { font-size: 22px; }
.login-card p { color: var(--muted); font-size: 13.5px; }
.login-card code { color: var(--text); }
.login-logo { width: 64px; height: 64px; font-size: 34px; border-radius: 16px; }

#token-input {
  width: 100%;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  color: var(--text);
  font-size: 16px;
  padding: 12px 14px;
  outline: none;
  text-align: center;
}
#token-input:focus { border-color: var(--accent); }

#token-save {
  width: 100%;
  background: var(--accent-bg);
  border: none;
  color: var(--accent-ink);
  font-size: 15px;
  font-weight: 600;
  border-radius: 12px;
  padding: 13px;
  cursor: pointer;
  transition: transform 150ms var(--ease-out);
}
#token-save:active { transform: scale(0.97); }

.login-error { color: var(--red); font-size: 13px; min-height: 16px; }
