:root {
  --bg: #ffffff;
  --bg-soft: #f3f5f8;
  --fg: #1b1f24;
  --muted: #667080;
  --line: #dde2e8;
  --accent: #1f6feb;
  --accent-fg: #ffffff;
  --bubble-in: #eef1f5;
  --bubble-out: #1f6feb;
  --bubble-out-fg: #ffffff;
  --danger: #c62828;
  --ok: #2e7d32;
  --warn: #b26a00;
  --radius: 16px;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 16px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0d1117;
    --bg-soft: #161b22;
    --fg: #e6edf3;
    --muted: #8b949e;
    --line: #30363d;
    --bubble-in: #21262d;
    --bubble-out: #1f6feb;
    --danger: #ff6b6b;
    --ok: #3fb950;
    --warn: #d29922;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { background: var(--bg); color: var(--fg); overflow: hidden; }
button { font: inherit; cursor: pointer; }
.muted { color: var(--muted); }
.error { color: var(--danger); }

.layout {
  display: grid;
  grid-template-columns: minmax(260px, 340px) 1fr;
  height: var(--app-h, 100dvh);
}

.bar {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 56px;
  padding: 8px 12px;
  padding-top: max(8px, env(safe-area-inset-top));
  border-bottom: 1px solid var(--line);
  background: var(--bg-soft);
}
.bar h1, .bar h2 { margin: 0; font-size: 1.05rem; }
.bar h1 { flex: 1; }

.icon-btn {
  border: 0;
  background: transparent;
  color: var(--accent);
  font-size: 1.6rem;
  line-height: 1;
  width: 40px;
  height: 40px;
  border-radius: 50%;
}
.icon-btn:hover { background: var(--line); }
.link-btn { border: 0; background: transparent; color: var(--accent); padding: 6px 8px; text-align: left; }

.conn { width: 10px; height: 10px; border-radius: 50%; background: var(--muted); }
.conn[data-state="on"] { background: var(--ok); }
.conn[data-state="connecting"] { background: var(--warn); }

/* Sidebar */
.sidebar { display: flex; flex-direction: column; border-right: 1px solid var(--line); min-height: 0; }
.threads { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; }
.thread {
  display: grid;
  grid-template-columns: 44px 1fr auto;
  gap: 10px;
  align-items: center;
  padding: 10px 12px;
  border-bottom: 1px solid var(--line);
  cursor: pointer;
}
.thread:hover, .thread:focus-visible { background: var(--bg-soft); outline: none; }
.thread[aria-current="true"] { background: var(--bubble-in); }
.avatar {
  width: 44px; height: 44px; border-radius: 50%;
  display: grid; place-items: center;
  background: var(--accent); color: var(--accent-fg); font-weight: 600;
}
.thread-main { min-width: 0; }
.thread-name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.thread-last { color: var(--muted); font-size: .9rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.thread-meta { display: flex; flex-direction: column; align-items: flex-end; gap: 4px; font-size: .75rem; color: var(--muted); }
.badge {
  min-width: 22px; padding: 2px 7px; border-radius: 11px;
  background: var(--accent); color: var(--accent-fg); font-size: .75rem; text-align: center;
}
.sidebar-foot { display: flex; flex-direction: column; align-items: flex-start; gap: 2px; }
.hint { font-size: .8rem; margin: 4px 8px; }
.sidebar-foot { padding: 8px 12px; padding-bottom: max(8px, env(safe-area-inset-bottom)); border-top: 1px solid var(--line); }
.empty-list { padding: 24px 16px; color: var(--muted); }

/* Chat */
.chat { display: flex; flex-direction: column; min-height: 0; position: relative; }
.chat-title { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.chat-title h2 { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-title .muted { font-size: .8rem; }
.back { display: none; }
.messages { flex: 1; overflow-y: auto; padding: 12px; display: flex; flex-direction: column; }
.more { align-self: center; margin-bottom: 8px; }
#message-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; margin-top: auto; }

.msg { max-width: min(78%, 680px); align-self: flex-start; }
.msg .bubble {
  background: var(--bubble-in);
  padding: 8px 12px;
  border-radius: var(--radius);
  border-bottom-left-radius: 4px;
  overflow-wrap: anywhere;
}
.bubble p, .bubble ul, .bubble ol { margin: 0; }
.bubble p + p, .bubble p + ul, .bubble p + ol, .bubble ul + p, .bubble ol + p { margin-top: .5em; }
.bubble ul, .bubble ol { padding-left: 1.3em; }
.bubble code { font-family: ui-monospace, monospace; font-size: .9em; background: rgb(127 127 127 / .18); border-radius: 4px; padding: 0 3px; white-space: pre-wrap; }
.msg.out .bubble a { color: inherit; }

/* Rich blocks */
.quick-replies { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.quick-reply { border: 1px solid var(--accent); background: var(--bg); color: var(--accent); border-radius: 16px; padding: 5px 12px; font-size: .9rem; }
.quick-reply:hover { background: var(--accent); color: var(--accent-fg); }
.checklist { margin-top: 6px; display: flex; flex-direction: column; gap: 4px; }
.checklist-title, .card-title { font-weight: 600; }
.check-item { display: flex; align-items: center; gap: 8px; cursor: pointer; }
.check-item input { width: 18px; height: 18px; accent-color: var(--accent); }
.check-item input:checked + span { text-decoration: line-through; opacity: .65; }
.card { margin-top: 6px; border: 1px solid var(--line); border-radius: 12px; padding: 10px 12px; background: var(--bg); color: var(--fg); }
.card .att-img-link { margin: -10px -12px 8px; }
.card .att-img { max-width: 100%; border-radius: 12px 12px 0 0; }
.card-subtitle { color: var(--muted); font-size: .85rem; }
.card ul { margin: 6px 0 0; padding-left: 1.2em; }
.msg.out { align-self: flex-end; }
.msg.out .bubble { background: var(--bubble-out); color: var(--bubble-out-fg); border-radius: var(--radius); border-bottom-right-radius: 4px; }
.msg.out .bubble a { color: inherit; }
.msg .meta { font-size: .72rem; color: var(--muted); margin: 2px 6px; }
.msg.out .meta { text-align: right; }
.msg .author { font-weight: 600; margin-right: 6px; }
.msg.system { align-self: center; max-width: 90%; }
.msg.system .bubble { background: transparent; color: var(--muted); font-size: .85rem; text-align: center; padding: 2px 8px; }
.msg.pending .bubble { opacity: .6; }
.msg.queued .bubble { opacity: .45; }
.retry { padding: 0 4px; font-size: .72rem; }
.offline { background: var(--warn); color: #fff; text-align: center; font-size: .85rem; padding: 4px 8px; }
.offline[hidden] { display: none; }
.msg.failed .bubble { outline: 2px solid var(--danger); }
.day-sep { align-self: center; font-size: .75rem; color: var(--muted); background: var(--bg-soft); border-radius: 10px; padding: 2px 10px; margin: 8px 0 2px; }
.day-sep::first-letter { text-transform: uppercase; }
.jump { position: absolute; right: 16px; bottom: 76px; min-width: 40px; height: 40px; border-radius: 20px; border: 1px solid var(--line); background: var(--bg); color: var(--accent); box-shadow: 0 2px 8px rgb(0 0 0 / .15); padding: 0 10px; z-index: 2; }
.jump[hidden] { display: none; }
.unread-sep { align-self: stretch; text-align: center; font-size: .75rem; color: var(--accent); border-top: 1px solid var(--accent); padding-top: 2px; margin: 6px 0; }

.composer {
  display: flex;
  gap: 8px;
  align-items: flex-end;
  padding: 8px 12px;
  padding-bottom: max(8px, env(safe-area-inset-bottom));
  border-top: 1px solid var(--line);
  background: var(--bg-soft);
}
.composer textarea {
  flex: 1;
  resize: none;
  max-height: 40vh;
  padding: 10px 14px;
  border-radius: 20px;
  border: 1px solid var(--line);
  background: var(--bg);
  color: var(--fg);
  font: inherit;
}
.composer textarea:focus { outline: none; border-color: var(--accent); }
.composer button {
  width: 44px; height: 44px; border-radius: 50%; border: 0;
  background: var(--accent); color: var(--accent-fg); font-size: 1.1rem;
}
.composer button:disabled { opacity: .5; cursor: default; }
.composer .tool { background: transparent; color: var(--fg); width: 40px; flex: none; }
.composer .tool:hover { background: var(--line); }
.composer .tool[data-recording] { background: var(--danger); color: #fff; }
.recording { flex: 1; display: flex; align-items: center; gap: 10px; min-height: 44px; padding: 0 8px; }
.recording[hidden] { display: none; }
.rec-dot { width: 10px; height: 10px; border-radius: 50%; background: var(--danger); animation: pulse 1s infinite; }
.composer.drop { outline: 2px dashed var(--accent); outline-offset: -4px; }

/* Attachments */
.att-img-link { display: block; margin: -2px -6px 4px; }
.att-img { display: block; max-width: min(320px, 100%); max-height: 360px; border-radius: 12px; background: var(--line); }
.att-audio { display: block; width: min(300px, 70vw); height: 40px; margin: 2px 0; }
.att-file { display: flex; align-items: center; gap: 8px; color: inherit; text-decoration: none; padding: 2px 0; }
.att-file .att-icon { font-size: 1.5rem; }
.att-file .att-name { font-weight: 600; text-decoration: underline; }
.att-label { font-size: .8rem; opacity: .8; }
.att-caption { margin-top: 6px; }
.progress { font-size: .8rem; opacity: .85; }

/* Voice call */
.voice { border-top: 1px solid var(--line); background: var(--bg-soft); padding: 10px 12px; max-height: 35vh; display: flex; flex-direction: column; gap: 8px; }
.voice[hidden] { display: none; }
.voice-head { display: flex; align-items: center; gap: 10px; }
.voice-head strong { flex: 1; font-weight: 600; }
.voice-dot { width: 12px; height: 12px; border-radius: 50%; background: var(--warn); }
.voice-dot[data-state="listening"] { background: var(--ok); animation: pulse 1.6s infinite; }
.voice-dot[data-state="speaking"] { background: var(--accent); animation: pulse .8s infinite; }
.voice-dot[data-state="error"] { background: var(--danger); }
@keyframes pulse { 50% { opacity: .35; } }
@media (prefers-reduced-motion: reduce) { .voice-dot { animation: none !important; } }
.hangup { border: 0; border-radius: 18px; padding: 6px 14px; background: var(--danger); color: #fff; }
.captions { list-style: none; margin: 0; padding: 0; overflow-y: auto; display: flex; flex-direction: column; gap: 4px; font-size: .9rem; }
.captions .user { color: var(--muted); }
.captions .user::before { content: "Toi : "; font-weight: 600; }
.captions .assistant::before { content: "Coach : "; font-weight: 600; }

.empty { position: absolute; inset: 56px 0 0; display: grid; place-items: center; color: var(--muted); margin: 0; background: var(--bg); }
.layout:not([data-thread]) .chat > :not(.empty) { visibility: hidden; }
.layout[data-thread] .empty { display: none; }

/* Dialog */
dialog { border: 1px solid var(--line); border-radius: var(--radius); background: var(--bg); color: var(--fg); width: min(440px, calc(100vw - 32px)); padding: 20px; }
dialog::backdrop { background: rgb(0 0 0 / .45); }
dialog h2 { margin: 0 0 8px; font-size: 1.1rem; }
dialog input { width: 100%; padding: 10px 12px; border-radius: 10px; border: 1px solid var(--line); background: var(--bg-soft); color: var(--fg); font: inherit; }
dialog .actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 16px; }
dialog button[type="submit"] { border: 0; border-radius: 10px; padding: 8px 16px; background: var(--accent); color: var(--accent-fg); }

/* Phone: one pane at a time */
@media (max-width: 720px) {
  .layout { grid-template-columns: 1fr; }
  .layout[data-view="list"] .chat { display: none; }
  .layout[data-view="chat"] .sidebar { display: none; }
  .back { display: inline-block; }
  .msg { max-width: 86%; }
}
