:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-2: #f8f9fb;
  --border: #e3e6ea;
  --text: #17202b;
  --muted: #5d6b7a;
  --faint: #8a96a3;
  --brand: #0f4c5c;
  --brand-ink: #ffffff;
  --brand-soft: #e3eef0;
  --accent: #e36414;
  --urgent: #c0262d;
  --urgent-soft: #fdecec;
  --hot: #b45309;
  --hot-soft: #fff4e0;
  --ok: #1e7b4f;
  --ok-soft: #e5f4ec;
  --warn-soft: #fff7e0;
  --bubble-in: #ffffff;
  --bubble-out: #0f4c5c;
  --bubble-out-ink: #ffffff;
  --bubble-ai: #dfeef1;
  --bubble-ai-ink: #10313a;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .08);
  --radius: 10px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0e1318; --surface: #161d24; --surface-2: #1b232b; --border: #2a343e; --text: #e6ebf0;
    --muted: #a3b0bd; --faint: #7d8a97; --brand: #5fb3c4; --brand-ink: #0b1a1f; --brand-soft: #173038;
    --urgent: #f26b70; --urgent-soft: #3a1a1c; --hot: #f0a64b; --hot-soft: #3a2a12; --ok: #59c48f; --ok-soft: #12301f;
    --warn-soft: #33290f; --bubble-in: #1f2830; --bubble-out: #2f7d8f; --bubble-out-ink: #fff; --bubble-ai: #173038; --bubble-ai-ink: #d6ecf1;
    --shadow: 0 1px 2px rgba(0,0,0,.4); color-scheme: dark;
  }
}
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body { margin: 0; background: var(--bg); color: var(--text); font: 15px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif; }
a { color: var(--brand); text-decoration: none; }
a:hover { text-decoration: underline; }
h1, h2, h3 { line-height: 1.25; margin: 0 0 .5em; }
h1 { font-size: 1.45rem; letter-spacing: -.01em; }
h2 { font-size: 1.1rem; }
h3 { font-size: .95rem; }
p { margin: 0 0 .8em; }
small, .muted { color: var(--muted); }
.faint { color: var(--faint); }
code { background: var(--surface-2); border: 1px solid var(--border); border-radius: 5px; padding: 1px 5px; font-size: .9em; word-break: break-all; }

/* Layout */
.topbar { background: var(--brand); color: var(--brand-ink); position: sticky; top: 0; z-index: 10; }
.topbar .in { max-width: 1180px; margin: 0 auto; padding: 0 16px; display: flex; align-items: center; gap: 18px; height: 54px; }
.topbar .logo { font-weight: 700; color: var(--brand-ink); white-space: nowrap; display: flex; align-items: center; gap: 8px; }
.topbar .logo .dot { width: 10px; height: 10px; border-radius: 50%; background: var(--accent); }
.topbar nav { display: flex; gap: 2px; overflow-x: auto; flex: 1; scrollbar-width: none; }
.topbar nav a { color: var(--brand-ink); opacity: .82; padding: 7px 10px; border-radius: 7px; white-space: nowrap; font-size: .93rem; }
.topbar nav a:hover { opacity: 1; text-decoration: none; background: rgba(255,255,255,.1); }
.topbar nav a.on { opacity: 1; background: rgba(255,255,255,.16); }
.topbar form { margin: 0; }
.topbar .link-btn { color: var(--brand-ink); opacity: .82; }
.viewas { background: var(--accent); color: #fff; font-size: .9rem; }
.viewas .in { max-width: 1180px; margin: 0 auto; padding: 7px 16px; display: flex; gap: 12px; align-items: center; justify-content: space-between; }
.viewas button { background: #fff; color: var(--accent); border: 0; border-radius: 6px; padding: 4px 10px; font-weight: 600; cursor: pointer; }
main { max-width: 1180px; margin: 0 auto; padding: 22px 16px 60px; }
.narrow { max-width: 760px; }
.page-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; flex-wrap: wrap; margin-bottom: 16px; }
.page-head p { margin: 2px 0 0; color: var(--muted); }

/* Cards and grids */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); padding: 18px; margin-bottom: 16px; }
.card > h2:first-child, .card > h3:first-child { margin-top: 0; }
.card.flush { padding: 0; overflow: hidden; }
.grid { display: grid; gap: 16px; }
.grid.two { grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); }
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px; margin-bottom: 18px; }
.stat { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; box-shadow: var(--shadow); }
.stat .n { font-size: 1.6rem; font-weight: 700; letter-spacing: -.02em; }
.stat .l { color: var(--muted); font-size: .85rem; }

/* Forms */
label { display: block; font-weight: 600; font-size: .9rem; margin: 0 0 5px; }
.field { margin-bottom: 14px; }
.field .help { font-size: .84rem; color: var(--muted); margin-top: 4px; font-weight: 400; }
input[type=text], input[type=email], input[type=password], input[type=tel], input[type=url], input[type=time], input[type=number], input[type=search], select, textarea {
  width: 100%; font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--border); border-radius: 8px; padding: 9px 11px;
}
textarea { min-height: 90px; resize: vertical; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--brand); outline-offset: -1px; border-color: var(--brand); }
.check { display: flex; gap: 9px; align-items: flex-start; font-weight: 400; margin-bottom: 10px; }
.check input { margin-top: 4px; width: 16px; height: 16px; flex: none; }
.row { display: flex; gap: 10px; align-items: flex-end; flex-wrap: wrap; }
.row > .field { flex: 1 1 180px; }
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; font: inherit; font-weight: 600; border-radius: 8px; padding: 9px 15px; border: 1px solid transparent; cursor: pointer; background: var(--brand); color: var(--brand-ink); text-decoration: none !important; white-space: nowrap; }
.btn:hover { filter: brightness(1.08); }
.btn.secondary { background: var(--surface); color: var(--text); border-color: var(--border); }
.btn.danger { background: var(--surface); color: var(--urgent); border-color: var(--border); }
.btn.small { padding: 5px 10px; font-size: .86rem; }
.btn.block { width: 100%; }
.link-btn { background: none; border: 0; padding: 0; font: inherit; color: var(--brand); cursor: pointer; }
form.inline { display: inline; margin: 0; }

/* Alerts */
.flash { border-radius: 8px; padding: 10px 14px; margin-bottom: 14px; border: 1px solid var(--border); background: var(--surface); }
.flash.success { background: var(--ok-soft); border-color: transparent; }
.flash.error { background: var(--urgent-soft); border-color: transparent; }
.flash.warning { background: var(--warn-soft); border-color: transparent; }
.notice { background: var(--warn-soft); border-radius: 8px; padding: 12px 14px; margin-bottom: 16px; }

/* Badges */
.badge { display: inline-block; font-size: .74rem; font-weight: 700; padding: 2px 8px; border-radius: 999px; background: var(--surface-2); color: var(--muted); border: 1px solid var(--border); text-transform: uppercase; letter-spacing: .03em; white-space: nowrap; }
.badge.urgent { background: var(--urgent); color: #fff; border-color: transparent; }
.badge.hot { background: var(--hot-soft); color: var(--hot); border-color: transparent; }
.badge.spam { opacity: .7; }
.badge.ok, .badge.active { background: var(--ok-soft); color: var(--ok); border-color: transparent; }
.badge.trial { background: var(--brand-soft); color: var(--brand); border-color: transparent; }
.badge.past_due, .badge.paused, .badge.cancelled { background: var(--urgent-soft); color: var(--urgent); border-color: transparent; }

/* Tables */
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 10px 12px; border-bottom: 1px solid var(--border); vertical-align: top; }
th { font-size: .78rem; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); font-weight: 700; background: var(--surface-2); }
tr:last-child td { border-bottom: 0; }
.table-wrap { overflow-x: auto; }
td.num, th.num { text-align: right; white-space: nowrap; }

/* Inbox */
.tabs { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 12px; align-items: center; }
.tabs a { padding: 6px 12px; border-radius: 999px; background: var(--surface); border: 1px solid var(--border); color: var(--text); font-size: .9rem; }
.tabs a.on { background: var(--brand); color: var(--brand-ink); border-color: var(--brand); }
.tabs a:hover { text-decoration: none; }
.tabs form { margin-left: auto; flex: 0 1 240px; }
.conv-list a.conv { display: flex; gap: 12px; padding: 13px 16px; border-bottom: 1px solid var(--border); color: var(--text); align-items: flex-start; }
.conv-list a.conv:last-child { border-bottom: 0; }
.conv-list a.conv:hover { background: var(--surface-2); text-decoration: none; }
.conv-list a.conv.is-urgent { box-shadow: inset 4px 0 0 var(--urgent); }
.conv-list a.conv.is-hot { box-shadow: inset 4px 0 0 var(--hot); }
.avatar { width: 38px; height: 38px; border-radius: 50%; background: var(--brand-soft); color: var(--brand); display: flex; align-items: center; justify-content: center; font-weight: 700; flex: none; font-size: .9rem; }
.conv .main { flex: 1; min-width: 0; }
.conv .top { display: flex; gap: 8px; align-items: center; justify-content: space-between; }
.conv .name { font-weight: 650; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv.unread .name::after { content: ""; display: inline-block; width: 8px; height: 8px; border-radius: 50%; background: var(--accent); margin-left: 7px; vertical-align: middle; }
.conv .when { color: var(--faint); font-size: .82rem; white-space: nowrap; }
.conv .summary { color: var(--text); font-size: .92rem; margin-top: 2px; }
.conv .preview { color: var(--muted); font-size: .88rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.empty { text-align: center; padding: 40px 16px; color: var(--muted); }

/* Conversation */
.thread-wrap { display: grid; grid-template-columns: minmax(0, 1fr) 300px; gap: 16px; align-items: start; }
@media (max-width: 880px) { .thread-wrap { grid-template-columns: 1fr; } }
.thread { padding: 16px; display: flex; flex-direction: column; gap: 8px; min-height: 300px; max-height: 62vh; overflow-y: auto; background: var(--surface-2); }
.msg { max-width: 78%; display: flex; flex-direction: column; }
.msg .b { padding: 9px 12px; border-radius: 14px; white-space: pre-wrap; word-wrap: break-word; }
.msg .meta { font-size: .74rem; color: var(--faint); margin-top: 3px; padding: 0 4px; }
.msg.in { align-self: flex-start; }
.msg.in .b { background: var(--bubble-in); border: 1px solid var(--border); border-bottom-left-radius: 4px; }
.msg.out { align-self: flex-end; align-items: flex-end; }
.msg.out .b { background: var(--bubble-out); color: var(--bubble-out-ink); border-bottom-right-radius: 4px; }
.msg.out.ai .b, .msg.out.auto .b { background: var(--bubble-ai); color: var(--bubble-ai-ink); }
.msg.failed .b { outline: 2px solid var(--urgent); }
.msg.note { align-self: center; max-width: 92%; text-align: center; }
.msg.note .b { background: none; color: var(--muted); font-size: .84rem; padding: 2px 6px; }
.composer { display: flex; gap: 8px; padding: 12px; border-top: 1px solid var(--border); background: var(--surface); }
.composer textarea { min-height: 44px; height: 44px; flex: 1; }
.kv { display: grid; grid-template-columns: auto 1fr; gap: 6px 12px; font-size: .92rem; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; word-break: break-word; }
.actions { display: flex; flex-wrap: wrap; gap: 8px; }

/* Hours editor */
.hours { display: grid; grid-template-columns: 100px 1fr 1fr auto; gap: 8px 10px; align-items: center; }
.hours .closed { font-weight: 400; display: flex; gap: 6px; align-items: center; margin: 0; white-space: nowrap; }
@media (max-width: 520px) { .hours { grid-template-columns: 1fr 1fr; } .hours .day { grid-column: 1 / -1; font-weight: 600; margin-top: 6px; } }

/* Steps */
.steps { counter-reset: s; padding: 0; list-style: none; margin: 0; }
.steps li { counter-increment: s; position: relative; padding: 0 0 16px 40px; }
.steps li::before { content: counter(s); position: absolute; left: 0; top: 0; width: 28px; height: 28px; border-radius: 50%; background: var(--brand); color: var(--brand-ink); font-weight: 700; display: flex; align-items: center; justify-content: center; font-size: .88rem; }

/* Auth pages */
.auth { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 24px 16px; }
.auth .box { width: 100%; max-width: 420px; }
.auth .brand { text-align: center; margin-bottom: 18px; font-weight: 700; font-size: 1.15rem; color: var(--brand); display: flex; align-items: center; justify-content: center; gap: 8px; }
.auth .brand .dot { width: 12px; height: 12px; border-radius: 50%; background: var(--accent); }
.install .box { max-width: 640px; }
.checklist { list-style: none; padding: 0; margin: 0 0 8px; }
.checklist li { padding: 6px 0; border-bottom: 1px solid var(--border); font-size: .92rem; }
.checklist li:last-child { border-bottom: 0; }
.checklist .y { color: var(--ok); font-weight: 700; }
.checklist .n { color: var(--urgent); font-weight: 700; }
.copy { display: flex; gap: 6px; }
.copy input[readonly] { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: .85rem; }
hr { border: 0; border-top: 1px solid var(--border); margin: 18px 0; }
@media (max-width: 600px) { main { padding-top: 16px; } .card { padding: 15px; } h1 { font-size: 1.25rem; } .msg { max-width: 90%; } .hide-sm { display: none; } }
@media (max-width: 700px) {
  .topbar .in { flex-wrap: wrap; height: auto; padding-top: 8px; gap: 4px 12px; }
  .topbar .logo { flex: 1; }
  .topbar nav { order: 3; flex: 1 0 100%; margin: 0 -6px; padding-bottom: 6px; }
  .stats { grid-template-columns: 1fr 1fr; }
  .stat .n { font-size: 1.3rem; }
  .tabs form { flex: 1 0 100%; margin-left: 0; }
}
