:root {
  --bg: #f4f5f7;
  --panel: #ffffff;
  --text: #16181d;
  --muted: #6b7280;
  --border: #e5e7eb;
  --accent: #2563eb;
  --accent-text: #ffffff;
  --hover: #f0f2f5;
  --selected: #e8efff;
  --visitor-bg: #ffffff;
  --ai-bg: #eef2ff;
  --owner-bg: #2563eb;
  --owner-text: #ffffff;
  --hot: #dc2626;
  --hot-bg: #fef2f2;
  --warn-bg: #fff7e0;
  --ok: #059669;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0d0f13;
    --panel: #16191f;
    --text: #e8eaed;
    --muted: #9aa0a9;
    --border: #2a2e36;
    --accent: #4f8cff;
    --hover: #1e222a;
    --selected: #1c2740;
    --visitor-bg: #1f232b;
    --ai-bg: #1d2236;
    --owner-bg: #3b74f0;
    --hot: #f87171;
    --hot-bg: #2a1416;
    --warn-bg: #2a2412;
    --ok: #34d399;
  }
}
:root[data-theme="dark"] {
  --bg: #0d0f13; --panel: #16191f; --text: #e8eaed; --muted: #9aa0a9; --border: #2a2e36;
  --accent: #4f8cff; --hover: #1e222a; --selected: #1c2740; --visitor-bg: #1f232b; --ai-bg: #1d2236;
  --owner-bg: #3b74f0; --hot: #f87171; --hot-bg: #2a1416; --warn-bg: #2a2412; --ok: #34d399;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior: none;
}
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 12.5px; }
.strong { font-weight: 650; }
.error { color: var(--hot); min-height: 1.2em; margin: 0; }
.hint { display: block; color: var(--muted); font-size: 12.5px; font-weight: 400; margin: 2px 0 6px; }
code { font-size: 12px; }

.btn {
  border: 1px solid var(--border); background: var(--panel); color: var(--text);
  border-radius: 10px; padding: 10px 16px; font: inherit; font-weight: 600; cursor: pointer;
  text-decoration: none; display: inline-flex; align-items: center; justify-content: center;
}
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.danger { color: var(--hot); }
.btn.small { padding: 6px 12px; font-size: 13.5px; }
.btn:disabled { opacity: .5; }
.icon-btn {
  border: none; background: none; color: var(--text); font-size: 20px; cursor: pointer;
  width: 40px; height: 40px; border-radius: 10px; flex: none;
}
.icon-btn:hover { background: var(--hover); }

.screen { position: fixed; inset: 0; display: flex; }
.overlay { flex-direction: column; background: var(--bg); z-index: 20; overflow-y: auto; }
.bar {
  display: flex; align-items: center; gap: 8px; padding: 8px 10px;
  padding-top: max(8px, env(safe-area-inset-top));
  background: var(--panel); border-bottom: 1px solid var(--border); position: sticky; top: 0; z-index: 2;
}
.bar > .strong { flex: 1; text-align: center; }

/* Login */
#login { align-items: center; justify-content: center; padding: 16px; }
.card { background: var(--panel); border: 1px solid var(--border); border-radius: 14px; padding: 16px; }
.login-card { width: 100%; max-width: 360px; display: flex; flex-direction: column; gap: 12px; text-align: center; align-items: center; }
.login-card img { border-radius: 16px; }
.login-card h1 { margin: 0; font-size: 22px; }
.login-card p { margin: 0; }
.login-card input, .login-card button { width: 100%; }
input, textarea, select {
  font: inherit; color: var(--text); background: var(--panel);
  border: 1px solid var(--border); border-radius: 10px; padding: 10px 12px; width: 100%;
}
input:focus, textarea:focus, select:focus { outline: 2px solid var(--accent); outline-offset: -1px; }

/* Layout */
#list-pane {
  width: 360px; flex: none; display: flex; flex-direction: column;
  background: var(--panel); border-right: 1px solid var(--border);
}
#business-filter { flex: 1; min-width: 0; padding: 8px 10px; }
#conversation-list { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; }
#conversation-list li {
  padding: 12px 14px; border-bottom: 1px solid var(--border); cursor: pointer;
  display: grid; grid-template-columns: 1fr auto; gap: 2px 8px;
}
#conversation-list li:hover { background: var(--hover); }
#conversation-list li.active { background: var(--selected); }
#conversation-list li.hot { box-shadow: inset 3px 0 0 var(--hot); }
.c-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.c-time { color: var(--muted); font-size: 12px; }
.c-preview { color: var(--muted); font-size: 13.5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.c-tags { display: flex; gap: 4px; align-items: center; justify-content: flex-end; }
.tag { font-size: 11px; font-weight: 600; padding: 1px 7px; border-radius: 999px; border: 1px solid var(--border); color: var(--muted); white-space: nowrap; }
.tag.hot { background: var(--hot); border-color: var(--hot); color: #fff; }
.tag.unread { background: var(--accent); border-color: var(--accent); color: #fff; }
.tag.biz { border-color: transparent; color: #fff; }
.empty { color: var(--muted); text-align: center; padding: 40px 20px; margin: auto; }
.banner { background: var(--warn-bg); padding: 10px 14px; font-size: 13.5px; border-bottom: 1px solid var(--border); }
.banner button { margin-top: 6px; }

#chat-pane { flex: 1; display: flex; flex-direction: column; min-width: 0; }
#chat { display: flex; flex-direction: column; height: 100%; }
.chat-title { flex: 1; min-width: 0; }
.chat-title > div { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.back { display: none; font-size: 30px; }
.handoff { background: var(--hot-bg); color: var(--text); padding: 10px 14px; font-size: 13.5px; border-bottom: 1px solid var(--border); display: flex; gap: 10px; align-items: center; }
.handoff span { flex: 1; }
.mode-bar { display: flex; gap: 10px; align-items: center; padding: 8px 14px; font-size: 13px; background: var(--panel); border-bottom: 1px solid var(--border); }
.mode-bar span { flex: 1; }
.dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 6px; background: var(--ok); }
.dot.owner { background: var(--accent); }

#messages { flex: 1; overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 8px; }
.m { max-width: 78%; display: flex; flex-direction: column; }
.m .who { font-size: 11px; color: var(--muted); margin: 0 6px 2px; }
.m .b { padding: 9px 13px; border-radius: 16px; white-space: pre-wrap; word-wrap: break-word; }
.m.visitor { align-self: flex-start; }
.m.visitor .b { background: var(--visitor-bg); border: 1px solid var(--border); border-bottom-left-radius: 4px; }
.m.ai { align-self: flex-end; align-items: flex-end; }
.m.ai .b { background: var(--ai-bg); border: 1px solid var(--border); border-bottom-right-radius: 4px; }
.m.owner { align-self: flex-end; align-items: flex-end; }
.m.owner .b { background: var(--owner-bg); color: var(--owner-text); border-bottom-right-radius: 4px; }
.m.system { align-self: center; color: var(--muted); font-size: 12.5px; }
.typing { padding: 0 16px 8px; color: var(--muted); font-size: 12.5px; }

#composer {
  display: flex; gap: 8px; padding: 10px; background: var(--panel); border-top: 1px solid var(--border);
  padding-bottom: max(10px, env(safe-area-inset-bottom));
}
#composer textarea { resize: none; max-height: 140px; }

/* Forms */
.form-body { padding: 16px; display: flex; flex-direction: column; gap: 14px; max-width: 720px; width: 100%; margin: 0 auto; padding-bottom: max(24px, env(safe-area-inset-bottom)); }
.form-body label { display: flex; flex-direction: column; font-weight: 600; font-size: 14px; gap: 4px; }
.form-body label.inline { flex-direction: row; align-items: center; gap: 10px; }
.form-body label.inline input[type=color] { width: 48px; height: 34px; padding: 2px; }
.form-body label.inline input[type=checkbox] { width: 20px; height: 20px; flex: none; }
#email-form { display: flex; flex-direction: column; gap: 10px; }
#email-form label { display: flex; flex-direction: column; font-weight: 600; font-size: 13.5px; gap: 4px; }
.m .sent { font-size: 11px; color: var(--muted); margin: 2px 6px 0; }
.m.auto { align-self: flex-end; align-items: flex-end; }
.m.auto .b { background: var(--panel); border: 1px dashed var(--border); color: var(--muted); border-bottom-right-radius: 4px; }
.chat-sub a { color: inherit; }
.chat-title > .chat-sub { white-space: normal; overflow: visible; }
.plain-list { list-style: none; margin: 0 auto; padding: 16px; width: 100%; max-width: 720px; display: flex; flex-direction: column; gap: 10px; }
.plain-list li { background: var(--panel); border: 1px solid var(--border); border-radius: 12px; padding: 14px; cursor: pointer; display: flex; align-items: center; gap: 12px; }
.swatch { width: 14px; height: 14px; border-radius: 50%; flex: none; }
.embed { background: var(--panel); border: 1px solid var(--border); border-radius: 12px; padding: 14px; }
.embed pre { background: var(--bg); padding: 10px; border-radius: 8px; overflow-x: auto; font-size: 12px; white-space: pre-wrap; word-break: break-all; }
.row-btns { display: flex; gap: 8px; flex-wrap: wrap; }

.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); z-index: 50;
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 10px; font-size: 14px;
  max-width: calc(100vw - 32px);
}

/* Phone layout: one pane at a time */
@media (max-width: 760px) {
  #list-pane { width: 100%; border-right: none; }
  #chat-pane { display: none; }
  #main.chatting #list-pane { display: none; }
  #main.chatting #chat-pane { display: flex; }
  .back { display: block; }
  #chat-empty { display: none; }
  .m { max-width: 88%; }
}
