:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --border: #e2e5ea;
  --text: #1a1d21;
  --muted: #646b76;
  --accent: #2563eb;
  --accent-hover: #1d4fd8;
  --accent-soft: #e8efff;
  --on-accent: #ffffff;
  --in-bubble: #eceef2;
  --danger: #c4321c;
  --danger-soft: #fdebe8;
  --ok: #18794e;
  --warn-soft: #fff4dc;
  --warn-text: #7a4b00;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .05), 0 2px 6px rgba(16, 24, 40, .06);
  --top: 56px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121417;
    --surface: #1b1e23;
    --surface-2: #23272d;
    --border: #2f343c;
    --text: #e8eaed;
    --muted: #9aa1ab;
    --accent: #4b83f0;
    --accent-hover: #6595f3;
    --accent-soft: #1f2d4a;
    --in-bubble: #2a2f36;
    --danger: #f0735e;
    --danger-soft: #3a1f1a;
    --ok: #4cc38a;
    --warn-soft: #3a2f17;
    --warn-text: #f3c969;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--accent); }
h1, h2, h3 { margin: 0 0 12px; line-height: 1.2; }
h2 { font-size: 18px; }
h3 { font-size: 17px; }
p { margin: 0 0 10px; }
[hidden] { display: none !important; }

.muted { color: var(--muted); }
.small { font-size: 13px; }
.nowrap { white-space: nowrap; }
.spacer { flex: 1; }

/* ---------- controls ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 8px 14px; border-radius: 8px; border: 1px solid var(--border);
  background: var(--surface); cursor: pointer; text-decoration: none; color: var(--text);
  font-weight: 500; white-space: nowrap;
}
.btn:hover { background: var(--surface-2); }
.btn:disabled { opacity: .55; cursor: default; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--on-accent); }
.btn.primary:hover { background: var(--accent-hover); }
.btn.ghost { background: transparent; border-color: transparent; }
.btn.ghost:hover { background: var(--surface-2); }
.btn.danger { color: var(--danger); }
.btn.small { padding: 5px 10px; font-size: 13px; }

input:not([type=checkbox]):not([type=radio]):not([type=file]), select, textarea {
  width: 100%; padding: 8px 10px; border: 1px solid var(--border); border-radius: 8px;
  background: var(--surface); outline: none;
}
input:focus, select:focus, textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
textarea { resize: vertical; }
label { display: block; font-size: 13px; font-weight: 600; color: var(--muted); margin-bottom: 12px; }
label > input, label > select, label > textarea { margin-top: 4px; font-weight: 400; color: var(--text); }
label.check { display: flex; align-items: center; gap: 8px; font-weight: 500; color: var(--text); font-size: 14px; }
fieldset { border: 0; padding: 0; margin: 0 0 12px; }
legend { font-size: 13px; font-weight: 600; color: var(--muted); margin-bottom: 6px; padding: 0; }

.badge {
  display: inline-block; min-width: 20px; padding: 1px 6px; border-radius: 10px;
  background: var(--accent); color: var(--on-accent); font-size: 12px; font-weight: 700; text-align: center;
}
.chip { display: inline-block; padding: 1px 8px; border-radius: 10px; background: var(--surface-2); font-size: 12px; margin: 1px 2px 1px 0; }
.tag { display: inline-block; padding: 1px 8px; border-radius: 6px; font-size: 12px; font-weight: 600; }
.tag.danger { background: var(--danger-soft); color: var(--danger); }
.notice { padding: 10px 12px; border-radius: 8px; background: var(--accent-soft); margin: 10px 0; font-size: 14px; }
.notice.warn { background: var(--warn-soft); color: var(--warn-text); }

.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 18px; box-shadow: var(--shadow); margin-bottom: 16px; }

/* ---------- login ---------- */
.login-wrap { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.login { width: 100%; max-width: 360px; }
.login .logo { width: 44px; height: 44px; margin-bottom: 12px; }
.login .error { color: var(--danger); min-height: 1.4em; font-size: 14px; }
.login .btn { width: 100%; padding: 10px; }

/* ---------- shell ---------- */
.topbar {
  height: var(--top); display: flex; align-items: center; gap: 16px; padding: 0 16px;
  background: var(--surface); border-bottom: 1px solid var(--border); position: sticky; top: 0; z-index: 5;
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; white-space: nowrap; }
.brand img { width: 26px; height: 26px; }
.tabs { display: flex; gap: 2px; overflow-x: auto; flex: 1; scrollbar-width: none; }
.tabs a {
  padding: 8px 12px; border-radius: 8px; color: var(--muted); text-decoration: none; font-weight: 600; white-space: nowrap;
  display: inline-flex; align-items: center; gap: 6px;
}
.tabs a:hover { background: var(--surface-2); color: var(--text); }
.tabs a.active { background: var(--accent-soft); color: var(--accent); }
.who { display: flex; align-items: center; gap: 8px; }
.who select { width: auto; max-width: 240px; padding: 5px 8px; font-size: 13px; }
.line-pill { font-size: 13px; color: var(--muted); white-space: nowrap; }
.user { font-size: 13px; font-weight: 600; }

#view { height: calc(100dvh - var(--top)); overflow: auto; }
.page { max-width: 1100px; margin: 0 auto; padding: 20px 16px 40px; }
.page.narrow { max-width: 760px; }
.page-head { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; margin-bottom: 14px; }
.page-head h2 { margin: 0; flex: 1; }
.page-head .actions { display: flex; gap: 8px; flex-wrap: wrap; }

/* ---------- inbox ---------- */
.inbox { display: grid; grid-template-columns: 330px minmax(0, 1fr); height: 100%; }
.convos, .convo { min-width: 0; }
.convos { border-right: 1px solid var(--border); background: var(--surface); display: flex; flex-direction: column; min-height: 0; }
.convos-head { display: flex; gap: 8px; padding: 12px; border-bottom: 1px solid var(--border); }
.convos-filter { padding: 8px 12px; border-bottom: 1px solid var(--border); }
.convos-filter select { padding: 6px 8px; font-size: 13px; }
.convo-where { font-size: 12px; color: var(--muted); margin: 1px 0 2px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.inline-select { width: auto; min-width: 180px; padding: 6px 8px; font-size: 13px; }
.convo-list { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; }
.convo { padding: 11px 14px; border-bottom: 1px solid var(--border); cursor: pointer; }
.convo:hover { background: var(--surface-2); }
.convo.active { background: var(--accent-soft); }
.convo-top, .convo-bottom { display: flex; justify-content: space-between; gap: 8px; align-items: center; }
.convo-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.convo.unread .convo-name::before { content: ""; display: inline-block; width: 8px; height: 8px; border-radius: 50%; background: var(--accent); margin-right: 6px; vertical-align: middle; }
.convo-time { font-size: 12px; color: var(--muted); white-space: nowrap; }
.convo-preview { font-size: 13px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.convo-list .empty { padding: 24px 16px; color: var(--muted); text-align: center; font-size: 14px; }

.thread { display: flex; flex-direction: column; min-height: 0; min-width: 0; }
.thread-empty { margin: auto; color: var(--muted); text-align: center; padding: 24px; }
.thread-head { display: flex; align-items: center; gap: 10px; padding: 10px 16px; border-bottom: 1px solid var(--border); background: var(--surface); }
.thread-title { flex: 1; min-width: 0; }
.thread-name { font-weight: 700; }
.thread-sub { font-size: 13px; color: var(--muted); }
.back { display: none; font-size: 22px; padding: 2px 10px; }
.bubbles { flex: 1; overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 8px; }
.bubbles .empty { margin: auto; color: var(--muted); }
.bubble { max-width: min(72%, 560px); }
.bubble .text { padding: 8px 12px; border-radius: 16px; white-space: pre-wrap; overflow-wrap: anywhere; }
.bubble .meta { font-size: 11.5px; color: var(--muted); margin-top: 3px; padding: 0 6px; }
.bubble .err { color: var(--danger); }
.bubble.in { align-self: flex-start; }
.bubble.in .text { background: var(--in-bubble); border-bottom-left-radius: 4px; }
.bubble.out { align-self: flex-end; text-align: right; }
.bubble.out .text { background: var(--accent); color: var(--on-accent); border-bottom-right-radius: 4px; text-align: left; }
.bubble.out.failed .text { background: var(--danger-soft); color: var(--text); border: 1px solid var(--danger); }
.bubble.out.queued .text, .bubble.out.sending .text { opacity: .7; }
.optout-note { margin: 0 16px; }
.composer { border-top: 1px solid var(--border); padding: 10px 16px calc(10px + env(safe-area-inset-bottom)); background: var(--surface); }
.composer textarea { min-height: 44px; max-height: 200px; }
.composer-row { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-top: 8px; }
.counter { font-size: 12px; color: var(--muted); }

/* ---------- tables ---------- */
.filters { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 12px; }
.filters input { flex: 1; min-width: 200px; }
.filters select { width: auto; min-width: 160px; }
.table-wrap { overflow-x: auto; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 9px 12px; border-bottom: 1px solid var(--border); vertical-align: top; }
th { font-size: 12px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); background: var(--surface-2); }
tr:last-child td { border-bottom: 0; }
.cname { font-weight: 600; }
.row-actions { text-align: right; white-space: nowrap; }
.clamp { display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; max-width: 380px; }
table.mini th, table.mini td { padding: 5px 8px; font-size: 13px; }
.groups-bar { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; margin: 14px 0 0; }
.group-chip { display: inline-flex; align-items: center; gap: 4px; padding: 3px 4px 3px 10px; border-radius: 14px; background: var(--surface); border: 1px solid var(--border); font-size: 13px; }
.group-chip button { border: 0; background: none; cursor: pointer; color: var(--muted); padding: 0 6px; font-size: 15px; }
.group-chip button:hover { color: var(--danger); }
.more-note { padding: 10px 12px; color: var(--muted); font-size: 13px; }

/* ---------- bulk ---------- */
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 12px; }
.merge { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; margin: -4px 0 12px; font-size: 13px; color: var(--muted); }
.when { display: flex; gap: 14px; align-items: center; flex-wrap: wrap; }
.when label { margin: 0; }
.when input[type=datetime-local] { width: auto; }
.form-foot { display: flex; align-items: center; gap: 12px; justify-content: space-between; flex-wrap: wrap; margin-top: 14px; }
.progress { height: 6px; border-radius: 3px; background: var(--surface-2); overflow: hidden; margin-bottom: 4px; min-width: 120px; }
.progress span { display: block; height: 100%; background: var(--accent); }
.review { display: grid; grid-template-columns: auto 1fr; gap: 6px 14px; margin: 0 0 14px; }
.review dt { color: var(--muted); font-size: 14px; }
.review dd { margin: 0; font-weight: 600; }
.preview-label { font-size: 13px; font-weight: 600; color: var(--muted); margin-bottom: 6px; }
.bubble.preview { max-width: 100%; margin-bottom: 4px; }

/* ---------- settings ---------- */
.copy-row { display: flex; gap: 8px; align-items: center; margin: 4px 0 10px; }
.copy-row code { flex: 1; padding: 8px 10px; background: var(--surface-2); border-radius: 8px; font-size: 13px; overflow-wrap: anywhere; }
.row-form { display: flex; gap: 8px; flex-wrap: wrap; }
.row-form input { flex: 1; min-width: 180px; }
ul.plain { list-style: none; padding: 0; margin: 0 0 10px; }
ul.plain li { padding: 4px 0; }
ol.steps { padding-left: 20px; margin: 0 0 10px; }
ol.steps li { margin-bottom: 4px; }

/* ---------- modal & toast ---------- */
.modal-backdrop { position: fixed; inset: 0; background: rgba(10, 12, 16, .45); display: grid; place-items: center; padding: 16px; z-index: 20; }
.modal { background: var(--surface); border-radius: 14px; padding: 20px; width: 100%; max-width: 480px; max-height: calc(100dvh - 32px); overflow: auto; box-shadow: 0 10px 40px rgba(0, 0, 0, .25); }
.modal.wide { max-width: 760px; }
.modal-foot { display: flex; gap: 8px; justify-content: flex-end; align-items: center; margin-top: 16px; }
.group-picks .chip-check { display: inline-flex; margin: 0 6px 6px 0; font-weight: 500; color: var(--text); }
.chip-check input { position: absolute; opacity: 0; pointer-events: none; }
.chip-check span { padding: 4px 10px; border: 1px solid var(--border); border-radius: 14px; cursor: pointer; font-size: 13px; }
.chip-check input:checked + span { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); }
.chip-check input:focus-visible + span { box-shadow: 0 0 0 3px var(--accent-soft); }
.map-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: 0 12px; margin-top: 14px; }

#toasts { position: fixed; bottom: 16px; left: 50%; transform: translateX(-50%); display: flex; flex-direction: column; gap: 8px; z-index: 30; width: max-content; max-width: calc(100vw - 32px); }
.toast { background: #1f2329; color: #fff; padding: 10px 14px; border-radius: 8px; box-shadow: var(--shadow); font-size: 14px; }
.toast.error { background: var(--danger); }

/* ---------- AI drafting ---------- */
.ai-box { margin-bottom: 12px; }
.ai-panel { margin-top: 10px; padding: 14px; border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface-2); }
.ai-actions { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; margin-bottom: 4px; }
.ai-options { display: grid; gap: 8px; margin-top: 10px; }
.ai-options:empty { display: none; }
.composer .ai-options { margin: 0 0 8px; max-height: 40vh; overflow-y: auto; }
.ai-option { border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); padding: 10px 12px; }
.ai-option .text { white-space: pre-wrap; overflow-wrap: anywhere; }
.ai-option-foot { display: flex; justify-content: space-between; align-items: center; gap: 8px; margin-top: 6px; }
.composer-actions { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; justify-content: flex-end; }
.mini-select { width: auto; padding: 5px 8px; font-size: 13px; }

/* ---------- public legal pages ---------- */
.legal { padding-top: 32px; line-height: 1.6; }
.legal h1 { font-size: 26px; }
.legal h2 { margin-top: 22px; }
.legal hr { border: 0; border-top: 1px solid var(--border); margin: 28px 0 12px; }

/* ---------- phones ---------- */
@media (max-width: 760px) {
  .topbar { gap: 8px; padding: 0 10px; }
  .brand span, .user, .line-pill, #logout { display: none; }
  .tabs a { padding: 8px; }
  .who select { max-width: 130px; }
  .inbox { grid-template-columns: minmax(0, 1fr); }
  .composer-actions .btn { padding: 8px 10px; }
  .mini-select { max-width: 110px; }
  .inbox.has-thread .convos { display: none; }
  .inbox:not(.has-thread) .thread { display: none; }
  .back { display: inline-flex; }
  .bubble { max-width: 85%; }
  .grid2 { grid-template-columns: 1fr; }
  .contacts-table .col-groups { display: none; }
}
