:root {
  --purple: #6d28d9;
  --purple-dark: #4b1d8f;
  --purple-light: #ede9fe;
  --bg: #f6f4fb;
  --card: #ffffff;
  --text: #1f1633;
  --muted: #6b6580;
  --line: #e4e0ef;
  --danger: #c0262d;
  --warn: #b45309;
  --radius: 14px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}
#app { height: 100%; }
button { font: inherit; cursor: pointer; }
input, textarea, select {
  font: inherit; font-size: 16px; color: var(--text);
  border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; background: #fff; width: 100%;
}
input:focus, textarea:focus, select:focus { outline: 2px solid var(--purple); outline-offset: -1px; border-color: transparent; }
h1 { font-size: 1.5rem; margin: 8px 0; }
h3, h4 { margin: 0 0 6px; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 0.85rem; }
.center { display: flex; align-items: center; justify-content: center; height: 100%; }
.stack { display: flex; flex-direction: column; gap: 12px; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.row.between { justify-content: space-between; flex-wrap: nowrap; }
.row input { flex: 1; min-width: 140px; }
.row select { width: auto; }

.btn {
  border: 1px solid var(--line); background: #fff; color: var(--text);
  padding: 10px 16px; border-radius: 10px; font-weight: 600;
}
.btn.primary { background: var(--purple); color: #fff; border-color: var(--purple); }
.btn.primary:hover { background: var(--purple-dark); }
.btn.danger { color: var(--danger); border-color: #f3c7c9; }
.btn.small { padding: 6px 10px; font-size: 0.85rem; }
.btn:disabled { opacity: 0.6; cursor: default; }
.link { background: none; border: none; color: var(--purple); padding: 4px; text-decoration: underline; }
.icon-btn { background: none; border: none; font-size: 1.25rem; padding: 6px 10px; border-radius: 8px; color: inherit; }
.icon-btn:hover { background: rgba(0,0,0,0.06); }

.error { color: var(--danger); min-height: 0; margin: 0; }
.error:empty { display: none; }
.notice { background: var(--purple-light); padding: 10px 12px; border-radius: 10px; margin: 0; }
.notice:empty { display: none; }
.notice input { margin-top: 8px; }

/* ---------- auth ---------- */
.auth { min-height: 100%; display: flex; flex-direction: column; align-items: center; justify-content: center; padding: calc(24px + var(--safe-top)) 16px calc(24px + var(--safe-bottom)); background: linear-gradient(160deg, #4b1d8f 0%, #6d28d9 55%, #f6f4fb 55%); }
.card { background: var(--card); border-radius: 18px; padding: 24px; width: 100%; max-width: 420px; box-shadow: 0 10px 40px rgba(40, 10, 90, 0.18); display: flex; flex-direction: column; gap: 12px; }
.brand { display: flex; align-items: center; gap: 10px; font-weight: 800; font-size: 1.1rem; }
.brand img { width: 34px; height: 34px; border-radius: 9px; }
.lock-note { font-size: 0.85rem; color: var(--muted); margin: 0; }
.field { display: flex; flex-direction: column; gap: 6px; font-size: 0.9rem; font-weight: 600; }
.tabs { display: flex; background: var(--bg); border-radius: 10px; padding: 4px; }
.tab { flex: 1; border: none; background: none; padding: 8px; border-radius: 8px; font-weight: 600; color: var(--muted); }
.tab.active { background: #fff; color: var(--text); box-shadow: 0 1px 3px rgba(0,0,0,0.1); }
.install-link { color: var(--purple-dark); margin-top: 14px; }

/* ---------- shared chat ---------- */
.topbar {
  display: flex; align-items: center; gap: 10px; padding: calc(10px + var(--safe-top)) 14px 10px;
  background: var(--purple-dark); color: #fff;
}
.topbar .brand img { width: 28px; height: 28px; }
.top-actions { margin-left: auto; display: flex; align-items: center; gap: 6px; }
.topbar .btn.small { background: rgba(255,255,255,0.12); color: #fff; border-color: rgba(255,255,255,0.25); }
.badge { background: rgba(255,255,255,0.15); padding: 3px 8px; border-radius: 999px; font-size: 0.75rem; }
.menu-wrap { position: relative; }
.menu { position: absolute; right: 0; top: 110%; background: #fff; color: var(--text); border-radius: 12px; box-shadow: 0 8px 30px rgba(0,0,0,0.18); min-width: 170px; overflow: hidden; z-index: 20; }
.menu button { display: block; width: 100%; text-align: left; background: none; border: none; padding: 12px 16px; }
.menu button:hover { background: var(--bg); }

.chat-head { display: flex; align-items: center; gap: 10px; padding: 10px 14px; background: #fff; border-bottom: 1px solid var(--line); }
.chat-head .who { min-width: 0; }
.chat-head .who b, .chat-head .who div { display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.head-actions { margin-left: auto; display: flex; gap: 6px; align-items: center; }
.head-actions select { width: auto; max-width: 150px; padding: 6px 8px; font-size: 0.85rem; }
.banner { background: #fff7e6; color: #7a4b00; padding: 10px 14px; font-size: 0.9rem; border-bottom: 1px solid #f5deb0; }

.thread { flex: 1; overflow-y: auto; padding: 16px 14px; display: flex; flex-direction: column; gap: 6px; -webkit-overflow-scrolling: touch; }
.msg { display: flex; flex-direction: column; max-width: 78%; }
.msg.mine { align-self: flex-end; align-items: flex-end; }
.msg.theirs { align-self: flex-start; align-items: flex-start; }
.msg-name { font-size: 0.75rem; color: var(--muted); margin: 6px 4px 2px; }
.bubble { padding: 9px 13px; border-radius: 18px; white-space: pre-wrap; word-wrap: break-word; overflow-wrap: anywhere; line-height: 1.35; }
.mine .bubble { background: var(--purple); color: #fff; border-bottom-right-radius: 6px; }
.theirs .bubble { background: #fff; border: 1px solid var(--line); border-bottom-left-radius: 6px; }
.bubble.locked { background: #eee !important; color: var(--muted) !important; font-style: italic; }
.msg-time { font-size: 0.7rem; color: var(--muted); margin: 2px 6px; }
.system { align-self: center; text-align: center; background: var(--purple-light); color: var(--purple-dark); font-size: 0.85rem; padding: 8px 12px; border-radius: 12px; margin: 8px 0; max-width: 90%; }
.seen { align-self: flex-end; font-size: 0.72rem; color: var(--muted); margin-right: 6px; }
.typing { font-size: 0.8rem; color: var(--muted); padding: 0 16px 6px; font-style: italic; }

.composer-wrap { background: #fff; border-top: 1px solid var(--line); padding: 8px 10px calc(8px + var(--safe-bottom)); }
.composer { display: flex; gap: 8px; align-items: flex-end; }
.composer textarea { resize: none; border-radius: 20px; padding: 10px 14px; max-height: 140px; }
.composer .send { border-radius: 20px; }
.quick { display: flex; gap: 6px; overflow-x: auto; padding-bottom: 6px; }
.quick:empty { display: none; }

.chip { border: 1px solid var(--line); background: #fff; border-radius: 999px; padding: 5px 12px; font-size: 0.82rem; white-space: nowrap; color: var(--text); }
.chip.active { background: var(--purple); border-color: var(--purple); color: #fff; }
.pill { font-size: 0.7rem; padding: 2px 8px; border-radius: 999px; }
.pill.warn { background: #fef3c7; color: var(--warn); }
.dot { width: 9px; height: 9px; border-radius: 50%; background: var(--purple); display: inline-block; }
.toggle { border: 1px solid rgba(255,255,255,0.35); background: transparent; color: #fff; border-radius: 999px; padding: 6px 12px; font-size: 0.85rem; font-weight: 600; }
.toggle.on { background: #16a34a; border-color: #16a34a; }

/* ---------- customer ---------- */
.customer { display: flex; flex-direction: column; height: 100%; max-width: 760px; margin: 0 auto; background: var(--bg); }
@media (min-width: 761px) { .customer { border-left: 1px solid var(--line); border-right: 1px solid var(--line); } }

/* ---------- staff ---------- */
.staff { display: flex; flex-direction: column; height: 100%; }
.staff-body { flex: 1; display: grid; grid-template-columns: 320px 1fr; min-height: 0; }
.sidebar { border-right: 1px solid var(--line); background: #fff; display: flex; flex-direction: column; min-height: 0; }
.stats { display: flex; gap: 12px; padding: 10px 14px; border-bottom: 1px solid var(--line); color: var(--muted); flex-wrap: wrap; }
.stats b { color: var(--text); }
.filters { display: flex; gap: 6px; padding: 10px 14px; overflow-x: auto; border-bottom: 1px solid var(--line); }
.conv-list { flex: 1; overflow-y: auto; }
.conv { display: block; width: 100%; text-align: left; border: none; border-bottom: 1px solid var(--line); background: #fff; padding: 12px 14px; color: var(--text); }
.conv:hover { background: var(--bg); }
.conv.active { background: var(--purple-light); }
.conv-top, .conv-bottom { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.conv-bottom { margin-top: 4px; }
.chat-pane { display: flex; flex-direction: column; min-height: 0; background: var(--bg); }
.empty { padding: 30px; text-align: center; }
.back { display: none; }

@media (max-width: 760px) {
  .staff-body { grid-template-columns: 1fr; }
  .staff-body.show-chat .sidebar { display: none; }
  .staff-body:not(.show-chat) .chat-pane { display: none; }
  .back { display: inline-block; }
  .topbar .badge { display: none; }
  .head-actions select { max-width: 110px; }
}

/* ---------- modal + toast ---------- */
.modal-wrap { position: fixed; inset: 0; background: rgba(20, 10, 40, 0.45); display: flex; align-items: center; justify-content: center; padding: 16px; z-index: 50; }
.modal { background: #fff; border-radius: 18px; padding: 18px; width: 100%; max-width: 520px; max-height: 85vh; overflow-y: auto; }
.modal-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px; }
.modal ol { margin: 4px 0 0; padding-left: 20px; line-height: 1.6; }
.team-list { display: flex; flex-direction: column; }
.team-row { display: flex; justify-content: space-between; align-items: center; gap: 10px; padding: 10px 0; border-bottom: 1px solid var(--line); }
.team-row.off { opacity: 0.5; }
.invite { border-top: 1px solid var(--line); padding-top: 12px; }
.toast { position: fixed; left: 50%; bottom: calc(24px + var(--safe-bottom)); transform: translateX(-50%); background: #1f1633; color: #fff; padding: 12px 18px; border-radius: 12px; z-index: 60; max-width: 90%; text-align: center; font-size: 0.9rem; }

.pw-wrap { position: relative; }
.pw-wrap input { padding-right: 64px; }
.show-pw { position: absolute; right: 6px; top: 50%; transform: translateY(-50%); background: none; border: none; color: var(--purple); font-weight: 600; font-size: 0.85rem; padding: 6px 8px; }
