:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-2: #eef0f4;
  --text: #171a21;
  --muted: #5d6675;
  --border: #d9dde5;
  --accent: #1f6feb;
  --accent-text: #ffffff;
  --accent-soft: #e3eeff;
  --ok: #156c3a;
  --ok-soft: #e2f4e8;
  --err: #a8261c;
  --err-soft: #fbe6e3;
  --bubble-in: #ffffff;
  --bubble-out: #dcecff;
  --bubble-auto: #ece8fb;
  --from-ai: #5b3fc4;
  --warn: #8a5300;
  --warn-soft: #fdf0d5;
  --radius: 10px;
  --topbar: 56px;
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #101216;
    --surface: #181b21;
    --surface-2: #21252d;
    --text: #e8eaee;
    --muted: #9aa3b2;
    --border: #2d323c;
    --accent: #4d94ff;
    --accent-text: #0b1220;
    --accent-soft: #1b2a44;
    --ok: #6fd695;
    --ok-soft: #15301f;
    --err: #ff8f85;
    --err-soft: #3a1b18;
    --bubble-in: #21252d;
    --bubble-out: #1d3557;
    --bubble-auto: #2c2748;
    --from-ai: #b3a2ff;
    --warn: #f2c26b;
    --warn-soft: #3a2c10;
  }
}

* { box-sizing: border-box; }

html, body { height: 100%; }
html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

a { color: var(--accent); }

h1 { font-size: 1.5rem; margin: 0 0 12px; }
h2 { font-size: 1.1rem; margin: 0 0 12px; }

.muted { color: var(--muted); }
.lead { max-width: 70ch; margin: 0 0 20px; }

/* Top bar */
.topbar {
  position: sticky;
  top: 0;
  z-index: 5;
  display: flex;
  align-items: center;
  gap: 20px;
  height: var(--topbar);
  padding: 0 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.brand {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 650;
  color: var(--text);
  text-decoration: none;
  white-space: nowrap;
}

.brand-mark {
  width: 22px;
  height: 22px;
  border-radius: 7px;
  background: var(--accent);
}

.topbar-menu { display: contents; }
.topbar-title, .menu-toggle { display: none; }

.nav { display: flex; gap: 4px; overflow-x: auto; scrollbar-width: none; }
.nav::-webkit-scrollbar { display: none; }

.nav a {
  padding: 6px 12px;
  border-radius: 8px;
  color: var(--muted);
  text-decoration: none;
  white-space: nowrap;
}

.nav a:hover { background: var(--surface-2); color: var(--text); }
.nav a.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }

.topbar-end { margin-left: auto; display: flex; align-items: center; gap: 14px; }

.who { display: flex; flex-direction: column; line-height: 1.15; text-align: right; text-decoration: none; color: var(--text); max-width: 180px; }
.who strong, .who span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.who strong { font-size: 13px; font-weight: 600; }
.who span { font-size: 12px; color: var(--muted); }

.lang { display: flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }

.lang button {
  border: 0;
  border-radius: 0;
  padding: 4px 9px;
  background: transparent;
  color: var(--muted);
  font-size: 12px;
  font-weight: 600;
}

.lang button.active { background: var(--accent-soft); color: var(--accent); }

main { max-width: 980px; margin: 0 auto; padding: 24px 20px 48px; }

/* Cards and forms */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  margin: 0 0 16px;
}

.card.narrow { max-width: 420px; margin: 48px auto; }
.card-off { opacity: 0.65; }

.stack { display: flex; flex-direction: column; gap: 14px; }
.narrow-form { max-width: 420px; }

label { display: flex; flex-direction: column; gap: 5px; font-weight: 550; }
label small, form small { font-weight: 400; color: var(--muted); }
label.short { max-width: 320px; }

label.check { flex-direction: row; align-items: center; gap: 8px; font-weight: 400; }
.checks { display: flex; flex-wrap: wrap; gap: 8px 24px; }

input, select, textarea {
  width: 100%;
  padding: 9px 11px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
}

input[type="checkbox"] { width: 17px; height: 17px; padding: 0; accent-color: var(--accent); }
textarea { resize: vertical; }

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

button {
  padding: 9px 16px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-weight: 550;
  cursor: pointer;
}

button:hover { background: var(--surface-2); }
button:disabled { opacity: 0.6; cursor: default; }

button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
button.primary:hover { filter: brightness(1.08); background: var(--accent); }

button.danger { color: var(--err); border-color: var(--err); }
button.danger:hover { background: var(--err-soft); }

.link-button { border: 0; padding: 4px 6px; background: transparent; color: var(--muted); font-weight: 400; }
.link-button:hover { background: transparent; color: var(--text); text-decoration: underline; }

.actions { display: flex; flex-wrap: wrap; gap: 12px 20px; align-items: flex-start; }
.actions form { display: flex; flex-direction: column; gap: 5px; max-width: 320px; }
.actions form button { align-self: flex-start; }

.flash { padding: 10px 14px; border-radius: 8px; margin: 0 0 16px; }
.flash-ok { background: var(--ok-soft); color: var(--ok); }
.flash-err { background: var(--err-soft); color: var(--err); }
.flash a { color: inherit; font-weight: 600; }

.facts { display: grid; grid-template-columns: max-content 1fr; gap: 8px 20px; margin: 0 0 20px; }
.facts dt { color: var(--muted); }
.facts dd { margin: 0; overflow-wrap: anywhere; }

.status { font-weight: 550; }
.status-ok { color: var(--ok); }
.status-err { color: var(--err); }

.steps { margin: 0 0 20px; padding-left: 22px; }
.steps li { margin-bottom: 6px; }

.rule-grid { display: grid; grid-template-columns: 1fr 1.3fr 1.3fr 90px; gap: 14px; align-items: start; }

/* Users and roles */
.user-head { display: flex; flex-wrap: wrap; align-items: baseline; gap: 4px 12px; margin: 0 0 14px; }
.user-last { margin-left: auto; font-size: 13px; }
.user-grid { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 14px; align-items: start; }
.user-grid label.check { align-self: end; padding-bottom: 9px; }

.tag { padding: 1px 8px; border-radius: 999px; background: var(--accent-soft); color: var(--accent); font-size: 12px; font-weight: 600; }
.tag-off { background: var(--surface-2); color: var(--muted); }

.more { margin-top: 14px; }
.more summary { cursor: pointer; color: var(--muted); }
.more form { margin-top: 12px; }

.perms { margin: 0; padding: 12px 14px; border: 1px solid var(--border); border-radius: 8px; display: grid; grid-template-columns: 1fr 1fr; gap: 12px 24px; }
.perms legend { padding: 0 6px; font-weight: 550; }
label.perm { align-items: flex-start; }
label.perm input { flex: none; margin-top: 2px; }
label.perm span { display: flex; flex-direction: column; gap: 2px; }
label.perm strong { font-weight: 550; }
.perm-list { margin: 0 0 12px; padding-left: 20px; }

.test-result { padding: 12px 14px; border-radius: 8px; background: var(--surface-2); white-space: pre-wrap; overflow-wrap: anywhere; }
.test-result.is-error { background: var(--err-soft); color: var(--err); }

/* Inbox */
.page-inbox main { max-width: none; padding: 0; display: flex; flex-direction: column; height: calc(100dvh - var(--topbar)); }
.page-inbox .flash { margin: 12px 16px 0; }

.inbox { flex: 1; min-height: 0; display: grid; grid-template-columns: 320px 1fr; }

.chat-side { min-width: 0; min-height: 0; display: flex; flex-direction: column; background: var(--surface); border-right: 1px solid var(--border); }
.chat-list { flex: 1; min-height: 0; overflow-y: auto; }

/* The lists of the inbox: waiting for the team, handled by the AI, all. */
.boxes { flex: none; display: flex; gap: 2px; padding: 8px 8px 0; border-bottom: 1px solid var(--border); overflow-x: auto; scrollbar-width: none; }
.boxes::-webkit-scrollbar { display: none; }
.box {
  flex: none;
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 7px 10px 8px;
  border: 0;
  border-bottom: 2px solid transparent;
  border-radius: 6px 6px 0 0;
  background: transparent;
  color: var(--muted);
  font-size: 13px;
  font-weight: 550;
  white-space: nowrap;
}
.box:hover { background: var(--surface-2); color: var(--text); }
.box.active { color: var(--accent); border-bottom-color: var(--accent); }
.box-count { min-width: 20px; padding: 0 6px; border-radius: 10px; background: var(--warn-soft); color: var(--warn); font-size: 12px; font-weight: 650; text-align: center; }
.box-count[hidden] { display: none; }

.chat-item {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  padding: 12px 16px;
  border: 0;
  border-bottom: 1px solid var(--border);
  border-radius: 0;
  background: transparent;
  text-align: left;
  font-weight: 400;
}

.chat-item:hover { background: var(--surface-2); }
.chat-item.active { background: var(--accent-soft); }

.chat-row { display: flex; justify-content: space-between; align-items: baseline; gap: 10px; }
.chat-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-time { flex: none; font-size: 12px; color: var(--muted); }
.chat-preview { flex: 1; min-width: 0; font-size: 13px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.badge {
  flex: none;
  min-width: 20px;
  padding: 0 6px;
  border-radius: 10px;
  background: var(--accent);
  color: var(--accent-text);
  font-size: 12px;
  font-weight: 650;
  text-align: center;
}

.chat-empty { padding: 24px 16px; color: var(--muted); }
.chat-reason { margin-top: 4px; font-size: 12px; color: var(--warn); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.human-bar { flex: none; padding: 7px 16px; background: var(--warn-soft); color: var(--warn); font-size: 13px; overflow-wrap: anywhere; }
.human-bar[hidden] { display: none; }

.thread { min-width: 0; min-height: 0; display: flex; flex-direction: column; }
.thread-empty { margin: auto; color: var(--muted); padding: 24px; text-align: center; }
.thread-open { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.thread-open[hidden] { display: none; }

.thread-head {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.thread-who { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.thread-who strong, .thread-who span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.thread-who span { font-size: 13px; }
.back { display: none; }

.pill { flex: none; padding: 5px 12px; border-radius: 999px; font-size: 13px; }
.pill.is-on { background: var(--ok-soft); border-color: transparent; color: var(--ok); }
.pill.is-paused { background: var(--surface-2); color: var(--muted); }
.pill.is-hand { background: transparent; color: var(--muted); }
.pill.is-done { background: var(--warn-soft); border-color: transparent; color: var(--warn); font-weight: 600; }
.pill[hidden] { display: none; }

.messages { flex: 1; min-height: 0; overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 8px; }

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

.msg { max-width: min(640px, 82%); display: flex; flex-direction: column; gap: 2px; }
.msg-in { align-self: flex-start; }
.msg-out { align-self: flex-end; align-items: flex-end; }

.bubble { padding: 8px 12px; border-radius: 14px; border: 1px solid var(--border); background: var(--bubble-in); white-space: pre-wrap; overflow-wrap: anywhere; }
.msg-out .bubble { background: var(--bubble-out); border-color: transparent; }
.msg-out.msg-auto .bubble { background: var(--bubble-auto); }

.bubble-media { padding: 6px; }
.media-link { display: block; }
.media-link img { display: block; max-width: min(320px, 100%); max-height: 360px; min-width: 120px; min-height: 80px; border-radius: 9px; background: var(--surface-2); object-fit: contain; }
.media-caption { padding: 6px 6px 2px; }
.media-failed { padding: 6px; color: var(--err); font-size: 13px; }
.bubble-media > .media-link:not(:has(img)) { padding: 4px 6px; }

.msg-meta { font-size: 12px; color: var(--muted); padding: 0 4px; display: flex; align-items: baseline; gap: 8px; }
.msg-meta .unsend { padding: 0; font-size: 12px; }
.msg.is-unsent .bubble { opacity: 0.55; text-decoration: line-through; }
.msg.is-unsent .media-link img { filter: grayscale(1); }
.unsent-note { color: var(--err); }

/* Who sent an outgoing message: a panel user, the AI agent or an auto-reply. */
.msg-from { font-size: 12px; font-weight: 650; padding: 0 4px; }
.msg-from-staff { color: var(--accent); }
.msg-from-ai { color: var(--from-ai); }
.msg-from-rule { color: var(--muted); }

.reply-readonly { padding: 12px 16px; background: var(--surface); border-top: 1px solid var(--border); color: var(--muted); font-size: 13px; }
.reply[hidden] { display: none; }

.note { align-self: center; max-width: 80%; padding: 6px 12px; border-radius: 8px; background: var(--err-soft); color: var(--err); font-size: 13px; text-align: center; overflow-wrap: anywhere; }

.reply { display: grid; grid-template-columns: 1fr auto; gap: 10px; align-items: end; padding: 12px 16px; background: var(--surface); border-top: 1px solid var(--border); }
.reply-error { grid-column: 1 / -1; color: var(--err); font-size: 13px; }

/* Yes/no question shown in the page (public/confirm.js). */
dialog.confirm {
  width: min(420px, calc(100vw - 32px));
  padding: 20px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.25);
}
dialog.confirm::backdrop { background: rgba(0, 0, 0, 0.45); }
dialog.confirm p { margin: 0 0 18px; line-height: 1.45; }
.confirm-actions { display: flex; justify-content: flex-end; gap: 8px; flex-wrap: wrap; }

/* CRM page: linked accounts and checks */
.crm-title { margin: 0; }
.crm-try { margin-top: 18px; padding-top: 18px; border-top: 1px solid var(--border); }
.crm-structure { margin: 0; max-height: 420px; overflow: auto; font: 12px/1.5 ui-monospace, SFMono-Regular, Consolas, monospace; }
.crm-search { display: flex; gap: 8px; margin: 0 0 14px; }
.crm-search input { flex: 1; min-width: 0; }
.crm-list { list-style: none; margin: 0; padding: 0; }
.crm-row { display: flex; align-items: center; gap: 12px 16px; padding: 12px 0; border-top: 1px solid var(--border); }
.crm-main { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 3px; }
.crm-who, .crm-account { display: flex; flex-wrap: wrap; align-items: baseline; gap: 2px 10px; overflow-wrap: anywhere; }
.crm-who .muted, .crm-dates { font-size: 13px; }
.crm-number { font-weight: 650; font-variant-numeric: tabular-nums; letter-spacing: 0.02em; }
.crm-actions { flex: none; display: flex; align-items: center; gap: 12px; font-size: 14px; }
.crm-actions button { padding: 6px 12px; }

/* Round picture of a person: profile photo, or initials on a colour. */
.avatar {
  position: relative;
  flex: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  overflow: hidden;
  color: #ffffff;
  font-size: 15px;
  font-weight: 650;
  line-height: 1;
  user-select: none;
}
.avatar img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; opacity: 0; }
.avatar.has-photo img { opacity: 1; }
.avatar.has-photo .avatar-initials { visibility: hidden; }
.avatar-c0 { background: #c2553f; }
.avatar-c1 { background: #b0690f; }
.avatar-c2 { background: #5f8a1e; }
.avatar-c3 { background: #1f8a70; }
.avatar-c4 { background: #2b7cc4; }
.avatar-c5 { background: #5b5fc7; }
.avatar-c6 { background: #9a4fb8; }
.avatar-c7 { background: #c0467e; }
.thread-avatar { flex: none; display: inline-flex; }
.thread-avatar:empty { display: none; }
.chat-body { flex: 1; min-width: 0; }

/* A photo shown large over the page (public/inbox.js, showPhoto). */
dialog.viewer {
  width: 100vw;
  height: 100dvh;
  max-width: none;
  max-height: none;
  margin: 0;
  padding: 12px;
  border: 0;
  background: rgba(0, 0, 0, 0.88);
}
dialog.viewer[open] { display: flex; align-items: center; justify-content: center; }
dialog.viewer::backdrop { background: rgba(0, 0, 0, 0.6); }
dialog.viewer img { max-width: 100%; max-height: 100%; object-fit: contain; border-radius: 6px; }
.viewer-close { position: absolute; top: 12px; right: 12px; min-height: 40px; border-color: transparent; background: rgba(255, 255, 255, 0.92); color: #171a21; }
.viewer-close:hover { background: #ffffff; }
.media-link img { cursor: zoom-in; }

/* Phones and narrow windows */
@media (max-width: 760px) {
  .topbar { gap: 10px; padding: 0 12px; }
  .brand { font-size: 0; gap: 0; }
  main { padding: 16px 12px 40px; }

  /* Top bar: the page name and a menu button. The links, the language and
     the account open in a panel under the bar (public/nav.js). */
  .topbar-title { display: block; flex: 1; min-width: 0; font-size: 16px; font-weight: 650; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .menu-toggle {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex: none;
    width: 44px;
    height: 44px;
    margin-right: -8px;
    padding: 0;
    border: 0;
    background: transparent;
    color: var(--text);
  }
  .topbar.menu-open .menu-toggle { background: var(--surface-2); }
  .has-menu .topbar-menu {
    display: none;
    position: absolute;
    top: 100%;
    left: 0;
    right: 0;
    max-height: calc(100dvh - var(--topbar));
    overflow-y: auto;
    flex-direction: column;
    gap: 12px;
    padding: 8px 12px 16px;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    box-shadow: 0 16px 28px rgba(0, 0, 0, 0.22);
  }
  .has-menu.menu-open .topbar-menu { display: flex; }
  .has-menu .nav { flex-direction: column; gap: 2px; overflow: visible; }
  .has-menu .nav a { padding: 12px; font-size: 16px; }
  .has-menu .topbar-end { margin-left: 0; padding: 12px 0 0; border-top: 1px solid var(--border); flex-wrap: wrap; justify-content: space-between; gap: 12px; }
  .has-menu .who { order: -1; flex: 1 1 100%; max-width: none; padding: 0 12px; text-align: left; line-height: 1.3; }
  .has-menu .who strong { font-size: 15px; }
  .has-menu .who span { font-size: 13px; }
  .has-menu .lang { margin-left: 12px; }
  .has-menu .lang button { min-height: 40px; padding: 8px 14px; font-size: 13px; }
  .has-menu .topbar-end .link-button { min-height: 40px; padding: 8px 14px; border: 1px solid var(--border); color: var(--text); }
  .lang button { min-height: 34px; }

  /* Fields below 16px make iPhones zoom in when they are tapped. */
  input, select, textarea { font-size: 16px; }
  input[type="checkbox"] { width: 20px; height: 20px; }
  button { min-height: 44px; }
  .card { padding: 16px; }
  .card.narrow { margin: 24px auto; }
  .actions form { max-width: none; }
  .confirm-actions button { flex: 1; }

  .rule-grid { grid-template-columns: 1fr; }
  .user-grid { grid-template-columns: 1fr; }
  .user-grid label.check { padding-bottom: 0; }
  .user-last { margin-left: 0; }
  .perms { grid-template-columns: 1fr; }
  .facts { grid-template-columns: 1fr; gap: 2px; }
  .facts dd { margin-bottom: 10px; }

  .crm-row { flex-direction: column; align-items: stretch; }
  .crm-actions { justify-content: space-between; }
  .crm-search { flex-wrap: wrap; }

  /* Inbox: the list or one conversation, never both. */
  .inbox { grid-template-columns: 1fr; }
  .inbox .thread { display: none; }
  .inbox.has-open .chat-side { display: none; }
  .inbox.has-open .thread { display: flex; }
  .chat-side { border-right: 0; }
  .boxes { padding: 4px 4px 0; }
  .box { flex: 1 1 auto; justify-content: center; font-size: 14px; }
  .chat-item { padding: 14px 16px; }
  .thread-head { flex-wrap: wrap; gap: 6px 8px; padding: 6px 12px 8px; }
  .back { display: inline-block; padding: 4px 8px 4px 0; color: var(--accent); }
  .pill { min-height: 36px; }
  .messages { padding: 12px; }
  .msg { max-width: 88%; }
  .note { max-width: 92%; }
  .msg-meta .unsend { min-height: 0; margin: -8px -6px; padding: 8px 6px; font-size: 13px; }
  .reply { gap: 8px; padding: 8px 12px calc(8px + env(safe-area-inset-bottom, 0px)); }
}

/* Narrow phones: the name gets its own row, the two buttons the next. */
@media (max-width: 480px) {
  .thread-who { flex: 1 1 calc(100% - 150px); }
}
