:root {
  --bg: #f4f5fb;
  --surface: #ffffff;
  --text: #1a1d2e;
  --muted: #6b7085;
  --border: #e3e5ef;
  --accent: #4f46e5;
  --accent-text: #ffffff;
  --them: #ffffff;
  --badge-pending: #e5e7ef;   --badge-pending-text: #4b5065;
  --badge-progress: #dbe7ff;  --badge-progress-text: #1d4ed8;
  --badge-waiting: #fdecc8;   --badge-waiting-text: #92400e;
  --badge-answered: #d7f5e1;  --badge-answered-text: #166534;
  --badge-error: #fde0e0;     --badge-error-text: #b91c1c;
  --radius: 18px;
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1220;
    --surface: #171a2b;
    --text: #e8e9f1;
    --muted: #9097b0;
    --border: #262a40;
    --them: #1e2236;
    --badge-pending: #2a2e45;   --badge-pending-text: #b8bdd4;
    --badge-progress: #1e2d5a;  --badge-progress-text: #9cbcff;
    --badge-waiting: #4a3510;   --badge-waiting-text: #fcd38a;
    --badge-answered: #143d26;  --badge-answered-text: #86efac;
    --badge-error: #4a1818;     --badge-error-text: #fca5a5;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}
[hidden] { display: none !important; }
button { font: inherit; cursor: pointer; }

.screen { height: 100vh; height: 100dvh; display: flex; flex-direction: column; }

/* ---------- login ---------- */
#login { align-items: center; justify-content: center; padding: 16px; }
.card {
  width: 100%; max-width: 360px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 20px;
  padding: 28px 24px; display: flex; flex-direction: column; gap: 14px;
}
.card .logo { border-radius: 16px; align-self: center; }
.card h1 { margin: 0 0 4px; text-align: center; font-size: 22px; }
.card label { display: flex; flex-direction: column; gap: 6px; font-size: 14px; color: var(--muted); }
.card input {
  font-size: 16px; padding: 11px 12px; border-radius: 10px;
  border: 1px solid var(--border); background: var(--bg); color: var(--text);
}
.card input:focus { outline: 2px solid var(--accent); outline-offset: 1px; }
.primary-btn {
  border: 0; border-radius: 10px; padding: 12px; font-weight: 600;
  background: var(--accent); color: var(--accent-text);
}
.error { color: var(--badge-error-text); min-height: 1.2em; margin: 0; font-size: 14px; }

/* ---------- chat ---------- */
.topbar {
  display: flex; justify-content: space-between; align-items: center; gap: 12px;
  padding: calc(env(safe-area-inset-top) + 10px) 16px 10px;
  background: var(--surface); border-bottom: 1px solid var(--border);
}
.title { display: flex; align-items: center; gap: 8px; font-size: 17px; }
.conn { width: 8px; height: 8px; border-radius: 50%; background: var(--badge-error-text); }
.conn.on { background: var(--badge-answered-text); }
.who { display: flex; align-items: center; gap: 10px; font-size: 14px; color: var(--muted); }
.bell {
  display: grid; place-items: center; width: 34px; height: 34px; border-radius: 50%;
  border: 1px solid var(--border); background: var(--bg); color: var(--muted); padding: 0;
}
.bell[data-mode="on"] { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.bell[data-mode="blocked"] { opacity: .5; }
.link { background: none; border: 0; padding: 4px 2px; color: var(--accent); font-size: 14px; }

#messages {
  flex: 1; overflow-y: auto; overscroll-behavior: contain;
  padding: 16px; display: flex; flex-direction: column; gap: 16px;
}
#messages:empty::before {
  content: "Send an instruction to get started. Start with @machine to address a specific one.";
  margin: auto; max-width: 280px; text-align: center; color: var(--muted); font-size: 15px;
}

.node { display: flex; flex-direction: column; gap: 6px; }
.row { display: flex; }
.row.me { justify-content: flex-end; }
.bubble {
  max-width: min(82%, 680px); padding: 10px 14px; border-radius: var(--radius);
  white-space: pre-wrap; overflow-wrap: anywhere;
}
.bubble.me { background: var(--accent); color: var(--accent-text); border-bottom-right-radius: 6px; }
.bubble.them { background: var(--them); border: 1px solid var(--border); border-bottom-left-radius: 6px; }
.node[data-status="waiting"] .bubble.them { border-color: var(--badge-waiting-text); }
.node[data-status="closed"] { opacity: .6; }

.ref { font-size: 13px; opacity: .8; border-left: 2px solid currentColor; padding-left: 8px; margin-bottom: 6px; }
.meta { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; margin-top: 6px; font-size: 12px; opacity: .8; white-space: normal; }
.bubble.them .meta { opacity: 1; color: var(--muted); }

.badge { font-size: 11px; font-weight: 600; padding: 2px 8px; border-radius: 999px; background: var(--badge-pending); color: var(--badge-pending-text); }
[data-status="in_progress"] .badge { background: var(--badge-progress); color: var(--badge-progress-text); }
[data-status="waiting"] .badge { background: var(--badge-waiting); color: var(--badge-waiting-text); }
[data-status="answered"] .badge { background: var(--badge-answered); color: var(--badge-answered-text); }
[data-status="error"] .badge { background: var(--badge-error); color: var(--badge-error-text); }

.actions { display: flex; gap: 14px; margin-top: 4px; }

.typing { display: flex; gap: 4px; padding: 4px 0; }
.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; } }

/* ---------- composer ---------- */
.composer {
  background: var(--surface); border-top: 1px solid var(--border);
  padding: 8px 12px calc(env(safe-area-inset-bottom) + 8px);
}
.reply-bar {
  display: flex; justify-content: space-between; align-items: center; gap: 8px;
  font-size: 13px; color: var(--muted); padding: 2px 4px 8px;
}
.reply-bar span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#send-form { display: flex; align-items: flex-end; gap: 8px; }
#input {
  flex: 1; resize: none; max-height: 200px;
  font: inherit; font-size: 16px; line-height: 1.4;
  padding: 10px 14px; border-radius: 20px;
  border: 1px solid var(--border); background: var(--bg); color: var(--text);
}
#input:focus { outline: 2px solid var(--accent); outline-offset: 0; }
.icon-btn {
  flex: none; width: 44px; height: 44px; border-radius: 50%;
  display: grid; place-items: center;
  border: 1px solid var(--border); background: var(--bg); color: var(--text);
}
.icon-btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.icon-btn:disabled { opacity: .5; }
#mic.rec { background: var(--badge-error-text); border-color: var(--badge-error-text); color: #fff; animation: pulse 1.2s infinite; }
@keyframes pulse { 50% { transform: scale(1.08); } }
